#!/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: RunL04,v 1.4 2005/02/26 06:14:27 thoar Exp $
# $Source: /home/thoar/CVS.REPOS/DART/shell_scripts/RunL04,v $
# $Name: hawaii $
#
# script to run an experiment with existing intial conditions
# Interesting case #1 (filter divergence)
# N=20, cov_inflate = 1.0, cutoff=1000000.0, 200x12hours
# RERUN cov_inflate = 1.05
# RERUN cov_inflate = 1.05, cutoff = 0.2
#======================================================================
# Find the project and build it, if need be.
#======================================================================

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

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 = 100
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.00
set cutoff = 1000000.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 "   model_size = 960,"                 >> model.nml
echo "   forcing = 15.00,"                  >> model.nml
echo "   delta_t = 0.001,"                  >> model.nml
echo "   space_time_scale = 10.0,"          >> model.nml
echo "   coupling = 3.0,"                   >> model.nml
echo "   K =  32,"                          >> model.nml
echo "   smooth_steps = 12,"                >> model.nml
echo "   time_step_days = 0,"               >> model.nml
echo "   time_step_seconds = 3600   / "     >> model.nml

#======================================================================
# Simulate a particular observing system:
# 1) declare an observation set definition
# For a model size of 960 ... I am just scattering them about. 
# there is a perl script in models/lorenz_04/shell_scripts   that 
# is a bit more flexible.
#======================================================================

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

set COS = create_obs_sequence.in

echo "4"                   > $COS  ;# upper bound on number of observations in sequence
echo "0"                  >> $COS  ;# number of copies of data (0 for just a definition)
echo "0"                  >> $COS  ;# number of quality control values per field
echo "1"                  >> $COS  ;# anything (but -1) to continue ---------
echo "-1"                 >> $COS  ;# -1 == state variable 1                |
echo "0 0"                >> $COS  ;# obs time (days, seconds)              |
echo $obs_error_variance  >> $COS  ;# error variance for set 1, obs 1       |
echo "1"                  >> $COS  ;# -1 == no more observations ------------
echo "-300"               >> $COS  ;# -300 == observe state variable 300    |
echo "0 0"                >> $COS  ;# obs time (days, seconds)              |
echo $obs_error_variance  >> $COS  ;# error variance                        |
echo "1"                  >> $COS  ;# -1 == no more observations ------------
echo "-600"               >> $COS  ;# -600 == observe state variable 600    |
echo "0 0"                >> $COS  ;# obs time (days, seconds)              |
echo $obs_error_variance  >> $COS  ;# error variance                        |
echo "-1"                 >> $COS  ;# -1 == no more observations ------------
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

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