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

Add dodola get-attrs command #133

Merged
merged 2 commits into from
Nov 13, 2021
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
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ History

0.X.X (XXXX-XX-XX)
------------------
* Add `dodola get-attrs` command. (PR #133, @brews)
* Upgrade Docker base image to ``continuumio/miniconda3:4.10.3``. (PR #132, @brews)


Expand Down
8 changes: 8 additions & 0 deletions dodola/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,14 @@ def removeleapdays(x, out):
services.remove_leapdays(x, out)


@dodola_cli.command(help="Get attrs from data")
@click.argument("x", required=True)
@click.option("--variable", "-v", help="Variable name in data stores")
def get_attrs(x, variable=None):
"""Get JSON str of data attrs metadata."""
click.echo(services.get_attrs(x, variable))


@dodola_cli.command(help="Bias-correct GCM on observations")
@click.argument("x", required=True)
@click.argument("xtrain", required=True)
Expand Down
9 changes: 9 additions & 0 deletions dodola/services.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Used by the CLI or any UI to deliver services to our lovely users
"""
from functools import wraps
import json
import logging
from dodola.core import (
apply_bias_correction,
Expand Down Expand Up @@ -432,6 +433,14 @@ def apply_qplad(
storage.write(out, adjusted_ds, region=out_zarr_region)


def get_attrs(x, variable=None):
"""Get JSON str of `x` attrs metadata."""
d = storage.read(x)
if variable:
d = d[variable]
return json.dumps(d.attrs)


@log_service
def bias_correct(x, x_train, train_variable, y_train, out, out_variable, method):
"""Bias correct input model data with IO to storage
Expand Down
2 changes: 2 additions & 0 deletions dodola/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"validate-dataset",
"prime-qdm-output-zarrstore",
"prime-qplad-output-zarrstore",
"get-attrs",
],
ids=(
"--help",
Expand All @@ -35,6 +36,7 @@
"validate-dataset --help",
"prime-qdm-output-zarrstore --help",
"prime-aipqd-output-zarrstore --help",
"get-attrs --help",
),
)
def test_cli_helpflags(subcmd):
Expand Down
22 changes: 22 additions & 0 deletions dodola/tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
train_qplad,
apply_qplad,
validate,
get_attrs,
)
import dodola.repository as repository

Expand Down Expand Up @@ -1320,3 +1321,24 @@ def test_validation(variable, data_type, time_period):
repository.write(in_url, ds)

validate(in_url, variable, data_type, time_period)


def test_get_attrs_global():
"""Test that services.get_attrs returns json of global attrs"""
url = "memory://test_get_attrs_global/x.zarr"
repository.write(url, xr.Dataset({"bar": "SPAM"}, attrs={"fish": "chips"}))
out = get_attrs(url)
assert out == '{"fish": "chips"}'


def test_get_attrs_variable():
"""Test that services.get_attrs returns json of variable attrs"""
url = "memory://test_get_attrs/x.zarr"
variable_name = "bar"
ds_in = xr.Dataset({variable_name: "SPAM"}, attrs={"fish": "chips"})
ds_in[variable_name].attrs["carrot"] = "sticks"

repository.write(url, ds_in)
out = get_attrs(url, variable=variable_name)

assert out == '{"carrot": "sticks"}'