[gdal-dev] Fwd: Accessing to GRIB bands

Roger Veciana i Rovira rveciana at gmail.com
Tue Jun 18 03:42:28 PDT 2013


Hi Paolo,

I think that you will need a script. At least in my case, every file has
the bands written in a different order, no using directly the band number
is not possible. The only way I have found is to read the file metadata
using a python script:

------

from osgeo import gdal
from osgeo.gdalconst import GA_ReadOnly

grib_file = "wrf-03.2013060200_32.grib"
variable = "UGRD"
level = "950-ISBL"

dataset = gdal.Open(grib_file, GA_ReadOnly )

for i in range(1,dataset.RasterCount + 1):
    band = dataset.GetRasterBand(i)
    metadata = band.GetMetadata()
    level_band = metadata['GRIB_SHORT_NAME']
    variable_band = metadata['GRIB_ELEMENT']

    if level_band == level and variable_band == variable:
        bandnum = i

print bandnum

------


Of course, you have to change the filename, variable and level, and you'll
get the band number that fits the conditions. Then, just open this band and
do whatever you need.

I hope that helps.

Roger


2013/6/18 Paolo Corti <pcorti at gmail.com>

> On Tue, Jun 18, 2013 at 12:46 AM, Even Rouault
> <even.rouault at mines-paris.org> wrote:
> >
> > From gdal_translate man page:
> >
> >        -b band:
> >            Select an input band band for output. Bands are numbered from
> 1.
> > Multiple -b switches may be used to select a set of input bands to write
> to
> > the output file, or
> >            to reorder bands. Starting with GDAL 1.8.0, band can also be
> set to
> > 'mask,1' (or just 'mask') to mean the mask band of the first band of the
> input
> > dataset.
>
> Hey Even
>
> thanks for the reply.
>
> I am well aware of the -b option, the problem here is that each raster
> has not the same bands order.
> Therefore, as I had to make some work on just one single band
> representing the same variable in all of the datasets, I ended up to
> write a script, iterate the datasets, then iterate the bands, read the
> metadata, and when found the correct band ran the process on it.
>
> thanks a lot in any case
> cheers
> p
>
> --
> Paolo Corti
> Geospatial software developer
> web: http://www.paolocorti.net
> twitter: @capooti
> skype: capooti
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20130618/7862e2da/attachment.html>


More information about the gdal-dev mailing list