Commit 9ab250d3 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

Added function cdiInqKeyString.

parent 2321de43
......@@ -72,19 +72,6 @@ int main(void)
// Assign the variable list to the dataset
streamDefVlist(streamID, vlistID);
int vlistID0 = streamInqVlist(streamID);
/*
for ( int varID = 0; varID < 2; ++varID )
{
// vlistDefVarIntKey(vlistID0, varID, "centre", 252);
vlistDefVarIntKey(vlistID0, varID, "grib2LocalSectionPresent", 1);
vlistDefVarIntKey(vlistID0, varID, "grib2LocalSectionNumber", 1);
vlistDefVarIntKey(vlistID0, varID, "mpimType", 1);
vlistDefVarIntKey(vlistID0, varID, "mpimClass", 2);
vlistDefVarIntKey(vlistID0, varID, "mpimUser", 1403);
// vlistDefVarIntKey(vlistID0, varID, "shortendRevNo", 99);
}
*/
// Loop over the number of time steps
for ( int tsID = 0; tsID < nts; tsID++ )
{
......@@ -95,8 +82,6 @@ int main(void)
// Define the time step
streamDefTimestep(streamID, tsID);
vlistDefVarIntKey(vlistID0, varID1, "dataTime", 1200+(tsID+1)*10);
// Init var1 and var2
for ( size_t i = 0; i < nlon*nlat; i++ ) var1[i] = 1.1;
for ( size_t i = 0; i < nlon*nlat*nlev; i++ ) var2[i] = 2.2;
......
......@@ -824,14 +824,20 @@ int cdiDefKeyInt(int cdiID, int varID, int key, int value);
int cdiInqKeyInt(int cdiID, int varID, int key, int *value);
// cdiDefKeyBytes: Define a bytes array from a key of a CDI variable
int cdiDefKeyBytes(int cdiID, int varID, int key, unsigned char *bytes, int length);
int cdiDefKeyBytes(int cdiID, int varID, int key, const unsigned char *bytes, int length);
// 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);
// cdiDefKeyString: Define a string value from a key of a CDI variable
int cdiDefKeyString(int cdiID, int varID, int key, const char *string);
// cdiInqKeyLen: Get the length of the string representation of the key
int cdiInqKeyLen(int cdiID, int varID, int key, int *length);
// cdiInqKeyString: Get a string value from a key of a CDI variable
int cdiInqKeyString(int cdiID, int varID, int key, char *string, int *length);
int cdiCopyKeys(int cdiID1, int varID1, int cdiID2, int varID2);
// CDI grid keys
......
......@@ -9,7 +9,7 @@
#include "zaxis.h"
#include "resource_unpack.h"
enum {KEY_INT, KEY_FLOAT, KEY_BYTES, KEY_STRING};
enum {KEY_INT, KEY_FLOAT, KEY_BYTES};
static
cdi_keys_t *get_keysp(vlist_t *vlistptr, int varID)
......@@ -123,6 +123,7 @@ int cdiDelKeys(int cdiID, int varID)
cdi_key_t *keyp = &(keysp->value[keyid]);
if ( keyp->length && keyp->v.s )
{
printf("length %d %p %d %d %d\n", keyp->length, keyp->v.s, keyp->v.s[0], cdiID, varID);
free(keyp->v.s);
keyp->v.s = NULL;
keyp->length = 0;
......@@ -220,8 +221,9 @@ int cdiInqKeyInt(int cdiID, int varID, int key, int *value)
}
// cdiDefKeyBytes: Define a bytes array from a key of a CDI variable
int cdiDefKeyBytes(int cdiID, int varID, int key, unsigned char *bytes, int length)
int cdiDefKeyBytes(int cdiID, int varID, int key, const unsigned char *bytes, int length)
{
printf("cdiDefKeyBytes %d %d %d\n", cdiID, varID, length);
int status = CDI_NOERR;
cdi_keys_t *keysp = cdi_get_keysp(cdiID, varID);
......@@ -232,9 +234,18 @@ int cdiDefKeyBytes(int cdiID, int varID, int key, unsigned char *bytes, int leng
if ( keyp != NULL )
{
if ( keyp->length != 0 && keyp->length != length ) free(keyp->v.s);
keyp->length = length;
keyp->v.s = (unsigned char *) malloc(length);
if ( keyp->length != 0 && keyp->length != length )
{
free(keyp->v.s);
keyp->length = 0;
}
if ( keyp->length == 0 )
{
keyp->v.s = (unsigned char *) malloc(length);
printf("alloc %d %p\n", length, keyp->v.s);
keyp->length = length;
}
memcpy(keyp->v.s, bytes, length);
keyp->type = KEY_BYTES;
}
......@@ -246,32 +257,52 @@ int cdiDefKeyBytes(int cdiID, int varID, int key, unsigned char *bytes, int leng
int cdiInqKeyBytes(int cdiID, int varID, int key, unsigned char *bytes, int *length)
{
int status = -1;
return status;
}
xassert(bytes != NULL);
xassert(length != NULL);
// cdiDefKeyString: Define a string value from a key of a CDI variable
int cdiDefKeyString(int cdiID, int varID, int key, unsigned char *bytes, int length)
{
int status = CDI_NOERR;
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);
if ( keyp == NULL ) keyp = new_key(keysp, key);
if ( keyp != NULL )
if ( keyp != NULL ) // key in use
{
if ( keyp->length != 0 && keyp->length != length ) free(keyp->v.s);
keyp->length = length;
keyp->v.s = (unsigned char *) malloc(length+1);
memcpy(keyp->v.s, bytes, length+1);
keyp->type = KEY_STRING;
if ( keyp->type == KEY_BYTES )
{
if ( keyp->length < *length ) *length = keyp->length;
memcpy(bytes, keyp->v.s, *length);
status = CDI_NOERR;
}
}
return status;
}
// cdiDefKeyString: Define a string value from a key of a CDI variable
int cdiDefKeyString(int cdiID, int varID, int key, const char *string)
{
xassert(string != NULL);
int length = strlen(string)+1;
int status = cdiDefKeyBytes(cdiID, varID, key, (const unsigned char *) string, length);
return status;
}
// cdiInqKeyString: Get a string value from a key of a CDI variable
int cdiInqKeyString(int cdiID, int varID, int key, char *string, int *length)
{
xassert(string != NULL);
xassert(length != NULL);
string[0] = '\0';
int status = cdiInqKeyBytes(cdiID, varID, key, (unsigned char *) string, length);
return status;
}
/*
* Local Variables:
* c-file-style: "Java"
......
......@@ -3032,10 +3032,16 @@ size_t gribapiEncode(int varID, int levelID, int vlistID, int gridID, int zaxisI
grib_set_long(gh, "mpimClass", mpimClass);
grib_set_long(gh, "mpimUser", mpimUser);
int revNumber;
if ( cdiInqKeyInt(vlistID, varID, CDI_KEY_REVNUMBER, &revNumber) == CDI_NOERR )
grib_set_long(gh, "revNumber", revNumber);
int revNumLen = 20;
unsigned char revNumber[revNumLen];
if ( cdiInqKeyBytes(vlistID, varID, CDI_KEY_REVNUMBER, revNumber, &revNumLen) == CDI_NOERR )
{
size_t revNumLenS = revNumLen;
grib_set_bytes(gh, "revNumber", revNumber, &revNumLenS);
}
int revStatus;
if ( cdiInqKeyInt(vlistID, varID, CDI_KEY_REVSTATUS, &revStatus) == CDI_NOERR )
grib_set_long(gh, "revStatus", revStatus);
}
}
......
......@@ -162,6 +162,9 @@ void vlist_copy(vlist_t *vlistptr2, vlist_t *vlistptr1)
memcpy(vlistptr2, vlistptr1, sizeof(vlist_t));
vlistptr2->internal = vlist2internal; //the question who's responsible to destroy the vlist is tied to its containing memory region, so we retain this flag
vlistptr2->immutable = 0; //this is a copy, so it's mutable, independent of whether the original is mutable or not
vlistptr2->keys.nelems = 0;
for ( int i = 0; i < MAX_KEYS; ++i )
vlistptr2->keys.value[i].length = 0;
vlistptr2->atts.nelems = 0;
vlistptr2->self = vlistID2;
}
......
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