#! /bin/csh -f
# This C-SHELL script is to run the ESI computation
# using ET results from ALEXI and reference ET computed in GETD.

set in_inso_dir = "$STATIC_DATA_DIR/climinso"
set in_meteo_dir = $GETD_ALEXI_MET
set in_et_dir = $GETD_ESI_OUT
set work_dir = $WORK_DIR
set out_dir = $GETD_ESI_OUT

# 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 "Computing reference ET and ESI on day $1 $2"
set in_year=$1

#Default assume that input doy is a 3 digits number
  if ($2 < 10 ) then
    set in_doy="00"$2
  else if($2 < 100) then
    set in_doy="0"$2
  else
    set in_doy=$2
  endif


#1.1 ET
set in_file1 = $in_et_dir/$in_year$in_doy/"XLE2.bin"
set in_file1zip = $in_et_dir/$in_year$in_doy/"XLE2.bin.gz"
if ( -e $in_file1zip) then
  gunzip $in_file1zip
endif
if ( -e $in_file1) then
#Link the input files to the working directory
#  if ( -e $work_dir/XLE2.bin ) then
  rm $work_dir/XLE2.bin
#  endif
  ln -s $in_file1 $work_dir/XLE2.bin
else
  echo Cannot found input $in_file1
  goto error
endif

#1.2 Insolation
set in_file1 = $in_inso_dir/"alexi_insol55_smth_2011"$in_doy".dat"

if ( -e $in_file1) then
#Link the input files to the working directory
  rm $work_dir/inso55
  ln -s $in_file1 $work_dir/inso55
else
  echo Cannot found input $in_file1
  goto error
endif

#1.3 Temp
set in_file1 = $in_meteo_dir/$in_year$in_doy/"temp55"
set in_file1zip = $in_meteo_dir/$in_year$in_doy/"temp55.gz"
if ( -e $in_file1zip) then
  gunzip $in_file1zip
endif

if ( -e $in_file1) then
#Link the input files to the working directory
  rm $work_dir/temp55
  ln -s $in_file1 $work_dir/temp55
else
  echo Cannot found input $in_file1
  goto error
endif

#1.4 SPFH
set in_file1 = $in_meteo_dir/$in_year$in_doy/"spfh55"
set in_file1zip = $in_meteo_dir/$in_year$in_doy/"spfh55.gz"
if ( -e $in_file1zip) then
  gunzip $in_file1zip
endif

if ( -e $in_file1) then
#Link the input files to the working directory
  rm $work_dir/spfh55
  ln -s $in_file1 $work_dir/spfh55
else
  echo Cannot found input $in_file1
  goto error
endif

#1.5 PSFC
set in_file1 = $in_meteo_dir/$in_year$in_doy/"psfc55"
set in_file1zip = $in_meteo_dir/$in_year$in_doy/"psfc55.gz"
if ( -e $in_file1zip) then
  gunzip $in_file1zip
endif

if ( -e $in_file1) then
#Link the input files to the working directory
  rm $work_dir/psfc55
  ln -s $in_file1 $work_dir/psfc55
else
  echo Cannot found input $in_file1
  goto error
endif

#1.6 WIND
set in_file1 = $in_meteo_dir/$in_year$in_doy/"wind55"
set in_file1zip = $in_meteo_dir/$in_year$in_doy/"wind55.gz"
if ( -e $in_file1zip) then
  gunzip $in_file1zip
endif

if ( -e $in_file1) then
#Link the input files to the working directory
  rm $work_dir/wind55
  ln -s $in_file1 $work_dir/wind55
else
  echo Cannot found input $in_file1
  goto error
endif

#1.7 RNET2
set in_file1 = $in_et_dir/$in_year$in_doy/"RNET2.bin"
set in_file1zip = $in_et_dir/$in_year$in_doy/"RNET2.bin.gz"
if ( -e $in_file1zip) then
  gunzip $in_file1zip
endif

if ( -e $in_file1) then
#Link the input files to the working directory
  if ( -e $work_dir/RNET2.bin ) then
  rm $work_dir/RNET2.bin
  endif

  ln -s $in_file1 $work_dir/RNET2.bin
else
  echo Cannot found input $in_file1
  goto error
endif


# 2. Compute the reference ET and ESI in the
#    current working directory. 
  ./getd.x -refet GETD_ESI_refET.cfg $in_year $in_doy

#Move to the destination directory if have the data
set cur_out_dir = $out_dir/$in_year$in_doy

if (! -d $cur_out_dir ) then
  echo Creating $cur_out_dir
  mkdir $cur_out_dir
endif
 
  if ( -e  $work_dir/ref_et.bin ) then
    echo  mv $work_dir/ref_et.bin $cur_out_dir/REF_ET.bin
    mv $work_dir/ref_et.bin $cur_out_dir/REF_ET.bin
  else
    echo Cannot find reference ET file.
    goto error
  endif

  if ( -e  $work_dir/esi.bin ) then
    echo  mv $work_dir/esi.bin $cur_out_dir/ESI.bin
    mv $work_dir/esi.bin $cur_out_dir/ESI.bin
  else
    echo Cannot find output ESI file.
    goto error
  endif

done:
  exit 0
error:
  exit 1
