comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: A very simple FORMAT question
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: A very simple FORMAT question [message #39079] Sat, 24 April 2004 04:26
Christopher Lee is currently offline  Christopher Lee
Messages: 4
Registered: February 2001
Junior Member
> Any explanation for these extra brackets? (for IDL, ifc, ...)
>
> Cheers,
>
> A.

The answer to this lies with our old friend Google....

http://www.hef.kun.nl/~pfk/teaching/fortran/ftn-AB.html

"The rule for rescanning formats is complicated by the presence of
brackets surrounding groups of format codes. The full rule for rescan is
as follows. If there are no nested brackets, rescan returns to the
beginning of the format. Otherwise, the rescan returns to the left bracket
which matches the right bracket nearest to the closing bracket of the
format. If this left bracket is preceded by a repeat count, this count
takes effect during the rescan."

so...

print, findgen(6),format='(i," A",i," B",i," C")'
0 A 1 B 2 C
3 A 4 B 5 C

print, findgen(6),format='(i," A",(i," B"),i," C")'
0 A 1 B 2 C
3 B 4 C
5 B

If you want the read to include the terms before the first _real_
brackets in a repeat, you need an extra set of opening brackets.

Chris.
Re: A very simple FORMAT question [message #39090 is a reply to message #39079] Fri, 23 April 2004 11:38 Go to previous message
Andry William (Please is currently offline  Andry William (Please
Messages: 11
Registered: March 2004
Junior Member
> The line counting line confused me, I use a function called
> FILE_LINES (I'm not sure if it's local or not, it appears to
> be undocumented if it's a library function).

FILE_LINES is documented in the IDL help (at least with the V 6.0).

A.
Re: A very simple FORMAT question [message #39091 is a reply to message #39090] Fri, 23 April 2004 11:35 Go to previous message
Andry William (Please is currently offline  Andry William (Please
Messages: 11
Registered: March 2004
Junior Member
>>> ------------------------ test.datos ----------------------------
>>> |020922 10:00:00 | 339| 348| 63| 356| 400| 88| |020922
>>> 10:00:00 | 339| 348| 63| 356| 400| 88|
>>> -----------------
>>> SPAWN, "wc -l "+ flnm+ " | awk '{print $1}'", lineas
>>> ...
>>> READF, 1, data, format= '(1x,i6,3(1x,i2),4x,7(1x,i6))'
>
> Aha, L'inspiration, or something.
Bravo,
>
> READF, 1, data, format= '(1x,i6,3(1x,i2),4x,6(1x,i6))'
>
> does NOT work
>
> READF, 1, data, format= '((1x,i6,3(1x,i2),4x,6(1x,i6)))'
>
> does work fine (note the extra brackets).

This is just the command I am missing. I have never thought I
need to write 2 brackets in order to be able to read the whole
data with the specified format. It works (as it should be)....!!! :)
I have been looking for this solution for very long time.

Thank you very much for all of the suggestion.

Any explanation for these extra brackets? (for IDL, ifc, ...)

Cheers,

A.
Re: A very simple FORMAT question [message #39092 is a reply to message #39091] Fri, 23 April 2004 04:18 Go to previous message
Chris Lee is currently offline  Chris Lee
Messages: 101
Registered: August 2003
Senior Member
> In article <4086EE70.9070707@ya.com.spam>, "Unknown" <andry@ya.com.spam>
> wrote:
>
>> Hi all,
>> ------------------------ test.datos ----------------------------
>> |020922 10:00:00 | 339| 348| 63| 356| 400| 88| |020922
>> 10:00:00 | 339| 348| 63| 356| 400| 88|
>> -----------------
>> SPAWN, "wc -l "+ flnm+ " | awk '{print $1}'", lineas
>> ...
>> READF, 1, data, format= '(1x,i6,3(1x,i2),4x,7(1x,i6))'
> Hi,
>

Aha, L'inspiration, or something.

READF, 1, data, format= '(1x,i6,3(1x,i2),4x,6(1x,i6))'

does NOT work

READF, 1, data, format= '((1x,i6,3(1x,i2),4x,6(1x,i6)))'

does work fine (note the extra brackets).

It's not an IDL bug either, Fortran (at least the Intel and f77 compiler)
both have the same behaviour. I do think it's bizzare that the everything
after the repeat statement (i.e 3(1x,i2)) is repeated in the READ, but
not the bit before it.

Chris.
Re: A very simple FORMAT question [message #39111 is a reply to message #39092] Thu, 22 April 2004 02:06 Go to previous message
Chris Lee is currently offline  Chris Lee
Messages: 101
Registered: August 2003
Senior Member
In article <4086EE70.9070707@ya.com.spam>, "Unknown" <andry@ya.com.spam>
wrote:

> Hi all,
> ------------------------ test.datos ----------------------------
> |020922 10:00:00 | 339| 348| 63| 356| 400| 88|
> |020922 10:00:00 | 339| 348| 63| 356| 400| 88|
> -----------------
> SPAWN, "wc -l "+ flnm+ " | awk '{print $1}'", lineas
> ...
> READF, 1, data, format= '(1x,i6,3(1x,i2),4x,7(1x,i6))'

Hi,

The line counting line confused me, I use a function called FILE_LINES
(I'm not sure if it's local or not, it appears to be undocumented if it's
a library function).

As for the formatting, it works fine for single line reads. So you can
either read one line at a time or use STRSPLIT on each line (which can
then be read in all at once)
e.g.
flnm= 'test.datos'
n_data=10
n_lines=file_lines(flnm)
d=strARR(n_lines)
data=intarr(n_data,n_lines)

OPENR, 1, flnm
readf,1,d
CLOSE,1

for i=0, n_lines-1 do $
data[*,i]= fix(strsplit(d[i],'[ :|]',/regex,/ex))

I don't know why the FORMAT is broken, only that READF seems to lose its
position in the file and applies the format to the wrong string.

The 'missing second zero' is actually the first zero (the minutes in the time
string), which is being used as the the third dummy argument in the
format string. After that, everything blows up.

Chris.
Re: A very simple FORMAT question [message #39112 is a reply to message #39111] Thu, 22 April 2004 01:20 Go to previous message
wmconnolley is currently offline  wmconnolley
Messages: 106
Registered: November 2000
Senior Member
"Andry William (Please remove \".spam\")" <andry> wrote:
> wmc@bas.ac.uk wrote:
>>> ------------------------ test.datos ----------------------------
>>> |020922 10:00:00 | 339| 348| 63| 356| 400| 88|
>>> ------------------------ End data ------------------------------
>>
>>> data= INTARR(11, lineas)
>>> READF, 1, data, format= '(1x,i6,3(1x,i2),4x,7(1x,i6))'
>>
>> I count 10 not 11?
>> should be 6 not 7(ix,i6)?

> You are right. It was just a mistyping on the e-mail editor. The
> original code has the above variables.
>>
>> BTW, you could start of with
>>
>> spawn,"perl -i -pe 's/|:/ /g' test.datos"
>>
>> which would change your : and |'s into spaces, whereupon your
>> reading could be simplified.

> Does it make some difference if I have "|" or ":" instead of
> space?

Not as you've written it. But if you read in as

data= INTARR(11, lineas)
readf,1,data

and skip the format (as you can if you've removed the |'s etc in favour
of spaces) you have a simpler life.

> On the other side, the data I have come from some other database
> software and generated by some other people with a lot more data.
> I am also not supposed to alter the data since after my checking
> I should pass it to another program (with the original format),
> pretty picky (I agree).

-W.

--
William M Connolley | wmc@bas.ac.uk | http://www.antarctica.ac.uk/met/wmc/
Climate Modeller, British Antarctic Survey | Disclaimer: I speak for myself
I'm a .signature virus! copy me into your .signature file & help me spread!
Re: A very simple FORMAT question [message #39116 is a reply to message #39112] Wed, 21 April 2004 17:29 Go to previous message
Andry William (Please is currently offline  Andry William (Please
Messages: 11
Registered: March 2004
Junior Member
Here is the simple data:

------------------------ test.datos ----------------------------
|020922 10:00:00 | 339| 348| 63| 356| 400| 88|
|020922 10:00:00 | 339| 348| 63| 356| 400| 88|
|020922 11:00:00 | 345| 349| 111| 422| 482| 140|
|020922 12:00:00 | 339| 340| 113| 431| 533| 137|
|020922 13:00:00 | 335| 333| 99| 410| 492| 127|
------------------------ End data ------------------------------

Here is the simple program to read it.

------------------------- Simple IDL program -------------------
flnm= 'test.datos'
SPAWN, "wc -l "+ flnm+ " | awk '{print $1}'", lineas
; This can be done in IDL without any problem

lineas= LONG(lineas[0])
data= INTARR(10, lineas)

OPENR, 1, flnm
READF, 1, data, format= '(1x,i6,3(1x,i2),4x,7(1x,i6))'
CLOSE,1
END


> It looks to me that you have 10 things to read on each line,
> and you are trying to read 11 in your format statement. Could
> that be the problem?

That was just a mistyping. I just reproduced part of my data
in order to have it fill the e-mail editor. It should read 10
instead of 11. Here the result when reading the file:

IDL> print,data
20922 10 0 0 339 348 63 356 400 88
20922 11 0 345 349 111 20922 12 0 339
340 113 20922 13 0 335 333 99 0 0
0 0 0 0 0 0 0 0 0 0

Where does go the second "0" of the second line? The problem
keeps occuring even for more columns of data.

Andry
Re: A very simple FORMAT question [message #39117 is a reply to message #39116] Wed, 21 April 2004 17:35 Go to previous message
Andry William (Please is currently offline  Andry William (Please
Messages: 11
Registered: March 2004
Junior Member
wmc@bas.ac.uk wrote:
>> ------------------------ test.datos ----------------------------
>> |020922 10:00:00 | 339| 348| 63| 356| 400| 88|
>> ------------------------ End data ------------------------------
>
>> data= INTARR(11, lineas)
>>READF, 1, data, format= '(1x,i6,3(1x,i2),4x,7(1x,i6))'
>
> I count 10 not 11?
> should be 6 not 7(ix,i6)?

You are right. It was just a mistyping on the e-mail editor. The
original code has the above variables.
>
> BTW, you could start of with
>
> spawn,"perl -i -pe 's/|:/ /g' test.datos"
>
> which would change your : and |'s into spaces, whereupon your
> reading could be simplified.

Does it make some difference if I have "|" or ":" instead of
space?

On the other side, the data I have come from some other database
software and generated by some other people with a lot more data.
I am also not supposed to alter the data since after my checking
I should pass it to another program (with the original format),
pretty picky (I agree).

Thanks again for any input.

Andry
Re: A very simple FORMAT question [message #39119 is a reply to message #39116] Wed, 21 April 2004 12:38 Go to previous message
wmconnolley is currently offline  wmconnolley
Messages: 106
Registered: November 2000
Senior Member
"Andry William (Please remove \".spam\")" <andry> wrote:
> ------------------------ test.datos ----------------------------
> |020922 10:00:00 | 339| 348| 63| 356| 400| 88|
> ------------------------ End data ------------------------------

> lineas= LONG(lineas[0])
> data= INTARR(11, lineas)

I count 10 not 11?

> OPENR, 1, flnm
> READF, 1, data, format= '(1x,i6,3(1x,i2),4x,7(1x,i6))'

should be 6 not 7(ix,i6)?

BTW, you could start of with

spawn,"perl -i -pe 's/|:/ /g' test.datos"

which would change your : and |'s into spaces, whereupon your
reading could be simplified.

-W.

--
William M Connolley | wmc@bas.ac.uk | http://www.antarctica.ac.uk/met/wmc/
Climate Modeller, British Antarctic Survey | Disclaimer: I speak for myself
I'm a .signature virus! copy me into your .signature file & help me spread!
Re: A very simple FORMAT question [message #39121 is a reply to message #39119] Wed, 21 April 2004 12:31 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
"Andry William (Please remove \".spam\")" <andry@ya.com.spam> writes:

> Sorry for this very simple question but I would appreciate if
> someone can help in the use of FORMAT in READF command.
>
> Here is the simple data:
>
> ------------------------ test.datos ----------------------------
> |020922 10:00:00 | 339| 348| 63| 356| 400| 88|
> |020922 10:00:00 | 339| 348| 63| 356| 400| 88|
> |020922 11:00:00 | 345| 349| 111| 422| 482| 140|
> |020922 12:00:00 | 339| 340| 113| 431| 533| 137|
> |020922 13:00:00 | 335| 333| 99| 410| 492| 127|
> ------------------------ End data ------------------------------
>
> Here is the simple program to read it.
>
> ------------------------- Simple IDL program -------------------
> flnm= 'test.datos'
> SPAWN, "wc -l "+ flnm+ " | awk '{print $1}'", lineas
> ; This can be done in IDL without any problem
>
> lineas= LONG(lineas[0])
> data= INTARR(11, lineas)
>
> OPENR, 1, flnm
> READF, 1, data, format= '(1x,i6,3(1x,i2),4x,7(1x,i6))'
> CLOSE,1
> END
> --------------------- End IDL program -------------------------
>
> For some reason (I don't understand why) the READF command
> reaches the end of the file and can not close it.

It looks to me that you have 10 things to read on each line,
and you are trying to read 11 in your format statement. Could
that be the problem?

Cheers,

David

--
David Fanning, Ph.D.
Fanning Software Consulting
Coyote's Guide to IDL Programming: http://www.dfanning.com/
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: simplify a polyline?
Next Topic: Newbie needs help getting mpi_plot going

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 13:46:01 PDT 2025

Total time taken to generate the page: 0.01154 seconds