def search_for_projects(self): """ Search for any projects already created. This will look in the directory set in the config.ini file for any HDF5 files already created. If the file already exist in the directory, then the project is added to the list. :return: """ # Look inside the folder for any hdf5 files for file in os.listdir(self.config.config['RIVER']['output_dir']): if file.endswith(".hdf5"): # The project name is the file name without the extension prj_name = os.path.splitext(file) # Use the first part of prj_name which the file name # The second value is the file extension project_name_without_ext = prj_name[0] # Create a project and add it ot the list project = RiverProject( self.config, project_name_without_ext, os.path.join(self.config.config['RIVER']['output_dir'], file)) self.projects[project_name_without_ext] = project
def add_project(self, name: str, project_file_path: str) -> RiverProject: """ Add a project to the list of projects. If the no HDF5 project file can be found, return none. :param name: Name of the project. :param project_file_path: Project File path :return: Project file path if it exist or None if no HDF5 file exist. """ if os.path.exists(project_file_path): # Check if the project already exist # If it does, create a new project name with an index project_already_exist_index = 0 # Create files paths until a unique file name is found while name in self.projects.keys(): # Create a new name with an index name = name + "_" + str(project_already_exist_index) project_already_exist_index += 1 # Add project to the list self.projects[name] = RiverProject(self.config, name, project_file_path) # Create the H5DF file object to verify subgroups exist with h5py.File(project_file_path, "a") as prj_file: # Check if Transect Subgroup is in the project file if RiverProjectMeta.SUBGROUP_TRANSECT not in prj_file: # Create a folder for transects to be stored prj_file.create_group(RiverProjectMeta.SUBGROUP_TRANSECT) # Check if the Moving Bed Test Subgroup is in the project file if RiverProjectMeta.SUBGROUP_MBT not in prj_file: # Create a folder for moving bed tests prj_file.create_group(RiverProjectMeta.SUBGROUP_MBT) # Return the project file return self.projects[name] # Return None if the project HDF5 does not exist return None
def create_project(self, name: str): """ Create a project and add it to the list of projects. This will use the name given to create a new project file. The file is a HDF5 file. The project file will contain all the settings and transects and discharge calculation results. The HDF5 file is created in the directory set in the config.ini file. If the project already exist, it will create the project with an index number at the end of project name. :param name: Name of the project. :return: Return the HDF5 project file. """ # Create a file name and path file_path = os.path.join(self.config.config['RIVER']['output_dir'], name + ".hdf5") # Check if the project already exist # If it does, create a new project name with an index file_already_exist_index = 0 # Create a new name with an index new_name = name + "_" + str(file_already_exist_index) if os.path.exists(file_path): # Create a new file with index file_path = os.path.join(self.config.config['RIVER']['output_dir'], new_name + ".hdf5") # Create files paths until a unique file name is found while os.path.exists(file_path): file_already_exist_index += 1 new_name = name + "_" + str(file_already_exist_index) file_path = os.path.join( self.config.config['RIVER']['output_dir'], new_name + ".hdf5") # Create a project prj_file_path = os.path.join( self.config.config['RIVER']['output_dir'], new_name + ".hdf5") project = RiverProject(self.config, new_name, prj_file_path) # Add project to the list self.projects[new_name] = project # Set the name variable to new_name now # This is set to the name can be set in the attr for the project name = new_name else: # Create a project prj_file_path = os.path.join( self.config.config['RIVER']['output_dir'], name + ".hdf5") project = RiverProject(self.config, name, prj_file_path) # Add project to the list using original project name self.projects[name] = project # Create the H5DF file with h5py.File(file_path, "a") as prj_file: # Create a folder for transects to be stored prj_file.create_group(RiverProjectMeta.SUBGROUP_TRANSECT) # Create a folder for moving bed tests prj_file.create_group(RiverProjectMeta.SUBGROUP_MBT) # Set meta data for the project file prj_file.attrs[RiverProjectMeta.PROJECT_NAME] = name prj_file.attrs[RiverProjectMeta.PROJECT_FILE_PATH] = os.path.join( self.config.config['RIVER']['output_dir'], name + ".hdf5") # Return the path to the HDF5 project file created return self.projects[name]
def test_save_project_meta(): project = RiverProject(rti_config, "Project114", os.path.join(rti_config.config['RIVER']['output_dir'], "Project114.hdf5")) project.adcp_serial_num = "0001" project.firmware_ver = "0.0.1.2" project.draft_transducer = 11.11 project.magnetic_declination = 1.234 project.software_ver = "1.2.3.4" project.ident_of_crew = "Jon and Bob" project.station_name = "River1" project.station_id = "111R1" project.water_temp = 1.34 project.percentage_discharged_measured = 23.4 project.channel_width = 1234 project.cross_section_area = 34 project.comments = "everything is good" project.save_project_meta() with h5py.File(project.file_path, "a") as prj_file: assert "Project114" == prj_file.attrs[RiverProjectMeta.PROJECT_NAME] assert "0001" == prj_file.attrs[RiverProjectMeta.PROJECT_SERIAL_NUM] assert "0.0.1.2" == prj_file.attrs[RiverProjectMeta.PROJECT_FIRMWARE_VER] assert 11.11 == prj_file.attrs[RiverProjectMeta.PROJECT_DRAFT_XDCR] assert 1.234 == prj_file.attrs[RiverProjectMeta.PROJECT_MAG_DECL] assert "1.2.3.4" == prj_file.attrs[RiverProjectMeta.PROJECT_SOFT_VER] assert "Jon and Bob" == prj_file.attrs[RiverProjectMeta.PROJECT_IDENT_CREW] assert "River1" == prj_file.attrs[RiverProjectMeta.PROJECT_STATION_NAME] assert "111R1" == prj_file.attrs[RiverProjectMeta.PROJECT_STATION_ID] assert 1.34 == prj_file.attrs[RiverProjectMeta.PROJECT_WATER_TEMP] assert 23.4 == prj_file.attrs[RiverProjectMeta.PROJECT_PCT_DISCHARGED_MEASURED] assert 1234 == prj_file.attrs[RiverProjectMeta.PROJECT_CHANNEL_WIDTH] assert 34 == prj_file.attrs[RiverProjectMeta.PROJECT_CROSS_SEC_AREA] assert "everything is good" == prj_file.attrs[RiverProjectMeta.PROJECT_COMMENTS]
def test_set_values(): project = RiverProject(rti_config, "Project112", os.path.join(rti_config.config['RIVER']['output_dir'], "Project112.hdf5")) project.adcp_serial_num = "0001" project.firmware_ver = "0.0.1.2" project.draft_transducer = 11.11 project.magnetic_declination = 1.234 project.software_ver = "1.2.3.4" project.ident_of_crew = "Jon and Bob" project.station_name = "River1" project.station_id = "111R1" project.water_temp = 1.34 project.percentage_discharged_measured = 23.4 project.channel_width = 1234 project.cross_section_area = 34 project.comments = "everything is good" assert "0001" == project.adcp_serial_num assert "0.0.1.2" == project.firmware_ver assert 11.11 == project.draft_transducer assert 1.234 == project.magnetic_declination assert "1.2.3.4" == project.software_ver assert "Jon and Bob" == project.ident_of_crew assert "River1" == project.station_name assert "111R1" == project.station_id assert 1.34 == project.water_temp assert 23.4 == project.percentage_discharged_measured assert 1234 == project.channel_width assert 34 == project.cross_section_area assert "everything is good" == project.comments
def test_constructor(): project = RiverProject(rti_config, "Project111", os.path.join(rti_config.config['RIVER']['output_dir'], "Project111.hdf5")) assert "Project111" == project.name assert os.path.join(rti_config.config['RIVER']['output_dir'], "Project111.hdf5") == project.file_path