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

Relax logic for comparing grid attributes between Run and Model objs #199

Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion aospy/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def _add_grid_attributes(self, ds):
ds_coord_name = set(names_ext).intersection(set(ds.coords) |
set(ds.data_vars))
model_attr = getattr(self.model, name_int, None)
if ds_coord_name:
if ds_coord_name and (model_attr is not None):
# Force coords to have desired name.
ds = ds.rename({list(ds_coord_name)[0]: name_int})
ds = ds.set_coords(name_int)
Expand Down
13 changes: 9 additions & 4 deletions aospy/data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import numpy as np
import xarray as xr

from .internal_names import ETA_STR, GRID_ATTRS, TIME_STR, TIME_BOUNDS_STR
from .internal_names import (ETA_STR, GRID_ATTRS, TIME_STR, TIME_BOUNDS_STR,
TIME_VAR_STRS)
from .utils import times, io


Expand Down Expand Up @@ -71,10 +72,10 @@ def grid_attrs_to_aospy_names(data):
# Prevents headaches when subsequently sub-setting.
if name_int in data.dims and not data[name_int].coords:
data = data.assign_coords(**{name_int: data[name_int]})
return data
return set_grid_attrs_as_coords(data, set_time_vars=False)


def set_grid_attrs_as_coords(ds):
def set_grid_attrs_as_coords(ds, set_time_vars=True):
"""Set available grid attributes as coordinates in a given Dataset.

Grid attributes are assumed to have their internal aospy names. Grid
Expand All @@ -91,7 +92,11 @@ def set_grid_attrs_as_coords(ds):
Dataset
Dataset with grid attributes set as coordinates
"""
int_names = GRID_ATTRS.keys()
if not set_time_vars:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slight preference to use the affirmative, i.e. if set_time_vars: as the main if statement, with the else corresponding to not set_time_Vars

int_names = (set(GRID_ATTRS.keys()) -
set(TIME_VAR_STRS))
else:
int_names = GRID_ATTRS.keys()
grid_attrs_in_ds = set(int_names).intersection(set(ds.coords) |
set(ds.data_vars))
ds.set_coords(grid_attrs_in_ds, inplace=True)
Expand Down
Binary file modified aospy/test/data/netcdf/00040101.precip_monthly.nc
Binary file not shown.
Binary file modified aospy/test/data/netcdf/00050101.precip_monthly.nc
Binary file not shown.
Binary file modified aospy/test/data/netcdf/00060101.precip_monthly.nc
Binary file not shown.
7 changes: 6 additions & 1 deletion docs/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ Bug Fixes
Hill <https://github.com/spencerahill>`_.
- Always write output to a tar file in serial to prevent empty header file
errors (fixes :issue:`75` via :pull:`197`). By `Spencer Clark
<https://github.com/spencerkclark>`_
<https://github.com/spencerkclark>`_.
- Allow ``aospy`` to use grid attributes that are only defined in ``Run``
objects. Previously if a grid attribute were defined only in a ``Run``
object and not also in the Run's corresponding ``Model``, an error would
be raised (fixes :issue:`187` via :pull:`199`). By `Spencer Clark
<https://github.com/spencerkclark>`_.
- When input data for a calculation has a time bounds array, overwrite
its time array with the average of the start and end times for each
timestep. Prevents bug wherein time arrays equal to either the
Expand Down