#!/bin/bash

## Just run code in recipes, use it carefully
## example: use cdo to make cache file. Since cdo is quicker than ncl.
#;DIAG_NORCPM; RUNTHESECODES: echo '    no codes run here.'

a='1'
while [ ! -z "${a}" ] ; do
a=$(ncl -Q << EOF
load "/nird/home/pgchiu/scratch/diag_norcpm/Codes//func_read_all_members.ncl"
begin
forecastdirs=systemfunc("ls -d /cluster/shared/NS9039K/norcpm_ana_hindcast/archive//norcpm_ana_f09_tn14_decadal_hindcast_{1,2}???10??|sort")
varname = "sst"
ocngridfile = "/cluster/shared/noresm/inputdata/ocn/blom/grid/grid_tnx1v4_20170622.nc"
component = "ocn"
cacheprefix = "fig_cor_forecast_stg.nc"
plotmonths = 119
latbe = (/20.,40./)
lonbe = (/-70.,-20./)

do m = 0, plotmonths
moncachefn = cacheprefix+"_mon_"+sprinti("%3.3d",m)+".nc"
if(isfilepresent(moncachefn))then
continue
end if
print("make cache:"+moncachefn)
wcS = toint(systemfunc("date +%s"))
var1m = read_noresm_forecasts_ens_latlon_leadmonths(forecastdirs,component,varname,m,latbe,lonbe,ocngridfile)
f = addfile(moncachefn,"c")
f->var1m = var1m
wcE = toint(systemfunc("date +%s"))
print( "make cache in "+(wcE-wcS)+" seconds.")
exit
end do
end
EOF
)
echo $a
if [ ! -z "$(echo $a | grep -v 'make cache')" ] ; then
exit
fi
done


