#!/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

PS4=' + gfdl_pre_atmos_spc2grd.sh hour ${HOUR} line $LINENO: '

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

  if [ $HOUR = "00" ]
  then
    cp ${PARA_DAT_DIR}/${PARA_SIGANL_FILE}       for12.f${HOUR}
  else
    if [ $YYYY -ge 2005 ]
    then
      cp ${PARA_DAT_DIR}/gfs.t${cyc}z.sf${HOUR}  for12.f${HOUR}
    else
      cp ${PARA_DAT_DIR}/gblav.t${cyc}z.sf${HOUR}  for12.f${HOUR}
    fi
###  cp ${PARA_DAT_DIR}/sigf${HOUR}.gfs.${PDY}${cyc}  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

#-----------------------------------------------------------
# February 2007 -- Change for hybrid coordinate system
# Due to the new GFS files having a hybrid sigma/pressure
# vertical coordinate system, we have to adjust for this.
# We could either change our source code, which is a pain, 
# or we can change the resolution of the data (this takes
# more CPU time, but for our parallel testing, we will go 
# this route to save development time).
#-----------------------------------------------------------

export HOLDDATA=${DATA}

export DATA=${HOLDDATA}/spc2grd$HOUR

if [ ${LEVEL_FLAG} = "HYB" ]; then

  export DATA
  export SIGINP=for12.f${HOUR}
  export SIGOUT=for12.f${HOUR}.standard_sigma

  export SIGHDR=${SIGHDR:-/nwprod/exec/global_sighdr}
  export JCAP=${JCAP:-`echo jcap|$SIGHDR ${SIGINP}`}
  export LEVS=${LEVS:-`echo levs|$SIGHDR ${SIGINP}`}
  if [ $JCAP = 382 ]; then
   export LONB=768
   export LATB=384
  elif [ $JCAP = 574 ]; then
   export LONB=1760
   export LATB=880
  else
    echo 'unsupported GFS resolution'
    exit 95
  fi

  export OROGRAPHY=/dev/null
  export IDVC=1
  export VERBOSE=YES
#  export IVSSFC=200509
#  export NVCOORD=1
  export CHGRESVARS='NVCOORD=1'
  export NTRAC=3
  export SIGLEVEL=${FIXhur}/global_siglevel.l64.txt
  export CHGRESEXEC=${EXEChur}/global_chgres

  set +x
  echo "TIMING: Time before chgres = `date`"
  set -x

  ${EXhur}/global_chgres.sh
  rcc=$?

  set +x
  echo "TIMING: Time after chgres = `date`"
  set -x

  if [ ${rcc} -ne 0 ]; then
    set +x
    echo " "
    echo "ERROR running chgres in spc2grd, error = $rcc"
    echo "exiting...."
    echo " "
    set -x
    exit 99
  fi

  set +x
  echo " "
  echo "Doing ls before mv..."
  echo " "
  ls -la
  echo " "
  set -x

  mv ${SIGOUT} ${SIGINP}

fi

export DATA=${HOLDDATA}


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

set +x
echo " "
echo "TIMING: date before spc2grd HOUR ${HOUR} is `date`"
echo " "
set -x

startmsg

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

set +x
echo " "
echo "TIMING: date after  spc2grd HOUR ${HOUR} is `date`"
echo " "
set -x

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

cd $DATA
