#!/bin/tcsh

# This is the master script, which is run to prepare the input data (met, rwp) for WINDoe
# for post-processing

setenv combination halo287
setenv campaign fireWx
setenv siteid cl3
setenv site cl3
setenv campaigndir /Projects/fireWx/processed/postprocessing/WINDoe
setenv windoedir $campaigndir/$siteid/$combination
setenv siteidmet cl3
setenv met none
setenv lidar halo287
# magnetic compass reading 272, declination 7.7 E, needs to be added to get true north (August 26 2025)
setenv lidarheading 276
setenv lidarfid User5
setenv lidarraw HALO287
setenv rawmetdir /Projects/$campaign/modelobs/Measurement_Data/Stations/BIDout
setenv rawlidardir /Projects/fireWx/obs/Lidar_data/$siteid.$lidar
#setenv priorstation DNR
setenv priorstation TBM
setenv priordir $campaigndir/CLMprior
setenv funcdir $campaigndir/functions
setenv oversion c1_70only

mkdir -p $campaigndir/$siteid/$combination/retrieval_output
mkdir -p $campaigndir/$siteid/$combination/retrieval_output_small
mkdir -p $campaigndir/$siteid/$combination/quicklooks
mkdir -p $campaigndir/$siteid/$combination/logs
mkdir -p $rawlidardir

cd $windoedir/code
set pwd = $PWD
#date to process
if ($#argv == 0) then
   # current time in minutes since midnigght
  set currenttime = `date "+%H*60+%M" | bc`
  if ( $currenttime <= 55 ) then
      echo "Processing data from yesterday"
      setenv pdate `date -d "yesterday" "+%Y%m%d"`
  else
      echo "Processing data from today"
      setenv pdate `date -d "today" "+%Y%m%d"`
  endif
else if($#argv == 1) then
  echo "Processing data from specified date"
  setenv pdate $argv[1]
else
  echo "USAGE: %0 <yyyymmdd>"
  echo "   If yyyymmdd is set, then process that date"
  echo "   If yyyymmdd is not set, then process yesterday"
  exit
endif

set mlog = $campaigndir/$siteid/$combination/logs/mlog_cronjob_$pdate.txt
set month=`echo $pdate | awk '{print substr($0,5,2)}'`

#get date of next and previous day
set nextdate=`date -d  "$pdate +1 day" +"%Y%m%d"`
set prevdate=`date -d  "$pdate -1 day" +"%Y%m%d"`

mkdir -p $rawlidardir/netcdf/$pdate
mkdir -p $windoedir/quicklooks_lidar


echo "  >>>>> Processing " $pdate >>& $mlog
echo "            Starting $0 on `date`" >>& $mlog

setenv OPENBLAS_NUM_THREADS 4
setenv HDF5_USE_FILE_LOCKING FALSE

# avoid running retrieval using data from last 30 min
set hour = `date +"%H"`
set minute = `date +%M`
set decimal_minute = `echo "scale=2; $minute / 60" | bc`
set hour_decimal = `echo "$hour + $decimal_minute - 0.5" | bc`

eval `conda shell.tcsh activate tropoe`
/usr/bin/nice -n 10 python $funcdir/WINDoe.py $pdate $windoedir/code/vipfiles/vip.${siteid}.${combination}.${oversion}.txt $priordir/Xa_Sa_datafile.$priorstation.month_$month.covinflated200.cdf --shour 15 --ehour 18 --verbose 3
#(/usr/bin/nice -n 10 python $funcdir/WINDoe.py $pdate $windoedir/code/vipfiles/vip.${combination}.${oversion}.txt $priordir/Xa_Sa_datafile.$priorstation.month_$month.covinflated20.cdf --shour 0 --ehour $hour_decimal --verbose 3) >>&$mlog
eval `conda shell.tcsh deactivate`

# create WINDoe quicklooks
echo "    >> Create WINDoe quicklooks " >>& $mlog
eval `conda shell.tcsh activate windoe_proc`
#(python $funcdir/plot_WINDoe.py ) >>& $mlog
python $funcdir/plot_WINDoe.py
eval `conda shell.tcsh deactivate`
exit
# reduce file size by dropping  absdim and arbsdim before copying to ftp
#echo "    >> Reduce retrieval_output file size " >>& $mlog
eval `conda shell.tcsh activate tropoe`
(python $funcdir/reduce_retrieval_filesize.py ) >>& $mlog
#python $funcdir/reduce_retrieval_filesize.py
eval `conda shell.tcsh deactivate`



        # Pushing images / data back to my ftp
#echo "    >> Copy results to ftp" >>& $mlog
($funcdir/push_data_fireWx2ftp.sh) >>& $mlog

echo "            Finished WINDoe on `date`" >>& $mlog
exit
