#!/bin/csh -f
#
#   Master script for CCSM Ocean Time-Mean & Seasonal Diagnostics
#	including differences relative to a control case
#

#----------------- 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
setenv RESOLUTION gx1v6
setenv YEAR0 44
setenv YEAR1 61
setenv CPL 7       #set to 7 if CCSM4 or higher, set to 6 otherwise
setenv CNTRLCASE G_JRA55_test1_vdc01
setenv CNTRLRESOLUTION gx1v7
setenv CNTRLYEAR0 44
setenv CNTRLYEAR1 61
setenv CPLCNTRL 7

#----------------- 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 (std, 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_UOEQ	 	1	# Upperocean T,S,PD,U @ Equator
setenv PM_MOC	 	1	# MOC, HT&FW transports
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

#----------------- SET MSS INFO --------------------------------#
setenv TAVGFILE tavg.$YEAR0.$YEAR1.nc
setenv CNTRLFILE tavg.$CNTRLYEAR0.$CNTRLYEAR1.cntrl.nc
# In Swift version, MSROOT and CNTRLMSROOT are the locations 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 CNTRLMSROOT /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/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.${CNTRLCASE}.${CNTRLYEAR0}_${CNTRLYEAR1}
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/pd.${CASE}-${CNTRLCASE} 
# 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}/pd.${CASE}-${CNTRLCASE}
setenv PATH ${TOOLPATH}:${PATH}
setenv PATH ${MODPATH}:${TOOLPATH}:${PATH}:/fs/local/bin

#----------------- SET OBS INFO -------------------------------#
setenv FLUXOBSDIR /work/yhtseng00/diagnostic_packages/OBS/fluxes/Data/a.b27.03
setenv FLUXOBSFILE a.b27.03.mean.1984-2006.nc
#setenv FLUXOBSDIR /cgd/oce/yeager/obs/fluxes/TN460
#setenv FLUXOBSFILE gx1v3.020.clim.1984-2000.nc
setenv WINDOBSDIR /work/yhtseng00/diagnostic_packages/OBS/fluxes/QSCAT
setenv WINDOBSFILE gx1v3.022.clim.2000-2004.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

#----------------- SET NCL INFO -------------------------------#
setenv NCARG_ROOT /opt/ohpc/pkg/rcec/pkg/ncl
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

