Skip to content

Commit

Permalink
Search for parameter files in addition rpar files
Browse files Browse the repository at this point in the history
  • Loading branch information
Deborah Ferguson committed Feb 21, 2024
1 parent 2490150 commit d35f5d5
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 60 deletions.
20 changes: 11 additions & 9 deletions mayawaves/utils/postprocessingutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,19 +1072,21 @@ def _get_parameter_file_name_and_content(raw_directory: str) -> tuple:
par_file = files_with_par[0]

# if we haven't found a par file or a rpar file
if rpar_file is None and par_file is None:
if rpar_file is None or par_file is None:
output_directories, _ = _ordered_output_directories(raw_directory)
output_directories.reverse() # most recent output directory is first now

for output_dir in output_directories:
files_with_rpar = glob.glob(os.path.join(output_dir, "*.rpar"))
if len(files_with_rpar) > 0: # if there is a rpar file
rpar_file = files_with_rpar[0]
files_with_par = glob.glob(os.path.join(output_dir, "*.par"))
if len(files_with_par) > 0: # if there is a par file
par_file = files_with_par[0]

if rpar_file is not None or par_file is not None:
if rpar_file is None:
files_with_rpar = glob.glob(os.path.join(output_dir, "*.rpar"))
if len(files_with_rpar) > 0: # if there is a rpar file
rpar_file = files_with_rpar[0]
if par_file is None:
files_with_par = glob.glob(os.path.join(output_dir, "*.par"))
if len(files_with_par) > 0: # if there is a par file
par_file = files_with_par[0]

if rpar_file is not None and par_file is not None:
break

# if we never found a parameter file
Expand Down
103 changes: 52 additions & 51 deletions tests/test_postprocessingutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,6 @@ def test__simulation_name(self):
actual = _simulation_name("a/b/c/test_name")
self.assertEqual(expected, actual)

# def test__parameter_file_name_base(self):
# from mayawaves.utils.postprocessingutils import _parameter_file_name_base
# # same as simulation name
# expected = "D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67"
# actual = _parameter_file_name_base(
# os.path.join(TestPostprocessingUtils.CURR_DIR,
# "resources/main_test_simulations/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67"))
# self.assertEqual(expected, actual)
#
# # different from simulation name
# expected = "D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67"
# actual = _parameter_file_name_base(
# os.path.join(TestPostprocessingUtils.CURR_DIR,
# "resources/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67_different_name"))
# self.assertEqual(expected, actual)

def test__get_parameter_file_name_and_content(self):
from mayawaves.utils.postprocessingutils import _get_parameter_file_name_and_content
temp_raw_directory = os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp_raw")
Expand All @@ -131,33 +115,19 @@ def test__get_parameter_file_name_and_content(self):
self.assertIsNone(parameter_file_name_base)
self.assertIsNone(parfile_dict)

# par file in output-0000
shutil.copy2(
os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/main_test_simulation/%s/output-0000/%s.par") % (
TestPostprocessingUtils.simulation_name,
TestPostprocessingUtils.simulation_name), output_0)
parameter_file_name_base, parfile_dict = _get_parameter_file_name_and_content(temp_raw_directory)
expected_parameter_file = os.path.join(output_0, f'{TestPostprocessingUtils.simulation_name}.par')
with open(expected_parameter_file) as f:
expected_par_content = f.read()
self.assertIsNotNone(parfile_dict)
self.assertEqual(TestPostprocessingUtils.simulation_name, parameter_file_name_base)
self.assertTrue('par_content' in parfile_dict)
self.assertFalse('rpar_content' in parfile_dict)
self.assertEqual(expected_par_content, parfile_dict['par_content'])

