|
51 | 51 | ]
|
52 | 52 | },
|
53 | 53 | {
|
54 |
| - "cell_type": "markdown", |
| 54 | + "cell_type": "code", |
| 55 | + "execution_count": null, |
55 | 56 | "id": "2",
|
56 | 57 | "metadata": {},
|
| 58 | + "outputs": [], |
| 59 | + "source": [ |
| 60 | + "# Ensure we start with a clean directory for the tutorial\n", |
| 61 | + "import pathlib\n", |
| 62 | + "import shutil\n", |
| 63 | + "\n", |
| 64 | + "datadir = pathlib.Path('../data/io-tutorial')\n", |
| 65 | + "if datadir.exists():\n", |
| 66 | + " shutil.rmtree(datadir)\n", |
| 67 | + "else:\n", |
| 68 | + " datadir.mkdir()" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "markdown", |
| 73 | + "id": "3", |
| 74 | + "metadata": {}, |
57 | 75 | "source": [
|
58 | 76 | "The constructor of `Dataset` takes three parameters:\n",
|
59 | 77 | "\n",
|
|
66 | 84 | {
|
67 | 85 | "cell_type": "code",
|
68 | 86 | "execution_count": null,
|
69 |
| - "id": "3", |
| 87 | + "id": "4", |
70 | 88 | "metadata": {},
|
71 | 89 | "outputs": [],
|
72 | 90 | "source": [
|
|
94 | 112 | ")\n",
|
95 | 113 | "\n",
|
96 | 114 | "# write datasets\n",
|
97 |
| - "ds1.to_netcdf(\"ds1.nc\")\n", |
98 |
| - "ds2.to_netcdf(\"ds2.nc\")\n", |
| 115 | + "ds1.to_netcdf(datadir / \"ds1.nc\")\n", |
| 116 | + "ds2.to_netcdf(datadir / \"ds2.nc\")\n", |
99 | 117 | "\n",
|
100 | 118 | "# write dataarray\n",
|
101 |
| - "ds1.a.to_netcdf(\"da1.nc\")" |
| 119 | + "ds1.a.to_netcdf(datadir / \"da1.nc\")" |
102 | 120 | ]
|
103 | 121 | },
|
104 | 122 | {
|
105 | 123 | "cell_type": "markdown",
|
106 |
| - "id": "4", |
| 124 | + "id": "5", |
107 | 125 | "metadata": {},
|
108 | 126 | "source": [
|
109 | 127 | "Reading those files is just as simple:\n"
|
|
112 | 130 | {
|
113 | 131 | "cell_type": "code",
|
114 | 132 | "execution_count": null,
|
115 |
| - "id": "5", |
| 133 | + "id": "6", |
116 | 134 | "metadata": {},
|
117 | 135 | "outputs": [],
|
118 | 136 | "source": [
|
119 |
| - "xr.open_dataset(\"ds1.nc\")" |
| 137 | + "xr.open_dataset(datadir / \"ds1.nc\")" |
120 | 138 | ]
|
121 | 139 | },
|
122 | 140 | {
|
123 | 141 | "cell_type": "code",
|
124 | 142 | "execution_count": null,
|
125 |
| - "id": "6", |
| 143 | + "id": "7", |
126 | 144 | "metadata": {},
|
127 | 145 | "outputs": [],
|
128 | 146 | "source": [
|
129 |
| - "xr.open_dataarray(\"da1.nc\")" |
| 147 | + "xr.open_dataarray(datadir / \"da1.nc\")" |
130 | 148 | ]
|
131 | 149 | },
|
132 | 150 | {
|
133 | 151 | "cell_type": "markdown",
|
134 |
| - "id": "7", |
| 152 | + "id": "8", |
135 | 153 | "metadata": {},
|
136 | 154 | "source": [
|
137 | 155 | "<img src=\"https://zarr.readthedocs.io/en/stable/_static/logo1.png\" align=\"right\" width=\"20%\">\n",
|
|
151 | 169 | {
|
152 | 170 | "cell_type": "code",
|
153 | 171 | "execution_count": null,
|
154 |
| - "id": "8", |
| 172 | + "id": "9", |
155 | 173 | "metadata": {},
|
156 | 174 | "outputs": [],
|
157 | 175 | "source": [
|
158 |
| - "ds1.to_zarr(\"ds1.zarr\", mode=\"w\")" |
| 176 | + "ds1.to_zarr(datadir / \"ds1.zarr\", mode=\"w\")" |
159 | 177 | ]
|
160 | 178 | },
|
161 | 179 | {
|
162 | 180 | "cell_type": "markdown",
|
163 |
| - "id": "9", |
| 181 | + "id": "10", |
164 | 182 | "metadata": {},
|
165 | 183 | "source": [
|
166 | 184 | "We can then read the created file with:\n"
|
|
169 | 187 | {
|
170 | 188 | "cell_type": "code",
|
171 | 189 | "execution_count": null,
|
172 |
| - "id": "10", |
| 190 | + "id": "11", |
173 | 191 | "metadata": {},
|
174 | 192 | "outputs": [],
|
175 | 193 | "source": [
|
176 |
| - "xr.open_zarr(\"ds1.zarr\", chunks=None)" |
| 194 | + "xr.open_zarr(datadir / \"ds1.zarr\", chunks=None)" |
177 | 195 | ]
|
178 | 196 | },
|
179 | 197 | {
|
180 | 198 | "cell_type": "markdown",
|
181 |
| - "id": "11", |
| 199 | + "id": "12", |
182 | 200 | "metadata": {},
|
183 | 201 | "source": [
|
184 | 202 | "setting the `chunks` parameter to `None` avoids `dask` (more on that in a later\n",
|
|
187 | 205 | },
|
188 | 206 | {
|
189 | 207 | "cell_type": "markdown",
|
190 |
| - "id": "12", |
| 208 | + "id": "13", |
191 | 209 | "metadata": {},
|
192 | 210 | "source": [
|
193 | 211 | "**tip:** You can write to any dictionary-like (`MutableMapping`) interface:"
|
|
196 | 214 | {
|
197 | 215 | "cell_type": "code",
|
198 | 216 | "execution_count": null,
|
199 |
| - "id": "13", |
| 217 | + "id": "14", |
200 | 218 | "metadata": {},
|
201 | 219 | "outputs": [],
|
202 | 220 | "source": [
|
|
207 | 225 | },
|
208 | 226 | {
|
209 | 227 | "cell_type": "markdown",
|
210 |
| - "id": "14", |
| 228 | + "id": "15", |
211 | 229 | "metadata": {},
|
212 | 230 | "source": [
|
213 | 231 | "## Raster files using rioxarray\n",
|
|
220 | 238 | {
|
221 | 239 | "cell_type": "code",
|
222 | 240 | "execution_count": null,
|
223 |
| - "id": "15", |
| 241 | + "id": "16", |
224 | 242 | "metadata": {},
|
225 | 243 | "outputs": [],
|
226 | 244 | "source": [
|
|
241 | 259 | {
|
242 | 260 | "cell_type": "code",
|
243 | 261 | "execution_count": null,
|
244 |
| - "id": "16", |
| 262 | + "id": "17", |
245 | 263 | "metadata": {},
|
246 | 264 | "outputs": [],
|
247 | 265 | "source": [
|
248 |
| - "da.rio.to_raster('ds1_a.tiff')" |
| 266 | + "da.rio.to_raster(datadir / 'ds1_a.tiff')" |
249 | 267 | ]
|
250 | 268 | },
|
251 | 269 | {
|
252 | 270 | "cell_type": "markdown",
|
253 |
| - "id": "17", |
| 271 | + "id": "18", |
254 | 272 | "metadata": {},
|
255 | 273 | "source": [
|
256 | 274 | "NOTE: you can now load this file into GIS tools like [QGIS](https://www.qgis.org)! Or open back into Xarray:"
|
|
259 | 277 | {
|
260 | 278 | "cell_type": "code",
|
261 | 279 | "execution_count": null,
|
262 |
| - "id": "18", |
| 280 | + "id": "19", |
263 | 281 | "metadata": {},
|
264 | 282 | "outputs": [],
|
265 | 283 | "source": [
|
266 |
| - "DA = xr.open_dataarray('ds1_a.tiff', engine='rasterio')\n", |
| 284 | + "DA = xr.open_dataarray(datadir / 'ds1_a.tiff', engine='rasterio')\n", |
267 | 285 | "DA.rio.crs"
|
268 | 286 | ]
|
269 | 287 | }
|
|
279 | 297 | "name": "python",
|
280 | 298 | "nbconvert_exporter": "python",
|
281 | 299 | "pygments_lexer": "ipython3"
|
282 |
| - }, |
283 |
| - "vscode": { |
284 |
| - "interpreter": { |
285 |
| - "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" |
286 |
| - } |
287 | 300 | }
|
288 | 301 | },
|
289 | 302 | "nbformat": 4,
|
|
0 commit comments