# plot for NorCPM analysis change

TITLE: NorCPM nudge time series
Description: NorCPM analysis mean change
COMMENT: |
    NorCPM CASE nudge
THUMBNAIL: nino34_temp_nug_ts_thumb.png
Tags: 

ocnGridDes:
    plotScript: mk_runcode.sh
    RUNTHESECODES: |
        #!/bin/bash
        CDOENV
        gridfile='OCNGRIDFILE'
        griddes=griddes.txt
        ## for grid file of BLOM/MICOM
        xsize=$(ncdump -h $gridfile | grep 'x =' | sed 's/[ \t]*x = \([0-9]*\) .*/\1/')
        ysize=$(ncdump -h $gridfile | grep 'y =' | sed 's/[ \t]*y = \([0-9]*\) .*/\1/')
        gridsize=$(($xsize * $ysize))
        ncdump -v plon $gridfile | sed -e'1,/data:/d' -e's/^.*=//' -e's/;//' -e's/}//'> plon.txt
        ncdump -v plat $gridfile | sed -e'1,/data:/d' -e's/^.*=//' -e's/;//' -e's/}//'> plat.txt
        ncdump -v pclon $gridfile | sed -e'1,/data:/d' -e's/^.*=//' -e's/;//' -e's/}//'> pclon.txt
        ncdump -v pclat $gridfile | sed -e'1,/data:/d' -e's/^.*=//' -e's/;//' -e's/}//'> pclat.txt

        echo 'gridtype = unstructured'   > $griddes
        echo "gridsize = $gridsize"  >> $griddes
        echo "xsize    = $xsize " >> $griddes
        echo "ysize    = $ysize " >> $griddes
        echo "xvals    = " >> $griddes
        cat plon.txt >> $griddes
        echo "yvals    = " >> $griddes
        cat plat.txt >> $griddes

        echo 'nvertex  = 4' >> $griddes
        echo 'xbounds  = ' >> $griddes
        cat pclon.txt >> $griddes
        echo 'ybounds  = ' >> $griddes
        cat pclat.txt >> $griddes

calTS:
    plotScript: mk_runcode.sh
    IFILELIST: RESULT/{YEARB..YEARE}_{01..12}/
    LEVEL: 1
    RUNTHESECODES: |
        ## env for cdo
        CDOENV

        dir=BASEDIR
        var=VAR
        lonlatbox=LONLATBOX
        level=LEVEL
        
        gridfile=$(find $dir -name \*pop.h.\*|head -n1)
        griddes=griddes.txt
        ## make griddes first
        #cdo griddes2 $gridfile > $griddes
        #sed -i -e'1,5d' $griddes

        outfile="OUTANALYSIS"
        if [ ! -f "$outfile" ]; then
            ifiles=$(ls $dir/IFILELIST/analysis_avg_1.nc)
            cdo -s -O         \
                -fldmean  \
                -setctomiss,nan \
                -sellonlatbox,${lonlatbox}  \
                -setgrid,$griddes \
                -select,name=${var},level=${level} \
                ${ifiles} \
                ${outfile}
        fi

        outfile="OUTFORECAST"
        if [ ! -f "$outfile" ]; then
            ifiles=$(ls $dir/IFILELIST/forecast_avg_1.nc)
            cdo -s  -O       \
                -fldmean  \
                -setctomiss,nan \
                -sellonlatbox,${lonlatbox}  \
                -setgrid,$griddes \
                -select,name=${var},level=${level} \
                ${ifiles} \
                ${outfile}
        fi

    OUTANALYSIS: REG_VAR_ana_ts.nc
    OUTFORECAST: REG_VAR_fca_ts.nc

plotTS:
    plotScript: plot_empty.ncl
    ANALYSISFILE: REG_VAR_ana_ts.nc
    FORECASTFILE: REG_VAR_fca_ts.nc
    VAR: temp
    RUNTHESECODES: |
        load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"
        begin
            fana = addfile("ANALYSISFILE","r")
            ffca = addfile("FORECASTFILE","r")
            dif = ffca->VAR - fana->VAR 
            dif := dif(:,0,0,0)

            wks = gsn_open_wks("ps","REG_VAR_nug_ts")
            plotres = True
                plotres@tiMainString = "REG VAR nug. ts"
                plotres@gsnYRefLine = 0.
            lineres = True
            plot1 = gsn_csm_xy(wks,ispan(1,dimsizes(dif),1),dif,plotres)
            ;gsn_polyline(wks,plot1,nt,oa,lineres)

            ;; legend
            lgres = True
                lgres@lgLineLabelStrings = (/"",""/)
                lgres@vpWidthF  = .2
                lgres@vpHeightF = .1
                lgres@lgPerimOn = False
                lgres@lgJustification = "TopLeft"
                lgres@lgLineColors =       (/"green","black"/)
                lgres@lgDashIndexes = (/0,0/)
            ;gsn_legend_ndc(wks,2,(/"Obs","Model ens"/),.6,.8,lgres)
            ;drawNDCGrid(wks)
            ;draw(plot1)
            ;draw(plot2)
            ;frame(wks)
        end

