Automatically Read Array Size into Idl

I am trying to automate a code in IDL that takes a few .dat files created and reads them into an array. Each of these files has 4 columns but a different number of rows, and I am not sure how to set it such that it automatically fits the array to the number of rows each time.

openr, lun, +field+'_'+color+'.dat', /get_lun
array = fltarr(4, 380)

readf, lun, array
outlier=array[0,*]
mag=array[1,*]
ra=array[2,*]
dec=array[3,*]
position2 = [outlier, mag, ra, dec]
free_lun, lun
return, position2

This is the current block of code I am using, and I have just been changing the array value per file, which has become tedious, and it won't work if the array value is set too high.

This next part is something I am curious about but it's managed to be worked around mostly okay, but I would still like to ask if it could be reworked similar to the other code:

I also have a similar code that created the original files im reading (sorry I know it's a little messy I am very new to IDL):

i=0
position = []
this=[]
OpenW, lun, +field+'_'+color+'.dat', /get_lun
while i lt 16000 do begin       ;typically 16798
outlier = plot_data[0,i]
mag=plot_data[1,i]
ra = plot_data[2,i]
dec=plot_data[3,i]
if outlier lt 0.0 then begin
PrintF, lun, outlier, mag, ra, dec

                                ;
endif

position=[outlier, mag, ra, dec]

i=i+1
endwhile

... which reads a large .sav file and converts it into a 4-column array so I can create the new file with the outliers I use for the previous code. I'm not sure how to open or read a .sav file so I was using an old code from the person who worked on the project before me, but the size of 16798 doesn't work for every single .sav file so I reduced it, but I do want to see if I can fix it so I'm not excluding ~700 points from all other data to accommodate a small number of other files. Is there a way to use a similar code that would fix the first code here?

Sorry, know it's a lot of questions and any help would be greatly appreciated!

1 Answer

For text files, you can use FILE_LINES:

filename = field+'_'+color+'.dat'
n_lines = file_lines(filename)
openr, lun, filename, /get_lun
array = fltarr(4, n_lines)

and then the rest of your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest