Created
October 7, 2019 23:26
-
-
Save jhamman/f79ad43c3a742f27e2e8861427b5ea96 to your computer and use it in GitHub Desktop.
Reading/writing xarray with mongodb
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import xarray\n", | |
"import pymongo\n", | |
"\n", | |
"import xarray_mongodb\n", | |
"\n", | |
"import zarr" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"db = pymongo.MongoClient('localhost')['mydb']" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<xarray.DataArray (x: 2)>\n", | |
"array([1, 2])\n", | |
"Coordinates:\n", | |
" * x (x) <U2 'x1' 'x2'\n", | |
"Attributes:\n", | |
" units: foo" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a = xarray.DataArray([1, 2], dims=['x'], coords={'x': ['x1', 'x2']}, attrs={'units': 'foo'})\n", | |
"a" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"xdb = xarray_mongodb.XarrayMongoDB(db)\n", | |
"_id, _ = xdb.put(a)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<xarray.DataArray (x: 2)>\n", | |
"array([1, 2])\n", | |
"Coordinates:\n", | |
" * x (x) <U2 'x1' 'x2'" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"xdb.get(_id)\n", | |
"# loses attrinbutes?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<xarray.backends.zarr.ZarrStore at 0x119a70410>" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"store = zarr.MongoDBStore('localhost')\n", | |
"a.to_dataset(name='a').to_zarr(store, mode='w')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<xarray.DataArray 'a' (x: 2)>\n", | |
"dask.array<zarr, shape=(2,), dtype=int64, chunksize=(2,), chunktype=numpy.ndarray>\n", | |
"Coordinates:\n", | |
" * x (x) <U2 'x1' 'x2'\n", | |
"Attributes:\n", | |
" units: foo" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"xarray.open_zarr(store)['a']" | |
] | |
}, | |
{ | |
"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.7.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment