#! /bin/csh -f
# This C-SHELL script is to run the ESI smoothing procedure
# using ESI results from GETD.
# 1. Decide the smoothing date period by using the input data
#    and the preset number of days. The number of days should
#    be larger than  
#    Link the input data from different locations to the GETD
#    working directory for the processing.
# 3. Moved the output to the ALEXI input location
# in_et_dir - Directory contains the input time series ET/EST
#             data
# out_dir   - store the output files for ALEXI usage. 
set in_et_dir = $GETD_ET_OUT 
set work_dir  = $WORK_DIR
set out_dir   = $GETD_ESI_OUT 

# This is the number of days used for smoothing
# should be larger than 50 and smaller than 730 
set num_days=365

# Check if called properly with right command line inputs
if ($#argv != 2) then
  echo "Usage: $0 [Year] [DOY]"
  goto done
endif

## save command line args in variables
echo "Smoothing ESI on day $1 $2 with $num_days days"
set in_year=$1
set in_doy=$2

# compute the smooth starting date with 
# number of days and input date
set start_doy=0
set start_year=0
if ( $in_doy >= $num_days ) then
  @ start_doy=$in_doy - $num_days +  1
  @ start_year=$in_year
  echo From $start_year $start_doy to $in_year $in_doy
else
  @ start_doy=$in_doy - $num_days +  365 + 1 
  @ start_year=$in_year -  1
  echo From $start_year $start_doy to $in_year $in_doy
#2014 91 2014 191
endif

./getd.x -smooth GETD_smooth_ESI.cfg $start_year $start_doy $in_year $in_doy

done:
  exit 0
error:
  exit 1
