|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "0", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# CRS handling\n", |
| 9 | + "\n", |
| 10 | + "RasterIndex composes with [xproj](https://xproj.readthedocs.io) to provide CRS handling." |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": null, |
| 16 | + "id": "1", |
| 17 | + "metadata": { |
| 18 | + "editable": true, |
| 19 | + "slideshow": { |
| 20 | + "slide_type": "" |
| 21 | + }, |
| 22 | + "tags": [ |
| 23 | + "hide-input", |
| 24 | + "hide-output" |
| 25 | + ] |
| 26 | + }, |
| 27 | + "outputs": [], |
| 28 | + "source": [ |
| 29 | + "%xmode minimal\n", |
| 30 | + "\n", |
| 31 | + "import xarray as xr\n", |
| 32 | + "\n", |
| 33 | + "import rasterix\n", |
| 34 | + "\n", |
| 35 | + "xr.set_options(display_expand_indexes=True)" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "code", |
| 40 | + "execution_count": null, |
| 41 | + "id": "2", |
| 42 | + "metadata": { |
| 43 | + "editable": true, |
| 44 | + "slideshow": { |
| 45 | + "slide_type": "" |
| 46 | + }, |
| 47 | + "tags": [] |
| 48 | + }, |
| 49 | + "outputs": [], |
| 50 | + "source": [ |
| 51 | + "source = \"/vsicurl/https://noaadata.apps.nsidc.org/NOAA/G02135/south/daily/geotiff/2024/01_Jan/S_20240101_concentration_v3.0.tif\"" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "markdown", |
| 56 | + "id": "3", |
| 57 | + "metadata": { |
| 58 | + "editable": true, |
| 59 | + "slideshow": { |
| 60 | + "slide_type": "" |
| 61 | + }, |
| 62 | + "tags": [] |
| 63 | + }, |
| 64 | + "source": [ |
| 65 | + "To enable CRS-handling, start by assigning a CRS using the `.proj.assign_crs` function from xproj." |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "code", |
| 70 | + "execution_count": null, |
| 71 | + "id": "4", |
| 72 | + "metadata": { |
| 73 | + "editable": true, |
| 74 | + "slideshow": { |
| 75 | + "slide_type": "" |
| 76 | + }, |
| 77 | + "tags": [] |
| 78 | + }, |
| 79 | + "outputs": [], |
| 80 | + "source": [ |
| 81 | + "da = xr.open_dataarray(source, engine=\"rasterio\")\n", |
| 82 | + "da = da.proj.assign_crs(spatial_ref=da.spatial_ref.attrs[\"crs_wkt\"])\n", |
| 83 | + "da = da.pipe(rasterix.assign_index)\n", |
| 84 | + "da" |
| 85 | + ] |
| 86 | + }, |
| 87 | + { |
| 88 | + "cell_type": "markdown", |
| 89 | + "id": "5", |
| 90 | + "metadata": {}, |
| 91 | + "source": [ |
| 92 | + "Note how the `x` variable has appropriate attributes!" |
| 93 | + ] |
| 94 | + }, |
| 95 | + { |
| 96 | + "cell_type": "code", |
| 97 | + "execution_count": null, |
| 98 | + "id": "6", |
| 99 | + "metadata": {}, |
| 100 | + "outputs": [], |
| 101 | + "source": [ |
| 102 | + "da.x.attrs" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "markdown", |
| 107 | + "id": "7", |
| 108 | + "metadata": { |
| 109 | + "editable": true, |
| 110 | + "slideshow": { |
| 111 | + "slide_type": "" |
| 112 | + }, |
| 113 | + "tags": [] |
| 114 | + }, |
| 115 | + "source": [ |
| 116 | + "## Alignment\n", |
| 117 | + "\n", |
| 118 | + "For demo purposes we'll create a second copy with a different CRS." |
| 119 | + ] |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_type": "code", |
| 123 | + "execution_count": null, |
| 124 | + "id": "8", |
| 125 | + "metadata": { |
| 126 | + "editable": true, |
| 127 | + "slideshow": { |
| 128 | + "slide_type": "" |
| 129 | + }, |
| 130 | + "tags": [] |
| 131 | + }, |
| 132 | + "outputs": [], |
| 133 | + "source": [ |
| 134 | + "diffcrs = da.copy(deep=True).drop_vars(\"spatial_ref\").drop_indexes([\"x\", \"y\"])\n", |
| 135 | + "diffcrs = diffcrs.proj.assign_crs(spatial_ref=\"epsg:4326\").pipe(rasterix.assign_index)\n", |
| 136 | + "diffcrs" |
| 137 | + ] |
| 138 | + }, |
| 139 | + { |
| 140 | + "cell_type": "markdown", |
| 141 | + "id": "9", |
| 142 | + "metadata": {}, |
| 143 | + "source": [ |
| 144 | + "Again note that the attributes on `x` have changed appropriately. This is enabled by RasterIndex's integration with `xproj`" |
| 145 | + ] |
| 146 | + }, |
| 147 | + { |
| 148 | + "cell_type": "code", |
| 149 | + "execution_count": null, |
| 150 | + "id": "10", |
| 151 | + "metadata": {}, |
| 152 | + "outputs": [], |
| 153 | + "source": [ |
| 154 | + "diffcrs.x.attrs" |
| 155 | + ] |
| 156 | + }, |
| 157 | + { |
| 158 | + "cell_type": "markdown", |
| 159 | + "id": "11", |
| 160 | + "metadata": {}, |
| 161 | + "source": [ |
| 162 | + "Attempting to add the two raises an error (as it should)!" |
| 163 | + ] |
| 164 | + }, |
| 165 | + { |
| 166 | + "cell_type": "code", |
| 167 | + "execution_count": null, |
| 168 | + "id": "12", |
| 169 | + "metadata": { |
| 170 | + "editable": true, |
| 171 | + "slideshow": { |
| 172 | + "slide_type": "" |
| 173 | + }, |
| 174 | + "tags": [ |
| 175 | + "raises-exception" |
| 176 | + ] |
| 177 | + }, |
| 178 | + "outputs": [], |
| 179 | + "source": [ |
| 180 | + "diffcrs + da" |
| 181 | + ] |
| 182 | + } |
| 183 | + ], |
| 184 | + "metadata": { |
| 185 | + "language_info": { |
| 186 | + "codemirror_mode": { |
| 187 | + "name": "ipython", |
| 188 | + "version": 3 |
| 189 | + }, |
| 190 | + "file_extension": ".py", |
| 191 | + "mimetype": "text/x-python", |
| 192 | + "name": "python", |
| 193 | + "nbconvert_exporter": "python", |
| 194 | + "pygments_lexer": "ipython3" |
| 195 | + } |
| 196 | + }, |
| 197 | + "nbformat": 4, |
| 198 | + "nbformat_minor": 5 |
| 199 | +} |
0 commit comments