python gdal not processing grib file properly on CENTOS linux

142 Views Asked by At

I am trying to use python gdal to process a grib2 file into a geotiff file based on the following example:

https://geoexamples.com/d3-raster-tools-docs/code_samples/vardah.html

based on this I have the following code:

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()

I am attempting to do this on centos 7 and when I run the program I get the following error:

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'

How do I resolve this error to get a successful run of this script on a centos interface?

1

There are 1 best solutions below

0
On

Change

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

to

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

or even better

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