#!/bin/csh -f 
#
#=======================================================================
#
#  This is a CCM3 stand-alone atm model script for a COMPAQ ES40 cluster
#  with hybrid distributed memory (SPMD) shared memory (OPENMP) 
#  implementation 
#
#=======================================================================
#
#  This is a sample build script as one possible means to configure,
#  compile, link and run the model.  In general advanced users are
#  expected to customize this script for their own needs or to drop it
#  entirely and come up with their own procedures.  This script is
#  written with the beginning user in mind, and has been configured to
#  make it simple to use.
#
#=======================================================================
#
# o Edit 1 if source, executable and/or input data directories are
#    different from default
#
# o Edit 2 if load and runtime environment variables are different from
#    default
#
# o Edit 3 if platform-related information is different from default
#
# o Edit 4 and 5 if model resolution is different from default
#
# o Edit 6 if choice of model dynamics is different from default
#
# o Edit 7 if input namelist is different from default
#
#=======================================================================

#-----------------------------------------------------------------------
# 1. Set environment variables for source, executable and input data 
#    directories
#-----------------------------------------------------------------------
#
# Determine environment variables for paths
#
#   setenv MODEL_SRCDIR to directory where model source code resides
#   setenv MODEL_EXEDIR to directory where model executable should reside
#   setenv MODEL_DATDIR to directory where model input datasets reside
#   setenv MODEL_DEPDIR to directory where makdep source code resides
#
# THE FOLLOWING ASSUMES THAT THIS SCRIPT IS RUN from the ccm/bld directory
# IF IT IS NOT - THE FOLLOWING ENVIRONMENT VARIABLES MUST BE MODIFIED
# ACCORDINGLY
#
#-----------------------------------------------------------------------

setenv MODEL_SRCDIR  /usr/users/maur/ccm/src
#setenv MODEL_EXEDIR  /usr/users/maur/ccm/run
setenv MODEL_EXEDIR  /data/usr/users/maur/ccm/run
setenv MODEL_DATDIR  /usr/users/maur/ccm/data
setenv MODEL_DEPDIR  /usr/users/maur/ccm/tools/makdep

#setenv MODEL_SRCDIR  `pwd`/../src
#setenv MODEL_EXEDIR  `pwd`/../run
#setenv MODEL_DATDIR  ../data
#setenv MODEL_DEPDIR  `pwd`/../tools/makdep

mkdir -p $MODEL_EXEDIR/obj

#-----------------------------------------------------------------------
# 2. Set load and runtime environment variables
#-----------------------------------------------------------------------
#
# Determine location of NetCDF include files and library
#
#   setenv LIB_NETCDF to directory of netCDF library
#   setenv INC_NETCDF to directory of netCDF includes
#
# Runtime machine-specific environment variables
#
# MP_STACK_SIZE  slave stack size
# SPMD           set to true if using SPMD for Makefile compilation flags
# 
#-----------------------------------------------------------------------

setenv LIB_NETCDF  /usr/local/lib
setenv INC_NETCDF  /usr/local/include

#setenv SPMD TRUE
#setenv MP_STACK_SIZE 40000000
setenv SPMD FALSE
#setenv MP_STACK_SIZE 40000000

limit stacksize unlimited 

#-----------------------------------------------------------------------
# 3. Generate the general include file, "misc.h" for platform-related
#    information
#-----------------------------------------------------------------------
#
# OSF1         = platform type
# COUP_CSM     = undefine for stand-alone ccm3 model
#                (if defined, ccm3 is run through flux coupler)
# SHELL_MSS    = define when interface to routine mswrite does not exist
# FORTFFT      = always define
# SPMD         = define if use distributed memory implementation
#
#-----------------------------------------------------------------------


