#!/bin/csh -f
#!/bin/bash
#SBATCH -J popdiagts
#SBATCH -p ctest
#SBATCH -n 1
#SBATCH --ntasks-per-node=1
##SBATCH --account=MST108251
##SBATCH --account=MST107192
#SBATCH --account=MST111414
#SBATCH --export=NONE
#SBATCH --time=00:30:00
#SBATCH --mem=0
#SBATCH --no-requeue
#SBATCH -o %j.out
#SBATCH -e %j.err
#
#   Master script for CCSM Ocean Time Series Diagnostics
#

source /opt/ohpc/admin/lmod/lmod/init/csh
module purge
module load tools-intel19

#----------------- Turn Swift On/Off ---------------------------#
setenv SWIFT 0

#----------------- Have Swift copy ave files to hsi ------------#
setenv CPY_TO_HSI 0

#----------------- SET CASE INFO -------------------------------#
setenv CASE G_JRA55_test1_vdc013l
setenv RESOLUTION gx1v7
setenv YEAR0 306
setenv YEAR1 366 
setenv CPL 7b	 # set to 7b if CESM1.1 or higher, set to 7 if CCSM4 through CESM1.0.4, set to 6 if less than CCSM4
setenv MOC0 10.	 # y-bounds for MOC timeseries plots
setenv MOC1 35.

#----------------- SET PLOT INFO -------------------------------#
setenv DOPLOTS 1		# generate plots for flagged PM's
setenv BASEDATE       0306-01-01
setenv NINO_MON_SKIP  0
## available plot modules
setenv PM_CPLLOG 	1	# CPL log energy budget line plots
setenv PM_YPOPLOG       1	# Yeager's  POP log file line plots (new and improved)
setenv PM_HORZMN 	1	# Regional Mean T,S(z,t) w/ diff&rms from obs
setenv PM_ENSOWVLT      1	# ENSO wavelet plots
setenv PM_MOCANN 	1	# Annual mean max MOC time series
setenv PM_MOCMON  	0	# Monthly mean max MOC time series
setenv PM_GENANN 	0	# Generate annual means

#----------------- SET MSS INFO --------------------------------#
setenv TAVGFILE tavg.$YEAR0.$YEAR1.nc
setenv MSLOGREAD 1
# In Swift version, MSROOT is location of the POP monthly files
# either on HPSS or a local disk space.  Add an extra / at the 
# end if the path is local.
setenv MSROOT /work/yhtseng00/scratch/cesm2/archive
setenv MSPROJ 00000000
setenv MSRPD 3000
# NOTE! $TOOLPATH scripts assume the following mass store directory
# structure:
#     /${MSROOT}/${CASE}/ocn/hist
#     /${MSROOT}/${CASE}/ocn/logs
#     /${MSROOT}/${CASE}/ocn/proc/tavg/annual
#     /${MSROOT}/${CASE}/ocn/proc/tavg/monthly


#----------------- SET WEB PAGE INFO - -------------------------#
setenv DOWEB 1 		# generate a web page for flagged PM's 
setenv WEBDIR1 /work/yhtseng00/${CASE}
setenv WEBDIR2 ${WEBDIR1}/pdts
setenv APPEND 0		# append new plots if web page already exists
setenv GLOSSARY 0	# append a POP glossary at the bottom of web page

#----------------- SET PATH INFO -------------------------------#
#setenv DIAGROOTPATH $HOME/popdiag
setenv DIAGROOTPATH /work/yhtseng00/diagnostic_packages/popdiag_rel_20121121
setenv MODPATH ${DIAGROOTPATH}/sourcemods
setenv NCLPATH ${DIAGROOTPATH}/ncl_lib
setenv TOOLPATH ${DIAGROOTPATH}/tool_lib
setenv HTMLPATH ${DIAGROOTPATH}/html_lib
setenv WORKDIR /work/yhtseng00/DIAG_TEMP/${CASE}/pdts 
# RUNDIR LOGFILEPATH and DTFILEPATH are only used in the Swift version.  RUNDIR
# is the location of your swift script and configuration files.
# CPL/OCNLOGFILEPATH and DTFILEPATH are the locations of the log/dt files.  If they are located
# locally, set MSLOGREAD above to 0 and set CPL/OCNLOGFILEPATH and DTFILEPATH to their location.
# If the files are located on an HPSS devise, set MSLOGREAD to 1 and set 
# CPL/OCNLOGFILEPATH and DTFILEPATH to their location on the HPSS (needs full path). 
setenv RUNDIR /ptmp/${LOGNAME}/${CASE}/pdts 
setenv CPLLOGFILEPATH /full/path/to/cpl/log/files 
setenv OCNLOGFILEPATH /full/path/to/ocn/log/files
setenv DTFILEPATH /full/path/to/ocn/dt/files
setenv PATH ${MODPATH}:${TOOLPATH}:${PATH}

#----------------- SET OBS INFO -------------------------------#
setenv NINOOBSDIR /work/yhtseng00/diagnostic_packages/OBS/nino
setenv NINOOBSFILE ANOMS_1950-2000.nc
setenv TSCLIMDIR /work/yhtseng00/diagnostic_packages/OBS/phc
setenv TCLIMFILE PHC2_TEMP_${RESOLUTION}_ann_avg.nc
setenv SCLIMFILE PHC2_SALT_${RESOLUTION}_ann_avg.nc

#----------------- SET NCL INFO -------------------------------#
setenv NCARG_ROOT  /work/opt/ohpc/pkg/rcec/pkg/spack-stack/envs/unified-dev/install/intel/19.1.3.304/ncl-6.6.2-uopvvdc
#setenv NCARG_ROOT   /fs/local
setenv NCARG_COLORMAP_PATH $NCLPATH/colormaps:$NCARG_ROOT/lib/ncarg/colormaps
setenv PATH $NCARG_ROOT/bin:$PATH
###TS setenv PATH /fs/local/apps/rsi/idl/bin:/fs/local/bin:$PATH
setenv NCLCOLORTABLEFILE ${NCARG_COLORMAP_PATH}/colors1.tbl
setenv NCLCOLORTABLE 42         # Blue/White/Red
setenv NCLMINCOLOR 1
setenv NCLMAXCOLOR 255

#--------------------------------------------------------------#
#----------------- END USER DEFINED SETTINGS ------------------#
#--------------------------------------------------------------#

#----------------- EXTRA SWIFT VARIABLES ---------------------#
if ($SWIFT == 1) then
  setenv NEEDMOCTSANN 0
  setenv NEEDMOCTSMON 0
  setenv NEEDHORZMN 0
endif


if !(-d ${WORKDIR}) mkdir -p ${WORKDIR} 
cd ${WORKDIR} 
\rm -f *.pro  *.gif *.dat *.ps *.png

if ($DOWEB == 1) then
  if !(-d ${WEBDIR1}) mkdir -p -m 0775 ${WEBDIR1}
  if !(-d ${WEBDIR2}) mkdir -p -m 0775 ${WEBDIR2}
  if ($APPEND == 1) then 
    cp ${WEBDIR2}/popdiagts.html . && removefooter.csh popdiagts.html
  else
    svn info $DIAGROOTPATH > tmp
    setenv POPDIAGREV "`sed -n '/Revision/p' tmp`"
    echo "running popdiagts $POPDIAGREV"
    sed "s/CASENAME/${CASE}/g" ${HTMLPATH}/tsheader.html > tmp.html
    sed "s/REVISION/${POPDIAGREV}/g" tmp.html > popdiagts.html && \rm tmp*
  endif
endif

##==> generate a plot data file
##   which contains general plot information available
##   to any of the plot module routines via ascii read.
echo 0. 360. -90. 90. >! plot.dat
echo 1 >> plot.dat
echo 0. >> plot.dat

####################  plot modules
##==> CPL log annual mean energy budget time series plots
if ($PM_CPLLOG == 1) then
  if ($DOWEB == 1) cat ${HTMLPATH}/cpllog_timeseries.html >> popdiagts.html
  if ($DOPLOTS == 1) then
   if ($SWIFT == 0) then
    if ($MSLOGREAD == 1) then 
      echo "reading cpl log files"
      \rm -f cpl.log.*
      cp $MSROOT/$CASE/logs/cpl.log* ./
      gunzip -f cpl.log*.gz
    endif
    if ($CPL == 7b) then 
      setenv ntailht 22
      setenv ntailfw 16
      ${TOOLPATH}/process_cpl7b_logfiles_heat.awk y0=$YEAR0 y1=$YEAR1 cpl.log* >! cplheatbudget.txt
      ${TOOLPATH}/process_cpl7b_logfiles_fw.awk y0=$YEAR0 y1=$YEAR1 cpl.log* >! cplfwbudget.txt
     else
      if ($CPL == 7) then 
        setenv ntailht 21
        setenv ntailfw 16
        ${TOOLPATH}/process_cpl7_logfiles_heat.awk y0=$YEAR0 y1=$YEAR1 cpl.log* >! cplheatbudget.txt
        ${TOOLPATH}/process_cpl7_logfiles_fw.awk y0=$YEAR0 y1=$YEAR1 cpl.log* >! cplfwbudget.txt
      else
          setenv ntailht 16
        setenv ntailfw 16
        ${TOOLPATH}/process_cpl6_logfiles_heat.awk y0=$YEAR0 y1=$YEAR1 cpl.log* >! cplheatbudget.txt
        ${TOOLPATH}/process_cpl6_logfiles_fw.awk y0=$YEAR0 y1=$YEAR1 cpl.log* >! cplfwbudget.txt
      endif
    endif
    tail -${ntailfw} cplfwbudget.txt >! cplfwbudget.asc
    tail -${ntailht} cplheatbudget.txt >! cplheatbudget.asc
    endif
    addtowrapper.csh ncl cpl6_log_timeseries_heat.ncl ncl_wrap.pro 
    addtowrapper.csh ncl cpl6_log_timeseries_fw.ncl ncl_wrap.pro
  endif
endif

