#!/bin/csh
alias julian /users/rsg/panorama/panorama.svn/bin/julian

echo "================================="
echo "Running get_iridium_buoy_data.csh"
date 
set execdir = /users/rsg/observatory/buoy/pml/bin
#set execdir = /users/rsg/tjsm/src/buoy/iridium
set downloaddir = /users/rsg/observatory/buoy/pml/download/E1/ODAS51
set finaldir = /users/rsg/observatory/buoy/pml/download/E1
set base = 144
set HH = `date --utc +%k`
@ base = $base + $HH

set UnitID_atm = '300025060515020'
set UnitID_wat = '300025010838850'

# Move the files into day specific directories
foreach file(/users/rsg/observatory/buoy/pml/download/E1/2021????/300025060515020_*.sbd)
   echo $file 
   set stem = `echo $file:t:r | cut -c1-15` 
   set datename = `cat $file | sed -e 's/\,/\ /g' | tr '\n' '\t' | awk '{print $2}'`
   # Change needed for sbd formatted files 2021 onwards
   if ($stem == $UnitID_atm) then
      set yy = `echo $datename | cut -c5-6`
      set mm = `echo $datename | cut -c3-4`
      set dd = `echo $datename | cut -c1-2`
      set yyyy = `echo 20$yy`
   else
      set yrcheck = `echo $datename | cut -c1-2`
      if ($yrcheck == 20) then
         set yyyy = `echo $datename | cut -c1-4`
         set mm = `echo $datename | cut -c5-6`
         set dd = `echo $datename | cut -c7-8`
      else
         set yyyy = `echo 20$yrcheck`
         set mm = `echo $datename | cut -c3-4`
         set dd = `echo $datename | cut -c5-6`      
      endif
   endif
   set nchar = `echo $yyyy$mm$dd | wc -m`
   if ($nchar == 9) then
      #/bin/mkdir -p $finaldir/$yyyy$mm$dd
      set stem = `echo $file:t:r | cut -c1-15`
      #/bin/mv $file $finaldir/$yyyy$mm$dd

      # move to the created directory to find files of different types
      pushd $finaldir/$yyyy$mm$dd
      # in water files (S.txt for final version)
      # remove previous versions of the file
      #/bin/rm ${yyyy}${mm}${dd}S.txt
      #set fs_inwater = `grep -l 5208001e4525b0d0 *.sbd`
      #foreach f_inwater($fs_inwater)
      #   dos2unix -q $f_inwater
      #   cat $f_inwater | tr '\n' ',' | tr -s "\*" "," >> ${yyyy}${mm}${dd}S.txt
      #   echo "" >> ${yyyy}${mm}${dd}S.txt
      #end 
      # atmospheric files (A.txt for final version)
      # remove previous versions of the file
      /bin/rm ${yyyy}${mm}${dd}A.txt
      #set fs_atm = `grep -l 5208001e4525da2f *.sbd`
      set fs_atm = `ls ${UnitID_atm}_*.sbd`
      foreach f_atm($fs_atm)
         #echo $f_atm
         dos2unix -q $f_atm
         #cat $f_atm | tr '\n' ',' | tr -s "\*" "," >> ${yyyy}${mm}${dd}A.txt
         cat $f_atm | tr '\n' ',' >> ${yyyy}${mm}${dd}A.txt
         echo "" >> ${yyyy}${mm}${dd}A.txt
      end 
      # wave files (W.txt for final version)
      # remove previous versions of the file
      #/bin/rm ${yyyy}${mm}${dd}W.txt
      #set fs_wave = `grep -l TSPWA *.sbd`
      #foreach f_wave($fs_wave)
      #   dos2unix -q $f_wave
      #   cat $f_wave | tr '\n' ',' | tr -s "\*" "," >> ${yyyy}${mm}${dd}W.txt
      #   echo "" >> ${yyyy}${mm}${dd}W.txt
      #end 
      
      # combined wave and atmospheric files
      # remove previous versions of the file
      #/bin/rm ${yyyy}${mm}${dd}WA.txt
      #echo "creating combined wave atmosphere files"
      #set fs_wave_atm = `grep -l 5206001e45259026 *.sbd`
      #foreach f_wave_atm($fs_wave_atm)
      #   dos2unix -q $f_wave_atm
      #   cat $f_wave_atm | grep "5206001e45259026,1"  | tr -s "\*" "," >> ${yyyy}${mm}${dd}WA.txt
      #end 

      popd
   endif
