{ "cells": [ { "cell_type": "markdown", "id": "38237d70-0971-44a3-9cac-fd08071c8af9", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Reading files" ] }, { "cell_type": "code", "execution_count": 1, "id": "5e113e65-869c-47f9-a6a9-c5463f3d1563", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "import earthkit.data as ekd" ] }, { "cell_type": "markdown", "id": "cf233259-76f4-4e35-a9b0-c2b1d60d6c57", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "First, we ensure the example files used for this notebook are avaliable." ] }, { "cell_type": "code", "execution_count": 2, "id": "65d9d427-32fb-4b20-9370-163c5582696e", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "ekd.download_example_file(\n", " [\"test.grib\", \"test4.grib\", \"test6.grib\", \"test.nc\"])" ] }, { "cell_type": "raw", "id": "a1451947-f829-468f-b1b8-8eb85d9861e0", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "Files can be read by :ref:`from_source ` as a \"file\" source. The resulting object depends on the file type and chosen to best represent the the input data." ] }, { "cell_type": "code", "execution_count": 3, "id": "34918e4c-12c6-4d4a-b1ac-e012355210d1", "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
centreshortNametypeOfLevelleveldataDatedataTimestepRangedataTypenumbergridType
0ecmf2tsurface02020051312000an0regular_ll
1ecmfmslsurface02020051312000an0regular_ll
\n", "
" ], "text/plain": [ " centre shortName typeOfLevel level dataDate dataTime stepRange dataType \\\n", "0 ecmf 2t surface 0 20200513 1200 0 an \n", "1 ecmf msl surface 0 20200513 1200 0 an \n", "\n", " number gridType \n", "0 0 regular_ll \n", "1 0 regular_ll " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# grib data\n", "ds = ekd.from_source(\"file\", \"test.grib\")\n", "ds.ls()" ] }, { "cell_type": "code", "execution_count": 4, "id": "3d0be6a1-6bac-4e74-b461-ec5c58b7112d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/cgr/git/earthkit-data/src/earthkit/data/readers/netcdf/fieldlist.py:318: FutureWarning: In a future version of xarray decode_timedelta will default to False rather than None. To silence this warning, set decode_timedelta to True, False, or a 'CFTimedeltaCoder' instance.\n", " return xr.open_dataset(self.path_or_url)\n" ] }, { "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", " \n", "
variablelevelvalid_datetimeunits
0t2mNone2020-05-13T12:00:00K
1mslNone2020-05-13T12:00:00Pa
\n", "
" ], "text/plain": [ " variable level valid_datetime units\n", "0 t2m None 2020-05-13T12:00:00 K\n", "1 msl None 2020-05-13T12:00:00 Pa" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# netcdf data\n", "ds = ekd.from_source(\"file\", \"test.nc\")\n", "ds.ls()" ] }, { "cell_type": "raw", "id": "7c9b8aa2-c3c0-44ec-99a3-4a3c5f5d2a71", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "See the other file related notebooks for more details:\n", "\n", "- :ref:`/examples/multi_files.ipynb`\n", "- :ref:`/examples/tar_files.ipynb`\n", "- :ref:`/examples/file_parts.ipynb`" ] }, { "cell_type": "markdown", "id": "7e869e8c-cd1e-4bc2-ba08-6604b14f184d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Reading files using patterns" ] }, { "cell_type": "raw", "id": "c19279fc-c892-43e8-8022-991c5a1e1a39", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "The :ref:`file ` source can also be specified by using :ref:`patterns `. In the example below when pattern \"id\" is substituted it will match two files: test4.grib and test6.grib:" ] }, { "cell_type": "code", "execution_count": 5, "id": "d52872d1-8ff8-4f52-9ffc-9b72621a9a67", "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", " \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", " \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", " \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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
centreshortNametypeOfLevelleveldataDatedataTimestepRangedataTypenumbergridType
0ecmftisobaricInhPa5002007010112000an0regular_ll
1ecmfzisobaricInhPa5002007010112000an0regular_ll
2ecmftisobaricInhPa8502007010112000an0regular_ll
3ecmfzisobaricInhPa8502007010112000an0regular_ll
4ecmftisobaricInhPa10002018080112000an0regular_ll
5ecmfuisobaricInhPa10002018080112000an0regular_ll
6ecmfvisobaricInhPa10002018080112000an0regular_ll
7ecmftisobaricInhPa8502018080112000an0regular_ll
8ecmfuisobaricInhPa8502018080112000an0regular_ll
9ecmfvisobaricInhPa8502018080112000an0regular_ll
\n", "
" ], "text/plain": [ " centre shortName typeOfLevel level dataDate dataTime stepRange \\\n", "0 ecmf t isobaricInhPa 500 20070101 1200 0 \n", "1 ecmf z isobaricInhPa 500 20070101 1200 0 \n", "2 ecmf t isobaricInhPa 850 20070101 1200 0 \n", "3 ecmf z isobaricInhPa 850 20070101 1200 0 \n", "4 ecmf t isobaricInhPa 1000 20180801 1200 0 \n", "5 ecmf u isobaricInhPa 1000 20180801 1200 0 \n", "6 ecmf v isobaricInhPa 1000 20180801 1200 0 \n", "7 ecmf t isobaricInhPa 850 20180801 1200 0 \n", "8 ecmf u isobaricInhPa 850 20180801 1200 0 \n", "9 ecmf v isobaricInhPa 850 20180801 1200 0 \n", "\n", " dataType number gridType \n", "0 an 0 regular_ll \n", "1 an 0 regular_ll \n", "2 an 0 regular_ll \n", "3 an 0 regular_ll \n", "4 an 0 regular_ll \n", "5 an 0 regular_ll \n", "6 an 0 regular_ll \n", "7 an 0 regular_ll \n", "8 an 0 regular_ll \n", "9 an 0 regular_ll " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds = ekd.from_source(\"file-pattern\", \"./test{id}.grib\",\n", " {\"id\": [4, 6]})\n", "ds.ls()" ] }, { "cell_type": "code", "execution_count": null, "id": "5b18ef07-0b22-4b78-b83b-20014686e32f", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "dev", "language": "python", "name": "dev" }, "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.11.12" } }, "nbformat": 4, "nbformat_minor": 5 }