Skip to content

Commit

Permalink
Merge pull request #31 from boutproject/file-attributes
Browse files Browse the repository at this point in the history
Read and write file-level attributes in DataFile
  • Loading branch information
johnomotani authored Jan 19, 2021
2 parents d85070b + 0630c08 commit e8b8468
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions boututils/datafile.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def __init__(
write=write,
create=create,
format=format,
**kwargs
**kwargs,
)
elif format == "HDF5":
self.impl = DataFile_HDF5(
Expand Down Expand Up @@ -309,6 +309,12 @@ def write(self, name, data, info=False):
"""
return self.impl.write(name, data, info)

def read_file_attribute(self, name):
return self.impl.read_file_attribute(name)

def write_file_attribute(self, name, value):
return self.impl.write_file_attribute(name, value)

def __getitem__(self, name):
return self.impl.__getitem__(name)

Expand Down Expand Up @@ -356,7 +362,7 @@ def __init__(
write=False,
create=False,
format="NETCDF3_CLASSIC",
**kwargs
**kwargs,
):
self._kwargs = kwargs
if not has_netCDF:
Expand Down Expand Up @@ -669,6 +675,15 @@ def find_dim(dim):
except AttributeError:
pass

def read_file_attribute(self, name):
try:
return getattr(self.handle, name)
except AttributeError:
raise AttributeError(f"DataFile (netCDF4) has no file attribute {name}")

def write_file_attribute(self, name, value):
setattr(self.handle, name, value)

def attributes(self, varname):
try:
return self._attributes_cache[varname]
Expand Down Expand Up @@ -970,6 +985,15 @@ def write(self, name, data, info=False):
# data is not a BoutArray, so doesn't have attributes to write
pass

def read_file_attribute(self, name):
try:
return self.handle.attrs[name]
except KeyError:
raise AttributeError(f"DataFile (HDF5) has no file attribute {name}")

def write_file_attribute(self, name, value):
self.handle.attrs[name] = value

def attributes(self, varname):

try:
Expand Down

0 comments on commit e8b8468

Please sign in to comment.