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 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: problem with reading formated file [message #10137] Thu, 16 October 1997 00:00
Clemens v. Mann is currently offline  Clemens v. Mann
Messages: 1
Registered: October 1997
Junior Member
Hi

there is an other possibility for the problem,
when the float has no dot ( format problem) :

str_chr = { str_chr, L:'', F:''}
str_chr_arr = replicate( { str_chr}, lines)

// read in as characters
readf, fd, str_chr_arr, format = '(A15, A8)'

str = { str, L:0L, F:0.0}
str_arr = replicate( { str}, lines)

// no needed loop !
str_arr.L = str_chr_arr.L
str_arr.F = str_chr_arr.F

Clemens
Re: problem with reading formated file [message #10154 is a reply to message #10137] Sat, 11 October 1997 00:00 Go to previous message
rivers is currently offline  rivers
Messages: 228
Registered: March 1991
Senior Member
In article <343f0158.1848870@news-server>, ccampo1@nospamsan.nospamrr.com (Chris and Cathy Campo) writes:
> On Fri, 10 Oct 1997 19:25:54 +0300, Pavel Eiges <eiges@chat.ru> wrote:
>> 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).
>
>
> try this:
> n=5
> time = lonarr(n)
> data = fltarr(n)
> for i = 0, n-1 do begin
> readf,file, time(i), data(i)
> endfor

No, this will not work!!! You cannot read into a subscripted array element,
because time(i) is passed by "value" rather by "reference" to readf.

The simplest way to solve the problem is as follows:

n=5
time = lonarr(n)
data = fltarr(n)
temp = fltarr(2, n)
readf, file, temp
time(*) = temp(0,*)
data(*) = temp(1,*)

____________________________________________________________
Mark Rivers (773) 702-2279 (office)
CARS (773) 702-9951 (secretary)
Univ. of Chicago (773) 702-5454 (FAX)
5640 S. Ellis Ave. (708) 922-0499 (home)
Chicago, IL 60637 rivers@cars.uchicago.edu (e-mail)

or:
Argonne National Laboratory (630) 252-0422 (office)
Building 434A (630) 252-0405 (lab)
9700 South Cass Avenue (630) 252-1713 (beamline)
Argonne, IL 60439 (630) 252-0443 (FAX)
Re: problem with reading formated file [message #10156 is a reply to message #10154] Sat, 11 October 1997 00:00 Go to previous message
ccampo1 is currently offline  ccampo1
Messages: 1
Registered: October 1997
Junior Member
On Fri, 10 Oct 1997 19:25:54 +0300, Pavel Eiges <eiges@chat.ru> wrote:
> 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).


try this:
n=5
time = lonarr(n)
data = fltarr(n)
for i = 0, n-1 do begin
readf,file, time(i), data(i)
endfor
Re: problem with reading formated file [message #10158 is a reply to message #10154] Fri, 10 October 1997 00:00 Go 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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~
Re: problem with reading formated file [message #10160 is a reply to message #10158] Fri, 10 October 1997 00:00 Go to previous message
Martin Schultz is currently offline  Martin Schultz
Messages: 515
Registered: August 1997
Senior Member
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).
>
> %-\ HELP!
>
> Pavel Eiges



You must specify a format='(...)' statement with the READF command. In
your case thi swoul dbe something like

READF,file, time, data, format='(3X,i10,f9.3)'

Also, because time is a long variable, you may have to assign it to a
long value before you call READF, e.g. time=0L (well, you don't have to:
IDL converts it into a float if you don't. But this way you can save it
as a long variable if you like to).

Cheers,
Martin.




------------------------------------------------------------ ------------
Dr. Martin Schultz
Department for Earth&Planetary Sciences, Harvard University
186 Pierce Hall, 29 Oxford St., Cambridge, MA-02138, USA

phone: (617)-496-8318
fax : (617)-495-9837
Please indicate name and room (186 Pierce) when sending a fax

e-mail: mgs@io.harvard.edu
IDL-homepage: http://www-as.harvard.edu/people/staff/mgs/idl/
------------------------------------------------------------ ------------
  Switch to threaded view of this topic Create a new topic Submit Reply
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: Wed Oct 08 15:39:44 PDT 2025

Total time taken to generate the page: 0.00643 seconds