Commit f927d7da authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

Replaced gridDefUUID()/gridInqUUID() by cdiDefKeyBytes()/cdiInqKeyBytes() with CDI_KEY_UUID

parent 02e1a747
2019-11-12 Uwe Schulzweida
* Replaced gridDefUUID()/gridInqUUID() by cdiDefKeyBytes()/cdiInqKeyBytes() with CDI_KEY_UUID
2019-11-10 Uwe Schulzweida
* Replaced zaxisDefLtype()/zaxisInqLtype() by cdiDefKeyInt()/cdiInqKeyInt() with CDI_KEY_TYPEOFFIRSTFIXEDSURFACE
......
......@@ -824,6 +824,8 @@ size_t gridInqYCvals(int gridID, char *ycvals[]);
#define CDI_KEY_P0NAME 951 // Z-axis reference pressure name
#define CDI_KEY_P0VALUE 952 // Z-axis reference pressure in Pa
#define CDI_KEY_UUID 960 // UUID for grid/Z-axis reference
// cdiDefKeyInt: Define an integer value from a key of a CDI variable
int cdiDefKeyInt(int cdiID, int varID, int key, int value);
......
......@@ -51,18 +51,33 @@ cdi_key_t *new_key(cdi_keys_t *keysp, int key)
return keyp;
}
cdi_key_t *find_key(cdi_keys_t *keysp, int key)
{
xassert(keysp != NULL);
if ( keysp->nelems == 0 ) return NULL;
cdi_key_t *keys = keysp->value;
for ( size_t keyid = 0; keyid < keysp->nelems; keyid++ )
{
cdi_key_t *keyp = keys + keyid;
if ( keyp->key == key )
return keyp; // Normal return
cdi_key_t *keyp = &(keysp->value[keyid]);
if ( keyp->key == key ) return keyp; // Normal return
}
return NULL;
}
static
const cdi_key_t *find_key_const(const cdi_keys_t *keysp, int key)
{
xassert(keysp != NULL);
if ( keysp->nelems == 0 ) return NULL;
for ( size_t keyid = 0; keyid < keysp->nelems; keyid++ )
{
const cdi_key_t *keyp = &(keysp->value[keyid]);
if ( keyp->key == key ) return keyp; // Normal return
}
return NULL;
......@@ -173,7 +188,7 @@ int cdiInqKeyLen(int cdiID, int varID, int key, int *length)
cdi_keys_t *keysp = cdi_get_keysp(cdiID, varID);
xassert(keysp != NULL);
cdi_key_t *keyp = find_key(keysp, key);
const cdi_key_t *keyp = find_key(keysp, key);
if ( keyp != NULL )
{
*length = keyp->length;
......@@ -185,23 +200,23 @@ int cdiInqKeyLen(int cdiID, int varID, int key, int *length)
}
static
void cdi_copy_key(cdi_key_t *keyp, int cdiID, int varID)
void cdi_define_key(const cdi_key_t *keyp, cdi_keys_t *keysp)
{
if ( keyp->type == KEY_INT )
cdiDefKeyInt(cdiID, varID, keyp->key, keyp->v.i);
cdiDefVarKeyInt(keysp, keyp->key, keyp->v.i);
else if ( keyp->type == KEY_FLOAT )
cdiDefKeyFloat(cdiID, varID, keyp->key, keyp->v.d);
cdiDefVarKeyFloat(keysp, keyp->key, keyp->v.d);
else if ( keyp->type == KEY_BYTES )
cdiDefKeyBytes(cdiID, varID, keyp->key, keyp->v.s, keyp->length);
cdiDefVarKeyBytes(keysp, keyp->key, keyp->v.s, keyp->length);
}
void cdiCopyVarKeys(cdi_keys_t *keysp, int cdiID2, int varID2)
void cdiCopyVarKeys(const cdi_keys_t *keysp1, cdi_keys_t *keysp2)
{
for ( size_t keyid = 0; keyid < keysp->nelems; keyid++ )
for ( size_t keyid = 0; keyid < keysp1->nelems; keyid++ )
{
cdi_key_t *keyp = &(keysp->value[keyid]);
cdi_copy_key(keyp, cdiID2, varID2);
const cdi_key_t *keyp = &(keysp1->value[keyid]);
cdi_define_key(keyp, keysp2);
}
}
......@@ -210,10 +225,13 @@ int cdiCopyKeys(int cdiID1, int varID1, int cdiID2, int varID2)
{
int status = CDI_NOERR;
cdi_keys_t *keysp = cdi_get_keysp(cdiID1, varID1);
xassert(keysp != NULL);
cdi_keys_t *keysp1 = cdi_get_keysp(cdiID1, varID1);
xassert(keysp1 != NULL);
cdi_keys_t *keysp2 = cdi_get_keysp(cdiID2, varID2);
xassert(keysp2 != NULL);
cdiCopyVarKeys(keysp, cdiID2, varID2);
cdiCopyVarKeys(keysp1, keysp2);
return status;
}
......@@ -223,13 +241,16 @@ int cdiCopyKey(int cdiID1, int varID1, int key, int cdiID2)
{
int status = CDI_NOERR;
cdi_keys_t *keysp = cdi_get_keysp(cdiID1, varID1);
xassert(keysp != NULL);
cdi_keys_t *keysp1 = cdi_get_keysp(cdiID1, varID1);
xassert(keysp1 != NULL);
cdi_key_t *keyp = find_key(keysp, key);
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_copy_key(keyp, cdiID2, varID1);
cdi_define_key(keyp, keysp2);
return status;
}
......@@ -447,19 +468,11 @@ int cdiDefKeyBytes(int cdiID, int varID, int key, const unsigned char *bytes, in
return status;
}
// cdiInqKeyBytes: Get a bytes array from a key of a CDI variable
int cdiInqKeyBytes(int cdiID, int varID, int key, unsigned char *bytes, int *length)
int cdiInqVarKeyBytes(const cdi_keys_t *keysp, int key, unsigned char *bytes, int *length)
{
int status = -1;
xassert(bytes != NULL);
xassert(length != NULL);
// if ( varID != CDI_GLOBAL ) status = cdiInqKeyBytes(cdiID, CDI_GLOBAL, key, bytes, length);
cdi_keys_t *keysp = cdi_get_keysp(cdiID, varID);
xassert(keysp != NULL);
cdi_key_t *keyp = find_key(keysp, key);
const cdi_key_t *keyp = find_key_const(keysp, key);
if ( keyp != NULL ) // key in use
{
if ( keyp->type == KEY_BYTES )
......@@ -473,6 +486,20 @@ int cdiInqKeyBytes(int cdiID, int varID, int key, unsigned char *bytes, int *len
return status;
}
// cdiInqKeyBytes: Get a bytes array from a key of a CDI variable
int cdiInqKeyBytes(int cdiID, int varID, int key, unsigned char *bytes, int *length)
{
xassert(bytes != NULL);
xassert(length != NULL);
// if ( varID != CDI_GLOBAL ) status = cdiInqKeyBytes(cdiID, CDI_GLOBAL, key, bytes, length);
const cdi_keys_t *keysp = cdi_get_keysp(cdiID, varID);
xassert(keysp != NULL);
return cdiInqVarKeyBytes(keysp, key, bytes, length);
}
/*
@Function cdiDefKeyString
@Title Define a string value from a key of a CDI variable
......@@ -538,7 +565,7 @@ int cdiInqKeyString(int cdiID, int varID, int key, char *string, int *length)
const char *cdiInqVarKeyStringPtr(cdi_keys_t *keysp, int key)
{
cdi_key_t *keyp = find_key(keysp, key);
const cdi_key_t *keyp = find_key(keysp, key);
if ( keyp != NULL ) // key in use
{
if ( keyp->type == KEY_BYTES ) return (const char *)keyp->v.s;
......
......@@ -27,6 +27,7 @@ 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 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);
......@@ -50,7 +51,7 @@ const char *cdiInqVarKeyString(cdi_keys_t *keysp, int key)
return string;
}
void cdiCopyVarKeys(cdi_keys_t *keysp, int cdiID2, int varID2);
void cdiCopyVarKeys(const cdi_keys_t *keysp1, cdi_keys_t *keysp2);
void cdiDeleteVarKeys(cdi_keys_t *keysp);
void cdiDeleteKeys(int cdiID, int varID);
void cdiPrintKeys(int cdiID, int varID);
......
......@@ -754,7 +754,8 @@ void gribapiGetGridUnstructured(grib_handle *gh, grid_t *grid, size_t numberOfPo
}
*/
size_t len = (size_t)CDI_UUID_SIZE;
if ( grib_get_bytes(gh, "uuidOfHGrid", uuid, &len) == 0) memcpy(grid->uuid, uuid, CDI_UUID_SIZE);
if ( grib_get_bytes(gh, "uuidOfHGrid", uuid, &len) == 0)
cdiDefVarKeyBytes(&grid->keys, CDI_KEY_UUID, uuid, CDI_UUID_SIZE);
}
}
......
......@@ -166,7 +166,6 @@ void grid_init(grid_t *gridptr)
gridptr->lcomplex = false;
gridptr->hasdims = true;
memset(gridptr->uuid, 0, CDI_UUID_SIZE);
gridptr->name = NULL;
gridptr->vtable = &cdiGridVtable;
gridptr->keys.nalloc = MAX_KEYS;
......@@ -249,8 +248,13 @@ void grid_copy_base_scalar_fields(grid_t *gridptrOrig, grid_t *gridptrDup)
{
memcpy(gridptrDup, gridptrOrig, sizeof(grid_t));
gridptrDup->self = CDI_UNDEFID;
if ( gridptrOrig->reference )
gridptrDup->reference = strdupx(gridptrOrig->reference);
gridptrDup->keys.nelems = 0;
cdiCopyVarKeys(&gridptrOrig->keys, &gridptrDup->keys);
gridptrDup->x.keys.nelems = 0;
cdiCopyVarKeys(&gridptrOrig->x.keys, &gridptrDup->x.keys);
gridptrDup->y.keys.nelems = 0;
cdiCopyVarKeys(&gridptrOrig->y.keys, &gridptrDup->y.keys);
if ( gridptrOrig->reference ) gridptrDup->reference = strdupx(gridptrOrig->reference);
}
......@@ -2311,8 +2315,13 @@ bool gridCompare(int gridID, const grid_t *grid, bool coord_compare)
}
else if ( grid->type == GRID_UNSTRUCTURED )
{
/* FIXME: not octet 0 but octet 7 is guaranteed non-zero for any non-NULL UUID */
differ = ((gridRef->uuid[0] || grid->uuid[0]) && memcmp(gridRef->uuid, grid->uuid, CDI_UUID_SIZE));
unsigned char uuid1[CDI_UUID_SIZE]; memset(uuid1, 0, CDI_UUID_SIZE);
unsigned char uuid2[CDI_UUID_SIZE]; memset(uuid2, 0, CDI_UUID_SIZE);
int length = CDI_UUID_SIZE;
cdiInqVarKeyBytes(&gridRef->keys, CDI_KEY_UUID, uuid1, &length);
length = CDI_UUID_SIZE;
cdiInqVarKeyBytes(&grid->keys, CDI_KEY_UUID, uuid2, &length);
differ = ((uuid1[0] || uuid2[0]) && memcmp(uuid1, uuid2, CDI_UUID_SIZE));
if (!differ)
{
if ( coord_compare )
......@@ -2362,17 +2371,6 @@ bool gridCompare(int gridID, const grid_t *grid, bool coord_compare)
return differ;
}
/*
int gridIsEqual(int gridID1, int gridID2)
{
const grid_t *grid2 = grid_to_pointer(gridID2);
int grid_is_equal = gridCompare(gridID1, grid2, true) == false;
return grid_is_equal;
}
*/
int gridCompareP(void *gridptr1, void *gridptr2)
{
grid_t *g1 = ( grid_t * ) gridptr1;
......@@ -2536,8 +2534,13 @@ int gridCompareP(void *gridptr1, void *gridptr2)
else if ( g2->mask_gme )
return differ;
if ( memcmp(g1->uuid, g2->uuid, CDI_UUID_SIZE) )
return differ;
unsigned char uuid1[CDI_UUID_SIZE]; memset(uuid1, 0, CDI_UUID_SIZE);
unsigned char uuid2[CDI_UUID_SIZE]; memset(uuid2, 0, CDI_UUID_SIZE);
int length = CDI_UUID_SIZE;
cdiInqVarKeyBytes(&g1->keys, CDI_KEY_UUID, uuid1, &length);
length = CDI_UUID_SIZE;
cdiInqVarKeyBytes(&g2->keys, CDI_KEY_UUID, uuid2, &length);
if ( memcmp(uuid1, uuid2, CDI_UUID_SIZE) ) return differ;
return equal;
}
......@@ -2718,13 +2721,14 @@ int gridGenerate(const grid_t *grid)
gridptr->y.inc = grid->y.inc;
if ( valdef_group1 && grid->area)
gridDefArea(gridID, grid->area);
gridptr->number = grid->number;
gridptr->position = grid->position;
gridptr->scanningMode = grid->scanningMode;
gridptr->iScansNegatively = grid->iScansNegatively;
gridptr->jScansPositively = grid->jScansPositively;
gridptr->jPointsAreConsecutive = grid->jPointsAreConsecutive;
memcpy(gridptr->uuid, grid->uuid, CDI_UUID_SIZE);
if ( gridtype == GRID_UNSTRUCTURED && grid->reference )
gridDefReference(gridID, grid->reference);
if ( gridtype == GRID_PROJECTION )
......@@ -3396,13 +3400,7 @@ void gridPrintKernel(int gridID, int opt, FILE *fp)
fprintf(fp, "number = %d\n", number);
if ( position >= 0 ) fprintf(fp, "position = %d\n", position);
}
/*
gridInqUUID(gridID, uuidOfHGrid);
d = (unsigned char *) &uuidOfHGrid;
fprintf(fp, "uuid = %02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7],
d[8], d[9], d[10], d[11], d[12], d[13], d[14], d[15]);
*/
if ( gridInqReference(gridID, NULL) )
{
char reference_link[8192];
......@@ -4090,9 +4088,8 @@ The function @func{gridDefUUID} defines the UUID for an unstructured grid.
*/
void gridDefUUID(int gridID, const unsigned char uuid[CDI_UUID_SIZE])
{
grid_t* gridptr = grid_to_pointer(gridID);
cdiDefKeyBytes(gridID, CDI_GLOBAL, CDI_KEY_UUID, uuid, CDI_UUID_SIZE);
memcpy(gridptr->uuid, uuid, CDI_UUID_SIZE);
gridMark4Update(gridID);
}
......@@ -4113,9 +4110,9 @@ The function @func{gridInqUUID} returns the UUID to an unstructured grid.
*/
void gridInqUUID(int gridID, unsigned char uuid[CDI_UUID_SIZE])
{
grid_t *gridptr = grid_to_pointer(gridID);
memcpy(uuid, gridptr->uuid, CDI_UUID_SIZE);
memset(uuid, 0, CDI_UUID_SIZE);
int length = CDI_UUID_SIZE;
cdiInqKeyBytes(gridID, CDI_GLOBAL, CDI_KEY_UUID, uuid, &length);
}
......@@ -4205,7 +4202,6 @@ enum {
gridHasYBoundsFlag = 1 << 6,
gridHasReferenceFlag = 1 << 7,
gridHasReducedPointsFlag = 1 << 8,
gridHasUUIDFlag = 1 << 9,
};
......@@ -4223,8 +4219,7 @@ static int gridGetComponentFlags(const grid_t * gridP)
| (gridHasXBoundsFlag & (int)((unsigned)(gridP->x.bounds == NULL) - 1U))
| (gridHasYBoundsFlag & (int)((unsigned)(gridP->y.bounds == NULL) - 1U))
| (gridHasReferenceFlag & (int)((unsigned)(gridP->reference == NULL) - 1U))
| (gridHasReducedPointsFlag & (int)((unsigned)(gridP->reducedPoints == NULL) - 1U))
| (gridHasUUIDFlag & (int)((unsigned)cdiUUIDIsNull(gridP->uuid) - 1U));
| (gridHasReducedPointsFlag & (int)((unsigned)(gridP->reducedPoints == NULL) - 1U));
return flags;
}
......@@ -4302,6 +4297,7 @@ gridGetPackSize(void * voidP, void *context)
+ serializeGetSize(1, CDI_DATATYPE_UINT32, context));
}
packBuffSize += serializeKeysGetPackSize(&gridP->keys, context);
packBuffSize += serializeKeysGetPackSize(&gridP->x.keys, context);
packBuffSize += serializeKeysGetPackSize(&gridP->y.keys, context);
......@@ -4328,9 +4324,6 @@ gridGetPackSize(void * voidP, void *context)
+ serializeGetSize(1, CDI_DATATYPE_UINT32, context);
}
if (!cdiUUIDIsNull(gridP->uuid))
packBuffSize += serializeGetSize(CDI_UUID_SIZE, CDI_DATATYPE_UCHAR, context);
return packBuffSize;
}
......@@ -4476,6 +4469,7 @@ gridUnpack(char * unpackBuffer, int unpackBufferSize,
xassert(cdiCheckSum(CDI_DATATYPE_FLT, size, gridP->y.bounds) == d);
}
serializeKeysUnpack(unpackBuffer, unpackBufferSize, unpackBufferPos, &gridP->keys, context);
serializeKeysUnpack(unpackBuffer, unpackBufferSize, unpackBufferPos, &gridP->x.keys, context);
serializeKeysUnpack(unpackBuffer, unpackBufferSize, unpackBufferPos, &gridP->y.keys, context);
......@@ -4513,11 +4507,6 @@ gridUnpack(char * unpackBuffer, int unpackBufferSize,
&d, 1, CDI_DATATYPE_UINT32, context);
xassert(cdiCheckSum(CDI_DATATYPE_UCHAR, gridP->size, gridP->mask_gme) == d);
}
if (memberMask & gridHasUUIDFlag)
{
serializeUnpack(unpackBuffer, unpackBufferSize, unpackBufferPos,
gridP->uuid, CDI_UUID_SIZE, CDI_DATATYPE_UCHAR, context);
}
reshSetStatus(gridP->self, &gridOps,
reshGetStatus(gridP->self, &gridOps) & ~RESH_SYNC_BIT);
......@@ -4677,6 +4666,7 @@ gridPack(void * voidP, void * packBuffer, int packBufferSize,
packBuffer, packBufferSize, packBufferPos, context);
}
serializeKeysPack(&gridP->keys, packBuffer, packBufferSize, packBufferPos, context);
serializeKeysPack(&gridP->x.keys, packBuffer, packBufferSize, packBufferPos, context);
serializeKeysPack(&gridP->y.keys, packBuffer, packBufferSize, packBufferPos, context);
......@@ -4712,10 +4702,6 @@ gridPack(void * voidP, void * packBuffer, int packBufferSize,
serializePack(&d, 1, CDI_DATATYPE_UINT32,
packBuffer, packBufferSize, packBufferPos, context);
}
if (memberMask & gridHasUUIDFlag)
serializePack(gridP->uuid, CDI_UUID_SIZE, CDI_DATATYPE_UCHAR,
packBuffer, packBufferSize, packBufferPos, context);
}
......
......@@ -96,7 +96,6 @@ struct grid_t {
int trunc; /* parameter for GRID_SPECTEAL */
int nvertex;
char *reference;
unsigned char uuid[CDI_UUID_SIZE]; /* uuid for grid reference */
int *reducedPoints;
int reducedPointsSize;
int np; /* number of parallels between a pole and the equator */
......
......@@ -2572,7 +2572,7 @@ bool cdf_set_unstructured_par(ncvar_t *ncvar, grid_t *grid, int *xdimid, int *yd
if ( number_of_grid_used != CDI_UNDEFID ) grid->number = number_of_grid_used;
if ( ncvar->position > 0 ) grid->position = ncvar->position;
if ( uuidOfHGrid[0] != 0 ) memcpy(grid->uuid, uuidOfHGrid, CDI_UUID_SIZE);
if ( uuidOfHGrid[0] != 0 ) cdiDefVarKeyBytes(&grid->keys, CDI_KEY_UUID, uuidOfHGrid, CDI_UUID_SIZE);
return false;
}
......
......@@ -2155,13 +2155,14 @@ void gribapiDefGridUnstructured(grib_handle *gh, int gridID)
}
else
{
unsigned char uuid[CDI_UUID_SIZE];
int position = gridInqPosition(gridID);
int number = gridInqNumber(gridID);
if ( position < 0 ) position = 0;
if ( number < 0 ) number = 0;
GRIB_CHECK(my_grib_set_long(gh, "numberOfGridUsed", number), 0);
GRIB_CHECK(my_grib_set_long(gh, "numberOfGridInReference", position), 0);
unsigned char uuid[CDI_UUID_SIZE];
size_t len = CDI_UUID_SIZE;
gridInqUUID(gridID, uuid);
if (grib_set_bytes(gh, "uuidOfHGrid", uuid, &len) != 0)
......
......@@ -514,8 +514,9 @@ int cmpLevelTableInv(const void* s1, const void* s2)
void varCopyKeys(int vlistID, int varID)
{
cdi_keys_t *keysp = &(vartable[varID].keys);
cdiCopyVarKeys(keysp, vlistID, varID);
vlist_t *vlistptr = vlist_to_pointer(vlistID);
vlistptr->vars[varID].keys.nelems = 0;
cdiCopyVarKeys(&vartable[varID].keys, &vlistptr->vars[varID].keys);
}
......
......@@ -1942,21 +1942,17 @@ int vlistVarCompare(vlist_t *a, int varIDA, vlist_t *b, int varIDB)
{
int zaxisID = pva->zaxisID;
size_t nlevs = (size_t)zaxisInqSize(zaxisID);
diff |= (memcmp(pva->levinfo, pvb->levinfo, sizeof (levinfo_t) * nlevs)
!= 0);
if (diff)
return 1;
diff |= (memcmp(pva->levinfo, pvb->levinfo, sizeof (levinfo_t) * nlevs) != 0);
if (diff) return 1;
}
size_t natts = a->vars[varIDA].atts.nelems;
if (natts != b->vars[varIDB].atts.nelems)
return 1;
if (natts != b->vars[varIDB].atts.nelems) return 1;
for (size_t attID = 0; attID < natts; ++attID)
diff |= cdi_att_compare(a, varIDA, b, varIDB, (int)attID);
size_t nkeys = a->vars[varIDA].keys.nelems;
if (nkeys != b->vars[varIDB].keys.nelems)
return 1;
if (nkeys != b->vars[varIDB].keys.nelems) return 1;
for (size_t keyID = 0; keyID < nkeys; ++keyID)
diff |= vlist_key_compare(a, varIDA, b, varIDB, (int)keyID);
......
......@@ -175,6 +175,8 @@ void zaxis_copy(zaxis_t *zaxisptr2, zaxis_t *zaxisptr1)
const int zaxisID2 = zaxisptr2->self;
memcpy(zaxisptr2, zaxisptr1, sizeof(zaxis_t));
zaxisptr2->self = zaxisID2;
zaxisptr2->keys.nelems = 0;
cdiCopyVarKeys(&zaxisptr1->keys, &zaxisptr2->keys);
}
......
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