def get_h5_file_name_prefix(sc_model_id): """ :param sc_model_id: :return: """ # basic check if sc_model_id not in FileSystemDefinitions.h5_file_modelids: Debug.dl( "FileSystemDefinitions: Folder '{0}' has no files starting with '{1}'." .format(Settings.get("model_data_folder_path"), file_prefix), 1, debug_lvl) return None sc_model_idx = FileSystemDefinitions.h5_file_modelids.index( sc_model_id) return FileSystemDefinitions.h5_file_prefixes[sc_model_idx]
def get_index_data_from_hdf5_file_v2(hdf5_file_path, debug_lvl=0): """ Read HDF5 file :param hdf5_file_path: :param debug_lvl: :return: Array of linkids and array parameters matrix """ # basic check if (hdf5_file_path is None) or (not os.path.exists(hdf5_file_path)): Debug.dl( "bingen_asynchmodel254_hdf5: File '{0}' does not exist.". format(hdf5_file_path), debug_lvl, 1) return None, None # read file content with h5py.File(hdf5_file_path, 'r') as hdf_file: hdf_data = np.array(hdf_file.get('snapshot')) return hdf_data
def get_most_recent_file_with_prefix(file_prefix, debug_lvl=0): """ :param file_prefix: :param debug_lvl: :return: """ all_files = os.listdir(Settings.get("model_data_folder_path")) if len(all_files) <= 0: Debug.dl( "FileSystemDefinitions: Folder {0} is empty.".format( Settings.get("model_data_folder_path")), 1, debug_lvl) return None all_relevant_files = [] for cur_raw_file in all_files: if cur_raw_file.startswith(file_prefix): all_relevant_files.append(cur_raw_file) if len(all_relevant_files) <= 0: Debug.dl( "FileSystemDefinitions: Folder '{0}' has no files starting with '{1}'." .format(Settings.get("model_data_folder_path"), file_prefix), 1, debug_lvl) return None most_recent_filename = sorted(all_relevant_files, reverse=True)[0] Debug.dl( "FileSystemDefinitions: Most recent file is '{0}'.".format( most_recent_filename), 1, debug_lvl) return os.path.join(Settings.get("model_data_folder_path"), most_recent_filename)
def get_current_rounded_timestamp_from_hdf5_files(debug_lvl=0): """ :param debug_lvl: :return: """ # basic check model_output_folder_path = Settings.get("model_data_folder_path") if model_output_folder_path is None: Debug.dl( "FileSystemDefinitions: Invalid output folder path.".format( output_folder_path), debug_lvl, 1) return None elif not os.path.exists(model_output_folder_path): Debug.dl( "FileSystemDefinitions: Not found '{0}'.".format( model_output_folder_path), debug_lvl, 1) return None all_file_names_raw = os.listdir(model_output_folder_path) # basic check - must have at least one file if len(all_file_names_raw) == 0: Debug.dl( "FileSystemDefinitions: Not found '{0}'.".format( model_output_folder_path), debug_lvl, 1) return None Debug.dl( "FileSystemDefinitions: Scanning folder '{0}'.".format( model_output_folder_path), debug_lvl, 1) all_file_names = [] for cur_file_name in all_file_names_raw: if cur_file_name.startswith(FileSystemDefinitions.h5_file_prefix): all_file_names.append(cur_file_name) # basic check if len(all_file_names) <= 0: print( "FileSystemDefinitions: No flies with '{0}' prefix on '{1}' folder." .format(FileSystemDefinitions.h5_file_prefix, model_output_folder_path)) return else: print( "FileSystemDefinitions: Found {0} files with prefix '{1}' at '{2}'." .format(len(all_file_names), FileSystemDefinitions.h5_file_prefix, model_output_folder_path)) all_file_names.sort(reverse=True) # define most recent round timestamp most_recent_file_name = all_file_names[0] most_recent_timestamp = FileSystemDefinitions.retrieve_timestamp_from_hdf5_state_filename( most_recent_file_name, debug_lvl=debug_lvl) most_recent_datetime = datetime.datetime.fromtimestamp( most_recent_timestamp) most_recent_minute = most_recent_datetime.minute if most_recent_minute >= 30: return most_recent_timestamp replaced = most_recent_datetime.replace(minute=0, second=0, microsecond=0) most_recent_round_timestamp = time.mktime(replaced.timetuple()) # identify the closest available timestamp to most recent rounded timestamp closest_timestamp = None min_dist_to_round = 100000000 for cur_file_name in all_file_names: cur_file_timestamp = FileSystemDefinitions.retrieve_timestamp_from_hdf5_state_filename( cur_file_name, debug_lvl=debug_lvl) if cur_file_timestamp is None: continue cur_dist_to_round = abs(cur_file_timestamp - most_recent_round_timestamp) if cur_dist_to_round < min_dist_to_round: min_dist_to_round = cur_dist_to_round closest_timestamp = cur_file_timestamp return closest_timestamp