#!/bin/csh
set echo

if ( ! -e /ptmp/$USER ) then
   mkdir /ptmp/$USER
endif
set TMPDIR=/ptmp/$USER      [Change this is you want to work a directory different from the default]
cat >! ./Run_MM5_Code   << End_Of_Queue_Information
#!/bin/csh
# @ job_name         = mm5_run_job
# @ step_name        = mm5_step     [DO NOT CHANGE]
# @ job_type         = parallel
# @ environment      = COPY_ALL;MP_EUILIB=us
# @ output           = mm5.out
# @ error            = mm5.err
# @ node             = 1          [blackforest has 4 processors per node and bluesky either 8 or 32]
# @ network.MPI      = css0,shared,us
# @ tasks_per_node   = 4          [make sure total number of processors correspond to settings of PROCMIN]
# @ node_usage       = not_shared
# @ checkpoint       = no
# @ wall_clock_limit = 3600
# @ class            = com_reg    [this is for blackofrest - bluesky have different classes]
# @ executable       = mm5_poe
# @ queue

## for serial cleanup job
# @ dependency       = (mm5_step == 0)
# @ environment      = COPY_ALL;
# @ job_name         = clean_up_run
# @ step_name        = clean_step
# @ output           = myjob.out
# @ error            = myjob.err
# @ checkpoint       = no
# @ wall_clock_limit = 1500
# @ class            = interactive
# @ executable       = mm5_clean
# @ queue

End_Of_Queue_Information


############################################################################
#####         IBM batch job for MM5 MPP model run ONLY                 #####
#####                                                                  #####
#####  TO RUN, TYPE ./mm5.deck                                         #####
#####  THE SCRIPT WILL GET FILES AND COMPILE INETRACTIVELY AND THEN    #####
#####  SUBMIT THE RUNNING OF THE JOB TO THE LOADLEVELER                #####
#####                                                                  #####
#####  This deck has been set up to run on NCAR's IBM machines.        #####
#####  If you are not working with NCAR's IBM machines, this deck will #####
#####  not be much use to you.                                         #####
#####                                                                  #####
#####  This deck has been set up to run the MM5 MPP code, on 4         #####
#####  processors. This code expects to find the input files on mass   #####
#####  storage.                                                        #####
#####                                                                  #####
#####  IF YOU WANT TO RUN ON MORE PROCESSORS, PLEASE SET PROCMIN_NS    #####
#####  AND PROCMIN_EW TO CORRESPOND TO THE NUMBER OF PROCESSORS YOU    #####
#####  ARE RUNNING ON AND RECOMPILE                                    #####
#####                                                                  #####
#####   Questions and comments should be directed to:                  #####
#####                        mesouser@ucar.edu                         #####
#####                                                                  #####
############################################################################

#
#       this should be the user's case or experiment (used in MS name)
#
  set ExpName     = MM5V3/V36_3/MM5       # MSS path name for output
  set InName      = MM5V3/V36_3/INTERPF   # MSS path name for input
  set InTERName   = MM5V3/V36_3/TERRAIN   # MSS path name for TERRAIN input
  set InFDDAName  = MM5V3/V36_3/LITTLE_R  # MSS path name for FDDA input
  set RetPd       = 30                    # MSS retention period in days

#
#       type of mm5 job
#
  set compile   = yes         # if yes, compile the mm5 code
# set compile   = no          # 

  set execute   = yes         # if yes, execute model run
# set execute   = no          # if no, complie only
#
  set STARTsw   = NoReStart   # start model run at hour 0
# set STARTsw   = ReStart     # restart model run
#
  if ( $STARTsw == ReStart ) then
     set compile = no
  endif

  set UseMySource = no        # Use "standard" version of MM5
# set UseMySource = yes       # Use my version of MM5
#
  set SourceCode = /home/username
                              # Path to my mm5.tar and mpp.tar files
#
  set FDDAsw    = NoFDDA      # no FDDA input files
# set FDDAsw    = Anly        # gridded FDDA input files
# set FDDAsw    = Obs         # obs FDDA input files
# set FDDAsw    = Both        # gridded and obs FDDA input files
#
#------------------------------------------------------------------------------
#
#    Input files:
#        1. lateral and lower boundary files
#
  set InBdy  =  ( BDYOUT_DOMAIN1 )
  set InLow  =  ( LOWBDY_DOMAIN1 )
#
#        2. initial conditions and/or nest terrain file
#
  set InMM    = ( MMINPUT_DOMAIN1 )
  set InTER   = ( TERRAIN_DOMAIN2 )
#
#        3. 4dda surface analyses - if this is a 4DDA run
#
if (( $FDDAsw == Anly ) || ( $FDDAsw == Both )) then
  set In4DSfc  = ( SFCFDDA_DOMAIN1 \
                   SFCFDDA_DOMAIN2  )
endif
#
#        4. 4dda observations - if this is a 4DDA run
#
if (( $FDDAsw == Obs  ) || ( $FDDAsw == Both )) then
  set In4DObs  = ( MM5OBS_DOMAIN1 \
                   MM5OBS_DOMAIN2 )
endif
#
#-----------------------------------------------------------------------------
#                 Get source code
#
 set TOPDIR = `pwd`
 cd $TMPDIR
if ( $compile == yes ) then
  if ( $UseMySource == yes ) then
    echo "Making use of own source code"
    cp $SourceCode/mm5.tar .
    tar -xvf mm5.tar
    rm mm5.tar
    cd MM5
    cp $SourceCode/mpp.tar .
    tar -xvf mpp.tar
    rm mpp.tar
  else
      echo " MM5 Source code is downloaded "
      cp /mmm/users/mesouser/MM5V3/MM5.TAR.gz MM5.TAR.gz
      if ( ! -e MM5.TAR.gz ) then
         msread MM5.TAR.gz /MESOUSER/MM5V3/MM5.TAR.gz
      endif
      gunzip MM5.TAR.gz
      if ( ! -e MM5.TAR ) then
         echo "The MM5.TAR file does not exist.  Stopping."
         exit ( 1 )
      endif
      tar -xf MM5.TAR
      rm MM5.TAR
      cd MM5
      cp /mmm/users/mesouser/MM5V3/MPP.TAR.gz MPP.TAR.gz
      if ( ! -e MPP.TAR.gz ) then
         msread MPP.TAR.gz /MESOUSER/MM5V3/MPP.TAR.gz
      endif
      gunzip MPP.TAR.gz
      if ( ! -e MPP.TAR ) then
         echo "The MPP.TAR file does not exist.  Stopping."
         exit ( 1 )
      endif
      tar -xf MPP.TAR
      rm MPP.TAR
  endif
else
  echo "Compile is no"
  cd MM5
endif
  
