Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado
"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
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
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
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 ============================================================ =======
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 > ...
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
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 >
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 ============================================================ =======