0

I am trying to write a loop to process daily data for a whole month. My code works when manually doing 1 day at a time, but something about my loop is failing when i try to loop through every day of the month.

Basically I'm looping through n = 1,30 days to read in files, clip data to my study area, and add data from each additional day to my array of data. I want one array containing data for the whole month.

These are functions in my loop:

@mls_choosefile_co.pro --> this just gives me the 'ToOpen' file path+name

@mls_readin_co.pro --> this uses the file_id to read in the CO data and clip it to my study area. Output is 'co_sa_100', for CO in study area at 100 hpa, a 1x700 array (length varies by day, may be 706, 720, 680...)

^^ Both of these work fine on their own.

; make array of data for the whole month - CO at 100hpa
; to append each additional day to this on each loop iteration
    co_100_all = [ ] 
; make array to save how many data points per day. use -9 as a fill value to tell me when the loop has not gone over that day. 
    len = MAKE_ARRAY(1,ndays, VALUE = -9)

; the loop in question: 
    FOR n = 0,ndays DO BEGIN &$
        @mls_choosefile_co.pro &$
        file_id = H5F_OPEN(ToOpen) &$
        @mls_readin_co.pro &$
        len[n-1] = N_ELEMENTS(co_sa_100) &$
        co_100_all = [[co_100_all], [co_sa_100]] &$
    END 

I set up the 'len' variable as a test. This will tell me how many data points have been read in per day, with a value per day (usually around 700). I've set -9 as a fill value for where the loop is not operating. It should read something like: len = [702, 716, 706]. but it only ever saves the last index in the loop and I get: len = [-9, -9, 706].

Thanks in advance!

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
SofCh
  • 19
  • 2

2 Answers2

1

Update: its been fixed!

My problem was with calling the functions using @program.pro rather than in program, input, output format.

Loops through n number of days, pulls out data for the chemical named 'tracer', and saves the output for each day to the saveoutputs array.

Here's the edited code, in case it helps anyone:

FOR n = 0, ndays-1 DO BEGIN
    mls_choosefile, n, tracer, file_id
    mls_readin, file_id, tracer, output
    mls_len[n-1] = N_ELEMENTS(output)
    saveoutputs[n,*] = output
END 

Where mls_choosefile and mls_readin are set up as IDL procedures.

SofCh
  • 19
  • 2
0

I had similar problems with my IDL code too. My variable 'm' has ~17700 data points in the array and when I >print,m or >plot m,a ...only the last point was being printed or plotted.

So the alternative I tried is to save the data points on a .dat file using 'append' and then plot it.

openu,1,'m.dat',/append printf,1,m free_lun,1

plot,m,a,yrange=1,6.2],xstyle=1,ystyle=1,xtitle='Magnetic Local Time',ytitle='Voltage [volts]',position=[0.06, 0.10, 0.97, 0.95]

This worked for me. The key point is using the 'append' function.