#!/bin/csh -f

echo "Version 1.1 of $0"
echo ""
echo "This script creates a directory whose name is architecture-dependent,"
echo "resolution-dependent, and configuration-dependent.  It then installs"
echo "appropriate CCM3 source code, Makefile, and other ancillary files into"
echo "this directory, and puts sample namelists into the subdirectory 'run/'."
echo ""
echo "Configuration 'sld' (semi-Lagrangian dynamics) is not yet implemented."
echo "Boundary datasets are not necessarily available for all resolutions"
echo "and configurations.  For further information, please see the CCM3 home"
echo "page at http://www.cgd.ucar.edu/cms/ccm3/, and the CCM3 User's Guide."
echo ""
echo "Abbreviations are as follows:"
echo "sld: semi-Lagrangian dynamics"
echo "std: standard CCM3 configuration--climatological SST's and sea ice"
echo "som: Slab Ocean Model"
echo "csm: Climate System Model (builds the CCM3 configured for coupling to"
echo "     active ocean and sea-ice models)."
echo ""

#------------------------------------------------------------
# Set up Defaults
#------------------------------------------------------------

set validarch = (CRAY T3D RS6K SUN SGI)
set validres = (T42 T31 t31x15 T21 T5)
set validdyn = (spectral sld)
set validtype = (std som csm)

#------------------------------------------------------------
# Determine if batch 
#------------------------------------------------------------

unset batch
if ( $?ENVIRONMENT ) then
  if ( $ENVIRONMENT == BATCH ) then
    set batch
    if ( ! $?CCMARCH || ! $?CCMRES || ! $?CCMDYN || ! $?CCMTYPE || ! $?NCAR ) then
      echo 'Environment variables $CCMARCH, $CCMRES, $CCMDYN $CCMTYPE $NCAR '
      echo 'must all be set when $ENVIRONMENT == BATCH'
      exit 1
    endif
  endif
endif

#------------------------------------------------------------
# Architecture
#------------------------------------------------------------

