#!/bin/sh
################################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         cfs_atmos.sh
# Script description:  Runs a global spectral model forecast
#
# Author:        Mark Iredell       Org: NP23         Date: 1999-05-01
#
# Abstract: This script runs a single global spectral model forecast.
#   The initial conditions and run parameters are passed in the argument list.
#
# Script history log:
# 1999-05-01  Mark Iredell
# 2004-05-15  Shrinivas Moorthi
#
# Usage:  cfs_atmos.sh SIGI SFCI SIGO FLXO FHOUT FHMAX IGEN D3DO
#
#   Input script positional parameters:
#     1             Input sigma file 1
#                   defaults to $SIGI; one or the other is required
#     2             Input surface file
#                   defaults to $SFCI; one or the other is required
#     3             Output sigma file with embedded forecast hour '${FH}'
#                   defaults to $SIGO, then to ${COMOUT}/sigf'${FH}'$SUFOUT
#     4             Output flux file with embedded forecast hour '${FH}'
#                   defaults to $FLXO, then to ${COMOUT}/flxf'${FH}'$SUFOUT
#     5             Output frequency in hours
#                   defaults to $FHOUT, then to 3
#     6             Length of forecast in hours
#                   defaults to $FHMAX; otherwise FHSEG is required to be set
#     7             Output generating code
#                   defaults to $IGEN, defaults to 0
#     8             Output flux file with embedded forecast hour '${FH}'
#                   defaults to $D3DO, then to ${COMOUT}/d3df'${FH}'$SUFOUT
#
#   Imported Shell Variables:
#     SIGI          Input sigma file
#                   overridden by $1; one or the other is required
#     SFCI          Input surface file
#                   overridden by $2; one or the other is required
#     SIGO          Output sigma file with embedded forecast hour '${FH}'
#                   overridden by $3; defaults to ${COMOUT}/sigf'${FH}'$SUFOUT
#     FLXO          Output flux file with embedded forecast hour '${FH}'
#                   overridden by $4; defaults to ${COMOUT}/flxf'${FH}'$SUFOUT
#     FHOUT         Output frequency in hours
#                   overridden by $5; defaults to 3
#     FHMAX         Length of forecast in hours
#                   overridden by $6; either FHMAX or FHSEG must be set
#     IGEN          Output generating code
#                   overridden by $7; defaults to 0
#     FIXDIR        Directory for global fixed files
#                   defaults to /nwprod/fix
#     EXECGLOBAL    Directory for global executables
#                   defaults to /nwprod/exec
#     DATA          working directory
#                   (if nonexistent will be made, used and deleted)
#                   defaults to current working directory
#     COMOUT        output directory
#                   (if nonexistent will be made)
#                   defaults to current working directory
#     XC            Suffix to add to executables
#                   defaults to none
#     SUFOUT        Suffix to add to output filenames
#                   defaults to none
#     NCP           Copy command
#                   defaults to cp
#     SIGHDR        Command to read sigma header
#                   (required if JCAP, LEVS, or FHINI are not specified)
#                   defaults to ${EXECGLOBAL}/global_sighdr$XC
#     JCAP          Spectral truncation
#                   defaults to the value in the input sigma file header
#     LEVS          Number of levels
#                   defaults to the value in the input sigma file header
#     FCSTEXEC      Forecast executable
#                   defaults to ${EXECGLOBAL}/global_fcst${JCAP}${LEVS}$XC
#     SIGI2         Second time level sigma restart file
#                   defaults to NULL
#     model         either "global" or "cfs" - defaults to "cfs"
#     CO2CON        Input CO2 radiation (vertical resolution dependent)
#                   defaults to ${FIXDIR}/${model}_co2con.l${LEVS}.f77
#     MTNVAR        Input mountain variance (horizontal resolution dependent)
#                   defaults to ${FIXDIR}/${model}_mtnvar.t${JCAP}.f77
#     CLTUNE        Input cloud tuning file
#                   defaults to ${FIXDIR}/${model}_cldtune.f77
#     DTBTHE        Input equivalent potential temperature file
#                   defaults to ${FIXDIR}/${model}_tbthe.f77
#     O3PROD        Input ozone production climatology
#                   defaults to ${FIXDIR}/${model}_o3prod.txt
#     O3LOSS        Input ozone destruction climatology
#                   defaults to ${FIXDIR}/${model}_o3loss.txt
#     O3CLIM        Input ozone climatology
#                   defaults to ${FIXDIR}/${model}_o3clim.txt
#     FNGLAC        Input glacier climatology GRIB file
#                   defaults to ${FIXDIR}/${model}_glacier.2x2.grb
#     FNMXIC        Input maximum sea ice climatology GRIB file
#                   defaults to ${FIXDIR}/${model}_maxice.2x2.grb
#     FNTSFC        Input SST climatology GRIB file
#                   defaults to ${FIXDIR}/${model}_oi2sst1x1monclim19822001.grb
#     FNSNOC        Input snow climatology GRIB file
#                   defaults to ${FIXDIR}/${model}_snoclim.1.875.grb
#     FNZORC        Input roughness climatology GRIB file
#                   defaults to ${FIXDIR}/${model}_zorclim.1x1.grb
#     FNALBC        Input albedo climatology GRIB file
#                   defaults to ${FIXDIR}/${model}_albedo4.1x1.grb
#     FNAISC        Input sea ice climatology GRIB file
#                   defaults to ${FIXDIR}/${model}_ice1x1monclim19822001.grb
#     FNTG3C        Input deep soil temperature climatology GRIB file
#                   defaults to ${FIXDIR}/${model}_tg3clim.2.6x1.5.grb
#     FNVEGC        Input vegetation fraction climatology GRIB file
#                   defaults to ${FIXDIR}/${model}_vegfrac.1x1.grb
#     FNVETC        Input vegetation type climatology GRIB file
#                   defaults to ${FIXDIR}/${model}_vegtype.1x1.grb
#     FNSOTC        Input soil type climatology GRIB file
#                   defaults to ${FIXDIR}/${model}_soiltype.1x1.grb
#     FNSMCC        Input soil moisture climatology GRIB file
#                   defaults to ${FIXDIR}/${model}_soilmcpc.1x1.grb
#     OROGRAPHY     Input orography GRIB file (horiz resolution dependent)
#                   defaults to ${FIXDIR}/${model}_orography.t$JCAP.grb
#     FNMSKH        Input high resolution land mask GRIB file
#                   defaults to ${FIXDIR}/${model}_seaice_newland.grb
#     FNTSFA        Input SST analysis GRIB file
#                   defaults to none
#     FNACNA        Input sea ice analysis GRIB file
#                   defaults to none
#     FNSNOA        Input snow analysis GRIB file
#                   defaults to none
#     AERODIR       Input aersol climatology directory
#                   defaults to ${FIXDIR}
#     SIGR1         Output first time level sigma restart file
#                   defaults to ${DATA}/sigr1 which is deleted
#     SIGR2         Output second time level sigma restart file
#                   defaults to ${DATA}/sigr2 which is deleted
#     SFCR          Output surface restart file
#                   defaults to ${DATA}/sfcr which is deleted
#     SFCO          Output surface file with embedded forecast hour '${FH}'
#                   defaults to ${COMOUT}/sfcf'${FH}'$SUFOUT
#     LOGO          Output log file with embedded forecast hour '${FH}'
#                   defaults to ${COMOUT}/logf'${FH}'$SUFOUT
#     INISCRIPT     Preprocessing script
#                   defaults to none
#     LOGSCRIPT     Log posting script
#                   defaults to none
#     ERRSCRIPT     Error processing script
#                   defaults to 'eval [[ $err = 0 ]]'
#     ENDSCRIPT     Postprocessing script
#                   defaults to none
#     FHINI         Starting forecast hour
#                   defaults to the value in the input sigma file header
#     FHSEG         Number of hours to integrate
#                   (only required if FHMAX is not specified)
#                   defaults to 0
#     DELTIM        Timestep in seconds
#                   defaults to 3600/($JCAP/20)
#     FHRES         Restart frequency in hours
#                   defaults to 24
#     FHZER         Zeroing frequency in hours
#                   defaults to 6
#     FHLWR         Longwave radiation frequency in hours
#                   defaults to 3
#     FHSWR         Shortwave radiation frequency in hours
#                   defaults to 1
#     FHROT         Forecast hour to Read One Time level
#                   defaults to 0
#     FHDFI         Half number of hours of digital filter initialization
#                   defaults to 0
#     FHCYC         Surface cycling frequency in hours
#                   defaults to 0 for no cycling
#     FCSTVARS      Other namelist inputs to the forecast executable
#                   defaults to none set
#     FSMCL2        Scale in days to relax to soil moisture climatology
#                   defaults to 99999 for no relaxation
#     FTSFS         Scale in days to relax to SST anomaly to zero
#                   defaults to 90
#     FAISS         Scale in days to relax to sea ice to climatology
#                   defaults to 99999
#     FSNOL         Scale in days to relax to snow to climatology
#                   defaults to 99999
#     CYCLEVARS     Other namelist inputs to the surface cycling
#                   defaults to none set
#     NTHREADS      Number of threads
#                   defaults to 1
#     NTHSTACK      Size of stack per thread
#                   defaults to 64000000
#     FILESTYLE     File management style flag
#                   ('C' to copy to/from $DATA, 'L' for symbolic links in $DATA,
#                    'X' to use XLFUNIT or symbolic links where appropriate)
#                   defaults to 'X'
#     PGMOUT        Executable standard output
#                   defaults to $pgmout, then to '&1'
#     PGMERR        Executable standard error
#                   defaults to $pgmerr, then to '&1'
#     pgmout        Executable standard output default
#     pgmerr        Executable standard error default
#     REDOUT        standard output redirect ('1>' or '1>>')
#                   defaults to '1>', or to '1>>' to append if $PGMOUT is a file
#     REDERR        standard error redirect ('2>' or '2>>')
#                   defaults to '2>', or to '2>>' to append if $PGMERR is a file
#     VERBOSE       Verbose flag (YES or NO)
#                   defaults to NO
#
#   Exported Shell Variables:
#     PGM           Current program name
#     pgm
#     ERR           Last return code
#     err
#
#   Modules and files referenced:
#     scripts    : $INISCRIPT
#                  $LOGSCRIPT
#                  $ERRSCRIPT
#                  $ENDSCRIPT
#
#     programs   : $FCSTEXEC
#
#     input data : $1 or $SIGI
#                  $2 or $SFCI
#                  $SIGI2
#                  $FNTSFA
#                  $FNACNA
#                  $FNSNOA
#
#     fixed data : $CO2CON
#                  $MTNVAR
#                  $CLTUNE
#                  $DTBTHE
#                  $O3PROD
#                  $O3LOSS
#                  $O3CLIM
#                  $FNGLAC
#                  $FNMXIC
#                  $FNTSFC
#                  $FNSNOC
#                  $FNZORC
#                  $FNALBC
#                  $FNAISC
#                  $FNTG3C
#                  $FNVEGC
#                  $FNVETC
#                  $FNSOTC
#                  $FNSMCC
#                  $FNMSKH
#                  $OROGRAPHY
#
#     output data: $3 or $SIGO
#                  $4 or $FLXO
#                  $SFCO
#                  $LOGO
#                  $SIGR1
#                  $SIGR2
#                  $SFCR
#                  $PGMOUT
#                  $PGMERR
#
#     scratch    : ${DATA}/fort.11
#                  ${DATA}/fort.12
#                  ${DATA}/fort.14
#                  ${DATA}/fort.15
#                  ${DATA}/fort.24
#                  ${DATA}/fort.27
#                  ${DATA}/fort.28
#                  ${DATA}/fort.29
#                  ${DATA}/fort.43
#                  ${DATA}/fort.48
#                  ${DATA}/fort.51
#                  ${DATA}/fort.52
#                  ${DATA}/fort.53
#                  ${DATA}/SIG.F*
#                  ${DATA}/SFC.F*
#                  ${DATA}/FLX.F*
#                  ${DATA}/LOG.F*
#                  ${DATA}/sigr1
#                  ${DATA}/sigr2
#                  ${DATA}/sfcr
#                  ${DATA}/NULL
#
# Remarks:
#
#   Condition codes
#      0 - no problem encountered
#     >0 - some problem encountered
#
#  Control variable resolution priority
#    1 Command line argument.
#    2 Environment variable.
#    3 Inline default.
#
# Attributes:
#   Language: POSIX shell
#   Machine: IBM SP
#
####
################################################################################
#  Set environment.
export VERBOSE=${VERBOSE:-"NO"}
if [[ "$VERBOSE" = "YES" ]]
then
   echo $(date) EXECUTING $0 $* >&2
   set -x
fi
#  Command line arguments.
export SIGI=${1:-${SIGI:-?}}
export SFCI=${2:-${SFCI:-?}}
export SIGO=${3:-${SIGO}}
export FLXO=${4:-${FLXO}}
export FHOUT=${5:-${FHOUT:-3}}
export FHMAX=${6:-${FHMAX:-0}}
export IGEN=${7:-${IGEN:-0}}
export D3DO=${8:-${D3DO}}
#  Directories.
export FIXDIR=${FIXDIR:-/nwprod/fix}
export EXECGLOBAL=${EXECGLOBAL:-/nwprod/exec}
export DATA=${DATA:-$(pwd)}
export COMOUT=${COMOUT:-$(pwd)}
#  Filenames.
export model=${model:-cfs}
export XC=${XC}
export SUFOUT=${SUFOUT}
export SIGHDR=${SIGHDR:-${EXECGLOBAL}/global_sighdr$XC}
export JCAP=${JCAP:-$(echo jcap|$SIGHDR $SIGI)}
export LEVS=${LEVS:-$(echo levs|$SIGHDR $SIGI)}
export FCSTEXEC=${FCSTEXEC:-${EXECGLOBAL}/global_fcst${JCAP}${LEVS}$XC}
export SIGI2=${SIGI2:-NULL}
export CO2CON=${CO2CON:-${FIXDIR}/${model}_co2con.l${LEVS}.f77}
export MTNVAR=${MTNVAR:-${FIXDIR}/${model}_mtnvar.t${JCAP}.f77}
export CLTUNE=${CLTUNE:-${FIXDIR}/${model}_cldtune.f77}
export DTBTHE=${DTBTHE:-${FIXDIR}/${model}_tbthe.f77}
export O3PROD=${O3PROD:-${FIXDIR}/${model}_o3prod.f77}
export O3LOSS=${O3LOSS:-${FIXDIR}/${model}_o3loss.f77}
export O3CLIM=${O3CLIM:-${FIXDIR}/${model}_o3clim.txt}
export FNGLAC=${FNGLAC:-${FIXDIR}/${model}_glacier.2x2.grb}
export FNMXIC=${FNMXIC:-${FIXDIR}/${model}_maxice.2x2.grb}
export FNTSFC=${FNTSFC:-${FIXDIR}/${model}_oi2sst1x1monclim19822001.grb}
export FNSNOC=${FNSNOC:-${FIXDIR}/${model}_snoclim.1.875.grb}
export FNZORC=${FNZORC:-${FIXDIR}/${model}_zorclim.1x1.grb}
export FNALBC=${FNALBC:-${FIXDIR}/${model}_albedo4.1x1.grb}
export FNAISC=${FNAISC:-${FIXDIR}/${model}_ice1x1monclim19822001.grb}
export FNTG3C=${FNTG3C:-${FIXDIR}/${model}_tg3clim.2.6x1.5.grb}
export FNVEGC=${FNVEGC:-${FIXDIR}/${model}_vegfrac.1x1.grb}
export FNVETC=${FNVETC:-${FIXDIR}/${model}_vegtype.1x1.grb}
export FNSOTC=${FNSOTC:-${FIXDIR}/${model}_soiltype.1x1.grb}
export FNSMCC=${FNSMCC:-${FIXDIR}/${model}_soilmcpc.1x1.grb}
export FNMSKH=${FNMSKH:-${FIXDIR}/${model}_seaice_newland.grb}
export OROGRAPHY=${OROGRAPHY:-${FIXDIR}/${model}_orography.t$JCAP.grb}
export FNTSFA=${FNTSFA}
export FNACNA=${FNACNA}
export FNSNOA=${FNSNOA}
export AERODIR=${AERODIR:-${FIXDIR}}
export SIGR1=${SIGR1:-${DATA}/sigr1}
export SIGR2=${SIGR2:-${DATA}/sigr2}
export SFCR=${SFCR:-${DATA}/sfcr}
export SIGO=${SIGO:-${COMOUT}/sigf'${FH}'$SUFOUT}
export SFCO=${SFCO:-${COMOUT}/sfcf'${FH}'$SUFOUT}
export FLXO=${FLXO:-${COMOUT}/flxf'${FH}'$SUFOUT}
export LOGO=${LOGO:-${COMOUT}/logf'${FH}'$SUFOUT}
export D3DO=${D3DO:-${COMOUT}/d3df'${FH}'$SUFOUT}
export INISCRIPT=${INISCRIPT}
export ERRSCRIPT=${ERRSCRIPT:-'eval [[ $err = 0 ]]'}
export LOGSCRIPT=${LOGSCRIPT}
export ENDSCRIPT=${ENDSCRIPT}
#  Other variables.
export FHINI=${FHINI:-$(echo ifhr|$SIGHDR $SIGI)}
export FHSEG=${FHSEG:-0}
#export FHMAX=${FHMAX:-$((FHINI+FHSEG))}
export FHMAX=${FHMAX:-`expr $FHINI + $FHSEG`}
export DELTIM=${DELTIM:-$((3600/(JCAP/20)))}
export FHRES=${FHRES:-24}
export FHZER=${FHZER:-6}
export FHLWR=${FHLWR:-3}
export FHSWR=${FHSWR:-1}
export FHROT=${FHROT:-0}
export FHDFI=${FHDFI:-0}
export FHCYC=${FHCYC:-0}
export FCSTVARS=${FCSTVARS}
export FSMCL2=${FSMCL2:-99999}
export FTSFS=${FTSFS:-90}
export FAISS=${FAISS:-99999}
export FSNOL=${FSNOL:-99999}
export CYCLVARS=${CYCLVARS}
export NTHREADS=${NTHREADS:-1}
export NTHSTACK=${NTHSTACK:-64000000}
export XLSMPOPTS=${XLSMPOPTS:-"parthds=2:stack=64000000"}
export FILESTYLE=${FILESTYLE:-'X'}
export PGMOUT=${PGMOUT:-${pgmout:-'&1'}}
export PGMERR=${PGMERR:-${pgmerr:-'&2'}}
typeset -L1 l=$PGMOUT
[[ $l = '&' ]]&&a=''||a='>'
export REDOUT=${REDOUT:-'1>'$a}
typeset -L1 l=$PGMERR
[[ $l = '&' ]]&&a=''||a='>'
export REDERR=${REDERR:-'2>'$a}
################################################################################
#  Preprocessing
$INISCRIPT
pwd=$(pwd)
if [[ -d $DATA ]]
then
   mkdata=NO
