#!/usr/bin/env perl
#-----------------------------------------------------------------------------------------------
#
# build-namelist
#
# This script builds the namelists for the MPASLI configuration of ACME.
#
# build-namelist uses a config_cache.xml file that current contains the landice grid information.
# build-namelist reads this file to obtain information it needs to provide
# default values that are consistent with the MPASLI XML file.  For example, the grid resolution
# is obtained from the cache file and used to determine appropriate defaults for namelist input
# that is resolution dependent.
#
# The simplest use of build-namelist is to execute it from the build directory where configure
# was run.  By default it will use the config_cache.xml file that was written by configure to
# determine the build time properties of the executable, and will write the files that contain 
# the output namelists in that same directory.
#
#
# Date        Contributor      Modification
# -------------------------------------------------------------------------------------------
# 2015-03-16  mhoffman         Original version
#--------------------------------------------------------------------------------------------
use strict;
use Cwd qw(getcwd abs_path);
use English;
use Getopt::Long;
use IO::File;

#-----------------------------------------------------------------------------------------------

sub usage {
    die <<EOF;
SYNOPSIS
     build-namelist [options]
OPTIONS
     -infile "filepath"    Specify a file containing namelists to read values from.
     -namelist "namelist"  Specify namelist settings directly on the commandline by supplying 
                           a string containing FORTRAN namelist syntax, e.g.,
                              -namelist "&mpas-o_nml dt=1800 /"
     -help [or -h]         Print usage to STDOUT.
     -test                 Enable checking that input datasets exist on local filesystem.
     -verbose              Turn on verbose echoing of informational messages.
     -caseroot             CASEROOT directory variable
     -casebuild            CASEBUILD directory variable          
     -scriptsroot          SCRIPTSROOT directory variable
     -glc_grid             GLC_GRID variable
     -date_stamp           date_stamp variable
     -cfg_grid             Directory containing MPASLI configuration scripts.
                           If not defined, location is set as \$ProgDir or \$cwd
                           (Needed to run build-namelist from SourceMods dir)
     -inst_string          inst_string variable


NOTE: The precedence for setting the values of namelist variables is (highest to lowest):
      1. namelist values set by specific command-line options, i.e. (none right now)
      2. values set on the command-line using the -namelist option,
      3. values read from the file specified by -infile,
      4. values from the namelist defaults file - or values specifically set in build-namelist 
EOF
}

#-----------------------------------------------------------------------------------------------
# Set the directory that contains the MPASLI configuration scripts.  If the command was
# issued using a relative or absolute path, that path is in $ProgDir.  Otherwise assume the
# command was issued from the current working directory.

(my $ProgName = $0) =~ s!(.*)/!!;      # name of this script
my $ProgDir = $1;                      # name of directory containing this script -- may be a
                                       # relative or absolute path, or null if the script is in
                                       # the user's PATH
my $cwd = getcwd();                    # current working directory
my $cfgdir;                            # absolute pathname of directory that contains this script
if ($ProgDir) { 
    $cfgdir = abs_path($ProgDir);
} else {
    $cfgdir = $cwd;
}

#-----------------------------------------------------------------------------------------------

# Process command-line options.

my %opts = ( help        => 0,
             test        => 0,
             verbose     => 0,
             preview     => 0,
             caseroot    => undef,
             casebuild   => undef,
             scriptsroot => undef,
             inst_string => undef,
             glc_grid    => undef,
             date_stamp  => undef,
             cfg_dir     => $cfgdir,
           );

GetOptions(
    "h|help"        => \$opts{'help'},
    "infile=s"      => \$opts{'infile'},
    "namelist=s"    => \$opts{'namelist'},
    "v|verbose"     => \$opts{'verbose'},
    "caseroot=s"    => \$opts{'caseroot'},
    "casebuild=s"   => \$opts{'casebuild'},
    "scriptsroot=s" => \$opts{'scriptsroot'},
    "inst_string=s" => \$opts{'inst_string'},	   
    "glc_grid=s"    => \$opts{'glc_grid'},
    "date_stamp=s"  => \$opts{'date_stamp'},
    "cfg_dir=s"     => \$opts{'cfg_dir'},
    "preview"       => \$opts{'preview'},
)  or usage();

# Give usage message.
usage() if $opts{'help'};

# Check for unparsed arguments
if (@ARGV) {
    print "ERROR: unrecognized arguments: @ARGV\n";
    usage();
}

# Define print levels:
# 0 - only issue fatal error messages
# 1 - only informs what files are created (currently not used)
# 2 - verbose
my $print = 0;
my $preview = 0;
if ($opts{'verbose'}) { $print = 2; }
if ($opts{'preview'}) { $preview = 1; }
my $eol = "\n";

if ($print>=2) { print "Setting MPASLI configuration script directory to $cfgdir$eol"; }

my $CASEROOT    = $opts{'caseroot'};
my $CASEBUILD   = $opts{'casebuild'};
my $SCRIPTSROOT = $opts{'scriptsroot'};
my $inst_string = $opts{'inst_string'};
my $GLC_GRID    = $opts{'glc_grid'};
my $date_stamp  = $opts{'date_stamp'};
$cfgdir         = $opts{'cfg_dir'};

# Validate some of the commandline option values.
validate_options("commandline", \%opts);


# build config_cache.xml file (needed below)
my $config_cache = "${CASEBUILD}/mpasliconf/config_cache.xml";
my  $fh = new IO::File;
$fh->open(">$config_cache") or die "** can't open file: $config_cache\n";
print $fh  <<"EOF";
<?xml version="1.0"?>
<config_definition>
<entry id="glc_grid" value="$GLC_GRID">
<entry id="date_stamp" value="$date_stamp">
</config_definition>
EOF
$fh->close;
if ($print>=2) { print "Wrote file $config_cache $eol"; }
(-f "config_cache.xml")  or  die <<"EOF";
** $ProgName - Cannot find configuration cache file: config_cache.xml\" **
EOF

#-----------------------------------------------------------------------------------------------
# Make sure we can find required perl modules, definition, and defaults files.
# Look for them under the directory that contains the configure script.

# The root directory for the input data files must be specified.
my $cesmroot = abs_path("$SCRIPTSROOT/../../");
if (! -d "$cesmroot") {
    die "** Invalid CESM root directory: $cesmroot ** ";
}

print "CESM ROOT IS: $cesmroot\n";

my $perl5lib = "$cesmroot/cime/utils/perl5lib";
if (! -d "$perl5lib") {
    die "** Invalid perl5lib root directory: $perl5lib ** ";
}

# The Build::Config module provides utilities to access the configuration information
# in the config_cache.xml file
(-f "$perl5lib/Build/Config.pm")  or  die <<"EOF";
** $ProgName - Cannot find perl module \"Build/Config.pm\" in directory \"$perl5lib\" **
EOF

# The Build::NamelistDefinition module provides utilities to validate that the output
# namelists are consistent with the namelist definition file
(-f "$perl5lib/Build/NamelistDefinition.pm")  or  die <<"EOF";
** $ProgName - Cannot find perl module \"Build/NamelistDefinition.pm\" in directory \"$perl5lib\" **
EOF

# The Build::NamelistDefaults module provides a utility to obtain default values of namelist
# variables based on finding a best fit with the attributes specified in the defaults file.
(-f "$perl5lib/Build/NamelistDefaults.pm")  or  die <<"EOF";
** $ProgName - Cannot find perl module \"Build/NamelistDefaults.pm\" in directory \"$perl5lib\" **
EOF

# The Build::Namelist module provides utilities to parse input namelists, to query and modify
# namelists, and to write output namelists.
(-f "$perl5lib/Build/Namelist.pm")  or  die <<"EOF";
** $ProgName - Cannot find perl module \"Build/Namelist.pm\" in directory \"$perl5lib\" **
EOF

# The namelist definition file contains entries for all namelist variables that
# can be output by build-namelist.  The version of the file that is associate with a
# fixed MPASLI tag is $cfgdir/namelist_files/namelist_definition.xml.  To aid developers
# who make use of the SourceMods/src.mpasli directory - we allow the definition file 
# to come from that directory
my $nl_definition_file;
if (-f "${CASEROOT}/SourceMods/src.mpasli/namelist_definition_mpasli.xml") {
    $nl_definition_file = "${CASEROOT}/SourceMods/src.mpasli/namelist_definition_mpasli.xml";
}
if (! defined $nl_definition_file) {
    # default location of namelist definition file
    $nl_definition_file = "$cfgdir/namelist_files/namelist_definition_mpasli.xml";
    (-f "$nl_definition_file")  or  die <<"EOF";
    ** $ProgName - ERROR: Cannot find namelist definition file \"$nl_definition_file\" **
EOF
}
if ($print>=2) { print "Using namelist definition file $nl_definition_file$eol"; }

