#! /bin/csh -f     
# The C-SHELL script performs one day GETD processing 
#
# 1. Process all the input raw data into ALEXI input domain.
#   1.1 GFS meteorological data
#    a. Process the GFS meteorological data into common 
#       grid in UTC time.
#    b. Extract the GFS data into NA domain and generate
#       the two morning time variables for ALEXI model.
#       This step requires two days of UTC data to generate
#       a full day 24 hours meteorological data in local time.
#
#   1.2 GOES data     
#    a. The process requires the IDL program to generate the
#       GOES data remap the GOES EAST and WEST data
#       into 4-km grid.
#    b. Run run_daily_GOES_STEP1.csh to generate the clear
#       sky surface radiance and cloud mask for EAST and WEST.
#    c. Using MET data and TRAD to do the atmospheric correction
#       to get land surface temperature (LST).
#    d. Combine the GOES EAST and WEST data into one single domain.
#
#   1.3 VIIRS LAI processing
#    a. The process requires the VIIRS EVI processing program
#       to run and generate the global EVI data in binary format.
#    b. 
#       LAI is extracted to the NA domain.
#   1.4 Snowmask processing
#       Snow mask is extracted to the NA domain.
#
# 2. Compute the daily ET using ALEXI.
#
# 3. Post processing of the ALEXI ET
#   3.1 Compute the ESI
#   3.3 Run post processing steps to generate the final products.
#
# Main loop  

# allow larger stacks and heap usage
#limit stacksize unlimited
limit datasize unlimited
limit memoryuse unlimited

# reduce glibc arena fragmentation (helps large contiguous allocs)
setenv MALLOC_ARENA_MAX 2

# Path to compiled executables inside the container
if ( ! $?BIN_DIR ) then
  setenv BIN_DIR /app/run
endif

