Skip to content

Commit f779f1d

Browse files
Merge pull request #11 from supathdhitalGEO/main
Fine Tune the applications and code formatting
2 parents e0667e6 + 11e119e commit f779f1d

6 files changed

Lines changed: 124 additions & 4 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ dependencies = [
2828
"nbformat>=5.10.4,<6.0.0",
2929
"pyproj>=3.7.0,<4.0.0",
3030
"notebook>=7.3.2,<8.0.0",
31-
"boto3>=1.36.16,<2.0.0",
31+
"boto3>=1.40.0",
3232
"geemap",
33-
"uv",
3433
"seaborn"
3534
]
3635

src/fimeval/ContingencyMap/evaluationFIM.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ def resize_image(
277277
out_transform1,
278278
)
279279
merged = out_image1 + out_image2_resized
280-
merged[merged==7] = 5
281-
280+
merged[merged == 7] = 5
281+
282282
# Get Evaluation Metrics
283283
(
284284
unique_values,

src/fimeval/FIMs/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .owphandfim import get_HANDFIM
2+
3+
__all__ = ["get_HANDFIM"]

src/fimeval/FIMs/owphandfim.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import os
2+
import rasterio
3+
import shutil
4+
from pathlib import Path
5+
from rasterio.mask import mask
6+
import fimserve as fm
7+
8+
from .utilis import *
9+
10+
11+
# GET LOW FIDELITY USING FIMSERVE
12+
def get_HANDFIM(
13+
huc_id,
14+
event_date=None,
15+
data="retrospective",
16+
forecast_range=None,
17+
forecast_date=None,
18+
sort_by=None,
19+
):
20+
original_cwd = os.getcwd()
21+
try:
22+
createCWD("owp_fim")
23+
fm.DownloadHUC8(huc_id)
24+
25+
# For retrospective event
26+
if data == "retrospective":
27+
if not event_date:
28+
raise ValueError("event_date is required for retrospective analysis.")
29+
huc_event_dict = initialize_huc_event(huc_id, event_date)
30+
fm.getNWMretrospectivedata(huc_event_dict=huc_event_dict)
31+
32+
# For forecasting event
33+
elif data == "forecast":
34+
if not forecast_range:
35+
raise ValueError(
36+
"forecast_range ('short_range', 'medium_range', or 'long_range') is required for forecast."
37+
)
38+
39+
if forecast_range in ["medium_range", "long_range"]:
40+
if not sort_by:
41+
sort_by = "maximum"
42+
fm.getNWMForecasteddata(
43+
huc_id=huc_id,
44+
forecast_range=forecast_range,
45+
forecast_date=forecast_date,
46+
sort_by=sort_by,
47+
)
48+
else:
49+
fm.getNWMForecasteddata(
50+
huc_id=huc_id,
51+
forecast_range=forecast_range,
52+
forecast_date=forecast_date,
53+
)
54+
else:
55+
raise ValueError("data_type must be either 'retrospective' or 'forecast'.")
56+
57+
# Run the FIM
58+
fm.runOWPHANDFIM(huc_id)
59+
60+
finally:
61+
os.chdir(original_cwd)
62+
63+
64+
# Raster to binary
65+
def raster2binary(input_raster_path, geometry, final_raster_path):
66+
# Mask the raster with the geometry
67+
with rasterio.open(input_raster_path) as src:
68+
out_image, out_transform = mask(src, geometry, crop=True, filled=True, nodata=0)
69+
out_meta = src.meta.copy()
70+
out_meta.update(
71+
{
72+
"driver": "GTiff",
73+
"height": out_image.shape[1],
74+
"width": out_image.shape[2],
75+
"transform": out_transform,
76+
"crs": src.crs,
77+
"nodata": 0,
78+
}
79+
)
80+
81+
# Convert to binary (HAND logic: flooded if value > 0)
82+
binary_image = (out_image > 0).astype("uint8")
83+
84+
# Save the binary raster
85+
with rasterio.open(final_raster_path, "w", **out_meta) as dst:
86+
dst.write(binary_image)

src/fimeval/FIMs/utilis.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import os
2+
import rasterio
3+
4+
5+
# INITIALIZE IN HUC EVENT DICT
6+
def initialize_huc_event(huc_id, event_times):
7+
if isinstance(event_times, str):
8+
event_times = [event_times]
9+
return {huc_id: event_times}
10+
11+
12+
# Create a folder and make it a working directory
13+
def createCWD(folder_name):
14+
os.makedirs(folder_name, exist_ok=True)
15+
os.chdir(folder_name)
16+
new_path = os.getcwd()
17+
return new_path
18+
19+
20+
def compress_tif_lzw(tif_path):
21+
# Read original file
22+
with rasterio.open(tif_path) as src:
23+
profile = src.profile.copy()
24+
data = src.read()
25+
profile.update(compress="lzw")
26+
27+
with rasterio.open(tif_path, "w", **profile) as dst:
28+
dst.write(data)

src/fimeval/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010
# Evaluation with Building foorprint module
1111
from .BuildingFootprint.evaluationwithBF import EvaluationWithBuildingFootprint
1212

13+
# FIM evaluation
14+
from .FIMs.owphandfim import get_HANDFIM
15+
1316
__all__ = [
1417
"EvaluateFIM",
1518
"PrintContingencyMap",
1619
"PlotEvaluationMetrics",
1720
"get_PWB",
1821
"EvaluationWithBuildingFootprint",
1922
"compress_tif_lzw",
23+
"get_HANDFIM",
2024
]

0 commit comments

Comments
 (0)