set IREBUILD = YES
if($IREBUILD == "NO") then
touch $MODEL_SRCDIR/*/*.F
rm $MODEL_EXEDIR/obj/misc.h
rm $MODEL_EXEDIR/obj/params.h
rm $MODEL_EXEDIR/obj/preproc.h
rm $MODEL_EXEDIR/obj/Filepath

endif



cd $MODEL_EXEDIR/obj
if ( !(-e misc.h) ) then
cat >! misc.h << EOF
#ifndef MISC_SET
#define MISC_SET
#define OSF1
#undef  COUP_CSM
#define SHELL_MSS
#undef FORTFFT
#undef SPMD
#endif
EOF
endif

#-----------------------------------------------------------------------
# 4. Generate the atmosphere resolution include file, "params.h"
#    Default is 30 level T42
#-----------------------------------------------------------------------
#
#  PCNST  = number of advected tracers 
#          (default is "1" for water vapor)
#  PNATS  = number of non-advected tracers 
#          (default is "1" for prognostic cloud water)
#  PLEV   = number of vertical levels
#  PLEVR  = number of vertical levels in radiation (set to PLEV)
#  PLON   = number of longitudes
#  PLAT   = number of latitudes
#  PTRM   = spectral truncation 
#  PTRN   = spectral truncation
#  PTRK   = spectral truncation
#
#-----------------------------------------------------------------------

cd $MODEL_EXEDIR/obj
if ( !(-e params.h) ) then
cat >! params.h << EOF
#ifndef PARAMS_SET
#define PARAMS_SET
#define PCNST  1
#define PNATS  0
#define PLEV   18
#define PLEVR  18
#define PLON   128
#define PLAT   64
#define PTRM   42
#define PTRN   42
#define PTRK   42
#endif
EOF
endif

#-----------------------------------------------------------------------
# 5. Generate the lsm resolution include file, "preproc.h"
#    The default is T42
#-----------------------------------------------------------------------
#
#  LSMLON = number of longitudes
#  LSMLAT = number of latitudes
#
#-----------------------------------------------------------------------

cd $MODEL_EXEDIR/obj
if ( !(-e preproc.h) ) then
cat >! preproc.h << EOF
#ifndef PREPROC_SET
#define PREPROC_SET
#define LSMLON 128
#define LSMLAT 64
#endif
EOF
endif

#-----------------------------------------------------------------------
# 6. Generate Filepath.  Edit for for choice of model dynamics: 
#      Eulerian      ("eul"),
#      Semi-Lagrange ("sld"), or
#      Lin-Rood      ("lr").
#    Default is "eul"
#-----------------------------------------------------------------------

cd $MODEL_EXEDIR/obj
cat >! Filepath << EOF 
$MODEL_SRCDIR/control
$MODEL_SRCDIR/dynamics
$MODEL_SRCDIR/dynamics/eul
$MODEL_SRCDIR/dom
$MODEL_SRCDIR/physics
$MODEL_SRCDIR/csm_share
$MODEL_SRCDIR/ccmlsm_share
$MODEL_SRCDIR/lsm
$MODEL_SRCDIR/mathutil
EOF

#-----------------------------------------------------------------------
# 7. Generate input namelist 
#-----------------------------------------------------------------------
#
# caseid   = Case name for this run
# ncdata   = Path and filename of atm initial conditions
# bndtvs   = Path and filename of time-variant SST boundary dataset
# bndtvo   = Path and filename of time-invariant ozone mixing ratio
#            boundary dataset
# iradsw   = Frequency of short-wave radiation calculation 
#            ("hours" if < 0)
# iradlw   = Frequency of long-wave radiation calculation 
#            ("hours" if < 0)
# iradae   = Frequency of absorptivity/emissivity calculation 
#            ("hours" if < 0)
# nhtfrq   = History file write frequency 
#            ("hours" if < 0)
# dtime    = time step (seconds)
# nestep   = ending timestep (or days if < 0)
# irt      = retention period for files disposed to MSS (days)
# kmxhdc   = number of levels over which to apply Courant limiter
# iyear_ad = year used to compute earth's orbital parameters
#
# finidat  = LSM dataset.  If "finidat = arbitrary initialization", model
#            generates its own initial conditions from the following
#            datasets. 
#                        swmp.1x1
#                        bats.data
#                        flak.1x1
#                        drainage.p5xp5.nc
#                        olson.data
#                        webb.1x1
# mksrfdir = directory to find high resolution land boundary datasets
#            (only used when finidat = 'arbitrary initialization")
#            Note that if mksrfdir = $MODEL_DATADIR, as below, the above 
#            datasets need to be in the $MODEL_DATDIR directory:
#
#-----------------------------------------------------------------------

cd $MODEL_EXEDIR
cat >! atm.parm << EOF
 &CCMEXP
 caseid   = 'TEST1'
 ctitle   = 'SENAMHI TEST CCM3 1'
 ncdata   = '$MODEL_DATDIR/SEP1.T42.0198.nc'
 bndtvs   = '$MODEL_DATDIR/sst_anom_clim.200110.T42.nc'
 bndtvo   = '$MODEL_DATDIR/noaao3.1990.21999.nc'
 irt      =   0
 nsrest   =   0
 nnbdat   = 20011101
 nnbsec   =   0
 nndbas   =   0
 nnsbas   =   0
 dtime    =  1200
 mfilt    =  10
 nestep   =  -270
 nhtfrq   =  2160 
 kmxhdc   =  1
 fexcl1   =  'CGS','FLNS','FLNSC','FLNT','FLNTC'
             'FSDS','FSNIRT','FSNIRTSQ','FSNRTC','FSNS'
             'FSNSC','FSNT','FSNTC','SOLIN','SOLL'
             'SOLLD','SOLS','SOLSD','SRFRAD'
 iyear_ad = 1950
 /
 &lsmexp
 mksrfdir = '$MODEL_DATDIR'
 finidat  = '$MODEL_DATDIR/CCM3.5LSMICSEP1.0198.310lsm.nc'
 /
EOF

#-----------------------------------------------------------------------
# Generate the executable
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
#
# cd to $MODDEL_DEPDIR (default setting is "../tools/makdep) to create 
# an alternative dependency generator to cpp.  Running gmake will create 
# an executable named "makdep" in $(HOME)/bin.  This directory will need 
# to be in one's $PATH in order to build ccm with the standard Makefile.
#
#-----------------------------------------------------------------------

cd $MODEL_DEPDIR
/usr/local/bin/gmake

cd $MODEL_EXEDIR/obj

echo "Compiling CCM ... see $MODEL_EXEDIR/compile_log.atm for log"
echo 

touch $MODEL_EXEDIR/compile_log.atm
echo '-------------------------------------' >>& $MODEL_EXEDIR/compile_log.atm
date                                         >>& $MODEL_EXEDIR/compile_log.atm
echo '-------------------------------------' >>& $MODEL_EXEDIR/compile_log.atm
#
# Make the model: "-j" means multi-process
#                 "-e" means override default settings 
#                      with environment variables set here.
#

setenv EXENAME atm
# original below
#/usr/local/bin/gmake -j8 -e -f $MODEL_SRCDIR/../bld/Makefile >>& $MODEL_EXEDIR/compile_log.atm || exit 1

/usr/local/bin/gmake -j1 -e -f $MODEL_SRCDIR/../bld/Makefile >>& $MODEL_EXEDIR/compile_log.atm || exit 1

#-----------------------------------------------------------------------
# Run the model
# -B basenode
#    The number of the base node (the first node to use) in the
#    partition. Numbering within the partition starts at  0.
#    It is better not to choose 0 or 1
# -N nodes
#    The number of nodes required.
# -c CPUs
#    Number of CPUs per node
#-----------------------------------------------------------------------

echo 'Running CCM ... '
echo

cd $MODEL_EXEDIR
date >& time_spend.ccm

# prun -B4 -N4 -c4 sh -c './atm < atm.parm 1> atm.log.$$'
./atm < atm.parm >&! atm.log.$$

date >> time_spend.ccm
echo "CCM Finished - see $MODEL_EXEDIR/atm.log.$$ for log"
echo 