# rpar file in output-0000
# rpar file in output-0002
shutil.copy2(
os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/main_test_simulation/%s/output-0000/%s.rpar") % (
TestPostprocessingUtils.simulation_name,
TestPostprocessingUtils.simulation_name), output_0)
TestPostprocessingUtils.simulation_name), output_2)
parameter_file_name_base, parfile_dict = _get_parameter_file_name_and_content(temp_raw_directory)
expected_rpar_file = os.path.join(output_0, f'{TestPostprocessingUtils.simulation_name}.rpar')
expected_rpar_file = os.path.join(output_2, f'{TestPostprocessingUtils.simulation_name}.rpar')
with open(expected_rpar_file) as f:
expected_rpar_content = f.read()
expected_par_file = os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/main_test_simulation/%s/output-0000/%s.par") % (
TestPostprocessingUtils.simulation_name,
TestPostprocessingUtils.simulation_name)
expected_par_file = os.path.join(TestPostprocessingUtils.CURR_DIR,
"resources/main_test_simulation/%s/output-0000/%s.par") % (
TestPostprocessingUtils.simulation_name,
TestPostprocessingUtils.simulation_name)
with open(expected_par_file) as f:
expected_par_content = f.read()
self.assertIsNotNone(parfile_dict)
Expand All @@ -166,6 +136,7 @@ def test__get_parameter_file_name_and_content(self):
self.assertTrue('rpar_content' in parfile_dict)
self.assertEqual(expected_par_content, parfile_dict['par_content'])
self.assertEqual(expected_rpar_content, parfile_dict['rpar_content'])
os.remove(expected_rpar_file)

# par file in output-0001
shutil.copy2(
Expand Down Expand Up @@ -203,20 +174,36 @@ def test__get_parameter_file_name_and_content(self):
self.assertTrue('rpar_content' in parfile_dict)
self.assertEqual(expected_par_content, parfile_dict['par_content'])
self.assertEqual(expected_rpar_content, parfile_dict['rpar_content'])
os.remove(expected_rpar_file)
os.remove(os.path.join(output_1, f'{TestPostprocessingUtils.simulation_name}.par'))

# rpar file in output-0002
# par file in output-0000
shutil.copy2(
os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/main_test_simulation/%s/output-0000/%s.par") % (
TestPostprocessingUtils.simulation_name,
TestPostprocessingUtils.simulation_name), output_0)
parameter_file_name_base, parfile_dict = _get_parameter_file_name_and_content(temp_raw_directory)
expected_parameter_file = os.path.join(output_0, f'{TestPostprocessingUtils.simulation_name}.par')
with open(expected_parameter_file) as f:
expected_par_content = f.read()
self.assertIsNotNone(parfile_dict)
self.assertEqual(TestPostprocessingUtils.simulation_name, parameter_file_name_base)
self.assertTrue('par_content' in parfile_dict)
self.assertFalse('rpar_content' in parfile_dict)
self.assertEqual(expected_par_content, parfile_dict['par_content'])

# rpar file in output-0000
shutil.copy2(
os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/main_test_simulation/%s/output-0000/%s.rpar") % (
TestPostprocessingUtils.simulation_name,
TestPostprocessingUtils.simulation_name), output_2)
TestPostprocessingUtils.simulation_name), output_0)
parameter_file_name_base, parfile_dict = _get_parameter_file_name_and_content(temp_raw_directory)
expected_rpar_file = os.path.join(output_2, f'{TestPostprocessingUtils.simulation_name}.rpar')
expected_rpar_file = os.path.join(output_0, f'{TestPostprocessingUtils.simulation_name}.rpar')
with open(expected_rpar_file) as f:
expected_rpar_content = f.read()
expected_par_file = os.path.join(TestPostprocessingUtils.CURR_DIR,
"resources/main_test_simulation/%s/output-0000/%s.par") % (
TestPostprocessingUtils.simulation_name,
TestPostprocessingUtils.simulation_name)
expected_par_file = os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/main_test_simulation/%s/output-0000/%s.par") % (
TestPostprocessingUtils.simulation_name,
TestPostprocessingUtils.simulation_name)
with open(expected_par_file) as f:
expected_par_content = f.read()
self.assertIsNotNone(parfile_dict)
Expand All @@ -234,12 +221,16 @@ def test__get_parameter_file_name_and_content(self):
parameter_file_name_base, parfile_dict = _get_parameter_file_name_and_content(temp_raw_directory)
expected_parameter_file = os.path.join(par_directory, f'{TestPostprocessingUtils.simulation_name}.par')
with open(expected_parameter_file) as f:
expected_content = f.read()
expected_par_content = f.read()
expected_rpar_file = os.path.join(output_0, f'{TestPostprocessingUtils.simulation_name}.rpar')
with open(expected_rpar_file) as f:
expected_rpar_content = f.read()
self.assertIsNotNone(parfile_dict)
self.assertEqual(TestPostprocessingUtils.simulation_name, parameter_file_name_base)
self.assertTrue('par_content' in parfile_dict)
self.assertFalse('rpar_content' in parfile_dict)
self.assertTrue('rpar_content' in parfile_dict)
self.assertEqual(expected_par_content, parfile_dict['par_content'])
self.assertEqual(expected_rpar_content, parfile_dict['rpar_content'])

