#!/bin/ksh
################################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:       exrap_analysis_sh.sms
# Script description:  runs the GSI and boundary update for the RAP cycle
# Author:   Ming Hu / Geoff Manikin / Julia Zhu  Org: EMC          Date: 2011-10-01
# 2011-10-01  M Hu / G Manikin / J Zhu

set -x

cd $DATA

# Set up some constants
export HOMErap=${HOMErap:-/nwprod}
export PARMrap=${PARMrap:-$HOMErap/parm}
export FIXrap=${FIXrap:-$HOMErap/fix}
export EXECrap=${EXECrap:-$HOMErap/exec}
export COMGFS=${COMGFS:-/com/gfs/prod}
export ndate=${ndate:-/nwprod/util/exec/ndate}

# Directory for the CRTM files:
export FIXcrtm=${FIXcrtm:-/nwprod/fix}

export WPSNAMELIST=namelist.wps
START_TIME=$PDY$cyc
echo $START_TIME >STARTTIME

# Compute date & time components for the analysis time
ymd=`echo ${START_TIME} | cut -c1-8`
ymdh=`echo ${START_TIME} | cut -c1-10`
mdh=`echo ${START_TIME} | cut -c5-10`
hh=`echo ${START_TIME} | cut -c9-10`

### Now run the GSI
# Set up some NCEP loadlevelor environment variables
export SPINLOOPTIME=10000
export YIELDLOOPTIME=40000
export AIXTHREAD_SCOPE=S
export MALLOCMULTIHEAP=true
export XLSMPOPTS="parthds=2:spins=0:yields=0:stack=128000000"

export MEMORY_AFFINITY=MCM
export MP_SHARED_MEMORY=yes
export BIND_TASKS=YES

# Recommended MPI environment variable setttings from IBM
# (Appendix E, HPC Clusters Using InfiniBand on IBM Power Systems Servers)

export LAPI_DEBUG_ENABLE_AFFINITY=YES
export MP_FIFO_MTU=4K
export MP_SYNC_QP=YES
export MP_RFIFO_SIZE=16777216
export MP_SHM_ATTACH_THRESH=500000
export MP_EUIDEVELOP=min
export MP_USE_BULK_XFER=yes
export MP_USE_BULK_XFER=yes
export MP_BULK_MIN_MSG_SIZE=64k
export MP_RC_MAX_QP=8192
export LAPI_DEBUG_RC_DREG_THRESHOLD=1000000
export LAPI_DEBUG_QP_NOTIFICATION=no
export LAPI_DEBUG_RC_INIT_SETUP=yes

# Set environment variables for user preferences
## export XLFRTEOPTS="nlwidth=80"
## export MP_LABELIO=yes

# Set up some constants
export RAPGES_FCYC=${RAPGES_FCYC:-$gespath/rap/rapges}
export RAPGES_PCYC=${RAPGES_PCYC:-$gespath/rap/rapges_pcyc}
export RAPGES_SFC=${RAPGES_SFC:-$gespath/rap/rapges_sfc}
export RAPBC=${RAPBC:-$gespath/rap/rapbc}
export PREPDIR=${PREPDIR:-/com/rap/prod}

# Compute date & time components for the analysis time
YYYY=`echo ${START_TIME} | cut -c1-4`
MM=`echo ${START_TIME} | cut -c5-6`
DD=`echo ${START_TIME} | cut -c7-8`
HH=`echo ${START_TIME} | cut -c9-10`
yy=`echo ${START_TIME} | cut -c3-4`
YYYYMMDDHH=${YYYY}${MM}${DD}${HH}
YYYYMMDD=${YYYY}${MM}${DD}

# Compute date & time components for the analysis time minus 1 day
DDm1=`$ndate -24 $START_TIME`
YMDm1=`echo ${DDm1} | cut -c1-8`

# Create the ram work directory and cd into it
workdir=$DATA/gsiprd
rm -rf ${workdir}
mkdir -p ${workdir}
cd ${workdir}
/nwprod/util/ush/setup.sh

# Bring over background field
time_str=${YYYY}-${MM}-${DD}_${HH}:00:00
echo " time_str = ${time_str}"

