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 units/description to netcdf files #201

Closed
chuaxr opened this issue Sep 5, 2017 · 8 comments
Closed

Adding units/description to netcdf files #201

chuaxr opened this issue Sep 5, 2017 · 8 comments

Comments

@chuaxr
Copy link

chuaxr commented Sep 5, 2017

Currently, the descriptions/units in Var objects are not saved in the output netcdf files. It would be nice if this was the case.

@spencerahill
Copy link
Owner

Sure, I agree this would be nice, and it also should be relatively straightforward for us to implement.

Initial quick take: this will require adding a 'units' attribute to the DataArray somewhere in the Calc pipeline. One catch is different units whether or not the output type is a vertical integral; that logic is handled here: https://github.com/spencerahill/aospy/blob/develop/aospy/calc.py#L457

@spencerkclark
Copy link
Collaborator

One catch is different units whether or not the output type is a vertical integral

Indeed this is true; in the absence of more robust units support (e.g. with pint) there are more or less two options in this case:

  1. Simply drop the units attribute
  2. Append 'kg m^-2' to the units attribute, which while dimensionally correct, might look a little strange for units that could be simplified.

@spencerahill
Copy link
Owner

Append 'kg m^-2' to the units attribute, which while dimensionally correct, might look a little strange for units that could be simplified.

True, but I believe that's better than no units. What about being explicit about the vertical integral operation? E.g. '(vertical integral of {0}): {0} kg m^-2)'.format(units).

@spencerkclark
Copy link
Collaborator

True, but I believe that's better than no units. What about being explicit about the vertical integral operation? E.g. '(vertical integral of {0}): {0} kg m^-2)'.format(units).

Sounds good to me!

@micahkim23
Copy link
Collaborator

micahkim23 commented Nov 1, 2017

I started looking at this issue and was planning on adding attrs to data in the save function https://github.com/spencerahill/aospy/blob/develop/aospy/calc.py#L669. The only weird thing is that data can either be an DataArray or OrderedDict.

OrderedDict([('globe', <xarray.DataArray ()>
array(1.0588634264346013e-05)
Coordinates:
    raw_data_start_date  datetime64[ns] 1678-01-01
    raw_data_end_date    datetime64[ns] 1681-01-01
    subset_start_date    datetime64[ns] 1678-01-01
    subset_end_date      datetime64[ns] 1680-12-31), ('tropics', <xarray.DataArray ()>
array(5.266911604961124e-06)
Coordinates:
    raw_data_start_date  datetime64[ns] 1678-01-01
    raw_data_end_date    datetime64[ns] 1681-01-01
    subset_start_date    datetime64[ns] 1678-01-01
    subset_end_date      datetime64[ns] 1680-12-31)])

In this case I add the units/description to each DataArray inside of the OrderedDict?

@spencerahill
Copy link
Owner

spencerahill commented Nov 1, 2017

@micahkim23 I think we can avoid this if we add the metadata earlier in the pipeline. E.g. before this line in Calc.compute. I think the subsequent time and regional reductions will preserve the attrs.

@micahkim23
Copy link
Collaborator

@spencerahill When I add attrs to a Dataset, the Dataset gets the attrs but the DataArrays inside the Dataset don't get the attrs. Do you want me to add the attrs to the DataArrays inside the Dataset?

@spencerkclark
Copy link
Collaborator

@micahkim23 I think it would make the most sense if these attributes were added to only DataArrays (because the units and description attributes are variable-specific attributes). Attributes of a Dataset can be thought of as more "global" attributes, in the sense that they apply to all the variables it contains.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants