diff --git a/city_metrix/layers/era_5_hottest_day.py b/city_metrix/layers/era_5_hottest_day.py index eaa4ce6..44718eb 100644 --- a/city_metrix/layers/era_5_hottest_day.py +++ b/city_metrix/layers/era_5_hottest_day.py @@ -18,13 +18,18 @@ def __init__(self, start_date="2023-01-01", end_date="2024-01-01", **kwargs): self.end_date = end_date def get_data(self, bbox): + min_lon, min_lat, max_lon, max_lat = bbox + center_lon = (min_lon + max_lon) / 2 + center_lat = (min_lat + max_lat) / 2 + dataset = ee.ImageCollection("ECMWF/ERA5_LAND/HOURLY") # Function to find the city mean temperature of each hour def hourly_mean_temperature(image): + point_crs = 'EPSG:4326' hourly_mean = image.select('temperature_2m').reduceRegion( reducer=ee.Reducer.mean(), - geometry=ee.Geometry.BBox(*bbox), + geometry=ee.Geometry.Point([center_lon, center_lat], point_crs), scale=11132, bestEffort=True ).values().get(0) @@ -49,10 +54,6 @@ def hourly_mean_temperature(image): day = highest_temperature_day[6:8] time = highest_temperature_day[-2:] - min_lon, min_lat, max_lon, max_lat = bbox - center_lon = (min_lon + max_lon) / 2 - center_lat = (min_lat + max_lat) / 2 - # Initialize TimezoneFinder tf = TimezoneFinder() # Find the timezone of the center point diff --git a/tests/conftest.py b/tests/conftest.py index e140ab7..ec2563f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -18,9 +18,21 @@ def test_create_fishnet_grid(min_x, min_y, max_x, max_y, cell_size): fishnet.drop('fishnet_geometry', axis=1, inplace=True) return fishnet + +def _create_gdf_from_coords(xmin, ymin, xmax, ymax): + from shapely import geometry + import geopandas as gp + geom_array = [] + poly = geometry.Polygon(((xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin))) + geom_array.append(poly) + gdf = gp.GeoDataFrame(geom_array, columns=["geometry"]).set_crs("EPSG:4326") + return gdf + + # Test zones of a regular 0.01x0.01 grid over a 0.1x0.1 extent by degrees ZONES = test_create_fishnet_grid(106.7, -6.3, 106.8, -6.2, 0.01).reset_index() LARGE_ZONES = test_create_fishnet_grid(106, -7, 107, -6, 0.1).reset_index() +OR_PORTLAND_NO_TILE_ZONE = _create_gdf_from_coords(-122.7037,45.51995,-122.6923117,45.5232773) class MockLayer(Layer): diff --git a/tests/test_metrics.py b/tests/test_metrics.py index 07090af..5c6e85f 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -1,5 +1,5 @@ from city_metrix import * -from .conftest import ZONES, EXECUTE_IGNORED_TESTS +from .conftest import ZONES, EXECUTE_IGNORED_TESTS, OR_PORTLAND_NO_TILE_ZONE import pytest @@ -24,6 +24,12 @@ def test_built_land_without_tree_cover(): assert expected_zone_size == actual_indicator_size +@pytest.mark.skipif(EXECUTE_IGNORED_TESTS == False, reason="CDS API needs personal access token file to run") +def test_era_5_met_preprocess_portland(): + indicator = era_5_met_preprocessing(OR_PORTLAND_NO_TILE_ZONE) + assert len(indicator) == 24 + + @pytest.mark.skipif(EXECUTE_IGNORED_TESTS == False, reason="CDS API needs personal access token file to run") def test_era_5_met_preprocess(): indicator = era_5_met_preprocessing(ZONES)