-
Notifications
You must be signed in to change notification settings - Fork 91
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 metadata consolidation utility #278
Merged
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0a6c7fa
Add metadata consolidation utility
jrbourbeau 50f0cb6
Python 3.8
jrbourbeau 1fb9bd7
poetry.lock
jrbourbeau a303705
Updates
jrbourbeau 61d256b
Better gate imports
jrbourbeau a7760af
Lint
jrbourbeau fef1271
Add tests
jrbourbeau 81b25a3
Lint
jrbourbeau 079f385
Updates
jrbourbeau cd260a5
Support in-memory consolidated metadata
jrbourbeau 980cc76
Merge branch 'main' of https://github.com/nsidc/earthaccess into kerc…
jrbourbeau 4828ff9
Merge branch 'main' of https://github.com/nsidc/earthaccess into kerc…
jrbourbeau cd62af2
Lint
jrbourbeau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from __future__ import annotations | ||
|
||
from dask.base import flatten | ||
from dask.distributed import default_client, progress, Client, Worker, WorkerPlugin | ||
from kerchunk.combine import MultiZarrToZarr | ||
from kerchunk.hdf import SingleHdf5ToZarr | ||
|
||
import earthaccess | ||
from .auth import Auth | ||
|
||
|
||
def get_chunk_metadata( | ||
granuale: earthaccess.results.DataGranule, access: str | ||
) -> list[dict]: | ||
if access == "direct": | ||
fs_data = earthaccess.get_s3fs_session(provider=granuale["meta"]["provider-id"]) | ||
else: | ||
fs_data = earthaccess.get_fsspec_https_session() | ||
|
||
metadata = [] | ||
for url in granuale.data_links(access=access): | ||
with fs_data.open(url) as inf: | ||
h5chunks = SingleHdf5ToZarr(inf, url) | ||
m = h5chunks.translate() | ||
metadata.append(m) | ||
return metadata | ||
|
||
|
||
class EarthAccessAuth(WorkerPlugin): | ||
name = "earthaccess-auth" | ||
|
||
def __init__(self, auth: Auth): | ||
self.auth = auth | ||
|
||
def setup(self, worker: Worker) -> None: | ||
if not earthaccess.__auth__.authenticated: | ||
earthaccess.__auth__ = self.auth | ||
earthaccess.login() | ||
|
||
|
||
def consolidate_metadata( | ||
granuales: list[earthaccess.results.DataGranule], | ||
outfile: str, | ||
storage_options: dict | None = None, | ||
kerchunk_options: dict | None = None, | ||
access: str = "direct", | ||
client: Client | None = None, | ||
) -> str: | ||
if client is None: | ||
client = default_client() | ||
|
||
# Make sure cluster is authenticated | ||
client.register_worker_plugin(EarthAccessAuth(earthaccess.__auth__)) | ||
|
||
# Write out metadata file for each granuale | ||
futures = client.map(get_chunk_metadata, granuales, access=access) | ||
progress(futures) | ||
chunks = client.gather(futures) | ||
chunks = list(flatten(chunks)) | ||
|
||
# Write combined metadata file | ||
mzz = MultiZarrToZarr(chunks, **kerchunk_options) | ||
mzz.translate(outfile, storage_options=storage_options or {}) | ||
|
||
return outfile |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we try to kerchunk and consolidate non uniform datasets kerchunk will fail, if we try to kerchunk 1 granule kerchunk succeeds but MultiZarrToZarr requires a variable mapping, if we don't pass it fails, for one granule we should use
kerchunk_options={"coo_map": {}}
and then kerchunks stays happy.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that the same as not doing any combination at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess? or maybe we can just skip the MultiZarrToZarr when the input is only 1 file. I wanted to do a quick test on a granule from https://podaac.jpl.nasa.gov/MEaSUREs-MUR and ran into this issue.