#
#========================================================================
# configure.user file - User editting required for sections 5 and 6
#  **Please read Release-note for how to make changes in configure.user
#------------------------------------------------------------------------
#
cat >! ./configure.user << "EOF"
# Sections
# 1. System Variables
# 4. General commands
# 5. Options for making "./include/parame.incl"
# 6. Physics Options (memory related)
# 7. MPP Options 
#
#-----------------------------------------------------------------------------
# 1. System Variables 
#-----------------------------------------------------------------------------
SHELL = 	/bin/sh
.SUFFIXES: .F .i .o .f .c
#-----------------------------------------------------------------------------
# 4. General commands
#-----------------------------------------------------------------------------
AR = ar ru
RM = rm -f
RM_CMD = $(RM) *.CKP *.ln *.BAK *.bak *.o *.i core errs ,* *~ *.a \
.emacs_* tags TAGS make.log MakeOut *.f !
GREP = grep -s
CC = cc
#-----------------------------------------------------------------------------
# 5. Options for making ./include/parame.incl
#-----------------------------------------------------------------------------
#
# FDDAGD (integer)                  - "1" -> FDDA gridded run
FDDAGD = 0
#
# FDDAOBS (integer)                 - "1" -> FDDA obs run
FDDAOBS = 0
#
# MAXNES (integer)                  - Max Number of Domains in simulation
MAXNES = 2
#
# MIX,MJX (integer)                 - Maximum Dimensions of any Domain
MIX = 49
MJX = 52
# MKX (integer)                     - Number of half sigma levels in model
MKX = 23
#-----------------------------------------------------------------------------
# 6. Physics Options
#    The first MAXNES values in the list will be used for the corresponding
#        model nests; the rest in the list can be used to compile other options.
#        The exception is FRAD, of which only the first value is used in the model,
#        (i.e., only one radiation option is used for all nests). The rest allow 
#        other options to be compiled.
#    Compilation of Arakawa-Schubert cumulus scheme requires imsl.
#-----------------------------------------------------------------------------
# IMPHYS - for explicit moisture schemes (array,integer)
IMPHYS = "4,4,1,1,1,1,1,1,1,1"      
#                                   - Dry,stable,warm rain,simple ice,mix phase,
#                                   - 1  ,2     ,3        ,4         ,5
#                                   - graupel(gsfc),graupel(reisner2),schultz
#                                   -,6            ,7                ,8
MPHYSTBL = 0
#                                   - 0=do not use look-up tables for moist 
#                                     physics
#                                   - 1=use look-up tables for moist physics 
#                                     (currently only simple ice and mix phase 
#                                      are available)
#
# ICUPA - for cumulus schemes (array,integer)
#                                   - None,Kuo,Grell,AS,FC,KF,BM,KF2 - 1,2,3,4,5,6,7,8
ICUPA  = "3,3,1,1,1,1,1,1,1,1"
#
# IBLTYP - for planetary boundary layer (array,integer)
#                                   - 0=no PBL fluxes,1=bulk,2=Blackadar,
#                                     3=Burk-Thompson,4=Eta M-Y,5=MRF,
#                                     6=Gayno-Seaman,7=Pleim-Xiu
IBLTYP  = "5,5,0,0,0,0,0,0,0,0"
#
# FRAD - for atmospheric radiation (integer)
#                                   - Radiation cooling of atmosphere 
#                                     0=none,1=simple,2=cloud,3=ccm2,4=rrtm
FRAD = "2,0,0,0,0"
#
# IPOLAR - (integer) for polar model used only if ISOIL=1
#                                     0=not polar (5-layer soil model)
#                                     1=polar (7-layer snow/soil model)
IPOLAR = 0
#
# ISOIL - for multi-layer soil temperature model (integer)
#                                   - 0=no,1=yes (only works with IBLTYP=2,4,5,6)
#                                     2=OSU land-surface scheme (IBLTYP=5 only)
#                                     3=Pleim-Xiu LSM (IBLTYP=7 only)
ISOIL = 1
#
# ISHALLO (array,integer)           - Shallow Convection Option
#                                     1=shallow convection,0=No shallow convection
ISHALLO  = "0,0,0,0,0,0,0,0,0,0"
#-----------------------------------------------------------------------------
# 7. MPP options
#
# For general information and updated "helpdesk" information see
#    http://www2.mmm.ucar.edu/mm5/mpp
#    http://www2.mmm.ucar.edu/mm5/mpp/helpdesk
#
#-----------------------------------------------------------------------------
#
#                                 Presently, of the MPP platforms only the "sp2"
#                                 is supplied with the "make deck" capability.
#
# MPP Software Layer
MPP_LAYER=RSL
#MPP_LAYER=NNTSMS
# 
# PROCMIN_NS - minimum number of processors allowed in N/S dim
#
PROCMIN_NS = 2
#
# PROCMIN_EW - minimum number of processors allowed in E/W dim
#
PROCMIN_EW = 2
#
# ASSUME_HOMOGENOUS_ENVIRONMENT - on a machine with a heterogeneous
# mix of processors (different speeds) setting this compile time
# constant to 0 (zero) allows the program to detect the speed of each
# processor at the beginning of a run and then to attempt to come up with
# an optimal (static) mapping.  Set this to 0  for a heterogeneous
# mix of processors, set it to 1 for a homogeneous mix.  Unless you
# are certain you have a heterogeneous mix of processors, leave this
# set to 1.  Currently, this option is ignored on platforms other
# than the IBM SP.
#
ASSUME_HOMOGENEOUS_ENVIRONMENT = 1
#
#-----------------------------------------------------------------------------
#    7a. IBM SP2
#         type 'make mpp' for the SP2
#-----------------------------------------------------------------------------
RUNTIME_SYSTEM = "sp2"
MPP_TARGET=$(RUNTIME_SYSTEM)
MFC = xlf
MCC = mpcc
MLD = mpxlf
FCFLAGS = -O3 -qstrict -qarch=auto -qhsflt
LDOPTIONS = -bmaxstack:512000000 -bmaxdata:2000000000
##LOCAL_LIBRARIES = -lmass
#LOCAL_LIBRARIES = -lessl
LOCAL_LIBRARIES =
MAKE = make -i -r
AWK = awk
SED = sed
CAT = cat
CUT = cut
EXPAND = expand
M4 = m4
CPP = /lib/cpp -C -P
CPPFLAGS = -DMPI -Drs6000
CFLAGS = -DNOUNDERSCORE -DMPI
ARCH_OBJS =  milliclock.o
IWORDSIZE = 4
RWORDSIZE = 4
LWORDSIZE = 4
#-----------------------------------------------------------------------------
# Don't touch anything below this line
#-----------------------------------------------------------------------------
.F.i:
	$(RM) $@
	$(CPP) $(CPPFLAGS) $*.F > $@
	mv $*.i $(DEVTOP)/pick/$*.f
	cp $*.F $(DEVTOP)/pick
.c.o:
	$(RM) $@ && \
	$(CC) -c $(CFLAGS) $*.c

.F.o:
	$(RM) $@
	$(FC) -c $(FCFLAGS) $*.F

.F.f:
	$(RM) $@
	$(CPP) $(CPPFLAGS) $*.F > $@

.f.o:
	$(RM) $@
	$(FC) -c $(FCFLAGS) $*.f
