使用rioxarray剪切NetCDF文件,大小调整问题

问题描述 投票:0回答:1

我一直在尝试使用我拥有的盆地形状文件来剪辑全球降水 NetCDF 文件。这是代码:

import geopandas as gpd
import rioxarray
import xarray as xr
import matplotlib.pyplot as plt
from shapely.geometry import mapping

# Load shapefile
shapefile_path = "my_shapefile.shp"
shapefile = gpd.read_file(shapefile_path, crs="epsg:4326")

# Load NetCDF file
netcdf_path = "my_netcdffile.nc"
ds = xr.open_dataset(netcdf_path)

pre = ds["pre"] # selecting precipitation

# clipping
pre.rio.set_spatial_dims(x_dim="lon", y_dim="lat", inplace=True)
pre.rio.write_crs("epsg:4326", inplace=True)
clipped_nc = pre.rio.clip(
    shapefile.geometry.apply(mapping), pre.rio.crs, drop=False
)

clipped_nc.to_netcdf("clipped_netcdf.nc") # saving nc file

我还绘制了第一个时间步骤,这些是代码和绘图:

plt.figure(figsize=(10, 6))
clipped_nc["pre"].isel(time=0).plot() 
plt.savefig("plot.png")

如果我放大,那么它看起来像这样:

这是我使用的原始形状文件:

这是我的问题: 有谁知道如何减小生成的 Clipped_netcdf.nc 的大小,以便获得盆地所在区域的 NetCDF 文件,而不是全球降水量 NetCDF 文件的原始大小?

python geopandas python-xarray
1个回答
0
投票

我找到了自己的一个解决方案,但我愿意接受更多选择。

这是代码:


import geopandas as gpd
import rioxarray
import xarray as xr
import matplotlib.pyplot as plt
from shapely.geometry import mapping

# Load shapefile
shapefile_path = "my_shapefile.shp"
shapefile = gpd.read_file(shapefile_path, crs="epsg:4326")

# Load NetCDF file
netcdf_path = "my_netcdffile.nc"
ds = xr.open_dataset(netcdf_path)

pre = ds["pre"] # selecting precipitation

# Define the bounding box of the basin using the shapefile's bounds
min_lon, min_lat, max_lon, max_lat = shapefile.total_bounds

# Subset the original dataset to the bounding box of the basin
pre = pre.sel(
    lat=slice(max_lat, min_lat),
    lon=slice(min_lon, max_lon)
)

# clipping
pre.rio.set_spatial_dims(x_dim="lon", y_dim="lat", inplace=True)
pre.rio.write_crs("epsg:4326", inplace=True)
clipped_nc = pre.rio.clip(
    shapefile.geometry.apply(mapping), pre.rio.crs, drop=False
)

clipped_nc.to_netcdf("clipped_netcdf.nc") # saving nc file

这里是使用

ncview
生成的 NetCDF 文件:

© www.soinside.com 2019 - 2024. All rights reserved.