python gdal无法在CENTOS linux上正确处理grib文件

3zwjbxry  于 2022-11-07  发布在  Python
关注(0)|答案(1)|浏览(216)

我正在尝试使用python gdal将grib2文件处理为geotiff文件,基于以下示例:
https://geoexamples.com/d3-raster-tools-docs/code_samples/vardah.html
在此基础上,我有以下代码:

import gdal
import osr
ds = gdal.Open(r"/home/test/gfs.t12z.sfluxgrbf000.grib2",1)
gph = ds.GetRasterBand(84).ReadAsArray()
press = ds.GetRasterBand(54).ReadAsArray() / 100
temp = ds.GetRasterBand(52).ReadAsArray()
u = ds.GetRasterBand(50).ReadAsArray()
v = ds.GetRasterBand(51).ReadAsArray()

corr_press = press * (1 - (0.0065*gph/(0.0065*gph + temp  + 273.15)))**-5.257

driver = gdal.GetDriverByName('GTiff')
outRaster = driver.Create("/home/test/vardah2.tiff",     ds.RasterXSize,     ds.RasterYSize, 4, gdal.GDT_Float32)
outRaster.SetGeoTransform(ds.GetGeoTransform())

outband = outRaster.GetRasterBand(1)
outband.WriteArray(corr_press)
outband.SetMetadata({'name': 'press'})

outRasterSRS = osr.SpatialReference()
outRasterSRS.ImportFromEPSG(4326)
outRaster.SetProjection(outRasterSRS.ExportToWkt())
outband.FlushCache()

我尝试在Centos 7上执行此操作,当我运行程序时,我得到以下错误:

ERROR 6: The GRIB driver does not support update access to existing datasets.

Traceback (most recent call last):
  File "ficky.py", line 4, in <module>
    gph = ds.GetRasterBand(84).ReadAsArray()
 AttributeError: 'NoneType' object has no attribute 'GetRasterBand'

如何解决此错误以在centos界面上成功运行此脚本?

oxcyiej7

oxcyiej71#

变更

ds = gdal.Open(r"/home/test/gfs.t12z.sfluxgrbf000.grib2",1)

ds = gdal.Open("/home/test/gfs.t12z.sfluxgrbf000.grib2")

甚至更好

ds = gdal.Open("/home/test/gfs.t12z.sfluxgrbf000.grib2", GA_ReadOnly)

相关问题