New function get_concept_condition_string and unit tests for it

This commit is contained in:
Shahram Najm 2019-12-01 16:13:38 +00:00
parent 58e7d58707
commit e9045909f3
4 changed files with 65 additions and 38 deletions

View File

@ -305,3 +305,45 @@ static grib_concept_value* get_concept(grib_handle* h, grib_action_concept* self
GRIB_MUTEX_UNLOCK(&mutex);
return result;
}
/* Caller has to allocate space for the result.
* Example: key='typeOfLevel' whose value is 'mixedLayerDepth',
* result='typeOfFirstFixedSurface=169,typeOfSecondFixedSurface=255'
*/
int get_concept_condition_string(grib_handle* h, const char* key, char* result)
{
int err = 0;
int length = 0;
char strVal[64]={0,};
size_t len = sizeof(strVal);
grib_concept_value* concept_value = NULL;
grib_accessor* acc = grib_find_accessor(h, key);
if (!acc) return GRIB_NOT_FOUND;
err = grib_get_string(h, key, strVal,&len);
if (err) return GRIB_INTERNAL_ERROR;
concept_value = action_concept_get_concept(acc);
while (concept_value) {
int err = 0;
long lres = 0;
grib_concept_condition* concept_condition = concept_value->conditions;
if (strcmp(strVal, concept_value->name)==0) {
while (concept_condition) {
grib_expression* expression = concept_condition->expression;
Assert(expression);
err = grib_expression_evaluate_long(h,expression,&lres);
if (err) return err;
length += sprintf(result+length, "%s%s=%ld",
(length==0?"":","),concept_condition->name, lres);
concept_condition = concept_condition->next;
}
}
concept_value = concept_value->next;
}
if (length == 0) return GRIB_CONCEPT_NO_MATCH;
return GRIB_SUCCESS;
}

View File

@ -67,6 +67,7 @@ grib_action *grib_action_create_when(grib_context *context, grib_expression *exp
grib_concept_value *action_concept_get_concept(grib_accessor *a);
int action_concept_get_nofail(grib_accessor *a);
grib_action *grib_action_create_concept(grib_context *context, const char *name, grib_concept_value *concept, const char *basename, const char *name_space, const char *defaultkey, const char *masterDir, const char *localDir, const char *ecmfDir, int flags, int nofail);
int get_concept_condition_string(grib_handle* h, const char* key, char* result);
/* action_class_hash_array.c */
grib_action *grib_action_create_hash_array(grib_context *context, const char *name, grib_hash_array_value *hash_array, const char *basename, const char *name_space, const char *defaultkey, const char *masterDir, const char *localDir, const char *ecmfDir, int flags, int nofail);

View File

@ -2108,44 +2108,6 @@ size_t sum_of_pl_array(const long* pl, size_t plsize)
return count;
}
static int get_concept_condition_string(grib_handle* h, const char* key, char* result)
{
int err = 0;
int length = 0;
char strVal[32]={0,};
size_t len = sizeof(strVal);
grib_concept_value* concept_value = NULL;
grib_accessor* acc = grib_find_accessor(h, key);
if (!acc) return GRIB_NOT_FOUND;
err = grib_get_string(h, key, strVal,&len);
if (err) return GRIB_INTERNAL_ERROR;
concept_value = action_concept_get_concept(acc);
while (concept_value) {
int err = 0;
long lres = 0;
grib_concept_condition* concept_condition = concept_value->conditions;
if (strcmp(strVal, concept_value->name)==0) {
while (concept_condition) {
grib_expression* expression = concept_condition->expression;
Assert(expression);
err = grib_expression_evaluate_long(h,expression,&lres);
if (err) return err;
length += sprintf(result+length, "%s%s=%ld",
(length==0?"":","),concept_condition->name, lres);
concept_condition = concept_condition->next;
}
}
concept_value = concept_value->next;
}
if (length == 0) return GRIB_CONCEPT_NO_MATCH;
return GRIB_SUCCESS;
}
int grib_util_grib_data_quality_check(grib_handle* h, double min_val, double max_val)
{
int err = 0;

View File

@ -1425,10 +1425,32 @@ static void test_assertion_catching()
assertion_caught = 0;
}
static void test_concept_condition_strings()
{
int err = 0;
char result[128] = {0,};
grib_handle* h = grib_handle_new_from_samples(0, "GRIB2");
err = get_concept_condition_string(h, "typeOfLevel", result);
assert ( !err );
assert( strcmp(result, "typeOfFirstFixedSurface=1,typeOfSecondFixedSurface=255")==0 );
err = get_concept_condition_string(h, "paramId", result);
assert ( !err );
assert( strcmp(result, "discipline=0,parameterCategory=0,parameterNumber=0")==0 );
err = get_concept_condition_string(h, "gridType", result);
assert ( !err );
/*printf("%s\n", result);*/
assert( strcmp(result, "gridDefinitionTemplateNumber=0,PLPresent=0")==0 );
}
int main(int argc, char** argv)
{
/*printf("Doing unit tests. ecCodes version = %ld\n", grib_get_api_version());*/
test_concept_condition_strings();
test_assertion_catching();
test_gaussian_latitude_640();