#!/bin/tcsh
#
# Make runs of post-processor to generate plots from storm case benchmark runs:
# (instantaneous and time-dependent)
#
set mycwd = `pwd`
set home = /glade/p/hao/tgcm/tiegcm2.0/benchmarks         # base working directory
set proc = $home/postproc/tgcmproc_f90/tgcmproc/tgcmproc  # python app to make multiple proc runs

set singleut  = yes  # make instantaneous plots
set multiut   = yes  # make time slices (ut on x-axis)
set select    = yes  # less comprehensive (fewer fields, fewer plots)
set make_pdf  = yes  # will make pdf files from the ps, using ps2pdf
set remove_ps = yes  # remove ps file after making pdf (only if make_pdf=yes)

set resolutions = ( \
  '5.0' \
  '2.5' \
)

foreach res ($resolutions)

  set case   = tiegcm_res${res}_storms
  set work   = $home/$case

  if ($res == '5.0') then
    set exec = $home/postproc/tgcmproc_f90/build/tgcmproc_sres_f90  # single-res processor executable
  else if ($res == '2.5') then
    set exec = $home/postproc/tgcmproc_f90/build/tgcmproc_dres_f90  # double-res processor executable
  else
    echo ">>> Unknown resolution: $res"
    exit 
  endif
 
  set runs = ( \
    tiegcm_res${res}_dec2006_heelis_gpi \
    tiegcm_res${res}_dec2006_weimer_imf \
    tiegcm_res${res}_jul2000_heelis_gpi \
    tiegcm_res${res}_jul2000_weimer_imf \
    tiegcm_res${res}_nov2003_heelis_gpi \
    tiegcm_res${res}_nov2003_weimer_imf \
    tiegcm_res${res}_whi2008_heelis_gpi \
    tiegcm_res${res}_whi2008_weimer_imf \
  )

  set mtimes_singleut = ( \
    '349,12,0' '349,12,0' \
    '198,0,0'  '198,0,0'  \
    '325,0,0'  '325,0,0'  \
    '88,0,0'   '88,0,0'   \
  )

  set mtimes_daily = ( \
    "330,0,0,'to',360,0,0,'by',1440" \
    "330,0,0,'to',360,0,0,'by',1440" \
    "192,0,0,'to',202,0,0,'by',1440" \
    "192,0,0,'to',202,0,0,'by',1440" \
    "323,0,0,'to',328,0,0,'by',1440" \
    "323,0,0,'to',328,0,0,'by',1440" \
    "81,0,0,'to',106,0,0,'by',1440" \
    "81,0,0,'to',106,0,0,'by',1440" \
  )

  set days_daily = ( \
    'days330-360_daily' 'days330-360_daily'  \
    'days192-202_daily' 'days192-202_daily'  \
    'days323-328_daily' 'days323-328_daily'  \
    'days081-106_daily' 'days081-106_daily'  \
  )
    
  set mtimes_hourly = ( \
    "347,0,0,'to',352,0,0,'by',60" \
    "347,0,0,'to',352,0,0,'by',60" \
    "195,0,0,'to',200,0,0,'by',60" \
    "195,0,0,'to',200,0,0,'by',60" \
    "323,1,0,'to',328,0,0,'by',60" \
    "323,1,0,'to',328,0,0,'by',60" \
    "85,0,0,'to',90,0,0,'by',60"   \
    "85,0,0,'to',90,0,0,'by',60"   \
  )

  set days_hourly = ( \
    'days347-352_hourly' 'days347-352_hourly'  \
    'days195-200_hourly' 'days195-200_hourly'  \
    'days323-328_hourly' 'days323-328_hourly'  \
    'days085-090_hourly' 'days085-090_hourly'  \
  )

  set doselect = 0
runloop:
#
# Complete fields list:
#
  set cfields = "'TN','UN','VN','WN','O2','O1','N2','NO','N4S','HE','NE','TE','TI','TEC','O2P','OP','POTEN','UI_ExB','VI_ExB','WI_ExB','DEN','QJOULE','HMF2','NMF2','Z','ZG'"
# Selected fields:
  if ($doselect == '1') set cfields = "'TN','UN','VN','WN','HE','NE','TE','TI','HMF2','NMF2','POTEN','Z'"

  @ irun = 1
  foreach run ($runs)
    echo "------------------ $run --------------------"
    set outdir  = $work/$run/proc
    set histdir = $work/$run/hist
