Skip to content

Commit

Permalink
update notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
changliao1025 committed Sep 19, 2023
1 parent a833183 commit 410cc9d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 11 deletions.
52 changes: 44 additions & 8 deletions notebooks/mpas_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@
"metadata": {},
"outputs": [],
"source": [
"#check pyflowline\n",
"#check pyflowline installation\n",
"iFlag_pyflowline = importlib.util.find_spec(\"pyflowline\") \n",
"if iFlag_pyflowline is not None:\n",
" pass\n",
"else:\n",
" print('The pyflowline package is not installed. Please install it following the quickstart document.')\n",
"\n",
"#check optional packages \n",
"iFlag_cython = importlib.util.find_spec(\"cython\") \n",
"iFlag_cartopy = importlib.util.find_spec(\"cartopy\") \n",
"iFlag_geopandas = importlib.util.find_spec(\"geopandas\") \n",
"iFlag_cython = importlib.util.find_spec(\"cython\") #check whether cython is installed\n",
"iFlag_cartopy = importlib.util.find_spec(\"cartopy\") #check whether cartopy is installed\n",
"iFlag_geopandas = importlib.util.find_spec(\"geopandas\") #check whether geopandas is installed\n",
"if iFlag_cartopy is not None:\n",
" iFlag_cartopy = 1\n",
" pass\n",
Expand All @@ -78,6 +78,7 @@
" if iFlag_geopandas is not None:\n",
" pass\n",
" else:\n",
" #if both cartopy and geopandas are not available, we will install the geopandas package under the current environment\n",
" print('We will install the geopandas package for visualization.')\n",
" !conda install --yes --prefix {sys.prefix} gepandas\n",
" iFlag_geopandas = 1\n",
Expand Down Expand Up @@ -166,6 +167,7 @@
"outputs": [],
"source": [
"\n",
"#download the MPAS mesh from the github release\n",
"sFilename_mpas = 'https://github.com/changliao1025/pyflowline/releases/download/0.2.0/lnd_cull_mesh.nc'\n",
"\n",
"#combind folder with filename to get the full path\n",
Expand Down Expand Up @@ -318,7 +320,7 @@
"metadata": {},
"outputs": [],
"source": [
"#step 5\n",
"#step 5, set up some parameters\n",
"sMesh_type = 'mpas'\n",
"iCase_index = 1\n",
"dResolution_meter=5000\n",
Expand Down Expand Up @@ -349,6 +351,11 @@
}
],
"source": [
"#the read function accepts several keyword arguments that can be used to change the default parameters.\n",
"#the normal keyword arguments are:\n",
"#iCase_index_in: this is an ID to identify the simulation case\n",
"#sMesh_type_in: this specifies the mesh type ('mpas' in this example)\n",
"#sDate_in: this specifies the date of the simulation, the final output folder will have a pattern such as 'pyflowline20230901001', where pyflowline is model, 20230901 is the date, and 001 is the case index.\n",
"oPyflowline = pyflowline_read_model_configuration_file(sFilename_configuration_in, iCase_index_in=iCase_index, \n",
" sMesh_type_in= sMesh_type, sDate_in=sDate)"
]
Expand All @@ -360,7 +367,32 @@
"metadata": {},
"outputs": [],
"source": [
"oPyflowline.change_model_parameter('sFilename_mesh_netcdf', sFilename_download)"
"#other than setting the paraemeter using the read model configuration fucntion, user can also change model parameters after creating the model object\n",
"#rememere that, it is recommended to set output folder using the read model configuration function since the change_model_parameter function will not update output folder\n",
"#only a list of parameters can be changed, for full list, please check the documentation\n",
"#in this example, we will change the mesh file name\n",
"#the function will check the data type, if incorrect data type is provided, it will raise an error\n",
"oPyflowline.change_model_parameter('sFilename_mesh_netcdf', sFilename_download) #because the mpas mesh already contains elevation, we do not need to set the elevation file name\n",
"#we will set the boundary file name\n",
"#this file should be located in the input folder\n",
"sFolder_data = os.path.join(sPath_parent, 'data')\n",
"sFolder_data_susquehanna = os.path.join(sFolder_data, 'susquehanna')\n",
"sFolder_input = os.path.join(sFolder_data_susquehanna, 'input')\n",
"sFilename_mesh_boundary = os.path.join(sPath_parent, 'boundary_wgs.geojson')\n",
"oPyflowline.change_model_parameter('sFilename_boundary_netcdf', sFilename_mesh_boundary)\n",
"\n",
"#we will set the original flowline file name\n",
"sFilename_flowline = os.path.join(sFolder_input, 'flowline.geojson')\n",
"oPyflowline.change_model_parameter('sFilename_flowline_filtered', sFilename_flowline)\n",
"\n",
"#we can also set for individual basin in the domain, in this example, we only has one basin.\n",
"#remember that, each basin can have different parameters, so if you want to set them different (for example, basin 1 has no dam, but basin 2 has dam), you should edit the basin json instead using this function.\n",
"#because change_model_parameter will set all the basin using the same parameter in current version\n",
"#must set iFlag_basin_in = 1 for basin parameter\n",
"oPyflowline.change_model_parameter('iFlag_dam', 0, iFlag_basin_in= 1)\n",
"\n",
"\n",
"\n"
]
},
{
Expand Down Expand Up @@ -443,8 +475,12 @@
"metadata": {},
"outputs": [],
"source": [
"#another important setting for basin is the approximate outlet location\n",
"oPyflowline.aBasin[0].dLatitude_outlet_degree=39.4620\n",
"oPyflowline.aBasin[0].dLongitude_outlet_degree=-76.0093"
"oPyflowline.aBasin[0].dLongitude_outlet_degree=-76.0093\n",
"#you can also set it using the change_model_parameter function\n",
"oPyflowline.change_model_parameter('dLongitude_outlet_degree', -76.0093, iFlag_basin_in = 1)\n",
"oPyflowline.change_model_parameter('dLatitude_outlet_degree', 39.4620, iFlag_basin_in = 1)"
]
},
{
Expand Down Expand Up @@ -515,7 +551,7 @@
" oPyflowline.plot(sVariable_in = 'flowline_filter', sFilename_in = 'filter_flowline.png' )\n",
" pass\n",
"else: #use the default visualization method, only experimental\n",
" if iFlag_geopandas = 1:\n",
" if iFlag_geopandas == 1:\n",
" import geopandas as gpd\n",
" import matplotlib.pyplot as plt\n",
" #use the geopanda package\n",
Expand Down
20 changes: 17 additions & 3 deletions pyflowline/classes/pycase.py
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,15 @@ def setup(self):
def change_model_parameter(self, sVariable_in, dValue, iFlag_basin_in = None):
if iFlag_basin_in is None:
if hasattr(self, sVariable_in):
setattr(self, sVariable_in, dValue)
#get default data type
sType_default = type(getattr(self, sVariable_in))
#get the data type of the input value
sType_input = type(dValue)
if sType_default == sType_input:
setattr(self, sVariable_in, dValue)
pass
else:
print('Incorrect data type for the input value: ' + sVariable_in)
return True
else:
print("This model parameter is unknown, please check the full parameter list in the documentation: " + sVariable_in)
Expand All @@ -1017,8 +1025,14 @@ def change_model_parameter(self, sVariable_in, dValue, iFlag_basin_in = None):
#this mean the variable is in the basin object
for pBasin in self.aBasin:
if hasattr(pBasin, sVariable_in):
setattr(pBasin, sVariable_in, dValue)
return True
#get default data type
sType_default = type(getattr(pBasin, sVariable_in))
sType_input = type(dValue)
if sType_default == sType_input:
setattr(pBasin, sVariable_in, dValue)
else:
print('Incorrect data type for the input value: ' + sVariable_in)
return False
else:
print("This model parameter is unknown, please check the full parameter list in the documentation: " + sVariable_in)
return False
Expand Down

0 comments on commit 410cc9d

Please sign in to comment.