Re: IDL Array with mixed formating [message #91753 is a reply to message #91752] |
Thu, 20 August 2015 11:50   |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
Hello,
On 08/20/15 14:10, lucesmm@gmail.com wrote:
> Hello All
> I have a.txt that I am trying to read, and put into an array but it has mixed formats within the data (floats, and integers)
>
> example of the line:
>
> 243.828293 133.196523 19.764000 0.030571 2 0.046510 7 0.149040 14 0.214297 12
>
> This is how I am trying now to read it, but it doesn't work
>
> OPENR, lun, file_name, /GET_LUN
> nlines = FILE_LINES(file_name)
> data=fltarr(11, nlines, /NOZERO)
> line=[]
>
> while not EOF(file_name) do begin & $
> READF,lun, line, FORMAT='( 3(F+10.6, X), $( F+8.6, 1X, I3))'
> data=[[data],[line]]
> endwhile
>
>
> Any suggestions ?
$ more test.dat
243.228293 2 0.046510 10 0.949040 14 0.114297
243.328293 3 0.046510 4 0.189040 24 0.214297
243.428293 6 0.046510 9 0.148040 34 0.314297
243.528293 7 0.046510 1 0.549040 44 0.414297
243.628293 9 0.046510 5 0.249040 54 0.514297
Suggestion #1
-------------
I use "ddread" (by Fred Knight) for this sort of thing:
IDL> x=ddread('test.dat',type=4)
% Compiled module: DDREAD.
% Compiled module: NLINES.
% Compiled module: TYPEOF.
% DDREAD: Read 5 data lines selecting 7 of 7 columns; skipped 0 comment
lines.
IDL> print, x
243.228 2.00000 0.0465100 10.0000 0.949040 14.0000 0.114297
243.328 3.00000 0.0465100 4.00000 0.189040 24.0000 0.214297
243.428 6.00000 0.0465100 9.00000 0.148040 34.0000 0.314297
243.528 7.00000 0.0465100 1.00000 0.549040 44.0000 0.414297
243.628 9.00000 0.0465100 5.00000 0.249040 54.0000 0.514297
Suggestion #2
-------------
You can use ASCII_TEMPLATE and READ_ASCII:
IDL> q=ascii_template('test.dat')
% Compiled module: ASCII_TEMPLATE.
This starts up a gui that allows you to define a template with which to
read the datafile in question
IDL> help, q
** Structure <2615468>, 10 tags, length=232, data length=229, refs=1:
VERSION FLOAT 1.00000
DATASTART LONG 0
DELIMITER BYTE 32
MISSINGVALUE FLOAT NaN
COMMENTSYMBOL STRING ''
FIELDCOUNT LONG 7
FIELDTYPES LONG Array[7]
FIELDNAMES STRING Array[7]
FIELDLOCATIONS LONG Array[7]
FIELDGROUPS LONG Array[7]
IDL> x=read_ascii('test.dat',template=q)
IDL> help, x
** Structure <25b6ee8>, 7 tags, length=140, data length=140, refs=1:
FIELD1 FLOAT Array[5]
FIELD2 LONG Array[5]
FIELD3 FLOAT Array[5]
FIELD4 LONG Array[5]
FIELD5 FLOAT Array[5]
FIELD6 LONG Array[5]
FIELD7 FLOAT Array[5]
IDL> print, x.field1
243.228 243.328 243.428 243.528 243.628
IDL> print, x.field2
2 3 6 7 9
IDL> print, x.field3
0.0465100 0.0465100 0.0465100 0.0465100 0.0465100
cheers,
paulv
|
|
|