0

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!

Lorz.Astro
  • 23
  • 7
  • What kind of data are these ? Numeric ? Mixed types ? Do they have a header ? – lbusett Mar 06 '17 at 23:18
  • Yes numeric. 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 – Lorz.Astro Mar 07 '17 at 09:04
  • So each file has 2n+1 records: one for date and n each for fluxes and errors, right? – lbusett Mar 07 '17 at 11:44
  • Also, do you have a particular reason for storing in a structure and not in a matrix? – lbusett Mar 07 '17 at 11:46
  • yes each file has 2n+1 records. I thought a good way will be using structures because I was working with structures before. I don't have experience with matrix. – Lorz.Astro Mar 07 '17 at 12:14
  • Could you share an input file? Would be Easier that way... – lbusett Mar 07 '17 at 12:39
  • Yes, here is the link to the real file: https://www.dropbox.com/s/z164g7yyd76zpjo/phot.avg.1.0?dl=0 The real file has 3 columns that they are always there: Days Airmass Filter and the other columns are Flux1 Mag1 Err1 for star1 and so on.... Flux2 Mag2 Err2.... So, the real file has 3n+3 records. I just defined an easy description assuming that will be just the same for something more complex. Thanks a lot for the interest! – Lorz.Astro Mar 07 '17 at 13:57

1 Answers1

1

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
lbusett
  • 5,801
  • 2
  • 24
  • 47
  • Wonderful!! It is very fast and works perfect! Once I have time I will try to figure it out the same but with structures. Thanks a lot for your help. – Lorz.Astro Mar 07 '17 at 16:14
  • 1
    I don't know if it's directly possible with structures, since I think you should know the "size" beforehand. You could however easily create a structure for each star through a for loop, and then put them together in a single array of structures using structure manipulation routines (http://www.sos.siena.edu/~jmoustakas/idl_tutorials/tutorial_structures.html) – lbusett Mar 07 '17 at 16:50