{ "cells": [ { "cell_type": "markdown", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Reading multiple files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First we prepare the data." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import earthkit.data as ekd\n", "ekd.download_example_file([\"test.grib\", \"test4.grib\"])" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "!test -d _grib_dir_no_sql || (mkdir -p _grib_dir_no_sql; cp -f test.grib test4.grib _grib_dir_no_sql/)" ] }, { "cell_type": "raw", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "Multiple GRIB files can be :ref:`read ` in various ways:" ] }, { "cell_type": "markdown", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Option 1: using wildcards" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "fs = ekd.from_source(\"file\", \"test*.grib\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "12" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(fs)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "GribMultiFieldList(GRIBReader(test.grib),GRIBReader(test4.grib),GRIBReader(test6.grib))" ], "text/plain": [ "GribMultiFieldList(GRIBReader(test.grib),GRIBReader(test4.grib),GRIBReader(test6.grib))" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Option 2: using a list" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "fs = ekd.from_source(\"file\", [\"test.grib\", \"test4.grib\"])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(fs)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "GribMultiFieldList(GRIBReader(test.grib),GRIBReader(test4.grib))" ], "text/plain": [ "GribMultiFieldList(GRIBReader(test.grib),GRIBReader(test4.grib))" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Option 3: reading all files in a directory" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "fs = ekd.from_source(\"file\", \"./_grib_dir_no_sql\")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(fs)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "GribMultiFieldList(GRIBReader(./_grib_dir_no_sql/test.grib),GRIBReader(./_grib_dir_no_sql/test4.grib))" ], "text/plain": [ "GribMultiFieldList(GRIBReader(./_grib_dir_no_sql/test.grib),GRIBReader(./_grib_dir_no_sql/test4.grib))" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Working with the object" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The resulting GRIB object behaves in the same way as if it was created from a single file:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "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", "
centreshortNametypeOfLevelleveldataDatedataTimestepRangedataTypenumbergridType
0ecmf2tsurface02020051312000an0regular_ll
1ecmfmslsurface02020051312000an0regular_ll
2ecmftisobaricInhPa5002007010112000an0regular_ll
3ecmfzisobaricInhPa5002007010112000an0regular_ll
4ecmftisobaricInhPa8502007010112000an0regular_ll
5ecmfzisobaricInhPa8502007010112000an0regular_ll
\n", "
" ], "text/plain": [ " centre shortName typeOfLevel level dataDate dataTime stepRange \\\n", "0 ecmf 2t surface 0 20200513 1200 0 \n", "1 ecmf msl surface 0 20200513 1200 0 \n", "2 ecmf t isobaricInhPa 500 20070101 1200 0 \n", "3 ecmf z isobaricInhPa 500 20070101 1200 0 \n", "4 ecmf t isobaricInhPa 850 20070101 1200 0 \n", "5 ecmf z isobaricInhPa 850 20070101 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 " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fs.ls()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "GribField(t,500,20070101,1200,0,0)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fs[2]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "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", "
centreshortNametypeOfLevelleveldataDatedataTimestepRangedataTypenumbergridType
0ecmfmslsurface02020051312000an0regular_ll
1ecmftisobaricInhPa5002007010112000an0regular_ll
2ecmfzisobaricInhPa5002007010112000an0regular_ll
3ecmftisobaricInhPa8502007010112000an0regular_ll
4ecmfzisobaricInhPa8502007010112000an0regular_ll
\n", "
" ], "text/plain": [ " centre shortName typeOfLevel level dataDate dataTime stepRange \\\n", "0 ecmf msl surface 0 20200513 1200 0 \n", "1 ecmf t isobaricInhPa 500 20070101 1200 0 \n", "2 ecmf z isobaricInhPa 500 20070101 1200 0 \n", "3 ecmf t isobaricInhPa 850 20070101 1200 0 \n", "4 ecmf z isobaricInhPa 850 20070101 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 " ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g = fs[1:6]\n", "g.ls()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "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
0ecmfzisobaricInhPa5002007010112000an0regular_ll
1ecmfzisobaricInhPa8502007010112000an0regular_ll
\n", "
" ], "text/plain": [ " centre shortName typeOfLevel level dataDate dataTime stepRange \\\n", "0 ecmf z isobaricInhPa 500 20070101 1200 0 \n", "1 ecmf z isobaricInhPa 850 20070101 1200 0 \n", "\n", " dataType number gridType \n", "0 an 0 regular_ll \n", "1 an 0 regular_ll " ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g = fs[3:6].sel(param=\"z\").order_by(\"level\")\n", "g.ls()" ] } ], "metadata": { "kernelspec": { "display_name": "dev_ecc", "language": "python", "name": "dev_ecc" }, "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.10.13" } }, "nbformat": 4, "nbformat_minor": 4 }