#! /bin/tcsh -f
#
set NTKS = 4
setenv OMP_NUM_THREADS 1
setenv OMP_STACKSIZE 64M

set USER_FC = mpif90
set FC_TYPE = pgi
setenv USER_CC gcc

set NETCDF = /usr/local/netcdf-4.3.2

setenv INC_NETCDF ${NETCDF}/include
setenv LIB_NETCDF ${NETCDF}/lib
setenv MOD_NETCDF $INC_NETCDF

setenv MPI_TYPE_MAX 100000

## Do our best to get sufficient stack memory
limit stacksize unlimited

## ROOT OF CAM DISTRIBUTION - probably needs to be customized.
## Contains the source code for the CAM distribution.
## (the root directory contains the subdirectory "models")

set curdir = `pwd`
##

set camroot = $curdir/..

## ROOT OF CAM DATA DISTRIBUTION - needs to be customized unless running at NCAR.
## Contains the initial and boundary data for the CAM distribution.
## (the root directory contains the subdirectories "atm" and "lnd")
setenv CSMDATA  /data1/fvitt/inputdata
setenv CAMCHEM_EDITOR emacs

## Default namelist settings:
## $case is the case identifier for this run. It will be placed in the namelist.
## $runtype is the run type: initial, restart, or branch.
## $nelapse is the number of timesteps to integrate, or number of days if negative.
set case         = waccm5_epp_test
set runtype      = startup
#set runtype      = continue
set stop_n       = 1

## $wrkdir is a working directory where the model will be built and run.
## $blddir is the directory where model will be compiled.
## $rundir is the directory where the model will be run.
## $cfgdir is the directory containing the CAM configuration scripts.
set wrkdir       = /data1/$LOGNAME/testruns
set blddir       = $wrkdir/$case/bld
set rundir       = $wrkdir/$case
set cfgdir       = $camroot/components/cam/bld

#rm -fr $rundir
#rm -fr $rundir/bld/cam

## Ensureq that run and build directories exist
mkdir -p $rundir                || echo "cannot create $rundir" && exit 1
mkdir -p $blddir                || echo "cannot create $blddir" && exit 1

## If an executable doesn't exist, build one.
if ( ! -x $blddir/cam ) then
    cd $blddir                  || echo "cd $blddir failed" && exit 1
    $cfgdir/configure \
      -fc $USER_FC \
      -fc_type $FC_TYPE \
      -phys cam5 -chem waccm_ma_mam4 \
      -debug \
      -spmd \
      -nosmp \
      -dyn fv \
#      -res 1.9x2.5 -cice_bsizex 3 -cice_bsizey 96 -cice_maxblocks 24 -cice_decomptype cartesian \
#      -res 4x5 -cice_bsizex 3 -cice_bsizey 46 -cice_maxblocks 12 -cice_decomptype cartesian \
      -res 10x15 -cice_bsizex 3 -cice_bsizey 19 -cice_maxblocks 8 -cice_decomptype cartesian \
#      -res 48x96 -cice_bsizex 3 -cice_bsizey 24 -cice_maxblocks 8 -cice_decomptype cartesian -dyn eul\
    || echo "configure failed" && exit 1

    echo "building CAM in $blddir ..."
    rm -f Depends
    /usr/bin/gmake -j4 >&! MAKE.out      || echo "CAM build failed: see $blddir/MAKE.out" && exit 1
#    rm -f *.o *.mod
endif

echo " "
echo "build complete"
echo " "
#exit 0

## Create the namelist
cd $blddir                      || echo "cd $blddir failed" && exit 1
$cfgdir/build-namelist -case $case -runtype $runtype -d $rundir -ignore_ic_date -s \
 -namelist "&camexp  stop_n=$stop_n, stop_option='nsteps'\
 solar_parms_file = '/data1/fvitt/inputdata/atm/waccm/phot/wasolar_ave.nc'\
 start_ymd = 101 \
 epp_all_filepath = '/data1/fvitt/inputdata/atm/cam/solar/solarforcing_picontrol_fx_3.1_EPP_c160505.nc'\
 solar_data_file  = '/data1/fvitt/inputdata/atm/cam/solar/solarforcing_picontrol_fx_3.1_irrad_c160505.nc'\
 / "  || echo "build-namelist failed" && exit 1

#exit 0

## Run CAM
cd $rundir                      || echo "cd $rundir failed" && exit 1
echo "running CAM in $rundir..."

#totalview mpirun -a -np $NTKS $blddir/cam
#
mpirun -l -np $NTKS $blddir/cam || echo "CAM run failed" && exit 1

echo " FINISHED "

exit 0


 epp_all_filepath = '/data1/fvitt/solar_v3_final/solarforcing_picontrol_fx_3.1_particles.nc'\
 solar_data_file  = '/data1/fvitt/solar_v3_final/solarforcing_picontrol_fx_3.1_irradiance_ref.nc'\

 solar_parms_file = '/data1/fvitt/inputdata/atm/waccm/solar/proxy_solar_Solomon_Richmond_1845-2008_daily_c100423.nc'\

 start_ymd = 18500101 \
 epp_all_filepath = '/data1/fvitt/solar_v3_final/solarforcing_ref_day_3.1_particles_10days.nc'\
 solar_data_file  = '/data1/fvitt/solar_v3_final/solarforcing_ref_day_3.1_10days_irrad_c160503.nc'\
