Versions Compared

Key

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

...

Tabs Container
directionhorizontal
Tabs Page
titleCFortran 90
Code Block
languagecppnone
titlebufr_read_synop.cf90
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.
 */!
!
/*
! *FOTRAN 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. It is advised to use bufr_dump first to understand the structure of thethese messages.
 */
!

#include "eccodes.h"

void usage(char* prog) {
!
program bufr_read_synop
use eccodes
implicit none
integer       printf("usage: %s infile\n",prog);
    exit(1);
}

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

:: count=0
integer(kind=4)    long longVal;
:: blockNumber,stationNumber 
real(kind=8)    double doubleVal;
  ::  int err=0;
    int cnt=0;
    char* infile = "lat,lon,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  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) {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! 
            printf("Error: unable to create handle for message %d\n",cnt);
    ! wmo block number
    call codes_get(ibufr,'blockNumber',blockNumber)
    write(*,*) '  blockNumber:',blockNumber
     cnt++;
    ! station number
      continue;call codes_get(ibufr,'stationNumber',stationNumber)
    write(*,*) '   }stationNumber:',stationNumber
    
    ! location
    call printf("message: %d\n",cnt);codes_get(ibufr,'latitude',lat)
    write(*,*) '  latitude:',lat
    
    /* we need to instruct ecCodes to unpack the data values */
        CODES_CHECK(codes_set_long(h,"unpack",1),0)call codes_get(ibufr,'longitude',lat)
    write(*,*) '  longitude:',lat
    
    ! 2m temperature
    call codes_get(ibufr,'airTemperatureAt2M',t2m);
    
 write(*,*) '  airTemperatureAt2M:',t2m
   
  /* station id*/! 2m dewpoint   temperature
        CODES_CHECK(call codes_get_long(h,"blockNumber",&longVal),0)ibufr,'dewpointTemperatureAt2M',td2m);
    write(*,*) '   printf("dewpointTemperatureAt2M:',td2m
  blockNumber: %ld\n",longVal);
    
! 10m wind
    call  CODES_CHECK(codes_get_long(h,"stationNumber",&longVal),0ibufr,'windSpeedAt10M',ws);
    write(*,*) '   printf("  stationNumber: %ld\n",longVal);windSpeedAt10M:',ws
    
    call codes_get(ibufr,'windDirectionAt10M',wdir);
   / write(*,*) location*/'  windDirectionAt10M:',wdir
   
    ! The cloud information  CODES_CHECK(codes_get_double(h,"latitude",&doubleVal),0);is stored in several blocks in the
    ! SYNOP message  printf("  latitude: %f\n",doubleVal);
    and the same key means a different thing in different
    ! parts of the message. In  CODES_CHECK(codes_get_double(h,"longitude",&doubleVal),0);
        printf("  longitude: %f\n",doubleVal);
    this example we will read the first
    ! cloud block introduced by the key
    ! verticalSignificanceSurfaceObservations=1. 
  /* 2m temperature */
        CODES_CHECK(codes_get_double(h,"airTemperatureAt2M",&doubleVal),0);  ! We know that this is the first occurrence of the keys we want to
    ! read so  printf("  airTemperatureAt2M: %f\n",doubleVal);
    
we will use the # (occurrence) operator accordingly. 
        
 /* 2m dewpoint temperature! */
