#!/bin/sh
if [ $# = 0 ];then
echo "ppconvert.sh YYYYMMDD inputppfile"
echo
echo "Output will be in folder JOBIDYYYYMMDD"
echo "inputppfile should include full path"
echo
echo "NOTE: Ensure this dimensions of your UM files are correct in"
echo "      ${LAGRANTO}/ppconvert/um_dims.inc & pp2cdf.f"
echo "Changes will require recompiling this component of lagranto"
exit 1
fi
callingdir=`pwd`
#cd ${LAGRANTO}/ppconvert
YMD=$1
DFILE=$2
# set the following 8 parameters
JOBYYYY=`echo $YMD | cut -c 1-4` # set jobref Year YYYY
JOBYY=`echo $YMD | cut -c 3-4`
JOBMM=`echo $YMD | cut -c 5-6` # set jobref Month MM
JOBDD=`echo $YMD | cut -c 7-8` # set jobref Day DD
CONSTS_FILE=${JOBYYYY}${JOBMM}${JOBDD}_cst
DTYPE=USER          # set to LAM or UKMM or USER for data type

#TSTASH=4004                 # set Stash code of T field in $DFILE
TSTASH=90002                # set Stash code of T field in $DFILE
#QSTASH=4010                 # set Stash code of Q field in $DFILE
QSTASH=90077                # set Stash code of Q field in $DFILE
PSTASH=409                  # set Stash code of PS field in $DFILE
#PSTASH=90120
Pmin=1000.0                 # set Stash code of PS field in $DFILE
Pmax=160.0                  # set Stash code of PS field in $DFILE
Pdiff=15.0                  # set Stash code of PS field in $DFILE
NewDyn=.TRUE.               # set to true for New Dynamics code
IGNORET0=1                  # set to 1 to ignore T+0 data

# set USER_EXEC if DTYPE=USER
USER_EXEC=${LAGRANTO}/ppconvert/pp2cdf
# USER_EXEC=pp2cdf
# N.B Do not change following creation of fort.15

#fort.15 is a input file in the above executable file.
# any changes made for the following creation will changs made 
#for the input statement in it

JOBREF=${JOBYYYY}${JOBMM}${JOBDD}
cat > fort.15 << EOF
$DFILE
P${JOBREF}_
yes
$CONSTS_FILE
1.0e+30
$TSTASH
$QSTASH
$PSTASH
$Pmin
$Pmax
$Pdiff
$NewDyn
$IGNORET0
EOF
#$PSTASH
#$Pmin
#$Pmax
#$Pdiff

if [ "$DTYPE" = "LAM" ]
then
  $LAGRANTO/ppconvert/ppf2cdf_lam.x          # LAM data converter
elif [ "$DTYPE" = "UKMM" ]
then
  $LAGRANTO/ppconvert/ppf2cdf_ukmm_new.x         # UKMM data converter
elif [ "$DTYPE" = "USER" ]
then
  $USER_EXEC                                  # USER data converter
else
  echo "Data type: $DTYPE not recognised - Exiting"
#  exit(1)
fi
#rm -f $DFILE

# N.B use only year and month for filter as could go into next day
list=`ls P${JOBYYYY}${JOBMM}*`
# now create the S-files
for file in $list
do
  $LAGRANTO/ppconvert/p2s.csh $file 1
  #p2s.csh $file 5
done

# edit here if you want to move files
chmod g+r P${JOBREF}*
chmod g+r S${JOBREF}*
#mv P${JOBREF}* $TRAJ_HOME/traj/jan97/.
#mv S${JOBREF}* $TRAJ_HOME/traj/jan97/.
#mv $CONSTS_FILE $TRAJ_HOME/traj/jan97/.

cd $callingdir

exit 0