else
   mkdir -p $DATA
   mkdata=YES
fi
cd $DATA||exit 99
[[ -d $COMOUT ]]||mkdir -p $COMOUT
################################################################################
#  Make forecast
export XLSMPOPTS="parthds=$NTHREADS:stack=$NTHSTACK"
export XLFRTEOPTS="unit_vars=yes"
export PGM=$DATA/$(basename $FCSTEXEC)
export pgm=$PGM
$LOGSCRIPT
${NCP:-cp} $FCSTEXEC $DATA
rm -f NULL
FH=$FHINI
[[ $FHINI -gt 0 ]]&&FH=`expr $FHINI + $FHOUT`
while [[ $FH -le $FHMAX ]]
do
   eval rm -f $LOGO
   FH=`expr $FH + $FHOUT`
   [[ $FH -lt 10 ]]&&FH=0$FH
done
if [[ $FILESTYLE = "X" ]]
then
   export XLFUNIT_11="$SIGI"
   export XLFUNIT_12="$SIGI2"
   export XLFUNIT_14="$SFCI"
   export XLFUNIT_15="$CO2CON"
   export XLFUNIT_24="$MTNVAR"
   export XLFUNIT_27="$DTBTHE"
   export XLFUNIT_28="$O3PROD"
   export XLFUNIT_29="$O3LOSS"
   export XLFUNIT_43="$CLTUNE"
   export XLFUNIT_48="$O3CLIM"
   export XLFUNIT_51="$SIGR1"
   export XLFUNIT_52="$SIGR2"
   export XLFUNIT_53="$SFCR"
