Skip to content

Feature/custom grid litpop #1022

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

Draft
wants to merge 24 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f836170
Add target_grid option in LitPop.from_countries
zeliest Mar 3, 2025
14491bb
Update LitPop tests for target_grid argument
zeliest Mar 4, 2025
7d81f40
Merge branch 'develop' into feature/costum_grid_litpop
zeliest Mar 4, 2025
e938162
Add target grid litpop
zeliest Mar 7, 2025
9179696
Merge branch 'develop' into feature/costum_grid_litpop
zeliest Mar 7, 2025
44b0675
Move target grid definition to from_countries()
zeliest Mar 10, 2025
ac00534
Remove unused function in test_litpop
zeliest Mar 10, 2025
fbd2e9b
Update 15 arcsec grid definition and formatting
zeliest Mar 10, 2025
9c8704c
Continue refactoring of litpop with target_grid argument
zeliest Mar 18, 2025
8f5ba60
Uncomment test litpop
zeliest Mar 26, 2025
7874731
Merge branch 'develop' into feature/costum_grid_litpop
emanuel-schmid May 8, 2025
a9a2555
pylint
emanuel-schmid May 8, 2025
7491c0e
pylint
emanuel-schmid May 8, 2025
dbc5ceb
name arguments
emanuel-schmid May 8, 2025
b0d61ab
cosmetics
emanuel-schmid May 8, 2025
2415576
name arguments correctly
emanuel-schmid May 9, 2025
272ff46
move target_grid default into from_shape
emanuel-schmid May 9, 2025
a93aa3f
fix some tests
emanuel-schmid May 9, 2025
00f6afe
Merge branch 'develop' into feature/costum_grid_litpop
emanuel-schmid May 13, 2025
ce30fa4
distribute parts of _define_target_grid to respective helper modules
emanuel-schmid May 13, 2025
6bf9478
Merge branch 'develop' into feature/costum_grid_litpop
emanuel-schmid May 19, 2025
417af5d
Merge branch 'develop' into feature/costum_grid_litpop
emanuel-schmid Jun 2, 2025
8944221
make test_from_shape_zurich_pass pass
emanuel-schmid Jun 2, 2025
a87304f
test_from_shape_zurich_pass: remove notes about changed excpectations
emanuel-schmid Jun 2, 2025
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
51 changes: 51 additions & 0 deletions climada/entity/exposures/litpop/gpw_population.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import numpy as np
import rasterio
from affine import Affine

from climada import CONFIG
from climada.util.constants import SYSTEM_DIR
Expand Down Expand Up @@ -171,3 +172,53 @@
f" different folder. The data can be downloaded from {sedac_browse_url}, e.g.,"
f" {sedac_file_url} (Free NASA Earthdata login required)."
)


def grid_aligned_with_gpw(reference_year, gpw_version, res_arcsec, data_dir=SYSTEM_DIR):
"""
Defines a grid based on population metadata.

Parameters
----------
reference_year : int
The reference year for population and nightlight data.
gpw_version : int
Version number of GPW population data.
res_arcsec : int or None
Desired resolution in arcseconds. If None, aligns to population grid.
data_dir : str
Path to input data directory.

Returns
-------
grid : dict
A dictionary containing grid metadata, following the raster grid
specification.
"""
res_deg = res_arcsec / 3600

file_path = get_gpw_file_path(
gpw_version, reference_year, data_dir=data_dir, verbose=False
)
with rasterio.open(file_path, "r") as src:
global_crs = src.crs
gpw_transform = src.transform
# Align grid resolution with GPW dataset
aligned_lon_min = -180 + (round((gpw_transform[2] - (-180)) / res_deg) * res_deg)
aligned_lat_max = 90 - (round((90 - gpw_transform[5]) / res_deg) * res_deg)

global_transform = Affine(res_deg, 0, aligned_lon_min, 0, -res_deg, aligned_lat_max)

global_width = round(360 / res_deg)
global_height = round(180 / res_deg)

# Define the target grid using the computed values
return {

Check warning on line 216 in climada/entity/exposures/litpop/gpw_population.py

View check run for this annotation

Jenkins - WCR / Code Coverage

Not covered lines

Lines 198-216 are not covered by tests
"driver": "GTiff",
"dtype": "float32",
"nodata": None,
"crs": global_crs,
"width": global_width,
"height": global_height,
"transform": global_transform,
}
Loading
Loading