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

TITLE: NorCPM and ERA5 T2m
Description: T2m correlation map
COMMENT: |
    CASE and ERA5 T2m
Tags: temperature
THUMBNAIL: TREFHT_ens_corr_ANN_thumb.png
thumb: TREFHT_ens_corr_ANN_thumb.png

OVAR: t2m  ## obsvar in ERA5
VAR: TREFHT ## var name in model

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}

getERA5andCOR:
    plotScript: mk_runcode.sh
    #IFILELIST: /lfs/archive/Reanalysis/ERA5/SFC/mon/OVAR/ERA5_SFC_OVAR_{YEARB..YEARE}??_r1440x721_mon.nc
    #IFILELIST: /projects/NS9039K/shared/ERA5/monthly/OVAR/OVAR.{YEARB..YEARE}.nc
    #IFILELIST: /projects/NS9039K/shared/ERA5/monthly/ts_msl/ts_msl_monthly_{YEARB..YEARE}.nc
    IFILELIST: /cluster/shared/noresm/norcpm/Obs/AIR_TEM/ERA5/T2M/t2m.{YEARB..YEARE}.nc
    OUTFILE: era5_OVAR_cor_ANN.nc
    MODFILE: ens_VAR_ano.nc
    RUNTHESECODES: |
        ## env for cdo
        CDOENV
        #
        outfile=OUTFILE
        modfile=MODFILE
        var=OVAR
        outDJF=${outfile%_ANN.*}_DJF.nc
        outMAM=${outfile%_ANN.*}_MAM.nc
        outJJA=${outfile%_ANN.*}_JJA.nc
        outSON=${outfile%_ANN.*}_SON.nc
        test -f ${outfile} && exit 0
        ## loop for every member
        ifiles=$(ls IFILELIST)
        cdo -O    -b F32         \
            -select,name=${var} \
            ${ifiles} \
            ERA5_OVAR_YEARB-YEARE.nc
            
        # correlation, need regrid
        cdo  -O  \
            -ymonsub ERA5_OVAR_YEARB-YEARE.nc -ymonmean ERA5_OVAR_YEARB-YEARE.nc  \
            ERA5_OVAR_ano_YEARB-YEARE.nc
        ## Somehow remap is easily crash, should be run along
        cdo  -O  \
            -remapbic,${modfile} \
            ERA5_OVAR_ano_YEARB-YEARE.nc \
            tmp2.nc
        cdo  -O -timcor    \
            -selvar,OVAR tmp2.nc    \
            -select,name=VAR ${modfile} \
            ${outfile}
        cdo  -O -timcor    \
            -selseason,DJF -selvar,OVAR tmp2.nc    \
            -selseason,DJF -select,name=VAR ${modfile} \
            ${outDJF}
        cdo  -O -timcor    \
            -selseason,MAM -selvar,OVAR tmp2.nc    \
            -selseason,MAM -select,name=VAR ${modfile} \
            ${outMAM}
        cdo  -O -timcor    \
            -selseason,JJA -selvar,OVAR tmp2.nc    \
            -selseason,JJA -select,name=VAR ${modfile} \
            ${outJJA}
        cdo  -O -timcor    \
            -selseason,SON -selvar,OVAR tmp2.nc    \
            -selseason,SON -select,name=VAR ${modfile} \
            ${outSON}
        rm -f ERA5_OVAR_ano_YEARB-YEARE.nc tmp2.nc

plotCor:
    plotScript: plot_empty.ncl
    INPUTFILE: era5_OVAR_cor_SEASON.nc
    INPUTVAR:  OVAR
    OUTFILE: VAR_ens_corr_SEASON.ps
    SEASON: ANN
    PLOTRES: 
    RUNTHESECODES: |
        load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"
        begin
            dfn = "INPUTFILE"
            vn  = "INPUTVAR"
            ofig= "OUTFILE"

            df = addfile(dfn,"r")
            var = df->$vn$(0,:,:)

            res = True
                res@cnFillOn = True
                res@cnLinesOn = False
                res@cnFillMode = "CellFill"
                res@mpCenterLonF = 210.
                res@cnLevelSelectionMode = "ExplicitLevels"
                res@cnLevels = fspan(-1,1,21) ;;(/-.9,-.8,-.7,-.6,-.5,.5,.6,.7,.8,.9/)
                res@cnFillPalette = "BlAqGrWh2YeOrReVi22"
                res@gsnLeftString = "Corr. OVAR"
                res@gsnRightString = "SEASON"
                PLOTRES
            wks = gsn_open_wks("ps",ofig)
            plot = gsn_csm_contour_map(wks,var,res)
        end
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"
                res@cnLevels = (/-.3,.3,.5,.7,.9/)
                ;res@cnFillPalette = ""
                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.
                
                res@gsnLeftString = "Decadal trend OVAR"
                res@gsnRightString = "SEASON"
                PLOTRES
            wks = gsn_open_wks("ps",ofig)
            plot = gsn_csm_contour_map(wks,var,res)
        end

plotCorDJF:
    SEASON: DJF
plotCorMAM:
    SEASON: MAM
plotCorJJA:
    SEASON: JJA
plotCorSON:
    SEASON: SON

RegWNP: 
    PLOTRES: |
        res@mpMinLatF =   0.
        res@mpMaxLatF =  60.
        res@mpMinLonF = 110.
        res@mpMaxLonF = 180.
        res@mpCenterLonF = 145.
    OUTFILE: VAR_ens_corr_REG_SEASON.ps
    REG: WNP
    
Scripts:
-   varPack: getEnsMean
-   varPack: getERA5andCOR
-   varPack: plotTrend
-   varPack: plotCor
-   varPack: plotCor, plotCorDJF
-   varPack: plotCor, plotCorMAM
-   varPack: plotCor, plotCorJJA
-   varPack: plotCor, plotCorSON
-   varPack: plotCor, RegWNP
-   varPack: plotCor, plotCorDJF, RegWNP
-   varPack: plotCor, plotCorMAM, RegWNP
-   varPack: plotCor, plotCorJJA, RegWNP
-   varPack: plotCor, plotCorSON, RegWNP

-  # mk html pages with figure filename
    plotScript: mk_html_page.sh
    COLUMN: >
        Annual tr
        1
        DJF MAM JJA SON tr
        4 
        1
        1
        4
    FIGFILES: 
        VAR_ens_corr_ANN
        VAR_ens_corr_DJF
        VAR_ens_corr_MAM
        VAR_ens_corr_JJA
        VAR_ens_corr_SON

        VAR_ens_trend_ANN

        VAR_ens_corr_WNP_ANN
        VAR_ens_corr_WNP_DJF
        VAR_ens_corr_WNP_MAM
        VAR_ens_corr_WNP_JJA
        VAR_ens_corr_WNP_SON
