Skip to content

Instantly share code, notes, and snippets.

@rabernat
Last active July 16, 2019 18:12
Show Gist options
  • Save rabernat/cb822b190619e7368f834a16deef41e3 to your computer and use it in GitHub Desktop.
Save rabernat/cb822b190619e7368f834a16deef41e3 to your computer and use it in GitHub Desktop.
Zarr over http
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Open Zarr over HTTP using fsspec"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<xarray.Dataset>\n",
"Dimensions: (x: 20, y: 10)\n",
"Coordinates:\n",
" * x (x) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19\n",
" * y (y) int64 0 1 2 3 4 5 6 7 8 9\n",
"Data variables:\n",
" foo (y, x) float64 1.0 1.0 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0 1.0 1.0"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# create test data\n",
"import xarray as xr\n",
"import numpy as np\n",
"ds = xr.DataArray(np.ones((10, 20)), dims=['y', 'x'],\n",
" coords={'x': np.arange(20),\n",
" 'y': np.arange(10)},\n",
" attrs={'standards': 'None whatsover'},\n",
" name='foo').to_dataset()\n",
"ds"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<xarray.backends.zarr.ZarrStore at 0x11c5c1198>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# export to zarr directory store\n",
"ds.to_zarr('test_remote.zarr', mode='w', consolidated=True)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<subprocess.Popen at 0x11c692780>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# spawn a web server in the background to serve the data\n",
"import subprocess\n",
"subprocess.Popen([\"python\", \"-m\", \"http.server\", \"9999\"])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<xarray.Dataset>\n",
"Dimensions: (x: 20, y: 10)\n",
"Coordinates:\n",
" * x (x) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19\n",
" * y (y) int64 0 1 2 3 4 5 6 7 8 9\n",
"Data variables:\n",
" foo (y, x) float64 dask.array<shape=(10, 20), chunksize=(10, 20)>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# reopen it\n",
"from fsspec.implementations.http import HTTPFileSystem\n",
"fs = HTTPFileSystem()\n",
"http_map = fs.get_mapper('http://0.0.0.0:9999/test_remote.zarr')\n",
"ds_remote = xr.open_zarr(http_map, consolidated=True)\n",
"ds_remote"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<xarray.Dataset>\n",
"Dimensions: ()\n",
"Data variables:\n",
" foo float64 1.0"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# compute stuff\n",
"ds_remote.mean().load()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment