From 5c89870aa83c0a1fdae9c04b9c5155183ba4e8e8 Mon Sep 17 00:00:00 2001 From: weiqi-tori Date: Thu, 20 Jun 2024 17:15:03 +0800 Subject: [PATCH] add ERA 5 highest temperature layer --- city_metrix/layers/__init__.py | 1 + city_metrix/layers/era_5_high_temperature.py | 39 ++++++++++++++++++++ tests/layers.py | 6 ++- 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 city_metrix/layers/era_5_high_temperature.py diff --git a/city_metrix/layers/__init__.py b/city_metrix/layers/__init__.py index bef56c3..307b219 100644 --- a/city_metrix/layers/__init__.py +++ b/city_metrix/layers/__init__.py @@ -16,3 +16,4 @@ from .open_buildings import OpenBuildings from .tree_canopy_hight import TreeCanopyHeight from .alos_dsm import AlosDSM +from .era_5_high_temperature import Era5HighTemperature diff --git a/city_metrix/layers/era_5_high_temperature.py b/city_metrix/layers/era_5_high_temperature.py new file mode 100644 index 0000000..dac8b62 --- /dev/null +++ b/city_metrix/layers/era_5_high_temperature.py @@ -0,0 +1,39 @@ +import ee + +from .layer import Layer, get_image_collection + + +class Era5HighTemperature(Layer): + def __init__(self, start_date="2023-01-01", end_date="2024-01-01", **kwargs): + super().__init__(**kwargs) + self.start_date = start_date + self.end_date = end_date + + def get_data(self, bbox): + dataset = ee.ImageCollection("ECMWF/ERA5_LAND/HOURLY") + + # Function to find the maximum value - highest temperature - pixel in each image + def highest_temperature_image(image): + max_pixel = image.reduceRegion( + reducer=ee.Reducer.max(), + geometry=ee.Geometry.BBox(*bbox), + scale=11132, + bestEffort=True + ).values().get(0) + + return image.set('highest_temperature', max_pixel) + + era5 = ee.ImageCollection(dataset + .filterBounds(ee.Geometry.BBox(*bbox)) + .filterDate(self.start_date, self.end_date) + .select('temperature_2m') + ) + + era5_highest = era5.map(highest_temperature_image) + + # Sort the collection based on the highest temperature and get the first image + highest_temperature_day = ee.ImageCollection(era5_highest.sort('highest_temperature', False).first()) + + data = get_image_collection(highest_temperature_day, bbox, 11132, "ERA 5 Temperature").temperature_2m + + return data diff --git a/tests/layers.py b/tests/layers.py index adf01ea..05b6351 100644 --- a/tests/layers.py +++ b/tests/layers.py @@ -1,6 +1,6 @@ import ee -from city_metrix.layers import LandsatCollection2, Albedo, LandSurfaceTemperature, EsaWorldCover, EsaWorldCoverClass, TreeCover, AverageNetBuildingHeight, OpenStreetMap, OpenStreetMapClass, UrbanLandUse, OpenBuildings, TreeCanopyHeight, AlosDSM +from city_metrix.layers import LandsatCollection2, Albedo, LandSurfaceTemperature, EsaWorldCover, EsaWorldCoverClass, TreeCover, AverageNetBuildingHeight, OpenStreetMap, OpenStreetMapClass, UrbanLandUse, OpenBuildings, TreeCanopyHeight, AlosDSM, Era5HighTemperature from city_metrix.layers.layer import get_image_collection from .conftest import MockLayer, MockMaskLayer, ZONES, LARGE_ZONES, MockLargeLayer, MockGroupByLayer, \ MockLargeGroupByLayer @@ -108,3 +108,7 @@ def test_tree_canopy_hight(): def test_AlosDSM(): mean = AlosDSM().get_data(SAMPLE_BBOX).mean() assert mean + +def test_era_5_high_temperature(): + mean = Era5HighTemperature().get_data(SAMPLE_BBOX).mean() + assert mean