[gdal-dev] Grib2 band to Geo Tiff

manzione james.j.manzione at usace.army.mil
Tue Jan 18 20:26:45 EST 2011


Greetings and thank you for looking at my post,

I am converting a Grib2 file into a geo tiff file and then reprojecting it
in python.  When I reproject the tiff file it states it is "unable to
compute a transformation between pixel/line and georeferenced coordinates"
for my converted tiff file because "there is no affine transformation and no
GCPs."

I ran some tests and am under the assumption that my script is not embedding
any georefferencing information into the tiff file.  Any suggestions on how
to do this?  I am new to gdal and could use the help.  Below is my code for
the conversion.

Thank you in advance for your advice,
-James

Script:

    dataset = gdal.Open( Rpath, GA_ReadOnly )

    xsize = dataset.RasterXSize
    ysize = dataset.RasterYSize
    pixels = xsize * ysize
    
    num_bands = dataset.RasterCount
    for band_num in range(1, num_bands + 1):
        band = dataset.GetRasterBand(band_num)
        meta = band.GetMetadata()
        bandName = str(meta['GRIB_COMMENT'].split()[0])
        tiffImagePath = str(pngPath + bandName + '.tiff')

        ct = color_table(str(table))
        
        buf = dataset.ReadRaster( 0, 0, xsize, ysize, None, None, None, [
band_num ] )
        outBand = tiff.Create( tiffImagePath, xsize, ysize, 1, GDT_Byte)
        if outBand is None:
            sys.exit(1)
        
        outBand.GetRasterBand(1).SetRasterColorTable(ct)
        outbuf = []

        for offset in range(0, pixels):
            val = int(struct.unpack_from('d', buf, offset * 8)[0])
            if val > 255: val = 255
            if val < 0: val = 0
            outbuf.append('%c' % val)
        buffo = ''.join(outbuf)
        outBand.WriteRaster( 0, 0, xsize, ysize, buffo, xsize, ysize,
GDT_Byte )

-- 
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Grib2-band-to-Geo-Tiff-tp5937919p5937919.html
Sent from the GDAL - Dev mailing list archive at Nabble.com.


More information about the gdal-dev mailing list