#!/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 #mexecloc is replaced with $mexecloc by conf_script_cruise

set cpre = JR18002

rsync -av /mnt/data/jcr/current/ctd/jr18002/*.cnv ctd/ASCII_FILES/
rsync -av /mnt/data/jcr/current/ctd/jr18002/*.bl ctd/ASCII_FILES/
rsync -av /mnt/data/jcr/current/ctd/*.ros ctd/ASCII_FILES/
rsync -av /mnt/data/jcr/current/ctd/jr18002/SBE35/ 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_jr18002_${num}_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_jr18002_${num}_noctm.cnv
   if (! -e $linkfile) then
      echo Making link $linkfile for raw file
      ln -s $i $linkfile
   endif
end
#foreach i (`ls ${cpre}_???_align.cnv`)
#   gzip -f $i
#end
#foreach i (`ls ${cpre}_???.cnv`)
#   gzip -f $i
#end

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

cd ../RAW_CTD_FILES
rsync -av /mnt/data/jcr/current/ctd/jr18002/*.hdr .
rsync -av /mnt/data/jcr/current/ctd/jr18002/*.hex .
rsync -av /mnt/data/jcr/current/ctd/jr18002/*.XMLCON .
rsync -av /mnt/data/jcr/current/ctd/jr18002/*.btl .

#gzip -f *.hex

cd ../..