# plot for NorCPM2 analysis, which is import HadISST with EnKF

TITLE: NorCPM and ERA5 Z 200hPa
Description: Z200 correlation map
COMMENT: |
    CASE and ERA5 Z200
Tags: geopotential
THUMBNAIL: VAR_ens_trend_ANN_thumb.png

VAR: Z200 ## var name in model
COLORLVLS: |  ## for Tref
    res@cnLevels = (/-.3,.3,.5,.7,.9/)
    res@cnFillColors = (/(/225,239,223,255/), (/255,255,255,255/), (/248,246,226,255/), (/244,218,140,255/), (/241,192,45,255/), (/168,129,32,255/)/)
    res@cnFillColors := res@cnFillColors / 255.
COLORLVLS: |  ## for Z200
    res@cnLevels = (/-5.,5.,10.,15.,20./)
    res@cnFillColors = (/(/225,239,223,255/), (/255,255,255,255/), (/248,246,226,255/), (/244,218,140,255/), (/241,192,45,255/), (/168,129,32,255/)/)
    res@cnFillColors := res@cnFillColors / 255.

getEnsMean:
    plotScript: mk_runcode.sh
    IFILELIST: atm/hist/*.cam*.h0.{YEARB..YEARE}-??.nc
    AVGFILE: ens_VAR_avg.nc
    ANOFILE: ens_VAR_ano.nc
    AVGTRENDFILE: ens_VAR_trend.nc
    RUNTHESECODES: |
        ## env for cdo
        CDOENV
        ## ensemble
        dir=BASEDIR
        avgfile=AVGFILE
        anofile=ANOFILE
        trendfile=AVGTRENDFILE
        var=VAR
        test -f ${avgfile} && test -f ${anofile} && test -f ${trendfile} && exit 0
        ## loop for every member
        memdirs=$(ls -d ${dir}/*mem??)
        i=0
        for d in $memdirs; do
            ifiles=$(ls ${d}/IFILELIST)
            i=$(($i+1))
            ii=$(printf "%2.2d" $i)
            o1ts=VAR_${ii}.nc
            test -f "$o1ts" && continue
            ncrcat -O -h -v ${var} ${ifiles} ${o1ts}
        done
        for i in VAR_??.nc; do
            ## cal trend and avg
            avgf=$(echo $i | sed 's/VAR/VAR_avg/')
            trendf=$(echo $i | sed 's/VAR/VAR_trend/')
            cdo -s -O -trend ${i} $avgf $trendf
        done
        ## ensemble variables
        cdo -O -ensavg VAR_??.nc ${avgfile}
        cdo -O -ymonsub ${avgfile} -ymonmean ${avgfile} ${anofile}
        cdo -O -ensavg VAR_trend_??.nc ${trendfile}
        ## cat to 1 file
        #cdo --reduce_dim -O -merge avg_ts.nc std_ts.nc min_ts.nc max_ts.nc ${outfile}

plotTrend:
    plotScript: plot_empty.ncl
    INPUTFILE: ens_VAR_trend.nc
    INPUTVAR:  VAR
    OUTFILE: VAR_ens_trend_ANN.ps
    SEASON: ANN
    PLOTRES: 
    RUNTHESECODES: |
        begin
            dfn = "INPUTFILE"
            vn  = "INPUTVAR"
            ofig= "OUTFILE"

            df = addfile(dfn,"r")
            var = df->$vn$(0,:,:)
            var = var*120 ;; monthly trend to annual trend

            res = True
                res@cnFillOn = True
                res@cnLinesOn = False
                res@cnFillMode = "CellFill"
                res@mpCenterLonF = 210.
                res@cnLevelSelectionMode = "ExplicitLevels"
                COLORLVLS
                
                res@gsnLeftString = "Decadal trend VAR"
                res@gsnRightString = "SEASON"
                PLOTRES
            wks = gsn_open_wks("ps",ofig)
            plot = gsn_csm_contour_map(wks,var,res)
        end

Scripts:
-   varPack: getEnsMean
-   varPack: plotTrend

-  # mk html pages with figure filename
    plotScript: mk_html_page.sh
    COLUMN: >
        Trend tr
        1
    FIGFILES: 
        VAR_ens_trend_ANN
