#!/bin/csh -fx

# FVSSI
#
# Secondary driver for the fvSSI/GSI analysis in stand-alone mode
#
# !REVISION HISTORY:
#
#  ?????????  Cruz      Initial code
#  27Feb2004  Todling   Updated to work with latest changes in SSI/GSI
#  17May2004  Todling   Updated to bring 3 hourly background
#  19Jul2004  Todling   Added handler for unblocked buffer files
#  02Jul2005  Todling   A number of fixes in the call to acquire
#  28Oct2005  Todling   Modified to allow running asyn-bkg case offline
#                       Added acquire tcvitals for vtxrlc run
#  30Aug2006  Zhang/Guo Added bias correction options
#  21Sep2006  Meta/JCS  Bring all tmpl files into work directory, not just
#                       sac.nl.tmpl
#  13Dec2006  Todling   Bug fix in handling +3hr bkg files
#  30Jan2007  Todling   Add pre-qc file handler
#  15Feb2007  Todling   Changed def of ASYNBKG from hr to mn; generalized 
#                       acquiring obs and bkg for any ana time window
#  01Nov2007  Todling   Implemented obs sensitivity capability
#  23Sep2008  Todling   Generalized to do analysis from prog files (omb and omf)
#  05Oct2008  Todling   - Added acquire for discover
#                       - Block/Unblock as in GEOSdas.csm
#  05Feb2009  Todling   Update interface to analyzer
#  17Feb2009  Todling   Update interface to gsidiags; get sigo-imp
#  13Mar2009  Todling   Generalized suffix of NC files (hdf vs nc4)
#  30Mar2009  Todling   Upate interface to diag2ods
#  27Jul2009  Stassi/TO Sed EXPID in GSI_GridComp.rc.tmpl
#  02Nov2009  Todling   Update interface to analyzer
#  27Jan2010  Todling   Working to bring ADJ-GSI back to life
#  11Feb2010  Todling   Allow standalone to run independently of DAS (gsisa.rc.tmpl)
#  23Aug2010  Todling   Add USRMITER to allow setting diff miter for adjoint than forward
#  02Aug2011  Stassi    Use getsponsor.pl wrapper script
#  22Jun2013  Todling   Knob to calculate ana sens to background
#  06Feb2014  Todling   Add GETOBSENS_ONLY to allow placing sens rather than imp in ODS files
#  28Feb2014  Sienkiewicz Modify aircraft bias correction to add options 2 & 3
#                            for NCEP variational bias correction methods
#  23Jul2020  Todling   Add ability to generate agcm_import w/ IAU inc to allow fcst from SA
#------------------------------------------------------------------

# This scripts acquires data from storage and runs the analyzer

  set myname = `basename $0`

  set strict =    "" # by default do not be strict about input files
  set log    =     0 # by default do not log warnings or errors

  setenv FAILED 0
  if ( !($?ACFTBIAS)  )  setenv ACFTBIAS 0
  if ( !($?ANA4DUPD_IAU0_ONLY) ) setenv ANA4DUPD_IAU0_ONLY 0 # assume 4d increment
  if ( !($?ANGLEBC) )    setenv ANGLEBC  0
  if ( !($?BATCH_SUBCMD)  )  setenv BATCH_SUBCMD "sbatch"
  if ( !($?CLEANUP_TAIL) )        setenv CLEANUP_TAIL  0
  if ( !($?DATAMOVE_CONSTRAINT)  )  setenv DATAMOVE_CONSTRAINT NULL
  if ( !($?INCSENS)   )  setenv INCSENS 0
  if ( !($?GSI_NETCDF_DIAG) )  setenv GSI_NETCDF_DIAG 0
  if ( !($?SPECRES)   )  setenv FAILED 1
  if ( !($?VAROFFSET) )  setenv FAILED 1
  if ( !($?TIMEINC)   )  setenv FAILED 1
  if ( !($?STRICT)    )  setenv STRICT 0
  if ( !($?NVAROUTER) )  setenv NVAROUTER 1
  if ( !($?DO4DIAU)   )  setenv DO4DIAU 0
  if ( !($?DO4DVAR)   )  setenv DO4DVAR 0
  if ( !($?DIAG2ODS)  )  setenv DIAG2ODS 1
  if ( !($?ANASENS)   )  setenv ANASENS 0
  if ( !($?NEWRADBC)  )  setenv NEWRADBC 0
  if ( !($?NCSUFFIX)  )  setenv NCSUFFIX nc4
  if ( !($?LOCAL_ACQUIRE)  )  setenv LOCAL_ACQUIRE 0
  if ( !($?GETOBSENS_ONLY)  )  setenv GETOBSENS_ONLY 0

  if ( $FAILED ) exit 1

# Determine frequency of background availability
# ----------------------------------------------
  if ( !($?ASYNBKG)   )  setenv ASYNBKG 360
  @ bkgfq = $ASYNBKG / 60
  @ bkgfq = $bkgfq * 10000 + ( $ASYNBKG - $bkgfq * 60 ) * 100
  set asyn =
  if ( $ASYNBKG != 360 ) set asyn = "-no_asyn"

  # initialize flags
  # skip transforms
  set STF = ""
  # skip solver
  set SKA = ""
  # skip satbias
  set SSB = ""

