Skip to content

Commit

Permalink
try merge
Browse files Browse the repository at this point in the history
  • Loading branch information
changliao1025 committed May 6, 2024
2 parents 1dfec08 + 2af7bc4 commit 2730164
Show file tree
Hide file tree
Showing 7 changed files with 412 additions and 30 deletions.
6 changes: 3 additions & 3 deletions examples/create_model_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from os.path import realpath
import argparse

from pyflowline.configuration.create_template_configuration_file import pyflowline_create_template_configuration_file
from pyflowline.configuration.pyflowline_create_template_configuration_file import pyflowline_create_template_configuration_file

parser = argparse.ArgumentParser()
parser.add_argument("--sMesh_type", help = "sMesh_type", type = str)
Expand Down Expand Up @@ -37,10 +37,10 @@
sWorkspace_output= str(Path(sWorkspace_data) / 'output')

sFilename_configuration_in = realpath( sPath + '/tests/configurations/template.json' )

oPyflowline = pyflowline_create_template_configuration_file(sFilename_configuration_in,\
sWorkspace_input, sWorkspace_output, iFlag_use_mesh_dem_in = 1,sMesh_type_in=sMesh_type, iCase_index_in = iCase_index, sDate_in = sDate)

print(oPyflowline.tojson())

print('Finished')
Expand Down
4 changes: 3 additions & 1 deletion examples/susquehanna/run_simulation_hexagon.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
sPath_parent = str(Path(__file__).parents[2]) # data is located two dir's up
import sys
sys.path.append(sPath_parent)

from pyflowline.configuration.read_configuration_file import pyflowline_read_configuration_file


sPath_data = realpath( sPath_parent + '/data/susquehanna' )
sWorkspace_input = str(Path(sPath_data) / 'input')
sWorkspace_output= str(Path(sPath_data) / 'output')
Expand Down Expand Up @@ -36,7 +38,7 @@
oPyflowline.reconstruct_topological_relationship(aCell)
oPyflowline.export()
iCase_index= iCase_index+1

print('Finished')

# %%
6 changes: 4 additions & 2 deletions examples/susquehanna/run_simulation_latlon.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
sPath_parent = str(Path(__file__).parents[2]) # data is located two dir's up
import sys
sys.path.append(sPath_parent)
from pyflowline.configuration.read_configuration_file import pyflowline_read_configuration_file

from pyflowline.configuration.pyflowline_read_configuration_file import pyflowline_read_configuration_file

sPath_data = realpath( sPath_parent + '/data/susquehanna' )
sWorkspace_input = str(Path(sPath_data) / 'input')
sWorkspace_output= str(Path(sPath_data) / 'output')
Expand All @@ -29,7 +31,7 @@
oPyflowline = pyflowline_read_configuration_file(sFilename_configuration_in, \
iCase_index_in=iCase_index, dResolution_meter_in=dResolution_meter, sDate_in=sDate)

#%%
#%%
oPyflowline.aBasin[0].dLatitude_outlet_degree=39.462000
oPyflowline.aBasin[0].dLongitude_outlet_degree=-76.009300
oPyflowline.setup()
Expand Down
40 changes: 21 additions & 19 deletions examples/susquehanna/run_simulation_mpas.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os, sys
from pathlib import Path


from pyflowline.configuration.read_configuration_file import pyflowline_read_configuration_file
from pyflowline.configuration.change_json_key_value import pyflowline_change_json_key_value
from pyflowline.configuration import path_manager as pyflowline_path_manager


#%% Define the case information (configuration file parameters)
sDomainName = 'susquehanna'
iCase_index = 1
Expand Down Expand Up @@ -62,45 +64,45 @@
# Set the path to the output folder
# Pass the configuration filename followed by a single key-value pair.
pyflowline_change_json_key_value(
oFilename_domain_config,
oFilename_domain_config,
'sWorkspace_output', str(oFolder_output))

# Set the path to the mpas mesh file
pyflowline_change_json_key_value(
oFilename_domain_config,
oFilename_domain_config,
'sFilename_mesh_netcdf', str(oFilename_mesh_netcdf))

# Set the path to the boundary file used to clip the mesh
pyflowline_change_json_key_value(
oFilename_domain_config,
'sFilename_mesh_boundary', str(oFilename_mesh_boundary))
oFilename_domain_config,
'sFilename_mesh_boundary', str(oFilename_mesh_boundary))

# Set the path to the basin ("child") configuration file
pyflowline_change_json_key_value(
oFilename_domain_config,
oFilename_domain_config,
'sFilename_basins', str(oFilename_basins_config))

# These parameters are not strictly necessary, but will reduce the potential for errors or confusion.
pyflowline_change_json_key_value(
oFilename_domain_config,
oFilename_domain_config,
'sFilename_model_configuration', str(oFilename_domain_config))

