Skip to main content
12 votes
Accepted

xarray slicing across the antimeridian

Xarray is (intentionally) ignorant of coordinate systems, so it has no special handling for cyclic coordinates such as longitude. Because your longitude array has only increasing values, xarray ...
shoyer's user avatar
  • 238
12 votes

Mask xarray dataset using a shapefile

Masking NetCDF time series data from shapefile using Python You can use rioxarray. Here is an example: https://corteva.github.io/rioxarray/stable/examples/clip_geom.html import rioxarray import ...
snowman2's user avatar
  • 7,419
11 votes
Accepted

How to clip an xarray to a smaller extent given the lat/lon coordinates?

min_lon = -24.995 min_lat = 25.05 max_lon = 45.50 max_lat = 71.55 cropped_ds = ds.sel(lat=slice(min_lat,max_lat), lon=slice(min_lon,max_lon))
Paula's user avatar
  • 126
10 votes
Accepted

Mask xarray dataset using a shapefile

You can to use the rasterio.features.geometry_mask function to do this: ShapeMask = rasterio.features.geometry_mask(sf.iloc[0], out_shape=(len(ndvi.y), len(ndvi....
Claire K's user avatar
  • 116
10 votes

How to clip an xarray to a smaller extent given the lat/lon coordinates?

You could use rioxarray: https://corteva.github.io/rioxarray/stable/examples/clip_box.html import rioxarray min_lon = -24.995 min_lat = 25.05 max_lon = 45.50 max_lat = 71.55 subset = band.rio....
snowman2's user avatar
  • 7,419
9 votes
Accepted

Using Rioxarray / QGIS Projection

Upon inspecting the dataset, I realized that the units of the data are in radians. import xarray import rioxarray from pyproj import CRS xds = xarray.open_dataset("OR_ABI-L2-LSTF-...
snowman2's user avatar
  • 7,419
9 votes
Accepted

How to save an xarray.DataArray directly as a Cloud Optimized GeoTIFF?

rds.rio.to_raster(raster_path="cogeo.tif", driver="COG") https://gdal.org/drivers/raster/cog.html
snowman2's user avatar
  • 7,419
8 votes

Indexing coordinates in order to get a value by coordinates from xarray

You can use xarray.where. See: http://xarray.pydata.org/en/stable/indexing.html xds = xarray.open_dataset("TT_199501_daymean.nc") xds.where((xds.lon==5.8252) & (xds.lat==46.9359), drop=...
snowman2's user avatar
  • 7,419
8 votes
Accepted

Converting a NetCDF from 0 to 360 to -180 to 180 via xarray

This method worked: with xr.open_dataset(nc_fp, mask_and_scale=True, decode_times=True) as src: ds = src.sel(time="2020-01-01").max('time') ds.rio.write_crs("epsg:4326", ...
JWB's user avatar
  • 604
7 votes
Accepted

Opening EOS netCDF4/HDF5 file with correct format using xarray?

To open the data with the projection information you need to open the sub-datasets individually. I will use a MODIS dataset I have to hand as an example, MOD11A1, but it will be the same for yours. ...
Colabean's user avatar
  • 416
7 votes
Accepted

Using xarray to resample and merge two datasets

This documentation from xarray outlines quite simply the solution to the problem. xarray allows you to interpolate in multiple dimensions and specify another Dataset's x and y dimensions as the output ...
clifgray's user avatar
  • 409
6 votes
Accepted

Adding compression to a NetCDF file using xarray

The error hints at xarray trying to find a variable called "zlib" in your data. The correct structure for the encoding dict would be something like: encode = {"precipitation": {'...
bugmenot123's user avatar
  • 10.9k
6 votes

How to clip an xarray to a smaller extent given the lat/lon coordinates?

One way is to create a boolean mask for the dataset coordinates using the extent you specified and then using the .where() method on the dataset. Here is one example using a tutorial dataset that ...
Marcelo Villa's user avatar
5 votes
Accepted

Extracting data within geometry (shape)

I believe what you are looking for is rioxarray. An example of what you want to do is at: https://corteva.github.io/rioxarray/stable/examples/clip_geom.html import rioxarray geometries = [ { ...
snowman2's user avatar
  • 7,419
5 votes
Accepted

Error trying to open NetCDF file with xarray from s3 bucket

The problem is that the your netcdf engine cannot read this file as it is invalid for this engine. To solve that you need to install h5netcdf engine using pip or conda. Also, files from s3 cannot be ...
Aman Bagrecha's user avatar
4 votes
Accepted

How to save a clipped NetCDF data as a new NetCDF file in Python?

xarray.Dataset has a to_netcdf method...so it should be clipped.to_netcdf(path_on_disk) http://xarray.pydata.org/en/stable/generated/xarray.Dataset.to_netcdf.html
bcollins's user avatar
  • 3,563
4 votes

Unable to read GRIB datas with XArray

If you add the backend_kwargs line, it will work properly, for example:- ds = xr.open_dataset('gfs_4_20191010_0000_006.grb2', engine='cfgrib', backend_kwargs={...
Krishnaap's user avatar
  • 141
4 votes

GDAL crashes with segmentation fault when trying to open a NetCDF subdataset with time dimension

The problem turned out to be a bug in GDAL, which is now fixed: https://trac.osgeo.org/gdal/ticket/6870.
ajt's user avatar
  • 91
4 votes

Mask xarray dataset using a shapefile

You can try using Digital Earth Australia's xr_rasterize function to convert your geopandas geodataframe into an xarray object, and then use xarray's .where() method to mask you're array. e.g. mask ...
cbur's user avatar
  • 61
4 votes
Accepted

Resample (spatially-average) DataArray / raster to match the grid of another DataArray in Python

Solved! rioxarray has the method 'reproject_match', which does exactly what I describe above. For more info: https://corteva.github.io/rioxarray/stable/rioxarray.html?highlight=write_crs#rioxarray....
marcgm's user avatar
  • 95
4 votes
Accepted

Mask time series xarray dataset based on the other overlapping dataset?

The coordinates have to match exactly for where to work. Since your x and y values are slightly different no matches can be found. To fix this you can use interp() to interpolate one of the rasters to ...
JonasV's user avatar
  • 3,774
4 votes
Accepted

How to deal with Nan value when using rioxarray rio.reproject()?

You haven't actually set a NoData value. Explicitly set nan as the NoData value. import xarray as xr import rioxarray as rxr import numpy as np from rasterio.enums import Resampling data = [[1, 2, 3,...
user2856's user avatar
  • 66.5k
4 votes

Is it possible to change a coordinate in an netCDF loaded with xarray?

You created a new coordinate but it also created a new dimension instead of using the "band" dimension. @Bert Coever's suggestion should work. Also note that every operation creates a new ...
ZZZ's user avatar
  • 530
3 votes

NetCDF hourly data to year raster layers (using Python)

I would use xarray.Dataset.resample. xarray is very handy for time series and comes with several temporal dimension handling methods, some of which taken from the comprehensive pandas. Check the docs ...
Andrea Massetti's user avatar
3 votes
Accepted

Using rioxarray to assign spatial reference (EPSG:4326) to NetCDF built from CSV with lat long columns

Thanks to the answer of @snowman2, I was able to understand that the problem was that my coordinates were not properly sorted. Well, at least it seems to have solved the issue for Panoply and the ...
umbe1987's user avatar
  • 3,785
3 votes
Accepted

Difference between `reproject_match` and `interp_like`

xarray uses scipy to do the interpolation with the default linear method (http://xarray.pydata.org/en/stable/generated/xarray.Dataset.interp_like.html#xarray.Dataset.interp_like). Why use interp_like? ...
snowman2's user avatar
  • 7,419
3 votes
Accepted

Problem in setting dimension using rioxarray set_spatial_dims()

Your dims and coords need to have the same name. This should work: da0 = xr.DataArray( data = data, dims = ["x","y"], coords = dict( x = (["x&...
snowman2's user avatar
  • 7,419
3 votes
Accepted

Create one NetCDF file from multiple NetCDF files using Python

The trick is to first add the new (4th) dimension to each individual dataset, then you can easily merge them. Here I do this for just 2 datasets, but I think you can easily expand it to 9. import ...
Bert Coerver's user avatar
  • 1,945
2 votes

How to change Numpy array shape

It's impossible to do this kind of reshape [(100, 256, 256) -> (100, 256, 256,3)]. It's only possible a compatible reshape. This is an example: >>>import numpy as np >>>list = range(...
xunilk's user avatar
  • 30k
2 votes
Accepted

Clip global data by polygon using rioxarray fails (off by 180 longitude)

I think this should fix it: ds = ds.assign_coords(x=(((ds.x + 180) % 360) - 180)).sortby('x') The reason is that rioxarray uses the 1-d coordinate and lon is 2-d. <xarray.Dataset> Dimensions: ...
snowman2's user avatar
  • 7,419

Only top scored, non community-wiki answers of a minimum length are eligible