{ "cells": [ { "cell_type": "markdown", "id": "a7a87436-7cd6-4a71-9c6a-07f959813f43", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Using GeoTIFF data" ] }, { "cell_type": "raw", "id": "00346ea5-aff7-455a-82ce-4a69f75c677a", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "This example uses a small GeoTIFF file containing 3 bands. With :func:`from_source` we read this data into a fieldlist with one field per band." ] }, { "cell_type": "code", "execution_count": 1, "id": "d35c55f2-7320-4967-9a31-3c56e566df65", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "import earthkit.data as ekd\n", "ds = ekd.from_source(\"sample\", \"multi_band.tif\")" ] }, { "cell_type": "code", "execution_count": 2, "id": "2379a4b4-bbd9-48c3-84bb-61d674cf01f4", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
variableband
0band_11
1band_22
2band_33
\n", "
" ], "text/plain": [ " variable band\n", "0 band_1 1\n", "1 band_2 2\n", "2 band_3 3" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.ls()" ] }, { "cell_type": "code", "execution_count": 3, "id": "a0403583-b73b-42de-8ff9-b1523ef3b165", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "(294, 315)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds[0].shape" ] }, { "cell_type": "code", "execution_count": 4, "id": "ba2ef84a-32e8-49a1-8bb0-9957d935ce3d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "array([217., 217., 217., 217., 217.])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds[0].values[:5]" ] }, { "cell_type": "code", "execution_count": 5, "id": "df663a20-62e7-4c03-9f00-61dca9d8ce0b", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "{'lat': array([[7.12620136, 7.12692213, 7.12764289, ..., 7.35110235, 7.35182325,\n", " 7.35254416],\n", " [7.12622095, 7.12694171, 7.12766246, ..., 7.35111959, 7.35184048,\n", " 7.35256138],\n", " [7.12624053, 7.12696128, 7.12768203, ..., 7.35113682, 7.35185771,\n", " 7.3525786 ],\n", " ...,\n", " [7.13187772, 7.13259631, 7.13331489, ..., 7.35609821, 7.35681693,\n", " 7.35753565],\n", " [7.13189715, 7.13261573, 7.1333343 , ..., 7.35611531, 7.35683402,\n", " 7.35755274],\n", " [7.13191657, 7.13263514, 7.13335372, ..., 7.3561324 , 7.35685111,\n", " 7.35756982]]),\n", " 'lon': array([[50.82441594, 50.82442752, 50.82443909, ..., 50.82781176,\n", " 50.82782195, 50.82783213],\n", " [50.8239268 , 50.82393838, 50.82394995, ..., 50.82732256,\n", " 50.82733275, 50.82734293],\n", " [50.82343766, 50.82344924, 50.82346081, ..., 50.82683336,\n", " 50.82684355, 50.82685373],\n", " ...,\n", " [50.68207406, 50.68208557, 50.68209709, ..., 50.68545281,\n", " 50.68546294, 50.68547308],\n", " [50.6815849 , 50.68159642, 50.68160793, ..., 50.6849636 ,\n", " 50.68497373, 50.68498386],\n", " [50.68109575, 50.68110726, 50.68111878, ..., 50.68447438,\n", " 50.68448452, 50.68449465]])}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds[0].to_latlon()" ] }, { "cell_type": "code", "execution_count": 6, "id": "0a7d8a88", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\n", "Name: unknown\n", "Axis Info [cartesian]:\n", "- E[east]: Easting (metre)\n", "- N[north]: Northing (metre)\n", "Area of Use:\n", "- undefined\n", "Coordinate Operation:\n", "- name: unknown\n", "- method: Transverse Mercator\n", "Datum: Unknown based on GRS 1980 ellipsoid\n", "- Ellipsoid: GRS 1980\n", "- Prime Meridian: Greenwich" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.projection()" ] }, { "cell_type": "markdown", "id": "f1b8c839-92f6-472e-80f0-3a992142f384", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "The fieldlist can be converted into Xarray." ] }, { "cell_type": "code", "execution_count": 7, "id": "e15881a3-16da-4337-8479-c1bf6075387c", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset> Size: 1MB\n",
       "Dimensions:      (x: 315, y: 294)\n",
       "Coordinates:\n",
       "  * x            (x) float64 3kB 3.68e+05 3.681e+05 ... 3.839e+05 3.84e+05\n",
       "  * y            (y) float64 2kB 5.632e+06 5.632e+06 ... 5.616e+06 5.616e+06\n",
       "    spatial_ref  int64 8B 0\n",
       "Data variables:\n",
       "    band_1       (y, x) float32 370kB ...\n",
       "    band_2       (y, x) float32 370kB ...\n",
       "    band_3       (y, x) float32 370kB ...\n",
       "Attributes:\n",
       "    TIFFTAG_XRESOLUTION:     96\n",
       "    TIFFTAG_YRESOLUTION:     96\n",
       "    TIFFTAG_RESOLUTIONUNIT:  2 (pixels/inch)\n",
       "    AREA_OR_POINT:           Area
" ], "text/plain": [ " Size: 1MB\n", "Dimensions: (x: 315, y: 294)\n", "Coordinates:\n", " * x (x) float64 3kB 3.68e+05 3.681e+05 ... 3.839e+05 3.84e+05\n", " * y (y) float64 2kB 5.632e+06 5.632e+06 ... 5.616e+06 5.616e+06\n", " spatial_ref int64 8B 0\n", "Data variables:\n", " band_1 (y, x) float32 370kB ...\n", " band_2 (y, x) float32 370kB ...\n", " band_3 (y, x) float32 370kB ...\n", "Attributes:\n", " TIFFTAG_XRESOLUTION: 96\n", " TIFFTAG_YRESOLUTION: 96\n", " TIFFTAG_RESOLUTIONUNIT: 2 (pixels/inch)\n", " AREA_OR_POINT: Area" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.to_xarray()" ] }, { "cell_type": "markdown", "id": "0c28b67f-a6fa-4a47-aaa4-032f9c03e9f9", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "Each band is loaded into its own variable and masking and scaling is applied by default.\n", "Values from the bands are therefore returned as floats." ] }, { "cell_type": "code", "execution_count": 8, "id": "9cfb0203", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Frozen({'band_1': dtype('float32'), 'band_2': dtype('float32'), 'band_3': dtype('float32')})" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.to_xarray().dtypes" ] }, { "cell_type": "markdown", "id": "f103acbe", "metadata": {}, "source": [ "To obtain a dataset without this type conversion applied, specify `mask_and_scale=False` explicitly." ] }, { "cell_type": "code", "execution_count": 9, "id": "8a6cf45c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Frozen({'band_1': dtype('uint8'), 'band_2': dtype('uint8'), 'band_3': dtype('uint8')})" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.to_xarray(mask_and_scale=False).dtypes" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.13.1" } }, "nbformat": 4, "nbformat_minor": 5 }