Versions Compared

Key

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

Description

This example shows: how to read SYNOP BUFR messages.

Source code

Tabs Container
directionhorizontal


Tabs Page
titleCFortran 90


Code Block
languagecppnone
titlebufr_read_synop.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 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 = "../. It is advised to use bufr_dump first to understand the structure of these messages.

!
!
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,t2m,td2m,ws,wdir
integer(kind=4)    :: cloudAmount,cloudBaseHeight,lowCloud,midCloud,highCloud

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

  ! The  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;
}
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

    ! The cloud information is stored in several blocks in the
    ! SYNOP message and the same key means a different thing in different
    ! parts of the message. In this example we will read the first
    ! cloud block introduced by the key
    ! verticalSignificanceSurfaceObservations=1.
    ! We know that this is the first occurrence of the keys we want to
    ! read so we will use the # (occurrence) operator accordingly.

    ! Cloud amount (low and middleclouds)
    call codes_get(ibufr,'#1#cloudAmount',cloudAmount)
    write(*,*) '  cloudAmount (low and middle):',cloudAmount

    ! Height of cloud base
    call codes_get(ibufr,'#1#heightOfBaseOfCloud',cloudBaseHeight)
    write(*,*) '  heightOfBaseOfCloud:',cloudBaseHeight

    ! Cloud type (low clouds)
    call codes_get(ibufr,'#1#cloudType',lowCloud)
    write(*,*) '  cloudType (low):',lowCloud

    ! Cloud type (middle clouds)
    call codes_get(ibufr,'#2#cloudType',midCloud)
    write(*,*) '  cloudType (middle):',midCloud

    ! Cloud type (high clouds)
    call codes_get(ibufr,'#3#cloudType',highCloud)
    write(*,*) '  cloudType (high):',highCloud

    ! 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



Tabs Page
titlePython


Code Block
languagepython
Tabs Page
titleFortran 90
Code Block
languagenone
titlebufr_read_synop.f90py
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.
!
!#
!# FOTRANPython 90 Implementationimplementation: bufr_read_synop
!#
!# Description: how to read data SYNOPvalues 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. 


!
!
programIt is advised to use bufr_read_synop
use eccodes
implicit none
integer            :: ifile
integer            :: iret
integer            :: ibufr
integer            :: count=0
integer(kind=4)    :: blockNumber,stationNumber 
real(kind=8)dump to
# understand the structure of the messages.

import sys
import traceback

from eccodes import *

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


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

    # define the keys to be printed
    keys = [
        'blockNumber',
       :: 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! 
    'stationNumber',
        'latitude',
        'longitude',
        'airTemperatureAt2M',
        'dewpointTemperatureAt2M',
        'windSpeedAt10M',
        'windDirectionAt10M',
        '#1#cloudAmount',  # cloud amount (low and mid level)
        '#1#heightOfBaseOfCloud',
        '#1#cloudType',  # cloud type (low clouds)
        '#2#cloudType',  # cloud type (middle clouds)
       
 '#3#cloudType'  # !cloud wmo block numbertype (highclouds)
    call codes_get(ibufr,'blockNumber',blockNumber)]

    write(*,*) '  blockNumber:',blockNumber
    
    ! station number
    call codes_get(ibufr,'stationNumber',stationNumber)
    write(*,*) '  stationNumber:',stationNumber# The cloud information is stored in several blocks in the
    # SYNOP message and the same key means a different thing in different
    
# parts of the !message. location
In this example we call codes_get(ibufr,'latitude',lat)will read the first
    write(*,*) '  latitude:',lat
    # cloud block introduced by the key
    call codes_get(ibufr,'longitude',lat)# verticalSignificanceSurfaceObservations=1.
    write(*,*) '  longitude:',lat
    
    ! 2m temperature# We know that this is the first occurrence of the keys we want to
    call codes_get(ibufr,'airTemperatureAt2M',t2m);
    write(*,*) '  airTemperatureAt2M:',t2m
   # read so in the list above we used the # (occurrence) operator
    ! 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# accordingly.

    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)

    ! load the next bufr# message
we need to instruct call codes_bufr_new_from_file(ifile,ibufr,iret)
    ecCodes to expand all the descriptors
    count=count+1
    # 
i.e. unpack endthe dodata values
 

! close file  
  call codes_close_file(ifile)
 

end program bufr_read_synop

Tabs Page
titlePython
set(bufr, 'unpack', 1)

        # 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()
    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_synop.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_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. It is advised to use bufr_dump to understand the structure of the messages.
 */


#include "eccodes.h"

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;
    const char* infile = "../../data/bufr/syno_multi.bufr";

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

        /* The cloud information is stored in several blocks in the
         * SYNOP message and the same key means a different thing in different
         * parts of the message. In this example we will read the first
         * cloud block introduced by the key
         * verticalSignificanceSurfaceObservations=1.
         * We know that this is the first occurrence of the keys we want to
Code Block
languagepython
titlebufr_read_synop.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_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* handleread forso message
we will use the # (occurrence) operator  gid = codes_bufr_new_from_file(f)
accordingly. */

        /* Cloud ifamount gid(low isand None: break

middleclouds) */
        print "message: %s" % cnt
CODES_CHECK(codes_get_long(h,"#1#cloudAmount",&longVal),0);
        #printf(" we needcloudAmount to(low instructand ecCodes to expand all the descriptorsmiddle): %ld\n",longVal);

        # i.e. unpack the data values/* Height of cloud base */
        CODES_CHECK(codes_set(gid,'unpack',1)
get_long(h,"#1#heightOfBaseOfCloud",&longVal),0);
        #printf(" print the values for the selected keys from the messageheightOfBaseOfCloud: %ld\n",longVal);

        for/* keyCloud in keys:
  type (low clouds) */
          if not CODES_CHECK(codes_isget_definedlong(gid,key): raise Exception("Key: " + key + " was not defined")h,"#1#cloudType",&longVal),0);
        printf("    print '  %s: %s' % (key,codes_get(gid,key))cloudType (low): %ld\n",longVal);

        cnt+=1

     /* Cloud type (middle clouds) */
   # delete handle
        CODES_CHECK(codes_release(gid)

get_long(h,"#2#cloudType",&longVal),0);
    # close the file
 printf("  cloudType f.close(middle)

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

   try:
     /* Cloud type example()
    except CodesInternalError,err:high clouds) */
        if VERBOSE:CODES_CHECK(codes_get_long(h,"#3#cloudType",&longVal),0);
        printf("  cloudType  traceback.print_exc(file=sys.stderr)(high): %ld\n",longVal);

        else:/* delete handle */
          codes_handle_delete(h);

  print >>sys.stderr,err.msg

     cnt++;
   return 1}

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