#!/bin/bash

source ~/.bashrc
source ~/.bash_profile

umask 0022

cd "$(dirname "$0")"
work_dir="$( pwd -P )"

. ./address_email.sh

mode='manual'
mission='cosmic2'
days_ago=1

exec 6>&1  # Link file descriptor #6 with stdout, 
           # (necessary, otherwise the Restore stdout won't work)

LOGDIR=${work_dir}/logs/UCARatmPhs  ## pwd is useful in email alert
while getopts l:g:d: optionName; do
  case $optionName in
  l)   mode=${OPTARG};;
  g)   mission=${OPTARG};;
  d)   days_ago=${OPTARG};;
  ?)   echo "Usage: $0  -l logfile_name \n";;
  esac
done

if [ ${mode} == 'manual' ]; then
  start_year=2025
  start_month=9
  start_day=9

  end_year=2025
  end_month=9
  end_day=9

  mission='metopa2016'
  mission='paz'
  mission='tdx'
  mission='tsx'
  mission='grace'
  mission='kompsat5'
  mission='spire'
  mission='metopb2016'
  mission='cosmic2021'
  mission='cosmic2013'
  mission='cosmic'
  mission='metopa'
  mission='geooptrt' # 2020-12-15 to 2021-01-14 ; geoopt rt
                     # 2021-03-17 to 2021-09-16
                     # 2022-03-17 to 2022-10-21
  mission='geoopt'   # 2020-12-01 to 2021-01-14 ; geoopt pp
                     # 2021-03-08 to 2021-09-16
  mission='metopb'
  mission='metopc'
  mission='cosmic2'

elif [ ${mode} == 'cron' ]; then
  days_ago_max=`expr $days_ago + 7`
  days_ago_max=`expr $days_ago + 4`

  start_year=`date +%Y -d "${days_ago_max} day ago"`
  start_month=`date +%-m -d "${days_ago_max} day ago"`
  start_day=`date +%-d -d "${days_ago_max} day ago"`

  end_year=`date +%Y -d "${days_ago} day ago"`
  end_month=`date +%-m -d "${days_ago} day ago"`
  end_day=`date +%-d -d "${days_ago} day ago"`
  
  # mission from '-g' $OPTARG

  ## remove old log files
  find $LOGDIR -name "*.log" -type f -mtime +7 -exec rm -f {} \;

elif [ ${mode} == 'cron_month' ]; then

  month_ago1=3
  month_ago2=3

#  month_ago1=5
#  month_ago2=4

  start_year=`date +%Y -d "${month_ago1} month ago"`
  start_month=`date +%-m -d "${month_ago1} month ago"`
  start_day=1

  end_year=`date +%Y -d "${month_ago2} month ago"`
  end_month=`date +%-m -d "${month_ago2} month ago"`
  end_day=$(cal $end_month $end_year | tail -n +3 | wc -w)

  # echo $start_year-$start_month-$start_day  $end_year-$end_month-$end_day

  # mission from '-g' $OPTARG

fi

in_path=/data2/xinjiaz/data_GPSRO/UCAR/${mission}
out_path=./input_atmPhs/${mission^^}/UCAR
mission3=${mission}
atmPhs='conPhs'

if [ ${mission} == cosmic2 ]; then
  process=nrt
  mission2=${mission}
  cut_dir=3

elif [ ${mission} == geooptrt ];then
  in_path=${in_path}/DO1
  process=nrt
  mission2='geoopt/noaa'
  mission3=cicero
  cut_dir=4
  out_path=./input_atmPhs/GeoOptics_nrt/UCAR

elif [ ${mission} == geoopt ];then
  in_path=${in_path}/DO1
  process=postProc
  mission2='geoopt/noaa'
  mission3=cicero
  cut_dir=4
  out_path=./input_atmPhs/GeoOptics/UCAR

elif [ ${mission} == spire ];then
  in_path=${in_path}/DO1
  process=nrt
  mission2=spire/noaa
  cut_dir=4

else  ## k5/paz/metopa/b/c/tsx/tdx
  process=postProc
  mission2=${mission}
  cut_dir=3

  if [ ${mission} == kompsat5 ];then
    out_path=./input_atmPhs/KOMPSAT/UCAR
  elif [ ${mission} == paz ];then
    out_path=./input_atmPhs/PNOTS/UCAR
  elif [[ ${mission} == metop? ]];then
    out_path=./input_atmPhs/GRAS/UCAR
    atmPhs='atmPhs'
  elif [[ ${mission} == metop?2016 ]];then
    out_path=./input_atmPhs/GRAS/UCAR
    mission2=${mission:0:6}  ## correct, don't change
    mission3=${mission2}
    atmPhs='atmPhs'
    process=repro2016
    in_path=/data2/xinjiaz/data_GPSRO/UCAR/${mission2}
  elif [ ${mission} == cosmic ];then
    out_path=./input_atmPhs/COSMIC1/UCAR
    mission2=cosmic1
    atmPhs='atmPhs'
    in_path=/data2/xinjiaz/data_GPSRO/UCAR/cosmic1
  elif [ ${mission} == cosmic2013 ];then
    out_path=./input_atmPhs/COSMIC1/UCAR
    mission2=cosmic1
    mission3=cosmic
    atmPhs='atmPhs'
    process=repro2013
    in_path=/data2/xinjiaz/data_GPSRO/UCAR/cosmic1_v2013
  elif [ ${mission} == cosmic2021 ];then
    out_path=./input_atmPhs/COSMIC1_v2021/UCAR
    mission2=cosmic1
    mission3=cosmic
    process=repro2021
    in_path=/data2/xinjiaz/data_GPSRO/UCAR/cosmic1_v2021
  elif [ ${mission} == tdx ];then
    out_path=./input_atmPhs/IGOR/UCAR
  elif [ ${mission} == tsx ];then
    out_path=./input_atmPhs/IGOR/UCAR
    atmPhs='atmPhs'
  elif [ ${mission} == grace ];then
    atmPhs='atmPhs'
  fi
