Search code examples
pythonpython-3.xgoogle-colaboratorycartopymetpy

Cartopy (0.14.2) / MetPy (0.12.2) parse(cf) error when using Google Colab


I have a student facing a problem that is similar to what others are reporting when using older versions of MetPy and Cartopy.

The issue is the parse_cf() command under metpy. And it's happening under Google Colaboratory, of which I have little experience. Things are working fine on other platforms (anaconda, miniconda, homebrew, etc)

Previous feedback I've seen refers to rolling back the version of the cartopy library from 0.18.*. I have also followed previously posted guidance on Shapely, but we cannot seem to be able to change the versions of cartopy or metpy version in Google CoLab.

The version information for the environment and python libraries, followed by the code and error is below.

I'm at a loss here and have troubleshot it for my student about as far as I can go. Guidance would be appreciated by both of us.

System and Package Information

MetPy                         0.12.2         
Cartopy                       0.14.2         
3.6.9 (default, Oct  8 2020, 12:12:24) 
[GCC 8.4.0]
Linux d8a71c5140a6 4.19.112+ #1 SMP Thu Jul 23 08:00:38 PDT 2020 x86_64 x86_64 x86_64 GNU/Linux
Linux-4.19.112+-x86_64-with-Ubuntu-18.04-bionic

python-cartopy/bionic,now 0.14.2+dfsg1-2build3 amd64 [installed]
python3-cartopy/bionic,now 0.14.2+dfsg1-2build3 amd64 [installed]

Code to Replicate

import numpy as np
import cartopy as c
import metpy as metpy

file_218 = "https://www.ncei.noaa.gov/thredds/dodsC/model-nam218/202011/20201121/nam_218_20201121_0000_000.grb2"

ds218    = xr.open_dataset(file_218)
ds218    = ds218.metpy.parse_cf()

Error Message

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-9-4362f5b26659> in <module>()
      9 file_218 = "https://www.ncei.noaa.gov/thredds/dodsC/model-nam218/202011/20201121/nam_218_20201121_0000_000.grb2"
     10 ds218    = xr.open_dataset(file_218)
---> 11 ds218    = ds218.metpy.parse_cf()

1 frames
/usr/local/lib/python3.6/dist-packages/metpy/xarray.py in parse_cf(self, varname, coordinates)
    510         """
    511         from .cbook import iterable
--> 512         from .plots.mapping import CFProjection
    513 
    514         if varname is None:

/usr/local/lib/python3.6/dist-packages/metpy/plots/__init__.py in <module>()
     17 logger = logging.getLogger(__name__)
     18 
---> 19 __all__ = ctables.__all__[:]  # pylint: disable=undefined-variable
     20 __all__.extend(declarative.__all__)  # pylint: disable=undefined-variable
     21 __all__.extend(skewt.__all__)  # pylint: disable=undefined-variable

NameError: name 'ctables' is not defined

Solution

  • The core problem here is that 0.14.2 is too old work with MetPy--though the fact that it crashes is a bug that will be fixed in the impending 1.0 release. To get a newer version of CartoPy installed on Collab, this recipe has worked for me (type these commands in cells):

    !pip install metpy
    !apt-get -qq install libproj-dev libgeos-dev
    !pip install shapely --no-binary shapely cartopy