<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/bin/csh -f
# Module: Level1-2_merging.csh
# Author: Tim Smyth
# Date: 21 April 2016
# Version: 1.0
# Description:
#  Script to take the Level1 data and merge to deployment level with consistent
#  columns and entries

set years = (2013 2014 2015 2016 2017 2018 2019 2021 2022)
set years = (2022)

# STAGE 1:
# Requires as input: E1_buoy_YYYY_AW_deployment2.csv and E1_buoy_YYYY_S_deployment2.csv
# Outputs: E1_buoy_YYYY_AW_deployment2_premerge.csv and E1_buoy_YYYY_S_deployment2_premerge.csv
# Prepare the level1 files for merger
foreach year($years)
   echo $year
   echo "Creating the premerge file"
   foreach file($year/*deployment?.csv)
      echo $file
      dos2unix $file
      set header = `cat $file | awk '/YYMMDD/{print $0}'`
      set nfile_stem = `echo $file:r`
      set nfile = `echo ${nfile_stem}_premerge.csv`
      echo $nfile
      echo $header &gt; $nfile
      cat $file | uniq | sed -e 's/\*//g' | tail -n +2 | awk 'BEGIN {FS=","} {printf("%s,%d,%06d", $1,$2,$3)} {for(i=4;i&lt;=NF;++i) printf(",%s",$i)} {printf("\n")}' &gt;&gt; $nfile
      echo "====================="
   end
end

# use the premerge files to merge all by date and hour
# loop over day of year
foreach year($years)
   set n_deployment = 2
   echo "Year: " $year
   if ($year == 2013) then
      set n_deployment = 1
   endif
   if ($year == 2014) then 
      set n_deployment = (1 2)
   endif
   
   foreach deployment($n_deployment)
      echo "Deployment: " $deployment
      if ($year == 2013) then
         set jday = 162
         set jmax = 365
      endif
      if ($year == 2014 &amp;&amp; $deployment == 1) then
         set jday = 1
         set jmax = 105
      endif
      if ($year == 2014 &amp;&amp; $deployment == 2) then
         set jday = 320
         set jmax = 365
      endif
      if ($year == 2015) then
         set jday = 1
         set jmax = 365
      endif
      if ($year == 2016) then
         set jday = 1
         set jmax = 366
         mkdir -p ../level2/$year
      endif
      if ($year == 2017) then
         set jday = 1
         set jmax = 365
         mkdir -p ../level2/$year
      endif
      if ($year == 2018) then
         set jday = 1
         set jmax = 365
         mkdir -p ../level2/$year
      endif
      if ($year == 2019) then
         set jday = 1
         set jmax = 365
         mkdir -p ../level2/$year
      endif
      if ($year == 2021) then
         set jday = 1
         set jmax = 365
         mkdir -p ../level2/$year
      endif
      if ($year == 2022) then
         set jday = 1
         set jmax = 365
         mkdir -p ../level2/$year
      endif
      
      echo "$year/E1_buoy_${year}_*_deployment${deployment}_premerge.csv"
      set files = `ls $year/E1_buoy_${year}_*_deployment${deployment}_premerge.csv`
      echo $files
      echo $#files
      if ($#files == 3) then 
         set Aheader = `cat $year/E1_buoy_${year}_A_deployment${deployment}_premerge.csv | awk '/YYMMDD/{print $0}'`
         set Wheader = `cat $year/E1_buoy_${year}_W_deployment${deployment}_premerge.csv | awk '/YYMMDD/{print $0}'`
         set Sheader = `cat $year/E1_buoy_${year}_S_deployment${deployment}_premerge.csv | awk '/YYMMDD/{print $0}'`
         echo $Aheader","$Wheader","$Sheader &gt; ../level2/$year/E1_buoy_${year}_AWS_deployment${deployment}_merged.csv
      endif
      if ($#files == 2) then 
         set AWheader = `cat $year/E1_buoy_${year}_AW_deployment${deployment}_premerge.csv | awk '/YYMMDD/{print $0}'`
         set Sheader = `cat $year/E1_buoy_${year}_S_deployment${deployment}_premerge.csv | awk '/YYMMDD/{print $0}'`
         echo $AWheader","$Sheader &gt; ../level2/$year/E1_buoy_${year}_AWS_deployment${deployment}_merged.csv
      endif

      while($jday &lt;= $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 &lt; 24)
            set HH = `printf '%02d\n' $hour` 
            set datehourmatch = `echo $datematch,$HH`
            echo $datehourmatch
            
            if ($#files == 3) then 
               set Amatch = `grep -nc "${datehourmatch}0005" $year/E1_buoy_${year}_A_deployment${deployment}_premerge.csv`
               set Wmatch = `grep -nc "$datehourmatch" $year/E1_buoy_${year}_W_deployment${deployment}_premerge.csv`
               set Smatch = `grep -nc "$datehourmatch" $year/E1_buoy_${year}_S_deployment${deployment}_premerge.csv`
               
               if ($Amatch == 0 &amp;&amp; $Wmatch == 0 &amp;&amp; $Smatch == 0) then
                  echo "No data available"
               else
                  if ($Amatch == 0) then 
                     set Adata = ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
                  else 
                     set Adata = `grep "${datehourmatch}0005" $year/E1_buoy_${year}_A_deployment${deployment}_premerge.csv`
                  endif
                  if ($Wmatch == 0) then 
                     set Wdata = ",,,,,,,,,,,,,,,,,,,,,"
                  else
                     set Wdata = `grep "$datehourmatch" $year/E1_buoy_${year}_W_deployment${deployment}_premerge.csv`
                  endif
                  if ($Smatch == 0) then 
                     set Sdata = ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
                  else
                     set Sdata = `grep "${datehourmatch}10" $year/E1_buoy_${year}_S_deployment${deployment}_premerge.csv`
                  endif
                  
                  echo $Adata","$Wdata","$Sdata &gt;&gt; ../level2/$year/E1_buoy_${year}_AWS_deployment${deployment}_merged.csv

               endif
            endif
            if ($#files == 2) then 
               set AWmatch = `grep -nc "${datehourmatch}0005" $year/E1_buoy_${year}_AW_deployment${deployment}_premerge.csv`
               set Smatch = `grep -nc "${datehourmatch}1000" $year/E1_buoy_${year}_S_deployment${deployment}_premerge.csv`
               
               if ($AWmatch == 0 &amp;&amp; $Smatch == 0) then
                  echo "No data available"
               else
                  if ($AWmatch == 0) then 
                     set AWdata = ",,,,,,,,,,,,,,,,,,,," 
                  else 
                     set AWdata = `grep "${datehourmatch}0005" $year/E1_buoy_${year}_AW_deployment${deployment}_premerge.csv`
                  endif
                  if ($Smatch == 0) then 
                     set Sdata = ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,," 
                  else
                     set Sdata = `grep "${datehourmatch}1000" $year/E1_buoy_${year}_S_deployment${deployment}_premerge.csv`
                  endif
                  
                  echo $AWdata","$Sdata &gt;&gt; ../level2/$year/E1_buoy_${year}_AWS_deployment${deployment}_merged.csv
                  
               endif
            endif
            @ hour ++
         end
         @ jday ++
      end
   end
end

exit(0)
</pre></body></html>