#!/usr/bin/env perl
#-----------------------------------------------------------------------------------------------
#
# build-namelist
#
# This script builds the namelists for the MPAS-CICE configuration of ACME.
#
# build-namelist uses a config_cache.xml file that current contains the seaice grid information.
# build-namelist reads this file to obtain information it needs to provide
# default values that are consistent with the MPAS-CICE 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-08-21  Jon Wolfe        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-cice_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
     -ice_grid             ICE_GRID variable
     -date_stamp           date_stamp variable
     -cfg_grid             Directory containing MPAS-CICE 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 MPAS-CICE 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 = absolute_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,
             ice_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'},	   
    "ice_grid=s"    => \$opts{'ice_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 MPAS-CICE 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 $ICE_GRID    = $opts{'ice_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}/mpas-ciceconf/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="ice_grid" value="$ICE_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 (see below)
(-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 MPAS-CICE tag is $cfgdir/namelist_files/namelist_definition.xml.  To aid developers
# who make use of the SourceMods/src.mpas-cice directory - we allow the definition file 
# to come from that directory
my $nl_definition_file;
if (-f "${CASEROOT}/SourceMods/src.mpas-cice/namelist_definition_mpas-cice.xml") {
    $nl_definition_file = "${CASEROOT}/SourceMods/src.mpas-cice/namelist_definition_mpas-cice.xml";
}
if (! defined $nl_definition_file) {
    # default location of namelist definition file
    $nl_definition_file = "$cfgdir/namelist_files/namelist_definition_mpas-cice.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.mpas-cice/namelist_defaults_mpas-cice.xml") {
    $nl_defaults_file = "${CASEROOT}/SourceMods/src.mpas-cice/namelist_defaults_mpas-cice.xml";
}
if (! defined $nl_defaults_file) {
    $nl_defaults_file = "$cfgdir/namelist_files/namelist_defaults_mpas-cice.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 XML::Lite;
require Build::Config;
require Build::NamelistDefinition;
require Build::NamelistDefaults;
require Build::Namelist;
require SetupTools;

#-----------------------------------------------------------------------------------------------
# Create a configuration object from the MPAS-CICE config_cache.xml file-  created by 
# mpas-cice.cpl7.template in $CASEBUILD/mpas-ciceconf
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 MPAS-CICE 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_mpas-cice

        # 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_mpas-cice.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 MPAS-CICE namelist variable or $var_local = $val is not a valid value; please fix user_nl_mpas-cice. Note that $var_local may appear in multiple namelists, in which case you need to specify the correct namelist in user_nl_mpas-cice using the format $var_local\&namelist_nml = $val, where \&namelist_nml is the mpas-cice_in namelist containing $var_local.**
EOF

        # If group was specified in user_nl_mpas-cice, 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_mpas-cice. **
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_mpas-cice:\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);
}

#-----------------------------------------------------------------------------------------------
# Determine xml variables
#unshift @INC, "$CASEROOT/Tools";
#require XML::Lite;
#require SetupTools;

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 $ICE_NCPL               = "$xmlvars{'ICE_NCPL'}";
my $ICE_COUPLING           = "$xmlvars{'ICE_COUPLING'}";
my $NTASKS_ICE             = "$xmlvars{'NTASKS_ICE'}";
my $NINST_ICE              = "$xmlvars{'NINST_ICE'}";
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 $output_r = "./${CASE}.mpas-cice.r";
my $output_h = "./${CASE}.mpas-cice.h";
my $output_d = "./${CASE}.mpas-cice.d";
if ($inst_string) {
    $output_r = "./${CASE}.mpas-cice${inst_string}.r";
    $output_h = "./${CASE}.mpas-cice${inst_string}.h";
    $output_d = "./${CASE}.mpas-cice${inst_string}.d";
} 

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

my $ntasks = $NTASKS_ICE / $NINST_ICE; 

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

print "MPAS-CICE build-namelist: ice_grid is $ICE_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
# Copied from file "build-namelist-section"
#-----------------------------------------------------------------------------------------------

##############################
# Namelist group: cice_model #
##############################