#-----------------------------------------------------------------------
"EOF"
#
#=======================================================================
# Options for namelist ("mmlif") - User editting required
#   (Comments in the namelist are removed during execution)
#-----------------------------------------------------------------------
#
#	The first dimension (column) of the arrays denotes the domain
#	identifier.
#		Col 1 = Domain #1, Col 2 = Dom #2, etc.
#
cat > ./Run/oparam << EOF
 &OPARAM
 ;
 ;      ************* FORECAST TIME AND TIME STEP ******************
 ;
 TIMAX  = 720.,         ; forecast length in minutes
 TISTEP = 240.,         ; coarse domain DT in model, use 3*DX
 ;
 ;      ************** OUTPUT/RESTART OPTIONS ***************
 ;
 IFREST = .FALSE.,      ; whether this is a restart
    IXTIMR  = 720,      ; restart time in minutes
 IFSAVE = .TRUE.,       ; save data for restart
    SVLAST = .TRUE.,    ; T: only save the last file for restart
                        ; F: save multiple files
    SAVFRQ = 360.,      ; how frequently to save data (in minutes)
 IFTAPE = 1,            ; model output: 0,1
    TAPFRQ = 180.,      ; how frequently to output model results (in minutes)
    BUFFRQ = 0.,        ; how frequently to split model output files (in minutes),
 ;                        ignored if < TAPFRQ
    INCTAP = 1,1,1,1,1,1,1,1,1,1,  ; multipliers of TAPFRQ for outputting
 IFSKIP = .FALSE.,      ; whether to skip input files - DO NOT use this for restart
    CDATEST = '1993-03-13_00:00:00', ; the DATE for the starting file
 IFPRT = 0,             ; sample print out: =1, a lot of print
 PRTFRQ = 720.,         ; Print frequency for sample output (in minutes)
 MASCHK = 99999,        ; mass conservation check (KTAU or no. of time steps)
 IFTSOUT = .FALSE.,     ; output time series (default 30 points)
   TSLAT = 0.0,0.0,0.0,0.0,0.0, ; latitudes of time series points (S is negative)
   TSLON = 0.0,0.0,0.0,0.0,0.0, ; longitudes of time series points (W is negative)
 /
EOF
cat > ./Run/lparam << EOF
 &LPARAM
 ;
 ;       1. user-chosen options I
 ;
 RADFRQ    = 30.,  ;atmospheric radiation calculation frequency (in minutes)
 IMVDIF    = 1,    ;moist vertical diffusion in clouds - 0, 1 (IBLTYP=2,5 only)
 IVQADV    = 1,    ;vertical moisture advection uses log interpolation - 0, linear - 1
 IVTADV    = 1,    ;vertical temperature advection uses theta interpolation - 0, linear - 1
 ITHADV    = 1,    ;advection of temperature uses potential temperature - 1, standard - 0
 ITPDIF    = 1,    ;diffusion using perturbation temperature - 0,1
 ICOR3D    = 1,    ;3D Coriolis force - 0, 1
 IEXSI     = 0,    ;initial sea-ice - 0, 1(base on SST), 2(read in) (ISOIL=1 only)
 IFUPR     = 1,    ;upper radiative boundary condition - 0, 1
 ;
 ;       2. do not change IBOUDY
 ;
 IBOUDY = 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, ;boundary conditions
 ;        (fixed, time-dependent, relaxation -0,2,3)
 ;
 ;       3. keep the following 8 variables as they are
 ;          unless doing sensitivity runs
 ;
 IFDRY  = 0,                            ;fake-dry run (no latent heating) - 0, 1
 ;                                       for IMPHYS = 2,3,4,5,6,7 (requires ICUPA = 1)
 ISSTVAR= 0,                            ;varying SST in time - 1, otherwise, 0
 IMOIAV = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ;bucket soil moisture scheme. 0 - not used,
                                        ;1 - used w/o extra input, 2 - user w/ soil m input
 IZ0TOPT= 0,                            ;thermal roughness option in IBLTYP = 2,5. 
                                        ;0 - old option, 1 - Garratt, 2 - Zilitinkevich
 ISFMTHD= 1,                            ;method for calculation of 2m/10m diagnostics
                                        ;0 - old method, 1 - new method for stable conditions
 IFSNOW = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ;SNOW COVER EFFECTS - 0, 1, 2
 ;                                      ;0 - no effect, 1 - with effect, 2 - simple snow model
 ISFFLX = 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ;surface fluxes - 0, 1
 ITGFLG = 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ;surface temperature prediction - 1:yes, 3:no
 ISFPAR = 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ;surface characteristics - 0, 1
 ICLOUD = 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ;cloud effects on radiation - 0, 1
 ;                                       currently for IFRAD = 1,2
 IEVAP  = 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ;evap of cloud/rainwater - <0, 0, >0
 ;                                       (currently for IMPHYS=3,4,5 only)
 ISMRD  = 0,                            ;soil moisture initialization by PX LSM:
 ;                                       =0, use moisture avail from LANDUSE.TBL;
 ;                                       =2, use soil moisture from REGRID
 ; Next two switches for new version of NOAH LSM (ISOIL=2)
 RDMAXALB=.FALSE.                       ;use climo maximum snow albedo (not landuse table)
 RDBRDALB=.FALSE.                       ;use monthly climo background albedo (not landuse table)
 ;
