在同一行 idl 上打印 for 循环的计算

Print calculations from for loop on same line idl

我正在尝试在经历 for 循环后将从 netcdf 文件计算的数字打印到同一行。但是,对于循环的每次迭代,程序都会打印出每一步。将 print 语句移到 for 循环之外也不起作用。如何只打印 for 循环数据的最后一行?

相关代码如下:

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

具有以下输出:

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

如何只打印最后一行?谢谢!

如何不打印,只将行保存到字符串变量。如果条件再次为真,只需重新分配它。打印最后的字符串。这可能会影响性能,因此您可以保存 h 而不是重建字符串,而不是其他方法太慢。

我最终设置了一个布尔变量并从那里开始工作。这是工作代码:

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