#!/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 BASEDIR/RUNPRE{1,2}???10??|sort")
varname = "PRECT"
ocngridfile = "/cluster/shared/noresm/inputdata/ocn/blom/grid/grid_tnx1v4_20170622.nc"
component = "atm"
title = "TITLE"
cacheprefix = "ts_PRECT_nino34"
yearbe = (/1960,2018/)
years = ispan(min(yearbe),max(yearbe),1)
months = (/1,2,3,4,5,6,7,8,9,10,11,12/)
if(any(months.eq.0))then
    months := ispan(1,12,1)
end if
latbe = (/-5.,5./)
lonbe = (/-150.,-90./)

nfcast = dimsizes(forecastdirs)
do i = 0, nfcast -1
forecache = cacheprefix+"_f"+sprinti("%2.2d",i)+".nc"
if(isfilepresent(forecache))then
continue
end if
print("make cache:"+forecache)
wcS = toint(systemfunc("date +%s"))
ts = read_norcpm_forecast_members_var_ts_season_latbe_lonbe_yearly(forecastdirs(i),component,varname,months,latbe,lonbe,ocngridfile) ;; yearly ts
f = addfile(forecache,"c")
f->ts = ts
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


