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

###   Default directory where this script will write the
###   INTERPF code to, and work under is: /ptmp/$USER/INTERPF
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/INTERPF    # INTERPF output MSS pathname
   set InName  = MM5V3/V36_3/LITTLE_R   # INTERPF 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/INTERPF
 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 =  43200 # Time interval (seconds) to process.

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

#
# Set input file name
#
set input_file = ( LITTLE_R_DOMAIN1 )      # File should be on mass storage and will be read from $InName

#
# Namelist parameters: record2
 set sigma_f_bu     = 1.00,0.99,0.98,0.96,0.93,0.89,0.85,0.80,0.75,0.70,0.65,0.60,0.55,0.50,0.45,0.40,0.35,0.30,0.25,0.20,0.15,0.10,0.05,0.00
                              # full sigma, bottom-up, start with 1.0, end with 0.0

 set ptop           = 10000   # top pressure if need to be redefined
 set isfc           = 0       # sigma levels to spread surface information

#
# Namelist parameters: record3
 set p0             = 1.e5    # base state sea-level pres (Pa)
 set tlp            = 50.     # base state lapse rate d(T)/d(ln P)
 set ts0            = 275.    # base state sea-level temp (K)
 set tiso           = 0.      # base state isothermal stratospheric temp (K)

#
# Namelist parameters: record4
 set removediv      = .TRUE.  # T/F remove integrated mean divergence
 set usesfc         = .TRUE.  # T/F use surface data
 set wrth2o         = .TRUE.  # T/F specific humidity wrt H2O

#
# Namelist parameters: record5
 set ifdatim        = -1      # number of IC time periods to output


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

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

   cd INTERPF

#____________________________________________________________________________
# Make code
  make
  chmod 777 interpf

# 
#  Get data
# 
echo " msread interpf_input_file $InName/$input_file "
msread interpf_input_file $InName/$input_file

#_____________________________________________________________________________
cat << End_Of_Namelist | sed -e 's/#.*//; s/  *$//' > ./namelist.input
&record0
 input_file     = 'interpf_input_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
 ptop           = $ptop 
 isfc           = $isfc
/

&record3
 p0             = $p0
 tlp            = $tlp  
 ts0            = $ts0
 tiso           = $tiso
/

&record4
 removediv      = $removediv
 usesfc         = $usesfc
 wrth2o         = $wrth2o
/

&record5
 ifdatim        = $ifdatim
/

End_Of_Namelist

#_____________________________________________________________________________
# Run interpf
#
echo "RUNNING interpf"

interpf >& interpf.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 " $file
###  mswrite -t $RetPd $file $ExpName/$file
end
echo " mswrite -t $RetPd $ExpName/interpf.log "
###mswrite -t $RetPd interpf.log $ExpName/interpf.log

#_____________________________________________________________________________

echo "DONE"

exit