Skip to content

Adding support for attributes (NetCDF only)#770

Merged
ZedThree merged 3 commits intonextfrom
attributes
Dec 8, 2017
Merged

Adding support for attributes (NetCDF only)#770
ZedThree merged 3 commits intonextfrom
attributes

Conversation

@bendudson
Copy link
Contributor

Currently only supports string and integer attributes, but should be easy to extend. Only implemented in the NetCDF4 writer, and other writers will just ignore the attributes. Part solution to issue #736

Includes an integrated test and some documentation.

  • DataFile now has setAttribute() function, which adds string or integer attributes to maps. This should really be better integrated with the rest of the variable's data (in VarStr), but that would require more changes. Example:

    dump.setAttribute("Ve", "location", "CELL_YLOW");

    or for the integer:

    dump.setAttribute("Ve", "location", (int) Ve.getLocation());

  • Added a python function boutdata.attributes which returns a dictionary of values

    from boutdata import attributes

    attributes("Ve")
    -> {'location':'CELL_YLOW'}

    This only works with the NetCDF4 library; other libraries or formats (HDF5) will return either None or empty dictionary.

Currently only supports string and integer attributes,
but should be easy to extend. Only implemented in the NetCDF4 writer,
and other writers will just ignore the attributes.

* DataFile now has setAttribute() function, which adds
  string or integer attributes to maps. This should really be better
  integrated with the rest of the variable's data (in VarStr), but
  that would require more changes. Example:

    dump.setAttribute("Ve", "location", "CELL_YLOW");

  or for the integer:

    dump.setAttribute("Ve", "location", (int) Ve.getLocation());

* Added a python function boutdata.attributes which returns a
  dictionary of values

    from boutdata import attributes

    attributes("Ve")
    -> {'location':'CELL_YLOW'}

  This only works with the NetCDF4 library; other libraries or
  formats (HDF5) will return either None or empty dictionary.
Runs a simple example, which creates attributes for a Field2D and
Field3D variable. Loads the attributes into Python and tests that they
are as expected.
Documents how to add attributes to the output file, and how to read
them into python.
@ZedThree ZedThree merged commit 45be876 into next Dec 8, 2017
@ZedThree ZedThree deleted the attributes branch December 8, 2017 11:33
@ZedThree ZedThree added this to the BOUT-4.2 milestone Dec 8, 2017
bendudson added a commit that referenced this pull request May 1, 2018
The datafile class now sets an attribute "cell_location"
for each 2D and 3D field (and vector component).

The attribute is a string which is the same as the enum name
e.g. CELL_CENTRE, CELL_YLOW etc.

To do this, a const map has been added to bout_types.hxx, together
with an inline function CELL_LOC_STRING(). This replaces
`strLocation` in interpolation.hxx, which used a switch.

Together with PR #770 (saving attributes in NetCDF)
and #996 (reads attributes into a BoutArray class), this now
works (in test-io):

```
> f3d = collect("f3d")
> f3d.attributes
{'cell_location': 'CELL_CENTRE', 'bout_type': 'Field3D'}
```

Added some checks to `test-io` to check that the cell_location
attribute is read correctly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants