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

时间:2015-06-10 19:20:14

标签: for-loop netcdf idl

我试图在通过for循环后将从netcdf文件计算的数字打印到同一行。但是,对于循环的每次迭代,程序都会打印出每一步。在for循环之外移动print语句也不起作用。我怎样才能打印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

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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