#!/bin/csh -f
#!/bin/bash
#SBATCH -J popdiag
#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
#
#   Master script for CCSM Ocean Time-Mean & Seasonal Diagnostics
#

#----------------- 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_arc_vdc013
setenv RESOLUTION gx1v7
setenv YEAR0 44 
setenv YEAR1 61
setenv CPL 7       #set to 7 if CCSM4 or higher, set to 6 otherwise

#----------------- SET PLOT INFO -------------------------------#
setenv DOPLOTS 1		# generate plots for flagged PM's
set xyrange  = (30 390 -90 90)		# x0,x1,y0,y1
set depths = (0 50 100 200 300 500 1000 1500 2000 2500 3000 3500 4000 )
setenv CNTRLVLS		std	# contour level options (gokhan, gauss)
## available plot modules
setenv PM_SFC2D 	1	# 2D surface fluxes 
setenv PM_FLD2D 	1	# all other 2D fields (HMXL, SSH, BSF, etc)
setenv PM_FLD3DZA 	1	# 3D field zonal average plots
setenv PM_MOC	 	1	# MOC, HT&FW transports
setenv PM_WBC	 	1	# WBC and DWBC diagnostics
setenv PM_SEAS	 	1	# Seasonal cycle plots
setenv PM_MLD           1       # mixed layer depth
setenv PM_TSZ	 	1	# T&S on depth surfaces, contours
setenv PM_PASSIVEZ 	1	# Passive tracers on depth surfaces, contours
setenv PM_VELZ	 	1	# UVEL,VVEL,WVEL on depth surfaces, contours
setenv PM_VELISOPZ	1	# Bolus velocities on depth surfaces, contours
setenv PM_KAPPAZ	1	# Diffusion Coeffs on depth surfaces, contours
setenv PM_UOEQ	 	1	# Upperocean T,S,PD,U @ Equator
setenv PM_VECV	 	1	# Vector Velocity w/ magnitude contours
setenv PM_POLARTS	1	# Polar Plots of T & S at depth
setenv PM_BASINAVGTS	1	# Depth Profiles of Basin-average T & S 
setenv PM_REGIONALTS	1	# Depth Profiles of regional mean T & S anom/stddev


#----------------- SET MSS INFO --------------------------------#
setenv TAVGFILE tavg.$YEAR0.$YEAR1.nc
# 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 MSROOT /data3/yiwen/cesm2/EXP_POP/
setenv MSPROJ 00000000
setenv MSRPD 3000
# NOTE! $TOOLPATH scripts assume the following mass store directory
# structure:
#     /${MSROOT}/${CASE}/ocn/hist
#     /${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}/pd.${YEAR0}_${YEAR1}
setenv APPEND 0	        # append new plots if web page already exists
setenv GLOSSARY 1	# append a POP glossary at the bottom of web page

#----------------- SET PATH INFO -------------------------------#
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}/popdiag 
# RUNDIR needed to run swift version of the script.  This is where you set up
# top level script, swift script, and swift configuration files.
setenv RUNDIR /glade/scratch/${LOGNAME}/${CASE}/popdiag 
setenv PATH ${MODPATH}:${TOOLPATH}:${PATH}:/fs/local/bin

#----------------- SET OBS INFO -------------------------------#
if ($CPL == 7) then
  setenv FLUXOBSDIR /work/yhtseng00/diagnostic_packages/OBS/fluxes/Data/a.b27.03
  setenv FLUXOBSFILE a.b27.03.mean.1984-2006.nc
else
  setenv FLUXOBSDIR /work/yhtseng00/diagnostic_packages/OBS/fluxes/TN460
  setenv FLUXOBSFILE gx1v3.020.clim.1984-2000.nc
endif
setenv WINDOBSDIR /work/yhtseng00/diagnostic_packages/OBS/fluxes/QSCAT
setenv WINDOBSFILE gx1v3.022.clim.2000-2004.nc
setenv SSHOBSDIR /work/yhtseng00/diagnostic_packages/OBS/ssh
setenv SSHOBSFILE 1992-2002MDOT060401.${RESOLUTION}.nc
setenv SSTOBSDIR /work/yhtseng00/diagnostic_packages/OBS/sst
setenv SSTOBSFILE roisst.nc
setenv TSOBSDIR /work/yhtseng00/diagnostic_packages/OBS/phc
setenv TOBSFILE PHC2_TEMP_${RESOLUTION}_ann_avg.nc
setenv SOBSFILE PHC2_SALT_${RESOLUTION}_ann_avg.nc
setenv TOGATAODIR /work/yhtseng00/diagnostic_packages/OBS/johnson_pmel
setenv TOGATAOFILE meanfit_m.nc
setenv RHOOBSDIR /work/yhtseng00/diagnostic_packages/OBS/phc/
setenv RHOOBSFILE PHC2_RHO0_${RESOLUTION}.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_COLORMAP_PATH $NCLPATH/colormaps:$NCARG_ROOT/lib/ncarg/colormaps
setenv PATH ~/src/NCL_TRUNK/ni/src/ncl:$NCARG_ROOT/bin:$PATH
setenv NCLCOLORTABLEFILE ${NCARG_COLORMAP_PATH}/colors1.tbl
setenv NCLCOLORTABLE 42		
setenv NCLMINCOLOR 1
setenv NCLMAXCOLOR 254
setenv NCLLNDCOLOR 255

