#! /bin/csh -f

# runNikoCoordModel.csh -s site_list -t run_type [-f -u]

# -s site_list can be a 4-letter site ID (e.g. ldes)
#              or the name of a file in the working 
#              directory that contains a list of sites

# -t run_type  (clean/model/stack) 
#       'clean' operates on raw neu data 
#               (SITEr.neu, created by cats)
#               to output cleaned neu data (sitec.neu).
#               Currently hardcoded to edit postfit 
#               residuals > 3*IQR within a sliding window
#               of length 365 days.
#       'model' operates on clean neu data 
#               (SITEc.neu, created by runNikoCoordModel, clean option)
#               to output model parameter files and plots
#               of the modeled timeseries. 
#       'stack' operates on postfit residuals 
#               (SITE_resids_unf.dat, created by runNikoCoordModel, model option)
#               to create common mode error signal to be removed from neu data
#               to filter out global errors 
#
# -f filter    substract stack from neu data (for clean/model).
#              default is no regional filtering.
#
# -u update    turns update option on to update the raw 
#              neu files in the working directory before cleaning
                
# typical usage:  
#   runNikoCoordModel.csh -s Model.sites -t clean -u  
#   runNikoCoordModel.csh -s Model.sites -t model    #use output site_unf.xyz to update itrf and website
#   runNikoCoordModel.csh -s Model.stack -t stack
#   runNikoCoordModel.csh -s Model.sites -t model -f
#
# or, if you want to clean filtered data (assuming Model.sites.stack is a subset of Model.sites):
#   runNikoCoordModel.csh -s Model.stack -t clean -u  
#   runNikoCoordModel.csh -s Model.stack -t model
#   runNikoCoordModel.csh -s Model.stack -t stack
#   runNikoCoordModel.csh -s Model.sites -t clean -u -f
#   runNikoCoordModel.csh -s Model.sites -t model 
#   runNikoCoordModel.csh -s Model.sites -t model -f

# by Rosanne Nikolaidis, June, 2002

if ($#argv < 1) then
        sed -e "1 d" -e "s/#//" -e "/Rosanne/q" $0
        exit
endif

# pj, 10/29/2002: create lockfile
#set lockfile = /shared/var/gpsproc/log/runNikoCoordModel.csh.lock
#touch $lockfile

# defaults:
# pj, 05/30/2003: no longer needed.  write *r.neu files from createRawSeries
# directly to wdir.  removed copy statement from ddir to wdir below.
#set ddir = /shared/data/www/sopac/input/processing/timeseries/neu  #change this to wherever your neu are
set wdir = /archive/projects/sugar/timeseries_xml
set update = no
set filter = 0

foreach a ($argv)
switch($a)
        case -s:
                set site_list = $argv[2]
                breaksw
        case -t:
                set run_type = $argv[2]
                breaksw
        case -u:
                set update = yes
                breaksw
        case -f:
                set filter = 1
                breaksw
endsw
shift
end

cd $wdir
set sites = \'$site_list[1]\'

############################################################
# update the raw neu files if necessary (works for list of sites only)
############################################################

# pj removed this code, 05/30/2003: raw neu are now in working dir
#if ($update == yes) then
## get new raw neu files from data directory
#  \rm $wdir/*r.neu
#  set n = 1
#  set nmax = `cat $site_list|wc -l` 
#  while ($n <= $nmax)
#     set site = `sed -e "$n,$n p" -e "1,$ d" $site_list`
#     # skip comment lines
#     if (`echo $site|cut -c1` != "#") then
#        \cp -f $ddir/$site[1]r.neu $wdir/
#     endif
#     @ n++
#  end
#endif 

############################################################
# clean the raw neu files
############################################################
if ($run_type == clean) then
cd $wdir/
date > start_clean.time
  matlab -nosplash -nodisplay -nojvm << EOF_matlab 
  runNikoCoordModelSopac
  'clean'
  3
  365
  $sites
  $filter
  'Model.offsets'
  1
  1
  1
  'Model.decay' 
  'Model.vbreak'
  0
  0
  1
  2
  quit
  EOF_matlab
date > stop_clean.time

############################################################
# model the clean neu files (no regional filtering)
############################################################
else if ($run_type == model) then
cd $wdir/

if ($filter == 0) then
   /bin/rm vel_unf.gmt
else
   /bin/rm vel_flt.gmt
endif

date > start_model.time
matlab -nosplash -nodisplay -nojvm << EOF_matlab 
runNikoCoordModelSopac
'model'
$sites
$filter
'Model.offsets'
1
1
1
'Model.decay'
'Model.vbreak'
1
1
1
2
quit
EOF_matlab
date > stop_model.time

############################################################
# stack the postfit residuals for fiducial sites 
############################################################
else if ($run_type == stack) then
cd $wdir/
date > start_stack.time
matlab -nosplash -nodisplay -nojvm << EOF_matlab 
regional_stack
$sites
'n'
3
regional_stack
$sites
'e'
3
regional_stack
$sites
'u'
3
quit
EOF_matlab
date > stop_stack.time

endif

# pj, 10/29/2002: remove lockfile
#echo "removing lockfile"
#\rm -f $lockfile

exit


