1

When trying to mask a NetCDF (a daily time series of 31 days) with the code below, I get the following error related to the part of writing the CRS to the rio NetCDF data:

CRSError: The WKT could not be parsed. OGR Error code 6

Here is the code I am using (taken from snowman2's answer here: How to mask NetCDF time series data from a shapefile in Python?):

import rioxarray
import xarray
from shapely.geometry import mapping

ERA5_daily = xarray.open_dataarray('ERA5_VIMD_2018_08_Daily.nc')
ERA5_daily.rio.set_spatial_dims(x_dim="longitude", y_dim="latitude", inplace=True)
ERA5_daily.rio.write_crs("epsg:4326", inplace=True)
shapefile = geopandas.read_file('Cluster_34586.shp', crs="epsg:4326")

clipped = ERA5_daily.rio.clip(shapefile.geometry.apply(mapping), shapefile.crs, drop=False)

Does anyone know how to fix it?

8
  • What is the output of python -c "import rioxarray; rioxarray.show_versions()"? What is the CRS?
    – snowman2
    Sep 9, 2020 at 20:19
  • Hi, @snowman2! This is what I get: Traceback (most recent call last): File "<ipython-input-65-54d7e150b1f6>", line 1, in <module> rioxarray.show_versions() AttributeError: module 'rioxarray' has no attribute 'show_versions'
    – Camila
    Sep 9, 2020 at 21:16
  • @snowman2: It looks like there is no CRS (couldn't find it either when opening the netcdf file in python with netcdf library, rioxarray or when opening with QGIS)
    – Camila
    Sep 9, 2020 at 21:23
  • I think that you have an old version of rioxarray. Mind updating and trying again?
    – snowman2
    Sep 9, 2020 at 22:02
  • Are you able to share the file? Or the output of ncdump -h file.nc?
    – snowman2
    Sep 9, 2020 at 22:05

1 Answer 1

1

Using this code:

import geopandas
import rioxarray
import xarray
from shapely.geometry import mapping

ERA5_daily = xarray.open_dataarray('ERA5_VIMD_2018_08.nc')
ERA5_daily.rio.write_crs("EPSG:4326", inplace=True)
shapefile = geopandas.read_file('Cluster_34586.shp', crs="EPSG:4326")
clipped = ERA5_daily.rio.clip(shapefile.geometry.apply(mapping), shapefile.crs, drop=False)

It works with this setup (from: rioxarray.show_versions()):

rioxarray (0.0.31) deps:
  rasterio: 1.1.5
    xarray: 0.16.0
      GDAL: 3.0.4

Other python deps:
     scipy: 1.5.2
    pyproj: 2.6.1.post1

System:
    python: 3.7.8 | packaged by conda-forge | (default, Jul 31 2020, 02:25:08)  [GCC 7.5.0]

I would recommend upgrading the versions you have on your system.

2
  • I've uninstalled rioxarray and installed again using pip install and it is now with the same version as yours. The code is now working just fine! Thank you very much!
    – Camila
    Sep 10, 2020 at 16:12
  • Glad to hear that it worked 👍
    – snowman2
    Sep 10, 2020 at 23:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.