From 1de2c32032950486b54dac0c2b874b121c9bc7f9 Mon Sep 17 00:00:00 2001 From: Chang Liao <20618384+changliao1025@users.noreply.github.com> Date: Tue, 16 Apr 2024 19:31:32 -0600 Subject: [PATCH] fix output path issue --- pyhexwatershed/classes/pycase.py | 42 +++++++++++-------- ...watershed_read_model_configuration_file.py | 19 ++++++++- 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/pyhexwatershed/classes/pycase.py b/pyhexwatershed/classes/pycase.py index 7d4a8cf..0231988 100644 --- a/pyhexwatershed/classes/pycase.py +++ b/pyhexwatershed/classes/pycase.py @@ -281,9 +281,15 @@ def __init__(self, aConfig_in): self.sCase = sCase sPath = str(Path(self.sWorkspace_output) / sCase) - self.sWorkspace_output = sPath - Path(sPath).mkdir(parents=True, exist_ok=True) - + self.sWorkspace_output = sPath + try: + Path(sPath).mkdir(parents=True, exist_ok=True) + except Exception as e: + print(f"Failed to create directory {sPath} due to error: {e}") + print('You should provide a valid path to create the output directory') + return + else: + print(f"Directory {sPath} created successfully") if 'sMesh_type' in aConfig_in: self.sMesh_type = aConfig_in['sMesh_type'] @@ -427,12 +433,7 @@ def pyhexwatershed_setup(self): copy2(self.sFilename_hexwatershed_bin, sFilename_new) os.chmod(sFilename_new, stat.S_IRWXU ) pass - else: - # Get the distribution object for the package - distribution = pkg_resources.get_distribution('hexwatershed') - # Get the installation path for the package - sPath_installation = distribution.location - + else: if system == 'Windows': sFilename_executable = 'hexwatershed.exe' else: @@ -448,8 +449,15 @@ def pyhexwatershed_setup(self): else: print('Binary not found in system path.') if iFlag_found_binary ==1: + sFilename_new = os.path.join(str(Path(self.sWorkspace_output_hexwatershed) ) , sFilename_executable ) + copy2(sFilename_hexwatershed_bin, sFilename_new) + os.chmod(sFilename_new, stat.S_IRWXU ) pass - else: + else: + # Get the distribution object for the package + distribution = pkg_resources.get_distribution('hexwatershed') + # Get the installation path for the package + sPath_installation = distribution.location sFilename_hexwatershed_bin = os.path.join(str(Path(sPath_installation + '/pyhexwatershed/_bin/') ) , sFilename_executable ) if os.path.isfile(sFilename_hexwatershed_bin): iFlag_found_binary=1 @@ -510,7 +518,7 @@ def pyhexwatershed_run_hexwatershed(self): elif system == 'Darwin': print('Running on a Unix-based system') #run the model using bash - self.generate_bash_script() + self.pyhexwatershed_generate_bash_script() os.chdir(self.sWorkspace_output_hexwatershed) sCommand = "./run_hexwatershed.sh" #print(sCommand) @@ -1105,14 +1113,14 @@ def pyhexwatershed_export_all_polygon_variables(self): aVariable_geojson = ['subbasin','hillslope','area','elevation', 'slope', 'drainage_area','travel_distance'] aVariable_type= [1,1,2,2,2,2,2] - #export_json_to_geojson_polygon(sFilename_json, - # sFilename_geojson, - # aVariable_json, - # aVariable_geojson, - # aVariable_type) + export_json_to_geojson_polygon(sFilename_json, + sFilename_geojson, + aVariable_json, + aVariable_geojson, + aVariable_type) #convert to geoparquet for visualization sFilename_parquet = sFilename_geojson.replace('.geojson','.parquet') - #convert_geojson_to_geoparquet(sFilename_geojson, sFilename_parquet) + convert_geojson_to_geoparquet(sFilename_geojson, sFilename_parquet) #because each geojson file has many small polygons, we can merge them into large polygons #get the folder of the geojson sFolder = os.path.dirname(sFilename_geojson) diff --git a/pyhexwatershed/pyhexwatershed_read_model_configuration_file.py b/pyhexwatershed/pyhexwatershed_read_model_configuration_file.py index 3354b03..b6631ef 100644 --- a/pyhexwatershed/pyhexwatershed_read_model_configuration_file.py +++ b/pyhexwatershed/pyhexwatershed_read_model_configuration_file.py @@ -18,7 +18,8 @@ def pyhexwatershed_read_model_configuration_file(sFilename_configuration_in, dResolution_meter_in = None, sDate_in = None, sDggrid_type_in = None, - sMesh_type_in=None): + sMesh_type_in=None, + sWorkspace_output_in=None): # Opening JSON file @@ -86,6 +87,19 @@ def pyhexwatershed_read_model_configuration_file(sFilename_configuration_in, dResolution_meter = float( aConfig['dResolution_meter']) pass + if sWorkspace_output_in is not None: + sWorkspace_output = sWorkspace_output_in + else: + sWorkspace_output = aConfig['sWorkspace_output'] + # try to create this output folder first using + + try: + print(sWorkspace_output) + Path(sWorkspace_output).mkdir(parents=True, exist_ok=True) + except ValueError: + print("The specified output workspace cannot be created!") + exit + aConfig["sDate"] = sDate aConfig["sMesh_type"] = sMesh_type aConfig["iCase_index"] = iCase_index @@ -96,6 +110,7 @@ def pyhexwatershed_read_model_configuration_file(sFilename_configuration_in, aConfig["dResolution_meter"] = dResolution_meter aConfig["sFilename_model_configuration"] = sFilename_configuration_in aConfig["sDggrid_type"] = sDggrid_type + aConfig["sWorkspace_output"] = sWorkspace_output oPyhexwatershed = None oPyflowline = None @@ -110,7 +125,7 @@ def pyhexwatershed_read_model_configuration_file(sFilename_configuration_in, #set up the basin object #check flowline flag - if oPyhexwatershed.iFlag_flowline == 1: + if oPyhexwatershed.iFlag_flowline == 1 and os.path.exists(oPyhexwatershed.sFilename_basins): with open(oPyhexwatershed.sFilename_basins) as json_file: dummy_data = json.load(json_file) for i in range(oPyhexwatershed.nOutlet):