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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~
|