# The namelist defaults file contains default values for all required namelist variables.
my $nl_defaults_file;
if (-f "${CASEROOT}/SourceMods/src.mpasli/namelist_defaults_mpasli.xml") {
    $nl_defaults_file = "${CASEROOT}/SourceMods/src.mpasli/namelist_defaults_mpasli.xml";
}
if (! defined $nl_defaults_file) {
    $nl_defaults_file = "$cfgdir/namelist_files/namelist_defaults_mpasli.xml";
    (-f "$nl_defaults_file")  or  die <<"EOF";
    ** $ProgName - Cannot find namelist defaults file \"$nl_defaults_file\" **
EOF
}
if ($print>=2) { print "Using namelist defaults file $nl_defaults_file$eol"; }

#-----------------------------------------------------------------------------------------------
# Add $perl5lib_dir to the list of paths that Perl searches for modules
unshift @INC, "$perl5lib", "$CASEROOT/Tools", "$cesmroot/cime/scripts/Tools";
require Build::Config;
require Build::NamelistDefinition;
require Build::NamelistDefaults;
require Build::Namelist;
require SetupTools;

#-----------------------------------------------------------------------------------------------
# Create a configuration object from the MPASLI config_cache.xml file-  created by 
# mpasli.cpl7.template in $CASEBUILD/mpasliconf
my $cfg = Build::Config->new('config_cache.xml');

# Create a namelist definition object.  This object provides a method for verifying that the
# output namelist variables are in the definition file, and are output in the correct
# namelist groups.
my $definition = Build::NamelistDefinition->new($nl_definition_file);

# Create a namelist defaults object.  This object provides default values for variables
# contained in the input defaults file.  The configuration object provides attribute
# values that are relevent for the MPASLI library for which the namelist is being produced.
my $defaults = Build::NamelistDefaults->new($nl_defaults_file, $cfg);

# Create an empty namelist object.  Add values to it in order of precedence.
my $nl = Build::Namelist->new();

#-----------------------------------------------------------------------------------------------
# Process the user input in order of precedence.  At each point we'll only add new
# values to the namelist and not overwrite previously specified specified values which
# have higher precedence.

# Process the commandline args that provide specific namelist values.

# Process the -namelist arg.
if (defined $opts{'namelist'}) {
    # Parse commandline namelist
    my $nl_arg = Build::Namelist->new($opts{'namelist'});

    # Validate input namelist -- trap exceptions
    my $nl_arg_valid;
    eval { $nl_arg_valid = $definition->validate($nl_arg); };
    if ($@) {
      die "$ProgName - ERROR: Invalid namelist variable in commandline arg '-namelist'.\n $@";
    }

    # Merge input values into namelist.  Previously specified values have higher precedence
    # and are not overwritten.
    $nl->merge_nl($nl_arg_valid);
}

# Process the -infile arg.
if (defined $opts{'infile'}) {
    # Parse namelist input from a file
    my $nl_infile = Build::Namelist->new($opts{'infile'});
    my $nl_infile_valid = Build::Namelist->new();

    # Validate namelist variables (going to do this one variable at a time)
    for my $group ($nl_infile->get_group_names()) {
      for my $var ($nl_infile->get_variable_names($group)) {
        my $var_local; # Name of variable to write to infile
        my $nl_check_var = Build::Namelist->new();
        my $nl_check_valid;
        my $val = $nl_infile->get_variable_value($group, $var);
        my @broken = split(/&/,$var);
        my $check_grp = 0; # If 1, make sure group found in definitions file
                           # matches that specified in user_nl_mpasli

        # if variable has ampersand, truncate it unless it is type derived
        if ($broken[1]) {
          my $nl_check_amp = Build::Namelist->new();
          $nl_check_amp->set_variable_value($group, $var, $val);
          eval { $definition->validate($nl_check_amp) };
          if (not $@) {
            # & is required in variable name
            $var_local = $var;
          } else {
            # & should not be in variable name
            $var_local = $broken[0];
            $check_grp = 1;
          }
        } else {
          $var_local = $var;
        }

        # Make sure variable is defined in namelist_definition_mpasli.xml
        $nl_check_var->set_variable_value($group, $var_local,$val);
        eval { $nl_check_valid = $definition->validate($nl_check_var); };
        (not $@) or die <<"EOF";
** ERROR: either $var_local is not a valid MPASLI namelist variable or $var_local = $val is not a valid value; please fix user_nl_mpasli. Note that $var_local may appear in multiple namelists, in which case you need to specify the correct namelist in user_nl_mpasli using the format $var_local\&namelist_nml = $val, where \&namelist_nml is the mpasli_in namelist containing $var_local.**
EOF

        # If group was specified in user_nl_mpasli, make sure it matches
        # the group in the definitions file.
        my @group_valid = $nl_check_valid->get_group_names();
        ((not $check_grp) or ($broken[1] eq $group_valid[0])) or die <<"EOF";
** ERROR: $broken[0] is in $group_valid[0], not $broken[1]! Please fix this in user_nl_mpasli. **
EOF

        # Add variable to validated namelist
        $nl_infile_valid->set_variable_value($group_valid[0], $var_local, $val);
      }
    }

    # If preview is desired and something has been changed in $nl_infile_valid,
    # output everything in $nl_infile_valid
    if (($preview == 1) && ($nl_infile_valid->get_group_names)) {
      print " - The following values have been set in user_nl_mpasli:\n";
      print_nl_to_screen($nl_infile_valid);
    }
    # Merge input values into namelist.  Previously specified values have higher
    # precedence and are not overwritten.
    $nl->merge_nl($nl_infile_valid);
}

