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 20 commits into
base: develop
Choose a base branch
from
Draft
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
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