Skip to content

Commit

Permalink
Fix issues with linting from pdm run lint (#8)
Browse files Browse the repository at this point in the history
* Fix issues with linting from pdm run lint

* Fix more mypy errors
  • Loading branch information
ciaransweet authored Jul 26, 2024
1 parent b4b5de8 commit d3d4566
Show file tree
Hide file tree
Showing 29 changed files with 138 additions and 250 deletions.
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ repos:
args:
['--no-error-on-unmatched-pattern', '--ignore-unknown']

# Lint: Markdown
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.39.0
hooks:
- id: markdownlint
args: ["--fix"]
# Lint: Markdown - Disabled as most rules aren't useful
# - repo: https://github.com/igorshubovych/markdownlint-cli
# rev: v0.39.0
# hooks:
# - id: markdownlint
# args: ["--fix"]

# - repo: https://github.com/asottile/pyupgrade
# rev: v3.15.0
Expand Down
39 changes: 20 additions & 19 deletions src/hazard/docs_store.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
import os
from typing import Any, Dict, Iterable, List, Optional
from typing import Any, Dict, Iterable, List, Optional, cast

import s3fs # type: ignore
from fsspec import AbstractFileSystem # type: ignore
Expand Down Expand Up @@ -94,42 +94,47 @@ def write_inventory_stac(self, resources: Iterable[HazardResource]):
"""Write a hazard models inventory as STAC."""

if self._fs == s3fs.S3FileSystem:
path_Root = self._root
path_root = self._root
else:
path_root = "."

items = HazardResources(resources=resources).to_stac_items(path_root=path_root, items_as_dicts=True)
for it in items:
items = HazardResources(resources=list(resources)).to_stac_items(path_root=path_root, items_as_dicts=True)
items_cast: List[Dict[str, Any]] = cast(List[Dict[str, Any]], items)
for it in items_cast:
with self._fs.open(self._full_path_stac_item(id=it["id"]), "w") as f:
f.write(json.dumps(it, indent=4))
catalog_path = self._full_path_stac_catalog()
catalog = self.stac_catalog(items=items)
catalog = self.stac_catalog(items=items_cast)
with self._fs.open(catalog_path, "w") as f:
json_str = json.dumps(catalog, indent=4)
f.write(json_str)
collection_path = self._full_path_stac_collection()
collection = self.stac_collection(items=items)
collection = self.stac_collection(items=items_cast)
with self._fs.open(collection_path, "w") as f:
json_str = json.dumps(collection, indent=4)
f.write(json_str)

def stac_catalog(self, items: List[Dict[str, Any]]) -> Dict[str, Any]:

links = [
{"rel": "self", "href": "./catalog.json"},
{"rel": "root", "href": "./catalog.json"},
{"rel": "child", "href": "./collection.json"},
]
links.extend([{"rel": "item", "href": f"./{x['id']}.json"} for x in items])
return {
"stac_version": "1.0.0",
"id": "osc-hazard-indicators-catalog",
"type": "Catalog",
"description": "OS-C hazard indicators catalog",
"links": [
{"rel": "self", "href": "./catalog.json"},
{"rel": "root", "href": "./catalog.json"},
{"rel": "child", "href": "./collection.json"},
]
+ [{"rel": "item", "href": f"./{x['id']}.json"} for x in items],
"links": links,
}

def stac_collection(self, items: List[Dict[str, Any]]) -> Dict[str, Any]:

links = [
{"rel": "self", "type": "application/json", "href": "./collection.json"},
{"rel": "root", "type": "application/json", "href": "./catalog.json"},
]
links.extend([{"rel": "item", "href": f"./{x['id']}.json"} for x in items])
return {
"stac_version": "1.0.0",
"type": "Collection",
Expand All @@ -143,11 +148,7 @@ def stac_collection(self, items: List[Dict[str, Any]]) -> Dict[str, Any]:
"temporal": {"interval": [["1950-01-01T00:00:00Z", "2100-12-31T23:59:59Z"]]},
},
"providers": [{"name": "UKRI", "roles": ["producer"], "url": "https://www.ukri.org/"}],
"links": [
{"rel": "self", "type": "application/json", "href": "./collection.json"},
{"rel": "root", "type": "application/json", "href": "./catalog.json"},
]
+ [{"rel": "item", "href": f"./{x['id']}.json"} for x in items],
"links": links,
}

