#!/bin/bash
#-----------------

# -- load netcdf
module 'load netcdf/4.3.2'

# -- set environment
export LAGRANTO="${HOME}/lagranto.ocean/"

# -- set data dir
ddir="/atmosdyn/schemms/oras5/cdf"

# -- experiement data dir
odir=${ddir}/"2009.2010"
mkdir -pv $odir
cd $odir
find -type l -delete

# ---- start date
date="20091112_00"


# ---- end date 
fdat=$( tim2dat -d +5 $date )
fdat=$( tim2dat -d +5 $fdat  )

# --- link bathymetry file
ln -s ${ddir}/bath .

# -- current start date of trajectories
idat=$date

# -- link all necessary fields for time period
while [[ ${idat} != ${fdat} ]]; do

            echo $idat
            # -- date of this file
            y=$(echo $idat | cut -c1-4)
            m=$(echo $idat | cut -c5-6)
            d=$(echo $idat | cut -c7-8)
            h=$(echo $idat | cut -c9-10)

            # -- link start file
            ln -s ${ddir}/P${idat} . &> /dev/null
            ln -s ${ddir}/S${idat} . &> /dev/null

            # -- go to next time step
            idat=$( tim2dat -d +5 $idat )

done
fdat=$( tim2dat -d -5 $fdat  )

# ---- create the startfile

# -- Examples
#create_startf.ocean ${date} startf.2 'box.grid(-52,-53,55,55)@profile(0.6,10,20)@m'
#create_startf.ocean ${date} startf.2 'line(-55,-47,54,59,40)@profile(0.6,10,20)@m'

# -- Exp 1: Labrador current
#create_startf.ocean ${date} startf.2 'circle.grid(-55,55,100)@profile(0.6,10,20)@m'

# -- Exp 2: West Greenland current
#create_startf.ocean ${date} startf.2 'circle.grid(-50,60,100)@profile(0.6,10,20)@m'

# -- Exp 3: Labrador Sea deep convection
#create_startf.ocean ${date} startf.2 'circle.grid(-55,57.5,50)@profile(0.6,10,20)@m'

# -- Exp 4: North Labrador Sea turn
create_startf.ocean ${date} startf.2 'circle.grid(-58,63,50)@profile(0.6,5,10)@m'

# --- start calculation and output all 24 hours / computing time steps is 12h instead of 10h
caltra.ocean ${date} ${fdat} startf.2 traj.1 -j -o 1440

# --- Create the Tracevars file ---
\rm tracevars  &> /dev/null
cat >> tracevars << end_trace
U    1.  0 Pq
HFLX 1.  0 S
end_trace

# -- trace some variables
trace.ocean traj.1 LSL${date}

# ---
find -type l -delete

exit 0