#-----------------------------------------------------------------------------------------------
my %xmlvars = ();
SetupTools::getxmlvars($CASEROOT, \%xmlvars);
foreach my $attr (keys %xmlvars) {
  $xmlvars{$attr} = SetupTools::expand_xml_var($xmlvars{$attr}, \%xmlvars);
}

my $RUNDIR                 = "$xmlvars{'RUNDIR'}";
my $CODEROOT               = "$xmlvars{'CODEROOT'}";
my $DIN_LOC_ROOT           = "$xmlvars{'DIN_LOC_ROOT'}";
my $CASE                   = "$xmlvars{'CASE'}";
my $CALENDAR               = "$xmlvars{'CALENDAR'}";
my $NCPL_BASE_PERIOD       = "$xmlvars{'NCPL_BASE_PERIOD'}";
my $GLC_NCPL               = "$xmlvars{'GLC_NCPL'}";
my $GLC_COUPLING           = "$xmlvars{'GLC_COUPLING'}";
my $NTASKS_GLC             = "$xmlvars{'NTASKS_GLC'}";
my $NINST_GLC              = "$xmlvars{'NINST_GLC'}";
my $INFO_DBUG              = "$xmlvars{'INFO_DBUG'}";
my $RUN_TYPE               = "$xmlvars{'RUN_TYPE'}";
my $RUN_STARTDATE          = "$xmlvars{'RUN_STARTDATE'}";
my $START_TOD              = "$xmlvars{'START_TOD'}";
my $RUN_REFDATE            = "$xmlvars{'RUN_REFDATE'}";
my $CONTINUE_RUN           = "$xmlvars{'CONTINUE_RUN'}";
my $MPASLI_USE_ALBANY      = "$xmlvars{'MPASLI_USE_ALBANY'}";

my $output_r = "./${CASE}.mpasli.r";
my $output_h = "./${CASE}.mpasli.h";
my $output_d = "./${CASE}.mpasli.d";
if ($inst_string) {
    $output_r = "./${CASE}.mpasli${inst_string}.r";
    $output_h = "./${CASE}.mpasli${inst_string}.h";
    $output_d = "./${CASE}.mpasli${inst_string}.d";
} 

# Environment variables set in mpasli.buildnml.csh that are not xml variables
my $RESTART_INPUT_TS_FMT = "$ENV{'RESTART_INPUT_TS_FMT'}"; 
my $LID = $ENV{'LID'};

my $ntasks = $NTASKS_GLC / $NINST_GLC; 

if ($CONTINUE_RUN eq 'TRUE') {$RUN_TYPE = "continue";}

print "MPASLI build-namelist: glc_grid is $GLC_GRID \n";

(-d $DIN_LOC_ROOT)  or mkdir $DIN_LOC_ROOT;
if ($print>=2) { print "CESM inputdata root directory: $DIN_LOC_ROOT$eol"; }

#-----------------------------------------------------------------------------------------------
# Determine namelist
#-----------------------------------------------------------------------------------------------