#--------------------------------------------------------------#
#----------------- END USER DEFINED SETTINGS ------------------#
#--------------------------------------------------------------#
set yr0 = `printf "%04d" $YEAR0`
set yr1 = `printf "%04d" $YEAR1`
echo $yr0 $yr1

setenv NEED_CLIM 0
if ($PM_SEAS == 1) setenv NEED_CLIM 1
if ($PM_MLD == 1) setenv NEED_CLIM 1

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

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

####################  Generate needed netcdfs 

setenv SEASAVGTEMP ${CASE}.pop.h.TEMP.mavg_${yr0}-${yr1}.nc
setenv SEASAVGSALT ${CASE}.pop.h.SALT.mavg_${yr0}-${yr1}.nc
if (-e $SEASAVGTEMP && -e $SEASAVGSALT) setenv NEED_CLIM 0
if ($SWIFT == 1) then
  setenv COMPUTE_RHO 0
endif

setenv NEEDTAVG 0
if !(-e $TAVGFILE) setenv NEEDTAVG 1

if ($SWIFT == 0) echo "NEED_CLIM="$NEED_CLIM" NEEDTAVG="$NEEDTAVG

if ($SWIFT == 0) then
 if ($NEED_CLIM == 1 || $NEEDTAVG == 1) then
  gen_mavg_annmean.csh $MSROOT $CASE $YEAR0 $YEAR1 $TAVGFILE TEMP SALT
 endif
endif

# NEEDZA variables used for Swift version
setenv NEEDZAVG 0
setenv NEEDZA_TOBS 0
setenv NEEDZA_SOBS 0
setenv gridfile $DIAGROOTPATH/tool_lib/zon_avg/grids/${RESOLUTION}_grid_info.nc

##==> generate a plot data file 
##   which contains general plot information available
##   to any of the plot module routines via ascii read.
echo $xyrange >! plot.dat
echo $#depths >> plot.dat
echo $depths >> plot.dat

####################  plot modules
##==> surface flux fields
if ($PM_SFC2D == 1) then
  if ($DOWEB == 1) cat ${HTMLPATH}/sfcflx.html >> popdiag.html
    addtowrapper.csh ncl sfcflx.ncl ncl_wrap.pro
    addtowrapper.csh ncl sfcflx_za.ncl ncl_wrap.pro 
  ln -sf ${FLUXOBSDIR}/${FLUXOBSFILE} .
  ln -sf ${FLUXOBSDIR}/za_${FLUXOBSFILE} .
  ln -sf ${WINDOBSDIR}/${WINDOBSFILE} .
  ln -sf ${WINDOBSDIR}/za_${WINDOBSFILE} .
  if !(-e za_$TAVGFILE) then
   if ($SWIFT == 0) then
    za -O -time_const -grid_file $gridfile $TAVGFILE
   else
    setenv NEEDZAVG 1
   endif
  endif
endif

##==> 2D (surface) fields
if ($PM_FLD2D == 1) then
  if ($DOWEB == 1) cat ${HTMLPATH}/field_2d.html >> popdiag.html
  addtowrapper.csh ncl ssh.ncl ncl_wrap.pro 
  addtowrapper.csh ncl field_2d.ncl ncl_wrap.pro 
  addtowrapper.csh ncl field_2d_za.ncl ncl_wrap.pro 
  ln -sf ${SSHOBSDIR}/${SSHOBSFILE} .
  if !(-e za_$TAVGFILE) then
    if ($SWIFT == 0) then
     za -O -time_const -grid_file $gridfile $TAVGFILE
    else
     setenv NEEDZAVG 1
    endif
  endif
endif

