You can run this notebook in a live session or view it on Github.

# GRIB Data Example

GRIB format is commonly used to disseminate atmospheric model data. With xarray and the cfgrib engine, GRIB data can easily be analyzed and visualized.

    [1]:

    import xarray as xr
    import matplotlib.pyplot as plt

To read GRIB data, you can use `xarray.load_dataset`. The only extra code you need is to specify the engine as `cfgrib`.

    [2]:

    ds = xr.tutorial.load_dataset("era5-2mt-2019-03-uk.grib", engine="cfgrib")

    ---------------------------------------------------------------------------
    gaierror                                  Traceback (most recent call last)
    File /usr/lib/python3/dist-packages/urllib3/connection.py:198, in HTTPConnection._new_conn(self)
        197 try:
    --> 198     sock = connection.create_connection(
        199         (self._dns_host, self.port),
        200         self.timeout,
        201         source_address=self.source_address,
        202         socket_options=self.socket_options,
        203     )
        204 except socket.gaierror as e:

    File /usr/lib/python3/dist-packages/urllib3/util/connection.py:60, in create_connection(address, timeout, source_address, socket_options)
         58     raise LocationParseError(f"'{host}', label empty or too long") from None
    ---> 60 for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
         61     af, socktype, proto, canonname, sa = res

    File /usr/lib/python3.13/socket.py:977, in getaddrinfo(host, port, family, type, proto, flags)
        976 addrlist = []
    --> 977 for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
        978     af, socktype, proto, canonname, sa = res

    gaierror: [Errno -2] Name or service not known

    The above exception was the direct cause of the following exception:

    NameResolutionError                       Traceback (most recent call last)
    File /usr/lib/python3/dist-packages/urllib3/connectionpool.py:787, in HTTPConnectionPool.urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, preload_content, decode_content, **response_kw)
        786 # Make the request on the HTTPConnection object
    --> 787 response = self._make_request(
        788     conn,
        789     method,
        790     url,
        791     timeout=timeout_obj,
        792     body=body,
        793     headers=headers,
        794     chunked=chunked,
        795     retries=retries,
        796     response_conn=response_conn,
        797     preload_content=preload_content,
        798     decode_content=decode_content,
        799     **response_kw,
        800 )
        802 # Everything went great!

    File /usr/lib/python3/dist-packages/urllib3/connectionpool.py:488, in HTTPConnectionPool._make_request(self, conn, method, url, body, headers, retries, timeout, chunked, response_conn, preload_content, decode_content, enforce_content_length)
        487         new_e = _wrap_proxy_error(new_e, conn.proxy.scheme)
    --> 488     raise new_e
        490 # conn.request() calls http.client.*.request, not the method in
        491 # urllib3.request. It also calls makefile (recv) on the socket.

    File /usr/lib/python3/dist-packages/urllib3/connectionpool.py:464, in HTTPConnectionPool._make_request(self, conn, method, url, body, headers, retries, timeout, chunked, response_conn, preload_content, decode_content, enforce_content_length)
        463 try:
    --> 464     self._validate_conn(conn)
        465 except (SocketTimeout, BaseSSLError) as e:

    File /usr/lib/python3/dist-packages/urllib3/connectionpool.py:1093, in HTTPSConnectionPool._validate_conn(self, conn)
       1092 if conn.is_closed:
    -> 1093     conn.connect()
       1095 # TODO revise this, see https://github.com/urllib3/urllib3/issues/2791

    File /usr/lib/python3/dist-packages/urllib3/connection.py:704, in HTTPSConnection.connect(self)
        703 sock: socket.socket | ssl.SSLSocket
    --> 704 self.sock = sock = self._new_conn()
        705 server_hostname: str = self.host

    File /usr/lib/python3/dist-packages/urllib3/connection.py:205, in HTTPConnection._new_conn(self)
        204 except socket.gaierror as e:
    --> 205     raise NameResolutionError(self.host, self, e) from e
        206 except SocketTimeout as e:

    NameResolutionError: <urllib3.connection.HTTPSConnection object at 0x75b95319b620>: Failed to resolve 'github.com' ([Errno -2] Name or service not known)

    The above exception was the direct cause of the following exception:

    MaxRetryError                             Traceback (most recent call last)
    File /usr/lib/python3/dist-packages/requests/adapters.py:667, in HTTPAdapter.send(self, request, stream, timeout, verify, cert, proxies)
        666 try:
    --> 667     resp = conn.urlopen(
        668         method=request.method,
        669         url=url,
        670         body=request.body,
        671         headers=request.headers,
        672         redirect=False,
        673         assert_same_host=False,
        674         preload_content=False,
        675         decode_content=False,
        676         retries=self.max_retries,
        677         timeout=timeout,
        678         chunked=chunked,
        679     )
        681 except (ProtocolError, OSError) as err:

    File /usr/lib/python3/dist-packages/urllib3/connectionpool.py:841, in HTTPConnectionPool.urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, preload_content, decode_content, **response_kw)
        839     new_e = ProtocolError("Connection aborted.", new_e)
    --> 841 retries = retries.increment(
        842     method, url, error=new_e, _pool=self, _stacktrace=sys.exc_info()[2]
        843 )
        844 retries.sleep()

    File /usr/lib/python3/dist-packages/urllib3/util/retry.py:519, in Retry.increment(self, method, url, response, error, _pool, _stacktrace)
        518     reason = error or ResponseError(cause)
    --> 519     raise MaxRetryError(_pool, url, reason) from reason  # type: ignore[arg-type]
        521 log.debug("Incremented Retry for (url='%s'): %r", url, new_retry)

    MaxRetryError: HTTPSConnectionPool(host='github.com', port=443): Max retries exceeded with url: /pydata/xarray-data/raw/master/era5-2mt-2019-03-uk.grib (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x75b95319b620>: Failed to resolve 'github.com' ([Errno -2] Name or service not known)"))

    During handling of the above exception, another exception occurred:

    ConnectionError                           Traceback (most recent call last)
    Cell In[2], line 1
    ----> 1 ds = xr.tutorial.load_dataset("era5-2mt-2019-03-uk.grib", engine="cfgrib")

    File /usr/lib/python3/dist-packages/xarray/tutorial.py:215, in load_dataset(*args, **kwargs)
        178 def load_dataset(*args, **kwargs) -> Dataset:
        179     """
        180     Open, load into memory, and close a dataset from the online repository
        181     (requires internet).
       (...)
        213     load_dataset
        214     """
    --> 215     with open_dataset(*args, **kwargs) as ds:
        216         return ds.load()

    File /usr/lib/python3/dist-packages/xarray/tutorial.py:167, in open_dataset(name, cache, cache_dir, engine, **kws)
        164 downloader = pooch.HTTPDownloader(headers=headers)
        166 # retrieve the file
    --> 167 filepath = pooch.retrieve(
        168     url=url, known_hash=None, path=cache_dir, downloader=downloader
        169 )
        170 ds = _open_dataset(filepath, engine=engine, **kws)
        171 if not cache:

    File /usr/lib/python3/dist-packages/pooch/core.py:239, in retrieve(url, known_hash, fname, path, processor, downloader, progressbar)
        236 if downloader is None:
        237     downloader = choose_downloader(url, progressbar=progressbar)
    --> 239 stream_download(url, full_path, known_hash, downloader, pooch=None)
        241 if known_hash is None:
        242     get_logger().info(
        243         "SHA256 hash of downloaded file: %s\n"
        244         "Use this value as the 'known_hash' argument of 'pooch.retrieve'"
       (...)
        247         file_hash(str(full_path)),
        248     )

    File /usr/lib/python3/dist-packages/pooch/core.py:807, in stream_download(url, fname, known_hash, downloader, pooch, retry_if_failed)
        803 try:
        804     # Stream the file to a temporary so that we can safely check its
        805     # hash before overwriting the original.
        806     with temporary_file(path=str(fname.parent)) as tmp:
    --> 807         downloader(url, tmp, pooch)
        808         hash_matches(tmp, known_hash, strict=True, source=str(fname.name))
        809         shutil.move(tmp, str(fname))

    File /usr/lib/python3/dist-packages/pooch/downloaders.py:220, in HTTPDownloader.__call__(self, url, output_file, pooch, check_only)
        218     # pylint: enable=consider-using-with
        219 try:
    --> 220     response = requests.get(url, timeout=timeout, **kwargs)
        221     response.raise_for_status()
        222     content = response.iter_content(chunk_size=self.chunk_size)

    File /usr/lib/python3/dist-packages/requests/api.py:73, in get(url, params, **kwargs)
         62 def get(url, params=None, **kwargs):
         63     r"""Sends a GET request.
         64
         65     :param url: URL for the new :class:`Request` object.
       (...)
         70     :rtype: requests.Response
         71     """
    ---> 73     return request("get", url, params=params, **kwargs)

    File /usr/lib/python3/dist-packages/requests/api.py:59, in request(method, url, **kwargs)
         55 # By using the 'with' statement we are sure the session is closed, thus we
         56 # avoid leaving sockets open which can trigger a ResourceWarning in some
         57 # cases, and look like a memory leak in others.
         58 with sessions.Session() as session:
    ---> 59     return session.request(method=method, url=url, **kwargs)

    File /usr/lib/python3/dist-packages/requests/sessions.py:589, in Session.request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
        584 send_kwargs = {
        585     "timeout": timeout,
        586     "allow_redirects": allow_redirects,
        587 }
        588 send_kwargs.update(settings)
    --> 589 resp = self.send(prep, **send_kwargs)
        591 return resp

    File /usr/lib/python3/dist-packages/requests/sessions.py:703, in Session.send(self, request, **kwargs)
        700 start = preferred_clock()
        702 # Send the request
    --> 703 r = adapter.send(request, **kwargs)
        705 # Total elapsed time of the request (approximately)
        706 elapsed = preferred_clock() - start

    File /usr/lib/python3/dist-packages/requests/adapters.py:700, in HTTPAdapter.send(self, request, stream, timeout, verify, cert, proxies)
        696     if isinstance(e.reason, _SSLError):
        697         # This branch is for urllib3 v1.22 and later.
        698         raise SSLError(e, request=request)
    --> 700     raise ConnectionError(e, request=request)
        702 except ClosedPoolError as e:
        703     raise ConnectionError(e, request=request)

    ConnectionError: HTTPSConnectionPool(host='github.com', port=443): Max retries exceeded with url: /pydata/xarray-data/raw/master/era5-2mt-2019-03-uk.grib (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x75b95319b620>: Failed to resolve 'github.com' ([Errno -2] Name or service not known)"))

Let’s create a simple plot of 2-m air temperature in degrees Celsius:

    [3]:

    ds = ds - 273.15
    ds.t2m[0].plot(cmap=plt.cm.coolwarm)

    ---------------------------------------------------------------------------
    NameError                                 Traceback (most recent call last)
    Cell In[3], line 1
    ----> 1 ds = ds - 273.15
          2 ds.t2m[0].plot(cmap=plt.cm.coolwarm)

    NameError: name 'ds' is not defined

With CartoPy, we can create a more detailed plot, using built-in shapefiles to help provide geographic context:

    [4]:

    import cartopy.crs as ccrs
    import cartopy

    fig = plt.figure(figsize=(10, 10))
    ax = plt.axes(projection=ccrs.Robinson())
    ax.coastlines(resolution="10m")
    plot = ds.t2m[0].plot(
        cmap=plt.cm.coolwarm, transform=ccrs.PlateCarree(), cbar_kwargs={"shrink": 0.6}
    )
    plt.title("ERA5 - 2m temperature British Isles March 2019")

    ---------------------------------------------------------------------------
    NameError                                 Traceback (most recent call last)
    Cell In[4], line 7
          5 ax = plt.axes(projection=ccrs.Robinson())
          6 ax.coastlines(resolution="10m")
    ----> 7 plot = ds.t2m[0].plot(
          8     cmap=plt.cm.coolwarm, transform=ccrs.PlateCarree(), cbar_kwargs={"shrink": 0.6}
          9 )
         10 plt.title("ERA5 - 2m temperature British Isles March 2019")

    NameError: name 'ds' is not defined

    /usr/lib/python3/dist-packages/cartopy/io/__init__.py:241: DownloadWarning: Downloading: https://naturalearth.s3.amazonaws.com/10m_physical/ne_10m_coastline.zip
      warnings.warn(f'Downloading: {url}', DownloadWarning)

    Error in callback <function _draw_all_if_interactive at 0x75b953481e40> (for post_execute), with arguments args (),kwargs {}:

    ---------------------------------------------------------------------------
    gaierror                                  Traceback (most recent call last)
    File /usr/lib/python3.13/urllib/request.py:1319, in AbstractHTTPHandler.do_open(self, http_class, req, **http_conn_args)
       1318 try:
    -> 1319     h.request(req.get_method(), req.selector, req.data, headers,
       1320               encode_chunked=req.has_header('Transfer-encoding'))
       1321 except OSError as err: # timeout error

    File /usr/lib/python3.13/http/client.py:1338, in HTTPConnection.request(self, method, url, body, headers, encode_chunked)
       1337 """Send a complete request to the server."""
    -> 1338 self._send_request(method, url, body, headers, encode_chunked)

    File /usr/lib/python3.13/http/client.py:1384, in HTTPConnection._send_request(self, method, url, body, headers, encode_chunked)
       1383     body = _encode(body, 'body')
    -> 1384 self.endheaders(body, encode_chunked=encode_chunked)

    File /usr/lib/python3.13/http/client.py:1333, in HTTPConnection.endheaders(self, message_body, encode_chunked)
       1332     raise CannotSendHeader()
    -> 1333 self._send_output(message_body, encode_chunked=encode_chunked)

    File /usr/lib/python3.13/http/client.py:1093, in HTTPConnection._send_output(self, message_body, encode_chunked)
       1092 del self._buffer[:]
    -> 1093 self.send(msg)
       1095 if message_body is not None:
       1096
       1097     # create a consistent interface to message_body

    File /usr/lib/python3.13/http/client.py:1037, in HTTPConnection.send(self, data)
       1036 if self.auto_open:
    -> 1037     self.connect()
       1038 else:

    File /usr/lib/python3.13/http/client.py:1472, in HTTPSConnection.connect(self)
       1470 "Connect to a host on a given (SSL) port."
    -> 1472 super().connect()
       1474 if self._tunnel_host:

    File /usr/lib/python3.13/http/client.py:1003, in HTTPConnection.connect(self)
       1002 sys.audit("http.client.connect", self, self.host, self.port)
    -> 1003 self.sock = self._create_connection(
       1004     (self.host,self.port), self.timeout, self.source_address)
       1005 # Might fail in OSs that don't implement TCP_NODELAY

    File /usr/lib/python3.13/socket.py:840, in create_connection(address, timeout, source_address, all_errors)
        839 exceptions = []
    --> 840 for res in getaddrinfo(host, port, 0, SOCK_STREAM):
        841     af, socktype, proto, canonname, sa = res

    File /usr/lib/python3.13/socket.py:977, in getaddrinfo(host, port, family, type, proto, flags)
        976 addrlist = []
    --> 977 for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
        978     af, socktype, proto, canonname, sa = res

    gaierror: [Errno -2] Name or service not known

    During handling of the above exception, another exception occurred:

    URLError                                  Traceback (most recent call last)
    File /usr/lib/python3/dist-packages/matplotlib/pyplot.py:279, in _draw_all_if_interactive()
        277 def _draw_all_if_interactive() -> None:
        278     if matplotlib.is_interactive():
    --> 279         draw_all()

    File /usr/lib/python3/dist-packages/matplotlib/_pylab_helpers.py:131, in Gcf.draw_all(cls, force)
        129 for manager in cls.get_all_fig_managers():
        130     if force or manager.canvas.figure.stale:
    --> 131         manager.canvas.draw_idle()

    File /usr/lib/python3/dist-packages/matplotlib/backend_bases.py:1891, in FigureCanvasBase.draw_idle(self, *args, **kwargs)
       1889 if not self._is_idle_drawing:
       1890     with self._idle_draw_cntx():
    -> 1891         self.draw(*args, **kwargs)

    File /usr/lib/python3/dist-packages/matplotlib/backends/backend_agg.py:382, in FigureCanvasAgg.draw(self)
        379 # Acquire a lock on the shared font cache.
        380 with (self.toolbar._wait_cursor_for_draw_cm() if self.toolbar
        381       else nullcontext()):
    --> 382     self.figure.draw(self.renderer)
        383     # A GUI class may be need to update a window using this draw, so
        384     # don't forget to call the superclass.
        385     super().draw()

    File /usr/lib/python3/dist-packages/matplotlib/artist.py:94, in _finalize_rasterization.<locals>.draw_wrapper(artist, renderer, *args, **kwargs)
         92 @wraps(draw)
         93 def draw_wrapper(artist, renderer, *args, **kwargs):
    ---> 94     result = draw(artist, renderer, *args, **kwargs)
         95     if renderer._rasterizing:
         96         renderer.stop_rasterizing()

    File /usr/lib/python3/dist-packages/matplotlib/artist.py:71, in allow_rasterization.<locals>.draw_wrapper(artist, renderer)
         68     if artist.get_agg_filter() is not None:
         69         renderer.start_filter()
    ---> 71     return draw(artist, renderer)
         72 finally:
         73     if artist.get_agg_filter() is not None:

    File /usr/lib/python3/dist-packages/matplotlib/figure.py:3257, in Figure.draw(self, renderer)
       3254             # ValueError can occur when resizing a window.
       3256     self.patch.draw(renderer)
    -> 3257     mimage._draw_list_compositing_images(
       3258         renderer, self, artists, self.suppressComposite)
       3260     renderer.close_group('figure')
       3261 finally:

    File /usr/lib/python3/dist-packages/matplotlib/image.py:134, in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
        132 if not_composite or not has_images:
        133     for a in artists:
    --> 134         a.draw(renderer)
        135 else:
        136     # Composite any adjacent images together
        137     image_group = []

    File /usr/lib/python3/dist-packages/matplotlib/artist.py:71, in allow_rasterization.<locals>.draw_wrapper(artist, renderer)
         68     if artist.get_agg_filter() is not None:
         69         renderer.start_filter()
    ---> 71     return draw(artist, renderer)
         72 finally:
         73     if artist.get_agg_filter() is not None:

    File /usr/lib/python3/dist-packages/cartopy/mpl/geoaxes.py:524, in GeoAxes.draw(self, renderer, **kwargs)
        519         self.imshow(img, extent=extent, origin=origin,
        520                     transform=factory.crs, *factory_args[1:],
        521                     **factory_kwargs)
        522 self._done_img_factory = True
    --> 524 return super().draw(renderer=renderer, **kwargs)

    File /usr/lib/python3/dist-packages/matplotlib/artist.py:71, in allow_rasterization.<locals>.draw_wrapper(artist, renderer)
         68     if artist.get_agg_filter() is not None:
         69         renderer.start_filter()
    ---> 71     return draw(artist, renderer)
         72 finally:
         73     if artist.get_agg_filter() is not None:

    File /usr/lib/python3/dist-packages/matplotlib/axes/_base.py:3210, in _AxesBase.draw(self, renderer)
       3207 if artists_rasterized:
       3208     _draw_rasterized(self.get_figure(root=True), artists_rasterized, renderer)
    -> 3210 mimage._draw_list_compositing_images(
       3211     renderer, self, artists, self.get_figure(root=True).suppressComposite)
       3213 renderer.close_group('axes')
       3214 self.stale = False

    File /usr/lib/python3/dist-packages/matplotlib/image.py:134, in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
        132 if not_composite or not has_images:
        133     for a in artists:
    --> 134         a.draw(renderer)
        135 else:
        136     # Composite any adjacent images together
        137     image_group = []

    File /usr/lib/python3/dist-packages/matplotlib/artist.py:71, in allow_rasterization.<locals>.draw_wrapper(artist, renderer)
         68     if artist.get_agg_filter() is not None:
         69         renderer.start_filter()
    ---> 71     return draw(artist, renderer)
         72 finally:
         73     if artist.get_agg_filter() is not None:

    File /usr/lib/python3/dist-packages/cartopy/mpl/feature_artist.py:185, in FeatureArtist.draw(self, renderer)
        180     geoms = self._feature.geometries()
        181 else:
        182     # For efficiency on local maps with high resolution features (e.g
        183     # from Natural Earth), only create paths for geometries that are
        184     # in view.
    --> 185     geoms = self._feature.intersecting_geometries(extent)
        187 stylised_paths = {}
        188 # Make an empty placeholder style dictionary for when styler is not
        189 # used.  Freeze it so that we can use it as a dict key.  We will need
        190 # to unfreeze all style dicts with dict(frozen) before passing to mpl.

    File /usr/lib/python3/dist-packages/cartopy/feature/__init__.py:309, in NaturalEarthFeature.intersecting_geometries(self, extent)
        302 """
        303 Returns an iterator of shapely geometries that intersect with
        304 the given extent.
        305 The extent is assumed to be in the CRS of the feature.
        306 If extent is None, the method returns all geometries for this dataset.
        307 """
        308 self.scaler.scale_from_extent(extent)
    --> 309 return super().intersecting_geometries(extent)

    File /usr/lib/python3/dist-packages/cartopy/feature/__init__.py:112, in Feature.intersecting_geometries(self, extent)
        109 if extent is not None and not np.isnan(extent[0]):
        110     extent_geom = sgeom.box(extent[0], extent[2],
        111                             extent[1], extent[3])
    --> 112     return (geom for geom in self.geometries() if
        113             geom is not None and extent_geom.intersects(geom))
        114 else:
        115     return self.geometries()

    File /usr/lib/python3/dist-packages/cartopy/feature/__init__.py:291, in NaturalEarthFeature.geometries(self)
        289 key = (self.name, self.category, self.scale)
        290 if key not in _NATURAL_EARTH_GEOM_CACHE:
    --> 291     path = shapereader.natural_earth(resolution=self.scale,
        292                                      category=self.category,
        293                                      name=self.name)
        294     geometries = tuple(shapereader.Reader(path).geometries())
        295     _NATURAL_EARTH_GEOM_CACHE[key] = geometries

    File /usr/lib/python3/dist-packages/cartopy/io/shapereader.py:306, in natural_earth(resolution, category, name)
        302 ne_downloader = Downloader.from_config(('shapefiles', 'natural_earth',
        303                                         resolution, category, name))
        304 format_dict = {'config': config, 'category': category,
        305                'name': name, 'resolution': resolution}
    --> 306 return ne_downloader.path(format_dict)

    File /usr/lib/python3/dist-packages/cartopy/io/__init__.py:203, in Downloader.path(self, format_dict)
        200     result_path = target_path
        201 else:
        202     # we need to download the file
    --> 203     result_path = self.acquire_resource(target_path, format_dict)
        205 return result_path

    File /usr/lib/python3/dist-packages/cartopy/io/shapereader.py:359, in NEShpDownloader.acquire_resource(self, target_path, format_dict)
        355 target_dir.mkdir(parents=True, exist_ok=True)
        357 url = self.url(format_dict)
    --> 359 shapefile_online = self._urlopen(url)
        361 zfh = ZipFile(io.BytesIO(shapefile_online.read()), 'r')
        363 for member_path in self.zip_file_contents(format_dict):

    File /usr/lib/python3/dist-packages/cartopy/io/__init__.py:242, in Downloader._urlopen(self, url)
        235 """
        236 Returns a file handle to the given HTTP resource URL.
        237
        238 Caller should close the file handle when finished with it.
        239
        240 """
        241 warnings.warn(f'Downloading: {url}', DownloadWarning)
    --> 242 return urlopen(url)

    File /usr/lib/python3.13/urllib/request.py:189, in urlopen(url, data, timeout, context)
        187 else:
        188     opener = _opener
    --> 189 return opener.open(url, data, timeout)

    File /usr/lib/python3.13/urllib/request.py:489, in OpenerDirector.open(self, fullurl, data, timeout)
        486     req = meth(req)
        488 sys.audit('urllib.Request', req.full_url, req.data, req.headers, req.get_method())
    --> 489 response = self._open(req, data)
        491 # post-process response
        492 meth_name = protocol+"_response"

    File /usr/lib/python3.13/urllib/request.py:506, in OpenerDirector._open(self, req, data)
        503     return result
        505 protocol = req.type
    --> 506 result = self._call_chain(self.handle_open, protocol, protocol +
        507                           '_open', req)
        508 if result:
        509     return result

    File /usr/lib/python3.13/urllib/request.py:466, in OpenerDirector._call_chain(self, chain, kind, meth_name, *args)
        464 for handler in handlers:
        465     func = getattr(handler, meth_name)
    --> 466     result = func(*args)
        467     if result is not None:
        468         return result

    File /usr/lib/python3.13/urllib/request.py:1367, in HTTPSHandler.https_open(self, req)
       1366 def https_open(self, req):
    -> 1367     return self.do_open(http.client.HTTPSConnection, req,
       1368                         context=self._context)

    File /usr/lib/python3.13/urllib/request.py:1322, in AbstractHTTPHandler.do_open(self, http_class, req, **http_conn_args)
       1319         h.request(req.get_method(), req.selector, req.data, headers,
       1320                   encode_chunked=req.has_header('Transfer-encoding'))
       1321     except OSError as err: # timeout error
    -> 1322         raise URLError(err)
       1323     r = h.getresponse()
       1324 except:

    URLError: <urlopen error [Errno -2] Name or service not known>

    /usr/lib/python3/dist-packages/cartopy/io/__init__.py:241: DownloadWarning: Downloading: https://naturalearth.s3.amazonaws.com/10m_physical/ne_10m_coastline.zip
      warnings.warn(f'Downloading: {url}', DownloadWarning)

    ---------------------------------------------------------------------------
    gaierror                                  Traceback (most recent call last)
    File /usr/lib/python3.13/urllib/request.py:1319, in AbstractHTTPHandler.do_open(self, http_class, req, **http_conn_args)
       1318 try:
    -> 1319     h.request(req.get_method(), req.selector, req.data, headers,
       1320               encode_chunked=req.has_header('Transfer-encoding'))
       1321 except OSError as err: # timeout error

    File /usr/lib/python3.13/http/client.py:1338, in HTTPConnection.request(self, method, url, body, headers, encode_chunked)
       1337 """Send a complete request to the server."""
    -> 1338 self._send_request(method, url, body, headers, encode_chunked)

    File /usr/lib/python3.13/http/client.py:1384, in HTTPConnection._send_request(self, method, url, body, headers, encode_chunked)
       1383     body = _encode(body, 'body')
    -> 1384 self.endheaders(body, encode_chunked=encode_chunked)

    File /usr/lib/python3.13/http/client.py:1333, in HTTPConnection.endheaders(self, message_body, encode_chunked)
       1332     raise CannotSendHeader()
    -> 1333 self._send_output(message_body, encode_chunked=encode_chunked)

    File /usr/lib/python3.13/http/client.py:1093, in HTTPConnection._send_output(self, message_body, encode_chunked)
       1092 del self._buffer[:]
    -> 1093 self.send(msg)
       1095 if message_body is not None:
       1096
       1097     # create a consistent interface to message_body

    File /usr/lib/python3.13/http/client.py:1037, in HTTPConnection.send(self, data)
       1036 if self.auto_open:
    -> 1037     self.connect()
       1038 else:

    File /usr/lib/python3.13/http/client.py:1472, in HTTPSConnection.connect(self)
       1470 "Connect to a host on a given (SSL) port."
    -> 1472 super().connect()
       1474 if self._tunnel_host:

    File /usr/lib/python3.13/http/client.py:1003, in HTTPConnection.connect(self)
       1002 sys.audit("http.client.connect", self, self.host, self.port)
    -> 1003 self.sock = self._create_connection(
       1004     (self.host,self.port), self.timeout, self.source_address)
       1005 # Might fail in OSs that don't implement TCP_NODELAY

    File /usr/lib/python3.13/socket.py:840, in create_connection(address, timeout, source_address, all_errors)
        839 exceptions = []
    --> 840 for res in getaddrinfo(host, port, 0, SOCK_STREAM):
        841     af, socktype, proto, canonname, sa = res

    File /usr/lib/python3.13/socket.py:977, in getaddrinfo(host, port, family, type, proto, flags)
        976 addrlist = []
    --> 977 for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
        978     af, socktype, proto, canonname, sa = res

    gaierror: [Errno -2] Name or service not known

    During handling of the above exception, another exception occurred:

    URLError                                  Traceback (most recent call last)
    File /usr/lib/python3/dist-packages/IPython/core/formatters.py:402, in BaseFormatter.__call__(self, obj)
        400     pass
        401 else:
    --> 402     return printer(obj)
        403 # Finally look for special method names
        404 method = get_real_method(obj, self.print_method)

    File /usr/lib/python3/dist-packages/IPython/core/pylabtools.py:170, in print_figure(fig, fmt, bbox_inches, base64, **kwargs)
        167     from matplotlib.backend_bases import FigureCanvasBase
        168     FigureCanvasBase(fig)
    --> 170 fig.canvas.print_figure(bytes_io, **kw)
        171 data = bytes_io.getvalue()
        172 if fmt == 'svg':

    File /usr/lib/python3/dist-packages/matplotlib/backend_bases.py:2155, in FigureCanvasBase.print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, pad_inches, bbox_extra_artists, backend, **kwargs)
       2152     # we do this instead of `self.figure.draw_without_rendering`
       2153     # so that we can inject the orientation
       2154     with getattr(renderer, "_draw_disabled", nullcontext)():
    -> 2155         self.figure.draw(renderer)
       2156 if bbox_inches:
       2157     if bbox_inches == "tight":

    File /usr/lib/python3/dist-packages/matplotlib/artist.py:94, in _finalize_rasterization.<locals>.draw_wrapper(artist, renderer, *args, **kwargs)
         92 @wraps(draw)
         93 def draw_wrapper(artist, renderer, *args, **kwargs):
    ---> 94     result = draw(artist, renderer, *args, **kwargs)
         95     if renderer._rasterizing:
         96         renderer.stop_rasterizing()

    File /usr/lib/python3/dist-packages/matplotlib/artist.py:71, in allow_rasterization.<locals>.draw_wrapper(artist, renderer)
         68     if artist.get_agg_filter() is not None:
         69         renderer.start_filter()
    ---> 71     return draw(artist, renderer)
         72 finally:
         73     if artist.get_agg_filter() is not None:

    File /usr/lib/python3/dist-packages/matplotlib/figure.py:3257, in Figure.draw(self, renderer)
       3254             # ValueError can occur when resizing a window.
       3256     self.patch.draw(renderer)
    -> 3257     mimage._draw_list_compositing_images(
       3258         renderer, self, artists, self.suppressComposite)
       3260     renderer.close_group('figure')
       3261 finally:

    File /usr/lib/python3/dist-packages/matplotlib/image.py:134, in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
        132 if not_composite or not has_images:
        133     for a in artists:
    --> 134         a.draw(renderer)
        135 else:
        136     # Composite any adjacent images together
        137     image_group = []

    File /usr/lib/python3/dist-packages/matplotlib/artist.py:71, in allow_rasterization.<locals>.draw_wrapper(artist, renderer)
         68     if artist.get_agg_filter() is not None:
         69         renderer.start_filter()
    ---> 71     return draw(artist, renderer)
         72 finally:
         73     if artist.get_agg_filter() is not None:

    File /usr/lib/python3/dist-packages/cartopy/mpl/geoaxes.py:524, in GeoAxes.draw(self, renderer, **kwargs)
        519         self.imshow(img, extent=extent, origin=origin,
        520                     transform=factory.crs, *factory_args[1:],
        521                     **factory_kwargs)
        522 self._done_img_factory = True
    --> 524 return super().draw(renderer=renderer, **kwargs)

    File /usr/lib/python3/dist-packages/matplotlib/artist.py:71, in allow_rasterization.<locals>.draw_wrapper(artist, renderer)
         68     if artist.get_agg_filter() is not None:
         69         renderer.start_filter()
    ---> 71     return draw(artist, renderer)
         72 finally:
         73     if artist.get_agg_filter() is not None:

    File /usr/lib/python3/dist-packages/matplotlib/axes/_base.py:3210, in _AxesBase.draw(self, renderer)
       3207 if artists_rasterized:
       3208     _draw_rasterized(self.get_figure(root=True), artists_rasterized, renderer)
    -> 3210 mimage._draw_list_compositing_images(
       3211     renderer, self, artists, self.get_figure(root=True).suppressComposite)
       3213 renderer.close_group('axes')
       3214 self.stale = False

    File /usr/lib/python3/dist-packages/matplotlib/image.py:134, in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
        132 if not_composite or not has_images:
        133     for a in artists:
    --> 134         a.draw(renderer)
        135 else:
        136     # Composite any adjacent images together
        137     image_group = []

    File /usr/lib/python3/dist-packages/matplotlib/artist.py:71, in allow_rasterization.<locals>.draw_wrapper(artist, renderer)
         68     if artist.get_agg_filter() is not None:
         69         renderer.start_filter()
    ---> 71     return draw(artist, renderer)
         72 finally:
         73     if artist.get_agg_filter() is not None:

    File /usr/lib/python3/dist-packages/cartopy/mpl/feature_artist.py:185, in FeatureArtist.draw(self, renderer)
        180     geoms = self._feature.geometries()
        181 else:
        182     # For efficiency on local maps with high resolution features (e.g
        183     # from Natural Earth), only create paths for geometries that are
        184     # in view.
    --> 185     geoms = self._feature.intersecting_geometries(extent)
        187 stylised_paths = {}
        188 # Make an empty placeholder style dictionary for when styler is not
        189 # used.  Freeze it so that we can use it as a dict key.  We will need
        190 # to unfreeze all style dicts with dict(frozen) before passing to mpl.

    File /usr/lib/python3/dist-packages/cartopy/feature/__init__.py:309, in NaturalEarthFeature.intersecting_geometries(self, extent)
        302 """
        303 Returns an iterator of shapely geometries that intersect with
        304 the given extent.
        305 The extent is assumed to be in the CRS of the feature.
        306 If extent is None, the method returns all geometries for this dataset.
        307 """
        308 self.scaler.scale_from_extent(extent)
    --> 309 return super().intersecting_geometries(extent)

    File /usr/lib/python3/dist-packages/cartopy/feature/__init__.py:112, in Feature.intersecting_geometries(self, extent)
        109 if extent is not None and not np.isnan(extent[0]):
        110     extent_geom = sgeom.box(extent[0], extent[2],
        111                             extent[1], extent[3])
    --> 112     return (geom for geom in self.geometries() if
        113             geom is not None and extent_geom.intersects(geom))
        114 else:
        115     return self.geometries()

    File /usr/lib/python3/dist-packages/cartopy/feature/__init__.py:291, in NaturalEarthFeature.geometries(self)
        289 key = (self.name, self.category, self.scale)
        290 if key not in _NATURAL_EARTH_GEOM_CACHE:
    --> 291     path = shapereader.natural_earth(resolution=self.scale,
        292                                      category=self.category,
        293                                      name=self.name)
        294     geometries = tuple(shapereader.Reader(path).geometries())
        295     _NATURAL_EARTH_GEOM_CACHE[key] = geometries

    File /usr/lib/python3/dist-packages/cartopy/io/shapereader.py:306, in natural_earth(resolution, category, name)
        302 ne_downloader = Downloader.from_config(('shapefiles', 'natural_earth',
        303                                         resolution, category, name))
        304 format_dict = {'config': config, 'category': category,
        305                'name': name, 'resolution': resolution}
    --> 306 return ne_downloader.path(format_dict)

    File /usr/lib/python3/dist-packages/cartopy/io/__init__.py:203, in Downloader.path(self, format_dict)
        200     result_path = target_path
        201 else:
        202     # we need to download the file
    --> 203     result_path = self.acquire_resource(target_path, format_dict)
        205 return result_path

    File /usr/lib/python3/dist-packages/cartopy/io/shapereader.py:359, in NEShpDownloader.acquire_resource(self, target_path, format_dict)
        355 target_dir.mkdir(parents=True, exist_ok=True)
        357 url = self.url(format_dict)
    --> 359 shapefile_online = self._urlopen(url)
        361 zfh = ZipFile(io.BytesIO(shapefile_online.read()), 'r')
        363 for member_path in self.zip_file_contents(format_dict):

    File /usr/lib/python3/dist-packages/cartopy/io/__init__.py:242, in Downloader._urlopen(self, url)
        235 """
        236 Returns a file handle to the given HTTP resource URL.
        237
        238 Caller should close the file handle when finished with it.
        239
        240 """
        241 warnings.warn(f'Downloading: {url}', DownloadWarning)
    --> 242 return urlopen(url)

    File /usr/lib/python3.13/urllib/request.py:189, in urlopen(url, data, timeout, context)
        187 else:
        188     opener = _opener
    --> 189 return opener.open(url, data, timeout)

    File /usr/lib/python3.13/urllib/request.py:489, in OpenerDirector.open(self, fullurl, data, timeout)
        486     req = meth(req)
        488 sys.audit('urllib.Request', req.full_url, req.data, req.headers, req.get_method())
    --> 489 response = self._open(req, data)
        491 # post-process response
        492 meth_name = protocol+"_response"

    File /usr/lib/python3.13/urllib/request.py:506, in OpenerDirector._open(self, req, data)
        503     return result
        505 protocol = req.type
    --> 506 result = self._call_chain(self.handle_open, protocol, protocol +
        507                           '_open', req)
        508 if result:
        509     return result

    File /usr/lib/python3.13/urllib/request.py:466, in OpenerDirector._call_chain(self, chain, kind, meth_name, *args)
        464 for handler in handlers:
        465     func = getattr(handler, meth_name)
    --> 466     result = func(*args)
        467     if result is not None:
        468         return result

    File /usr/lib/python3.13/urllib/request.py:1367, in HTTPSHandler.https_open(self, req)
       1366 def https_open(self, req):
    -> 1367     return self.do_open(http.client.HTTPSConnection, req,
       1368                         context=self._context)

    File /usr/lib/python3.13/urllib/request.py:1322, in AbstractHTTPHandler.do_open(self, http_class, req, **http_conn_args)
       1319         h.request(req.get_method(), req.selector, req.data, headers,
       1320                   encode_chunked=req.has_header('Transfer-encoding'))
       1321     except OSError as err: # timeout error
    -> 1322         raise URLError(err)
       1323     r = h.getresponse()
       1324 except:

    URLError: <urlopen error [Errno -2] Name or service not known>

    <Figure size 1000x1000 with 1 Axes>

Finally, we can also pull out a time series for a given location easily:

    [5]:

    ds.t2m.sel(longitude=0, latitude=51.5).plot()
    plt.title("ERA5 - London 2m temperature March 2019")

    ---------------------------------------------------------------------------
    NameError                                 Traceback (most recent call last)
    Cell In[5], line 1
    ----> 1 ds.t2m.sel(longitude=0, latitude=51.5).plot()
          2 plt.title("ERA5 - London 2m temperature March 2019")

    NameError: name 'ds' is not defined