diff --git a/mayawaves/coalescence.py b/mayawaves/coalescence.py index 280753f..77f68d2 100644 --- a/mayawaves/coalescence.py +++ b/mayawaves/coalescence.py @@ -664,6 +664,9 @@ def eccentricity_and_mean_anomaly_at_time(self, start_time, desired_time) -> tup # rough estimate with initial velocity time_momentum, momentum_vector = self.primary_compact_object.momentum_vector + if momentum_vector is None: + return -1, -1 + initial_momentum = momentum_vector[0] tangential_initial_momentum = initial_momentum[1] qc_tangential_momentum = pn.tangential_momentum_from_separation(separation_magnitude[0], mass_ratio, diff --git a/mayawaves/utils/postprocessingutils.py b/mayawaves/utils/postprocessingutils.py index 2593746..0862b73 100644 --- a/mayawaves/utils/postprocessingutils.py +++ b/mayawaves/utils/postprocessingutils.py @@ -568,7 +568,13 @@ def store_compact_object_data_from_filetype(filepaths, compact_object_dict, para header_info = header_info + line compact_object_count = None - parfile_content = parameter_file_group.attrs['par_content'] + if 'par_content' in parameter_file_group.attrs: + parfile_content = parameter_file_group.attrs['par_content'] + elif 'rpar_content' in parameter_file_group.attrs: + parfile_content = parameter_file_group.attrs['rpar_content'] + else: + warnings.warn('No parameter file information. Unable to read QuasiLocalMeasures data.') + return for line in parfile_content.splitlines(): if 'QuasiLocalMeasures::num_surfaces' in line: compact_object_count = int(line.split('=')[-1].strip()) @@ -1039,42 +1045,86 @@ def _simulation_name(raw_directory: str) -> str: simulation_name = raw_directory.split('/')[-1] return simulation_name +def _get_parameter_file_name_and_content(raw_directory: str) -> tuple: + """Store the parameter file in the h5 file -def _parameter_file_name_base(raw_directory: str) -> str: - """Base name from the parameter file - - From the raw directory, compute the base of the parameter file name, whether it be a .rpar or .par file. + Search for a .rpar file (or a .par file if there is no .rpar file) and store it in a dictionary Args: raw_directory (str): the directory that contains all simulation data Returns: - str: the base name of the parameter file + tuple: base name of the parameter file and a dictionary containing the rpar and par content """ + parfile_name = None + parfile_dict = {} + + # check if SIMFACTORY directory exists + rpar_file = None + par_file = None if os.path.isdir(os.path.join(raw_directory, "SIMFACTORY/par")): - parameter_files = glob.glob(os.path.join(raw_directory, "SIMFACTORY/par/*.rpar")) + glob.glob( - os.path.join(raw_directory, "SIMFACTORY/par/*.par")) - if len(parameter_files) > 0: # if there is an rpar file - parameter_file = parameter_files[0] - parfile_name = parameter_file.split('/')[-1] - parfile_name_base = parfile_name[:parfile_name.rfind('.')] - return parfile_name_base + files_with_rpar = glob.glob(os.path.join(raw_directory, "SIMFACTORY/par/*.rpar")) + if len(files_with_rpar) > 0: # if there is an rpar file + rpar_file = files_with_rpar[0] + files_with_par = glob.glob(os.path.join(raw_directory, "SIMFACTORY/par/*.par")) + if len(files_with_par) > 0: # if there is a par file + par_file = files_with_par[0] - output_directories, _ = _ordered_output_directories(raw_directory) - output_directories.reverse() # most recent output directory is first now + # if we haven't found a par file or a rpar file + if rpar_file is None and 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: + break - for output_dir in output_directories: - parameter_files = glob.glob(os.path.join(output_dir, "*.rpar")) + glob.glob(os.path.join(output_dir, "*.par")) - if len(parameter_files) > 0: # if there is an rpar file - parameter_file = parameter_files[0] - parfile_name = parameter_file.split('/')[-1] - parfile_name_base = parfile_name[:parfile_name.rfind('.')] - return parfile_name_base + # if we never found a parameter file + if rpar_file is None and par_file is None: + warnings.warn("Unable to locate a .rpar or .par file in this simulation directory.") + return None, None - warnings.warn( - "Unable to find a parameter file. Cannot determine parameter file name. Will assume same as simulation name.") - return _simulation_name(raw_directory) + created_par = False + + # store the rpar file if it exists and create parfile + if rpar_file is not None: + rpar_name = rpar_file.split('/')[-1] + rpar_name_base = rpar_name[:rpar_name.rfind('.')] + parfile_name = rpar_name_base + with open(rpar_file, 'r') as f: + content = f.read() + parfile_dict['rpar_content'] = content + if par_file is None: + temporary_rpar = rpar_file[:rpar_file.rfind('/') + 1] + "temp.rpar" + par_file = rpar_file[:rpar_file.rfind('/') + 1] + "temp.par" + with open(temporary_rpar, 'w') as f: + f.write(parfile_dict['rpar_content']) + os.system(f'chmod +x {temporary_rpar}') + os.system('%s' % temporary_rpar) + created_par = True + os.remove(temporary_rpar) + + # store the par file + if parfile_name is None: + par_name = par_file.split('/')[-1] + parfile_name = par_name[:par_name.rfind('.')] + if os.path.exists(par_file): + with open(par_file, 'r') as f: + content = f.read() + parfile_dict['par_content'] = content + + if created_par and os.path.exists(par_file): + os.remove(par_file) + + return parfile_name, parfile_dict def _ordered_output_directories(raw_directory: str) -> tuple: @@ -1102,7 +1152,7 @@ def _ordered_output_directories(raw_directory: str) -> tuple: return output_directories, prestitched -def _ordered_data_directories(raw_directory: str, parameter_file_name_base: str) -> list: +def _ordered_data_directories(raw_directory: str, parameter_file: str, parameter_file_name_base: str) -> list: """The directories containing the simulation data files, ordered. Within each output directory is a data directory. Returns an ordered list of all the data directories. If the data @@ -1117,85 +1167,54 @@ def _ordered_data_directories(raw_directory: str, parameter_file_name_base: str) """ output_directories, prestitched = _ordered_output_directories(raw_directory) + + simulation_name = raw_directory.split('/')[-1] + if prestitched: # pre-stitched data data_directories = output_directories else: - data_directories = [os.path.join(output_directory, parameter_file_name_base) for output_directory in + if parameter_file is None: + return None + data_dir_name = "" + + result = re.search('IO::out_dir\s*=\s*(\S*)\s*\n', parameter_file) + try: + data_dir_name = result.group(1) + data_dir_name = data_dir_name.strip('"\'') + except: + warnings.warn("Can't find name of the data directory, assuming it is an empty string") + if data_dir_name == '\$parfile' or data_dir_name == '$parfile': + data_dir_name = parameter_file_name_base + if data_dir_name == '@SIMULATION_NAME@': + data_dir_name = simulation_name + + data_directories = [os.path.join(output_directory, data_dir_name) for output_directory in output_directories] return data_directories - -def _store_parameter_file(raw_directory: str, h5_file: h5py.File): +def _store_parameter_file(parfile_dict: dict, h5_file: h5py.File): """Store the parameter file in the h5 file - Search for a .rpar file (or a .par file if there is no .rpar file) and store it + Store .rpar and .par file information Args: - raw_directory (str): the directory that contains all simulation data + parfile_dict (dict): dictionary containing the rpar and par content h5_file (h5py.file): the h5 file to store the parameter file in """ - # check if SIMFACTORY directory exists - rpar_file = None - par_file = None - if os.path.isdir(os.path.join(raw_directory, "SIMFACTORY/par")): - files_with_rpar = glob.glob(os.path.join(raw_directory, "SIMFACTORY/par/*.rpar")) - if len(files_with_rpar) > 0: # if there is an rpar file - rpar_file = files_with_rpar[0] - files_with_par = glob.glob(os.path.join(raw_directory, "SIMFACTORY/par/*.par")) - if len(files_with_par) > 0: # if there is a par file - 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: - 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: - break - - # if we never found a parameter file - if rpar_file is None and par_file is None: - warnings.warn("Unable to locate a .rpar or .par file in this simulation directory.") + if parfile_dict is None or ("par_content" not in parfile_dict and "rpar_content" not in parfile_dict): return parfile_group = h5_file.create_group('parfile') - created_par = False - - # store the rpar file if it exists and create parfile - if rpar_file is not None: - with open(rpar_file, 'r') as f: - content = f.read() - parfile_group.attrs['rpar_content'] = content - if par_file is None: - temporary_rpar = rpar_file[:rpar_file.rfind('/') + 1] + "temp.rpar" - par_file = rpar_file[:rpar_file.rfind('/') + 1] + "temp.par" - with open(temporary_rpar, 'w') as f: - f.write(parfile_group.attrs['rpar_content']) - os.system(f'chmod +x {temporary_rpar}') - os.system('%s' % temporary_rpar) - created_par = True - os.remove(temporary_rpar) - # store the par file - with open(par_file, 'r') as f: - content = f.read() - parfile_group.attrs['par_content'] = content + if "par_content" in parfile_dict: + parfile_group.attrs['par_content'] = parfile_dict['par_content'] - if created_par: - os.remove(par_file) + if "rpar_content" in parfile_dict: + parfile_group.attrs['rpar_content'] = parfile_dict['rpar_content'] - -def _all_relevant_data_filepaths(raw_directory: str, parameter_file_name_base: str) -> dict: +def _all_relevant_data_filepaths(raw_directory: str, parameter_file: str, parameter_file_name_base: str) -> dict: """Dictionary of all relevant data files. The dictionary points from a data type to a filename which in turn points to a list of filepaths with that filename. @@ -1208,7 +1227,7 @@ def _all_relevant_data_filepaths(raw_directory: str, parameter_file_name_base: s dict: dictionary containing prefix -> filename -> list of filepaths to relevant files """ - data_directories = _ordered_data_directories(raw_directory, parameter_file_name_base=parameter_file_name_base) + data_directories = _ordered_data_directories(raw_directory, parameter_file=parameter_file, parameter_file_name_base=parameter_file_name_base) relevant_data_filepaths = {"compact_object": {}, "radiative": {}, "misc": {}} # go through all output directories @@ -1972,12 +1991,7 @@ def create_h5_from_simulation(raw_directory: str, output_directory: str, catalog return simulation_name = _simulation_name(raw_directory) - parameter_file_name_base = _parameter_file_name_base(raw_directory) - - # get all relevant filepaths - relevant_data_filepaths = _all_relevant_data_filepaths(raw_directory, - parameter_file_name_base=parameter_file_name_base) - relevant_output_filepaths = _all_relevant_output_filepaths(raw_directory) + parameter_file_name_base, parameter_file_dict = _get_parameter_file_name_and_content(raw_directory) if catalog_id is not None: h5_filename = os.path.join(output_directory, catalog_id + ".h5") @@ -1989,7 +2003,18 @@ def create_h5_from_simulation(raw_directory: str, output_directory: str, catalog # store parameter file print("storing parameter file") - _store_parameter_file(raw_directory, h5_file) + _store_parameter_file(parameter_file_dict, h5_file) + + # get all relevant filepaths + if "parfile" in h5_file.keys(): + if 'par_content'in h5_file["parfile"].attrs: + parameter_file = h5_file["parfile"].attrs["par_content"] + elif 'rpar_content'in h5_file["parfile"].attrs: + parameter_file = h5_file["parfile"].attrs["rpar_content"] + else: + parameter_file = None + relevant_data_filepaths = _all_relevant_data_filepaths(raw_directory, parameter_file, parameter_file_name_base) + relevant_output_filepaths = _all_relevant_output_filepaths(raw_directory) # process radiative data print("storing radiative information") @@ -2029,9 +2054,13 @@ def get_stitched_data(raw_directory: str, filename: str) -> np.ndarray: warnings.warn("That directory does not exist") return None - parameter_file_name_base = _parameter_file_name_base(raw_directory) + parameter_file_name_base, parameter_file_dict = _get_parameter_file_name_and_content(raw_directory) + + if parameter_file_dict is None or 'par_content' not in parameter_file_dict: + warnings.warn('Unable to determine file structure due to lack of parameter file') + return None - data_directories = _ordered_data_directories(raw_directory, parameter_file_name_base=parameter_file_name_base) + data_directories = _ordered_data_directories(raw_directory, parameter_file=parameter_file_dict['par_content'], parameter_file_name_base=parameter_file_name_base) filepaths = [] # go through all output directories diff --git a/tests/test_postprocessingutils.py b/tests/test_postprocessingutils.py index eb205e6..733b406 100644 --- a/tests/test_postprocessingutils.py +++ b/tests/test_postprocessingutils.py @@ -66,6 +66,17 @@ def tearDownClass(cls) -> None: if os.path.exists(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp")): shutil.rmtree(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp")) + if os.path.exists(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp_stitched")): + shutil.rmtree(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp_stitched")) + + if os.path.exists(os.path.join(TestPostprocessingUtils.CURR_DIR, + "resources/main_test_simulation/stitched/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67.par")): + os.remove(os.path.join(TestPostprocessingUtils.CURR_DIR, + "resources/main_test_simulation/stitched/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67.par")) + + if os.path.exists(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5")): + os.remove(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5")) + def test__simulation_name(self): from mayawaves.utils.postprocessingutils import _simulation_name expected = "D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67" @@ -82,21 +93,238 @@ 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) + # 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") + os.mkdir(temp_raw_directory) + temp_output_directory = os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp_output") + os.mkdir(temp_output_directory) + simfactory_directory = "%s/SIMFACTORY" % temp_raw_directory + os.mkdir(simfactory_directory) + par_directory = "%s/par" % simfactory_directory + os.mkdir(par_directory) + output_0 = "%s/output-0000" % temp_raw_directory + os.mkdir(output_0) + output_1 = "%s/output-0001" % temp_raw_directory + os.mkdir(output_1) + output_2 = "%s/output-0002" % temp_raw_directory + os.mkdir(output_2) - # 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) + # no parfile exists + parameter_file_name_base, parfile_dict = _get_parameter_file_name_and_content(temp_raw_directory) + 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 + 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) + 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') + 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) + with open(expected_par_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.assertTrue('rpar_content' in parfile_dict) + self.assertEqual(expected_par_content, parfile_dict['par_content']) + self.assertEqual(expected_rpar_content, parfile_dict['rpar_content']) + + # par file in output-0001 + shutil.copy2( + os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/main_test_simulation/%s/output-0000/%s.par") % ( + TestPostprocessingUtils.simulation_name, + TestPostprocessingUtils.simulation_name), output_1) + parameter_file_name_base, parfile_dict = _get_parameter_file_name_and_content(temp_raw_directory) + expected_parameter_file = os.path.join(output_1, 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-0001 + shutil.copy2( + os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/main_test_simulation/%s/output-0000/%s.rpar") % ( + TestPostprocessingUtils.simulation_name, + TestPostprocessingUtils.simulation_name), output_1) + parameter_file_name_base, parfile_dict = _get_parameter_file_name_and_content(temp_raw_directory) + expected_rpar_file = os.path.join(output_1, 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) + with open(expected_par_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.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 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_2) + 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') + 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) + with open(expected_par_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.assertTrue('rpar_content' in parfile_dict) + self.assertEqual(expected_par_content, parfile_dict['par_content']) + self.assertEqual(expected_rpar_content, parfile_dict['rpar_content']) + + # par file in simfactory + shutil.copy2( + os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/main_test_simulation/%s/output-0000/%s.par") % ( + TestPostprocessingUtils.simulation_name, + TestPostprocessingUtils.simulation_name), par_directory) + 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() + 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 simfactory + shutil.copy2( + os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/main_test_simulation/%s/output-0000/%s.rpar") % ( + TestPostprocessingUtils.simulation_name, + TestPostprocessingUtils.simulation_name), par_directory) + parameter_file_name_base, parfile_dict = _get_parameter_file_name_and_content(temp_raw_directory) + expected_rpar_file = os.path.join(par_directory, 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) + with open(expected_par_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.assertTrue('rpar_content' in parfile_dict) + self.assertEqual(expected_par_content, parfile_dict['par_content']) + self.assertEqual(expected_rpar_content, parfile_dict['rpar_content']) + + # prestitched data + temp_stitched_directory = os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp_stitched") + os.mkdir(temp_stitched_directory) + + # if rpar in prestitched directory + shutil.copy2(os.path.join(TestPostprocessingUtils.CURR_DIR, + "resources/main_test_simulation/stitched/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67.rpar"), + temp_stitched_directory) + parameter_file_name_base, parfile_dict = _get_parameter_file_name_and_content(temp_stitched_directory) + expected_rpar_file = os.path.join(temp_stitched_directory, + f'{TestPostprocessingUtils.simulation_name}.rpar') + with open(expected_rpar_file) as f: + expected_rpar_content = f.read() + os.system(os.path.join(TestPostprocessingUtils.CURR_DIR, + "resources/main_test_simulation/stitched/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67.rpar")) + expected_par_file = os.path.join(TestPostprocessingUtils.CURR_DIR, + "resources/main_test_simulation/stitched/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67.par") + with open(expected_par_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.assertTrue('rpar_content' in parfile_dict) + print(len(expected_par_content)) + print(len(parfile_dict['par_content'])) + print(expected_par_content[:10]) + print(parfile_dict['par_content'][:10]) + for i in range(len(expected_par_content)): + if expected_par_content[i] != parfile_dict['par_content'][i]: + print(f'{i}: {expected_par_content[i]}, {parfile_dict["par_content"][i]}') + self.assertEqual(expected_par_content, parfile_dict['par_content']) + self.assertEqual(expected_rpar_content, parfile_dict['rpar_content']) + os.remove(os.path.join(TestPostprocessingUtils.CURR_DIR, + "resources/main_test_simulation/stitched/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67.par")) + + # if par in prestitched directory + os.system(os.path.join(TestPostprocessingUtils.CURR_DIR, + "resources/main_test_simulation/stitched/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67.rpar")) + shutil.copy2(os.path.join(TestPostprocessingUtils.CURR_DIR, + "resources/main_test_simulation/stitched/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67.par"), + temp_stitched_directory) + parameter_file_name_base, parfile_dict = _get_parameter_file_name_and_content(temp_stitched_directory) + expected_rpar_file = os.path.join(temp_stitched_directory, + f'{TestPostprocessingUtils.simulation_name}.rpar') + with open(expected_rpar_file) as f: + expected_rpar_content = f.read() + expected_par_file = os.path.join(temp_stitched_directory, + f'{TestPostprocessingUtils.simulation_name}.par') + with open(expected_par_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.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(os.path.join(TestPostprocessingUtils.CURR_DIR, + "resources/main_test_simulation/stitched/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67.par")) + + shutil.rmtree(temp_stitched_directory) + shutil.rmtree(temp_raw_directory) + shutil.rmtree(temp_output_directory) def test__ordered_output_directories(self): from mayawaves.utils.postprocessingutils import _ordered_output_directories @@ -122,12 +350,15 @@ def test__ordered_output_directories(self): def test__ordered_data_directories(self): from mayawaves.utils.postprocessingutils import _ordered_data_directories + parameter_filepath = os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/main_test_simulation/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/SIMFACTORY/par/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67.rpar") + with open(parameter_filepath, 'r') as f: + parfile_content = f.read() # parfile and simulation have same name expected_ordered_data_directories = [os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/main_test_simulation/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/output-0000/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67"), os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/main_test_simulation/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/output-0001/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67")] - actual_ordered_data_directories = _ordered_data_directories(TestPostprocessingUtils.raw_directory, + actual_ordered_data_directories = _ordered_data_directories(TestPostprocessingUtils.raw_directory, parfile_content, TestPostprocessingUtils.parameter_file_name) self.assertTrue(np.all(expected_ordered_data_directories == actual_ordered_data_directories)) @@ -135,7 +366,7 @@ def test__ordered_data_directories(self): expected_ordered_data_directories = [os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67_different_name/output-0000/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67")] actual_ordered_data_directories = _ordered_data_directories(os.path.join(TestPostprocessingUtils.CURR_DIR, - "resources/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67_different_name"), + "resources/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67_different_name"), parfile_content, "D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67") self.assertTrue(np.all(expected_ordered_data_directories == actual_ordered_data_directories)) @@ -145,14 +376,122 @@ def test__ordered_data_directories(self): "resources/main_test_simulation/stitched/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67")] actual_ordered_output_directories = _ordered_data_directories( os.path.join(TestPostprocessingUtils.CURR_DIR, - "resources/main_test_simulation/stitched/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67"), + "resources/main_test_simulation/stitched/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67"), parfile_content, "D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67" ) self.assertTrue(np.all(expected_ordered_output_directories == actual_ordered_output_directories)) + # test when outdir isn't $parfile + parfile_content = """############################################################# +# Output +############################################################# + +IO::out_dir = D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67 +IO::out_fileinfo = "all" + +""" + expected_ordered_data_directories = [os.path.join(TestPostprocessingUtils.CURR_DIR, + "resources/main_test_simulation/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/output-0000/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67"), + os.path.join(TestPostprocessingUtils.CURR_DIR, + "resources/main_test_simulation/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/output-0001/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67")] + actual_ordered_data_directories = _ordered_data_directories(TestPostprocessingUtils.raw_directory, + parfile_content, + TestPostprocessingUtils.parameter_file_name) + self.assertTrue(np.all(expected_ordered_data_directories == actual_ordered_data_directories)) + + # test when outdir isn't $parfile and has quotes + parfile_content = """############################################################# + # Output + ############################################################# + + IO::out_dir = "D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67" + IO::out_fileinfo = "all" + + """ + expected_ordered_data_directories = [os.path.join(TestPostprocessingUtils.CURR_DIR, + "resources/main_test_simulation/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/output-0000/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67"), + os.path.join(TestPostprocessingUtils.CURR_DIR, + "resources/main_test_simulation/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/output-0001/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67")] + actual_ordered_data_directories = _ordered_data_directories(TestPostprocessingUtils.raw_directory, + parfile_content, + TestPostprocessingUtils.parameter_file_name) + self.assertTrue(np.all(expected_ordered_data_directories == actual_ordered_data_directories)) + + # test when outdir isn't $parfile and has quotes + parfile_content = """############################################################# + # Output + ############################################################# + + IO::out_dir = 'D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67' + IO::out_fileinfo = "all" + + """ + expected_ordered_data_directories = [os.path.join(TestPostprocessingUtils.CURR_DIR, + "resources/main_test_simulation/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/output-0000/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67"), + os.path.join(TestPostprocessingUtils.CURR_DIR, + "resources/main_test_simulation/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/output-0001/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67")] + actual_ordered_data_directories = _ordered_data_directories(TestPostprocessingUtils.raw_directory, + parfile_content, + TestPostprocessingUtils.parameter_file_name) + self.assertTrue(np.all(expected_ordered_data_directories == actual_ordered_data_directories)) + + # test when outdir is @SIMULATION_NAME@ + parfile_content = """############################################################# + # Output + ############################################################# + + IO::out_dir = "@SIMULATION_NAME@" + IO::out_fileinfo = "all" + + """ + expected_ordered_data_directories = [os.path.join(TestPostprocessingUtils.CURR_DIR, + "resources/main_test_simulation/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/output-0000/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67"), + os.path.join(TestPostprocessingUtils.CURR_DIR, + "resources/main_test_simulation/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/output-0001/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67")] + actual_ordered_data_directories = _ordered_data_directories(TestPostprocessingUtils.raw_directory, + parfile_content, + 'par_name') + self.assertTrue(np.all(expected_ordered_data_directories == actual_ordered_data_directories)) + + # test when outdir is @SIMULATION_NAME@ + parfile_content = """############################################################# + # Output + ############################################################# + + IO::out_dir = '@SIMULATION_NAME@' + IO::out_fileinfo = "all" + + """ + expected_ordered_data_directories = [os.path.join(TestPostprocessingUtils.CURR_DIR, + "resources/main_test_simulation/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/output-0000/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67"), + os.path.join(TestPostprocessingUtils.CURR_DIR, + "resources/main_test_simulation/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/output-0001/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67")] + actual_ordered_data_directories = _ordered_data_directories(TestPostprocessingUtils.raw_directory, + parfile_content, + 'par_name') + self.assertTrue(np.all(expected_ordered_data_directories == actual_ordered_data_directories)) + + # test when outdir is @SIMULATION_NAME@ + parfile_content = """############################################################# + # Output + ############################################################# + + IO::out_dir = @SIMULATION_NAME@ + IO::out_fileinfo = "all" + + """ + expected_ordered_data_directories = [os.path.join(TestPostprocessingUtils.CURR_DIR, + "resources/main_test_simulation/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/output-0000/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67"), + os.path.join(TestPostprocessingUtils.CURR_DIR, + "resources/main_test_simulation/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/output-0001/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67")] + actual_ordered_data_directories = _ordered_data_directories(TestPostprocessingUtils.raw_directory, + parfile_content, + 'par_name') + self.assertTrue(np.all(expected_ordered_data_directories == actual_ordered_data_directories)) + def test__store_parameter_file(self): - # todo test if there is only an rpar from mayawaves.utils.postprocessingutils import _store_parameter_file + from mayawaves.utils.postprocessingutils import _get_parameter_file_name_and_content temp_raw_directory = os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp_raw") os.mkdir(temp_raw_directory) temp_output_directory = os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp_output") @@ -168,7 +507,8 @@ def test__store_parameter_file(self): # no parfile exists temp_h5_file = h5py.File(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5"), 'w') - _store_parameter_file(temp_raw_directory, temp_h5_file) + parfile_name_base, parfile_dict = _get_parameter_file_name_and_content(temp_raw_directory) + _store_parameter_file(parfile_dict, temp_h5_file) self.assertFalse('parfile' in temp_h5_file.keys()) # clean up @@ -181,7 +521,8 @@ def test__store_parameter_file(self): TestPostprocessingUtils.simulation_name, TestPostprocessingUtils.simulation_name), output_0) temp_h5_file = h5py.File(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5"), 'w') - _store_parameter_file(temp_raw_directory, temp_h5_file) + parfile_name_base, parfile_dict = _get_parameter_file_name_and_content(temp_raw_directory) + _store_parameter_file(parfile_dict, temp_h5_file) expected_parameter_file = os.path.join(output_0, f'{TestPostprocessingUtils.simulation_name}.par') with open(expected_parameter_file) as f: expected_content = f.read() @@ -206,7 +547,8 @@ def test__store_parameter_file(self): TestPostprocessingUtils.simulation_name, TestPostprocessingUtils.simulation_name), output_0) temp_h5_file = h5py.File(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5"), 'w') - _store_parameter_file(temp_raw_directory, temp_h5_file) + parfile_name_base, parfile_dict = _get_parameter_file_name_and_content(temp_raw_directory) + _store_parameter_file(parfile_dict, temp_h5_file) expected_parameter_file = os.path.join(output_0, f'{TestPostprocessingUtils.simulation_name}.rpar') with open(expected_parameter_file) as f: expected_content = f.read() @@ -234,7 +576,8 @@ def test__store_parameter_file(self): TestPostprocessingUtils.simulation_name, TestPostprocessingUtils.simulation_name), output_1) temp_h5_file = h5py.File(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5"), 'w') - _store_parameter_file(temp_raw_directory, temp_h5_file) + parfile_name_base, parfile_dict = _get_parameter_file_name_and_content(temp_raw_directory) + _store_parameter_file(parfile_dict, temp_h5_file) expected_parameter_file = os.path.join(output_1, f'{TestPostprocessingUtils.simulation_name}.par') with open(expected_parameter_file) as f: expected_content = f.read() @@ -259,7 +602,8 @@ def test__store_parameter_file(self): TestPostprocessingUtils.simulation_name, TestPostprocessingUtils.simulation_name), output_1) temp_h5_file = h5py.File(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5"), 'w') - _store_parameter_file(temp_raw_directory, temp_h5_file) + parfile_name_base, parfile_dict = _get_parameter_file_name_and_content(temp_raw_directory) + _store_parameter_file(parfile_dict, temp_h5_file) expected_parameter_file = os.path.join(output_1, f'{TestPostprocessingUtils.simulation_name}.rpar') with open(expected_parameter_file) as f: expected_content = f.read() @@ -287,7 +631,8 @@ def test__store_parameter_file(self): TestPostprocessingUtils.simulation_name, TestPostprocessingUtils.simulation_name), par_directory) temp_h5_file = h5py.File(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5"), 'w') - _store_parameter_file(temp_raw_directory, temp_h5_file) + parfile_name_base, parfile_dict = _get_parameter_file_name_and_content(temp_raw_directory) + _store_parameter_file(parfile_dict, temp_h5_file) expected_parameter_file = os.path.join(par_directory, f'{TestPostprocessingUtils.simulation_name}.par') with open(expected_parameter_file) as f: expected_content = f.read() @@ -312,7 +657,8 @@ def test__store_parameter_file(self): TestPostprocessingUtils.simulation_name, TestPostprocessingUtils.simulation_name), par_directory) temp_h5_file = h5py.File(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5"), 'w') - _store_parameter_file(temp_raw_directory, temp_h5_file) + parfile_name_base, parfile_dict = _get_parameter_file_name_and_content(temp_raw_directory) + _store_parameter_file(parfile_dict, temp_h5_file) expected_parameter_file = os.path.join(par_directory, f'{TestPostprocessingUtils.simulation_name}.rpar') with open(expected_parameter_file) as f: expected_content = f.read() @@ -345,7 +691,8 @@ def test__store_parameter_file(self): "resources/main_test_simulation/stitched/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67.par"), temp_stitched_directory) temp_h5_file = h5py.File(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5"), 'w') - _store_parameter_file(temp_stitched_directory, temp_h5_file) + parfile_name_base, parfile_dict = _get_parameter_file_name_and_content(temp_stitched_directory) + _store_parameter_file(parfile_dict, temp_h5_file) expected_parameter_file = os.path.join(temp_stitched_directory, f'{TestPostprocessingUtils.simulation_name}.par') with open(expected_parameter_file) as f: @@ -373,7 +720,8 @@ def test__store_parameter_file(self): "resources/main_test_simulation/stitched/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67.rpar"), temp_stitched_directory) temp_h5_file = h5py.File(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5"), 'w') - _store_parameter_file(temp_stitched_directory, temp_h5_file) + parfile_name_base, parfile_dict = _get_parameter_file_name_and_content(temp_stitched_directory) + _store_parameter_file(parfile_dict, temp_h5_file) expected_parameter_file = os.path.join(temp_stitched_directory, f'{TestPostprocessingUtils.simulation_name}.rpar') with open(expected_parameter_file) as f: @@ -400,15 +748,77 @@ def test__store_parameter_file(self): shutil.rmtree(temp_raw_directory) shutil.rmtree(temp_output_directory) + # parfile dict is None + parfile_dict = None + temp_h5_file = h5py.File(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5"), 'w') + _store_parameter_file(parfile_dict, temp_h5_file) + self.assertFalse('parfile' in temp_h5_file.keys()) + temp_h5_file.close() + os.remove(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5")) + + # neither par_content nor rpar_content in parfile_dict + parfile_dict = {} + temp_h5_file = h5py.File(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5"), 'w') + _store_parameter_file(parfile_dict, temp_h5_file) + self.assertFalse('parfile' in temp_h5_file.keys()) + temp_h5_file.close() + os.remove(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5")) + + # both par_content and rpar_content in parfile_dict + parfile_dict = { + 'par_content': 'ABCDE', + 'rpar_content': 'FGHIJ' + } + temp_h5_file = h5py.File(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5"), 'w') + _store_parameter_file(parfile_dict, temp_h5_file) + self.assertTrue('parfile' in temp_h5_file.keys()) + self.assertTrue('par_content' in temp_h5_file['parfile'].attrs) + self.assertTrue('rpar_content' in temp_h5_file['parfile'].attrs) + self.assertEqual('ABCDE', temp_h5_file['parfile'].attrs['par_content']) + self.assertEqual('FGHIJ', temp_h5_file['parfile'].attrs['rpar_content']) + temp_h5_file.close() + os.remove(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5")) + + # par_content in parfile_dict + parfile_dict = { + 'par_content': '12345' + } + temp_h5_file = h5py.File(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5"), 'w') + _store_parameter_file(parfile_dict, temp_h5_file) + self.assertTrue('parfile' in temp_h5_file.keys()) + self.assertTrue('par_content' in temp_h5_file['parfile'].attrs) + self.assertFalse('rpar_content' in temp_h5_file['parfile'].attrs) + self.assertEqual('12345', temp_h5_file['parfile'].attrs['par_content']) + temp_h5_file.close() + os.remove(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5")) + + # rpar_content in parfile_dict + parfile_dict = { + 'rpar_content': '67890' + } + temp_h5_file = h5py.File(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5"), 'w') + _store_parameter_file(parfile_dict, temp_h5_file) + self.assertTrue('parfile' in temp_h5_file.keys()) + self.assertFalse('par_content' in temp_h5_file['parfile'].attrs) + self.assertTrue('rpar_content' in temp_h5_file['parfile'].attrs) + self.assertEqual('67890', temp_h5_file['parfile'].attrs['rpar_content']) + temp_h5_file.close() + os.remove(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/temp.h5")) + def test__all_relevant_data_filepaths(self): from mayawaves.utils.postprocessingutils import _all_relevant_data_filepaths from mayawaves.utils.postprocessingutils import _RadiativeFilenames from mayawaves.utils.postprocessingutils import _CompactObjectFilenames from mayawaves.utils.postprocessingutils import _MiscDataFilenames + parameter_filepath = os.path.join(TestPostprocessingUtils.CURR_DIR, + "resources/main_test_simulation/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67/SIMFACTORY/par/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67.rpar") + with open(parameter_filepath, 'r') as f: + parfile_content = f.read() + # simulation has same name as parameter file actual_relevant_data_filepaths = _all_relevant_data_filepaths( - TestPostprocessingUtils.raw_directory, TestPostprocessingUtils.parameter_file_name) + TestPostprocessingUtils.raw_directory, parfile_content, TestPostprocessingUtils.parameter_file_name) actual_psi4_filetypes = set(list(actual_relevant_data_filepaths["radiative"].keys())) expected_psi4_filetypes = {_RadiativeFilenames.YLM_WEYLSCAL4_ASC} @@ -480,6 +890,7 @@ def test__all_relevant_data_filepaths(self): # simulation has different name than parameter file actual_relevant_data_filepaths = _all_relevant_data_filepaths(os.path.join(TestPostprocessingUtils.CURR_DIR, "resources/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67_different_name"), + parfile_content, "D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67") actual_included_prefixes = set(list(actual_relevant_data_filepaths["radiative"].keys()) + list(actual_relevant_data_filepaths["compact_object"].keys()) @@ -492,7 +903,7 @@ def test__all_relevant_data_filepaths(self): # test prestitched data actual_relevant_data_filepaths = _all_relevant_data_filepaths( os.path.join(TestPostprocessingUtils.CURR_DIR, - "resources/main_test_simulation/stitched/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67"), + "resources/main_test_simulation/stitched/D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67"), parfile_content, "D2.33_q1_a1_0_0_0_a2_0_0_0_m42.67") actual_psi4_filetypes = set(list(actual_relevant_data_filepaths["radiative"].keys())) diff --git a/tests/test_quasilocalmeasures.py b/tests/test_quasilocalmeasures.py index dcaa283..ce2af0e 100644 --- a/tests/test_quasilocalmeasures.py +++ b/tests/test_quasilocalmeasures.py @@ -159,6 +159,42 @@ def test_store_compact_object_data_from_filetype(self): temp_h5_file.close() os.remove(os.path.join(TestQuasiLocalMeasures.CURR_DIR, "resources/temp.h5")) + # multiple outputs, 3 tracked surfaces, rpar data only + simulation_name = "GW150914" + filepaths = { + "quasilocalmeasures-qlm_scalars..asc": + [ + os.path.join(TestQuasiLocalMeasures.CURR_DIR, + "resources/sample_etk_simulations/%s/output-0000/%s/quasilocalmeasures-qlm_scalars..asc") % ( + simulation_name, simulation_name), + os.path.join(TestQuasiLocalMeasures.CURR_DIR, + "resources/sample_etk_simulations/%s/output-0001/%s/quasilocalmeasures-qlm_scalars..asc") % ( + simulation_name, simulation_name), + os.path.join(TestQuasiLocalMeasures.CURR_DIR, + "resources/sample_etk_simulations/%s/output-0002/%s/quasilocalmeasures-qlm_scalars..asc") % ( + simulation_name, simulation_name) + ] + } + rpar_file_path = os.path.join(TestQuasiLocalMeasures.CURR_DIR, + "resources/sample_etk_simulations/%s/output-0000/%s.rpar") % ( + simulation_name, simulation_name) + temp_h5_file = h5py.File(os.path.join(TestQuasiLocalMeasures.CURR_DIR, "resources/temp.h5"), 'w') + parfile_group = temp_h5_file.create_group('parfile') + with open(rpar_file_path) as f: + parfile_group.attrs['rpar_content'] = f.read() + compact_object_dict = {} + actual_compact_object_dict, metadata_dict = _QuasiLocalMeasures.store_compact_object_data_from_filetype( + filepaths, + compact_object_dict, + parfile_group) + + expected_compact_objects = [0, 1, 2] + actual_compact_objects = sorted(actual_compact_object_dict.keys()) + self.assertEqual(expected_compact_objects, actual_compact_objects) + + temp_h5_file.close() + os.remove(os.path.join(TestQuasiLocalMeasures.CURR_DIR, "resources/temp.h5")) + # multiple outputs, 3 tracked surfaces simulation_name = "GW150914" filepaths = {