EOF
cat > ./Run/nparam << EOF
 &NPARAM
 ;
 ;      ************** NEST AND MOVING NEST OPTIONS ***************
 ;
 LEVIDN =   0,1,2,1,1,1,1,1,1,1,               ; level of nest for each domain
 NUMNC  =   1,1,2,1,1,1,1,1,1,1,               ; ID of mother domain for each nest
 NESTIX =  35,  49,  31,  46,  46,  46,  46,  46,  46,  46,  ; domain size i
 NESTJX =  41,  52,  31,  61,  61,  61,  61,  61,  61,  61,  ; domain size j
 NESTI  =   1,  10,   8,   1,   1,   1,   1,   1,   1,   1,  ; start location i
 NESTJ  =   1,  17,   9,   1,   1,   1,   1,   1,   1,   1,  ; start location i
 XSTNES =   0.,  0.,900.,  0.,  0.,  0.,  0.,  0.,  0.,  0., ; domain initiation
 XENNES =1440.,1440.,1440.,720.,720.,720.,720.,720.,720.,720.; domain termination
 IOVERW =   1,   2,   0,   0,   0,   0,   0,   0,   0,   0,  ; overwrite nest input
 ;          0=interpolate from coarse mesh (for nest domains);
 ;          1=read in domain initial conditions
 ;          2=read in nest terrain file
 IACTIV =   1,   0,   0,   0,   0,   0,   0,   0,   0,   0,  ;
 ;          in case of restart: is this domain active?
 ;
 ;      ************* MOVING NEST OPTIONS ******************
 ;
 IMOVE  =   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, ; move domain 0,1
 IMOVCO =   1,   1,   1,   1,   1,   1,   1,   1,   1,   1, ; 1st move #
 ;          imovei(j,k)=L, ; I-INCREMENT MOVE (DOMAIN J, MOVE NUMBER K) IS L
 IMOVEI =   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, ; I move #1
            0,   0,   0,   0,   0,   0,   0,   0,   0,   0, ; I move #2
            0,   0,   0,   0,   0,   0,   0,   0,   0,   0, ; I move #3
            0,   0,   0,   0,   0,   0,   0,   0,   0,   0, ; I move #4
            0,   0,   0,   0,   0,   0,   0,   0,   0,   0, ; I move #5
 IMOVEJ =   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, ; J move #1
            0,   0,   0,   0,   0,   0,   0,   0,   0,   0, ; J move #2
            0,   0,   0,   0,   0,   0,   0,   0,   0,   0, ; J move #3
            0,   0,   0,   0,   0,   0,   0,   0,   0,   0, ; J move #4
            0,   0,   0,   0,   0,   0,   0,   0,   0,   0, ; J move #5
 IMOVET =   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, ; time of move #1
            0,   0,   0,   0,   0,   0,   0,   0,   0,   0, ; time of move #2
            0,   0,   0,   0,   0,   0,   0,   0,   0,   0, ; time of move #3
            0,   0,   0,   0,   0,   0,   0,   0,   0,   0, ; time of move #4
            0,   0,   0,   0,   0,   0,   0,   0,   0,   0, ; time of move #5
 IFEED  =  3,     ; no feedback; 9-pt weighted average; 1-pt feedback w/o smoothing / 
                  ; light smoothing / heavy smoothing - 0,1,2,3, and 4
 /
EOF
cat > ./Run/pparam << EOF
 &PPARAM
 ;
 ;      ************* MISCELLANEOUS OPTIONS *****************
 ;
 ; The values for the following 5 variables are only used if ISFPAR = 0
 ;     (i.e. only land/water surface catagories)
 ;
 ZZLND  = 0.1,          ; roughness length over land in meters
 ZZWTR  = 0.0001,       ; roughness length over water in meters
 ALBLND = 0.15,         ; albedo
 THINLD = 0.04,         ; surface thermal inertia
 XMAVA  = 0.3,          ; moisture availability over land as a decimal fraction of one
 ;
 CONF   = 1.0,          ; non-convective precipitation saturation threshold (=1: 100%)
 /
