{ "cells": [ { "cell_type": "markdown", "id": "025d50e4", "metadata": {}, "source": [ "# 2. NetCDF and GRIB Datasets I/O\n", "\n", "## Read a NetCDF File\n", "\n", "The command to open a single netCDF file is simply **`xarray.open_dataset(filename)`**。\n", "\n", "**Example 1:** Read an OLR file into a xarray.Dataset. " ] }, { "cell_type": "code", "execution_count": 1, "id": "87e44596", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset> Size: 1GB\n",
       "Dimensions:   (time: 8760, lon: 360, bnds: 2, lat: 90)\n",
       "Coordinates:\n",
       "  * time      (time) datetime64[ns] 70kB 1998-01-01 1998-01-02 ... 2021-12-31\n",
       "  * lon       (lon) float32 1kB 0.5 1.5 2.5 3.5 4.5 ... 356.5 357.5 358.5 359.5\n",
       "  * lat       (lat) float32 360B -44.5 -43.5 -42.5 -41.5 ... 41.5 42.5 43.5 44.5\n",
       "Dimensions without coordinates: bnds\n",
       "Data variables:\n",
       "    lon_bnds  (lon, bnds) float32 3kB ...\n",
       "    lat_bnds  (lat, bnds) float32 720B ...\n",
       "    olr       (time, lat, lon) float32 1GB ...\n",
       "Attributes: (12/49)\n",
       "    CDI:                        Climate Data Interface version 1.9.10 (https:...\n",
       "    Conventions:                CF-1.6\n",
       "    source:                     NOAA Archive of HIRS L1B data from TIROS-N Se...\n",
       "    institution:                UMD/ESSIC > Earth System Science Interdiscipl...\n",
       "    history:                    Fri Jan 14 11:02:54 2022: cdo sellonlatbox,0,...\n",
       "    conventions:                CF-1.6\n",
       "    ...                         ...\n",
       "    Metadata_Link:              gov.noaa.ncdc:C00875\n",
       "    product_version:            Ver01Rev02\n",
       "    platform:                   TIROS-N > Television Infrared Observation Sat...\n",
       "    sensor:                     HIRS-2 > High Resolution Infra-red Sounder/2,...\n",
       "    spatial_resolution:         1.0 by 1.0 degree equal angle\n",
       "    CDO:                        Climate Data Operators version 1.9.10 (https:...
" ], "text/plain": [ " Size: 1GB\n", "Dimensions: (time: 8760, lon: 360, bnds: 2, lat: 90)\n", "Coordinates:\n", " * time (time) datetime64[ns] 70kB 1998-01-01 1998-01-02 ... 2021-12-31\n", " * lon (lon) float32 1kB 0.5 1.5 2.5 3.5 4.5 ... 356.5 357.5 358.5 359.5\n", " * lat (lat) float32 360B -44.5 -43.5 -42.5 -41.5 ... 41.5 42.5 43.5 44.5\n", "Dimensions without coordinates: bnds\n", "Data variables:\n", " lon_bnds (lon, bnds) float32 3kB ...\n", " lat_bnds (lat, bnds) float32 720B ...\n", " olr (time, lat, lon) float32 1GB ...\n", "Attributes: (12/49)\n", " CDI: Climate Data Interface version 1.9.10 (https:...\n", " Conventions: CF-1.6\n", " source: NOAA Archive of HIRS L1B data from TIROS-N Se...\n", " institution: UMD/ESSIC > Earth System Science Interdiscipl...\n", " history: Fri Jan 14 11:02:54 2022: cdo sellonlatbox,0,...\n", " conventions: CF-1.6\n", " ... ...\n", " Metadata_Link: gov.noaa.ncdc:C00875\n", " product_version: Ver01Rev02\n", " platform: TIROS-N > Television Infrared Observation Sat...\n", " sensor: HIRS-2 > High Resolution Infra-red Sounder/2,...\n", " spatial_resolution: 1.0 by 1.0 degree equal angle\n", " CDO: Climate Data Operators version 1.9.10 (https:..." ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import xarray as xr\n", "\n", "olr_ds = xr.open_dataset(\"data/olr.nc\")\n", "olr_ds" ] }, { "cell_type": "markdown", "id": "b0854d00", "metadata": {}, "source": [ "Now we obtain a xarray.Dataset, where the `olr` **variable** and the **coordinates** are saved in this dataset. The name of the corrdinates is called **dimension**. Note that the time coordinate is converted to a **datetime64** object." ] }, { "cell_type": "markdown", "id": "4e8caa02", "metadata": {}, "source": [ "## Reading Multiple NetCDF Files\n", "\n", "Most operational centers provide data in multiple files. For example, NCEP R2 data is provided as one file per year. Therefore, we use the `xarray.open_mfdataset(paths)` command to open all the files at once. We can also set the `combine='by_coords'` option so that xarray automatically identifies the coordinates to concatenate all the files.\n", "\n", "**Example 2:** In this example, we open NCEP R2 wind files, where the data is one file per year.\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "d8bbe0a7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset> Size: 369MB\n",
       "Dimensions:    (time: 8766, bnds: 2, level: 1, lat: 73, lon: 144)\n",
       "Coordinates:\n",
       "  * time       (time) datetime64[ns] 70kB 1998-01-01 1998-01-02 ... 2021-12-31\n",
       "  * lon        (lon) float32 576B 0.0 2.5 5.0 7.5 ... 350.0 352.5 355.0 357.5\n",
       "  * lat        (lat) float32 292B 90.0 87.5 85.0 82.5 ... -85.0 -87.5 -90.0\n",
       "  * level      (level) float32 4B 850.0\n",
       "Dimensions without coordinates: bnds\n",
       "Data variables:\n",
       "    time_bnds  (time, bnds) datetime64[ns] 140kB dask.array<chunksize=(365, 2), meta=np.ndarray>\n",
       "    uwnd       (time, level, lat, lon) float32 369MB dask.array<chunksize=(365, 1, 73, 144), meta=np.ndarray>\n",
       "Attributes:\n",
       "    CDI:            Climate Data Interface version 1.9.10 (https://mpimet.mpg...\n",
       "    Conventions:    CF-1.0\n",
       "    source:         NCEP/DOE AMIP-II Reanalysis (Reanalysis-2) Model\n",
       "    institution:    National Centers for Environmental Prediction\n",
       "    title:          Daily NCEP/DOE Reanalysis 2\n",
       "    history:        Tue Jan 04 11:04:24 2022: cdo select,level=850 uwnd.1998....\n",
       "    comments:       Data is from \\nNCEP/DOE AMIP-II Reanalysis (Reanalysis-2)...\n",
       "    platform:       Model\n",
       "    dataset_title:  NCEP-DOE AMIP-II Reanalysis\n",
       "    References:     https://www.esrl.noaa.gov/psd/data/gridded/data.ncep.rean...\n",
       "    source_url:     http://www.cpc.ncep.noaa.gov/products/wesley/reanalysis2/\n",
       "    CDO:            Climate Data Operators version 1.9.10 (https://mpimet.mpg...
" ], "text/plain": [ " Size: 369MB\n", "Dimensions: (time: 8766, bnds: 2, level: 1, lat: 73, lon: 144)\n", "Coordinates:\n", " * time (time) datetime64[ns] 70kB 1998-01-01 1998-01-02 ... 2021-12-31\n", " * lon (lon) float32 576B 0.0 2.5 5.0 7.5 ... 350.0 352.5 355.0 357.5\n", " * lat (lat) float32 292B 90.0 87.5 85.0 82.5 ... -85.0 -87.5 -90.0\n", " * level (level) float32 4B 850.0\n", "Dimensions without coordinates: bnds\n", "Data variables:\n", " time_bnds (time, bnds) datetime64[ns] 140kB dask.array\n", " uwnd (time, level, lat, lon) float32 369MB dask.array\n", "Attributes:\n", " CDI: Climate Data Interface version 1.9.10 (https://mpimet.mpg...\n", " Conventions: CF-1.0\n", " source: NCEP/DOE AMIP-II Reanalysis (Reanalysis-2) Model\n", " institution: National Centers for Environmental Prediction\n", " title: Daily NCEP/DOE Reanalysis 2\n", " history: Tue Jan 04 11:04:24 2022: cdo select,level=850 uwnd.1998....\n", " comments: Data is from \\nNCEP/DOE AMIP-II Reanalysis (Reanalysis-2)...\n", " platform: Model\n", " dataset_title: NCEP-DOE AMIP-II Reanalysis\n", " References: https://www.esrl.noaa.gov/psd/data/gridded/data.ncep.rean...\n", " source_url: http://www.cpc.ncep.noaa.gov/products/wesley/reanalysis2/\n", " CDO: Climate Data Operators version 1.9.10 (https://mpimet.mpg..." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "uds = (xr.open_mfdataset( 'data/ncep_r2_uv850/u850.*.nc', # 檔案名稱\n", " combine = \"by_coords\", \n", " parallel=True # 運用dask平行運算,提高運算效率\n", " )) \n", "uds" ] }, { "cell_type": "markdown", "id": "fbc8255f", "metadata": {}, "source": [ "We can also set the option `combine='nested'` and manually set the dimension for concatenation with `concat_dim='time'`." ] }, { "cell_type": "code", "execution_count": 3, "id": "03ce0189", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset> Size: 369MB\n",
       "Dimensions:    (time: 8766, bnds: 2, level: 1, lat: 73, lon: 144)\n",
       "Coordinates:\n",
       "  * time       (time) datetime64[ns] 70kB 1998-01-01 1998-01-02 ... 2021-12-31\n",
       "  * lon        (lon) float32 576B 0.0 2.5 5.0 7.5 ... 350.0 352.5 355.0 357.5\n",
       "  * lat        (lat) float32 292B 90.0 87.5 85.0 82.5 ... -85.0 -87.5 -90.0\n",
       "  * level      (level) float32 4B 850.0\n",
       "Dimensions without coordinates: bnds\n",
       "Data variables:\n",
       "    time_bnds  (time, bnds) datetime64[ns] 140kB dask.array<chunksize=(365, 2), meta=np.ndarray>\n",
       "    uwnd       (time, level, lat, lon) float32 369MB dask.array<chunksize=(365, 1, 73, 144), meta=np.ndarray>\n",
       "Attributes:\n",
       "    CDI:            Climate Data Interface version 1.9.10 (https://mpimet.mpg...\n",
       "    Conventions:    CF-1.0\n",
       "    source:         NCEP/DOE AMIP-II Reanalysis (Reanalysis-2) Model\n",
       "    institution:    National Centers for Environmental Prediction\n",
       "    title:          Daily NCEP/DOE Reanalysis 2\n",
       "    history:        Tue Jan 04 11:04:24 2022: cdo select,level=850 uwnd.1998....\n",
       "    comments:       Data is from \\nNCEP/DOE AMIP-II Reanalysis (Reanalysis-2)...\n",
       "    platform:       Model\n",
       "    dataset_title:  NCEP-DOE AMIP-II Reanalysis\n",
       "    References:     https://www.esrl.noaa.gov/psd/data/gridded/data.ncep.rean...\n",
       "    source_url:     http://www.cpc.ncep.noaa.gov/products/wesley/reanalysis2/\n",
       "    CDO:            Climate Data Operators version 1.9.10 (https://mpimet.mpg...
" ], "text/plain": [ " Size: 369MB\n", "Dimensions: (time: 8766, bnds: 2, level: 1, lat: 73, lon: 144)\n", "Coordinates:\n", " * time (time) datetime64[ns] 70kB 1998-01-01 1998-01-02 ... 2021-12-31\n", " * lon (lon) float32 576B 0.0 2.5 5.0 7.5 ... 350.0 352.5 355.0 357.5\n", " * lat (lat) float32 292B 90.0 87.5 85.0 82.5 ... -85.0 -87.5 -90.0\n", " * level (level) float32 4B 850.0\n", "Dimensions without coordinates: bnds\n", "Data variables:\n", " time_bnds (time, bnds) datetime64[ns] 140kB dask.array\n", " uwnd (time, level, lat, lon) float32 369MB dask.array\n", "Attributes:\n", " CDI: Climate Data Interface version 1.9.10 (https://mpimet.mpg...\n", " Conventions: CF-1.0\n", " source: NCEP/DOE AMIP-II Reanalysis (Reanalysis-2) Model\n", " institution: National Centers for Environmental Prediction\n", " title: Daily NCEP/DOE Reanalysis 2\n", " history: Tue Jan 04 11:04:24 2022: cdo select,level=850 uwnd.1998....\n", " comments: Data is from \\nNCEP/DOE AMIP-II Reanalysis (Reanalysis-2)...\n", " platform: Model\n", " dataset_title: NCEP-DOE AMIP-II Reanalysis\n", " References: https://www.esrl.noaa.gov/psd/data/gridded/data.ncep.rean...\n", " source_url: http://www.cpc.ncep.noaa.gov/products/wesley/reanalysis2/\n", " CDO: Climate Data Operators version 1.9.10 (https://mpimet.mpg..." ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "uds = xr.open_mfdataset( 'data/ncep_r2_uv850/u850.*.nc', \n", " combine = \"nested\", \n", " concat_dim='time', \n", " parallel=True \n", " ) \n", "uds" ] }, { "cell_type": "markdown", "id": "4d90d7da", "metadata": {}, "source": [ "We get the exact same result. Then what is the difference between the two settings? From the definition of the `combine` options on [xarray website - Combining data](https://xarray.pydata.org/en/stable/user-guide/combining.html#combining-along-multiple-dimensions),\n", "\n", "- `combine_nested()`: requires specifying the order in which the objects should be combined. E.g.: a linearly-increasing ‘time’ dimension coordinate.\n", "\n", "- `combine_by_coords()`: attempts to infer this ordering automatically from the coordinates in the data.\n", "\n", "In the first method with `combine='by_coords'`, xarray will automatically concatenate files, whereas the second method with `combine='nested'` requires the user to manually set the dimension to concatenate with the option of `concat_dim`. Unless you are very confident about the file formats and contents, I would recommend using `combine_nested` rather than `combine_by_coords`." ] }, { "attachments": {}, "cell_type": "markdown", "id": "6a0f3b34", "metadata": {}, "source": [ "### Specify a file list with `glob`\n", "\n", "We can use Linux file list syntax to obtain the file list in the `glob.glob` function, then specify the list to `xarray.open_mfdataset()`. For example," ] }, { "cell_type": "code", "execution_count": 4, "id": "b3b14813", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset> Size: 184MB\n",
       "Dimensions:    (time: 4383, bnds: 2, level: 1, lat: 73, lon: 144)\n",
       "Coordinates:\n",
       "  * time       (time) datetime64[ns] 35kB 1998-01-01 1998-01-02 ... 2009-12-31\n",
       "  * lon        (lon) float32 576B 0.0 2.5 5.0 7.5 ... 350.0 352.5 355.0 357.5\n",
       "  * lat        (lat) float32 292B 90.0 87.5 85.0 82.5 ... -85.0 -87.5 -90.0\n",
       "  * level      (level) float32 4B 850.0\n",
       "Dimensions without coordinates: bnds\n",
       "Data variables:\n",
       "    time_bnds  (time, bnds) datetime64[ns] 70kB dask.array<chunksize=(365, 2), meta=np.ndarray>\n",
       "    uwnd       (time, level, lat, lon) float32 184MB dask.array<chunksize=(365, 1, 73, 144), meta=np.ndarray>\n",
       "Attributes:\n",
       "    CDI:            Climate Data Interface version 1.9.10 (https://mpimet.mpg...\n",
       "    Conventions:    CF-1.0\n",
       "    source:         NCEP/DOE AMIP-II Reanalysis (Reanalysis-2) Model\n",
       "    institution:    National Centers for Environmental Prediction\n",
       "    title:          Daily NCEP/DOE Reanalysis 2\n",
       "    history:        Tue Jan 04 11:04:24 2022: cdo select,level=850 uwnd.1998....\n",
       "    comments:       Data is from \\nNCEP/DOE AMIP-II Reanalysis (Reanalysis-2)...\n",
       "    platform:       Model\n",
       "    dataset_title:  NCEP-DOE AMIP-II Reanalysis\n",
       "    References:     https://www.esrl.noaa.gov/psd/data/gridded/data.ncep.rean...\n",
       "    source_url:     http://www.cpc.ncep.noaa.gov/products/wesley/reanalysis2/\n",
       "    CDO:            Climate Data Operators version 1.9.10 (https://mpimet.mpg...
" ], "text/plain": [ " Size: 184MB\n", "Dimensions: (time: 4383, bnds: 2, level: 1, lat: 73, lon: 144)\n", "Coordinates:\n", " * time (time) datetime64[ns] 35kB 1998-01-01 1998-01-02 ... 2009-12-31\n", " * lon (lon) float32 576B 0.0 2.5 5.0 7.5 ... 350.0 352.5 355.0 357.5\n", " * lat (lat) float32 292B 90.0 87.5 85.0 82.5 ... -85.0 -87.5 -90.0\n", " * level (level) float32 4B 850.0\n", "Dimensions without coordinates: bnds\n", "Data variables:\n", " time_bnds (time, bnds) datetime64[ns] 70kB dask.array\n", " uwnd (time, level, lat, lon) float32 184MB dask.array\n", "Attributes:\n", " CDI: Climate Data Interface version 1.9.10 (https://mpimet.mpg...\n", " Conventions: CF-1.0\n", " source: NCEP/DOE AMIP-II Reanalysis (Reanalysis-2) Model\n", " institution: National Centers for Environmental Prediction\n", " title: Daily NCEP/DOE Reanalysis 2\n", " history: Tue Jan 04 11:04:24 2022: cdo select,level=850 uwnd.1998....\n", " comments: Data is from \\nNCEP/DOE AMIP-II Reanalysis (Reanalysis-2)...\n", " platform: Model\n", " dataset_title: NCEP-DOE AMIP-II Reanalysis\n", " References: https://www.esrl.noaa.gov/psd/data/gridded/data.ncep.rean...\n", " source_url: http://www.cpc.ncep.noaa.gov/products/wesley/reanalysis2/\n", " CDO: Climate Data Operators version 1.9.10 (https://mpimet.mpg..." ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import glob\n", "\n", "fls = (glob.glob('data/ncep_r2_uv850/u850.199?.nc') , \n", " glob.glob('data/ncep_r2_uv850/u850.200?.nc')) \n", "fls = sum(fls, [])\n", "\n", "uds = (xr.open_mfdataset( fls, # 檔案名稱\n", " combine = \"by_coords\", \n", " parallel=True # 運用dask平行運算,提高運算效率\n", " )) \n", "uds" ] }, { "attachments": {}, "cell_type": "markdown", "id": "6980dc7d", "metadata": {}, "source": [ "If concerning about the list order does not meet the time order, we can add the following line to re-order the time coordinate. " ] }, { "cell_type": "code", "execution_count": 5, "id": "c5a01087", "metadata": {}, "outputs": [], "source": [ "uds = uds.sortby('time')" ] }, { "cell_type": "markdown", "id": "1814c1a9", "metadata": {}, "source": [ "### `parallel` Option\n", "\n", "In the above example, we added a `parallel=True` option. This option utilizes the `dask` package to parallelize reading the files, which speeds up the reading process.\n", "\n", "> parallel - If True, the open and preprocess steps of this function will be performed in parallel using dask.delayed. Default is False.\n", "\n", "When using this option, the data is saved to a `dask.array` instead of an `xarray.dataset`. This is because the data is temporarily stored in virtual memory. Use `.load()` to convert the dask array to an xarray.dataset. Details on using dask will be introduced in Unit 12.\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "156582d0", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset> Size: 184MB\n",
       "Dimensions:    (time: 4383, bnds: 2, level: 1, lat: 73, lon: 144)\n",
       "Coordinates:\n",
       "  * time       (time) datetime64[ns] 35kB 1998-01-01 1998-01-02 ... 2009-12-31\n",
       "  * lon        (lon) float32 576B 0.0 2.5 5.0 7.5 ... 350.0 352.5 355.0 357.5\n",
       "  * lat        (lat) float32 292B 90.0 87.5 85.0 82.5 ... -85.0 -87.5 -90.0\n",
       "  * level      (level) float32 4B 850.0\n",
       "Dimensions without coordinates: bnds\n",
       "Data variables:\n",
       "    time_bnds  (time, bnds) datetime64[ns] 70kB 1998-01-01 ... 2010-01-01\n",
       "    uwnd       (time, level, lat, lon) float32 184MB -7.99 -7.96 ... -3.74 -3.71\n",
       "Attributes:\n",
       "    CDI:            Climate Data Interface version 1.9.10 (https://mpimet.mpg...\n",
       "    Conventions:    CF-1.0\n",
       "    source:         NCEP/DOE AMIP-II Reanalysis (Reanalysis-2) Model\n",
       "    institution:    National Centers for Environmental Prediction\n",
       "    title:          Daily NCEP/DOE Reanalysis 2\n",
       "    history:        Tue Jan 04 11:04:24 2022: cdo select,level=850 uwnd.1998....\n",
       "    comments:       Data is from \\nNCEP/DOE AMIP-II Reanalysis (Reanalysis-2)...\n",
       "    platform:       Model\n",
       "    dataset_title:  NCEP-DOE AMIP-II Reanalysis\n",
       "    References:     https://www.esrl.noaa.gov/psd/data/gridded/data.ncep.rean...\n",
       "    source_url:     http://www.cpc.ncep.noaa.gov/products/wesley/reanalysis2/\n",
       "    CDO:            Climate Data Operators version 1.9.10 (https://mpimet.mpg...
" ], "text/plain": [ " Size: 184MB\n", "Dimensions: (time: 4383, bnds: 2, level: 1, lat: 73, lon: 144)\n", "Coordinates:\n", " * time (time) datetime64[ns] 35kB 1998-01-01 1998-01-02 ... 2009-12-31\n", " * lon (lon) float32 576B 0.0 2.5 5.0 7.5 ... 350.0 352.5 355.0 357.5\n", " * lat (lat) float32 292B 90.0 87.5 85.0 82.5 ... -85.0 -87.5 -90.0\n", " * level (level) float32 4B 850.0\n", "Dimensions without coordinates: bnds\n", "Data variables:\n", " time_bnds (time, bnds) datetime64[ns] 70kB 1998-01-01 ... 2010-01-01\n", " uwnd (time, level, lat, lon) float32 184MB -7.99 -7.96 ... -3.74 -3.71\n", "Attributes:\n", " CDI: Climate Data Interface version 1.9.10 (https://mpimet.mpg...\n", " Conventions: CF-1.0\n", " source: NCEP/DOE AMIP-II Reanalysis (Reanalysis-2) Model\n", " institution: National Centers for Environmental Prediction\n", " title: Daily NCEP/DOE Reanalysis 2\n", " history: Tue Jan 04 11:04:24 2022: cdo select,level=850 uwnd.1998....\n", " comments: Data is from \\nNCEP/DOE AMIP-II Reanalysis (Reanalysis-2)...\n", " platform: Model\n", " dataset_title: NCEP-DOE AMIP-II Reanalysis\n", " References: https://www.esrl.noaa.gov/psd/data/gridded/data.ncep.rean...\n", " source_url: http://www.cpc.ncep.noaa.gov/products/wesley/reanalysis2/\n", " CDO: Climate Data Operators version 1.9.10 (https://mpimet.mpg..." ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "uds.load()" ] }, { "cell_type": "markdown", "id": "eda3c752", "metadata": {}, "source": [ "## Create and Write to NetCDF File\n", "\n", "After analysis and computation, we can also save the data into a netCDF file. For example, if we'd like to save the concatenated `uds` into a single netCDF file, we do\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "208ccb61", "metadata": {}, "outputs": [], "source": [ "uds.to_netcdf('ex_out/ncep_r2_u850.nc',unlimited_dims='time')" ] }, { "cell_type": "markdown", "id": "9bab63fb", "metadata": {}, "source": [ "It's always a good practice to set `unlimited_dims='time'` for the time coordinate because it will be especially useful in combination with the Climate Data Operator (CDO). We will introduce CDO in Unit 11." ] }, { "cell_type": "markdown", "id": "7032275a", "metadata": {}, "source": [ "## Read GRIB files\n", "\n", "A GRIB (GRIdded Binary) file saves data in binary format along with grid information such as time, longitude, latitude, and pressure levels. This format is widely used by ECMWF. Xarray can also open and read GRIB files (the `cfgrib` package is required). The syntax is as follows: \n", "\n", "```\n", "ds_grib = xr.open_dataset(\"example.grib\", engine=\"cfgrib\")\n", "```\n", "\n", "After reading, cfgrib will automatically create index files in the format `example.grb.923a8.idx`. These index files can speed up subsequent reading processes. However, if write permissions are denied, the .idx files cannot be created.\n", "\n", "\n" ] } ], "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.10.14" }, "vscode": { "interpreter": { "hash": "8e905df1d4d920326545d879dea538d50859be77412bc9bf54949dad3bde9dd6" } } }, "nbformat": 4, "nbformat_minor": 5 }