ECC-418: Fortran and Python codes_bufr_keys_iterator (Part 1: F90)

This commit is contained in:
Shahram Najm 2017-01-26 15:50:09 +00:00
parent f020620c1e
commit f3dfab8a75
9 changed files with 280 additions and 37 deletions

View File

@ -12,7 +12,7 @@
!
!
! Description: how to use keys_iterator functions and the
! codes_keys_iterator structure to get all the available
! codes_bufr_keys_iterator structure to get all the available
! keys in a BUFR message.
!
!
@ -23,49 +23,47 @@ integer :: ifile
integer :: iret
integer :: ibufr
integer :: count=0
character(len=20) :: name_space
character(len=256) :: key
integer :: kiter
call codes_open_file(ifile,'../../data/bufr/syno_1.bufr','r')
! name_space='' to get all the keys
name_space='ls'
! 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)
! Get and print some keys form the BUFR header
! Get and print some keys form the BUFR header
write(*,*) 'message: ',count
! Create key iterator
call codes_keys_iterator_new(ibufr,kiter,name_space,iret)
! We need to instruct ecCodes to expand all the descriptors
! i.e. unpack the data values
call codes_set(ibufr,"unpack",1);
! Create BUFR keys iterator
call codes_bufr_keys_iterator_new(ibufr,kiter,iret)
if (iret .ne. 0) then
write(*,*) 'ERROR: Unable to create keys iterator'
write(*,*) 'ERROR: Unable to create BUFR keys iterator'
call exit(1)
end if
end if
! Get first key
call codes_keys_iterator_next(kiter, iret)
call codes_bufr_keys_iterator_next(kiter, iret)
! Loop over keys
do while (iret == CODES_SUCCESS)
do while (iret == CODES_SUCCESS)
! Print key name
call codes_keys_iterator_get_name(kiter,key)
call codes_bufr_keys_iterator_get_name(kiter,key)
write(*,*) ' ',trim(key)
! Get next key
call codes_keys_iterator_next(kiter, iret)
call codes_bufr_keys_iterator_next(kiter, iret)
end do
! Delete key iterator
call codes_keys_iterator_delete(kiter)
! Delete key iterator
call codes_bufr_keys_iterator_delete(kiter)
! Release the bufr message
call codes_release(ibufr)
@ -77,7 +75,7 @@ integer :: kiter
end do
! Close file
! Close file
call codes_close_file(ifile)
end program bufr_keys_iterator

View File

@ -22,7 +22,7 @@ REDIRECT=/dev/null
f=${data_dir}/bufr/syno_1.bufr
#The input ($f) is hardcoded in the f90 example!!!
${examples_dir}/eccodes_f_bufr_keys_iterator 2> $REDIRECT > $fTmp
${examples_dir}/eccodes_f_bufr_keys_iterator #2> $REDIRECT > $fTmp
#TODO: check the output

View File