EOF
cat > ./Run/fparam << EOF
 &FPARAM
 ;
 ;      ************* 4DDA OPTIONS **********************
 ;
 ;     THE FIRST DIMENSION (COLUMN) IS THE DOMAIN IDENTIFIER:
 ;         COLUMN 1 = DOMAIN #1, COLUMN 2 = DOMAIN #2, ETC.
 ;
 ;       START TIME FOR FDDA (ANALYSIS OR OBS) FOR EACH DOMAIN
 ;       (IN MINUTES RELATIVE TO MODEL INITIAL TIME)
 FDASTA=0.,0.,0.,0.,0.,0.,0.,0.,0.,0.
 ;       ENDING TIME FOR FDDA (ANALYSIS OR OBS) FOR EACH DOMAIN
 ;       (IN MINUTES RELATIVE TO MODEL INITIAL TIME)
 FDAEND=780.,0.,0.,0.,0.,0.,0.,0.,0.,0.,
 ;
 ;      **************** ANALYSIS NUDGING ******************
 ;
 ;     THE FIRST DIMENSION (COLUMN) OF THE ARRAYS DENOTES THE
 ;     DOMAIN IDENTIFIER:
 ;         COLUMN 1 = DOMAIN #1, COLUMN 2 = DOMAIN #2, ETC.
 ;     THE SECOND DIMENSION (ROW OR LINE) EITHER REFERS TO THE 3D VS
 ;     SFC ANALYSIS OR WHICH VARIABLE IS ACCESSED:
 ;         LINE 1 = 3D, LINE 2 = SFC   OR
 ;         LINE 1 = U, LINE 2 = V, LINE 3 = T, LINE 4 = Q
 ;
 ;       IS THIS A GRID 4DDA RUN? 0 = NO; 1 = YES
 I4D= 0,0,0,0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,0,0,0,
 ;
 ;       SPECIFY THE TIME IN MINUTES BETWEEN THE INPUT (USUALLY
 ;       FROM INTERP) USED FOR GRID FDDA
 DIFTIM=720.,720.,0.,0.,0.,0.,0.,0.,0.,0.,      ; 3D ANALYSIS NUDGING
        180.,180.,0.,0.,0.,0.,0.,0.,0.,0.,      ; SFC ANALYSIS NUDGING
 ;
 ;       GRID NUDGE THE WIND FIELD? 0 = NO; 1 = YES
 IWIND=1,1,0,0,0,0,0,0,0,0,    ; 3D ANALYSIS NUDGING
       1,1,0,0,0,0,0,0,0,0,    ; SFC ANALYSIS NUDGING
 ;
 ;       NUDGING COEFFICIENT FOR WINDS ANALYSES
 GV=2.5E-4,1.0E-4,0.,0.,0.,0.,0.,0.,0.,0.,       ; 3D ANALYSIS NUDGING
    2.5E-4,1.0E-4,0.,0.,0.,0.,0.,0.,0.,0.,       ; SFC ANALYSIS NUDGING
 ;
 ;       GRID NUDGE THE TEMPERATURE FIELD? 0 = NO; 1 = YES
 ITEMP=1,1,0,0,0,0,0,0,0,0,    ; 3D ANALYSIS NUDGING
       1,1,0,0,0,0,0,0,0,0,    ; SFC ANALYSIS NUDGING
 ;
 ;       NUDGING COEFFICIENT FOR TEMPERATURE ANALYSES
 GT=2.5E-4,1.0E-4,0.,0.,0.,0.,0.,0.,0.,0.,       ; 3D ANALYSIS NUDGING
    2.5E-4,1.0E-4,0.,0.,0.,0.,0.,0.,0.,0.,       ; SFC ANALYSIS NUDGING
 ;
 IMOIS=1,1,0,0,0,0,0,0,0,0,    ; 3D ANALYSIS NUDGING
       1,1,0,0,0,0,0,0,0,0,    ; SFC ANALYSIS NUDGING
 ;
 ;       NUDGING COEFFICIENT FOR THE MIXING RATIO ANALYSES
 GQ=1.E-5,1.E-5,0.,0.,0.,0.,0.,0.,0.,0.,        ; 3D ANALYSIS NUDGING
    1.E-5,1.E-5,0.,0.,0.,0.,0.,0.,0.,0.,        ; SFC ANALYSIS NUDGING
 ;
 ;       GRID NUDGE THE ROTATIONAL WIND FIELD? 0 = NO; 1 = YES
 IROT=0,0,0,0,0,0,0,0,0,0,     ; 3D ANALYSIS NUDGING
