Create a structure in IDL [message #94248] |
Mon, 06 March 2017 11:53  |
francisco.pozon
Messages: 2 Registered: March 2017
|
Junior Member |
|
|
Hello everyone:
I would like to create a structure in IDL and put the information from my ASCII file. The problem is that I have several ASCII files and always the number of columns and rows are different. For example, I have the ASCII file "data.dat" and has 50 lines and 2040 columns. I know that we can define the data structure (if we assume I have only 5 columns):
datastruct = { col1:0L, col2:0.0, col3:0.0, col4:0.0, col5:0.0}
I can read my file and then replicate the structure:
file = 'data.dat'
nrows = file_line(file) ; to estimate the number of rows
data = replicate(datastruct, nrows)
openr, lun, file, /GET_LUN
readF, lun, data
free_lun, lun
I can do: print, data.col1 or print, data.col2 and so on... but this will give me only the first 5 columns. How I can do the same but for 2040 columns and also when we don't know in advance the number of columns in the file. Thanks for your help!
|
|
|
Re: Create a structure in IDL [message #94250 is a reply to message #94248] |
Mon, 06 March 2017 16:00   |
wlandsman
Messages: 743 Registered: June 2000
|
Senior Member |
|
|
You can make repeated calls to the intrinsic CREATE_STRUCT() function to build up your structure. For example, to make a structure with 10 tags of scalar floats
IDL> name = 'col' + strtrim(indgen(10),2)
IDL> struct = create_struct(name[0],0.)
IDL> for i=1,9 do struct= create_struct(temporary(struct),name[i],0.0)
IDL> help,/str,struct
** Structure <23013f8>, 10 tags, length=40, data length=40, refs=1:
COL0 FLOAT 0.00000
COL1 FLOAT 0.00000
COL2 FLOAT 0.00000
COL3 FLOAT 0.00000
COL4 FLOAT 0.00000
COL5 FLOAT 0.00000
COL6 FLOAT 0.00000
COL7 FLOAT 0.00000
COL8 FLOAT 0.00000
COL9 FLOAT 0.00000
You can also use https://idlastro.gsfc.nasa.gov/ftp/pro/structure/mrd_struct. pro
which is a wrapper for building a structure this way, e.g.
IDL> str = mrd_struct(name,replicate('0.',10),1)
IDL> help,/str,str
** Structure <2216d78>, 10 tags, length=40, data length=40, refs=1:
COL0 FLOAT 0.00000
COL1 FLOAT 0.00000
COL2 FLOAT 0.00000
COL3 FLOAT 0.00000
COL4 FLOAT 0.00000
COL5 FLOAT 0.00000
COL6 FLOAT 0.00000
COL7 FLOAT 0.00000
COL8 FLOAT 0.00000
COL9 FLOAT 0.00000
--Wayne
On Monday, March 6, 2017 at 2:53:55 PM UTC-5, Francisco wrote:
> Hello everyone:
>
> I would like to create a structure in IDL and put the information from my ASCII file. The problem is that I have several ASCII files and always the number of columns and rows are different. For example, I have the ASCII file "data.dat" and has 50 lines and 2040 columns. I know that we can define the data structure (if we assume I have only 5 columns):
>
> datastruct = { col1:0L, col2:0.0, col3:0.0, col4:0.0, col5:0.0}
>
> I can read my file and then replicate the structure:
>
> file = 'data.dat'
> nrows = file_line(file) ; to estimate the number of rows
> data = replicate(datastruct, nrows)
> openr, lun, file, /GET_LUN
> readF, lun, data
> free_lun, lun
>
> I can do: print, data.col1 or print, data.col2 and so on... but this will give me only the first 5 columns. How I can do the same but for 2040 columns and also when we don't know in advance the number of columns in the file. Thanks for your help!
|
|
|
|