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
N = 384
r = 64
N = N/r
N = N*N
print("N = "+N)

f = addfile("data/fg2.nc","r")
fg2 = f->fg2
f = addfile("data/J.nc","r")
J = f->J

sec = new(160,float)
do i = 0,159
    ;print("time: "+(i+1))
    if(ismissing(J(i)).eq.True) then
        sec(i) = sec@_FillValue
    ;    print("J is missing !!!")
    end if
    if(ismissing(J(i)).eq.False) then
        if(J(i).ne.0.and.ismissing(fg2(i)).eq.False) then
            sec(i) = (N-J(i))*fg2(i)/J(i)
        else
            sec(i) = 0.
        end if    
    end if
end do
sec!0 = "time"
printVarSummary(sec)
printMinMax(sec,True)

fn = "data/sec.nc"
system("rm -f "+fn)
filout = addfile(fn,"c")
filout->sec = sec
end
