Add function codes_key_is_computed

This commit is contained in:
shahramn 2024-12-05 20:50:54 +01:00
parent 7c9f046531
commit dd6bdb950d
12 changed files with 131 additions and 3 deletions

View File

@ -18,6 +18,7 @@ if( HAVE_BUILD_TOOLS )
bufr_ecc-1284 bufr_ecc-1284
bufr_ecc-1019 bufr_ecc-1019
get_native_type get_native_type
key_is_computed
grib_ecc-671 ) grib_ecc-671 )
list( APPEND tests_extra list( APPEND tests_extra
grib_index grib_index
@ -81,6 +82,7 @@ else()
codes_set_paths codes_set_paths
codes_f90_misc codes_f90_misc
get_native_type get_native_type
key_is_computed
grib_ecc-671 ) grib_ecc-671 )
list( APPEND tests_extra list( APPEND tests_extra
grib_index grib_index

View File

@ -0,0 +1,38 @@
! (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/LICENSECODES_INTERNAL_ERROR.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.
!
program key_is_computed
use eccodes
implicit none
integer :: ihandle, is_computed=0
CALL codes_grib_new_from_samples(ihandle, "regular_ll_sfc_grib2")
CALL codes_key_is_computed(ihandle, 'edition', is_computed)
IF (is_computed /= 0) THEN
call codes_check(CODES_INTERNAL_ERROR, 'Error', 'key edition is coded')
END IF
CALL codes_key_is_computed(ihandle, 'longitudeOfLastGridPoint', is_computed)
IF (is_computed /= 0) THEN
call codes_check(CODES_INTERNAL_ERROR, 'Error', 'key longitudeOfLastGridPoint is coded')
END IF
CALL codes_key_is_computed(ihandle, 'longitudeOfLastGridPointInDegrees', is_computed)
IF (is_computed /= 1) THEN
call codes_check(CODES_INTERNAL_ERROR, 'Error', 'key longitudeOfLastGridPointInDegrees is computed')
END IF
CALL codes_key_is_computed(ihandle, 'gridType', is_computed)
IF (is_computed /= 1) THEN
call codes_check(CODES_INTERNAL_ERROR, 'Error', 'key gridType is computed')
END IF
CALL codes_release(ihandle)
end program key_is_computed

16
examples/F90/key_is_computed.sh Executable file
View File

@ -0,0 +1,16 @@
#!/bin/sh
# (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.
#
. ./include.ctest.sh
label="f_key_is_computed"
${examples_dir}/eccodes_f_key_is_computed

View File

@ -1710,6 +1710,36 @@
call grib_is_defined(msgid, key, is_defined, status) call grib_is_defined(msgid, key, is_defined, status)
end subroutine codes_is_defined end subroutine codes_is_defined
!!!
!> Check if a key is computed(virtual) or coded
!>
!> 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 message loaded in memory
!> @param key key name
!> @param is_computed 0->coded, 1->computed
!> @param status CODES_SUCCESS if OK, integer value on error
subroutine codes_key_is_computed(msgid, key, is_computed, status)
integer(kind=kindOfInt), intent(in) :: msgid
character(len=*), intent(in) :: key
integer(kind=kindOfInt), intent(out) :: is_computed
integer(kind=kindOfInt), optional, intent(out) :: status
integer(kind=kindOfInt) :: iret
iret = grib_f_key_is_computed(msgid, key, is_computed)
if (iret /= 0) then
call grib_f_write_on_fail(msgid)
end if
if (present(status)) then
status = iret
else
call grib_check(iret, 'key_is_computed', key)
end if
end subroutine codes_key_is_computed
!!!
!> Get the real(4) value of a key from a message. !> Get the real(4) value of a key from a message.
!> !>
!> In case of error, if the status parameter (optional) is not given, the program will !> In case of error, if the status parameter (optional) is not given, the program will

View File

@ -1,4 +1,4 @@
public :: codes_get, codes_set, codes_set_force, codes_grib_get_data, codes_is_missing, codes_is_defined public :: codes_get, codes_set, codes_set_force, codes_grib_get_data, codes_is_missing, codes_is_defined, codes_key_is_computed
public :: codes_open_file, codes_close_file,codes_read_bytes,codes_write_bytes public :: codes_open_file, codes_close_file,codes_read_bytes,codes_write_bytes
public :: codes_grib_multi_support_on, codes_grib_multi_support_off public :: codes_grib_multi_support_on, codes_grib_multi_support_off
public :: codes_keys_iterator_new, & public :: codes_keys_iterator_new, &

View File

@ -48,7 +48,7 @@ integer, external :: grib_f_get_int, grib_f_get_long,grib_f_get_int_array, &
grib_f_get_real4_elements, grib_f_get_real8_elements, & grib_f_get_real4_elements, grib_f_get_real8_elements, &
grib_f_get_string,grib_f_get_string_array, & grib_f_get_string,grib_f_get_string_array, &
codes_f_bufr_copy_data, & codes_f_bufr_copy_data, &
grib_f_is_missing,grib_f_is_defined grib_f_is_missing, grib_f_is_defined, grib_f_key_is_computed
integer, external :: grib_f_new_from_index, & integer, external :: grib_f_new_from_index, &
grib_f_index_new_from_file, & grib_f_index_new_from_file, &
grib_f_index_add_file, & grib_f_index_add_file, &

View File

@ -2223,7 +2223,7 @@ int grib_f_is_missing_(int* gid, char* key,int* isMissing,int len)
int err=0; int err=0;
grib_handle *h = get_handle(*gid); grib_handle *h = get_handle(*gid);
char buf[1024]; char buf[1024];
if(!h) return GRIB_INVALID_GRIB; if (!h) return GRIB_INVALID_GRIB;
*isMissing=grib_is_missing(h, cast_char(buf,key,len),&err); *isMissing=grib_is_missing(h, cast_char(buf,key,len),&err);
return err; return err;
@ -2240,6 +2240,18 @@ int grib_f_is_defined_(int* gid, char* key,int* isDefined,int len)
return GRIB_SUCCESS; return GRIB_SUCCESS;
} }
/*****************************************************************************/
int grib_f_key_is_computed_(int* gid, char* key,int* isComputed,int len)
{
int err = 0;
grib_handle *h = get_handle(*gid);
char buf[1024];
if (!h) return GRIB_INVALID_GRIB;
*isComputed = codes_key_is_computed(h, cast_char(buf,key,len), &err);
return err;
}
/*****************************************************************************/ /*****************************************************************************/
int grib_f_set_real4_(int* gid, char* key, float* val, int len) int grib_f_set_real4_(int* gid, char* key, float* val, int len)
{ {

View File

@ -327,6 +327,7 @@ int grib_f_datetime_to_julian_(long* year,long* month,long* day, long* hour,long
int grib_f_set_string_array_(int* gid, char* key, char* val,int* nvals,int* slen, int len); int grib_f_set_string_array_(int* gid, char* key, char* val,int* nvals,int* slen, int len);
int codes_f_bufr_keys_iterator_get_name_(int* iterid, char* name, int len); int codes_f_bufr_keys_iterator_get_name_(int* iterid, char* name, int len);
int codes_f_bufr_multi_element_constant_arrays_off_(); int codes_f_bufr_multi_element_constant_arrays_off_();
int grib_f_key_is_computed_(int *gid, char *key, int *isComputed, int len);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -545,3 +545,15 @@ int codes_get_features(char* result, size_t* length, int select)
*length = actual_length; *length = actual_length;
return GRIB_SUCCESS; return GRIB_SUCCESS;
} }
// Returns 1 if the key is computed (virtual) and 0 if it is coded
int codes_key_is_computed(const grib_handle* h, const char* key, int* err)
{
const grib_accessor* acc = grib_find_accessor(h, key);
if (!acc) {
*err = GRIB_NOT_FOUND;
return 0;
}
*err = GRIB_SUCCESS;
return (acc->length_ == 0);
}

View File

@ -1339,6 +1339,9 @@ int codes_is_missing(const codes_handle* h, const char* key, int* err);
Returns a bool i.e. 0 or 1 */ Returns a bool i.e. 0 or 1 */
int codes_is_defined(const codes_handle* h, const char* key); int codes_is_defined(const codes_handle* h, const char* key);
/* Returns 1 if the key is computed (virtual) and 0 if it is coded */
int codes_key_is_computed(const grib_handle* h, const char* key, int* err);
/* Returns 1 if the BUFR key is in the header and 0 if it is in the data section. /* Returns 1 if the BUFR key is in the header and 0 if it is in the data section.
The error code is the final argument */ The error code is the final argument */
int codes_bufr_key_is_header(const codes_handle* h, const char* key, int* err); int codes_bufr_key_is_header(const codes_handle* h, const char* key, int* err);

View File

@ -847,6 +847,7 @@ bool is_sorted_descending(const double arr[], size_t n);
int compute_scaled_value_and_scale_factor(double input, int64_t scaled_value_max, int64_t scale_factor_max, int64_t* ret_value, int64_t* ret_factor); int compute_scaled_value_and_scale_factor(double input, int64_t scaled_value_max, int64_t scale_factor_max, int64_t* ret_value, int64_t* ret_factor);
int codes_is_feature_enabled(const char* feature); int codes_is_feature_enabled(const char* feature);
int codes_get_features(char* result, size_t* length, int select); int codes_get_features(char* result, size_t* length, int select);
int codes_key_is_computed(const grib_handle* h, const char* key, int* err);
/* grib_util.cc */ /* grib_util.cc */

View File

@ -65,6 +65,19 @@ int main(int argc, char* argv[])
int e = grib_keys_iterator_get_long(kiter, &lVal, &llen); int e = grib_keys_iterator_get_long(kiter, &lVal, &llen);
Assert(!e); Assert(!e);
Assert(lVal == 1 || lVal == 2); Assert(lVal == 1 || lVal == 2);
Assert(codes_key_is_computed(h, name, &e) == 0 && !e);
}
if (STR_EQUAL(name, "gridType")) {
int e = 0;
Assert(codes_key_is_computed(h, name, &e) == 1 && !e);
}
if (STR_EQUAL(name, "longitudeOfLastGridPointInDegrees")) {
int e = 0;
Assert(codes_key_is_computed(h, name, &e) == 1 && !e);
}
if (STR_EQUAL(name, "longitudeOfLastGridPoint")) {
int e = 0;
Assert(codes_key_is_computed(h, name, &e) == 0 && !e);
} }
} }