#! /bin/csh -f     
# The C-SHELL script performs one day GETD processing 
# for near real time data on STAR servers.
# The data paths and scripts are different from 
# OSPO servers.
# Input/Output paths for METEOROLOGY data processing

#setenv LD_LIBRARY_PATH /data/home004/li.fang/softwares/GETDlib_9187/lib
setenv LD_LIBRARY_PATH /home/tree/packages/lib
setenv GADDIR /data/home004/li.fang/myGrads/grads-2.2.0
setenv GAUDPT /data/home004/li.fang/myGrads/grads-2.2.0
#setenv MYGRB2 /data/home004/li.fang/softwares/GETDlib_9187/wgrib2lib/wgrib2/wgrib2/wgrib2
setenv MYGRB2 /home/tree/Software/GETD/run/wgrib2

#setenv HOME_DIR /data/data123/GETD_CWB_v1.1
setenv HOME_DIR /home/tree/Software/GETD

# Don't change the following directory unless you have to
# these paths are related with the settings in the cfg file
# and must be tested if changed.
setenv WORK_DIR $HOME_DIR/run
setenv CLIMO_DIR $HOME_DIR/climo

setenv local_wgrib2 $WORK_DIR/wgrib2 

setenv IN_DATA_DIR  $HOME_DIR/data
#setenv IN_DATA_DIR  /data/data123/zpli/UnitTest/data

setenv OUT_DATA_DIR  $HOME_DIR/data
setenv TMP_DIR  $HOME_DIR/data/tmp
setenv STATIC_DATA_DIR $HOME_DIR/data/static
#setenv STATIC_DATA_DIR /data/data123/zpli/UnitTest/data/static

setenv SCRIPT_DIR $HOME_DIR/run
setenv CUR_DAY_LOG $WORK_DIR/cur_day.log

# Climatology input data files
setenv climo_in_east_dir  $STATIC_DATA_DIR/EAST_MASK
setenv east_max_tb11_dir  $climo_in_east_dir/MAX_TB11_SMOOTH
setenv east_diff_neg_dir  $climo_in_east_dir/CHAN_DIFF_NEG_DIFF_SMOOTH
setenv east_diff_pos_dir  $climo_in_east_dir/CHAN_DIFF_POS_DIFF_SMOOTH
setenv east_albedo_dir    $climo_in_east_dir/VIS_ALBEDO_SMTH
setenv east_rise15_dir    $climo_in_east_dir/SUNRISE

setenv climo_in_west_dir $STATIC_DATA_DIR/WEST_MASK
setenv west_max_tb11_dir $climo_in_west_dir/MAX_TB11_SMOOTH
setenv west_diff_neg_dir $climo_in_west_dir/CHAN_DIFF_NEG_DIFF_SMOOTH
setenv west_diff_pos_dir $climo_in_west_dir/CHAN_DIFF_POS_DIFF_SMOOTH
setenv west_albedo_dir   $climo_in_west_dir/VIS_ALBEDO_SMTH
setenv west_rise15_dir   $climo_in_west_dir/SUNRISE

# All raw data inputs into GETD

setenv GETD_MET_GFS_IN $IN_DATA_DIR/GFS

setenv GETD_AHI_BT $IN_DATA_DIR/AHI/BT
setenv GETD_AHI_CLD $IN_DATA_DIR/AHI/cloud
setenv GETD_AHIBTP_EAST $IN_DATA_DIR/AHIBTP/EAST
setenv GETD_AHIBTP_WEST $IN_DATA_DIR/AHIBTP/WEST
#directory contains the climatology data files
setenv GETD_AHI_EAST_MASK $IN_DATA_DIR/climo/EAST_MASK
setenv GETD_AHI_WEST_MASK $IN_DATA_DIR/climo/WEST_MASK

#AHI GSIP 
setenv GETD_GSIP_EAST $IN_DATA_DIR/GSIP/Ori
#No more GSIP West
setenv GETD_GSIP_WEST /data/data123/lifang/GET-D_2km/GSIP_NEW/Archive/GSIP_G17/CONUS/