Cloud amount (low and middleclouds)
    CODES_CHECK(call codes_get_double(h,"dewpointTemperatureAt2M",&doubleVal),0);ibufr,'#1#cloudAmount',cloudAmount)
    write(*,*) '  cloudAmount printf("low and dewpointTemperatureAt2M: %f\n",doubleVal);middle):',cloudAmount
    
    
    ! /*Height 10of windcloud */base
    call    CODES_CHECK(codes_get_double(h,"windSpeedAt10M",&doubleVal),0);
   ibufr,'#1#heightOfBaseOfCloud',cloudBaseHeight)
    write(*,*) '  heightOfBaseOfCloud:',cloudBaseHeight
     printf("  windSpeedAt10M: %f\n",doubleVal);
    ! Cloud type (low clouds)
    call    CODES_CHECK(codes_get_double(h,"windDirectionAt10M",&doubleVal),0);ibufr,'#1#cloudType',lowCloud)
        printf("  windDirectionAt10M: %f\n",doubleVal);write(*,*) '  cloudType (low):',lowCloud 
        
    ! Cloud type  /* The cloud information is stored in several blocks in the
    (middle clouds)
    call codes_get(ibufr,'#2#cloudType',midCloud)
    write(*,*) '  cloudType (middle):',midCloud 
    
 * SYNOP message and! theCloud sametype key means a different thing in different(high clouds)
    call codes_get(ibufr,'#3#cloudType',highCloud)
    write(*,*) '  cloudType  * parts of the message. In this example we will read the first(high):',highCloud 
   
    ! release the bufr message
      call codes_release(ibufr)

   * cloud! blockload introducedthe bynext thebufr keymessage
    call codes_bufr_new_from_file(ifile,ibufr,iret)
    * verticalSignificanceSurfaceObservations=1. 
    count=count+1
    
 * Weend knowdo that this

! isclose thefile 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,"cloudAmount#1",&longVal),0);
        printf("  cloudAmount (low and middle): %ld\n",longVal);
        
        /* Height of cloud base */
        CODES_CHECK(codes_get_long(h,"heightOfBaseOfCloud#1",&longVal),0);
        printf("  heightOfBaseOfCloud: %ld\n",longVal);
        
        /* Cloud type (low clouds) */
        CODES_CHECK(codes_get_long(h,"cloudType#1",&longVal),0);
        printf("  cloudType (low): %ld\n",longVal);
        
        /* Cloud type (middle clouds) */call codes_close_file(ifile)
 

end program bufr_read_synop

Tabs Page
titlePython
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. 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',
        CODES_CHECK(codes_get_long(h,"cloudType#2",&longVal),0);
'stationNumber',
        'latitude',
  printf("  cloudType (middle): %ld\n",longVal);    'longitude',
        'airTemperatureAt2M',
        'dewpointTemperatureAt2M',
   /* Cloud type (high clouds) */ 'windSpeedAt10M',
        CODES_CHECK(codes_get_long(h,"cloudType#3",&longVal),0);'windDirectionAt10M',
        printf("'#1#cloudAmount',  cloudType# (high): %ld\n",longVal);cloud amount (low and mid level)
        '#1#heightOfBaseOfCloud',
        /* delete handle */
  '#1#cloudType',  # cloud type (low clouds)
      codes_handle_delete(h);
  '#2#cloudType',  # cloud type (middle clouds)
        cnt++;
'#3#cloudType'  # cloud }type (highclouds)
    ]

    fclose(in);
    return 0;
}
Tabs Page
titleFortran 90
Code Block
languagenone
titlebufr_read_synop.f90
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.
!
!
! 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. It is advised to use bufr_dump first to understand the structure of these messages.

!
!
program bufr_read_synop
use eccodes
implicit none
integer   # 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
        gid :: ifile
integer= codes_bufr_new_from_file(f)
        if gid is None:
   :: iret
integer         break

        print :: ibufr
integer"message: %s" % cnt

        # we need to :: count=0
integer(kind=4)    :: blockNumber,stationNumber 
real(kind=8)instruct ecCodes to expand all the descriptors
        :: lat,lon,t2m,td2m,ws,wdir
integer(kind=4)# i.e. unpack the data values
    :: cloudAmount,cloudBaseHeight,lowCloud,midCloud,highCloud 

  call codes_open_fileset(ifilegid,'../../data/bufr/syno_multi.bufr','r' 'unpack', 1)

! 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   # print the values for the selected keys from the message
        for key in keys:
            try:
                print '  %s: %s' % (key, codes_get(gid, key))
    
    !read and print some dataexcept values. This example was written 
CodesInternalError, err:
        ! for a SYNOP BUFR file! 
  print 'Error with key="%s" : %s' %   (key, err.msg)

    ! wmo block number
 cnt += 1

 call codes_get(ibufr,'blockNumber',blockNumber)
      write(*,*) '  blockNumber:',blockNumber # delete handle
    
    ! station number
