#!/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 = JC159
set cruise = jc159
set ctdloc = /local/users/pstar/mounts/mnt_cruise_data/Specific_Equipment/CTD/

rsync -av ${ctdloc}/Processed\ Data/*.cnv ctd/ASCII_FILES/
rsync -av ${ctdloc}/Raw\ Data/*.bl ctd/ASCII_FILES/
rsync -av ${ctdloc}/Processed\ Data/*.ros ctd/ASCII_FILES/
rsync -av ${ctdloc}/Raw\ Data/*.hex ctd/RAW_CTD_FILES/
rsync -av ${ctdloc}/Raw\ Data/*.XMLCON ctd/RAW_CTD_FILES/
rsync -av ${ctdloc}/Raw\ Data/*.hdr ctd/RAW_CTD_FILES/
rsync -av ${ctdloc}/Processed\ Data/*.btl ctd/ASCII_FILES/

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}_ctm.cnv 
   #echo $linkfile
   if (! -e $linkfile) then
      echo Making link $linkfile for raw file $i
     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

#below block could be commented out if your cruise doesn't have much of a problem with spikes
foreach i (`ls ${cpre}_???.cnv`)
   set num = `awk -v nm="$i" 'BEGIN {print substr(nm,index(nm,"cnv")-4,3)}'`
   set linkfile = ctd_${cruise}_${num}_noctm.cnv 
   #echo $linkfile
   if (! -e $linkfile) then
      echo Making link $linkfile for raw file $i
     ln -s $i $linkfile
   endif
end

cd ../..