Versions Compared

Key

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

Description

This example shows:

...

How to use

...

keys_iterator

...

to get all the available keys in a GRIB message.

Source code

Tabs Container
directionhorizontal


Tabs Page
titleCFortran 90


Code Block
languagecppnone
titlegrib_keys_iterator.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.
 */!
!
/*
! * C Implementation: grib_keys_iterator
 *
 * Description:
 * Example on howDescription:
!       How to use keys_iterator functions and the
 * codes_keys_iterator structure to get all the available
!     *  keys in a GRIB message.
!
!
!
program keys_iterator
  use eccodes
  implicit none
  character(len=20)  :: name_space
  integer            :: kiter,ifile,igrib,iret
  character(len=256) :: key
  character(len=256) :: value
  character(len=512) :: all1
  integer            :: grib_count

  call codes_open_file(ifile, &
       '../../data/regular_latlon_surface.grib1','r')

  ! Loop on all the messages in a file.

  call codes_grib_new_from_file(ifile,igrib, iret)
  grib_count=0
  do while (iret /= CODES_END_OF_FILE)

    grib_count=grib_count+1
    write(*,*) '-- GRIB N. ',grib_count,' --'

    ! valid name_spaces are ls and mars
    name_space='ls'

    call codes_keys_iterator_new(igrib,kiter,name_space)

    do
      call codes_keys_iterator_next(kiter, iret)

      if (iret .ne. CODES_SUCCESS) exit !terminate the loop

      call codes_keys_iterator_get_name(kiter,key)
      call codes_get(igrib,trim(key),value)
      all1=trim(key)// ' = ' // trim(value)
      write(*,*) trim(all1)

    end do

    call codes_keys_iterator_delete(kiter)
    call codes_release(igrib)
    call codes_grib_new_from_file(ifile,igrib, iret)
  end do


  call codes_close_file(ifile)

end program keys_iterator



Tabs Page
titlePython


Code Block
languagepython
titlegrib_keys_iterator.py
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.
#

import traceback
import sys

from eccodes import *

INPUT = '../../data/reduced_latlon_surface.grib1'
VERBOSE = 1  # verbose error reporting


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

    while 1:
        gid = codes_grib_new_from_file(f)
        if gid is None:
            break

        iterid *
 */

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

#include "eccodes.h"

#define MAX_KEY_LEN  255
#define MAX_VAL_LEN  1024

static void usage(char* progname);