##==> POP log & dt file time series plots
if ($PM_YPOPLOG == 1) then
  if ($DOWEB == 1) then 
    cat ${HTMLPATH}/poplog_timeseries_yeager.html >> popdiagts.html
  endif
  if ($DOPLOTS == 1) then
   if ($SWIFT == 0) then
    if ($MSLOGREAD == 1) then 
      echo "reading pop log files"
      \rm -f ocn.log.*     
       cp $MSROOT/$CASE/logs/ocn.log* ./
       cp $MSROOT/$CASE/ocn/hist/*.dt.* ./
      gunzip -f ocn.log*.gz
    endif
    ${TOOLPATH}/process_pop2_logfiles.globaldiag.awk ocn.log.* 
    ${TOOLPATH}/process_pop2_dtfiles.awk *.dt.* 
    endif
    addtowrapper.csh ncl pop_log_diagts_3d.monthly.ncl ncl_wrap.pro
    addtowrapper.csh ncl pop_log_diagts_hflux.monthly.ncl ncl_wrap.pro
    addtowrapper.csh ncl pop_log_diagts_fwflux.monthly.ncl ncl_wrap.pro
    addtowrapper.csh ncl pop_log_diagts_cfc.monthly.ncl ncl_wrap.pro
    addtowrapper.csh ncl pop_log_diagts_nino.monthly.ncl ncl_wrap.pro
    addtowrapper.csh ncl pop_log_diagts_transports.ncl ncl_wrap.pro
    if ($SWIFT == 1) then
      addtowrapper.csh ncl pop_log_diagts_precf.ncl ncl_wrap.pro
    else
      if (-e diagts_precfactor.asc) addtowrapper.csh ncl pop_log_diagts_precf.ncl ncl_wrap.pro
    endif
  endif
endif

if ($PM_HORZMN == 1) then
  if ($DOWEB == 1) cat ${HTMLPATH}/horz_mean_timeseries.html >> popdiagts.html
  if ($DOPLOTS == 1) then
    ln -sf ${TSCLIMDIR}/${TCLIMFILE} .
    ln -sf ${TSCLIMDIR}/${SCLIMFILE} .
    set yr0 = `printf "%04d" $YEAR0`
    set yr1 = `printf "%04d" $YEAR1`
    if (! -e Sou_hor_mean_$CASE.$yr0-$yr1.nc) then
      if ($SWIFT == 0) then 
        gen_TS_meandiffrms_timeseries.csh $MSROOT $CASE $YEAR0 $YEAR1
      else
        setenv NEEDHORZMN 1
      endif
    endif
    addtowrapper.csh ncl TS_profiles_diff_plot.ncl ncl_wrap.pro
  endif
endif

if ($PM_ENSOWVLT == 1) then
  if ($DOWEB == 1) cat ${HTMLPATH}/enso_wavelet.html >> popdiagts.html
  if ($DOPLOTS == 1) then
    if (! -e diagts_nino.asc) echo "need to run POP log diagnostics"
    setenv FILE_IN diagts_nino.asc
    addtowrapper.csh ncl enso_wavelet_asc.ncl ncl_wrap.pro
  endif
else
  setenv FILE_IN "null"
endif

if ($PM_MOCANN == 1) then
  if ($DOWEB == 1) cat ${HTMLPATH}/maxmoc.html >> popdiagts.html
  if ($DOPLOTS == 1) then
    set fields = (MOC N_HEAT N_SALT transport_regions transport_components moc_components)
    set yr0 = `printf "%04d" $YEAR0`
    set yr1 = `printf "%04d" $YEAR1`
    setenv MOCTSANNFILE $CASE.pop.h.MOC.${yr0}_cat_${yr1}.nc
    if !(-e ${MOCTSANNFILE}) then
     if ($SWIFT == 0) then
       gen_pop_annual_timeseries.csh $MOCTSANNFILE $MSROOT $CASE $YEAR0 $YEAR1 $fields
     else
       setenv NEEDMOCTSANN 1
     endif
    endif
    addtowrapper.csh ncl moc_annual_timeseries.ncl ncl_wrap.pro
  endif
else
  setenv MOCTSANNFILE "null"
endif

if ($PM_MOCMON == 1) then
  if ($DOWEB == 1) cat ${HTMLPATH}/maxmoc_monthly.html >> popdiagts.html
  if ($DOPLOTS == 1) then
    set fields = (MOC N_HEAT N_SALT transport_regions transport_components moc_components)
    set yr0 = `printf "%04d" $YEAR0`
    set yr1 = `printf "%04d" $YEAR1`
    setenv MOCTSMONFILE $CASE.pop.h.MOC.${yr0}-01_cat_${yr1}-12.nc
    if !(-e ${MOCTSMONFILE}) then
      if ($SWIFT == 0) then
        gen_pop_monthly_timeseries.csh $MOCTSMONFILE $MSROOT $CASE $YEAR0 $YEAR1 $fields
      else
        setenv NEEDMOCTSMON 1
      endif
    endif
    addtowrapper.csh ncl moc_monthly_timeseries.ncl ncl_wrap.pro
  endif
else
  setenv MOCTSMONFILE "null"
endif

if ($PM_GENANN == 1) then
    echo "inside genann"
    if ($SWIFT == 0) then
      gen_pop_annmean.csh $MSROOT $CASE $YEAR0 $YEAR1
    endif
endif

################### Call Swift to compute averages and create plots in parallel or 
################### create plots in serial
if ($SWIFT == 1) then
  cd $WORKDIR

  setenv NEEDTAVG 0
  if ($NEEDMOCTSANN == 1 || $PM_GENANN == 1 || $NEEDHORZMN == 1) then
    if !(-e $TAVGFILE) setenv NEEDTAVG 1
  endif

  swift -config  $RUNDIR/cf.properties -cdm.file  $RUNDIR/fs.data \
  -sites.file  $RUNDIR/sites.xml -tc.file  $RUNDIR/tc.data $RUNDIR/popdiagts.swift -wrkdir=$WORKDIR/ \
  -msroot=$MSROOT -case=$CASE -resolution=$RESOLUTION -year0=$YEAR0 -year1=$YEAR1 \
  -ncl_wrap=$WORKDIR/"ncl_wrap.pro" -cpl=$CPL -doplots=$DOPLOTS -genann=$PM_GENANN \
  -needtavg=$NEEDTAVG -tavgfile=$TAVGFILE -moctsannfile=$MOCTSANNFILE \
  -needmoctsann=$NEEDMOCTSANN -needmoctsmon=$NEEDMOCTSMON -needhorzmn=$NEEDHORZMN -moctsmonfile=$MOCTSMONFILE \
  -ncarg_root=$NCARG_ROOT -nclpath=$NCLPATH -diagrootpath=$DIAGROOTPATH -cpy_hsi=$CPY_TO_HSI\
  -ncarg_colormap_path=$NCARG_COLORMAP_PATH -mslogread=$MSLOGREAD -pm_cpllog=$PM_CPLLOG -pm_ypoplog=$PM_YPOPLOG \
  -moc0=$MOC0 -moc1=$MOC1 -file_in=$FILE_IN -tsclimdir=$TSCLIMDIR -tclimfile=$TCLIMFILE -sclimfile=$SCLIMFILE \
  -cpllogfilepath=$CPLLOGFILEPATH -ocnlogfilepath=$OCNLOGFILEPATH -dtfilepath=$DTFILEPATH 
else


####################  Call analysis tools for plotting here
if ($DOPLOTS == 1) then

#  if (-e idl_wrap.pro) then
#    mv idl_wrap.pro tmp.pro
#    echo "@$IDLPATH/pro/set_path" >> idl_wrap.pro
#    cat tmp.pro >> idl_wrap.pro && \rm tmp.pro
#    echo "exit" >> idl_wrap.pro
#    idl idl_wrap.pro
#    ls *.ps
#    if ($status == 0) then 
#      foreach i (*.ps)
#        ps2gif $i $i.gif
#        rename.pl '.ps' '' $i.gif
#      end
##     \rm -f *.ps
#    endif
#  endif

  if (-e ncl_wrap.pro) then
    foreach i (`cat ncl_wrap.pro`)
      ncl $i
    end
    ls *.ps
    if ($status == 0) then 
      foreach i (*.ps)
        convert -trim -bordercolor white -border 5x5 -density 85 $i $i.gif
        rename.pl '.ps' '' $i.gif
      end
#      \rm -f *.ps
    endif
  endif

#  if (-e matlab_wrap.pro) then
#    matlab matlab_wrap.pro
#  endif

#  if (-e fer_wrap.pro) then
#    echo "exit" >> fer_wrap.pro
#    ${FER_DIR}/bin/ferret -gif < fer_wrap.pro
#  endif

endif
endif #end swift

####################  Finish & transfer web page and plots
if ($DOWEB == 1) then
  if ($GLOSSARY == 1) cat ${HTMLPATH}/glossary.html >> popdiagts.html
  cat ${HTMLPATH}/footer.html >> popdiagts.html
  cp popdiagts.html ${WEBDIR2}
  if ($DOPLOTS == 1) then
    cp *.asc *.gif *.png ${WEBDIR2}
  endif
endif
echo "DONE popdiagts.csh:  CASE = $CASE, YEAR0 = $YEAR0, YEAR1 = $YEAR1"
