Example #1
0
def _update_time_namelist(run_date, run_type, run_duration, host_run_config):
    prev_runs = {
        # run-type: based-on run-type, date offset
        'nowcast': ('nowcast', -1),
        'nowcast-green': ('nowcast-green', -1),
        'nowcast-dev': ('nowcast-dev', -1),
        'forecast': ('nowcast', 0),
        'forecast2': ('forecast', 0),
    }
    prev_run_type, date_offset = prev_runs[run_type]
    results_dir = Path(host_run_config['results'][prev_run_type])
    dmy = run_date.replace(days=date_offset).format('DDMMMYY').lower()
    prev_run_namelist = namelist2dict(
        os.fspath(results_dir / dmy / 'namelist_cfg'))
    prev_it000 = prev_run_namelist['namrun'][0]['nn_it000']
    try:
        namelist_domain_path = Path(host_run_config['run prep dir'],
                                    'namelist.domain')
        namelist_domain = namelist2dict(os.fspath(namelist_domain_path))
        rdt = namelist_domain['namdom'][0]['rn_rdt']
    except FileNotFoundError:
        rdt = prev_run_namelist['namdom'][0]['rn_rdt']
    timesteps_per_day = 86400 / rdt
    namelist_time = Path(host_run_config['run prep dir'], 'namelist.time')
    with namelist_time.open('rt') as f:
        lines = f.readlines()
    new_lines, restart_timestep = _calc_new_namelist_lines(
        run_date, run_type, run_duration, prev_it000, timesteps_per_day, lines)
    with namelist_time.open('wt') as f:
        f.writelines(new_lines)
    return restart_timestep
Example #2
0
 def test_ignore_empty_group(self):
     """
      Ignore empty namelist group.
     """
     group = "&group\n" "&end"
     namelist_dict = namelist2dict(StringIO(group))
     self.assertEqual(namelist_dict, {})
Example #3
0
 def test_group_ends_w_amp_end(self):
     """
     Test simple namelist group with &end as end token.
     """
     group = "&group\n" "    float = 0.75\n" "&end"
     namelist_dict = namelist2dict(StringIO(group))
     self.assertEqual(namelist_dict, {"group": [{"float": 0.75}]})
Example #4
0
 def test_double_quote_string(self):
     """
     Test simple namelist group with string value enclosed in double quotes.
     """
     group = "&group\n" '    string = "test"\n' "/"
     namelist_dict = namelist2dict(StringIO(group))
     self.assertEqual(namelist_dict, {"group": [{"string": "test"}]})
Example #5
0
 def test_simple_group(self):
     """
     Test simple namelist group with values of different types.
     """
     group = ("&group\n"
              "    float = 0.75\n"
              "    integer = 700\n"
              "    string = 'test'\n"
              "    truth = .TRUE.\n"
              "    untruth = .FALSE.\n"
              "    lowercase_truth = .true.\n"
              "    lowercase_untruth = .false.\n"
              "    undotted_true = TRUE\n"
              "    undotted_false = FALSE\n"
              "    lowercase_undotted_true = true\n"
              "    lowercase_undotted_false = false\n"
              "/")
     namelist_dict = namelist2dict(StringIO(group))
     self.assertEqual(
         namelist_dict, {
             "group": [{
                 "float": 0.75,
                 "integer": 700,
                 "string": "test",
                 "truth": True,
                 "untruth": False,
                 "lowercase_truth": True,
                 "lowercase_untruth": False,
                 "undotted_true": True,
                 "undotted_false": False,
                 "lowercase_undotted_true": True,
                 "lowercase_undotted_false": False
             }]
         })
Example #6
0
 def test_group_mixed_and_lists(self):
     """
     Tests a real world example.
     """
     group = ("&receiver\n"
              "    station ='XX02'\n"
              "    location = 'a'\n"
              "    lon = 12.51\n"
              "    lat = -0.01\n"
              "    depth = 1.0\n"
              "    attributes = 'vx' 'vy' 'vz'\n"
              "    file_name_prefix = './DATA/mess/'\n"
              "    override = .TRUE.\n"
              "/\n")
     namelist_dict = namelist2dict(StringIO(group))
     self.assertEqual(
         namelist_dict, {
             "receiver": [{
                 "station": "XX02",
                 "location": "a",
                 "lon": 12.51,
                 "lat": -0.01,
                 "depth": 1.0,
                 "attributes": ["vx", "vy", "vz"],
                 "file_name_prefix": "./DATA/mess/",
                 "override": True
             }]
         })
Example #7
0
 def test_real_world_example(self):
     """
     Tests example from
         http://owen.sj.ca.us/~rk/howto/slides/f90model/slides/namelist.html
     """
     groups = (
         "! can have blank lines and comments in the namelist input file\n"
         "! place these comments between NAMELISTs\n"
         "\n"
         "!\n"
         "! not every compiler supports comments within the namelist\n"
         "!  in particular vastf90/g77 does not\n"
         "!\n"
         "! some will skip NAMELISTs not directly referenced in read\n"
         "!&BOGUS rko=1 /\n"
         "!\n"
         "&TTDATA \n"
         " TTREAL =  1.,\n"
         " TTINTEGER = 2,\n"
         " TTCOMPLEX = (3.,4.), \n"
         " TTCHAR = 'namelist', \n"
         " TTBOOL = .TRUE./\n"
         "&AADATA\n"
         " AAREAL =  1.  1.  2.  3., \n"
         " AAINTEGER = 2 2 3 4, \n"
         " AACOMPLEX = (3.,4.) (3.,4.) (5.,6.) (7.,7.), \n"
         " AACHAR = 'namelist' 'namelist' 'array' ' the lot', \n"
         " AABOOL = .TRUE. .TRUE. .FALSE. .FALSE./\n"
         "&XXDATA \n"
         " XXREAL =  1., \n"
         " XXINTEGER = 2, \n"
         " XXCOMPLEX = (3.,4.)/")
     namelist_dict = namelist2dict(StringIO(groups))
     self.assertEqual(
         namelist_dict, {
             "TTDATA": [{
                 "TTREAL": 1.0,
                 "TTINTEGER": 2,
                 "TTCOMPLEX": 3.0 + 4.0j,
                 "TTCHAR": "namelist",
                 "TTBOOL": True
             }],
             "AADATA": [{
                 "AAREAL": [1.0, 1.0, 2.0, 3.0],
                 "AAINTEGER": [2, 2, 3, 4],
                 "AACOMPLEX":
                 [3.0 + 4.0j, 3.0 + 4.0j, 5.0 + 6.0j, 7.0 + 7.0j],
                 "AACHAR": ["namelist", "namelist", "array", " the lot"],
                 "AABOOL": [True, True, False, False]
             }],
             "XXDATA": [{
                 "XXREAL": 1.0,
                 "XXINTEGER": 2,
                 "XXCOMPLEX": 3.0 + 4.0j
             }]
         })
Example #8
0
    def test_multiple_groups(self):
        """
        Mixes groups from some of the previous tests.
        """
        group = ("&group\n"
                 "    float = 0.75\n"
                 "    integer = 700\n"
                 "    string = 'test'\n"
                 "    truth = .TRUE.\n"
                 "    untruth = .FALSE.\n"
                 "/\n"
                 "\n"
                 "&list a=1, b=1,2 c='12 / !' / \n"
                 "&list a=1\n"
                 "b=1,2, c='12 / !' /\n"
                 "&receiver\n"
                 "    station ='XX02'\n"
                 "    location = 'a'\n"
                 "    lon = 12.51\n"
                 "    lat = -0.01\n"
                 "    depth = 1.0\n"
                 "    attributes = 'vx' 'vy' 'vz'\n"
                 "    file_name_prefix = './DATA/mess/'\n"
                 "    override = .TRUE.\n"
                 "/\n")
        namelist_dict = namelist2dict(StringIO(group))

        self.assertEqual(
            namelist_dict, {
                "group": [{
                    "float": 0.75,
                    "integer": 700,
                    "string": "test",
                    "truth": True,
                    "untruth": False
                }],
                "list": [{
                    "a": 1,
                    "b": [1, 2],
                    "c": "12 / !"
                }, {
                    "a": 1,
                    "b": [1, 2],
                    "c": "12 / !"
                }],
                "receiver": [{
                    "station": "XX02",
                    "location": "a",
                    "lon": 12.51,
                    "lat": -0.01,
                    "depth": 1.0,
                    "attributes": ["vx", "vy", "vz"],
                    "file_name_prefix": "./DATA/mess/",
                    "override": True
                }]
            })
Example #9
0
 def test_heterogeneous_list(self):
     """
     Test list of heterogeneous values.
     """
     group = "&group\n" "    foo = 0.75, 700, 'test', .TRUE.\n" "/"
     namelist_dict = namelist2dict(StringIO(group))
     self.assertEqual(namelist_dict,
                      {"group": [{
                          "foo": [0.75, 700, "test", True]
                      }]})
Example #10
0
 def test_array_element_assignment(self):
     """
     Test simple namelist group with assignment to array element.
     """
     group = ("&group\n"
              "    float(1) = 0.75\n"
              "    float(2) = 0.85\n"
              "&end")
     namelist_dict = namelist2dict(StringIO(group))
     self.assertEqual(namelist_dict, {"group": [{"float": [0.75, 0.85]}]})
