Skip to end of metadata
Go to start of metadata

Description

This example shows: how to read data for a given beam from scatterometer BUFR messages.

Source code

bufr_read_scatterometer.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_scatterometer
!
! Description: how to read data for a given beam from scatterometer BUFR messages.
!
! Please note that scatterometer data can be encoded in various ways in BUFR. Therefore the code
! below might not work directly for other types of 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_scatterometer
use eccodes
implicit none
integer            :: ifile
integer            :: iret
integer            :: ibufr
integer            :: i, count=0
integer(kind=4)    :: numObs,ii
real(kind=8), dimension(:), allocatable :: latVal,lonVal,bscatterVal
real(kind=8), dimension(:), allocatable :: year

  call codes_open_file(ifile,'../../data/bufr/asca_139.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(*,'(A,I3)') 'message: ',count

    ! We need to instruct ecCodes to expand all the descriptors
    ! i.e. unpack the data values
    call codes_set(ibufr,"unpack",1);

    ! The BUFR file contains a single message with 2016 subsets in a compressed form.
    ! It means each subset has exactly the same structure: they store one location with
    ! several beams and one backscatter value in each beam.
    !
    ! To print the backScatter values for beamIdentifier=2 from all the subsets
    ! we will simply access the key by condition (see below).

    ! Read the total number of subsets.
    call codes_get(ibufr,'numberOfSubsets',numObs)

    write(*,'(A,I5)') "Number of values:",numObs

    ! Get latitude (for all the subsets)
    call codes_get(ibufr,'latitude',latVal);

    ! Get longitude (for all the subsets)
    call codes_get(ibufr,'longitude',lonVal);

    allocate(year(numObs))
    call codes_get(ibufr,'year',year);
    do ii= 1, size(year)
      write(*,'(A,I4,A,F8.1)') 'year(',ii,')=',year(ii)
    enddo

    ! Get backScatter for beam two. We use an access by condition for this key.
    ! (for all the subsets)
    call codes_get(ibufr,'/beamIdentifier=2/backscatter',bscatterVal);

    ! Check that all arrays are same size
    if (size(latVal)/= numObs .or. size(lonVal)/= numObs .or. size(bscatterVal)/= numObs) then
      print *,'inconsistent array dimension'
      exit
    endif

    ! Print the values
    write(*,*) 'pixel   lat    lon     backscatter'
    write(*,*) "--------------------------------------"

    do i=1,numObs
        write(*,'(I4,3F10.5)') i,latVal(i),lonVal(i),bscatterVal(i)
    end do

    ! Free arrays
    deallocate(latVal)
    deallocate(lonVal)
    deallocate(bscatterVal)

    ! 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_scatterometer
bufr_read_scatterometer.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_scatterometer
#
# Description: how to read data for a given beam from scatterometer BUFR
# messages.
#
# Please note that scatterometer data can be encoded in various ways in BUFR.
# Therefore the code below might not work directly for other types of messages
# than the one used in the example. It is advised to use bufr_dump first to
# understand the structure of these messages.
#

from __future__ import print_function
import traceback
import sys

from eccodes import *

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


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

    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)

        # The BUFR file contains a single message with 2016 subsets in a
        # compressed form. It means each subset has exactly the same structure:
        # they store one location with several beams and one backscatter value
        # in each beam.
        #
        # To print the backScatter values for beamIdentifier=2 from all the
        # subsets we will simply access the key by condition (see below)

        # Get the total number of subsets.
        numObs = codes_get(bufr, "numberOfSubsets")

        print('  Number of values: %ld' % numObs)

        # Get latitude (for all the subsets)
        lat = codes_get_array(bufr, "latitude")

        # Get longitude (for all the subsets)
        lon = codes_get_array(bufr, "longitude")

        # Get backScatter for beam two. We use an access by condition for this
        # key (for all the subsets).
        bscat = codes_get_array(bufr, "/beamIdentifier=2/backscatter")

        # Check that all arrays are same size
        if len(lat) != numObs or len(lon) != numObs or len(bscat) != numObs:
            print('inconsistent array dimension')
            return 1

        # Print the values
        print("pixel  lat    lon    backscatter")
        print("-------------------------------")

        for i in range(numObs):
            print("%3d %.2f %.2f %.2f" % (i + 1, lat[i], lon[i], bscat[i]))

        cnt += 1

        # Release 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_scatterometer.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_scatterometer
 *
 * Description: how to read data for a given beam from scatterometer BUFR messages.
 *
 */

/*
 * Please note that scatterometer data can be encoded in various ways in BUFR. Therefore the code
 * below might not work directly for other types of 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;

    double *lat=NULL, *lon=NULL, *bscatter=NULL;
    long numObs=0;
    size_t len=0;
    int i, err=0;
    int cnt=0;
    const char* infile = "../../data/bufr/asca_139.bufr";
    char key_name[128];

    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 expand the descriptors
         * i.e. unpack the data values */
        CODES_CHECK(codes_set_long(h,"unpack",1),0);

        /* The BUFR file contains a single message with 2016 subsets in a compressed form.
         * It means each subset has exactly the same structure: they store one location with
         * several beams and one backscatter value in each beam.
         *
         * To print the backScatter values for beamIdentifier=2 from all the subsets
         * we will simply access the key by condition (see below) */

        /* Get the total number of subsets. */
        CODES_CHECK(codes_get_long(h,"numberOfSubsets",&numObs),0);

        printf("Number of values: %ld\n",numObs);

        /* Allocate memory for the values to be read. Each
         * parameter must have the same number of values. */
        lat = (double*)malloc(numObs*sizeof(double));
        lon = (double*)malloc(numObs*sizeof(double));
        bscatter = (double*)malloc(numObs*sizeof(double));

        /* Get latitude */
        sprintf(key_name,"latitude");

        /* Check the size (including all the subsets) */
        CODES_CHECK(codes_get_size(h,key_name,&len),0);
        if(len != numObs)
        {
            printf("inconsistent number of %s values found!\n",key_name);
            return 1;
        }

        /* Get the values (from all the subsets) */
        CODES_CHECK(codes_get_double_array(h,key_name,lat,&len),0);

        /* Get longitude */
        sprintf(key_name,"longitude");

        /* Check the size (including all the subsets) */
        CODES_CHECK(codes_get_size(h,key_name,&len),0);
        if(len != numObs)
        {
            printf("inconsistent number of %s values found!\n",key_name);
            return 1;
        }

        /* Get the values (from all the subsets) */
        CODES_CHECK(codes_get_double_array(h,key_name,lon,&len),0);

        /* Get backScatter for beam two. We use an access by condition for this key. */
        sprintf(key_name,"/beamIdentifier=2/backscatter");

        /* Check the size (including all the subsets) */
        CODES_CHECK(codes_get_size(h,key_name,&len),0);
        if(len != numObs)
        {
            printf("inconsistent number of %s values found!\n",key_name);
            return 1;
        }

        /* Get the values (from all the subsets) */
        CODES_CHECK(codes_get_double_array(h,key_name,bscatter,&len),0);

        /* Print the values */
        printf("pixel   lat    lon     backscatter    \n");
        printf("-------------------------------\n");

        for(i=0; i < numObs; i++)
        {
            printf("%4d %.3f %.3f %.3f \n",
                    i+1,lat[i],lon[i],bscatter[i]);
        }

        /* Delete handle */
        codes_handle_delete(h);

        /* Release memory */
        free(lat);
        free(lon);
        free(bscatter);

        cnt++;
    }

    fclose(in);
    return 0;
}

  • No labels