#!/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.'

test -f PSL_JJA_trend_ensmean.nc && test -f PSL_JJA_avg_ensmean.nc && exit 0
var=PSL
season='JJA'
avgfs=''
trendfs=''
shifttime='-shifttime,-15day' ## noresm1 atm save monthly data at next month
test "$season" == 'DJF' &&  djfshift="" || djfshift="-shifttime,-2mo"  ## move to Dec. year for DJF
for i in PSL_??.nc; do
    avgf=$(echo $i | sed "s/\.nc/_${season}_avg.nc/")
    trendf=$(echo $i | sed "s/\.nc/_${season}_trend.nc/")
    test -f $avgf && test -f $trendf && continue
    cdo -s -O -trend -yearmean $djfshift -selseason,$season $shifttime $i $avgf $trendf 
    trendfs="$trendfs $trendf"
    avgfs="$avgfs $avgf"
done
cdo -s -O -ensmean -selname,$var $trendfs PSL_JJA_trend_ensmean.nc
cdo -s -O -ensmean -selname,$var $avgfs PSL_JJA_avg_ensmean.nc


