Skip to content

Commit

Permalink
Add WRI maps (size 512); update exposure for inventory update (#150)
Browse files Browse the repository at this point in the history
* Add WRI maps (size 512); update exposure for inventory update

---------

Signed-off-by: Joe Moorhouse <[email protected]>
  • Loading branch information
joemoorhouse authored Sep 30, 2023
1 parent 9e18fd7 commit e472bc2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 92 deletions.
7 changes: 5 additions & 2 deletions src/physrisk/data/image_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ def _to_image(
max_value: Optional[float] = None,
) -> Image.Image:
"""Get image for path specified as array of bytes."""
tile_path = path if tile is None else str(PurePosixPath(path, f"{tile.z}"))
tile_path = path if tile is None else str(PurePosixPath(path, f"{tile.z + 1}"))
data = get_data(self.reader, tile_path)
tile_size = 512
# data = self.reader.all_data(tile_path)
if len(data.shape) == 3:
index = len(self.reader.get_index_values(data)) - 1 if index is None else index
Expand All @@ -93,7 +94,9 @@ def _to_image(
data = data[index, :, :] # .squeeze(axis=0)
else:
# (from zarr 2.16.0 we can also use block indexing)
data = data[index, 256 * tile.y : 256 * (tile.y + 1), 256 * tile.x : 256 * (tile.x + 1)]
data = data[
index, tile_size * tile.y : tile_size * (tile.y + 1), tile_size * tile.x : tile_size * (tile.x + 1)
]

if any(dim > 4000 for dim in data.shape):
raise Exception("dimension too large (over 1500).")
Expand Down
50 changes: 25 additions & 25 deletions src/physrisk/data/static/hazard/inventory.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"nodata_index": 0,
"units": "m"
},
"path": "inunriver_{scenario}_000000000WATCH_{year}_rp{return_period:05d}",
"path": "inundation/wri/v2/inunriver_{scenario}_000000000WATCH_{year}_map",
"bounds": [
[
-180.0,
Expand All @@ -40,7 +40,7 @@
-85.0
]
],
"source": "mapbox"
"source": "map_array_pyramid"
},
"scenarios": [
{
Expand Down Expand Up @@ -73,7 +73,7 @@
"nodata_index": 0,
"units": "m"
},
"path": "inunriver_{scenario}_00000NorESM1-M_{year}_map",
"path": "inundation/wri/v2/inunriver_{scenario}_00000NorESM1-M_{year}_map",
"bounds": [
[
-180.0,
Expand Down Expand Up @@ -135,7 +135,7 @@
"nodata_index": 0,
"units": "m"
},
"path": "inunriver_{scenario}_0000GFDL-ESM2M_{year}_map",
"path": "inundation/wri/v2/inunriver_{scenario}_0000GFDL-ESM2M_{year}_map",
"bounds": [
[
-180.0,
Expand Down Expand Up @@ -197,7 +197,7 @@
"nodata_index": 0,
"units": "m"
},
"path": "inunriver_{scenario}_0000HadGEM2-ES_{year}_map",
"path": "inundation/wri/v2/inunriver_{scenario}_0000HadGEM2-ES_{year}_map",
"bounds": [
[
-180.0,
Expand Down Expand Up @@ -259,7 +259,7 @@
"nodata_index": 0,
"units": "m"
},
"path": "inunriver_{scenario}_00IPSL-CM5A-LR_{year}_map",
"path": "inundation/wri/v2/inunriver_{scenario}_00IPSL-CM5A-LR_{year}_map",
"bounds": [
[
-180.0,
Expand Down Expand Up @@ -321,7 +321,7 @@
"nodata_index": 0,
"units": "m"
},
"path": "inunriver_{scenario}_MIROC-ESM-CHEM_{year}_rp{return_period:05d}",
"path": "inundation/wri/v2/inunriver_{scenario}_MIROC-ESM-CHEM_{year}_map",
"bounds": [
[
-180.0,
Expand All @@ -340,7 +340,7 @@
-85.0
]
],
"source": "mapbox"
"source": "map_array_pyramid"
},
"scenarios": [
{
Expand Down Expand Up @@ -383,7 +383,7 @@
"nodata_index": 0,
"units": "m"
},
"path": "inuncoast_historical_nosub_hist_rp{return_period:04d}_0",
"path": "inundation/wri/v2/inuncoast_historical_nosub_hist_0_map",
"bounds": [
[
-180.0,
Expand All @@ -402,7 +402,7 @@
-85.0
]
],
"source": "mapbox"
"source": "map_array_pyramid"
},
"scenarios": [
{
Expand Down Expand Up @@ -435,7 +435,7 @@
"nodata_index": 0,
"units": "m"
},
"path": "inuncoast_{scenario}_nosub_{year}_rp{return_period:04d}_0",
"path": "inundation/wri/v2/inuncoast_{scenario}_nosub_{year}_0_map",
"bounds": [
[
-180.0,
Expand All @@ -454,7 +454,7 @@
-85.0
]
],
"source": "mapbox"
"source": "map_array_pyramid"
},
"scenarios": [
{
Expand Down Expand Up @@ -497,7 +497,7 @@
"nodata_index": 0,
"units": "m"
},
"path": "inuncoast_{scenario}_nosub_{year}_rp{return_period:04d}_0_perc_05",
"path": "inundation/wri/v2/inuncoast_{scenario}_nosub_{year}_0_perc_05_map",
"bounds": [
[
-180.0,
Expand All @@ -516,7 +516,7 @@
-85.0
]
],
"source": "mapbox"
"source": "map_array_pyramid"
},
"scenarios": [
{
Expand Down Expand Up @@ -559,7 +559,7 @@
"nodata_index": 0,
"units": "m"
},
"path": "inuncoast_{scenario}_nosub_{year}_rp{return_period:04d}_0_perc_50",
"path": "inundation/wri/v2/inuncoast_{scenario}_nosub_{year}_0_perc_50_map",
"bounds": [
[
-180.0,
Expand All @@ -578,7 +578,7 @@
-85.0
]
],
"source": "mapbox"
"source": "map_array_pyramid"
},
"scenarios": [
{
Expand Down Expand Up @@ -621,7 +621,7 @@
"nodata_index": 0,
"units": "m"
},
"path": "inuncoast_historical_wtsub_hist_rp{return_period:04d}_0",
"path": "inundation/wri/v2/inuncoast_historical_wtsub_hist_0_map",
"bounds": [
[
-180.0,
Expand All @@ -640,7 +640,7 @@
-85.0
]
],
"source": "mapbox"
"source": "map_array_pyramid"
},
"scenarios": [
{
Expand Down Expand Up @@ -673,7 +673,7 @@
"nodata_index": 0,
"units": "m"
},
"path": "inuncoast_{scenario}_wtsub_{year}_rp{return_period:04d}_0",
"path": "inundation/wri/v2/inuncoast_{scenario}_wtsub_{year}_0_map",
"bounds": [
[
-180.0,
Expand All @@ -692,7 +692,7 @@
-85.0
]
],
"source": "mapbox"
"source": "map_array_pyramid"
},
"scenarios": [
{
Expand Down Expand Up @@ -735,7 +735,7 @@
"nodata_index": 0,
"units": "m"
},
"path": "inuncoast_{scenario}_wtsub_{year}_rp{return_period:04d}_0_perc_05",
"path": "inundation/wri/v2/inuncoast_{scenario}_wtsub_{year}_0_perc_05_map",
"bounds": [
[
-180.0,
Expand All @@ -754,7 +754,7 @@
-85.0
]
],
"source": "mapbox"
"source": "map_array_pyramid"
},
"scenarios": [
{
Expand Down Expand Up @@ -797,7 +797,7 @@
"nodata_index": 0,
"units": "m"
},
"path": "inuncoast_{scenario}_wtsub_{year}_rp{return_period:04d}_0_perc_50",
"path": "inundation/wri/v2/inuncoast_{scenario}_wtsub_{year}_0_perc_50_map",
"bounds": [
[
-180.0,
Expand All @@ -816,7 +816,7 @@
-85.0
]
],
"source": "mapbox"
"source": "map_array_pyramid"
},
"scenarios": [
{
Expand Down Expand Up @@ -1629,7 +1629,7 @@
-60.0
]
],
"source": "map_array"
"source": "map_array_pyramid"
},
"scenarios": [
{
Expand Down
2 changes: 1 addition & 1 deletion src/physrisk/kernel/exposure.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def get_exposure_bins(self):
Bounds(category=Category.HIGHEST, lower=30, upper=float("inf")),
]
)
categories[(Wind, "max/1min")] = self.bounds_to_lookup(
categories[(Wind, "max_speed")] = self.bounds_to_lookup(
[
Bounds(category=Category.LOWEST, lower=float("-inf"), upper=63),
Bounds(category=Category.LOW, lower=63, upper=90),
Expand Down
78 changes: 14 additions & 64 deletions src/test/kernel/test_exposure.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

import physrisk.api.v1.common
from physrisk.api.v1.exposure_req_resp import AssetExposureRequest, AssetExposureResponse
from physrisk.api.v1.hazard_data import HazardResource, Scenario
from physrisk.data.inventory import EmbeddedInventory, Inventory
from physrisk.data.inventory import EmbeddedInventory
from physrisk.data.inventory_reader import InventoryReader
from physrisk.data.pregenerated_hazard_model import ZarrHazardModel
from physrisk.data.zarr_reader import ZarrReader
Expand Down Expand Up @@ -54,69 +53,19 @@ def test_jupiter_exposure(self):
assert categories[k][0] == v

def _get_components(self):
resources = [
HazardResource(
hazard_type="CombinedInundation",
path="fraction_{scenario}_{year}",
indicator_id="flooded_fraction",
indicator_model_gcm="unknown",
display_name="",
description="",
scenarios=[Scenario(id="ssp585", years=[2030])],
units="",
),
HazardResource(
hazard_type="ChronicHeat",
path="days_above_35c_{scenario}_{year}",
indicator_id="days/above/35c",
indicator_model_gcm="unknown",
display_name="",
description="",
scenarios=[Scenario(id="ssp585", years=[2030])],
units="",
),
HazardResource(
hazard_type="Wind",
path="max_1min_{scenario}_{year}",
indicator_id="max/1min",
indicator_model_gcm="unknown",
display_name="",
description="",
scenarios=[Scenario(id="ssp585", years=[2030])],
units="",
),
HazardResource(
hazard_type="Drought",
path="months_spei3m_below_-2_{scenario}_{year}",
indicator_id="months/spei3m/below/-2",
indicator_model_gcm="unknown",
display_name="",
description="",
scenarios=[Scenario(id="ssp585", years=[2030])],
units="",
),
HazardResource(
hazard_type="Hail",
path="days_above_5cm_{scenario}_{year}",
indicator_id="days/above/5cm",
indicator_model_gcm="unknown",
display_name="",
description="",
scenarios=[Scenario(id="ssp585", years=[2030])],
units="",
),
HazardResource(
hazard_type="Fire",
path="fire_probability_{scenario}_{year}",
indicator_id="fire_probability",
indicator_model_gcm="unknown",
display_name="",
description="",
scenarios=[Scenario(id="ssp585", years=[2030])],
units="",
),
# "precipitation/jupiter/v1/max_daily_water_equivalent_{scenario}_{year}"
paths = [
"combined_flood/jupiter/v1/fraction_{scenario}_{year}",
"chronic_heat/jupiter/v1/days_above_35c_{scenario}_{year}",
"wind/jupiter/v1/max_1min_{scenario}_{year}",
"drought/jupiter/v1/months_spei3m_below_-2_{scenario}_{year}",
"hail/jupiter/v1/days_above_5cm_{scenario}_{year}",
"fire/jupiter/v1/fire_probability_{scenario}_{year}",
]

all_resources = EmbeddedInventory().resources
resources = [all_resources[p] for p in paths]

values = [np.array([v]) for v in [0.02, 15, 100, 0.7, 0.1, 0.9]]

expected = {
Expand All @@ -134,6 +83,7 @@ def path_curves():
assets = [Asset(lat, lon) for (lat, lon) in zip(TestData.latitudes, TestData.longitudes)]

store = mock_hazard_model_store_path_curves(TestData.longitudes, TestData.latitudes, path_curves())
hazard_model = ZarrHazardModel(source_paths=get_default_source_paths(Inventory(resources)), store=store)

hazard_model = ZarrHazardModel(source_paths=get_default_source_paths(EmbeddedInventory()), store=store)

return assets, store, hazard_model, expected

0 comments on commit e472bc2

Please sign in to comment.