end

# make long files for all the parameters
# Met files
#echo "Creating time-series met files"
#cat $finaldir/????????/*A.txt | awk 'BEGIN {FS = ","; RS = "\n"} \
#    {if ($1 ~ /2/ && $5 <= 504600 && NF > 6) \
#    {printf "$W5M5A,%02d%02d%02d,%04d05,5206001e45259026,1,E1_Buoy,%2.4fN,%1.4fW,%3.1f,%2.2f,%2.1f,-9.99,%3.1f,%4.2f,-9.99,%2.1f,-9.99,-9.99,-9.99,-9.99,02A\n",\
#    substr($2,5,2),substr($2,3,2),substr($2,1,2),$3,$5/10000,-$6/10000,$11/100,$10/10,$12/10,$7/10,$8/10,$9/100}}' \
#    > $finaldir/buoy_met_E1_all.txt

echo "Creating time-series met files"
cat $finaldir/????????/*A.txt | awk 'BEGIN {FS = ","; RS = "\n"} \
    {if ($1 ~ /2/ && $5 <= 504600 && NF == 15) \
    {printf "$W5M5A,%02d%02d%02d,%04d05,5206001e45259026,1,E1_Buoy,%2.4fN,%1.4fW,%3.1f,%2.2f,%2.1f,-9.99,%3.1f,%4.2f,-9.99,%2.1f,-9.99,-9.99,-9.99,-9.99,02A\n",\
    substr($2,5,2),substr($2,3,2),substr($2,1,2),$3,$5/10000,-$6/10000,$12/100,$11/10,$13/10,$7/10,$8/10,$9/100}}' \
    > $finaldir/buoy_met_E1_all.txt

# GPS information
echo "Creating time-series GPS files"
# find if there are new files
set newatm = `find $finaldir -name \*A.txt -mmin -120`
echo $#newatm

# Change in 2021 that Atmospheric files don't report N and W
cat $finaldir/????????/*A.txt | awk 'BEGIN {FS = ","; RS = "\n"} {if ($1 ~ /2/) \
   {printf "%02d%02d%02d %04d05 %2.4fN %1.4fW\n",substr($2,5,2),substr($2,3,2),substr($2,1,2),$3,$5/10000,-$6/10000}}' \
    > $finaldir/buoy_GPS_E1_all.txt

# Change in 2021 that seawater data do not contain the GPS
# In water files
echo "Creating time-series in-water files"
# find if there are new files
set newwtr = `find $finaldir -name \*S.txt -mmin -120`
echo $#newwtr
#cat $finaldir/????????/*S*.txt | awk 'BEGIN {FS = ","; RS = "\n"} {if (NF >= 33 && $6 == 0){printf "%06d,%06d,0.00,%f,0.00,%f,%f,%f,%f,%f\n",$2,$3,$8,$12,$21,$24,$27,$30}} \
#                                            {if (NF >= 33 && $6 == 120){printf "%06d,%06d,%f,%f,0.00,%f,%f,%f,%f,%f\n",$2,$3,$7,$10,$14,$23,$26,$29,$32}}' \
#      > $finaldir/buoy_water_E1_all.txt

cat $finaldir/????????/*S*.txt | awk 'BEGIN {FS = ","; RS = "\n"} {if ((NF == 35 || NF == 48)&& $6 == 0)\
                                            {printf "%06d,%06d,0.00,%f,0.00,%f,%f,%f,%f,%f\n",$2,$3,$8,$12,$21,$24,$27,$30}} \
                                            {if ((NF == 35 || NF == 48) && $6 == 120)\
                                            {printf "%06d,%06d,%f,%f,0.00,%f,%f,%f,%f,%f\n",$2,$3,$7,$10,$14,$23,$26,$29,$32}} \
                                            {if ((NF == 33 || NF == 46) && $9 == 0)\
                                            {printf "%06d,%06d,%f,0.00,0.00,%f,%f,%f,%f,%f\n",$2,$3,$7,$12,$21,$24,$27,$30}}' \
      > $finaldir/buoy_water_E1_all.txt

# Final data for table on web
echo "Creating short table file"
set short_metdata = `cat $finaldir/buoy_met_E1_all.txt | tail -1 | awk 'BEGIN {FS = ","; RS = "\n"} {print $16,$10,$9,$10,$13,substr($3,1,2)}'`
if ($#newatm == 0) then
   set short_metdata = "-9.99 -9.99 -9.99 -9.99 -9.99 -9.99"
   set m_julian = 0
endif

#set short_metdata_waves = `cat $finaldir/buoy_waves_E1_all.txt | tail -1 | awk '{print $3,$4,$5,$7}'`
set short_metdata_waves = "-9.99 -9.99 -9.99 -999"
if ($#newatm != 0) then
   set last_date = `cat $finaldir/buoy_met_E1_all.txt | tail -1 | awk 'BEGIN {FS = ","; RS = "\n"} {print $2}'`
   set yy = `echo $last_date | cut -c1-2`
   set yyyy = `echo "20"$yy`
   set mm = `echo $last_date | cut -c3-4`
   set dd = `echo $last_date | cut -c5-6`
   set latest_time = `echo $short_metdata[6]`
endif

# extract the date and time information from the in-water file
set w_last_date = `cat $finaldir/buoy_water_E1_all.txt | tail -1 | awk 'BEGIN {FS = ","; RS = "\n"} {print $1}'`
set w_yy = `echo $w_last_date | cut -c1-2`
set w_yyyy = `echo 20$w_yy`
set w_mm = `echo $w_last_date | cut -c3-4`
set w_dd = `echo $w_last_date | cut -c5-6`
set w_latest_time = `cat $finaldir/buoy_water_E1_all.txt | tail -1 | awk 'BEGIN {FS = ","; RS = "\n"} {print $2}' | cut -c1-2`
echo $w_latest_time

# return only the in-water data (and time-stamp) if in-water data more recent
set w_julian = `julian $w_dd $w_mm $w_yyyy`
if ($#newatm != 0) then
  set m_julian = `julian $dd $mm $yyyy`
endif

echo $w_julian $m_julian

set waterdate_epoch = `date '+%s' -d "$w_yyyy$w_mm$w_dd"`
set metdate_epoch = `date '+%s' -d "$yyyy$mm$dd"`

echo $metdate_epoch
echo $waterdate_epoch

if ($waterdate_epoch > $metdate_epoch) then
#if ($w_julian[1] > $m_julian[1]) then
   echo "More recent data available from in-water observations"
   set yyyy = $w_yyyy
   set mm = $w_mm
   set dd = $w_dd
   set short_metdata = "-10.0 -10.0 -10.0 -10.0 -10.0 $w_latest_time"
   set short_metdata_waves = "-10.0 -10.0 -10.0 -10.0"
endif

if ($w_julian[1] == $m_julian[1]) then
   #if ($w_latest_time > $latest_time) then
   #   echo "More recent data available from in-water observations"
   #   set yyyy = $w_yyyy
   #   set mm = $w_mm
   #   set dd = $w_dd
   #   set short_metdata = "-9.99 -9.99 -9.99 -9.99 -9.99 $w_latest_time"
   #   set short_metdata_waves = "-9.99 -9.99 -9.99 -9.99"
   #endif
endif
   
set short_temp_sal = `cat $finaldir/buoy_water_E1_all.txt | tail -1 | awk 'BEGIN {FS = ","; RS = "\n"} {print $7,$8}'`
set short_oxy_chl_turb = `cat $finaldir/buoy_water_E1_all.txt | tail -1 | awk 'BEGIN {FS = ","; RS = "\n"} {print $6/100.,$10,$9}'`
set short_GPS = `cat $finaldir/buoy_GPS_E1_all.txt | tail -1 | sed 's/N//g' | sed 's/W//g' | awk '{print $3,-$4}'`
set short_PAR = `cat $finaldir/buoy_water_E1_all.txt | tail -1 | awk 'BEGIN {FS = ","; RS = "\n"} {printf "%f",$3/2.77}'`

# cludge to sort out missing in-water data (new scaling added when WQM removed from field: 24 Jul 2018).
# calibration achieved using last 5 days of data from deployment regressing WQM SST against MO SST sensor.
#set short_sst_MO = `cat $finaldir/buoy_met_E1_all.txt | tail -1 | awk '{print ($6-4.316)/0.914}'`
set short_sst_MO = "-9.99"

if ($#newwtr == 0) then 
   set short_temp_sal = `echo "$short_sst_MO -9.99"`
   set short_oxy_chl_turb = "-9.99 -9.99 -9.99"
   set short_PAR = "-9.99"
endif

echo $short_metdata $short_temp_sal $dd $mm $yyyy $short_oxy_chl_turb $short_GPS $short_PAR $short_metdata_waves> $finaldir/buoy_latest.txt

# Do the gnuplot routines and stage files on the web
echo "Creating last ${base} hours plot (Met and PAR and waves)"
cat $finaldir/buoy_met_E1_all.txt | tail -${base} | awk 'BEGIN {FS=","} {print $2$3,$9,$10,$11,$12,$13,$14,$15,$16}' > /tmp/buoy_met_E1.txt
#cat $finaldir/buoy_water_E1_all.txt | tail -${base} | awk '{print $1$2,$3/2.77,$4,$5,$6,$7,$8,$9,$10}' > /tmp/buoy_water_E1.txt
#cat $finaldir/buoy_waves_E1_all.txt | tail -${base} | awk '{print $1$2,$3,$4,$5,$6,$7,$8}' > /tmp/buoy_waves_E1.txt

echo "Creating gnuplot compatible format file"
cat $finaldir/buoy_water_E1_all.txt | awk 'BEGIN {FS=","} {printf "%d,%f,%f,%f,%f,%f,%f,%f,%f\n", $1$2,$3/2.77,$4,$5,$6,$7,$8,$9,$10}' > /tmp/buoy_water_E1_all.txt

echo "Creating plots for the web"
pwd
/usr/bin/gnuplot /users/rsg/observatory/buoy/pml/bin/gnuplot/plot_temp_hum_ODAS51
/usr/bin/gnuplot /users/rsg/observatory/buoy/pml/bin/gnuplot/plot_pressure_ODAS51
/usr/bin/gnuplot /users/rsg/observatory/buoy/pml/bin/gnuplot/plot_wind_ODAS51
/usr/bin/gnuplot /users/rsg/observatory/buoy/pml/bin/gnuplot/plot_temp_sal_ODAS51
/usr/bin/gnuplot /users/rsg/observatory/buoy/pml/bin/gnuplot/plot_oxygen_fluor_ODAS51
/usr/bin/gnuplot /users/rsg/observatory/buoy/pml/bin/gnuplot/plot_nitrate_ODAS51
#/usr/bin/gnuplot /users/rsg/observatory/buoy/pml/bin/gnuplot/plot_waves_height_ODAS51
#/usr/bin/gnuplot /users/rsg/observatory/buoy/pml/bin/gnuplot/plot_waves_period_ODAS51

set latest_files = `ls [a-z]*.png`
#/usr/bin/scp $latest_files ${webuser}@${web}:/home/rsgadmin/westernchannelobservatory.org.uk/data/buoy/download/{$buoy}
/bin/mv $latest_files /users/rsg/observatory/buoy/pml/download/E1

exit(0)
