Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mberz opened this issue Mar 15, 2023 · 3 comments · May be fixed by #83
Open

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

mberz opened this issue Mar 15, 2023 · 3 comments · May be fixed by #83
Assignees
Labels
enhancement New feature or request

Comments

@mberz
Copy link
Member

mberz commented Mar 15, 2023

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.

@mberz mberz added the enhancement New feature or request label Mar 15, 2023
@f-brinkmann
Copy link
Member

@f-brinkmann
Copy link
Member

f-brinkmann commented May 11, 2023

@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
Copy link
Member

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

@hoyer-a hoyer-a self-assigned this Apr 22, 2024
@hoyer-a hoyer-a linked a pull request May 15, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants