#!/bin/csh

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

# Move the files into day specific directories
set yyyy = 2014
set mm = 12
set dd = 01

# 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)
   echo $f_inwater
   dos2unix -q $f_inwater
   cat $f_inwater | tr '\n' ',' | tr -s "\*" "," >> ${yyyy}${mm}${dd}S.txt
   echo "" >> ${yyyy}${mm}${dd}S.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 6090028449ce116 *.sbd`
foreach f_wave_atm($fs_wave_atm)
   echo $f_wave_atm
   dos2unix -q $f_wave_atm
   cat $f_wave_atm | grep "66090028449ce116,1"  | tr -s "\*" "," >> ${yyyy}${mm}${dd}WA.txt
end 

popd

cat $finaldir/????????/*WA.txt | awk 'BEGIN {FS = ","; RS = "\n"} {if ($7 ~ /N/ && NF == 22) {print $2,$3,$9,$10,$11,$12,$13,$14,$15,$16}\
                                      if ($6 ~ /DAS/ && NF == 20) {print $2,$3,$7,$8,$9,$10,$11,$12,$13,$14}}' \
    > $finaldir/buoy_met_E1_all.txt

cat $finaldir/????????/*S*.txt | awk 'BEGIN {FS = ","; RS = "\n"} {if (NF >= 33){printf "%06d,%06d,0.00,%f,0.00,%f,%f,%f,%f,%f\n",$2,$3,$8,$12,$21,$24,$27,$30}}' \
   > $finaldir/buoy_water_E1_all.txt



echo $finaldir

exit(0)
