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 SYNOP BUFR messages.

Source code

bufr_read_synop.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_synop
 *
 * Description: how to read SYNOP BUFR messages.
 *
 */

/* 
 * Please note that SYNOP reports can be encoded in various ways in BUFR. Therefore the code
 * below might not work directly for other types of SYNOP messages than the one used in the
 * example.
 */


#include "eccodes.h"

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

int main(int argc,char* argv[])
{
    FILE* in = NULL;
    
    /* message handle. Required in all the eccodes calls acting on a message.*/
    codes_handle* h=NULL;

    long longVal;
    double doubleVal;
    int err=0;
    int cnt=0;
    char* infile = "../../data/bufr/syno_multi.bufr";

    in=fopen(infile,"r");
    if (!in) {
        printf("ERROR: unable to open file %s\n", infile);
        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);
    
        /* we need to instruct ecCodes to unpack the data values */
        CODES_CHECK(codes_set_long(h,"unpack",1),0);
    
        /* station id*/     
        CODES_CHECK(codes_get_long(h,"blockNumber",&longVal),0);
        printf("  blockNumber: %ld\n",longVal);
    
        CODES_CHECK(codes_get_long(h,"stationNumber",&longVal),0);
        printf("  stationNumber: %ld\n",longVal);
    
        /* location*/     
        CODES_CHECK(codes_get_double(h,"latitude",&doubleVal),0);
        printf("  latitude: %f\n",doubleVal);
    
        CODES_CHECK(codes_get_double(h,"longitude",&doubleVal),0);
        printf("  longitude: %f\n",doubleVal);
    
        /* 2m temperature */
        CODES_CHECK(codes_get_double(h,"airTemperatureAt2M",&doubleVal),0);
        printf("  airTemperatureAt2M %f\n",doubleVal);
    
        /* 2m dewpoint temperature */
        CODES_CHECK(codes_get_double(h,"dewpointTemperatureAt2M",&doubleVal),0);
        printf("  dewpointTemperatureAt2M %f\n",doubleVal);
    
        /* 10 wind */
        CODES_CHECK(codes_get_double(h,"windSpeedAt10M",&doubleVal),0);
        printf("  windSpeedAt10M %f\n",doubleVal);
        
        CODES_CHECK(codes_get_double(h,"windDirectionAt10M",&doubleVal),0);
        printf("  windDirectionAt10M %f\n",doubleVal);
        
        /* delete handle */
        codes_handle_delete(h);
        
        cnt++;
    }
    
    fclose(in);
    return 0;
}
bufr_read_synop.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.
!
!
! FOTRAN 90 Implementation: bufr_read_synop
!
! Description: how to read SYNOP BUFR messages.
 
! Please note that SYNOP reports can be encoded in various ways in BUFR. Therefore the code
! below might not work directly for other types of SYNOP messages than the one used in the
! example. 


!
!
program bufr_read_synop
use eccodes
implicit none
integer            :: ifile
integer            :: iret
integer            :: ibufr
integer            :: count=0
integer(kind=4)    :: blockNumber,stationNumber 
real(kind=8)       :: lat,lon,t2m,td2m,ws,wdir

  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)

    write(*,*) 'message: ',count

    ! we need to instruct ecCodes to expand all the descriptors 
    ! i.e. unpack the data values
    call codes_set(ibufr,"unpack",1)
    
    !read and print some data values. This example was written 
    ! for a SYNOP BUFR file! 
           
    ! wmo block number
    call codes_get(ibufr,'blockNumber',blockNumber)
    write(*,*) '  blockNumber:',blockNumber
    
    ! station number
    call codes_get(ibufr,'stationNumber',stationNumber)
    write(*,*) '  stationNumber:',stationNumber
    
    ! location
    call codes_get(ibufr,'latitude',lat)
    write(*,*) '  latitude:',lat
    
    call codes_get(ibufr,'longitude',lat)
    write(*,*) '  longitude:',lat
    
    ! 2m temperature
    call codes_get(ibufr,'airTemperatureAt2M',t2m);
    write(*,*) '  airTemperatureAt2M:',t2m
   
    ! 2m dewpoint temperature
    call codes_get(ibufr,'dewpointTemperatureAt2M',td2m);
    write(*,*) '  dewpointTemperatureAt2M:',td2m
   
    ! 10m wind
    call codes_get(ibufr,'windSpeedAt10M',ws);
    write(*,*) '  windSpeedAt10M:',ws
   
    call codes_get(ibufr,'windDirectionAt10M',wdir);
    write(*,*) '  windDirectionAt10M',wdir
   
   
    ! 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_synop

bufr_read_synop.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_synop
#
# Description: how to read data values from BUFR messages. 
#

# 
# Please note that SYNOP reports can be encoded in various ways in BUFR. Therefore the code
# below might not work directly for other types of SYNOP messages than the one used in the
# example.
#

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 = [
        'blockNumber',
        'stationNumber',
        'latitude',
        'longitude',
        'airTemperatureAt2M',
        'dewpointTemperatureAt2M',
        'windSpeedAt10M',
        'windDirectionAt10M'
        ]
        
    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
        
        # we need to instruct ecCodes to expand all the descriptors
        # i.e. unpack the data values
        codes_set(gid,'unpack',1);
        
        # 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