read a multi-line string in IDL? or, read numeric values sequentially without automatic <CR>? [message #87407] |
Fri, 31 January 2014 11:00  |
pcoelho.astro
Messages: 1 Registered: January 2014
|
Junior Member |
|
|
hello everyone,
here is the problem. I need to read numeric values from an ASC file which doesn't have any predefined format. The only thing I know is the number of values I'm supposed to read. FORTRAN will read it as:
READ(12,*) (NV(J),J=1,NUMBER)
(the ASC file is like that because it is used by an old FORTRAN code)
The values can be in a multi-line string or not (example below). READF will read one number per line only. Read the whole line as a string and use STRSPLIT doesn't work because the whole data set might be multi-line. The number of values per line may also change (the format also).
Messy I know. Any tip or tricks?
many thx
Paula
For example, I need to read 197 than 33 numbers from a file like below:
0.00 2042.39 4058.52 6048.34 8011.79 9948.83 0.00 2042.39 4058.52 6048.34
8011.79 9948.83 11859.39 0.00 2042.39 4058.52 6048.34 8011.79 9948.83 11859.39
13743.42 0.00 2042.39 4058.52 6048.34 8011.79 9948.83 11859.39 13743.42 15600.88
2042.39 4058.52 6048.34 8011.79 9948.83 11859.39 13743.42 15600.88 17431.69 4058.52
6048.34 8011.79 9948.83 11859.39 13743.42 15600.88 17431.69 19235.81 21013.18 6048.34
8011.79 9948.83 11859.39 13743.42 15600.88 17431.69 19235.81 21013.18 22763.76 8011.79
9948.83 11859.39 13743.42 15600.88 17431.69 19235.81 21013.18 22763.76 24487.47 9948.83
11859.39 13743.42 15600.88 17431.69 19235.81 21013.18 22763.76 24487.47 26184.28 9948.83
11859.39 13743.42 15600.88 17431.69 19235.81 21013.18 22763.76 24487.47 26184.28 27854.12
11859.39 13743.42 15600.88 17431.69 19235.81 21013.18 22763.76 24487.47 26184.28 27854.12
29496.95 13743.42 15600.88 17431.69 19235.81 21013.18 22763.76 24487.47 26184.28 27854.12
29496.95 15600.88 17431.69 19235.81 21013.18 22763.76 24487.47 26184.28 27854.12 29496.95
31112.70 32701.31 17431.69 19235.81 21013.18 22763.76 24487.47 26184.28 27854.12 29496.95
31112.70 32701.31 34262.75 19235.81 21013.18 22763.76 24487.47 26184.28 27854.12 29496.95
31112.70 32701.31 34262.75 35796.95 19235.81 21013.18 22763.76 24487.47 26184.28 27854.12
29496.95 31112.70 32701.31 34262.75 35796.95 21013.18 22763.76 24487.47 26184.28 27854.12
29496.95 31112.70 32701.31 34262.75 35796.95 22763.76 24487.47 26184.28 27854.12 29496.95
31112.70 32701.31 34262.75 35796.95 24487.47 26184.28 27854.12 29496.95 31112.70 32701.31
34262.75 35796.95 24487.47 26184.28 27854.12 29496.95 31112.70 32701.31 34262.75 35796.95
26184.28 27854.12 29496.95 31112.70 32701.31 34262.75 35796.95
1.14E-03 3.85E-03 7.85E-04 8.55E-03 3.66E-02 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
|
|
|
Re: read a multi-line string in IDL? or, read numeric values sequentially without automatic <CR>? [message #87408 is a reply to message #87407] |
Fri, 31 January 2014 11:20   |
Lajos Foldy
Messages: 176 Registered: December 2011
|
Senior Member |
|
|
On Friday, January 31, 2014 8:00:07 PM UTC+1, pcoelh...@gmail.com wrote:
>
> READ(12,*) (NV(J),J=1,NUMBER)
> (the ASC file is like that because it is used by an old FORTRAN code)
>
> ... READF will read one number per line only. ...
False.
NV=fltarr(NUMBER)
openr, unit, filename, /get
readf, unit, NV
free_lun, unit
regards,
Lajos
|
|
|
Re: read a multi-line string in IDL? or, read numeric values sequentially without automatic <CR>? [message #87409 is a reply to message #87408] |
Fri, 31 January 2014 11:25   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
fawltylanguage@gmail.com writes:
> On Friday, January 31, 2014 8:00:07 PM UTC+1, pcoelh...@gmail.com wrote:
>>
>> READ(12,*) (NV(J),J=1,NUMBER)
>> (the ASC file is like that because it is used by an old FORTRAN code)
>>
>> ... READF will read one number per line only. ...
>
> False.
>
> NV=fltarr(NUMBER)
>
> openr, unit, filename, /get
> readf, unit, NV
> free_lun, unit
Don't you just love IDL! ;-)
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
Re: read a multi-line string in IDL? or, read numeric values sequentially without automatic <CR>? [message #87410 is a reply to message #87408] |
Fri, 31 January 2014 11:33   |
Phillip Bitzer
Messages: 223 Registered: June 2006
|
Senior Member |
|
|
And if you don't know the number of values in the file a priori, you can do this:
file = 'test.txt'
nLines = FILE_LINES(file)
strA = STRARR(nLines) ;define an array to hold the "raw" data
OPENR, lun, file, /GET_LUN
READF, lun, strA ;read in the "raw" data, i.e., the data as it is in the file
FREE_LUN, lun
strJ = STRJOIN(strA) ;join all the data to one line
nVal = N_ELEMENTS(STRSPLIT(strJ)) ;number of values
arr = FLTARR(nVal) ;an array to hold the numbers
READS, strJ, arr ;read 'em in
cgPlot, arr ;or whatever
Notice that this does not give the same number of elements that you provide. For example, I get 230 values....
|
|
|
|