#!/bin/csh

set years = (2008 2009 2010 2011)

set outstem = /users/rsg/observatory/buoy/pml/E1/data
set finalstem = /users/rsg/observatory/buoy/pml/E1/final/annual


foreach year($years)
   echo $year
   mkdir -p $finalstem/{$year}

   echo " WQM"
   cat $outstem/{$year}/{$year}???/*WQM*.txt | awk -v YYYY=$year '$0 ~ YYYY {print $0}' > $finalstem/{$year}/E1_WQM.txt
   echo " CDM"
   cat $outstem/{$year}/{$year}???/*CDM*.txt | awk -v YYYY=$year '$0 ~ YYYY {print $0}' > $finalstem/{$year}/E1_CDM.txt

   # Change to use the GPS and Met (MPB) from the AMOS system rather than the AirMar
   echo " GPS & MPB"
   if ($year < 2014) then
      cat $outstem/{$year}/{$year}???/*GPS*.txt | awk -v YYYY=$year '$0 ~ YYYY {print $0}' > $finalstem/{$year}/E1_GPS.txt
      cat $outstem/{$year}/{$year}???/*MPB*.txt | awk -v YYYY=$year '$0 ~ YYYY {print $0}' > $finalstem/{$year}/E1_MPB.txt
   else
      cat $outstem/{$year}/{$year}???/*GPS_AMS*.txt | awk -v YYYY=$year '$0 ~ YYYY {print $0}' > $finalstem/{$year}/E1_GPS.txt
      cat $outstem/{$year}/{$year}???/*MPB_AMS.txt | awk -v YYYY=$year '$0 ~ YYYY {print $0}' > $finalstem/{$year}/E1_MPB.txt
   endif
   #####
   
   echo " HSE"
   cat $outstem/{$year}/{$year}???/*HSE*.txt | awk -v YYYY=$year '$0 ~ YYYY {print $0}'  | awk '/\./{if (NF==140) print $0}' > $finalstem/{$year}/E1_HSE.txt
   cat $finalstem/E1_HSE_header.txt $finalstem/{$year}/E1_HSE.txt > tmp.txt
   /bin/mv tmp.txt $finalstem/{$year}/E1_HSE.txt

   echo " NLF"
   cat $outstem/{$year}/{$year}???/*NLF*.txt | awk -v YYYY=$year '$0 ~ YYYY {print $0}' > $finalstem/{$year}/E1_NLF.txt
   echo " PHL"
   cat $outstem/{$year}/{$year}???/*PHL*.txt | awk -v YYYY=$year '$0 ~ YYYY {print $0}' > $finalstem/{$year}/E1_PHL.txt
   echo "============="
end


exit(0)