Example #11
0
 def test_empty_string(self):
     """
     Test simple namelist group with empty string value.
     """
     group = "&group\n" '    string1 = ""\n' "    string2 = ''\n" "/"
     namelist_dict = namelist2dict(StringIO(group))
     self.assertEqual(namelist_dict,
                      {"group": [{
                          "string1": "",
                          "string2": ""
                      }]})
Example #12
0
 def test_complex_multiple_group(self):
     """
     Same as test_complex_single_line_group() just split over lines.
     """
     group = "&list a=1\n" "b=1,2, c='12 / !' /"
     namelist_dict = namelist2dict(StringIO(group))
     self.assertEqual(namelist_dict,
                      {"list": [{
                          "a": 1,
                          "b": [1, 2],
                          "c": "12 / !"
                      }]})
Example #13
0
 def test_complex_single_line_group(self):
     """
     Tests a rather complex single line group.
     """
     group = "&list a=1, b=1,2 c='12 / !' / "
     namelist_dict = namelist2dict(StringIO(group))
     self.assertEqual(namelist_dict,
                      {"list": [{
                          "a": 1,
                          "b": [1, 2],
                          "c": "12 / !"
                      }]})
def watch_NEMO(parsed_args, config, tell_manager):
    host_name = parsed_args.host_name
    run_type = parsed_args.run_type
    pid = parsed_args.pid
    # Ensure that the run is in progress
    if not _pid_exists(pid):
        logger.error(
            '{}: NEMO run pid {} on {} does not exist'
            .format(run_type, pid, host_name))
        raise WorkerError
    # Get monitored run info from manager and namelist
    run_info = tell_manager('need', 'NEMO run').payload
    run_dir = Path(run_info[run_type]['run dir'])
    namelist = namelist2dict(str(run_dir/'namelist_cfg'))
    it000 = namelist['namrun'][0]['nn_it000']
    itend = namelist['namrun'][0]['nn_itend']
    date0 = arrow.get(str(namelist['namrun'][0]['nn_date0']), 'YYYYMMDD')
    rdt = namelist['namdom'][0]['rn_rdt']
    # Watch for the run bash script process to end
    while _pid_exists(pid):
        try:
            with (run_dir/'time.step').open('rt') as f:
                time_step = int(f.read().strip())
            model_seconds = (time_step - it000) * rdt
            model_time = (
                date0.replace(seconds=model_seconds)
                .format('YYYY-MM-DD HH:mm:ss UTC'))
            fraction_done = (time_step - it000) / (itend - it000)
            msg = (
                '{} on {}: timestep: {} = {}, {:.1%} complete'.format(
                    run_type, host_name, time_step, model_time, fraction_done))
        except FileNotFoundError:
            # time.step file not found; assume that run is young and it
            # hasn't been created yet, or has finished and it has been
            # moved to the results directory
            msg = (
                '{} on {}: time.step not found; continuing to watch...'
                .format(run_type, host_name))
        logger.info(msg)
        time.sleep(POLL_INTERVAL)
    ## TODO: confirm that the run and subsequent results gathering
    ## completed successfully
    return {run_type: {
        'host': host_name,
        'run date': run_info[run_type]['run date'],
        'completed': True,
    }}
Example #15
0
    def test_complex_numbers(self):
        """
        Tests complex numbers. Complex number parsing is rather forgiving.
        """
        group = ("&complex_group\n"
                 "    number_a = (1,2)\n"
                 "    number_b = (1.2,3.4)\n"
                 "    number_c = (-1.2,0.0)\n"
                 "    number_d = (0.0, 1.0)\n"
                 "/")

        namelist_dict = namelist2dict(StringIO(group))
        self.assertEqual(
            namelist_dict, {
                "complex_group": [{
                    "number_a": 1.0 + 2.0j,
                    "number_b": 1.2 + 3.4j,
                    "number_c": -1.2 + 0.0j,
                    "number_d": 0.0j + 1.0j
                }]
            })
Example #16
0
"""
Build AGRIF_FixedGrids.in by reading the values in the nesting tools' namelist
"""
import sys
from nemo_cmd.namelist import namelist2dict

namelistfile = sys.argv[1]
nst = namelist2dict(namelistfile)['nesting'][0]
imin, imax = nst['imin'], nst['imax']
jmin, jmax = nst['jmin'], nst['jmax']
rho, rhot = nst['rho'], nst['rhot']

print("Writing AGRIF_FixedGrids.in")
with open('AGRIF_FixedGrids.in', 'w') as f:
    f.write("1\n")
    f.write("{} {} {} {} {} {} {} {}\n".format(imin, imax, jmin, jmax, rho,
                                               rho, rhot, rhot))
    f.write("0\n\n")
    f.write("# number of children per parent\n")
    f.write("# imin imax jmin jmax spacerefx spacerefy timerefx timerefy\n")
    f.write("# [all coordinates are relative to each parent grid!]\n")