Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Description

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

Source code

Tabs Container
directionhorizontal


Tabs Page
titleCFortran 90


Code Block
languagecppnone
titlebufr_read_header.cf90
linenumbersfalse
/*!
! *(C) Copyright 2005-2015 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 C90 Implementationimplementation: bufr_read_header
 *!
! * Description: how to read the header of BUFR messages.
 *!
!
 */

#include "eccodes.h"

void usage(char* prog) {
program bufr_read_header
use eccodes
implicit none
integer            printf("usage:: %s infile\n",prog);
ifile
integer    exit(1);
}

int main(int argc,char* argv[])
{
    char* filename =:: NULL;iret
integer    FILE* in = NULL;
    
 :: ibufr
integer  /* message handle. Required in all the eccodes calls acting on a message.*/
:: count=0
integer(kind=4)    codes_handle* h=NULL;
:: dataCategory,dataSubCategory,typicalDate
integer(kind=4)    long longVal;
:: centre,subcentre
integer(kind=4)    int err=0, cnt=0;
    
    if (argc!=2) usage(argv[0]);
    
    filename=argv[1];:: masterversion,localversion
integer(kind=4)    :: numberofsubsets

    in=fopen(filename,"r");
    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);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



Tabs Page
titlePython


Code Block
languagepython
titlebufr_read_header.py
linenumbersfalse
# (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',
        cnt++;
'typicalDate',
      }  'bufrHeaderCentre',
        'bufrHeaderSubCentre',
       fclose(in);
 'masterTablesVersionNumber',
       return 0;
}
Tabs Page
titleFortran 90
Code Block
languagenone
titlebufr_read_header.f90
linenumbersfalse
!
!Copyright 2005-2015 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 '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: ibufr
integer            try:: count=0
integer(kind=4)
     :: dataCategory,dataSubCategory,typicalDate
integer(kind=4)    :: centre,subcentre
integer(kind=4)    :: masterversion,localversion
integer(kind=4)      print('  %s: %s' % (key, codes_get(bufr, key)))
       :: numberofsubsets   

 except 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
Tabs Page
titlePython
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())



Tabs Page
titleC


Code Block
languagecpp
titlebufr_read_header.c
linenumbersfalse
/*
 * (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);
Code Block
languagepython
titlebufr_read_header.py
linenumbersfalse
# Copyright 2005-2015 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 traceback
import sys

from eccodes import *

INPUT='../../data/bufr/syno_multi.bufr'
VERBOSE=1 # verbose error reporting

def example():

    # open bufr file
    f = open(INPUT)

    # define the keys to be printed
    keys = [
        'dataCategory',
        'dataSubCategory',
        'typicalDate',
        'bufrHeaderCentre',
        'bufrHeaderSubCentre',
    cnt++;
    'masterTablesVersionNumber',
        'localTablesVersionNumber',continue;
        'numberOfSubsets',}

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

    cnt=0

    /* #get loopand forprint thesome messageskeys inform the file BUFR header */

    while 1:
   CODES_CHECK(codes_get_long(h,"dataCategory",&longVal),0);
     # get handle forprintf(" message
 dataCategory: %ld\n",longVal);

      gid = CODES_CHECK(codes_bufr_new_from_file(f)get_long(h,"dataSubCategory",&longVal),0);
        ifprintf(" gid is NonedataSubCategory: break%ld\n",longVal);

        print "message: %s" % cnt

CODES_CHECK(codes_get_long(h,"typicalDate",&longVal),0);
        printf("  # print the values for the selected keys from the messagetypicalDate: %ld\n",longVal);

        CODES_CHECK(codes_get_long(h,"bufrHeaderCentre",&longVal),0);
        forprintf(" key in keys:bufrHeaderCentre: %ld\n",longVal);

            if not CODES_CHECK(codes_isget_definedlong(gid,key): raise Exception("Key " + key + " was not defined")h,"bufrHeaderSubCentre",&longVal),0);
        printf("  bufrHeaderSubCentre:  print '  %s: %s' % (key,codes_get(gid,key))
%ld\n",longVal);

        CODES_CHECK(codes_get_long(h,"masterTablesVersionNumber",&longVal),0);
        cnt+=1

printf("        # delete handlemasterTablesVersionNumber: %ld\n",longVal);

        CODES_CHECK(codes_get_release(gid)

    # close the file
long(h,"localTablesVersionNumber",&longVal),0);
       f.close()

def main():
    try: printf("  localTablesVersionNumber: %ld\n",longVal);

        example()
    except CodesInternalError,err:CODES_CHECK(codes_get_long(h,"numberOfSubsets",&longVal),0);
        if VERBOSE:printf("  numberOfSubsets: %ld\n",longVal);

        /* delete   traceback.print_exc(file=sys.stderr)handle */
        else:
 codes_handle_delete(h);

        cnt++;
   print >>sys.stderr,err.msg }

    fclose(in);
    return 1

if __name__ == "__main__":
    sys.exit(main())0;
}