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

Home » Public Forums » archive » Formatted read of text 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
Formatted read of text file [message #9154] Wed, 11 June 1997 00:00 Go to next message
dione is currently offline  dione
Messages: 1
Registered: June 1997
Junior Member
I have a file that contains row and column data like the following:

# I E E_conv E_bl E_saa Resid_RMS
# 0 3.512799e-05 3.512799e-05 0.000000e+00 0.000000e+00 3.512799e-05
1 1.445887e-05 1.445921e-05 0.000000e+00 0.000000e+00 1.445921e-05
2 7.363944e-06 7.364333e-06 0.000000e+00 0.000000e+00 7.364333e-06
.
.
.

So my question is a two-parter.

First, how to I skip the first two lines of this file?

Second, what is proper way to do a formatted read of each of the 6 columns in
the remaining rows? I would like to have each column value put into its
own array so I can then plot/print out each column individually.

My guess is that I have to use and OPENR first to open the file, but that
may not even be the best way to do it.

Much Thanks,

--
+----------------------------------------------------------- ------------------+
Matthew Cheselka, Philomat Center for Astronomical Adaptive Optics
internet: dione@as.arizona.edu Steward Observatory
wk: (520) 621-1624 hm: (520) 575-0992
http://aquarius.as.arizona.edu:8080
Re: Formatted read of text file [message #9251 is a reply to message #9154] Sun, 15 June 1997 00:00 Go to previous message
Vibor Paravic is currently offline  Vibor Paravic
Messages: 7
Registered: June 1997
Junior Member
Matt Cheselka wrote:

> I have a file that contains row and column data like the following:
>
> # I E E_conv E_bl E_saa Resid_RMS
> # 0 3.512799e-05 3.512799e-05 0.000000e+00 0.000000e+00 3.512799e-05
> 1 1.445887e-05 1.445921e-05 0.000000e+00 0.000000e+00 1.445921e-05
> 2 7.363944e-06 7.364333e-06 0.000000e+00 0.000000e+00 7.364333e-06
> .
> .
> .
>

Matthew,

I do not program in IDL but in Pv-Wave I would use the following code:

st = DC_READ_FREE(filename, col_1, col_2, col_3, col_4, col_5, col_6,
/col, Resize=[1,2,3,4,5,6], NSkip = 2)

you can also add a parameter which tells the function wha teh delimiter
is in you data
for example

Delim = [','] .. for comma delimited files

Vibor Paravic
Research Engineer
Institute of Orthopedic Research and Education
vparavic@bcm.tmc.edu
Re: Formatted read of text file [message #9286 is a reply to message #9154] Thu, 12 June 1997 00:00 Go to previous message
Marty Ryba is currently offline  Marty Ryba
Messages: 16
Registered: May 1996
Junior Member
Matt Cheselka wrote:
> I have a file that contains row and column data like the following:
>
> # I E E_conv E_bl E_saa Resid_RMS
> # 0 3.512799e-05 3.512799e-05 0.000000e+00 0.000000e+00 3.512799e-05
> 1 1.445887e-05 1.445921e-05 0.000000e+00 0.000000e+00 1.445921e-05
> 2 7.363944e-06 7.364333e-06 0.000000e+00 0.000000e+00 7.364333e-06
> .

Matt (and any others):

You should really check out Fred Knight's DDREAD.PRO (available from
one of the FTP archives). It automatically parses the file, skips
comments, and loads the data in either a 2-d array or a array of
structures. You can pre-define the structure if you wish (so it will
have mneumonic names and the correct data types). Truly a must-have
general-purpose function.

--
Dr. Marty Ryba | Of course nothing I say here is official
MIT Lincoln Laboratory | policy, and Laboratory affililaton is
ryba@ll.mit.edu | for identification purposes only,
| blah, blah, blah, ...
Re: Formatted read of text file [message #9289 is a reply to message #9154] Thu, 12 June 1997 00:00 Go to previous message
Andy Bristow is currently offline  Andy Bristow
Messages: 6
Registered: June 1997
Junior Member
Matt Cheselka wrote:
>
> I have a file that contains row and column data like the following:
>
> # I E E_conv E_bl E_saa Resid_RMS
> # 0 3.512799e-05 3.512799e-05 0.000000e+00 0.000000e+00 3.512799e-05
> 1 1.445887e-05 1.445921e-05 0.000000e+00 0.000000e+00 1.445921e-05
> 2 7.363944e-06 7.364333e-06 0.000000e+00 0.000000e+00 7.364333e-06
> .
> .
> .
>
> So my question is a two-parter.
>
> First, how to I skip the first two lines of this file?
>
> Second, what is proper way to do a formatted read of each of the 6 columns in
> the remaining rows? I would like to have each column value put into its
> own array so I can then plot/print out each column individually.
>
> My guess is that I have to use and OPENR first to open the file, but that
> may not even be the best way to do it.
>

Okay. You hit the nail with the OPENR guess. You don't really need a
FORMAT statement or anything - the data will read in fine without one as
long as there is at least 1 space between each column. Something like
this should work.
;+++
rubbish=''
; open the file
openr,1,filename
; read the 2 first lines to get to the data
readf,1,rubbish
readf,1,rubbish
; num is number of rows of data
; set up a data array for floating numbers - don't worry about the
integer in the
; 1st column, it will work
data=fltarr(6,num)
; read the data
readf,1,data
;close the file
close,1
;+++

You would then have a 6 x num array. You could split this up into 6
individual arrays with:

array1=reform(data(0,*))
array2=reform(data(1,*))

etc

The first column is a integer, so if you want it in integer form just do

array1=fix(array1)


Hope this helps

Andy



P.S. If you don't know how may rows are in the file (for the num
variable, above), then start the same, declare a temporary array
(temp=fltarr(6)) and a main data array big enough to hold the maximum
amount of data yuo will read in (data(fltarr(6,maxnum)). Then

count=0
while NOT(EOF(1)) do begin
readf,1,temp
data(*,count)=temp(*)
count=count+1
endwhile

Then reform the data according to how many records there actually were:

data=reform(data(*,0:count-1))


--
Andy Bristow ajbristow@taz.dra.hmg.gb
DERA Ocean Modelling Tel: +44 (0) 1305 212323
Winfrith Technology Centre Fax: +44 (0) 1305 212103
Dorchester, DT2 8XJ, UK.
Re: Formatted read of text file [message #9293 is a reply to message #9154] Wed, 11 June 1997 00:00 Go to previous message
Harald Frey is currently offline  Harald Frey
Messages: 41
Registered: March 1997
Member
Matt Cheselka wrote:
>
> I have a file that contains row and column data like the following:
>
> # I E E_conv E_bl E_saa Resid_RMS
> # 0 3.512799e-05 3.512799e-05 0.000000e+00 0.000000e+00 3.512799e-05
> 1 1.445887e-05 1.445921e-05 0.000000e+00 0.000000e+00 1.445921e-05
> 2 7.363944e-06 7.364333e-06 0.000000e+00 0.000000e+00 7.364333e-06
> .
> .
> .
>
> So my question is a two-parter.
>
> First, how to I skip the first two lines of this file?
>
> Second, what is proper way to do a formatted read of each of the 6 columns in
> the remaining rows? I would like to have each column value put into its
> own array so I can then plot/print out each column individually.
>
> My guess is that I have to use and OPENR first to open the file, but that
> may not even be the best way to do it.
>

; -----------------------------------------------------------
pro read_formatted_file

file_name='test.dat' ; change if you want

dummy=' '
openr,unit,file_name,/get_lun
readf,unit,dummy,format='(/a20)'
print,dummy ; only to check the result of ; readf

on_ioerror,io_error
a=fltarr(5) ; define input
num=0
numb_of_lines=2
numb=intarr(numb_of_lines)
array=fltarr(5,numb_of_lines) ; define result

; if you don't know the number of lines you can also use the ;
"while" command
for i=0,numb_of_lines do begin
readf,unit,num,a,format='(i2,5g13.6)'
array(*,i)=a
endfor

io_error:
plot_io,array(0,*)

end
; ----------------------------------------------------

Harald Frey
Space Sciences Laboratory
University of California
Berkeley, CA 94720
hfrey@ssl.berkeley.edu
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Image Object Scaling Bug
Next Topic: Matlab <--> PV-Wave ?

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

Current Time: Wed Oct 08 15:33:24 PDT 2025

Total time taken to generate the page: 0.00686 seconds