@ -1327,6 +1327,121 @@ subroutine codes_keys_iterator_rewind ( iterid, status )
call grib_keys_iterator_rewind ( iterid, status )
end subroutine codes_keys_iterator_rewind
! BUFR keys iterator
! -----------------------
!> Create a new iterator on the keys of a BUFR message.
!>
!> The keys iterator can be navigated to give all the key names which
!> can then be used to get or set the key values with \ref codes_get or
!> \ref codes_set.
!>
!> In case of error, if the status parameter (optional) is not given, the program will
!> exit with an error message.\n Otherwise the error message can be
!> gathered with @ref codes_get_error_string.
!>
!> @param msgid id of the BUFR message loaded in memory
!> @param iterid keys iterator id to be used in the keys iterator functions
!> @param status CODES_SUCCESS if OK, integer value on error
subroutine codes_bufr_keys_iterator_new ( msgid, iterid, status )
integer(kind=kindOfInt), intent(in) :: msgid
integer(kind=kindOfInt), intent(inout) :: iterid
integer(kind=kindOfInt),optional, intent(out) :: status
integer(kind=kindOfInt) :: iret
iret = codes_f_bufr_keys_iterator_new(msgid, iterid)
if (present(status)) then
status = iret
else
call grib_check(iret,'bufr_keys_iterator_new','')
endif
end subroutine codes_bufr_keys_iterator_new
!> Advance to the next BUFR keys iterator value.
!>
!> @param iterid keys iterator id created with @ref codes_bufr_keys_iterator_new
!> @param status CODES_SUCCESS if next iterator exists, integer value if no more elements to iterate on
subroutine codes_bufr_keys_iterator_next (iterid , status)
integer(kind=kindOfInt), intent(in) :: iterid
integer(kind=kindOfInt),optional, intent(out) :: status
integer(kind=kindOfInt) :: iret
status = GRIB_SUCCESS
iret = codes_f_bufr_keys_iterator_next( iterid )
if (iret == 0) then
! no more elements
status = GRIB_END
endif
end subroutine codes_bufr_keys_iterator_next
!> Get the name of a key from a BUFR keys iterator.
!>
!> If the status parameter (optional) is not given the program will exit with an error message\n
!> otherwise the error message can be gathered with @ref codes_get_error_string.\n
!>
!> @param iterid keys iterator id created with @ref codes_bufr_keys_iterator_new
!> @param name key name to be retrieved
!> @param status CODES_SUCCESS if OK, integer value on error
subroutine codes_bufr_keys_iterator_get_name( iterid, name, status )
integer(kind=kindOfInt), intent(in) :: iterid
character(LEN=*), intent(out) :: name
integer(kind=kindOfInt),optional, intent(out) :: status
integer(kind=kindOfInt) :: iret
iret = codes_f_bufr_keys_iterator_get_name( iterid, name )
if (present(status)) then
status = iret
else
call grib_check(iret,'bufr_keys_iterator_get_name',name)
endif
end subroutine codes_bufr_keys_iterator_get_name
!> Rewind a BUFR keys iterator.
!>
!> In case of error, if the status parameter (optional) is not given, the program will
!> exit with an error message.\n Otherwise the error message can be
!> gathered with @ref grib_get_error_string.
!>
!> @param iterid keys iterator id created with @ref codes_bufr_keys_iterator_new
!> @param status GRIB_SUCCESS if OK, integer value on error
subroutine codes_bufr_keys_iterator_rewind( iterid, status )
integer(kind=kindOfInt), intent(in) :: iterid
integer(kind=kindOfInt),optional, intent(out) :: status
integer(kind=kindOfInt) :: iret
iret = codes_f_bufr_keys_iterator_rewind( iterid )
if (present(status)) then
status = iret
else
call grib_check(iret,'bufr_keys_iterator_rewind','')
endif
end subroutine codes_bufr_keys_iterator_rewind
!> Delete a BUFR keys iterator and free memory.
!>
!> In case of error, if the status parameter (optional) is not given, the program will
!> exit with an error message.\n Otherwise the error message can be
!> gathered with @ref grib_get_error_string.
!>
!> @param iterid keys iterator id created with @ref codes_bufr_keys_iterator_new
!> @param status GRIB_SUCCESS if OK, integer value on error
subroutine codes_bufr_keys_iterator_delete (iterid , status)
integer(kind=kindOfInt), intent(in) :: iterid
integer(kind=kindOfInt),optional, intent(out) :: status
integer(kind=kindOfInt) :: iret
iret = codes_f_bufr_keys_iterator_delete(iterid)
if (present(status)) then
status = iret
else
call grib_check(iret,'bufr_keys_iterator_delete','')
endif
end subroutine codes_bufr_keys_iterator_delete
!> Dump the content of a message.
!>
!> In case of error, if the status parameter (optional) is not given, the program will

View File

