Commit 77e3adce authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

Replaced gridInqNumber()/gridDefNumber() by cdiInqKeyInt()/cdiDefKeyInt() with...

Replaced gridInqNumber()/gridDefNumber() by cdiInqKeyInt()/cdiDefKeyInt() with CDI_KEY_NUMBEROFGRIDUSED.
parent b2ed2be6
......@@ -11,6 +11,8 @@ gridInqYname(gridID, yname) -> cdiInqKeyString(gridID, CDI_YAXIS, CDI_KE
gridInqYlongname(gridID, ylongname) -> cdiInqKeyString(gridID, CDI_YAXIS, CDI_KEY_LONGNAME, ylongname, &length)
gridInqYunits(gridID, yunits) -> cdiInqKeyString(gridID, CDI_YAXIS, CDI_KEY_UNITS, yunits, &length)
gridDefNumber(zaxisID, number) -> cdiDefKeyInt(gridID, CDI_GLOBAL, CDI_KEY_NUMBEROFGRIDUSED, number)
gridInqNumber(zaxisID) -> cdiInqKeyInt(gridID, CDI_GLOBAL, CDI_KEY_NUMBEROFGRIDUSED, &number)
gridDefUUID(zaxisID, uuidOfHGrid) -> cdiDefKeyBytes(gridID, CDI_GLOBAL, CDI_KEY_UUID, uuidOfHGrid, length)
gridInqUUID(zaxisID) -> cdiInqKeyBytes(gridID, CDI_GLOBAL, CDI_KEY_UUID, uuidOfHGrid, &length)
......
......@@ -828,6 +828,8 @@ size_t gridInqYCvals(int gridID, char *ycvals[]);
#define CDI_KEY_UUID 960 // UUID for grid/Z-axis reference
#define CDI_KEY_NUMBEROFVGRIDUSED 961 // GRIB2 numberOfVGridUsed
#define CDI_KEY_NLEV 962 // GRIB2 nlev
#define CDI_KEY_NUMBEROFGRIDUSED 963 // GRIB2 numberOfGridUsed
#define CDI_KEY_NUMBEROFGRIDINREFERENCE 964 // GRIB2 numberOfGridInReference
// cdiDefKeyInt: Define an integer value from a key of a CDI variable
int cdiDefKeyInt(int cdiID, int varID, int key, int value);
......
......@@ -234,22 +234,28 @@ int cdiCopyKeys(int cdiID1, int varID1, int cdiID2, int varID2)
}
int cdiCopyKey(int cdiID1, int varID1, int key, int cdiID2)
int cdiCopyVarKey(const cdi_keys_t *keysp1, int key, cdi_keys_t *keysp2)
{
int status = CDI_NOERR;
const cdi_key_t *keyp = find_key_const (keysp1, key);
if (keyp == NULL) return -1;
cdi_define_key(keyp, keysp2);
return status;
}
int cdiCopyKey(int cdiID1, int varID1, int key, int cdiID2)
{
cdi_keys_t *keysp1 = cdi_get_keysp(cdiID1, varID1);
xassert(keysp1 != NULL);
cdi_keys_t *keysp2 = cdi_get_keysp(cdiID2, varID1);
xassert(keysp2 != NULL);
cdi_key_t *keyp = find_key(keysp1, key);
if (keyp == NULL) return -1;
cdi_define_key(keyp, keysp2);
return status;
return cdiCopyVarKey(keysp1, key, keysp2);
}
......@@ -341,6 +347,17 @@ int cdiInqKeyInt(int cdiID, int varID, int key, int *value)
}
int cdiInqVarKeyInt(const cdi_keys_t *keysp, int key)
{
int value = 0;
const cdi_key_t *keyp = find_key_const(keysp, key);
if (keyp && keyp->type == KEY_INT ) value = keyp->v.i;
return value;
}
void cdiDefVarKeyFloat(cdi_keys_t *keysp, int key, double value)
{
cdi_key_t *keyp = find_key(keysp, key);
......
......@@ -27,22 +27,12 @@ enum {KEY_INT = 1, KEY_FLOAT, KEY_BYTES};
void cdiDefVarKeyInt(cdi_keys_t *keysp, int key, int value);
void cdiDefVarKeyFloat(cdi_keys_t *keysp, int key, double value);
void cdiDefVarKeyBytes(cdi_keys_t *keysp, int key, const unsigned char *bytes, int length);
int cdiInqVarKeyInt(const cdi_keys_t *keysp, int key);
int cdiInqVarKeyBytes(const cdi_keys_t *keysp, int key, unsigned char *bytes, int *length);
cdi_key_t *find_key(cdi_keys_t *keysp, int key);
const char *cdiInqVarKeyStringPtr(cdi_keys_t *keysp, int key);
static inline
int cdiInqVarKeyInt(cdi_keys_t *keysp, int key)
{
int value = 0;
cdi_key_t *keyp = find_key(keysp, key);
if (keyp && keyp->type == KEY_INT ) value = keyp->v.i;
return value;
}
static inline
const char *cdiInqVarKeyString(cdi_keys_t *keysp, int key)
{
......@@ -51,6 +41,7 @@ const char *cdiInqVarKeyString(cdi_keys_t *keysp, int key)
return string;
}
int cdiCopyVarKey(const cdi_keys_t *keysp1, int key, cdi_keys_t *keysp2);
void cdiCopyVarKeys(const cdi_keys_t *keysp1, cdi_keys_t *keysp2);
void cdiDeleteVarKeys(cdi_keys_t *keysp);
void cdiDeleteKeys(int cdiID, int varID);
......
......@@ -743,7 +743,7 @@ void gribapiGetGridUnstructured(grib_handle *gh, grid_t *grid, size_t numberOfPo
long lpar;
if ( grib_get_long(gh, "numberOfGridUsed", &lpar) == 0 )
{
grid->number = (int)lpar;
cdiDefVarKeyInt(&grid->keys, CDI_KEY_NUMBEROFGRIDUSED, (int)lpar);
if ( grib_get_long(gh, "numberOfGridInReference", &lpar) == 0 )
grid->position = (int)lpar;
/*
......
......@@ -154,7 +154,6 @@ void grid_init(grid_t *gridptr)
gridptr->trunc = 0;
gridptr->nvertex = 0;
gridptr->number = 0;
gridptr->position = 0;
gridptr->reference = NULL;
gridptr->datatype = CDI_DATATYPE_FLT64;
......@@ -2311,10 +2310,12 @@ bool gridCompare(int gridID, const grid_t *grid, bool coord_compare)
differ = ((uuid1[0] || uuid2[0]) && memcmp(uuid1, uuid2, CDI_UUID_SIZE));
if (!differ)
{
int numberA = cdiInqVarKeyInt(&grid->keys, CDI_KEY_NUMBEROFGRIDUSED);
int numberB = cdiInqVarKeyInt(&gridRef->keys, CDI_KEY_NUMBEROFGRIDUSED);
if ( coord_compare )
{
differ = grid->nvertex != gridRef->nvertex
|| (grid->number > 0 && grid->position != gridRef->position)
|| (numberA > 0 && grid->position != gridRef->position)
|| gridRef->vtable->compareXYAO((grid_t *)gridRef, (grid_t *)grid);
}
else
......@@ -2322,20 +2323,19 @@ bool gridCompare(int gridID, const grid_t *grid, bool coord_compare)
if ( ((grid->x.vals == NULL) ^ (gridRef->x.vals == NULL)) &&
((grid->y.vals == NULL) ^ (gridRef->y.vals == NULL)) )
{
int nvertexA, nvertexB, numberA, numberB;
int nvertexA, nvertexB;
differ = ( (nvertexA = grid->nvertex)
&& (nvertexB = gridRef->nvertex)
&& (nvertexA != nvertexB) )
|| (numberA = grid->number, numberB = gridRef->number,
( numberA && numberB && (numberA != numberB) )
|| (( numberA && numberB && (numberA != numberB) )
|| ( (numberA && numberB)
&& (grid->position) != (gridRef->position) ) );
}
else
{
differ = grid->nvertex != gridRef->nvertex
|| grid->number != gridRef->number
|| (grid->number > 0 && grid->position != gridRef->position)
|| numberA != numberB
|| (numberA > 0 && grid->position != gridRef->position)
|| gridRef->vtable->compareXYAO((grid_t *)gridRef, (grid_t *)grid);
}
}
......@@ -2358,6 +2358,16 @@ bool gridCompare(int gridID, const grid_t *grid, bool coord_compare)
return differ;
}
int cmp_key_int(const cdi_keys_t *keysp1, const cdi_keys_t *keysp2, int key)
{
int v1 = cdiInqVarKeyInt(keysp1, key);
int v2 = cdiInqVarKeyInt(keysp2, key);
if (v1 != v2) return 1;
return 0;
}
int gridCompareP(void *gridptr1, void *gridptr2)
{
grid_t *g1 = ( grid_t * ) gridptr1;
......@@ -2378,7 +2388,7 @@ int gridCompareP(void *gridptr1, void *gridptr2)
if ( g1->gme.ni != g2->gme.ni ) return differ;
if ( g1->gme.ni2 != g2->gme.ni2 ) return differ;
if ( g1->gme.ni3 != g2->gme.ni3 ) return differ;
if ( g1->number != g2->number ) return differ;
if ( cmp_key_int(&g1->keys, &g2->keys, CDI_KEY_NUMBEROFGRIDUSED) ) return differ;
if ( g1->position != g2->position ) return differ;
if ( g1->trunc != g2->trunc ) return differ;
if ( g1->nvertex != g2->nvertex ) return differ;
......@@ -2595,9 +2605,9 @@ void gridComplete(grid_t *grid)
if ( gridtype == GRID_UNSTRUCTURED )
{
const int number = grid->number;
int number = cdiInqVarKeyInt(&grid->keys, CDI_KEY_NUMBEROFGRIDUSED);
if ( number > 0 ) cdiDefKeyInt(gridID, CDI_GLOBAL, CDI_KEY_NUMBEROFGRIDUSED, number);
const int position = grid->position >= 0 ? grid->position : 0;
if ( number > 0 ) gridDefNumber(gridID, number);
gridDefPosition(gridID, position);
}
......@@ -2709,7 +2719,7 @@ int gridGenerate(const grid_t *grid)
if ( valdef_group1 && grid->area)
gridDefArea(gridID, grid->area);
gridptr->number = grid->number;
cdiCopyVarKey(&grid->keys, CDI_KEY_NUMBEROFGRIDUSED, &gridptr->keys);
gridptr->position = grid->position;
gridptr->scanningMode = grid->scanningMode;
if ( gridtype == GRID_UNSTRUCTURED && grid->reference )
......@@ -3370,7 +3380,8 @@ void gridPrintKernel(int gridID, int opt, FILE *fp)
if ( type == GRID_UNSTRUCTURED )
{
int number = gridInqNumber(gridID);
int number = 0;
cdiInqKeyInt(gridID, CDI_GLOBAL, CDI_KEY_NUMBEROFGRIDUSED, &number);
int position = gridInqPosition(gridID);
// const unsigned char *d;
if ( number > 0 )
......@@ -3485,13 +3496,12 @@ void gridPrintP(void *voidptr, FILE *fp)
"ni = %d\n"
"ni2 = %d\n"
"ni3 = %d\n"
"number = %d\n"
"position = %d\n"
"trunc = %d\n"
"lcomplex = %d\n"
"reducedPointsSize = %d\n",
gridptr->datatype, gridptr->gme.nd, gridptr->gme.ni, gridptr->gme.ni2,
gridptr->gme.ni3, gridptr->number, gridptr->position, gridptr->trunc,
gridptr->gme.ni3, gridptr->position, gridptr->trunc,
gridptr->lcomplex, gridptr->reducedPointsSize );
}
......@@ -3907,13 +3917,7 @@ The function @func{gridDefNumber} defines the reference number for an unstructur
*/
void gridDefNumber(int gridID, int number)
{
grid_t *gridptr = grid_to_pointer(gridID);
if ( gridptr->number != number )
{
gridptr->number = number;
gridMark4Update(gridID);
}
cdiDefKeyInt(gridID, CDI_GLOBAL, CDI_KEY_NUMBEROFGRIDUSED, number);
}
/*
......@@ -3933,8 +3937,9 @@ The function @func{gridInqNumber} returns the reference number to an unstructure
*/
int gridInqNumber(int gridID)
{
grid_t* gridptr = grid_to_pointer(gridID);
return gridptr->number;
int number = 0;
cdiInqKeyInt(gridID, CDI_GLOBAL, CDI_KEY_NUMBEROFGRIDUSED, &number);
return number;
}
/*
......@@ -4135,7 +4140,6 @@ enum {
GRID_PACK_INT_IDX_GME_NI,
GRID_PACK_INT_IDX_GME_NI2,
GRID_PACK_INT_IDX_GME_NI3,
GRID_PACK_INT_IDX_NUMBER,
GRID_PACK_INT_IDX_POSITION,
GRID_PACK_INT_IDX_TRUNC,
GRID_PACK_INT_IDX_NVERTEX,
......@@ -4334,7 +4338,6 @@ gridUnpack(char * unpackBuffer, int unpackBufferSize,
gridP->gme.ni = intBuffer[GRID_PACK_INT_IDX_GME_NI];
gridP->gme.ni2 = intBuffer[GRID_PACK_INT_IDX_GME_NI2];
gridP->gme.ni3 = intBuffer[GRID_PACK_INT_IDX_GME_NI3];
gridP->number = intBuffer[GRID_PACK_INT_IDX_NUMBER];
gridP->position = intBuffer[GRID_PACK_INT_IDX_POSITION];
gridP->trunc = intBuffer[GRID_PACK_INT_IDX_TRUNC];
gridP->nvertex = intBuffer[GRID_PACK_INT_IDX_NVERTEX];
......@@ -4506,7 +4509,6 @@ gridPack(void * voidP, void * packBuffer, int packBufferSize,
intBuffer[GRID_PACK_INT_IDX_GME_NI] = gridP->gme.ni;
intBuffer[GRID_PACK_INT_IDX_GME_NI2] = gridP->gme.ni2;
intBuffer[GRID_PACK_INT_IDX_GME_NI3] = gridP->gme.ni3;
intBuffer[GRID_PACK_INT_IDX_NUMBER] = gridP->number;
intBuffer[GRID_PACK_INT_IDX_POSITION] = gridP->position;
intBuffer[GRID_PACK_INT_IDX_TRUNC] = gridP->trunc;
intBuffer[GRID_PACK_INT_IDX_NVERTEX] = gridP->nvertex;
......
......@@ -92,7 +92,7 @@ struct grid_t {
mask_t *mask_gme;
double *area;
struct grid_gme_t gme;
int number, position; /* parameter for GRID_REFERENCE */
int position; /* parameter for GRID_REFERENCE */
int trunc; /* parameter for GRID_SPECTRAL */
int nvertex;
char *reference;
......
......@@ -2570,7 +2570,7 @@ bool cdf_set_unstructured_par(ncvar_t *ncvar, grid_t *grid, int *xdimid, int *yd
return true;
}
if ( number_of_grid_used != CDI_UNDEFID ) grid->number = number_of_grid_used;
if ( number_of_grid_used != CDI_UNDEFID ) cdiDefVarKeyInt(&grid->keys, CDI_KEY_NUMBEROFGRIDUSED, number_of_grid_used);
if ( ncvar->position > 0 ) grid->position = ncvar->position;
if ( uuidOfHGrid[0] != 0 ) cdiDefVarKeyBytes(&grid->keys, CDI_KEY_UUID, uuidOfHGrid, CDI_UUID_SIZE);
......
......@@ -605,7 +605,8 @@ void cdfDefGridReference(stream_t *streamptr, int gridID)
{
const int fileID = streamptr->fileID;
const int number = gridInqNumber(gridID);
int number = 0;
cdiInqKeyInt(gridID, CDI_GLOBAL, CDI_KEY_NUMBEROFGRIDUSED, &number);
if ( number > 0 ) cdf_put_att_int(fileID, NC_GLOBAL, "number_of_grid_used", NC_INT, 1, &number);
const char *gridfile = gridInqReferencePtr(gridID);
......@@ -619,7 +620,8 @@ void cdfDefGridUUID(stream_t *streamptr, int gridID)
unsigned char uuidOfHGrid[CDI_UUID_SIZE];
size_t len = CDI_UUID_SIZE;
memset(uuidOfHGrid, 0, len);
cdiInqKeyBytes(gridID, CDI_GLOBAL, CDI_KEY_UUID, uuidOfHGrid, &len);
int length = CDI_UUID_SIZE;
cdiInqKeyBytes(gridID, CDI_GLOBAL, CDI_KEY_UUID, uuidOfHGrid, &length);
if ( !cdiUUIDIsNull(uuidOfHGrid) )
{
char uuidOfHGridStr[37];
......
......@@ -2155,17 +2155,20 @@ void gribapiDefGridUnstructured(grib_handle *gh, int gridID)
}
else
{
int position = gridInqPosition(gridID);
int number = gridInqNumber(gridID);
if ( position < 0 ) position = 0;
int number = 0;
cdiInqKeyInt(gridID, CDI_GLOBAL, CDI_KEY_NUMBEROFGRIDUSED, &number);
if ( number < 0 ) number = 0;
GRIB_CHECK(my_grib_set_long(gh, "numberOfGridUsed", number), 0);
int position = gridInqPosition(gridID);
if ( position < 0 ) position = 0;
GRIB_CHECK(my_grib_set_long(gh, "numberOfGridInReference", position), 0);
unsigned char uuid[CDI_UUID_SIZE];
size_t len = CDI_UUID_SIZE;
memset(uuid, 0, len);
cdiInqKeyBytes(gridID, CDI_GLOBAL, CDI_KEY_UUID, uuid, &len);
int length = CDI_UUID_SIZE;
cdiInqKeyBytes(gridID, CDI_GLOBAL, CDI_KEY_UUID, uuid, &length);
if (grib_set_bytes(gh, "uuidOfHGrid", uuid, &len) != 0)
Warning("Can't write UUID!");
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment