#!/bin/csh
#
# Data Assimilation Research Testbed -- DART
# Copyright 2004, 2005, Data Assimilation Initiative, University Corporation for Atmospheric Research
# Licensed under the GPL -- www.gpl.org/licenses/gpl.html
#
# <next three lines automatically updated by CVS, do not edit>
# $Id: Run9var,v 1.14 2005/02/26 06:14:27 thoar Exp $
# $Source: /home/thoar/CVS.REPOS/DART/shell_scripts/Run9var,v $
# $Name: hawaii $
#
# script to run an experiment with existing intial conditions
# Interesting case #1
# identity obs for all 9 state vars, var = 1.0, obdelta =3600s
#     state vars 1,2,3  --> filter divergence
#     rerun with cov_inflate = 1.01, 1.05, 0.99 ...
#======================================================================
# Find the project and build it, if need be.
#======================================================================

set SNAME = $0
set clobber
set DART_HOME = ~/DART
set Model = 9var

switch ( $#argv )
   case 1:
      # supplying one argument -- the number of ensemble members
      set NumEns = $1
      breaksw
   case 2:
      # supplying two arguments
      set NumEns = $1
      set DART_HOME = $2
      breaksw
   default:
      echo " "
      echo "usage: $SNAME:t #_ensemble_members [DART_HOME]"
      echo " "
      echo "This script demonstrates how to conduct a DART assimilation experiment for"
      echo "a given model which has already been spun-up and has initial conditions"
      echo "files <perfect_ics> and <filter_ics>."
      echo " "
      echo "'#_ensemble_members' can be no greater than the number of ensemble members "
      echo "in the <filter_ics> initial conditions file."
      echo " "
      echo "'DART_HOME' must point to your 'top-level' DART directory."
      echo "If DART_HOME is not supplied -- it is assumed to be ~/DART."
      echo " "
      exit 1
      breaksw
endsw

#======================================================================
# Query all the environment variables.
# This is only meant to be illustrative.
# It is ABSOLUTELY not comprehensive.
#======================================================================

set obs_error_variance = 1.0
if ( ${?ObsError} ) then
   set obs_error_variance = $ObsError
endif

set num_obs = 4000
set ob_day_1 = 0
set ob_seconds_1 = 3600

if ( ${?NumObs} ) then     ;# if environment var NumObs exists, use it
   set num_obs = $NumObs
endif
if ( ${?ObDay1} ) then
   set ob_day_1 = $ObDay1
endif
if ( ${?ObSec1} ) then
   set ob_seconds_1 = $ObSec1
endif

set cov_inflate = 1.00
set cutoff = 22222222.0

if ( ${?CovInflate} ) then
   set cov_inflate = $CovInflate
endif
if ( ${?Cutoff} ) then
   set cutoff = $Cutoff
endif

#======================================================================
# This block checks to see if input is viable.
#======================================================================

setenv DARTDIR ${DART_HOME}/models/${Model}/work
if ( ! -d ${DARTDIR} ) then                                                     
   echo "${DARTDIR} does not exist ... "
   exit 2
endif

echo "DART project directory is $DART_HOME"
echo "Building Initial Conditions for $Model"
echo "in directory $DARTDIR"
echo "You have 4 seconds to stop this ..."
sleep 1
echo "3"
sleep 1
echo "2"
sleep 1
echo "1"
sleep 1
echo
                                                                                
cd ${DARTDIR}

#======================================================================
# Save any existing namelist ...
# Creating a null namelist just to avoid moving nothing.
#======================================================================

touch nullnamelist$$.nml
touch nullnamelist$$.nml_default

set SAVEME = .NamelistStorage.$$
if ( ! -d $SAVEME ) then
   mkdir ${SAVEME}
endif
\mv -f *.nml         ${SAVEME}
\mv -f *.nml_default ${SAVEME}

echo 'Saved all existing *.nml into directory '"$SAVEME"
echo 'Will restore and remove the directory at the end.'

#======================================================================
# Create the executables if they do not exist.
#======================================================================

if !( -e create_obs_sequence ) then
   csh mkmf_create_obs_sequence
   make || exit 1
   \rm -f input.nml.create_obs_sequence_default 
endif
if !( -e create_fixed_network_seq ) then
   csh mkmf_create_fixed_network_seq
   make || exit 2
   \rm -f input.nml.create_fixed_network_seq_default
endif
if !( -e perfect_model_obs ) then
   csh mkmf_perfect_model_obs
   make || exit 3
   \rm -f input.nml.perfect_model_obs_default
endif
if !( -e filter ) then
   csh mkmf_filter
   make || exit 4
   \rm -f input.nml.filter_default
endif
\rm -f *.o *.d *.mod Makefile

#======================================================================
# Create common namelists -- lots of ways to do it.
#======================================================================

cat << ENDofTask01 > assim_mod.nml
&assim_model_nml
   read_binary_restart_files = .false. 
   write_binary_restart_files = .false. /
ENDofTask01

echo '&model_nml'                          > model.nml
echo '   g = 8.0,'                        >> model.nml
echo '   deltat = 0.0833333333333333  /'  >> model.nml

#======================================================================
# Simulate a particular observing system:
# 1) declare an observation set definition
#======================================================================

echo " "
echo "Running create_obs_sequence ..."
echo " "

# This sequence block is not great. It will NOT handle
# arbitrary sets of state variables. You can observe
# the first N, or #N, but not 3,6,9 for example.
# For that, you must run interactively.

set num_obs_set_1 = 9
if ( ${?NumObsSet1} ) then
   set num_obs_set_1 = $NumObsSet1
endif
@ loopvar = -1

set COS = create_obs_sequence.in

echo "10"                            > $COS ;# upper bound on number of obs in sequence
echo "0"                            >> $COS ;# number of copies of data ( 0 == just define )
echo "0"                            >> $COS ;# number of qc vales per field ( 0 == just define )
while ($loopvar + $num_obs_set_1 >= 0)
   echo "1"                         >> $COS ;# anything but -1 to continue ...
   echo $loopvar                    >> $COS ;# observing state variable [-1 ... -9]
   echo "0  0"                      >> $COS ;# obs time (days, seconds)
   echo $obs_error_variance         >> $COS ;# rather obvious
   @ loopvar--
end
echo "-1"                           >> $COS ;# -1 to exit ...
echo "set_def.out"                  >> $COS ;# output file name

echo '&utilities_nml'                                  > utilities.nml
echo '   logfilename = "create_obs_sequence.out"'     >> utilities.nml
echo '   TERMLEVEL = 1   /'                           >> utilities.nml

cat utilities.nml assim_mod.nml model.nml > input.nml

./create_obs_sequence < $COS

if ( $status > 0 ) then
   echo "ERROR: running create_obs_sequence bombed ..."
   exit
endif

#======================================================================
# Simulate a particular observing system:
# 2) declare sequences of observation sets
#======================================================================

echo " "
echo "Running create_fixed_network_seq ..."
echo " "

set CFN = create_fixed_network_seq.in

echo "set_def.out"            > $CFN ;# same filename as 'output' above
echo "1"                     >> $CFN ;# make a "regular" sequence
echo $num_obs                >> $CFN ;# number of observations in set 1
echo $ob_day_1 $ob_seconds_1 >> $CFN ;# time of first observation
echo $ob_day_1 $ob_seconds_1 >> $CFN ;# observation interval
echo "obs_seq.in"            >> $CFN ;# output file name -- destined for perfect_model_obs
 
echo '&utilities_nml'                                   > utilities.nml
echo '   logfilename = "create_fixed_network_seq.out"' >> utilities.nml
echo '   TERMLEVEL = 1   /'                            >> utilities.nml

cat utilities.nml assim_mod.nml model.nml > input.nml

./create_fixed_network_seq < $CFN

if ( $status > 0 ) then
   echo "ERROR: create_fixed_network_seq bombed ..."
   exit
endif

#======================================================================
# Simulate a particular observing system
# 3) Populate the observation sequences with synthetic observations
#    creates files True_State.nc, obs_seq.out
#======================================================================

echo " "
echo "Running perfect_model_obs ..."
echo " "

cat << ENDofTask03 > perfect.nml

&perfect_model_obs_nml
   async = 0,
   obs_seq_in_file_name = "obs_seq.in",
   obs_seq_out_file_name = "obs_seq.out",
   start_from_restart = .true.,
   output_restart = .true.,
   restart_in_file_name = "perfect_ics",
   restart_out_file_name = "perfect_restart",
   init_time_days = 0,
   init_time_seconds = 0,
   output_interval = 1  /
ENDofTask03

echo '&utilities_nml'                              > utilities.nml
echo '   logfilename = "perfect_obs.out"'         >> utilities.nml
echo '   TERMLEVEL = 1  /'                        >> utilities.nml

cat utilities.nml assim_mod.nml model.nml perfect.nml > input.nml

./perfect_model_obs
 
if ( $status > 0 ) then
   echo "ERROR: perfect_model_obs (synthetic) bombed ..."
   exit
else
   echo " "
   echo "perfect_model_obs (synthetic) terminated normally"
   echo " "
endif

#======================================================================
# Simulate a particular observing system
# 4) Filter
#    creates files Prior_Diag.nc, Posterior_Diag.nc
#======================================================================
# Set defaults
#======================================================================

echo " "
echo "Running filter ..."
echo " "

set async = 0
set ens_size = $NumEns
set start_from_restart = .true.
set output_restart = .true.
set obs_sequence_in_name = '"obs_seq.out"'
set obs_sequence_out_name = '"obs_seq.final"'
set restart_in_file_name = '"filter_ics"'
set restart_out_file_name = '"filter_restart"'
set init_time_days = 0
set init_time_seconds = 0
set output_state_ens_mean = .true.
set output_state_ens_spread = .true.
set output_obs_ens_mean = .true.
set output_obs_ens_spread = .true.
set num_output_state_members = $NumEns
set num_output_obs_members = $NumEns
set output_interval = 1
set num_groups = 2


echo '&utilities_nml'                            > utilities.nml
echo '   logfilename = "filter.out"'            >> utilities.nml
echo '   TERMLEVEL = 1   /'                     >> utilities.nml

echo " "                                                          > filter.nml
echo "&filter_nml"                                               >> filter.nml
echo "   async = $async ,"                                       >> filter.nml
echo "   ens_size = $ens_size ,"                                 >> filter.nml
echo "   cutoff = $cutoff ,"                                     >> filter.nml
echo "   cov_inflate = $cov_inflate ,"                           >> filter.nml
echo "   start_from_restart = $start_from_restart ,"             >> filter.nml
echo "   output_restart = $output_restart ,"                     >> filter.nml
echo "   obs_sequence_in_name = $obs_sequence_in_name ,"         >> filter.nml
echo "   obs_sequence_out_name = $obs_sequence_out_name ,"       >> filter.nml
echo "   restart_in_file_name = $restart_in_file_name ,"         >> filter.nml
echo "   restart_out_file_name = $restart_out_file_name ,"       >> filter.nml
echo "   init_time_days = $init_time_days ,"                     >> filter.nml
echo "   init_time_seconds = $init_time_seconds ,"               >> filter.nml
echo "   output_state_ens_mean = $output_state_ens_mean ,"       >> filter.nml
echo "   output_state_ens_spread = $output_state_ens_spread ,"   >> filter.nml
echo "   output_obs_ens_mean = $output_obs_ens_mean ,"           >> filter.nml
echo "   output_obs_ens_spread = $output_obs_ens_spread ,"       >> filter.nml
echo "   num_output_state_members = $num_output_state_members ," >> filter.nml
echo "   num_output_obs_members = $num_output_obs_members ,"     >> filter.nml
echo "   output_interval = $output_interval ,"                   >> filter.nml
echo "   num_groups = $num_groups    /"                          >> filter.nml
echo " "                                                         >> filter.nml

cat utilities.nml assim_mod.nml model.nml filter.nml > input.nml

./filter

if ( $status > 0 ) then
   echo "ERROR: filter bombed ..."
else
   echo " "
   echo "filter terminated normally"
   echo " "
   \rm -f $COS $CFN set_def.out
   \rm -f utilities.nml assim_mod.nml model.nml perfect.nml filter.nml input.nml
endif

#======================================================================
# Restore the existing namelists ...
#======================================================================

\mv -f ${SAVEME}/*.nml .
\mv -f ${SAVEME}/*default .
\rmdir ${SAVEME}
\rm -f nullnamelist$$.nml nullnamelist$$.nml_default

exit