set cntrlyr0 = `printf "%04d" $CNTRLYEAR0`
set cntrlyr1 = `printf "%04d" $CNTRLYEAR1`
echo $cntrlyr0 $cntrlyr1

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
  @ dlon = ($xyrange[2] - $xyrange[1])
  @ dlat = ($xyrange[4] - $xyrange[3])
  if ($dlon == 360 && $dlat == 180) then
    set tail = global
  else
    set tail = ${xyrange[1]}to${xyrange[2]}E.${xyrange[3]}to${xyrange[4]}N
  endif
  setenv WEBDIR ${WEBDIR2}.${tail}
  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`" && \rm -f tmp
    echo "running popdiag $POPDIAGREV"
    sed "s/CASENAME/${CASE}/g" ${HTMLPATH}/diffheader.html > tmp.html
    sed "s/CNTRLCASE/${CNTRLCASE}/g" tmp.html > popdiag.html
    sed "s/REVISION/${POPDIAGREV}/g" popdiag.html > tmp.html 
    mv -f tmp.html popdiag.html
  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
setenv CNTRLSEASAVGTEMP ${CNTRLCASE}.pop.h.TEMP.mavg_${cntrlyr0}-${cntrlyr1}.nc
setenv CNTRLSEASAVGSALT ${CNTRLCASE}.pop.h.SALT.mavg_${cntrlyr0}-${cntrlyr1}.nc

setenv NEED_CLIM_EXP 0
setenv NEED_CLIM_CNTRL 0
if ($NEED_CLIM == 1) then
 if !(-e $SEASAVGTEMP && -e $SEASAVGSALT) setenv NEED_CLIM_EXP 1
 if !(-e $CNTRLSEASAVGTEMP && -e $CNTRLSEASAVGSALT) setenv NEED_CLIM_CNTRL 1
endif

setenv NEEDTAVG 0
if !(-e $TAVGFILE) setenv NEEDTAVG 1
echo "NEED_CLIM_EXP="$NEED_CLIM_EXP" NEEDTAVG="$NEEDTAVG
setenv NEED_CLIM $NEED_CLIM_EXP
if ($SWIFT == 0) then
if ($NEED_CLIM_EXP == 1 || $NEEDTAVG == 1) then
 gen_mavg_annmean.csh $MSROOT $CASE $YEAR0 $YEAR1 $TAVGFILE TEMP SALT
endif
endif

setenv NEEDTAVG 0
if !(-e $CNTRLFILE) setenv NEEDTAVG 1
echo "NEED_CLIM_CNTRL="$NEED_CLIM_CNTRL" NEEDTAVG="$NEEDTAVG

setenv $NEED_CLIM $NEED_CLIM_CNTRL
if ($SWIFT == 0) then
if ($NEED_CLIM_CNTRL == 1 || $NEEDTAVG == 1) then
  gen_mavg_annmean.csh $CNTRLMSROOT $CNTRLCASE $CNTRLYEAR0 $CNTRLYEAR1 $CNTRLFILE TEMP SALT
endif
endif

# NEEDZAVG variables are used for Swift version
setenv NEEDZAVG_EXP 0
setenv NEEDZAVG_CNTRL 0
setenv gridfile $DIAGROOTPATH/tool_lib/zon_avg/grids/$RESOLUTION*_grid_info.nc
setenv gridfilecntrl $DIAGROOTPATH/tool_lib/zon_avg/grids/$CNTRLRESOLUTION*_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_diff.ncl ncl_wrap.pro 
  addtowrapper.csh ncl sfcflx_za_diff.ncl ncl_wrap.pro 
  if ($SWIFT == 0) then
    if !(-e za_$TAVGFILE) then
       za -O -time_const -grid_file $gridfile $TAVGFILE
    endif
    if !(-e za_$CNTRLFILE) then
       za -O -time_const -grid_file $gridfilecntrl $CNTRLFILE
    endif
  else
    setenv NEEDZAVG_EXP 1
    setenv NEEDZAVG_CNTRL 1
  endif
endif

##==> 2D (surface) fields
if ($PM_FLD2D == 1) then
  if ($DOWEB == 1) cat ${HTMLPATH}/field_2d.html >> popdiag.html
  addtowrapper.csh ncl field_2d_diff.ncl ncl_wrap.pro 
  addtowrapper.csh ncl field_2d_za_diff.ncl ncl_wrap.pro 
  if ($SWIFT == 0) then
    if !(-e za_$TAVGFILE) then
       za -O -time_const -grid_file $gridfile $TAVGFILE
    endif
  else
    setenv NEEDZAVG_EXP 1
    setenv NEEDZAVG_CNTRL 1
  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_diff.ncl ncl_wrap.pro
  if ($SWIFT == 1) then
    setenv NEEDZAVG_EXP 1
    setenv NEEDZAVG_CNTRL 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_diff.ncl ncl_wrap.pro
endif

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

if ($SWIFT == 1) then
  setenv COMPUTE_RHO 0
  setenv COMPUTE_CNTL_RHO 0
endif

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
  setenv CNTRLSEASAVGRHO  ${CNTRLCASE}.pop.h.RHO.mavg_${cntrlyr0}-${cntrlyr1}.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
  if !(-e $CNTRLSEASAVGRHO) then
    if ($SWIFT == 0) then
      addtowrapper.csh ncl compute_rho_cntl.ncl ncl_wrap.pro
    else
      setenv COMPUTE_CNTL_RHO 1
    endif
  endif
  addtowrapper.csh ncl mld_diff.ncl ncl_wrap.pro
else
  setenv SEASAVGRHO "null"
  setenv CNTRLSEASAVGRHO "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_diff.ncl ncl_wrap.pro 
  addtowrapper.csh ncl saltz_diff.ncl ncl_wrap.pro
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_diff.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_diff.ncl ncl_wrap.pro
  addtowrapper.csh ncl vvelz_diff.ncl ncl_wrap.pro
  addtowrapper.csh ncl wvelz_diff.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_diff.ncl ncl_wrap.pro
  addtowrapper.csh ncl visopz_diff.ncl ncl_wrap.pro
  addtowrapper.csh ncl wisopz_diff.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_diff.ncl ncl_wrap.pro
  addtowrapper.csh ncl kappa_thicz_diff.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_diff.ncl ncl_wrap.pro
  addtowrapper.csh ncl S_eq_diff.ncl ncl_wrap.pro
  addtowrapper.csh ncl U_eq_diff.ncl ncl_wrap.pro
  addtowrapper.csh ncl U_eq_meridional_diff.ncl ncl_wrap.pro
  addtowrapper.csh ncl T_eq_meridional_diff.ncl ncl_wrap.pro
  addtowrapper.csh ncl S_eq_meridional_diff.ncl ncl_wrap.pro
  addtowrapper.csh ncl PD_eq_meridional_diff.ncl ncl_wrap.pro
  ln -sf ${TOGATAODIR}/${TOGATAOFILE} .
endif

################### Call Swift to compute averages and create plots in parallel or 
################### create plots in serial

if ($SWIFT == 1) then
  cd $WORKDIR  

 setenv NEEDTAVG_EXP 0
  if !(-e $TAVGFILE) setenv NEEDTAVG_EXP 1
  setenv NEEDTAVG_CNTRL 0
  if !(-e $CNTRLFILE) setenv NEEDTAVG_CNTRL 1

  ############ Check to see if the clim file exist on hsi
  setenv MSPM_EXP $MSROOT/$CASE/ocn/proc/tavg/monthly/
  setenv MSPM_CNTRL $MSROOT/$CNTRLCASE/ocn/proc/tavg/monthly/

  setenv NEED_CLIM_TEMP_EXP 1
  setenv NEED_CLIM_SALT_EXP 1
  setenv NEED_CLIM_TEMP_CNTRL 1
  setenv NEED_CLIM_SALT_CNTRL 1

  ## Check for TEMP
  if !(-e ${SEASAVGTEMP}) then
    hsi -P "ls ${MSPM_EXP}/${SEASAVGTEMP}"
    if ($status == 0) then
      echo "File ${MSPM_EXP}/${SEASAVGTEMP} already exists...reading from HPSS"
      hsi -P "get ${MSPM_EXP}/${SEASAVGTEMP}"
      setenv NEED_CLIM_TEMP_EXP 0
    endif
  else
    setenv NEED_CLIM_TEMP_EXP 0
  endif
  if !(-e ${CNTRLSEASAVGTEMP}) then
    hsi -P "ls ${MSPM_CNTRL}/${CNTRLSEASAVGTEMP}"
    if ($status == 0) then
      echo "File ${MSPM_CNTRL}/${CNTRLSEASAVGTEMP} already exists...reading from HPSS"
      hsi -P "get ${MSPM_CNTRL}/${CNTRLSEASAVGTEMP}"
      setenv NEED_CLIM_TEMP_CNTRL 0
    endif
  else
    setenv NEED_CLIM_TEMP_CNTRL 0
  endif

  ## Check for SALT
  if !(-e ${SEASAVGSALT}) then
    hsi -P "ls ${MSPM_EXP}/${SEASAVGSALT}"
    if ($status == 0) then
      echo "File ${MSPM_EXP}/${SEASAVGSALT} already exists...reading from HPSS"
      hsi -P "get ${MSPM_EXP}/${SEASAVGSALT}"
      setenv NEED_CLIM_SALT_EXP 0
    endif
  else
    setenv NEED_CLIM_SALT_EXP 0
  endif
  if !(-e ${CNTRLSEASAVGSALT}) then
    hsi -P "ls ${MSPM_CNTRL}/${CNTRLSEASAVGSALT}"
    if ($status == 0) then
      echo "File ${MSPM_CNTRL}/${CNTRLSEASAVGSALT} already exists...reading from HPSS"
      hsi -P "get ${MSPM_CNTRL}/${CNTRLSEASAVGSALT}"
      setenv NEED_CLIM_SALT_CNTRL 0
    endif
  else
    setenv NEED_CLIM_SALT_CNTRL 0
  endif

  if ($NEED_CLIM_TEMP_EXP == 0 && $NEED_CLIM_SALT_EXP == 0) then
      setenv NEED_CLIM_EXP 0
  endif
  if ($NEED_CLIM_TEMP_CNTRL == 0 && $NEED_CLIM_SALT_CNTRL == 0) then
      setenv NEED_CLIM_CNTRL 0
  endif

  swift -config $RUNDIR/cf.properties -sites.file $RUNDIR/sites.xml \
  -tc.file $RUNDIR/tc.data -cdm.file $RUNDIR/fs.data $RUNDIR/popdiagdiff.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 \
  -sobsfilefp=$SOBSFILE -sstobsfile=$SSTOBSFILE -needzavg_exp=$NEEDZAVG_EXP -needzavg_cntrl=$NEEDZAVG_CNTRL \
  -tavgfile=$TAVGFILE -tobsfilefp=$TOBSFILE -togataofile=$TOGATAOFILE -windobsfile=$WINDOBSFILE -tobsdir=$TSOBSDIR \
  -need_clim_exp=$NEED_CLIM_EXP -needtavg_exp=$NEEDTAVG_EXP -needtavg_cntrl=$NEEDTAVG_CNTRL -need_clim_cntrl=$NEED_CLIM_CNTRL \
  -ncarg_root=$NCARG_ROOT -nclpath=$NCLPATH -diagrootpath=$DIAGROOTPATH -cntrlfile=$CNTRLFILE \
  -cntrlcase=$CNTRLCASE -cntrlresolution=$CNTRLRESOLUTION -cntrlyear0=$CNTRLYEAR0\
  -cntrlyear1=$CNTRLYEAR1 -cplcntrl=$CPLCNTRL -cntrlmsroot=$CNTRLMSROOT \
  -ncarg_colormap_path=$NCARG_COLORMAP_PATH -seasavgtemp=$SEASAVGTEMP -seasavgsalt=$SEASAVGSALT \
  -seasavgfile=$SEASAVGFILE -seasavgrho=$SEASAVGRHO -cntrlseasavgtemp=$CNTRLSEASAVGTEMP \
  -cntrlseasavgsalt=$CNTRLSEASAVGSALT -cntrlseasavgfile=$CNTRLSEASAVGFILE -cntrlseasavgrho=$CNTRLSEASAVGRHO \
  -compute_rho=$COMPUTE_RHO -compute_cntl_rho=$COMPUTE_CNTL_RHO -doplots=$DOPLOTS -need_clim_TEMP_exp=$NEED_CLIM_TEMP_EXP \
  -need_clim_SALT_exp=$NEED_CLIM_SALT_EXP -need_clim_TEMP_cntrl=$NEED_CLIM_TEMP_CNTRL -need_clim_SALT_cntrl=$NEED_CLIM_SALT_CNTRL

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 >> popdiag.html
  cat ${HTMLPATH}/footer.html >> popdiag.html
  cp popdiag.html ${WEBDIR}

  if ($DOPLOTS == 1) then
    cp *.gif *.png ${WEBDIR}
  endif
endif
echo "DONE popdiagdiff.csh:  CASE = $CASE, CNTRLCASE = $CNTRLCASE"
