Search code examples
idl-programming-language

Create a structure in IDL


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.

The real data file contains fluxes of several stars observed in different days with respective errors. The table has does not have a header.

Days Flux1 Err1 Flux2 Err2 Flux3 Err3..............Flux2040 Err2040

Thanks for your help!


Solution

  • If a numeric matrix is ok for you instrad than a structure, since you have properly formatted ASCII files, an easy solution would be to just use read_ascii:

    infile = "C:\Users\LB_laptop\Downloads\phot.avg.1.0"
    data = read_ascii(infile)
    data = data.FIELD001
    

    this gives you a numeric matrix, easy to deal with. For example:

    IDL> data[0:5,0:10]
           2457454.3       1.6190000             NaN      0.52709073       25.695290      0.20502000
           2457455.3       1.8770000             NaN      0.14101060       27.126869      0.71034002
           2457499.5       1.2810000             NaN      0.63232613       25.497650      0.17557999
           2457500.3       1.5319999             NaN      0.41562101       25.953260      0.25950000
           2457519.5       1.3420000             NaN      0.38020891       26.049940      0.28159001
           2457525.3       1.2880000             NaN      0.29697639       26.318199      0.35189000
           2457528.3       1.3510000             NaN      0.41356701       25.958639      0.26128000
           2457529.3       1.3300000             NaN      0.36875120       26.083170      0.28975999
           2457530.3       1.3400000             NaN      0.41647339       25.951031      0.25999999
           2457533.3       1.3120000             NaN      0.33893269       26.174721      0.19237000
           2457534.3       1.2800000             NaN      0.38690910       26.030979      0.15137000