add_default($nl, 'config_dt');
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}'");
}
add_default($nl, 'config_stop_time');
add_default($nl, 'config_run_duration');
add_default($nl, 'config_num_halos');

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

add_default($nl, 'config_pio_num_iotasks');
add_default($nl, 'config_pio_stride');
add_default($nl, 'config_test_case_diag');
add_default($nl, 'config_test_case_diag_type');

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

add_default($nl, 'config_block_decomp_file_prefix', 'val'=>"'${DIN_LOC_ROOT}/ice/mpas-cice/${ICE_GRID}/mpas-cice.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: restart #
###########################

if ($CONTINUE_RUN eq 'TRUE') {
        add_default($nl, 'config_do_restart', 'val'=>".true.");
        add_default($nl, 'config_do_restart_hbrine', 'val'=>".true.");
        add_default($nl, 'config_do_restart_zsalinity', 'val'=>".true.");
        add_default($nl, 'config_do_restart_bgc', 'val'=>".true.");
} else {
        add_default($nl, 'config_do_restart', 'val'=>".false.");
        add_default($nl, 'config_do_restart_hbrine', 'val'=>".false.");
        add_default($nl, 'config_do_restart_zsalinity', 'val'=>".false.");
        add_default($nl, 'config_do_restart_bgc', 'val'=>".false.");
}
add_default($nl, 'config_restart_timestamp_name');
add_default($nl, 'config_do_restart_hbrine');
add_default($nl, 'config_do_restart_zsalinity');
add_default($nl, 'config_do_restart_bgc');

##############################
# Namelist group: initialize #
##############################

add_default($nl, 'config_initial_condition_type');
add_default($nl, 'config_initial_ice_area');
add_default($nl, 'config_initial_ice_volume');
add_default($nl, 'config_initial_snow_volume');
add_default($nl, 'config_initial_latitude_north');
add_default($nl, 'config_initial_latitude_south');
add_default($nl, 'config_initial_velocity_type');
add_default($nl, 'config_initial_uvelocity');
add_default($nl, 'config_initial_vvelocity');

################################
# Namelist group: use_sections #
################################

add_default($nl, 'config_use_velocity_solver');
add_default($nl, 'config_use_advection');
add_default($nl, 'config_use_forcing');
add_default($nl, 'config_use_column_package');

###########################
# Namelist group: forcing #
###########################

add_default($nl, 'config_atmospheric_forcing_type');
add_default($nl, 'config_forcing_start_time');
add_default($nl, 'config_forcing_cycle_start');
add_default($nl, 'config_forcing_cycle_duration');
add_default($nl, 'config_forcing_restart_file');
add_default($nl, 'config_forcing_precipitation_units');
add_default($nl, 'config_forcing_sst_type');
add_default($nl, 'config_update_ocean_fluxes');
add_default($nl, 'config_include_pond_freshwater_feedback');

#############################
# Namelist group: unit_test #
#############################

add_default($nl, 'config_perform_unit_test');
add_default($nl, 'config_unit_test_type');
add_default($nl, 'config_unit_test_subtype');

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

add_default($nl, 'config_dynamics_subcycle_number');
add_default($nl, 'config_rotate_cartesian_grid');
add_default($nl, 'config_include_metric_terms');
add_default($nl, 'config_elastic_subcycle_number');
add_default($nl, 'config_stress_divergence_scheme');
add_default($nl, 'config_variational_basis');
add_default($nl, 'config_revised_evp');
add_default($nl, 'config_use_air_stress');
add_default($nl, 'config_use_ocean_stress');
add_default($nl, 'config_use_surface_tilt');
add_default($nl, 'config_geostrophic_surface_tilt');

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

add_default($nl, 'config_advection_type');
add_default($nl, 'config_monotonic');
add_default($nl, 'config_conservation_check');
add_default($nl, 'config_monotonicity_check');
add_default($nl, 'config_recover_tracer_means_check');

##################################
# Namelist group: column_package #
##################################

add_default($nl, 'config_use_column_shortwave');
add_default($nl, 'config_use_column_vertical_thermodynamics');
add_default($nl, 'config_use_column_biogeochemistry');
add_default($nl, 'config_use_column_itd_thermodynamics');
add_default($nl, 'config_use_column_ridging');

##################################
# Namelist group: column_tracers #
##################################

add_default($nl, 'config_use_ice_age');
add_default($nl, 'config_use_first_year_ice');
add_default($nl, 'config_use_level_ice');
add_default($nl, 'config_use_cesm_meltponds');
add_default($nl, 'config_use_level_meltponds');
add_default($nl, 'config_use_topo_meltponds');
add_default($nl, 'config_use_aerosols');

###################################
# Namelist group: biogeochemistry #
###################################

add_default($nl, 'config_use_brine');
add_default($nl, 'config_use_vertical_zsalinity');
add_default($nl, 'config_use_vertical_biochemistry');
add_default($nl, 'config_use_shortwave_bioabsorption');
add_default($nl, 'config_use_vertical_tracers');
add_default($nl, 'config_use_skeletal_biochemistry');
add_default($nl, 'config_use_nitrate');
add_default($nl, 'config_use_carbon');
add_default($nl, 'config_use_chlorophyll');
add_default($nl, 'config_use_ammonium');
add_default($nl, 'config_use_silicate');
add_default($nl, 'config_use_DMS');
add_default($nl, 'config_use_nonreactive');
add_default($nl, 'config_use_humics');
add_default($nl, 'config_use_DON');
add_default($nl, 'config_use_iron');
add_default($nl, 'config_use_modal_aerosols');
add_default($nl, 'config_use_zaerosols');
add_default($nl, 'config_skeletal_bgc_flux_type');
add_default($nl, 'config_scale_initial_vertical_bgc');
add_default($nl, 'config_biogrid_bottom_molecular_sublayer');
add_default($nl, 'config_biogrid_top_molecular_sublayer');
add_default($nl, 'config_bio_gravity_drainage_length_scale');
add_default($nl, 'config_zsalinity_molecular_sublayer');
add_default($nl, 'config_zsalinity_gravity_drainage_scale');
add_default($nl, 'config_snow_porosity_at_ice_surface');
add_default($nl, 'config_new_ice_fraction_biotracer');
add_default($nl, 'config_fraction_biotracer_in_frazil');

#############################
# Namelist group: shortwave #
#############################

add_default($nl, 'config_shortwave_type');
add_default($nl, 'config_albedo_type');
add_default($nl, 'config_visible_ice_albedo');
add_default($nl, 'config_infrared_ice_albedo');
add_default($nl, 'config_visible_snow_albedo');
add_default($nl, 'config_infrared_snow_albedo');
add_default($nl, 'config_variable_albedo_thickness_limit');
add_default($nl, 'config_ice_shortwave_tuning_parameter');
add_default($nl, 'config_pond_shortwave_tuning_parameter');
add_default($nl, 'config_snow_shortwave_tuning_parameter');
add_default($nl, 'config_temp_change_snow_grain_radius_change');
add_default($nl, 'config_max_melting_snow_grain_radius');
add_default($nl, 'config_algae_absorption_coefficient');

#############################
# Namelist group: meltponds #
#############################

add_default($nl, 'config_snow_to_ice_transition_depth');
add_default($nl, 'config_pond_refreezing_type');
add_default($nl, 'config_pond_flushing_timescale');
add_default($nl, 'config_min_meltwater_retained_fraction');
add_default($nl, 'config_max_meltwater_retained_fraction');
add_default($nl, 'config_pond_depth_to_fraction_ratio');
add_default($nl, 'config_snow_on_pond_ice_tapering_parameter');
add_default($nl, 'config_critical_pond_ice_thickness');

##################################
# Namelist group: thermodynamics #
##################################

add_default($nl, 'config_thermodynamics_type');
add_default($nl, 'config_heat_conductivity_type');
add_default($nl, 'config_rapid_mode_channel_radius');
add_default($nl, 'config_rapid_model_critical_Ra');
add_default($nl, 'config_rapid_mode_aspect_ratio');
add_default($nl, 'config_slow_mode_drainage_strength');
add_default($nl, 'config_slow_mode_critical_porosity');
add_default($nl, 'config_congelation_ice_porosity');

#######################
# Namelist group: itd #
#######################

add_default($nl, 'config_itd_conversion_type');
add_default($nl, 'config_category_bounds_type');

###########################
# Namelist group: ridging #
###########################

add_default($nl, 'config_ice_strength_formulation');
add_default($nl, 'config_ridging_participation_function');
add_default($nl, 'config_ridging_redistribution_function');
add_default($nl, 'config_ridiging_efolding_scale');
add_default($nl, 'config_ratio_ridging_work_to_PE');

##############################
# Namelist group: atmosphere #
##############################

add_default($nl, 'config_atmos_boundary_method');
add_default($nl, 'config_calc_surface_stresses');
add_default($nl, 'config_calc_surface_temperature');
add_default($nl, 'config_use_form_drag');
add_default($nl, 'config_use_high_frequency_coupling');
add_default($nl, 'config_boundary_layer_iteration_number');

#########################
# Namelist group: ocean #
#########################

add_default($nl, 'config_use_ocean_mixed_layer');
add_default($nl, 'config_min_friction_velocity');
add_default($nl, 'config_ocean_heat_transfer_type');
add_default($nl, 'config_sea_freezing_temperature_type');

###############################
# Namelist group: diagnostics #
###############################

add_default($nl, 'config_check_state');

##########################################
# Namelist group: AM_highFrequencyOutput #
##########################################

add_default($nl, 'config_AM_highFrequencyOutput_enable');
add_default($nl, 'config_AM_highFrequencyOutput_compute_interval');
add_default($nl, 'config_AM_highFrequencyOutput_output_stream');
add_default($nl, 'config_AM_highFrequencyOutput_compute_on_startup');
add_default($nl, 'config_AM_highFrequencyOutput_write_on_startup');

###################################
# Namelist group: AM_temperatures #
###################################

add_default($nl, 'config_AM_temperatures_enable');
add_default($nl, 'config_AM_temperatures_compute_interval');
add_default($nl, 'config_AM_temperatures_output_stream');
add_default($nl, 'config_AM_temperatures_compute_on_startup');
add_default($nl, 'config_AM_temperatures_write_on_startup');

#########################################
# Namelist group: AM_regionalStatistics #
#########################################

add_default($nl, 'config_AM_regionalStatistics_enable');
add_default($nl, 'config_AM_regionalStatistics_compute_interval');
add_default($nl, 'config_AM_regionalStatistics_output_stream');
add_default($nl, 'config_AM_regionalStatistics_compute_on_startup');
add_default($nl, 'config_AM_regionalStatistics_write_on_startup');
add_default($nl, 'config_AM_regionalStatistics_ice_extent_limit');

#########################################
# Namelist group: AM_ridgingDiagnostics #
#########################################

add_default($nl, 'config_AM_ridgingDiagnostics_enable');
add_default($nl, 'config_AM_ridgingDiagnostics_compute_interval');
add_default($nl, 'config_AM_ridgingDiagnostics_output_stream');
add_default($nl, 'config_AM_ridgingDiagnostics_compute_on_startup');
add_default($nl, 'config_AM_ridgingDiagnostics_write_on_startup');

########################################
# Namelist group: AM_conservationCheck #
########################################

add_default($nl, 'config_AM_conservationCheck_enable');
add_default($nl, 'config_AM_conservationCheck_compute_interval');
add_default($nl, 'config_AM_conservationCheck_output_stream');
add_default($nl, 'config_AM_conservationCheck_compute_on_startup');
add_default($nl, 'config_AM_conservationCheck_write_on_startup');
add_default($nl, 'config_AM_conservationCheck_write_to_logfile');

##########################################
# Namelist group: AM_geographicalVectors #
##########################################

add_default($nl, 'config_AM_geographicalVectors_enable');
add_default($nl, 'config_AM_geographicalVectors_compute_interval');
add_default($nl, 'config_AM_geographicalVectors_output_stream');
add_default($nl, 'config_AM_geographicalVectors_compute_on_startup');
add_default($nl, 'config_AM_geographicalVectors_write_on_startup');

##################################
# Namelist group: AM_loadBalance #
##################################

add_default($nl, 'config_AM_loadBalance_enable');
add_default($nl, 'config_AM_loadBalance_compute_interval');
add_default($nl, 'config_AM_loadBalance_output_stream');
add_default($nl, 'config_AM_loadBalance_compute_on_startup');
add_default($nl, 'config_AM_loadBalance_write_on_startup');
add_default($nl, 'config_AM_loadBalance_nProcs');

#########################################
# Namelist group: AM_maximumIcePresence #
#########################################

add_default($nl, 'config_AM_maximumIcePresence_enable');
add_default($nl, 'config_AM_maximumIcePresence_compute_interval');
add_default($nl, 'config_AM_maximumIcePresence_output_stream');
add_default($nl, 'config_AM_maximumIcePresence_compute_on_startup');
add_default($nl, 'config_AM_maximumIcePresence_write_on_startup');
add_default($nl, 'config_AM_maximumIcePresence_start_time');

####################################
# Namelist group: AM_miscellaneous #
####################################

add_default($nl, 'config_AM_miscellaneous_enable');
add_default($nl, 'config_AM_miscellaneous_compute_interval');
add_default($nl, 'config_AM_miscellaneous_output_stream');
add_default($nl, 'config_AM_miscellaneous_compute_on_startup');
add_default($nl, 'config_AM_miscellaneous_write_on_startup');

####################################
# Namelist group: AM_areaVariables #
####################################

add_default($nl, 'config_AM_areaVariables_enable');
add_default($nl, 'config_AM_areaVariables_compute_interval');
add_default($nl, 'config_AM_areaVariables_output_stream');
add_default($nl, 'config_AM_areaVariables_compute_on_startup');
add_default($nl, 'config_AM_areaVariables_write_on_startup');

######################################
# Namelist group: AM_pondDiagnostics #
######################################

add_default($nl, 'config_AM_pondDiagnostics_enable');
add_default($nl, 'config_AM_pondDiagnostics_compute_interval');
add_default($nl, 'config_AM_pondDiagnostics_output_stream');
add_default($nl, 'config_AM_pondDiagnostics_compute_on_startup');
add_default($nl, 'config_AM_pondDiagnostics_write_on_startup');

#####################################
# Namelist group: AM_unitConversion #
#####################################

add_default($nl, 'config_AM_unitConversion_enable');
add_default($nl, 'config_AM_unitConversion_compute_interval');
add_default($nl, 'config_AM_unitConversion_output_stream');
add_default($nl, 'config_AM_unitConversion_compute_on_startup');
add_default($nl, 'config_AM_unitConversion_write_on_startup');

#####################################
# Namelist group: AM_pointwiseStats #
#####################################

add_default($nl, 'config_AM_pointwiseStats_enable');
add_default($nl, 'config_AM_pointwiseStats_compute_interval');
add_default($nl, 'config_AM_pointwiseStats_output_stream');
add_default($nl, 'config_AM_pointwiseStats_compute_on_startup');
add_default($nl, 'config_AM_pointwiseStats_write_on_startup');

#################################
# Namelist group: AM_icePresent #
#################################

add_default($nl, 'config_AM_icePresent_enable');
add_default($nl, 'config_AM_icePresent_compute_interval');
add_default($nl, 'config_AM_icePresent_output_stream');
add_default($nl, 'config_AM_icePresent_compute_on_startup');
add_default($nl, 'config_AM_icePresent_write_on_startup');

###########################################
# Namelist group: AM_timeSeriesStatsDaily #
###########################################

add_default($nl, 'config_AM_timeSeriesStatsDaily_enable');
add_default($nl, 'config_AM_timeSeriesStatsDaily_compute_on_startup');
add_default($nl, 'config_AM_timeSeriesStatsDaily_write_on_startup');
add_default($nl, 'config_AM_timeSeriesStatsDaily_compute_interval');
add_default($nl, 'config_AM_timeSeriesStatsDaily_output_stream');
add_default($nl, 'config_AM_timeSeriesStatsDaily_restart_stream');
add_default($nl, 'config_AM_timeSeriesStatsDaily_add_mesh');
add_default($nl, 'config_AM_timeSeriesStatsDaily_operation');
add_default($nl, 'config_AM_timeSeriesStatsDaily_reference_times');
add_default($nl, 'config_AM_timeSeriesStatsDaily_duration_intervals');
add_default($nl, 'config_AM_timeSeriesStatsDaily_repeat_intervals');
add_default($nl, 'config_AM_timeSeriesStatsDaily_reset_intervals');

#############################################
# Namelist group: AM_timeSeriesStatsMonthly #
#############################################

add_default($nl, 'config_AM_timeSeriesStatsMonthly_enable');
add_default($nl, 'config_AM_timeSeriesStatsMonthly_compute_on_startup');
add_default($nl, 'config_AM_timeSeriesStatsMonthly_write_on_startup');
add_default($nl, 'config_AM_timeSeriesStatsMonthly_compute_interval');
add_default($nl, 'config_AM_timeSeriesStatsMonthly_output_stream');
add_default($nl, 'config_AM_timeSeriesStatsMonthly_restart_stream');
add_default($nl, 'config_AM_timeSeriesStatsMonthly_add_mesh');
add_default($nl, 'config_AM_timeSeriesStatsMonthly_operation');
add_default($nl, 'config_AM_timeSeriesStatsMonthly_reference_times');
add_default($nl, 'config_AM_timeSeriesStatsMonthly_duration_intervals');
add_default($nl, 'config_AM_timeSeriesStatsMonthly_repeat_intervals');
add_default($nl, 'config_AM_timeSeriesStatsMonthly_reset_intervals');

#################################################
# Namelist group: AM_timeSeriesStatsClimatology #
#################################################

add_default($nl, 'config_AM_timeSeriesStatsClimatology_enable');
add_default($nl, 'config_AM_timeSeriesStatsClimatology_compute_on_startup');
add_default($nl, 'config_AM_timeSeriesStatsClimatology_write_on_startup');
add_default($nl, 'config_AM_timeSeriesStatsClimatology_compute_interval');
add_default($nl, 'config_AM_timeSeriesStatsClimatology_output_stream');
add_default($nl, 'config_AM_timeSeriesStatsClimatology_restart_stream');
add_default($nl, 'config_AM_timeSeriesStatsClimatology_add_mesh');
add_default($nl, 'config_AM_timeSeriesStatsClimatology_operation');
add_default($nl, 'config_AM_timeSeriesStatsClimatology_reference_times');
add_default($nl, 'config_AM_timeSeriesStatsClimatology_duration_intervals');
add_default($nl, 'config_AM_timeSeriesStatsClimatology_repeat_intervals');
add_default($nl, 'config_AM_timeSeriesStatsClimatology_reset_intervals');


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

my @groups = qw(cice_model
                io
                decomposition
                restart
                initialize
                use_sections
                forcing
                unit_test
                velocity_solver
                advection
                column_package
                column_tracers
                biogeochemistry
                shortwave
                meltponds
                thermodynamics
                itd
                ridging
                atmosphere
                ocean
                diagnostics
                am_highfrequencyoutput
                am_temperatures
                am_regionalstatistics
                am_ridgingdiagnostics
                am_conservationcheck
                am_geographicalvectors
                am_loadbalance
                am_maximumicepresence
                am_miscellaneous
                am_areavariables
                am_ponddiagnostics
                am_unitconversion
                am_pointwisestats
                am_icepresent
                am_timeseriesstatsdaily
                am_timeseriesstatsmonthly
                am_timeseriesstatsclimatology
                );

# 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 mpas-cice_in
my $outfile = "./mpas-cice_in";
$nl->write($outfile, 'groups'=>\@groups);
if ($print>=2) { print "Writing mpas-cice ice component namelist to $outfile $eol"; }

# Write input dataset list.
check_input_files($DIN_LOC_ROOT, "../mpas-cice.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;
}

