#!/bin/csh -f
#
#copy the raw and SBE-processed CTD files from the ship machine
#then make links to the files to be ingested by mexec using the expected name format

cd /local/users/pstar/cruise/data

set cpre = DY113_CTD
set cruise = dy113
set ctdloc = /mnt/CTD/CTD/Data/

rsync -auv ${ctdloc}/CTD_Pro_Data/*.cnv ctd/ASCII_FILES/
rsync -auv ${ctdloc}/CTD_Raw_Data/*.bl ctd/ASCII_FILES/
rsync -auv ${ctdloc}/CTD_Pro_Data/*.ros ctd/ASCII_FILES/
rsync -auv ${ctdloc}/CTD_Raw_Data/*.hex ctd/RAW_CTD_FILES/
rsync -auv ${ctdloc}/CTD_Raw_Data/*.XMLCON ctd/RAW_CTD_FILES/
rsync -auv ${ctdloc}/CTD_Raw_Data/*.hdr ctd/RAW_CTD_FILES/
rsync -auv ${ctdloc}/CTD_Pro_Data/*.btl ctd/ASCII_FILES/
rsync -auv ${ctdloc}/SBE35/*.asc ctd/ASCII_FILES/SBE35/

cd ctd/ASCII_FILES
foreach i (`ls ${cpre}???_align_ctm.cnv`)
   set num = `awk -v nm="$i" 'BEGIN {print substr(nm,index(nm,"_align")-3,3)}'`
   set linkfile = ctd_${cruise}_${num}_align_ctm.cnv
   if (! -e $linkfile) then
      echo Making link $linkfile for raw file $i
      ln -s $i $linkfile
   endif
end
#below could be commented out if T spikes aren't a problem
#foreach i (`ls ${cpre}???_align.cnv`)
#   set num = `awk -v nm="$i" 'BEGIN {print substr(nm,index(nm,"_align")-3,3)}'`
#   set linkfile = ctd_${cruise}_${num}_align_noctm.cnv
#   if (! -e $linkfile) then
#      echo Making link $linkfile for raw file
#      ln -s $i $linkfile
#   endif
#end

foreach i (`ls ${cpre}???.bl`)
   set num = `awk -v nm="$i" 'BEGIN {print substr(nm,index(nm,".bl")-3,3)}'`
   set linkfile = ctd_${cruise}_${num}.bl
   if (! -e $linkfile) then
      echo Making link $linkfile for raw file $i
      ln -s $i $linkfile
   endif
end

##compress files we probably won't need to use
##or don't because then it will sync them again
#foreach i (`ls ${cpre}???_align.cnv`)
#   gzip -f $i
#end
#foreach i (`ls ${cpre}???.cnv`)
#   gzip -f $i
#end
#
#cd ../RAW_CTD_FILES
#gzip -f *.hex

cd ../..