def update_inventory(self, resources: Iterable[HazardResource], remove_existing: bool = False):
Expand Down
12 changes: 7 additions & 5 deletions src/hazard/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def to_stac_items(self, path_root: str, items_as_dicts: bool = False) -> List[Un
"""
keys, values = zip(*self.params.items())
params_permutations = list(itertools.product(*values))
params_permutations = [dict(zip(keys, p)) for p in params_permutations]
params_permutations_dicts = [dict(zip(keys, p)) for p in params_permutations]

scenarios_permutations = []
for s in self.scenarios:
Expand All @@ -128,7 +128,7 @@ def to_stac_items(self, path_root: str, items_as_dicts: bool = False) -> List[Un

permutations = [
dict(**param, **scenario)
for param, scenario in itertools.product(params_permutations, scenarios_permutations)
for param, scenario in itertools.product(params_permutations_dicts, scenarios_permutations)
]

items = []
Expand Down Expand Up @@ -161,10 +161,12 @@ def to_stac_item(

link = pystac.Link(rel="collection", media_type="application/json", target="./collection.json")

coordinates = self.map.bounds if self.map else None
bbox = self.map.bbox if self.map else None
stac_item = pystac.Item(
id=item_id,
geometry={"type": "Polygon", "coordinates": [self.map.bounds]},
bbox=self.map.bbox,
geometry={"type": "Polygon", "coordinates": [coordinates]},
bbox=bbox,
datetime=None,
start_datetime=datetime.datetime(2000, 1, 1, tzinfo=datetime.timezone.utc),
end_datetime=datetime.datetime(2100, 1, 1, tzinfo=datetime.timezone.utc),
Expand All @@ -186,7 +188,7 @@ def to_stac_item(
class HazardResources(BaseModel):
resources: List[HazardResource]

def to_stac_items(self, path_root: str, items_as_dicts: bool = False) -> List[Dict[str, Any]]:
def to_stac_items(self, path_root: str, items_as_dicts: bool = False) -> List[Union[pystac.Item, Dict]]:
"""
converts hazard resources to a list of STAC items.
"""
Expand Down
9 changes: 6 additions & 3 deletions src/hazard/models/degree_days.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,20 +265,22 @@ def _calculate_single_year_indicators(self, source: OpenDataset, item: BatchItem
above = self._degree_days_indicator(tas, year, AboveBelow.ABOVE)
below = self._degree_days_indicator(tas, year, AboveBelow.BELOW)
logger.info(f"Calculation complete for year {year}")
bounds_above = self.resources["above"].map.bounds if self.resources["above"].map else []
bounds_below = self.resources["below"].map.bounds if self.resources["below"].map else []
return [
Indicator(
above,
PurePosixPath(
self.resources["above"].path.format(gcm=item.gcm, scenario=item.scenario, year=item.central_year)
),
self.resources["above"].map.bounds,
bounds_above,
),
Indicator(
below,
PurePosixPath(
self.resources["below"].path.format(gcm=item.gcm, scenario=item.scenario, year=item.central_year)
),
self.resources["below"].map.bounds,
bounds_below,
),
]

Expand Down Expand Up @@ -307,7 +309,8 @@ def _degree_days_indicator(self, tas: xr.DataArray, year: int, above_below: Abov
)
return da

def _resource(self):
def _resource(self) -> Dict[str, HazardResource]: # type: ignore[override]
# Unsure why this returns a dict vs a single resource
resources: Dict[str, HazardResource] = {}
for above_below in ["above", "below"]:
with open(os.path.join(os.path.dirname(__file__), "degree_days.md"), "r") as f:
Expand Down
4 changes: 2 additions & 2 deletions src/hazard/models/drought_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class BatchItem:


class ZarrWorkingStore(Protocol):
def get_store(self, path: str): ...
def get_store(self, path: str): ... # noqa:E704


class S3ZarrWorkingStore(ZarrWorkingStore):
Expand Down Expand Up @@ -376,7 +376,7 @@ def _resource(self) -> HazardResource:
description="",
display_groups=["Drought SPEI index"], # display names of groupings
group_id="",
map=MapInfo(
map=MapInfo( # type: ignore[call-arg] # has a default value for bbox
colormap=Colormap(
name="heating",
nodata_index=0,
Expand Down
2 changes: 1 addition & 1 deletion src/hazard/models/multi_year_average.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,4 @@ def _get_indicators(self, item: BatchItem, data_arrays: List[xr.DataArray], para
]

@abstractmethod
def _resource(self) -> HazardResource: ...
def _resource(self) -> HazardResource: ... # noqa:E704
7 changes: 3 additions & 4 deletions src/hazard/models/water_temp.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ def water_temp_download_path(self, gcm: str, scenario: str, year: int) -> Tuple[
filename = self.filename(gcm, scenario, year)
url = (
"https://geo.public.data.uu.nl/vault-futurestreams/research-futurestreams%5B1633685642%5D/"
+ f"original/waterTemp/{adjusted_scenario}/{adjusted_gcm}/"
+ filename
f"original/waterTemp/{adjusted_scenario}/{adjusted_gcm}/{filename}"
)
return os.path.join(self.working_dir, filename), url

Expand Down Expand Up @@ -273,7 +272,7 @@ def _resource(self) -> HazardResource:
description=description,
display_groups=["Weeks with average water temperature above threshold in °C"], # display names of groupings
group_id="",
map=MapInfo(
map=MapInfo( # type: ignore[call-arg] # has a default value for bbox
colormap=Colormap(
name="heating",
nodata_index=0,
Expand Down Expand Up @@ -301,7 +300,7 @@ def _resource(self) -> HazardResource:

def _other_resource(self) -> HazardResource:
assert self.resource.map is not None
map = MapInfo(
map = MapInfo( # type: ignore[call-arg] # has a default value for bbox
colormap=self.resource.map.colormap,
bounds=self.resource.map.bounds,
path=self.resource.map.path.format(gcm="E2O", scenario="{scenario}", year="{year}"),
Expand Down
2 changes: 1 addition & 1 deletion src/hazard/models/wet_bulb_globe_temp.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def _resource(self) -> HazardResource:
description=description,
display_groups=["Days with wet-bulb globe temperature above threshold in °C"], # display names of groupings
group_id="",
map=MapInfo(
map=MapInfo( # type: ignore[call-arg] # has a default value for bbox
colormap=Colormap(
name="heating",
nodata_index=0,
Expand Down
2 changes: 1 addition & 1 deletion src/hazard/models/work_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _resource(self) -> HazardResource:
display_groups=["Mean work loss"], # display names of groupings
# we want "Mean work loss" -> "Low intensity", "Medium intensity", "High intensity" -> "GCM1", "GCM2", ...
group_id="",
map=MapInfo(
map=MapInfo( # type: ignore[call-arg] # has a default value for bbox
colormap=Colormap(
name="heating",
nodata_index=0,
Expand Down
2 changes: 1 addition & 1 deletion src/hazard/onboard/iris_wind.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def _hazard_resource(self) -> HazardResource:
description=description,
group_id="iris_osc",
display_groups=[],
map=MapInfo(
map=MapInfo( # type: ignore[call-arg] # has a default value for bbox
bounds=[(-180.0, 60.0), (180.0, 60.0), (180.0, -60.0), (-180.0, -60.0)],
colormap=Colormap(
name="heating",
Expand Down
28 changes: 14 additions & 14 deletions src/hazard/onboard/jupiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ def inventory(self) -> Iterable[HazardResource]:
a given location based on several parameters from multiple bias corrected and downscaled Global Climate Models (GCMs).
For example, if the probability of occurrence of a wildfire is 5% in July, 20% in August, 10% in September
and 0% for other months, the hazard indicator value is 20%.
""",
""", # noqa:W503
group_id="jupiter_osc",
display_groups=[],
map=MapInfo(
map=MapInfo( # type: ignore[call-arg] # has a default value for bbox
bounds=[
(-180.0, 85.0),
(180.0, 85.0),
Expand Down Expand Up @@ -175,10 +175,10 @@ def inventory(self) -> Iterable[HazardResource]:
This drought model computes the number of months per annum where the 3-month rolling average
of SPEI is below -2 based on the mean values of several parameters from
bias-corrected and downscaled multiple Global Climate Models (GCMs).
""",
""", # noqa:W503
group_id="jupiter_osc",
display_groups=[],
map=MapInfo(
map=MapInfo( # type: ignore[call-arg] # has a default value for bbox
bounds=[
(-180.0, 85.0),
(180.0, 85.0),
Expand Down Expand Up @@ -217,10 +217,10 @@ def inventory(self) -> Iterable[HazardResource]:
This model computes the maximum daily water equivalent precipitation (in mm) measured at the 100 year
return period based on the mean of the precipitation distribution from multiple bias corrected and
downscaled Global Climate Models (GCMs).
""",
""", # noqa:W503
group_id="jupiter_osc",
display_groups=[],
map=MapInfo(
map=MapInfo( # type: ignore[call-arg] # has a default value for bbox
bounds=[
(-180.0, 85.0),
(180.0, 85.0),
Expand Down Expand Up @@ -259,10 +259,10 @@ def inventory(self) -> Iterable[HazardResource]:
This hail model computes the number of days per annum where hail exceeding 5 cm diameter is possible
based on the mean distribution of several parameters
across multiple bias-corrected and downscaled Global Climate Models (GCMs).
""",
""", # noqa:W503
group_id="jupiter_osc",
display_groups=[],
map=MapInfo(
map=MapInfo( # type: ignore[call-arg] # has a default value for bbox
bounds=[
(-180.0, 85.0),
(180.0, 85.0),
Expand Down Expand Up @@ -301,10 +301,10 @@ def inventory(self) -> Iterable[HazardResource]:
This heat model computes the number of days exceeding 35°C per annum based on the mean of distribution fits
to the bias-corrected and downscaled high temperature distribution
across multiple Global Climate Models (GCMs).
""",
""", # noqa:W503
group_id="jupiter_osc",
display_groups=[],
map=MapInfo(
map=MapInfo( # type: ignore[call-arg] # has a default value for bbox
bounds=[
(-180.0, 85.0),
(180.0, 85.0),
Expand Down Expand Up @@ -343,10 +343,10 @@ def inventory(self) -> Iterable[HazardResource]:
This wind speed model computes the maximum 1-minute sustained wind speed (in km/hr) experienced over a
100 year return period based on mean wind speed distributions
from multiple Global Climate Models (GCMs).
""",
""", # noqa:W503
group_id="jupiter_osc",
display_groups=[],
map=MapInfo(
map=MapInfo( # type: ignore[call-arg] # has a default value for bbox
bounds=[
(-180.0, 85.0),
(180.0, 85.0),
Expand Down Expand Up @@ -387,10 +387,10 @@ def inventory(self) -> Iterable[HazardResource]:
cells within the 30-km cell that have non-zero flooding at that return period.
This model uses a 30-km grid that experiences flooding at the 200-year return period.
Open oceans are excluded.
""",
""", # noqa:W503
group_id="jupiter_osc",
display_groups=[],
map=MapInfo(
map=MapInfo( # type: ignore[call-arg] # has a default value for bbox
bounds=[
(-180.0, 85.0),
(180.0, 85.0),
Expand Down
2 changes: 1 addition & 1 deletion src/hazard/onboard/tudelft_flood.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def inventory(self) -> Iterable[HazardResource]:
description=description,
group_id="",
display_groups=[],
map=MapInfo(
map=MapInfo( # type: ignore[call-arg] # has a default value for bbox
bounds=[],
colormap=Colormap(
max_index=255,
Expand Down
Loading

0 comments on commit d3d4566

Please sign in to comment.