if ($MPASLI_USE_ALBANY eq 'TRUE') {
    print "Setting up MPASLI namelist to use Albany.\n"; }

###################################
# Namelist group: velocity_solver #
###################################

add_default($nl, 'config_velocity_solver', 'mpasli_use_albany'=>"$MPASLI_USE_ALBANY");
add_default($nl, 'config_sia_tangent_slope_calculation');
add_default($nl, 'config_flowParamA_calculation');
add_default($nl, 'config_do_velocity_reconstruction_for_external_dycore');
add_default($nl, 'config_simple_velocity_type');

#############################
# Namelist group: advection #
#############################

add_default($nl, 'config_thickness_advection');
add_default($nl, 'config_tracer_advection');

###########################
# Namelist group: calving #
###########################

add_default($nl, 'config_calving');
add_default($nl, 'config_calving_topography');
add_default($nl, 'config_calving_thickness');
add_default($nl, 'config_data_calving');
add_default($nl, 'config_calving_timescale');
add_default($nl, 'config_restore_calving_front');

##################################
# Namelist group: thermal_solver #
##################################

add_default($nl, 'config_thermal_solver');
add_default($nl, 'config_temperature_init');
add_default($nl, 'config_thermal_thickness');
add_default($nl, 'config_surface_air_temperature_source');
add_default($nl, 'config_surface_air_temperature_value');
add_default($nl, 'config_basal_heat_flux_source');
add_default($nl, 'config_basal_heat_flux_value');
add_default($nl, 'config_basal_mass_bal_float');

#######################################
# Namelist group: physical_parameters #
#######################################

add_default($nl, 'config_ice_density');
add_default($nl, 'config_ocean_density');
add_default($nl, 'config_sea_level');
add_default($nl, 'config_default_flowParamA');
add_default($nl, 'config_enhancementFactor');
add_default($nl, 'config_flowLawExponent');
add_default($nl, 'config_dynamic_thickness');

####################################
# Namelist group: time_integration #
####################################

add_default($nl, 'config_dt');
add_default($nl, 'config_time_integration');
add_default($nl, 'config_adaptive_timestep');
add_default($nl, 'config_min_adaptive_timestep');
add_default($nl, 'config_max_adaptive_timestep');
add_default($nl, 'config_adaptive_timestep_CFL_fraction');
add_default($nl, 'config_adaptive_timestep_include_DCFL');
add_default($nl, 'config_adaptive_timestep_force_interval');

###################################
# Namelist group: time_management #
###################################

if ($CONTINUE_RUN eq 'TRUE') {
	add_default($nl, 'config_do_restart', 'val'=>".true.");
} else {
	add_default($nl, 'config_do_restart', 'val'=>".false.");
}
add_default($nl, 'config_restart_timestamp_name');
add_default($nl, 'config_calendar_type', 'calendar'=>"$CALENDAR");
if ($CONTINUE_RUN eq 'TRUE') {
	add_default($nl, 'config_start_time', 'val'=>"'file'");
} else {
	add_default($nl, 'config_start_time', 'val'=>"'${RUN_STARTDATE}_${START_TOD}'");
}

######################
# Namelist group: io #
######################

add_default($nl, 'config_stats_interval');
add_default($nl, 'config_write_stats_on_startup');
add_default($nl, 'config_stats_cell_ID');
add_default($nl, 'config_write_output_on_startup');
add_default($nl, 'config_pio_num_iotasks');
add_default($nl, 'config_pio_stride');
add_default($nl, 'config_year_digits');
add_default($nl, 'config_output_external_velocity_solver_data');

#################################
# Namelist group: decomposition #
#################################

add_default($nl, 'config_num_halos');
add_default($nl, 'config_block_decomp_file_prefix', 'val'=>"'${DIN_LOC_ROOT}/glc/mpasli/${GLC_GRID}/mpasli.graph.info.${date_stamp}.part.'");
add_default($nl, 'config_number_of_blocks');
add_default($nl, 'config_explicit_proc_decomp');
add_default($nl, 'config_proc_decomp_file_prefix');

#########################
# Namelist group: debug #
#########################

add_default($nl, 'config_print_thickness_advection_info');
add_default($nl, 'config_print_calving_info');
add_default($nl, 'config_print_thermal_info');
add_default($nl, 'config_always_compute_fem_grid');
add_default($nl, 'config_print_velocity_cleanup_details');

##################################
# Namelist group: AM_globalStats #
##################################

