#!/bin/csh -f
#
# This script assumes that a mount to adcp data has been setup and files are named like
# /local/users/pstar/cruise/data/vmadcp/jc145_os75/rawraw/OS075_JC145004_000000.ENR  
# or mostly
# /local/users/pstar/cruise/data/vmadcp/jc145_os75/rawraw/ADCP75_BTon_nosync010_000000.ENR
# note unfortunately on this cruise the names didn't change for BToff files, 
# however BT status is determined separately
#
# Go through loop once for each adcp

set cruise = jc145
set CRUISE = JC145
    
foreach adp ('75' '150')
  echo $adp
  cd /local/users/pstar/cruise/data/vmadcp
  cd ${cruise}_os${adp}

  echo "Synchronising raw data for the $adp"
  rsync -av /mnt/adcp${adp}/${CRUISE}/raw_data/ rawraw

# --------------------------------------------------
# These next steps were needed as naming of files was not correct
  cd rawdata
  foreach fl (`ls ../rawraw/ADCP${adp}_BT*`)
    set bb = `echo $fl | awk '{print substr($1,length($1)-13,length($1))}'`
    set fl2 = OS${adp}_JC145_${bb}
    if (! -e $fl2) then
      ln -s $fl $fl2
    endif
  end
  cd ..
# --------------------------------------------------

end

#JC145 only: now deal with the initial (transit) files, one sequence at a time because
#the sequences will have to be renumbered
cd /local/users/pstar/cruise/data/vmadcp/jc145_os75/rawdata
foreach fl (`ls ../rawraw/OS75_JC145001*`)
  set bb = `echo $fl | awk '{print substr($1,length($1)-9,length($1))}'`
  set fl2 = OS75_JC145_901_${bb}
  if (! -e $fl2) then
    ln -s $fl $fl2
  endif
end
foreach fl (`ls ../rawraw/OS75_JC145002*`)
  set bb = `echo $fl | awk '{print substr($1,length($1)-9,length($1))}'`
  set fl2 = OS75_JC145_902_${bb}
  if (! -e $fl2) then
    ln -s $fl $fl2
  endif
end
cd /local/users/pstar/cruise/data/vmadcp/jc145_os150/rawdata
foreach fl (`ls ../rawraw/OS150_JC145001*`)
  set bb = `echo $fl | awk '{print substr($1,length($1)-9,length($1))}'`
  set fl2 = OS150_JC145_901_${bb}
  if (! -e $fl2) then
    ln -s $fl $fl2
  endif
end
foreach fl (`ls ../rawraw/OS150_JC145002*`)
  set bb = `echo $fl | awk '{print substr($1,length($1)-9,length($1))}'`
  set fl2 = OS150_JC145_902_${bb}
  if (! -e $fl2) then
    ln -s $fl $fl2
  endif
end