#!/bin/csh
#
# Script to create secondary data files (S-files) from P-files
# H.Wernli	1. April 96

unalias cd

echo "file in p2s.csh" $1

if ( $#argv < 2 ) then
  echo "Usage: p2s p_filename mode"
  echo "       if mode=2: put PS, TH, PV, RH, THW, DHR, DPVR on S-files"
  echo "       if mode=5: put PS, TH, PV, RH, THW, DTHWDP on S-files"
  echo "       if mode=6: put PS, TH, RH on S-files"
  echo "       if mode=1: put PS, TH, RH, THE, THW on S-files"
  echo "       if mode=4: put PS, TH, PV, RH, DTHDP, DTHWDP, THW, VO, RI on S-files"
  exit 1
endif

set file=$1
set mode=$2

set gva=$LAGRANTO/goodies/getvars

set inputfile=fort.9

if ( ! -f $file ) then
  echo "*** error: P-file $file does not exist ***"
  exit 1
endif

# check whether Z is already defined on P-file (important for mode=10)

set z=`$gva $file | grep Z | wc -c`
if ( $z == 0 ) then
  set zdef=0
else
  set zdef=1
endif
echo zdef $zdef

# determine the qmode

set qd=`$gva $file | grep QD | wc -c`
if ( $qd == 0 ) then
  set qmode=1
else
  set qmode=2
endif

# prepare inputfile

\rm -f $inputfile
touch $inputfile

#set date=`echo $file | cut -c 2-12`
set date=`echo $file | cut -c 2-14`

echo $date >> $inputfile
echo $mode >> $inputfile
echo $qmode >> $inputfile
if ( $mode == 10 ) echo $zdef >> $inputfile

# execute program ptos

$LAGRANTO/ppconvert/ptos

#\rm -f $inputfile
