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

Home » Public Forums » archive » IDL Array with mixed formating
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
IDL Array with mixed formating [message #91752] Thu, 20 August 2015 11:10 Go to next message
lucesmm is currently offline  lucesmm
Messages: 26
Registered: October 2014
Junior Member
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 ?

thanks
Re: IDL Array with mixed formating [message #91753 is a reply to message #91752] Thu, 20 August 2015 11:50 Go to previous messageGo to next message
Paul Van Delst[1] is currently offline  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
Re: IDL Array with mixed formating [message #91763 is a reply to message #91752] Mon, 24 August 2015 09:31 Go to previous message
lucesmm is currently offline  lucesmm
Messages: 26
Registered: October 2014
Junior Member
I ended up doing something like this and it looks like it's working fine

OPENR,lun,file_name,/GET_LUN
nlines = FILE_LINES(file_name)

array= strarr(1,nlines)
data=[]
line=[]

READF,lun,array
close, /all
for i=0,nlines-1 do begin
lines=float(strsplit(array(i), /EXTRACT))
data=[[data],[lines]]
endfor
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Passing Arrays BY REFERENCE
Next Topic: Plot and Legend problem

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

Current Time: Wed Oct 08 07:17:29 PDT 2025

Total time taken to generate the page: 0.00479 seconds