Open mberz opened 1 year ago
@mberz how about doing it this way:
from netCDF4 import Dataset
filename = 'some\file.sofa'
class SofaStream():
def __init__(self, filename):
self._filename = filename
def __enter__(self):
self._file = Dataset(filename, mode='r')
return self
def __exit__(self, *args):
self._file.close()
def __getattr__(self, name):
if name.startswith('GLOBAL_'):
name = name.replace('GLOBAL_', '')
# more handling of name. getattr would also
# return variables.
return getattr(self._file, name)
with SofaStream(filename) as file:
# access data inside SOFA files
# according to sofar naming conventions
print(file.GLOBAL_RoomType)
It would generally work. A problem is that the names inside the SOFA files follow NetCDF convention and are different from the names of the attributes in the sofar.SOFA object. Hints to how it could be done may be found in the __getitem__
method of the NetCDF Python API. But it is more tricky than initially expected
For large files reading the entire file into memory is not always economic. To solve this, we could support opening sofa files without reading the data into memory using the enter method. This is already supported by netcdf
In addition we could use support netcdfs slicing support for arrays to improve this even further, i.e. for channel wise reading of data or reading only partial spherical harmonic orders, etc.