Description

This example shows: how to read the header of BUFR messages.

Source code

bufr_read_header.f90
!
! (C) Copyright 2005- ECMWF.
!
! This software is licensed under the terms of the Apache Licence Version 2.0
!which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
!
! In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
! virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
!
!
! FORTRAN 90 implementation: bufr_read_header
!
! Description: how to read the header of BUFR messages.
!
!
program bufr_read_header
use eccodes
implicit none
integer            :: ifile
integer            :: iret
integer            :: ibufr
integer            :: count=0
integer(kind=4)    :: dataCategory,dataSubCategory,typicalDate
integer(kind=4)    :: centre,subcentre
integer(kind=4)    :: masterversion,localversion
integer(kind=4)    :: numberofsubsets

  call codes_open_file(ifile,'../../data/bufr/syno_multi.bufr','r')

  ! The first bufr message is loaded from file,
  ! ibufr is the bufr id to be used in subsequent calls
  call codes_bufr_new_from_file(ifile,ibufr,iret)

  do while (iret/=CODES_END_OF_FILE)

    ! Get and print some keys form the BUFR header
    write(*,*) 'message: ',count

    call codes_get(ibufr,'dataCategory',dataCategory);
    write(*,*) '  dataCategory:',dataCategory

    call codes_get(ibufr,'dataSubCategory',dataSubCategory);
    write(*,*) '  dataSubCategory:',dataSubCategory

    call codes_get(ibufr,'typicalDate',typicalDate);
    write(*,*) '  typicalDate:',typicalDate

    call codes_get(ibufr,'bufrHeaderCentre',centre);
    write(*,*) '  bufrHeaderCentre:',centre

    call codes_get(ibufr,'bufrHeaderSubCentre',subcentre)
    write(*,*) '  bufrHeaderSubCentre:',subcentre

    call codes_get(ibufr,'masterTablesVersionNumber',masterversion)
    write(*,*) '  masterTablesVersionNumber:',masterversion

    call codes_get(ibufr,'localTablesVersionNumber',localversion)
    write(*,*) '  localTablesVersionNumber:',localversion

    call codes_get(ibufr,'numberOfSubsets',numberofsubsets)
    write(*,*) '  numberOfSubsets:',numberofsubsets

    ! Release the bufr message
    call codes_release(ibufr)

    ! Load the next bufr message
    call codes_bufr_new_from_file(ifile,ibufr,iret)

    count=count+1

  end do

  ! Close file
  call codes_close_file(ifile)

end program bufr_read_header
bufr_read_header.py
# (C) Copyright 2005- ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
#
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.


#
# Python implementation: bufr_read_header
#
# Description: how to read the header from BUFR messages.
#
#

import sys
import traceback

from eccodes import *

VERBOSE = 1  # verbose error reporting


def example(INPUT):
    # open bufr file
    f = open(INPUT, 'rb')

    # define the keys to be printed
    keys = [
        'dataCategory',
        'dataSubCategory',
        'typicalDate',
        'bufrHeaderCentre',
        'bufrHeaderSubCentre',
        'masterTablesVersionNumber',
        'localTablesVersionNumber',
        'numberOfSubsets',
    ]

    cnt = 0

    # loop for the messages in the file
    while 1:
        # get handle for message
        bufr = codes_bufr_new_from_file(f)
        if bufr is None:
            break

        print("message: %s" % cnt)

        # print the values for the selected keys from the message
        for key in keys:
            try:
                print('  %s: %s' % (key, codes_get(bufr, key)))
            except CodesInternalError as err:
                print('Error with key="%s" : %s' % (key, err.msg))

        cnt += 1

        # delete handle
        codes_release(bufr)

    # close the file
    f.close()


def main():
    try:
        example(sys.argv[1])
    except CodesInternalError as err:
        if VERBOSE:
            traceback.print_exc(file=sys.stderr)
        else:
            sys.stderr.write(err.msg + '\n')

        return 1


if __name__ == "__main__":
    sys.exit(main())
bufr_read_header.c
/*
 * (C) Copyright 2005- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
 * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
 */

/*
 * C Implementation: bufr_read_header
 *
 * Description: how to read the header of BUFR messages.
 *
 */

#include "eccodes.h"

static void usage(const char* prog) {
    printf("usage: %s infile\n",prog);
    exit(1);
}

int main(int argc,char* argv[])
{
    char* filename = NULL;
    FILE* in = NULL;

    /* message handle. Required in all the eccodes calls acting on a message.*/
    codes_handle* h=NULL;
    long longVal;
    int err=0, cnt=0;

    if (argc!=2) usage(argv[0]);

    filename=argv[1];

    in=fopen(filename,"rb");
    if (!in) {
        printf("ERROR: unable to open file %s\n", filename);
        return 1;
    }

    /* loop over the messages in the bufr file */
    while ((h = codes_handle_new_from_file(NULL,in,PRODUCT_BUFR,&err)) != NULL || err != CODES_SUCCESS)
    {
        if (h == NULL) {
            printf("Error: unable to create handle for message %d\n",cnt);
            cnt++;
            continue;
        }

        printf("message: %d\n",cnt);

        /* get and print some keys form the BUFR header */

        CODES_CHECK(codes_get_long(h,"dataCategory",&longVal),0);
        printf("  dataCategory: %ld\n",longVal);

        CODES_CHECK(codes_get_long(h,"dataSubCategory",&longVal),0);
        printf("  dataSubCategory: %ld\n",longVal);

        CODES_CHECK(codes_get_long(h,"typicalDate",&longVal),0);
        printf("  typicalDate: %ld\n",longVal);

        CODES_CHECK(codes_get_long(h,"bufrHeaderCentre",&longVal),0);
        printf("  bufrHeaderCentre: %ld\n",longVal);

        CODES_CHECK(codes_get_long(h,"bufrHeaderSubCentre",&longVal),0);
        printf("  bufrHeaderSubCentre: %ld\n",longVal);

        CODES_CHECK(codes_get_long(h,"masterTablesVersionNumber",&longVal),0);
        printf("  masterTablesVersionNumber: %ld\n",longVal);

        CODES_CHECK(codes_get_long(h,"localTablesVersionNumber",&longVal),0);
        printf("  localTablesVersionNumber: %ld\n",longVal);

        CODES_CHECK(codes_get_long(h,"numberOfSubsets",&longVal),0);
        printf("  numberOfSubsets: %ld\n",longVal);

        /* delete handle */
        codes_handle_delete(h);

        cnt++;
    }

    fclose(in);
    return 0;
}