Re: ascii-template [message #44136] |
Wed, 18 May 2005 05:48 |
btt
Messages: 345 Registered: December 2000
|
Senior Member |
|
|
Lasse Clausen wrote:
> does anybody know if i can directly manipulate the saved ascii
> template? the documentation of the structure ASCII_TEMPLATE produces is
> rather poor.
>
Hi,
Yes, you may change the template's individual fields or you can compose
one of you own on the fly since it is simply an anonymous structure.
IDL> t = ascii_template('example.txt')
IDL> help, t,/str
** Structure <38f2410>, 10 tags, length=104, data length=101, refs=2:
VERSION FLOAT 1.00000
DATASTART LONG 0
DELIMITER BYTE 9
MISSINGVALUE FLOAT NaN
COMMENTSYMBOL STRING ''
FIELDCOUNT LONG 3
FIELDTYPES LONG Array[3]
FIELDNAMES STRING Array[3]
FIELDLOCATIONS LONG Array[3]
FIELDGROUPS LONG Array[3]
IDL> print, t.fieldnames
ICECREAM PICKLES COLLARDS
IDL> t.fieldnames[2] = 'CANDY'
IDL> print, t.fieldnames
ICECREAM PICKLES CANDY
It seems to me that you might prefer to have some routine handle the
whole thing for you. This is pretty easy if you can ensure that the
text files have a predictable layout. Something like the following
might suffice...
IDL> data = readmyascii('myexample.txt',count = n)
IDL> for i = 0, n-1 do print, data[i]
{ 0.00000 3.70000e-06 0.0300000}
{ 1.00000 0.0892474 0.0300000}
{ 2.00000 0.00911740 0.0300000}
{ 3.00000 0.0279552 0.0300000}
{ 4.00000 0.0209155 0.0300000}
{ 5.00000 2.43000e-05 0.0300000}
{ 6.00000 0.0107673 0.0300000}
{ 7.00000 4.94000e-05 0.0300000}
{ 8.00000 6.98000e-05 0.0300000}
{ 9.00000 0.0589265 0.0300000}
{ 10.0000 0.000136500 0.0300000}
IDL> plot, data.thisX, data.thisY
IDL> plots, data[3].thisX, data[3].thisY, psym = 5
Here's the code and example data.
Cheers,
Ben
*****START*****
FUNCTION ReadMyASCII, file, COUNT = count
if n_elements(file) EQ 0 then file = 'myexample.txt'
;the number of records is one less than
;the number of lines in the file
COUNT = FILE_LINES(file)-1
OPENR, U, file, /GET_LUN
;read in the line of fieldnamers
alpha = ''
READF, U, alpha
;break up the line of fieldnames and
;compose a template structure
beta = STRSPLIT(alpha, STRING(9B), /EXTRACT, COUNT = nFields)
t = CREATE_STRUCT(beta[0], 0.0)
FOR i = 1, nFields-1 DO t = CREATE_STRUCT(t, beta[i], 0.0)
;build an vector of structures
data = REPLICATE(t, COUNT)
;read in the data - note that the formatting
;is not specified, but could be if you wanted to get fancy
READF, U, data
FREE_LUN, U
Return, data
END
*****END******
And here's some example data...
*****START*****
THISX THISY THISZ
0.0000000 0.0000037 0.0300000
1.0000000 0.0892474 0.0300000
2.0000000 0.0091174 0.0300000
3.0000000 0.0279552 0.0300000
4.0000000 0.0209155 0.0300000
5.0000000 0.0000243 0.0300000
6.0000000 0.0107673 0.0300000
7.0000000 0.0000494 0.0300000
8.0000000 0.0000698 0.0300000
9.0000000 0.0589265 0.0300000
10.0000000 0.0001365 0.0300000
*****END*******
|
|
|