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

Remote NetCDF TableDAP #801

Merged
merged 4 commits into from
May 4, 2020
Merged

Conversation

daltonkell
Copy link
Contributor

Take elements of #799 and #800 to fetch remote NetCDF resources and uses this capability to fetch ERDDAP TableDAP datasets with the .ncCF extension.

benjwadams and others added 3 commits April 27, 2020 14:10
Issues an HTTP HEAD request and handles "Content-Type: application/x-netcdf"
headers by attempting to open a netCDF file in memory.  Opening in
memory will require the netCDF-C library to be compiled with that
feature.
Use the content-type request header to inform how a dataset
should be created. Remote NetCDF resources can be created via
fetching the raw bytes and instantiating a MemoizedDataset object.
Extend this capability to fetch TableDAP datasets by simply adding
the .ncCF extension and the string of variables to the dataset URL.
Copy link
Contributor

@benjwadams benjwadams left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good. You might want to integrate @ocefpaf's snippet in #799 for reading in files where reading datasets into memory in netCDF hasn't been enabled.

@daltonkell
Copy link
Contributor Author

Good call @benjwadams, I'll implement that

Use a try-catch routine to first attempt creating an in-memory
netCDF4-python Dataset. If the netcdf-c lib was compiled without
in-memory options, an OSError will be thrown; this is excepted
and a NamedTemporaryFile is used to hold the raw bytes.
timeout=60)
try:
return MemoizedDataset(response.content, memory=response.content)
except OSError as e:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

@@ -20,3 +23,30 @@ class MemoizedDataset(Dataset):
@lru_cache(128)
def get_variables_by_attributes(self, **kwargs):
return super(MemoizedDataset, self).get_variables_by_attributes(**kwargs)

@contextmanager
def tempnc(data: BinaryIO) -> Generator[str, None, None]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename this to create_temporary_ncfile or something more explicit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants