-
Notifications
You must be signed in to change notification settings - Fork 20
[POC]: Added "pc" fsspec filesystem #43
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
Open
TomAugspurger
wants to merge
1
commit into
main
Choose a base branch
from
feature/pc-filesystem
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 hidden or 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,90 @@ | ||
import fsspec | ||
import planetary_computer | ||
|
||
|
||
class PCFileSystem(fsspec.AbstractFileSystem): | ||
""" | ||
Planetary Computer filesystem for fsspec. | ||
|
||
This file system is solely a convenience for automatically | ||
signing assets in fsspec URLs. It uses fsspec's | ||
`URL chaining <https://filesystem-spec.readthedocs.io/en/latest/features.html#url-chaining>`_ | ||
and :meth:`planetary_computer.sign` to transform URLs like | ||
``pc://https://<account>.blob.core.windows.net/container/asset`` to the signed version. | ||
|
||
Parameters | ||
---------- | ||
target_protocol : str | ||
The protocol used to load the actual asset (e.g. 'https') | ||
target_options : dict | ||
Additional keywords to use for the target protocol's fsspec filesystem | ||
fo: str, optional | ||
The target path. | ||
|
||
Examples | ||
-------- | ||
This example loads a Kerchunk index file from Azure Blob Storage. The index file is in a private blob | ||
storage container and so needs to be signed. The ``pc`` in the URL will automatically sign the | ||
asset before attempting to load it. | ||
|
||
>>> import xarray as xr | ||
>>> url = "reference::pc::https://deltaresreservoirssa.blob.core.windows.net/references/reservoirs/CHIRPS.json" | ||
>>> result = xr.open_dataset(url, engine="zarr", consolidated=False) | ||
<xarray.Dataset> | ||
Dimensions: (time: 13515, GrandID: 2951, ksathorfrac: 5) | ||
Coordinates: | ||
* GrandID (GrandID) float64 nan nan nan nan nan ... nan nan nan nan nan | ||
* ksathorfrac (ksathorfrac) float64 5.0 20.0 50.0 100.0 250.0 | ||
* time (time) datetime64[ns] NaT NaT NaT NaT NaT ... NaT NaT NaT NaT | ||
Data variables: (12/14) | ||
ETa (time, GrandID, ksathorfrac) float32 dask.array<chunksize=(1, 2951, 5), meta=np.ndarray> | ||
Ea_res (time, GrandID, ksathorfrac) float32 dask.array<chunksize=(1, 2951, 5), meta=np.ndarray> | ||
FracFull (time, GrandID, ksathorfrac) float32 dask.array<chunksize=(1, 2951, 5), meta=np.ndarray> | ||
Melt (time, GrandID, ksathorfrac) float32 dask.array<chunksize=(1, 2951, 5), meta=np.ndarray> | ||
P (time, GrandID, ksathorfrac) float32 dask.array<chunksize=(1, 2951, 5), meta=np.ndarray> | ||
PET (time, GrandID, ksathorfrac) float32 dask.array<chunksize=(1, 2951, 5), meta=np.ndarray> | ||
... ... | ||
Qout_res (time, GrandID, ksathorfrac) float32 dask.array<chunksize=(1, 2951, 5), meta=np.ndarray> | ||
S_res (time, GrandID, ksathorfrac) float32 dask.array<chunksize=(1, 2951, 5), meta=np.ndarray> | ||
Snow (time, GrandID, ksathorfrac) float32 dask.array<chunksize=(1, 2951, 5), meta=np.ndarray> | ||
Temp (time, GrandID, ksathorfrac) float32 dask.array<chunksize=(1, 2951, 5), meta=np.ndarray> | ||
latitude (GrandID) float32 dask.array<chunksize=(2951,), meta=np.ndarray> | ||
longitude (GrandID) float32 dask.array<chunksize=(2951,), meta=np.ndarray> | ||
""" | ||
def __init__( | ||
self, | ||
target_protocol=None, | ||
target_options=None, | ||
fo=None, | ||
**kwargs, | ||
): | ||
self.target_protocol = target_protocol | ||
self.target_options = target_options | ||
if fo: | ||
fo = planetary_computer.sign(fo) | ||
self.fo = fo | ||
self.target_fs = fsspec.filesystem(self.target_protocol, **self.target_options) | ||
if isinstance(self.target_fs, fsspec.implementations.reference.ReferenceFileSystem): | ||
# this is a hack, but we need to sign the references after they've been loaded. | ||
# for k, v in self.target_fs.templates.items(): | ||
# print(k, v) | ||
# # print(k) | ||
# self.target_fs.templates[k] = planetary_computer.sign(v) | ||
|
||
# ReferenceFileSystem.__init__ does some processing, which means this is too late. | ||
for k, v in self.target_fs.references.items(): | ||
if isinstance(v, list) and len(v) == 3: | ||
# print("sign", k) | ||
self.target_fs.references[k] = [planetary_computer.sign(v[0]),] + v[1:] | ||
|
||
super().__init__(**kwargs) | ||
|
||
def open(self, path, mode="rb", block_size=None, cache_options=None, **kwargs): | ||
# print("open", path) | ||
if self.fo: | ||
path = self.fo | ||
return self.target_fs.open(path, mode=mode, block_size=block_size, cache_options=cache_options, **kwargs) | ||
|
||
def ls(self, path, detail=True, **kwargs): | ||
# print("ls", path) | ||
return self.target_fs.ls(path, detail=detail, **kwargs) |
This file contains hidden or 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
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.
I'm not a fan of this block.
The reference filesystem has the idea of "template" URLs, which are the NetCDF files in blob storage. We want to sign those URLs before anyone attempts to access data via this reference filesystem.
It seems that the reference filesystem's
__init__
calls a method at https://github.com/fsspec/filesystem_spec/blob/7effb83e8ab31010ec5796c14193b5fcd5774e05/fsspec/implementations/reference.py#L149, which does a lot of work including in-lining thetemplate
URLs in the reference(url, start, end)
tuples. Unfortunately, we don't have a way to update the template URLs before the tuples are built, so we have to do it again.