Fieldset: refactoring

This commit is contained in:
Shahram Najm 2022-02-26 13:34:58 +00:00
parent 4535dad22e
commit ca293c1da9
3 changed files with 30 additions and 25 deletions

View File

@ -44,7 +44,7 @@ static grib_fieldset* grib_fieldset_create_from_order_by(grib_context* c, grib_o
static int grib_fieldset_resize(grib_fieldset* set, size_t newsize); static int grib_fieldset_resize(grib_fieldset* set, size_t newsize);
static void grib_trim(char** x); static void grib_trim(char** x);
static grib_order_by* grib_fieldset_new_order_by(grib_context* c, const char* z); static grib_order_by* grib_fieldset_new_order_by(grib_context* c, const char* z);
static void grib_fieldset_sort(grib_fieldset* set, int beg, int theEnd); static void grib_fieldset_sort(grib_fieldset* set, int theStart, int theEnd);
static grib_int_array* grib_fieldset_create_int_array(grib_context* c, size_t size); static grib_int_array* grib_fieldset_create_int_array(grib_context* c, size_t size);
static int grib_fieldset_resize_int_array(grib_int_array* a, size_t newsize); static int grib_fieldset_resize_int_array(grib_int_array* a, size_t newsize);
static void grib_fieldset_delete_int_array(grib_int_array* f); static void grib_fieldset_delete_int_array(grib_int_array* f);
@ -296,8 +296,13 @@ grib_fieldset* grib_fieldset_new_from_files(grib_context* c, char* filenames[],
} }
} }
if (where_string) if (where_string) {
grib_fieldset_apply_where(set, where_string); ret = grib_fieldset_apply_where(set, where_string);
if (ret != GRIB_SUCCESS) {
*err = ret;
return NULL;
}
}
if (order_by_string) { if (order_by_string) {
if (!set->order_by && ob) if (!set->order_by && ob)
@ -311,8 +316,7 @@ grib_fieldset* grib_fieldset_new_from_files(grib_context* c, char* filenames[],
return set; return set;
} }
static grib_fieldset* grib_fieldset_create_from_keys(grib_context* c, char** keys, int nkeys, static grib_fieldset* grib_fieldset_create_from_keys(grib_context* c, char** keys, int nkeys, int* err)
int* err)
{ {
grib_fieldset* set = NULL; grib_fieldset* set = NULL;
size_t msize = 0, size = 0; size_t msize = 0, size = 0;
@ -378,8 +382,7 @@ static grib_fieldset* grib_fieldset_create_from_keys(grib_context* c, char** key
return set; return set;
} }
static grib_fieldset* grib_fieldset_create_from_order_by(grib_context* c, grib_order_by* ob, static grib_fieldset* grib_fieldset_create_from_order_by(grib_context* c, grib_order_by* ob, int* err)
int* err)
{ {
char** keys = NULL; char** keys = NULL;
size_t nkeys = 0; size_t nkeys = 0;
@ -407,6 +410,7 @@ static grib_fieldset* grib_fieldset_create_from_order_by(grib_context* c, grib_o
return set; return set;
} }
/* Experimental: Needs more work */
int grib_fieldset_apply_where(grib_fieldset* set, const char* where_string) int grib_fieldset_apply_where(grib_fieldset* set, const char* where_string)
{ {
int err = GRIB_NOT_IMPLEMENTED; int err = GRIB_NOT_IMPLEMENTED;
@ -416,11 +420,12 @@ int grib_fieldset_apply_where(grib_fieldset* set, const char* where_string)
return GRIB_INVALID_ARGUMENT; return GRIB_INVALID_ARGUMENT;
m = grib_math_new(set->context, where_string, &err); m = grib_math_new(set->context, where_string, &err);
if (err || !m) return err;
print_math(m); print_math(m);
printf("\n"); printf("\n");
grib_math_delete(set->context, m); grib_math_delete(set->context, m);
return err; return GRIB_NOT_IMPLEMENTED;
} }
int grib_fieldset_apply_order_by(grib_fieldset* set, const char* order_by_string) int grib_fieldset_apply_order_by(grib_fieldset* set, const char* order_by_string)
@ -501,18 +506,18 @@ static int grib_fieldset_compare(grib_fieldset* set, int* i, int* j)
return ret; return ret;
} }
static void grib_fieldset_sort(grib_fieldset* set, int beg, int theEnd) static void grib_fieldset_sort(grib_fieldset* set, int theStart, int theEnd)
{ {
double temp; double temp;
int l = 0, r = 0; int l = 0, r = 0;
if (theEnd > beg) { if (theEnd > theStart) {
l = beg + 1; l = theStart + 1;
r = theEnd; r = theEnd;
while (l < r) { while (l < r) {
if (grib_fieldset_compare(set, &l, &beg) <= 0) { if (grib_fieldset_compare(set, &l, &theStart) <= 0) {
l++; l++;
} }
else if (grib_fieldset_compare(set, &r, &beg) >= 0) { else if (grib_fieldset_compare(set, &r, &theStart) >= 0) {
r--; r--;
} }
else { else {
@ -520,16 +525,16 @@ static void grib_fieldset_sort(grib_fieldset* set, int beg, int theEnd)
} }
} }
if (grib_fieldset_compare(set, &l, &beg) < 0) { if (grib_fieldset_compare(set, &l, &theStart) < 0) {
SWAP(set->order->el[l], set->order->el[beg]) SWAP(set->order->el[l], set->order->el[theStart])
l--; l--;
} }
else { else {
l--; l--;
SWAP(set->order->el[l], set->order->el[beg]) SWAP(set->order->el[l], set->order->el[theStart])
} }
grib_fieldset_sort(set, beg, l); grib_fieldset_sort(set, theStart, l);
grib_fieldset_sort(set, r, theEnd); grib_fieldset_sort(set, r, theEnd);
} }
} }
@ -738,7 +743,7 @@ grib_handle* grib_fieldset_next_handle(grib_fieldset* set, int* err)
return h; return h;
} }
int grib_fieldset_count(grib_fieldset* set) int grib_fieldset_count(const grib_fieldset* set)
{ {
return set->size; return set->size;
} }
@ -779,7 +784,6 @@ static grib_int_array* grib_fieldset_create_int_array(grib_context* c, size_t si
c = grib_context_get_default(); c = grib_context_get_default();
a = (grib_int_array*)grib_context_malloc_clear(c, sizeof(grib_int_array)); a = (grib_int_array*)grib_context_malloc_clear(c, sizeof(grib_int_array));
if (!a) { if (!a) {
grib_context_log(c, GRIB_LOG_ERROR, grib_context_log(c, GRIB_LOG_ERROR,
"grib_fieldset_create_int_array: Cannot malloc %ld bytes", "grib_fieldset_create_int_array: Cannot malloc %ld bytes",

View File

@ -445,9 +445,9 @@ void grib_math_delete(grib_context* c, grib_math* m)
grib_math* grib_math_new(grib_context* c, const char* formula, int* err) grib_math* grib_math_new(grib_context* c, const char* formula, int* err)
{ {
grib_math* x; grib_math* x = NULL;
char* f = 0; char* f = NULL;
char* fsave = 0; char* fsave = NULL;
*err = 0; *err = 0;
@ -467,6 +467,7 @@ grib_math* grib_math_new(grib_context* c, const char* formula, int* err)
if (*f) { if (*f) {
grib_context_log(c, GRIB_LOG_ERROR, grib_context_log(c, GRIB_LOG_ERROR,
"grib_math_new : Part of the formula was not processed: '%s'", f); "grib_math_new : Part of the formula was not processed: '%s'", f);
*err = GRIB_INVALID_ARGUMENT;
return NULL; return NULL;
} }

View File

@ -18,7 +18,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h>
#include "eccodes.h" #include "eccodes.h"
int grib_fieldset_apply_where(grib_fieldset* set, const char* where_string); /*experimental*/ int grib_fieldset_apply_where(grib_fieldset* set, const char* where_string); /*experimental*/
@ -75,8 +75,8 @@ int main(int argc, char** argv)
CODES_CHECK(err, 0); CODES_CHECK(err, 0);
/* grib_fieldset_apply_where not fully implemented*/ /* grib_fieldset_apply_where not fully implemented*/
err=grib_fieldset_apply_where(set, "(centre=='ecmf') && number==1 || step==6"); err = grib_fieldset_apply_where(set, "(centre=='ecmf') && number==1 || step==6");
CODES_CHECK(err, 0); assert(err == CODES_NOT_IMPLEMENTED);
printf("ordering by %s\n", order_by); printf("ordering by %s\n", order_by);
printf("%d fields in the fieldset\n", codes_fieldset_count(set)); printf("%d fields in the fieldset\n", codes_fieldset_count(set));