calCli:
    plotScript: mk_runcode.sh
    MVAR: TS
    OVAR: sst
    MODELCLI: model_MVAR_cli.nc
    OBSCLI: obs_OVAR_cli.nc
    CLIDIF: dif_sst.nc
    RUNTHESECODES: |
        ## make model and HadISST2 climatology
        ## env for cdo
        CDOENV
        
        ## make model climatology
        ofn=MODELCLI
        if [ ! -f $ofn ];then
            #filelist=$(ls BASEDIR/*/atm/hist/*.h0.{YEARB..YEARE}-??.nc | sort )
            #echo $filelist | ncrcat -v MVAR -o ${ofn}_tmp.nc
            #ls BASEDIR/*/atm/hist/*.h0.{YEARB..YEARE}-??.nc | sort | ncrcat -O -v MVAR -o ${ofn}_tmp.nc
            find  BASEDIR/*/atm/hist/ -name \*.h0.????-??.nc | grep -e{YEARB..YEARE}-...nc | sort | ncrcat -H -O -v MVAR -o ${ofn}_tmp.nc
            cdo -s -ymonmean -selname,MVAR ${ofn}_tmp.nc $ofn 
        fi
        ## make HadISST mean climatology
        ofn=OBSCLI
        if [ ! -f $ofn ];then
            ##filelist=$(ls ~/work/HadISST_2.1.0.0/*.nc)
            ##cdo -s -ymonmean -vertavg -select,name=OVAR $filelist $ofn 
            filelist=$(ls OBSSSTFILE)
            cdo -s -ymonmean -setattribute,OVAR@units="degC" -addc,273.15 -setmissval,-1000 -selyear,YEARB/YEARE -select,name=OVAR $filelist $ofn 
        fi
        ## diff
        ofn=CLIDIF
        if [ ! -f $ofn ];then
            cdo -sub -selvar,MVAR MODELCLI -remapbic,MODELCLI -selvar,OVAR OBSCLI $ofn
        fi
        
plotNCfile:
    plotScript: plot_empty.ncl
    NCFILE: dif_sst.nc
    VAR: TS
    PLOTRES: |
        res@cnFillOn = True
        res@cnLinesOn = False
        res@cnFillPalette = "hotcold_18lev"
        res@cnLevels = (/-5.,-4.,-3.,-2.,-1.,0.,1.,2.,3.,4.,5./)
        res@cnLevelSelectionMode = "ExplicitLevels"
    OUTFIG: sst_bias
    RUNTHESECODES: |
        ;; plot variable in nc file
        begin
            f = addfile("NCFILE","r")
            var12 = f->VAR

            var = dim_avg_n_Wrap(var12,0)
            res = True
                res@mpCenterLonF = 210.
                PLOTRES
            do m = 0,11
                ofn = "OUTFIG_"+sprinti("%2.2d",m+1)
                wks = gsn_open_wks("ps",ofn)
                plot = gsn_csm_contour_map_ce(wks,var12(m,:,:),res)
            end do
        end

plotBox:
    plotScript: plot_box_on_map.ncl
    LONLATBOX: 
    OUTFILE: box_REG
    PLOTRES: 

RegNino34:
    LONLATBOX: -150,-90,-5,5
    REG: nino34
RegNAtl:
    LONLATBOX: -60,-20,10,60
    REG: natl
RegNPac:
    #LONLATBOX: 120,200,10,60
    LONLATBOX: 120,200,10,30
    REG: npac

varSALT:
    VAR: saln
varTEMP:
    VAR: temp

Scripts:
-   varPack: ocnGridDes
-   varPack: calCli
-   varPack: plotNCfile
-   varPack: plotBox, RegNino34
-   varPack: plotBox, RegNPac
-   varPack: plotBox, RegNAtl
-   varPack: calTS, varTEMP, RegNino34
-   varPack: calTS, varTEMP, RegNPac
-   varPack: calTS, varTEMP, RegNAtl
-   varPack: plotTS, varTEMP, RegNino34
-   varPack: plotTS, varTEMP, RegNPac
-   varPack: plotTS, varTEMP, RegNAtl
-   varPack: calTS, varSALT, RegNino34
-   varPack: calTS, varSALT, RegNPac
-   varPack: calTS, varSALT, RegNAtl
-   varPack: plotTS, varSALT, RegNino34
-   varPack: plotTS, varSALT, RegNPac
-   varPack: plotTS, varSALT, RegNAtl

-  # mk html pages with figure filename
    plotScript: mk_html_page.sh
    COLUMN: >
        NINO34        NPac       NAtl    tr
        3  
        3
        3
        Model\ bias\(TS-SST\)YEARB-YEARE 
        tr
        Dec Jan Feb tr
        3
        Mar Apr May tr
        3
        Jun Jul Aug tr
        3
        Sep Oct Nov tr
        3
    FIGFILES: 
         box_nino34             box_npac             box_natl  
         nino34_temp_nug_ts npac_temp_nug_ts natl_temp_nug_ts
         nino34_saln_nug_ts npac_saln_nug_ts natl_saln_nug_ts
         sst_bias_12 sst_bias_01 sst_bias_02
         sst_bias_03 sst_bias_04 sst_bias_05
         sst_bias_06 sst_bias_07 sst_bias_08
         sst_bias_09 sst_bias_10 sst_bias_11