pyflowline_change_json_key_value(
oFilename_domain_config,
oFilename_domain_config,
'sWorkspace_data', str(oPath_parent.joinpath('data')))

#%% Update the basin ("child") configuration file

# Set the path to the flowline file.
# Set the path to the flowline file.
pyflowline_change_json_key_value(
oFilename_basins_config,
'sFilename_flowline_filter', str(oFilename_flowline),
oFilename_basins_config,
'sFilename_flowline_filter', str(oFilename_flowline),
iFlag_basin_in=1) # Set iFlag_basin_in=1 when changing the basin configuration file.

#%% Read the configuration file
oPyflowline = pyflowline_read_configuration_file(
oFilename_domain_config,
iCase_index_in=iCase_index,
oFilename_domain_config,
iCase_index_in=iCase_index,
sDate_in=sDate)

#%% Check the model parameters
Expand All @@ -114,11 +116,11 @@

# To change a parameter for a basin instead of the whole model domain, use the iFlag_basin_in option, this will change the value for all of the basins in the basin configuration file.
oPyflowline.pyflowline_change_model_parameter(
'dLatitude_outlet_degree', 39.462000,
'dLatitude_outlet_degree', 39.462000,
iFlag_basin_in=1)

oPyflowline.pyflowline_change_model_parameter(
'dLongitude_outlet_degree', -76.009300,
'dLongitude_outlet_degree', -76.009300,
iFlag_basin_in=1)

# The second way is to assign a value directly
Expand All @@ -140,12 +142,12 @@

#%%
if iFlag_visualization == 1:

aExtent_meander = [-76.5, -76.2, 41.6, 41.9]
# oPyflowline.plot(sVariable_in='flowline_simplified', sFilename_output_in='flowline_simplified.png')
# oPyflowline.plot(sVariable_in='flowline_simplified',
# sFilename_output_in='flowline_simplified_zoom.png',
# aExtent_in=aExtent_meander)
# oPyflowline.plot(sVariable_in='flowline_simplified',
# sFilename_output_in='flowline_simplified_zoom.png',
# aExtent_in=aExtent_meander)
pass

#%%
Expand All @@ -155,7 +157,7 @@
#%%
if iFlag_visualization == 1:
# This will fail unless iFlag_simulation is true or has been run
# oPyflowline.plot(sVariable_in='mesh', sFilename_output_in='mesh.png' )
# oPyflowline.plot(sVariable_in='mesh', sFilename_output_in='mesh.png' )
pass
#%%
if iFlag_simulation == 1:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
import os
from pathlib import Path
#use this function to generate an initial json file for hexwatershed
import json
#once it's generated, you can modify it and use it for different simulations
from pyflowline.classes.pycase import flowlinecase
from pyflowline.classes.basin import pybasin

def pyflowline_create_template_basin_configuration_file(
sFilename_basins_json,
nBasin,
sWorkspace_input_in,
sWorkspace_output_in):
"""generate basin configuration
Args:
sFilename_basins_json (str or Path): the filename
nBasin (int): the total number of basin
sWorkspace_input_in (str or Path): the input data path
sWorkspace_output (str or Path): the output path
Returns:
basin: a basin object
"""

# Ensure input pathnames are strings
sFilename_basins_json = str(sFilename_basins_json)
sWorkspace_input_in = str(sWorkspace_input_in)
sWorkspace_output_in = str(sWorkspace_output_in)

aBasin_out = list()
for i in range(nBasin):
sBasin = "{:03d}".format(i+1)
aConfig_basin = {}
aConfig_basin['iFlag_dam'] = 0
aConfig_basin['iFlag_disconnected'] = 0
aConfig_basin['lBasinID'] = i + 1
aConfig_basin['dLatitude_outlet_degree'] = -180
aConfig_basin['dLongitude_outlet_degree'] = 180
aConfig_basin['dAccumulation_threshold'] = -90
aConfig_basin['dThreshold_small_river'] = 90
aConfig_basin['sFilename_dam'] = str(Path(sWorkspace_input_in) / 'ICoM_dams.csv')
aConfig_basin['sFilename_flowline_filter'] = str(Path(sWorkspace_input_in) / 'streamord7above.shp')
aConfig_basin['sFilename_flowline_raw'] = str(Path(sWorkspace_input_in) / 'allflowline.shp')
aConfig_basin['sFilename_flowline_topo'] = str(Path(sWorkspace_input_in) / 'flowline.csv')
aConfig_basin['sWorkspace_output_basin'] = str(Path(sWorkspace_output_in) / sBasin )
pBasin = pybasin(aConfig_basin)
aBasin_out.append(pBasin)
pass

#export basin config to a file
with open(sFilename_basins_json, 'w', encoding='utf-8') as f:
sJson = json.dumps([json.loads(ob.tojson()) for ob in aBasin_out], indent = 4)
f.write(sJson)
f.close()

return aBasin_out

