Jekyll2022-05-20T19:58:28+00:00https://www.earthdatascience.org/feed.xmlEarth Data Science - Earth LabCode, tutorials, and tools for modern Earth analyticsEarth Analyticsearth.lab@colorado.eduPlot Data With Matplotlib2020-12-08T00:00:00+00:002020-12-08T00:00:00+00:00https://www.earthdatascience.org/courses/earth-analytics-bootcamp/matplotlib-landing<aside class="sidebar__right">
<nav class="toc">
<header><h4 class="nav__title"><i class="fa fa-file-text"></i> This Week</h4></header>
<ul class="toc__menu" id="markdown-toc">
<li><a href="#welcome-to-week-4" id="markdown-toc-welcome-to-week-4"><i class="fa fa-ship" aria-hidden="true"></i> Welcome to Week 4!</a></li>
<li><a href="#learning-objectives" id="markdown-toc-learning-objectives"><i class="fa fa-graduation-cap" aria-hidden="true"></i> Learning Objectives</a></li>
<li><a href="#homework" id="markdown-toc-homework"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Homework</a></li>
<li><a href="#earth-data-science-textbook-readings" id="markdown-toc-earth-data-science-textbook-readings"><i class="fa fa-book"></i> Earth Data Science Textbook Readings</a></li>
<li><a href="#example-homework-plots" id="markdown-toc-example-homework-plots">Example Homework Plots</a></li>
</ul>
</nav>
</aside>
<div class="notice--info">
<h2 id="welcome-to-week-4"><i class="fa fa-ship" aria-hidden="true"></i> Welcome to Week 4!</h2>
<p>Welcome to week 4 of Earth Analytics Bootcamp! This week, you will explore
plotting data using <code class="language-plaintext highlighter-rouge">matplotlib</code> in <code class="language-plaintext highlighter-rouge">Python</code>.</p>
<h2 id="learning-objectives"><i class="fa fa-graduation-cap" aria-hidden="true"></i> Learning Objectives</h2>
<p>After completing the lessons for Week 4, you will be able to:</p>
<ul>
<li>Describe the basic structure of a matplotlib figure in Python</li>
<li>Create a figure with 1 or more subplots</li>
<li>Customize matplotlib figures and subplots.</li>
</ul>
<hr />
<h2 id="homework"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Homework</h2>
<h3 id="the-homework-assignment-for-this-week-can-be-found-on-github">The Homework Assignment for This Week Can Be Found on Github</h3>
<p><a href="https://github.com/earthlab-education/bootcamp-2020-04-plots-template" target="_blank" class="btn btn--info btn--x-large"> <i class="fa fa-link" aria-hidden="true"></i> Click here to view the GitHub Repo with the assignment template. </a></p>
<h2 id="earth-data-science-textbook-readings"><i class="fa fa-book"></i> Earth Data Science Textbook Readings</h2>
<p>Please read the following chapters to support completing this week’s assignment:</p>
<ul>
<li><a href="https://www.earthdatascience.org/courses/scientists-guide-to-plotting-data-in-python/plot-with-matplotlib/">The first chapter of the earth data science plotting textbook.</a></li>
</ul>
</div>
<h2 id="example-homework-plots">Example Homework Plots</h2>
<p>The plots below are examples of what your plot could look like. Feel free to
customize or modify plot settings as you see fit!</p>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Downloading from https://ndownloader.figshare.com/files/24649844
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/ea-bootcamp/04-plotting/2020-12-08-matplotlib-landing/2020-12-08-matplotlib-landing_2_1.png" alt="Bar plot of total fires in the USA by year from 2000-2015." />
<figcaption>Bar plot of total fires in the USA by year from 2000-2015.</figcaption>
</figure>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/ea-bootcamp/04-plotting/2020-12-08-matplotlib-landing/2020-12-08-matplotlib-landing_3_0.png" alt="Bar plot of total fires in the USA by year from 2000-2015. The bars have a custom edgecolor." />
<figcaption>Bar plot of total fires in the USA by year from 2000-2015. The bars have a custom edgecolor.</figcaption>
</figure>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/ea-bootcamp/04-plotting/2020-12-08-matplotlib-landing/2020-12-08-matplotlib-landing_4_0.png" alt="Two plots. The top plot is a bar plot of total fires in the USA by year from 2000-2015. The bottom plot is a scatter plot of mean fire size in hectacres in the USA from 2000-2015." />
<figcaption>Two plots. The top plot is a bar plot of total fires in the USA by year from 2000-2015. The bottom plot is a scatter plot of mean fire size in hectacres in the USA from 2000-2015.</figcaption>
</figure>Earth Analyticsearth.lab@colorado.eduThis WeekIntroduction to the NetCDF4 Hierarchical Data Format2020-10-16T00:00:00+00:002020-10-16T00:00:00+00:00https://www.earthdatascience.org/courses/use-data-open-source-python/hierarchical-data-formats-hdf/netcdf-01-intro-to-climate-data-netcdf<aside class="sidebar__right">
<nav class="toc">
<header><h4 class="nav__title"><i class="fa fa-file-text"></i> In This Chapter</h4></header>
<ul class="toc__menu" id="markdown-toc">
<li><a href="#chapter-13---netcdf-4-climate-data-in-open-source-python" id="markdown-toc-chapter-13---netcdf-4-climate-data-in-open-source-python"><i class="fa fa-ship" aria-hidden="true"></i> Chapter 13 - NETCDF 4 Climate Data in Open Source Python</a></li>
<li><a href="#learning-objectives" id="markdown-toc-learning-objectives"><i class="fa fa-graduation-cap" aria-hidden="true"></i> Learning Objectives</a></li>
<li><a href="#what-you-need" id="markdown-toc-what-you-need"><i class="fa fa-check-square-o fa-2" aria-hidden="true"></i> What You Need</a></li>
<li><a href="#what-is-netcdf-data" id="markdown-toc-what-is-netcdf-data">What is netCDF Data?</a></li>
<li><a href="#tools-to-work-with-netcdf-data" id="markdown-toc-tools-to-work-with-netcdf-data">Tools to Work With NetCDF Data</a></li>
</ul>
</nav>
</aside>
<div class="notice--success">
<h2 id="chapter-13---netcdf-4-climate-data-in-open-source-python"><i class="fa fa-ship" aria-hidden="true"></i> Chapter 13 - NETCDF 4 Climate Data in Open Source Python</h2>
<p>In this chapter, you will learn how to work with Climate Data Sets (MACA v2 for the United states) stored in netcdf 4 format using open source <strong>Python</strong>.</p>
<h2 id="learning-objectives"><i class="fa fa-graduation-cap" aria-hidden="true"></i> Learning Objectives</h2>
<p>After completing this chapter, you will be able to:</p>
<ul>
<li>Describe the netcdf data format as it is used to store climate data</li>
<li>Define the characteristics of MACAv2 and CMIP5 data.</li>
<li>Explain what downscaling is.</li>
</ul>
<h2 id="what-you-need"><i class="fa fa-check-square-o fa-2" aria-hidden="true"></i> What You Need</h2>
<p>OPTIONAL: If you want to explore the netcdf 4 files in a graphics based tool, you can download th <a href="https://www.hdfgroup.org/downloads/hdfview/" target="_blank">free HDF viewer</a> from the HDF Group website.</p>
</div>
<h2 id="what-is-netcdf-data">What is netCDF Data?</h2>
<p>NetCDF (network Common Data Form) is a hierarchical data format similar to <a href="https://www.earthdatascience.org/courses/use-data-open-source-python/hierarchical-data-formats-hdf/intro-to-hdf4/">hdf4</a> and hdf5. It is what is known as a “self-describing” data structure which means that metadata, or descriptions of the data, are included in the file itself and can be parsed programmatically, meaning that they can be accessed using code to build automated and reproducible workflows.</p>
<p>The NetCDF format can store data with multiple dimensions. It can also store different types of data through arrays that can contain geospatial imagery, rasters, terrain data, climate data, and text. These arrays support metadata, making the netCDF format highly flexible. NetCDF was developed and is supported by UCAR who maintains standards and software that support the use of the format.</p>
<h3 id="netcdf4-format-for-climate-data">NetCDF4 Format for Climate Data</h3>
<p>The hierarchical and flexible nature of netcdf files supports storing data in many different ways. This flexibility is nice, however, similar to hdf5 data formats, it can be challenging when communities make different decisions about how and where they store data in a netCDF file. The netCDF4 data standard is used broadly by the climate science community to store climate data. Climate data are:</p>
<ul>
<li>often delivered in a time series format (months and years of historic or future projected data).</li>
<li>spatial in nature, covering regions such as the United States or even the world.</li>
<li>driven by models which require documentation making the self describing aspect of netCDF files useful.</li>
</ul>
<p>The netCDF4 format supports data stored in an array format. Arrays are used to store raster spatial data (terrain layers, gridded temperature data, etc) and also point based time series data (for example temperature for a single location over 10 years). Climate data typically have three dimensions—x and y values representing latitude and longitude location for a point or a grid cell location on the earth’s surface and time.</p>
<p>The x/y locations often store a data value such as temperature, humidity, precipitation or wind direction.</p>
<h3 id="netcdf-is-self-describing">NetCDF is Self Describing</h3>
<p>One of the biggest benefits of working with a data type like netCDF is that it is self-describing. This means that all of the metadata needed to work with the data is often contained within the netCDF file (the <code class="language-plaintext highlighter-rouge">.nc</code> file) itself.</p>
<figure>
<img src="https://www.earthdatascience.org/images/earth-analytics/hierarchical-data-formats/hdf5-example-data-structure.jpg" alt="netcdf is a hierarchical data format. It is self describing meaning that the metadata for the file is contained within the file itself. Self-contained metadata makes it easier to create a fully reproducible workflow given you can pull metadata elements such as coordinate reference systems and units directly from the file itself." />
<figcaption>Netcdf is a hierarchical data format. It is self describing meaning that the metadata for the file is contained within the file itself. Self-contained metadata makes it easier to create a fully reproducible workflow given you can pull metadata elements such as coordinate reference systems and units directly from the file itself. </figcaption>
</figure>
<h2 id="tools-to-work-with-netcdf-data">Tools to Work With NetCDF Data</h2>
<p>Python also has several open source tools that are useful for processing netcdf files including:</p>
<ul>
<li><code class="language-plaintext highlighter-rouge">Xarray</code>: one of the most common tools used to process netcdf data. Xarray knows how to open netcdf 4 files automatically giving you access to the data and metadata in spatial formats.</li>
<li><code class="language-plaintext highlighter-rouge">rioxarray</code>: a wrapper that adds spatial functionality such as reproject and export to geotiff to xarray objects.</li>
<li><code class="language-plaintext highlighter-rouge">Regionmask</code>: regionmask builds on top of xarray to support spatial subsetting and AOIs for xarray objects.</li>
</ul>
<p>In the next lessons you will learn more about climate data and how to open climate data stored in <strong>netCDF</strong> format using open source Python tools. Read the section below to learn more about the climate data itself.</p>Leah WasserIn this lesson you will learn about that netcdf 4 data format which is a format, commonly used to store climate data. In later lessons you will learn how to open climate data using open source Python tools.Introduction to the CMIP and MACA v2 Climate Data2020-10-16T00:00:00+00:002020-10-16T00:00:00+00:00https://www.earthdatascience.org/courses/use-data-open-source-python/hierarchical-data-formats-hdf/netcdf-02-intro-to-climate-data-macav2<aside class="sidebar__right">
<nav class="toc">
<header><h4 class="nav__title"><i class="fa fa-file-text"></i> In This Chapter</h4></header>
<ul class="toc__menu" id="markdown-toc">
<li><a href="#introduction-to-climate-data-in-open-source-python" id="markdown-toc-introduction-to-climate-data-in-open-source-python"><i class="fa fa-ship" aria-hidden="true"></i> Introduction to Climate Data in Open Source Python</a></li>
<li><a href="#learning-objectives" id="markdown-toc-learning-objectives"><i class="fa fa-graduation-cap" aria-hidden="true"></i> Learning Objectives</a></li>
<li><a href="#about-climate-data-what-is-cmip5-data" id="markdown-toc-about-climate-data-what-is-cmip5-data">About Climate Data: What is CMIP5 data?</a></li>
<li><a href="#what-is-maca-v2-metdata" id="markdown-toc-what-is-maca-v2-metdata">What is MACA v2 Metdata?</a></li>
<li><a href="#about-downscaling-data---creating-higher-resolution-climate-data-for-the-united-states-using-lower-resolution-global-data" id="markdown-toc-about-downscaling-data---creating-higher-resolution-climate-data-for-the-united-states-using-lower-resolution-global-data">About Downscaling Data - Creating Higher Resolution Climate Data For the United States Using Lower Resolution Global Data</a></li>
<li><a href="#what-formats-are-maca-v2-data-in" id="markdown-toc-what-formats-are-maca-v2-data-in">What Formats Are MACA v2 Data In?</a></li>
<li><a href="#how-do-you-get-maca-v2-data" id="markdown-toc-how-do-you-get-maca-v2-data">How do you get MACA v2 data?</a></li>
<li><a href="#the-climate-toolbox" id="markdown-toc-the-climate-toolbox">The Climate Toolbox</a></li>
<li><a href="#additional-resources" id="markdown-toc-additional-resources">Additional Resources</a></li>
</ul>
</nav>
</aside>
<div class="notice--success">
<h2 id="introduction-to-climate-data-in-open-source-python"><i class="fa fa-ship" aria-hidden="true"></i> Introduction to Climate Data in Open Source Python</h2>
<p>In this lesson, you will learn more about the MACA v2 climate data which is a downsampled data set
created from the global CMIP5 climate data.</p>
<h2 id="learning-objectives"><i class="fa fa-graduation-cap" aria-hidden="true"></i> Learning Objectives</h2>
<p>After completing this chapter, you will be able to:</p>
<ul>
<li>Define the characteristics of MACAv2 and CMIP5 data.</li>
<li>Explain what downscaling is.</li>
</ul>
</div>
<h2 id="about-climate-data-what-is-cmip5-data">About Climate Data: What is CMIP5 data?</h2>
<p>The Climate Model Intercomparison Project, better known as CMIP, is a framework for analyzing and comparing Global Climate Models (GCMs) to better understand climate change occuring now and into the future. GCMs are a type of climate models that simulate the global and regional scale climate processes that include the general circulation of atmosphere and oceans and its interaction with the land, radiative fluxes, cloud processes, and land surface processes that include hydrology and biological feedbacks. CMIP5, phase 5 of the project, is the current and most extensive CMIP, drawing data from over 40 GCMs from countries around the world.</p>
<h2 id="what-is-maca-v2-metdata">What is MACA v2 Metdata?</h2>
<p>The CMIP models are global and thus cover the entire world. Due to this global coverage they are delivered at a coarse resolution ranging from 100-300km. Multivariate Adaptive Constructed Analogs (MACA) is a statistical method for downscaling GCM data from its coarse format to a higher spatial resolution for the Continental United States (CONUS). The MACA downscaling approach takes 20 GCMs from CMIP5 and downscales them to 4km or 6km resolution data. The resolution of MACA v2 Metdata is 4km.</p>
<p>The data variables include temperature, precipitation, humidity, downward shortwave solar radiation, and eastward and northward wind. Data are also provided for historical time periods from 1950-2005 and future scenarios spanning from 2006-2100.</p>
<h2 id="about-downscaling-data---creating-higher-resolution-climate-data-for-the-united-states-using-lower-resolution-global-data">About Downscaling Data - Creating Higher Resolution Climate Data For the United States Using Lower Resolution Global Data</h2>
<p>Downscaling refers to the process of taking climate projections data produced at a large scale, with bigger pixels covering larger areas (100-300 km), and increasing its spatial resolution so that the pixels are smaller and cover smaller areas (1-50 km). Downscaling processes also generally tend to remove biases (often referred to as bias correction) between the simulated GCM climatology and the real world climatology. Downscaling allows us to have future climate projections available at a much finer spatial scale and more representative of a region’s climate and therefore more applicable for regional-scale applications. For example, it is more appropriate for discerning projected hydrological changes at a watershed scale as opposed to taking those projections directly from a GCM output. Overall, downscaling makes the data more useful on a local and regional level, which is important because adaptation strategies are generally identified and implemented on regional and local scales. This allows resource managers to identify vulnerable areas and prioritize adaptation efforts there.</p>
<figure>
<img src="https://www.earthdatascience.org/images/earth-analytics/climate-data/downscale-climate-data-met.jpg" alt="An example of the downscaling process, converting coarse data to a higher resolution. Source: Databasin.org." />
<figcaption>An example of the downscaling process, converting coarse data to a higher resolution. Source: Databasin.org. </figcaption>
</figure>
<p>Downscaling however comes with its challenges. There are many different processing and statistical approaches that can be used to downscale GCM data, each of which produce different levels and types of uncertainty. The MACA downscaling method uses a constructed analog approach that is a more advanced form of statistical downscaling and has been getting greater acceptability and usability in the user community. For example, the recent National Climate Assessment (NCA4) considered Localized Constructed Analogs (LOCA) downscaled data, while a 2020 Resources Planning Act (RPA) Assessment by the US Forest Service considered MACA v2 Metdata. The constructed analog approach also provides climate projections at daily timescales which is usually not available for most other downscaled datasets. Additionally, MACA v2 Metdata also applies a multivariate approach by combining temperature and humidity variables. It has now been extensively vetted by the research community, and has been found suitable for a wide range of applications.</p>
<h2 id="what-formats-are-maca-v2-data-in">What Formats Are MACA v2 Data In?</h2>
<ul>
<li>Tabular (txt and csv) - Learn how to work with txt and csv data <a href="https://www.earthdatascience.org/courses/intro-to-earth-data-science/file-formats/use-text-files/use-tabular-data/">in this lesson</a>.</li>
<li>GeoTIFF - Learn how to work with geotiff data <a href="https://www.earthdatascience.org/courses/use-data-open-source-python/intro-raster-data-python/fundamentals-raster-data/intro-to-the-geotiff-file-format/">in this lesson</a>.</li>
<li>netCDF - Get more detail on the netCDF documentation from UCAR <a href="https://www.unidata.ucar.edu/software/netcdf/docs/netcdf_introduction.html" target="_blank">here</a>.</li>
</ul>
<h2 id="how-do-you-get-maca-v2-data">How do you get MACA v2 data?</h2>
<p>There are several different options for downloading MACA v2 data including:</p>
<ul>
<li><a href="https://climate.northwestknowledge.net/MACA/data_portal.php" target="_blank">Through the MACA v2 Data portal </a></li>
<li><a href="http://thredds.northwestknowledge.net:8080/thredds/reacch_climate_CMIP5_aggregated_macav2_monthly_catalog.html" target="_blank">Through the MACA v2 Catalog </a></li>
<li><a href="https://cida.usgs.gov/gdp/client/#!catalog/gdp/dataset/5752f2d9e4b053f0edd15628" target="_blank">Through the USGS GeoData Portal </a></li>
<li><a href="https://github.com/earthlab/cft" target="_blank">Using the <code class="language-plaintext highlighter-rouge">R</code> Climate Futures Toolbox (CFT)</a>, an R tool created by CU Boulder Earth Lab</li>
<li><a href="https://climatetoolbox.org/tool/Climate-Mapper">The Climate Mapper Toolbox</a>, a tool from the <a href="https://climatetoolbox.org/" target="_blank">Climate Toolbox</a></li>
</ul>
<div class="notice--warning">
<h2 id="the-climate-toolbox">The Climate Toolbox</h2>
<p>The <a href="https://climatetoolbox.org/tool/climate-mapper" target="_blank">Climate Toolbox </a>is a collection of approximately 20 web-based tools that allow the user to visualize past and future projected climate, agriculture, wildfire, and hydrological data for the United States. It is maintained by the University of California, Merced in collaboration with government organizations including the USDA, NOAA, and the USGS.</p>
<h3 id="the-climate-toolbox-climate-mapper-tool">The Climate Toolbox Climate Mapper Tool</h3>
<p>The Climate Mapper Tool allows you to map climate data extracted from MACA v2 Metdata, display current conditions, forecasts, and future projections for the U.S. The data includes variables related to climate, agriculture, wildfire, and hydrology. The tool aims to help scientists and decision-makers visualize climate information in a straightforward and easy-to-use way.</p>
</div>
<div class="notice--info">
<h2 id="additional-resources">Additional Resources</h2>
<ul>
<li>Learn more about <a href="http://www.climatologylab.org/maca.html" target="_blank">MACA v2 data</a> from its creators at the University of California Merced.</li>
<li>See <a href="https://www.earthdatascience.org/cft/articles/cft-intro.html">example applications of MACA v2 data</a> .</li>
<li>Explore the <a href="https://climatetoolbox.org/" target="_blank">Climate Toolbox</a></li>
<li>Use the <a href="https://climatetoolbox.org/tool/Climate-Mapper" target="_blank">Climate Mapper Tool</a></li>
</ul>
</div>Leah WasserIn this lesson you will learn the basics of what CMIP5 and MACA v 2 data are and how global climate data are downscaled to higher resolutions to support regional analysis.How to Download MACA2 Climate Data Using Python2020-10-16T00:00:00+00:002020-10-16T00:00:00+00:00https://www.earthdatascience.org/courses/use-data-open-source-python/hierarchical-data-formats-hdf/netcdf-03-get-climate-data<aside class="sidebar__right">
<nav class="toc">
<header><h4 class="nav__title"><i class="fa fa-file-text"></i> In This Chapter</h4></header>
<ul class="toc__menu" id="markdown-toc">
<li><a href="#open-maca-v2-climate-data-programmatically-using-open-source-python-and-xarray" id="markdown-toc-open-maca-v2-climate-data-programmatically-using-open-source-python-and-xarray"><i class="fa fa-ship" aria-hidden="true"></i> Open MACA v2 Climate data Programmatically using Open Source Python and Xarray</a></li>
<li><a href="#learning-objectives" id="markdown-toc-learning-objectives"><i class="fa fa-graduation-cap" aria-hidden="true"></i> Learning Objectives</a></li>
<li><a href="#get-started-downloading-maca-v2-climate-data-in-python" id="markdown-toc-get-started-downloading-maca-v2-climate-data-in-python">Get Started Downloading MACA v2 Climate Data in Python</a></li>
<li><a href="#get-started-with-downloading-data" id="markdown-toc-get-started-with-downloading-data">Get Started With Downloading Data</a></li>
<li><a href="#select-data-download-options" id="markdown-toc-select-data-download-options">Select Data Download Options</a></li>
<li><a href="#data-access-tip" id="markdown-toc-data-access-tip"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Data Access Tip</a></li>
<li><a href="#open-your-data" id="markdown-toc-open-your-data">Open Your Data</a></li>
<li><a href="#subset-your-data" id="markdown-toc-subset-your-data">Subset Your Data</a></li>
</ul>
</nav>
</aside>
<div class="notice--success">
<h2 id="open-maca-v2-climate-data-programmatically-using-open-source-python-and-xarray"><i class="fa fa-ship" aria-hidden="true"></i> Open MACA v2 Climate data Programmatically using Open Source Python and Xarray</h2>
<p>In this lesson, you will learn how to work with Climate Data Sets (MACA v2 for the Continental United States - CONUS) stored in netcdf 4 format using open source <strong>Python</strong>.</p>
<h2 id="learning-objectives"><i class="fa fa-graduation-cap" aria-hidden="true"></i> Learning Objectives</h2>
<p>After completing this chapter, you will be able to:</p>
<ul>
<li>Download different types of MACA v2 climate data in <code class="language-plaintext highlighter-rouge">netcdf 4</code> format</li>
<li>Open and process netcdf4 data using <code class="language-plaintext highlighter-rouge">xarray</code></li>
</ul>
</div>
<h2 id="get-started-downloading-maca-v2-climate-data-in-python">Get Started Downloading MACA v2 Climate Data in Python</h2>
<p>To begin, load the libraries below.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Import packages
</span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="n">np</span>
<span class="kn">import</span> <span class="nn">netCDF4</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="n">plt</span>
<span class="kn">import</span> <span class="nn">xarray</span> <span class="k">as</span> <span class="n">xr</span>
<span class="kn">import</span> <span class="nn">cartopy.crs</span> <span class="k">as</span> <span class="n">ccrs</span>
<span class="kn">import</span> <span class="nn">cartopy.feature</span> <span class="k">as</span> <span class="n">cfeature</span>
<span class="kn">import</span> <span class="nn">seaborn</span> <span class="k">as</span> <span class="n">sns</span>
<span class="c1"># Plotting options
</span><span class="n">sns</span><span class="p">.</span><span class="nb">set</span><span class="p">(</span><span class="n">font_scale</span><span class="o">=</span><span class="mf">1.3</span><span class="p">)</span>
<span class="n">sns</span><span class="p">.</span><span class="n">set_style</span><span class="p">(</span><span class="s">"white"</span><span class="p">)</span>
</code></pre></div></div>
<h2 id="get-started-with-downloading-data">Get Started With Downloading Data</h2>
<p>The data you will use in this lesson are “Monthly aggregation of downscaled
daily meteorological data of Monthly Precipitation Amount from College of Global
Change and Earth System Science, Beijing Normal University”. In short, the data
contain a monthly summary of lots of meteorological data, such as precipitation,
air temperature, and more. The data are derived from a climate model that predicts
future trendsin these variables over time.</p>
<p>Below, you will create and assign three <strong>Python</strong> variables that allow you to
programatically select which data you wish to download in this notebook.
This workflow could then be convered into an automated workflow that accesses
and slices MACA v2 data for an analysis.</p>
<p>The variables including:</p>
<h3 id="select-a-climate-model">Select a Climate Model</h3>
<ul>
<li><code class="language-plaintext highlighter-rouge">model = </code> This Python variable can be set to any number between 0 and 19 which represents the 20 climate models that are available for MACA v2 data. The model represents how (the methods used) the climate data were created.
<a href="https://climate.northwestknowledge.net/MACA/GCMs.php" target="_blank">You can learn more about each model by clicking here</a></li>
</ul>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Models to chose from
</span><span class="n">model_name</span> <span class="o">=</span> <span class="p">(</span><span class="s">'bcc-csm1-1'</span><span class="p">,</span>
<span class="s">'bcc-csm1-1-m'</span><span class="p">,</span>
<span class="s">'BNU-ESM'</span><span class="p">,</span>
<span class="s">'CanESM2'</span><span class="p">,</span>
<span class="s">'CCSM4'</span><span class="p">,</span>
<span class="s">'CNRM-CM5'</span><span class="p">,</span>
<span class="s">'CSIRO-Mk3-6-0'</span><span class="p">,</span>
<span class="s">'GFDL-ESM2G'</span><span class="p">,</span>
<span class="s">'GFDL-ESM2M'</span><span class="p">,</span>
<span class="s">'HadGEM2-CC365'</span><span class="p">,</span>
<span class="s">'HadGEM2-ES365'</span><span class="p">,</span>
<span class="s">'inmcm4'</span><span class="p">,</span>
<span class="s">'IPSL-CM5A-MR'</span><span class="p">,</span>
<span class="s">'IPSL-CM5A-LR'</span><span class="p">,</span>
<span class="s">'IPSL-CM5B-LR'</span><span class="p">,</span>
<span class="s">'MIROC5'</span><span class="p">,</span>
<span class="s">'MIROC-ESM'</span><span class="p">,</span>
<span class="s">'MIROC-ESM-CHEM'</span><span class="p">,</span>
<span class="s">'MRI-CGCM3'</span><span class="p">,</span>
<span class="s">'NorESM1-M'</span><span class="p">)</span>
</code></pre></div></div>
<h3 id="climate-data-variables">Climate Data Variables</h3>
<ul>
<li><code class="language-plaintext highlighter-rouge">var = </code> is the variable in the dataset you want to work with. There are 9 options for variables and they are listed in both short and long name versions below. You can assign <code class="language-plaintext highlighter-rouge">var = </code> to any number between 0 and 8, where 0 is the first option in the list, and 8 is the last. In the list below note that is <code class="language-plaintext highlighter-rouge">var = 0</code> you would be selecting <strong>tax_max</strong> or <strong>max temperature</strong>.</li>
</ul>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># These are the variable options for the met data
</span><span class="n">variable_name</span> <span class="o">=</span> <span class="p">(</span><span class="s">'tasmax'</span><span class="p">,</span>
<span class="s">'tasmin'</span><span class="p">,</span>
<span class="s">'rhsmax'</span><span class="p">,</span>
<span class="s">'rhsmin'</span><span class="p">,</span>
<span class="s">'pr'</span><span class="p">,</span>
<span class="s">'rsds'</span><span class="p">,</span>
<span class="s">'uas'</span><span class="p">,</span>
<span class="s">'vas'</span><span class="p">,</span>
<span class="s">'huss'</span><span class="p">)</span>
<span class="c1"># These are var options in long form
</span><span class="n">var_long_name</span> <span class="o">=</span> <span class="p">(</span><span class="s">'air_temperature'</span><span class="p">,</span>
<span class="s">'air_temperature'</span><span class="p">,</span>
<span class="s">'relative_humidity'</span><span class="p">,</span>
<span class="s">'relative_humidity'</span><span class="p">,</span>
<span class="s">'precipitation'</span><span class="p">,</span>
<span class="s">'surface_downwelling_shortwave_flux_in_air'</span><span class="p">,</span>
<span class="s">'eastward_wind'</span><span class="p">,</span>
<span class="s">'northward_wind'</span><span class="p">,</span>
<span class="s">'specific_humidity'</span><span class="p">)</span>
</code></pre></div></div>
<h3 id="climate-data-scenarios">Climate Data Scenarios</h3>
<ul>
<li><code class="language-plaintext highlighter-rouge">scenario = </code> can be chosen to pick which climate scenario you want to you. <code class="language-plaintext highlighter-rouge">0</code> is the historical actual data. This data is based on actual data and is not modeled. <code class="language-plaintext highlighter-rouge">1</code> is the <code class="language-plaintext highlighter-rouge">rcp45</code> scenario, which is described as an intermediate climate scenario. <code class="language-plaintext highlighter-rouge">2</code> is the <code class="language-plaintext highlighter-rouge">rcp85</code> scenario, which is a worst case (strongest immissions) climate scenario.</li>
</ul>
<p class="notice--success"><i class="fa fa-star"></i> <strong>Data Tip:</strong> <a href="https://climate.northwestknowledge.net/NWTOOLBOX/mapping.php" target="_blank">You can learn more about the various variables and scenario options by going to the toolbox and clicking on the small yellow question mark next to “variable” or “scenario”</a>. Note that the scenario options are only available when you try to download future predicted data.</p>
<h2 id="select-data-download-options">Select Data Download Options</h2>
<p>Below you first create lists containing the the options that you wish to use to download your data.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># This is the base url required to download data from the thredds server.
</span><span class="n">dir_path</span> <span class="o">=</span> <span class="s">'http://thredds.northwestknowledge.net:8080/thredds/dodsC/'</span>
<span class="c1"># These are the variable options for the met data
</span><span class="n">variable_name</span> <span class="o">=</span> <span class="p">(</span><span class="s">'tasmax'</span><span class="p">,</span>
<span class="s">'tasmin'</span><span class="p">,</span>
<span class="s">'rhsmax'</span><span class="p">,</span>
<span class="s">'rhsmin'</span><span class="p">,</span>
<span class="s">'pr'</span><span class="p">,</span>
<span class="s">'rsds'</span><span class="p">,</span>
<span class="s">'uas'</span><span class="p">,</span>
<span class="s">'vas'</span><span class="p">,</span>
<span class="s">'huss'</span><span class="p">)</span>
<span class="c1"># These are var options in long form
</span><span class="n">var_long_name</span> <span class="o">=</span> <span class="p">(</span><span class="s">'air_temperature'</span><span class="p">,</span>
<span class="s">'air_temperature'</span><span class="p">,</span>
<span class="s">'relative_humidity'</span><span class="p">,</span>
<span class="s">'relative_humidity'</span><span class="p">,</span>
<span class="s">'precipitation'</span><span class="p">,</span>
<span class="s">'surface_downwelling_shortwave_flux_in_air'</span><span class="p">,</span>
<span class="s">'eastward_wind'</span><span class="p">,</span>
<span class="s">'northward_wind'</span><span class="p">,</span>
<span class="s">'specific_humidity'</span><span class="p">)</span>
<span class="c1"># Models to chose from
</span><span class="n">model_name</span> <span class="o">=</span> <span class="p">(</span><span class="s">'bcc-csm1-1'</span><span class="p">,</span>
<span class="s">'bcc-csm1-1-m'</span><span class="p">,</span>
<span class="s">'BNU-ESM'</span><span class="p">,</span>
<span class="s">'CanESM2'</span><span class="p">,</span>
<span class="s">'CCSM4'</span><span class="p">,</span>
<span class="s">'CNRM-CM5'</span><span class="p">,</span>
<span class="s">'CSIRO-Mk3-6-0'</span><span class="p">,</span>
<span class="s">'GFDL-ESM2G'</span><span class="p">,</span>
<span class="s">'GFDL-ESM2M'</span><span class="p">,</span>
<span class="s">'HadGEM2-CC365'</span><span class="p">,</span>
<span class="s">'HadGEM2-ES365'</span><span class="p">,</span>
<span class="s">'inmcm4'</span><span class="p">,</span>
<span class="s">'IPSL-CM5A-MR'</span><span class="p">,</span>
<span class="s">'IPSL-CM5A-LR'</span><span class="p">,</span>
<span class="s">'IPSL-CM5B-LR'</span><span class="p">,</span>
<span class="s">'MIROC5'</span><span class="p">,</span>
<span class="s">'MIROC-ESM'</span><span class="p">,</span>
<span class="s">'MIROC-ESM-CHEM'</span><span class="p">,</span>
<span class="s">'MRI-CGCM3'</span><span class="p">,</span>
<span class="s">'NorESM1-M'</span><span class="p">)</span>
<span class="c1"># Scenarios
</span><span class="n">scenario_type</span> <span class="o">=</span> <span class="p">(</span><span class="s">'historical'</span><span class="p">,</span> <span class="s">'rcp45'</span><span class="p">,</span> <span class="s">'rcp85'</span><span class="p">)</span>
<span class="c1"># Year start and ends (historical vs projected)
</span><span class="n">year_start</span> <span class="o">=</span> <span class="p">(</span><span class="s">'1950'</span><span class="p">,</span> <span class="s">'2006'</span><span class="p">,</span> <span class="s">'2006'</span><span class="p">)</span>
<span class="n">year_end</span> <span class="o">=</span> <span class="p">(</span><span class="s">'2005'</span><span class="p">,</span> <span class="s">'2099'</span><span class="p">,</span> <span class="s">'2099'</span><span class="p">)</span>
<span class="n">run_num</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="mi">20</span>
<span class="n">run_num</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span> <span class="o">=</span> <span class="mi">6</span> <span class="c1"># setting CCSM4 with run 6
</span><span class="n">domain</span> <span class="o">=</span> <span class="s">'CONUS'</span>
</code></pre></div></div>
<p>Next, select the options that you want to use for your data download.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Model options between 0-19
</span><span class="n">model</span> <span class="o">=</span> <span class="mi">2</span>
<span class="c1"># Options 0-8 will work for var. Var maps to the variable name below
</span><span class="n">var</span> <span class="o">=</span> <span class="mi">0</span>
<span class="c1"># Options range from 0-2
</span><span class="n">scenario</span> <span class="o">=</span> <span class="mi">2</span>
<span class="k">try</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">"Great! You have selected: </span><span class="se">\n</span><span class="s"> </span><span class="se">\u2705</span><span class="s"> Variable: {} </span><span class="se">\n</span><span class="s"> </span><span class="se">\u2705</span><span class="s"> Model: {}, "</span>
<span class="s">"</span><span class="se">\n</span><span class="s"> </span><span class="se">\u2705</span><span class="s"> Scenario: {}"</span><span class="p">.</span><span class="nb">format</span><span class="p">(</span><span class="n">variable_name</span><span class="p">[</span><span class="n">var</span><span class="p">],</span>
<span class="n">model_name</span><span class="p">[</span><span class="n">model</span><span class="p">],</span>
<span class="n">scenario_type</span><span class="p">[</span><span class="n">scenario</span><span class="p">]))</span>
<span class="k">except</span> <span class="nb">IndexError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
<span class="k">raise</span> <span class="nb">IndexError</span><span class="p">(</span><span class="s">"Oops, it looks like you selected value that is "</span>
<span class="s">"not within the range of values which is 0-2. please look"</span>
<span class="s">"closely at your selected values."</span><span class="p">)</span>
</code></pre></div></div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Great! You have selected:
✅ Variable: tasmax
✅ Model: BNU-ESM,
✅ Scenario: rcp85
</code></pre></div></div>
<p>Finally, use the <code class="language-plaintext highlighter-rouge">scenario</code> variable to select the time period associated with
the options selected above.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">try</span><span class="p">:</span>
<span class="n">time</span> <span class="o">=</span> <span class="n">year_start</span><span class="p">[</span><span class="n">scenario</span><span class="p">]</span><span class="o">+</span><span class="s">'_'</span> <span class="o">+</span> <span class="n">year_end</span><span class="p">[</span><span class="n">scenario</span><span class="p">]</span>
<span class="k">print</span><span class="p">(</span><span class="s">"</span><span class="se">\u2705</span><span class="s"> Your selected time period is:"</span><span class="p">,</span> <span class="n">time</span><span class="p">)</span>
<span class="k">except</span> <span class="nb">IndexError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
<span class="k">raise</span> <span class="nb">IndexError</span><span class="p">(</span><span class="s">"Oops, it looks like you selected a scenario value that is </span><span class="se">\
</span><span class="s"> not within the range of values which is 0-2"</span><span class="p">)</span>
</code></pre></div></div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>✅ Your selected time period is: 2006_2099
</code></pre></div></div>
<p>Below you create a path to the correct MACA
data using the Python variables created abive. The file name containing both <code class="language-plaintext highlighter-rouge">agg_macav2metdata_</code> and <code class="language-plaintext highlighter-rouge">_monthly.nc</code> represents monthly data. You will
use that data for this lesson over the daily data because it will be a smaller file to
download.</p>
<div class="notice--warning">
<h2 id="data-access-tip"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Data Access Tip</h2>
<h3 id="monthly-vs-daily-data">Monthly vs. Daily Data</h3>
<p>The example below creates a path to the non aggregated monthly CONUS
(Continental United States) data. However you can also access the daily
or aggregated data using a similar approach</p>
<ul>
<li><a href="https://climate.northwestknowledge.net/MACA/OPENDAP.php" target="_blank">Here is a slightly dated but good examples of accessing MACA v2 data using Python.</a> The demo
further shows you how to access data for specific locations rather than needing to download the entire file.</li>
</ul>
</div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># This code creates a path to the monthly MACA v2 data
</span><span class="n">file_name</span> <span class="o">=</span> <span class="p">(</span><span class="s">'agg_macav2metdata_'</span> <span class="o">+</span>
<span class="nb">str</span><span class="p">(</span><span class="n">variable_name</span><span class="p">[</span><span class="n">var</span><span class="p">])</span> <span class="o">+</span>
<span class="s">'_'</span> <span class="o">+</span>
<span class="nb">str</span><span class="p">(</span><span class="n">model_name</span><span class="p">[</span><span class="n">model</span><span class="p">])</span> <span class="o">+</span>
<span class="s">'_r'</span> <span class="o">+</span>
<span class="nb">str</span><span class="p">(</span><span class="n">run_num</span><span class="p">[</span><span class="n">model</span><span class="p">])</span><span class="o">+</span><span class="s">'i1p1_'</span> <span class="o">+</span>
<span class="nb">str</span><span class="p">(</span><span class="n">scenario_type</span><span class="p">[</span><span class="n">scenario</span><span class="p">])</span> <span class="o">+</span>
<span class="s">'_'</span> <span class="o">+</span>
<span class="n">time</span> <span class="o">+</span> <span class="s">'_'</span> <span class="o">+</span>
<span class="n">domain</span> <span class="o">+</span> <span class="s">'_monthly.nc'</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">"</span><span class="se">\u2705</span><span class="s"> You are accessing:</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">file_name</span><span class="p">,</span> <span class="s">"</span><span class="se">\n</span><span class="s"> data in netcdf format"</span><span class="p">)</span>
</code></pre></div></div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>✅ You are accessing:
agg_macav2metdata_tasmax_BNU-ESM_r1i1p1_rcp85_2006_2099_CONUS_monthly.nc
data in netcdf format
</code></pre></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">full_file_path</span> <span class="o">=</span> <span class="n">dir_path</span> <span class="o">+</span> <span class="n">file_name</span>
<span class="k">print</span><span class="p">(</span><span class="s">"The full path to your data is: </span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">full_file_path</span><span class="p">)</span>
</code></pre></div></div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>The full path to your data is:
http://thredds.northwestknowledge.net:8080/thredds/dodsC/agg_macav2metdata_tasmax_BNU-ESM_r1i1p1_rcp85_2006_2099_CONUS_monthly.nc
</code></pre></div></div>
<h2 id="open-your-data">Open Your Data</h2>
<p>Below you open your data with xarray. The open data code is wrapped in a
try/except block to ensure that it fails gracefully if the data can’t be
accessed. Remember that when you are opening data here, you are hitting a
server online. Thus you need internet access to run the code below.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Open the data from the thredds server
</span><span class="k">try</span><span class="p">:</span>
<span class="n">max_temp_xr</span> <span class="o">=</span> <span class="n">xr</span><span class="p">.</span><span class="n">open_dataset</span><span class="p">(</span><span class="n">full_file_path</span><span class="p">)</span>
<span class="k">except</span> <span class="nb">OSError</span> <span class="k">as</span> <span class="n">oe</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">"Oops, it looks like the file that you are trying to connect to, "</span>
<span class="s">"{}, doesn't exist. Try to revisit your model options to ensure "</span>
<span class="s">"the data exist on the server. "</span><span class="p">.</span><span class="nb">format</span><span class="p">(</span><span class="n">full_file_path</span><span class="p">))</span>
</code></pre></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># View your temperature data
</span><span class="n">max_temp_xr</span>
</code></pre></div></div>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.Dataset>
Dimensions: (lat: 585, crs: 1, lon: 1386, time: 1128)
Coordinates:
* lat (lat) float64 25.06 25.1 25.15 25.19 ... 49.31 49.35 49.4
* crs (crs) int32 1
* lon (lon) float64 235.2 235.3 235.3 235.4 ... 292.9 292.9 292.9
* time (time) object 2006-01-15 00:00:00 ... 2099-12-15 00:00:00
Data variables:
air_temperature (time, lat, lon) float32 ...
Attributes: (12/46)
description: Multivariate Adaptive Constructed Analog...
id: MACAv2-METDATA
naming_authority: edu.uidaho.reacch
Metadata_Conventions: Unidata Dataset Discovery v1.0
Metadata_Link:
cdm_data_type: FLOAT
... ...
contributor_role: Postdoctoral Fellow
publisher_name: REACCH
publisher_email: reacch@uidaho.edu
publisher_url: http://www.reacchpna.org/
license: Creative Commons CC0 1.0 Universal Dedic...
coordinate_system: WGS84,EPSG:4326</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.Dataset</div></div><ul class="xr-sections"><li class="xr-section-item"><input id="section-ce3a6852-ffef-40f9-97cf-5852ee3c6b79" class="xr-section-summary-in" type="checkbox" disabled="" /><label for="section-ce3a6852-ffef-40f9-97cf-5852ee3c6b79" class="xr-section-summary" title="Expand/collapse section">Dimensions:</label><div class="xr-section-inline-details"><ul class="xr-dim-list"><li><span class="xr-has-index">lat</span>: 585</li><li><span class="xr-has-index">crs</span>: 1</li><li><span class="xr-has-index">lon</span>: 1386</li><li><span class="xr-has-index">time</span>: 1128</li></ul></div><div class="xr-section-details"></div></li><li class="xr-section-item"><input id="section-99c4ce21-aa76-4b2c-94e5-5fc3e8dd7327" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-99c4ce21-aa76-4b2c-94e5-5fc3e8dd7327" class="xr-section-summary">Coordinates: <span>(4)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lat</span></div><div class="xr-var-dims">(lat)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">25.06 25.1 25.15 ... 49.35 49.4</div><input id="attrs-eba4c49e-a73c-406c-8d10-8b9022a4000b" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-eba4c49e-a73c-406c-8d10-8b9022a4000b" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-439930ca-d64d-4834-86d6-6d86b8232902" class="xr-var-data-in" type="checkbox" /><label for="data-439930ca-d64d-4834-86d6-6d86b8232902" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>latitude</dd><dt><span>standard_name :</span></dt><dd>latitude</dd><dt><span>units :</span></dt><dd>degrees_north</dd><dt><span>axis :</span></dt><dd>Y</dd><dt><span>description :</span></dt><dd>Latitude of the center of the grid cell</dd></dl></div><div class="xr-var-data"><pre>array([25.063078, 25.104744, 25.14641 , ..., 49.312691, 49.354359, 49.396023])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">crs</span></div><div class="xr-var-dims">(crs)</div><div class="xr-var-dtype">int32</div><div class="xr-var-preview xr-preview">1</div><input id="attrs-11721bf8-5d00-4a03-b181-1f24e156b40b" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-11721bf8-5d00-4a03-b181-1f24e156b40b" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-65d56353-8810-4638-bd13-674d6c0bb9b9" class="xr-var-data-in" type="checkbox" /><label for="data-65d56353-8810-4638-bd13-674d6c0bb9b9" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>grid_mapping_name :</span></dt><dd>latitude_longitude</dd><dt><span>longitude_of_prime_meridian :</span></dt><dd>0.0</dd><dt><span>semi_major_axis :</span></dt><dd>6378137.0</dd><dt><span>inverse_flattening :</span></dt><dd>298.257223563</dd></dl></div><div class="xr-var-data"><pre>array([1], dtype=int32)</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lon</span></div><div class="xr-var-dims">(lon)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">235.2 235.3 235.3 ... 292.9 292.9</div><input id="attrs-f7c2504e-922b-4f21-af18-33c47520a00d" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-f7c2504e-922b-4f21-af18-33c47520a00d" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-518eab69-abda-42b9-9936-c0c7c8baffd5" class="xr-var-data-in" type="checkbox" /><label for="data-518eab69-abda-42b9-9936-c0c7c8baffd5" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>units :</span></dt><dd>degrees_east</dd><dt><span>axis :</span></dt><dd>X</dd><dt><span>description :</span></dt><dd>Longitude of the center of the grid cell</dd><dt><span>long_name :</span></dt><dd>longitude</dd><dt><span>standard_name :</span></dt><dd>longitude</dd></dl></div><div class="xr-var-data"><pre>array([235.227844, 235.269501, 235.311157, ..., 292.851929, 292.893585,
292.935242])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">time</span></div><div class="xr-var-dims">(time)</div><div class="xr-var-dtype">object</div><div class="xr-var-preview xr-preview">2006-01-15 00:00:00 ... 2099-12-...</div><input id="attrs-1a476945-c112-4a2f-9879-015c55c18131" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-1a476945-c112-4a2f-9879-015c55c18131" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-1a0a0646-748e-4c49-ac8d-a5deed65c5db" class="xr-var-data-in" type="checkbox" /><label for="data-1a0a0646-748e-4c49-ac8d-a5deed65c5db" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>description :</span></dt><dd>days since 1900-01-01</dd></dl></div><div class="xr-var-data"><pre>array([cftime.DatetimeNoLeap(2006, 1, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2006, 2, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2006, 3, 15, 0, 0, 0, 0, has_year_zero=True), ...,
cftime.DatetimeNoLeap(2099, 10, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2099, 11, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2099, 12, 15, 0, 0, 0, 0, has_year_zero=True)],
dtype=object)</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-95574928-5146-4dc0-8b1e-945ffb66e560" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-95574928-5146-4dc0-8b1e-945ffb66e560" class="xr-section-summary">Data variables: <span>(1)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span>air_temperature</span></div><div class="xr-var-dims">(time, lat, lon)</div><div class="xr-var-dtype">float32</div><div class="xr-var-preview xr-preview">...</div><input id="attrs-a811ca12-fcbf-4ebc-9312-15ac038b5976" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-a811ca12-fcbf-4ebc-9312-15ac038b5976" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-dd5ae40a-0d0d-4544-a613-ae506ba11eda" class="xr-var-data-in" type="checkbox" /><label for="data-dd5ae40a-0d0d-4544-a613-ae506ba11eda" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>Monthly Average of Daily Maximum Near-Surface Air Temperature</dd><dt><span>units :</span></dt><dd>K</dd><dt><span>grid_mapping :</span></dt><dd>crs</dd><dt><span>standard_name :</span></dt><dd>air_temperature</dd><dt><span>height :</span></dt><dd>2 m</dd><dt><span>cell_methods :</span></dt><dd>time: maximum(interval: 24 hours);mean over days</dd><dt><span>_ChunkSizes :</span></dt><dd>[ 10 44 107]</dd></dl></div><div class="xr-var-data"><pre>[914593680 values with dtype=float32]</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-e433ac79-2053-43a9-b790-70a5347db626" class="xr-section-summary-in" type="checkbox" /><label for="section-e433ac79-2053-43a9-b790-70a5347db626" class="xr-section-summary">Attributes: <span>(46)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"><dt><span>description :</span></dt><dd>Multivariate Adaptive Constructed Analogs (MACA) method, version 2.3,Dec 2013.</dd><dt><span>id :</span></dt><dd>MACAv2-METDATA</dd><dt><span>naming_authority :</span></dt><dd>edu.uidaho.reacch</dd><dt><span>Metadata_Conventions :</span></dt><dd>Unidata Dataset Discovery v1.0</dd><dt><span>Metadata_Link :</span></dt><dd></dd><dt><span>cdm_data_type :</span></dt><dd>FLOAT</dd><dt><span>title :</span></dt><dd>Monthly aggregation of downscaled daily meteorological data of Monthly Average of Daily Maximum Near-Surface Air Temperature from College of Global Change and Earth System Science, Beijing Normal University (BNU-ESM) using the run r1i1p1 of the rcp85 scenario.</dd><dt><span>summary :</span></dt><dd>This archive contains monthly downscaled meteorological and hydrological projections for the Conterminous United States at 1/24-deg resolution. These monthly values are obtained by aggregating the daily values obtained from the downscaling using the Multivariate Adaptive Constructed Analogs (MACA, Abatzoglou, 2012) statistical downscaling method with the METDATA (Abatzoglou,2013) training dataset. The downscaled meteorological variables are maximum/minimum temperature(tasmax/tasmin), maximum/minimum relative humidity (rhsmax/rhsmin),precipitation amount(pr), downward shortwave solar radiation(rsds), eastward wind(uas), northward wind(vas), and specific humidity(huss). The downscaling is based on the 365-day model outputs from different global climate models (GCMs) from Phase 5 of the Coupled Model Inter-comparison Project (CMIP3) utlizing the historical (1950-2005) and future RCP4.5/8.5(2006-2099) scenarios. </dd><dt><span>keywords :</span></dt><dd>monthly, precipitation, maximum temperature, minimum temperature, downward shortwave solar radiation, specific humidity, wind velocity, CMIP5, Gridded Meteorological Data</dd><dt><span>keywords_vocabulary :</span></dt><dd></dd><dt><span>standard_name_vocabulary :</span></dt><dd>CF-1.0</dd><dt><span>history :</span></dt><dd>No revisions.</dd><dt><span>comment :</span></dt><dd></dd><dt><span>geospatial_bounds :</span></dt><dd>POLYGON((-124.7722 25.0631,-124.7722 49.3960, -67.0648 49.3960,-67.0648, 25.0631, -124.7722,25.0631))</dd><dt><span>geospatial_lat_min :</span></dt><dd>25.0631</dd><dt><span>geospatial_lat_max :</span></dt><dd>49.3960</dd><dt><span>geospatial_lon_min :</span></dt><dd>-124.7722</dd><dt><span>geospatial_lon_max :</span></dt><dd>-67.0648</dd><dt><span>geospatial_lat_units :</span></dt><dd>decimal degrees north</dd><dt><span>geospatial_lon_units :</span></dt><dd>decimal degrees east</dd><dt><span>geospatial_lat_resolution :</span></dt><dd>0.0417</dd><dt><span>geospatial_lon_resolution :</span></dt><dd>0.0417</dd><dt><span>geospatial_vertical_min :</span></dt><dd>0.0</dd><dt><span>geospatial_vertical_max :</span></dt><dd>0.0</dd><dt><span>geospatial_vertical_resolution :</span></dt><dd>0.0</dd><dt><span>geospatial_vertical_positive :</span></dt><dd>up</dd><dt><span>time_coverage_start :</span></dt><dd>2091-01-01T00:0</dd><dt><span>time_coverage_end :</span></dt><dd>2095-12-31T00:00</dd><dt><span>time_coverage_duration :</span></dt><dd>P5Y</dd><dt><span>time_coverage_resolution :</span></dt><dd>P1M</dd><dt><span>date_created :</span></dt><dd>2014-05-15</dd><dt><span>date_modified :</span></dt><dd>2014-05-15</dd><dt><span>date_issued :</span></dt><dd>2014-05-15</dd><dt><span>creator_name :</span></dt><dd>John Abatzoglou</dd><dt><span>creator_url :</span></dt><dd>http://maca.northwestknowledge.net</dd><dt><span>creator_email :</span></dt><dd>jabatzoglou@uidaho.edu</dd><dt><span>institution :</span></dt><dd>University of Idaho</dd><dt><span>processing_level :</span></dt><dd>GRID</dd><dt><span>project :</span></dt><dd></dd><dt><span>contributor_name :</span></dt><dd>Katherine C. Hegewisch</dd><dt><span>contributor_role :</span></dt><dd>Postdoctoral Fellow</dd><dt><span>publisher_name :</span></dt><dd>REACCH</dd><dt><span>publisher_email :</span></dt><dd>reacch@uidaho.edu</dd><dt><span>publisher_url :</span></dt><dd>http://www.reacchpna.org/</dd><dt><span>license :</span></dt><dd>Creative Commons CC0 1.0 Universal Dedication(http://creativecommons.org/publicdomain/zero/1.0/legalcode)</dd><dt><span>coordinate_system :</span></dt><dd>WGS84,EPSG:4326</dd></dl></div></li></ul></div></div>
<h2 id="subset-your-data">Subset Your Data</h2>
<p>Currently, the dataset you have is too big to work with. You can fix this by subsetting the data. There are two ways you can subset the data: spatially, and temporally.</p>
<p>To spatially subset the data, you will only look at data from one point in the xarray Dataset. Below, assign a new number for <code class="language-plaintext highlighter-rouge">latitude</code> and <code class="language-plaintext highlighter-rouge">longitude</code> to pick a new point. The data’s latitude values range from about 25 to 50, and the data’s longitude values range from 235 to 292. So try and pick new values within those ranges.</p>
<p>To temporally subset the data, you can pick a start date and end date to trim the data to. Below, assign new values for the data to start and end at. Make sure the values you assign stay in the quotes provided. The format should be <code class="language-plaintext highlighter-rouge">'yyyy-mm'</code>. Keep in mind that depending on which scenario you chose above, the years of your data will be different. So pick dates that are within the scenario you chose.</p>
<table>
<thead>
<tr>
<th>Scenario Number</th>
<th>Date Range</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>1950-2005</td>
</tr>
<tr>
<td>1</td>
<td>2006-2099</td>
</tr>
<tr>
<td>2</td>
<td>2006-2099</td>
</tr>
</tbody>
</table>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Select the latitude, longitude, and timeframe to subset the data to
</span>
<span class="c1"># Ensure your latitude value is between 25 and 50, and your longitude value is between 235 and 292
# latitude = 35
# longitude = 270
</span><span class="n">start_date</span> <span class="o">=</span> <span class="s">'2008-01'</span>
<span class="n">end_date</span> <span class="o">=</span> <span class="s">'2012-09'</span>
</code></pre></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Select a lat / lon location that you wish to use to extract the data
</span><span class="n">latitude</span> <span class="o">=</span> <span class="n">max_temp_xr</span><span class="p">.</span><span class="n">lat</span><span class="p">.</span><span class="n">values</span><span class="p">[</span><span class="mi">300</span><span class="p">]</span>
<span class="n">longitude</span> <span class="o">=</span> <span class="n">max_temp_xr</span><span class="p">.</span><span class="n">lon</span><span class="p">.</span><span class="n">values</span><span class="p">[</span><span class="mi">150</span><span class="p">]</span>
<span class="k">print</span><span class="p">(</span><span class="s">"You selected the following x,y location:"</span><span class="p">,</span> <span class="n">longitude</span><span class="p">,</span> <span class="n">latitude</span><span class="p">)</span>
</code></pre></div></div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>You selected the following x,y location: 241.4777374267578 37.5628776550293
</code></pre></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Slice one lat/lon data point
</span><span class="n">temp_single_point</span> <span class="o">=</span> <span class="n">max_temp_xr</span><span class="p">[</span><span class="s">"air_temperature"</span><span class="p">].</span><span class="n">sel</span><span class="p">(</span>
<span class="n">lat</span><span class="o">=</span><span class="n">latitude</span><span class="p">,</span>
<span class="n">lon</span><span class="o">=</span><span class="n">longitude</span><span class="p">)</span>
<span class="n">temp_single_point</span>
</code></pre></div></div>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.DataArray 'air_temperature' (time: 1128)>
array([282.93192, 285.54318, 291.04315, ..., 301.6674 , 290.809 , 288.78992],
dtype=float32)
Coordinates:
lat float64 37.56
lon float64 241.5
* time (time) object 2006-01-15 00:00:00 ... 2099-12-15 00:00:00
Attributes:
long_name: Monthly Average of Daily Maximum Near-Surface Air Tempera...
units: K
grid_mapping: crs
standard_name: air_temperature
height: 2 m
cell_methods: time: maximum(interval: 24 hours);mean over days
_ChunkSizes: [ 10 44 107]</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.DataArray</div><div class="xr-array-name">'air_temperature'</div><ul class="xr-dim-list"><li><span class="xr-has-index">time</span>: 1128</li></ul></div><ul class="xr-sections"><li class="xr-section-item"><div class="xr-array-wrap"><input id="section-16734e11-e290-483d-aa5a-a0ce22fce1f9" class="xr-array-in" type="checkbox" checked="" /><label for="section-16734e11-e290-483d-aa5a-a0ce22fce1f9" title="Show/hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-array-preview xr-preview"><span>282.9 285.5 291.0 290.9 292.2 298.0 ... 308.8 306.6 301.7 290.8 288.8</span></div><div class="xr-array-data"><pre>array([282.93192, 285.54318, 291.04315, ..., 301.6674 , 290.809 , 288.78992],
dtype=float32)</pre></div></div></li><li class="xr-section-item"><input id="section-d3296bcc-537c-42b6-aca9-b3a57d76794a" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-d3296bcc-537c-42b6-aca9-b3a57d76794a" class="xr-section-summary">Coordinates: <span>(3)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span>lat</span></div><div class="xr-var-dims">()</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">37.56</div><input id="attrs-96b3f503-4707-4e6e-a674-9fe9f22961ca" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-96b3f503-4707-4e6e-a674-9fe9f22961ca" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-7e3fd502-8b14-4674-908c-46178f6af76a" class="xr-var-data-in" type="checkbox" /><label for="data-7e3fd502-8b14-4674-908c-46178f6af76a" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>latitude</dd><dt><span>standard_name :</span></dt><dd>latitude</dd><dt><span>units :</span></dt><dd>degrees_north</dd><dt><span>axis :</span></dt><dd>Y</dd><dt><span>description :</span></dt><dd>Latitude of the center of the grid cell</dd></dl></div><div class="xr-var-data"><pre>array(37.56287766)</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span>lon</span></div><div class="xr-var-dims">()</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">241.5</div><input id="attrs-2e9338a0-5840-42f1-b58e-6e1ab4c930ce" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-2e9338a0-5840-42f1-b58e-6e1ab4c930ce" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-10d7dc3b-a919-40f9-9e40-0c998f687c04" class="xr-var-data-in" type="checkbox" /><label for="data-10d7dc3b-a919-40f9-9e40-0c998f687c04" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>units :</span></dt><dd>degrees_east</dd><dt><span>axis :</span></dt><dd>X</dd><dt><span>description :</span></dt><dd>Longitude of the center of the grid cell</dd><dt><span>long_name :</span></dt><dd>longitude</dd><dt><span>standard_name :</span></dt><dd>longitude</dd></dl></div><div class="xr-var-data"><pre>array(241.47773743)</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">time</span></div><div class="xr-var-dims">(time)</div><div class="xr-var-dtype">object</div><div class="xr-var-preview xr-preview">2006-01-15 00:00:00 ... 2099-12-...</div><input id="attrs-d1c23f48-febf-4dfe-a9ce-a0bfdd4a5d1f" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-d1c23f48-febf-4dfe-a9ce-a0bfdd4a5d1f" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-08e6724f-ad43-4b25-be9d-ef7f64e652af" class="xr-var-data-in" type="checkbox" /><label for="data-08e6724f-ad43-4b25-be9d-ef7f64e652af" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>description :</span></dt><dd>days since 1900-01-01</dd></dl></div><div class="xr-var-data"><pre>array([cftime.DatetimeNoLeap(2006, 1, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2006, 2, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2006, 3, 15, 0, 0, 0, 0, has_year_zero=True), ...,
cftime.DatetimeNoLeap(2099, 10, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2099, 11, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2099, 12, 15, 0, 0, 0, 0, has_year_zero=True)],
dtype=object)</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-cb04422e-4ee0-48b9-98a2-8c3c0ae60437" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-cb04422e-4ee0-48b9-98a2-8c3c0ae60437" class="xr-section-summary">Attributes: <span>(7)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>Monthly Average of Daily Maximum Near-Surface Air Temperature</dd><dt><span>units :</span></dt><dd>K</dd><dt><span>grid_mapping :</span></dt><dd>crs</dd><dt><span>standard_name :</span></dt><dd>air_temperature</dd><dt><span>height :</span></dt><dd>2 m</dd><dt><span>cell_methods :</span></dt><dd>time: maximum(interval: 24 hours);mean over days</dd><dt><span>_ChunkSizes :</span></dt><dd>[ 10 44 107]</dd></dl></div></li></ul></div></div>
<p>Below you quickly plot the data. You will learn more about working with these
data (and creating nicer plots) in the following lessons.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Quick plot of the data
</span><span class="n">temp_single_point</span><span class="p">.</span><span class="n">plot</span><span class="p">.</span><span class="n">line</span><span class="p">()</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-03-get-climate-data/2020-10-16-netcdf-03-get-climate-data_20_0.png" />
</figure>Leah WasserMACA V2 climate data provides but historica and future predictions of climate variables using different models. Learn how to download netcdf 4 format programatically using open source Python and open the data with xarray.How to Open and Process NetCDF 4 Data Format in Open Source Python2020-10-16T00:00:00+00:002020-10-16T00:00:00+00:00https://www.earthdatascience.org/courses/use-data-open-source-python/hierarchical-data-formats-hdf/netcdf-04-open-netcdf-climate-data<aside class="sidebar__right">
<nav class="toc">
<header><h4 class="nav__title"><i class="fa fa-file-text"></i> In This Chapter</h4></header>
<ul class="toc__menu" id="markdown-toc">
<li><a href="#open-netcdf-4-climate-data-in-open-source-python-using-xarray" id="markdown-toc-open-netcdf-4-climate-data-in-open-source-python-using-xarray"><i class="fa fa-ship" aria-hidden="true"></i> Open NETCDF 4 Climate Data in Open Source Python Using Xarray</a></li>
<li><a href="#learning-objectives" id="markdown-toc-learning-objectives"><i class="fa fa-graduation-cap" aria-hidden="true"></i> Learning Objectives</a></li>
<li><a href="#what-you-need" id="markdown-toc-what-you-need"><i class="fa fa-check-square-o fa-2" aria-hidden="true"></i> What You Need</a></li>
<li><a href="#get-started-wtih-maca-version-2-data-using-open-source-python" id="markdown-toc-get-started-wtih-maca-version-2-data-using-open-source-python">Get Started wtih MACA Version 2 Data Using Open Source Python</a></li>
<li><a href="#hierarchical-formats-are-self-describing" id="markdown-toc-hierarchical-formats-are-self-describing">Hierarchical Formats Are Self Describing</a></li>
<li><a href="#subsetting-or-slicing-your-data" id="markdown-toc-subsetting-or-slicing-your-data">Subsetting or “Slicing” Your Data</a></li>
<li><a href="#subset-your-data" id="markdown-toc-subset-your-data">Subset Your Data</a></li>
<li><a href="#slice-climate-macav2-data-by-time-and-location" id="markdown-toc-slice-climate-macav2-data-by-time-and-location">Slice Climate MACAv2 Data By Time and Location</a></li>
<li><a href="#convert-subsetted-to-a-dataframe--export-to-a-csv-file" id="markdown-toc-convert-subsetted-to-a-dataframe--export-to-a-csv-file">Convert Subsetted to a DataFrame & Export to a .csv File</a></li>
<li><a href="#slice-the-data-across-a-spatial-extent-for-a-specific-time-period" id="markdown-toc-slice-the-data-across-a-spatial-extent-for-a-specific-time-period">Slice The Data Across a Spatial Extent For A Specific Time Period</a></li>
<li><a href="#spatial-raster-plots-of-maca-v2-climate-data" id="markdown-toc-spatial-raster-plots-of-maca-v2-climate-data">Spatial Raster Plots of MACA v2 Climate Data</a></li>
<li><a href="#plot-multiple-maca-v2-climate-data-raster-files-with-a-spatial-projection" id="markdown-toc-plot-multiple-maca-v2-climate-data-raster-files-with-a-spatial-projection">Plot Multiple MACA v2 Climate Data Raster Files With a Spatial Projection</a></li>
<li><a href="#export-raster-to-geotiff-file" id="markdown-toc-export-raster-to-geotiff-file">Export Raster to Geotiff File</a></li>
</ul>
</nav>
</aside>
<div class="notice--success">
<h2 id="open-netcdf-4-climate-data-in-open-source-python-using-xarray"><i class="fa fa-ship" aria-hidden="true"></i> Open NETCDF 4 Climate Data in Open Source Python Using Xarray</h2>
<p>In this chapter, you will learn how to work with Climate Data Sets (MACA v2 for the United states) stored in netcdf 4 format using open source <strong>Python</strong>.</p>
<h2 id="learning-objectives"><i class="fa fa-graduation-cap" aria-hidden="true"></i> Learning Objectives</h2>
<p>After completing this chapter, you will be able to:</p>
<ul>
<li>Download MACA v2 climate data in <code class="language-plaintext highlighter-rouge">netcdf 4</code> format</li>
<li>Open and process netcdf4 data using <code class="language-plaintext highlighter-rouge">xarray</code></li>
<li>Export climate data in tabular format to <code class="language-plaintext highlighter-rouge">.csv</code> format</li>
</ul>
<h2 id="what-you-need"><i class="fa fa-check-square-o fa-2" aria-hidden="true"></i> What You Need</h2>
<p>OPTIONAL: If you want to explore the netcdf 4 files in a graphics based tool, you can download th <a href="https://www.hdfgroup.org/downloads/hdfview/" target="_blank">free HDF viewer</a> from the HDF Group website.</p>
</div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="n">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="n">pd</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="n">plt</span>
<span class="c1"># netCDF4 needs to be installed in your environment for this to work
</span><span class="kn">import</span> <span class="nn">xarray</span> <span class="k">as</span> <span class="n">xr</span>
<span class="kn">import</span> <span class="nn">rioxarray</span> <span class="k">as</span> <span class="n">rxr</span>
<span class="kn">import</span> <span class="nn">cartopy.crs</span> <span class="k">as</span> <span class="n">ccrs</span>
<span class="kn">import</span> <span class="nn">cartopy.feature</span> <span class="k">as</span> <span class="n">cfeature</span>
<span class="kn">import</span> <span class="nn">seaborn</span> <span class="k">as</span> <span class="n">sns</span>
<span class="kn">import</span> <span class="nn">geopandas</span> <span class="k">as</span> <span class="n">gpd</span>
<span class="kn">import</span> <span class="nn">earthpy</span> <span class="k">as</span> <span class="n">et</span>
<span class="c1"># Plotting options
</span><span class="n">sns</span><span class="p">.</span><span class="nb">set</span><span class="p">(</span><span class="n">font_scale</span><span class="o">=</span><span class="mf">1.3</span><span class="p">)</span>
<span class="n">sns</span><span class="p">.</span><span class="n">set_style</span><span class="p">(</span><span class="s">"white"</span><span class="p">)</span>
<span class="c1"># Optional - set your working directory if you wish to use the data
# accessed lower down in this notebook (the USA state boundary data)
</span><span class="n">os</span><span class="p">.</span><span class="n">chdir</span><span class="p">(</span><span class="n">os</span><span class="p">.</span><span class="n">path</span><span class="p">.</span><span class="n">join</span><span class="p">(</span><span class="n">et</span><span class="p">.</span><span class="n">io</span><span class="p">.</span><span class="n">HOME</span><span class="p">,</span>
<span class="s">'earth-analytics'</span><span class="p">,</span>
<span class="s">'data'</span><span class="p">))</span>
</code></pre></div></div>
<h2 id="get-started-wtih-maca-version-2-data-using-open-source-python">Get Started wtih MACA Version 2 Data Using Open Source Python</h2>
<p>In this lesson you will work with historic projected MACA 2 data that represents
maximum monthly temperature for the Continental United States (CONUS).</p>
<p><code class="language-plaintext highlighter-rouge">agg_macav2metdata_tasmax_BNU-ESM_r1i1p1_historical_1950_2005_CONUS_monthly</code></p>
<p>The file name itself tells you a lot about the data.</p>
<ol>
<li><strong>macav2metdata</strong>: the data are the MACA version two data which are downsampled to the extent of the continental United S tates</li>
<li><strong>tasmax</strong>: Max temperature is the parameter contained within the data</li>
<li><strong>BNU-ESM</strong>: This is the climate (CLM) model used to generate the data</li>
<li><strong>historical</strong>: these data are the modeled historical values for the years <strong>1950 - 2005</strong></li>
<li><strong>CONUS</strong>: These data are for the <strong>CON</strong>tinental <strong>U</strong>nited <strong>S</strong>tates boundary</li>
<li><strong>monthly</strong>: These data are aggregated monthly (rather than daily)</li>
</ol>
<p>Below, you will learn how to open and work with MACA 2 data using open source Python tools.
You will use the <strong>xarray</strong> package which requires the <code class="language-plaintext highlighter-rouge">netcdf4</code> package to work with netcdf data.
The <a href="https://github.com/earthlab/earth-analytics-python-env" target="_blank">most current earth-analytics-python environment </a> contains all of the
packages that you need to complete this tutorial.</p>
<p>To begin, you open up the data using <code class="language-plaintext highlighter-rouge">xarray.open_dataset</code>.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># The (online) url for a MACAv2 dataset for max monthly temperature
</span><span class="n">data_path</span> <span class="o">=</span> <span class="s">"http://thredds.northwestknowledge.net:8080/thredds/dodsC/agg_macav2metdata_tasmax_BNU-ESM_r1i1p1_historical_1950_2005_CONUS_monthly.nc"</span>
<span class="n">max_temp_xr</span> <span class="o">=</span> <span class="n">xr</span><span class="p">.</span><span class="n">open_dataset</span><span class="p">(</span><span class="n">data_path</span><span class="p">)</span>
<span class="c1"># View xarray object
</span><span class="n">max_temp_xr</span>
</code></pre></div></div>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.Dataset>
Dimensions: (lat: 585, crs: 1, lon: 1386, time: 672)
Coordinates:
* lat (lat) float64 25.06 25.1 25.15 25.19 ... 49.31 49.35 49.4
* crs (crs) int32 1
* lon (lon) float64 235.2 235.3 235.3 235.4 ... 292.9 292.9 292.9
* time (time) object 1950-01-15 00:00:00 ... 2005-12-15 00:00:00
Data variables:
air_temperature (time, lat, lon) float32 ...
Attributes: (12/46)
description: Multivariate Adaptive Constructed Analog...
id: MACAv2-METDATA
naming_authority: edu.uidaho.reacch
Metadata_Conventions: Unidata Dataset Discovery v1.0
Metadata_Link:
cdm_data_type: FLOAT
... ...
contributor_role: Postdoctoral Fellow
publisher_name: REACCH
publisher_email: reacch@uidaho.edu
publisher_url: http://www.reacchpna.org/
license: Creative Commons CC0 1.0 Universal Dedic...
coordinate_system: WGS84,EPSG:4326</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.Dataset</div></div><ul class="xr-sections"><li class="xr-section-item"><input id="section-e270c0ad-bb07-4e73-a725-e45c3fe269c6" class="xr-section-summary-in" type="checkbox" disabled="" /><label for="section-e270c0ad-bb07-4e73-a725-e45c3fe269c6" class="xr-section-summary" title="Expand/collapse section">Dimensions:</label><div class="xr-section-inline-details"><ul class="xr-dim-list"><li><span class="xr-has-index">lat</span>: 585</li><li><span class="xr-has-index">crs</span>: 1</li><li><span class="xr-has-index">lon</span>: 1386</li><li><span class="xr-has-index">time</span>: 672</li></ul></div><div class="xr-section-details"></div></li><li class="xr-section-item"><input id="section-c4f2c760-a018-484b-be3c-00efa4e92ed9" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-c4f2c760-a018-484b-be3c-00efa4e92ed9" class="xr-section-summary">Coordinates: <span>(4)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lat</span></div><div class="xr-var-dims">(lat)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">25.06 25.1 25.15 ... 49.35 49.4</div><input id="attrs-caf2debd-0162-403d-8349-73be3948ab51" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-caf2debd-0162-403d-8349-73be3948ab51" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-5a7f4836-2ea2-4b12-860b-a4085c4bbfc7" class="xr-var-data-in" type="checkbox" /><label for="data-5a7f4836-2ea2-4b12-860b-a4085c4bbfc7" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>latitude</dd><dt><span>standard_name :</span></dt><dd>latitude</dd><dt><span>units :</span></dt><dd>degrees_north</dd><dt><span>axis :</span></dt><dd>Y</dd><dt><span>description :</span></dt><dd>Latitude of the center of the grid cell</dd></dl></div><div class="xr-var-data"><pre>array([25.063078, 25.104744, 25.14641 , ..., 49.312691, 49.354359, 49.396023])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">crs</span></div><div class="xr-var-dims">(crs)</div><div class="xr-var-dtype">int32</div><div class="xr-var-preview xr-preview">1</div><input id="attrs-b054e719-f9a3-4462-9db8-5b9f7f2b459b" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-b054e719-f9a3-4462-9db8-5b9f7f2b459b" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-a28fd675-66df-453c-b63f-9bd89649c230" class="xr-var-data-in" type="checkbox" /><label for="data-a28fd675-66df-453c-b63f-9bd89649c230" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>grid_mapping_name :</span></dt><dd>latitude_longitude</dd><dt><span>longitude_of_prime_meridian :</span></dt><dd>0.0</dd><dt><span>semi_major_axis :</span></dt><dd>6378137.0</dd><dt><span>inverse_flattening :</span></dt><dd>298.257223563</dd></dl></div><div class="xr-var-data"><pre>array([1], dtype=int32)</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lon</span></div><div class="xr-var-dims">(lon)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">235.2 235.3 235.3 ... 292.9 292.9</div><input id="attrs-0056911c-e136-453c-8828-b6a6fa9b74d9" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-0056911c-e136-453c-8828-b6a6fa9b74d9" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-4e1e625c-258d-4d59-b813-a62f8a1c5803" class="xr-var-data-in" type="checkbox" /><label for="data-4e1e625c-258d-4d59-b813-a62f8a1c5803" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>units :</span></dt><dd>degrees_east</dd><dt><span>description :</span></dt><dd>Longitude of the center of the grid cell</dd><dt><span>long_name :</span></dt><dd>longitude</dd><dt><span>standard_name :</span></dt><dd>longitude</dd><dt><span>axis :</span></dt><dd>X</dd></dl></div><div class="xr-var-data"><pre>array([235.227844, 235.269501, 235.311157, ..., 292.851929, 292.893585,
292.935242])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">time</span></div><div class="xr-var-dims">(time)</div><div class="xr-var-dtype">object</div><div class="xr-var-preview xr-preview">1950-01-15 00:00:00 ... 2005-12-...</div><input id="attrs-8e3615cf-5ce9-421f-ad68-97fd07049f19" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-8e3615cf-5ce9-421f-ad68-97fd07049f19" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-3db07dcf-a179-4454-89d1-543955387485" class="xr-var-data-in" type="checkbox" /><label for="data-3db07dcf-a179-4454-89d1-543955387485" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>description :</span></dt><dd>days since 1900-01-01</dd></dl></div><div class="xr-var-data"><pre>array([cftime.DatetimeNoLeap(1950, 1, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(1950, 2, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(1950, 3, 15, 0, 0, 0, 0, has_year_zero=True), ...,
cftime.DatetimeNoLeap(2005, 10, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2005, 11, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2005, 12, 15, 0, 0, 0, 0, has_year_zero=True)],
dtype=object)</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-80829507-dca6-440e-89f8-d7d392259ed3" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-80829507-dca6-440e-89f8-d7d392259ed3" class="xr-section-summary">Data variables: <span>(1)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span>air_temperature</span></div><div class="xr-var-dims">(time, lat, lon)</div><div class="xr-var-dtype">float32</div><div class="xr-var-preview xr-preview">...</div><input id="attrs-9d7c8aee-5760-4a41-b791-e2c75441aa25" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-9d7c8aee-5760-4a41-b791-e2c75441aa25" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-ea9ddb6c-c60c-4860-a315-e21d21888d5c" class="xr-var-data-in" type="checkbox" /><label for="data-ea9ddb6c-c60c-4860-a315-e21d21888d5c" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>Monthly Average of Daily Maximum Near-Surface Air Temperature</dd><dt><span>units :</span></dt><dd>K</dd><dt><span>grid_mapping :</span></dt><dd>crs</dd><dt><span>standard_name :</span></dt><dd>air_temperature</dd><dt><span>height :</span></dt><dd>2 m</dd><dt><span>cell_methods :</span></dt><dd>time: maximum(interval: 24 hours);mean over days</dd><dt><span>_ChunkSizes :</span></dt><dd>[ 10 44 107]</dd></dl></div><div class="xr-var-data"><pre>[544864320 values with dtype=float32]</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-a6e35d10-651f-4a28-9c2f-2d7ff2e6acdd" class="xr-section-summary-in" type="checkbox" /><label for="section-a6e35d10-651f-4a28-9c2f-2d7ff2e6acdd" class="xr-section-summary">Attributes: <span>(46)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"><dt><span>description :</span></dt><dd>Multivariate Adaptive Constructed Analogs (MACA) method, version 2.3,Dec 2013.</dd><dt><span>id :</span></dt><dd>MACAv2-METDATA</dd><dt><span>naming_authority :</span></dt><dd>edu.uidaho.reacch</dd><dt><span>Metadata_Conventions :</span></dt><dd>Unidata Dataset Discovery v1.0</dd><dt><span>Metadata_Link :</span></dt><dd></dd><dt><span>cdm_data_type :</span></dt><dd>FLOAT</dd><dt><span>title :</span></dt><dd>Monthly aggregation of downscaled daily meteorological data of Monthly Average of Daily Maximum Near-Surface Air Temperature from College of Global Change and Earth System Science, Beijing Normal University (BNU-ESM) using the run r1i1p1 of the historical scenario.</dd><dt><span>summary :</span></dt><dd>This archive contains monthly downscaled meteorological and hydrological projections for the Conterminous United States at 1/24-deg resolution. These monthly values are obtained by aggregating the daily values obtained from the downscaling using the Multivariate Adaptive Constructed Analogs (MACA, Abatzoglou, 2012) statistical downscaling method with the METDATA (Abatzoglou,2013) training dataset. The downscaled meteorological variables are maximum/minimum temperature(tasmax/tasmin), maximum/minimum relative humidity (rhsmax/rhsmin),precipitation amount(pr), downward shortwave solar radiation(rsds), eastward wind(uas), northward wind(vas), and specific humidity(huss). The downscaling is based on the 365-day model outputs from different global climate models (GCMs) from Phase 5 of the Coupled Model Inter-comparison Project (CMIP3) utlizing the historical (1950-2005) and future RCP4.5/8.5(2006-2099) scenarios. </dd><dt><span>keywords :</span></dt><dd>monthly, precipitation, maximum temperature, minimum temperature, downward shortwave solar radiation, specific humidity, wind velocity, CMIP5, Gridded Meteorological Data</dd><dt><span>keywords_vocabulary :</span></dt><dd></dd><dt><span>standard_name_vocabulary :</span></dt><dd>CF-1.0</dd><dt><span>history :</span></dt><dd>No revisions.</dd><dt><span>comment :</span></dt><dd></dd><dt><span>geospatial_bounds :</span></dt><dd>POLYGON((-124.7722 25.0631,-124.7722 49.3960, -67.0648 49.3960,-67.0648, 25.0631, -124.7722,25.0631))</dd><dt><span>geospatial_lat_min :</span></dt><dd>25.0631</dd><dt><span>geospatial_lat_max :</span></dt><dd>49.3960</dd><dt><span>geospatial_lon_min :</span></dt><dd>-124.7722</dd><dt><span>geospatial_lon_max :</span></dt><dd>-67.0648</dd><dt><span>geospatial_lat_units :</span></dt><dd>decimal degrees north</dd><dt><span>geospatial_lon_units :</span></dt><dd>decimal degrees east</dd><dt><span>geospatial_lat_resolution :</span></dt><dd>0.0417</dd><dt><span>geospatial_lon_resolution :</span></dt><dd>0.0417</dd><dt><span>geospatial_vertical_min :</span></dt><dd>0.0</dd><dt><span>geospatial_vertical_max :</span></dt><dd>0.0</dd><dt><span>geospatial_vertical_resolution :</span></dt><dd>0.0</dd><dt><span>geospatial_vertical_positive :</span></dt><dd>up</dd><dt><span>time_coverage_start :</span></dt><dd>2000-01-01T00:0</dd><dt><span>time_coverage_end :</span></dt><dd>2004-12-31T00:00</dd><dt><span>time_coverage_duration :</span></dt><dd>P5Y</dd><dt><span>time_coverage_resolution :</span></dt><dd>P1M</dd><dt><span>date_created :</span></dt><dd>2014-05-15</dd><dt><span>date_modified :</span></dt><dd>2014-05-15</dd><dt><span>date_issued :</span></dt><dd>2014-05-15</dd><dt><span>creator_name :</span></dt><dd>John Abatzoglou</dd><dt><span>creator_url :</span></dt><dd>http://maca.northwestknowledge.net</dd><dt><span>creator_email :</span></dt><dd>jabatzoglou@uidaho.edu</dd><dt><span>institution :</span></dt><dd>University of Idaho</dd><dt><span>processing_level :</span></dt><dd>GRID</dd><dt><span>project :</span></dt><dd></dd><dt><span>contributor_name :</span></dt><dd>Katherine C. Hegewisch</dd><dt><span>contributor_role :</span></dt><dd>Postdoctoral Fellow</dd><dt><span>publisher_name :</span></dt><dd>REACCH</dd><dt><span>publisher_email :</span></dt><dd>reacch@uidaho.edu</dd><dt><span>publisher_url :</span></dt><dd>http://www.reacchpna.org/</dd><dt><span>license :</span></dt><dd>Creative Commons CC0 1.0 Universal Dedication(http://creativecommons.org/publicdomain/zero/1.0/legalcode)</dd><dt><span>coordinate_system :</span></dt><dd>WGS84,EPSG:4326</dd></dl></div></li></ul></div></div>
<p>By default <code class="language-plaintext highlighter-rouge">xarray</code> does not handle spatial operations. However, if you
load <code class="language-plaintext highlighter-rouge">rioxarray</code> it adds additional spatial functionality (supported by
<code class="language-plaintext highlighter-rouge">rasterio</code>) that supports handling:</p>
<ul>
<li>coordinate reference systems</li>
<li>reprojection</li>
<li>clipping</li>
</ul>
<p>and more.</p>
<p>These data are spatial and thus have a coordinate reference system
associated with them. Below you grab the <code class="language-plaintext highlighter-rouge">coordinate reference system</code> of the
climate data using the <code class="language-plaintext highlighter-rouge">.rio.crs</code> method that is available
because you have rioxarray loaded in this notebook.</p>
<p class="notice--success"><i class="fa fa-star"></i> <strong>Data Tip:</strong> <code class="language-plaintext highlighter-rouge">max_temp_xr["crs"]</code> will also support accesing the CRS of this dataset however we suggest that you get in the habit of using rioxarray for spatial operations given the native lack of support for spatial data in xarray. Rioxarray wraps around xarray to provide spatial support and is well maintained and supported.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># For later - grab the crs of the data using rioxarray
</span><span class="n">climate_crs</span> <span class="o">=</span> <span class="n">max_temp_xr</span><span class="p">.</span><span class="n">rio</span><span class="p">.</span><span class="n">crs</span>
<span class="n">climate_crs</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>CRS.from_wkt('GEOGCS["undefined",DATUM["undefined",SPHEROID["undefined",6378137,298.257223563]],PRIMEM["undefined",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Longitude",EAST],AXIS["Latitude",NORTH]]')
</code></pre></div></div>
<h3 id="work-with-the-netcdf-data-structure---a-hierarchical-data-format">Work With the NetCDF Data Structure - A Hierarchical Data Format</h3>
<p>The object above is hierarchical and contains metadata making it self-describing.
There are three dimensions to consider when working with this data which represent the
x,y and z dimensions of the data:</p>
<ol>
<li>latitude</li>
<li>longitude</li>
<li>time</li>
</ol>
<p>The latitude and longitude dimension values represent an array containing the point location
value of each pixel in decimal degrees. This information represents the location of each
pixel in your data. The
time array similarly represents the time location for each array in the data cube.
This particular dataset contains historical modeleled monthly max temperature values for the
Continental United States (CONUS).</p>
<p>Begin by exploring your data. What are the min and maximum lat/lon values in the data?</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># View first 5 latitude values
</span><span class="n">max_temp_xr</span><span class="p">[</span><span class="s">"air_temperature"</span><span class="p">][</span><span class="s">"lat"</span><span class="p">].</span><span class="n">values</span><span class="p">[:</span><span class="mi">5</span><span class="p">]</span>
<span class="k">print</span><span class="p">(</span><span class="s">"The min and max latitude values in the data is:"</span><span class="p">,</span>
<span class="n">max_temp_xr</span><span class="p">[</span><span class="s">"air_temperature"</span><span class="p">][</span><span class="s">"lat"</span><span class="p">].</span><span class="n">values</span><span class="p">.</span><span class="nb">min</span><span class="p">(),</span>
<span class="n">max_temp_xr</span><span class="p">[</span><span class="s">"air_temperature"</span><span class="p">][</span><span class="s">"lat"</span><span class="p">].</span><span class="n">values</span><span class="p">.</span><span class="nb">max</span><span class="p">())</span>
<span class="k">print</span><span class="p">(</span><span class="s">"The min and max longitude values in the data is:"</span><span class="p">,</span>
<span class="n">max_temp_xr</span><span class="p">[</span><span class="s">"air_temperature"</span><span class="p">][</span><span class="s">"lon"</span><span class="p">].</span><span class="n">values</span><span class="p">.</span><span class="nb">min</span><span class="p">(),</span>
<span class="n">max_temp_xr</span><span class="p">[</span><span class="s">"air_temperature"</span><span class="p">][</span><span class="s">"lon"</span><span class="p">].</span><span class="n">values</span><span class="p">.</span><span class="nb">max</span><span class="p">())</span>
</code></pre></div></div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>The min and max latitude values in the data is: 25.063077926635742 49.39602279663086
The min and max longitude values in the data is: 235.22784423828125 292.93524169921875
</code></pre></div></div>
<p>What is the date range?</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># View first 5 and last. 5 time values - notice the span of
# dates range from 1950 to 2005
</span><span class="k">print</span><span class="p">(</span><span class="s">"The earliest date in the data is:"</span><span class="p">,</span> <span class="n">max_temp_xr</span><span class="p">[</span><span class="s">"air_temperature"</span><span class="p">][</span><span class="s">"time"</span><span class="p">].</span><span class="n">values</span><span class="p">.</span><span class="nb">min</span><span class="p">())</span>
<span class="k">print</span><span class="p">(</span><span class="s">"The latest date in the data is:"</span><span class="p">,</span> <span class="n">max_temp_xr</span><span class="p">[</span><span class="s">"air_temperature"</span><span class="p">][</span><span class="s">"time"</span><span class="p">].</span><span class="n">values</span><span class="p">.</span><span class="nb">max</span><span class="p">())</span>
</code></pre></div></div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>The earliest date in the data is: 1950-01-15 00:00:00
The latest date in the data is: 2005-12-15 00:00:00
</code></pre></div></div>
<p>What is the shape of the time values in data?</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">max_temp_xr</span><span class="p">[</span><span class="s">"air_temperature"</span><span class="p">][</span><span class="s">"time"</span><span class="p">].</span><span class="n">values</span><span class="p">.</span><span class="n">shape</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>(672,)
</code></pre></div></div>
<p>Time is an additional dimension of this dataset. The value returned above tells you that
you have 672 months worth of data.</p>
<h2 id="hierarchical-formats-are-self-describing">Hierarchical Formats Are Self Describing</h2>
<p>Hierarchical data formats are self-describing. This means that the
metadata for the data are contained within the file itself. <code class="language-plaintext highlighter-rouge">xarray</code>
stores metadata in the <code class="language-plaintext highlighter-rouge">.attrs</code> part of the file structure using a
Python dictionary structure. You can view the metadata using <code class="language-plaintext highlighter-rouge">.attrs</code>.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># View metadata
</span><span class="n">metadata</span> <span class="o">=</span> <span class="n">max_temp_xr</span><span class="p">.</span><span class="n">attrs</span>
<span class="n">metadata</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>{'description': 'Multivariate Adaptive Constructed Analogs (MACA) method, version 2.3,Dec 2013.',
'id': 'MACAv2-METDATA',
'naming_authority': 'edu.uidaho.reacch',
'Metadata_Conventions': 'Unidata Dataset Discovery v1.0',
'Metadata_Link': '',
'cdm_data_type': 'FLOAT',
'title': 'Monthly aggregation of downscaled daily meteorological data of Monthly Average of Daily Maximum Near-Surface Air Temperature from College of Global Change and Earth System Science, Beijing Normal University (BNU-ESM) using the run r1i1p1 of the historical scenario.',
'summary': 'This archive contains monthly downscaled meteorological and hydrological projections for the Conterminous United States at 1/24-deg resolution. These monthly values are obtained by aggregating the daily values obtained from the downscaling using the Multivariate Adaptive Constructed Analogs (MACA, Abatzoglou, 2012) statistical downscaling method with the METDATA (Abatzoglou,2013) training dataset. The downscaled meteorological variables are maximum/minimum temperature(tasmax/tasmin), maximum/minimum relative humidity (rhsmax/rhsmin),precipitation amount(pr), downward shortwave solar radiation(rsds), eastward wind(uas), northward wind(vas), and specific humidity(huss). The downscaling is based on the 365-day model outputs from different global climate models (GCMs) from Phase 5 of the Coupled Model Inter-comparison Project (CMIP3) utlizing the historical (1950-2005) and future RCP4.5/8.5(2006-2099) scenarios. ',
'keywords': 'monthly, precipitation, maximum temperature, minimum temperature, downward shortwave solar radiation, specific humidity, wind velocity, CMIP5, Gridded Meteorological Data',
'keywords_vocabulary': '',
'standard_name_vocabulary': 'CF-1.0',
'history': 'No revisions.',
'comment': '',
'geospatial_bounds': 'POLYGON((-124.7722 25.0631,-124.7722 49.3960, -67.0648 49.3960,-67.0648, 25.0631, -124.7722,25.0631))',
'geospatial_lat_min': '25.0631',
'geospatial_lat_max': '49.3960',
'geospatial_lon_min': '-124.7722',
'geospatial_lon_max': '-67.0648',
'geospatial_lat_units': 'decimal degrees north',
'geospatial_lon_units': 'decimal degrees east',
'geospatial_lat_resolution': '0.0417',
'geospatial_lon_resolution': '0.0417',
'geospatial_vertical_min': 0.0,
'geospatial_vertical_max': 0.0,
'geospatial_vertical_resolution': 0.0,
'geospatial_vertical_positive': 'up',
'time_coverage_start': '2000-01-01T00:0',
'time_coverage_end': '2004-12-31T00:00',
'time_coverage_duration': 'P5Y',
'time_coverage_resolution': 'P1M',
'date_created': '2014-05-15',
'date_modified': '2014-05-15',
'date_issued': '2014-05-15',
'creator_name': 'John Abatzoglou',
'creator_url': 'http://maca.northwestknowledge.net',
'creator_email': 'jabatzoglou@uidaho.edu',
'institution': 'University of Idaho',
'processing_level': 'GRID',
'project': '',
'contributor_name': 'Katherine C. Hegewisch',
'contributor_role': 'Postdoctoral Fellow',
'publisher_name': 'REACCH',
'publisher_email': 'reacch@uidaho.edu',
'publisher_url': 'http://www.reacchpna.org/',
'license': 'Creative Commons CC0 1.0 Universal Dedication(http://creativecommons.org/publicdomain/zero/1.0/legalcode)',
'coordinate_system': 'WGS84,EPSG:4326'}
</code></pre></div></div>
<p>Above you grabbed the metadata for this data which is returned in a Python dictionary format.
Below you view the “title” of the dataset by grabbing the <code class="language-plaintext highlighter-rouge">title</code> key from the dictionary.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># View data title
</span><span class="n">metadata</span><span class="p">[</span><span class="s">"title"</span><span class="p">]</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>'Monthly aggregation of downscaled daily meteorological data of Monthly Average of Daily Maximum Near-Surface Air Temperature from College of Global Change and Earth System Science, Beijing Normal University (BNU-ESM) using the run r1i1p1 of the historical scenario.'
</code></pre></div></div>
<h2 id="subsetting-or-slicing-your-data">Subsetting or “Slicing” Your Data</h2>
<p>You can quickly and efficiently slice and subset your data using
<code class="language-plaintext highlighter-rouge">xarray</code>. Below, you will learn how to slice the data using the <code class="language-plaintext highlighter-rouge">.sel()</code> method.</p>
<p>This will return exactly two data points at the location - the temperature
value for each day in your temporal slice.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Select a single x,y combination from the data
</span><span class="n">key</span><span class="o">=</span><span class="mi">400</span>
<span class="n">longitude</span> <span class="o">=</span> <span class="n">max_temp_xr</span><span class="p">[</span><span class="s">"air_temperature"</span><span class="p">][</span><span class="s">"lon"</span><span class="p">].</span><span class="n">values</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
<span class="n">latitude</span> <span class="o">=</span> <span class="n">max_temp_xr</span><span class="p">[</span><span class="s">"air_temperature"</span><span class="p">][</span><span class="s">"lat"</span><span class="p">].</span><span class="n">values</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
<span class="k">print</span><span class="p">(</span><span class="s">"Long, Lat values:"</span><span class="p">,</span> <span class="n">longitude</span><span class="p">,</span> <span class="n">latitude</span><span class="p">)</span>
</code></pre></div></div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Long, Lat values: 251.89422607421875 41.72947692871094
</code></pre></div></div>
<p><strong>This step is optional.</strong></p>
<p>Below, you can see the x, y location that for the latitude/longitude
location that you will use to slice the data above plotted on a map.
The code below is an example of creating a spatial plot using
the <code class="language-plaintext highlighter-rouge">cartopy</code> package that shows the location that you selected.</p>
<p>Notie that you need to subtract 360 from the longitude value. This is because
the range of values in this data are 0-360 rather than +/- 0-180 given the CRS
of the data.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Create a spatial map of your selected location with cartopy
</span>
<span class="c1"># Set the spatial extent to cover the CONUS (Continental United States)
</span><span class="n">extent</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="mi">120</span><span class="p">,</span> <span class="o">-</span><span class="mi">70</span><span class="p">,</span> <span class="mi">24</span><span class="p">,</span> <span class="mf">50.5</span><span class="p">]</span>
<span class="n">central_lon</span> <span class="o">=</span> <span class="n">np</span><span class="p">.</span><span class="n">mean</span><span class="p">(</span><span class="n">extent</span><span class="p">[:</span><span class="mi">2</span><span class="p">])</span>
<span class="n">central_lat</span> <span class="o">=</span> <span class="n">np</span><span class="p">.</span><span class="n">mean</span><span class="p">(</span><span class="n">extent</span><span class="p">[</span><span class="mi">2</span><span class="p">:])</span>
<span class="c1"># Create your figure and axis object
# Albers equal area is a common CRS used to make maps of the United States
</span><span class="n">f</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="p">.</span><span class="n">subplots</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">12</span><span class="p">,</span> <span class="mi">6</span><span class="p">),</span>
<span class="n">subplot_kw</span><span class="o">=</span><span class="p">{</span><span class="s">'projection'</span><span class="p">:</span> <span class="n">ccrs</span><span class="p">.</span><span class="n">AlbersEqualArea</span><span class="p">(</span><span class="n">central_lon</span><span class="p">,</span> <span class="n">central_lat</span><span class="p">)})</span>
<span class="n">ax</span><span class="p">.</span><span class="n">coastlines</span><span class="p">()</span>
<span class="c1"># Plot the selected location
</span><span class="n">ax</span><span class="p">.</span><span class="n">plot</span><span class="p">(</span><span class="n">longitude</span><span class="o">-</span><span class="mi">360</span><span class="p">,</span> <span class="n">latitude</span><span class="p">,</span>
<span class="s">'*'</span><span class="p">,</span>
<span class="n">transform</span><span class="o">=</span><span class="n">ccrs</span><span class="p">.</span><span class="n">PlateCarree</span><span class="p">(),</span>
<span class="n">color</span><span class="o">=</span><span class="s">"purple"</span><span class="p">,</span>
<span class="n">markersize</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
<span class="n">ax</span><span class="p">.</span><span class="n">set_extent</span><span class="p">(</span><span class="n">extent</span><span class="p">)</span>
<span class="n">ax</span><span class="p">.</span><span class="nb">set</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s">"Location of the Latitude / Longitude Being Used To to Slice Your netcdf Climate Data File"</span><span class="p">)</span>
<span class="c1"># Adds continent boundaries to the map
</span><span class="n">ax</span><span class="p">.</span><span class="n">add_feature</span><span class="p">(</span><span class="n">cfeature</span><span class="p">.</span><span class="n">LAND</span><span class="p">,</span> <span class="n">edgecolor</span><span class="o">=</span><span class="s">'black'</span><span class="p">)</span>
<span class="n">ax</span><span class="p">.</span><span class="n">gridlines</span><span class="p">()</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>/opt/conda/envs/EDS/lib/python3.8/site-packages/cartopy/io/__init__.py:241: DownloadWarning: Downloading: https://naturalearth.s3.amazonaws.com/50m_physical/ne_50m_land.zip
warnings.warn(f'Downloading: {url}', DownloadWarning)
/opt/conda/envs/EDS/lib/python3.8/site-packages/cartopy/io/__init__.py:241: DownloadWarning: Downloading: https://naturalearth.s3.amazonaws.com/50m_physical/ne_50m_coastline.zip
warnings.warn(f'Downloading: {url}', DownloadWarning)
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-04-open-netcdf-climate-data/2020-10-16-netcdf-04-open-netcdf-climate-data_24_1.png" />
</figure>
<h2 id="subset-your-data">Subset Your Data</h2>
<p>Now it is time to subset the data for the point location that
you are interested in studying (the lat / lon value that you plotted above).
You can slice the data for one single latitude, longitude location using
the <code class="language-plaintext highlighter-rouge">.sel()</code> method.</p>
<p>When you select the data using one single point, your output for every
time step in the data will be a single pixel
value representing max temperature. For this monthly data this means that
you will have one pixel value for every month in the data.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Slice the data spatially using a single lat/lon point
</span><span class="n">one_point</span> <span class="o">=</span> <span class="n">max_temp_xr</span><span class="p">[</span><span class="s">"air_temperature"</span><span class="p">].</span><span class="n">sel</span><span class="p">(</span><span class="n">lat</span><span class="o">=</span><span class="n">latitude</span><span class="p">,</span>
<span class="n">lon</span><span class="o">=</span><span class="n">longitude</span><span class="p">)</span>
<span class="n">one_point</span>
</code></pre></div></div>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.DataArray 'air_temperature' (time: 672)>
array([271.11615, 274.05585, 279.538 , ..., 286.85074, 279.6746 , 271.4411 ],
dtype=float32)
Coordinates:
lat float64 41.73
lon float64 251.9
* time (time) object 1950-01-15 00:00:00 ... 2005-12-15 00:00:00
Attributes:
long_name: Monthly Average of Daily Maximum Near-Surface Air Tempera...
units: K
grid_mapping: crs
standard_name: air_temperature
height: 2 m
cell_methods: time: maximum(interval: 24 hours);mean over days
_ChunkSizes: [ 10 44 107]</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.DataArray</div><div class="xr-array-name">'air_temperature'</div><ul class="xr-dim-list"><li><span class="xr-has-index">time</span>: 672</li></ul></div><ul class="xr-sections"><li class="xr-section-item"><div class="xr-array-wrap"><input id="section-7b34e906-b29b-4a0f-8126-76c5e88f8d03" class="xr-array-in" type="checkbox" checked="" /><label for="section-7b34e906-b29b-4a0f-8126-76c5e88f8d03" title="Show/hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-array-preview xr-preview"><span>271.1 274.1 279.5 284.4 294.1 298.4 ... 301.5 294.3 286.9 279.7 271.4</span></div><div class="xr-array-data"><pre>array([271.11615, 274.05585, 279.538 , ..., 286.85074, 279.6746 , 271.4411 ],
dtype=float32)</pre></div></div></li><li class="xr-section-item"><input id="section-dcec770e-b908-4afa-80df-4ca891147dc6" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-dcec770e-b908-4afa-80df-4ca891147dc6" class="xr-section-summary">Coordinates: <span>(3)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span>lat</span></div><div class="xr-var-dims">()</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">41.73</div><input id="attrs-5be65744-856f-4bb9-a544-8b5d46c7f0d1" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-5be65744-856f-4bb9-a544-8b5d46c7f0d1" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-b2183107-d2b3-42ce-9222-873d39eaaa9d" class="xr-var-data-in" type="checkbox" /><label for="data-b2183107-d2b3-42ce-9222-873d39eaaa9d" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>latitude</dd><dt><span>standard_name :</span></dt><dd>latitude</dd><dt><span>units :</span></dt><dd>degrees_north</dd><dt><span>axis :</span></dt><dd>Y</dd><dt><span>description :</span></dt><dd>Latitude of the center of the grid cell</dd></dl></div><div class="xr-var-data"><pre>array(41.72947693)</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span>lon</span></div><div class="xr-var-dims">()</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">251.9</div><input id="attrs-a92a19fd-e583-484d-b3cc-58046076de8b" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-a92a19fd-e583-484d-b3cc-58046076de8b" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-0e616545-88ba-4b6b-aedf-b68b2aa1709f" class="xr-var-data-in" type="checkbox" /><label for="data-0e616545-88ba-4b6b-aedf-b68b2aa1709f" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>units :</span></dt><dd>degrees_east</dd><dt><span>description :</span></dt><dd>Longitude of the center of the grid cell</dd><dt><span>long_name :</span></dt><dd>longitude</dd><dt><span>standard_name :</span></dt><dd>longitude</dd><dt><span>axis :</span></dt><dd>X</dd></dl></div><div class="xr-var-data"><pre>array(251.89422607)</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">time</span></div><div class="xr-var-dims">(time)</div><div class="xr-var-dtype">object</div><div class="xr-var-preview xr-preview">1950-01-15 00:00:00 ... 2005-12-...</div><input id="attrs-fccf3751-24cc-439f-9c87-7051056c374a" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-fccf3751-24cc-439f-9c87-7051056c374a" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-e74ee8f0-b4bd-4235-9321-bd03175db1f6" class="xr-var-data-in" type="checkbox" /><label for="data-e74ee8f0-b4bd-4235-9321-bd03175db1f6" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>description :</span></dt><dd>days since 1900-01-01</dd></dl></div><div class="xr-var-data"><pre>array([cftime.DatetimeNoLeap(1950, 1, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(1950, 2, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(1950, 3, 15, 0, 0, 0, 0, has_year_zero=True), ...,
cftime.DatetimeNoLeap(2005, 10, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2005, 11, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2005, 12, 15, 0, 0, 0, 0, has_year_zero=True)],
dtype=object)</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-d555c2c2-33d6-4437-83a9-40cc2ec0de73" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-d555c2c2-33d6-4437-83a9-40cc2ec0de73" class="xr-section-summary">Attributes: <span>(7)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>Monthly Average of Daily Maximum Near-Surface Air Temperature</dd><dt><span>units :</span></dt><dd>K</dd><dt><span>grid_mapping :</span></dt><dd>crs</dd><dt><span>standard_name :</span></dt><dd>air_temperature</dd><dt><span>height :</span></dt><dd>2 m</dd><dt><span>cell_methods :</span></dt><dd>time: maximum(interval: 24 hours);mean over days</dd><dt><span>_ChunkSizes :</span></dt><dd>[ 10 44 107]</dd></dl></div></li></ul></div></div>
<p>When you slice the data by a single point, notice that output data
only has a single array of values. In this case these values represent
air temperature (in K) over time.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Notice the shape of the output array
</span><span class="n">one_point</span><span class="p">.</span><span class="n">shape</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>(672,)
</code></pre></div></div>
<p>The data stored in the xarray object is a numpy array. You can process
the data in the same way you would process any other numpy array.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># View the first 5 values for that single point
</span><span class="n">one_point</span><span class="p">.</span><span class="n">values</span><span class="p">[:</span><span class="mi">5</span><span class="p">]</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>array([271.11615, 274.05585, 279.538 , 284.42365, 294.1337 ],
dtype=float32)
</code></pre></div></div>
<h3 id="plot-a-time-series-for-a-single-location">Plot A Time Series For a Single Location</h3>
<p>Above you used a single point location to slice your data. Because
the data are for only one location, but over time, you can quickly
create a scatterplot of the data using <code class="language-plaintext highlighter-rouge">objectname.plot()</code>.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Use xarray to create a quick time series plot
</span><span class="n">one_point</span><span class="p">.</span><span class="n">plot</span><span class="p">.</span><span class="n">line</span><span class="p">()</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-04-open-netcdf-climate-data/2020-10-16-netcdf-04-open-netcdf-climate-data_32_0.png" alt="Standard plot output from xarray .plot() with no plot elements customized. You can make this plot look much nicer by customizing the line and point markers and colors." />
<figcaption>Standard plot output from xarray .plot() with no plot elements customized. You can make this plot look much nicer by customizing the line and point markers and colors.</figcaption>
</figure>
<p>You can make the plot a bit prettier if you’d like using the
standard Python matplotlib plot parameters. Below you change the marker color
to purple and the lines to grey. <code class="language-plaintext highlighter-rouge">figsize</code> is used to adjust the
size of the plot.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># You can clean up your plot as you wish using standard matplotlib approaches
</span><span class="n">f</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="p">.</span><span class="n">subplots</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">12</span><span class="p">,</span> <span class="mi">6</span><span class="p">))</span>
<span class="n">one_point</span><span class="p">.</span><span class="n">plot</span><span class="p">.</span><span class="n">line</span><span class="p">(</span><span class="n">hue</span><span class="o">=</span><span class="s">'lat'</span><span class="p">,</span>
<span class="n">marker</span><span class="o">=</span><span class="s">"o"</span><span class="p">,</span>
<span class="n">ax</span><span class="o">=</span><span class="n">ax</span><span class="p">,</span>
<span class="n">color</span><span class="o">=</span><span class="s">"grey"</span><span class="p">,</span>
<span class="n">markerfacecolor</span><span class="o">=</span><span class="s">"purple"</span><span class="p">,</span>
<span class="n">markeredgecolor</span><span class="o">=</span><span class="s">"purple"</span><span class="p">)</span>
<span class="n">ax</span><span class="p">.</span><span class="nb">set</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s">"Time Series For a Single Lat / Lon Location"</span><span class="p">)</span>
<span class="c1"># Uncomment the line below if you wish to export the figure as a .png file
# plt.savefig("single_point_timeseries.png")
</span><span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-04-open-netcdf-climate-data/2020-10-16-netcdf-04-open-netcdf-climate-data_34_0.png" alt="Plot showing historic max temperature climate data plotted using xarray .plot()." />
<figcaption>Plot showing historic max temperature climate data plotted using xarray .plot().</figcaption>
</figure>
<h3 id="convert-climate-time-series-data-to-a-pandas-dataframe--export-to-a-csv-file">Convert Climate Time Series Data to a Pandas DataFrame & Export to a .csv File</h3>
<p>You can convert your data to a <code class="language-plaintext highlighter-rouge">DataFrame</code> and export to
a <code class="language-plaintext highlighter-rouge">.csv</code> file using <code class="language-plaintext highlighter-rouge">to_dataframe()</code> and <code class="language-plaintext highlighter-rouge">to_csv()</code>.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Convert to dataframe -- then this can easily be exported to a csv
</span><span class="n">one_point_df</span> <span class="o">=</span> <span class="n">one_point</span><span class="p">.</span><span class="n">to_dataframe</span><span class="p">()</span>
<span class="c1"># View just the first 5 rows of the data
</span><span class="n">one_point_df</span><span class="p">.</span><span class="n">head</span><span class="p">()</span>
</code></pre></div></div>
<div class="output execute_result">
<style scoped="">
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>lat</th>
<th>lon</th>
<th>air_temperature</th>
</tr>
<tr>
<th>time</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>1950-01-15 00:00:00</th>
<td>41.729477</td>
<td>251.894226</td>
<td>271.116150</td>
</tr>
<tr>
<th>1950-02-15 00:00:00</th>
<td>41.729477</td>
<td>251.894226</td>
<td>274.055847</td>
</tr>
<tr>
<th>1950-03-15 00:00:00</th>
<td>41.729477</td>
<td>251.894226</td>
<td>279.537994</td>
</tr>
<tr>
<th>1950-04-15 00:00:00</th>
<td>41.729477</td>
<td>251.894226</td>
<td>284.423645</td>
</tr>
<tr>
<th>1950-05-15 00:00:00</th>
<td>41.729477</td>
<td>251.894226</td>
<td>294.133698</td>
</tr>
</tbody>
</table>
</div>
<p>Once you have a dataframe object, you can export it
directly to a <code class="language-plaintext highlighter-rouge">.csv</code> file format.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Export data to .csv file
</span><span class="n">one_point_df</span><span class="p">.</span><span class="n">to_csv</span><span class="p">(</span><span class="s">"one-location.csv"</span><span class="p">)</span>
</code></pre></div></div>
<h2 id="slice-climate-macav2-data-by-time-and-location">Slice Climate MACAv2 Data By Time and Location</h2>
<p>Above you sliced the data by spatial location, selecting only one point
location at a specific latitude and longitude. You can also slice the data by time.
Below, you slice the data at the selected lat / long location and for a 5-year time period
in the year 2000.</p>
<p>Notice that the output shape is 60. 60 represents 12 months a year over 5 years
for the selected lat / lon location. Again because you are slicing out data
for a single point location, you have a total of 60 data points in the output
numpy array.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">start_date</span> <span class="o">=</span> <span class="s">"2000-01-01"</span>
<span class="n">end_date</span> <span class="o">=</span> <span class="s">"2005-01-01"</span>
<span class="n">temp_2000_2005</span> <span class="o">=</span> <span class="n">max_temp_xr</span><span class="p">[</span><span class="s">"air_temperature"</span><span class="p">].</span><span class="n">sel</span><span class="p">(</span><span class="n">time</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="n">start_date</span><span class="p">,</span> <span class="n">end_date</span><span class="p">),</span>
<span class="n">lat</span><span class="o">=</span><span class="mf">45.02109146118164</span><span class="p">,</span>
<span class="n">lon</span><span class="o">=</span><span class="mf">243.01937866210938</span><span class="p">)</span>
<span class="n">temp_2000_2005</span>
</code></pre></div></div>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.DataArray 'air_temperature' (time: 60)>
array([273.74884, 277.1434 , 280.0702 , 280.6042 , 289.23993, 290.86853,
297.70242, 301.36722, 296.4382 , 291.29434, 275.8434 , 273.0037 ,
275.154 , 278.77863, 282.29105, 283.87787, 290.53387, 296.701 ,
297.4607 , 301.16116, 295.5421 , 287.3863 , 276.85397, 274.35953,
278.0117 , 275.68024, 282.12674, 286.41693, 292.4094 , 297.30606,
300.84628, 302.10593, 288.83698, 288.44 , 276.38992, 276.5348 ,
275.86145, 276.5708 , 280.8476 , 284.35712, 292.14087, 293.66147,
301.09534, 302.7656 , 294.99573, 287.5425 , 277.80093, 273.25894,
274.09232, 276.94736, 280.25446, 283.47614, 289.63776, 290.5187 ,
301.8519 , 300.40402, 290.73633, 289.60886, 277.12424, 275.7836 ],
dtype=float32)
Coordinates:
lat float64 45.02
lon float64 243.0
* time (time) object 2000-01-15 00:00:00 ... 2004-12-15 00:00:00
Attributes:
long_name: Monthly Average of Daily Maximum Near-Surface Air Tempera...
units: K
grid_mapping: crs
standard_name: air_temperature
height: 2 m
cell_methods: time: maximum(interval: 24 hours);mean over days
_ChunkSizes: [ 10 44 107]</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.DataArray</div><div class="xr-array-name">'air_temperature'</div><ul class="xr-dim-list"><li><span class="xr-has-index">time</span>: 60</li></ul></div><ul class="xr-sections"><li class="xr-section-item"><div class="xr-array-wrap"><input id="section-acdd7961-e48d-4d7b-9608-7df3489207dc" class="xr-array-in" type="checkbox" checked="" /><label for="section-acdd7961-e48d-4d7b-9608-7df3489207dc" title="Show/hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-array-preview xr-preview"><span>273.7 277.1 280.1 280.6 289.2 290.9 ... 300.4 290.7 289.6 277.1 275.8</span></div><div class="xr-array-data"><pre>array([273.74884, 277.1434 , 280.0702 , 280.6042 , 289.23993, 290.86853,
297.70242, 301.36722, 296.4382 , 291.29434, 275.8434 , 273.0037 ,
275.154 , 278.77863, 282.29105, 283.87787, 290.53387, 296.701 ,
297.4607 , 301.16116, 295.5421 , 287.3863 , 276.85397, 274.35953,
278.0117 , 275.68024, 282.12674, 286.41693, 292.4094 , 297.30606,
300.84628, 302.10593, 288.83698, 288.44 , 276.38992, 276.5348 ,
275.86145, 276.5708 , 280.8476 , 284.35712, 292.14087, 293.66147,
301.09534, 302.7656 , 294.99573, 287.5425 , 277.80093, 273.25894,
274.09232, 276.94736, 280.25446, 283.47614, 289.63776, 290.5187 ,
301.8519 , 300.40402, 290.73633, 289.60886, 277.12424, 275.7836 ],
dtype=float32)</pre></div></div></li><li class="xr-section-item"><input id="section-4788e5b9-1b64-4f25-8ae9-ae2a290437cd" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-4788e5b9-1b64-4f25-8ae9-ae2a290437cd" class="xr-section-summary">Coordinates: <span>(3)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span>lat</span></div><div class="xr-var-dims">()</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">45.02</div><input id="attrs-1f4e878f-fa26-41ad-b3ee-2d005a428103" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-1f4e878f-fa26-41ad-b3ee-2d005a428103" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-26c7bfe0-91a1-4145-870b-7011c5e5f537" class="xr-var-data-in" type="checkbox" /><label for="data-26c7bfe0-91a1-4145-870b-7011c5e5f537" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>latitude</dd><dt><span>standard_name :</span></dt><dd>latitude</dd><dt><span>units :</span></dt><dd>degrees_north</dd><dt><span>axis :</span></dt><dd>Y</dd><dt><span>description :</span></dt><dd>Latitude of the center of the grid cell</dd></dl></div><div class="xr-var-data"><pre>array(45.02109146)</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span>lon</span></div><div class="xr-var-dims">()</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">243.0</div><input id="attrs-8e6e02d1-fd93-4bb8-b1af-f5f09138f5b5" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-8e6e02d1-fd93-4bb8-b1af-f5f09138f5b5" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-e9d5c23f-3059-447e-8064-34fe5bcffbb0" class="xr-var-data-in" type="checkbox" /><label for="data-e9d5c23f-3059-447e-8064-34fe5bcffbb0" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>units :</span></dt><dd>degrees_east</dd><dt><span>description :</span></dt><dd>Longitude of the center of the grid cell</dd><dt><span>long_name :</span></dt><dd>longitude</dd><dt><span>standard_name :</span></dt><dd>longitude</dd><dt><span>axis :</span></dt><dd>X</dd></dl></div><div class="xr-var-data"><pre>array(243.01937866)</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">time</span></div><div class="xr-var-dims">(time)</div><div class="xr-var-dtype">object</div><div class="xr-var-preview xr-preview">2000-01-15 00:00:00 ... 2004-12-...</div><input id="attrs-529a0952-7025-4590-8cd3-59543368dc68" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-529a0952-7025-4590-8cd3-59543368dc68" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-4380f863-e24c-4afe-b74c-2a2797fac183" class="xr-var-data-in" type="checkbox" /><label for="data-4380f863-e24c-4afe-b74c-2a2797fac183" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>description :</span></dt><dd>days since 1900-01-01</dd></dl></div><div class="xr-var-data"><pre>array([cftime.DatetimeNoLeap(2000, 1, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2000, 2, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2000, 3, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2000, 4, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2000, 5, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2000, 6, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2000, 7, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2000, 8, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2000, 9, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2000, 10, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2000, 11, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2000, 12, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2001, 1, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2001, 2, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2001, 3, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2001, 4, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2001, 5, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2001, 6, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2001, 7, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2001, 8, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2001, 9, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2001, 10, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2001, 11, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2001, 12, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2002, 1, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2002, 2, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2002, 3, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2002, 4, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2002, 5, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2002, 6, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2002, 7, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2002, 8, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2002, 9, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2002, 10, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2002, 11, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2002, 12, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2003, 1, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2003, 2, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2003, 3, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2003, 4, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2003, 5, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2003, 6, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2003, 7, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2003, 8, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2003, 9, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2003, 10, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2003, 11, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2003, 12, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2004, 1, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2004, 2, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2004, 3, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2004, 4, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2004, 5, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2004, 6, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2004, 7, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2004, 8, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2004, 9, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2004, 10, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2004, 11, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2004, 12, 15, 0, 0, 0, 0, has_year_zero=True)],
dtype=object)</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-fb70dc1d-4ae2-451c-b2d0-310575b53035" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-fb70dc1d-4ae2-451c-b2d0-310575b53035" class="xr-section-summary">Attributes: <span>(7)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>Monthly Average of Daily Maximum Near-Surface Air Temperature</dd><dt><span>units :</span></dt><dd>K</dd><dt><span>grid_mapping :</span></dt><dd>crs</dd><dt><span>standard_name :</span></dt><dd>air_temperature</dd><dt><span>height :</span></dt><dd>2 m</dd><dt><span>cell_methods :</span></dt><dd>time: maximum(interval: 24 hours);mean over days</dd><dt><span>_ChunkSizes :</span></dt><dd>[ 10 44 107]</dd></dl></div></li></ul></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">temp_2000_2005</span><span class="p">.</span><span class="n">shape</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>(60,)
</code></pre></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># View the 60 data points (raster cell values) associated with the spatial and temporal subset
</span><span class="n">temp_2000_2005</span><span class="p">.</span><span class="n">values</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>array([273.74884, 277.1434 , 280.0702 , 280.6042 , 289.23993, 290.86853,
297.70242, 301.36722, 296.4382 , 291.29434, 275.8434 , 273.0037 ,
275.154 , 278.77863, 282.29105, 283.87787, 290.53387, 296.701 ,
297.4607 , 301.16116, 295.5421 , 287.3863 , 276.85397, 274.35953,
278.0117 , 275.68024, 282.12674, 286.41693, 292.4094 , 297.30606,
300.84628, 302.10593, 288.83698, 288.44 , 276.38992, 276.5348 ,
275.86145, 276.5708 , 280.8476 , 284.35712, 292.14087, 293.66147,
301.09534, 302.7656 , 294.99573, 287.5425 , 277.80093, 273.25894,
274.09232, 276.94736, 280.25446, 283.47614, 289.63776, 290.5187 ,
301.8519 , 300.40402, 290.73633, 289.60886, 277.12424, 275.7836 ],
dtype=float32)
</code></pre></div></div>
<p>Notice that in this instance you have much less data for that specific point
(5 years worth of data to be exact).</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Plot the data just like you did above
</span><span class="n">f</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="p">.</span><span class="n">subplots</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">12</span><span class="p">,</span> <span class="mi">6</span><span class="p">))</span>
<span class="n">temp_2000_2005</span><span class="p">.</span><span class="n">plot</span><span class="p">.</span><span class="n">line</span><span class="p">(</span><span class="n">hue</span><span class="o">=</span><span class="s">'lat'</span><span class="p">,</span>
<span class="n">marker</span><span class="o">=</span><span class="s">"o"</span><span class="p">,</span>
<span class="n">ax</span><span class="o">=</span><span class="n">ax</span><span class="p">,</span>
<span class="n">color</span><span class="o">=</span><span class="s">"grey"</span><span class="p">,</span>
<span class="n">markerfacecolor</span><span class="o">=</span><span class="s">"purple"</span><span class="p">,</span>
<span class="n">markeredgecolor</span><span class="o">=</span><span class="s">"purple"</span><span class="p">)</span>
<span class="n">ax</span><span class="p">.</span><span class="nb">set</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s">"A 5 Year Time Series of Temperature Data For A Single Location"</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-04-open-netcdf-climate-data/2020-10-16-netcdf-04-open-netcdf-climate-data_44_0.png" alt="Plot showing 5 years of monthly max temperature data. Here the data are cleaned up a bit with marker colors and lines adjusted." />
<figcaption>Plot showing 5 years of monthly max temperature data. Here the data are cleaned up a bit with marker colors and lines adjusted.</figcaption>
</figure>
<h2 id="convert-subsetted-to-a-dataframe--export-to-a-csv-file">Convert Subsetted to a DataFrame & Export to a .csv File</h2>
<p>Similar to what you did above, you can also convert your data to a DataFrame. Once your data are in a
DataFrame format, you can quickly export a <code class="language-plaintext highlighter-rouge">.csv</code> file.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Convert to dataframe -- then this can be exported to a csv if you want that
</span><span class="n">temp_2000_2005_df</span> <span class="o">=</span> <span class="n">temp_2000_2005</span><span class="p">.</span><span class="n">to_dataframe</span><span class="p">()</span>
<span class="c1"># View just the first 5 rows of the data
</span><span class="n">temp_2000_2005_df</span><span class="p">.</span><span class="n">head</span><span class="p">()</span>
</code></pre></div></div>
<div class="output execute_result">
<style scoped="">
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>lat</th>
<th>lon</th>
<th>air_temperature</th>
</tr>
<tr>
<th>time</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>2000-01-15 00:00:00</th>
<td>45.021091</td>
<td>243.019379</td>
<td>273.748840</td>
</tr>
<tr>
<th>2000-02-15 00:00:00</th>
<td>45.021091</td>
<td>243.019379</td>
<td>277.143402</td>
</tr>
<tr>
<th>2000-03-15 00:00:00</th>
<td>45.021091</td>
<td>243.019379</td>
<td>280.070190</td>
</tr>
<tr>
<th>2000-04-15 00:00:00</th>
<td>45.021091</td>
<td>243.019379</td>
<td>280.604187</td>
</tr>
<tr>
<th>2000-05-15 00:00:00</th>
<td>45.021091</td>
<td>243.019379</td>
<td>289.239929</td>
</tr>
</tbody>
</table>
</div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Create filename from subset info
</span><span class="n">file_name</span> <span class="o">=</span> <span class="s">"monthly-temp-"</span> <span class="o">+</span> <span class="n">start_date</span> <span class="o">+</span> <span class="s">"-"</span> <span class="o">+</span> <span class="n">end_date</span> <span class="o">+</span> <span class="s">".csv"</span>
<span class="n">file_name</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>'monthly-temp-2000-01-01-2005-01-01.csv'
</code></pre></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Export to a csv file to share with your friends!
</span><span class="n">temp_2000_2005_df</span><span class="p">.</span><span class="n">to_csv</span><span class="p">(</span><span class="n">file_name</span><span class="p">)</span>
</code></pre></div></div>
<h2 id="slice-the-data-across-a-spatial-extent-for-a-specific-time-period">Slice The Data Across a Spatial Extent For A Specific Time Period</h2>
<p>In the above example you sliced out a subset of the data for a specific
point location over time. Below you select
data for the entire CONUS (Continental United States) study area and analyze it
as a spatial raster object.</p>
<p>Once again you use <code class="language-plaintext highlighter-rouge">.sel()</code> combined with <code class="language-plaintext highlighter-rouge">slice()</code> subset the data.</p>
<p>Notice below that if you don’t specify the lat and lon extent,
it will by default return all of the pixels available in the data for the
specified time period.</p>
<p>Notice that you are plotting the entire spatial extent of the data which is
in this case called CONUS (<strong>CON</strong>tinental <strong>U</strong>nited <strong>S</strong>tates)</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">start_date</span> <span class="o">=</span> <span class="s">"1950-01-15"</span>
<span class="n">end_date</span> <span class="o">=</span> <span class="s">"1950-02-15"</span>
<span class="n">two_months_conus</span> <span class="o">=</span> <span class="n">max_temp_xr</span><span class="p">[</span><span class="s">"air_temperature"</span><span class="p">].</span><span class="n">sel</span><span class="p">(</span>
<span class="n">time</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="n">start_date</span><span class="p">,</span> <span class="n">end_date</span><span class="p">))</span>
<span class="c1"># Notice that time has a value of **2** below representing two time steps or months worth of data
</span><span class="n">two_months_conus</span>
</code></pre></div></div>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.DataArray 'air_temperature' (time: 2, lat: 585, lon: 1386)>
[1621620 values with dtype=float32]
Coordinates:
* lat (lat) float64 25.06 25.1 25.15 25.19 ... 49.27 49.31 49.35 49.4
* lon (lon) float64 235.2 235.3 235.3 235.4 ... 292.8 292.9 292.9 292.9
* time (time) object 1950-01-15 00:00:00 1950-02-15 00:00:00
Attributes:
long_name: Monthly Average of Daily Maximum Near-Surface Air Tempera...
units: K
grid_mapping: crs
standard_name: air_temperature
height: 2 m
cell_methods: time: maximum(interval: 24 hours);mean over days
_ChunkSizes: [ 10 44 107]</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.DataArray</div><div class="xr-array-name">'air_temperature'</div><ul class="xr-dim-list"><li><span class="xr-has-index">time</span>: 2</li><li><span class="xr-has-index">lat</span>: 585</li><li><span class="xr-has-index">lon</span>: 1386</li></ul></div><ul class="xr-sections"><li class="xr-section-item"><div class="xr-array-wrap"><input id="section-e6a1d808-401c-47f4-8860-c0b7efb2ca40" class="xr-array-in" type="checkbox" checked="" /><label for="section-e6a1d808-401c-47f4-8860-c0b7efb2ca40" title="Show/hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-array-preview xr-preview"><span>...</span></div><div class="xr-array-data"><pre>[1621620 values with dtype=float32]</pre></div></div></li><li class="xr-section-item"><input id="section-f49931b2-f01f-4e67-b827-ec74c1a0afd5" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-f49931b2-f01f-4e67-b827-ec74c1a0afd5" class="xr-section-summary">Coordinates: <span>(3)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lat</span></div><div class="xr-var-dims">(lat)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">25.06 25.1 25.15 ... 49.35 49.4</div><input id="attrs-88bd0ac8-f2b1-464a-93ce-5a9c5004e146" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-88bd0ac8-f2b1-464a-93ce-5a9c5004e146" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-5f3ce023-667e-428a-b855-fb7ece430220" class="xr-var-data-in" type="checkbox" /><label for="data-5f3ce023-667e-428a-b855-fb7ece430220" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>latitude</dd><dt><span>standard_name :</span></dt><dd>latitude</dd><dt><span>units :</span></dt><dd>degrees_north</dd><dt><span>axis :</span></dt><dd>Y</dd><dt><span>description :</span></dt><dd>Latitude of the center of the grid cell</dd></dl></div><div class="xr-var-data"><pre>array([25.063078, 25.104744, 25.14641 , ..., 49.312691, 49.354359, 49.396023])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lon</span></div><div class="xr-var-dims">(lon)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">235.2 235.3 235.3 ... 292.9 292.9</div><input id="attrs-e89fdcaa-0903-437f-9c1e-0403cc35f3f9" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-e89fdcaa-0903-437f-9c1e-0403cc35f3f9" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-4733dcc5-9171-4a0d-a0ff-badb12972775" class="xr-var-data-in" type="checkbox" /><label for="data-4733dcc5-9171-4a0d-a0ff-badb12972775" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>units :</span></dt><dd>degrees_east</dd><dt><span>description :</span></dt><dd>Longitude of the center of the grid cell</dd><dt><span>long_name :</span></dt><dd>longitude</dd><dt><span>standard_name :</span></dt><dd>longitude</dd><dt><span>axis :</span></dt><dd>X</dd></dl></div><div class="xr-var-data"><pre>array([235.227844, 235.269501, 235.311157, ..., 292.851929, 292.893585,
292.935242])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">time</span></div><div class="xr-var-dims">(time)</div><div class="xr-var-dtype">object</div><div class="xr-var-preview xr-preview">1950-01-15 00:00:00 1950-02-15 0...</div><input id="attrs-57cc9e4d-52a0-41ac-b17d-e5ca4a294f1e" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-57cc9e4d-52a0-41ac-b17d-e5ca4a294f1e" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-c80ea9c4-aa4b-4a70-8baf-1e5a41ddc0a8" class="xr-var-data-in" type="checkbox" /><label for="data-c80ea9c4-aa4b-4a70-8baf-1e5a41ddc0a8" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>description :</span></dt><dd>days since 1900-01-01</dd></dl></div><div class="xr-var-data"><pre>array([cftime.DatetimeNoLeap(1950, 1, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(1950, 2, 15, 0, 0, 0, 0, has_year_zero=True)],
dtype=object)</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-f31d1e80-ed09-4b38-9ac6-3be41876b37a" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-f31d1e80-ed09-4b38-9ac6-3be41876b37a" class="xr-section-summary">Attributes: <span>(7)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>Monthly Average of Daily Maximum Near-Surface Air Temperature</dd><dt><span>units :</span></dt><dd>K</dd><dt><span>grid_mapping :</span></dt><dd>crs</dd><dt><span>standard_name :</span></dt><dd>air_temperature</dd><dt><span>height :</span></dt><dd>2 m</dd><dt><span>cell_methods :</span></dt><dd>time: maximum(interval: 24 hours);mean over days</dd><dt><span>_ChunkSizes :</span></dt><dd>[ 10 44 107]</dd></dl></div></li></ul></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">two_months_conus</span><span class="p">.</span><span class="n">shape</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>(2, 585, 1386)
</code></pre></div></div>
<p>The default return when you plot an xarray object with multipple years worth of
data across a spatial extent is a histogram.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">two_months_conus</span><span class="p">.</span><span class="n">shape</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>(2, 585, 1386)
</code></pre></div></div>
<p>When you call <code class="language-plaintext highlighter-rouge">.plot()</code> on the data, the default plot is a
histogram representing the range of raster pixel values in your data
for all time periods (2 months in this case).</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Directly plot just a single day using time in "years"
</span><span class="n">two_months_conus</span><span class="p">.</span><span class="n">plot</span><span class="p">()</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-04-open-netcdf-climate-data/2020-10-16-netcdf-04-open-netcdf-climate-data_55_0.png" alt="The default output of .plot() when you have more than a single point location selected will be a histogram. Here the histogram represents the spread of raster data values in the 2 months of modeled historical data." />
<figcaption>The default output of .plot() when you have more than a single point location selected will be a histogram. Here the histogram represents the spread of raster data values in the 2 months of modeled historical data.</figcaption>
</figure>
<h2 id="spatial-raster-plots-of-maca-v2-climate-data">Spatial Raster Plots of MACA v2 Climate Data</h2>
<p>If you want to plot the data spatially as a raster, you can use
<code class="language-plaintext highlighter-rouge">.plot()</code> but specify the lon and lat values as the x and y dimensions
to plot. You can add the following parameters to your <code class="language-plaintext highlighter-rouge">.plot()</code> call to
make sure each time step in your data plots spatially:</p>
<p><code class="language-plaintext highlighter-rouge">col_wrap=2</code>: adjust how how many columns the each subplot is spread across
<code class="language-plaintext highlighter-rouge">col=</code>: what dimension is being plotted in each subplot.</p>
<p>In this case, you want a single raster for each month (time step) in the
data so you specify <code class="language-plaintext highlighter-rouge">col='time'</code>. <code class="language-plaintext highlighter-rouge">col_wrap=1</code> forces the plots to
stack on top of each other in your <code class="language-plaintext highlighter-rouge">matplotlib</code> figure.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Quickly plot the data using xarray.plot()
</span><span class="n">two_months_conus</span><span class="p">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="s">"lon"</span><span class="p">,</span>
<span class="n">y</span><span class="o">=</span><span class="s">"lat"</span><span class="p">,</span>
<span class="n">col</span><span class="o">=</span><span class="s">"time"</span><span class="p">,</span>
<span class="n">col_wrap</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">suptitle</span><span class="p">(</span><span class="s">"Two Time Steps of Monthly Average Temp"</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mf">1.03</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-04-open-netcdf-climate-data/2020-10-16-netcdf-04-open-netcdf-climate-data_57_0.png" alt="Plot showing two months of the historic max temperature climate data plotted using xarray .plot(). If you set col_wrap to 1, then you end up with one column and two rows of subplots." />
<figcaption>Plot showing two months of the historic max temperature climate data plotted using xarray .plot(). If you set col_wrap to 1, then you end up with one column and two rows of subplots.</figcaption>
</figure>
<p>If you set <code class="language-plaintext highlighter-rouge">col_wrap</code> to <code class="language-plaintext highlighter-rouge">2</code> you end up with two columns and
one subplot in each column.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Plot the data using 2 columns
</span><span class="n">two_months_conus</span><span class="p">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="s">"lon"</span><span class="p">,</span>
<span class="n">y</span><span class="o">=</span><span class="s">"lat"</span><span class="p">,</span>
<span class="n">col</span><span class="o">=</span><span class="s">"time"</span><span class="p">,</span>
<span class="n">col_wrap</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-04-open-netcdf-climate-data/2020-10-16-netcdf-04-open-netcdf-climate-data_59_0.png" alt="Plot showing two months of the historic max temperature climate data plotted using xarray .plot(). If you set col_wrap to 2, then you end up with two columns." />
<figcaption>Plot showing two months of the historic max temperature climate data plotted using xarray .plot(). If you set col_wrap to 2, then you end up with two columns.</figcaption>
</figure>
<h2 id="plot-multiple-maca-v2-climate-data-raster-files-with-a-spatial-projection">Plot Multiple MACA v2 Climate Data Raster Files With a Spatial Projection</h2>
<p>Below you plot the same data using <code class="language-plaintext highlighter-rouge">cartopy</code> which support spatial
projectsion. The <code class="language-plaintext highlighter-rouge">coastlines()</code> basemap is also added to the plot.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">central_lat</span> <span class="o">=</span> <span class="mf">37.5</span>
<span class="n">central_long</span> <span class="o">=</span> <span class="mi">96</span>
<span class="n">extent</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="mi">120</span><span class="p">,</span> <span class="o">-</span><span class="mi">70</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mf">55.5</span><span class="p">]</span> <span class="c1"># CONUS
</span>
<span class="n">map_proj</span> <span class="o">=</span> <span class="n">ccrs</span><span class="p">.</span><span class="n">AlbersEqualArea</span><span class="p">(</span><span class="n">central_longitude</span><span class="o">=</span><span class="n">central_lon</span><span class="p">,</span>
<span class="n">central_latitude</span><span class="o">=</span><span class="n">central_lat</span><span class="p">)</span>
<span class="n">aspect</span> <span class="o">=</span> <span class="n">two_months_conus</span><span class="p">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">/</span> <span class="n">two_months_conus</span><span class="p">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="n">p</span> <span class="o">=</span> <span class="n">two_months_conus</span><span class="p">.</span><span class="n">plot</span><span class="p">(</span><span class="n">transform</span><span class="o">=</span><span class="n">ccrs</span><span class="p">.</span><span class="n">PlateCarree</span><span class="p">(),</span> <span class="c1"># the data's projection
</span> <span class="n">col</span><span class="o">=</span><span class="s">'time'</span><span class="p">,</span> <span class="n">col_wrap</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
<span class="n">aspect</span><span class="o">=</span><span class="n">aspect</span><span class="p">,</span>
<span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span>
<span class="n">subplot_kws</span><span class="o">=</span><span class="p">{</span><span class="s">'projection'</span><span class="p">:</span> <span class="n">map_proj</span><span class="p">})</span> <span class="c1"># the plot's projection
</span>
<span class="n">plt</span><span class="p">.</span><span class="n">suptitle</span><span class="p">(</span><span class="s">"Two Time Steps of CONUS Historic Temperature Data"</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="c1"># Add the coastlines to each axis object and set extent
</span><span class="k">for</span> <span class="n">ax</span> <span class="ow">in</span> <span class="n">p</span><span class="p">.</span><span class="n">axes</span><span class="p">.</span><span class="n">flat</span><span class="p">:</span>
<span class="n">ax</span><span class="p">.</span><span class="n">coastlines</span><span class="p">()</span>
<span class="n">ax</span><span class="p">.</span><span class="n">set_extent</span><span class="p">(</span><span class="n">extent</span><span class="p">)</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-04-open-netcdf-climate-data/2020-10-16-netcdf-04-open-netcdf-climate-data_61_0.png" alt="Map showing air temperature data on a map using the PlateCarree CRS for two months." />
<figcaption>Map showing air temperature data on a map using the PlateCarree CRS for two months.</figcaption>
</figure>
<h2 id="export-raster-to-geotiff-file">Export Raster to Geotiff File</h2>
<p>In the above workflows you converted your data into a DataFrame and exported
it to a .csv file. This approach works well if you only need the summary values
and don’t need any spatial information. However sometimes you may need to
export spatial raster files.</p>
<p>You can export your data to a geotiff file format using rioxarray.
To do this you will need to:</p>
<ol>
<li>ensure that your xarray object has a crs defined and define it if it’s missing.</li>
<li>call xarray to export your data</li>
</ol>
<p>Notice below that your two month subset no long contains CRS information.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Double check the crs still exist
</span><span class="n">two_months_conus</span><span class="p">.</span><span class="n">rio</span><span class="p">.</span><span class="n">crs</span>
</code></pre></div></div>
<p>At the very beginning of this lesson, you saved the crs information
from the original xarray object. You can use that to export your geotiff data
below.</p>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>CRS.from_wkt('GEOGCS["undefined",DATUM["undefined",SPHEROID["undefined",6378137,298.257223563]],PRIMEM["undefined",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Longitude",EAST],AXIS["Latitude",NORTH]]')
</code></pre></div></div>
<p>Once the crs is set, you can export to a geotiff file format.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Export to geotiff
</span><span class="n">file_path</span> <span class="o">=</span> <span class="s">"two_months_temp_data.tif"</span>
<span class="n">two_months_conus</span><span class="p">.</span><span class="n">rio</span><span class="p">.</span><span class="n">to_raster</span><span class="p">(</span><span class="n">file_path</span><span class="p">)</span>
</code></pre></div></div>
<p>Now test our your data. Reimport it using <code class="language-plaintext highlighter-rouge">xarray.open_rasterio</code>.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Open the data up as a geotiff
</span><span class="n">two_months_tiff</span> <span class="o">=</span> <span class="n">rxr</span><span class="p">.</span><span class="n">open_rasterio</span><span class="p">(</span><span class="n">file_path</span><span class="p">)</span>
<span class="n">two_months_tiff</span>
</code></pre></div></div>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.DataArray (band: 2, y: 585, x: 1386)>
[1621620 values with dtype=float32]
Coordinates:
* band (band) int64 1 2
* x (x) float64 235.2 235.3 235.3 235.4 ... 292.8 292.9 292.9 292.9
* y (y) float64 25.06 25.1 25.15 25.19 ... 49.27 49.31 49.35 49.4
spatial_ref int64 0
Attributes:
_FillValue: -9999.0
scale_factor: 1.0
add_offset: 0.0
long_name: Monthly Average of Daily Maximum Near-Surface Air Temperature</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.DataArray</div><div class="xr-array-name"></div><ul class="xr-dim-list"><li><span class="xr-has-index">band</span>: 2</li><li><span class="xr-has-index">y</span>: 585</li><li><span class="xr-has-index">x</span>: 1386</li></ul></div><ul class="xr-sections"><li class="xr-section-item"><div class="xr-array-wrap"><input id="section-c269b647-cb9b-404f-8466-5820784145df" class="xr-array-in" type="checkbox" checked="" /><label for="section-c269b647-cb9b-404f-8466-5820784145df" title="Show/hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-array-preview xr-preview"><span>...</span></div><div class="xr-array-data"><pre>[1621620 values with dtype=float32]</pre></div></div></li><li class="xr-section-item"><input id="section-137bc23a-2647-44b0-a5b6-6050d976630e" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-137bc23a-2647-44b0-a5b6-6050d976630e" class="xr-section-summary">Coordinates: <span>(4)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">band</span></div><div class="xr-var-dims">(band)</div><div class="xr-var-dtype">int64</div><div class="xr-var-preview xr-preview">1 2</div><input id="attrs-38631921-f4ae-4b97-b30e-2f8235f081fb" class="xr-var-attrs-in" type="checkbox" disabled="" /><label for="attrs-38631921-f4ae-4b97-b30e-2f8235f081fb" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-eb659239-6887-4153-9e61-c2977c32b830" class="xr-var-data-in" type="checkbox" /><label for="data-eb659239-6887-4153-9e61-c2977c32b830" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"></dl></div><div class="xr-var-data"><pre>array([1, 2])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">x</span></div><div class="xr-var-dims">(x)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">235.2 235.3 235.3 ... 292.9 292.9</div><input id="attrs-70af3ae9-eee9-46f6-bd9b-c82e0ee9ce59" class="xr-var-attrs-in" type="checkbox" disabled="" /><label for="attrs-70af3ae9-eee9-46f6-bd9b-c82e0ee9ce59" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-50d37664-95de-493d-9eda-2c70b7acad6c" class="xr-var-data-in" type="checkbox" /><label for="data-50d37664-95de-493d-9eda-2c70b7acad6c" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"></dl></div><div class="xr-var-data"><pre>array([235.227844, 235.26951 , 235.311176, ..., 292.85191 , 292.893576,
292.935242])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">y</span></div><div class="xr-var-dims">(y)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">25.06 25.1 25.15 ... 49.35 49.4</div><input id="attrs-6b71730a-8c31-42fd-b9fd-9925a4f7824c" class="xr-var-attrs-in" type="checkbox" disabled="" /><label for="attrs-6b71730a-8c31-42fd-b9fd-9925a4f7824c" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-90e0826e-e975-4544-9e7d-c393f662e12d" class="xr-var-data-in" type="checkbox" /><label for="data-90e0826e-e975-4544-9e7d-c393f662e12d" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"></dl></div><div class="xr-var-data"><pre>array([25.063078, 25.104744, 25.14641 , ..., 49.312691, 49.354357, 49.396023])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span>spatial_ref</span></div><div class="xr-var-dims">()</div><div class="xr-var-dtype">int64</div><div class="xr-var-preview xr-preview">0</div><input id="attrs-1bc2238f-ff86-4535-b547-14ac03e7ae12" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-1bc2238f-ff86-4535-b547-14ac03e7ae12" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-aa8fb609-7cbd-4355-8448-775c8220d431" class="xr-var-data-in" type="checkbox" /><label for="data-aa8fb609-7cbd-4355-8448-775c8220d431" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>crs_wkt :</span></dt><dd>GEOGCS["undefined",DATUM["undefined",SPHEROID["undefined",6378137,298.257223563]],PRIMEM["undefined",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST]]</dd><dt><span>semi_major_axis :</span></dt><dd>6378137.0</dd><dt><span>semi_minor_axis :</span></dt><dd>6356752.314245179</dd><dt><span>inverse_flattening :</span></dt><dd>298.257223563</dd><dt><span>reference_ellipsoid_name :</span></dt><dd>undefined</dd><dt><span>longitude_of_prime_meridian :</span></dt><dd>0.0</dd><dt><span>prime_meridian_name :</span></dt><dd>undefined</dd><dt><span>geographic_crs_name :</span></dt><dd>undefined</dd><dt><span>grid_mapping_name :</span></dt><dd>latitude_longitude</dd><dt><span>spatial_ref :</span></dt><dd>GEOGCS["undefined",DATUM["undefined",SPHEROID["undefined",6378137,298.257223563]],PRIMEM["undefined",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST]]</dd><dt><span>GeoTransform :</span></dt><dd>235.207011242808 0.04166599094652527 0.0 25.042244925890884 0.0 0.04166600148971766</dd></dl></div><div class="xr-var-data"><pre>array(0)</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-2ac4e9c5-f91b-4f42-8251-9f854ddfd150" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-2ac4e9c5-f91b-4f42-8251-9f854ddfd150" class="xr-section-summary">Attributes: <span>(4)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"><dt><span>_FillValue :</span></dt><dd>-9999.0</dd><dt><span>scale_factor :</span></dt><dd>1.0</dd><dt><span>add_offset :</span></dt><dd>0.0</dd><dt><span>long_name :</span></dt><dd>Monthly Average of Daily Maximum Near-Surface Air Temperature</dd></dl></div></li></ul></div></div>
<p>Plot the data. Do you notice anything about the values
in the histogram below?</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">two_months_tiff</span><span class="p">.</span><span class="n">plot</span><span class="p">()</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-04-open-netcdf-climate-data/2020-10-16-netcdf-04-open-netcdf-climate-data_73_0.png" alt="Histogram showing temperature values for the Continental United States. What do you notice about the range of values?" />
<figcaption>Histogram showing temperature values for the Continental United States. What do you notice about the range of values?</figcaption>
</figure>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Plot the data - this doesn't look right!
</span><span class="n">two_months_tiff</span><span class="p">.</span><span class="n">plot</span><span class="p">(</span><span class="n">col</span><span class="o">=</span><span class="s">"band"</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-04-open-netcdf-climate-data/2020-10-16-netcdf-04-open-netcdf-climate-data_74_0.png" alt="Plot showing unmasked temperature data (containing nodata values) for the Continental United States." />
<figcaption>Plot showing unmasked temperature data (containing nodata values) for the Continental United States.</figcaption>
</figure>
<p>The data above are plotting oddly because there are no data
values in your array. You can mask those values using <code class="language-plaintext highlighter-rouge">xarray.where()</code>.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">two_months_tiff</span><span class="p">.</span><span class="n">rio</span><span class="p">.</span><span class="n">nodata</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>-9999.0
</code></pre></div></div>
<p>Below you use <code class="language-plaintext highlighter-rouge">.where()</code> to mask all values in your data that are
equal to <code class="language-plaintext highlighter-rouge">-9999</code>, the nodata value for this data.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Remove no data values and try to plot again
</span><span class="n">two_months_tiff</span> <span class="o">=</span> <span class="n">rxr</span><span class="p">.</span><span class="n">open_rasterio</span><span class="p">(</span><span class="n">file_path</span><span class="p">)</span>
<span class="n">two_months_clean</span> <span class="o">=</span> <span class="n">two_months_tiff</span><span class="p">.</span><span class="n">where</span><span class="p">(</span>
<span class="n">two_months_tiff</span> <span class="o">!=</span> <span class="n">two_months_tiff</span><span class="p">.</span><span class="n">rio</span><span class="p">.</span><span class="n">nodata</span><span class="p">)</span>
<span class="n">two_months_clean</span><span class="p">.</span><span class="n">plot</span><span class="p">(</span><span class="n">col</span><span class="o">=</span><span class="s">"band"</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-04-open-netcdf-climate-data/2020-10-16-netcdf-04-open-netcdf-climate-data_78_0.png" alt="Plot showing masked temperature data for the Continental United States." />
<figcaption>Plot showing masked temperature data for the Continental United States.</figcaption>
</figure>
<p>Above you learned how to begin to work with NETCDF4 format files containing
climate focused data. You learned how to open and subset the data by time and location.
In the next lesson you will learn how to implement spatial subsets using shapefiles of your data.</p>Leah WasserHistoric and projected climate data are most often stored in netcdf 4 format. Learn how to open and process MACA version 2 climate data for the Continental United States using the open source python package, xarray.Calculate Summary Values Using Spatial Areas of Interest (AOIs) including Shapefiles for Climate Data Variables Stored in NetCDF 4 Format: Work With MACA v2 Climate Data in Python2020-10-16T00:00:00+00:002020-10-16T00:00:00+00:00https://www.earthdatascience.org/courses/use-data-open-source-python/hierarchical-data-formats-hdf/netcdf-05-spatial-subsets<aside class="sidebar__right">
<nav class="toc">
<header><h4 class="nav__title"><i class="fa fa-file-text"></i> In This Chapter</h4></header>
<ul class="toc__menu" id="markdown-toc">
<li><a href="#chapter-13---netcdf-4-climate-data-in-open-source-python" id="markdown-toc-chapter-13---netcdf-4-climate-data-in-open-source-python"><i class="fa fa-ship" aria-hidden="true"></i> Chapter 13 - NETCDF 4 Climate Data in Open Source Python</a></li>
<li><a href="#learning-objectives" id="markdown-toc-learning-objectives"><i class="fa fa-graduation-cap" aria-hidden="true"></i> Learning Objectives</a></li>
<li><a href="#what-you-need" id="markdown-toc-what-you-need"><i class="fa fa-check-square-o fa-2" aria-hidden="true"></i> What You Need</a></li>
<li><a href="#subset-maca-2-climate-data-in-netcdf-4-format-by-time-and-spatial-extents" id="markdown-toc-subset-maca-2-climate-data-in-netcdf-4-format-by-time-and-spatial-extents">Subset MACA 2 Climate Data in NetCDF 4 Format By Time and Spatial Extents</a></li>
<li><a href="#spatial-subsets-of-data-using-an-aoi" id="markdown-toc-spatial-subsets-of-data-using-an-aoi">Spatial Subsets of Data Using an AOI</a></li>
<li><a href="#subset-a-netcdf4-using-a-shapefile-feature-or-features" id="markdown-toc-subset-a-netcdf4-using-a-shapefile-feature-or-features">Subset a netcdf4 Using a Shapefile Feature or Features</a></li>
<li><a href="#workflow-with-multiple-regions" id="markdown-toc-workflow-with-multiple-regions">Workflow with Multiple Regions</a></li>
<li><a href="#calculate-mean-air-temperature-by-region" id="markdown-toc-calculate-mean-air-temperature-by-region">Calculate Mean Air Temperature by Region</a></li>
<li><a href="#the-entire-workflow-to-subset-netcdf-4-files-by-temporal-and-spatial-extents" id="markdown-toc-the-entire-workflow-to-subset-netcdf-4-files-by-temporal-and-spatial-extents">The Entire Workflow To Subset netcdf 4 Files by Temporal and Spatial Extents</a></li>
</ul>
</nav>
</aside>
<div class="notice--success">
<h2 id="chapter-13---netcdf-4-climate-data-in-open-source-python"><i class="fa fa-ship" aria-hidden="true"></i> Chapter 13 - NETCDF 4 Climate Data in Open Source Python</h2>
<p>In this chapter, you will learn how to work with Climate Data Sets (MACA v2 for the United states) stored in netcdf 4 format using open source <strong>Python</strong>.</p>
<h2 id="learning-objectives"><i class="fa fa-graduation-cap" aria-hidden="true"></i> Learning Objectives</h2>
<p>After completing this chapter, you will be able to:</p>
<ul>
<li>Summarize MACA v 2 climate data stored in netcdf 4 format by seasons across all time periods using <code class="language-plaintext highlighter-rouge">xarray</code>.</li>
<li>Summarize MACA v 2 climate data stored in netcdf 4 format by seasons and across years using <code class="language-plaintext highlighter-rouge">xarray</code>.</li>
</ul>
<h2 id="what-you-need"><i class="fa fa-check-square-o fa-2" aria-hidden="true"></i> What You Need</h2>
<p>You will need a computer with internet access to complete this lesson and the earth-analytics-python
conda environment installed.</p>
</div>
<h2 id="subset-maca-2-climate-data-in-netcdf-4-format-by-time-and-spatial-extents">Subset MACA 2 Climate Data in NetCDF 4 Format By Time and Spatial Extents</h2>
<p>In this lesson, you will learn how to subset MACA v2 climate data using the open
source Python packages <code class="language-plaintext highlighter-rouge">xarray</code> and <code class="language-plaintext highlighter-rouge">regionmask</code>.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="n">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="n">pd</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="n">plt</span>
<span class="kn">import</span> <span class="nn">cartopy.crs</span> <span class="k">as</span> <span class="n">ccrs</span>
<span class="kn">import</span> <span class="nn">cartopy.feature</span> <span class="k">as</span> <span class="n">cfeature</span>
<span class="kn">import</span> <span class="nn">seaborn</span> <span class="k">as</span> <span class="n">sns</span>
<span class="kn">import</span> <span class="nn">geopandas</span> <span class="k">as</span> <span class="n">gpd</span>
<span class="kn">import</span> <span class="nn">earthpy</span> <span class="k">as</span> <span class="n">et</span>
<span class="kn">import</span> <span class="nn">xarray</span> <span class="k">as</span> <span class="n">xr</span>
<span class="c1"># Spatial subsetting of netcdf files
</span><span class="kn">import</span> <span class="nn">regionmask</span>
<span class="c1"># Plotting options
</span><span class="n">sns</span><span class="p">.</span><span class="nb">set</span><span class="p">(</span><span class="n">font_scale</span><span class="o">=</span><span class="mf">1.3</span><span class="p">)</span>
<span class="n">sns</span><span class="p">.</span><span class="n">set_style</span><span class="p">(</span><span class="s">"white"</span><span class="p">)</span>
<span class="c1"># Optional - set your working directory if you wish to use the data
# accessed lower down in this notebook (the USA state boundary data)
</span><span class="n">os</span><span class="p">.</span><span class="n">chdir</span><span class="p">(</span><span class="n">os</span><span class="p">.</span><span class="n">path</span><span class="p">.</span><span class="n">join</span><span class="p">(</span><span class="n">et</span><span class="p">.</span><span class="n">io</span><span class="p">.</span><span class="n">HOME</span><span class="p">,</span> <span class="s">'earth-analytics'</span><span class="p">,</span> <span class="s">'data'</span><span class="p">))</span>
</code></pre></div></div>
<h2 id="spatial-subsets-of-data-using-an-aoi">Spatial Subsets of Data Using an AOI</h2>
<p>In the previous lesson, you learned how to select a single point and extract
temperature data at that location. You can also create areas of interest
(AOIs) that define the geographic region that you wish to extract data for.</p>
<p>Below you will learn how to use a shapefile that contains a boundary
area of interest that you wish to subset data for. You will then learn
how to subset the data using the</p>
<ol>
<li>geographic boundary extent of the AOI (a rectangular extent) and</li>
<li>using the actual shape boundary of the AOI (example: the state of California)</li>
</ol>
<p>To begin, you will open up a new netcdf file. This file contains
future projected maximum temperature data for the Continental United States(CONUS).</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Get netcdf file
</span><span class="n">data_path_monthly</span> <span class="o">=</span> <span class="s">'http://thredds.northwestknowledge.net:8080/thredds/dodsC/agg_macav2metdata_tasmax_BNU-ESM_r1i1p1_rcp45_2006_2099_CONUS_monthly.nc'</span>
<span class="c1"># Open up the data
</span><span class="k">with</span> <span class="n">xr</span><span class="p">.</span><span class="n">open_dataset</span><span class="p">(</span><span class="n">data_path_monthly</span><span class="p">)</span> <span class="k">as</span> <span class="n">file_nc</span><span class="p">:</span>
<span class="n">monthly_forecast_temp_xr</span> <span class="o">=</span> <span class="n">file_nc</span>
<span class="c1"># View xarray object
</span><span class="n">monthly_forecast_temp_xr</span>
</code></pre></div></div>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.Dataset>
Dimensions: (lat: 585, crs: 1, lon: 1386, time: 1128)
Coordinates:
* lat (lat) float64 25.06 25.1 25.15 25.19 ... 49.31 49.35 49.4
* crs (crs) int32 1
* lon (lon) float64 235.2 235.3 235.3 235.4 ... 292.9 292.9 292.9
* time (time) object 2006-01-15 00:00:00 ... 2099-12-15 00:00:00
Data variables:
air_temperature (time, lat, lon) float32 ...
Attributes: (12/46)
description: Multivariate Adaptive Constructed Analog...
id: MACAv2-METDATA
naming_authority: edu.uidaho.reacch
Metadata_Conventions: Unidata Dataset Discovery v1.0
Metadata_Link:
cdm_data_type: FLOAT
... ...
contributor_role: Postdoctoral Fellow
publisher_name: REACCH
publisher_email: reacch@uidaho.edu
publisher_url: http://www.reacchpna.org/
license: Creative Commons CC0 1.0 Universal Dedic...
coordinate_system: WGS84,EPSG:4326</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.Dataset</div></div><ul class="xr-sections"><li class="xr-section-item"><input id="section-63d955cc-4ced-4b4d-986e-68d0e55c19c5" class="xr-section-summary-in" type="checkbox" disabled="" /><label for="section-63d955cc-4ced-4b4d-986e-68d0e55c19c5" class="xr-section-summary" title="Expand/collapse section">Dimensions:</label><div class="xr-section-inline-details"><ul class="xr-dim-list"><li><span class="xr-has-index">lat</span>: 585</li><li><span class="xr-has-index">crs</span>: 1</li><li><span class="xr-has-index">lon</span>: 1386</li><li><span class="xr-has-index">time</span>: 1128</li></ul></div><div class="xr-section-details"></div></li><li class="xr-section-item"><input id="section-db73d187-0b99-424c-9f94-8571f73c79a2" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-db73d187-0b99-424c-9f94-8571f73c79a2" class="xr-section-summary">Coordinates: <span>(4)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lat</span></div><div class="xr-var-dims">(lat)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">25.06 25.1 25.15 ... 49.35 49.4</div><input id="attrs-1a039172-0954-4603-90a1-77cb192c6ed6" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-1a039172-0954-4603-90a1-77cb192c6ed6" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-b6c692d4-fe2e-43cf-9db8-8223e1b7bc6a" class="xr-var-data-in" type="checkbox" /><label for="data-b6c692d4-fe2e-43cf-9db8-8223e1b7bc6a" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>latitude</dd><dt><span>standard_name :</span></dt><dd>latitude</dd><dt><span>units :</span></dt><dd>degrees_north</dd><dt><span>axis :</span></dt><dd>Y</dd><dt><span>description :</span></dt><dd>Latitude of the center of the grid cell</dd></dl></div><div class="xr-var-data"><pre>array([25.063078, 25.104744, 25.14641 , ..., 49.312691, 49.354359, 49.396023])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">crs</span></div><div class="xr-var-dims">(crs)</div><div class="xr-var-dtype">int32</div><div class="xr-var-preview xr-preview">1</div><input id="attrs-e7102f20-bd3f-4eda-a1f2-e009894b5f18" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-e7102f20-bd3f-4eda-a1f2-e009894b5f18" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-9cf04474-be4b-46fe-a145-0ef5f03e5d3e" class="xr-var-data-in" type="checkbox" /><label for="data-9cf04474-be4b-46fe-a145-0ef5f03e5d3e" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>grid_mapping_name :</span></dt><dd>latitude_longitude</dd><dt><span>longitude_of_prime_meridian :</span></dt><dd>0.0</dd><dt><span>semi_major_axis :</span></dt><dd>6378137.0</dd><dt><span>inverse_flattening :</span></dt><dd>298.257223563</dd></dl></div><div class="xr-var-data"><pre>array([1], dtype=int32)</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lon</span></div><div class="xr-var-dims">(lon)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">235.2 235.3 235.3 ... 292.9 292.9</div><input id="attrs-6f35db4c-1f39-430a-8800-db805f3656f6" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-6f35db4c-1f39-430a-8800-db805f3656f6" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-03be8e96-68c5-4c6b-ba81-c475eab304f5" class="xr-var-data-in" type="checkbox" /><label for="data-03be8e96-68c5-4c6b-ba81-c475eab304f5" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>units :</span></dt><dd>degrees_east</dd><dt><span>axis :</span></dt><dd>X</dd><dt><span>description :</span></dt><dd>Longitude of the center of the grid cell</dd><dt><span>long_name :</span></dt><dd>longitude</dd><dt><span>standard_name :</span></dt><dd>longitude</dd></dl></div><div class="xr-var-data"><pre>array([235.227844, 235.269501, 235.311157, ..., 292.851929, 292.893585,
292.935242])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">time</span></div><div class="xr-var-dims">(time)</div><div class="xr-var-dtype">object</div><div class="xr-var-preview xr-preview">2006-01-15 00:00:00 ... 2099-12-...</div><input id="attrs-3ac85acf-5638-470b-9eca-bdbab26c498e" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-3ac85acf-5638-470b-9eca-bdbab26c498e" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-91138d12-5c8b-4b0a-8806-4ddd9d02b5e9" class="xr-var-data-in" type="checkbox" /><label for="data-91138d12-5c8b-4b0a-8806-4ddd9d02b5e9" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>description :</span></dt><dd>days since 1900-01-01</dd></dl></div><div class="xr-var-data"><pre>array([cftime.DatetimeNoLeap(2006, 1, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2006, 2, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2006, 3, 15, 0, 0, 0, 0, has_year_zero=True), ...,
cftime.DatetimeNoLeap(2099, 10, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2099, 11, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2099, 12, 15, 0, 0, 0, 0, has_year_zero=True)],
dtype=object)</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-3f93d90e-5456-4787-bb1a-c0adf165c97b" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-3f93d90e-5456-4787-bb1a-c0adf165c97b" class="xr-section-summary">Data variables: <span>(1)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span>air_temperature</span></div><div class="xr-var-dims">(time, lat, lon)</div><div class="xr-var-dtype">float32</div><div class="xr-var-preview xr-preview">...</div><input id="attrs-3f99bc86-0755-47cc-a1f5-ad2d71df3756" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-3f99bc86-0755-47cc-a1f5-ad2d71df3756" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-6d691b7f-6e67-4d11-bacd-5bba3c916da6" class="xr-var-data-in" type="checkbox" /><label for="data-6d691b7f-6e67-4d11-bacd-5bba3c916da6" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>Monthly Average of Daily Maximum Near-Surface Air Temperature</dd><dt><span>units :</span></dt><dd>K</dd><dt><span>grid_mapping :</span></dt><dd>crs</dd><dt><span>standard_name :</span></dt><dd>air_temperature</dd><dt><span>height :</span></dt><dd>2 m</dd><dt><span>cell_methods :</span></dt><dd>time: maximum(interval: 24 hours);mean over days</dd><dt><span>_ChunkSizes :</span></dt><dd>[ 10 44 107]</dd></dl></div><div class="xr-var-data"><pre>[914593680 values with dtype=float32]</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-51c8cd03-25d2-403d-bf24-6b8579c387c0" class="xr-section-summary-in" type="checkbox" /><label for="section-51c8cd03-25d2-403d-bf24-6b8579c387c0" class="xr-section-summary">Attributes: <span>(46)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"><dt><span>description :</span></dt><dd>Multivariate Adaptive Constructed Analogs (MACA) method, version 2.3,Dec 2013.</dd><dt><span>id :</span></dt><dd>MACAv2-METDATA</dd><dt><span>naming_authority :</span></dt><dd>edu.uidaho.reacch</dd><dt><span>Metadata_Conventions :</span></dt><dd>Unidata Dataset Discovery v1.0</dd><dt><span>Metadata_Link :</span></dt><dd></dd><dt><span>cdm_data_type :</span></dt><dd>FLOAT</dd><dt><span>title :</span></dt><dd>Monthly aggregation of downscaled daily meteorological data of Monthly Average of Daily Maximum Near-Surface Air Temperature from College of Global Change and Earth System Science, Beijing Normal University (BNU-ESM) using the run r1i1p1 of the rcp45 scenario.</dd><dt><span>summary :</span></dt><dd>This archive contains monthly downscaled meteorological and hydrological projections for the Conterminous United States at 1/24-deg resolution. These monthly values are obtained by aggregating the daily values obtained from the downscaling using the Multivariate Adaptive Constructed Analogs (MACA, Abatzoglou, 2012) statistical downscaling method with the METDATA (Abatzoglou,2013) training dataset. The downscaled meteorological variables are maximum/minimum temperature(tasmax/tasmin), maximum/minimum relative humidity (rhsmax/rhsmin),precipitation amount(pr), downward shortwave solar radiation(rsds), eastward wind(uas), northward wind(vas), and specific humidity(huss). The downscaling is based on the 365-day model outputs from different global climate models (GCMs) from Phase 5 of the Coupled Model Inter-comparison Project (CMIP3) utlizing the historical (1950-2005) and future RCP4.5/8.5(2006-2099) scenarios. </dd><dt><span>keywords :</span></dt><dd>monthly, precipitation, maximum temperature, minimum temperature, downward shortwave solar radiation, specific humidity, wind velocity, CMIP5, Gridded Meteorological Data</dd><dt><span>keywords_vocabulary :</span></dt><dd></dd><dt><span>standard_name_vocabulary :</span></dt><dd>CF-1.0</dd><dt><span>history :</span></dt><dd>No revisions.</dd><dt><span>comment :</span></dt><dd></dd><dt><span>geospatial_bounds :</span></dt><dd>POLYGON((-124.7722 25.0631,-124.7722 49.3960, -67.0648 49.3960,-67.0648, 25.0631, -124.7722,25.0631))</dd><dt><span>geospatial_lat_min :</span></dt><dd>25.0631</dd><dt><span>geospatial_lat_max :</span></dt><dd>49.3960</dd><dt><span>geospatial_lon_min :</span></dt><dd>-124.7722</dd><dt><span>geospatial_lon_max :</span></dt><dd>-67.0648</dd><dt><span>geospatial_lat_units :</span></dt><dd>decimal degrees north</dd><dt><span>geospatial_lon_units :</span></dt><dd>decimal degrees east</dd><dt><span>geospatial_lat_resolution :</span></dt><dd>0.0417</dd><dt><span>geospatial_lon_resolution :</span></dt><dd>0.0417</dd><dt><span>geospatial_vertical_min :</span></dt><dd>0.0</dd><dt><span>geospatial_vertical_max :</span></dt><dd>0.0</dd><dt><span>geospatial_vertical_resolution :</span></dt><dd>0.0</dd><dt><span>geospatial_vertical_positive :</span></dt><dd>up</dd><dt><span>time_coverage_start :</span></dt><dd>2091-01-01T00:0</dd><dt><span>time_coverage_end :</span></dt><dd>2095-12-31T00:00</dd><dt><span>time_coverage_duration :</span></dt><dd>P5Y</dd><dt><span>time_coverage_resolution :</span></dt><dd>P1M</dd><dt><span>date_created :</span></dt><dd>2014-05-15</dd><dt><span>date_modified :</span></dt><dd>2014-05-15</dd><dt><span>date_issued :</span></dt><dd>2014-05-15</dd><dt><span>creator_name :</span></dt><dd>John Abatzoglou</dd><dt><span>creator_url :</span></dt><dd>http://maca.northwestknowledge.net</dd><dt><span>creator_email :</span></dt><dd>jabatzoglou@uidaho.edu</dd><dt><span>institution :</span></dt><dd>University of Idaho</dd><dt><span>processing_level :</span></dt><dd>GRID</dd><dt><span>project :</span></dt><dd></dd><dt><span>contributor_name :</span></dt><dd>Katherine C. Hegewisch</dd><dt><span>contributor_role :</span></dt><dd>Postdoctoral Fellow</dd><dt><span>publisher_name :</span></dt><dd>REACCH</dd><dt><span>publisher_email :</span></dt><dd>reacch@uidaho.edu</dd><dt><span>publisher_url :</span></dt><dd>http://www.reacchpna.org/</dd><dt><span>license :</span></dt><dd>Creative Commons CC0 1.0 Universal Dedication(http://creativecommons.org/publicdomain/zero/1.0/legalcode)</dd><dt><span>coordinate_system :</span></dt><dd>WGS84,EPSG:4326</dd></dl></div></li></ul></div></div>
<h3 id="open-a-shapefile-to-use-as-an-aoi">Open A Shapefile to Use as an AOI</h3>
<p>Often you want to subset and summarize climate data by specific regions of
interest. In the example below, you will open a natural earth layer that
contains state and region boundaries. You will extract a state boundary (California)
within the United States to use as an AOI.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Download natural earth data which contains state boundaries to generate AOI
</span><span class="n">url</span> <span class="o">=</span> <span class="p">(</span>
<span class="s">"https://naturalearth.s3.amazonaws.com/"</span>
<span class="s">"50m_cultural/ne_50m_admin_1_states_provinces_lakes.zip"</span>
<span class="p">)</span>
<span class="n">states_gdf</span> <span class="o">=</span> <span class="n">gpd</span><span class="p">.</span><span class="n">read_file</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
<span class="n">states_gdf</span><span class="p">.</span><span class="n">head</span><span class="p">()</span>
</code></pre></div></div>
<div class="output execute_result">
<style scoped="">
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>featurecla</th>
<th>scalerank</th>
<th>adm1_code</th>
<th>diss_me</th>
<th>iso_3166_2</th>
<th>wikipedia</th>
<th>iso_a2</th>
<th>adm0_sr</th>
<th>name</th>
<th>name_alt</th>
<th>...</th>
<th>name_nl</th>
<th>name_pl</th>
<th>name_pt</th>
<th>name_ru</th>
<th>name_sv</th>
<th>name_tr</th>
<th>name_vi</th>
<th>name_zh</th>
<th>ne_id</th>
<th>geometry</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Admin-1 scale rank</td>
<td>2</td>
<td>AUS-2651</td>
<td>2651</td>
<td>AU-WA</td>
<td>None</td>
<td>AU</td>
<td>6</td>
<td>Western Australia</td>
<td>None</td>
<td>...</td>
<td>West-Australië</td>
<td>Australia Zachodnia</td>
<td>Austrália Ocidental</td>
<td>Западная Австралия</td>
<td>Western Australia</td>
<td>Batı Avustralya</td>
<td>Tây Úc</td>
<td>西澳大利亚州</td>
<td>1159315805</td>
<td>MULTIPOLYGON (((113.13181 -25.95199, 113.14823...</td>
</tr>
<tr>
<th>1</th>
<td>Admin-1 scale rank</td>
<td>2</td>
<td>AUS-2650</td>
<td>2650</td>
<td>AU-NT</td>
<td>None</td>
<td>AU</td>
<td>6</td>
<td>Northern Territory</td>
<td>None</td>
<td>...</td>
<td>Noordelijk Territorium</td>
<td>Terytorium Północne</td>
<td>Território do Norte</td>
<td>Северная территория</td>
<td>Northern Territory</td>
<td>Kuzey Toprakları</td>
<td>Lãnh thổ Bắc Úc</td>
<td>北領地</td>
<td>1159315809</td>
<td>MULTIPOLYGON (((129.00196 -25.99901, 129.00196...</td>
</tr>
<tr>
<th>2</th>
<td>Admin-1 scale rank</td>
<td>2</td>
<td>AUS-2655</td>
<td>2655</td>
<td>AU-SA</td>
<td>None</td>
<td>AU</td>
<td>3</td>
<td>South Australia</td>
<td>None</td>
<td>...</td>
<td>Zuid-Australië</td>
<td>Australia Południowa</td>
<td>Austrália Meridional</td>
<td>Южная Австралия</td>
<td>South Australia</td>
<td>Güney Avustralya</td>
<td>Nam Úc</td>
<td>南澳大利亚州</td>
<td>1159313267</td>
<td>MULTIPOLYGON (((129.00196 -31.69266, 129.00196...</td>
</tr>
<tr>
<th>3</th>
<td>Admin-1 scale rank</td>
<td>2</td>
<td>AUS-2657</td>
<td>2657</td>
<td>AU-QLD</td>
<td>None</td>
<td>AU</td>
<td>5</td>
<td>Queensland</td>
<td>None</td>
<td>...</td>
<td>Queensland</td>
<td>Queensland</td>
<td>Queensland</td>
<td>Квинсленд</td>
<td>Queensland</td>
<td>Queensland</td>
<td>Queensland</td>
<td>昆士蘭州</td>
<td>1159315807</td>
<td>MULTIPOLYGON (((138.00196 -25.99901, 138.00174...</td>
</tr>
<tr>
<th>4</th>
<td>Admin-1 scale rank</td>
<td>2</td>
<td>AUS-2660</td>
<td>2660</td>
<td>AU-TAS</td>
<td>None</td>
<td>AU</td>
<td>5</td>
<td>Tasmania</td>
<td>None</td>
<td>...</td>
<td>Tasmanië</td>
<td>Tasmania</td>
<td>Tasmânia</td>
<td>Тасмания</td>
<td>Tasmanien</td>
<td>Tasmanya</td>
<td>Tasmania</td>
<td>塔斯馬尼亞州</td>
<td>1159313261</td>
<td>MULTIPOLYGON (((147.31246 -43.28038, 147.34238...</td>
</tr>
</tbody>
</table>
<p>5 rows × 84 columns</p>
</div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># You will use the bounds to determine the slice values for this data
# Select any state in the CONUS that you wish here! California is the default
</span><span class="n">cali_aoi</span> <span class="o">=</span> <span class="n">states_gdf</span><span class="p">[</span><span class="n">states_gdf</span><span class="p">.</span><span class="n">name</span> <span class="o">==</span> <span class="s">"California"</span><span class="p">]</span>
<span class="c1"># Get the total spatial extent for California
</span><span class="n">cali_aoi</span><span class="p">.</span><span class="n">total_bounds</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>array([-124.37165376, 32.53336527, -114.12501824, 42.00076797])
</code></pre></div></div>
<p>Next, convert the bounds of your AOI into the min and max longitude
and latitude values. You will use these values to <code class="language-plaintext highlighter-rouge">slice</code> your data.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Get lat min, max
</span><span class="n">aoi_lat</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">cali_aoi</span><span class="p">.</span><span class="n">total_bounds</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span> <span class="nb">float</span><span class="p">(</span><span class="n">cali_aoi</span><span class="p">.</span><span class="n">total_bounds</span><span class="p">[</span><span class="mi">3</span><span class="p">])]</span>
<span class="n">aoi_lon</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">cali_aoi</span><span class="p">.</span><span class="n">total_bounds</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> <span class="nb">float</span><span class="p">(</span><span class="n">cali_aoi</span><span class="p">.</span><span class="n">total_bounds</span><span class="p">[</span><span class="mi">2</span><span class="p">])]</span>
<span class="c1"># Notice that the longitude values have negative numbers
# we need these values in a global crs so we can subtract from 360
</span><span class="n">aoi_lat</span><span class="p">,</span> <span class="n">aoi_lon</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>([32.533365269889316, 42.00076797479207],
[-124.3716537616361, -114.12501823892204])
</code></pre></div></div>
<p>The netcdf files use a global lat/lon rather than positive and negative
longitude values. To ensure you are subsetting the data for the correct
region, you can add 360 degrees to each longitude value which represent the
min x and max x values for the extent.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># The netcdf files use a global lat/lon so adjust values accordingly
</span><span class="n">aoi_lon</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">aoi_lon</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="mi">360</span>
<span class="n">aoi_lon</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">aoi_lon</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="mi">360</span>
<span class="n">aoi_lon</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>[235.62834623836392, 245.87498176107795]
</code></pre></div></div>
<p>Once you have your data AOI defined, you can slice out the
AOI region using xarray <code class="language-plaintext highlighter-rouge">slice</code>.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Slice the data by time and spatial extent
</span><span class="n">start_date</span> <span class="o">=</span> <span class="s">"2010-01-15"</span>
<span class="n">end_date</span> <span class="o">=</span> <span class="s">"2010-02-15"</span>
<span class="n">two_months_cali</span> <span class="o">=</span> <span class="n">monthly_forecast_temp_xr</span><span class="p">[</span><span class="s">"air_temperature"</span><span class="p">].</span><span class="n">sel</span><span class="p">(</span>
<span class="n">time</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="n">start_date</span><span class="p">,</span> <span class="n">end_date</span><span class="p">),</span>
<span class="n">lon</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="n">aoi_lon</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">aoi_lon</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span>
<span class="n">lat</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="n">aoi_lat</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">aoi_lat</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
<span class="n">two_months_cali</span>
</code></pre></div></div>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.DataArray 'air_temperature' (time: 2, lat: 227, lon: 246)>
[111684 values with dtype=float32]
Coordinates:
* lat (lat) float64 32.56 32.6 32.65 32.69 ... 41.85 41.9 41.94 41.98
* lon (lon) float64 235.6 235.7 235.7 235.8 ... 245.7 245.8 245.8 245.9
* time (time) object 2010-01-15 00:00:00 2010-02-15 00:00:00
Attributes:
long_name: Monthly Average of Daily Maximum Near-Surface Air Tempera...
units: K
grid_mapping: crs
standard_name: air_temperature
height: 2 m
cell_methods: time: maximum(interval: 24 hours);mean over days
_ChunkSizes: [ 10 44 107]</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.DataArray</div><div class="xr-array-name">'air_temperature'</div><ul class="xr-dim-list"><li><span class="xr-has-index">time</span>: 2</li><li><span class="xr-has-index">lat</span>: 227</li><li><span class="xr-has-index">lon</span>: 246</li></ul></div><ul class="xr-sections"><li class="xr-section-item"><div class="xr-array-wrap"><input id="section-db029b64-529e-4298-b761-5628f0a6a818" class="xr-array-in" type="checkbox" checked="" /><label for="section-db029b64-529e-4298-b761-5628f0a6a818" title="Show/hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-array-preview xr-preview"><span>...</span></div><div class="xr-array-data"><pre>[111684 values with dtype=float32]</pre></div></div></li><li class="xr-section-item"><input id="section-726d156e-c251-42cd-bdaa-55b6c38dcf81" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-726d156e-c251-42cd-bdaa-55b6c38dcf81" class="xr-section-summary">Coordinates: <span>(3)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lat</span></div><div class="xr-var-dims">(lat)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">32.56 32.6 32.65 ... 41.94 41.98</div><input id="attrs-bb308e5f-a5a6-44ad-9883-807b7b2754e2" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-bb308e5f-a5a6-44ad-9883-807b7b2754e2" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-55227aa9-d4f9-4b8c-a958-0177ea604f3c" class="xr-var-data-in" type="checkbox" /><label for="data-55227aa9-d4f9-4b8c-a958-0177ea604f3c" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>latitude</dd><dt><span>standard_name :</span></dt><dd>latitude</dd><dt><span>units :</span></dt><dd>degrees_north</dd><dt><span>axis :</span></dt><dd>Y</dd><dt><span>description :</span></dt><dd>Latitude of the center of the grid cell</dd></dl></div><div class="xr-var-data"><pre>array([32.562958, 32.604626, 32.64629 , ..., 41.896141, 41.937809, 41.979473])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lon</span></div><div class="xr-var-dims">(lon)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">235.6 235.7 235.7 ... 245.8 245.9</div><input id="attrs-859e912b-9725-4687-a96c-e1e2514c029b" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-859e912b-9725-4687-a96c-e1e2514c029b" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-88d780f0-ca8f-4051-b090-c34b9741f989" class="xr-var-data-in" type="checkbox" /><label for="data-88d780f0-ca8f-4051-b090-c34b9741f989" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>units :</span></dt><dd>degrees_east</dd><dt><span>axis :</span></dt><dd>X</dd><dt><span>description :</span></dt><dd>Longitude of the center of the grid cell</dd><dt><span>long_name :</span></dt><dd>longitude</dd><dt><span>standard_name :</span></dt><dd>longitude</dd></dl></div><div class="xr-var-data"><pre>array([235.644501, 235.686157, 235.727829, ..., 245.769333, 245.811005,
245.852661])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">time</span></div><div class="xr-var-dims">(time)</div><div class="xr-var-dtype">object</div><div class="xr-var-preview xr-preview">2010-01-15 00:00:00 2010-02-15 0...</div><input id="attrs-3ff6f45c-5e3f-4fec-b94a-adde14ea9582" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-3ff6f45c-5e3f-4fec-b94a-adde14ea9582" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-1d58dbfb-d5f0-4e1c-aa06-30cd0ae9e86d" class="xr-var-data-in" type="checkbox" /><label for="data-1d58dbfb-d5f0-4e1c-aa06-30cd0ae9e86d" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>description :</span></dt><dd>days since 1900-01-01</dd></dl></div><div class="xr-var-data"><pre>array([cftime.DatetimeNoLeap(2010, 1, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2010, 2, 15, 0, 0, 0, 0, has_year_zero=True)],
dtype=object)</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-cf063e6b-da00-49a1-9260-52ad09cff5c4" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-cf063e6b-da00-49a1-9260-52ad09cff5c4" class="xr-section-summary">Attributes: <span>(7)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>Monthly Average of Daily Maximum Near-Surface Air Temperature</dd><dt><span>units :</span></dt><dd>K</dd><dt><span>grid_mapping :</span></dt><dd>crs</dd><dt><span>standard_name :</span></dt><dd>air_temperature</dd><dt><span>height :</span></dt><dd>2 m</dd><dt><span>cell_methods :</span></dt><dd>time: maximum(interval: 24 hours);mean over days</dd><dt><span>_ChunkSizes :</span></dt><dd>[ 10 44 107]</dd></dl></div></li></ul></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Plot a quick histogram
</span><span class="n">two_months_cali</span><span class="p">.</span><span class="n">plot</span><span class="p">()</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-05-spatial-subsets/2020-10-16-netcdf-05-spatial-subsets_16_0.png" alt="Histogram showing the spread of temperature values over time for the selected MACA 2 max temperature dataset and for the AOI (the spatial extend of california). " />
<figcaption>Histogram showing the spread of temperature values over time for the selected MACA 2 max temperature dataset and for the AOI (the spatial extend of california). </figcaption>
</figure>
<p>Remember that these data are spatial. Below you
plot each time step as a raster dataset. Notice that this spatial extent
is a rectangle representing the entire rectangular extent for the
state of California.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Spatial Plot for the selected AOI (California)
</span><span class="n">two_months_cali</span><span class="p">.</span><span class="n">plot</span><span class="p">(</span><span class="n">col</span><span class="o">=</span><span class="s">'time'</span><span class="p">,</span>
<span class="n">col_wrap</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-05-spatial-subsets/2020-10-16-netcdf-05-spatial-subsets_18_0.png" alt="Spatial plots of monthly maximum temperature for two time steps. Notice that the AOI or spatial extent is a square boundary as you subsetted it above. " />
<figcaption>Spatial plots of monthly maximum temperature for two time steps. Notice that the AOI or spatial extent is a square boundary as you subsetted it above. </figcaption>
</figure>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Only subset by location / not time
</span><span class="n">cali_ts</span> <span class="o">=</span> <span class="n">monthly_forecast_temp_xr</span><span class="p">[</span><span class="s">"air_temperature"</span><span class="p">].</span><span class="n">sel</span><span class="p">(</span>
<span class="n">lon</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="n">aoi_lon</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">aoi_lon</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span>
<span class="n">lat</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="n">aoi_lat</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">aoi_lat</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
<span class="n">cali_ts</span>
</code></pre></div></div>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.DataArray 'air_temperature' (time: 1128, lat: 227, lon: 246)>
[62989776 values with dtype=float32]
Coordinates:
* lat (lat) float64 32.56 32.6 32.65 32.69 ... 41.85 41.9 41.94 41.98
* lon (lon) float64 235.6 235.7 235.7 235.8 ... 245.7 245.8 245.8 245.9
* time (time) object 2006-01-15 00:00:00 ... 2099-12-15 00:00:00
Attributes:
long_name: Monthly Average of Daily Maximum Near-Surface Air Tempera...
units: K
grid_mapping: crs
standard_name: air_temperature
height: 2 m
cell_methods: time: maximum(interval: 24 hours);mean over days
_ChunkSizes: [ 10 44 107]</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.DataArray</div><div class="xr-array-name">'air_temperature'</div><ul class="xr-dim-list"><li><span class="xr-has-index">time</span>: 1128</li><li><span class="xr-has-index">lat</span>: 227</li><li><span class="xr-has-index">lon</span>: 246</li></ul></div><ul class="xr-sections"><li class="xr-section-item"><div class="xr-array-wrap"><input id="section-5e2a3de7-46cd-426a-aa97-19e8ec8bd122" class="xr-array-in" type="checkbox" checked="" /><label for="section-5e2a3de7-46cd-426a-aa97-19e8ec8bd122" title="Show/hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-array-preview xr-preview"><span>...</span></div><div class="xr-array-data"><pre>[62989776 values with dtype=float32]</pre></div></div></li><li class="xr-section-item"><input id="section-f0e5a6b5-58bf-493e-b3d9-1b1f9b6d8266" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-f0e5a6b5-58bf-493e-b3d9-1b1f9b6d8266" class="xr-section-summary">Coordinates: <span>(3)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lat</span></div><div class="xr-var-dims">(lat)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">32.56 32.6 32.65 ... 41.94 41.98</div><input id="attrs-18d3b8c6-303a-4da2-9a1a-c3f424ecbe99" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-18d3b8c6-303a-4da2-9a1a-c3f424ecbe99" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-68f406fe-c082-4c9f-9845-6274d88c7f05" class="xr-var-data-in" type="checkbox" /><label for="data-68f406fe-c082-4c9f-9845-6274d88c7f05" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>latitude</dd><dt><span>standard_name :</span></dt><dd>latitude</dd><dt><span>units :</span></dt><dd>degrees_north</dd><dt><span>axis :</span></dt><dd>Y</dd><dt><span>description :</span></dt><dd>Latitude of the center of the grid cell</dd></dl></div><div class="xr-var-data"><pre>array([32.562958, 32.604626, 32.64629 , ..., 41.896141, 41.937809, 41.979473])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lon</span></div><div class="xr-var-dims">(lon)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">235.6 235.7 235.7 ... 245.8 245.9</div><input id="attrs-e135aa1b-54c9-4903-897c-07679879dc93" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-e135aa1b-54c9-4903-897c-07679879dc93" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-6b04bcd9-e2d1-46bf-9f99-b6361868f84a" class="xr-var-data-in" type="checkbox" /><label for="data-6b04bcd9-e2d1-46bf-9f99-b6361868f84a" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>units :</span></dt><dd>degrees_east</dd><dt><span>axis :</span></dt><dd>X</dd><dt><span>description :</span></dt><dd>Longitude of the center of the grid cell</dd><dt><span>long_name :</span></dt><dd>longitude</dd><dt><span>standard_name :</span></dt><dd>longitude</dd></dl></div><div class="xr-var-data"><pre>array([235.644501, 235.686157, 235.727829, ..., 245.769333, 245.811005,
245.852661])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">time</span></div><div class="xr-var-dims">(time)</div><div class="xr-var-dtype">object</div><div class="xr-var-preview xr-preview">2006-01-15 00:00:00 ... 2099-12-...</div><input id="attrs-10f084b7-976e-4895-bfc1-0c4bc0818ac6" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-10f084b7-976e-4895-bfc1-0c4bc0818ac6" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-00f25f94-7c85-4473-9077-c39039eb4e4b" class="xr-var-data-in" type="checkbox" /><label for="data-00f25f94-7c85-4473-9077-c39039eb4e4b" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>description :</span></dt><dd>days since 1900-01-01</dd></dl></div><div class="xr-var-data"><pre>array([cftime.DatetimeNoLeap(2006, 1, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2006, 2, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2006, 3, 15, 0, 0, 0, 0, has_year_zero=True), ...,
cftime.DatetimeNoLeap(2099, 10, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2099, 11, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2099, 12, 15, 0, 0, 0, 0, has_year_zero=True)],
dtype=object)</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-7f483723-7794-49b2-af3a-2f5a020d1605" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-7f483723-7794-49b2-af3a-2f5a020d1605" class="xr-section-summary">Attributes: <span>(7)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>Monthly Average of Daily Maximum Near-Surface Air Temperature</dd><dt><span>units :</span></dt><dd>K</dd><dt><span>grid_mapping :</span></dt><dd>crs</dd><dt><span>standard_name :</span></dt><dd>air_temperature</dd><dt><span>height :</span></dt><dd>2 m</dd><dt><span>cell_methods :</span></dt><dd>time: maximum(interval: 24 hours);mean over days</dd><dt><span>_ChunkSizes :</span></dt><dd>[ 10 44 107]</dd></dl></div></li></ul></div></div>
<p>Calculate a summary of max temperature over time for california</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Time series plot of max temperature per year. for California
# You will get a RuntimeWarning warning here because of nan values...
# This is the max value in each pixel across all months for each year
</span><span class="n">cali_annual_max</span> <span class="o">=</span> <span class="n">cali_ts</span><span class="p">.</span><span class="n">groupby</span><span class="p">(</span><span class="s">'time.year'</span><span class="p">).</span><span class="nb">max</span><span class="p">(</span><span class="n">skipna</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">cali_annual_max</span>
</code></pre></div></div>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.DataArray 'air_temperature' (year: 94, lat: 227, lon: 246)>
array([[[ nan, nan, nan, ..., 314.2675 , 314.05508,
313.95047],
[ nan, nan, nan, ..., 314.08972, 313.86816,
313.9082 ],
[ nan, nan, nan, ..., 314.02127, 313.8809 ,
313.9658 ],
...,
[ nan, nan, nan, ..., 304.58743, 304.74078,
304.8877 ],
[ nan, nan, nan, ..., 303.80627, 303.83685,
304.6529 ],
[ nan, 293.99442, 294.31528, ..., 302.92322, 302.578 ,
303.59604]],
[[ nan, nan, nan, ..., 314.5754 , 314.44275,
314.34146],
[ nan, nan, nan, ..., 314.50024, 314.32486,
314.3367 ],
[ nan, nan, nan, ..., 314.4508 , 314.33475,
314.3861 ],
...
[ nan, nan, nan, ..., 306.28198, 306.4167 ,
306.61166],
[ nan, nan, nan, ..., 305.46466, 305.49435,
306.3104 ],
[ nan, 296.4974 , 296.71976, ..., 304.55817, 304.22375,
305.2304 ]],
[[ nan, nan, nan, ..., 316.17175, 315.9877 ,
315.91992],
[ nan, nan, nan, ..., 316.05585, 315.89587,
315.90164],
[ nan, nan, nan, ..., 316.0458 , 315.94336,
315.96744],
...,
[ nan, nan, nan, ..., 309.6978 , 310.04214,
310.2721 ],
[ nan, nan, nan, ..., 308.9082 , 308.9424 ,
309.8799 ],
[ nan, 296.43567, 296.677 , ..., 307.94104, 307.59906,
308.6584 ]]], dtype=float32)
Coordinates:
* lat (lat) float64 32.56 32.6 32.65 32.69 ... 41.85 41.9 41.94 41.98
* lon (lon) float64 235.6 235.7 235.7 235.8 ... 245.7 245.8 245.8 245.9
* year (year) int64 2006 2007 2008 2009 2010 ... 2095 2096 2097 2098 2099</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.DataArray</div><div class="xr-array-name">'air_temperature'</div><ul class="xr-dim-list"><li><span class="xr-has-index">year</span>: 94</li><li><span class="xr-has-index">lat</span>: 227</li><li><span class="xr-has-index">lon</span>: 246</li></ul></div><ul class="xr-sections"><li class="xr-section-item"><div class="xr-array-wrap"><input id="section-6c2eb64a-fdd6-43e0-b9a2-b01e8c3e3590" class="xr-array-in" type="checkbox" checked="" /><label for="section-6c2eb64a-fdd6-43e0-b9a2-b01e8c3e3590" title="Show/hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-array-preview xr-preview"><span>nan nan nan nan nan nan nan ... 308.3 306.5 307.6 307.9 307.6 308.7</span></div><div class="xr-array-data"><pre>array([[[ nan, nan, nan, ..., 314.2675 , 314.05508,
313.95047],
[ nan, nan, nan, ..., 314.08972, 313.86816,
313.9082 ],
[ nan, nan, nan, ..., 314.02127, 313.8809 ,
313.9658 ],
...,
[ nan, nan, nan, ..., 304.58743, 304.74078,
304.8877 ],
[ nan, nan, nan, ..., 303.80627, 303.83685,
304.6529 ],
[ nan, 293.99442, 294.31528, ..., 302.92322, 302.578 ,
303.59604]],
[[ nan, nan, nan, ..., 314.5754 , 314.44275,
314.34146],
[ nan, nan, nan, ..., 314.50024, 314.32486,
314.3367 ],
[ nan, nan, nan, ..., 314.4508 , 314.33475,
314.3861 ],
...
[ nan, nan, nan, ..., 306.28198, 306.4167 ,
306.61166],
[ nan, nan, nan, ..., 305.46466, 305.49435,
306.3104 ],
[ nan, 296.4974 , 296.71976, ..., 304.55817, 304.22375,
305.2304 ]],
[[ nan, nan, nan, ..., 316.17175, 315.9877 ,
315.91992],
[ nan, nan, nan, ..., 316.05585, 315.89587,
315.90164],
[ nan, nan, nan, ..., 316.0458 , 315.94336,
315.96744],
...,
[ nan, nan, nan, ..., 309.6978 , 310.04214,
310.2721 ],
[ nan, nan, nan, ..., 308.9082 , 308.9424 ,
309.8799 ],
[ nan, 296.43567, 296.677 , ..., 307.94104, 307.59906,
308.6584 ]]], dtype=float32)</pre></div></div></li><li class="xr-section-item"><input id="section-fd34f26d-43c4-42fc-b9a7-0c0f50af14ba" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-fd34f26d-43c4-42fc-b9a7-0c0f50af14ba" class="xr-section-summary">Coordinates: <span>(3)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lat</span></div><div class="xr-var-dims">(lat)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">32.56 32.6 32.65 ... 41.94 41.98</div><input id="attrs-d7ac992d-b121-4d08-b3a4-9b97155b4742" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-d7ac992d-b121-4d08-b3a4-9b97155b4742" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-d73e04d7-1cc2-41fa-95af-5cb374f2ac46" class="xr-var-data-in" type="checkbox" /><label for="data-d73e04d7-1cc2-41fa-95af-5cb374f2ac46" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>latitude</dd><dt><span>standard_name :</span></dt><dd>latitude</dd><dt><span>units :</span></dt><dd>degrees_north</dd><dt><span>axis :</span></dt><dd>Y</dd><dt><span>description :</span></dt><dd>Latitude of the center of the grid cell</dd></dl></div><div class="xr-var-data"><pre>array([32.562958, 32.604626, 32.64629 , ..., 41.896141, 41.937809, 41.979473])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lon</span></div><div class="xr-var-dims">(lon)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">235.6 235.7 235.7 ... 245.8 245.9</div><input id="attrs-8a8109e4-9f59-4be6-ab64-78eed2281efd" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-8a8109e4-9f59-4be6-ab64-78eed2281efd" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-abafa66c-721f-43a0-9655-72667b1ad1c1" class="xr-var-data-in" type="checkbox" /><label for="data-abafa66c-721f-43a0-9655-72667b1ad1c1" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>units :</span></dt><dd>degrees_east</dd><dt><span>axis :</span></dt><dd>X</dd><dt><span>description :</span></dt><dd>Longitude of the center of the grid cell</dd><dt><span>long_name :</span></dt><dd>longitude</dd><dt><span>standard_name :</span></dt><dd>longitude</dd></dl></div><div class="xr-var-data"><pre>array([235.644501, 235.686157, 235.727829, ..., 245.769333, 245.811005,
245.852661])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">year</span></div><div class="xr-var-dims">(year)</div><div class="xr-var-dtype">int64</div><div class="xr-var-preview xr-preview">2006 2007 2008 ... 2097 2098 2099</div><input id="attrs-5bc90550-4671-4f05-97f8-6c1114831919" class="xr-var-attrs-in" type="checkbox" disabled="" /><label for="attrs-5bc90550-4671-4f05-97f8-6c1114831919" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-1ad14865-67c3-40b2-a0d9-0a51fa048f22" class="xr-var-data-in" type="checkbox" /><label for="data-1ad14865-67c3-40b2-a0d9-0a51fa048f22" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"></dl></div><div class="xr-var-data"><pre>array([2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029,
2030, 2031, 2032, 2033, 2034, 2035, 2036, 2037, 2038, 2039, 2040, 2041,
2042, 2043, 2044, 2045, 2046, 2047, 2048, 2049, 2050, 2051, 2052, 2053,
2054, 2055, 2056, 2057, 2058, 2059, 2060, 2061, 2062, 2063, 2064, 2065,
2066, 2067, 2068, 2069, 2070, 2071, 2072, 2073, 2074, 2075, 2076, 2077,
2078, 2079, 2080, 2081, 2082, 2083, 2084, 2085, 2086, 2087, 2088, 2089,
2090, 2091, 2092, 2093, 2094, 2095, 2096, 2097, 2098, 2099])</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-773738a8-db77-49aa-88ea-8c521c0731f0" class="xr-section-summary-in" type="checkbox" disabled="" /><label for="section-773738a8-db77-49aa-88ea-8c521c0731f0" class="xr-section-summary" title="Expand/collapse section">Attributes: <span>(0)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"></dl></div></li></ul></div></div>
<h3 id="calculate-annual-summary-data">Calculate Annual Summary Data</h3>
<p>You can calculate summary values for your aoi using the <code class="language-plaintext highlighter-rouge">.groupby</code>
method. Below you calculate the max value for each raster in the
time series</p>
<p><code class="language-plaintext highlighter-rouge">.max(["lat", "lon"]</code> tells xarray to calculate the max on the entire raster.</p>
<p>the data are grouped by year.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">cali_annual_max_val</span> <span class="o">=</span> <span class="n">cali_annual_max</span><span class="p">.</span><span class="n">groupby</span><span class="p">(</span><span class="s">"year"</span><span class="p">).</span><span class="nb">max</span><span class="p">([</span><span class="s">"lat"</span><span class="p">,</span> <span class="s">"lon"</span><span class="p">])</span>
<span class="n">cali_annual_max_val</span>
</code></pre></div></div>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.DataArray 'air_temperature' (year: 94)>
array([320.67538, 322.2723 , 320.86505, 321.88 , 321.55954, 320.30484,
322.17026, 322.1134 , 322.16568, 321.32007, 321.67624, 322.07996,
321.5724 , 320.7888 , 321.4106 , 320.6512 , 322.2032 , 322.18158,
322.05664, 322.96497, 322.20325, 321.28458, 321.51407, 324.0671 ,
323.02896, 322.10184, 322.73853, 322.56485, 321.98178, 322.4918 ,
322.19852, 322.44962, 321.97635, 322.33276, 322.55652, 322.4373 ,
322.4933 , 323.7776 , 323.3054 , 321.22037, 322.00476, 322.40897,
323.43805, 323.57452, 322.63904, 321.85776, 324.09442, 323.27408,
323.67053, 322.62167, 323.70203, 322.82205, 321.93048, 323.48462,
323.89087, 323.4939 , 324.15057, 322.88406, 322.6229 , 324.0509 ,
323.06467, 324.27808, 324.30505, 323.27487, 323.3609 , 324.03644,
323.1147 , 324.6987 , 322.52747, 323.00424, 324.0726 , 322.78583,
323.49216, 322.4643 , 321.52664, 323.14383, 323.44632, 323.4093 ,
324.51718, 322.68958, 324.83566, 323.24158, 323.22598, 323.8113 ,
323.70255, 321.93524, 325.1739 , 322.6527 , 323.33313, 323.01407,
323.40854, 323.8004 , 321.59985, 324.6846 ], dtype=float32)
Coordinates:
* year (year) int64 2006 2007 2008 2009 2010 ... 2095 2096 2097 2098 2099</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.DataArray</div><div class="xr-array-name">'air_temperature'</div><ul class="xr-dim-list"><li><span class="xr-has-index">year</span>: 94</li></ul></div><ul class="xr-sections"><li class="xr-section-item"><div class="xr-array-wrap"><input id="section-5e702465-a565-48d2-b589-ecbc5e52f652" class="xr-array-in" type="checkbox" checked="" /><label for="section-5e702465-a565-48d2-b589-ecbc5e52f652" title="Show/hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-array-preview xr-preview"><span>320.7 322.3 320.9 321.9 321.6 320.3 ... 323.0 323.4 323.8 321.6 324.7</span></div><div class="xr-array-data"><pre>array([320.67538, 322.2723 , 320.86505, 321.88 , 321.55954, 320.30484,
322.17026, 322.1134 , 322.16568, 321.32007, 321.67624, 322.07996,
321.5724 , 320.7888 , 321.4106 , 320.6512 , 322.2032 , 322.18158,
322.05664, 322.96497, 322.20325, 321.28458, 321.51407, 324.0671 ,
323.02896, 322.10184, 322.73853, 322.56485, 321.98178, 322.4918 ,
322.19852, 322.44962, 321.97635, 322.33276, 322.55652, 322.4373 ,
322.4933 , 323.7776 , 323.3054 , 321.22037, 322.00476, 322.40897,
323.43805, 323.57452, 322.63904, 321.85776, 324.09442, 323.27408,
323.67053, 322.62167, 323.70203, 322.82205, 321.93048, 323.48462,
323.89087, 323.4939 , 324.15057, 322.88406, 322.6229 , 324.0509 ,
323.06467, 324.27808, 324.30505, 323.27487, 323.3609 , 324.03644,
323.1147 , 324.6987 , 322.52747, 323.00424, 324.0726 , 322.78583,
323.49216, 322.4643 , 321.52664, 323.14383, 323.44632, 323.4093 ,
324.51718, 322.68958, 324.83566, 323.24158, 323.22598, 323.8113 ,
323.70255, 321.93524, 325.1739 , 322.6527 , 323.33313, 323.01407,
323.40854, 323.8004 , 321.59985, 324.6846 ], dtype=float32)</pre></div></div></li><li class="xr-section-item"><input id="section-9efb2082-0ba3-4e67-bbcd-bdccb63f35b5" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-9efb2082-0ba3-4e67-bbcd-bdccb63f35b5" class="xr-section-summary">Coordinates: <span>(1)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">year</span></div><div class="xr-var-dims">(year)</div><div class="xr-var-dtype">int64</div><div class="xr-var-preview xr-preview">2006 2007 2008 ... 2097 2098 2099</div><input id="attrs-e5555852-76e9-4278-b32c-9db3ae534c2f" class="xr-var-attrs-in" type="checkbox" disabled="" /><label for="attrs-e5555852-76e9-4278-b32c-9db3ae534c2f" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-dcf09944-49dd-47dc-8f1c-8c7060991ad1" class="xr-var-data-in" type="checkbox" /><label for="data-dcf09944-49dd-47dc-8f1c-8c7060991ad1" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"></dl></div><div class="xr-var-data"><pre>array([2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029,
2030, 2031, 2032, 2033, 2034, 2035, 2036, 2037, 2038, 2039, 2040, 2041,
2042, 2043, 2044, 2045, 2046, 2047, 2048, 2049, 2050, 2051, 2052, 2053,
2054, 2055, 2056, 2057, 2058, 2059, 2060, 2061, 2062, 2063, 2064, 2065,
2066, 2067, 2068, 2069, 2070, 2071, 2072, 2073, 2074, 2075, 2076, 2077,
2078, 2079, 2080, 2081, 2082, 2083, 2084, 2085, 2086, 2087, 2088, 2089,
2090, 2091, 2092, 2093, 2094, 2095, 2096, 2097, 2098, 2099])</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-e63e387e-db03-43b3-acb0-764ff81a0b10" class="xr-section-summary-in" type="checkbox" disabled="" /><label for="section-e63e387e-db03-43b3-acb0-764ff81a0b10" class="xr-section-summary" title="Expand/collapse section">Attributes: <span>(0)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"></dl></div></li></ul></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Plot the data
</span><span class="n">f</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="p">.</span><span class="n">subplots</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">12</span><span class="p">,</span> <span class="mi">6</span><span class="p">))</span>
<span class="n">cali_annual_max_val</span><span class="p">.</span><span class="n">plot</span><span class="p">.</span><span class="n">line</span><span class="p">(</span><span class="n">hue</span><span class="o">=</span><span class="s">'lat'</span><span class="p">,</span>
<span class="n">marker</span><span class="o">=</span><span class="s">"o"</span><span class="p">,</span>
<span class="n">ax</span><span class="o">=</span><span class="n">ax</span><span class="p">,</span>
<span class="n">color</span><span class="o">=</span><span class="s">"grey"</span><span class="p">,</span>
<span class="n">markerfacecolor</span><span class="o">=</span><span class="s">"purple"</span><span class="p">,</span>
<span class="n">markeredgecolor</span><span class="o">=</span><span class="s">"purple"</span><span class="p">)</span>
<span class="n">ax</span><span class="p">.</span><span class="nb">set</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s">"Annual Max Temperature (K) in California"</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-05-spatial-subsets/2020-10-16-netcdf-05-spatial-subsets_25_0.png" alt="Scatter plot showing the annual max temperature as projected in the California from 2009 to 2100 using MACA v2 climate data." />
<figcaption>Scatter plot showing the annual max temperature as projected in the California from 2009 to 2100 using MACA v2 climate data.</figcaption>
</figure>
<h2 id="subset-a-netcdf4-using-a-shapefile-feature-or-features">Subset a netcdf4 Using a Shapefile Feature or Features</h2>
<p>Above you created a rectangular spatial subset and plotted the data.
Sometimes however you may have an AOI that is a specific polygon boundary.
Below you will modify your subset to represent just the region of the
state of California. You will use the <code class="language-plaintext highlighter-rouge">regionmask</code> package to create
this mask.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># This is the AOI of interest. You only want to calculate summary values for
# pixels within this AOI rather the entire rectangular spatial extent.
</span><span class="n">f</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="p">.</span><span class="n">subplots</span><span class="p">()</span>
<span class="n">cali_aoi</span><span class="p">.</span><span class="n">plot</span><span class="p">(</span><span class="n">ax</span><span class="o">=</span><span class="n">ax</span><span class="p">)</span>
<span class="n">ax</span><span class="p">.</span><span class="nb">set</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s">"California AOI Subset"</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-05-spatial-subsets/2020-10-16-netcdf-05-spatial-subsets_27_0.png" alt="Plot showing the area of interest - California." />
<figcaption>Plot showing the area of interest - California.</figcaption>
</figure>
<p>You will use the cali_aoi object created above to first create a
mask.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">cali_aoi</span>
</code></pre></div></div>
<div class="output execute_result">
<style scoped="">
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>featurecla</th>
<th>scalerank</th>
<th>adm1_code</th>
<th>diss_me</th>
<th>iso_3166_2</th>
<th>wikipedia</th>
<th>iso_a2</th>
<th>adm0_sr</th>
<th>name</th>
<th>name_alt</th>
<th>...</th>
<th>name_nl</th>
<th>name_pl</th>
<th>name_pt</th>
<th>name_ru</th>
<th>name_sv</th>
<th>name_tr</th>
<th>name_vi</th>
<th>name_zh</th>
<th>ne_id</th>
<th>geometry</th>
</tr>
</thead>
<tbody>
<tr>
<th>53</th>
<td>Admin-1 scale rank</td>
<td>2</td>
<td>USA-3521</td>
<td>3521</td>
<td>US-CA</td>
<td>http://en.wikipedia.org/wiki/California</td>
<td>US</td>
<td>8</td>
<td>California</td>
<td>CA|Calif.</td>
<td>...</td>
<td>Californië</td>
<td>Kalifornia</td>
<td>Califórnia</td>
<td>Калифорния</td>
<td>Kalifornien</td>
<td>Kaliforniya</td>
<td>California</td>
<td>加利福尼亚州</td>
<td>1159308415</td>
<td>MULTIPOLYGON (((-114.61054 34.99112, -114.6109...</td>
</tr>
</tbody>
</table>
<p>1 rows × 84 columns</p>
</div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code><regionmask.Regions>
Name: name
Regions:
53 US-CA California
[1 regions]
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-05-spatial-subsets/2020-10-16-netcdf-05-spatial-subsets_33_0.png" alt="Plot showing the mask region for the AOI - California, USA." />
<figcaption>Plot showing the mask region for the AOI - California, USA.</figcaption>
</figure>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.DataArray 'region' (lat: 585, lon: 1386)>
array([[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
...,
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan]])
Coordinates:
* lat (lat) float64 25.06 25.1 25.15 25.19 ... 49.27 49.31 49.35 49.4
* lon (lon) float64 235.2 235.3 235.3 235.4 ... 292.8 292.9 292.9 292.9</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.DataArray</div><div class="xr-array-name">'region'</div><ul class="xr-dim-list"><li><span class="xr-has-index">lat</span>: 585</li><li><span class="xr-has-index">lon</span>: 1386</li></ul></div><ul class="xr-sections"><li class="xr-section-item"><div class="xr-array-wrap"><input id="section-7bb21f9c-5556-480b-b441-6829fb38a1d5" class="xr-array-in" type="checkbox" checked="" /><label for="section-7bb21f9c-5556-480b-b441-6829fb38a1d5" title="Show/hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-array-preview xr-preview"><span>nan nan nan nan nan nan nan nan ... nan nan nan nan nan nan nan nan</span></div><div class="xr-array-data"><pre>array([[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
...,
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan]])</pre></div></div></li><li class="xr-section-item"><input id="section-10b05e64-bb3f-4bee-a814-5290906d22f2" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-10b05e64-bb3f-4bee-a814-5290906d22f2" class="xr-section-summary">Coordinates: <span>(2)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lat</span></div><div class="xr-var-dims">(lat)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">25.06 25.1 25.15 ... 49.35 49.4</div><input id="attrs-4e00f016-acd3-43df-a461-0c8bd362e17d" class="xr-var-attrs-in" type="checkbox" disabled="" /><label for="attrs-4e00f016-acd3-43df-a461-0c8bd362e17d" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-ed2a98ef-bec0-428f-8d31-77e1f4ad8181" class="xr-var-data-in" type="checkbox" /><label for="data-ed2a98ef-bec0-428f-8d31-77e1f4ad8181" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"></dl></div><div class="xr-var-data"><pre>array([25.063078, 25.104744, 25.14641 , ..., 49.312691, 49.354359, 49.396023])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lon</span></div><div class="xr-var-dims">(lon)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">235.2 235.3 235.3 ... 292.9 292.9</div><input id="attrs-54a46074-c6d5-42af-843d-a1400477bb04" class="xr-var-attrs-in" type="checkbox" disabled="" /><label for="attrs-54a46074-c6d5-42af-843d-a1400477bb04" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-94acb56d-526a-4d88-90d2-102d522ddbe1" class="xr-var-data-in" type="checkbox" /><label for="data-94acb56d-526a-4d88-90d2-102d522ddbe1" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"></dl></div><div class="xr-var-data"><pre>array([235.227844, 235.269501, 235.311157, ..., 292.851929, 292.893585,
292.935242])</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-8676b21b-de7d-49e8-8ffc-17885bc674c3" class="xr-section-summary-in" type="checkbox" disabled="" /><label for="section-8676b21b-de7d-49e8-8ffc-17885bc674c3" class="xr-section-summary" title="Expand/collapse section">Attributes: <span>(0)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"></dl></div></li></ul></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>(<xarray.DataArray 'region' ()>
array(53.),
<xarray.DataArray 'region' ()>
array(53.))
</code></pre></div></div>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.DataArray 'air_temperature' (time: 2, lat: 227, lon: 246)>
array([[[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
...,
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan]],
[[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
...,
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan]]], dtype=float32)
Coordinates:
* lat (lat) float64 32.56 32.6 32.65 32.69 ... 41.85 41.9 41.94 41.98
* lon (lon) float64 235.6 235.7 235.7 235.8 ... 245.7 245.8 245.8 245.9
* time (time) object 2010-01-15 00:00:00 2010-02-15 00:00:00
Attributes:
long_name: Monthly Average of Daily Maximum Near-Surface Air Tempera...
units: K
grid_mapping: crs
standard_name: air_temperature
height: 2 m
cell_methods: time: maximum(interval: 24 hours);mean over days
_ChunkSizes: [ 10 44 107]</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.DataArray</div><div class="xr-array-name">'air_temperature'</div><ul class="xr-dim-list"><li><span class="xr-has-index">time</span>: 2</li><li><span class="xr-has-index">lat</span>: 227</li><li><span class="xr-has-index">lon</span>: 246</li></ul></div><ul class="xr-sections"><li class="xr-section-item"><div class="xr-array-wrap"><input id="section-f9434393-84c4-4d1f-928e-214970c81fdf" class="xr-array-in" type="checkbox" checked="" /><label for="section-f9434393-84c4-4d1f-928e-214970c81fdf" title="Show/hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-array-preview xr-preview"><span>nan nan nan nan nan nan nan nan ... nan nan nan nan nan nan nan nan</span></div><div class="xr-array-data"><pre>array([[[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
...,
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan]],
[[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
...,
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan]]], dtype=float32)</pre></div></div></li><li class="xr-section-item"><input id="section-a8d01af2-25b1-40e8-96b3-998b978cf13d" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-a8d01af2-25b1-40e8-96b3-998b978cf13d" class="xr-section-summary">Coordinates: <span>(3)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lat</span></div><div class="xr-var-dims">(lat)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">32.56 32.6 32.65 ... 41.94 41.98</div><input id="attrs-6747dfc7-8878-4cb8-85b2-967d4ca16e6b" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-6747dfc7-8878-4cb8-85b2-967d4ca16e6b" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-2fb252c1-3ee0-4233-987a-fea1ccbdfc74" class="xr-var-data-in" type="checkbox" /><label for="data-2fb252c1-3ee0-4233-987a-fea1ccbdfc74" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>latitude</dd><dt><span>standard_name :</span></dt><dd>latitude</dd><dt><span>units :</span></dt><dd>degrees_north</dd><dt><span>axis :</span></dt><dd>Y</dd><dt><span>description :</span></dt><dd>Latitude of the center of the grid cell</dd></dl></div><div class="xr-var-data"><pre>array([32.562958, 32.604626, 32.64629 , ..., 41.896141, 41.937809, 41.979473])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lon</span></div><div class="xr-var-dims">(lon)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">235.6 235.7 235.7 ... 245.8 245.9</div><input id="attrs-3d715cc8-0938-499a-a291-f36759bab624" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-3d715cc8-0938-499a-a291-f36759bab624" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-4bf661d5-7aae-4a80-bbb0-a93ecc660fb7" class="xr-var-data-in" type="checkbox" /><label for="data-4bf661d5-7aae-4a80-bbb0-a93ecc660fb7" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>units :</span></dt><dd>degrees_east</dd><dt><span>axis :</span></dt><dd>X</dd><dt><span>description :</span></dt><dd>Longitude of the center of the grid cell</dd><dt><span>long_name :</span></dt><dd>longitude</dd><dt><span>standard_name :</span></dt><dd>longitude</dd></dl></div><div class="xr-var-data"><pre>array([235.644501, 235.686157, 235.727829, ..., 245.769333, 245.811005,
245.852661])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">time</span></div><div class="xr-var-dims">(time)</div><div class="xr-var-dtype">object</div><div class="xr-var-preview xr-preview">2010-01-15 00:00:00 2010-02-15 0...</div><input id="attrs-43b75f6f-804f-430e-b0f1-e2f604385f28" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-43b75f6f-804f-430e-b0f1-e2f604385f28" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-aad22fb0-9713-4724-8c16-a53ac7dee0b2" class="xr-var-data-in" type="checkbox" /><label for="data-aad22fb0-9713-4724-8c16-a53ac7dee0b2" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>description :</span></dt><dd>days since 1900-01-01</dd></dl></div><div class="xr-var-data"><pre>array([cftime.DatetimeNoLeap(2010, 1, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2010, 2, 15, 0, 0, 0, 0, has_year_zero=True)],
dtype=object)</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-ef0715f7-571c-44c9-9240-bea33a64b313" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-ef0715f7-571c-44c9-9240-bea33a64b313" class="xr-section-summary">Attributes: <span>(7)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>Monthly Average of Daily Maximum Near-Surface Air Temperature</dd><dt><span>units :</span></dt><dd>K</dd><dt><span>grid_mapping :</span></dt><dd>crs</dd><dt><span>standard_name :</span></dt><dd>air_temperature</dd><dt><span>height :</span></dt><dd>2 m</dd><dt><span>cell_methods :</span></dt><dd>time: maximum(interval: 24 hours);mean over days</dd><dt><span>_ChunkSizes :</span></dt><dd>[ 10 44 107]</dd></dl></div></li></ul></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-05-spatial-subsets/2020-10-16-netcdf-05-spatial-subsets_39_0.png" alt="Map showing mean temperature for California in Januray and Feb 2010." />
<figcaption>Map showing mean temperature for California in Januray and Feb 2010.</figcaption>
</figure>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.DataArray 'air_temperature' (time: 2)>
array([286.05795, 286.02655], dtype=float32)
Coordinates:
* time (time) object 2010-01-15 00:00:00 2010-02-15 00:00:00</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.DataArray</div><div class="xr-array-name">'air_temperature'</div><ul class="xr-dim-list"><li><span class="xr-has-index">time</span>: 2</li></ul></div><ul class="xr-sections"><li class="xr-section-item"><div class="xr-array-wrap"><input id="section-6d4bcf55-82a8-425d-87f6-a4b00c84794b" class="xr-array-in" type="checkbox" checked="" /><label for="section-6d4bcf55-82a8-425d-87f6-a4b00c84794b" title="Show/hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-array-preview xr-preview"><span>286.1 286.0</span></div><div class="xr-array-data"><pre>array([286.05795, 286.02655], dtype=float32)</pre></div></div></li><li class="xr-section-item"><input id="section-834876b6-a5cf-445a-987c-404dcf8e8dba" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-834876b6-a5cf-445a-987c-404dcf8e8dba" class="xr-section-summary">Coordinates: <span>(1)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">time</span></div><div class="xr-var-dims">(time)</div><div class="xr-var-dtype">object</div><div class="xr-var-preview xr-preview">2010-01-15 00:00:00 2010-02-15 0...</div><input id="attrs-32e6755e-5f3c-455b-9878-8c852eb98f19" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-32e6755e-5f3c-455b-9878-8c852eb98f19" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-1330502b-fcba-47e0-a6f6-3c327d589fe7" class="xr-var-data-in" type="checkbox" /><label for="data-1330502b-fcba-47e0-a6f6-3c327d589fe7" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>description :</span></dt><dd>days since 1900-01-01</dd></dl></div><div class="xr-var-data"><pre>array([cftime.DatetimeNoLeap(2010, 1, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2010, 2, 15, 0, 0, 0, 0, has_year_zero=True)],
dtype=object)</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-bff1a054-7439-435f-9d1d-0db0f1073c40" class="xr-section-summary-in" type="checkbox" disabled="" /><label for="section-bff1a054-7439-435f-9d1d-0db0f1073c40" class="xr-section-summary" title="Expand/collapse section">Attributes: <span>(0)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"></dl></div></li></ul></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Create a 3d mask - this contains the true / false values identifying pixels
# inside vs outside of the mask region
</span><span class="n">cali_mask</span> <span class="o">=</span> <span class="n">regionmask</span><span class="p">.</span><span class="n">mask_3D_geopandas</span><span class="p">(</span><span class="n">cali_aoi</span><span class="p">,</span>
<span class="n">monthly_forecast_temp_xr</span><span class="p">.</span><span class="n">lon</span><span class="p">,</span>
<span class="n">monthly_forecast_temp_xr</span><span class="p">.</span><span class="n">lat</span><span class="p">)</span>
<span class="n">cali_mask</span>
</code></pre></div></div>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.DataArray 'region' (region: 1, lat: 585, lon: 1386)>
array([[[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
...,
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False]]])
Coordinates:
* lat (lat) float64 25.06 25.1 25.15 25.19 ... 49.27 49.31 49.35 49.4
* lon (lon) float64 235.2 235.3 235.3 235.4 ... 292.8 292.9 292.9 292.9
* region (region) int64 53</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.DataArray</div><div class="xr-array-name">'region'</div><ul class="xr-dim-list"><li><span class="xr-has-index">region</span>: 1</li><li><span class="xr-has-index">lat</span>: 585</li><li><span class="xr-has-index">lon</span>: 1386</li></ul></div><ul class="xr-sections"><li class="xr-section-item"><div class="xr-array-wrap"><input id="section-aafd0202-2290-4cb0-99cf-f0b88757381c" class="xr-array-in" type="checkbox" checked="" /><label for="section-aafd0202-2290-4cb0-99cf-f0b88757381c" title="Show/hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-array-preview xr-preview"><span>False False False False False False ... False False False False False</span></div><div class="xr-array-data"><pre>array([[[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
...,
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False]]])</pre></div></div></li><li class="xr-section-item"><input id="section-61647b74-cf96-4bc1-aed0-21c3e6ac1ccf" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-61647b74-cf96-4bc1-aed0-21c3e6ac1ccf" class="xr-section-summary">Coordinates: <span>(3)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lat</span></div><div class="xr-var-dims">(lat)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">25.06 25.1 25.15 ... 49.35 49.4</div><input id="attrs-fdaeb160-7b2c-4be6-8295-ac98cbee5729" class="xr-var-attrs-in" type="checkbox" disabled="" /><label for="attrs-fdaeb160-7b2c-4be6-8295-ac98cbee5729" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-277a778c-5574-4807-b364-2943f66b96d4" class="xr-var-data-in" type="checkbox" /><label for="data-277a778c-5574-4807-b364-2943f66b96d4" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"></dl></div><div class="xr-var-data"><pre>array([25.063078, 25.104744, 25.14641 , ..., 49.312691, 49.354359, 49.396023])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lon</span></div><div class="xr-var-dims">(lon)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">235.2 235.3 235.3 ... 292.9 292.9</div><input id="attrs-c62e5154-b866-408d-928c-c71783ee7380" class="xr-var-attrs-in" type="checkbox" disabled="" /><label for="attrs-c62e5154-b866-408d-928c-c71783ee7380" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-8f6c1039-6a75-44ec-9984-ddf888ee310d" class="xr-var-data-in" type="checkbox" /><label for="data-8f6c1039-6a75-44ec-9984-ddf888ee310d" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"></dl></div><div class="xr-var-data"><pre>array([235.227844, 235.269501, 235.311157, ..., 292.851929, 292.893585,
292.935242])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">region</span></div><div class="xr-var-dims">(region)</div><div class="xr-var-dtype">int64</div><div class="xr-var-preview xr-preview">53</div><input id="attrs-42d54710-03eb-48f1-8754-e97d6dd3936a" class="xr-var-attrs-in" type="checkbox" disabled="" /><label for="attrs-42d54710-03eb-48f1-8754-e97d6dd3936a" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-58058141-b283-47c5-b5b8-125b2711afca" class="xr-var-data-in" type="checkbox" /><label for="data-58058141-b283-47c5-b5b8-125b2711afca" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"></dl></div><div class="xr-var-data"><pre>array([53])</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-c6aa5ba5-19e2-42fd-ac58-4a025119e0b6" class="xr-section-summary-in" type="checkbox" disabled="" /><label for="section-c6aa5ba5-19e2-42fd-ac58-4a025119e0b6" class="xr-section-summary" title="Expand/collapse section">Attributes: <span>(0)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"></dl></div></li></ul></div></div>
<p>To make processing a bit quicker, below you slice out two months of the data.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Slice out two months of data
</span><span class="n">two_months</span> <span class="o">=</span> <span class="n">monthly_forecast_temp_xr</span><span class="p">.</span><span class="n">sel</span><span class="p">(</span>
<span class="n">time</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="s">'2099-10-25'</span><span class="p">,</span> <span class="s">'2099-12-15'</span><span class="p">))</span>
</code></pre></div></div>
<p>Next, apply the mask for just the state of California to the data.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Apply the mask for California to the data
</span><span class="n">two_months</span> <span class="o">=</span> <span class="n">two_months</span><span class="p">.</span><span class="n">where</span><span class="p">(</span><span class="n">cali_mask</span><span class="p">)</span>
<span class="n">two_months</span>
</code></pre></div></div>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.Dataset>
Dimensions: (time: 2, lat: 585, lon: 1386, region: 1, crs: 1)
Coordinates:
* lat (lat) float64 25.06 25.1 25.15 25.19 ... 49.31 49.35 49.4
* crs (crs) int32 1
* lon (lon) float64 235.2 235.3 235.3 235.4 ... 292.9 292.9 292.9
* time (time) object 2099-11-15 00:00:00 2099-12-15 00:00:00
* region (region) int64 53
Data variables:
air_temperature (time, lat, lon, region) float32 nan nan nan ... nan nan
Attributes: (12/46)
description: Multivariate Adaptive Constructed Analog...
id: MACAv2-METDATA
naming_authority: edu.uidaho.reacch
Metadata_Conventions: Unidata Dataset Discovery v1.0
Metadata_Link:
cdm_data_type: FLOAT
... ...
contributor_role: Postdoctoral Fellow
publisher_name: REACCH
publisher_email: reacch@uidaho.edu
publisher_url: http://www.reacchpna.org/
license: Creative Commons CC0 1.0 Universal Dedic...
coordinate_system: WGS84,EPSG:4326</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.Dataset</div></div><ul class="xr-sections"><li class="xr-section-item"><input id="section-37c33558-5922-4143-b10f-d75ae9abda45" class="xr-section-summary-in" type="checkbox" disabled="" /><label for="section-37c33558-5922-4143-b10f-d75ae9abda45" class="xr-section-summary" title="Expand/collapse section">Dimensions:</label><div class="xr-section-inline-details"><ul class="xr-dim-list"><li><span class="xr-has-index">time</span>: 2</li><li><span class="xr-has-index">lat</span>: 585</li><li><span class="xr-has-index">lon</span>: 1386</li><li><span class="xr-has-index">region</span>: 1</li><li><span class="xr-has-index">crs</span>: 1</li></ul></div><div class="xr-section-details"></div></li><li class="xr-section-item"><input id="section-8300b770-77ef-4dbb-8daa-a19e9ac72201" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-8300b770-77ef-4dbb-8daa-a19e9ac72201" class="xr-section-summary">Coordinates: <span>(5)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lat</span></div><div class="xr-var-dims">(lat)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">25.06 25.1 25.15 ... 49.35 49.4</div><input id="attrs-066b69c8-3cb0-4e98-9171-f66fdc2f8c67" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-066b69c8-3cb0-4e98-9171-f66fdc2f8c67" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-dcaacb1a-eaf7-45a8-8a9c-5ad3a41e646d" class="xr-var-data-in" type="checkbox" /><label for="data-dcaacb1a-eaf7-45a8-8a9c-5ad3a41e646d" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>latitude</dd><dt><span>standard_name :</span></dt><dd>latitude</dd><dt><span>units :</span></dt><dd>degrees_north</dd><dt><span>axis :</span></dt><dd>Y</dd><dt><span>description :</span></dt><dd>Latitude of the center of the grid cell</dd></dl></div><div class="xr-var-data"><pre>array([25.063078, 25.104744, 25.14641 , ..., 49.312691, 49.354359, 49.396023])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">crs</span></div><div class="xr-var-dims">(crs)</div><div class="xr-var-dtype">int32</div><div class="xr-var-preview xr-preview">1</div><input id="attrs-ac7e3911-3ab7-4b18-ae89-cfeae39e2944" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-ac7e3911-3ab7-4b18-ae89-cfeae39e2944" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-5792a670-41bf-4f3a-a462-eb37342256b8" class="xr-var-data-in" type="checkbox" /><label for="data-5792a670-41bf-4f3a-a462-eb37342256b8" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>grid_mapping_name :</span></dt><dd>latitude_longitude</dd><dt><span>longitude_of_prime_meridian :</span></dt><dd>0.0</dd><dt><span>semi_major_axis :</span></dt><dd>6378137.0</dd><dt><span>inverse_flattening :</span></dt><dd>298.257223563</dd></dl></div><div class="xr-var-data"><pre>array([1], dtype=int32)</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lon</span></div><div class="xr-var-dims">(lon)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">235.2 235.3 235.3 ... 292.9 292.9</div><input id="attrs-ce0fad86-7ca4-46f0-aeea-579e2e176a17" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-ce0fad86-7ca4-46f0-aeea-579e2e176a17" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-8de77f4f-e5c5-496d-a490-e1f28d840e21" class="xr-var-data-in" type="checkbox" /><label for="data-8de77f4f-e5c5-496d-a490-e1f28d840e21" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>units :</span></dt><dd>degrees_east</dd><dt><span>axis :</span></dt><dd>X</dd><dt><span>description :</span></dt><dd>Longitude of the center of the grid cell</dd><dt><span>long_name :</span></dt><dd>longitude</dd><dt><span>standard_name :</span></dt><dd>longitude</dd></dl></div><div class="xr-var-data"><pre>array([235.227844, 235.269501, 235.311157, ..., 292.851929, 292.893585,
292.935242])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">time</span></div><div class="xr-var-dims">(time)</div><div class="xr-var-dtype">object</div><div class="xr-var-preview xr-preview">2099-11-15 00:00:00 2099-12-15 0...</div><input id="attrs-f665bf38-4e38-42ac-8fe2-dbaa3da29109" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-f665bf38-4e38-42ac-8fe2-dbaa3da29109" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-be85d414-9574-4fe2-8d13-f11595843150" class="xr-var-data-in" type="checkbox" /><label for="data-be85d414-9574-4fe2-8d13-f11595843150" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>description :</span></dt><dd>days since 1900-01-01</dd></dl></div><div class="xr-var-data"><pre>array([cftime.DatetimeNoLeap(2099, 11, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2099, 12, 15, 0, 0, 0, 0, has_year_zero=True)],
dtype=object)</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">region</span></div><div class="xr-var-dims">(region)</div><div class="xr-var-dtype">int64</div><div class="xr-var-preview xr-preview">53</div><input id="attrs-be2c44a5-1977-485f-b295-a7a05b379768" class="xr-var-attrs-in" type="checkbox" disabled="" /><label for="attrs-be2c44a5-1977-485f-b295-a7a05b379768" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-51d7ede6-c70e-487a-9dfe-11d3ba66f364" class="xr-var-data-in" type="checkbox" /><label for="data-51d7ede6-c70e-487a-9dfe-11d3ba66f364" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"></dl></div><div class="xr-var-data"><pre>array([53])</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-ede7dc24-f52b-43c4-9943-ee794826a8fe" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-ede7dc24-f52b-43c4-9943-ee794826a8fe" class="xr-section-summary">Data variables: <span>(1)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span>air_temperature</span></div><div class="xr-var-dims">(time, lat, lon, region)</div><div class="xr-var-dtype">float32</div><div class="xr-var-preview xr-preview">nan nan nan nan ... nan nan nan nan</div><input id="attrs-9cfaf4f0-8e8f-4a34-bd1b-ed78171845c2" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-9cfaf4f0-8e8f-4a34-bd1b-ed78171845c2" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-7b930835-ff6a-41d6-86aa-d367a56da9ef" class="xr-var-data-in" type="checkbox" /><label for="data-7b930835-ff6a-41d6-86aa-d367a56da9ef" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>Monthly Average of Daily Maximum Near-Surface Air Temperature</dd><dt><span>units :</span></dt><dd>K</dd><dt><span>grid_mapping :</span></dt><dd>crs</dd><dt><span>standard_name :</span></dt><dd>air_temperature</dd><dt><span>height :</span></dt><dd>2 m</dd><dt><span>cell_methods :</span></dt><dd>time: maximum(interval: 24 hours);mean over days</dd><dt><span>_ChunkSizes :</span></dt><dd>[ 10 44 107]</dd></dl></div><div class="xr-var-data"><pre>array([[[[nan],
[nan],
[nan],
...,
[nan],
[nan],
[nan]],
[[nan],
[nan],
[nan],
...,
[nan],
[nan],
[nan]],
[[nan],
[nan],
[nan],
...,
...
...,
[nan],
[nan],
[nan]],
[[nan],
[nan],
[nan],
...,
[nan],
[nan],
[nan]],
[[nan],
[nan],
[nan],
...,
[nan],
[nan],
[nan]]]], dtype=float32)</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-09526e6f-0dc9-4803-91d9-514fc3d2bb96" class="xr-section-summary-in" type="checkbox" /><label for="section-09526e6f-0dc9-4803-91d9-514fc3d2bb96" class="xr-section-summary">Attributes: <span>(46)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"><dt><span>description :</span></dt><dd>Multivariate Adaptive Constructed Analogs (MACA) method, version 2.3,Dec 2013.</dd><dt><span>id :</span></dt><dd>MACAv2-METDATA</dd><dt><span>naming_authority :</span></dt><dd>edu.uidaho.reacch</dd><dt><span>Metadata_Conventions :</span></dt><dd>Unidata Dataset Discovery v1.0</dd><dt><span>Metadata_Link :</span></dt><dd></dd><dt><span>cdm_data_type :</span></dt><dd>FLOAT</dd><dt><span>title :</span></dt><dd>Monthly aggregation of downscaled daily meteorological data of Monthly Average of Daily Maximum Near-Surface Air Temperature from College of Global Change and Earth System Science, Beijing Normal University (BNU-ESM) using the run r1i1p1 of the rcp45 scenario.</dd><dt><span>summary :</span></dt><dd>This archive contains monthly downscaled meteorological and hydrological projections for the Conterminous United States at 1/24-deg resolution. These monthly values are obtained by aggregating the daily values obtained from the downscaling using the Multivariate Adaptive Constructed Analogs (MACA, Abatzoglou, 2012) statistical downscaling method with the METDATA (Abatzoglou,2013) training dataset. The downscaled meteorological variables are maximum/minimum temperature(tasmax/tasmin), maximum/minimum relative humidity (rhsmax/rhsmin),precipitation amount(pr), downward shortwave solar radiation(rsds), eastward wind(uas), northward wind(vas), and specific humidity(huss). The downscaling is based on the 365-day model outputs from different global climate models (GCMs) from Phase 5 of the Coupled Model Inter-comparison Project (CMIP3) utlizing the historical (1950-2005) and future RCP4.5/8.5(2006-2099) scenarios. </dd><dt><span>keywords :</span></dt><dd>monthly, precipitation, maximum temperature, minimum temperature, downward shortwave solar radiation, specific humidity, wind velocity, CMIP5, Gridded Meteorological Data</dd><dt><span>keywords_vocabulary :</span></dt><dd></dd><dt><span>standard_name_vocabulary :</span></dt><dd>CF-1.0</dd><dt><span>history :</span></dt><dd>No revisions.</dd><dt><span>comment :</span></dt><dd></dd><dt><span>geospatial_bounds :</span></dt><dd>POLYGON((-124.7722 25.0631,-124.7722 49.3960, -67.0648 49.3960,-67.0648, 25.0631, -124.7722,25.0631))</dd><dt><span>geospatial_lat_min :</span></dt><dd>25.0631</dd><dt><span>geospatial_lat_max :</span></dt><dd>49.3960</dd><dt><span>geospatial_lon_min :</span></dt><dd>-124.7722</dd><dt><span>geospatial_lon_max :</span></dt><dd>-67.0648</dd><dt><span>geospatial_lat_units :</span></dt><dd>decimal degrees north</dd><dt><span>geospatial_lon_units :</span></dt><dd>decimal degrees east</dd><dt><span>geospatial_lat_resolution :</span></dt><dd>0.0417</dd><dt><span>geospatial_lon_resolution :</span></dt><dd>0.0417</dd><dt><span>geospatial_vertical_min :</span></dt><dd>0.0</dd><dt><span>geospatial_vertical_max :</span></dt><dd>0.0</dd><dt><span>geospatial_vertical_resolution :</span></dt><dd>0.0</dd><dt><span>geospatial_vertical_positive :</span></dt><dd>up</dd><dt><span>time_coverage_start :</span></dt><dd>2091-01-01T00:0</dd><dt><span>time_coverage_end :</span></dt><dd>2095-12-31T00:00</dd><dt><span>time_coverage_duration :</span></dt><dd>P5Y</dd><dt><span>time_coverage_resolution :</span></dt><dd>P1M</dd><dt><span>date_created :</span></dt><dd>2014-05-15</dd><dt><span>date_modified :</span></dt><dd>2014-05-15</dd><dt><span>date_issued :</span></dt><dd>2014-05-15</dd><dt><span>creator_name :</span></dt><dd>John Abatzoglou</dd><dt><span>creator_url :</span></dt><dd>http://maca.northwestknowledge.net</dd><dt><span>creator_email :</span></dt><dd>jabatzoglou@uidaho.edu</dd><dt><span>institution :</span></dt><dd>University of Idaho</dd><dt><span>processing_level :</span></dt><dd>GRID</dd><dt><span>project :</span></dt><dd></dd><dt><span>contributor_name :</span></dt><dd>Katherine C. Hegewisch</dd><dt><span>contributor_role :</span></dt><dd>Postdoctoral Fellow</dd><dt><span>publisher_name :</span></dt><dd>REACCH</dd><dt><span>publisher_email :</span></dt><dd>reacch@uidaho.edu</dd><dt><span>publisher_url :</span></dt><dd>http://www.reacchpna.org/</dd><dt><span>license :</span></dt><dd>Creative Commons CC0 1.0 Universal Dedication(http://creativecommons.org/publicdomain/zero/1.0/legalcode)</dd><dt><span>coordinate_system :</span></dt><dd>WGS84,EPSG:4326</dd></dl></div></li></ul></div></div>
<p>Below you can see that the data for only the region of interest -
California, is plotted. However, you have a lot of extra white space
surrounding the data.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">two_months</span><span class="p">[</span><span class="s">"air_temperature"</span><span class="p">].</span><span class="n">plot</span><span class="p">(</span><span class="n">col</span><span class="o">=</span><span class="s">'time'</span><span class="p">,</span>
<span class="n">col_wrap</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
<span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">))</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-05-spatial-subsets/2020-10-16-netcdf-05-spatial-subsets_47_0.png" alt="Map showing temperature values where California is masked however the data are not subsetted so you have significant additional white space / no data values in the data." />
<figcaption>Map showing temperature values where California is masked however the data are not subsetted so you have significant additional white space / no data values in the data.</figcaption>
</figure>
<p>If you slice our your data by time and lat lon and apply the mask for your
AOI you get the d</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">two_months_masked</span> <span class="o">=</span> <span class="n">monthly_forecast_temp_xr</span><span class="p">[</span><span class="s">"air_temperature"</span><span class="p">].</span><span class="n">sel</span><span class="p">(</span><span class="n">time</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="s">'2099-10-25'</span><span class="p">,</span>
<span class="s">'2099-12-15'</span><span class="p">),</span>
<span class="n">lon</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="n">aoi_lon</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
<span class="n">aoi_lon</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span>
<span class="n">lat</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="n">aoi_lat</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
<span class="n">aoi_lat</span><span class="p">[</span><span class="mi">1</span><span class="p">])).</span><span class="n">where</span><span class="p">(</span><span class="n">cali_mask</span><span class="p">)</span>
<span class="n">two_months_masked</span><span class="p">.</span><span class="n">dims</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>('time', 'lat', 'lon', 'region')
</code></pre></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">two_months_masked</span><span class="p">.</span><span class="n">plot</span><span class="p">(</span><span class="n">col</span><span class="o">=</span><span class="s">'time'</span><span class="p">,</span> <span class="n">col_wrap</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-05-spatial-subsets/2020-10-16-netcdf-05-spatial-subsets_50_0.png" alt="Map showing temperature for California after the data have been masked and sliced by AOI." />
<figcaption>Map showing temperature for California after the data have been masked and sliced by AOI.</figcaption>
</figure>
<h2 id="workflow-with-multiple-regions">Workflow with Multiple Regions</h2>
<p>In the example above, you had one region of interest. Below you will implement a
workflow where you have multiple regions. For this example, you will subset multiple states
from your shapefile.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Start by extracting a few states from the states_gdf
</span><span class="n">states_gdf</span><span class="p">[</span><span class="s">"name"</span><span class="p">]</span>
<span class="n">cali_or_wash_nev</span> <span class="o">=</span> <span class="n">states_gdf</span><span class="p">[</span><span class="n">states_gdf</span><span class="p">.</span><span class="n">name</span><span class="p">.</span><span class="n">isin</span><span class="p">(</span>
<span class="p">[</span><span class="s">"California"</span><span class="p">,</span> <span class="s">"Oregon"</span><span class="p">,</span> <span class="s">"Washington"</span><span class="p">,</span> <span class="s">"Nevada"</span><span class="p">])]</span>
<span class="n">cali_or_wash_nev</span><span class="p">.</span><span class="n">plot</span><span class="p">()</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-05-spatial-subsets/2020-10-16-netcdf-05-spatial-subsets_52_0.png" alt="Map showing a new study area containing multiple AOI's which in this case are 4 states in the western United States." />
<figcaption>Map showing a new study area containing multiple AOI's which in this case are 4 states in the western United States.</figcaption>
</figure>
<p>Next, create a mask just like you did above. However this time
you will use the new extent that contains 4 states (4 regions).</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">west_mask</span> <span class="o">=</span> <span class="n">regionmask</span><span class="p">.</span><span class="n">mask_3D_geopandas</span><span class="p">(</span><span class="n">cali_or_wash_nev</span><span class="p">,</span>
<span class="n">monthly_forecast_temp_xr</span><span class="p">.</span><span class="n">lon</span><span class="p">,</span>
<span class="n">monthly_forecast_temp_xr</span><span class="p">.</span><span class="n">lat</span><span class="p">)</span>
<span class="n">west_mask</span>
</code></pre></div></div>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.DataArray 'region' (region: 4, lat: 585, lon: 1386)>
array([[[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
...,
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False]],
[[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
...,
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False]],
[[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
...,
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False]],
[[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
...,
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False]]])
Coordinates:
* lat (lat) float64 25.06 25.1 25.15 25.19 ... 49.27 49.31 49.35 49.4
* lon (lon) float64 235.2 235.3 235.3 235.4 ... 292.8 292.9 292.9 292.9
* region (region) int64 53 82 86 96</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.DataArray</div><div class="xr-array-name">'region'</div><ul class="xr-dim-list"><li><span class="xr-has-index">region</span>: 4</li><li><span class="xr-has-index">lat</span>: 585</li><li><span class="xr-has-index">lon</span>: 1386</li></ul></div><ul class="xr-sections"><li class="xr-section-item"><div class="xr-array-wrap"><input id="section-48b16ef4-4204-44ef-b15d-4700cd46baba" class="xr-array-in" type="checkbox" checked="" /><label for="section-48b16ef4-4204-44ef-b15d-4700cd46baba" title="Show/hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-array-preview xr-preview"><span>False False False False False False ... False False False False False</span></div><div class="xr-array-data"><pre>array([[[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
...,
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False]],
[[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
...,
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False]],
[[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
...,
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False]],
[[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
...,
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False]]])</pre></div></div></li><li class="xr-section-item"><input id="section-145e7d15-6449-4f1e-89d0-a947783dda34" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-145e7d15-6449-4f1e-89d0-a947783dda34" class="xr-section-summary">Coordinates: <span>(3)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lat</span></div><div class="xr-var-dims">(lat)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">25.06 25.1 25.15 ... 49.35 49.4</div><input id="attrs-131212a0-6d86-4618-b381-6f74439db97f" class="xr-var-attrs-in" type="checkbox" disabled="" /><label for="attrs-131212a0-6d86-4618-b381-6f74439db97f" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-b3b43154-543a-482d-985f-715c3adfdabb" class="xr-var-data-in" type="checkbox" /><label for="data-b3b43154-543a-482d-985f-715c3adfdabb" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"></dl></div><div class="xr-var-data"><pre>array([25.063078, 25.104744, 25.14641 , ..., 49.312691, 49.354359, 49.396023])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lon</span></div><div class="xr-var-dims">(lon)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">235.2 235.3 235.3 ... 292.9 292.9</div><input id="attrs-598b16df-fd0b-423f-9ce5-8f30e19fc6eb" class="xr-var-attrs-in" type="checkbox" disabled="" /><label for="attrs-598b16df-fd0b-423f-9ce5-8f30e19fc6eb" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-d3ee462e-8aee-483b-85fa-a5260408c4d2" class="xr-var-data-in" type="checkbox" /><label for="data-d3ee462e-8aee-483b-85fa-a5260408c4d2" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"></dl></div><div class="xr-var-data"><pre>array([235.227844, 235.269501, 235.311157, ..., 292.851929, 292.893585,
292.935242])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">region</span></div><div class="xr-var-dims">(region)</div><div class="xr-var-dtype">int64</div><div class="xr-var-preview xr-preview">53 82 86 96</div><input id="attrs-a8dcb74a-af1c-40c4-b4bc-d963c428e4ce" class="xr-var-attrs-in" type="checkbox" disabled="" /><label for="attrs-a8dcb74a-af1c-40c4-b4bc-d963c428e4ce" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-dd367309-3d4a-4a7d-9e69-db9c39b814d0" class="xr-var-data-in" type="checkbox" /><label for="data-dd367309-3d4a-4a7d-9e69-db9c39b814d0" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"></dl></div><div class="xr-var-data"><pre>array([53, 82, 86, 96])</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-6005ba2b-9c70-49d9-9923-4b19a206016f" class="xr-section-summary-in" type="checkbox" disabled="" /><label for="section-6005ba2b-9c70-49d9-9923-4b19a206016f" class="xr-section-summary" title="Expand/collapse section">Attributes: <span>(0)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"></dl></div></li></ul></div></div>
<p>Below is a small helper function that grabs the AOI from a shapefile and
returns a dictionary that you can use to slice your data.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">get_aoi</span><span class="p">(</span><span class="n">shp</span><span class="p">,</span> <span class="n">world</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
<span class="s">"""Takes a geopandas object and converts it to a lat/ lon
extent
Parameters
-----------
shp : GeoPandas GeoDataFrame
A geodataframe containing the spatial boundary of interest
world : boolean
True if you want lat / long to represent sinusoidal (0-360 degrees)
Returns
-------
Dictionary of lat and lon spatial bounds
"""</span>
<span class="n">lon_lat</span> <span class="o">=</span> <span class="p">{}</span>
<span class="c1"># Get lat min, max
</span> <span class="n">aoi_lat</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">shp</span><span class="p">.</span><span class="n">total_bounds</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span> <span class="nb">float</span><span class="p">(</span><span class="n">shp</span><span class="p">.</span><span class="n">total_bounds</span><span class="p">[</span><span class="mi">3</span><span class="p">])]</span>
<span class="n">aoi_lon</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">shp</span><span class="p">.</span><span class="n">total_bounds</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> <span class="nb">float</span><span class="p">(</span><span class="n">shp</span><span class="p">.</span><span class="n">total_bounds</span><span class="p">[</span><span class="mi">2</span><span class="p">])]</span>
<span class="k">if</span> <span class="n">world</span><span class="p">:</span>
<span class="n">aoi_lon</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">aoi_lon</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="mi">360</span>
<span class="n">aoi_lon</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">aoi_lon</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="mi">360</span>
<span class="n">lon_lat</span><span class="p">[</span><span class="s">"lon"</span><span class="p">]</span> <span class="o">=</span> <span class="n">aoi_lon</span>
<span class="n">lon_lat</span><span class="p">[</span><span class="s">"lat"</span><span class="p">]</span> <span class="o">=</span> <span class="n">aoi_lat</span>
<span class="k">return</span> <span class="n">lon_lat</span>
<span class="n">west_bounds</span> <span class="o">=</span> <span class="n">get_aoi</span><span class="p">(</span><span class="n">cali_or_wash_nev</span><span class="p">)</span>
</code></pre></div></div>
<p>Similar to your workflow above, you can subset that data as you wish by time and
extent. Subsetting the data will make all of your data processing faster!</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Slice the data
</span><span class="n">start_date</span> <span class="o">=</span> <span class="s">"2010-01-15"</span>
<span class="n">end_date</span> <span class="o">=</span> <span class="s">"2010-02-15"</span>
<span class="c1"># Subset
</span><span class="n">two_months_west_coast</span> <span class="o">=</span> <span class="n">monthly_forecast_temp_xr</span><span class="p">[</span><span class="s">"air_temperature"</span><span class="p">].</span><span class="n">sel</span><span class="p">(</span>
<span class="n">time</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="n">start_date</span><span class="p">,</span> <span class="n">end_date</span><span class="p">),</span>
<span class="n">lon</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="n">west_bounds</span><span class="p">[</span><span class="s">"lon"</span><span class="p">][</span><span class="mi">0</span><span class="p">],</span> <span class="n">west_bounds</span><span class="p">[</span><span class="s">"lon"</span><span class="p">][</span><span class="mi">1</span><span class="p">]),</span>
<span class="n">lat</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="n">west_bounds</span><span class="p">[</span><span class="s">"lat"</span><span class="p">][</span><span class="mi">0</span><span class="p">],</span> <span class="n">west_bounds</span><span class="p">[</span><span class="s">"lat"</span><span class="p">][</span><span class="mi">1</span><span class="p">]))</span>
<span class="n">two_months_west_coast</span>
</code></pre></div></div>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.DataArray 'air_temperature' (time: 2, lat: 395, lon: 256)>
[202240 values with dtype=float32]
Coordinates:
* lat (lat) float64 32.56 32.6 32.65 32.69 ... 48.85 48.9 48.94 48.98
* lon (lon) float64 235.3 235.4 235.4 235.4 ... 245.8 245.9 245.9 245.9
* time (time) object 2010-01-15 00:00:00 2010-02-15 00:00:00
Attributes:
long_name: Monthly Average of Daily Maximum Near-Surface Air Tempera...
units: K
grid_mapping: crs
standard_name: air_temperature
height: 2 m
cell_methods: time: maximum(interval: 24 hours);mean over days
_ChunkSizes: [ 10 44 107]</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.DataArray</div><div class="xr-array-name">'air_temperature'</div><ul class="xr-dim-list"><li><span class="xr-has-index">time</span>: 2</li><li><span class="xr-has-index">lat</span>: 395</li><li><span class="xr-has-index">lon</span>: 256</li></ul></div><ul class="xr-sections"><li class="xr-section-item"><div class="xr-array-wrap"><input id="section-30958aff-94e2-436d-89f7-fa64d49430d0" class="xr-array-in" type="checkbox" checked="" /><label for="section-30958aff-94e2-436d-89f7-fa64d49430d0" title="Show/hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-array-preview xr-preview"><span>...</span></div><div class="xr-array-data"><pre>[202240 values with dtype=float32]</pre></div></div></li><li class="xr-section-item"><input id="section-51fce8a0-3436-402c-9ea8-9b59508215fe" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-51fce8a0-3436-402c-9ea8-9b59508215fe" class="xr-section-summary">Coordinates: <span>(3)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lat</span></div><div class="xr-var-dims">(lat)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">32.56 32.6 32.65 ... 48.94 48.98</div><input id="attrs-0f31729b-e9f9-47f8-9cab-8b7696464925" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-0f31729b-e9f9-47f8-9cab-8b7696464925" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-25d1f101-028c-49d7-8338-85f57141f12a" class="xr-var-data-in" type="checkbox" /><label for="data-25d1f101-028c-49d7-8338-85f57141f12a" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>latitude</dd><dt><span>standard_name :</span></dt><dd>latitude</dd><dt><span>units :</span></dt><dd>degrees_north</dd><dt><span>axis :</span></dt><dd>Y</dd><dt><span>description :</span></dt><dd>Latitude of the center of the grid cell</dd></dl></div><div class="xr-var-data"><pre>array([32.562958, 32.604626, 32.64629 , ..., 48.89603 , 48.937698, 48.979362])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lon</span></div><div class="xr-var-dims">(lon)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">235.3 235.4 235.4 ... 245.9 245.9</div><input id="attrs-6b9f7eaf-f65c-482e-b547-8730874c93b9" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-6b9f7eaf-f65c-482e-b547-8730874c93b9" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-143a4d4c-b89a-4fdd-b9da-d08513433741" class="xr-var-data-in" type="checkbox" /><label for="data-143a4d4c-b89a-4fdd-b9da-d08513433741" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>units :</span></dt><dd>degrees_east</dd><dt><span>axis :</span></dt><dd>X</dd><dt><span>description :</span></dt><dd>Longitude of the center of the grid cell</dd><dt><span>long_name :</span></dt><dd>longitude</dd><dt><span>standard_name :</span></dt><dd>longitude</dd></dl></div><div class="xr-var-data"><pre>array([235.311157, 235.352844, 235.394501, ..., 245.852661, 245.894333,
245.936005])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">time</span></div><div class="xr-var-dims">(time)</div><div class="xr-var-dtype">object</div><div class="xr-var-preview xr-preview">2010-01-15 00:00:00 2010-02-15 0...</div><input id="attrs-0e9c73a0-8a77-4bc7-b96f-dc29b08e9e14" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-0e9c73a0-8a77-4bc7-b96f-dc29b08e9e14" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-3f7c8b46-dadd-4560-a67a-704172a5e63c" class="xr-var-data-in" type="checkbox" /><label for="data-3f7c8b46-dadd-4560-a67a-704172a5e63c" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>description :</span></dt><dd>days since 1900-01-01</dd></dl></div><div class="xr-var-data"><pre>array([cftime.DatetimeNoLeap(2010, 1, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2010, 2, 15, 0, 0, 0, 0, has_year_zero=True)],
dtype=object)</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-bd936587-4a64-446c-860e-b22650d0a537" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-bd936587-4a64-446c-860e-b22650d0a537" class="xr-section-summary">Attributes: <span>(7)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>Monthly Average of Daily Maximum Near-Surface Air Temperature</dd><dt><span>units :</span></dt><dd>K</dd><dt><span>grid_mapping :</span></dt><dd>crs</dd><dt><span>standard_name :</span></dt><dd>air_temperature</dd><dt><span>height :</span></dt><dd>2 m</dd><dt><span>cell_methods :</span></dt><dd>time: maximum(interval: 24 hours);mean over days</dd><dt><span>_ChunkSizes :</span></dt><dd>[ 10 44 107]</dd></dl></div></li></ul></div></div>
<p>Plot the data.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">two_months_west_coast</span><span class="p">.</span><span class="n">plot</span><span class="p">(</span><span class="n">col</span><span class="o">=</span><span class="s">"time"</span><span class="p">,</span>
<span class="n">col_wrap</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
<span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">8</span><span class="p">))</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-05-spatial-subsets/2020-10-16-netcdf-05-spatial-subsets_60_0.png" alt="Map showing temperature data for the entire sliced region of western united states." />
<figcaption>Map showing temperature data for the entire sliced region of western united states.</figcaption>
</figure>
<p>Again following the same steps as above, you can apply your
spatial mask. This will return a new array with the air
temperature data that can be grouped by each of the 4 regions.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Apply the mask for the west coast subset
</span><span class="n">two_months_west_coast</span> <span class="o">=</span> <span class="n">two_months_west_coast</span><span class="p">.</span><span class="n">where</span><span class="p">(</span><span class="n">west_mask</span><span class="p">)</span>
<span class="n">two_months_west_coast</span><span class="p">.</span><span class="n">dims</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>('time', 'lat', 'lon', 'region')
</code></pre></div></div>
<p>By default region mask takes the index of the geopandas
object and uses it as a region identifier. If you remember
from above, california was 53.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">cali_or_wash_nev</span>
</code></pre></div></div>
<div class="output execute_result">
<style scoped="">
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>featurecla</th>
<th>scalerank</th>
<th>adm1_code</th>
<th>diss_me</th>
<th>iso_3166_2</th>
<th>wikipedia</th>
<th>iso_a2</th>
<th>adm0_sr</th>
<th>name</th>
<th>name_alt</th>
<th>...</th>
<th>name_nl</th>
<th>name_pl</th>
<th>name_pt</th>
<th>name_ru</th>
<th>name_sv</th>
<th>name_tr</th>
<th>name_vi</th>
<th>name_zh</th>
<th>ne_id</th>
<th>geometry</th>
</tr>
</thead>
<tbody>
<tr>
<th>53</th>
<td>Admin-1 scale rank</td>
<td>2</td>
<td>USA-3521</td>
<td>3521</td>
<td>US-CA</td>
<td>http://en.wikipedia.org/wiki/California</td>
<td>US</td>
<td>8</td>
<td>California</td>
<td>CA|Calif.</td>
<td>...</td>
<td>Californië</td>
<td>Kalifornia</td>
<td>Califórnia</td>
<td>Калифорния</td>
<td>Kalifornien</td>
<td>Kaliforniya</td>
<td>California</td>
<td>加利福尼亚州</td>
<td>1159308415</td>
<td>MULTIPOLYGON (((-114.61054 34.99112, -114.6109...</td>
</tr>
<tr>
<th>82</th>
<td>Admin-1 scale rank</td>
<td>2</td>
<td>USA-3523</td>
<td>3523</td>
<td>US-NV</td>
<td>http://en.wikipedia.org/wiki/Nevada</td>
<td>US</td>
<td>1</td>
<td>Nevada</td>
<td>NV|Nev.</td>
<td>...</td>
<td>Nevada</td>
<td>Nevada</td>
<td>Nevada</td>
<td>Невада</td>
<td>Nevada</td>
<td>Nevada</td>
<td>Nevada</td>
<td>内华达州</td>
<td>1159315345</td>
<td>POLYGON ((-114.04027 37.00415, -114.04117 36.6...</td>
</tr>
<tr>
<th>86</th>
<td>Admin-1 scale rank</td>
<td>2</td>
<td>USA-3525</td>
<td>3525</td>
<td>US-OR</td>
<td>http://en.wikipedia.org/wiki/Oregon</td>
<td>US</td>
<td>6</td>
<td>Oregon</td>
<td>OR|Ore.</td>
<td>...</td>
<td>Oregon</td>
<td>Oregon</td>
<td>Oregon</td>
<td>Орегон</td>
<td>Oregon</td>
<td>Oregon</td>
<td>Oregon</td>
<td>俄勒冈州</td>
<td>1159309549</td>
<td>POLYGON ((-120.00081 42.00056, -120.26446 42.0...</td>
</tr>
<tr>
<th>96</th>
<td>Admin-1 scale rank</td>
<td>2</td>
<td>USA-3519</td>
<td>3519</td>
<td>US-WA</td>
<td>http://en.wikipedia.org/wiki/Washington_(state)</td>
<td>US</td>
<td>6</td>
<td>Washington</td>
<td>WA|Wash.</td>
<td>...</td>
<td>Washington</td>
<td>Waszyngton</td>
<td>Washington</td>
<td>Вашингтон</td>
<td>Washington</td>
<td>Vaşington</td>
<td>Washington</td>
<td>华盛顿州</td>
<td>1159309547</td>
<td>MULTIPOLYGON (((-122.78878 48.99303, -122.6863...</td>
</tr>
</tbody>
</table>
<p>4 rows × 84 columns</p>
</div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Note that the region values align with the index of the geodataframe above
</span><span class="n">two_months_west_coast</span><span class="p">.</span><span class="n">region</span>
</code></pre></div></div>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.DataArray 'region' (region: 4)>
array([53, 82, 86, 96])
Coordinates:
* region (region) int64 53 82 86 96</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.DataArray</div><div class="xr-array-name">'region'</div><ul class="xr-dim-list"><li><span class="xr-has-index">region</span>: 4</li></ul></div><ul class="xr-sections"><li class="xr-section-item"><div class="xr-array-wrap"><input id="section-5851e75b-681a-489e-9af2-22224eab9cfd" class="xr-array-in" type="checkbox" checked="" /><label for="section-5851e75b-681a-489e-9af2-22224eab9cfd" title="Show/hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-array-preview xr-preview"><span>53 82 86 96</span></div><div class="xr-array-data"><pre>array([53, 82, 86, 96])</pre></div></div></li><li class="xr-section-item"><input id="section-9ed7aa47-a03f-4245-932a-cc295cc0dfc7" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-9ed7aa47-a03f-4245-932a-cc295cc0dfc7" class="xr-section-summary">Coordinates: <span>(1)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">region</span></div><div class="xr-var-dims">(region)</div><div class="xr-var-dtype">int64</div><div class="xr-var-preview xr-preview">53 82 86 96</div><input id="attrs-f469c050-a8ec-434e-a3a0-4bb371634865" class="xr-var-attrs-in" type="checkbox" disabled="" /><label for="attrs-f469c050-a8ec-434e-a3a0-4bb371634865" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-cf4956f0-b961-44a6-b097-cca3f3895088" class="xr-var-data-in" type="checkbox" /><label for="data-cf4956f0-b961-44a6-b097-cca3f3895088" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"></dl></div><div class="xr-var-data"><pre>array([53, 82, 86, 96])</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-71e7627d-2a85-466a-9a8d-c95c9fdd2369" class="xr-section-summary-in" type="checkbox" disabled="" /><label for="section-71e7627d-2a85-466a-9a8d-c95c9fdd2369" class="xr-section-summary" title="Expand/collapse section">Attributes: <span>(0)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"></dl></div></li></ul></div></div>
<p>Having an additional region dimension in your array makes it easier
to summarize your data by region. Below you plot the
data by time and region in a matrix.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">two_months_west_coast</span><span class="p">.</span><span class="n">plot</span><span class="p">(</span><span class="n">col</span><span class="o">=</span><span class="s">"time"</span><span class="p">,</span>
<span class="n">row</span><span class="o">=</span><span class="s">"region"</span><span class="p">,</span>
<span class="n">sharey</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">sharex</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-05-spatial-subsets/2020-10-16-netcdf-05-spatial-subsets_67_0.png" alt="Faceted plot showing temperature for each aoi region (on the y axis) and accross time (on the y axis)." />
<figcaption>Faceted plot showing temperature for each aoi region (on the y axis) and accross time (on the y axis).</figcaption>
</figure>
<h2 id="calculate-mean-air-temperature-by-region">Calculate Mean Air Temperature by Region</h2>
<p>Once you have your data subsetted by region, you can
calculate summary statistics on the data for each region.</p>
<p>Below, you calculate the mean air temperature value for the
time subset that you created above. At this point you could export
the dataframe to a csv file if you want!</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">summary</span> <span class="o">=</span> <span class="n">two_months_west_coast</span><span class="p">.</span><span class="n">groupby</span><span class="p">(</span><span class="s">"time"</span><span class="p">).</span><span class="n">mean</span><span class="p">([</span><span class="s">"lat"</span><span class="p">,</span> <span class="s">"lon"</span><span class="p">])</span>
<span class="n">summary</span><span class="p">.</span><span class="n">to_dataframe</span><span class="p">()</span>
</code></pre></div></div>
<div class="output execute_result">
<style scoped="">
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th></th>
<th>air_temperature</th>
</tr>
<tr>
<th>time</th>
<th>region</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th rowspan="4" valign="top">2010-01-15 00:00:00</th>
<th>53</th>
<td>286.057922</td>
</tr>
<tr>
<th>82</th>
<td>280.085175</td>
</tr>
<tr>
<th>86</th>
<td>279.390350</td>
</tr>
<tr>
<th>96</th>
<td>278.031158</td>
</tr>
<tr>
<th rowspan="4" valign="top">2010-02-15 00:00:00</th>
<th>53</th>
<td>286.026550</td>
</tr>
<tr>
<th>82</th>
<td>279.355438</td>
</tr>
<tr>
<th>86</th>
<td>278.442688</td>
</tr>
<tr>
<th>96</th>
<td>277.346008</td>
</tr>
</tbody>
</table>
</div>
<h2 id="the-entire-workflow-to-subset-netcdf-4-files-by-temporal-and-spatial-extents">The Entire Workflow To Subset netcdf 4 Files by Temporal and Spatial Extents</h2>
<p>Putting all of the above together, below is an example workflow that you might use to summarize
climate data in netcdf 4 format by region and over time.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Helper Function to extract AOI
</span><span class="k">def</span> <span class="nf">get_aoi</span><span class="p">(</span><span class="n">shp</span><span class="p">,</span> <span class="n">world</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
<span class="s">"""Takes a geopandas object and converts it to a lat/ lon
extent """</span>
<span class="n">lon_lat</span> <span class="o">=</span> <span class="p">{}</span>
<span class="c1"># Get lat min, max
</span> <span class="n">aoi_lat</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">shp</span><span class="p">.</span><span class="n">total_bounds</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span> <span class="nb">float</span><span class="p">(</span><span class="n">shp</span><span class="p">.</span><span class="n">total_bounds</span><span class="p">[</span><span class="mi">3</span><span class="p">])]</span>
<span class="n">aoi_lon</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">shp</span><span class="p">.</span><span class="n">total_bounds</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> <span class="nb">float</span><span class="p">(</span><span class="n">shp</span><span class="p">.</span><span class="n">total_bounds</span><span class="p">[</span><span class="mi">2</span><span class="p">])]</span>
<span class="c1"># Handle the 0-360 lon values
</span> <span class="k">if</span> <span class="n">world</span><span class="p">:</span>
<span class="n">aoi_lon</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">aoi_lon</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="mi">360</span>
<span class="n">aoi_lon</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">aoi_lon</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="mi">360</span>
<span class="n">lon_lat</span><span class="p">[</span><span class="s">"lon"</span><span class="p">]</span> <span class="o">=</span> <span class="n">aoi_lon</span>
<span class="n">lon_lat</span><span class="p">[</span><span class="s">"lat"</span><span class="p">]</span> <span class="o">=</span> <span class="n">aoi_lat</span>
<span class="k">return</span> <span class="n">lon_lat</span>
</code></pre></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Here is the entire workflow from start to end
</span>
<span class="n">url</span> <span class="o">=</span> <span class="p">(</span>
<span class="s">"https://naturalearth.s3.amazonaws.com/"</span>
<span class="s">"50m_cultural/ne_50m_admin_1_states_provinces_lakes.zip"</span>
<span class="p">)</span>
<span class="n">states_gdf</span> <span class="o">=</span> <span class="n">gpd</span><span class="p">.</span><span class="n">read_file</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
<span class="c1"># Get netcdf file
</span><span class="n">data_path_monthly</span> <span class="o">=</span> <span class="s">'http://thredds.northwestknowledge.net:8080/thredds/dodsC/agg_macav2metdata_tasmax_BNU-ESM_r1i1p1_rcp45_2006_2099_CONUS_monthly.nc'</span>
<span class="c1"># Open up the data
</span><span class="k">with</span> <span class="n">xr</span><span class="p">.</span><span class="n">open_dataset</span><span class="p">(</span><span class="n">data_path_monthly</span><span class="p">)</span> <span class="k">as</span> <span class="n">file_nc</span><span class="p">:</span>
<span class="n">monthly_forecast_temp_xr</span> <span class="o">=</span> <span class="n">file_nc</span>
<span class="c1"># View xarray object
</span><span class="n">monthly_forecast_temp_xr</span>
<span class="c1"># Start by extracting a few states from the states_gdf
# I am using this as a shapefile because i presume some of my students
# will want to use custom aoi's so i want to teach using a shapefil rather than
# the regionmask built in shapes as nice as they are :)
</span><span class="n">states_gdf</span><span class="p">[</span><span class="s">"name"</span><span class="p">]</span>
<span class="n">cali_or_wash_nev</span> <span class="o">=</span> <span class="n">states_gdf</span><span class="p">[</span><span class="n">states_gdf</span><span class="p">.</span><span class="n">name</span><span class="p">.</span><span class="n">isin</span><span class="p">(</span>
<span class="p">[</span><span class="s">"California"</span><span class="p">,</span> <span class="s">"Oregon"</span><span class="p">,</span> <span class="s">"Washington"</span><span class="p">,</span> <span class="s">"Nevada"</span><span class="p">])]</span>
<span class="n">west_mask</span> <span class="o">=</span> <span class="n">regionmask</span><span class="p">.</span><span class="n">mask_3D_geopandas</span><span class="p">(</span><span class="n">cali_or_wash_nev</span><span class="p">,</span>
<span class="n">monthly_forecast_temp_xr</span><span class="p">.</span><span class="n">lon</span><span class="p">,</span>
<span class="n">monthly_forecast_temp_xr</span><span class="p">.</span><span class="n">lat</span><span class="p">)</span>
<span class="n">west_mask</span>
<span class="n">west_bounds</span> <span class="o">=</span> <span class="n">get_aoi</span><span class="p">(</span><span class="n">cali_or_wash_nev</span><span class="p">)</span>
<span class="c1"># Slice the data
</span><span class="n">start_date</span> <span class="o">=</span> <span class="s">"2010-01-15"</span>
<span class="n">end_date</span> <span class="o">=</span> <span class="s">"2020-02-15"</span>
<span class="c1"># Subset the data - this is now a dataarray rather than a DataSet
</span><span class="n">two_months_west_coast</span> <span class="o">=</span> <span class="n">monthly_forecast_temp_xr</span><span class="p">[</span><span class="s">"air_temperature"</span><span class="p">].</span><span class="n">sel</span><span class="p">(</span>
<span class="n">time</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="n">start_date</span><span class="p">,</span> <span class="n">end_date</span><span class="p">),</span>
<span class="n">lon</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="n">west_bounds</span><span class="p">[</span><span class="s">"lon"</span><span class="p">][</span><span class="mi">0</span><span class="p">],</span> <span class="n">west_bounds</span><span class="p">[</span><span class="s">"lon"</span><span class="p">][</span><span class="mi">1</span><span class="p">]),</span>
<span class="n">lat</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="n">west_bounds</span><span class="p">[</span><span class="s">"lat"</span><span class="p">][</span><span class="mi">0</span><span class="p">],</span> <span class="n">west_bounds</span><span class="p">[</span><span class="s">"lat"</span><span class="p">][</span><span class="mi">1</span><span class="p">])).</span><span class="n">where</span><span class="p">(</span><span class="n">west_mask</span><span class="p">)</span>
<span class="c1"># This output is a dataarray
</span><span class="n">two_months_west_coast</span>
<span class="c1"># Plot the data - this produces a single histogram
</span><span class="n">two_months_west_coast</span><span class="p">.</span><span class="n">plot</span><span class="p">()</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-05-spatial-subsets/2020-10-16-netcdf-05-spatial-subsets_74_0.png" alt="Histogram showing all pixels values in the area of interest." />
<figcaption>Histogram showing all pixels values in the area of interest.</figcaption>
</figure>
<p>Once the data are subsetted, you can begin to create plots and dataframes
with the summary data.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Keep in mind that as you add more data to your slice, the processing will slow down.
</span><span class="n">regional_summary</span> <span class="o">=</span> <span class="n">two_months_west_coast</span><span class="p">.</span><span class="n">groupby</span><span class="p">(</span><span class="s">"region"</span><span class="p">).</span><span class="n">mean</span><span class="p">([</span><span class="s">"lat"</span><span class="p">,</span> <span class="s">"lon"</span><span class="p">])</span>
<span class="n">regional_summary</span><span class="p">.</span><span class="n">plot</span><span class="p">(</span><span class="n">col</span><span class="o">=</span><span class="s">"region"</span><span class="p">,</span>
<span class="n">marker</span><span class="o">=</span><span class="s">"o"</span><span class="p">,</span>
<span class="n">color</span><span class="o">=</span><span class="s">"grey"</span><span class="p">,</span>
<span class="n">markerfacecolor</span><span class="o">=</span><span class="s">"purple"</span><span class="p">,</span>
<span class="n">markeredgecolor</span><span class="o">=</span><span class="s">"purple"</span><span class="p">,</span>
<span class="n">col_wrap</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-05-spatial-subsets/2020-10-16-netcdf-05-spatial-subsets_76_0.png" alt="Facetted scatter plot showing mean temperature data by region over time" />
<figcaption>Facetted scatter plot showing mean temperature data by region over time</figcaption>
</figure>
<p>Export to a dataframe.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">two_months_west_coast</span><span class="p">.</span><span class="n">groupby</span><span class="p">(</span><span class="s">"region"</span><span class="p">).</span><span class="n">mean</span><span class="p">([</span><span class="s">"lat"</span><span class="p">,</span> <span class="s">"lon"</span><span class="p">]).</span><span class="n">to_dataframe</span><span class="p">()</span>
</code></pre></div></div>
<div class="output execute_result">
<style scoped="">
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th></th>
<th>air_temperature</th>
</tr>
<tr>
<th>time</th>
<th>region</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th rowspan="4" valign="top">2010-01-15 00:00:00</th>
<th>53</th>
<td>286.057922</td>
</tr>
<tr>
<th>82</th>
<td>280.085175</td>
</tr>
<tr>
<th>86</th>
<td>279.390350</td>
</tr>
<tr>
<th>96</th>
<td>278.031158</td>
</tr>
<tr>
<th>2010-02-15 00:00:00</th>
<th>53</th>
<td>286.026550</td>
</tr>
<tr>
<th>...</th>
<th>...</th>
<td>...</td>
</tr>
<tr>
<th>2020-01-15 00:00:00</th>
<th>96</th>
<td>275.706940</td>
</tr>
<tr>
<th rowspan="4" valign="top">2020-02-15 00:00:00</th>
<th>53</th>
<td>285.709076</td>
</tr>
<tr>
<th>82</th>
<td>279.856842</td>
</tr>
<tr>
<th>86</th>
<td>279.198822</td>
</tr>
<tr>
<th>96</th>
<td>278.026459</td>
</tr>
</tbody>
</table>
<p>488 rows × 1 columns</p>
</div>Leah WasserClimate datasets stored in netcdf 4 format often cover the entire globe or an entire country. Learn how to subset climate data spatially and by time slices using xarray and regionmask in open source python.Calculate Seasonal Summary Values from Climate Data Variables Stored in NetCDF 4 Format: Work With MACA v2 Climate Data in Python2020-10-16T00:00:00+00:002020-10-16T00:00:00+00:00https://www.earthdatascience.org/courses/use-data-open-source-python/hierarchical-data-formats-hdf/netcdf-06-seasonal-summary<aside class="sidebar__right">
<nav class="toc">
<header><h4 class="nav__title"><i class="fa fa-file-text"></i> In This Chapter</h4></header>
<ul class="toc__menu" id="markdown-toc">
<li><a href="#chapter-13---netcdf-4-climate-data-in-open-source-python" id="markdown-toc-chapter-13---netcdf-4-climate-data-in-open-source-python"><i class="fa fa-ship" aria-hidden="true"></i> Chapter 13 - NETCDF 4 Climate Data in Open Source Python</a></li>
<li><a href="#learning-objectives" id="markdown-toc-learning-objectives"><i class="fa fa-graduation-cap" aria-hidden="true"></i> Learning Objectives</a></li>
<li><a href="#what-you-need" id="markdown-toc-what-you-need"><i class="fa fa-check-square-o fa-2" aria-hidden="true"></i> What You Need</a></li>
<li><a href="#calculate-seasonal-averages-using-maca-vs-climate-data" id="markdown-toc-calculate-seasonal-averages-using-maca-vs-climate-data">Calculate Seasonal Averages Using MACA vs Climate Data</a></li>
<li><a href="#calculate-unweighted-seasonal-averages-for-by-season-across-each-year" id="markdown-toc-calculate-unweighted-seasonal-averages-for-by-season-across-each-year">Calculate UnWeighted Seasonal Averages For By Season Across Each Year</a></li>
<li><a href="#weighted-summary-by-season" id="markdown-toc-weighted-summary-by-season">Weighted Summary by Season</a></li>
<li><a href="#the-same-analysis-for-the-west-coast" id="markdown-toc-the-same-analysis-for-the-west-coast">The Same Analysis for the West Coast</a></li>
</ul>
</nav>
</aside>
<div class="notice--success">
<h2 id="chapter-13---netcdf-4-climate-data-in-open-source-python"><i class="fa fa-ship" aria-hidden="true"></i> Chapter 13 - NETCDF 4 Climate Data in Open Source Python</h2>
<p>In this chapter, you will learn how to work with Climate Data Sets (MACA v2 for the United states) stored in netcdf 4 format using open source <strong>Python</strong>.</p>
<h2 id="learning-objectives"><i class="fa fa-graduation-cap" aria-hidden="true"></i> Learning Objectives</h2>
<p>After completing this chapter, you will be able to:</p>
<ul>
<li>Summarize MACA v 2 climate data stored in netcdf 4 format by seasons across all time periods using <code class="language-plaintext highlighter-rouge">xarray</code>.</li>
<li>Summarize MACA v 2 climate data stored in netcdf 4 format by seasons and across years using <code class="language-plaintext highlighter-rouge">xarray</code>.</li>
</ul>
<h2 id="what-you-need"><i class="fa fa-check-square-o fa-2" aria-hidden="true"></i> What You Need</h2>
<p>You will need a computer with internet access to complete this lesson and …</p>
</div>
<h2 id="calculate-seasonal-averages-using-maca-vs-climate-data">Calculate Seasonal Averages Using MACA vs Climate Data</h2>
<p>In this lesson, you will learn how to calculate seasonal averages over several years
using MACA v 2 Climate Data downloaded in <code class="language-plaintext highlighter-rouge">netcdf4</code> format using
<code class="language-plaintext highlighter-rouge">xarray</code>.</p>
<p>In this example you will use the forecast temperature data downloaded from the
northwestknowledge.net website.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="n">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="n">pd</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="n">plt</span>
<span class="kn">import</span> <span class="nn">cartopy.crs</span> <span class="k">as</span> <span class="n">ccrs</span>
<span class="kn">import</span> <span class="nn">cartopy.feature</span> <span class="k">as</span> <span class="n">cfeature</span>
<span class="kn">import</span> <span class="nn">seaborn</span> <span class="k">as</span> <span class="n">sns</span>
<span class="kn">import</span> <span class="nn">geopandas</span> <span class="k">as</span> <span class="n">gpd</span>
<span class="kn">import</span> <span class="nn">earthpy</span> <span class="k">as</span> <span class="n">et</span>
<span class="kn">import</span> <span class="nn">xarray</span> <span class="k">as</span> <span class="n">xr</span>
<span class="kn">import</span> <span class="nn">regionmask</span>
<span class="c1"># Plotting options
</span><span class="n">sns</span><span class="p">.</span><span class="nb">set</span><span class="p">(</span><span class="n">font_scale</span><span class="o">=</span><span class="mf">1.3</span><span class="p">)</span>
<span class="n">sns</span><span class="p">.</span><span class="n">set_style</span><span class="p">(</span><span class="s">"white"</span><span class="p">)</span>
<span class="c1"># Optional - set your working directory if you wish to use the data
# accessed lower down in this notebook (the USA state boundary data)
</span><span class="n">os</span><span class="p">.</span><span class="n">chdir</span><span class="p">(</span><span class="n">os</span><span class="p">.</span><span class="n">path</span><span class="p">.</span><span class="n">join</span><span class="p">(</span><span class="n">et</span><span class="p">.</span><span class="n">io</span><span class="p">.</span><span class="n">HOME</span><span class="p">,</span>
<span class="s">'earth-analytics'</span><span class="p">,</span>
<span class="s">'data'</span><span class="p">))</span>
</code></pre></div></div>
<p>To begin, you can download and open up a MACA v2 netcdf file. The file below is a
projected maximum temperature dataset downscaled using the <code class="language-plaintext highlighter-rouge">BNU-ESM</code> model for 2006-2099.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Get netcdf file
</span><span class="n">data_path_monthly</span> <span class="o">=</span> <span class="s">'http://thredds.northwestknowledge.net:8080/thredds/dodsC/agg_macav2metdata_tasmax_BNU-ESM_r1i1p1_rcp45_2006_2099_CONUS_monthly.nc'</span>
<span class="c1"># Open up the data
</span><span class="k">with</span> <span class="n">xr</span><span class="p">.</span><span class="n">open_dataset</span><span class="p">(</span><span class="n">data_path_monthly</span><span class="p">)</span> <span class="k">as</span> <span class="n">file_nc</span><span class="p">:</span>
<span class="n">monthly_forecast_temp_xr</span> <span class="o">=</span> <span class="n">file_nc</span>
<span class="c1"># xarray object
</span><span class="n">monthly_forecast_temp_xr</span>
</code></pre></div></div>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.Dataset>
Dimensions: (lat: 585, crs: 1, lon: 1386, time: 1128)
Coordinates:
* lat (lat) float64 25.06 25.1 25.15 25.19 ... 49.31 49.35 49.4
* crs (crs) int32 1
* lon (lon) float64 235.2 235.3 235.3 235.4 ... 292.9 292.9 292.9
* time (time) object 2006-01-15 00:00:00 ... 2099-12-15 00:00:00
Data variables:
air_temperature (time, lat, lon) float32 ...
Attributes: (12/46)
description: Multivariate Adaptive Constructed Analog...
id: MACAv2-METDATA
naming_authority: edu.uidaho.reacch
Metadata_Conventions: Unidata Dataset Discovery v1.0
Metadata_Link:
cdm_data_type: FLOAT
... ...
contributor_role: Postdoctoral Fellow
publisher_name: REACCH
publisher_email: reacch@uidaho.edu
publisher_url: http://www.reacchpna.org/
license: Creative Commons CC0 1.0 Universal Dedic...
coordinate_system: WGS84,EPSG:4326</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.Dataset</div></div><ul class="xr-sections"><li class="xr-section-item"><input id="section-6567586c-1f98-45ed-bfdc-1a738765ac6d" class="xr-section-summary-in" type="checkbox" disabled="" /><label for="section-6567586c-1f98-45ed-bfdc-1a738765ac6d" class="xr-section-summary" title="Expand/collapse section">Dimensions:</label><div class="xr-section-inline-details"><ul class="xr-dim-list"><li><span class="xr-has-index">lat</span>: 585</li><li><span class="xr-has-index">crs</span>: 1</li><li><span class="xr-has-index">lon</span>: 1386</li><li><span class="xr-has-index">time</span>: 1128</li></ul></div><div class="xr-section-details"></div></li><li class="xr-section-item"><input id="section-f8842ddd-4d6e-496c-bbb0-bc313af0dc82" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-f8842ddd-4d6e-496c-bbb0-bc313af0dc82" class="xr-section-summary">Coordinates: <span>(4)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lat</span></div><div class="xr-var-dims">(lat)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">25.06 25.1 25.15 ... 49.35 49.4</div><input id="attrs-052dbdae-8b45-48e8-8fc5-aa73c1fbc462" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-052dbdae-8b45-48e8-8fc5-aa73c1fbc462" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-f2d8b2f6-3d03-4907-a730-6e550b379865" class="xr-var-data-in" type="checkbox" /><label for="data-f2d8b2f6-3d03-4907-a730-6e550b379865" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>latitude</dd><dt><span>standard_name :</span></dt><dd>latitude</dd><dt><span>units :</span></dt><dd>degrees_north</dd><dt><span>axis :</span></dt><dd>Y</dd><dt><span>description :</span></dt><dd>Latitude of the center of the grid cell</dd></dl></div><div class="xr-var-data"><pre>array([25.063078, 25.104744, 25.14641 , ..., 49.312691, 49.354359, 49.396023])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">crs</span></div><div class="xr-var-dims">(crs)</div><div class="xr-var-dtype">int32</div><div class="xr-var-preview xr-preview">1</div><input id="attrs-a7f9fe92-4822-4462-a2ce-6060faa74a1f" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-a7f9fe92-4822-4462-a2ce-6060faa74a1f" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-25c08ea2-04be-4d18-941d-9e3d7f0df3f0" class="xr-var-data-in" type="checkbox" /><label for="data-25c08ea2-04be-4d18-941d-9e3d7f0df3f0" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>grid_mapping_name :</span></dt><dd>latitude_longitude</dd><dt><span>longitude_of_prime_meridian :</span></dt><dd>0.0</dd><dt><span>semi_major_axis :</span></dt><dd>6378137.0</dd><dt><span>inverse_flattening :</span></dt><dd>298.257223563</dd></dl></div><div class="xr-var-data"><pre>array([1], dtype=int32)</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lon</span></div><div class="xr-var-dims">(lon)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">235.2 235.3 235.3 ... 292.9 292.9</div><input id="attrs-0366f13b-721b-4ac2-a872-0a3dab7960d2" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-0366f13b-721b-4ac2-a872-0a3dab7960d2" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-7c47cab1-333f-48f8-855f-27fb79872c80" class="xr-var-data-in" type="checkbox" /><label for="data-7c47cab1-333f-48f8-855f-27fb79872c80" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>units :</span></dt><dd>degrees_east</dd><dt><span>axis :</span></dt><dd>X</dd><dt><span>description :</span></dt><dd>Longitude of the center of the grid cell</dd><dt><span>long_name :</span></dt><dd>longitude</dd><dt><span>standard_name :</span></dt><dd>longitude</dd></dl></div><div class="xr-var-data"><pre>array([235.227844, 235.269501, 235.311157, ..., 292.851929, 292.893585,
292.935242])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">time</span></div><div class="xr-var-dims">(time)</div><div class="xr-var-dtype">object</div><div class="xr-var-preview xr-preview">2006-01-15 00:00:00 ... 2099-12-...</div><input id="attrs-76d22f25-5c18-4e36-9009-ab35ec47b483" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-76d22f25-5c18-4e36-9009-ab35ec47b483" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-46c4c9c2-9919-46e9-a7d4-f8ef330a5a30" class="xr-var-data-in" type="checkbox" /><label for="data-46c4c9c2-9919-46e9-a7d4-f8ef330a5a30" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>description :</span></dt><dd>days since 1900-01-01</dd></dl></div><div class="xr-var-data"><pre>array([cftime.DatetimeNoLeap(2006, 1, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2006, 2, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2006, 3, 15, 0, 0, 0, 0, has_year_zero=True), ...,
cftime.DatetimeNoLeap(2099, 10, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2099, 11, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2099, 12, 15, 0, 0, 0, 0, has_year_zero=True)],
dtype=object)</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-201321b3-4ff1-4ec7-ae21-9771b787e472" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-201321b3-4ff1-4ec7-ae21-9771b787e472" class="xr-section-summary">Data variables: <span>(1)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span>air_temperature</span></div><div class="xr-var-dims">(time, lat, lon)</div><div class="xr-var-dtype">float32</div><div class="xr-var-preview xr-preview">...</div><input id="attrs-398c71ed-f12a-4e5d-a7cf-eb3b5a30ad78" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-398c71ed-f12a-4e5d-a7cf-eb3b5a30ad78" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-cc9ad74d-4a18-4b9e-8a3a-86a41154bce9" class="xr-var-data-in" type="checkbox" /><label for="data-cc9ad74d-4a18-4b9e-8a3a-86a41154bce9" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>Monthly Average of Daily Maximum Near-Surface Air Temperature</dd><dt><span>units :</span></dt><dd>K</dd><dt><span>grid_mapping :</span></dt><dd>crs</dd><dt><span>standard_name :</span></dt><dd>air_temperature</dd><dt><span>height :</span></dt><dd>2 m</dd><dt><span>cell_methods :</span></dt><dd>time: maximum(interval: 24 hours);mean over days</dd><dt><span>_ChunkSizes :</span></dt><dd>[ 10 44 107]</dd></dl></div><div class="xr-var-data"><pre>[914593680 values with dtype=float32]</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-f7eb324b-6200-4731-bc28-035e8a3e71c2" class="xr-section-summary-in" type="checkbox" /><label for="section-f7eb324b-6200-4731-bc28-035e8a3e71c2" class="xr-section-summary">Attributes: <span>(46)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"><dt><span>description :</span></dt><dd>Multivariate Adaptive Constructed Analogs (MACA) method, version 2.3,Dec 2013.</dd><dt><span>id :</span></dt><dd>MACAv2-METDATA</dd><dt><span>naming_authority :</span></dt><dd>edu.uidaho.reacch</dd><dt><span>Metadata_Conventions :</span></dt><dd>Unidata Dataset Discovery v1.0</dd><dt><span>Metadata_Link :</span></dt><dd></dd><dt><span>cdm_data_type :</span></dt><dd>FLOAT</dd><dt><span>title :</span></dt><dd>Monthly aggregation of downscaled daily meteorological data of Monthly Average of Daily Maximum Near-Surface Air Temperature from College of Global Change and Earth System Science, Beijing Normal University (BNU-ESM) using the run r1i1p1 of the rcp45 scenario.</dd><dt><span>summary :</span></dt><dd>This archive contains monthly downscaled meteorological and hydrological projections for the Conterminous United States at 1/24-deg resolution. These monthly values are obtained by aggregating the daily values obtained from the downscaling using the Multivariate Adaptive Constructed Analogs (MACA, Abatzoglou, 2012) statistical downscaling method with the METDATA (Abatzoglou,2013) training dataset. The downscaled meteorological variables are maximum/minimum temperature(tasmax/tasmin), maximum/minimum relative humidity (rhsmax/rhsmin),precipitation amount(pr), downward shortwave solar radiation(rsds), eastward wind(uas), northward wind(vas), and specific humidity(huss). The downscaling is based on the 365-day model outputs from different global climate models (GCMs) from Phase 5 of the Coupled Model Inter-comparison Project (CMIP3) utlizing the historical (1950-2005) and future RCP4.5/8.5(2006-2099) scenarios. </dd><dt><span>keywords :</span></dt><dd>monthly, precipitation, maximum temperature, minimum temperature, downward shortwave solar radiation, specific humidity, wind velocity, CMIP5, Gridded Meteorological Data</dd><dt><span>keywords_vocabulary :</span></dt><dd></dd><dt><span>standard_name_vocabulary :</span></dt><dd>CF-1.0</dd><dt><span>history :</span></dt><dd>No revisions.</dd><dt><span>comment :</span></dt><dd></dd><dt><span>geospatial_bounds :</span></dt><dd>POLYGON((-124.7722 25.0631,-124.7722 49.3960, -67.0648 49.3960,-67.0648, 25.0631, -124.7722,25.0631))</dd><dt><span>geospatial_lat_min :</span></dt><dd>25.0631</dd><dt><span>geospatial_lat_max :</span></dt><dd>49.3960</dd><dt><span>geospatial_lon_min :</span></dt><dd>-124.7722</dd><dt><span>geospatial_lon_max :</span></dt><dd>-67.0648</dd><dt><span>geospatial_lat_units :</span></dt><dd>decimal degrees north</dd><dt><span>geospatial_lon_units :</span></dt><dd>decimal degrees east</dd><dt><span>geospatial_lat_resolution :</span></dt><dd>0.0417</dd><dt><span>geospatial_lon_resolution :</span></dt><dd>0.0417</dd><dt><span>geospatial_vertical_min :</span></dt><dd>0.0</dd><dt><span>geospatial_vertical_max :</span></dt><dd>0.0</dd><dt><span>geospatial_vertical_resolution :</span></dt><dd>0.0</dd><dt><span>geospatial_vertical_positive :</span></dt><dd>up</dd><dt><span>time_coverage_start :</span></dt><dd>2091-01-01T00:0</dd><dt><span>time_coverage_end :</span></dt><dd>2095-12-31T00:00</dd><dt><span>time_coverage_duration :</span></dt><dd>P5Y</dd><dt><span>time_coverage_resolution :</span></dt><dd>P1M</dd><dt><span>date_created :</span></dt><dd>2014-05-15</dd><dt><span>date_modified :</span></dt><dd>2014-05-15</dd><dt><span>date_issued :</span></dt><dd>2014-05-15</dd><dt><span>creator_name :</span></dt><dd>John Abatzoglou</dd><dt><span>creator_url :</span></dt><dd>http://maca.northwestknowledge.net</dd><dt><span>creator_email :</span></dt><dd>jabatzoglou@uidaho.edu</dd><dt><span>institution :</span></dt><dd>University of Idaho</dd><dt><span>processing_level :</span></dt><dd>GRID</dd><dt><span>project :</span></dt><dd></dd><dt><span>contributor_name :</span></dt><dd>Katherine C. Hegewisch</dd><dt><span>contributor_role :</span></dt><dd>Postdoctoral Fellow</dd><dt><span>publisher_name :</span></dt><dd>REACCH</dd><dt><span>publisher_email :</span></dt><dd>reacch@uidaho.edu</dd><dt><span>publisher_url :</span></dt><dd>http://www.reacchpna.org/</dd><dt><span>license :</span></dt><dd>Creative Commons CC0 1.0 Universal Dedication(http://creativecommons.org/publicdomain/zero/1.0/legalcode)</dd><dt><span>coordinate_system :</span></dt><dd>WGS84,EPSG:4326</dd></dl></div></li></ul></div></div>
<p>In the example below you subset data for the state of
California similar to what you did in the previous lesson. You can
select any state that you wish for this analysis!</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Download natural earth data to generate AOI
</span><span class="n">url</span> <span class="o">=</span> <span class="p">(</span>
<span class="s">"https://naturalearth.s3.amazonaws.com/"</span>
<span class="s">"50m_cultural/ne_50m_admin_1_states_provinces_lakes.zip"</span>
<span class="p">)</span>
<span class="n">states_gdf</span> <span class="o">=</span> <span class="n">gpd</span><span class="p">.</span><span class="n">read_file</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
<span class="n">cali_aoi</span> <span class="o">=</span> <span class="n">states_gdf</span><span class="p">[</span><span class="n">states_gdf</span><span class="p">.</span><span class="n">name</span> <span class="o">==</span> <span class="s">"California"</span><span class="p">]</span>
</code></pre></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Would this be better if it only returned 4 values?? probably so
# Helper Function to extract AOI
</span><span class="k">def</span> <span class="nf">get_aoi</span><span class="p">(</span><span class="n">shp</span><span class="p">,</span> <span class="n">world</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
<span class="s">"""Takes a geopandas object and converts it to a lat/ lon
extent """</span>
<span class="n">lon_lat</span> <span class="o">=</span> <span class="p">{}</span>
<span class="c1"># Get lat min, max
</span> <span class="n">aoi_lat</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">shp</span><span class="p">.</span><span class="n">total_bounds</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span> <span class="nb">float</span><span class="p">(</span><span class="n">shp</span><span class="p">.</span><span class="n">total_bounds</span><span class="p">[</span><span class="mi">3</span><span class="p">])]</span>
<span class="n">aoi_lon</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">shp</span><span class="p">.</span><span class="n">total_bounds</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> <span class="nb">float</span><span class="p">(</span><span class="n">shp</span><span class="p">.</span><span class="n">total_bounds</span><span class="p">[</span><span class="mi">2</span><span class="p">])]</span>
<span class="c1"># Handle the 0-360 lon values
</span> <span class="k">if</span> <span class="n">world</span><span class="p">:</span>
<span class="n">aoi_lon</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">aoi_lon</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="mi">360</span>
<span class="n">aoi_lon</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">aoi_lon</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="mi">360</span>
<span class="n">lon_lat</span><span class="p">[</span><span class="s">"lon"</span><span class="p">]</span> <span class="o">=</span> <span class="n">aoi_lon</span>
<span class="n">lon_lat</span><span class="p">[</span><span class="s">"lat"</span><span class="p">]</span> <span class="o">=</span> <span class="n">aoi_lat</span>
<span class="k">return</span> <span class="n">lon_lat</span>
</code></pre></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Get lat min, max from Cali aoi extent
</span><span class="n">cali_bounds</span> <span class="o">=</span> <span class="n">get_aoi</span><span class="p">(</span><span class="n">cali_aoi</span><span class="p">)</span>
<span class="c1"># Slice by time & aoi location
</span><span class="n">start_date</span> <span class="o">=</span> <span class="s">"2059-12-15"</span>
<span class="n">end_date</span> <span class="o">=</span> <span class="s">"2099-12-15"</span>
<span class="n">cali_temp</span> <span class="o">=</span> <span class="n">monthly_forecast_temp_xr</span><span class="p">[</span><span class="s">"air_temperature"</span><span class="p">].</span><span class="n">sel</span><span class="p">(</span>
<span class="n">time</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="n">start_date</span><span class="p">,</span> <span class="n">end_date</span><span class="p">),</span>
<span class="n">lon</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="n">cali_bounds</span><span class="p">[</span><span class="s">"lon"</span><span class="p">][</span><span class="mi">0</span><span class="p">],</span> <span class="n">cali_bounds</span><span class="p">[</span><span class="s">"lon"</span><span class="p">][</span><span class="mi">1</span><span class="p">]),</span>
<span class="n">lat</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="n">cali_bounds</span><span class="p">[</span><span class="s">"lat"</span><span class="p">][</span><span class="mi">0</span><span class="p">],</span> <span class="n">cali_bounds</span><span class="p">[</span><span class="s">"lat"</span><span class="p">][</span><span class="mi">1</span><span class="p">]))</span>
<span class="n">cali_temp</span>
</code></pre></div></div>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.DataArray 'air_temperature' (time: 481, lat: 227, lon: 246)>
[26860002 values with dtype=float32]
Coordinates:
* lat (lat) float64 32.56 32.6 32.65 32.69 ... 41.85 41.9 41.94 41.98
* lon (lon) float64 235.6 235.7 235.7 235.8 ... 245.7 245.8 245.8 245.9
* time (time) object 2059-12-15 00:00:00 ... 2099-12-15 00:00:00
Attributes:
long_name: Monthly Average of Daily Maximum Near-Surface Air Tempera...
units: K
grid_mapping: crs
standard_name: air_temperature
height: 2 m
cell_methods: time: maximum(interval: 24 hours);mean over days
_ChunkSizes: [ 10 44 107]</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.DataArray</div><div class="xr-array-name">'air_temperature'</div><ul class="xr-dim-list"><li><span class="xr-has-index">time</span>: 481</li><li><span class="xr-has-index">lat</span>: 227</li><li><span class="xr-has-index">lon</span>: 246</li></ul></div><ul class="xr-sections"><li class="xr-section-item"><div class="xr-array-wrap"><input id="section-bac62aa5-2325-4bc9-b75b-eb3d97ac6a9f" class="xr-array-in" type="checkbox" checked="" /><label for="section-bac62aa5-2325-4bc9-b75b-eb3d97ac6a9f" title="Show/hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-array-preview xr-preview"><span>...</span></div><div class="xr-array-data"><pre>[26860002 values with dtype=float32]</pre></div></div></li><li class="xr-section-item"><input id="section-c5859bdc-e4cf-44a5-871f-c3a7161789ed" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-c5859bdc-e4cf-44a5-871f-c3a7161789ed" class="xr-section-summary">Coordinates: <span>(3)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lat</span></div><div class="xr-var-dims">(lat)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">32.56 32.6 32.65 ... 41.94 41.98</div><input id="attrs-0c029e2d-361d-4e24-a8cb-3dff509a5fad" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-0c029e2d-361d-4e24-a8cb-3dff509a5fad" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-a1b22b8d-6e47-42d7-b7d9-cb73903ff2ec" class="xr-var-data-in" type="checkbox" /><label for="data-a1b22b8d-6e47-42d7-b7d9-cb73903ff2ec" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>latitude</dd><dt><span>standard_name :</span></dt><dd>latitude</dd><dt><span>units :</span></dt><dd>degrees_north</dd><dt><span>axis :</span></dt><dd>Y</dd><dt><span>description :</span></dt><dd>Latitude of the center of the grid cell</dd></dl></div><div class="xr-var-data"><pre>array([32.562958, 32.604626, 32.64629 , ..., 41.896141, 41.937809, 41.979473])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lon</span></div><div class="xr-var-dims">(lon)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">235.6 235.7 235.7 ... 245.8 245.9</div><input id="attrs-3b85065c-ef5d-4a11-9370-dcd5dce03c9b" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-3b85065c-ef5d-4a11-9370-dcd5dce03c9b" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-93d474a3-274f-443a-814a-c9be754fde17" class="xr-var-data-in" type="checkbox" /><label for="data-93d474a3-274f-443a-814a-c9be754fde17" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>units :</span></dt><dd>degrees_east</dd><dt><span>axis :</span></dt><dd>X</dd><dt><span>description :</span></dt><dd>Longitude of the center of the grid cell</dd><dt><span>long_name :</span></dt><dd>longitude</dd><dt><span>standard_name :</span></dt><dd>longitude</dd></dl></div><div class="xr-var-data"><pre>array([235.644501, 235.686157, 235.727829, ..., 245.769333, 245.811005,
245.852661])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">time</span></div><div class="xr-var-dims">(time)</div><div class="xr-var-dtype">object</div><div class="xr-var-preview xr-preview">2059-12-15 00:00:00 ... 2099-12-...</div><input id="attrs-85373d27-4115-4e46-b8b4-3f1fd0321f46" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-85373d27-4115-4e46-b8b4-3f1fd0321f46" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-c323e5e9-db32-4e1f-bfd8-dd9253f02ab4" class="xr-var-data-in" type="checkbox" /><label for="data-c323e5e9-db32-4e1f-bfd8-dd9253f02ab4" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>description :</span></dt><dd>days since 1900-01-01</dd></dl></div><div class="xr-var-data"><pre>array([cftime.DatetimeNoLeap(2059, 12, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2060, 1, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2060, 2, 15, 0, 0, 0, 0, has_year_zero=True), ...,
cftime.DatetimeNoLeap(2099, 10, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2099, 11, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2099, 12, 15, 0, 0, 0, 0, has_year_zero=True)],
dtype=object)</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-5e7a2e30-4351-41ad-9973-5a1f9e73f3ec" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-5e7a2e30-4351-41ad-9973-5a1f9e73f3ec" class="xr-section-summary">Attributes: <span>(7)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>Monthly Average of Daily Maximum Near-Surface Air Temperature</dd><dt><span>units :</span></dt><dd>K</dd><dt><span>grid_mapping :</span></dt><dd>crs</dd><dt><span>standard_name :</span></dt><dd>air_temperature</dd><dt><span>height :</span></dt><dd>2 m</dd><dt><span>cell_methods :</span></dt><dd>time: maximum(interval: 24 hours);mean over days</dd><dt><span>_ChunkSizes :</span></dt><dd>[ 10 44 107]</dd></dl></div></li></ul></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">print</span><span class="p">(</span><span class="s">"Time Period start: "</span><span class="p">,</span> <span class="n">cali_temp</span><span class="p">.</span><span class="n">time</span><span class="p">.</span><span class="nb">min</span><span class="p">().</span><span class="n">values</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">"Time Period end: "</span><span class="p">,</span> <span class="n">cali_temp</span><span class="p">.</span><span class="n">time</span><span class="p">.</span><span class="nb">max</span><span class="p">().</span><span class="n">values</span><span class="p">)</span>
</code></pre></div></div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Time Period start: 2059-12-15 00:00:00
Time Period end: 2099-12-15 00:00:00
</code></pre></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Create the region mask object - this is used to identify each region
# cali_region = regionmask.from_geopandas(cali_aoi,
# names="name",
# name="name",
# abbrevs="iso_3166_2")
</span><span class="n">cali_mask</span> <span class="o">=</span> <span class="n">regionmask</span><span class="p">.</span><span class="n">mask_3D_geopandas</span><span class="p">(</span><span class="n">cali_aoi</span><span class="p">,</span>
<span class="n">monthly_forecast_temp_xr</span><span class="p">.</span><span class="n">lon</span><span class="p">,</span>
<span class="n">monthly_forecast_temp_xr</span><span class="p">.</span><span class="n">lat</span><span class="p">)</span>
<span class="c1"># Mask the netcdf data
</span><span class="n">cali_temp_masked</span> <span class="o">=</span> <span class="n">cali_temp</span><span class="p">.</span><span class="n">where</span><span class="p">(</span><span class="n">cali_mask</span><span class="p">)</span>
<span class="n">cali_temp_masked</span><span class="p">.</span><span class="n">dims</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>('time', 'lat', 'lon', 'region')
</code></pre></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">cali_temp</span><span class="p">.</span><span class="n">values</span><span class="p">.</span><span class="n">shape</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>(481, 227, 246)
</code></pre></div></div>
<p>Calculate the mean temperature for each season across the entire dataset.
This will produce 4 arrays - one representing mean temperature for each seasons.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">cali_season_summary</span> <span class="o">=</span> <span class="n">cali_temp_masked</span><span class="p">.</span><span class="n">groupby</span><span class="p">(</span>
<span class="s">'time.season'</span><span class="p">).</span><span class="n">mean</span><span class="p">(</span><span class="s">'time'</span><span class="p">,</span> <span class="n">skipna</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="c1"># This will create 4 arrays - one for each season showing mean temperature values
</span><span class="n">cali_season_summary</span><span class="p">.</span><span class="n">shape</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>(4, 227, 246, 1)
</code></pre></div></div>
<p>Plot the seasonal data.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Create a plot showing mean temperature aross seasons
</span><span class="n">cali_season_summary</span><span class="p">.</span><span class="n">plot</span><span class="p">(</span><span class="n">col</span><span class="o">=</span><span class="s">'season'</span><span class="p">,</span> <span class="n">col_wrap</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">))</span>
<span class="n">plt</span><span class="p">.</span><span class="n">suptitle</span><span class="p">(</span><span class="s">"Mean Temperature Across All Selected Years By Season </span><span class="se">\n</span><span class="s"> California, USA"</span><span class="p">,</span>
<span class="n">y</span><span class="o">=</span><span class="mf">1.05</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-06-seasonal-summary/2020-10-16-netcdf-06-seasonal-summary_18_0.png" alt="Facet plot showing seasonal mean temperature values for the State of California summarized over time." />
<figcaption>Facet plot showing seasonal mean temperature values for the State of California summarized over time.</figcaption>
</figure>
<h2 id="calculate-unweighted-seasonal-averages-for-by-season-across-each-year">Calculate UnWeighted Seasonal Averages For By Season Across Each Year</h2>
<p>Above you created one single value per season which summarized seasonal data across all years.
However you may want to look at seasonal variation year to year in the projected data.
You can calculate seasonal statistcs by</p>
<ol>
<li>resampling the data and then</li>
<li>grouping the data and summarizing it</li>
</ol>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Resample the data by season across all years
</span><span class="n">cali_season_mean_all_years</span> <span class="o">=</span> <span class="n">cali_temp_masked</span><span class="p">.</span><span class="n">resample</span><span class="p">(</span>
<span class="n">time</span><span class="o">=</span><span class="s">'QS-DEC'</span><span class="p">,</span> <span class="n">keep_attrs</span><span class="o">=</span><span class="bp">True</span><span class="p">).</span><span class="n">mean</span><span class="p">()</span>
<span class="n">cali_season_mean_all_years</span><span class="p">.</span><span class="n">shape</span>
</code></pre></div></div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>/opt/conda/envs/EDS/lib/python3.8/site-packages/xarray/core/common.py:1122: UserWarning: Passing ``keep_attrs`` to ``resample`` has no effect and will raise an error in xarray 0.20. Pass ``keep_attrs`` directly to the applied function, e.g. ``resample(...).mean(keep_attrs=True)``.
warnings.warn(
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>(161, 227, 246, 1)
</code></pre></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Summarize each array into one single (mean) value
</span><span class="n">cali_seasonal_mean</span> <span class="o">=</span> <span class="n">cali_season_mean_all_years</span><span class="p">.</span><span class="n">groupby</span><span class="p">(</span><span class="s">'time'</span><span class="p">).</span><span class="n">mean</span><span class="p">([</span>
<span class="s">"lat"</span><span class="p">,</span> <span class="s">"lon"</span><span class="p">])</span>
<span class="n">cali_seasonal_mean</span><span class="p">.</span><span class="n">shape</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>(161, 1)
</code></pre></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># This data now has one value per season rather than an array
</span><span class="n">cali_seasonal_mean</span><span class="p">.</span><span class="n">shape</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>(161, 1)
</code></pre></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Plot the data
</span><span class="n">f</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="p">.</span><span class="n">subplots</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">4</span><span class="p">))</span>
<span class="n">cali_seasonal_mean</span><span class="p">.</span><span class="n">plot</span><span class="p">(</span><span class="n">marker</span><span class="o">=</span><span class="s">"o"</span><span class="p">,</span>
<span class="n">color</span><span class="o">=</span><span class="s">"grey"</span><span class="p">,</span>
<span class="n">markerfacecolor</span><span class="o">=</span><span class="s">"purple"</span><span class="p">,</span>
<span class="n">markeredgecolor</span><span class="o">=</span><span class="s">"purple"</span><span class="p">)</span>
<span class="n">ax</span><span class="p">.</span><span class="nb">set</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s">"Seasonal Mean Temperature"</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-06-seasonal-summary/2020-10-16-netcdf-06-seasonal-summary_23_0.png" alt="Scatter plot showing seasonal mean temperature values for the State of California displayed over time." />
<figcaption>Scatter plot showing seasonal mean temperature values for the State of California displayed over time.</figcaption>
</figure>
<h3 id="export-seasonal-climate-project-data-to-csv-file">Export Seasonal Climate Project Data To .csv File</h3>
<p>At this point you can convert the data to a dataframe and export
it to a <code class="language-plaintext highlighter-rouge">.csv</code> format if you wish.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Convert to a dataframe
</span><span class="n">cali_seasonal_mean_df</span> <span class="o">=</span> <span class="n">cali_seasonal_mean</span><span class="p">.</span><span class="n">to_dataframe</span><span class="p">()</span>
<span class="n">cali_seasonal_mean_df</span>
</code></pre></div></div>
<div class="output execute_result">
<style scoped="">
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th></th>
<th>air_temperature</th>
</tr>
<tr>
<th>time</th>
<th>region</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>2059-12-01 00:00:00</th>
<th>53</th>
<td>287.588531</td>
</tr>
<tr>
<th>2060-03-01 00:00:00</th>
<th>53</th>
<td>294.914917</td>
</tr>
<tr>
<th>2060-06-01 00:00:00</th>
<th>53</th>
<td>307.549652</td>
</tr>
<tr>
<th>2060-09-01 00:00:00</th>
<th>53</th>
<td>296.199188</td>
</tr>
<tr>
<th>2060-12-01 00:00:00</th>
<th>53</th>
<td>286.922699</td>
</tr>
<tr>
<th>...</th>
<th>...</th>
<td>...</td>
</tr>
<tr>
<th>2098-12-01 00:00:00</th>
<th>53</th>
<td>289.853882</td>
</tr>
<tr>
<th>2099-03-01 00:00:00</th>
<th>53</th>
<td>296.710419</td>
</tr>
<tr>
<th>2099-06-01 00:00:00</th>
<th>53</th>
<td>308.563416</td>
</tr>
<tr>
<th>2099-09-01 00:00:00</th>
<th>53</th>
<td>300.005157</td>
</tr>
<tr>
<th>2099-12-01 00:00:00</th>
<th>53</th>
<td>289.929535</td>
</tr>
</tbody>
</table>
<p>161 rows × 1 columns</p>
</div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Export a csv file
</span><span class="n">cali_seasonal_mean_df</span><span class="p">.</span><span class="n">to_csv</span><span class="p">(</span><span class="s">"cali-seasonal-temp.csv"</span><span class="p">)</span>
</code></pre></div></div>
<h3 id="plot-seasonal-data-by-season">Plot Seasonal Data By Season</h3>
<p>Using <code class="language-plaintext highlighter-rouge">groupby()</code> you can group the data and plot it by season to better look at seasonal trends.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">colors</span> <span class="o">=</span> <span class="p">{</span><span class="mi">3</span><span class="p">:</span> <span class="s">"grey"</span><span class="p">,</span> <span class="mi">6</span><span class="p">:</span> <span class="s">"lightgreen"</span><span class="p">,</span> <span class="mi">9</span><span class="p">:</span> <span class="s">"green"</span><span class="p">,</span> <span class="mi">12</span><span class="p">:</span> <span class="s">"purple"</span><span class="p">}</span>
<span class="n">seasons</span> <span class="o">=</span> <span class="p">{</span><span class="mi">3</span><span class="p">:</span> <span class="s">"spring"</span><span class="p">,</span> <span class="mi">6</span><span class="p">:</span> <span class="s">"summer"</span><span class="p">,</span> <span class="mi">9</span><span class="p">:</span> <span class="s">"fall"</span><span class="p">,</span> <span class="mi">12</span><span class="p">:</span> <span class="s">"winter"</span><span class="p">}</span>
<span class="n">f</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="p">.</span><span class="n">subplots</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">7</span><span class="p">))</span>
<span class="k">for</span> <span class="n">month</span><span class="p">,</span> <span class="n">arr</span> <span class="ow">in</span> <span class="n">cali_seasonal_mean</span><span class="p">.</span><span class="n">groupby</span><span class="p">(</span><span class="s">'time.month'</span><span class="p">):</span>
<span class="n">arr</span><span class="p">.</span><span class="n">plot</span><span class="p">(</span><span class="n">ax</span><span class="o">=</span><span class="n">ax</span><span class="p">,</span>
<span class="n">color</span><span class="o">=</span><span class="s">"grey"</span><span class="p">,</span>
<span class="n">marker</span><span class="o">=</span><span class="s">"o"</span><span class="p">,</span>
<span class="n">markerfacecolor</span><span class="o">=</span><span class="n">colors</span><span class="p">[</span><span class="n">month</span><span class="p">],</span>
<span class="n">markeredgecolor</span><span class="o">=</span><span class="n">colors</span><span class="p">[</span><span class="n">month</span><span class="p">],</span>
<span class="n">label</span><span class="o">=</span><span class="n">seasons</span><span class="p">[</span><span class="n">month</span><span class="p">])</span>
<span class="n">ax</span><span class="p">.</span><span class="n">legend</span><span class="p">(</span><span class="n">bbox_to_anchor</span><span class="o">=</span><span class="p">(</span><span class="mf">1.05</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="n">loc</span><span class="o">=</span><span class="s">'upper left'</span><span class="p">)</span>
<span class="n">ax</span><span class="p">.</span><span class="nb">set</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s">"Seasonal Change in Mean Temperature Over Time"</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-06-seasonal-summary/2020-10-16-netcdf-06-seasonal-summary_28_0.png" alt="Scatter plot showing seasonal mean temperature values for the State of California summarized over time and colored by season." />
<figcaption>Scatter plot showing seasonal mean temperature values for the State of California summarized over time and colored by season.</figcaption>
</figure>
<h2 id="weighted-summary-by-season">Weighted Summary by Season</h2>
<p>To begin, you will generate a list of days in each month which will be used to weight
your seasonal summary data according to the the days in each month.</p>
<ul>
<li>TODO – redo this section to use the approach above which is perfect</li>
</ul>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Calculate seasonal averages
# http://xarray.pydata.org/en/stable/examples/monthly-means.html
</span>
<span class="n">month_length</span> <span class="o">=</span> <span class="n">cali_temp_masked</span><span class="p">.</span><span class="n">time</span><span class="p">.</span><span class="n">dt</span><span class="p">.</span><span class="n">days_in_month</span>
<span class="n">month_length</span>
</code></pre></div></div>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.DataArray 'days_in_month' (time: 481)>
array([31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30,
31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30,
31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28,
31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31,
31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31,
30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31,
30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31,
30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31,
28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30,
31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30,
31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28,
31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31,
31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31,
30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31,
30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31,
30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31,
28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30,
31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30,
31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28,
31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31,
31, 30, 31, 30, 31])
Coordinates:
* time (time) object 2059-12-15 00:00:00 ... 2099-12-15 00:00:00</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.DataArray</div><div class="xr-array-name">'days_in_month'</div><ul class="xr-dim-list"><li><span class="xr-has-index">time</span>: 481</li></ul></div><ul class="xr-sections"><li class="xr-section-item"><div class="xr-array-wrap"><input id="section-9165fc8c-f161-4d8c-9a63-2a60275454eb" class="xr-array-in" type="checkbox" checked="" /><label for="section-9165fc8c-f161-4d8c-9a63-2a60275454eb" title="Show/hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-array-preview xr-preview"><span>31 31 28 31 30 31 30 31 31 30 31 ... 28 31 30 31 30 31 31 30 31 30 31</span></div><div class="xr-array-data"><pre>array([31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30,
31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30,
31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28,
31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31,
31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31,
30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31,
30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31,
30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31,
28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30,
31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30,
31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28,
31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31,
31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31,
30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31,
30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31,
30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31,
28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30,
31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30,
31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28,
31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31,
31, 30, 31, 30, 31])</pre></div></div></li><li class="xr-section-item"><input id="section-49d0ead5-8c65-416d-8775-1510de7a9eab" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-49d0ead5-8c65-416d-8775-1510de7a9eab" class="xr-section-summary">Coordinates: <span>(1)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">time</span></div><div class="xr-var-dims">(time)</div><div class="xr-var-dtype">object</div><div class="xr-var-preview xr-preview">2059-12-15 00:00:00 ... 2099-12-...</div><input id="attrs-56911b8d-992a-4143-b64b-97b7801e9ed9" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-56911b8d-992a-4143-b64b-97b7801e9ed9" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-35f8f460-2fd9-4ee6-9dd1-97cedc574991" class="xr-var-data-in" type="checkbox" /><label for="data-35f8f460-2fd9-4ee6-9dd1-97cedc574991" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>description :</span></dt><dd>days since 1900-01-01</dd></dl></div><div class="xr-var-data"><pre>array([cftime.DatetimeNoLeap(2059, 12, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2060, 1, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2060, 2, 15, 0, 0, 0, 0, has_year_zero=True), ...,
cftime.DatetimeNoLeap(2099, 10, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2099, 11, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2099, 12, 15, 0, 0, 0, 0, has_year_zero=True)],
dtype=object)</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-a1f4f9e3-cbb8-4ee9-88e5-be4eb5f51206" class="xr-section-summary-in" type="checkbox" disabled="" /><label for="section-a1f4f9e3-cbb8-4ee9-88e5-be4eb5f51206" class="xr-section-summary" title="Expand/collapse section">Attributes: <span>(0)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"></dl></div></li></ul></div></div>
<p>Next, divide the data grouped by season by the total days represented
in each season to create weighted values</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># This is returning values of 0 rather than na
# Calculate a weighted mean by season
</span><span class="n">cali_weighted_mean</span> <span class="o">=</span> <span class="p">((</span><span class="n">cali_temp</span> <span class="o">*</span> <span class="n">month_length</span><span class="p">).</span><span class="n">resample</span><span class="p">(</span><span class="n">time</span><span class="o">=</span><span class="s">'QS-DEC'</span><span class="p">).</span><span class="nb">sum</span><span class="p">()</span> <span class="o">/</span>
<span class="n">month_length</span><span class="p">.</span><span class="n">resample</span><span class="p">(</span><span class="n">time</span><span class="o">=</span><span class="s">'QS-DEC'</span><span class="p">).</span><span class="nb">sum</span><span class="p">())</span>
<span class="c1"># Replace 0 values with nan
</span><span class="n">cali_weighted_mean</span> <span class="o">=</span> <span class="n">cali_weighted_mean</span><span class="p">.</span><span class="n">where</span><span class="p">(</span><span class="n">cali_weighted_mean</span><span class="p">)</span>
<span class="n">cali_weighted_mean</span><span class="p">.</span><span class="n">shape</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>(161, 227, 246)
</code></pre></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">cali_weighted_season_value</span> <span class="o">=</span> <span class="n">cali_weighted_mean</span><span class="p">.</span><span class="n">groupby</span><span class="p">(</span><span class="s">'time'</span><span class="p">).</span><span class="n">mean</span><span class="p">([</span>
<span class="s">"lat"</span><span class="p">,</span> <span class="s">"lon"</span><span class="p">])</span>
<span class="n">cali_weighted_season_value</span><span class="p">.</span><span class="n">shape</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>(161,)
</code></pre></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">colors</span> <span class="o">=</span> <span class="p">{</span><span class="mi">3</span><span class="p">:</span> <span class="s">"grey"</span><span class="p">,</span> <span class="mi">6</span><span class="p">:</span> <span class="s">"lightgreen"</span><span class="p">,</span> <span class="mi">9</span><span class="p">:</span> <span class="s">"green"</span><span class="p">,</span> <span class="mi">12</span><span class="p">:</span> <span class="s">"purple"</span><span class="p">}</span>
<span class="n">seasons</span> <span class="o">=</span> <span class="p">{</span><span class="mi">3</span><span class="p">:</span> <span class="s">"Spring"</span><span class="p">,</span> <span class="mi">6</span><span class="p">:</span> <span class="s">"Summer"</span><span class="p">,</span> <span class="mi">9</span><span class="p">:</span> <span class="s">"Fall"</span><span class="p">,</span> <span class="mi">12</span><span class="p">:</span> <span class="s">"Winter"</span><span class="p">}</span>
<span class="n">f</span><span class="p">,</span> <span class="p">(</span><span class="n">ax1</span><span class="p">,</span> <span class="n">ax2</span><span class="p">)</span> <span class="o">=</span> <span class="n">plt</span><span class="p">.</span><span class="n">subplots</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">7</span><span class="p">),</span> <span class="n">sharey</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="k">for</span> <span class="n">month</span><span class="p">,</span> <span class="n">arr</span> <span class="ow">in</span> <span class="n">cali_weighted_season_value</span><span class="p">.</span><span class="n">groupby</span><span class="p">(</span><span class="s">'time.month'</span><span class="p">):</span>
<span class="n">arr</span><span class="p">.</span><span class="n">plot</span><span class="p">(</span><span class="n">ax</span><span class="o">=</span><span class="n">ax1</span><span class="p">,</span>
<span class="n">color</span><span class="o">=</span><span class="s">"grey"</span><span class="p">,</span>
<span class="n">marker</span><span class="o">=</span><span class="s">"o"</span><span class="p">,</span>
<span class="n">markerfacecolor</span><span class="o">=</span><span class="n">colors</span><span class="p">[</span><span class="n">month</span><span class="p">],</span>
<span class="n">markeredgecolor</span><span class="o">=</span><span class="n">colors</span><span class="p">[</span><span class="n">month</span><span class="p">],</span>
<span class="n">label</span><span class="o">=</span><span class="n">seasons</span><span class="p">[</span><span class="n">month</span><span class="p">])</span>
<span class="n">ax1</span><span class="p">.</span><span class="n">legend</span><span class="p">(</span><span class="n">bbox_to_anchor</span><span class="o">=</span><span class="p">(</span><span class="mf">1.05</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="n">loc</span><span class="o">=</span><span class="s">'upper left'</span><span class="p">)</span>
<span class="n">ax1</span><span class="p">.</span><span class="nb">set</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s">"Weighted Seasonal Change in Mean Temperature Over Time"</span><span class="p">)</span>
<span class="k">for</span> <span class="n">month</span><span class="p">,</span> <span class="n">arr</span> <span class="ow">in</span> <span class="n">cali_seasonal_mean</span><span class="p">.</span><span class="n">groupby</span><span class="p">(</span><span class="s">'time.month'</span><span class="p">):</span>
<span class="n">arr</span><span class="p">.</span><span class="n">plot</span><span class="p">(</span><span class="n">ax</span><span class="o">=</span><span class="n">ax2</span><span class="p">,</span>
<span class="n">color</span><span class="o">=</span><span class="s">"grey"</span><span class="p">,</span>
<span class="n">marker</span><span class="o">=</span><span class="s">"o"</span><span class="p">,</span>
<span class="n">markerfacecolor</span><span class="o">=</span><span class="n">colors</span><span class="p">[</span><span class="n">month</span><span class="p">],</span>
<span class="n">markeredgecolor</span><span class="o">=</span><span class="n">colors</span><span class="p">[</span><span class="n">month</span><span class="p">],</span>
<span class="n">label</span><span class="o">=</span><span class="n">seasons</span><span class="p">[</span><span class="n">month</span><span class="p">])</span>
<span class="n">ax2</span><span class="p">.</span><span class="nb">set</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s">"Unweighted Seasonal Change in Mean Temperature Over Time"</span><span class="p">)</span>
<span class="n">f</span><span class="p">.</span><span class="n">tight_layout</span><span class="p">()</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-06-seasonal-summary/2020-10-16-netcdf-06-seasonal-summary_35_0.png" alt="Comparison of seasonal mean values - weighted vs unweighted by days in each month and colored by season." />
<figcaption>Comparison of seasonal mean values - weighted vs unweighted by days in each month and colored by season.</figcaption>
</figure>
<p>If you want, you can compare the difference between weighted vs unweighted
values.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># What does the difference look like weighted vs unweighted?
</span><span class="n">cali_seasonal_mean</span> <span class="o">-</span> <span class="n">cali_weighted_season_value</span>
</code></pre></div></div>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.DataArray (time: 161, region: 1)>
array([[2.42445398],
[1.1149551 ],
[0.56349408],
[1.41986191],
[2.10024405],
[0.99747663],
[0.74377694],
[1.67514298],
[1.65447404],
[0.6276056 ],
[0.30815426],
[1.46946358],
[1.82464189],
[1.19897048],
[0.32238954],
[1.51911652],
[2.03105749],
[1.18102475],
[0.23554512],
[1.7812482 ],
...
[1.04313289],
[0.54936444],
[1.29146021],
[2.11720145],
[0.86377328],
[0.17050946],
[1.50163027],
[2.07188164],
[1.04584555],
[0.58235629],
[1.44673595],
[2.39297506],
[1.09318667],
[0.62666739],
[1.67848387],
[1.81829381],
[0.85855153],
[0.46202606],
[1.72129072],
[1.99271339]])
Coordinates:
* time (time) object 2059-12-01 00:00:00 ... 2099-12-01 00:00:00
* region (region) int64 53</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.DataArray</div><div class="xr-array-name"></div><ul class="xr-dim-list"><li><span class="xr-has-index">time</span>: 161</li><li><span class="xr-has-index">region</span>: 1</li></ul></div><ul class="xr-sections"><li class="xr-section-item"><div class="xr-array-wrap"><input id="section-ca29c65f-5e09-4460-835f-88a3eb551579" class="xr-array-in" type="checkbox" checked="" /><label for="section-ca29c65f-5e09-4460-835f-88a3eb551579" title="Show/hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-array-preview xr-preview"><span>2.424 1.115 0.5635 1.42 2.1 0.9975 ... 1.818 0.8586 0.462 1.721 1.993</span></div><div class="xr-array-data"><pre>array([[2.42445398],
[1.1149551 ],
[0.56349408],
[1.41986191],
[2.10024405],
[0.99747663],
[0.74377694],
[1.67514298],
[1.65447404],
[0.6276056 ],
[0.30815426],
[1.46946358],
[1.82464189],
[1.19897048],
[0.32238954],
[1.51911652],
[2.03105749],
[1.18102475],
[0.23554512],
[1.7812482 ],
...
[1.04313289],
[0.54936444],
[1.29146021],
[2.11720145],
[0.86377328],
[0.17050946],
[1.50163027],
[2.07188164],
[1.04584555],
[0.58235629],
[1.44673595],
[2.39297506],
[1.09318667],
[0.62666739],
[1.67848387],
[1.81829381],
[0.85855153],
[0.46202606],
[1.72129072],
[1.99271339]])</pre></div></div></li><li class="xr-section-item"><input id="section-f3d785d4-b92e-401c-98b9-c1d0ff6132d5" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-f3d785d4-b92e-401c-98b9-c1d0ff6132d5" class="xr-section-summary">Coordinates: <span>(2)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">time</span></div><div class="xr-var-dims">(time)</div><div class="xr-var-dtype">object</div><div class="xr-var-preview xr-preview">2059-12-01 00:00:00 ... 2099-12-...</div><input id="attrs-93b7152f-f9c1-4ead-add0-19a9c13f0702" class="xr-var-attrs-in" type="checkbox" disabled="" /><label for="attrs-93b7152f-f9c1-4ead-add0-19a9c13f0702" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-71f0571c-0623-4f02-bf5e-40fcba106406" class="xr-var-data-in" type="checkbox" /><label for="data-71f0571c-0623-4f02-bf5e-40fcba106406" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"></dl></div><div class="xr-var-data"><pre>array([cftime.DatetimeNoLeap(2059, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2060, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2060, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2060, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2060, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2061, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2061, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2061, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2061, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2062, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2062, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2062, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2062, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2063, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2063, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2063, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2063, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2064, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2064, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2064, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2064, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2065, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2065, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2065, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2065, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2066, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2066, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2066, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2066, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2067, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2067, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2067, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2067, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2068, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2068, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2068, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2068, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2069, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2069, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2069, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2069, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2070, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2070, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2070, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2070, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2071, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2071, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2071, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2071, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2072, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2072, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2072, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2072, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2073, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2073, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2073, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2073, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2074, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2074, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2074, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2074, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2075, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2075, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2075, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2075, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2076, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2076, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2076, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2076, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2077, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2077, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2077, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2077, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2078, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2078, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2078, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2078, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2079, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2079, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2079, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2079, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2080, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2080, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2080, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2080, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2081, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2081, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2081, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2081, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2082, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2082, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2082, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2082, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2083, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2083, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2083, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2083, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2084, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2084, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2084, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2084, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2085, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2085, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2085, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2085, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2086, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2086, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2086, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2086, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2087, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2087, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2087, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2087, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2088, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2088, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2088, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2088, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2089, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2089, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2089, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2089, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2090, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2090, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2090, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2090, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2091, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2091, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2091, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2091, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2092, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2092, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2092, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2092, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2093, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2093, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2093, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2093, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2094, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2094, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2094, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2094, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2095, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2095, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2095, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2095, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2096, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2096, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2096, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2096, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2097, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2097, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2097, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2097, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2098, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2098, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2098, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2098, 12, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2099, 3, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2099, 6, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2099, 9, 1, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2099, 12, 1, 0, 0, 0, 0, has_year_zero=True)],
dtype=object)</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">region</span></div><div class="xr-var-dims">(region)</div><div class="xr-var-dtype">int64</div><div class="xr-var-preview xr-preview">53</div><input id="attrs-ea09508f-acae-4a69-bd13-4cce024f963f" class="xr-var-attrs-in" type="checkbox" disabled="" /><label for="attrs-ea09508f-acae-4a69-bd13-4cce024f963f" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-2a978928-450f-40a7-977f-7b221233195e" class="xr-var-data-in" type="checkbox" /><label for="data-2a978928-450f-40a7-977f-7b221233195e" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"></dl></div><div class="xr-var-data"><pre>array([53])</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-91343aad-c465-4cb7-b60c-993126c088f7" class="xr-section-summary-in" type="checkbox" disabled="" /><label for="section-91343aad-c465-4cb7-b60c-993126c088f7" class="xr-section-summary" title="Expand/collapse section">Attributes: <span>(0)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"></dl></div></li></ul></div></div>
<h2 id="the-same-analysis-for-the-west-coast">The Same Analysis for the West Coast</h2>
<p>Above you calculate seasonal summaries for the state of California. You can
implement the same analysis for each aoi region in a shapefile if you want
following the workflow that you learned in the previous lesson.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Create AOI Subset
</span><span class="n">cali_or_wash_nev</span> <span class="o">=</span> <span class="n">states_gdf</span><span class="p">[</span><span class="n">states_gdf</span><span class="p">.</span><span class="n">name</span><span class="p">.</span><span class="n">isin</span><span class="p">(</span>
<span class="p">[</span><span class="s">"California"</span><span class="p">,</span> <span class="s">"Oregon"</span><span class="p">,</span> <span class="s">"Washington"</span><span class="p">,</span> <span class="s">"Nevada"</span><span class="p">])]</span>
<span class="n">west_bounds</span> <span class="o">=</span> <span class="n">get_aoi</span><span class="p">(</span><span class="n">cali_or_wash_nev</span><span class="p">)</span>
<span class="c1"># Create the mask
</span><span class="n">west_mask</span> <span class="o">=</span> <span class="n">regionmask</span><span class="p">.</span><span class="n">mask_3D_geopandas</span><span class="p">(</span><span class="n">cali_or_wash_nev</span><span class="p">,</span>
<span class="n">monthly_forecast_temp_xr</span><span class="p">.</span><span class="n">lon</span><span class="p">,</span>
<span class="n">monthly_forecast_temp_xr</span><span class="p">.</span><span class="n">lat</span><span class="p">)</span>
<span class="c1"># Slice by time & aoi location
</span><span class="n">start_date</span> <span class="o">=</span> <span class="s">"2059-12-15"</span>
<span class="n">end_date</span> <span class="o">=</span> <span class="s">"2099-12-15"</span>
<span class="n">west_temp</span> <span class="o">=</span> <span class="n">monthly_forecast_temp_xr</span><span class="p">[</span><span class="s">"air_temperature"</span><span class="p">].</span><span class="n">sel</span><span class="p">(</span>
<span class="n">time</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="n">start_date</span><span class="p">,</span> <span class="n">end_date</span><span class="p">),</span>
<span class="n">lon</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="n">west_bounds</span><span class="p">[</span><span class="s">"lon"</span><span class="p">][</span><span class="mi">0</span><span class="p">],</span> <span class="n">west_bounds</span><span class="p">[</span><span class="s">"lon"</span><span class="p">][</span><span class="mi">1</span><span class="p">]),</span>
<span class="n">lat</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="n">west_bounds</span><span class="p">[</span><span class="s">"lat"</span><span class="p">][</span><span class="mi">0</span><span class="p">],</span> <span class="n">west_bounds</span><span class="p">[</span><span class="s">"lat"</span><span class="p">][</span><span class="mi">1</span><span class="p">]))</span>
<span class="c1"># Apply the mask
</span><span class="n">west_temp_masked</span> <span class="o">=</span> <span class="n">west_temp</span><span class="p">.</span><span class="n">where</span><span class="p">(</span><span class="n">west_mask</span><span class="p">)</span>
<span class="n">west_temp_masked</span>
<span class="c1"># Resample the data by season across all years
#west_season_mean_all_years = west_temp_masked.groupby('region').resample(time='QS-DEC', keep_attrs=True).mean()
# cali_seasonal_mean = cali_season_mean_all_years.groupby('time').mean(["lat", "lon"])
# cali_seasonal_mean
</span></code></pre></div></div>
<div class="output execute_result"><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme=dark],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1F1F1F;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 20px 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: none;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: '▼';
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: '(';
}
.xr-dim-list:after {
content: ')';
}
.xr-dim-list li:not(:last-child):after {
content: ',';
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2 {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.DataArray 'air_temperature' (time: 481, lat: 395, lon: 256, region: 4)>
array([[[[nan, nan, nan, nan],
[nan, nan, nan, nan],
[nan, nan, nan, nan],
...,
[nan, nan, nan, nan],
[nan, nan, nan, nan],
[nan, nan, nan, nan]],
[[nan, nan, nan, nan],
[nan, nan, nan, nan],
[nan, nan, nan, nan],
...,
[nan, nan, nan, nan],
[nan, nan, nan, nan],
[nan, nan, nan, nan]],
[[nan, nan, nan, nan],
[nan, nan, nan, nan],
[nan, nan, nan, nan],
...,
...
...,
[nan, nan, nan, nan],
[nan, nan, nan, nan],
[nan, nan, nan, nan]],
[[nan, nan, nan, nan],
[nan, nan, nan, nan],
[nan, nan, nan, nan],
...,
[nan, nan, nan, nan],
[nan, nan, nan, nan],
[nan, nan, nan, nan]],
[[nan, nan, nan, nan],
[nan, nan, nan, nan],
[nan, nan, nan, nan],
...,
[nan, nan, nan, nan],
[nan, nan, nan, nan],
[nan, nan, nan, nan]]]], dtype=float32)
Coordinates:
* lat (lat) float64 32.56 32.6 32.65 32.69 ... 48.85 48.9 48.94 48.98
* lon (lon) float64 235.3 235.4 235.4 235.4 ... 245.8 245.9 245.9 245.9
* time (time) object 2059-12-15 00:00:00 ... 2099-12-15 00:00:00
* region (region) int64 53 82 86 96
Attributes:
long_name: Monthly Average of Daily Maximum Near-Surface Air Tempera...
units: K
grid_mapping: crs
standard_name: air_temperature
height: 2 m
cell_methods: time: maximum(interval: 24 hours);mean over days
_ChunkSizes: [ 10 44 107]</pre><div class="xr-wrap" hidden=""><div class="xr-header"><div class="xr-obj-type">xarray.DataArray</div><div class="xr-array-name">'air_temperature'</div><ul class="xr-dim-list"><li><span class="xr-has-index">time</span>: 481</li><li><span class="xr-has-index">lat</span>: 395</li><li><span class="xr-has-index">lon</span>: 256</li><li><span class="xr-has-index">region</span>: 4</li></ul></div><ul class="xr-sections"><li class="xr-section-item"><div class="xr-array-wrap"><input id="section-a14ca1d3-3ccf-4252-b282-e37c67916155" class="xr-array-in" type="checkbox" checked="" /><label for="section-a14ca1d3-3ccf-4252-b282-e37c67916155" title="Show/hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-array-preview xr-preview"><span>nan nan nan nan nan nan nan nan ... nan nan nan nan nan nan nan nan</span></div><div class="xr-array-data"><pre>array([[[[nan, nan, nan, nan],
[nan, nan, nan, nan],
[nan, nan, nan, nan],
...,
[nan, nan, nan, nan],
[nan, nan, nan, nan],
[nan, nan, nan, nan]],
[[nan, nan, nan, nan],
[nan, nan, nan, nan],
[nan, nan, nan, nan],
...,
[nan, nan, nan, nan],
[nan, nan, nan, nan],
[nan, nan, nan, nan]],
[[nan, nan, nan, nan],
[nan, nan, nan, nan],
[nan, nan, nan, nan],
...,
...
...,
[nan, nan, nan, nan],
[nan, nan, nan, nan],
[nan, nan, nan, nan]],
[[nan, nan, nan, nan],
[nan, nan, nan, nan],
[nan, nan, nan, nan],
...,
[nan, nan, nan, nan],
[nan, nan, nan, nan],
[nan, nan, nan, nan]],
[[nan, nan, nan, nan],
[nan, nan, nan, nan],
[nan, nan, nan, nan],
...,
[nan, nan, nan, nan],
[nan, nan, nan, nan],
[nan, nan, nan, nan]]]], dtype=float32)</pre></div></div></li><li class="xr-section-item"><input id="section-8c75c793-45e6-4be2-b6e4-09e981e7e1dc" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-8c75c793-45e6-4be2-b6e4-09e981e7e1dc" class="xr-section-summary">Coordinates: <span>(4)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lat</span></div><div class="xr-var-dims">(lat)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">32.56 32.6 32.65 ... 48.94 48.98</div><input id="attrs-bfbaa13c-eccb-416d-a8c5-27f8cd5842c2" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-bfbaa13c-eccb-416d-a8c5-27f8cd5842c2" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-12534ef0-2c5d-4112-a9b9-e0757f866d59" class="xr-var-data-in" type="checkbox" /><label for="data-12534ef0-2c5d-4112-a9b9-e0757f866d59" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>latitude</dd><dt><span>standard_name :</span></dt><dd>latitude</dd><dt><span>units :</span></dt><dd>degrees_north</dd><dt><span>axis :</span></dt><dd>Y</dd><dt><span>description :</span></dt><dd>Latitude of the center of the grid cell</dd></dl></div><div class="xr-var-data"><pre>array([32.562958, 32.604626, 32.64629 , ..., 48.89603 , 48.937698, 48.979362])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">lon</span></div><div class="xr-var-dims">(lon)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">235.3 235.4 235.4 ... 245.9 245.9</div><input id="attrs-7f3343d8-1feb-47ee-bb6e-b4070eef79d9" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-7f3343d8-1feb-47ee-bb6e-b4070eef79d9" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-6c1e427f-e500-4d27-a7c1-8295903c5fd3" class="xr-var-data-in" type="checkbox" /><label for="data-6c1e427f-e500-4d27-a7c1-8295903c5fd3" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>units :</span></dt><dd>degrees_east</dd><dt><span>axis :</span></dt><dd>X</dd><dt><span>description :</span></dt><dd>Longitude of the center of the grid cell</dd><dt><span>long_name :</span></dt><dd>longitude</dd><dt><span>standard_name :</span></dt><dd>longitude</dd></dl></div><div class="xr-var-data"><pre>array([235.311157, 235.352844, 235.394501, ..., 245.852661, 245.894333,
245.936005])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">time</span></div><div class="xr-var-dims">(time)</div><div class="xr-var-dtype">object</div><div class="xr-var-preview xr-preview">2059-12-15 00:00:00 ... 2099-12-...</div><input id="attrs-8df965cd-97c3-49e3-932f-687dcb942f59" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-8df965cd-97c3-49e3-932f-687dcb942f59" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-c1338541-ab36-400d-81fc-d25d4a34bb39" class="xr-var-data-in" type="checkbox" /><label for="data-c1338541-ab36-400d-81fc-d25d4a34bb39" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>description :</span></dt><dd>days since 1900-01-01</dd></dl></div><div class="xr-var-data"><pre>array([cftime.DatetimeNoLeap(2059, 12, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2060, 1, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2060, 2, 15, 0, 0, 0, 0, has_year_zero=True), ...,
cftime.DatetimeNoLeap(2099, 10, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2099, 11, 15, 0, 0, 0, 0, has_year_zero=True),
cftime.DatetimeNoLeap(2099, 12, 15, 0, 0, 0, 0, has_year_zero=True)],
dtype=object)</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">region</span></div><div class="xr-var-dims">(region)</div><div class="xr-var-dtype">int64</div><div class="xr-var-preview xr-preview">53 82 86 96</div><input id="attrs-fa8c6aa6-d85e-48a0-beb6-513c43f7eb3e" class="xr-var-attrs-in" type="checkbox" disabled="" /><label for="attrs-fa8c6aa6-d85e-48a0-beb6-513c43f7eb3e" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-778a9517-4dd9-4611-95a3-34aa90ce075f" class="xr-var-data-in" type="checkbox" /><label for="data-778a9517-4dd9-4611-95a3-34aa90ce075f" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"></dl></div><div class="xr-var-data"><pre>array([53, 82, 86, 96])</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-f0881820-d2f8-4edc-b4ea-e3db42ac83a1" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-f0881820-d2f8-4edc-b4ea-e3db42ac83a1" class="xr-section-summary">Attributes: <span>(7)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>Monthly Average of Daily Maximum Near-Surface Air Temperature</dd><dt><span>units :</span></dt><dd>K</dd><dt><span>grid_mapping :</span></dt><dd>crs</dd><dt><span>standard_name :</span></dt><dd>air_temperature</dd><dt><span>height :</span></dt><dd>2 m</dd><dt><span>cell_methods :</span></dt><dd>time: maximum(interval: 24 hours);mean over days</dd><dt><span>_ChunkSizes :</span></dt><dd>[ 10 44 107]</dd></dl></div></li></ul></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># This produces a raster for each season over time across regions
</span><span class="n">west_coast_mean_temp_raster</span> <span class="o">=</span> <span class="n">west_temp_masked</span><span class="p">.</span><span class="n">resample</span><span class="p">(</span>
<span class="n">time</span><span class="o">=</span><span class="s">'QS-DEC'</span><span class="p">,</span> <span class="n">keep_attrs</span><span class="o">=</span><span class="bp">True</span><span class="p">).</span><span class="n">mean</span><span class="p">()</span>
<span class="n">west_coast_mean_temp_raster</span><span class="p">.</span><span class="n">shape</span>
</code></pre></div></div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>/opt/conda/envs/EDS/lib/python3.8/site-packages/xarray/core/common.py:1122: UserWarning: Passing ``keep_attrs`` to ``resample`` has no effect and will raise an error in xarray 0.20. Pass ``keep_attrs`` directly to the applied function, e.g. ``resample(...).mean(keep_attrs=True)``.
warnings.warn(
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>(161, 395, 256, 4)
</code></pre></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># This produces a regional summary
</span><span class="n">regional_summary</span> <span class="o">=</span> <span class="n">west_coast_mean_temp_raster</span><span class="p">.</span><span class="n">groupby</span><span class="p">(</span><span class="s">'time'</span><span class="p">).</span><span class="n">mean</span><span class="p">([</span>
<span class="s">"lat"</span><span class="p">,</span> <span class="s">"lon"</span><span class="p">])</span>
<span class="n">regional_summary</span><span class="p">.</span><span class="n">plot</span><span class="p">(</span><span class="n">col</span><span class="o">=</span><span class="s">"region"</span><span class="p">,</span>
<span class="n">marker</span><span class="o">=</span><span class="s">"o"</span><span class="p">,</span>
<span class="n">color</span><span class="o">=</span><span class="s">"grey"</span><span class="p">,</span>
<span class="n">markerfacecolor</span><span class="o">=</span><span class="s">"purple"</span><span class="p">,</span>
<span class="n">markeredgecolor</span><span class="o">=</span><span class="s">"purple"</span><span class="p">,</span>
<span class="n">col_wrap</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span>
<span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">12</span><span class="p">,</span> <span class="mi">8</span><span class="p">))</span>
<span class="n">plt</span><span class="p">.</span><span class="n">suptitle</span><span class="p">(</span><span class="s">"Seasonal Temperature by Region"</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mf">1.03</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>
<figure class="output display_data">
<img src="https://www.earthdatascience.org/images/courses/intermediate-eds-textbook/06-hierchical-data-formats/03-netcdf/2020-10-16-netcdf-06-seasonal-summary/2020-10-16-netcdf-06-seasonal-summary_41_0.png" alt="Facet plot showing seasonal mean temperature values for the several areas of interest (aoi's) including California, Washington, Oregon and Nevada and summarized over time." />
<figcaption>Facet plot showing seasonal mean temperature values for the several areas of interest (aoi's) including California, Washington, Oregon and Nevada and summarized over time.</figcaption>
</figure>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># The data can then be easily converted to a dataframe
</span><span class="n">regional_summary</span><span class="p">.</span><span class="n">to_dataframe</span><span class="p">()</span>
</code></pre></div></div>
<div class="output execute_result">
<style scoped="">
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th></th>
<th>air_temperature</th>
</tr>
<tr>
<th>time</th>
<th>region</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th rowspan="4" valign="top">2059-12-01 00:00:00</th>
<th>53</th>
<td>287.588531</td>
</tr>
<tr>
<th>82</th>
<td>281.238739</td>
</tr>
<tr>
<th>86</th>
<td>280.193787</td>
</tr>
<tr>
<th>96</th>
<td>279.038940</td>
</tr>
<tr>
<th>2060-03-01 00:00:00</th>
<th>53</th>
<td>294.914886</td>
</tr>
<tr>
<th>...</th>
<th>...</th>
<td>...</td>
</tr>
<tr>
<th>2099-09-01 00:00:00</th>
<th>96</th>
<td>290.840363</td>
</tr>
<tr>
<th rowspan="4" valign="top">2099-12-01 00:00:00</th>
<th>53</th>
<td>289.929504</td>
</tr>
<tr>
<th>82</th>
<td>284.486908</td>
</tr>
<tr>
<th>86</th>
<td>282.051971</td>
</tr>
<tr>
<th>96</th>
<td>280.577484</td>
</tr>
</tbody>
</table>
<p>644 rows × 1 columns</p>
</div>Leah WasserLearn how to calculate seasonal summary values for MACA 2 climate data using xarray and region mask in open source Python.Introduction to List Comprehensions in Python: Write More Efficient Loops2020-07-03T00:00:00+00:002020-07-03T00:00:00+00:00https://www.earthdatascience.org/courses/intro-to-earth-data-science/write-efficient-python-code/loops/loops-04-list-comprehension<aside class="sidebar__right">
<nav class="toc">
<header><h4 class="nav__title"><i class="fa fa-file-text"></i> On This Page</h4></header>
<ul class="toc__menu" id="markdown-toc">
<li><a href="#learning-objectives" id="markdown-toc-learning-objectives"><i class="fa fa-graduation-cap" aria-hidden="true"></i> Learning Objectives</a></li>
<li><a href="#list-comprehension-basics" id="markdown-toc-list-comprehension-basics">List Comprehension Basics</a></li>
<li><a href="#time-saved-with-list-comprehension" id="markdown-toc-time-saved-with-list-comprehension">Time Saved with List Comprehension</a></li>
<li><a href="#modify-values-with-list-comprehension" id="markdown-toc-modify-values-with-list-comprehension">Modify Values with List Comprehension</a></li>
<li><a href="#apply-a-function-to-a-list" id="markdown-toc-apply-a-function-to-a-list">Apply a Function to a List</a></li>
<li><a href="#conditionals" id="markdown-toc-conditionals">Conditionals</a></li>
</ul>
</nav>
</aside>
<div class="notice--success">
<h2 id="learning-objectives"><i class="fa fa-graduation-cap" aria-hidden="true"></i> Learning Objectives</h2>
<p>After completing this chapter, you will be able to:</p>
<ul>
<li>Modify values in a list using a list comprehension</li>
<li>Apply a function to values in a list using a list comprehension</li>
<li>Use conditional statements within a list comprehension to control list outputs</li>
</ul>
</div>
<h2 id="list-comprehension-basics">List Comprehension Basics</h2>
<p>Loops, as you’ve seen, can be a very powerful tool to manipulate and create data.
However, they’re not the only option when it comes to these types of operations.
Another popular method is list comprehension. It’s a concise and quick way to modify
values in a list and create a new list from the output. It works in a similar way to
a <code class="language-plaintext highlighter-rouge">for</code> loop, but has slightly different syntax. One can be translated to the other fairly
easily!</p>
<p>To perform list comprehension, you have to put the for loop and the desired outcome inside
of a list. So this:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>new_list = []
for i in list:
new_list.append(i*i)
</code></pre></div></div>
<p>becomes this:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>new_list = [i*i for i in list]
</code></pre></div></div>
<p>You can see that the code takes up less space, and uses similar words to the for loop.
However, the execution is different.</p>
<h3 id="benefits-and-downsides-of-list-comprehension">Benefits and Downsides of List Comprehension</h3>
<p>There are many pros and cons to consider when using list comprehension.</p>
<p>Pros:</p>
<ul>
<li>Generally faster than for loops, especially for large datasets.</li>
<li>Takes less code to write and fits in a smaller space than a for loop.</li>
</ul>
<p>Cons:</p>
<ul>
<li>Can be less legible in certain situations.</li>
<li>Can be harder to implement for complicated operations in for loops.</li>
</ul>
<p>In this lesson, you will go over things you learned how to do with traditional <code class="language-plaintext highlighter-rouge">for</code>
loops and see how to do them with list comprehension.</p>
<h2 id="time-saved-with-list-comprehension">Time Saved with List Comprehension</h2>
<p>Because of differences in how <strong>Python</strong> implements for loops and list comprehension,
list comprehensions are almost always faster than for loops when performing operations.
Below, the same operation is performed by list comprehension and by for loop. It’s a simple
operation, it’s just creating a list of the squares of numbers from 1 to 50000. From the
timed cells below, you can see that the list comprehension runs almost twice as fast as the
for loop for this calculation. This is one of the primary benefits of using list
comprehension.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">%%</span><span class="n">time</span>
<span class="c1"># Time a cell using a for loop
</span><span class="n">for_list</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">50000</span><span class="p">):</span>
<span class="n">for_list</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">i</span><span class="o">*</span><span class="n">i</span><span class="p">)</span>
</code></pre></div></div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>CPU times: user 0 ns, sys: 7.63 ms, total: 7.63 ms
Wall time: 7.6 ms
</code></pre></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">%%</span><span class="n">time</span>
<span class="c1"># Time a cell using list comprehension
</span><span class="n">comp_list</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span><span class="o">*</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">50000</span><span class="p">)]</span>
</code></pre></div></div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>CPU times: user 3.61 ms, sys: 0 ns, total: 3.61 ms
Wall time: 3.62 ms
</code></pre></div></div>
<h2 id="modify-values-with-list-comprehension">Modify Values with List Comprehension</h2>
<p>Operations previously done by for loops can use list comprehension. You have
converted inches to millimeters many times in these lessons. A cleaner way to
do this is using list comprehension. Below is a list of Boulder precipitation
values in inches being modified to millimeters with list comprehension.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Create list of average monthly precip (inches) in Boulder, CO
</span><span class="n">avg_monthly_precip_in</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.70</span><span class="p">,</span> <span class="mf">0.75</span><span class="p">,</span> <span class="mf">1.85</span><span class="p">,</span> <span class="mf">2.93</span><span class="p">,</span> <span class="mf">3.05</span><span class="p">,</span> <span class="mf">2.02</span><span class="p">,</span>
<span class="mf">1.93</span><span class="p">,</span> <span class="mf">1.62</span><span class="p">,</span> <span class="mf">1.84</span><span class="p">,</span> <span class="mf">1.31</span><span class="p">,</span> <span class="mf">1.39</span><span class="p">,</span> <span class="mf">0.84</span><span class="p">]</span>
<span class="c1"># Convert each item in list from in to mm
</span><span class="p">[</span><span class="n">month</span> <span class="o">*</span> <span class="mf">25.4</span> <span class="k">for</span> <span class="n">month</span> <span class="ow">in</span> <span class="n">avg_monthly_precip_in</span><span class="p">]</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>[17.779999999999998,
19.049999999999997,
46.99,
74.422,
77.46999999999998,
51.308,
49.022,
41.148,
46.736,
33.274,
35.306,
21.336]
</code></pre></div></div>
<h2 id="apply-a-function-to-a-list">Apply a Function to a List</h2>
<p>Similar to modifying a value in a list, it’s possible to use list comprehension
to apply a function to every value in a list. This can be useful for more complicated
operations that need to be performed. This can also be done with the <code class="language-plaintext highlighter-rouge">map</code> function.
More info on mapping can be found in the Data Tip below.</p>
<div class="notice--success alert alert-info">
<p><i class="fa fa-star"></i> <strong>Data Tip:</strong> <code class="language-plaintext highlighter-rouge">map</code> in <strong>Python</strong></p>
<p>While a list comprehension is one way to apply a function to every variable in a list, <strong>Python</strong> has functions more suited for this type of operation, namely the <code class="language-plaintext highlighter-rouge">map()</code> function. Although it can be more complicated, it is very useful for the type of situation where you would be applying a complicated function to every variable in a list, <strong>pandas</strong> <code class="language-plaintext highlighter-rouge">DataFrame</code>, or other data storage object. <a href="https://www.geeksforgeeks.org/python-map-function/">For further reading on <code class="language-plaintext highlighter-rouge">map()</code>, see this website explaining the fundamentals.</a></p>
</div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Function written to convert from inches to mm
</span><span class="k">def</span> <span class="nf">convert_in_to_mm</span><span class="p">(</span><span class="n">num</span><span class="p">):</span>
<span class="k">return</span> <span class="n">num</span> <span class="o">*</span> <span class="mf">25.4</span>
<span class="c1"># Using list comprehension to convert all the variables in the list
</span><span class="p">[</span><span class="n">convert_in_to_mm</span><span class="p">(</span><span class="n">month</span><span class="p">)</span> <span class="k">for</span> <span class="n">month</span> <span class="ow">in</span> <span class="n">avg_monthly_precip_in</span><span class="p">]</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>[17.779999999999998,
19.049999999999997,
46.99,
74.422,
77.46999999999998,
51.308,
49.022,
41.148,
46.736,
33.274,
35.306,
21.336]
</code></pre></div></div>
<h2 id="conditionals">Conditionals</h2>
<h3 id="if-condition-only">If Condition Only</h3>
<p>Conditionals can be implemented in list comprehension. This is can be an easy way
to filter out unwanted variables from a list. If the conditional doesn’t have an
<code class="language-plaintext highlighter-rouge">else</code> statement, the <code class="language-plaintext highlighter-rouge">if</code> condition is put after the for loop.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Filtering out values in a month that are less than 1.5
</span><span class="p">[</span><span class="n">month</span> <span class="k">for</span> <span class="n">month</span> <span class="ow">in</span> <span class="n">avg_monthly_precip_in</span> <span class="k">if</span> <span class="n">month</span> <span class="o">></span> <span class="mf">1.5</span><span class="p">]</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>[1.85, 2.93, 3.05, 2.02, 1.93, 1.62, 1.84]
</code></pre></div></div>
<h3 id="if-else-conditionals">If Else Conditionals</h3>
<p>If your conditional has an else statement, it is formatted differently. In this
case, it would go before the for loop, with the operation for the <code class="language-plaintext highlighter-rouge">if</code> condition
going before <code class="language-plaintext highlighter-rouge">if</code>, and the operation for the <code class="language-plaintext highlighter-rouge">else</code> condition going after <code class="language-plaintext highlighter-rouge">else</code>.</p>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Performing two different operations on the variables depending on if they are more or less than 1.5.
# If they are more then 1.5, they are multiplied by negative 2. Otherwise, they are multiplied by positive 2.
</span><span class="p">[</span><span class="n">month</span> <span class="o">*</span> <span class="o">-</span><span class="mi">2</span> <span class="k">if</span> <span class="n">month</span> <span class="o">></span> <span class="mf">1.5</span> <span class="k">else</span> <span class="n">month</span> <span class="o">*</span> <span class="mi">2</span> <span class="k">for</span> <span class="n">month</span> <span class="ow">in</span> <span class="n">avg_monthly_precip_in</span><span class="p">]</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>[1.4, 1.5, -3.7, -5.86, -6.1, -4.04, -3.86, -3.24, -3.68, 2.62, 2.78, 1.68]
</code></pre></div></div>Nathan KorinekA list comprehensions in Python is a type of loop that is often faster than traditional loops. Learn how to create list comprehensions to automate data tasks in Python.Loops in Python Exercise2020-07-03T00:00:00+00:002020-07-03T00:00:00+00:00https://www.earthdatascience.org/courses/intro-to-earth-data-science/write-efficient-python-code/loops/loops-05-loops-exercise<aside class="sidebar__right">
<nav class="toc">
<header><h4 class="nav__title"><i class="fa fa-file-text"></i> On This Page</h4></header>
<ul class="toc__menu" id="markdown-toc">
<li><a href="#learning-objectives" id="markdown-toc-learning-objectives"><i class="fa fa-graduation-cap" aria-hidden="true"></i> Learning Objectives</a></li>
<li><a href="#challenge-1-print-numbers-in-a-list" id="markdown-toc-challenge-1-print-numbers-in-a-list"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Challenge 1: Print Numbers in a list</a></li>
<li><a href="#challenge-2-modify-numeric-values-in-a-list" id="markdown-toc-challenge-2-modify-numeric-values-in-a-list"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Challenge 2: Modify Numeric Values in a List</a></li>
<li><a href="#challenge-3-round-values-in-a-list" id="markdown-toc-challenge-3-round-values-in-a-list"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Challenge 3: Round Values In a List</a></li>
<li><a href="#challenge-4-print-a-list-of-directories" id="markdown-toc-challenge-4-print-a-list-of-directories"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Challenge 4: Print A List of Directories</a></li>
<li><a href="#challenge-5-print-a-list-of-all-files-within-each-directory" id="markdown-toc-challenge-5-print-a-list-of-all-files-within-each-directory"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Challenge 5: Print A List of All Files Within Each Directory</a></li>
<li><a href="#bonus-challenge-1-get-data-from-list-of-files" id="markdown-toc-bonus-challenge-1-get-data-from-list-of-files"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Bonus Challenge 1: Get Data from List of Files</a></li>
<li><a href="#bonus-challenge-2-collatz-conjecture" id="markdown-toc-bonus-challenge-2-collatz-conjecture"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Bonus Challenge 2: Collatz Conjecture</a></li>
</ul>
</nav>
</aside>
<div class="notice--success">
<h2 id="learning-objectives"><i class="fa fa-graduation-cap" aria-hidden="true"></i> Learning Objectives</h2>
<p>This page of exercises will test the skills that you learned in the previous lessons
in this chapter. You will practice using loops to help with common coding tasks, using
for and while loops, and looping over different types of data.</p>
</div>
<div class="notice--warning">
<h2 id="challenge-1-print-numbers-in-a-list"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Challenge 1: Print Numbers in a list</h2>
<p>The list below contains temperature values for a location in Boulder, Colorado.
Create a for loop that loops through each value in the list and prints the
value like this: `</p>
<p><code class="language-plaintext highlighter-rouge">temp: 47</code></p>
<p>HINT: you can print a string and a variable together using the syntax:</p>
<p><code class="language-plaintext highlighter-rouge">print("temp:", variable_name_here)</code></p>
</div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Data to convert to celsius
</span>
<span class="n">boulder_avg_high_temp_f</span> <span class="o">=</span> <span class="p">[</span>
<span class="mi">47</span><span class="p">,</span>
<span class="mi">49</span><span class="p">,</span>
<span class="mi">57</span><span class="p">,</span>
<span class="mi">64</span><span class="p">,</span>
<span class="mi">72</span><span class="p">,</span>
<span class="mi">83</span><span class="p">,</span>
<span class="mi">89</span><span class="p">,</span>
<span class="mi">87</span><span class="p">,</span>
<span class="mi">79</span><span class="p">,</span>
<span class="mi">67</span><span class="p">,</span>
<span class="mi">55</span><span class="p">,</span>
<span class="mi">47</span>
<span class="p">]</span>
<span class="n">boulder_avg_high_temp_f</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>[47, 49, 57, 64, 72, 83, 89, 87, 79, 67, 55, 47]
</code></pre></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Add your code here
</span>
</code></pre></div></div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>temp: 47
temp: 49
temp: 57
temp: 64
temp: 72
temp: 83
temp: 89
temp: 87
temp: 79
temp: 67
temp: 55
temp: 47
</code></pre></div></div>
<div class="notice--warning">
<h2 id="challenge-2-modify-numeric-values-in-a-list"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Challenge 2: Modify Numeric Values in a List</h2>
<p>Below is a list of values that represents the average monthly high temperature
in Boulder, CO., collected by NOAA. They are currently in Fahrenheit, but can be
converted to Celsius by subtracting 32, and multiplying by 5/9.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>celcius = (fahrenheit - 32) * 5/9
</code></pre></div> </div>
<p>Create a <strong>new list</strong> with these same temperatures converted to Celsius using a for loop.
Call your new list: <code class="language-plaintext highlighter-rouge">boulder_avg_high_temp_c</code></p>
<p>HINT: to complete this challenge you may want to create a new empty list first.
Then you can use <code class="language-plaintext highlighter-rouge">list_name.append()</code> in each loop iteration to add a new
value to your list.</p>
</div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Add your code here
</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>[8.333333333333334,
9.444444444444445,
13.88888888888889,
17.77777777777778,
22.22222222222222,
28.333333333333336,
31.666666666666668,
30.555555555555557,
26.11111111111111,
19.444444444444446,
12.777777777777779,
8.333333333333334]
</code></pre></div></div>
<div class="notice--warning">
<h2 id="challenge-3-round-values-in-a-list"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Challenge 3: Round Values In a List</h2>
<p>Create a loop that rounds the values in the list that you created above:
<code class="language-plaintext highlighter-rouge">boulder_avg_high_temp_c</code> to only two decimal places.</p>
<p>To round your data, you can use the <strong>Python</strong> function <code class="language-plaintext highlighter-rouge">round()</code>. The
first argument in the <code class="language-plaintext highlighter-rouge">round()</code> function is the number to round, and the
second argument is the number of decimals you want after it’s been rounded.
See how this works below.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>c = 7.3848234
round(c, 2)
# 7.38
</code></pre></div> </div>
<p>Create a new list called <code class="language-plaintext highlighter-rouge">boulder_avg_high_temp_c_round</code> that contains temperature
data that has been rounded.</p>
</div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Add your code here
</span>
</code></pre></div></div>
<div class="language-plaintext output execute_result highlighter-rouge"><div class="highlight"><pre class="highlight"><code>[8.33,
9.44,
13.89,
17.78,
22.22,
28.33,
31.67,
30.56,
26.11,
19.44,
12.78,
8.33]
</code></pre></div></div>
<div class="notice--warning">
<h2 id="challenge-4-print-a-list-of-directories"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Challenge 4: Print A List of Directories</h2>
<p>The code below creates a list of directories called <code class="language-plaintext highlighter-rouge">all_dirs</code>.
Create a <strong>for loop</strong> that prints each directory name.</p>
</div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">glob</span> <span class="kn">import</span> <span class="n">glob</span>
<span class="kn">import</span> <span class="nn">earthpy</span> <span class="k">as</span> <span class="n">et</span>
<span class="c1"># Download data on average monthly temp for two California sites
</span><span class="n">file_url</span> <span class="o">=</span> <span class="s">"https://ndownloader.figshare.com/files/21894528"</span>
<span class="n">out_path</span> <span class="o">=</span> <span class="n">et</span><span class="p">.</span><span class="n">data</span><span class="p">.</span><span class="n">get_data</span><span class="p">(</span><span class="n">url</span> <span class="o">=</span> <span class="n">file_url</span><span class="p">)</span>
<span class="c1"># Set working directory to earth-analytics
</span><span class="n">os</span><span class="p">.</span><span class="n">chdir</span><span class="p">(</span><span class="n">os</span><span class="p">.</span><span class="n">path</span><span class="p">.</span><span class="n">join</span><span class="p">(</span><span class="n">et</span><span class="p">.</span><span class="n">io</span><span class="p">.</span><span class="n">HOME</span><span class="p">,</span>
<span class="s">"earth-analytics"</span><span class="p">,</span>
<span class="s">"data"</span><span class="p">,</span>
<span class="s">"earthpy-downloads"</span><span class="p">))</span>
<span class="c1"># Creating all_dirs list of directories to loop through
</span>
<span class="n">data_dirs</span> <span class="o">=</span> <span class="n">os</span><span class="p">.</span><span class="n">path</span><span class="p">.</span><span class="n">join</span><span class="p">(</span><span class="n">out_path</span><span class="p">,</span> <span class="s">"*"</span><span class="p">)</span>
<span class="n">all_dirs</span> <span class="o">=</span> <span class="n">glob</span><span class="p">(</span><span class="n">data_dirs</span><span class="p">)</span>
</code></pre></div></div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Add your code here
</span>
</code></pre></div></div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>/root/earth-analytics/data/earthpy-downloads/avg-monthly-temp-fahr/Sonoma
/root/earth-analytics/data/earthpy-downloads/avg-monthly-temp-fahr/San-Diego
</code></pre></div></div>
<div class="notice--warning">
<h2 id="challenge-5-print-a-list-of-all-files-within-each-directory"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Challenge 5: Print A List of All Files Within Each Directory</h2>
<p>Above, you printed the name of each directory stored in a list of directories.
Use the same for loop that you created above to print a list of all files in
each directory.</p>
<p>HINT: you will want to use the glob function to create a list of files within each directory.</p>
<ul>
<li><a href="https://www.earthdatascience.org/courses/intro-to-earth-data-science/python-code-fundamentals/work-with-files-directories-paths-in-python/os-glob-manipulate-file-paths/">More on using the glob function here</a></li>
</ul>
</div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Add your code here
</span>
</code></pre></div></div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>['/root/earth-analytics/data/earthpy-downloads/avg-monthly-temp-fahr/Sonoma/Sonoma-2001-temp.csv', '/root/earth-analytics/data/earthpy-downloads/avg-monthly-temp-fahr/Sonoma/Sonoma-1999-temp.csv', '/root/earth-analytics/data/earthpy-downloads/avg-monthly-temp-fahr/Sonoma/Sonoma-2000-temp.csv', '/root/earth-analytics/data/earthpy-downloads/avg-monthly-temp-fahr/Sonoma/Sonoma-2002-temp.csv', '/root/earth-analytics/data/earthpy-downloads/avg-monthly-temp-fahr/Sonoma/Sonoma-2003-temp.csv']
['/root/earth-analytics/data/earthpy-downloads/avg-monthly-temp-fahr/San-Diego/San-Diego-2003-temp.csv', '/root/earth-analytics/data/earthpy-downloads/avg-monthly-temp-fahr/San-Diego/San-Diego-1999-temp.csv', '/root/earth-analytics/data/earthpy-downloads/avg-monthly-temp-fahr/San-Diego/San-Diego-2000-temp.csv', '/root/earth-analytics/data/earthpy-downloads/avg-monthly-temp-fahr/San-Diego/San-Diego-2001-temp.csv', '/root/earth-analytics/data/earthpy-downloads/avg-monthly-temp-fahr/San-Diego/San-Diego-2002-temp.csv']
</code></pre></div></div>
<div class="notice--warning">
<h2 id="bonus-challenge-1-get-data-from-list-of-files"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Bonus Challenge 1: Get Data from List of Files</h2>
<p>Above, you created a list inside of a <code class="language-plaintext highlighter-rouge">for</code> loop to view all of the files
stored in two separate folders. These files are <code class="language-plaintext highlighter-rouge">csv</code> files that can be opened
with <strong>pandas</strong> as a <code class="language-plaintext highlighter-rouge">DataFrame</code>. The files contain the average monthly temperature
for two different study locations, Sonoma and San Diego. Their are <code class="language-plaintext highlighter-rouge">csv</code> files for
each location for the years between 1999 and 2003.</p>
<p>For this challenge, use nested <code class="language-plaintext highlighter-rouge">for</code> loops to get data from the files and find the
average temperature in January over the years for the two sites. The end result
should be two variables that represent the average January temperature for each site.
Their are many ways to get this data, so don’t be afraid to get creative!</p>
</div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># You will need pandas for this challenge
</span><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="n">pd</span>
<span class="c1"># Add your code here
</span>
</code></pre></div></div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>San Diego January Mean Temperature from 1999 to 2003: 65.52 ºF
Sonoma January Mean Temperature from 1999 to 2003: 56.82 ºF
</code></pre></div></div>
<div class="notice--warning">
<h2 id="bonus-challenge-2-collatz-conjecture"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Bonus Challenge 2: Collatz Conjecture</h2>
<p>The Collatz Conjecture is a mathematic rule that says that if the following
rules are performed on any positive interger, the number will eventually reach</p>
<ol>
<li>
<p>The rules are:</p>
</li>
<li>If the integer is even, the next integer is one half of the current integer.</li>
<li>If the integer is odd, the next term is 3 times the current integer plus 1.</li>
</ol>
<p>If these rules are followed, any integer will eventually reach one. Using a <code class="language-plaintext highlighter-rouge">while</code>
loop, implement these rules so that a variable you enter into the while loop has these
rules run on it until it equals one. Here are some helpful hints to help you implement
these rules:</p>
<ol>
<li>To check if a number is odd or even in <strong>Python</strong>, it is common practice to see if the remainder of the number divided by 2 equals zero. If you remember, <code class="language-plaintext highlighter-rouge">%</code> will get the remainder of a number divided by another number. So, to see if a number is even, the code <code class="language-plaintext highlighter-rouge">n%2 == 0</code> will return <code class="language-plaintext highlighter-rouge">True</code> if <code class="language-plaintext highlighter-rouge">n</code> is even, and <code class="language-plaintext highlighter-rouge">False</code> if <code class="language-plaintext highlighter-rouge">n</code> is odd.</li>
<li>The <code class="language-plaintext highlighter-rouge">while</code> loop will run until the input number equals one. But you also need to remember not to run the code on the number if it does equal one. So in the odd calculation, make sure that the number doesn’t equal one before you run the calculation on it.</li>
</ol>
<p>Print out the number variable with each pass through of the while loop. Have your number
variable equal <code class="language-plaintext highlighter-rouge">10000</code> before the <code class="language-plaintext highlighter-rouge">while</code> loop is run. Careful with this, it shouldn’t
take long to run. If it is taking a long time to run, there’s probably a mistake in your
code and your while loop will be running forever until you stop it! Once your code runs,
change the number variable to see it run on any number you want!</p>
<p>For further explanation on the Collatz Conjecture, and what it looks like to implement it, <a href="https://www.youtube.com/watch?v=5mFpVDpKX70">this YouTube video explains the basics of the math behind it</a> and <a href="https://en.wikipedia.org/wiki/Collatz_conjecture">the Wikipedia page on the number has more in depth explanations of the math</a>.</p>
</div>
<div class="language-python input highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Add your code here
</span>
</code></pre></div></div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>10000
5000.0
2500.0
1250.0
625.0
1876.0
938.0
469.0
1408.0
704.0
352.0
176.0
88.0
44.0
22.0
11.0
34.0
17.0
52.0
26.0
13.0
40.0
20.0
10.0
5.0
16.0
8.0
4.0
2.0
1.0
</code></pre></div></div>Nathan KorinekLoops can be used to automate data tasks in Python by iteratively executing the same code on multiple data structures. Practice using loops to automate certain functionality in Python.Python Plot Activities2020-06-24T00:00:00+00:002020-06-24T00:00:00+00:00https://www.earthdatascience.org/courses/scientists-guide-to-plotting-data-in-python/activity-00-landing-page<aside class="sidebar__right">
<nav class="toc">
<header><h4 class="nav__title"><i class="fa fa-file-text"></i> Section Three</h4></header>
<ul class="toc__menu" id="markdown-toc">
<li><a href="#section-three---open-source-python-plotting-activities" id="markdown-toc-section-three---open-source-python-plotting-activities"><i class="fa fa-ship" aria-hidden="true"></i> Section Three - Open Source Python Plotting Activities</a></li>
<li><a href="#learning-objectives" id="markdown-toc-learning-objectives"><i class="fa fa-graduation-cap" aria-hidden="true"></i> Learning Objectives</a></li>
</ul>
</nav>
</aside>
<div class="notice--info">
<h2 id="section-three---open-source-python-plotting-activities"><i class="fa fa-ship" aria-hidden="true"></i> Section Three - Open Source Python Plotting Activities</h2>
<p>In section two of this textbook, you will test your skills with plotting different
types of data using open source <strong>Python</strong>.</p>
<h2 id="learning-objectives"><i class="fa fa-graduation-cap" aria-hidden="true"></i> Learning Objectives</h2>
<p>After completing this section of the Scientist’s Guide to Plotting Data in Python online textbook, you will be able to:</p>
<ul>
<li>Plot different types of data using open source python approaches.</li>
</ul>
</div>
<div>
<table>
<thead>
<tr>
<th>
<a href="https://www.earthdatascience.org/courses/scientists-guide-to-plotting-data-in-python/plot-activities/"> Section 3. Python Plot Activities</a>
</th>
</tr>
</thead>
<tbody>
<!-- list the chapters for each section -->
<tr>
<td>
<a href="https://www.earthdatascience.org/courses/scientists-guide-to-plotting-data-in-python/plot-activities/plot-tabular-pandas-data-python/">Chapter 5 : Practice Plotting </a>
</td>
</tr>
</tbody>
</table>
</div>Earth Analyticsearth.lab@colorado.eduSection Three