#!/usr/bin/ksh

set -x

###################################################################
# This script is responsible for running the executable 
# spc2grd, which interpolates the spectral sigma data
# from NCEP's global analysis onto the GFDL model's 
# integration grid.  A separate script is needed for this
# since we are running all of the forecast times 
# simultaneously under poe.
#
# We need to copy in the spectral file for the input forecast
# hour.  If the file is not in /com, look in /scom.
#
# NOTE (5/2000): This spc2grd stuff has now become more general,
# and the resolution is read in from the sfcanl file.  Because of
# this, and the fact that the unit containing the sfcanl file is
# rewound during execution, it is now necessary to have a separate 
# directory for each forecast hour.  It is also necesary to copy
# for11 (the sfcanl file) from the $DATA directory into each 
# hour's directory, for that same reason....Marchok.
###################################################################

HOUR=$1

set +x
echo " "
echo "  At beginning of gfdl_pre_spc2grd.sh, FORAENV= $FORAENV"
echo "  HOUR= $HOUR, HRIN= $HRIN, XLFRTEOPTS= $XLFRTEOPTS"
echo "  pwd= `pwd`"
echo " "
set -x

mkdir -p $DATA/spc2grd$HOUR
cd $DATA/spc2grd$HOUR

sh $utilscript/setup.sh

if [ $HOUR = "00" ]
then
  EXT=sanl
else
  EXT=sf$HOUR
fi

if [ $FORAENV = "GDAS" -o $FORAENV = "GFS" ]
then
  if test -f /com/${FORA}.$EXT
  then
     cp /com/${FORA}.$EXT for12.f${HOUR}
  else
     export pgm=gfdl_pre_spc2grd.sh
     export err=911;err_chk
  fi
else

#  cp ${FORA}.${EXT}  for12.f${HOUR}

  if [ $HOUR = "00" ]
  then
    cp ${PARA_DAT_DIR}/gfs.t${cyc}z.sanl       for12.f${HOUR}
  else
    cp ${PARA_DAT_DIR}/gfs.t${cyc}z.sf${HOUR}  for12.f${HOUR}
  fi

  if [ $HOUR -eq 78 -a ! -s ${PARA_DAT_DIR}/sigf${HOUR}.${PDY}${cyc} ]; 
  then
    cp ${PARA_DAT_DIR}/sigf72.${PDY}${cyc} for12.f78
  fi

fi

cp ${DATA}/for11 for11

export pgm=gfdl_spc2grd
. prep_step

export XLFUNIT_11=for11
export XLFUNIT_12=for12.f${HOUR}
if [ ${HOUR} -lt 100 ]
then
  export XLFUNIT_30=${DATA}/fcst0${HOUR}gaus
else
  export XLFUNIT_30=${DATA}/fcst${HOUR}gaus
fi

startmsg
$EXEChur/gfdl_spc2grd >> spc2grd.out$HOUR
export err=$?;err_chk

rm ${DATA}/spc2grd${HOUR}/for11

cd $DATA
