Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switching to point retrieval of hottest day from GEE #109

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions city_metrix/layers/era_5_hottest_day.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 7 additions & 1 deletion tests/test_metrics.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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)
Expand Down
Loading