Re: Writing a modified .txt file issue [message #85370 is a reply to message #85368] |
Mon, 29 July 2013 13:03   |
Phillip Bitzer
Messages: 223 Registered: June 2006
|
Senior Member |
|
|
I also have an ASCII data file that contains both numeric and string columns. I'm not a fan. What I'm doing now is read the whole thing in as a string array:
nLines = FILE_LINES(filename)
OPENR, lun, filename, /GET_LUN
word = STRARR(nLines)
READF, lun, word
FREE_LUN, lun
Then, I loop through each line, using STR_SPLIT to divvy up the fields at the space that separates the columns, and throw them into the output structure (not defined here for simplicity)
FOR i=0L, nLines-1 DO BEGIN
this_row = STRSPLIT(word[i], ' ', /extract)
data[i].date = this_row[0]
data[i].time = this_row[1]
data[i].latitude = DOUBLE(this_row[2])
data[i].longitude = DOUBLE(this_row[3])
....
ENDFOR
I have been looking for a better/more efficient way of doing this. Can't seem to find a good way, but maybe Coyote has cooked up something :-) ReadCol works about as quickly as my code.
I should mention that there are several string columns mixed up amongst the numeric columns. Yuck.
For the output, Hakan, I would say those numbers *do* match, although they might be formatted differently. You'll want to use the FORMAT keyword, however you're writing the file back out if you want to match the numbers/decimal places/etc.
A simple example:
IDL> a = 13.2
IDL> PRINT, a
13.2000
IDL> PRINT, a, format='(F5.2)'
13.20
IDL> PRINT, a, format='(F4.1)'
13.2
You would do something similar using PRINTF.
|
|
|