codes_release(gid)

    # close call codes_get(ibufr,'stationNumber',stationNumber)the file
    write(*,*) '  stationNumber:',stationNumberf.close()


def main():
    try:
    ! location
    call codes_get(ibufr,'latitude',latexample()
    write(*,*) '  latitude:',latexcept CodesInternalError, err:
        if VERBOSE:
    
       call codestraceback.print_get(ibufr,'longitude',latexc(file=sys.stderr)
    write(*,*) '    longitudeelse:',lat
    
    ! 2m temperature
   print call codes_get(ibufr,'airTemperatureAt2M',t2m);>>sys.stderr, err.msg

    write(*,*) '  airTemperatureAt2M:',t2m
  return 1

if __name__   ! 2m dewpoint temperature== "__main__":
    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,'cloudAmount#1',cloudAmount)
    write(*,*) '  cloudAmount (low and middle):',cloudAmount
        
    ! Height of cloud base
    call codes_get(ibufr,'heightOfBaseOfCloud#1',cloudBaseHeight)
    write(*,*) '  heightOfBaseOfCloud:',cloudBaseHeight
        
    ! Cloud type (low clouds)
    call codes_get(ibufr,'cloudType#1',lowCloud)
    write(*,*) '  cloudType (low):',lowCloud 
        
    ! Cloud type (middle clouds)
    call codes_get(ibufr,'cloudType#2',midCloud)
    write(*,*) '  cloudType (middle):',midCloud 
    
    ! Cloud type (high clouds)
    call codes_get(ibufr,'cloudType#3',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
sys.exit(main())
Tabs Page
titleC
Code Block
languagecpp
titlebufr_read_synop.c
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.
 */

/*
 * 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);
        
        /*
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. 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',
        'cloudAmount#1',  #cloud amount (low and mid level)
        'heightOfBaseOfCloud#1', 
        'cloudType#1',   #cloud type (low clouds)
        'cloudType#2',   #cloud type (middle clouds)
        'cloudType#3'    #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
 * cloud block #introduced readby sothe inkey
 the list above we used the # (occurrence) operator* accordinglyverticalSignificanceSurfaceObservations=1. 
        
 * We know cnt=0

that this is the #first loopoccurrence forof the messageskeys inwe thewant fileto
     while   1:
 * read so we will use the # get(occurrence) handleoperator foraccordingly. message*/
        gid
 = codes_bufr_new_from_file(f)
      /* Cloud ifamount gid(low isand None: break

middleclouds) */
        print "message: %s" % cnt

CODES_CHECK(codes_get_long(h,"#1#cloudAmount",&longVal),0);
        printf("  cloudAmount (low #and we need to instruct 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);
        # print the values for the selected keys from the messageprintf("  heightOfBaseOfCloud: %ld\n",longVal);
        
        for/* keyCloud in keys:
  type (low clouds) */
          try:CODES_CHECK(codes_get_long(h,"#1#cloudType",&longVal),0);
        printf("  cloudType (low): %ld\n",longVal);
    print '  %s: %s'
 % (key,codes_get(gid,key))
      /* Cloud type (middle clouds) */
 except CodesInternalError,err:
      CODES_CHECK(codes_get_long(h,"#2#cloudType",&longVal),0);
        printf("  print 'Error with key="%s" : %s' % (key,err.msg) 

cloudType (middle): %ld\n",longVal);
        cnt+=1

        #/* deleteCloud handle
type (high       codes_release(gid)

    # close the file
    f.close()

def main():clouds) */
        CODES_CHECK(codes_get_long(h,"#3#cloudType",&longVal),0);
    try:
    printf("  cloudType  example()(high): %ld\n",longVal);
    except  CodesInternalError,err:
  
      if VERBOSE:
 /* delete handle */
        traceback.print_exc(file=sys.stderr)codes_handle_delete(h);
        else:
        cnt++;
    print >>sys.stderr,err.msg

}
    
    return 1

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