https://github.com/Unidata/netcdf4-python
Raw File
Tip revision: 1d43c79c011fc1f5f93f1520d92e24b2ae156890 authored by Jeff Whitaker on 26 February 2018, 20:53:12 UTC
add netcdftime dep
Tip revision: 1d43c79
json_att.py
from netCDF4 import Dataset
import json
# example showing how python objects (lists, dicts, None, True)
# can be serialized as strings, saved as netCDF attributes,
# and then converted back to python objects using json.
ds = Dataset('json.nc', 'w')
ds.pythonatt1 =  json.dumps([u'foo', {u'bar': [u'baz', None, 1.0, 2]}])
ds.pythonatt2 = "true" # converted to bool
ds.pythonatt3 = "null" # converted to None
print(ds)
ds.close()
ds = Dataset('json.nc')
def convert_json(s):
    try:
        a = json.loads(s)
        return a
    except:
        return s
x = convert_json(ds.pythonatt1)
print(type(x))
print(x)
print(convert_json(ds.pythonatt2))
print(convert_json(ds.pythonatt3))
ds.close()
back to top