Skip to content

[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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions planetary_computer/_pc_fs.py
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):
Copy link
Author

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 the template 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.

# 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)
7 changes: 7 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ install_requires =
pytz>=2020.5
requests>=2.25.1

[options.extras_requires]
fsspec =
fsspec

[options.entry_points]
console_scripts =
planetarycomputer = planetary_computer.scripts.cli:app

fsspec.specs =
pc = planetary_computer._pc_fs.PCFileSystem