# if the cyce is 09 or 21, we should have the last 1-hr forecast
#   from the partial cycle available

if [[ ${HH} -eq '09' || ${HH} -eq '21' ]]; then
  PCYC_TIME=`$ndate -1 $START_TIME`
  if [ -r ${RAPGES_PCYC}/rap_${PCYC_TIME}f001 ]; then
    cp ${RAPGES_PCYC}/rap_${PCYC_TIME}f001 ./wrf_inout
     echo " Cycle ${YYYYMMDDHH}: GSI started with PCYC guess rap_${PCYC_TIME}f001" 
  fi
fi

# if we don't have a guess from the partial cycle, first try to use a
#   guess from the full cycle

if [ ! -r wrf_inout ]; then
    counter=1
    while [[ $counter -lt 19 ]]; do
     counterhr=$counter
     typeset -Z2 counterhr
     FCYC_TIME=`$ndate -${counter} $START_TIME`
     if [ -r ${RAPGES_FCYC}/rap_${FCYC_TIME}f0$counterhr ]; then
      echo " Cycled run using rap_${FCYC_TIME}f0$counterhr"
      cp ${RAPGES_FCYC}/rap_${FCYC_TIME}f0$counterhr ./wrf_inout
      echo " Cycle ${YYYYMMDDHH}: GSI background=rap_${FCYC_TIME}f0$counterhr"
      break
     fi
     counter=` expr $counter + 1 `
    done
fi

# attempt cold start if no guess has been found
if [ ! -r wrf_inout ]; then
   if [ -r "${RAPBC}/wrfinput_d01_${time_str}" ]; then
      echo "Cold Start with ${RAPBC}/wrfinput_d01_${time_str} "
      cp ${RAPBC}/wrfinput_d01_${time_str} ./wrf_inout
      echo " Cycle ${YYYYMMDDHH}: GSI cold start with wrfout_d01_${time_str}"
## cycle surface variables
      if [ -r "${RAPGES_SFC}/wrfout_d01_${HH}" ]; then
          echo "cycle Surface fields based on ${RAPGES_SFC}/wrfout_d01_${HH} "
          cp ${RAPGES_SFC}/wrfout_d01_${HH} ./wrfoutSfc
          poe ${EXECrap}/rap_full_cycle_surface >> $DATA/$pgmout 2>errfile 
          export err=$?; err_chk
      else
          echo "${RAPGES_SFC}/wrfout_d01_${HH} does not exist!!"
          echo "WARNING: no surface data cycled for background at ${time_str}!!!!"
      fi
   else
      echo "${RAPBC}/wrfinput_d01_${time_str} does not exist!!"
      echo "ERROR: no background file for analaysis at ${time_str}!!!!"
      echo " Cycle ${YYYYMMDDHH}: GSI failed because of no background" 
      err_exit
   fi
fi

## update SST
time_updateSST='00'
if [ ${HH} -eq ${time_updateSST} ]; then
  echo "update SST "
  NCEPSST=$COMGFS/sst.${YYYYMMDD}
  NCEPSST2=$COMGFS/sst.${YMDm1}
  cp ${FIXrap}/rap_RTG_SST_landmask.dat ./RTG_SST_landmask.dat
  cp ${FIXrap}/rap_geo_em.d01.int  ./geo_em.d1
  if [ -r "${NCEPSST}/rtgssthr_grb_0.083" ]; then
    cp ${NCEPSST}/rtgssthr_grb_0.083 SSTRTG
    cp ${NCEPSST}/sst2dvar.t12z.nam_grid SST14km
    poe ${EXECrap}/rap_process_sst >> $DATA/$pgmout 2>errfile 
    export err=$?; err_chk
  elif [ -r "${NCEPSST2}/rtgssthr_grb_0.083" ]; then
    cp ${NCEPSST2}/rtgssthr_grb_0.083 SSTRTG
    cp ${NCEPSST2}/sst2dvar.t12z.nam_grid SST14km
    poe ${EXECrap}/rap_process_sst >> $DATA/$pgmout 2>errfile 
    export err=$?; err_chk
  else
    echo "NOTE: no SST data available for ${HH}z cycle!"
  fi
else
  echo "NOTE: we do not update SST data for this cycle!"
fi

## trim snow
trim_snow='23'
trim_snow2='15'
if [ ${HH} -eq ${trim_snow} -o ${HH} -eq ${trim_snow2} ]; then
  if [ -r "${COMIN}/rap.t${HH}z.imssnow" ]; then
     echo "trim snow based on imssnow "
     cp  ${COMIN}/rap.t${HH}z.imssnow ./imssnow
     cp  ${FIXrap}/rap_geo_em.d01.int ./geo_em.d1 
     poe ${EXECrap}/rap_process_imssnow >> $DATA/$pgmout 2>errfile 
     export err=$?; err_chk
  else
    echo "${COMIN}/rap.t${HH}z.imssnow does not exist!!"
    echo "no snow file available at for ${HH}z cycle!!!!"
  fi
else
  echo "NOTE: we do not trim snow during this cycle!"
fi

# prepbufr section
ln -s ${PREPDIR}/rap.${YYYYMMDD}/rap.t${HH}z.prepbufr.tm00 ./prepbufr

if [ -r "${PREPDIR}/rap.${YYYYMMDD}/rap.t${HH}z.mosaic.bufr" ]; then
  ln -s ${PREPDIR}/rap.${YYYYMMDD}/rap.t${HH}z.mosaic.bufr ./refInGSI
else
  echo "Warning: Processed Mosaic data not available"
fi

if [ -r "${PREPDIR}/rap.${YYYYMMDD}/rap.t${HH}z.nasacloud.bufr" ]; then
  ln -s ${PREPDIR}/rap.${YYYYMMDD}/rap.t${HH}z.nasacloud.bufr ./larcInGSI
else
  echo "Warning: Processed Cloud data not available"
fi

if [ -r "${PREPDIR}/rap.${YYYYMMDD}/rap.t${HH}z.1bamua.tm00.bufr_d" ]; then
  ln -s ${PREPDIR}/rap.${YYYYMMDD}/rap.t${HH}z.1bamua.tm00.bufr_d ./amsuabufr
else
  echo "Warning: ${PREPDIR}/rap.${YYYYMMDD}/rap.t${HH}z.1bamua.tm00.bufr_d is not available"
fi

if [ -r "${PREPDIR}/rap.${YYYYMMDD}/rap.t${HH}z.1bamub.tm00.bufr_d" ]; then
  ln -s ${PREPDIR}/rap.${YYYYMMDD}/rap.t${HH}z.1bamub.tm00.bufr_d ./amsubbufr
else
  echo "Warning: ${PREPDIR}/rap.${YYYYMMDD}/rap.t${HH}z.1bamub.tm00.bufr_d is not available"
fi

if [ -r "${PREPDIR}/rap.${YYYYMMDD}/rap.t${HH}z.1bhrs3.tm00.bufr_d" ]; then
  ln -s ${PREPDIR}/rap.${YYYYMMDD}/rap.t${HH}z.1bhrs3.tm00.bufr_d ./hirs3bufr
else
  echo "Warning: ${PREPDIR}/rap.${YYYYMMDD}/rap.t${HH}z.1bhrs3.tm00.bufr_d is not available"
fi

if [ -r "${PREPDIR}/rap.${YYYYMMDD}/rap.t${HH}z.1bhrs4.tm00.bufr_d" ]; then
  ln -s ${PREPDIR}/rap.${YYYYMMDD}/rap.t${HH}z.1bhrs4.tm00.bufr_d ./hirs4bufr
else
  echo "Warning: ${PREPDIR}/rap.${YYYYMMDD}/rap.t${HH}z.1bhrs4.tm00.bufr_d is not available"
fi

if [ -r "${PREPDIR}/rap.${YYYYMMDD}/rap.t${HH}z.1bmhs.tm00.bufr_d" ]; then
  ln -s ${PREPDIR}/rap.${YYYYMMDD}/rap.t${HH}z.1bmhs.tm00.bufr_d ./mhsbufr
else
  echo "Warning: ${PREPDIR}/rap.${YYYYMMDD}/rap.t${HH}z.1bmhs.tm00.bufr_d is not available"