int main(int argc, char *argv[])
{
    /* To skip read only and not coded keys
     unsigned long key_iterator_filter_flags=CODES_KEYS_ITERATOR_SKIP_READ_ONLY ||
     CODES_KEYS_ITERATOR_SKIP_COMPUTED;
     */
    unsigned long key_iterator_filter_flags=CODES_KEYS_ITERATOR_ALL_KEYS;

    /* Choose a namespace. E.g. "ls", "time", "parameter", "geography", "statistics" */
    char* name_space="ls";

    /* name_space=NULL to get all the keys */
    /* char* name_space=0; */

    FILE* f;
    codes_handle* h=NULL;

    int err=0;
    int msg_count=0;

    char value[MAX_VAL_LEN];
    size_t vlen=MAX_VAL_LEN;

    if (argc != 2) usage(argv[0]);

    f = fopen(argv[1],"r");
    if(!f) {
        perror(argv[1]);
        exit(1);
    }

    while((h = codes_handle_new_from_file(0,f,PRODUCT_GRIB,&err)) != NULL)
    {
        codes_keys_iterator* kiter=NULL;
        msg_count++;
        printf("-- GRIB N. %d --\n",msg_count);
        if(!h) {
            printf("ERROR: Unable to create grib handle\n");
            exit(1);
        }

        kiter=codes_keys_iterator_new(h,key_iterator_filter_flags,name_space);
        if (!kiter) {
            printf("ERROR: Unable to create keys iterator\n");
            exit(1);
        }

        while(codes_keys_iterator_next(kiter))
        {
            const char* name = codes_keys_iterator_get_name(kiter);new(gid, 'ls')

        # Different types  vlen=MAX_VAL_LEN;
   of keys can be skipped
        # bzero(value,vlen);codes_skip_computed(iterid)
            CODES_CHECK(# codes_getskip_string(h,name,value,&vlen),name);coded(iterid)
            printf("%s = %s\n",name,value);
# codes_skip_edition_specific(iterid)
        # codes_skip_duplicates(iterid)
        # codes_skip_read_only(iterid)
   /* Alternative way of getting the string value */# codes_skip_function(iterid)

            CODES_CHECK(while codes_keys_iterator_get_string(kiter, value, &vlen),0);next(iterid):
        }

    keyname   = codes_keys_iterator_get_deletename(kiteriterid);
            keyval = codes_handleget_delete(h);
string(gid, keyname)
         }

   print("%s return 0;

}

static void usage(char* progname)
{= %s" % (keyname, keyval))

    printf("\nUsage: %s grib_file\n",progname);    codes_keys_iterator_delete(iterid)
        codes_release(gid)

    exitf.close(1);

}

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
titlegrib_keys_iterator.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: grib_keys_iterator
 *
 * Description:
 * Example on how to use keys_iterator functions and the
 * codes_keys_iterator structure to get all the available
 * keys in a GRIB message.
 *
 */

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

#include "eccodes.h"

#define MAX_VAL_LEN  1024

static void usage(char* progname);

int main(int argc, char *argv[])
{
    /* To skip read only and computed keys
     unsigned long key_iterator_filter_flags=CODES_KEYS_ITERATOR_SKIP_READ_ONLY |
                            
Tabs Page
titleFortran 90
Code Block
languagenone
titlegrib_keys_iterator.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.
!
!
!  Description:
!       How to use keys_iterator to get all the available
!       keys in a message.
!
!
!
program keys_iterator
  use eccodes
  implicit none
  character(len=20)  :: name_space
  integer            :: kiter,ifile,igrib,iret
  character(len=256) :: key
  character(len=256) :: value
  character(len=512) :: all1
  integer            :: grib_count
  
  call codes_open_file(ifile, &
       '../../data/regular_latlon_surface.grib1','r')
  
  ! Loop on all the messages in a file.
  
  call codes_grib_new_from_file(ifile,igrib, iret)
  grib_count=0
  do while (iret /= CODES_END_OF_FILE)

    grib_count=grib_count+1
    write(*,*) '-- GRIB N. ',grib_count,' --'
     
    ! valid name_spaces are ls and mars
    name_space='ls'
     
    call codes_keys_iterator_new(igrib,kiter,name_space)
     
    do
      call codes_keys_iterator_next(kiter, iret) 
        
      if (iret .ne. CODES_SUCCESS) exit_KEYS_ITERATOR_SKIP_COMPUTED;
     */
   
 unsigned long key_iterator_filter_flags =  call codes_keys_iterator_get_name(kiter,key)CODES_KEYS_ITERATOR_ALL_KEYS |
      call codes_get(igrib,trim(key),value)
      all1=trim(key)// ' = ' // trim(value)
                write(*,*) trim(all1)
        
    end do
     
    call codesCODES_keysKEYS_iterator_delete(kiter)ITERATOR_SKIP_DUPLICATES;

    call codes_release(igrib)
    call codes_grib_new_from_file(ifile,igrib, iret)
  end do
  
  
  call codes_close_file(ifile)
  
end program keys_iterator

Tabs Page
titlePython
Code Block
languagepython
titlegrib_keys_iterator.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.
#

import traceback
import sys

from eccodes import *

INPUT='../../data/reduced_latlon_surface.grib1'
VERBOSE=1 # verbose error reporting

def example():
    f = open(INPUT)

    while 1:
        gid = codes_grib_new_from_file(f)
        if gid is None: break

/* Choose a namespace. E.g. "ls", "time", "parameter", "geography", "statistics" */
    const char* name_space="ls";

    /* name_space=NULL to get all the keys */
    /* char* name_space=0; */

    FILE* f = NULL;
    codes_handle* h=NULL;

    int err=0;
    int msg_count=0;

    char value[MAX_VAL_LEN];
    size_t vlen=MAX_VAL_LEN;

    if (argc != 2) usage(argv[0]);

    f = fopen(argv[1],"r");
    if(!f) {
        perror(argv[1]);
        exit(1);
    }

    while((h = codes_handle_new_from_file(0,f,PRODUCT_GRIB,&err)) != NULL)
    {
        codes_keys_iterator* kiter=NULL;
        msg_count++;
        printf("-- GRIB N. %d --\n",msg_count);
        if(!h) {
            printf("ERROR: Unable to create grib handle\n");
          iterid = codes_keys_iterator_new(gid,'ls')

 exit(1);
        # Different types of keys can be skipped}

        # codes_skip_computed(iterid)kiter=codes_keys_iterator_new(h,key_iterator_filter_flags,name_space);
        #if codes_skip_coded(iterid)
 (!kiter) {
       # codes_skip_edition_specific(iterid)
    printf("ERROR: Unable to create # codes_skip_duplicates(iterid)
keys iterator\n");
           # codes_skip_read_only(iterid) exit(1);
        # codes_skip_function(iterid)}

        while (codes_keys_iterator_next(iterid):
kiter))
        {
            const char* keynamename = codes_keys_iterator_get_name(iteridkiter);
            keyval vlen= codesMAX_get_string(iterid,keyname)
VAL_LEN;
            bzero(value,vlen);
        print "%s = %s" % (keyname,keyval)

 CODES_CHECK(codes_get_string(h,name,value,&vlen),name);
        codes_keys_iterator_delete(iterid)
    printf("%s    codes_release(gid)

= %s\n",name,value);

      f.close()

def main():
      try:
/* Alternative way of getting the string value example()
*/
        except CodesInternalError,err:
   CODES_CHECK(codes_keys_iterator_get_string(kiter, value, &vlen),0);
   if VERBOSE:
    }

        traceback.print_exc(file=sys.stderr)codes_keys_iterator_delete(kiter);
        else:codes_handle_delete(h);
    }
    fclose(f);
    print >>sys.stderr,err.msgreturn 0;
}

static void usage(char* progname)
{
     return 1

if __name__ == "__main__":printf("\nUsage: %s grib_file\n",progname);
    sys.exit(main())1);
}