fi

mkdir -p ${out_path}

for (( year_loop = $start_year; year_loop <= $end_year; year_loop++ )); do
 thisYEAR=${year_loop}

 month_begin=1
 month_over=12
 if [ $year_loop -eq $start_year ];then month_begin=$start_month; fi
 if [ $year_loop -eq $end_year ];then month_over=$end_month; fi

 for (( month_loop = $month_begin; month_loop <= $month_over; month_loop++ )); do 
  thisMONTH=$(printf '%02d' $month_loop)

  day_begin=1
  day_over=$(cal $month_loop $year_loop | tail -n +3 | wc -w)
  if [ $year_loop -eq $start_year ] && [ $month_loop -eq $start_month ]; then day_begin=$start_day; fi
  if [ $year_loop -eq $end_year ]   && [ $month_loop -eq $end_month ];   then day_over=$end_day; fi

  for (( day_loop = $day_begin; day_loop <= $day_over; day_loop++ )); do 
   thisDAY=$(printf '%02d' $day_loop)

   thisDATE=${thisYEAR}-${thisMONTH}-${thisDAY}
   thisDOY=`date +%j -d "${thisDATE}"`

   exec 1>&6 # Restore stdout, to show the echo message on screen, for monitor in manual mode

   echo -e "$(basename $0) (${mission}) in ${thisDATE} run at `date` \n"

   datestring=`date +%m.%d.%H`
   days_ago_this=$(( ( $(date +%s) - $(date -d "${thisYEAR}-${thisMONTH}-${thisDAY}" +%s) ) /(24 * 60 * 60 ) ))
   filename="${mode}_${thisDATE}_${mission}_${datestring}_${days_ago_this}days.log"
   mkdir -p $LOGDIR/
   exec > $LOGDIR/$filename   2>&1 


   # ---------------------
   raw_file=${in_path}/tarball/level1b/${thisYEAR}/${thisDOY}/${atmPhs}_${process}_${thisYEAR}_${thisDOY}.tar.gz
   outfile=${out_path}/${mission3}_${thisDATE}.sav


   # ---------------------
   if true; then
#   if false; then
     # ---
#     timeout 1h wget -a "$LOGDIR/$filename" --no-check-certificate -rN -np -nH -nv --cut-dir=${cut_dir} https://data.cosmic.ucar.edu/gnss-ro/${mission2}/${process}/level1b/${thisYEAR}/${thisDOY}/ -P ${in_path}/tarball
     timeout 1h wget -a "$LOGDIR/$filename" --no-check-certificate -rN -np -nH -nv --cut-dir=${cut_dir} https://data.cosmic.ucar.edu/gnss-ro/${mission2}/${process}/level1b/${thisYEAR}/${thisDOY}/$(basename $raw_file) -P ${in_path}/tarball

     # ---
     if [ ! -f ${raw_file} ]; then
       echo " wget -rN -np -nH -nv --cut-dir=${cut_dir} https://data.cosmic.ucar.edu/gnss-ro/${mission2}/${process}/level1b/${thisYEAR}/${thisDOY} -P ${in_path}/tarball"
       echo "${raw_file} non-exist"
       continue
     fi
   fi


   # ---------------------
   if true; then
#   if false; then
     # ---------------------
     # judge the necessarity to run it

     # condition 1 for skip: rawfile non-exist
     is_jump0=0
     if [ -f ${raw_file} ];then
       echo -e "\ninput: $(stat -c%y ${raw_file}) $raw_file"
     else
       echo -e "\ninput: Missing input at ${raw_file}"
       if [ $mode == 'cron' ] ;then
         is_jump0=1
       fi
     fi

     # condition 2 for skip: rawfile is older than outfile
     # only works in cron mode
     is_jump1=0
     if [ -f ${outfile} ]; then
       echo output: $(stat -c%y ${outfile}) "$outfile"
       if [ $mode == 'cron' ] && [ -f ${raw_file} ] && [ $(stat -c%Y ${raw_file}) -lt $(stat -c%Y ${outfile}) ] ;then  # input is later than output
         is_jump1=1
       fi
     else
       echo output: Missing output at "${outfile}"
     fi

     if [ $is_jump0 -eq 1 ] || [ $is_jump1 -eq 1 ] ; then
       echo -e "skip today\n"
       echo "rm -fr ${in_path}/tarball/level[01]*/${thisYEAR}/${thisDOY}/"
       rm -fr ${in_path}/tarball/level[01]*/${thisYEAR}/${thisDOY}/
       continue
     fi
   fi


   # ---------------------
   if true; then
