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

###   Default directory where this script will write the
###   INTERPB code to, and work under is: /ptmp/$USER/INTERPB
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/INTERPB    # INTERPB output MSS pathname
   set InName  = MM5V3/V36_3/MM5        # INTERPB 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/INTERPB
 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 input file name
#
set input_file = ( MMOUT_DOMAIN1 )      # File should be on mass storage and will be read from $ExpName

#
# Namelist parameters: record2
 set pressure_bu_no_sfc_Pa     = 100000,95000,90000,85000,80000,75000,70000,65000,60000,55000,50000,45000,40000,35000,30000,25000,20000,15000,10000


#
# Namelist parameters: record3
 set print_info            = .false.


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

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

   cd INTERPB


#____________________________________________________________________________
# Make code
  make
  chmod 777 interpb

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

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

&record2
 pressure_bu_no_sfc_Pa       = $pressure_bu_no_sfc_Pa
/

&record3
 print_info            = $print_info
/

End_Of_Namelist

#_____________________________________________________________________________
# Run interpb
#
echo "RUNNING interpb"

interpb >& interpb.log

#_____________________________________________________________________________
# Clean up and write files to mass storage

echo "Move INTERPF files to mass storage"
echo " "
foreach file ( MMOUTP_DOMAIN* )
  echo "mswrite " $file
  mswrite -t $RetPd $file $ExpName/$file
end
foreach file ( REGRID_DOMAIN* )
  echo "mswrite " $file
  mswrite -t $RetPd $file $ExpName/$file
end
tar -cf FILE_MMOUTP.tar FILE_MMOUTP:*
gzip FILE_MMOUTP.tar 
echo "mswrite $ExpName/FILE_MMOUTP.tar.gz"
mswrite -t $RetPd FILE_MMOUTP.tar.gz $ExpName/FILE_MMOUTP.tar.gz
echo " mswrite -t $RetPd $ExpName/interpb.log "
mswrite -t $RetPd interpb.log $ExpName/interpb.log

#_____________________________________________________________________________

echo "DONE"

exit