1

Is there a way to see the metadata of each band of a grib file using rasterio? Using gdal this can be accomplished by:

import gdal

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

ds = gdal.Open(path)  

for band in range(1, ds.RasterCount):
    print(ds.GetRasterBand(band).GetMetadata())

an example of one band's metadata from a GFS grib using the method above is as follows

{'GRIB_COMMENT': 'Temperature [C]', 'GRIB_DISCIPLINE': '0(Meteorological)', 'GRIB_ELEMENT': 'TMP', 'GRIB_FORECAST_SECONDS': '21600 sec', 'GRIB_IDS': 'CENTER=7(US-NCEP) SUBCENTER=0 MASTER_TABLE=2 LOCAL_TABLE=1 SIGNF_REF_TIME=1(Start_of_Forecast) REF_TIME=2022-02-02T06:00:00Z PROD_STATUS=0(Operational) TYPE=1(Forecast)', 'GRIB_PDS_PDTN': '0', 'GRIB_PDS_TEMPLATE_ASSEMBLED_VALUES': '0 0 2 0 96 0 0 1 6 1 0 0 255 0 0', 'GRIB_PDS_TEMPLATE_NUMBERS': '0 0 2 0 96 0 0 0 1 0 0 0 6 1 0 0 0 0 0 255 0 0 0 0 0', 'GRIB_REF_TIME': '  1643781600 sec UTC', 'GRIB_SHORT_NAME': '0-SFC', 'GRIB_UNIT': '[C]', 'GRIB_VALID_TIME': '  1643803200 sec UTC'}

I'm trying to get this same information but using rasterio instead. Is this possible? So far I've tried

import rasterio 

with rasterio.open(file) as src:
    print(src.descriptions)
    print(src.meta)
    print(src.profile)

with the closest being src.descriptions. However, it only gave the following

('0[-] SFC="Ground or water surface"')

It seems to be only getting a piece of the metadata. Does anyone know how to get the rest?

1
  • Could you share a file that we can test on our side ? Commented Feb 6, 2022 at 15:42

1 Answer 1

3

You can get those using the tags method.

import rasterio 

with rasterio.open(input_file) as src:
    print(src.tags(band_number))
1
  • This worked, thank you!
    – JWB
    Commented Feb 9, 2022 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.