setenv GETD_SNOW_IN $IN_DATA_DIR/SNOWMASK

setenv GETD_EVI_IN $IN_DATA_DIR/weekly_evi

#Intermediate data directory, should not be changed
#Data holding the GFS meteorological data in common grid
#setenv GETD_GFS_COMMON $IN_DATA_DIR/GFS_COMMON
setenv GETD_CFS_COMMON $IN_DATA_DIR/CFS_COMMON

setenv GETD_TRAD_EAST $IN_DATA_DIR/TRAD/EAST
setenv GETD_TRAD_WEST $IN_DATA_DIR/TRAD/WEST
#
setenv GETD_LST_EAST $IN_DATA_DIR/LST/EAST
setenv GETD_LST_WEST $IN_DATA_DIR/LST/WEST
#
setenv GETD_AHILSTP_EAST $IN_DATA_DIR/AHILSTP/EAST
setenv GETD_AHILSTP_WEST $IN_DATA_DIR/AHILSTP/WEST
#
setenv GETD_AHIBTP_EAST $IN_DATA_DIR/AHIBTP/EAST
setenv GETD_AHIBTP_WEST $IN_DATA_DIR/AHIBTP/WEST


setenv GETD_GSIP_UTP $IN_DATA_DIR/GSIP/UTP

#weekly EVI composite from VIIRS GVF product
setenv GETD_WEEKLY_EVI_IN $IN_DATA_DIR/weekly_evi
setenv GETD_EVI $IN_DATA_DIR/EVI

#Alexi input data in local time
setenv GETD_ALEXI_LST $IN_DATA_DIR/ALEXI_LST
setenv GETD_ALEXI_MET $IN_DATA_DIR/ALEXI_MET
setenv GETD_ALEXI_LAI $IN_DATA_DIR/ALEXI_LAI
setenv GETD_ALEXI_SNOW $IN_DATA_DIR/ALEXI_SNOW
setenv GETD_ALEXI_INSO $STATIC_DATA_DIR/inso

#GETD output
setenv GETD_ET_OUT  $OUT_DATA_DIR/ET
setenv GETD_ESI_OUT $OUT_DATA_DIR/ESI

# GETD4CWA version for delivery
setenv VERSION "1.1"

#
# 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 AHI data     
#    a. The process requires the IDL program to generate the
#       AHI data remap the AHI EAST and WEST data
#       into 4-km grid.
#    b. Run run_daily_AHI_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 AHI 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  


# 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

