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

Compare with Current View Page History

« Previous Version 12 Next »

Description

This example shows: how to read SYNOP BUFR messages.

Source code

bufr_read_synop.f90
!
!Copyright 2005-2017 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_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 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 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
bufr_read_synop.py
# Copyright 2005-2017 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. It is advised to use bufr_dump to
# understand the structure of the 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 = [
        'blockNumber',
        '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 type (highclouds)
    ]

    # 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 in the list above we used the # (occurrence) operator
    # 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

        # we need to instruct ecCodes to expand all the descriptors
        # i.e. unpack the data values
        codes_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())
bufr_read_synop.c
/*
 * Copyright 2005-2017 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"

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);

        /* 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) */
        CODES_CHECK(codes_get_long(h,"#1#cloudAmount",&longVal),0);
        printf("  cloudAmount (low and middle): %ld\n",longVal);

        /* Height of cloud base */
        CODES_CHECK(codes_get_long(h,"#1#heightOfBaseOfCloud",&longVal),0);
        printf("  heightOfBaseOfCloud: %ld\n",longVal);

        /* Cloud type (low clouds) */
        CODES_CHECK(codes_get_long(h,"#1#cloudType",&longVal),0);
        printf("  cloudType (low): %ld\n",longVal);

        /* Cloud type (middle clouds) */
        CODES_CHECK(codes_get_long(h,"#2#cloudType",&longVal),0);
        printf("  cloudType (middle): %ld\n",longVal);

        /* Cloud type (high clouds) */
        CODES_CHECK(codes_get_long(h,"#3#cloudType",&longVal),0);
        printf("  cloudType (high): %ld\n",longVal);

        /* delete handle */
        codes_handle_delete(h);

        cnt++;
    }

    fclose(in);
    return 0;
}
  • No labels