add_default($nl, 'config_AM_globalStats_enable');
add_default($nl, 'config_AM_globalStats_compute_interval');
add_default($nl, 'config_AM_globalStats_stream_name');
add_default($nl, 'config_AM_globalStats_compute_on_startup');
add_default($nl, 'config_AM_globalStats_write_on_startup');


#-----------------------------------------------------------------------------------------------
# *** Write output namelist file (mpasli_in) and input dataset list (mpasli.input_data_list) ***
#-----------------------------------------------------------------------------------------------
# Set namelist groups to be written out

my @groups = qw(velocity_solver
                advection
                calving
                thermal_solver
                physical_parameters
                time_integration
                time_management
                io
                decomposition
                debug
                am_globalstats
                );

# Check for variables in the "derived" group, add them to appropriate group
for my $var ($nl->get_variable_names('derived')) {
  my @broken = split(/&/,$var);
  my $val = $nl->get_variable_value('derived', $var);
  $nl->set_variable_value($broken[1], $broken[0], $val);
}

# Write out all groups  to mpasli_in
my $outfile = "./mpasli_in";
$nl->write($outfile, 'groups'=>\@groups);
if ($print>=2) { print "Writing mpasli glc component namelist to $outfile $eol"; }

# Write input dataset list.
check_input_files($DIN_LOC_ROOT, "../mpasli.input_data_list");

#-----------------------------------------------------------------------------------------------
# END OF MAIN SCRIPT
#===============================================================================================

#===============================================================================================
sub add_default {

# Add a value for the specified variable to the specified namelist object.  The variables
# already in the object have the higher precedence, so if the specified variable is already
# defined in the object then don't overwrite it, just return.
#
# This method checks the definition file and adds the variable to the correct
# namelist group.
#
# The value can be provided by using the optional argument key 'val' in the
# calling list.  Otherwise a default value is obtained from the namelist
# defaults object.  If no default value is found this method throws an exception
# unless the 'nofail' option is set true.
#
# Additional optional keyword=>value pairs may be specified.  If the keyword 'val' is
# not present, then any other keyword=>value pairs that are specified will be used to
# match attributes in the defaults file.
#
# Example 1: Specify the default value $val for the namelist variable $var in namelist
#            object $nl:
#
#  add_default($nl, $var, 'val'=>$val)
#
# Example 2: Add a default for variable $var if an appropriate value is found.  Otherwise
#            don't add the variable
#
#  add_default($nl, $var, 'nofail'=>1)
#
#
# ***** N.B. ***** This routine assumes the following variables are in package main::
#  $definition        -- the namelist definition object
#  $DIN_LOC_ROOT -- CCSM inputdata root directory

    my $nl = shift;     # namelist object
    my $var = shift;    # name of namelist variable
    my %opts = @_;      # options

    my $val = undef;

    # Query the definition to find which group the variable belongs to.  Exit if not found.
    my $group = $definition->get_group_name($var);
    unless ($group) {
      my $fname = $definition->get_file_name();
      die "$ProgName - ERROR: variable \"$var\" not found in namelist definition file $fname.\n";
    }

    # check whether the variable has a value in the namelist object -- if so then return
    $val = $nl->get_variable_value($group, $var);
    if (defined $val) { return; }

    # Look for a specified value in the options hash
    if (defined $opts{'val'}) {
      $val = $opts{'val'};
    }
    # or else get a value from namelist defaults object.
    # Note that if the 'val' key isn't in the hash, then just pass anything else
    # in %opts to the get_value method to be used as attributes that are matched
    # when looking for default values.
    else {
      $val = get_default_value($var, \%opts);
    }

    # if no value is found then exit w/ error (unless 'nofail' option set)
    unless (defined $val) {
      unless ($opts{'nofail'}) {
        print "$ProgName - ERROR: No default value found for $var\n".
              "user defined attributes:\n";
        foreach my $key (keys(%opts)) {
          if ($key ne 'nofail' and $key ne 'val') {
            print "key=$key  val=$opts{$key}\n";
          }
        }
        die;
      } else {
        return;
      }
    }

    # query the definition to find out if the variable is an input pathname
    my $is_input_pathname = $definition->is_input_pathname($var);

    # The default values for input pathnames are relative.  If the namelist
    # variable is defined to be an absolute pathname, then prepend
    # the CCSM inputdata root directory.
    # TODO: unless ignore_abs is passed as argument 
    if ($is_input_pathname eq 'abs') {
      unless ($opts{'noprepend'}){
        $val = set_abs_filepath($val, $DIN_LOC_ROOT);
      }
    }

    # query the definition to find out if the variable takes a string value.
    # The returned string length will be >0 if $var is a string, and 0 if not.
    my $str_len = $definition->get_str_len($var);

    # If the variable is a string, then add quotes if they're missing
    if ($str_len > 0) {
      $val = quote_string($val);
    }

    # set the value in the namelist
    $nl->set_variable_value($group, $var, $val);
}

