pyfar / sofar

maybe the most complete python package for SOFA files so far
https://pyfar.org/
MIT License
20 stars 0 forks source link

Add support to open/enter a sofa file without reading the data #58

Open mberz opened 1 year ago

mberz commented 1 year ago

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

with Dataset(filename, "r", format="NETCDF4") as file:
    # read the data here

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.

f-brinkmann commented 1 year ago

This might help: https://python-sofa.readthedocs.io/en/latest/_modules/sofa/_database.html#Database.open

f-brinkmann commented 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)
f-brinkmann commented 7 months ago

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