#!/usr/bin/env python3

"""
Namelist creator for E3SM's SCREAM component

This script takes the namelist_defaults_scream.xml file from the repo
and produces the processed $case/namelist_scream.xml. From the
processed file, raw input files are produced which should not be
modified by users.  The user is allowed to modify
$case/namelist_scream.xml either directly via text editor (highly
discouraged, requires SCREAM_HACK_XML to be on) or via atmchange
(preferred). The user can also use atmquery to query current
configurations stored in the XML file.

This script can be internally tested standalone via: buildnml --test

It is also encouraged to run pylint on this file when it gets changed:
python3 -m pylint --disable C --disable R buildnml
Some import errors are expected and can be ignored

It is encouraged that any changes to this system be tested with
eamxx/cime-nml-tests
"""

import os, sys

from CIME.case import Case
from CIME.utils import expect, safe_copy, SharedArea, run_cmd_no_fail
from CIME.buildnml import parse_input

from eamxx_buildnml import create_raw_xml_file, create_input_files, create_input_data_list_file, \
    do_cime_vars_on_yaml_output_files

###############################################################################
def buildnml(case, caseroot, compname):
###############################################################################
    """
    This routine needs to live here in order for CIME's run_sub_or_cmd to be
    able to run this as a library. All other implemention should go in .py
    files so doctest/pylint will run on it.
    """
    expect(compname == "scream", compname)

    rundir     = case.get_value("RUNDIR")
    screamroot = os.path.join(case.get_value("SRCROOT"), "components/eamxx")
    rundata    = os.path.join(rundir, "data")

    #
    # Copy default output YAML file, and atmchange/query scripts to rundir
    #
    with SharedArea():
        if not os.path.isdir(rundata):
            os.mkdir(rundata)

        # Create link to atmchange and atmquery scripts
        for script in ['atmchange', 'atmquery']:
            tgt_file = os.path.join(caseroot,script)
            if not os.path.exists(tgt_file):
                src_file = os.path.join(screamroot,'scripts',script)
                os.symlink(src_file, tgt_file)

    #
    # Create the raw/processed XML input file and create input files
    # from it.
    #
    create_raw_xml_file(case, caseroot)
    create_input_files(caseroot, screamroot, rundir)

    # For all YAML files listed in the XML for model output, expand CIME vars (if any)
    do_cime_vars_on_yaml_output_files(case,caseroot)

    #
    # Create input data list. This is the list of files that CIME needs
    # to ensure are present on the machine (possibly downloading them)
    #
    create_input_data_list_file(case,caseroot)

###############################################################################
def _main_func():
###############################################################################
    if "--test" in sys.argv:
        from doctest import testmod
        import eamxx_buildnml
        import eamxx_buildnml_impl
        testmod(m=eamxx_buildnml)
        testmod(m=eamxx_buildnml_impl)

    else:
        caseroot = parse_input(sys.argv)
        with Case(caseroot) as case:
            buildnml(case, caseroot, "scream")

if __name__ == "__main__":
    _main_func()