#   if false; then
     # ---
     tar -tf ${raw_file} &> /dev/null; return_code=$?
     [ $return_code -ne 0 ] && echo -e "\n tarball demaged: ${raw_file}" && continue

     rm -fr ${in_path}/level1b/${atmPhs}/${thisYEAR}/${thisDOY}/
     mkdir -p ${in_path}/level1b/${atmPhs}/${thisYEAR}/${thisDOY}/
     timeout 3h tar -xzf ${raw_file} -C ${in_path}/level1b/${atmPhs}/${thisYEAR}/${thisDOY}/
   fi

   # ---------------------
   if true; then
#   if false; then
     # ---
     return_code=0  ## 2 is good, 0 is error
     /usr/local/bin/idl<<EOF
     !quiet=1
     @codes/precom_UCARatmPhs
;     Read_UCARatmPhs, '${in_path}/level1b', '${outfile}', '${thisYEAR}.${thisDOY}', version='${mission}', return_code=return_code
     Read_UCARatmPhs, '${in_path}/level1b', '${outfile}', '${thisYEAR}/${thisDOY}', version='${mission}', return_code=return_code

     exit,status=return_code
EOF
     return_code=$?
     [ $return_code -eq 0 ] && echo -e "check :\n $LOGDIR/$filename " | mail -s "$(basename $0) (${mission}) is failed for $mode in $thisDATE" $email_address
     [ $return_code -eq 1 ] && echo -e "check :\n $LOGDIR/$filename " | mail -s "UCAR2 atmPhs ${mission} is non-exist in $thisDATE" $email_address

     echo -e "\nIDL finish at: `date +%Y.%m.%d.%H:%M`"
   fi

   # ---------------------
   if true; then  
#   if false; then  ## not download those data for now

     timeout 1h wget -a "$LOGDIR/$filename" --no-check-certificate -rN -np -nH -nv --cut-dir=${cut_dir} https://data.cosmic.ucar.edu/gnss-ro/${mission2}/${process}/level1b/${thisYEAR}/${thisDOY}/leoOrb_${process}_${thisYEAR}_${thisDOY}.tar.gz -P ${in_path}/tarball

     # --- download level1a ---  
     timeout 1h wget -a "$LOGDIR/$filename" --no-check-certificate -rN -np -nH -nv --cut-dir=${cut_dir} https://data.cosmic.ucar.edu/gnss-ro/${mission2}/${process}/level1a/${thisYEAR}/${thisDOY}/ -P ${in_path}/tarball

     ##            level1a level1a level1a level1a level1b level1b level1b level1b
     product_list='leoAtt  opnGns  opnGps  podCrx  gpsBit  leoClk  leoOrb  podTc2'

     # --- download level0 ---  
     if [ ${mission} == cosmic2 ]; then
       timeout 1h wget -a "$LOGDIR/$filename" --no-check-certificate -rN -np -nH -nv --cut-dir=${cut_dir} https://data.cosmic.ucar.edu/gnss-ro/${mission2}/${process}/level0/${thisYEAR}/${thisDOY}/ -P ${in_path}/tarball

       ##            level0 level1a level1a level1a level1b level1b
       product_list='trgLv0 leoAtt  opnGns  podCrx  leoOrb podTc2'
     fi

     for product in $product_list ; do 
       if [ $product == 'trgLv0' ]; then
         level='level0'
       elif [ $product == 'leoAtt' ] || [ $product == 'opnGns' ] || [ $product == 'opnGps' ] || [ $product == 'podCrx' ]; then
         level='level1a'
       elif [ $product == 'gpsBit' ] || [ $product == 'leoClk' ] || [ $product == 'leoOrb' ] || [ $product == 'podTc2' ]; then
         level='level1b'
       fi
       raw_file=${in_path}/tarball/${level}/${thisYEAR}/${thisDOY}/${product}_${process}_${thisYEAR}_${thisDOY}.tar.gz

       if [ -f ${raw_file} ]; then 
         rm -fr ${in_path}/${level}/${product}/${thisYEAR}/${thisDOY}/
         mkdir -p ${in_path}/${level}/${product}/${thisYEAR}/${thisDOY}/
         timeout 3h tar -xzf ${raw_file} -C ${in_path}/${level}/${product}/${thisYEAR}/${thisDOY}/
       fi
     done

     echo "rm -fr ${in_path}/tarball/level[01]*/${thisYEAR}/${thisDOY}/"
     rm -fr ${in_path}/tarball/level[01]*/${thisYEAR}/${thisDOY}/
  
   fi
   # ---------------------


  done  # day
 done  # month
done # year

exec 1>&6 # Restore stdout, to show the echo message on screen, for monitor purpose
date

exit
