Re: idl readf error [message #65121] |
Sat, 14 February 2009 06:35  |
Vince Hradil
Messages: 574 Registered: December 1999
|
Senior Member |
|
|
On Feb 13, 10:17 pm, David Fanning <n...@dfanning.com> wrote:
> rollo.tom...@hotmail.com writes:
>> Here's what I have:
>
>> IDL> openr, 1, 'file1.out'
>> IDL> h = strarr(5)
>> IDL> readf, 1, h
>> IDL> close, 1
>> IDL> for j=0,4 do print, h[j]
>> T U 17.81 201 145225.9
>> T U 37.67 191 98975.8
>> T U 21.77 201 129774.9
>> T I 0.65 1 69.5
>> T I 0.36 1 69.3
>
>> By doing this I noticed that the values in the last column get pretty
>> large, so I changed the format to
>> format='(2A2, G7.2, G7.2, G10.2)'
>
>> And I still get the same error.
>
>> The largest values in the numerical columns are:
>> column 3: 272.23
>> column 4: 1000
>> column 5: 145225.9
>
> OK, well, as I expected, that format does not correspond
> to what is in the file. And I am still mystified as to
> why it worked (if it did) initially. The format looks to
> me to be something more like this:
>
> format='(2(A1,7x),F5.2,3x,I3,5x,G8.2)'
>
> Try that and see if you have better luck. :-)
>
> Here is the code I used to read the first line:
>
> IDL> openr, 1, file
> IDL> a="" &b = "" & c=0.0 & d=8 & e=0.0
> IDL> readf, 1, a, b, c, d, e, format='(2(A1,7x),F5.2,3x,I3,5x,G8.2)'
> IDL> print, a, b, c, d, e
> TU 37.6700 191 98975.8
>
> Cheers,
>
> David
> --
> David Fanning, Ph.D.
> Fanning Software Consulting, Inc.
> Coyote's Guide to IDL Programming:http://www.dfanning.com/
> Sepore ma de ni thui. ("Perhaps thou speakest truth.")
This thread makes me think... Can anyone compare and contrast this
approach with the approach I usually use - that is:
read each line into a string buf: buf = '' $ readf, lun, buf
then use strsplit(buf,/extract) to chop up the buf
then use fix, float, etc. to get the values.
I don't usually use format statements for reading, just writing.
Thanks,
Vince
|
|
|
Re: idl readf error [message #65122 is a reply to message #65121] |
Fri, 13 February 2009 20:17   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
rollo.tomasi@hotmail.com writes:
> Here's what I have:
>
> IDL> openr, 1, 'file1.out'
> IDL> h = strarr(5)
> IDL> readf, 1, h
> IDL> close, 1
> IDL> for j=0,4 do print, h[j]
> T U 17.81 201 145225.9
> T U 37.67 191 98975.8
> T U 21.77 201 129774.9
> T I 0.65 1 69.5
> T I 0.36 1 69.3
>
> By doing this I noticed that the values in the last column get pretty
> large, so I changed the format to
> format='(2A2, G7.2, G7.2, G10.2)'
>
> And I still get the same error.
>
> The largest values in the numerical columns are:
> column 3: 272.23
> column 4: 1000
> column 5: 145225.9
OK, well, as I expected, that format does not correspond
to what is in the file. And I am still mystified as to
why it worked (if it did) initially. The format looks to
me to be something more like this:
format='(2(A1,7x),F5.2,3x,I3,5x,G8.2)'
Try that and see if you have better luck. :-)
Here is the code I used to read the first line:
IDL> openr, 1, file
IDL> a="" &b = "" & c=0.0 & d=8 & e=0.0
IDL> readf, 1, a, b, c, d, e, format='(2(A1,7x),F5.2,3x,I3,5x,G8.2)'
IDL> print, a, b, c, d, e
TU 37.6700 191 98975.8
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|
|
Re: idl readf error [message #65124 is a reply to message #65123] |
Fri, 13 February 2009 15:54   |
rollo.tomasi
Messages: 4 Registered: February 2009
|
Junior Member |
|
|
Here's what I have:
IDL> openr, 1, 'file1.out'
IDL> h = strarr(5)
IDL> readf, 1, h
IDL> close, 1
IDL> for j=0,4 do print, h[j]
T U 17.81 201 145225.9
T U 37.67 191 98975.8
T U 21.77 201 129774.9
T I 0.65 1 69.5
T I 0.36 1 69.3
By doing this I noticed that the values in the last column get pretty
large, so I changed the format to
format='(2A2, G7.2, G7.2, G10.2)'
And I still get the same error.
The largest values in the numerical columns are:
column 3: 272.23
column 4: 1000
column 5: 145225.9
|
|
|
Re: idl readf error [message #65126 is a reply to message #65124] |
Fri, 13 February 2009 15:04   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
rollo.tomasi@hotmail.com writes:
> Thanks for replying! I changed the format to (2A2, 3G7.2) and even
> (2A2, G7.2, G7.2, G7.2), but I get the same error: "Type conversion
> error: Unable to convert given STRING to Float."
>
> There aren't any header lines or column labels in my input file. I
> actually use the script for 3 columns every week to do system
> performance reports, but I can't seem to add the extra columns without
> getting this error.
>
> Maybe it is something trivial I am overlooking? I don't mind changing
> the script altogether if someone suggests a different approach.
Maybe you could post the first five lines of your file
for us:
openr, 1, 'yourfile.txt'
h = strarr(5)
readf, 1, h
close, 1
for j=0,4 do print, h[j]
You could also try using ReadCol from the NASA Astronomy
Library, or even READ_BINARY from IDL, although you would
think this would be so simple you wouldn't need any fancy
programs. :-)
Cheers,
David
--
David Fanning, Ph.D.
Coyote's Guide to IDL Programming (www.dfanning.com)
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|
|
Re: idl readf error [message #65145 is a reply to message #65127] |
Thu, 12 February 2009 12:19   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
rollo.tomasi@hotmail.com writes:
> I have a file with 5 columns that looks like this (except for the
> column labels):
>
> [/color]
[color=blue]> T/F I/U Time Blocks Volume[/color]
[color=blue]> T I 7.12 227 11145.6[/color]
[color=blue]> F U 16.34 227 10914.9[/color]
[color=blue]> T I 21.43 232 11151.9[/color]
[color=blue]> T U 25.99 224 11364.1[/color]
[color=blue]> F I 37.13 199 7566.9[/color]
[color=blue]> [/color]
>
> Essentially I want to calculate the median for each time, blocks, and
> volume for the entire file, and then depending on T/F and I/U.
>
> I've gotten the script to work with the first three columns, but when
> I add the last two columns (blocks and volume) it returns an error.
> Can anyone tell me what I am doing wrong?
On this line:
readf,68, data, x1i, x2i, xxi, x3i, x4i, format='(2A2, G7.2)'
You are asking IDL to read five things, but you only
told it how to read three of them. When it reads the
first three, it then thinks, "Well, he didn't tell me anything,
so I'll try to use the format statement over again." Thus,
it tries to read a string, but it finds a float.
So, you have to fix your format statement. And I would be
careful of this, because even though you said it worked when
you had three variables rather than 5, that seems doubtful to
me. Make sure you can read the first line properly, then you will
be good to go.
Cheers,
David
--
David Fanning, Ph.D.
Coyote's Guide to IDL Programming (www.dfanning.com)
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|
Re: idl readf error [message #65220 is a reply to message #65121] |
Sat, 14 February 2009 06:52  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Vince Hradil writes:
> This thread makes me think... Can anyone compare and contrast this
> approach with the approach I usually use - that is:
> read each line into a string buf: buf =3D '' $ readf, lun, buf
> then use strsplit(buf,/extract) to chop up the buf
> then use fix, float, etc. to get the values.
>
> I don't usually use format statements for reading, just writing.
Can you spell S-L-O-W? ;-)
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|