#-----------------------------------------------------------------------------------------------

sub get_default_value {

# Return a default value for the requested variable.
# Return undef if no default found.
#
# ***** N.B. ***** This routine assumes the following variables are in package main::
#  $defaults          -- the namelist defaults object
#  $uc_defaults       -- the use CASE defaults object

    my $var_name    = lc(shift);   # name of namelist variable (CASE insensitive interface)
    my $usr_att_ref = shift;       # reference to hash containing user supplied attributes

    # Check in the namelist defaults
    return $defaults->get_value($var_name, $usr_att_ref);

}

#-----------------------------------------------------------------------------------------------

sub check_input_files {

# For each variable in the namelist which is an input dataset, check to see if it
# exists locally.
#
# ***** N.B. ***** This routine assumes the following variables are in package main::
#  $definition        -- the namelist definition object

    my $inputdata_rootdir = shift;    # if false prints test, else creates inputdata file
    my $data_file_list = shift;
    open(my $fh, "<:encoding(UTF-8)", $data_file_list) or die "Couldn't open data file list $data_file_list";

	while (my $row = <$fh>) {
		chomp $row;
		my @split = split(' = ', $row);
		#my $input_path = $split[2]

		if (-e $split[1] ) {
			print "OK -- found $split[1]\n"
		} else {
			print "NOT FOUND: $split[1]\n"
		}
	}
    close $fh;
    return 0 if defined $inputdata_rootdir;
}

#-----------------------------------------------------------------------------------------------

