Skip to content

Commit

Permalink
Fix xarray metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
b8raoult committed Jan 24, 2021
1 parent d2d3622 commit e656ad1
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 14 deletions.
2 changes: 1 addition & 1 deletion climetlab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

# import logging

__version__ = "0.1.7"
__version__ = "0.1.8"


# if ipython_active:
Expand Down
8 changes: 5 additions & 3 deletions climetlab/datasets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ def __getattr__(self, name):

def load_dataset(name, *args, **kwargs):
try:
# Backwards compatibility
ds = dataset(name, *args, **kwargs)
assert getattr(ds, "_load", None) is None
return ds
except Exception:
ds = dataset(name)
ds._load(*args, **kwargs)
return ds
except Exception:
# Backwards compatibility
return dataset(name, *args, **kwargs)
2 changes: 1 addition & 1 deletion climetlab/helpers/ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def plot_map(self, driver):
)

driver.plot_numpy(
self.data,
self.data.reshape(metadata.get("shape", self.data.shape)),
metadata=metadata,
)

Expand Down
21 changes: 13 additions & 8 deletions climetlab/helpers/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,20 @@ def plot_map(self, driver):
driver.plot_xarray(self.data, self.name, dimension_settings)

def field_metadata(self):
return dict(
north=self.north,
south=self.south,
east=self.east,
west=self.west,
# # TODO:
# south_north_increment=None,
# west_east_increment=None,
result = dict(shape=self.var.shape)
result.update(self.var.attrs)
result.update(
dict(
north=self.north,
south=self.south,
east=self.east,
west=self.west,
# # TODO:
# south_north_increment=None,
# west_east_increment=None,
)
)
return result


def helper(data, *args, **kwargs):
Expand Down
10 changes: 9 additions & 1 deletion climetlab/plotting/drivers/magics/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,22 @@ def plot_numpy(
data.shape[-1] - 1
)

# TODO: remove me when Magics supports full json
def tidy(x):
r = {}
for k, v in x.items():
if v is None or isinstance(v, (int, float, str, bool)):
r[k] = v
return r

self._push_layer(
minput(
input_field=data,
input_field_initial_latitude=float(north),
input_field_latitude_step=-float(south_north_increment),
input_field_initial_longitude=float(west),
input_field_longitude_step=float(west_east_increment),
input_metadata=metadata,
input_metadata=tidy(metadata),
)
)

Expand Down
1 change: 1 addition & 0 deletions climetlab/sources/readers/grib.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def field_metadata(self):
p = self.handle.get(n)
if p is not None:
m[n] = str(p)
m["shape"] = self.shape
return m

def helper(self):
Expand Down

0 comments on commit e656ad1

Please sign in to comment.