##==> 3D fields (zonal average)
if ($PM_FLD3DZA == 1) then
  if ($DOWEB == 1) cat ${HTMLPATH}/field_3d_za.html >> popdiag.html
  addtowrapper.csh ncl field_3d_za.ncl ncl_wrap.pro
  if !(-e za_$TAVGFILE) then
   if ($SWIFT == 0) then
    za -O -time_const -grid_file $gridfile $TAVGFILE
   else
    setenv NEEDZAVG 1
   endif
  endif
  if ($SWIFT == 0) then
    if !(-e za_${TOBSFILE}) then
        cp ${TSOBSDIR}/${TOBSFILE} ${TOBSFILE}_tmp
        ncks -A -v UAREA $TAVGFILE ${TOBSFILE}_tmp
	za -O -time_const -grid_file $gridfile ${TOBSFILE}_tmp
        mv za_${TOBSFILE}_tmp za_${TOBSFILE}
        \rm -f ${TOBSFILE}_tmp
    endif
    if !(-e za_${SOBSFILE}) then
        cp ${TSOBSDIR}/${SOBSFILE} ${SOBSFILE}_tmp
        ncks -A -v UAREA $TAVGFILE ${SOBSFILE}_tmp
        za -O -time_const -grid_file $gridfile ${SOBSFILE}_tmp
        mv za_${SOBSFILE}_tmp za_${SOBSFILE}
        \rm -f ${SOBSFILE}_tmp
    endif
  else
    if !(-e za_${TOBSFILE}) setenv NEEDZA_TOBS 1
    if !(-e za_${SOBSFILE}) setenv NEEDZA_SOBS 1
  endif
endif

##==> MOC & Heat/Freshwater transport plots
if ($PM_MOC == 1) then
  if ($DOWEB == 1) cat ${HTMLPATH}/moc.html >> popdiag.html
  addtowrapper.csh ncl moc_netcdf.ncl ncl_wrap.pro
endif

##==> Western Boundary Current & DWBC diagnostics
if ($PM_WBC == 1) then
  if ($DOWEB == 1) cat ${HTMLPATH}/wbc.html >> popdiag.html
  addtowrapper.csh ncl dwbc.ncl ncl_wrap.pro
endif

if ($PM_SEAS == 1) then
  setenv SEASAVGFILE $SEASAVGTEMP
  if ($DOWEB == 1) cat ${HTMLPATH}/seasonal.html >> popdiag.html
   addtowrapper.csh ncl sst_eq_pac_seasonal_cycle.ncl ncl_wrap.pro
  ln -sf ${SSTOBSDIR}/${SSTOBSFILE} .
else
  setenv SEASAVGFILE "null"
endif

##==> Mixed layer depth plots
if ($PM_MLD == 1) then
#  setenv IDL_COLOR_FILE /cgd/oce/gokhan/home/bin/idl_src/colors1.tbl 
  if ($DOWEB == 1) cat ${HTMLPATH}/mld.html >> popdiag.html
  setenv SEASAVGRHO  ${CASE}.pop.h.RHO.mavg_${yr0}-${yr1}.nc
  if !(-e $SEASAVGRHO) then
   if ($SWIFT == 0) then
     addtowrapper.csh ncl compute_rho.ncl ncl_wrap.pro
   else
     setenv COMPUTE_RHO 1
  endif
  endif
  addtowrapper.csh ncl mld.ncl ncl_wrap.pro
  ln -sf ${RHOOBSDIR}/${RHOOBSFILE} .
else
  setenv SEASAVGRHO "null"
endif

##==> TEMP & SALT at depth levels
if ($PM_TSZ == 1) then
  if ($DOWEB == 1) then
        setenv MODULETITLE 'T & S at depth'
	gen_html_multivarz.csh 2 TEMP SALT 13 $depths
        cat tmp.html >> popdiag.html && \rm tmp.html
  endif
  addtowrapper.csh ncl tempz.ncl ncl_wrap.pro 
  addtowrapper.csh ncl saltz.ncl ncl_wrap.pro
  ln -sf ${TSOBSDIR}/${TOBSFILE} .
  ln -sf ${TSOBSDIR}/${SOBSFILE} .
endif

##==> Passive Tracers at depth levels
if ($PM_PASSIVEZ == 1) then
  if ($DOWEB == 1) then
        setenv MODULETITLE 'Passive Tracers at depth'
	gen_html_multivarz.csh 1 IAGE 13 $depths
        cat tmp.html >> popdiag.html && \rm tmp.html
  endif
  addtowrapper.csh ncl iagez.ncl ncl_wrap.pro
endif