elif [[ $FILESTYLE = "L" ]]
then
   ln -fs $SIGI fort.11
   ln -fs $SIGI2 fort.12
   ln -fs $SFCI fort.14
   ln -fs $CO2CON fort.15
   ln -fs $MTNVAR fort.24
   ln -fs $DTBTHE fort.27
   ln -fs $O3PROD fort.28
   ln -fs $O3LOSS fort.29
   ln -fs $CLTUNE fort.43
   ln -fs $O3CLIM fort.48
   ln -fs $SIGR1 fort.51
   ln -fs $SIGR2 fort.52
   ln -fs $SFCR fort.53
elif [[ $FILESTYLE = "C" ]]
then
   ${NCP:-cp} $SIGI fort.11
   ${NCP:-cp} $SIGI2 fort.12
   ${NCP:-cp} $SFCI fort.14
   ${NCP:-cp} $CO2CON fort.15
   ${NCP:-cp} $MTNVAR fort.24
   ${NCP:-cp} $DTBTHE fort.27
   ${NCP:-cp} $O3PROD fort.28
   ${NCP:-cp} $O3LOSS fort.29
   ${NCP:-cp} $CLTUNE fort.43
   ${NCP:-cp} $O3CLIM fort.48
   ${NCP:-cp} $OROGRAPHY orography
   for m in 01 02 03 04 05 06 07 08 09 10 11 12
   do
     ${NCP:-cp} $AERODIR/${model}_aeropac3a.m$m.txt aeropac3a.m$m
   done
