Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,12 @@ cython_debug/
.pypirc

# Ignore model save files (too large)
*.pt
*.pt

# Scratch / development / old / and other temporary files
*scratch*
*_dev*
*_old*
temp/
tmp/
*.tmp
2 changes: 1 addition & 1 deletion bmi_config_files/bmi_config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
### Full-catchment configuration ###
train_cfg_file: ../trained_models/merit_conus_40yr/config.yaml
train_cfg_file: models/hydrofabric_15yr/config.yaml
initial_state: zero # zero: set initial states of the model to zero
verbose: 1 # 0: no output, 1: print output

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,39 @@ catchment_id: 'cat-88306'
catchment_name: 'JRB-88306'

# Static catchment attributes
aridity: 1.041
meanP: 991.3
ETPOT_Hargr: 1031.0
NDVI: 0.594
FW: 0.004622
meanslope: 10.55
SoilGrids1km_sand: 38.65
SoilGrids1km_clay: 23.31
SoilGrids1km_silt: 38.06
glaciers: 0.0
HWSD_clay: 25.0
HWSD_gravel: 15.0
HWSD_sand: 33.0
T_clay: 20.0
uparea: 3.254
T_gravel: 10.0
HWSD_silt: 42.0
meanelevation: 335.1
meanP: 991.3
HWSD_gravel: 15.0
seasonality_P: 0.1035
T_sand: 41.0
SoilGrids1km_silt: 38.06
meanTa: 9.845
permafrost: 0.0
snowfall_fraction: 0.1049
SoilGrids1km_sand: 38.65
Porosity: 0.01
T_silt: 39.0
glaciers: 0.0
HWSD_silt: 42.0
meanslope: 10.55
permeability: -15.05
seasonality_P: 0.1035
seasonality_PET: 0.5703
ETPOT_Hargr: 1031.0
meanTa: 9.845
SoilGrids1km_clay: 23.31
snow_fraction: 0.02967
aridity: 1.041
NDVI: 0.594
snowfall_fraction: 0.1049
T_clay: 20.0
T_gravel: 10.0
T_sand: 41.0
T_silt: 39.0
Porosity: 0.01
uparea: 3.254

train_cfg_file: ../trained_models/merit_conus_40yr/config.yaml
config_model: models/hydrofabric_15yr/config.yaml
stepwise: False # True: stepwise inference, False: Single forward simulation on all data in one go
initial_state: zero # zero: set initial states of the model to zero
dtype: np.float32
verbose: 1 # 0: no output, 1: print output

