#!/bin/csh

#
# RUN MULTIPLE WRF RUNS ON KINGSPEAK OR EMBER OR ATMOS 01 or 04 (atmos doesn't work yet)
#
# JEFF MASSEY 6/6/2013
# MODIFIED 10/12/2013 FOR KINGSPEAK
# AND GSL TEMPERATURE
# MODIFIED BY LEAH 4/24/14 FOR OWLES

set machine = 'kingspeak'

#directories
# WHERE THE EXECUTABLES, TABLES LIVE
if ( $machine == 'atmos04' ) then
set WRFDIR = 'full path to WRF compiles/WRFV3_atmos/run'
else if ( $machine == 'kingspeak' ) then
set WRFDIR = 'full path to WRF compiles/WRFV3_king/run'
endif
# WHERE MET_EM FILES LIVE
set METEMDIR = 'path to WPS for met_ems, or set it to be equal to outputdir if you move them yourself'
# THE DIR ABOVE WHERE YOU WANT THE WRF TO RUN
set OUTPUTDIR = 'a directory will be created in the directory specified here for the specific run'

#CHANGE THE LAKE TEMPERATURE IN INTIALIZE_REAL?
set changelaketemp = 'no'
#RECOMPILE?
set compilewrf = 'no'

###############################################
# LOOP THROUGH MULTIPLE RUNS, CAN BE EASILY MODIFIED TO ADD MORE

@ runnum = 1
while ( $runnum < 2 ) #change to 2 if you want to do 2 runs at once

#clean up after previous run
cd $WRFDIR
rm met_em.d*
rm rsl*
rm wrfinput*

# input options
if ( $runnum == 1 ) then
set event = 'noterrain' #name for specific run directory
set onttemp = '278.15' #only matters if you set changelaketemp to 'yes'
# time for initialization of run
set year = '2014'
set month = '01'
set day = '11'
set hour = '06'
#end time
set emonth = '01'
set eday = '12'
set ehour = '18'
set run_hours = '36'
# time for kingspeak run (if you're running it on a regular node, this doesn't matter)
set walltime = '04:00:00'
endif

#kingspeak allows us to submit more than one job at a time
if ( $runnum == 2 ) then
set event = 'iop4_sst_277.3'
set onttemp = '277.3'
# time for initialization of run
set year = '2014'
set month = '01'
set day = '11'
set hour = '18'
#end time
set emonth = '01'
set eday = '12'
set ehour = '18'
set run_hours = '30'
#endifn_hours = '36'
endif

##########################

# make the output directory for wrf.exe and met_em files
set nameofrun = ${event}
set outputfolder = ${OUTPUTDIR}'/'${nameofrun}
mkdir $outputfolder

#fix the namelist.input file

set var1a = 'YYYY'
set var1b = $year
set var2a = 'MMMM'
set var2b = $month
set var3a = 'DDDD'
set var3b = $day
set var4a = 'HHHH'
set var4b = $hour
set var5a = 'EMMM'
set var5b = $emonth
set var6a = 'EDDD'
set var6b = $eday
set var7a = 'EHHH'
set var7b = $ehour
set var8a = 'YYYY2'
set var8b = $year
set var9a = 'RH'
set var9b = $run_hours

sed 's/'"$var1a"'/'"$var1b"'/g \
s/'"$var2a"'/'"$var2b"'/g \
s/'"$var3a"'/'"$var3b"'/g \
s/'"$var4a"'/'"$var4b"'/g \
s/'"$var5a"'/'"$var5b"'/g \
s/'"$var6a"'/'"$var6b"'/g \
s/'"$var7a"'/'"$var7b"'/g \
s/'"$var8a"'/'"$var8b"'/g \
s/'"$var9a"'/'"$var9b"'/g' namelist.input.base.sample > namelist.input

echo $event

#####################
# change lake temperature in initialization
cd ../dyn_em

if ( $changelaketemp == 'yes' ) then
echo 'CHANGING WRF LAKE TEMP'

set var10a = 'LAKEONTARIOTEMPERATURE'
set var10b = ${onttemp}
sed 's/'"$var10a"'/'"$var10b"'/g' module_initialize_real.F.base > module_initialize_real.F

cd ..

else if ( $changelaketemp == 'no' ) then
cp module_initialize_real.F.original module_initialize_real.F 

cd ..
endif

# compile

if ( $compilewrf == 'yes' ) then
echo 'COMPILING THE WRF'

# a lot of these have changed recently
if ( $machine == 'atmos04' ) then
setenv WRF_EM_CORE 1 
source /uufs/chpc.utah.edu/sys/pkg/pgi/std/etc/pgi.csh 
source /uufs/chpc.utah.edu/sys/pkg/mpich2/std_pgi/etc/mpich2.csh 
setenv NETCDF /uufs/chpc.utah.edu/sys/pkg/netcdf/4.0pg
setenv NCARG_ROOT /uufs/chpc.utah.edu/sys/pkg/ncarg/5.0.0
source /uufs/chpc.utah.edu/sys/pkg/pgi/std/etc/pgi.csh
endif

if ( $machine == 'ember' ) then
setenv WRF_EM_CORE 1
setenv NETCDF /uufs/chpc.utah.edu/sys/pkg/netcdf/4.0pg
source /uufs/chpc.utah.edu/sys/pkg/pgi/9.0.4/etc/pgi.csh
source /uufs/ember.arches/sys/pkg/mvapich2/std_pgi/etc/mvapich2.csh
setenv NCARG_ROOT /uufs/chpc.utah.edu/sys/pkg/ncarg/5.0.0
source /uufs/chpc.utah.edu/sys/pkg/pgi/std/etc/pgi.csh
endif

if ( $machine == 'kingspeak' ) then
source /uufs/chpc.utah.edu/sys/pkg/pgi/std_rh6/etc/pgi.csh 
#Sources pgi compilers that work with RED HAT 6 
source /uufs/kingspeak.peaks/sys/pkg/mvapich2/std_pgi/etc/mvapich2.csh 
#Sources new MPI for Kingspeak server 
setenv NETCDF /uufs/chpc.utah.edu/sys/pkg/netcdf/4.0pg 
setenv NCARG_ROOT /uufs/chpc.utah.edu/sys/pkg/ncarg/5.1.1 
setenv WRF_EM_CORE 1
endif

cp configure.wrf.kingspeak configure.wrf

./compile em_real >& compile.txt

echo 'FINISHED COMPILING THE WRF'

endif

#copy the correct met_em_files and executables to output dir

#get met_ems to outputfolder, may need to change this path
cd $METEMDIR'/'${event}
cp * $outputfolder
cd $outputfolder
rename .NC .nc *.NC

cd $WRFDIR
cp basescript2.pbs $outputfolder
cp namelist.input $outputfolder
cd $outputfolder
ln -s /uufs/chpc.utah.edu/common/home/steenburgh-group2/leah/wrf/WRFV3_king/main/real.exe .
ln -s /uufs/chpc.utah.edu/common/home/steenburgh-group2/leah/wrf/WRFV3_king/main/wrf.exe .
ln -s /uufs/chpc.utah.edu/common/home/steenburgh-group2/leah/wrf/WRFV3_king/main/ndown.exe .
ln -s /uufs/chpc.utah.edu/common/home/steenburgh-group2/leah/wrf/WRFV3_king/run/*.TBL .
ln -s /uufs/chpc.utah.edu/common/home/steenburgh-group2/leah/wrf/WRFV3_king/run/RRTM* .
ln -s /uufs/chpc.utah.edu/common/home/steenburgh-group2/leah/wrf/WRFV3_king/run/ozone* .
ln -s /uufs/chpc.utah.edu/common/home/steenburgh-group2/leah/wrf/WRFV3_king/run/grib* .
ln -s /uufs/chpc.utah.edu/common/home/steenburgh-group2/leah/wrf/WRFV3_king/run/ETA* .
ln -s /uufs/chpc.utah.edu/common/home/steenburgh-group2/leah/wrf/WRFV3_king/run/CAM* .
echo 'FINISHED LINKING FILES TO OUTPUT DIRECTORY'

#run real.exe
echo 'RUNNING REAL.EXE' 
./real.exe

# set up the pbs script to run wrf on ember or kingspeak cluster

set var11a = 'NAMEOFRUN'
set var11b = ${event}
set var12a = 'WALL_TIME'
set var12b = ${walltime}
sed 's/'"$var12a"'/'"$var12b"'/g \
s/'"$var11a"'/'"$var11b"'/g' basescript.pbs.sample > script.pbs

# run the wrf on atmos
if ( $machine == 'atmos04' ) then
screen
mpiexec -n 12 ./wrf.exe
echo 'WRF RUN IS SUBMITTED ON ATMOS'
endif

# run the wrf on ember
if ( $machine == 'ember' ) then
qsub script.pbs
echo 'WRF RUN IS SUBMITTED ON EMBER'
endif

# run the wrf on kingspeak
if ( $machine == 'kingspeak' ) then
qsub script.pbs
echo 'WRF RUN IS SUBMITTED ON KINGSPEAK'
endif

#echo 'STARTING NEXT RUN'

@ runnum = ($runnum + 1)

end