# rpar file in simfactory
shutil.copy2(
Expand Down Expand Up @@ -567,6 +558,7 @@ def test__store_parameter_file(self):

# clean up
temp_h5_file.close()
os.remove(expected_parameter_file)
os.remove(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5"))
os.remove(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp_parfile.rpar"))

Expand All @@ -593,6 +585,7 @@ def test__store_parameter_file(self):

# clean up
temp_h5_file.close()
os.remove(expected_parameter_file)
os.remove(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5"))
os.remove(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp_parfile.par"))

Expand All @@ -615,13 +608,17 @@ def test__store_parameter_file(self):
with open(temp_parameter_file, 'w') as f:
f.write(parfile_group.attrs['rpar_content'])
self.assertTrue(filecmp.cmp(expected_parameter_file, temp_parameter_file))
with open(expected_parameter_file.replace('.rpar', '.par')) as f:
expected_par_file = os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/main_test_simulation/%s/output-0000/%s.par") % (
TestPostprocessingUtils.simulation_name,
TestPostprocessingUtils.simulation_name)
with open(expected_par_file) as f:
expected_par_contents = f.read()
self.assertTrue('par_content' in parfile_group.attrs)
self.assertEqual(expected_par_contents, parfile_group.attrs['par_content'])

# clean up
temp_h5_file.close()
os.remove(expected_parameter_file)
os.remove(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5"))
os.remove(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp_parfile.rpar"))

Expand All @@ -648,6 +645,7 @@ def test__store_parameter_file(self):

# clean up
temp_h5_file.close()
os.remove(expected_parameter_file)
os.remove(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5"))
os.remove(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp_parfile.par"))

Expand All @@ -670,13 +668,17 @@ def test__store_parameter_file(self):
with open(temp_parameter_file, 'w') as f:
f.write(parfile_group.attrs['rpar_content'])
self.assertTrue(filecmp.cmp(expected_parameter_file, temp_parameter_file))
with open(expected_parameter_file.replace('.rpar', '.par')) as f:
expected_par_file = os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/main_test_simulation/%s/output-0000/%s.par") % (
TestPostprocessingUtils.simulation_name,
TestPostprocessingUtils.simulation_name)
with open(expected_par_file) as f:
expected_par_contents = f.read()
self.assertTrue('par_content' in parfile_group.attrs)
self.assertEqual(expected_par_contents, parfile_group.attrs['par_content'])

# clean up
temp_h5_file.close()
os.remove(expected_parameter_file)
os.remove(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5"))
os.remove(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp_parfile.rpar"))

Expand Down Expand Up @@ -709,6 +711,7 @@ def test__store_parameter_file(self):

# clean up
temp_h5_file.close()
os.remove(expected_parameter_file)
os.remove(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5"))
os.remove(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp_parfile.par"))

Expand All @@ -734,13 +737,11 @@ def test__store_parameter_file(self):
with open(temp_parameter_file, 'w') as f:
f.write(parfile_group.attrs['rpar_content'])
self.assertTrue(filecmp.cmp(expected_parameter_file, temp_parameter_file))
with open(expected_parameter_file.replace('.rpar', '.par')) as f:
expected_par_contents = f.read()
self.assertTrue('par_content' in parfile_group.attrs)
self.assertEqual(expected_par_contents, parfile_group.attrs['par_content'])

# clean up
temp_h5_file.close()
os.remove(expected_parameter_file)
os.remove(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5"))
os.remove(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp_parfile.rpar"))

Expand Down

0 comments on commit d35f5d5

Please sign in to comment.