@ prev_doy = $iday - 1

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 = `./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

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

TIME_step:
$SCRIPT_DIR/run_daily_SUNTIMES.csh $in_year $iday

#goto cfs_step2
#goto AHI_step
#goto gsip_step
#goto snow_step
#goto lai_step
#goto et_step
#goto esi_compute
#goto esi_step
#goto img_step

# shift one day for Asian region
cfs_step:
# METEOROLOGY data step 1
#This is to run the current day's data 
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/$in_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/$in_year$prev_day $GETD_CFS_COMMON/$in_year$idoy

cfs_step3:
$SCRIPT_DIR/run_daily_CFS_STEP2.csh $in_year $iday

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

#CFSR insolation
rm $GETD_ALEXI_MET/$in_year$idoy/inso_24h
rm $GETD_ALEXI_MET/$in_year$idoy/inso15
rm $GETD_ALEXI_MET/$in_year$idoy/inso55
ln -s $GETD_ALEXI_MET/$in_year$idoy/swdn_24h $GETD_ALEXI_MET/$in_year$idoy/inso_24h
ln -s $GETD_ALEXI_MET/$in_year$idoy/swdn15 $GETD_ALEXI_MET/$in_year$idoy/inso15
ln -s $GETD_ALEXI_MET/$in_year$idoy/swdn55 $GETD_ALEXI_MET/$in_year$idoy/inso55

else
echo "Missing CFSR on Previous day - $GETD_CFS_COMMON/$in_year$prev_day"
goto error
endif

AHI_step:

  echo "Processing H08 AHI Band14"
  mkdir $GETD_AHIBTP_EAST/$in_year$idoy
  rm $WORK_DIR/GOES_EAST13_bt11_clear*.dat
  $SCRIPT_DIR/run_AHIBand14_PREPROCESS.csh $in_year $iday

if ( -d $GETD_AHIBTP_EAST/$in_year$prev_day ) then

  rm -rf $GETD_ALEXI_LST/$in_year$idoy
  ln -s $GETD_AHIBTP_EAST/$in_year$prev_day/*.dat $WORK_DIR
  echo $GETD_AHIBTP_EAST/$in_year$prev_day

  set jj = 0
  set nexthr = 0

  while ($jj <= 23)
     if($jj < 10)then
     set curhr = "0"$jj
     else
     set curhr = $jj
     endif
     @ nexthr = $jj + 24
     set AHIday2fl = ${GETD_AHIBTP_EAST}/$in_year$idoy/GOES_EAST13_bt11_clear${curhr}00.dat
     set AHIday2outfl = GOES_EAST13_bt11_clear${nexthr}00.dat
     ln -s $AHIday2fl $WORK_DIR/$AHIday2outfl

    @ jj++
  end

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

   mkdir $GETD_ALEXI_LST/$in_year$idoy
   cp $GETD_TRAD_EAST/$in_year$idoy/trad1.dat $GETD_ALEXI_LST/$in_year$idoy/lst15
   cp $GETD_TRAD_EAST/$in_year$idoy/trad2.dat $GETD_ALEXI_LST/$in_year$idoy/lst55

   rm $WORK_DIR/GOES*clear*.dat
else
echo "Missing TIR on Previous day!"
goto error
endif

# GSIP step 1, 2, 3
gsip_step:

$SCRIPT_DIR/run_daily_NEWGSIP_STEP1_2.csh $in_year $iday

if ( $status != 0 ) then
  goto error
endif

set gsipfile1 = $GETD_GSIP_UTP/$in_year/"inso_na_24h_$in_year$prev_day.dat"
if ( -e $gsipfile1 ) then
set gsipfile2 = $GETD_GSIP_UTP/$in_year/"inso_na_24h_$in_year$idoy.dat"

$SCRIPT_DIR/run_daily_NEWGSIP_STEP3.csh $in_year $iday $gsipfile1 $gsipfile2

if ( $status != 0 ) then
  goto error
endif

else
echo "Missing GSIP Insolation on Previous day!"
echo "$gsipfile1"
goto error
endif

snow_step:
# SNOWMASK
# $SCRIPT_DIR/download_daily_snowmask.csh $in_year $iday

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

lai_step:
# MODIS LAI
set vlaifolder = $HOME_DIR/data/EVI
rm -rf $GETD_ALEXI_LAI/$in_year$idoy
mkdir $GETD_ALEXI_LAI/$in_year$idoy
ln -s $vlaifolder/$in_year/MODIS_LAI_2km_${in_year}${idoy}_GETD.dat $GETD_ALEXI_LAI/$in_year$idoy/VLAI

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

esi_compute:
# compute the potential ET
$SCRIPT_DIR/run_daily_ESI_compute.csh $in_year $iday
if ( $status != 0 ) then
  echo Error on run_daily_ESI_compute $in_year $iday
  @ er ++
  goto error
endif

esi_smooth:
# compute ESI smooth
$SCRIPT_DIR/run_daily_ESI_smooth.csh $in_year $iday
if ( $status != 0 ) then
  echo Error on run_daily_ESI_smooth $in_year $iday
  @ er ++
  goto error
endif

esi_composite:
#ESI composite
rm $GETD_ESI_OUT/$in_year$idoy/NA_DFPPM_*.dat*
$SCRIPT_DIR/run_daily_ESI_composite.csh $in_year $iday
if ( $status != 0 ) then
  echo Error on run_daily_ESI_composite $in_year $iday
  @ er ++
  goto error
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