fi

# Set fixed files
#   berror   = forecast model background error statistics
#   specoef  = CRTM spectral coefficients
#   trncoef  = CRTM transmittance coefficients
#   emiscoef = CRTM coefficients for IR sea surface emissivity model
#   aerocoef = CRTM coefficients for aerosol effects
#   cldcoef  = CRTM coefficients for cloud effects
#   satinfo  = text file with information about assimilation of brightness temperatures
#   satangl  = angle dependent bias correction file (fixed in time)
#   pcpinfo  = text file with information about assimilation of prepcipitation rates
#   ozinfo   = text file with information about assimilation of ozone data
#   errtable = text file with obs error for conventional data (regional only)
#   convinfo = text file with information about assimilation of conventional data
#   bufrtable= text file ONLY needed for single obs test (oneobstest=.true.)
#   bftab_sst= bufr table for sst ONLY needed for sst retrieval (retrieval=.true.)

anavinfo=${FIXrap}/rap_anavinfo_arw_netcdf
BERROR=${FIXrap}/rap_berror_stats_global
SATANGL=${FIXrap}/rap_global_satangbias.txt
SATINFO=${FIXrap}/rap_global_satinfo.txt
RTMFIX=${FIXcrtm}/crtm_2.0.2

RTMEMIS=${RTMFIX}/EmisCoeff/Big_Endian/EmisCoeff.bin
RTMAERO=${RTMFIX}/AerosolCoeff/Big_Endian/AerosolCoeff.bin
RTMCLDS=${RTMFIX}/CloudCoeff/Big_Endian/CloudCoeff.bin
CONVINFO=${FIXrap}/rap_nam_regional_convinfo
OZINFO=${FIXrap}/rap_global_ozinfo.txt    
PCPINFO=${FIXrap}/rap_global_pcpinfo.txt

OBERROR=${FIXrap}/rap_nam_errtable.r3dv
AIRCRAFTREJECTLIST=${FIXrap}/rap_current_bad_aircraft.txt   # aircraft reject list

# Fixed fields
 cp $anavinfo anavinfo
 cp $BERROR   berror_stats
 cp $SATANGL  satbias_angle
 cp $SATINFO  satinfo
 cp $RTMEMIS  EmisCoeff.bin
 cp $RTMAERO  AerosolCoeff.bin
 cp $RTMCLDS  CloudCoeff.bin
 cp $CONVINFO convinfo
 cp $OZINFO   ozinfo
 cp $PCPINFO  pcpinfo
 cp $OBERROR  errtable
 cp $AIRCRAFTREJECTLIST current_bad_aircraft
# 
#    # CRTM Spectral and Transmittance coefficients
    nsatsen=`cat satinfo | wc -l`
    isatsen=1
    while [[ $isatsen -le $nsatsen ]]; do
       flag=`head -n $isatsen satinfo | tail -1 | cut -c1-1`
       if [[ "$flag" != "!" ]]; then
          satsen=`head -n $isatsen satinfo | tail -1 | cut -f 2 -d" "`
          spccoeff=${satsen}.SpcCoeff.bin
          if  [[ ! -s $spccoeff ]]; then
             cp $RTMFIX/SpcCoeff/Big_Endian/$spccoeff $spccoeff
             cp $RTMFIX/TauCoeff/Big_Endian/${satsen}.TauCoeff.bin ${satsen}.TauCoeff.bin
          fi
       fi
       isatsen=` expr $isatsen + 1 `
    done

# Only need this file for single obs test
 bufrtable=${PARMrap}/rap_prepobs.bufrtable
 cp $bufrtable ./prepobs_prep.bufrtable

# Set some parameters for use by the GSI executable and to build the namelist
export JCAP=62
export LEVS=60
export DELTIM=${DELTIM:-$((3600/($JCAP/20)))}

