# Grib Metadata with GDAL in Python

I'm trying to use GDAL in python to conditionally select raster bands from a grib file based on the time when the weather feature was measured (think temperature).

If I run GetMetadata() on the dataset as a whole, I get no information.

path = r'...\ds.snow.bin'
dataset = gdal.Open(path)


{}

However, I do get information if I run GetMetadata() on a specific band:

dataset.GetRasterBand(1).GetMetadata()


{'GRIB_COMMENT': 'Total snowfall [m]', 'GRIB_DISCIPLINE':
'0(Meteorological)', 'GRIB_ELEMENT': 'SnowAmt',
'GRIB_FORECAST_SECONDS': '0 sec', 'GRIB_IDS': 'CENTER=8(US-NWSTG)
MASTER_TABLE=1 LOCAL_TABLE=0 SIGNF_REF_TIME=1(Start_of_Forecast)
REF_TIME=2019-05-29T15:00:00Z PROD_STATUS=1(Operational_test)
TYPE=1(Forecast)', 'GRIB_PDS_PDTN': '8',
'GRIB_PDS_TEMPLATE_ASSEMBLED_VALUES': '1 29 2 0 0 255 255 1 0 1 0 0
255 -1 -2147483647 2019 5 29 18 0 0 1 0 1 2 1 6 1 0',
'GRIB_PDS_TEMPLATE_NUMBERS': '1 29 2 0 0 0 255 255 1 0 0 0 0 1 0 0 0 0
0 255 129 255 255 255 255 7 227 5 29 18 0 0 1 0 0 0 0 1 2 1 0 0 0 6 1
0 0 0 0', 'GRIB_REF_TIME': ' 1559142000 sec UTC',
'GRIB_SHORT_NAME': '0-SFC', 'GRIB_UNIT': '[m]', 'GRIB_VALID_TIME': '
1559152800 sec UTC'}

Is there a way to get this metadata for the entire grib file at once so I can parse over it and select only bands that fall within a specified time?

I know cfgrib using XAarray can do this, but I would rather use GDAL.

I think the answer to your question is "no". You can't get the metadata for the entire grib file "at once".

You can however loop over all the bands in the grib-file and access the metadata like that, which also allows you to make a selection based on the metadata.

for example like this:

path = r"D:\path\to\somewhere.grib"

import gdal

ds = gdal.Open(path)

for band in range(ds.RasterCount):