load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"

begin
r = 64
f = addfile("~/WORK4/CASPO/mb_jiwen_no_shallow/m"+r+"/data/dcape.nc","r")
v0 = f->dcape
printMinMax(v0,True)
nd = dimsizes(v0)
f = addfile("data/sec.nc","r")
sec = f->sec
f = addfile("data/third.nc","r")
third = f->third
printVarSummary(v0)
printVarSummary(third)
do t = 0,nd(0)-1
    do i = 0,nd(1)-1
        do j = 0,nd(2)-1
            if(ismissing(sec(t)).eq.True) then
                v0(t,:,:) = 0.
            else
                v0(t,i,j) = v0(t,i,j)+sec(t)+third(t,i,j)
            end if
        end do    
    end do
end do    
printMinMax(v0,True)

fn = "~/WORK4/CASPO/dcape2_jiwen/m"+r+"/dcape2.nc"
system("rm -f "+fn)
filout = addfile(fn,"c")
filout->dcape = v0
end
