#!/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: RunL96_2scale,v 1.5 2005/06/04 20:21:51 thoar Exp $
# $Source: /home/thoar/CVS.REPOS/DART/shell_scripts/RunL96_2scale,v $
# $Name: pre_iceland $
#
# script to run an experiment with existing intial conditions

#======================================================================
# Find the project and build it, if need be.
#======================================================================

set SNAME = $0
set clobber
set DART_HOME = ~/DART
set Model = lorenz_96_2scale

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 = 0.1
if ( ${?ObsError} ) then
   set obs_error_variance = $ObsError
endif

set num_obs = 360
set num_periods = 24
set ob_day_1 = 0
set ob_seconds_1 = 43200

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.1
set cutoff = 400.0
set         cutoff = 0.20
set    cov_inflate = 1.05
set cov_inflate_sd = 0.05
set sd_lower_bound = 0.05
set          async = 0

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

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 = 1

#======================================================================
# 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
if !( -e integrate_model ) then
   csh mkmf_integrate_model
   make || exit 4
   \rm -f input.nml.integrate_model_default
endif
\rm -f *.o *.d *.mod Makefile

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

echo '&assim_model_nml'                           > assim_mod.nml
echo '    read_binary_restart_files = .false. ,' >> assim_mod.nml
echo '   write_binary_restart_files = .false. /' >> assim_mod.nml

echo '&model_nml'                           > model.nml
echo '   model_size_x = 36,'               >> model.nml
echo '   y_per_x      = 10,'               >> model.nml
echo '   forcing      = 10.00,'            >> model.nml
echo '   delta_t      =  0.005,'           >> model.nml
echo '   coupling_b   = 10.0,'             >> model.nml
echo '   coupling_c   = 10.0,'             >> model.nml
echo '   coupling_h   =  1.0,'             >> model.nml
echo '   output_state_vector = .false.,'   >> model.nml
echo '   local_y             = .false.  /' >> 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.

set startobs = -37
set statevar = $startobs

set COS = create_obs_sequence.in
@ maxobs = $num_obs + 1

echo "$maxobs"                       > $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 ($statevar + $num_obs > $startobs )
   echo "1"                         >> $COS ;# anything but -1 to continue ...
   echo $statevar                   >> $COS ;# observing state variable [-1 ... -9]
   echo "0  0"                      >> $COS ;# obs time (days, seconds)
   echo $obs_error_variance         >> $COS ;# rather obvious
   @ statevar--
end
echo "-1"                           >> $COS ;# -1 to exit ...
echo "set_def.out"                  >> $COS ;# output file name

cat 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_periods            >> $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
 
cat 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
   start_from_restart = .true.,
   output_restart = .true.  / 
ENDofTask03

echo " "                                                          > assim_tools.nml
echo "&assim_tools_nml"                                          >> assim_tools.nml
echo "   filter_kind = 1,"                                       >> assim_tools.nml
echo "   cutoff = $cutoff ,"                                     >> assim_tools.nml
echo "   sort_obs_inc = .false."                                 >> assim_tools.nml
echo "   cov_inflate = $cov_inflate ,"                           >> assim_tools.nml
echo "   cov_inflate_sd = $cov_inflate_sd ,"                     >> assim_tools.nml
echo "   sd_lower_bound = $sd_lower_bound ,"                     >> assim_tools.nml
echo "   deterministic_cov_inflate = .true. ,"                   >> assim_tools.nml
echo "   start_from_assim_restart = .false. ,"                   >> assim_tools.nml
echo "   assim_restart_in_file_name = 'assim_tools_ics',"        >> assim_tools.nml
echo "   assim_restart_out_file_name = 'assim_tools_restart',"   >> assim_tools.nml
echo "   do_parallel = 0,"                                       >> assim_tools.nml
echo "   num_domains = 1  /"                                     >> assim_tools.nml
echo " "                                                         >> assim_tools.nml

cat perfect.nml assim_tools.nml assim_mod.nml model.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
#======================================================================

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

echo " "                                                          > filter.nml
echo "&filter_nml"                                               >> filter.nml
echo "   async = $async ,"                                       >> filter.nml
echo "   ens_size = $ens_size ,"                                 >> 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 filter.nml assim_tools.nml assim_mod.nml model.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 assim_mod.nml model.nml perfect.nml filter.nml input.nml
   \rm -f assim_tools.nml go_end_filter
endif

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

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

exit