fi
if [[ $FILESTYLE != "C" ]]
then
   for m in 01 02 03 04 05 06 07 08 09 10 11 12
   do
     ln -fs $AERODIR/${model}_aeropac3a.m$m.txt aeropac3a.m$m
   done
   ln -fs $OROGRAPHY orography
   FH=$FHINI
   [[ $FHINI -gt 0 ]]&&FH=`expr $FHINI + $FHOUT`
   while [[ $FH -le $FHMAX ]]
   do
      eval ln -fs $SIGO SIG.F$FH
      eval ln -fs $SFCO SFC.F$FH
      eval ln -fs $FLXO FLX.F$FH
      eval ln -fs $LOGO LOG.F$FH
      eval ln -fs $D3DO D3D.F$FH
      FH=`expr $FH + $FHOUT`
      [[ $FH -lt 10 ]]&&FH=0$FH
   done
fi

eval $PGM <<EOF $REDOUT$PGMOUT $REDERR$PGMERR
 &nam_mrf
  FHOUT=$FHOUT, FHMAX=$FHMAX, IGEN=$IGEN, DELTIM=$DELTIM,
  FHRES=$FHRES, FHZER=$FHZER, FHLWR=$FHLWR, FHSWR=$FHSWR,
  FHROT=$FHROT, FHDFI=$FHDFI, FHCYC=$FHCYC,
  $FCSTVARS /
