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

Home » Public Forums » archive » Re: String Data 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: String Data file [message #35836] Thu, 31 July 2003 23:57
R.Bauer is currently offline  R.Bauer
Messages: 1424
Registered: November 1998
Senior Member
Pavel Romashkin wrote:
> This code doesn't seem to work because the first field, a string, reads
> in the entire line and the numerical variables won't have anything to read:
>
> IDL> line = {col1:"a string", col2:0.0, col3:0.0}
> IDL> OPENR, lun, 'untitled.txt', /GET_LUN
> IDL> READF, lun, data
> % READF: Input conversion error. Unit: 100
> File: SCSI data:untitled.txt
> % Execution halted at: $MAIN$
>
> Would work if the string was the last in the line.
> Cheers,
> Pavel
>

I know but if we use a byte array and some calculations it is much easier.

My routine wasn't working because it determines chars und numbers in the
first byte of each line to find out where are comments.
So it hangs because all looked like comments.

If the text column would be the second or later it works fine.

One workaround could be to add a dummy column in front of the text column.


best regards

Reimar








> Justin wrote:
>
>> Use structures. In fact always use structures for everything because
>> Structures Are Your Friends. They'll make your programs elegant.
>>
>> ;Use a structure to specify that each line is a string and two floats
>> line = {col1:"a string", col2:0.0, col3:0.0}
>> num_lines = 100
>> ;Make an array of structures
>> data = REPLICATE(line, num_lines)
>> OPENR, lun, 'yourfile.dat', /GET_LUN
>> ;Read in all the data in one go
>> READF, lun, data
>> FREE_LUN, lun
>> ;Print out some of the data
>> PRINT, data[5].col1, data[7:9].col2, data.col3
>>


--
Reimar Bauer

Institut fuer Stratosphaerische Chemie (ICG-I)
Forschungszentrum Juelich
email: R.Bauer@fz-juelich.de
------------------------------------------------------------ -------
a IDL library at ForschungsZentrum Juelich
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_lib_intro. html
============================================================ =======
Re: String Data file [message #35844 is a reply to message #35836] Thu, 31 July 2003 13:38 Go to previous message
Pavel Romashkin is currently offline  Pavel Romashkin
Messages: 166
Registered: April 1999
Senior Member
This code doesn't seem to work because the first field, a string, reads
in the entire line and the numerical variables won't have anything to read:

IDL> line = {col1:"a string", col2:0.0, col3:0.0}
IDL> OPENR, lun, 'untitled.txt', /GET_LUN
IDL> READF, lun, data
% READF: Input conversion error. Unit: 100
File: SCSI data:untitled.txt
% Execution halted at: $MAIN$

Would work if the string was the last in the line.
Cheers,
Pavel

Justin wrote:
>
> Use structures. In fact always use structures for everything because
> Structures Are Your Friends. They'll make your programs elegant.
>
> ;Use a structure to specify that each line is a string and two floats
> line = {col1:"a string", col2:0.0, col3:0.0}
> num_lines = 100
> ;Make an array of structures
> data = REPLICATE(line, num_lines)
> OPENR, lun, 'yourfile.dat', /GET_LUN
> ;Read in all the data in one go
> READF, lun, data
> FREE_LUN, lun
> ;Print out some of the data
> PRINT, data[5].col1, data[7:9].col2, data.col3
>
Re: String Data file [message #35848 is a reply to message #35844] Thu, 31 July 2003 10:52 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Justin writes:

> Use structures. In fact always use structures for everything because
> Structures Are Your Friends. They'll make your programs elegant.

Actually, I think you mean "objects". :-)

Cheers,

David

--
David W. Fanning, Ph.D.
Fanning Software Consulting, Inc.
Phone: 970-221-0438, E-mail: david@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
Re: String Data file [message #35849 is a reply to message #35848] Thu, 31 July 2003 10:46 Go to previous message
Justin[2] is currently offline  Justin[2]
Messages: 16
Registered: October 2002
Junior Member
Use structures. In fact always use structures for everything because
Structures Are Your Friends. They'll make your programs elegant.

;Use a structure to specify that each line is a string and two floats
line = {col1:"a string", col2:0.0, col3:0.0}
num_lines = 100
;Make an array of structures
data = REPLICATE(line, num_lines)
OPENR, lun, 'yourfile.dat', /GET_LUN
;Read in all the data in one go
READF, lun, data
FREE_LUN, lun
;Print out some of the data
PRINT, data[5].col1, data[7:9].col2, data.col3



Karthikayan Balakrishnan wrote:
> Hello All,
> I have a data file that looks like this:
> abc 1 2
> def 3 4.5
> ghij 2.3 23.44
> ...
Re: String Data file [message #35857 is a reply to message #35849] Thu, 31 July 2003 00:02 Go to previous message
R.Bauer is currently offline  R.Bauer
Messages: 1424
Registered: November 1998
Senior Member
Karthikayan Balakrishnan wrote:
> Hello All,
> I have a data file that looks like this:
> abc 1 2
> def 3 4.5
> ghij 2.3 23.44
> ...
> ...
> The first column is string, the next two are
> floating point. Is there any way I can read such a file?
> I am familiar with counting the number of rows and reading
> data file with just numbers, but this combination of numbers
> and strings is too much for me.
>
> Thanks in advance,
> Karthik.

Our published read_date_file with the /vstruct keyword does not read
this file.
x=read_data_file('file.txt',/vstruct)

I missed to include first column of text type. I have fixed this in our
working library. Now I am testing the whole thing.

help,x.data,/str
** Structure <83892dc>, 3 tags, length=20, data length=20, refs=2:
VAR0 STRING 'abc'
VAR1 FLOAT 1.00000
VAR2 FLOAT 2.00000

help,x.data[1],/str
** Structure <83892dc>, 3 tags, length=20, data length=20, refs=2:
VAR0 STRING 'def'
VAR1 FLOAT 3.00000
VAR2 FLOAT 4.50000

help,x.data[2],/str
** Structure <83892dc>, 3 tags, length=20, data length=20, refs=2:
VAR0 STRING 'ghij'
VAR1 FLOAT 2.30000
VAR2 FLOAT 23.4400

If you are interested I can sent a separate copy of the changed files.

Reimar


--
Reimar Bauer

Institut fuer Stratosphaerische Chemie (ICG-I)
Forschungszentrum Juelich
email: R.Bauer@fz-juelich.de
------------------------------------------------------------ -------
a IDL library at ForschungsZentrum Juelich
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_lib_intro. html
============================================================ =======
Re: String Data file [message #35858 is a reply to message #35857] Wed, 30 July 2003 21:26 Go to previous message
mperrin+news is currently offline  mperrin+news
Messages: 81
Registered: May 2001
Member
Karthikayan Balakrishnan <lovedale27@hotmail.NOSPAM.com.PLEASEREMOVETHIS> wrote:
> Hello All,
> I have a data file that looks like this:
> abc 1 2
> def 3 4.5
> ghij 2.3 23.44
> ...
> ...
> The first column is string, the next two are
> floating point. Is there any way I can read such a file?
> I am familiar with counting the number of rows and reading
> data file with just numbers, but this combination of numbers
> and strings is too much for me.

Grab a copy of readcol.pro from
http://idlastro.gsfc.nasa.gov/ftp/pro/misc/readcol.pro

Then it's just as simple as
readcol,"filename.txt",format="A,F,F,",col1var,col2var,col3var

- Marshall
Re: String Data file [message #35859 is a reply to message #35858] Wed, 30 July 2003 19:55 Go to previous message
andrew.cool is currently offline  andrew.cool
Messages: 47
Registered: July 2003
Member
Karthikayan Balakrishnan <lovedale27@hotmail.NOSPAM.com.PLEASEREMOVETHIS> wrote in message news:<HIuzA3.5Mw@sysadm.physics.uiowa.edu>...
> Hello All,
> I have a data file that looks like this:
> abc 1 2
> def 3 4.5
> ghij 2.3 23.44
> ...
> ...
> The first column is string, the next two are
> floating point. Is there any way I can read such a file?
> I am familiar with counting the number of rows and reading
> data file with just numbers, but this combination of numbers
> and strings is too much for me.
>
> Thanks in advance,
> Karthik.

Karthik,

Try this :-

line = 'ghij 2.3 23.44'

READS,line,s,f1,f2,form='(a4,x,f3.1,x,f5.2)'


Andrew
Re: String Data file [message #35861 is a reply to message #35859] Wed, 30 July 2003 16:30 Go to previous message
Rick Towler is currently offline  Rick Towler
Messages: 821
Registered: August 1998
Senior Member
"Karthikayan Balakrishnan" wrote...

> I have a data file that looks like this:
> abc 1 2
> def 3 4.5
> ghij 2.3 23.44
> ...
> ...
> The first column is string, the next two are
> floating point. Is there any way I can read such a file?

You have a couple of options depending on how strict a format your data file
adheres to.

If your file has a strict format (which it doesn't look like it does) you
can use the FORMAT keyword to READF to specify exactly how your data is
read. For example:

OPENR, lun, 'yourfile.dat', /GET_LUN
while (not EOF(lun)) do begin
READF, lun, str, fl1, fl2, FORMAT='(a5,1x,f3.1,1x,f5.2)'
PRINT, str, fl1, fl2
endwhile
FREE_LUN, lun

would print your file to the screen, assuming your format was fixed.


If you don't have a strict format for your file, and it starts with a
string, then you'll need to read the entire line in as a string and parse
the line for your data. Something like this:

OPENR, lun, 'yourfile.dat', /GET_LUN
str = ''
while (not EOF(lun)) do begin
READF, lun, str
data = STRSPLIT(str, ' ', /EXTRACT)
str = data[0]
fl1 = FLOAT(data[1])
fl2 = FLOAT(data[2])
PRINT, str, fl1, fl2
endwhile
FREE_LUN, lun


Hope this helps!

-Rick
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: BOX_CURSOR
Next Topic: minor glitch in total()

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

Current Time: Wed Oct 08 14:56:43 PDT 2025

Total time taken to generate the page: 0.00489 seconds