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

adding unit and name attributes to layers #36

Merged
merged 5 commits into from
Jul 14, 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
16 changes: 13 additions & 3 deletions src/openmethane_prior/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
from openmethane_prior.layers import layer_names
from openmethane_prior.utils import SECS_PER_YEAR

coordNames = ["TSTEP", "LAY", "ROW", "COL"]
COORD_NAMES = ["TSTEP", "LAY", "ROW", "COL"]
REQUIRED_ATTRIBUTES = {"units": "kg/m^2/s"}
TOTAL_LAYER_NAME = "OCH4_TOTAL"
TOTAL_LAYER_LONG_NAME = "total methane flux"


def convert_to_timescale(emission, cell_area):
Expand Down Expand Up @@ -75,7 +78,11 @@ def write_layer(
# coerce to four dimensions if it's not
for i in range(layer_data.ndim, 4):
copy = np.expand_dims(copy, 0) # should now have four dimensions
ds[layer_name] = (coordNames[:], copy)
ds[layer_name] = (COORD_NAMES[:], copy)

for k, v in REQUIRED_ATTRIBUTES.items():
ds[layer_name].attrs[k] = v
ds[layer_name].attrs["long_name"] = layer_name

output_path.parent.mkdir(parents=True, exist_ok=True)
ds.to_netcdf(output_path)
Expand Down Expand Up @@ -112,5 +119,8 @@ def sum_layers(output_path: pathlib.Path):
summed += ds[layerName].values # it will broadcast time dimensions of 1 correctly

if summed is not None:
ds["OCH4_TOTAL"] = (["date", "LAY", *coordNames[-2:]], summed)
ds[TOTAL_LAYER_NAME] = (["date", "LAY", *COORD_NAMES[-2:]], summed)
for k, v in REQUIRED_ATTRIBUTES.items():
ds[TOTAL_LAYER_NAME].attrs[k] = v
ds[TOTAL_LAYER_NAME].attrs["long_name"] = TOTAL_LAYER_LONG_NAME
ds.to_netcdf(output_path)
3 changes: 2 additions & 1 deletion src/openmethane_prior/raster.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os

import rasterio as rio
from openmethane_prior.config import PriorConfig
from rasterio.warp import Resampling, calculate_default_transform, reproject

from openmethane_prior.config import PriorConfig


def reproject_tiff(image, output, dst_crs="EPSG:4326", resampling="nearest", **kwargs):
"""Reprojects an image.
Expand Down
12 changes: 11 additions & 1 deletion tests/test_om_prior.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import pandas as pd
import requests
import xarray as xr

from openmethane_prior.layers.omGFASEmis import download_GFAS
from openmethane_prior.utils import SECS_PER_YEAR

Expand Down Expand Up @@ -104,3 +103,14 @@ def test_compare_out_domain_with_cro_dot_files(output_domain, cro_xr, dot_xr):

assert cro_xr.NCOLS == output_domain.COL.size
assert cro_xr.NROWS == output_domain.ROW.size


def test_required_attributes(output_domain):
assert output_domain.variables["OCH4_TOTAL"].attrs == {
"units": "kg/m^2/s",
"long_name": "total methane flux",
}
assert output_domain.variables["OCH4_WETLANDS"].attrs == {
"units": "kg/m^2/s",
"long_name": "OCH4_WETLANDS",
}
Loading