# Set up the loop time start and end day
if( $#argv != 4 ) then
  echo "Usage: $0 [Year_BEGIN] [Year_END] [DOY_BEGIN] [DOY_END]"
  echo DOY is 3 digits
  echo $0 2014 2015 1 365
  goto error
endif
# save command line args in variables
set beginyear = $1
set endyear = $2
set begindoy = $3
set enddoy = $4

set in_year = $1

while ($in_year <= $endyear )

set iday = $3

while ($iday <= $enddoy)

if ( $iday < 1 ) then
  echo Input day: $iday is invalid
  goto error
endif

set next_year = $in_year
set next_day = $iday

set leap_flag = 0
# Check for leap year
# set leap_flag = 0
  if ( $in_year % 4 == 0 ) then
    if ( $in_year % 100 != 0 ) then
      @ leap_flag = 1
    else
      if ( $in_year % 400 == 0 ) then
        @ leap_flag = 1
      endif
    endif
  endif #Check Leap Year

if ( $leap_flag == 1 ) then

  if (  $iday < 366 ) then
    @ next_day = $iday + 1
  else
  # End of year, the next day is the
  # new year's day
    @ next_year = $in_year + 1
    @ next_day = 1
  endif

else

  if (  $iday < 365 ) then
    @ next_day = $iday + 1
  else
  # End of year, the next day is the
  # new year's day
    @ next_year = $in_year + 1
    @ next_day = 1
  endif

endif

echo Next day: $next_year, $next_day

# Make sure the day of the year is 3 digits.

if ( $iday < 10 ) then
  set idoy = "00"$iday
else if ( $iday < 100 ) then
  set idoy = "0"$iday
else
  set idoy = $iday
endif


if ( $next_day < 10 ) then
  set next_day_3 = "00"$next_day
else if ( $next_day < 100 ) then
  set next_day_3 = "0"$next_day
else
  set next_day_3 = $next_day
endif

set pre_year = $in_year
set prev_doy = $iday

  if (  $iday > 1 ) then
    @ prev_doy = $iday - 1
  else
    @ pre_year = $in_year - 1
    @ prev_doy = 365
  endif

if ( $prev_doy < 10 ) then
  set prev_day = "00"$prev_doy
else if ( $prev_doy < 100 ) then
  set prev_day = "0"$prev_doy
else
  set prev_day = $prev_doy
endif

  echo Convert input DOY to month and day
  set dummy = `$BIN_DIR/month_day.x $in_year $iday`
  set mm = $dummy[1]
  set dd = $dummy[2]
  echo Convert $iday into Month $mm Day $dd
  set month = $mm
  set day = $dd

# Record the time to start
#
set t1=`date +%s`

#goto cfs_step
#goto snow_step
#goto lai_step
#goto viirs_step
#goto et_step
#goto static_step
#goto clean_up

TIME_step:
echo "run $SCRIPT_DIR/run_daily_SUNTIMES.csh $in_year $iday"
echo "SCRIPT_DIR = $SCRIPT_DIR"
echo "GETD_ALEXI_MET = $GETD_ALEXI_MET"

$SCRIPT_DIR/run_daily_SUNTIMES.csh $in_year $iday

cfs_step:
# METEOROLOGY data step 1
rm -rf $GETD_CFS_COMMON/$in_year$idoy
mkdir $GETD_CFS_COMMON/$in_year$idoy
$SCRIPT_DIR/run_daily_CFSR_wINSO_STEP1.csh $in_year $iday

cfs_step2:

if ( -d $GETD_CFS_COMMON/$pre_year${prev_day} ) then
mv $GETD_CFS_COMMON/$in_year$idoy $GETD_CFS_COMMON/${in_year}${idoy}_backup
mv $GETD_CFS_COMMON/$next_year$next_day_3 $GETD_CFS_COMMON/${next_year}${next_day_3}_backup
cp -rf $GETD_CFS_COMMON/${in_year}${idoy}_backup $GETD_CFS_COMMON/$next_year$next_day_3
cp -rf $GETD_CFS_COMMON/$pre_year${prev_day} $GETD_CFS_COMMON/$in_year$idoy
#gunzip $GETD_CFS_COMMON/$next_year$next_day_3/*
#gunzip $GETD_CFS_COMMON/$in_year$idoy/*

else
echo "Missing CFSR on Previous day - $GETD_CFS_COMMON/$pre_year${prev_day}"
goto clean_up
endif

snow_step:
# SNOWMASK - download the data outside of container
# $SCRIPT_DIR/download_daily_snowmask.csh $in_year $iday

$SCRIPT_DIR/run_daily_SNOWMASK.csh $in_year $iday
if ( $status != 0 ) then
  goto clean_up
endif

viirs_step:
if ( ! $?S3_VIIRS_PREFIX ) setenv S3_VIIRS_PREFIX s3://et-poc-project/viirs
# destination directory used by the pipeline
set dst = "$GETD_ALEXI_LST/$in_year$idoy"
rm -rf "$dst"
mkdir -p "$dst" || exit 1

# S3 object keys
set srcN = "$S3_VIIRS_PREFIX/$in_year/VIIRSP_${in_year}${idoy}_N.dat"
set srcD = "$S3_VIIRS_PREFIX/$in_year/VIIRSP_${in_year}${idoy}_D.dat"
set srcN_gz  = "$srcN.gz"
set srcD_gz  = "$srcD.gz"

# sanity: need aws CLI
if ( ! $?AWS_PROFILE && ! $?AWS_ACCESS_KEY_ID ) then
  echo "Note: Using default AWS credential chain (instance role or default profile)."
endif
if ( `which aws >& /dev/null; echo $status` ) then
  echo "ERROR: 'aws' CLI not found in PATH inside container."
  echo "Install it (e.g. apt-get update && apt-get install -y awscli) or mount ~/.aws."
  exit 2
endif

# Sanity: gunzip present?
if ( `which gunzip >& /dev/null; echo $status` ) then
  echo "ERROR: 'gunzip' not found in PATH (package 'gzip' missing)."
  goto clean_up
endif

echo "Downloading VIIRS LST (Night, gz): $srcN_gz"
aws s3 cp --no-progress "$srcN_gz" "$dst/lst15.gz"
set rc = $status
if ( $rc == 0 ) then
  echo "Unzipping Night -> $dst/lst15"
  gunzip -f "$dst/lst15.gz" || exit 4
else
  echo "gz not found or failed; falling back to .dat: $srcN"
  goto clean_up
  aws s3 cp --no-progress "$srcN" "$dst/lst15" || exit 3
endif

# --- Day ---
echo "Downloading VIIRS LST (Day, gz):   $srcD_gz"
aws s3 cp --no-progress "$srcD_gz" "$dst/lst55.gz"
set rc = $status
if ( $rc == 0 ) then
  echo "Unzipping Day -> $dst/lst55"
  gunzip -f "$dst/lst55.gz" || exit 5
else
  echo "gz not found or failed; falling back to .dat: $srcD"
  goto clean_up
  aws s3 cp --no-progress "$srcD" "$dst/lst55" || exit 3
endif

# optional: verify sizes
ls -l "$dst/lst15" "$dst/lst55"

lai_step:
if ( ! $?S3_LAI_PREFIX ) setenv S3_LAI_PREFIX s3://et-poc-project/IIJA_LAI
# destination directory used by the pipeline
set dst = "$GETD_ALEXI_LAI/$in_year$idoy"
rm -rf "$dst"
mkdir -p "$dst" || exit 1

# S3 object keys
set src_gz   = "$S3_LAI_PREFIX/${in_year}/MODIS_LAI_1km_${in_year}${idoy}_IIJA.dat.gz"

# Download -> tmp, unzip -> VLAI, cleanup
set tmpgz = "$dst/.VLAI.tmp.gz"
echo "Downloading LAI (gz): $src_gz"
aws s3 cp --no-progress "$src_gz" "$tmpgz" || exit 3

echo "Unzipping to: $dst/VLAI"
gunzip -c "$tmpgz" > "$dst/VLAI" || exit 4
/bin/rm -f "$tmpgz"

# Confirm
if ( -s "$dst/VLAI" ) then
  echo "LAI ready: $dst/VLAI"
else
  echo "ERROR: output empty: $dst/VLAI"
  goto clean_up
endif

#cp static data from s3 for $in_year$idoy
static_step:
if ( ! $?S3_STATIC_PREFIX ) setenv S3_STATIC_PREFIX s3://et-poc-project/Static/climinso

set dst = "$STATIC_DATA_DIR/climinso"
# S3 object keys
set srcN_gz = "$S3_STATIC_PREFIX/alexi_insol15_smth_2011${idoy}.dat.gz"
set srcD_gz = "$S3_STATIC_PREFIX/alexi_insol55_smth_2011${idoy}.dat.gz"

# --- static t15 ---
echo "Downloading STATIC inso15 $srcN_gz"
aws s3 cp --no-progress "$srcN_gz" "$dst"
set rc = $status
if ( $rc == 0 ) then
  echo "Unzipping static inso15 -> $dst/alexi_insol15_smth_2011${idoy}.dat"
  gunzip -f "$dst/alexi_insol15_smth_2011${idoy}.dat.gz" || exit 4
else
  echo "gz not found or failed: $srcN_gz" || exit 3
endif

# --- static t55 ---
echo "Downloading STATIC inso55 $srcD_gz"
aws s3 cp --no-progress "$srcD_gz" "$dst"
set rc = $status
if ( $rc == 0 ) then
  echo "Unzipping static inso55 -> $dst/alexi_insol55_smth_2011${idoy}.dat"
  gunzip -f "$dst/alexi_insol55_smth_2011${idoy}.dat.gz" || exit 4
else
  echo "gz not found or failed: $srcD_gz" || exit 3
endif


#ET 
et_step:
#remove the old results files
rm -rf $GETD_ET_OUT/$in_year$idoy
rm -rf $GETD_ESI_OUT/$in_year$idoy

$SCRIPT_DIR/run_VLAI_ALEXI.csh $in_year $iday
if ( $status != 0 ) then
  echo Error on $in_year $iday
  @ er ++
  goto error
endif

#clean up
clean_up:
rm -f $STATIC_DATA_DIR/climinso/*.dat
rm -rf $GETD_ALEXI_LAI/$in_year$idoy
rm -rf $GETD_ALEXI_LST/$in_year$idoy
rm -rf $GETD_ALEXI_SNOW/$in_year$idoy
rm -rf $GETD_ALEXI_MET/$in_year$idoy
gzip $GETD_ET_OUT/$in_year$idoy/*.bin
gunzip $GETD_ET_OUT/$in_year$idoy/EDAY.bin

if ( -d $GETD_CFS_COMMON/${in_year}${idoy}_backup ) then
rm -rf $GETD_CFS_COMMON/$in_year$idoy
rm -rf $GETD_CFS_COMMON/${next_year}${next_day_3}
mv $GETD_CFS_COMMON/${in_year}${idoy}_backup $GETD_CFS_COMMON/$in_year$idoy
mv $GETD_CFS_COMMON/${next_year}${next_day_3}_backup $GETD_CFS_COMMON/$next_year$next_day_3
endif

  set t2=`date +%s`
  set tmin
@ tmin = ( $t2 - $t1 ) / 60
echo "Data processing time is" $tmin "min"

@ iday ++

end

@ in_year ++

end


done:
  exit 0

error:
  exit 1

