Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def assert_write_file(self, nml, target_fname, sort=False):
tmp_fname = 'tmp.nml'
with open(tmp_fname, 'w') as tmp_file:
f90nml.write(nml, tmp_file, sort=sort)
self.assertFalse(tmp_file.closed)
try:
self.assert_file_equal(tmp_fname, target_fname)
finally:
os.remove(tmp_fname)
def assert_write_path(self, nml, target_fname, sort=False):
tmp_fname = 'tmp.nml'
f90nml.write(nml, tmp_fname, sort=sort)
try:
self.assert_file_equal(tmp_fname, target_fname)
finally:
os.remove(tmp_fname)
def init_config(self):
"""Patch input.nml as a new or restart run."""
input_fpath = os.path.join(self.work_path, 'input.nml')
input_nml = f90nml.read(input_fpath)
input_type = 'n' if self.expt.counter == 0 else 'r'
input_nml['GOLD_input_nml']['input_filename'] = input_type
f90nml.write(input_nml, input_fpath, force=True)
# Write to output
if not args.patch:
if output_fmt in ('json', 'yaml'):
if output_fmt == 'json':
input_data = input_data.todict(complex_tuple=True)
json.dump(input_data, output_file,
indent=4, separators=(',', ': '))
output_file.write('\n')
elif output_fmt == 'yaml':
input_data = input_data.todict(complex_tuple=True)
yaml.dump(input_data, output_file,
default_flow_style=False)
else:
# Default to namelist output
f90nml.write(input_data, output_file)
# Cleanup
if output_file != sys.stdout:
output_file.close()
def init_config(self):
"""Patch input.nml as a new or restart run."""
input_fpath = os.path.join(self.work_path, 'input.nml')
input_nml = f90nml.read(input_fpath)
if self.expt.counter == 0 or self.expt.repeat_run:
input_type = 'n'
else:
input_type = 'r'
input_nml['MOM_input_nml']['input_filename'] = input_type
f90nml.write(input_nml, input_fpath, force=True)
else:
run_runtime = cpl_nml[cpl_group]['runtime']
# Now write out new run start date and total runtime.
cpl_nml[cpl_group]['inidate'] = cal.date_to_int(run_start_date)
cpl_nml[cpl_group][runtime0_key] = total_runtime
cpl_nml[cpl_group]['runtime'] = int(run_runtime)
if model.model_type == 'cice':
if self.expt.counter and not self.expt.repeat_run:
cpl_nml[cpl_group]['jobnum'] = 1 + self.expt.counter
else:
cpl_nml[cpl_group]['jobnum'] = 1
nml_work_path = os.path.join(model.work_path, cpl_fname)
f90nml.write(cpl_nml, nml_work_path + '~')
shutil.move(nml_work_path + '~', nml_work_path)
# Now change the oasis runtime. This needs to be done after the others.
for model in self.expt.models:
if model.model_type == 'oasis':
namcouple = os.path.join(model.work_path, 'namcouple')
s = ''
with open(namcouple, 'r+') as f:
s = f.read()
m = re.search(r"^[ \t]*\$RUNTIME.*?^[ \t]*(\d+)", s,
re.MULTILINE | re.DOTALL)
assert(m is not None)
s = s[:m.start(1)] + str(run_runtime) + s[m.end(1):]
with open(namcouple, 'w') as f: