#!/bin/csh

# main script to calculate cps parameters from trackfile (stormtrack) and then
# plot them at end

set name = `head -1 stormtrack | cut -d" " -f1`
set startt = `head -2 stormtrack | tail -1 | cut -d" " -f1`
set endt = `tail -1 stormtrack | cut -d" " -f1`
set ctl = "/kodos/r0/operational_analyses/AVN/2008/analysis/avn.ctl"

cat << EOF_CAT > desc.dat
$startt
$endt
$name
$ctl
EOF_CAT

echo "Calculating B"
grads -clb "run calcB.gs" >& /dev/null
echo "Calculing VTL"
grads -clb "run calcVTL.gs" >& /dev/null
echo "Calculing VTU"
grads -clb "run calcVTU.gs" >& /dev/null
echo "Calculing Size"
grads -clb "run calcsize.gs" >& /dev/null

grads -clb "run conv.gs" | grep DATA | cut -c6- > track.tmp

./conv.exe

cat << EOF_CAT > tmp.ctl
dset ^track.grads
undef 9.999E+20
options little_endian
title EP Prof File
ydef 1 linear -90.000000 2.5
xdef 1 linear 0.000000 2.500000
zdef 1 levels 1000
EOF_CAT
grep tdef B.ctl >> tmp.ctl
cat << EOF_CAT >> tmp.ctl
vars 4
size 1 99 size
B 1 99 thick
vtl 1 99 slope-lower
vtu 1 99 slope-upper
ENDVARS
EOF_CAT

grads -clb "run phase1-smooth.gs" >& /dev/null
grads -clb "run phase2-smooth.gs" >& /dev/null

rm -f B.ctl B.grads vtl.ctl vtl.grads vtu.ctl vtu.grads size.ctl size.grads tmp.ctl track.grads track.tmp