# Simulation window
Expand Down
File renamed without changes.
181 changes: 181 additions & 0 deletions example/bmi_demo.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Forward $\\delta$ HBV 2.0UH with BMI\n",
"\n",
"\n",
"Notes:\n",
"- This example uses a subset of AORC forcings that can be downloaded from S3 here.\n",
"\n",
"- An environment can be set up with either pip or conda using `./envs/requirements.txt` or `./envs/ngen_env.yaml`, respectively.\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"sys.path.append('../../')\n",
" \n",
"# import numpy as np\n",
"\n",
"from dHBV_2_0.bmi import DeltaModelBmi\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'dHbv2Bmi' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[15], line 28\u001b[0m\n\u001b[0;32m 24\u001b[0m attr \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mload(attr_path)\n\u001b[0;32m 25\u001b[0m \u001b[38;5;66;03m# obs = np.load(obs_path)\u001b[39;00m\n\u001b[0;32m 26\u001b[0m \n\u001b[0;32m 27\u001b[0m \u001b[38;5;66;03m# Create an instance of the dHBV 2.0 through BMI\u001b[39;00m\n\u001b[1;32m---> 28\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[43mdHbv2Bmi\u001b[49m()\n\u001b[0;32m 30\u001b[0m \u001b[38;5;66;03m### BMI initialization ###\u001b[39;00m\n\u001b[0;32m 31\u001b[0m model\u001b[38;5;241m.\u001b[39minitialize(bmi_cfg_file\u001b[38;5;241m=\u001b[39mPath(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mC:/Users/LeoLo/Desktop/noaa_owp/dHBV_2_0/bmi_config_files/bmi_config_\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mbasin_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m.yaml\u001b[39m\u001b[38;5;124m'\u001b[39m))\n",
"\u001b[1;31mNameError\u001b[0m: name 'dHbv2Bmi' is not defined"
]
}
],
"source": [
"from pathlib import Path\n",
"\n",
"\n",
"\n",
"# os.chdir(os.path.expanduser('../dHBV_2_0/'))\n",
"\n",
"\n",
"### Select a basin from the sample data ###\n",
"basin_id = \"cat-88306\"\n",
"### ----------------------------------- ###\n",
"\n",
"\n",
"# Load the USGS data\n",
"# REPLACE THIS PATH WITH YOUR LOCAL FILE PATH:\n",
"forc_path = f'C:/Users/LeoLo/Desktop/noaa_owp/dHBV_2_0/data/aorc/juniata_river_basin/forcings_5yr_{basin_id}.npy'\n",
"attr_path = f'C:/Users/LeoLo/Desktop/noaa_owp/dHBV_2_0/data/aorc/juniata_river_basin/attributes_5yr_{basin_id}.npy'\n",
"# obs_path = f'/Users/LeoLo/Desktop/noaa_owp/dHBV_2_0/data/aorc/juniata_river_basin/obs_5yr_{basin_id}.npy'\n",
"\n",
"forc = np.load(forc_path)\n",
"attr = np.load(attr_path)\n",
"# obs = np.load(obs_path)\n",
"\n",
"# Create an instance of the dHBV 2.0 through BMI\n",
"model = dHbv2Bmi()\n",
"\n",
"### BMI initialization ###\n",
"model.initialize(bmi_cfg_file=Path(f'C:/Users/LeoLo/Desktop/noaa_owp/dHBV_2_0/bmi_config_files/bmi_config_{basin_id}.yaml'))\n",
"\n",
"streamflow_pred = np.zeros(forc.shape[0])\n",
"\n",
"for i in range(0, forc.shape[0]):\n",
" # Extract forcing/attribute data for the current time step\n",
" prcp = forc[i, :0, 0]\n",
" temp = forc[i, :0, 1]\n",
" pet = forc[i, :0, 2]\n",
"\n",
" # # Check if any of the inputs are NaN\n",
" # if np.isnan([prcp, temp, pet]).any():\n",
" # if model.verbose > 0:\n",
" # print(f\"Skipping timestep {i} due to NaN values in inputs.\")\n",
" # continue\n",
"\n",
" model.set_value('atmosphere_water__liquid_equivalent_precipitation_rate', prcp)\n",
" model.set_value('land_surface_air__temperature', temp)\n",
" model.set_value('land_surface_water__potential_evaporation_volume_flux', pet)\n",
"\n",
" ### BMI update ###\n",
" model.update()\n",
"\n",
" # Retrieve and scale the runoff output\n",
" dest_array = np.zeros(1)\n",
" model.get_value('land_surface_water__runoff_volume_flux', dest_array)\n",
" \n",
" # streamflow_pred[i] = dest_array[0] * 1000 # Convert to mm/hr\n",
" streamflow_pred[i] = dest_array[0]\n",
"\n",
"\n",
"# Calculate NSE for the model predictions\n",
"obs = obs.dropna()\n",
"sim = streamflow_pred.dropna()\n",
"\n",
"denom = ((obs - obs.mean()) ** 2).sum()\n",
"num = ((sim - obs) ** 2).sum()\n",
"nse = 1 - num / denom\n",
"print(f\"NSE: {nse:.2f}\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"import dMG\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "initialize_config() missing 1 required positional argument: 'config'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[6], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mdMG\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mutils\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minitialize_config\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
"\u001b[1;31mTypeError\u001b[0m: initialize_config() missing 1 required positional argument: 'config'"
]
}
],
"source": [
"dMG.utils.initialize_config()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading