Error using data structures while reading ASCII file with different data types [message #87438] |
Mon, 03 February 2014 08:51  |
atmospheric physics
Messages: 121 Registered: June 2010
|
Senior Member |
|
|
Hello,
I have the following data in an ASCII file (filename.txt):
2006-01-31 08:59:26 65.04 22.92 0.207226 0.150028 0.000000 0.057198
2006-01-31 08:59:26 65.22 22.92 0.231006 0.167339 0.000000 0.063667
2006-01-31 08:59:26 65.40 22.92 0.273280 0.201961 0.000000 0.071319
2006-01-31 08:59:26 65.58 22.92 0.255984 0.190420 0.000000 0.065564
2006-01-31 08:59:26 65.76 22.92 0.303038 0.225042 0.000000 0.077996
...
...
...
I tried to read this file with the following lines in IDL:
; ----------------------------------
PRO test.pro
nlines=FILE_LINES('filename.txt')
file_data=STRARR(nlines)
OPENR,lun,'filename.txt',/GET_LUN
READF,lun,file_data
CLOSE,lun & FREE_LUN,lun
dataStruct= { yyyy:0, mm:0, dd:0, hr:0, mi:0, sec:0, $
lon:0.0, lat:0.0, p1:0.0, p2:0.0, p3:0.0, p4:0.0 }
data=REPLICATE(dataStruct,nlines)
FOR ii=0,(nlines-1) DO BEGIN
str=file_data[ii]
str_array=STRSPLIT(str,' ',/EXTRACT)
yyyymmdd=STRSPLIT(str_array(0),'-',/EXTRACT)
hhmmss=STRSPLIT(str_array(1),':',/EXTRACT)
year=FIX(yyyymmdd(0)) & month=FIX(yyyymmdd(1)) & day=FIX(yyyymmdd(2))
hour=FIX(hhmmss(0)) & minutes=FIX(hhmmss(1)) & seconds=FIX(hhmmss(2))
long=FLOAT(str_array(2)) & lat=FLOAT(str_array(3))
par1=FLOAT(str_array(4)) & par2=FLOAT(str_array(5))
par3=FLOAT(str_array(6)) & par4=FLOAT(str_array(7))
data[*,ii]=[year,month,day,hour,minutes,seconds, $
long,lat,par1,par2,par3,par4]
ENDFOR
END
;-------------------------------------------------
I get the following error ...
% Conflicting data structures: POLDER_DATA,<FLOAT Array[12]>.
Can anyone suggest me where I am going wrong and what my mistake was? Any solution to my problem?
Thanks in advance ...
|
|
|
Re: Error using data structures while reading ASCII file with different data types [message #87439 is a reply to message #87438] |
Mon, 03 February 2014 09:21   |
Matthew Argall
Messages: 286 Registered: October 2011
|
Senior Member |
|
|
You have an array of data structures, so you need to pass in a structure. Right now you are passing an array. Instead of this:
data[*,ii]=[year,month,day,hour,minutes,seconds, $
long,lat,par1,par2,par3,par4]
try:
data[ii].year = year
data[ii].month = month
data[ii].day = day
...
...
Or look at the Struct_Assign procedure
http://exelisvis.com/docs/STRUCT_ASSIGN.html
|
|
|
|
|
Re: Error using data structures while reading ASCII file with different data types [message #87455 is a reply to message #87448] |
Tue, 04 February 2014 06:07   |
Matthew Argall
Messages: 286 Registered: October 2011
|
Senior Member |
|
|
On Tuesday, February 4, 2014 5:12:25 AM UTC-5, Madhavan Bomidi wrote:
> Thanks Matthew ...This works!!!
You are welcome! After thinking a little more, you could also use a structure, similar to the original:
data[ii] = { YYYY:year, MM:month, DD:day, HR:hour, MI:minutes, SEC:seconds, $
LON:long, LAT:lat, P1:par1, P2:par2, P3:par3, P4:par4 }
By using square brackets, you created a numeric array and forced all of your numbers to be floats. An array of floats cannot fit into an array of structures. You can, however, use curly brackets to create a structure.
|
|
|
|