sub set_abs_filepath {

# check whether the input filepath is an absolute path, and if it isn't then
# prepend a root directory

    my ($filepath, $rootdir) = @_;

    # strip any leading/trailing whitespace
    $filepath =~ s/^\s+//;
    $filepath =~ s/\s+$//;
    $rootdir  =~ s/^\s+//;
    $rootdir  =~ s/\s+$//;

    # strip any leading/trailing quotes
    $filepath =~ s/^['"]+//;
    $filepath =~ s/["']+$//;
    $rootdir =~ s/^['"]+//;
    $rootdir =~ s/["']+$//;

    my $out = $filepath;
    unless ( $filepath =~ /^\// ) {  # unless $filepath starts with a /
      $out = "$rootdir/$filepath"; # prepend the root directory
    }
    return $out;
}

#-----------------------------------------------------------------------------------------------


sub absolute_path {
#
# Convert a pathname into an absolute pathname, expanding any . or .. characters.
# Assumes pathnames refer to a local filesystem.
# Assumes the directory separator is "/".
#
  my $path = shift;
  my $cwd = getcwd();  # current working directory
  my $abspath;         # resulting absolute pathname

# Strip off any leading or trailing whitespace.  (This pattern won't match if
# there's embedded whitespace.
  $path =~ s!^\s*(\S*)\s*$!$1!;

# Convert relative to absolute path.

  if ($path =~ m!^\.$!) {          # path is "."
      return $cwd;
  } elsif ($path =~ m!^\./!) {     # path starts with "./"
      $path =~ s!^\.!$cwd!;
  } elsif ($path =~ m!^\.\.$!) {   # path is ".."
      $path = "$cwd/..";
  } elsif ($path =~ m!^\.\./!) {   # path starts with "../"
      $path = "$cwd/$path";
  } elsif ($path =~ m!^[^/]!) {    # path starts with non-slash character
      $path = "$cwd/$path";
  }

  my ($dir, @dirs2);
  my @dirs = split "/", $path, -1;   # The -1 prevents split from stripping trailing nulls
                                     # This enables correct processing of the input "/".

  # Remove any "" that are not leading.
  for (my $i=0; $i<=$#dirs; ++$i) {
      if ($i == 0 or $dirs[$i] ne "") {
        push @dirs2, $dirs[$i];
      }
  }
  @dirs = ();

  # Remove any "."
  foreach $dir (@dirs2) {
      unless ($dir eq ".") {
        push @dirs, $dir;
      }
  }
  @dirs2 = ();

  # Remove the "subdir/.." parts.
  foreach $dir (@dirs) {
    if ( $dir !~ /\.\./ ) {
        push @dirs2, $dir;
    } else {
        pop @dirs2;   # remove previous dir when current dir is ..
    }
  }
  if ($#dirs2 == 0 and $dirs2[0] eq "") { return "/"; }
  $abspath = join '/', @dirs2;
  return( $abspath );
}

#-------------------------------------------------------------------------------

sub valid_option {

    my ($val, @expect) = @_;
    my ($expect);

    $val =~ s/^\s+//;
    $val =~ s/\s+$//;
    foreach $expect (@expect) {
      if ($val =~ /^$expect$/i) { return $expect; }
    }
    return undef;
}

#-------------------------------------------------------------------------------

sub validate_options {

    my $source = shift;   # text string declaring the source of the options being validated
    my $opts   = shift;   # reference to hash that contains the options

    my ($opt, $old, @expect);

}

#-------------------------------------------------------------------------------

sub quote_string {
    my $str = shift;
    $str =~ s/^\s+//;
    $str =~ s/\s+$//;
    unless ($str =~ /^['"]/) {        #"'
        $str = "\'$str\'";
    }
    return $str;
}

#-------------------------------------------------------------------------------

sub expand_env_xml {

    my $value = shift;

    if ($value =~ /\$([\w_]+)(.*)$/) {
	my $subst = $xmlvars{$1};
	$value =~ s/\$${1}/$subst/g;
    }
    return $value; 
}	 

#-------------------------------------------------------------------------------

sub print_nl_to_screen {

  my $namelist = $_[0];
  # Loop through every group in the namelist
  for my $group ($namelist->get_group_names()) {
    # Loop through every variable in group
    for my $var ($namelist->get_variable_names($group)) {
      my $val = $namelist->get_variable_value($group, $var);
      # For derived type, $var contains variable name and group name
      if ($group eq "derived") {
        my @broken = split(/&/,$var);
        print "   * ", $broken[0], " = ", $val, " in \&", $broken[1], "\n";
      }
      else {
        print "   * ", $var, " = ", $val, " in \&", $group, "\n";
      }
    }
  }
}

#-------------------------------------------------------------------------------

sub valid_date {
# return 1 if given date ($$month/$$day/$$year) exists in calendar $cal
# otherwise subtract number of days in $$month from $$day, and increment
# $$month by 1 (also incrementing $$year if going from Dec to Jan) and 
# then return 0.

  use Switch;

  my $day = shift;
  my $month = shift;
  my $year = shift;
  my $cal = shift;

  my $maxday = -1;
  switch ($$month) {
    case 1 { $maxday = 31; }
    case 2 {
      if (($cal eq 'NO_LEAP') || (not leap($$year))) {
        $maxday = 28;
      } else {
        $maxday = 29;
      }
    }
    case 3 { $maxday = 31; }
    case 4 { $maxday = 30; }
    case 5 { $maxday = 31; }
    case 6 { $maxday = 30; }
    case 7 { $maxday = 31; }
    case 8 { $maxday = 31; }
    case 9 { $maxday = 30; }
    case 10 { $maxday = 31; }
    case 11 { $maxday = 30; }
    case 12 { $maxday = 31; }
  }
  if ($maxday == -1) {
    die "ERROR: can not figure out what month $$month is";
  }
  if ($$day > $maxday) {
    $$month++;
    if ($$month == 13) {
      $$year++;
      $$month = 1;
    }
    $$day = $$day - $maxday;
    return 0;
  }
  return 1;
}

#-------------------------------------------------------------------------------

sub leap() {
# return 1 if given year is a leap year, 0 otherwise

  my $year = shift;

  if (($year%4 == 0) && (($year%400 == 0) || ($year%100 != 0))) {
    return 1;
  }
  return 0;
}


#-------------------------------------------------------------------------------

sub any() {
# return 1 if array (arg 0) contains val (arg 1). Note that this uses "eq"
# instead of "==" because it's meant for strings

  my $array_ref = shift;
  my @array = @$array_ref;
  my $val = shift;

  foreach (@array) {
    if ($_ eq $val) {
      return 1;
    }
  }
  return 0;
}

