Skip to content
Snippets Groups Projects
Commit 9966a3e1 authored by lkugler's avatar lkugler
Browse files

input.nml functions

parent 2fdfc0d2
Branches
Tags
No related merge requests found
......@@ -5,6 +5,7 @@ from config.cluster import cluster
earth_radius_km = 6370
def read_namelist(filepath):
"""Read the DART namelist file into a dictionary.
......@@ -64,7 +65,7 @@ def read_namelist(filepath):
param_data.append(val)
print('this iteration var, val ...', {param: param_data})
# print('this iteration var, val ...', {param: param_data})
# add variable to dictionary
d[section][param] = param_data
......@@ -86,7 +87,7 @@ def write_namelist_from_dict(d, filepath):
try:
parameters = d[section].keys()
print(parameters, [len(p) for p in parameters])
# print(parameters, [len(p) for p in parameters])
max_width_of_parameter_name = max([len(p) for p in parameters])
width = max_width_of_parameter_name + 1
except:
......@@ -107,9 +108,9 @@ def write_namelist_from_dict(d, filepath):
if i == 0:
f.write(' '+parameter.ljust(width)+' = '+line+'\n')
f.write(' '+parameter.ljust(width)+' = '+line+',\n')
else:
f.write(' '+' '*width+' = '+line+'\n')
f.write(' '+' '*width+' '+line+',\n')
f.write(' /\n\n')
......@@ -191,7 +192,8 @@ def _get_list_of_localizations():
def _to_fortran_list(l):
"""Ensure formatting as "arg1", "arg2", """
"""Ensure formatting with quotation mark, e.g. parameter = "arg1", "arg2",
"""
assert isinstance(l, list)
if len(l) > 1: # multiple entries
......@@ -243,7 +245,6 @@ def write_namelist(just_prior_values=False):
nml['&location_nml']['special_vert_normalization_heights'] = [_to_fortran_list(list_loc_vert_km)]
nml['&location_nml']['special_vert_normalization_scale_heights'] = [_to_fortran_list(list_loc_vert_scaleheight)]
print(nml['&location_nml']['special_vert_normalization_obs_types'])
# overwrite namelist with experiment configuration
for section, sdata in exp.dart_nml.items():
......
This diff is collapsed.
This diff is collapsed.
import os, shutil
import datetime as dt
from dartwrf import dart_nml
def test_input_nml():
test_input = './input.nml.original'
test_output = './input.nml.output'
desired_output = './input.nml.desired_output'
# read an existing input.nml
nml = dart_nml.read_namelist(test_input)
# modify one parameter
nml['&filter_nml']['ens_size'] = [[str(999)]]
nml['&filter_nml']['num_output_state_members'] = [[str(999)]]
nml['&filter_nml']['num_output_obs_members'] = [[str(999)]]
# save the configuration as input.nml
dart_nml.write_namelist_from_dict(nml, test_output)
# compare the saved input.nml to the true input.nml
# by reading both and comparing the dictionaries
nml_desired = dart_nml.read_namelist(desired_output)
nml_test = dart_nml.read_namelist(test_output)
for section, _ in nml_desired.items():
for param, value in nml_desired[section].items():
should_have = nml_desired[section][param]
should_have = [v.strip() for line in should_have for v in line]
have = nml_test[section][param]
have = [v.strip() for line in have for v in line]
if should_have != have:
raise ValueError(section, param, 'should be', should_have, 'but is', have)
os.remove(test_output)
if __name__ == '__main__':
test_input_nml()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment