TypeError:尝试将netcdf文件转换为多波段栅格时,在方法'Dataset_GetRasterBand'中,参数'int'的类型2为

时间:2019-12-18 10:30:12

标签: python gdal netcdf python-xarray geotiff

尝试将我的netcdf文件转换为多波段栅格(需要通过波段索引进行一些区域统计),但始终出现以下错误:

TypeError: in method 'Dataset_GetRasterBand', argument 2 of type 'int'

这是我的代码:

import xarray 
import gdal
from osgeo import osr

#Open and subset netcdf file 
netcdf_file=xarray.open_mfdataset("/MSLA/*.nc",concat_dim='time')
subset=netcdf_file.loc[dict(lat=slice(-30, 50), lon=slice(20, 110), n=1, time=slice('2010-01-01', '2015-12-31'))]

#Convert netcdf to a multiband raster 
sla=subset.variables["sla"][:,:,:]
lat=subset.variables["lat"][:]
lon=subset.variables["lon"][:]
datetimeindex = subset.indexes['time'].to_datetimeindex()
subset['time']=datetimeindex.strftime('%Y%m')
subset['time']=subset['time'].astype(int)
time=subset['time'].astype(int)
xmin, ymin, xmax, ymax=[lon.min(), lat.min(), lon.max(), lat.max()]
nx=subset.dims['lon']
ny=subset.dims['lat']
xres=(xmax-xmin)/float(nx)
yres=(ymax-ymin)/float(ny)
geotransform=(xmin, xres, 0, ymax, 0, -yres)
raster = gdal.GetDriverByName('GTiff').Create('multiband_test.tif', nx, ny, 72, gdal.GDT_Float32)
raster.SetGeoTransform(geotransform)
srs = osr.SpatialReference()   
srs.ImportFromEPSG(4326)  
raster.SetProjection(srs.ExportToWkt())
for i in time:
    raster.GetRasterBand(i).WriteArray(sla[:,:,i])
raster.FlushCache()

“时间”为int64

'sla'是一个xarray.core.variable

任何建议将不胜感激!

0 个答案:

没有答案