Python 如何将数组转换为光栅数据集?

Python 如何将数组转换为光栅数据集?,python,netcdf,python-xarray,masking,rasterio,Python,Netcdf,Python Xarray,Masking,Rasterio,我找不到一种简单的方法将numpy数组中的数据转换成可以由rasterio处理的格式。事实上,这甚至不是一条艰难的道路。 更一般地说,我想屏蔽netcdf文件中的海洋值,以进行计算(而不是绘图!否则cartopy.feature就可以了) 我尝试过但没有成功的是: import fiona import xarray as xr import rasterio.mask as rmask # load ocean shapefile: with fiona.open('path_to/ne_1

我找不到一种简单的方法将numpy数组中的数据转换成可以由rasterio处理的格式。事实上,这甚至不是一条艰难的道路。 更一般地说,我想屏蔽netcdf文件中的海洋值,以进行计算(而不是绘图!否则cartopy.feature就可以了) 我尝试过但没有成功的是:

import fiona
import xarray as xr
import rasterio.mask as rmask

# load ocean shapefile:
with fiona.open('path_to/ne_10m_ocean.shp', "r") as shapefile:
    shapes = [feature["geometry"] for feature in shapefile]

# load data to be maked:
rasterf = xr.open_dataset('path_to/my.nc')
raster = rasterf.tas[0]    # to select only first timestep of variable 'tas'

# this should do the masking, but my raster is not in a rasterio readable format:
raster_masked = rmask.mask(raster, shapes, all_touched=False, invert=True)