def pyflowline_create_template_configuration_file(
sFilename_json,
sWorkspace_input,
sWorkspace_output,
iFlag_standalone_in=None,
iFlag_use_mesh_dem_in=None,
iCase_index_in=None,
dResolution_degree_in=None,
dResolution_meter_in=None,
sDate_in=None,
sMesh_type_in=None,
sModel_in=None):
"""generate pyflowline config template file
Args:
sFilename_json (str or Path): _description_.
sWorkspace_input (str or Path): _description_.
sWorkspace_output (str or Path): _description_.
iFlag_standalone_in (int, optional): _description_. Defaults to None.
iFlag_use_mesh_dem_in (int, optional): _description_. Defaults to None.
iCase_index_in (int, optional): _description_. Defaults to None.
dResolution_degree_in (float, optional): _description_. Defaults to None.
dResolution_meter_in (float, optional): _description_. Defaults to None.
sDate_in (str, optional): _description_. Defaults to None.
sMesh_type_in (str, optional): _description_. Defaults to None.
sModel_in (str, optional): _description_. Defaults to None.
Returns:
_type_: _description_
"""

# Ensure input pathnames are strings
sFilename_json = str(sFilename_json)
sWorkspace_input = str(sWorkspace_input)
sWorkspace_output = str(sWorkspace_output)

if os.path.exists(sFilename_json):
os.remove(sFilename_json)

if iCase_index_in is not None:
iCase_index = iCase_index_in
else:
iCase_index = 1

if iFlag_standalone_in is not None:
iFlag_standalone = iFlag_standalone_in
else:
iFlag_standalone = 1

if iFlag_use_mesh_dem_in is not None:
iFlag_use_mesh_dem = iFlag_use_mesh_dem_in
else:
iFlag_use_mesh_dem = 0

#if iFlag_use_shapefile_extent_in is not None:
# iFlag_use_shapefile_extent = iFlag_use_shapefile_extent_in
#else:
# iFlag_use_shapefile_extent = 0

if sMesh_type_in is not None:
sMesh_type = sMesh_type_in
else:
sMesh_type = 'hexagon'
pass
if sDate_in is not None:
sDate = sDate_in
else:
sDate = '20220202'
pass

nBasin = 1

# Use a dict to initialize the class
aConfig = {}

#aConfig['iFlag_use_shapefile_extent'] = iFlag_use_shapefile_extent
aConfig['iFlag_use_mesh_dem'] = iFlag_use_mesh_dem
aConfig['iFlag_save_mesh'] = 1
aConfig['iFlag_simplification']=1
aConfig['iFlag_create_mesh']=1
aConfig['iFlag_intersect']=1
aConfig['iFlag_resample_method']=1
aConfig['iFlag_global']=0
aConfig['iFlag_multiple_outlet']=0
aConfig['iFlag_elevation_profile']=1
aConfig['iFlag_rotation']=0
aConfig['iFlag_stream_burning_topology']=1
aConfig['iFlag_save_elevation']=1
aConfig['nOutlet'] = nBasin
aConfig['dResolution_degree'] = 0.5
aConfig['dResolution_meter'] = 50000
aConfig['dLongitude_left'] = -180
aConfig['dLongitude_right'] = 180
aConfig['dLatitude_bot'] = -90
aConfig['dLatitude_top'] = 90
aConfig['sFilename_model_configuration'] = sFilename_json
aConfig['sWorkspace_input'] = sWorkspace_input
aConfig['sWorkspace_output'] = sWorkspace_output
aConfig['sRegion'] = 'susquehanna'
aConfig['sModel'] = 'pyflowline'
aConfig['iCase_index'] = iCase_index
aConfig['sMesh_type'] = sMesh_type
aConfig['sJob'] = 'pyflowline'
aConfig['sDate']= sDate
#full path
aConfig['sFilename_mesh_netcdf'] = str(Path(sWorkspace_input) / 'lnd_cull_mesh.nc')
aConfig['sFilename_spatial_reference'] = str(Path(sWorkspace_input) / 'boundary_proj.shp')
#relative path
aConfig['flowline_info'] = 'flowline_info.json'
aConfig['sFilename_mesh_info'] = 'mesh_info.json'
aConfig['sFilename_elevation'] = 'elevation.json'
oModel = flowlinecase(aConfig)

# Generate basin
sDirname = os.path.dirname(sFilename_json)
sFilename = Path(sFilename_json).stem + '_basins.json'
sFilename_basins_json = os.path.join(sDirname, sFilename)

aBasin = pyflowline_create_template_basin_configuration_file(
sFilename_basins_json,
nBasin,
sWorkspace_input,
oModel.sWorkspace_output)

oModel.aBasin = aBasin
oModel.sFilename_basins = sFilename_basins_json
oModel.export_config_to_json(sFilename_json)

return oModel
Loading

0 comments on commit 2730164

Please sign in to comment.