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

Home » Public Forums » archive » Re: problem with reading formated file
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: problem with reading formated file [message #10158 is a reply to message #10154] Fri, 10 October 1997 00:00 Go to previous messageGo to previous message
David Foster is currently offline  David Foster
Messages: 341
Registered: January 1996
Senior Member
Martin Schultz wrote:
>
> Pavel Eiges wrote:
>>
>> Hello!
>>
>> i'm new in IDL programming so my question may be very
>> dumb/simple/funny/etc but i cant solve this problem by myself.
>>
>> i have a formated file like this:
>> ===========================
>> 1877383765 1.778
>> 1877384765 1.685
>> 1877385765 1.599
>> 1877386765 1.599
>> 1877387765 1.685
>> ===========================
>> and i need to load it into two arrays. the first array is LONG-type,
>> the second one - FLOAT-type. How i can do this?
>> ("READF, file, Time, Data" does not work).
>>

Here's a real easy and efficient way to do this. First, create a
structure{} containing the variables from one row, and then you
can either:

1. If you know the number of lines in the file, create an
array of structures and read the file in one read;

st = {time: 0L, data: 0.0}
st_array = replicate( st, n_lines )
openr, unit, Filename, /get_lun
readf, unit, st_array
free_lun, unit

2. Otherwise, read the file one line at a time, testing for EOF.

st = {time: 0L, data: 0.0}
st_array = replicate( st, 1000 )
openr, unit, Filename, /get_lun
temp1 = 0L
temp2 = 0.0
i = 0
while (not EOF(unit)) do begin
readf, unit, temp1, temp2
st_array[i].time = temp1
st_array[i].data = temp2
i = i + 1
endwhile
free_lun, unit
st_array = st_array[0:i-1]

3. Or alternatively you could use some of my routines as follows:

strings = FILE_STRARR( Filename ) ; Read file into STRARR
st = {time: 0L, data: 0.0}
st_array = replicate( st, n_elements(strings) )
for i = 0, n_elements(strings) - 1 do begin
pos = 0 ; Pointer into string
st_array[i].time = GET_TOKEN(strings[i], pos, /int)
st_array[i].data = GET_TOKEN(strings[i], pos, /flt)
endfor

This is a useful technique if the data-file has delimiting
characters. In this case you just have to increment POS to
pass over the delimiter. Alternatively, you can read each
variable as a string, using the delimiter (in this case either
',' or ';') to define the fields:

for i = 0, n_elements(strings) - 1 do begin
time = GET_TOKEN(strings[i], pos, sep=',;', /increment)
st_array[i].time = fix(time)
data = GET_TOKEN(strings[i], pos, sep=',;', /increment)
st_array[i].data = float(data)
endfor

You can download FILE_STRARR.PRO and GET_TOKEN.PRO from:

ftp://bial8.ucsd.edu pub/software/idl/share

Hope this helps.

Dave
--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~
David S. Foster Univ. of California, San Diego
Programmer/Analyst Brain Image Analysis Laboratory
foster@bial1.ucsd.edu Department of Psychiatry
(619) 622-5892 8950 Via La Jolla Drive, Suite 2240
La Jolla, CA 92037
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: widget_control,draw_widget
Next Topic: open windows with IDL on two monitors?

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

Current Time: Thu Oct 09 20:30:33 PDT 2025

Total time taken to generate the page: 0.15610 seconds