@ -10,6 +10,13 @@ public :: codes_skip_computed, &
codes_skip_read_only
public :: codes_keys_iterator_get_name, &
codes_keys_iterator_rewind
public :: codes_bufr_keys_iterator_new, &
codes_bufr_keys_iterator_next, &
codes_bufr_keys_iterator_get_name, &
codes_bufr_keys_iterator_rewind, &
codes_bufr_keys_iterator_delete
public :: codes_new_from_message, &
codes_grib_new_from_samples, codes_new_from_file, &
codes_bufr_new_from_samples, &

View File

@ -4,6 +4,13 @@ integer, external :: grib_f_multi_support_on, grib_f_multi_support_off
integer, external :: grib_f_keys_iterator_new, &
grib_f_keys_iterator_next, &
grib_f_keys_iterator_delete
integer, external :: codes_f_bufr_keys_iterator_new, &
codes_f_bufr_keys_iterator_next, &
codes_f_bufr_keys_iterator_get_name, &
codes_f_bufr_keys_iterator_rewind, &
codes_f_bufr_keys_iterator_delete
integer, external :: grib_f_skip_computed, &
grib_f_skip_coded, &
grib_f_skip_edition_specific, &

View File

@ -763,7 +763,6 @@ static int clear_iterator(int iterator_id)
static int _clear_keys_iterator(int keys_iterator_id)
{
l_grib_keys_iterator* current = keys_iterator_set;
while(current){
@ -1093,7 +1092,6 @@ int grib_f_keys_iterator_next_(int* iterid) {
return grib_keys_iterator_next(iter);
}
int grib_f_keys_iterator_next__(int* iterid) {
return grib_f_keys_iterator_next_(iterid);
}
@ -1101,6 +1099,7 @@ int grib_f_keys_iterator_next(int* iterid) {
return grib_f_keys_iterator_next_(iterid);
}
/*****************************************************************************/
int grib_f_keys_iterator_delete_(int* iterid) {
return clear_keys_iterator(*iterid);
}
@ -1253,6 +1252,105 @@ int grib_f_keys_iterator_rewind(int* kiter) {
return grib_f_keys_iterator_rewind_(kiter);
}
/*BUFR keys iterator*/
/*****************************************************************************/
static int _codes_f_bufr_keys_iterator_new_(int* gid,int* iterid) {
int err=0;
grib_handle* h;
grib_keys_iterator* iter;
h=get_handle(*gid);
if (!h) {
*iterid=-1;
return GRIB_NULL_HANDLE;
}
iter=codes_bufr_keys_iterator_new(h);
if (iter)
*iterid=push_keys_iterator(iter);
else
*iterid=-1;
return err;
}
int codes_f_bufr_keys_iterator_new_(int* gid,int* iterid) {
int ret=0;
GRIB_MUTEX_INIT_ONCE(&once,&init)
GRIB_MUTEX_LOCK(&keys_iterator_mutex)
ret=_codes_f_bufr_keys_iterator_new_(gid,iterid);
GRIB_MUTEX_UNLOCK(&keys_iterator_mutex)
return ret;
}
int codes_f_bufr_keys_iterator_new__(int* gid,int* iterid) {
return codes_f_bufr_keys_iterator_new_(gid,iterid);
}
int codes_f_bufr_keys_iterator_new(int* gid,int* iterid) {
return codes_f_bufr_keys_iterator_new_(gid,iterid);
}
/*****************************************************************************/
int codes_f_bufr_keys_iterator_next_(int* iterid) {
grib_keys_iterator* iter=get_keys_iterator(*iterid);
if (!iter) return GRIB_INVALID_KEYS_ITERATOR;
return codes_bufr_keys_iterator_next(iter);
}
int codes_f_bufr_keys_iterator_next__(int* iterid) {
return codes_f_bufr_keys_iterator_next_(iterid);
}
int codes_f_bufr_keys_iterator_next(int* iterid) {
return codes_f_bufr_keys_iterator_next_(iterid);
}
/*****************************************************************************/
int codes_f_bufr_keys_iterator_get_name_(int* iterid,char* name,int len) {
size_t lsize=len;
char buf[1024]={0,};
grib_keys_iterator* kiter=get_keys_iterator(*iterid);
if (!kiter) return GRIB_INVALID_KEYS_ITERATOR;
fort_char_clean(name,len);
sprintf(buf,"%s",codes_bufr_keys_iterator_get_name(kiter));
lsize=strlen(buf);
if (len < lsize) return GRIB_ARRAY_TOO_SMALL;
memcpy(name,buf,lsize);
czstr_to_fortran(name,len);
return 0;
}
int codes_f_bufr_keys_iterator_get_name__(int* kiter,char* name,int len) {
return codes_f_bufr_keys_iterator_get_name_(kiter,name,len);
}
int codes_f_bufr_keys_iterator_get_name(int* kiter,char* name,int len) {
return codes_f_bufr_keys_iterator_get_name_(kiter,name,len);
}
/*****************************************************************************/
int codes_f_bufr_keys_iterator_rewind_(int* kiter) {
grib_keys_iterator* i=get_keys_iterator(*kiter);
if (!i) return GRIB_INVALID_KEYS_ITERATOR;
return codes_bufr_keys_iterator_rewind(i);
}
int codes_f_bufr_keys_iterator_rewind__(int* kiter) {
return codes_f_bufr_keys_iterator_rewind_(kiter);
}
int codes_f_bufr_keys_iterator_rewind(int* kiter) {
return codes_f_bufr_keys_iterator_rewind_(kiter);
}
/*****************************************************************************/
int codes_f_bufr_keys_iterator_delete_(int* iterid) {
return clear_keys_iterator(*iterid);
}
int codes_f_bufr_keys_iterator_delete__(int* iterid) {
return codes_f_bufr_keys_iterator_delete_(iterid);
}
int codes_f_bufr_keys_iterator_delete(int* iterid) {
return codes_f_bufr_keys_iterator_delete_(iterid);
}
/*****************************************************************************/
int grib_f_new_from_message_(int* gid, void* buffer , size_t* bufsize){
grib_handle *h = NULL;
@ -1284,7 +1382,6 @@ int grib_f_new_from_message_copy_(int* gid, void* buffer , size_t* bufsize){
*gid = -1;
return GRIB_INTERNAL_ERROR;
}
int grib_f_new_from_message_copy__(int* gid, void* buffer , size_t* bufsize){
return grib_f_new_from_message_copy_(gid, buffer , bufsize);
}
@ -1308,7 +1405,6 @@ int grib_f_new_from_samples_(int* gid, char* name , int lname){
*gid = -1;
return GRIB_FILE_NOT_FOUND;
}
int grib_f_new_from_samples__(int* gid, char* name , int lname){
return grib_f_new_from_samples_( gid, name , lname);
}
@ -1317,7 +1413,7 @@ int grib_f_new_from_samples(int* gid, char* name , int lname){
}
/*****************************************************************************/
int codes_bufr_f_new_from_samples_(int* gid, char* name , int lname){
int codes_bufr_f_new_from_samples_(int* gid, char* name, int lname){
char fname[1024];
grib_handle *h = NULL;
@ -1332,12 +1428,11 @@ int codes_bufr_f_new_from_samples_(int* gid, char* name , int lname){
*gid = -1;
return GRIB_FILE_NOT_FOUND;
}
int codes_bufr_f_new_from_samples__(int* gid, char* name , int lname){
return codes_bufr_f_new_from_samples_( gid, name , lname);
int codes_bufr_f_new_from_samples__(int* gid, char* name, int lname){
return codes_bufr_f_new_from_samples_( gid, name, lname);
}
int codes_bufr_f_new_from_samples(int* gid, char* name , int lname){
return codes_bufr_f_new_from_samples_( gid, name , lname);
int codes_bufr_f_new_from_samples(int* gid, char* name, int lname){
return codes_bufr_f_new_from_samples_( gid, name, lname);
}
/*****************************************************************************/

View File

@ -49,6 +49,7 @@ int grib_f_iterator_next(int *iterid, double *lat, double *lon, double *value);
int grib_f_iterator_delete_(int *iterid);
int grib_f_iterator_delete__(int *iterid);
int grib_f_iterator_delete(int *iterid);
int grib_f_keys_iterator_new_(int *gid, int *iterid, char *name_space, int len);
int grib_f_keys_iterator_new__(int *gid, int *iterid, char *name_space, int len);
int grib_f_keys_iterator_new(int *gid, int *iterid, char *name_space, int len);
@ -58,6 +59,24 @@ int grib_f_keys_iterator_next(int *iterid);
int grib_f_keys_iterator_delete_(int *iterid);
int grib_f_keys_iterator_delete__(int *iterid);
int grib_f_keys_iterator_delete(int *iterid);
/*BUFR keys iterator*/
int codes_f_bufr_keys_iterator_new_(int *gid, int *iterid);
int codes_f_bufr_keys_iterator_new__(int *gid, int *iterid);
int codes_f_bufr_keys_iterator_new(int *gid, int *iterid);
int codes_f_bufr_keys_iterator_next_(int *iterid);
int codes_f_bufr_keys_iterator_next__(int *iterid);
int codes_f_bufr_keys_iterator_next(int *iterid);
int codes_f_bufr_keys_iterator_get_name_(int *iterid, char *name, int len);
int codes_f_bufr_keys_iterator_get_name__(int *kiter, char *name, int len);
int codes_f_bufr_keys_iterator_get_name(int *kiter, char *name, int len);
int codes_f_bufr_keys_iterator_rewind_(int *kiter);
int codes_f_bufr_keys_iterator_rewind__(int *kiter);
int codes_f_bufr_keys_iterator_rewind(int *kiter);
int codes_f_bufr_keys_iterator_delete_(int *iterid);
int codes_f_bufr_keys_iterator_delete__(int *iterid);
int codes_f_bufr_keys_iterator_delete(int *iterid);
int grib_f_gribex_mode_on_(void);
int grib_f_gribex_mode_on__(void);
int grib_f_gribex_mode_on(void);

View File

@ -1116,7 +1116,7 @@ char **codes_bufr_copy_data_return_copied_keys(grib_handle *hin, grib_handle *ho
int codes_bufr_copy_data(grib_handle* hin, grib_handle* hout);
/*! Step to the next iterator.
/*! Step to the next item from the keys iterator.
* @param kiter : valid codes_keys_iterator
* @return 1 if next iterator exists, 0 if no more elements to iterate on
*/
@ -1124,23 +1124,25 @@ int codes_keys_iterator_next(codes_keys_iterator *kiter);
int codes_bufr_keys_iterator_next(codes_keys_iterator *kiter);
/*! get the key name from the iterator
/*! get the key name from the keys iterator
* @param kiter : valid codes_keys_iterator
* @return key name
*/
const char* codes_keys_iterator_get_name(codes_keys_iterator *kiter);
/*! Delete the iterator.
/*! Delete the keys iterator.
* @param kiter : valid codes_keys_iterator
* @return 0 if OK, integer value on error
*/
int codes_keys_iterator_delete( codes_keys_iterator* kiter);
int codes_bufr_keys_iterator_delete( codes_keys_iterator* kiter);
/*! Rewind the iterator.
/*! Rewind the keys iterator.
* @param kiter : valid codes_keys_iterator
* @return 0 if OK, integer value on error
*/
int codes_keys_iterator_rewind(codes_keys_iterator* kiter);
int codes_bufr_keys_iterator_rewind(codes_keys_iterator* kiter);
char* codes_bufr_keys_iterator_get_name(codes_keys_iterator* kiter);

View File

@ -43,7 +43,7 @@ int main(int argc,char* argv[])
printf("%s\n", kname);
grib_context_free(c, kname);
}
grib_keys_iterator_delete(kiter);
codes_bufr_keys_iterator_delete(kiter);
codes_handle_delete(h);
return 0;
}