Skip to content

Commit

Permalink
fix output path issue
Browse files Browse the repository at this point in the history
  • Loading branch information
changliao1025 committed Apr 17, 2024
1 parent 336648a commit 1de2c32
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 19 deletions.
42 changes: 25 additions & 17 deletions pyhexwatershed/classes/pycase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 17 additions & 2 deletions pyhexwatershed/pyhexwatershed_read_model_configuration_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit 1de2c32

Please sign in to comment.