# Build the GSI namelist on-the-fly
cat << EOF > gsiparm.anl
 &SETUP
   miter=2,niter(1)=50,niter(2)=50,
   write_diag(1)=.true.,write_diag(2)=.false.,write_diag(3)=.true.,
   qoption=2,
   gencode=78,factqmin=0.0,factqmax=0.0,deltim=$DELTIM,
   ndat=67,npred=5,iguess=-1,
   oneobtest=.flase.,retrieval=.false.,
   nhr_assimilation=3,l_foto=.false.,
   use_pbl=.false.,
 /     
 &GRIDOPTS
   JCAP=$JCAP,JCAP_B=$JCAP_B,NLAT=$NLAT,NLON=$LONA,nsig=$LEVS,hybrid=.true.,
   wrf_nmm_regional=.false.,wrf_mass_regional=.true.,
   diagnostic_reg=.false.,
   filled_grid=.false.,half_grid=.true.,netcdf=.false.,
 /
 &BKGERR
   vs=1.0,
   hzscl=0.373,0.746,1.5,
   bw=0.,fstat=.true.,
/
 &ANBKGERR
   anisotropic=.false.,
 /
 &JCOPTS
 /
 &STRONGOPTS
   jcstrong=.false.,
 /
 &OBSQC
   dfact=0.75,dfact1=3.0,noiqc=.false.,c_varqc=0.02,vadfile='prepbufr',
 /
 &OBS_INPUT
   dmesh(1)=120.0,dmesh(2)=60.0,dmesh(3)=60.0,dmesh(4)=60.0,dmesh(5)=120,time_window_max=1.5,
   dfile(01)='prepbufr',  dtype(01)='ps',        dplat(01)=' ',         dsis(01)='ps',                  dval(01)=1.0,  dthin(01)=0,
   dfile(02)='prepbufr'   dtype(02)='t',         dplat(02)=' ',         dsis(02)='t',                   dval(02)=1.0,  dthin(02)=0,
   dfile(03)='prepbufr',  dtype(03)='q',         dplat(03)=' ',         dsis(03)='q',                   dval(03)=1.0,  dthin(03)=0,
   dfile(04)='prepbufr',  dtype(04)='uv',        dplat(04)=' ',         dsis(04)='uv',                  dval(04)=1.0,  dthin(04)=0,
   dfile(05)='prepbufr',  dtype(05)='spd',       dplat(05)=' ',         dsis(05)='spd',                 dval(05)=1.0,  dthin(05)=0,
   dfile(06)='radarbufr', dtype(06)='rw',        dplat(06)=' ',         dsis(06)='rw',                  dval(06)=1.0,  dthin(06)=0,
   dfile(07)='prepbufr',  dtype(07)='dw',        dplat(07)=' ',         dsis(07)='dw',                  dval(07)=1.0,  dthin(07)=0,
   dfile(08)='prepbufr',  dtype(08)='sst',       dplat(08)=' ',         dsis(08)='sst',                 dval(08)=1.0,  dthin(08)=0,
   dfile(09)='prepbufr',  dtype(09)='pw',        dplat(09)=' ',         dsis(09)='pw',                  dval(09)=1.0,  dthin(09)=0,
   dfile(10)='gpsrobufr', dtype(10)='gps_ref',   dplat(10)=' ',         dsis(10)='gps_ref',             dval(10)=1.0,  dthin(10)=0,
   dfile(11)='ssmirrbufr',dtype(11)='pcp_ssmi',  dplat(11)='dmsp',      dsis(11)='pcp_ssmi',            dval(11)=1.0,  dthin(11)=-1,
   dfile(12)='tmirrbufr', dtype(12)='pcp_tmi',   dplat(12)='trmm',      dsis(12)='pcp_tmi',             dval(12)=1.0,  dthin(12)=-1,
   dfile(13)='sbuvbufr',  dtype(13)='sbuv2',     dplat(13)='n16',       dsis(13)='sbuv8_n16',           dval(13)=1.0,  dthin(13)=0,
   dfile(14)='sbuvbufr',  dtype(14)='sbuv2',     dplat(14)='n17',       dsis(14)='sbuv8_n17',           dval(14)=1.0,  dthin(14)=0,
   dfile(15)='sbuvbufr',  dtype(15)='sbuv2',     dplat(15)='n18',       dsis(15)='sbuv8_n18',           dval(15)=1.0,  dthin(15)=0,
   dfile(16)='omi',       dtype(16)='omi',       dplat(16)='aura',      dsis(16)='omi_aura',            dval(16)=1.0,  dthin(16)=6,
   dfile(17)='hirs2bufr', dtype(17)='hirs2',     dplat(17)='n14',       dsis(17)='hirs2_n14',           dval(17)=6.0,  dthin(17)=1,
   dfile(18)='hirs3bufr', dtype(18)='hirs3',     dplat(18)='n16',       dsis(18)='hirs3_n16',           dval(18)=0.0,  dthin(18)=1,
   dfile(19)='hirs3bufr', dtype(19)='hirs3',     dplat(19)='n17',       dsis(19)='hirs3_n17',           dval(19)=6.0,  dthin(19)=1,
   dfile(20)='hirs4bufr', dtype(20)='hirs4',     dplat(20)='n18',       dsis(20)='hirs4_n18',           dval(20)=0.0,  dthin(20)=1,
   dfile(21)='hirs4bufr', dtype(21)='hirs4',     dplat(21)='metop-a',   dsis(21)='hirs4_metop-a',       dval(21)=6.0,  dthin(21)=1,
   dfile(22)='gsndrbufr', dtype(22)='sndr',      dplat(22)='g11',       dsis(22)='sndr_g11',            dval(22)=0.0,  dthin(22)=1,
   dfile(23)='gsndrbufr', dtype(23)='sndr',      dplat(23)='g12',       dsis(23)='sndr_g12',            dval(23)=0.0,  dthin(23)=1,
   dfile(24)='gimgrbufr', dtype(24)='goes_img',  dplat(24)='g11',       dsis(24)='imgr_g11',            dval(24)=0.0,  dthin(24)=1,
   dfile(25)='gimgrbufr', dtype(25)='goes_img',  dplat(25)='g12',       dsis(25)='imgr_g12',            dval(25)=0.0,  dthin(25)=1,
   dfile(26)='airsbufr',  dtype(26)='airs',      dplat(26)='aqua',      dsis(26)='airs281SUBSET_aqua',  dval(26)=20.0, dthin(26)=1,
   dfile(27)='msubufr',   dtype(27)='msu',       dplat(27)='n14',       dsis(27)='msu_n14',             dval(27)=2.0,  dthin(27)=2,
   dfile(28)='amsuabufr', dtype(28)='amsua',     dplat(28)='n15',       dsis(28)='amsua_n15',           dval(28)=10.0, dthin(28)=2,
   dfile(29)='amsuabufr', dtype(29)='amsua',     dplat(29)='n16',       dsis(29)='amsua_n16',           dval(29)=0.0,  dthin(29)=2,
   dfile(30)='amsuabufr', dtype(30)='amsua',     dplat(30)='n17',       dsis(30)='amsua_n17',           dval(30)=0.0,  dthin(30)=2,
   dfile(31)='amsuabufr', dtype(31)='amsua',     dplat(31)='n18',       dsis(31)='amsua_n18',           dval(31)=10.0, dthin(31)=2,
   dfile(32)='amsuabufr', dtype(32)='amsua',     dplat(32)='metop-a',   dsis(32)='amsua_metop-a',       dval(32)=10.0, dthin(32)=2,
   dfile(33)='airsbufr',  dtype(33)='amsua',     dplat(33)='aqua',      dsis(33)='amsua_aqua',          dval(33)=5.0,  dthin(33)=2,
   dfile(34)='amsubbufr', dtype(34)='amsub',     dplat(34)='n15',       dsis(34)='amsub_n15',           dval(34)=3.0,  dthin(34)=3,
   dfile(35)='amsubbufr', dtype(35)='amsub',     dplat(35)='n16',       dsis(35)='amsub_n16',           dval(35)=3.0,  dthin(35)=3,
   dfile(36)='amsubbufr', dtype(36)='amsub',     dplat(36)='n17',       dsis(36)='amsub_n17',           dval(36)=3.0,  dthin(36)=3,
   dfile(37)='mhsbufr',   dtype(37)='mhs',       dplat(37)='n18',       dsis(37)='mhs_n18',             dval(37)=3.0,  dthin(37)=3,
   dfile(38)='mhsbufr',   dtype(38)='mhs',       dplat(38)='metop-a',   dsis(38)='mhs_metop-a',         dval(38)=3.0,  dthin(38)=3,
   dfile(39)='ssmitbufr', dtype(39)='ssmi',      dplat(39)='f13',       dsis(39)='ssmi_f13',            dval(39)=0.0,  dthin(39)=4,
   dfile(40)='ssmitbufr', dtype(40)='ssmi',      dplat(40)='f14',       dsis(40)='ssmi_f14',            dval(40)=0.0,  dthin(40)=4,
   dfile(41)='ssmitbufr', dtype(41)='ssmi',      dplat(41)='f15',       dsis(41)='ssmi_f15',            dval(41)=0.0,  dthin(41)=4,
   dfile(42)='amsrebufr', dtype(42)='amsre_low', dplat(42)='aqua',      dsis(42)='amsre_aqua',          dval(42)=0.0,  dthin(42)=4,
   dfile(43)='amsrebufr', dtype(43)='amsre_mid', dplat(43)='aqua',      dsis(43)='amsre_aqua',          dval(43)=0.0,  dthin(43)=4,
   dfile(44)='amsrebufr', dtype(44)='amsre_hig', dplat(44)='aqua',      dsis(44)='amsre_aqua',          dval(44)=0.0,  dthin(44)=4,
   dfile(45)='ssmisbufr', dtype(45)='ssmis_las', dplat(45)='f16',       dsis(45)='ssmis_f16',           dval(45)=0.0,  dthin(45)=4,
   dfile(46)='ssmisbufr', dtype(46)='ssmis_uas', dplat(46)='f16',       dsis(46)='ssmis_f16',           dval(46)=0.0,  dthin(46)=4,
   dfile(47)='ssmisbufr', dtype(47)='ssmis_img', dplat(47)='f16',       dsis(47)='ssmis_f16',           dval(47)=0.0,  dthin(47)=4,
   dfile(48)='ssmisbufr', dtype(48)='ssmis_env', dplat(48)='f16',       dsis(48)='ssmis_f16',           dval(48)=0.0,  dthin(48)=4,
   dfile(49)='gsnd1bufr', dtype(49)='sndrd1',    dplat(49)='g12',       dsis(49)='sndrD1_g12',          dval(49)=1.5,  dthin(49)=5,
   dfile(50)='gsnd1bufr', dtype(50)='sndrd2',    dplat(50)='g12',       dsis(50)='sndrD2_g12',          dval(50)=1.5,  dthin(50)=5,
   dfile(51)='gsnd1bufr', dtype(51)='sndrd3',    dplat(51)='g12',       dsis(51)='sndrD3_g12',          dval(51)=1.5,  dthin(51)=5,
   dfile(52)='gsnd1bufr', dtype(52)='sndrd4',    dplat(52)='g12',       dsis(52)='sndrD4_g12',          dval(52)=1.5,  dthin(52)=5,
   dfile(53)='gsnd1bufr', dtype(53)='sndrd1',    dplat(53)='g11',       dsis(53)='sndrD1_g11',          dval(53)=1.5,  dthin(53)=5,
   dfile(54)='gsnd1bufr', dtype(54)='sndrd2',    dplat(54)='g11',       dsis(54)='sndrD2_g11',          dval(54)=1.5,  dthin(54)=5,
   dfile(55)='gsnd1bufr', dtype(55)='sndrd3',    dplat(55)='g11',       dsis(55)='sndrD3_g11',          dval(55)=1.5,  dthin(55)=5,
   dfile(56)='gsnd1bufr', dtype(56)='sndrd4',    dplat(56)='g11',       dsis(56)='sndrD4_g11',          dval(56)=1.5,  dthin(56)=5,
   dfile(57)='gsnd1bufr', dtype(57)='sndrd1',    dplat(57)='g13',       dsis(57)='sndrD1_g13',          dval(57)=1.5,  dthin(57)=5,
   dfile(58)='gsnd1bufr', dtype(58)='sndrd2',    dplat(58)='g13',       dsis(58)='sndrD2_g13',          dval(58)=1.5,  dthin(58)=5,
   dfile(59)='gsnd1bufr', dtype(59)='sndrd3',    dplat(59)='g13',       dsis(59)='sndrD3_g13',          dval(59)=1.5,  dthin(59)=5,
   dfile(60)='gsnd1bufr', dtype(60)='sndrd4',    dplat(60)='g13',       dsis(60)='sndrD4_g13',          dval(60)=1.5,  dthin(60)=5,
   dfile(61)='iasibufr',  dtype(61)='iasi',      dplat(61)='metop-a',   dsis(61)='iasi616_metop-a',     dval(61)=20.0, dthin(61)=1,
   dfile(62)='gomebufr',  dtype(62)='gome',      dplat(62)='metop-a',   dsis(62)='gome_metop-a',        dval(62)=1.0,  dthin(62)=6,
   dfile(63)='prepbufr',  dtype(63)='mta_cld',   dplat(63)=' ',         dsis(63)='mta_cld',             dval(63)=1.0,  dthin(63)=0,
   dfile(64)='prepbufr',  dtype(64)='gos_ctp',   dplat(64)=' ',         dsis(64)='gos_ctp',             dval(64)=1.0,  dthin(64)=0,
   dfile(65)='refInGSI',  dtype(65)='rad_ref',   dplat(65)=' ',         dsis(65)='rad_ref',             dval(65)=1.0,  dthin(65)=0,
   dfile(66)='lghtInGSI', dtype(66)='lghtn',     dplat(66)=' ',         dsis(66)='lghtn',               dval(66)=1.0,  dthin(66)=0,
   dfile(67)='larcInGSI', dtype(67)='larccld',   dplat(67)=' ',         dsis(67)='larccld',             dval(67)=1.0,  dthin(67)=0,
 /
 &SUPEROB_RADAR
   del_azimuth=5.,del_elev=.25,del_range=5000.,del_time=.5,elev_angle_max=5.,minnum=50,range_max=100000., l2superob_only=.false.,
 /
 &LAG_DATA
 /
 &HYBRID_ENSEMBLE
   l_hyb_ens=.false.,
 /
 &RAPIDREFRESH_CLDSURF
   l_cloud_analysis=.true.,
   dfi_radar_latent_heat_time_period=10.0,
   metar_impact_radius=10.0,
   metar_impact_radius_lowCloud=4.0,
   l_gsd_terrain_match_surfTobs=.true.,
   l_gsd_th2_adjust=.true.,
   l_limit_ocean_q=.true.
 /
 &SINGLEOB_TEST
   maginnov=1.0,magoberr=0.8,oneob_type='t',
   oblat=38.,oblon=279.,obpres=500.,obdattim=${YYYYMMDDHH},
   obhourset=0.,
 /

