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

Home » Public Forums » archive » Re: Am I having a blackout?
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: Am I having a blackout? [message #66645] Fri, 29 May 2009 05:31
Brian Larsen is currently offline  Brian Larsen
Messages: 270
Registered: June 2006
Senior Member
I always have to remind myself each time exactly the right way to do
this but I am a big fan of reading in text files into a structure all
in one go.

Give something like this a try, for my brain this is the best way.


file='idl.dat'
lines=file_lines(file)
dat={date:0L, data:fltarr(5)}
dat=replicate(dat, lines)
openr, lun, file, /get_lun
readf, lun, dat
free_lun, lun
print, dat.date
; 20050206 20050207 20050402 20050411 20050421
20050422
; 20050423 20050614 20050618 20050619
print, dat.data[0]
; 0.386216 0.391236 0.685645 0.727419 0.768319
0.772067
; 0.775750 0.878818 0.879916 0.880047





Cheers,

Brian

------------------------------------------------------------ --------------
Brian Larsen
Boston University
Center for Space Physics
http://people.bu.edu/balarsen/Home/IDL
Re: Am I having a blackout? [message #66646 is a reply to message #66645] Fri, 29 May 2009 02:14 Go to previous message
Allan Whiteford is currently offline  Allan Whiteford
Messages: 117
Registered: June 2006
Senior Member
Mark wrote:
> Hi all,
>
> I'm trying to read a very simple file with very basic commands, but it
> reads the wrong values for the last variable (d2): readf does not see
> it are negative values and reads only the last five digits. The first
> five items it reads OK. I guess I'm making a very trivial mistake
> here, but I don't see it. Anyone having a clearer mind than me?
> Thanks!
>
> Mark
>
> sum1 = 0.0
> sum2 = 0.0
> sum3 = 0.0
> sum4 = 0.0
> count = 0
> dummy1=''
> openr, unit, /get_lun, 'sunny_days.fits'
> while ~eof(unit) do begin
> readf, unit, format='(A8,F,F,F,F,F)', dummy1, dummy2, A, beta, d1,
> d2

Mark,

My understanding of simply using 'F' as a format code doesn't mean "read
a floating point number in a smart way" it means "use the default values
for a floating point number". These are (according to the manual)
equivalent to 'F15.7'.

So, when you write:

format='(A8,F,F,F,F,F)'

this is equivalent to:

format='(A8,F15.7,F15.7,F15.7,F15.7,F15.7)',

A cursory glance at your file seems to explain what is happening, all
your whitespace are being slowly eaten until finally things go wrong at
the last column.

The behaviour of 'F' doesn't seem to be as useful as you would
reasonably expect it to be.

You probably want to use:

format='(A8,5F13)'

but when reading data in like this it's usually better not to supply a
format code at all - then IDL will try to do something smart for you
based on the types of variables you're using.

Thanks,

Allan
Re: Am I having a blackout? [message #66647 is a reply to message #66646] Fri, 29 May 2009 01:25 Go to previous message
Mark[2] is currently offline  Mark[2]
Messages: 4
Registered: May 2009
Junior Member
Hi Chris,

Thanks for replying. Thanks for your suggestion, but it's more a
matter of why this particular piece of code does not work as intended.
(And the first field is a date, which, I guess, can be more
appropriately stored in a string).
Well, a new day has begun, new chances...

Mark


On May 29, 7:27 am, rog...@googlemail.com wrote:
> On 28 Mai, 23:34, Mark <Mark.Saven...@gmail.com> wrote:
>
>
>
>> -> due to the line wraps the layout is somewhat disturbed: the d2 is
>> after the d1 and also the negative values are in fact after the other
>> items on the line.
>
>> On May 28, 11:27 pm, Mark <Mark.Saven...@gmail.com> wrote:
>
>>> Hi all,
>
>>> I'm trying to read a very simple file with very basic commands, but it
>>> reads the wrong values for the last variable (d2): readf does not see
>>> it are negative values and reads only the last five digits. The first
>>> five items it reads OK. I guess I'm making a very trivial mistake
>>> here, but I don't see it. Anyone having a clearer mind than me?
>>> Thanks!
>
>>> Mark
>
>>> sum1 = 0.0
>>> sum2 = 0.0
>>> sum3 = 0.0
>>> sum4 = 0.0
>>> count = 0
>>> dummy1=''
>>> openr, unit, /get_lun, 'sunny_days.fits'
>>> while ~eof(unit) do begin
>>> readf, unit, format='(A8,F,F,F,F,F)', dummy1, dummy2, A, beta, d1,
>>> d2
>>> help, dummy1, dummy2, A, beta, d1, d2
>>> sum1 += A
>>> sum2 += beta
>>> sum3 += d1
>>> sum4 += d2
>>> count++
>>> end
>>> print, sum1/count, sum2/count, sum3/count, sum4/count
>>> end
>
>>> part of sunny_days.fits:
>
>>> 20050206 0.386216 1286.08 1.34860 0.133876
>>> -0.695917
>>> 20050207 0.391236 1080.36 1.19682 0.285981
>>> -0.127826
>>> 20050402 0.685645 1137.82 1.20061 0.112449
>>> -0.716363
>>> 20050411 0.727419 1096.23 1.21388 0.134878
>>> -0.556709
>>> 20050421 0.768319 1110.19 1.19086 0.136384
>>> -0.636537
>>> 20050422 0.772067 1133.38 1.26480 0.214946
>>> -0.528211
>>> 20050423 0.775750 1115.85 1.26544 0.196289
>>> -0.642749
>>> 20050614 0.878818 1109.71 1.21778 0.140585
>>> -0.640095
>>> 20050618 0.879916 1055.35 1.16299 0.103695
>>> -0.706452
>>> 20050619 0.880047 1071.56 1.15538 0.0915420
>>> -0.690452
>
>>> Full file at:http://www.markjoe.com/tmp/sunny_days.fits-Zitierten Text ausblenden -
>
>> - Zitierten Text anzeigen -
>
> Dear Mark,
> no idea, what's going wrong there, but did you try the following?
>
> data=fltarr(6,file_lines('sunny_days.fits'),/nozero)
> openr, unit, /get_lun, 'sunny_days.fits'
> readf, unit, data
> free_lun, unit
> etc...
>
> Regards
> Chris
Re: Am I having a blackout? [message #66648 is a reply to message #66647] Fri, 29 May 2009 00:27 Go to previous message
rogass is currently offline  rogass
Messages: 200
Registered: April 2008
Senior Member
On 28 Mai, 23:34, Mark <Mark.Saven...@gmail.com> wrote:
> -> due to the line wraps the layout is somewhat disturbed: the d2 is
> after the d1 and also the negative values are in fact after the other
> items on the line.
>
> On May 28, 11:27 pm, Mark <Mark.Saven...@gmail.com> wrote:
>
>
>
>> Hi all,
>
>> I'm trying to read a very simple file with very basic commands, but it
>> reads the wrong values for the last variable (d2): readf does not see
>> it are negative values and reads only the last five digits. The first
>> five items it reads OK. I guess I'm making a very trivial mistake
>> here, but I don't see it. Anyone having a clearer mind than me?
>> Thanks!
>
>> Mark
>
>>   sum1 = 0.0
>>   sum2 = 0.0
>>   sum3 = 0.0
>>   sum4 = 0.0
>>   count = 0
>>   dummy1=''
>>   openr, unit, /get_lun, 'sunny_days.fits'
>>   while ~eof(unit) do begin
>>     readf, unit, format='(A8,F,F,F,F,F)', dummy1, dummy2, A, beta, d1,
>> d2
>> help, dummy1, dummy2, A, beta, d1, d2
>>     sum1 += A
>>     sum2 += beta
>>     sum3 += d1
>>     sum4 += d2
>>     count++
>>   end
>>   print, sum1/count, sum2/count, sum3/count, sum4/count
>> end
>
>> part of sunny_days.fits:
>
>> 20050206     0.386216      1286.08      1.34860     0.133876
>> -0.695917
>> 20050207     0.391236      1080.36      1.19682     0.285981
>> -0.127826
>> 20050402     0.685645      1137.82      1.20061     0.112449
>> -0.716363
>> 20050411     0.727419      1096.23      1.21388     0.134878
>> -0.556709
>> 20050421     0.768319      1110.19      1.19086     0.136384
>> -0.636537
>> 20050422     0.772067      1133.38      1.26480     0.214946
>> -0.528211
>> 20050423     0.775750      1115.85      1.26544     0.196289
>> -0.642749
>> 20050614     0.878818      1109.71      1.21778     0.140585
>> -0.640095
>> 20050618     0.879916      1055.35      1.16299     0.103695
>> -0.706452
>> 20050619     0.880047      1071.56      1.15538    0.0915420
>> -0.690452
>
>> Full file at:http://www.markjoe.com/tmp/sunny_days.fits- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

Dear Mark,
no idea, what's going wrong there, but did you try the following?

data=fltarr(6,file_lines('sunny_days.fits'),/nozero)
openr, unit, /get_lun, 'sunny_days.fits'
readf, unit, data
free_lun, unit
etc...

Regards
Chris
Re: Am I having a blackout? [message #66650 is a reply to message #66648] Thu, 28 May 2009 14:34 Go to previous message
Mark[2] is currently offline  Mark[2]
Messages: 4
Registered: May 2009
Junior Member
-> due to the line wraps the layout is somewhat disturbed: the d2 is
after the d1 and also the negative values are in fact after the other
items on the line.

On May 28, 11:27 pm, Mark <Mark.Saven...@gmail.com> wrote:
> Hi all,
>
> I'm trying to read a very simple file with very basic commands, but it
> reads the wrong values for the last variable (d2): readf does not see
> it are negative values and reads only the last five digits. The first
> five items it reads OK. I guess I'm making a very trivial mistake
> here, but I don't see it. Anyone having a clearer mind than me?
> Thanks!
>
> Mark
>
>   sum1 = 0.0
>   sum2 = 0.0
>   sum3 = 0.0
>   sum4 = 0.0
>   count = 0
>   dummy1=''
>   openr, unit, /get_lun, 'sunny_days.fits'
>   while ~eof(unit) do begin
>     readf, unit, format='(A8,F,F,F,F,F)', dummy1, dummy2, A, beta, d1,
> d2
> help, dummy1, dummy2, A, beta, d1, d2
>     sum1 += A
>     sum2 += beta
>     sum3 += d1
>     sum4 += d2
>     count++
>   end
>   print, sum1/count, sum2/count, sum3/count, sum4/count
> end
>
> part of sunny_days.fits:
>
> 20050206     0.386216      1286.08      1.34860     0.133876
> -0.695917
> 20050207     0.391236      1080.36      1.19682     0.285981
> -0.127826
> 20050402     0.685645      1137.82      1.20061     0.112449
> -0.716363
> 20050411     0.727419      1096.23      1.21388     0.134878
> -0.556709
> 20050421     0.768319      1110.19      1.19086     0.136384
> -0.636537
> 20050422     0.772067      1133.38      1.26480     0.214946
> -0.528211
> 20050423     0.775750      1115.85      1.26544     0.196289
> -0.642749
> 20050614     0.878818      1109.71      1.21778     0.140585
> -0.640095
> 20050618     0.879916      1055.35      1.16299     0.103695
> -0.706452
> 20050619     0.880047      1071.56      1.15538    0.0915420
> -0.690452
>
> Full file at:http://www.markjoe.com/tmp/sunny_days.fits
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Debugging Rules
Next Topic: Re: read multiple files with varying names

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

Current Time: Wed Oct 08 13:45:53 PDT 2025

Total time taken to generate the page: 0.00815 seconds