;
 ;       NUDGING COEFFICIENT FOR THE ROTATIONAL COMPONENT OF THE WINDS
 GR=5.E6,5.E6,0.,0.,0.,0.,0.,0.,0.,0.,          ; 3D ANALYSIS NUDGING
 ;
 ;        IF GRID NUDGING (I4D(1,1)=1) AND YOU WISH TO EXCLUDE THE
 ;        BOUNDARY LAYER FROM FDDA OF COARSE GRID THREE DIMENSIONAL
 ;        DATA (USUALLY FROM INTERP),
 ;             0 = NO,  INCLUDE BOUNDARY LAYER NUDGING
 ;             1 = YES, EXCLUDE BOUNDARY LAYER NUDGING
 INONBL =0,0,0,0,0,0,0,0,0,0,         ; U WIND
         0,0,0,0,0,0,0,0,0,0,         ; V WIND
         1,1,1,1,1,1,1,1,1,1,         ; TEMPERATURE
         1,1,1,1,1,1,1,1,1,1,         ; MIXING RATIO
 ;
 ;        RADIUS OF INFLUENCE FOR SURFACE ANALYSIS (KM).
 ;        IF I4D(2,1)=1 OR I4D(2,2)=1, ETC, DEFINE RINBLW (KM) USED
 ;        IN SUBROUTINE BLW TO DETERMINE THE HORIZONTAL VARIABILITY
 ;        OF THE SURFACE-ANALYSIS NUDGING AS A FUNCTION OF SURFACE
 ;        DATA DENSITY.  OVER LAND, THE STRENGTH OF THE SURFACE-
 ;        ANALYSIS NUDGING IS LINEARLY DECREASED BY 80 PERCENT AT
 ;        THOSE GRID POINTS GREATER THAN RINBLW FROM AN OBSERVATION
 ;        TO ACCOUNT FOR DECREASED CONFIDENCE IN THE ANALYSIS
 ;        IN REGIONS NOT NEAR ANY OBSERVATIONS.
 RINBLW=250.,
 ;
 ;        SET THE NUDGING PRINT FREQUENCY FOR SELECTED DIAGNOSTIC
 ;        PRINTS IN THE GRID (ANALYSIS) NUDGING CODE (IN CGM
 ;        TIMESTEPS)
 NPFG=50,
 ;
 ;      **************** OBSERVATION NUDGING ***************
 ;
 ;
 ;     INDIVIDUAL OBSERVATION NUDGING.  VARIABLES THAT ARE ARRAYS
 ;     USE THE FIRST DIMENSION (COLUMN) AS THE DOMAIN IDENTIFIER:
 ;         COLUMN 1 = DOMAIN #1, COLUMN 2 = DOMAIN #2, ETC.
 ;
 ;       IS THIS INDIVIDUAL OBSERVATION NUDGING? 0 = NO; 1 = YES
 I4DI   =0,0,0,0,0,0,0,0,0,0,
 ;
 ;       OBS NUDGE THE WIND FIELD FROM STATION DATA? 0 = NO; 1 = YES
 ISWIND =1,0,0,0,0,0,0,0,0,0,
 ;
 ;       NUDGING COEFFICIENT FOR WINDS FROM STATION DATA
 GIV  =4.E-4,4.E-4,0.,0.,0.,0.,0.,0.,0.,0.,
 ;
 ;       OBS NUDGE THE TEMPERATURE FIELD FROM STATION DATA? 0 = NO; 1 = YES
 ISTEMP=1,0,0,0,0,0,0,0,0,0,
 ;
 ;       NUDGING COEFFICIENT FOR TEMPERATURES FROM STATION DATA
 GIT  =4.E-4,4.E-4,0.,0.,0.,0.,0.,0.,0.,0.,
 ;
 ;       OBS NUDGE THE MIXING RATIO FIELD FROM STATION DATA? 0 = NO; 1 = YES
 ISMOIS=1,0,0,0,0,0,0,0,0,0,
 ;
 ;       NUDGING COEFFICIENT FOR THE MIXING RATIO FROM STATION DATA
 GIQ  =4.E-4,4.E-4,0.,0.,0.,0.,0.,0.,0.,0.,
 ;
 ;       THE OBS NUDGING RADIUS OF INFLUENCE IN THE
 ;       HORIZONTAL IN KM FOR CRESSMAN-TYPE DISTANCE-WEIGHTED
 ;       FUNCTIONS WHICH SPREAD THE OBS-NUDGING CORRECTION
 ;       IN THE HORIZONTAL.
 RINXY=240.,
 ;
 ;       THE OBS NUDGING RADIUS OF INFLUENCE IN THE
 ;       VERTICAL IN SIGMA UNITS FOR CRESSMAN-TYPE DISTANCE-
 ;       WEIGHTED FUNCTIONS WHICH SPREAD THE OBS-NUDGING
 ;       CORRECTION IN THE VERTICAL.
 RINSIG=0.001,
 ;
 ;       THE HALF-PERIOD OF THE TIME WINDOW, IN MINUTES, OVER
 ;       WHICH AN OBSERVATION WILL AFFECT THE FORECAST VIA OBS
 ;       NUDGING. THAT IS, THE OBS WILL INFLUENCE THE FORECAST
 ;       FROM TIMEOBS-TWINDO TO TIMEOBS+TWINDO.  THE TEMPORAL
 ;       WEIGHTING FUNCTION IS DEFINED SUCH THAT THE OBSERVATION
 ;       IS APPLIED WITH FULL STRENGTH WITHIN TWINDO/2. MINUTES
 ;       BEFORE OR AFTER THE OBSERVATION TIME, AND THEN LINEARLY
 ;       DECREASES TO ZERO TWINDO MINUTES BEFORE OR AFTER THE
 ;       OBSERVATION TIME.
 TWINDO=40.0,
 ;
 ;       THE NUDGING PRINT FREQUENCY FOR SELECTED DIAGNOSTIC PRINT
 ;       IN THE OBS NUDGING CODE (IN CGM TIMESTEPS)
 NPFI=20,
 ;
 ;       FREQUENCY (IN CGM TIMESTEPS) TO COMPUTE OBS NUDGING WEIGHTS
 IONF=2,
 IDYNIN=0,  ;for dynamic initialization using a ramp-down function to gradually
 ;           turn off the FDDA before the pure forecast, set idynin=1 [y=1, n=0]
 DTRAMP=60.,;the time period in minutes over which the
 ;           nudging (obs nudging and analysis nudging) is ramped down
 ;           from one to zero.  Set dtramp negative if FDDA is to be ramped
 ;           down BEFORE the end-of-data time (DATEND), and positive if the
 ;           FDDA ramp-down period extends beyond the end-of-data time.
 /
EOF
#
#----------------------------------------------------------------------------
#                    Make code
if ( $compile == yes ) then
  echo "CODE are now being compiled"
  make mpp
  tar -cvf mm5exe.tar * 
  echo "mswrite executable files"
  mswrite -t $RetPd mm5exe.tar $ExpName/mm5exe.tar
else
  echo "Code will not be COMPILED"
endif
#----------------------------------------------------------------------------
#
#      create namelist: mmlif, and remove comments from namelist:
#
if ( $execute == no ) then
  echo "NO execution"
  exit
endif

make mmlif
cd ./Run
sed -f ../Util/no_comment.sed mmlif | grep "[A-Z,a-z,/]" > mmlif.tmp
mv mmlif.tmp mmlif
rm fparam lparam nparam oparam pparam
#----------------------------------------------------------------------------
#
#       get boundary conditions from MS
#
set NUMFIL = 1
   foreach fil ( $InBdy )
       echo "msread " $fil
       msread BDYOUT_DOMAIN$NUMFIL $InName/$fil
   @ NUMFIL ++
   end
set NUMFIL = 1
   foreach fil ( $InLow )
       echo "msread " $fil
       msread LOWBDY_DOMAIN$NUMFIL $InName/$fil
   @ NUMFIL ++
   end
#
#       1. initial conditions
#
set NUMFIL = 1
   foreach fil ( $InMM )
       echo "msread " $fil
       msread MMINPUT_DOMAIN$NUMFIL $InName/$fil
   @ NUMFIL ++
   end
set NUMFIL = 2
   foreach fil ( $InTER )
       echo "msread " $fil
       msread TERRAIN_DOMAIN$NUMFIL $InTERName/$fil
   @ NUMFIL ++
   end

set NUMFIL = 1
while ( $NUMFIL <= ${#InMM} )
   if (( $FDDAsw == Anly ) || ( $FDDAsw == Both ) && ( -e MMINPUT_DOMAIN$NUMFIL)) then
      cp MMINPUT_DOMAIN$NUMFIL MMINPUT2_DOMAIN$NUMFIL
   endif
   @ NUMFIL ++
end
#
#       2. get surface analyses files for nudging
#
if (( $FDDAsw == Anly ) || ( $FDDAsw == Both )) then
   foreach fil ( $In4DSfc )
       echo "msread " $fil
       msread $fil $InFDDAName/$fil
   end
endif
#
#       3. observations if OBS nudging
#
if (( $FDDAsw == Obs ) || ( $FDDAsw == Both )) then
   foreach fil ( $In4DObs )
       echo "msread " $fil
       msread $fil $InFDDAName/$fil
   end
endif
#
#      4.  Restart files
#
if ( $STARTsw == ReStart ) then
  echo "Get restart files from mass storage"
  msread restart.tar $ExpName/restart.tar
  tar -xvf restart.tar
  rm restart.tar
else
  if ( ! -e restrts ) then
     mkdir restrts
  endif
endif

#----------------------------------------------------------------------------
# CLEAN UP

#       Write to MSS all output files
#
set file = dummy
cat >! $TOPDIR/mm5_clean   << End_Of_Clean
#!/bin/csh
cd $TMPDIR/MM5/Run
ls -ls
foreach file ( MMOUT_DOMAIN* )
   echo "mswrite" \$file
   mswrite -t $RetPd \$file $ExpName/\$file
end
#
tar -cvf restart.tar ./restrts
echo "mswrite restart files"
mswrite -t $RetPd restart.tar $ExpName/restart.tar
#
   tar -cvf mm5out.tar rsl.* show* mmlif
   echo " mswrite -t $RetPd mm5out.tar $ExpName/mm5out.tar "
   mswrite -t $RetPd mm5out.tar $ExpName/mm5out.tar

End_Of_Clean


cat > $TOPDIR/mm5_poe   << End_Of_Run_Information
#!/bin/csh
set MP_SHARED_MEMORY = yes
cd $TMPDIR/MM5/Run
timex poe ./mm5.mpp
End_Of_Run_Information


cd $TOPDIR
llsubmit Run_MM5_Code


echo "DONE - code has been submitted"

exit