##==> Eulerian velocity at depth levels
if ($PM_VELZ == 1) then
  if ($DOWEB == 1) then
        setenv MODULETITLE 'Eulerian Velocity Components at depth'
	gen_html_multivarz.csh 3 UVEL VVEL WVEL 13 $depths
        cat tmp.html >> popdiag.html && \rm tmp.html
  endif
  addtowrapper.csh ncl uvelz.ncl ncl_wrap.pro
  addtowrapper.csh ncl vvelz.ncl ncl_wrap.pro
  addtowrapper.csh ncl wvelz.ncl ncl_wrap.pro
endif

##==> Bolus velocity at depth levels
if ($PM_VELISOPZ == 1) then
  if ($DOWEB == 1) then
        setenv MODULETITLE 'Bolus Velocity Components at depth'
	gen_html_multivarz.csh 3 UISOP VISOP WISOP 13 $depths
        cat tmp.html >> popdiag.html && \rm tmp.html
  endif
  addtowrapper.csh ncl uisopz.ncl ncl_wrap.pro
  addtowrapper.csh ncl visopz.ncl ncl_wrap.pro
  addtowrapper.csh ncl wisopz.ncl ncl_wrap.pro
endif

##==> Diffusion Coefficients at depth levels
if ($PM_KAPPAZ == 1) then
  if ($DOWEB == 1) then
        setenv MODULETITLE 'Diffusion Coefficients at depth'
	gen_html_multivarz.csh 2 KAPPA_ISOP KAPPA_THIC 13 $depths
        cat tmp.html >> popdiag.html && \rm tmp.html
  endif
  addtowrapper.csh ncl kappa_isopz.ncl ncl_wrap.pro
  addtowrapper.csh ncl kappa_thicz.ncl ncl_wrap.pro
endif

##==> Upperocean at the Equator; compared to PHC2, TOGA-TAO
if ($PM_UOEQ == 1) then
  if ($DOWEB == 1) cat ${HTMLPATH}/eq_upperocean.html >> popdiag.html
  addtowrapper.csh ncl T_eq.ncl ncl_wrap.pro
  addtowrapper.csh ncl S_eq.ncl ncl_wrap.pro
  addtowrapper.csh ncl U_eq.ncl ncl_wrap.pro
  addtowrapper.csh ncl U_eq_meridional.ncl ncl_wrap.pro
  addtowrapper.csh ncl T_eq_meridional.ncl ncl_wrap.pro
  addtowrapper.csh ncl S_eq_meridional.ncl ncl_wrap.pro
  addtowrapper.csh ncl PD_eq_meridional.ncl ncl_wrap.pro
  ln -sf ${TOGATAODIR}/${TOGATAOFILE} .
endif

##==> Velocity vector/magnitude at depth levels
if ($PM_VECV == 1) then
  if ($DOWEB == 1) then
        setenv MODULETITLE 'Horizontal Vector Fields at depth'
	gen_html_multivarz.csh 1 VELOCITY 13 $depths
        cat tmp.html >> popdiag.html && \rm tmp.html
  endif
  addtowrapper.csh ncl vecvelz.ncl ncl_wrap.pro

endif

##==> POLAR TEMP & SALT at depth levels
if ($PM_POLARTS == 1) then
  if ($DOWEB == 1) then
        setenv MODULETITLE 'Polar T & S at depth'
	gen_html_multivarz.csh 4 Arctic_TEMP Arctic_SALT \
		Antarctic_TEMP Antarctic_SALT 13 $depths
        cat tmp.html >> popdiag.html && \rm tmp.html
  endif
  ln -sf ${TSOBSDIR}/${TOBSFILE} .
  ln -sf ${TSOBSDIR}/${SOBSFILE} .
  addtowrapper.csh ncl tempz_arctic.ncl ncl_wrap.pro
  addtowrapper.csh ncl saltz_arctic.ncl ncl_wrap.pro
  addtowrapper.csh ncl tempz_antarctic.ncl ncl_wrap.pro
  addtowrapper.csh ncl saltz_antarctic.ncl ncl_wrap.pro
endif

##==> Depth Profiles of Basin-average T & S
if ($PM_BASINAVGTS == 1) then
  if ($DOWEB == 1) cat ${HTMLPATH}/basinavg.html >> popdiag.html
  addtowrapper.csh ncl TS_basinavg_arctic.ncl ncl_wrap.pro
  if !(-e ${TOBSFILE}) ln -sf ${TSOBSDIR}/${TOBSFILE} .
  if !(-e ${SOBSFILE}) ln -sf ${TSOBSDIR}/${SOBSFILE} .
endif

