You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Description

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

Source code

bufr_read_header.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.
 */

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

#include "eccodes.h"

void usage(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,"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);
        
        cnt++;
    }
    
    fclose(in);
    return 0;
}
bufr_read_header.f90
!
!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_print_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
# 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',
        'masterTablesVersionNumber',
        'localTablesVersionNumber',
        'numberOfSubsets',
        ]
        
    cnt=0    
    
    # loop for the messages in the file
    while 1:
        # get handle for message
        gid = codes_bufr_new_from_file(f)
        if gid is None: break

        print "message: %s" % cnt
        
        # print the values for the selected keys from the message
        for key in keys:
            if not codes_is_defined(gid,key): raise Exception("Key " + key + " was not defined")
            print '  %s: %s' % (key,codes_get(gid,key))

        cnt+=1
        
        # delete handle
        codes_release(gid)
        
    # close the file
    f.close()

def main():
    try:
        example()
    except CodesInternalError,err:
        if VERBOSE:
            traceback.print_exc(file=sys.stderr)
        else:
            print >>sys.stderr,err.msg

        return 1

if __name__ == "__main__":
    sys.exit(main())
  • No labels