# Parse command line
# ------------------
  if ( $#argv > 0 ) then
  foreach token ( $argv )
     if ( "$token" == "-h" ) then
          goto usage
     endif
     if ( "$token" == "-strict" ) then
          set strict = "-strict"  # bomb if not all input files are there
          setenv STRICT 1
          shift
     endif
     if ( "$token" == "-obsclass" ) then
          shift
          set obsclass = $1
          shift
     endif
     if ( "$token" == "-gsensdir" ) then
          if ( ! $ANASENS ) then
              echo $myname": input inconsistent with environment "
              exit 1
          endif
          shift
          set gsensdir = $1
          shift
     endif
     if ( "$token" == "-xttag" ) then
          if ( ! $ANASENS ) then
              echo $myname": input inconsistent with environment "
              exit 1
          endif
          shift
          set xttag = $1
          shift
     endif
     if ( "$token" == "-log" ) then
          set log = 1  # write warning and error messages to log
          shift
     endif
     if ( "$token" == "-skipSOLVER" ) then
          set SKA = "-skipSOLVER"
          shift
     endif
     if ( "$token" == "-skipTRANSF" ) then
          set STF = "-skipTRANSF"
          shift
     endif
     if ( "$token" == "-skipSATBIAS" ) then
          set SSB = "-skipSATBIAS"
          shift
     endif
  end
  endif
  if ( $#argv < 6 ) then
       echo $myname": insufficient command line arguments"
       goto usage
  else
       setenv FVHOME  $1
       setenv FVWORK  $2
       setenv FVICS   $3
       setenv YMD     $4
       setenv HMS     $5
       setenv IDENT   $6
       setenv EXPID   `basename $FVHOME`

       set specres   = $SPECRES
       set expid     = $EXPID
  endif

# Check relevant env variables
# ----------------------------
  if ( $?FVSPOOL ) then
     set spool = "-s $FVSPOOL "
  else
     set diren = `dirname $FVHOME`
     set spool = "-s $diren/spool "
  endif


# Check whether relevant directories actually exist
# -------------------------------------------------
  if ( ! (-d $FVHOME) ) then
    echo $myname": cannot find FVHOME directory $FVHOME"
    exit 1
  endif
  if ( ! (-d $FVWORK) ) then
    echo $myname": cannot find FVWORK directory $FVWORK"
    exit 1
  endif

# Relevant background comes from ...
# ----------------------------------
  if ( $ANASENS ) then
       set getbkg = "asens.acq"
       set getbkg09 = "bkg09.acq"
  else
       set getbkg = "bkg.acq"
  endif
#                 -------------------------------------
#                  PART I - Prepare Working Directory
#                 -------------------------------------

# Set up working directory and copy all the restart files along
# with analysis resource files, model namelists and diagnostic table
# file to this directory. The simulation is carried out here in
# the working directory.
# ------------------------------------------------------------------
  cd $FVWORK

# Copy resource and restart files to here
# ---------------------------------------
  /bin/cp $FVHOME/run/*.arc  .      # archiving rules
  /bin/cp $FVHOME/run/*.acq  .      # acquiring rules
  /bin/cp $FVHOME/run/*.rc   .      # acquire resource files
  /bin/cp $FVHOME/run/gocart/*.rc . # acquire chemistry resource files
  /bin/cp $FVHOME/run/*.tmpl .      # acquire namelist template files
  /bin/cp $FVHOME/run/*table .      # buffer tables (only needed for testing)
  /bin/cp $FVHOME/run/*.tbl  .      # another suffix for tables!!
  /bin/cp $FVHOME/run/prepob* .     # oiqc-related files
  /bin/cp $FVHOME/run/*.namelist .  # old fvgcm stuff
  /bin/cp $FVHOME/fcst/initadj.rc . # at least one rc file from fcst (need by adm only)
  /bin/cp $FVHOME/recycle/$expid.*gesfile gesfile

  if ( $ANASENS ) then
    set ASENSDIR = $FVHOME/asens
    if (-d $FVHOME/run/asens ) set ASENSDIR = $FVHOME/run/asens

    foreach fn ( `ls $ASENSDIR/*.rc` )
       echo "Overwriting $fn with that inside anasa"
       /bin/cp $fn  .
    end
    foreach fn ( `ls $ASENSDIR/*.tmpl` )
       echo "Overwriting $fn with that inside anasa"
       /bin/cp $fn  .
    end
    foreach fn ( `ls $ASENSDIR/*.acq` )
       echo "Overwriting $fn with that inside anasa"
       /bin/cp $fn  .
    end

    if ( ! -e asensrules.rc ) then # (needed for GQ order)
       if ( -e $FVHOME/fcst/fvsens.ccmrun.namelist.tmpl ) then
          /bin/cp $FVHOME/fcst/fvsens.ccmrun.namelist.tmpl asensrules.rc
       else
          echo $myname": cannot find neither fvsens.ccmrun.namelist.tmpl nor asensrules.rc"
          exit 1
       endif
    endif

# Mods for ops
# ------------
    if( -e $ASENSDIR/initadj.rc           ) /bin/cp $ASENSDIR/initadj.rc . # at least one rc file from fcst (need by adm only)
    if( -e $ASENSDIR/GSI_GridComp.rc.tmpl ) /bin/cp $ASENSDIR/GSI_GridComp.rc.tmpl . # Allow independent CPU configuration from analysis
    if( -e $ASENSDIR/gsi_sens.rc.tmpl     ) /bin/cp $ASENSDIR/gsi_sens.rc.tmpl .

  else
    foreach fn ( `ls $FVHOME/anasa/*.rc` )
       echo "Overwriting $fn with that inside anasa"
       /bin/cp $fn  .
    end
    foreach fn ( `ls $FVHOME/anasa/*.tmpl` )
       echo "Overwriting $fn with that inside anasa"
       /bin/cp $fn  .
    end
    foreach fn ( `ls $FVHOME/anasa/*.acq` )
       echo "Overwriting $fn with that inside anasa"
       /bin/cp $fn  .
    end
  endif

  if ( ! -e $getbkg ) then
    echo $myname": cannot find $getbkg file needed to retrieve background files"
    exit 1
  endif

  # acquire initial conditions
  
  @ aoffset_hrs   = $VAROFFSET  / 60;   @ aoffset_sec    = $VAROFFSET  * 60
  @ varwindow_hrs = $TIMEINC    / 60;   @ varwindow_sec  = $TIMEINC    * 60
  @ nbtimes       = $TIMEINC / ( $ASYNBKG ) + 1
  if ( $ASYNBKG == 360 ) set nbtimes = 1

  set buf   = `tick $YMD $HMS -$aoffset_sec`       # tick clock to the start of time window
  set nymdm = $buf[1]
  set nhmsm = $buf[2]
  @ thishrs = ${nhmsm} / 10000
  set hhm   = `echo $thishrs |awk '{printf "%02d", $1}'` # two-digit hour this seg ends

  set buf  = ( `tick $YMD $HMS -$varwindow_sec` )   # previous analysis time
  set nymd0 = $buf[1]
  set nhms0 = $buf[2]
  set hh0   = `echo $buf[2] | cut -c1-2`
  set qsub_acquire = 0  

  if ( (`uname -n` !~ borg*) || ( $LOCAL_ACQUIRE ) ) then

       if ( $INCSENS ) then
          acquire -v -rc ana.acq  -d $FVICS $spool         -ssh $YMD $HMS 060000 1
       endif
#      acquire -v -rc $getbkg     -d $FVICS $spool -strict -ssh $nymdm $nhmsm $bkgfq $nbtimes
       acquire -v -rc $getbkg     -d $FVICS $spool         -ssh $nymdm $nhmsm $bkgfq $nbtimes
       if ( $?getbkg09 ) then
            if ( ! $DO4DVAR ) then
               acquire -v -rc $getbkg09 -d $FVICS $spool -ssh $nymdm $nhmsm 060000 2
            endif
       endif
       acquire -v -rc satbias.acq -d $FVICS $spool         -ssh $nymd0 $nhms0 060000 1

  else # if on discover, acquire via batch job
       # -------------------------------------
        set fname = "acquire_bkg.pbs" 
        alias fname1 "echo \!* >! $fname"
        alias fname2 "echo \!* >> $fname" 

        set qsub_acquire = 1

        if ($?gid) then
           set gsflg = "-grpID $gid"
        else
           set gsflg = "-dflt"
        endif
        set GID = `getsponsor.pl $gsflg`

        fname1 "#\!/bin/csh -xvf"
        fname2 "#$group_list"
        fname2 "#PBS -N acquire"
        fname2 "#PBS -l nodes=1:ppn=1"
        fname2 "#PBS -l walltime=1:00:00"
        fname2 "#PBS -q datamove"
        fname2 "#PBS -S /bin/csh"
        fname2 "#PBS -V"
        fname2 "#PBS -o acq.log.o%j"
        fname2 "#PBS -j eo"
        fname2 "#SBATCH -A $GID"
        fname2 "#SBATCH --partition=datamove"
        fname2 "#$DATAMOVE_CONSTRAINT"
        fname2 "#SBATCH --time=1:00:00"
        fname2 ""
        fname2 "cd $FVWORK"
        fname2 "if ( $INCSENS ) then"
        fname2 "acquire -v -rc ana.acq     -d $FVICS $spool         -ssh $YMD $HMS 060000 1"
        fname2 "endif"
        fname2 "acquire -v -rc $getbkg     -d $FVICS $spool         -ssh $nymdm $nhmsm $bkgfq $nbtimes"
        fname2 "acquire -v -rc satbias.acq -d $FVICS $spool         -ssh $nymd0 $nhms0 060000 1"
        fname2 "exit"

        if ( $BATCH_SUBCMD == "sbatch" ) then
           $BATCH_SUBCMD -W $fname
        else
           $BATCH_SUBCMD -W block=true $fname
        endif
        sleep 2

  endif # <acquire block>

   if ( ($?NCPUS) ) then
     @ d2o_nproc = $NCPUS / 4
     if ($d2o_nproc < 1 ) set d2o_nproc = 1
   else
     set d2o_nproc = 1
   endif

#   Now prepare resource files
#   --------------------------
     set anadt = `grep "^RUN_DT:" GSI_GridComp.rc.tmpl | cut -d: -f2`
     @ adtbck = $aoffset_sec
     @ adtfwd = $varwindow_sec
     set beg_ana = ( `tick $YMD $HMS -$adtbck` )
     set end_ana = ( `tick $beg_ana[1] $beg_ana[2] $adtfwd` )
     set beg_hh  = `echo $beg_ana[2] | cut -c1-2`
     set wrt_ana = ( `echo $beg_ana` )

     /bin/rm -f sed_file
     echo "s/>>>EXPID<<</${EXPID}/1"         > sed_file
     echo "s/>>>IOBBKGD<<</${beg_ana[1]}/1" >> sed_file
     echo "s/>>>IOBBKGT<<</${beg_ana[2]}/1" >> sed_file
     echo "s/>>>IOEBKGD<<</${end_ana[1]}/1" >> sed_file
     echo "s/>>>IOEBKGT<<</${end_ana[2]}/1" >> sed_file
     echo "s/>>>NCSUFFIX<<</${NCSUFFIX}/1"  >> sed_file
     if ( $ANASENS ) then
        echo "s/>>>RECANA<<</NO/1"          >> sed_file
     else
        echo "s/>>>RECANA<<</YES/1"         >> sed_file
     endif
     if ( $DO4DVAR ) then
        echo "s/>>>ANADATE<<</${wrt_ana[1]}/1" >> sed_file
        echo "s/>>>ANATIME<<</${wrt_ana[2]}/1" >> sed_file
     else
        echo "s/>>>ANADATE<<</${YMD}/1"        >> sed_file
        echo "s/>>>ANATIME<<</${HMS}/1"        >> sed_file
     endif
     /bin/rm -f ./GSI_GridComp.rc
     sed -f sed_file  ./GSI_GridComp.rc.tmpl  > ./GSI_GridComp.rc
     append_gsigcrc.pl obsys.rc GSI_GridComp.rc
     if ( $status ) then
          echo "fvssi: trouble appending obs table to GSI_GridComp.rc "
          exit 1
     endif
     cat GSI_GridComp.rc

#    Determine resolution of analysis
#    --------------------------------
     set gsinlat = `echorc.x -rc GSI_GridComp.rc "GSI JM"`
     set gsinlon = `echorc.x -rc GSI_GridComp.rc "GSI IM"`
     set gsinlev = `echorc.x -rc GSI_GridComp.rc "GSI LM"`
 
     set resol = "none"
     if ( $gsinlon ==   72 && $gsinlat ==   46 ) set resol = "a"
     if ( $gsinlon ==  144 && $gsinlat ==   91 ) set resol = "b"
     if ( $gsinlon ==  288 && $gsinlat ==  181 ) set resol = "c"
#    if ( $gsinlon ==  544 && $gsinlat ==  361 ) set resol = "d"
     if ( $gsinlon ==  576 && $gsinlat ==  361 ) set resol = "d"
     if ( $gsinlon == 1152 && $gsinlat ==  721 ) set resol = "e"
     if ( $gsinlon == 2304 && $gsinlat == 1441 ) set resol = "f"
     if ( $resol == "none" ) then
          echo "fvssi: unknown resolution "
          exit 1
     endif

#    If so, get forecast sensitivity vector
#    --------------------------------------
     if ( $ANASENS ) then

        if ( $INCSENS ) then

           set hh = `echo $HMS | cut -c1-2` 
           set reffile = `echorc.x -rc fvpsas.rc -template $EXPID $YMD $HMS reference_eta_filename`
           set verfile = `echorc.x -rc fvpsas.rc -template $EXPID $YMD $HMS verification_eta_filename`
           set jgrdnrm  = inc
           set order    = 1
           set mainc    = $expid.fsens_${jgrdnrm}.eta.${YMD}_${hh}z.$NCSUFFIX
         
           # calculate negative of increment
           if( -e $FVHOME/asens/initadj4inc.rc || -e $FVHOME/run/asens/initadj4inc.rc ) then
              if( -e $FVHOME/asens/initadj4inc.rc ) then
                 set rcNorm = $FVHOME/asens/initadj4inc.rc
              else
                 set rcNorm = $FVHOME/run/asens/initadj4inc.rc
              endif
              set norms  = `echorc.x -rc $rcNorm pert_norm`
              mpirun -np 1 initadj.x -g5 -pick $YMD ${hh}0000 -rc $rcNorm $reffile $verfile
              /bin/mv Jgradf_${norms[1]}.eta.nc4 $mainc
           else
              echo "Error: need initadj4inc.rc to be placed under asens"
              exit 2
           endif

        else

#         Determine how many different vector norms to handle
#         ---------------------------------------------------
          set order = 0
          set lookup = "initadj.rc"
          if ( -e $lookup ) then
             set jgrdnrm = `echorc.x -rc $lookup pert_norm`
          else
            echo " cannot find $lookup file; norm undetermined, aborting ..."
            exit 2
          endif
          @ hh = $HMS / 10000; set hh = `echo $hh |awk '{printf "%02d", $1}'`
          @ norder = `echorc.x -rc asensrules.rc ASENS_ORDER`
          @ norder = `echorc.x -rc asensrules.rc ASENS_ORDER`
          @ tauf   = `echorc.x -rc asensrules.rc integration_length_hrs`
          @ tauf_sec = $tauf * 3600
          set anadate = `tick $YMD $HMS $tauf_sec`
          set GRYMD = $anadate[1]
          set GRHMS = $anadate[2]
          set GRhh  = `echo $GRHMS | cut -c1-2`
          foreach gnorm ( $jgrdnrm )
             set gsenslst =  `ls -1r $gsensdir/$EXPID.fsens_${gnorm}.eta.????????_??z+????????_??z-${YMD}_${hh}z.$NCSUFFIX \
                                     $gsensdir/$EXPID.Jgradf_${gnorm}.eta.????????_??z+${GRYMD}_${GRhh}z.$NCSUFFIX\
                                     $gsensdir/$EXPID.fsensainc_${gnorm}.eta.????????_??z+????????_??z-${YMD}_${hh}z.$NCSUFFIX`
             # Explaining the scale coefficients in each opt:
             #  1) Regular case of doing LB04 approx. - 0.5 is the coeff in front of the fcst gradients, e.g., e(a,b;2) of DT09 
             #  2) First order expression (single gradient) - 1.0 is the coeff in front of gradient, e.g., e(b;1) or e(a;1) of DT09
             #  3) EnVarFSOI - the input in this case is e=C(eb+ea), the C-scaled, miggled, fcst error. Now, dJdf=2C(eb+ea), therefore
             #     a factor of 0.5 is also applied when dJdx if used.
             #  4) The forward/backward form is typically not used and involves handling the gradients dJdf in diff ways
             set scale = 0.5
             set admopt = "-adm"
             set pertype = `echo $gsenslst[1] | cut -d. -f2`
             if ( $pertype == "Jgradf_${gnorm}" ) then
                set scale = 1.0
                set admopt = 
             endif
             if ( $#gsenslst == 1 ) then           # will do 1st order calculation of observation sensitivies
                dynp.x -s $gsenslst[1] -p $gsenslst[1] -gsi -a 0 -g5 -pureadd -realp -twoperts -ainc -adm \
                       -os $expid.fsens_${gnorm}.eta.${YMD}_${hh}z.$NCSUFFIX -res $resol
                if ( $norder == 1 ) then
                   set order = 1
                else if ( $norder == 2 ) then
                   set order = 3
                endif
              else if ( $#gsenslst == 2 ) then
                if ( $pertype == "fsens_${gnorm}" || \
                     $pertype == "Jgradf_${gnorm}" ) then # add two estimates of the gradient to do 3rd order calculation
                                                         # note: because of r4/r8 precision the total is only roundoff the sum of two
                     dynp.x -s $gsenslst[1] -p $gsenslst[2] -gsi -scale $scale -g5 -pureadd -realp -twoperts -ainc $admopt \
                            -os $expid.fsens_${gnorm}.eta.${YMD}_${hh}z.$NCSUFFIX -res $resol
                     set order = 3
                else                                     # add first term and 1/2 the second for alternative calculation
                                                         # of observation impacts using forward and backward integration
                     dynp.x -s $gsenslst[1] -p $gsenslst[2] -gsi -a     0.5 -g5 -pureadd -realp -twoperts -ainc -adm \
                            -os $expid.fsens_${gnorm}.eta.${YMD}_${hh}z.$NCSUFFIX -res $resol
                     set order = N
                endif
              else                                 # this should not happen
                echo "unexpected number of gradient vectors found, aborting ..."
                exit 3 
              endif
           end
 
           # NOTE: whether handling fsens or Jgradf the final vector is named fsens for convenience
        endif # < INCSENS >

     endif # < ANASENS >


  # Run acquire_obsys (note only 1 synoptic time)
  
  if ( "$SKA" != "-skipSOLVER" ) then

       @ notimes = $TIMEINC / 360                 # number of 6-hr times to retrieve obs
       set buf = `tick $YMD $HMS -$varwindow_sec` # tick clock 6-hr back
       set nymd1 = $buf[1]
       set nhms1 = $buf[2]


       if ( (`uname -n` !~ borg*) || ( $LOCAL_ACQUIRE ) ) then

            acquire_obsys -v -d $FVWORK $spool $strict -ssh  \
                                $nymd1 $nhms1 060000 1 ncep_tcvitals  # acquire tcvitals (6-hrs back); 
 
            acquire_obsys -v -d $FVWORK $spool $strict -ssh $YMD $HMS 060000 $notimes $obsclass

       else # if on discover, acquire via batch job
            # -------------------------------------
             set fname = "acquire_obs.pbs" 
             alias fname1 "echo \!* >! $fname"
             alias fname2 "echo \!* >> $fname" 

             set qsub_acquire = 1

             if ($?gid) then
                set gsflg = "-grpID $gid"
             else
                set gsflg = "-dflt"
             endif
             set GID = `getsponsor.pl $gsflg`

             fname1 "#\!/bin/csh -xvf"
             fname2 "#$group_list"
             fname2 "#PBS -N acquire"
             fname2 "#PBS -l nodes=1:ppn=1"
             fname2 "#PBS -l walltime=1:00:00"
             fname2 "#PBS -q datamove"
             fname2 "#PBS -S /bin/csh"
             fname2 "#PBS -V"
             fname2 "#PBS -o acq.log.o%j"
             fname2 "#PBS -j eo"
             fname2 "#SBATCH -A $GID"
             fname2 "#SBATCH --partition=datamove"
             fname2 "#SBATCH --time=1:00:00"
             fname2 ""
             fname2 "cd $FVWORK"
             fname2 "acquire_obsys -v -d $FVWORK $spool $strict -ssh $nymd1 $nhms1 060000 1 ncep_tcvitals" 
             fname2 "acquire_obsys -v -d $FVWORK $spool $strict -ssh $YMD $HMS 060000 $notimes $obsclass"
             fname2 "exit"

             if ( $BATCH_SUBCMD == "sbatch" ) then
                $BATCH_SUBCMD -W $fname
             else
                $BATCH_SUBCMD -W block=true $fname
             endif
             sleep 2

       endif # <acquire block>

#      Get info about pre-qc files to be combined for QC
#      ------------------------------------------------------
       if ( -e obsys.acq ) then

          /bin/cp obsys.acq  ${FVHOME}/run/obsys.acq.latest
          grep pre-qc obsys.acq | grep -v ^\# | cut -d\> -f2 > pre-qc.acq
          cat pre-qc.acq
          ls *.prepbufr.*
          if ( $status && ! -z pre-qc.acq && -e pre-qc.acq ) then
             /bin/mv obsys.acq obsys.acq.orig
             grep -v pre-qc obsys.acq.orig > obsys.acq
             echo "/this/is/a/dummy/path =>  $EXPID.prepbufr.%y4%m2%d2.t%h2z.blk" >> obsys.acq
             /bin/mv ${FVHOME}/run/obsys.acq.latest ${FVHOME}/run/obsys.acq.raw
             /bin/cp obsys.acq  ${FVHOME}/run/obsys.acq.latest
          endif

       endif

  endif

# Fix unblocked files
# -------------------
  zeit_ci.x FixUnblocked
  foreach ublk ( `ls *.ublk` )
      FixUnblocked.csh $ublk &
  end
  wait
  ls *FAILED_BLOCK
  if ( ( ! $status ) && ( $STRICT ) ) exit 96
  zeit_co.x FixUnblocked


# Fix beg_endian/little_endian in the upa-buffer files on OSF1 systems
# --------------------------------------------------------------------
  zeit_ci.x FixEndian
  if ( ( `uname -s` == "OSF1" ) || (`uname -s` == "Linux" && (`uname -m` == "ia64" || `uname -m` == "x86_64")) ) then
     foreach upa ( `ls *upabufr*bfr` )
         Reblock.csh $upa &
     end
     foreach blk ( `ls *.blk` )
         Reblock.csh $blk &
     end
  endif
  wait
  ls *FAILED_BLOCK
  if ( ( ! $status ) && ( $STRICT ) ) exit 96
  zeit_co.x FixEndian


# Files available at working diretory before simulation
# -----------------------------------------------------

  echo ""
  /bin/ls -la
  echo ""

#                  ---------------
#                   PART  II - Run
#                  ---------------

# call ANALYZER here

    @ h2 = $HMS / 10000
    set h2 = `echo $h2 |awk '{printf "%02d", $1}'`

    set anafile  = `echorc.x -rc fvpsas.rc -template $EXPID $YMD $HMS upper-air_ana_filename`
    if ( "$STF" == "-skipTRANSF" ) then
        set infn = "-ssbkg $IDENT.sigbkg.${YMD}_${h2}z.bin -sssbkg $IDENT.sfcbkg.${YMD}_${h2}z.bin" 
    else
        set infn = ""

           if ( ! $DO4DVAR ) then
              set sfc09fn  = `echorc.x -rc fvpsas.rc -template $IDENT $end_ana[1] $end_ana[2] surface_bkg09_filename`
              set bkg09fn  = `echorc.x -rc fvpsas.rc -template $IDENT $end_ana[1] $end_ana[2] upper-air_bkg09_filename`
              set cbkg09fn = `echorc.x -rc fvpsas.rc -template $IDENT $end_ana[1] $end_ana[2] chem_bkg09_filename`
              set filesfc  = `echorc.x -rc fvpsas.rc -template $EXPID $end_ana[1] $end_ana[2] surface_bkg_filename`
              set filebkg  = `echorc.x -rc fvpsas.rc -template $EXPID $end_ana[1] $end_ana[2] upper-air_bkg_filename`
              set filecbkg = `echorc.x -rc fvpsas.rc -template $EXPID $end_ana[1] $end_ana[2] chem_bkg_filename`
              if ( -e $sfc09fn ) then
                   echo "Overwriting bkg.sfc file at end of time window ..."
                   /bin/mv $sfc09fn $filesfc
              endif
              if ( -e $bkg09fn ) then
                   echo "Overwriting bkg.eta file at end of time window ..."
                   /bin/mv $bkg09fn $filebkg
              endif
              if ( -e $cbkg09fn ) then
                   echo "Overwriting cbkg.eta file at end of time window ..."
                   /bin/mv $cbkg09fn $filecbkg
              endif
           endif

    endif

#  Satellite biases need to be from previous time
#  ----------------------------------------------
   if(-e $expid.ana.radstat.${nymd0}_${hh0}z.tar) /bin/mv $expid.ana.radstat.${nymd0}_${hh0}z.tar radstat 
   if(-e $expid.ana.satbias.${nymd0}_${hh0}z.txt) /bin/mv $expid.ana.satbias.${nymd0}_${hh0}z.txt satbias 
   if(-e $expid.ana.satbang.${nymd0}_${hh0}z.txt) /bin/mv $expid.ana.satbang.${nymd0}_${hh0}z.txt satbang
   if(-e $expid.ana.satbiaspc.${nymd0}_${hh0}z.txt) /bin/mv $expid.ana.satbiaspc.${nymd0}_${hh0}z.txt satbiaspc 
   if(-e $expid.ana.acftbias.${nymd0}_${hh0}z.txt) /bin/mv $expid.ana.acftbias.${nymd0}_${hh0}z.txt acftbias
   /bin/mv $FVHOME/recycle/$expid.biasinp.*ctrl   biasinp.ctrl
   /bin/mv $FVHOME/recycle/$expid.biasinp.*grads  biasinp.grads


   if ( $ANASENS ) then

#      Run forward analysis first to save intermediate increments
#      -----------------------------------------------------------
#      if ( $INCSENS ) then
#         setenv ANASENS 0  # de-activate analysis sensitivity to be able to run forward GSI first
#         ln -sf gsisa.rc.tmpl gsi.rc
#         set jiter = 1
#         set alog = "anasa.log"
#         analyzer $YMD $HMS $STF $SKA $SSB $infn \
#                   -oss $anafile -t $specres -levs $gsinlev -x $gsinlon -y $gsinlat -jiter $jiter -lnobs -log $alog $asyn
#         if ( $status ) then
#              echo $myname": abnormal exit from analysis ..."
#              exit 2
#         else
#            if ( -e $expid.ana.eta.${YMD}_${h2}z.$NCSUFFIX ) then
#              /bin/mv $expid.ana.eta.${YMD}_${h2}z.$NCSUFFIX $expid.anasa.eta.${YMD}_${h2}z.$NCSUFFIX
#            endif
#         endif
#         setenv ANASENS 1   # re-activate analysis sensitivity
#      endif

       foreach gnorm ( $jgrdnrm )

       set alog = "asens${order}_${gnorm}.log"
       set olog = "ods${order}_${gnorm}.log"

#      Loop over analysis iterations (backwards)
#      -----------------------------
       if ( ($?USRMITER) ) then
          set miter = $USRMITER
       else
          if ( $DO4DVAR ) then
               set miter = $NVAROUTER  # total number of iterations
          else
               set miter = `grep miter gsi.rc.tmpl | cut -d= -f2 | cut -d, -f1 `
          endif
       endif
       @ jitermax = $miter
       @ jiterend = $miter
       @ jiter    = 1
       set final  = 0
       while ( $jiter <= $jitermax )
            if( $jiter == $jitermax ) set final = 1

#           Prepare gsi.rc for this iteration
#           ---------------------------------
            set myrc = gsi_sens.rc.tmpl
            /bin/rm -f sed_file
            echo "s/>>>MITER<<</$miter/g"             > sed_file # maximum number of iteration
            set jj  = 1
            while ( $jj <= $miter + 5 ) # 5: there's should be smarter way of finding
                                        # out how many GSIWRTDIAG appear in rc file
               if ( $jj == $jiterend + 1 ) then
                    echo "s/>>>GSIWRTDIAG${jj}<<</.true./g"  >> sed_file     # turn on diagnostics in GSI (omf/oma)
               else
                    echo "s/>>>GSIWRTDIAG${jj}<<</.false./g" >> sed_file     # turn off diagnostics during intermediate iterations
               endif
               @ jj++
            end
#           control aircraft bias correction
            switch( $ACFTBIAS )
            case 0:
                 echo "s/>>>AIRCFT_BIAS<<<//g"   >> sed_file
                 echo 'Not using aircraft bias correction in GSI'
                 breaksw
            case 1:
                 echo "s/>>>AIRCFT_BIAS<<</aircraft_t_bc_ext=.true.,/g"  >> sed_file
                 echo 'Setting aircraft_t_bc_ext to true, using external bias correction'
                 breaksw
            case 2:
                 set cftstring = "aircraft_t_bc=.true.,"
                 if ( $CLEANUP_TAIL ) set cftstring = "$cftstring cleanup_tail=.true.,"
                 echo "s/>>>AIRCFT_BIAS<<</$cftstring/g"  >> sed_file
                 echo 'Setting aircraft_t_bc to true, using VV.VV^2 bias correction'
                 breaksw
            case 3:
                 set cftstring = "aircraft_t_bc_pof=.true.,"
                 if ( $CLEANUP_TAIL ) set cftstring = "$cftstring cleanup_tail=.true.,"
                 echo "s/>>>AIRCFT_BIAS<<</$cftstring/g"  >> sed_file
	         echo 'Setting aircraft_t_bc_pof to true, using POF bias correction'
                 breaksw
            default:
                 echo "s/>>>AIRCFT_BIAS<<<//g"   >> sed_file
                 echo 'Using default setting, not using aircraft bias correction in GSI'
                 breaksw
            endsw
#           control satwnd source based on date ...
            if ( $nymd0 < 20100701 ) then
                 echo "s/>>>USE_PREPB_SATWND<<</.true./g"  >> sed_file
            else
                 echo "s/>>>USE_PREPB_SATWND<<</.false./g" >> sed_file
            endif
            if ( $DO4DVAR ) then
                 echo "s/>>>JITERSTART<<</$jiter/g"  >> sed_file     # initial jiter for this iteration
            else
                 echo "s/>>>JITERSTART<<</1/g"       >> sed_file     # in IAU mode this is always one
            endif
            echo "s/>>>JITEREND<<</$jiterend/g"      >> sed_file     # final jiter for this iteration (only makes sens in adm mode)
            /bin/rm -f ./gsi.rc
            sed -f sed_file  ./$myrc  > ./gsi.rc
            cat gsi.rc

#           Run analysis
#           ------------
            analyzer $YMD $HMS $STF $SKA $SSB $infn \
                       -oss $anafile -t $specres -levs $gsinlev -x $gsinlon -y $gsinlat -jiter $jiter -lnobs -log $alog $asyn
                if ( $status ) then
                     echo $myname": abnormal exit from obs sensitivity part ..."
                     exit 2
                endif

            @ jiter++
            @ jiterend--
       end

#      Generate ods files with data sensitivities/impacts and re-timetag files
#      -----------------------------------------------------------------------
       if ($DIAG2ODS) then
          if ( $GETOBSENS_ONLY ) then
              set osens = "-sens"
          else
              set osens = ""
          endif

          zeit_ci.x diag2ods
          diag2ods -rc $FVWORK/odsmatch.rc -log $olog -m $miter -res ${gnorm} \
                   $osens -o $EXPID.imp${order}_${gnorm} $YMD $HMS $EXPID
          zeit_co.x diag2ods

#         Summarize impacts
#         -----------------
          obimp_summary.pl -ktsummary -type imp${order}_${gnorm} $YMD $HMS
       endif

#      Rename output to include proper time tag
#      ----------------------------------------
       foreach fname ( `ls $EXPID.imp${order}_${gnorm}_*.ods` )
         set prefix = `echo $fname | cut -d. -f1-3`
         set suffix = `echo $fname | cut -d. -f4-`
         /bin/mv $fname $prefix.$xttag-$suffix 
       end
       foreach fname ( `ls $EXPID.diag_*.bin` ) 
         set prefix = `echo $fname | cut -d. -f1-2`
         set suffix = `echo $fname | cut -d. -f3-`
         /bin/mv $fname $prefix.$xttag-$suffix 
       end
       /bin/mv $alog $EXPID.$alog.$xttag-${YMD}_${h2}z.txt
       /bin/mv $olog $EXPID.$olog.$xttag-${YMD}_${h2}z.txt

       end # gnorm

        ################################
  else  # REGULAR STAND-ALONE ANALYSIS #
        ################################

      set alog = anasa.log
      set olog = odssa.log
      if ( -e gsisa.rc.tmpl ) then
        set myrc = gsisa.rc.tmpl
      else
        set myrc = gsi.rc.tmpl
      endif
      /bin/rm -f sed_file
#     control aircraft bias correction
      switch( $ACFTBIAS )
      case 0:
           echo "s/>>>AIRCFT_BIAS<<<//g"   >> sed_file
           echo 'Not using aircraft bias correction in GSI'
           breaksw
      case 1:
           echo "s/>>>AIRCFT_BIAS<<</aircraft_t_bc_ext=.true.,/g"  >> sed_file
           echo 'Setting aircraft_t_bc_ext to true, using external bias correction'
           breaksw
      case 2:
           set cftstring = "aircraft_t_bc=.true.,"
           if ( $CLEANUP_TAIL ) set cftstring = "$cftstring cleanup_tail=.true.,"
           echo "s/>>>AIRCFT_BIAS<<</$cftstring/g"  >> sed_file
           echo 'Setting aircraft_t_bc to true, using VV.VV^2 bias correction'
           breaksw
      case 3:
           set cftstring = "aircraft_t_bc_pof=.true.,"
           if ( $CLEANUP_TAIL ) set cftstring = "$cftstring cleanup_tail=.true.,"
           echo "s/>>>AIRCFT_BIAS<<</$cftstring/g"  >> sed_file
	         echo 'Setting aircraft_t_bc_pof to true, using POF bias correction'
           breaksw
      default:
           echo "s/>>>AIRCFT_BIAS<<<//g"   >> sed_file
           echo 'Using default setting, not using aircraft bias correction in GSI'
           breaksw
      endsw
#     control satwnd source based on date ...
      if ( $nymd0 < 20100701 ) then
           echo "s/>>>USE_PREPB_SATWND<<</.true./g"  >> sed_file
      else
           echo "s/>>>USE_PREPB_SATWND<<</.false./g" >> sed_file
      endif
      /bin/rm -f ./gsi.rc
      sed -f sed_file  ./$myrc  > ./gsi.rc
      cat gsi.rc

      set jiter = 1

#     Run analysis
#     ------------
      analyzer $YMD $HMS $STF $SKA $SSB $infn \
                 -oss $anafile -t $specres -levs $gsinlev -x $gsinlon -y $gsinlat -jiter $jiter -lnobs -log $alog $asyn
          if ( $status ) then
             echo $myname": abnormal exit from analysis ..."
             exit 2
          else

#            Create analysis files when applicable
#            -------------------------------------
             if ( $DO4DIAU ) then
                set IAUX = `which mkiau.x`
                set nxiau   = `echorc.x -rc mkiau.rc.tmpl NX`
                set nyiau   = `echorc.x -rc mkiau.rc.tmpl NY`
                @ NCPUS_IAU = $nxiau * $nyiau
                setenv MPIRUN_IAU    "esma_mpirun -np $NCPUS_IAU $IAUX"
    
                zeit_ci.x ana4dupd
                ana4dupd.pl -ncpus 7 -iau -iter 0 -rcdir . \
                            $EXPID $beg_ana[1] $beg_ana[2] \
                                   $end_ana[1] $end_ana[2] $ASYNBKG
                tar cvf $EXPID.agcmrstsa.$beg_ana[1]_${beg_hh}z.tar *agcm_import*
                zeit_co.x ana4dupd
             endif
             foreach ftype ( ana xinc )
                foreach fn ( `ls *.$ftype.eta.*$NCSUFFIX` )
                  set sfx = `echo $fn | cut -d. -f3-`
                  /bin/mv $fn  $expid.${ftype}sa.$sfx
                end
             end
          endif

#      Rename bias files
#      -----------------
       if(-e $expid.ana.satbias.${YMD}_${h2}z.txt) /bin/mv $expid.ana.satbias.${YMD}_${h2}z.txt $expid.anasa.satbias.${YMD}_${h2}z.txt
       if(-e $expid.ana.satbang.${YMD}_${h2}z.txt) /bin/mv $expid.ana.satbang.${YMD}_${h2}z.txt $expid.anasa.satbang.${YMD}_${h2}z.txt
       if(-e $expid.ana.satbiaspc.${YMD}_${h2}z.txt) /bin/mv $expid.ana.satbiaspc.${YMD}_${h2}z.txt $expid.anasa.satbiaspc.${YMD}_${h2}z.txt
       if(-e $expid.ana.acftbias.${YMD}_${h2}z.txt) /bin/mv $expid.ana.acftbias.${YMD}_${h2}z.txt $expid.anasa.acftbias.${YMD}_${h2}z.txt

#      Generate ods files
#      ------------------
       if ($DIAG2ODS) then
          set omf = ""
          if ( $DO4DVAR ) then
               set miter = $NVAROUTER  # total number of iterations
          else
               set miter = `grep miter gsi.rc | cut -d= -f2 | cut -d, -f1 `
          endif
          if ( $miter == 1 ) set omf = "-omf"
          diag2ods -rc $FVWORK/odsmatch.rc -ncpus $d2o_nproc -o $EXPID.diagsa $omf $YMD $HMS $EXPID
       endif
       if ( $GSI_NETCDF_DIAG ) then
          foreach fname ( `ls $EXPID.diag_*.nc4` ) 
            set instrm = `echo $fname | cut -d. -f2 | cut -c5-`
            set suffix = `echo $fname | cut -d. -f3-`
            /bin/mv $fname $EXPID.diagsa${instrm}.$suffix 
          end
       endif

  endif

  if( -e $EXPID.ana_stats.log.${YMD}_${h2}z.txt ) /bin/mv -f $EXPID.ana_stats.log.${YMD}_${h2}z.txt $EXPID.anasa_stats.log.${YMD}_${h2}z.txt

  if( -e $alog ) /bin/mv -f $alog   $EXPID.$alog.${YMD}_${h2}z.txt
  if( -e $olog ) /bin/mv -f $olog   $EXPID.$olog.${YMD}_${h2}z.txt
  if( -e .zeit ) /bin/cp   .zeit    $EXPID.zeitsa.reg.${YMD}_${h2}z.txt
#                /bin/mv biasinp.grads $expid.biasinp.${YMD}_${h2}z.grads
#                /bin/mv biasinp.ctrl $expid.biasinp.${YMD}_${h2}z.ctrl
#                /bin/cp $expid.biasinp.${YMD}_${h2}z.grads $FVHOME/recycle/ &
#                /bin/cp $expid.biasinp.${YMD}_${h2}z.ctrl $FVHOME/recycle/ &

exit(0)

#...........................................................................

usage:

cat <<EOF

NAME
     fvssi - runs FVSSI system

SYNOPSIS

     fvssi [-h] [-log] [-cprs]  fvhome  fvwork

DESCRIPTION

     This script runs the FVSSI system.
     On input:

     fvhome     experiment home directory, e.g., /scratch1/$user/v000_b55
     fvwork     working directory, e.g., \$TMPDIR

     Restarts, namelists and resource files are  expected to be available
     in \$FVHOME/recycle and \$FVHOME/run.  The run is performed in \$FVWORK,
     and the output files are left there for archival by the calling script.

OPTIONS

     -h           prints this page
     -strict      returns with non-zero error if acquire fails to
                  resolve all input files
     -obsclass cls1,cls2...
                  observation data classes, such as conv_tovs,ssmi_wentz_tpw
     -log         log warning and errors to file

ENVIRONMENT VARIABLES

  SPECRES     resolution of spectral backgrounds (254 for T254, 62 for T62, etc)
  VAROFFSET   time offset (abs value) between analysis time and initial time of ana window
  TIMEINC     analysis time window (6-hr for 3dvar; 6,12,18,24,etc for 4dvar)


ENVIRONMENT VARIABLES (optional)

  ACFTBIAS    sets aircraft bias correction
  CLEANUP_TAIL  triggers cleanup of aircraft coefficient file
  ANASENS     trigger for analysis sensitivity (obs impact)
  INCSENS     allows running adjoint GSI with analysis increment for input
  DO4DVAR     trigger for 4DVAR-related features
  DIAG2ODS    produces ODS files from diag files (default: 1 (yes))
  GETOBSENS_ONLY places ob-sensitivity in ODS files instead of ob-impact (default: 0)
  LOCAL_ACQUIRE  expects files to be acquired to be on disc visible by PBS/SLURM (default: 0)
  NCSUFFIX    suffix for SDF output (default: nc4)
  NVAROUTER   number of 4DVAR outer loops (only applies to 4DVAR)
  STRICT      when set, will crash if obs files missing

SEE ALSO

    fvssisetup    Experiment setup utility
    fvssi/gsi.j   Main experiment script created by fvssisetup.

EOF

exit 1