##==> Depth Profiles of Regional area-averaged T & S bias and stddev
if ($PM_REGIONALTS == 1) then
  if ($DOWEB == 1) cat ${HTMLPATH}/regionalts.html >> popdiag.html
  addtowrapper.csh ncl regionalTbias500m.ncl ncl_wrap.pro
  addtowrapper.csh ncl regionalSbias500m.ncl ncl_wrap.pro
  addtowrapper.csh ncl regionalTbias2000m.ncl ncl_wrap.pro
  addtowrapper.csh ncl regionalSbias2000m.ncl ncl_wrap.pro
  if !(-e ${TOBSFILE}) ln -sf ${TSOBSDIR}/${TOBSFILE} .
  if !(-e ${SOBSFILE}) ln -sf ${TSOBSDIR}/${SOBSFILE} .
endif

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

  ############ Check to see if the clim file exist on hsi
  setenv MSPM $MSROOT/$CASE/ocn/proc/tavg/monthly/
  
  setenv NEED_CLIM_TEMP 1
  setenv NEED_CLIM_SALT 1

  ## Check for TEMP
  if !(-e ${SEASAVGTEMP}) then
    hsi -P "ls ${MSPM}/${SEASAVGTEMP}"
    if ($status == 0) then
      echo "File ${MSPM}/${SEASAVGTEMP} already exists...reading from HPSS"
      hsi -P "get ${MSPM}/${SEASAVGTEMP}"
      setenv NEED_CLIM_TEMP 0
    endif
  else
    setenv NEED_CLIM_TEMP 0
  endif
  
  ## Check for SALT
  if !(-e ${SEASAVGSALT}) then
    hsi -P "ls ${MSPM}/${SEASAVGSALT}"
    if ($status == 0) then
      echo "File ${MSPM}/${SEASAVGSALT} already exists...reading from HPSS"
      hsi -P "get ${MSPM}/${SEASAVGSALT}"
      setenv NEED_CLIM_SALT 0
    endif
  else
    setenv NEED_CLIM_SALT 0
  endif

  if ($NEED_CLIM_TEMP == 0 && $NEED_CLIM_SALT == 0) then
      setenv NEED_CLIM 0
  endif

  swift -config $RUNDIR/cf.properties \
  -sites.file $RUNDIR/sites.xml -tc.file $RUNDIR/tc.data -cdm.file $RUNDIR/fs.data $RUNDIR/popdiag.swift -wrkdir=$WORKDIR/ \
  -msroot=$MSROOT -sobsfilefn=$SOBSFILE -tobsfilefn=$TOBSFILE -case=$CASE -resolution=$RESOLUTION -year0=$YEAR0 -year1=$YEAR1 \
  -ncl_wrap=$WORKDIR/"ncl_wrap.pro" -cntrlvls=$CNTRLVLS -cpl=$CPL -cpy_hsi=$CPY_TO_HSI \
  -fluxobsfile=$FLUXOBSFILE -needzavg=$NEEDZAVG -needza_tobs=$NEEDZA_TOBS \
  -needza_sobs=$NEEDZA_SOBS -rhoobsfile=$RHOOBSFILE -sobsfilefp=$SOBSFILE -sshobsfile=$SSHOBSFILE -sstobsfile=$SSTOBSFILE \
  -tavgfile=$TAVGFILE -tobsfilefp=$TOBSFILE -togataofile=$TOGATAOFILE -windobsfile=$WINDOBSFILE -tobsdir=$TSOBSDIR \
  -need_clim=$NEED_CLIM -needtavg=$NEEDTAVG -ncarg_root=$NCARG_ROOT -nclpath=$NCLPATH -diagrootpath=$DIAGROOTPATH \
  -ncarg_colormap_path=$NCARG_COLORMAP_PATH -seasavgtemp=$SEASAVGTEMP -seasavgsalt=$SEASAVGSALT -seasavgfile=$SEASAVGFILE \
  -seasavgrho=$SEASAVGRHO -compute_rho=$COMPUTE_RHO -doplots=$DOPLOTS -need_clim_TEMP=$NEED_CLIM_TEMP -need_clim_SALT=$NEED_CLIM_SALT
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
##   exit
#    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
#    exit
    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 >> popdiag.html
  cat ${HTMLPATH}/footer.html >> popdiag.html
  cp popdiag.html ${WEBDIR}

  if ($DOPLOTS == 1) then
    cp *.asc *.gif *.png ${WEBDIR}
  endif
endif

echo "DONE popdiag.csh:  CASE = $CASE, YEAR0 = $YEAR0, YEAR1 = $YEAR1"
