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 ...
12
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))
11
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....
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....
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-...
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
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=...
8
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": {'...
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", ...
7
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 ...
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. ...
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 ...
6
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 = [
{
...
6
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 ...
5
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={...
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
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.
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 ...
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....
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 ...
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,...
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 ...
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 ...
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 ...
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? ...
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&...
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 ...
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(...
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: ...
2
votes
Accepted
How to visualize a NetCDF Dataset
Since a Dataset can contain more than one variable, it would not be trivial for software to know what you want it to display. If you select one of its variables as DataArray however, that can easily ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
xarray × 136python × 83
rioxarray × 58
netcdf × 53
rasterio × 27
raster × 14
geotiff-tiff × 12
open-data-cube × 8
gdal × 7
numpy × 7
dask × 7
shapefile × 6
python-3 × 6
qgis × 5
coordinate-system × 5
google-earth-engine × 5
grib × 5
noaa × 5
geopandas × 4
cartopy × 4
convert × 3
clip × 3
merge × 3
image × 3
resampling × 3