Print calculations from for loop on same line idl

498 views Asked by At

I am trying to print numbers calculated from an netcdf file onto the same line after going through a for loop. However, for every iteration of the loop the program prints out every step. Moving the print statement outside of the for loop doesn't work either. How can I just print the last line of the for loop data?

Here is the relevant code:

qT = where(IN eq 10 and SC eq 'T1')
if n_elements(qT) eq 1 and qT(0) ne -1 then begin
    cTval = MR(qT(0))-MRA(qT(0))
    cT = string(cTval,format='(F0.2)')
    if cTval le lowlim_cT or cTval ge uplim_cT then begin
    print, 'T1: ' + cT + "*************** RESIDUALS ARE OUT OF LIMIT " + strtrim(string(uplim_cT),2)
    endif else begin
    print, 'T1: ' + cT
    endelse
endif 
if n_elements(qT) eq 1 and qT(0) eq -1 then begin print, 'QT ERROR' + '$'
endif 
if n_elements(qT) gt 1 then begin
cT = strarr(n_elements(qT))
cTval = fltarr(n_elements(qT))
for h = 0, n_elements(qT)-1 do begin
    cTval(h)= MR(qT(h))-MRA(qT(h))
    cT(h) = string(cTval(h),format='(F0.2)')
    if cTval(h) lt lowlim_cT or cTval(h) gt uplim_cT then begin
    print, 'T1: ' + cT + "*************** RESIDUALS ARE OUT OF LIMIT " + strtrim(string(uplim_cT),2)
    endif else begin
    print, 'T1: ' + cT
    endelse
endfor
endif

With the following output:

T1: -59.67*************** RESIDUALS ARE OUT OF LIMIT 10 T1: *************** RESIDUALS ARE OUT OF LIMIT 10 T1: *************** RESIDUALS ARE OUT OF LIMIT 10
T1: -59.67*************** RESIDUALS ARE OUT OF LIMIT 10 T1: -65.91*************** RESIDUALS ARE OUT OF LIMIT 10 T1: *************** RESIDUALS ARE OUT OF LIMIT 10
T1: -59.67*************** RESIDUALS ARE OUT OF LIMIT 10 T1: -65.91*************** RESIDUALS ARE OUT OF LIMIT 10 T1: -48.13*************** RESIDUALS ARE OUT OF LIMIT 10

How do I only print out that last line? Thank you!

2

There are 2 answers

0
manateejoe On BEST ANSWER

I ended up setting a boolean variable and working from there. Here is the working code:

qT = where(IN eq 10 and SC eq 'T1')
if n_elements(qT) eq 1 and qT(0) ne -1 then begin
    cTval = MR(qT(0))-MRA(qT(0))
    cT = string(cTval,format='(F0.2)')
    if cTval lt lowlim_cT or cTval gt uplim_cT then begin
    print, 'CO_T1: ' + cT + "*************** RESIDUAL IS OUT OF LIMIT " + strtrim(string(uplim_cT),2)
    endif else begin
    print, 'CO_T1: ' + cT
    endelse
endif 
if n_elements(qT) eq 1 and qT(0) eq -1 then begin print, 'QT ERROR' + '$'
endif 
if n_elements(qT) gt 1 then begin
cT = strarr(n_elements(qT))
cTval = fltarr(n_elements(qT))
cTout = fltarr(n_elements(qT))
cts="CO_T1: "
for h = 0, n_elements(qT)-1 do begin
    cTval(h)= MR(qT(h))-MRA(qT(h))
    cT(h) = string(cTval(h),format='(F0.2)')
    if h ne n_elements(qT) - 1 then begin 
      cts = cts+ct(h)+", "
    endif else begin
      cts = cts+ct(h) + " "
    endelse

    if cTval(h) lt lowlim_cT or cTval(h) gt uplim_cT then begin
    cTout(h) = 1
    endif
endfor
numerr = strtrim(string(total(ctout)),2)
if max(cTout) ne 0 then begin cts=cts + "***************" + ' ' + numerr + ' RESIDUAL(S) ARE OUT OF LIMIT ' + strtrim(string(uplim_cT),2) + '$'
endif
print, cts
endif
0
mgalloy On

How about instead of printing, just save the line to a string variable. Just reassign over it if the condition is true again. Print the string at the end. This might affect performance, so you could save h instead and reconstruct the string instead of the other method is too slow.