#!/bin/csh -f
# Module: Level1-2_merging_atmosphere_waves.csh
# Author: Tim Smyth
# Date: 02 Jan 2024
# Version: 1.0
# Description:
#  Script to take the Level1 data and merge to deployment level with consistent
#  columns and entries

set years = (2023)
#set years = (2022)

# Requires as input: E1_buoy_YYYY_A_deployment3.csv and E1_buoy_YYYY_W_deployment3.csv
# Outputs: 
# merge all by date and hour
# loop over day of year
foreach year($years)
   set n_deployment = 3
   echo "Year: " $year
   foreach deployment($n_deployment)
      echo "Deployment: " $deployment
      if ($year == 2023) then
         set jday = 176
         set jmax = 365
         mkdir -p ../level2/$year
      endif
      if ($year == 2022) then
         set jday = 157
         set jmax = 365
         mkdir -p ../level2/$year
      endif
      
      echo "$year/E1_buoy_${year}_*_deployment${deployment}.csv"
      set files = `ls $year/E1_buoy_${year}_?_deployment${deployment}.csv`
      echo $files
      echo $#files
      if ($#files == 2) then 
         set Aheader = `cat $year/E1_buoy_${year}_A_deployment${deployment}.csv | awk '/YYMMDD/{print $0}'`
         set Wheader = `cat $year/E1_buoy_${year}_W_deployment${deployment}.csv | awk '/YYMMDD/{print $0}'`
         echo $Aheader","$Wheader > ../level2/$year/E1_buoy_${year}_AW_deployment${deployment}_merged.csv
      endif
      while($jday <= $jmax) 
         set hour = 0
         set julian = `julian -zeros $jday $year`
         set jyy = `echo $julian[3] | cut -c3-4`
         set jmm = $julian[2]
         set jdd = $julian[1]
         set datematch = `echo $jyy$jmm$jdd`
         
         while($hour < 24)
            set HH = `printf '%02d\n' $hour` 
            set datehourmatch = `echo $datematch,$HH`
            echo $datehourmatch
            
            if ($#files == 2) then 
               set Amatch = `grep -nc "${datehourmatch}0005" $year/E1_buoy_${year}_A_deployment${deployment}.csv`
               set Wmatch = `grep -nc "$datehourmatch" $year/E1_buoy_${year}_W_deployment${deployment}.csv`

               if ($Amatch == 0 && $Wmatch == 0) then
                  echo "No data available"
               else
                  if ($Amatch == 0) then 
                     set Adata = ",,,,,,,,,,,,," # 13 entries
                  else 
                     set Adata = `grep "${datehourmatch}0005" $year/E1_buoy_${year}_A_deployment${deployment}.csv`
                  endif
                  if ($Wmatch == 0) then 
                     set Wdata = ",,,,,,,,,,,,,,,,"  # 16 entries
                  else
                     set Wdata = `grep "$datehourmatch" $year/E1_buoy_${year}_W_deployment${deployment}.csv`
                  endif
                  
                  # Additional comma added in here
                  echo $Adata","$Wdata >> ../level2/$year/E1_buoy_${year}_AW_deployment${deployment}_merged.csv

               endif
            endif
            @ hour ++
         end
         @ jday ++
      end
   end
end

exit(0)