if ( ! $?batch ) then
  if ( ! $?CCMARCH ) then
    setenv CCMARCH CRAY
    echo -n "Architecture?  Options are $validarch [$CCMARCH]"
    set ans = $<
    if ( $ans != "" ) setenv CCMARCH $ans
  endif
  while ( $#validarch )
    if ( $CCMARCH == $validarch[1] ) break;
    shift validarch
  end
  if ( $#validarch == 0 ) then
    echo "$CCMARCH is not a supported architecture"
    exit 1
  endif
endif
echo "Architecture is $CCMARCH"

setccmenv:
if ( ! $?NCAR ) then
  setenv NCAR n
  if ( $CCMARCH == CRAY ) then
    echo "" 
    echo "If this code is being built to run on one of the NCAR Cray machines,"
    echo "the Makefile will be constructed to use library versions of the"
    echo "ECMWF FFT code and mass store interface routines.  If not, the"
    echo "slower version of the FFT code and shell interfaces to the"
    echo "mass store will be used"
    echo " "
    echo -n "Is the target architecture one of the NCAR Cray machines? [yn]"
    set ans =  $<
    if ( $ans != y && $ans != n ) then
      echo "You must answer y or n"
      unsetenv NCAR
      goto setccmenv
    endif
    if ( $ans == y ) then
      setenv NCAR y
      echo "Assuming existence of certain NCAR libraries (e.g. ecmfft, mss)"
    else
      setenv NCAR n
      echo "Assuming Non-NCAR Cray"
    endif
  endif
endif

set spmd = ""
if ( $CCMARCH == T3D ) then
  set spmd = .spmd
else if ( ! $?batch && $CCMARCH != CRAY ) then
  echo ""
  echo -n "Enable message-passing? [n] "
  set ans = $<
  if ( $ans == y ) then
    set spmd = .spmd
    echo "Message-passing is enabled"
    setenv SPMD .spmd
  else
    echo "Message-passing is not enabled"
    setenv SPMD ""
  endif
endif

#------------------------------------------------------------
# Determine Resolution
#------------------------------------------------------------

if ( ! $?CCMRES ) then
  setenv CCMRES T42
  echo ""
  echo -n "Resolution?  Options are $validres [$CCMRES] "
  set ans = $<
  if ( $ans != "" ) setenv CCMRES $ans
endif
while ( $#validres )
  if ( $CCMRES == $validres[1] ) break;
  shift validres
end
if ( $#validres == 0 ) then
  echo "$CCMRES is not a supported resolution"
  exit 1
endif
echo "Resolution is $CCMRES"

#------------------------------------------------------------
# Determine Type of run
#------------------------------------------------------------

if ( ! $?CCMTYPE ) then
  setenv CCMTYPE std
  echo ""
  echo -n "Type of run?  Options are $validtype [$CCMTYPE]"
  set ans = $<
  if ( $ans != "" ) setenv CCMTYPE $ans
endif
while ( $#validtype )
  if ( $CCMTYPE == $validtype[1] ) break;
  shift validtype
end
if ( $#validtype == 0 ) then
  echo "$CCMTYPE is not a supported type of run"
  exit 1
endif
echo "Type of run is $CCMTYPE"

echo "Sea surface temperatures and sea ice distribution are computed"
if ( $CCMTYPE == som ) then
  echo "using the thermodynamic slab ocean option."
else if ( $CCMTYPE == csm ) then
  echo "from separate ocean and sea-ice executables."
else
  echo "from a boundary dataset."
endif

#------------------------------------------------------------
# Determine Type of dynamics: Currently only option is spectral
#------------------------------------------------------------

setenv CCMDYN spectral
if ( ! $?CCMDYN ) then
  setenv CCMDYN spectral
  echo ""
  echo -n "Type of dynamics?  Options are $validdyn [$CCMDYN]"
  set ans = $<
  if ( $ans != "" ) setenv CCMDYN $ans
endif
while ( $#validtype )
  if ( $CCMDYN == $validdyn[1] ) break;
  shift validdyn
end
if ( $#validdyn == 0 ) then
  echo "$CCMDYN is not a supported type of dynamics"
  exit 1
endif
echo "Type of dynamics is $CCMDYN"

#------------------------------------------------------------
# Make directories, link specific files
#------------------------------------------------------------

set srcdir = $CCMARCH.$CCMRES.$CCMDYN.$CCMTYPE$spmd
mkdir -p $srcdir/run
cd $srcdir || exit 2

ln -s ../common/* .
ln -s ../$CCMDYN/* .
ln -s ../lsm/* .

if ( $NCAR == y ) then
  echo "Using the default NCAR CRAY utilities."
else if ( $CCMARCH == CRAY ) then
  ln -s ../util/fft99.F ../util/tremain.F 
else if ( $CCMARCH == T3D ) then
  ln -s ../util/fft99.F ../util/tremain.F ../util/packaf.F ../util/unpkaf.F .
  ln -s ../util/sgexx.F ../util/isrch*.F ../util/intmax.F ../util/second.F ../util/when*.F .
else
  ln -s ../util/* .
  if ( $CCMARCH == RS6K ) then
    /bin/rm drand48_.c
  endif
endif

if ( $spmd == .spmd ) then
  ln -s ../spmd/* .
endif

if ( $CCMTYPE == som ) then
  ln -s ../som/* .
endif

#------------------------------------------------------------
# Determine resolution dependent parameters
#------------------------------------------------------------

switch ( $CCMRES )

  case T42:
    set plon = 128
    set plat = 64
    set ptrm = 42
    set ptrn = 42
    set ptrk = 42

    set lsmlon = 128
    set lsmlat = 64
    set numlv = 39

    switch ( $CCMTYPE )
      case std:
      case som:
        set lpt = 2796
        set kpt = 6009
        breaksw
      case csm:
        set lpt = 3402
        set kpt = 7192
        breaksw
      default:
        exit 3
    endsw

    breaksw

  case T31:
    set plon = 96
    set plat = 48
    set ptrm = 31
    set ptrn = 31
    set ptrk = 31

    set lsmlon = 96
    set lsmlat = 48
    set numlv = 22

    switch ( $CCMTYPE )
      case std:
      case som:
        set lpt = 1558
        set kpt = 3327
        breaksw
      case csm:
        set lpt = ?
        set kpt = ?
        breaksw
      default:
        exit 3
    endsw

    breaksw

  case t31x15:
    set plon = 48
    set plat = 48
    set ptrm = 15
    set ptrn = 31
    set ptrk = 31

    set lsmlon = 48
    set lsmlat = 48

    switch ( $CCMTYPE )
      case std:
      case som:
        set lpt =  838 
        set kpt = 1812
        set numlv = 12
       breaksw
      case csm:
        set lpt = 1076
        set kpt = 2289
        set numlv = 15
       breaksw
      default:
        exit 3
    endsw

    breaksw

  case T21:
    set plon = 64
    set plat = 32
    set ptrm = 21
    set ptrn = 21
    set ptrk = 21

    set lsmlon = 64
    set lsmlat = 32
    set numlv = 20

    switch ( $CCMTYPE )
      case std:
      case som:
        set lpt = 699
        set kpt = 1502
        breaksw
      case csm:
        set lpt = 981
        set kpt = 2100
        set numlv = 14
        breaksw
      default:
        exit 3
    endsw

    breaksw

  case T5:
    set plon = 16
    set plat =  8
    set ptrm =  5
    set ptrn =  5
    set ptrk =  5

    set lsmlon = 16
    set lsmlat = 8
    set lpt = 39
    set kpt = 87
    set numlv = 4
    breaksw

  default:
    exit 3
endsw

#------------------------------------------------------------
# Remove params.h and preproc.h if they exist and rebuild 
# them to specifications
#------------------------------------------------------------

echo ""
echo "Building header files params.h and preproc.h for CCM3 to be run on"
echo "architecture $CCMARCH at a horizontal resolution of ${CCMRES}..."
echo ""

/bin/rm -f params.h preproc.h

echo "#ifndef PARAMS_SET" >! params.h
echo "#define PARAMS_SET" >> params.h
if ( $CCMTYPE == som ) then
  echo "#define COUP_SOM" >> params.h
else if ( $CCMTYPE == csm ) then
  echo "#define COUP_CSM" >> params.h
endif
echo "#define $CCMARCH"   >> params.h
if ( $spmd == .spmd ) then
  echo "#define SPMD"     >> params.h
endif
if ( $NCAR == n ) then
  echo "#define FORTFFT"  >> params.h
endif
if ( $NCAR == n || $CCMARCH != CRAY ) then
  echo "#define SHELL_MSS">> params.h 
endif
echo "#define PCNST 1"    >> params.h
echo "#define PLEV 18"    >> params.h
echo "#define PLEVR 18"   >> params.h
echo "#define PLON $plon" >> params.h
echo "#define PLAT $plat" >> params.h
echo "#define PTRM $ptrm" >> params.h
echo "#define PTRN $ptrn" >> params.h
echo "#define PTRK $ptrk" >> params.h
if ( $spmd == .spmd ) then
  if ( $CCMARCH == T3D ) then
    echo "#define ISIZE INTEGER8" >> params.h
  else
    echo "#define ISIZE INTEGER4" >> params.h
  endif
  if ( $CCMARCH == RS6K ) then
    echo "#define LOGLEN 4"       >> params.h
  else
    echo "#define LOGLEN 8"       >> params.h
  endif
endif
echo "#endif"                     >> params.h

echo "#ifndef PREPROC_SET"    >! preproc.h
echo "#define PREPROC_SET"    >> preproc.h
echo "#define COUP_CCM"       >> preproc.h
if ( $spmd == .spmd ) then
  echo "#define SPMD"         >> preproc.h
endif
echo "#define LSMLON $lsmlon" >> preproc.h
echo "#define LSMLAT $lsmlat" >> preproc.h
echo "#define LPT $lpt"       >> preproc.h
echo "#define KPT $kpt"       >> preproc.h
echo "#define NUMLV $numlv"   >> preproc.h
echo "#endif"                 >> preproc.h

#------------------------------------------------------------
# Build the appropriate Makefile
#------------------------------------------------------------

# Build list of object files and their dependencies for "make"  
# No ".c" files are needed on Cray PVP machines.

if ( $CCMARCH == T3D ) then
  echo "Copying over namelist source files for T3D"
  cp -f ../$CCMARCH/lsmnldata.$CCMRES.F ./lsmnldata.F || exit 4
  cp -f ../$CCMARCH/nldata.$CCMRES.F    ./nldata.F    || exit 4
endif

if ( $CCMARCH == CRAY ) then
#  rm packaf.F
#  rm unpkaf.F
  /bin/ls -1 *.F >! Srcfiles
else
  /bin/ls -1 *.[Fc] >! Srcfiles
endif

echo "Building dependency list for make.  This may take a while..."

perl ../makdep || exit 5
if ( $status != 0 ) then
  echo "perl may not have been found in any of the directories $path"
  echo "or the script 'makdep' (builds dependency list) encountered an error"
endif

if ( ! -f Makefile) then
  echo "Building the Makefile..."
  ../build.makefile
endif

#------------------------------------------------------------
# Build appropriate namelist files
# T3D does not support namelist, so copy appropriate code
#------------------------------------------------------------

if ( $CCMARCH == T3D ) then
  echo "Copying over run script for T3D"
  cp ../T3D/runit                       ./run/runit
else
  if ( ! $?batch) then
    set ans = y
    if ( ! $?BUILDNL ) then
      echo -n "Should a namelist be built? [y] "
      set ans =  $<
    endif
    if ( $ans != n ) then
      (cd run ; ../../build.namelist)
      echo "You may need to edit the namelist before running the model"
    endif
  endif   
endif

#------------------------------------------------------------
# Final comments
#------------------------------------------------------------

echo ""
echo "Done configuring CCM3 code for a $CCMARCH at a horizontal resolution of $CCMRES"
echo ""
echo "- To build the executable, type: 'make' from directory:"
echo "     $cwd"
echo ""
echo "Before running the model, make sure boundary datasets are in place"
echo ""
echo "- To start the model, type: 'ccm3bin < nl.init' from directory:"
echo "     $cwd/run"
echo ""
if ( $CCMARCH == CRAY ) then
  echo ""
  echo "- To run the model in batch mode, see the example scripts in: "
  echo "     $cwd/CRAY"
  echo ""
endif
echo "- To rebuild the makefile, type: '../build.makefile' from directory:"
echo "     $cwd"
echo ""
echo "- To rebuild the namelist, type: '../../build.namelist' from directory:"
echo "     $cwd/run"
echo ""

exit 0
