#!/bin/csh -f 
#=======================================================================
#  This is a CCM3 stand alone atm model NQS batch job script for an
#                 non-CRAY computer (default SGI)
#        without distributed memory (SPMD) 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 begining
#  user in mind, and has been configured in such a way to try to make it
#  simple, yet powerful to use. And it has been configured such that a user
#  need only edit this script to make changes to the model configuration, 
#  build, or run.
#
#=======================================================================
# o Edit 1a if source, executable and/or input data directories are
#   different from default
#
# o Edit 1b if MAXCPUS or location of netCDF and/or MPI libraries and 
#   include files are different from default
#
# o Edit 1c if choice of model dynamics or ocean model is different 
#   from default
#
# o Edit 2 if model input datasets are different from default
#
# o Edit 3 if input ozone dataset is different from default
#
# o Edit 3 and 4 if model resolution is different from default
#
# o Edit 5 if input namelist is different from default
#=======================================================================

#-----------------------------------------------------------------------
# 1a. Set environment variables for source, executable and input data 
#     directories
#-----------------------------------------------------------------------

# Determine environment variables for paths
#
#   setenv MODEL_EXEDIR to directory where model executable should reside
#   setenv MODEL_SRCDIR to directory where model source code resides
#   setenv MODEL_DATDIR to directory where model input data resides

setenv MODEL_EXEDIR  /ccm3.6/ccm/run
setenv MODEL_SRCDIR  /ccm3.6/ccm/src
setenv MODEL_DATDIR  /ccm3.6/ccm/data

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

if !(-d $MODEL_DATDIR) mkdir -p $MODEL_DATDIR
if !(-d $MODEL_EXEDIR) mkdir -p $MODEL_EXEDIR; cd $MODEL_EXEDIR; 
if !(-d obj) mkdir obj ; cd obj

#-----------------------------------------------------------------------
# 1b. Set environment variables for platform related information
#-----------------------------------------------------------------------

# Determine platform and and related information
#

#   setenv CPU       to SGI or SUN (must be uppercase) 
#   setenv MAXCPUS   to number of processors
#
# Determine location of NetCDF include files and library
#
#   setenv LIB_NETCDF to directory of netCDF library
#   setenv INC_NETCDF to directory of netCDF includes
#
# Determine the location of MPI library and  include files 
#
#   setenv LIB_MPI to directory of MPI library
#   setenv INC_MPI to directory of MPI includes

# The following default settings are for running the stand-alone model 
# on an SGI with 2 CPUS. Eulerian dynamics and the
# data ocean model will be used. The distributed memory (SPMD) 
# implementation is not used.

setenv CPU           LINUX
setenv SPMD          TRUE
setenv LIB_NETCDF    /usr/local/devel/netcdf/lib
setenv INC_NETCDF    /usr/local/devel/netcdf/include
setenv LIB_MPI       /usr/local/mpich/build/LINUX/ch_p4/lib
setenv INC_MPI       /usr/local/mpich/include
setenv MAXCPUS       2

#-----------------------------------------------------------------------
# 1c. Set environment variables for model grid resolution
#
#  PCNST  = number of advected tracers
#  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
#-----------------------------------------------------------------------

setenv PLON  128
setenv PLAT   64
setenv PLEV   18
setenv PTRM   42
setenv PTRN   42 
setenv PTRK   42
setenv PCNST   1

#-----------------------------------------------------------------------
# 1d. Set environment variables for choice of model dynamics and 
#     ocean model
#-----------------------------------------------------------------------

# Determine type of dynamics (Default is eulerian)
#
#   setenv DYNAMICS to "eul" for eulerian dynamics
#   setenv DYNAMICS to "sld" for semi-lagrange dynamics
#
# Determine ocean model component (Default is data-ocean model)
#
#   setenv OCEAN to "dom" for data ocean model 
#   setenv OCEAN to "som" for slab ocean model

# The following default settings are for running the stand-alone model 
# with Eulerian dynamics and the data ocean model 

setenv DYNAMICS      eul
setenv OCEAN         dom

#-----------------------------------------------------------------------
# 2. Determine the input datasets
#-----------------------------------------------------------------------

# set the file names of the required input data files

set DATINI  = SEP1.T42.0198.nc           # CCM3 initial data
set DATSST  = T42M5079.nc                # sst data
set DATOZN  = ozn.0596.r8.nc             # ozone data   
set DATLSM  = 'arbitrary initialization' # LSM initial data

#-----------------------------------------------------------------------
# 3. Determine input namelist 
#-----------------------------------------------------------------------

# the following namelist is the minimum namelist for running
# the stand-alone ccm3. 

cd $MODEL_EXEDIR
cat >! atm.parm << EOF
 \$CCMEXP
 caseid   = 'ISem98' 
 ctitle   = 'Example 01'
 datadir  = '$MODEL_DATDIR'
 ncdata   = '$DATINI'
 bndtvs   = '$DATSST'
 bndtvo   = '$DATOZN'
 irt      = 100
 nsrest   = 0
 ndens    = 1
 nnbdat   = 19980101 
 nnbsec   = 0
 nndbas   = 0
 nnsbas   = 0
 mfilt    = 20
 dtime    = 1200
 nestep   = 72
 nhtfrq   = 72
 nrefrq   = 1
 incorbuf = .TRUE.
 incorhst = .TRUE.
 incorrad = .TRUE.
 iyear_ad = 1998
 \$
 \$lsmexp
 datadir  = '$MODEL_DATDIR'
 finidat  = '$DATLSM'
 \$
EOF

#-----------------------------------------------------------------------
#                IN GENERAL NEED NOT EDIT BELOW THIS POINT 
#-----------------------------------------------------------------------

echo
echo "BUILDING CCM3 WITH THE FOLLOWING OPTIONS"
echo "========================================"
echo "Target Machine      : $CPU"
echo "Dynamics            : $DYNAMICS"
echo "Ocean config        : $OCEAN"
echo "Buffers incore      : TRUE"
echo "Initial dataset     : $DATINI"
echo "SST Dataset         : $DATSST"
echo "Ozone Dataset       : $DATOZN"
echo "LSM Dataset         : $DATLSM"
echo "Number of Longitudes: $PLON"
echo "Number of Latitudes : $PLAT"
echo "Number of tracers   : $PCNST"
echo "Spectral truncation : $PTRM, $PTRN, $PTRK"
echo 

#-----------------------------------------------------------------------
# The source code filepath file, "Filepath",
# Modify Filepath only if user modified code is to be inserted into the model
#-----------------------------------------------------------------------

# SEE example_Filepath (in this directory) for more details.

# the following "Filepath" is for stand-alone ccm3 running on a 
# non-CRAY machine with Eulerian dynamics, the data ocean model
# annd the distributed memory (SPMD) implementation

cd $MODEL_EXEDIR
set FN = obj/Filepath
cat >! tmp << EOF 
$MODEL_SRCDIR/control
$MODEL_SRCDIR/dynamics
$MODEL_SRCDIR/dynamics/$DYNAMICS
$MODEL_SRCDIR/$OCEAN
$MODEL_SRCDIR/physics
$MODEL_SRCDIR/csm_share
$MODEL_SRCDIR/ccmlsm_share
$MODEL_SRCDIR/lsm
$MODEL_SRCDIR/mathutil
$MODEL_SRCDIR/srchutil
$MODEL_SRCDIR/spmd
$MODEL_SRCDIR/spmd/$DYNAMICS
EOF

cmp -s $FN tmp
if ( $status != 0 )then
  if ( -e Filepath ) then 
     echo "Filepath changed, removing all files except Filepath"
  else     
     echo "creating Filepath"
  endif 
  \rm $MODEL_EXEDIR/obj/[A-Z]*.o
  \rm $MODEL_EXEDIR/obj/[a-l]*.o
  \rm $MODEL_EXEDIR/obj/[m-z]*.o
  \rm $MODEL_EXEDIR/obj/[A-Z]*.d
  \rm $MODEL_EXEDIR/obj/[a-l]*.d
  \rm $MODEL_EXEDIR/obj/[m-z]*.d
  mv -f tmp $FN
else
  echo "Use previous $FN"
  \rm tmp
endif


#-----------------------------------------------------------------------
# 3. Determine the atmosphere resolution include file, "params.h"
#    Default is 18 level T42 with 23 level ozone datset
#-----------------------------------------------------------------------