EOF

cp ${FIXrap}/rap_ndas.t06z.satbias.tm03 ./satbias_in

export pgm=rap_gsi
. prep_step

startmsg
poe ${EXECrap}/rap_gsi < gsiparm.anl >> $DATA/$pgmout 2>errfile 
export err=$?; err_chk

mv fort.201 fit_p1
mv fort.202 fit_w1
mv fort.203 fit_t1
mv fort.204 fit_q1
mv fort.205 fit_pw1
mv fort.206 fit_oz1
mv fort.207 fit_rad1
mv fort.208 fit_pcp1
mv fort.209 fit_rw1
mv fort.213 fit_sst1

cat fit_p1 fit_w1 fit_t1 fit_q1 fit_pw1 fit_rad1 fit_rw1 > ${COMOUT}/rap.t${cyc}z.fits.${tmmark}
cat fort.210 fort.211 fort.212 fort.214 fort.215 fort.217 fort.220 > ${COMOUT}/rap.t${cyc}z.fits2.${tmmark}

cp satbias_out ${COMOUT}/rap.t${cyc}z.satbias.${tmmark}
cp wrf_inout ${COMOUT}/rap.t${cyc}z.wrf_inout

msg="JOB $job HAS COMPLETED NORMALLY."
postmsg "$jlogfile" "$msg"

exit 0
