#!/bin/csh

set dirs = `ls -d 2013????`

foreach dir($dirs)
   set yyyy = `echo $dir | cut -c1-4`
   set mm = `echo $dir | cut -c5-6`
   set dd = `echo $dir | cut -c7-8`
   echo $yyyy$mm$dd
   # move to the created directory to find files of different types
   pushd $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`
   foreach f_atm($fs_atm)
      dos2unix -q $f_atm
      cat $f_atm | tr '\n' ',' | tr -s "\*" "," >> ${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 
   popd
end

exit(0)

