# Tag Info

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 ...
• 238

### 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 ...
• 7,419
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))
• 126
Accepted

• 116

### 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....
• 7,419
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-...
• 7,419
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
• 7,419

### 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=...
• 7,419
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", ...
• 604
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. ...
• 416
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 ...
• 409
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": {'...
• 10.9k

### 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 ...
• 5,968
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 = [ { ...
• 7,419
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 ...
• 1,055
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
• 3,563

### 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={...
• 141

### 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.
• 91

### 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 ...
• 61
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....
• 95
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 ...
• 3,774
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,...
• 66.5k

### 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 ...
• 530

### 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 ...
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 ...
• 3,785
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? ...
• 7,419
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&...
• 7,419
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 ...
• 1,945

### 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(...
• 30k