#!/bin/bash
#export IDL_STARTUP=/users/rsg/observatory/idl/startup
shopt -s nullglob
self=`basename $0`
basedir=/users/rsg/observatory/nutrients
incoming=$basedir/incoming
#IDL=/users/rsg/rsi/idl87/bin/idl
#IDL=/users/rsg/rsi/idl71/bin/idl
#export DISPLAY=":2"

echo "===================================="
date
echo "rclone data from Sharepoint to Linux"
# rclone the data from the WCO sharepoint
/bin/rclone sync --update --verbose --transfers 30 --checkers 8 --contimeout 60s --timeout 300s --retries 3 --low-level-retries 10 --stats 1s "sharepoint-WCO:/General/Data/E1/nutrients" $incoming/E1
/bin/rclone sync --update --verbose --transfers 30 --checkers 8 --contimeout 60s --timeout 300s --retries 3 --low-level-retries 10 --stats 1s "sharepoint-WCO:/General/Data/L4/nutrients" $incoming/L4

/bin/scp $incoming/E1/* $basedir
/bin/scp $incoming/L4/* $basedir

echo "Cleaning up the input csv files"
nutfiles=`ls /users/rsg/observatory/nutrients/nutrients_{l4,e1}*`
for f in $nutfiles
do
   xpath=${f%/*}   
   xbase=${f##*/}
   xfext=${xbase##*.}
   xpref=${xbase%.*}
   station=`echo $xbase | cut -c11-12`
   #echo $station
   if [ $station == e1 ]
   then 
      #cat $f | sed -e 's/,,,,,,,,,//g' | sed -r '/^\s*$/d' > /tmp/$xbase 
      cat $f | sed -e 's/,,,,,,,,,//g' | sed -r '/^\s*$/d' > $basedir/e1_nutrients.csv 
   else
      #cat $f | sed -e 's/,,,,,,,,,,,,,,,,,,,,,,//g' | sed -r '/^\s*$/d' > /tmp/$xbase 
      cat $f | sed -e 's/,,,,,,,,,,,,,,,,,,,,,,//g' | sed -r '/^\s*$/d' > $basedir/l4_nutrients.csv
   fi
done

echo "Executing Python script plot_nutrients.py"
pushd ${basedir} >& /dev/null
   #(echo "plot_nutrients, /L4";) | $IDL
   #(echo "plot_nutrients, /E1";) | $IDL
   ${basedir}/plot_nutrients.py --station L4
   ${basedir}/plot_nutrients.py --station E1
popd >& /dev/null

#for f in ${basedir}/*.ps
#do
#   echo $f
#   stem=`echo ${f%%.*}`
#   /bin/convert -density 300 -units PixelsPerInch -alpha off +profile "icc" ${stem}.ps -resize 25% ${stem}.png
#   /bin/rm $f
#done

exit 0