#
# All file paths are absolute, except for history files, which are relative to $histdir
#
    cd $histdir
#
# Instantaneous maps and slices:
#
    if ($singleut == 'yes') then
      set root = $outdir/${run}_singleut
      if ($doselect == '1') set root = $outdir/${run}_singleut_select
      $proc -submit yes -executable $exec -singleut 1 -mtimes $mtimes_singleut[$irun] \
        -nlfile $root.inp -stdout $root.out -flnm_ps $root.ps -cfields $cfields \*sech\*.nc
      if ($make_pdf == 'yes') then
        ps2pdf $root.ps $root.pdf
        if ($remove_ps == 'yes' && -e $root.ps) rm $root.ps
      endif
      echo Completed singleut run for $run mtime=$mtimes_singleut[$irun]
      echo ' '
    endif
#
# Time series plots (utvert, utlat, daily, hourly):
#
    if ($multiut == 'yes') then
#
# Daily utvert:
      set root = $outdir/${run}_${days_daily[$irun]}_utvert
      if ($doselect == '1') set root = $outdir/${run}_${days_daily[$irun]}_utvert_select
      $proc -submit yes -executable $exec -multiut 1 -mtimes $mtimes_daily[$irun] \
        -pltutlat 0 -nlfile $root.inp -stdout $root.out -flnm_ps $root.ps \
        -cfields $cfields \*prim\*.nc
      if ($make_pdf == 'yes') then
        ps2pdf -dAutoRotatePages=/None $root.ps $root.pdf
        if ($remove_ps == 'yes' && -e $root.ps) rm $root.ps
      endif
      echo Completed daily utvert for $run $days_daily[$irun]
      echo ' '
#
# Daily utlat:
      set root = $outdir/${run}_${days_daily[$irun]}_utlat
      if ($doselect == '1') set root = $outdir/${run}_${days_daily[$irun]}_utlat_select
      $proc -submit yes -executable $exec -multiut=1 -mtimes $mtimes_daily[$irun] \
        -pltutvert=0 -nlfile $root.inp -stdout $root.out -flnm_ps $root.ps \
        -cfields $cfields \*prim\*.nc
      if ($make_pdf == 'yes') then
        ps2pdf $root.ps $root.pdf
        if ($remove_ps == 'yes') rm $root.ps
      endif
      echo Completed daily utlat for $run $days_daily[$irun]
      echo ' '
#
# Hourly utvert:
      set root = $outdir/${run}_${days_hourly[$irun]}_utvert
      if ($doselect == '1') set root = $outdir/${run}_${days_hourly[$irun]}_utvert_select
      $proc -submit yes -executable $exec -multiut=1 -mtimes $mtimes_hourly[$irun] \
        -pltutlat=0 -nlfile $root.inp -stdout $root.out -flnm_ps $root.ps \
        -cfields $cfields \*sech\*.nc
      if ($make_pdf == 'yes') then
        ps2pdf -dAutoRotatePages=/None $root.ps $root.pdf
        if ($remove_ps == 'yes') rm $root.ps
      endif
      echo Completed hourly utvert for $run $days_daily[$irun]
      echo ' '
#
# Hourly utlat:
      set root = $outdir/${run}_${days_hourly[$irun]}_utlat
      if ($doselect == '1') set root = $outdir/${run}_${days_hourly[$irun]}_utlat_select
      $proc -submit yes -executable $exec -multiut=1 -mtimes $mtimes_hourly[$irun] \
        -pltutvert=0 -nlfile $root.inp -stdout $root.out -flnm_ps $root.ps \
        -cfields $cfields \*sech\*.nc
      if ($make_pdf == 'yes') then
        ps2pdf $root.ps $root.pdf
        if ($remove_ps == 'yes') rm $root.ps
      endif
      echo Completed hourly utlat for $run $days_daily[$irun]
      echo ' '

    endif # multiut

    @ irun += 1
  end # foreach run
#
# Go back up and make select files (select subgroup of fields)
  if ($select == 'yes' && $doselect == '0') then
    set doselect = 1
    goto runloop
  endif

  cd $mycwd
  echo tgcmproc moved back to $mycwd
end # foreach res

