Skip to content

Commit

Permalink
add units and description to output netcdf files
Browse files Browse the repository at this point in the history
  • Loading branch information
micahkim23 committed Nov 12, 2017
1 parent 1a39157 commit 6fab9bb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
23 changes: 20 additions & 3 deletions aospy/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ def region_calcs(self, arr, func):
**{reg.name + '_pressure': coord}
)
reg_dat.update(**{reg.name: data_out})
return OrderedDict(sorted(reg_dat.items(), key=lambda t: t[0]))
return xr.Dataset(reg_dat)

def _apply_all_time_reductions(self, full_ts, monthly_ts, eddy_ts):
"""Apply all requested time reductions to the data."""
Expand Down Expand Up @@ -587,6 +587,7 @@ def compute(self, write_to_tar=True):
reduced = self._apply_all_time_reductions(full, monthly, eddy)
logging.info("Writing desired gridded outputs to disk.")
for dtype_time, data in reduced.items():
self.addAttrs(data)
self.save(data, dtype_time, dtype_out_vert=self.dtype_out_vert,
save_files=True, write_to_tar=write_to_tar)
return self
Expand All @@ -601,9 +602,9 @@ def _save_files(self, data, dtype_out_time):
reg_data = xr.open_dataset(path)
except (EOFError, RuntimeError, IOError):
reg_data = xr.Dataset()
# Add the new data to the dictionary or Dataset.
# Same method works for both.
# Add the new data to the Dataset.
reg_data.update(data)
reg_data.attrs = data.attrs
data_out = reg_data
else:
data_out = data
Expand Down Expand Up @@ -767,3 +768,19 @@ def load(self, dtype_out_time, dtype_out_vert=False, region=False,
if plot_units:
data = self.var.to_plot_units(data, dtype_vert=dtype_out_vert)
return data

def addAttrs(self, data):
if (isinstance(data, xr.DataArray)):
self.addAttrsDataArray(data)
else:
for name, da in data.data_vars.items():
self.addAttrsDataArray(da)

def addAttrsDataArray(self, data):
if self.var.units != '':
if self.var.dtype_out_vert == 'vert_int':
data.attrs['units'] = '(vertical integral of {0}): {0} kg m^-2)'.format(self.var.units)
else:
data.attrs['units'] = self.var.units
if self.var.description != '':
data.attrs['description'] = self.var.description
3 changes: 3 additions & 0 deletions docs/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ Corrected link to documentation badge on repository main page (:pull:213). By Da
Enhancements
~~~~~~~~~~~~

- Add units and description from ``Var`` objects to output netcdf
files (closes :issue:`201` via :pull:`227`). By `Micah Kim
<https://github.com/micahkim23>`_.
- Remove potentially confusing attributes from example netcdf files.
(closes :issue:`214` via :pull:`216`). By `Micah Kim
<https://github.com/micahkim23>`_.
Expand Down

0 comments on commit 6fab9bb

Please sign in to comment.