&NAMSFC
  FNGLAC="$FNGLAC",
  FNMXIC="$FNMXIC",
  FNTSFC="$FNTSFC",
  FNSNOC="$FNSNOC",
  FNZORC="$FNZORC",
  FNALBC="$FNALBC",
  FNAISC="$FNAISC",
  FNTG3C="$FNTG3C",
  FNVEGC="$FNVEGC",
  FNVETC="$FNVETC",
  FNSOTC="$FNSOTC",
  FNSMCC="$FNSMCC",
  FNMSKH="$FNMSKH",
  FNTSFA="$FNTSFA",
  FNACNA="$FNACNA",
  FNSNOA="$FNSNOA",
  LDEBUG=.false.,
  FSMCL(2)=$FSMCL2,
  FTSFS=$FTSFS,
  FAISS=$FAISS,
  FSNOL=$FSNOL,
  FTSFL=99999,
  FAISL=99999,
  FSNOS=99999,
  $CYCLVARS /
EOF

export ERR=$?
export err=$ERR
$ERRSCRIPT||exit 2

if [[ $FILESTYLE = "C" ]]
then
   ${NCP:-cp} fort.51 $SIGR1
   ${NCP:-cp} fort.52 $SIGR2
   ${NCP:-cp} fort.53 $SFCR
   FH=$FHINI
   [[ $FHINI -gt 0 ]]&&FH=`expr $FHINI + $FHOUT`
   while [[ $FH -le $FHMAX ]]
   do
      eval ${NCP:-cp} SIG.F$FH $SIGO
      eval ${NCP:-cp} SFC.F$FH $SFCO
      eval ${NCP:-cp} FLX.F$FH $FLXO
      eval ${NCP:-cp} LOG.F$FH $LOGO
      eval ${NCP:-cp} D3D.F$FH $D3DO
      FH=`expr $FH + $FHOUT`
      [[ $FH -lt 10 ]]&&FH=0$FH
   done
fi

rm -f NULL
rm -f fort.11 fort.12 fort.14
rm -f fort.15 fort.24 fort.27 fort.28 fort.29 fort.43 fort.48
rm -f orography
rm -f fort.51 fort.52 fort.53
rm -f SIG.F* SFC.F* FLX.F* LOG.F* D3D.F*
#rm -f sigr1 sigr2 sfcr
################################################################################
#  Postprocessing
cd $pwd
[[ $mkdata = YES ]]&&rmdir $DATA
$ENDSCRIPT
set +x
if [[ "$VERBOSE" = "YES" ]]
then
   echo $(date) EXITING $0 with return code $err >&2
fi
exit $err
