#!/bin/csh
# JOB best run interactively
#
# Copy script to your working area
set echo

###   Default directory where this script will write the
###   NESTDOWN code to, and work under is: /ptmp/$USER/NESTDOWN
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]
cd $TMPDIR

#               ********************************************
#               *******    interactivet jobs ONLY     ******
#               *******        NCAR IBM's only        ******
#               ********************************************
#       This should be the user's case or experiment (used in MSS name).
#       This is where the data will be stored on the MSS.

   set ExpName    = MM5V3/V36_3/NESTDOWN    # NESTDOWN output MSS pathname
   set InName     = MM5V3/V36_3/MM5         # NESTDOWN input  MSS pathname
   set InTERName  = MM5V3/V36_3/TERRAIN     # NESTDOWN input  MSS pathname
   set InLOWName  = MM5V3/V36_3/MM5         # NESTDOWN input  MSS pathname
   set RetPd      = 30                      # MSS retention period in days

#       Are we getting the source code, or is it available already?
#       Set to "no" if you wish to download the code.
#       IF set to "yes" script will expect to find the source code
#       in $TMPDIR/NESTDOWN
 set UseMySource = no
#set UseMySource = yes


#
# Set the starting date of the time period you want to process:
#
 set START_YEAR  = 1993   # Year (Four digits)
 set START_MONTH = 03     # Month ( 01 - 12 )
 set START_DAY   = 13     # Day ( 01 - 31 )
 set START_HOUR  = 00     # Hour ( 00 - 23 )

 set END_YEAR  = 1993   # Year (Four digits)
 set END_MONTH = 03     # Month ( 01 - 12 )
 set END_DAY   = 14     # Day ( 01 - 31 )
 set END_HOUR  = 00     # Hour ( 00 - 23 )

 set INTERVAL =  10800 # Time interval (seconds) to process.

set less_than_24h  = .true.         # if input is less than 24h

#
# Set input file names  (All these files should be on mass storage and will be read from $ExpName
#
set input_file         = ( MMOUT_DOMAIN1 )        # Input file name
set input_lowbdy_file  = ( LOWBDY_DOMAIN1 )       # Input lower boundary file name (optional) - if no lowbdy file is supplied, remember to set use_mm5_lowbdy = .FALSE.
set input_terrain_file = ( TERRAIN_DOMAIN2 )      # Input terrain file name 

#
# Namelist parameters: record2
 set sigma_f_bu     = 1.00,0.99,0.98,0.96,0.93,0.89,0.86,0.83,0.80,0.77,0.74,0.71,0.68,0.65,0.62,0.59,0.56,0.53,0.50,0.47,0.44,0.41,0.38,0.35,0.32,0.29,0.26,0.23,0.20,0.17,0.14,0.11,0.08,0.05,0.02,0.00
                              # full sigma, bottom-up, start with 1.0, end with 0.0
			      # DO NOT ADD SPACES - it will prevent the namelist being created correctly

 set sst_to_ice_threshold =  -9999

#
# Namelist parameters: record4
 set wrth2o         = .TRUE.  # T/F specific humidity wrt H2O

#
# Namelist parameters: record5
 set ifdatim        =  1 

#
# Namelist parameters: record6
 set interp_method        = 1
 set print_info           = .TRUE.
 set use_mm5_lowbdy       = .TRUE.


#	########################################################
#	########                                       #########
#	########       END OF USER  MODIFICATIONS      #########
#	########                                       #########
#	########################################################
#
# --------------------------------------------------------------
#       Get source code. - Do not change
# --------------------------------------------------------------

   if ( $UseMySource == no ) then
      echo " NESTDOWN Source code is downloaded "
      cp /mmm/users/mesouser/MM5V3/NESTDOWN.TAR.gz NESTDOWN.TAR.gz
      if ( ! -e NESTDOWN.TAR.gz ) then
         msread NESTDOWN.TAR.gz /MESOUSER/MM5V3/NESTDOWN.TAR.gz
      endif
      gunzip NESTDOWN.TAR.gz
      if ( ! -e NESTDOWN.TAR ) then
         echo "The NESTDOWN.TAR file does not exist.  Stopping."
         exit ( 1 )
      endif
      tar -xf NESTDOWN.TAR
      rm NESTDOWN.TAR
   endif

   cd NESTDOWN

#____________________________________________________________________________
# Make code
  make
  chmod 777 nestdown


# 
#  Get data
# 
echo " msread nestdown_input_file $InName/$input_file "
msread nestdown_input_file $InName/$input_file
echo " msread input_terrain_file $InTERName/$input_terrain_file "
msread input_terrain_file $InTERName/$input_terrain_file
if ( $input_lowbdy_file != ) then
  echo " msread input_lowbdy_file  $InLOWName/$input_lowbdy_file "
  msread input_lowbdy_file  $InLOWName/$input_lowbdy_file
endif

#_____________________________________________________________________________
cat << End_Of_Namelist | sed -e 's/#.*//; s/  *$//' > ./namelist.input
&record0
 input_file           = 'nestdown_input_file' 
 input_lowbdy_file    = 'input_lowbdy_file'
 input_terrain_file   = 'input_terrain_file'
/

&record1
 start_year                  = $START_YEAR
 start_month                 = $START_MONTH
 start_day                   = $START_DAY
 start_hour                  = $START_HOUR
 end_year                    = $END_YEAR
 end_month                   = $END_MONTH
 end_day                     = $END_DAY
 end_hour                    = $END_HOUR
 interval                    = $INTERVAL 
 less_than_24h               = $less_than_24h
/

&record2
 sigma_f_bu                     = $sigma_f_bu
 sst_to_ice_threshold           = $sst_to_ice_threshold 
/

&record4
 wrth2o         = $wrth2o
/

&record5
 ifdatim        = $ifdatim
/

&record6
 interp_method        = $interp_method
 print_info           = $print_info
 use_mm5_lowbdy       = $use_mm5_lowbdy
/

End_Of_Namelist

#_____________________________________________________________________________
# Run Nestdown
#
echo "RUNNING nestdown"

nestdown >& nestdown.log

#_____________________________________________________________________________
# Clean up and write files to mass storage

echo "Move INTERPF files to mass storage"
echo " "
foreach file ( MMINPUT_DOMAIN* BDYOUT_DOMAIN* LOWBDY_DOMAIN* )
  echo " mswrite -t $RetPd $file $ExpName/$file "
  mswrite -t $RetPd $file $ExpName/$file
end
echo " mswrite -t $RetPd $ExpName/nestdown.log "
mswrite -t $RetPd nestdown.log $ExpName/nestdown.log

#_____________________________________________________________________________

echo "DONE"

exit