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

fix(grid): Fix regression in global gridding #51

Merged
merged 1 commit into from
May 21, 2024
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
28 changes: 18 additions & 10 deletions src/concordia/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,13 @@ def to_netcdf(
class Proxy:
data: xr.DataArray
indexrasters: dict[str, pt.IndexRaster]
cell_area: xr.DataArray | None
name: str = "unnamed"
as_flux: bool = True

@classmethod
def from_variables(cls, df, indexrasters=None, proxy_dir=None, **kwargs):
def from_variables(
cls, df, indexrasters=None, proxy_dir=None, cell_area=None, as_flux=None
):
if isinstance(df, VariableDefinitions):
df = df.data
if proxy_dir is None:
Expand Down Expand Up @@ -147,19 +149,25 @@ def from_variables(cls, df, indexrasters=None, proxy_dir=None, **kwargs):
f"Variables need indexrasters for all griddinglevels: {', '.join(griddinglevels)}"
)

if as_flux is False:
cell_area = None
elif cell_area is not None:
cell_area = cell_area.astype(proxy.dtype, copy=False)
elif as_flux:
indexraster = next(i for i in indexrasters.values() if i is not None)
cell_area = indexraster.cell_area.astype(proxy.dtype, copy=False)

return cls(
proxy, {l: indexrasters.get(l) for l in griddinglevels}, name=name, **kwargs
proxy,
{l: indexrasters.get(l) for l in griddinglevels},
cell_area=cell_area,
name=name,
)

@property
def cell_area(self):
indexraster = next(i for i in self.indexrasters.values() if i is not None)
return indexraster.cell_area.astype(self.data.dtype, copy=False)

@cached_property
def proxy_as_flux(self):
da = self.data
if self.as_flux:
if self.cell_area is not None:
da = da / self.cell_area
return da

Expand Down Expand Up @@ -217,7 +225,7 @@ def verify_gridded(self, gridded, downscaled, compute: bool = True):
scen = self.prepare_downscaled(downscaled)

global_gridded = self.reduce_dimensions(gridded)
if self.as_flux:
if self.cell_area is not None:
global_gridded *= self.cell_area
global_gridded = global_gridded.sum(["lat", "lon"])
diff = verify_global_values(
Expand Down
1 change: 1 addition & 0 deletions src/concordia/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def proxies(self):
self.variabledefs.for_proxy(proxy_name),
dict(country=self.indexraster_country, region=self.indexraster_region),
self.settings.proxy_path,
as_flux=True,
)
for proxy_name in self.variabledefs.proxies
}
Expand Down