# Edit only #define settings below for 
# PNATS,POZLEV

#  PNATS  = number of non-advected tracers
#  POZLEV = number of ozone levels on ozone dataset  

# the following "params.h" assumes:
# 18 level T42 model with only water as an advected tracer, no
# non-advected tracers and a 23 level ozone dataset

cd $MODEL_EXEDIR/obj
set FN = params.h
cat >! tmp << EOF
#ifndef PARAMS_SET
#define PARAMS_SET
#define PNATS  0
#define POZLEV 23
#define PCNST  $PCNST
#define PLEV   $PLEV
#define PLEVR  $PLEV
#define PLON   $PLON
#define PLAT   $PLAT
#define PTRM   $PTRM
#define PTRN   $PTRN
#define PTRK   $PTRK
#endif
EOF

cmp -s $FN tmp
if ( $status != 0 )then
  echo "Create new $FN"
  mv -f tmp $FN
else
  echo "Use previous $FN"
  \rm tmp
endif

#-----------------------------------------------------------------------
# 4. Edit the lsm resolution include file, "preproc.h"
#    The default is 18 level T42
#-----------------------------------------------------------------------

# Edit only #define settings below for 
# LSMLON, LSMLAT 

#  LSMLON = number of longitudes
#  LSMLAT = number of latitudes

# the following "preproc.h" assumes a an output 
# NetCDF that is single precision.

cd $MODEL_EXEDIR/obj
set FN = preproc.h
cat >! tmp << EOF
#ifndef PREPROC_SET
#define PREPROC_SET
#define LSMLON $PLON
#define LSMLAT $PLAT
#endif
EOF

cmp -s $FN tmp
if ( $status != 0 )then
  echo "Create new $FN"
  mv -f tmp $FN
else
  echo "Use previous $FN"
  \rm tmp
endif
#-----------------------------------------------------------------------
# The general include file "misc.h"
# Modify misc.h only if NCPREC is to be changed to NF_DOUBLE
#-----------------------------------------------------------------------

# SUN or SGI   = platform type (this is set to $CPU below)
# PVP          = parallel vector processor, undefine for non-CRAY
# REALTYPE     = set to MPI_DOUBLE_PRECISION for non-CRAY
# COUP_CSM     = if defined, ccm3 is run through flux coupler
#                undefine for stand-alone model
# COUP_SOM     = if defined  , slab ocean model is enabled for stand-alone ccm3
#                if undefined, data ocean model is enabled for stand-alone ccm3
# NCPREC       = precision of output NetCDF history file 
#                set to NF_FLOAT or NF_DOUBLE, default is NF_FLOAT
# SHELL_MSS    = define when interface to routine mswrite does not exist
# FORTFFT      = define for non-CRAY
# SPMD         = define for non-CRAY with distributed memory implementation

# the following "misc.h" assumes:
# a stand-alone model running on an non-CRAY machine 
# with the distributed memory (SPMD) implementation

if ( $OCEAN == 'dom' ) then
  set coup_som = '#undef  COUP_SOM'
endif 
if ( $OCEAN == 'som' ) then
  set coup_som = '#define COUP_SOM'
endif

cd $MODEL_EXEDIR/obj
set FN = misc.h
cat >! tmp << EOF
#ifndef MISC_SET
#define MISC_SET
#define $CPU
#undef  PVP 
#define REALTYPE MPI_DOUBLE_PRECISION
$coup_som
#undef  COUP_CSM
#define NCPREC   NF_FLOAT
#define SHELL_MSS
#define FORTFFT
#define SPMD
#endif
EOF

cmp -s $FN tmp
if ( $status != 0 )then
  echo "Create new $FN"
  mv -f tmp $FN
else
  echo "Use previous $FN"
  \rm tmp
endif

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

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.
#
gmake -j4 -e -f $MODEL_SRCDIR/../bld/Makefile >>& $MODEL_EXEDIR/compile_log.atm || exit 3

#-----------------------------------------------------------------------
# Run the model
#-----------------------------------------------------------------------

echo 'Running CCM ... '
echo

cd $MODEL_EXEDIR
mpirun -np $MAXCPUS atm < atm.parm >&! atm.log.$$

echo "CCM Finished - see atm.log.$$ for log"
echo 
