Commit 212256a1 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

Replaced vlistDefVarName()/vlistInqVarName() by...

Replaced vlistDefVarName()/vlistInqVarName() by cdiDefKeyString()/cdiInqKeyString() with CDI_KEY_NAME.
parent ee3ebf79
2020-06-10 Uwe Schulzweida
* Replaced vlistDefVarName()/vlistInqVarName() by cdiDefKeyString()/cdiInqKeyString() with CDI_KEY_NAME
2020-05-15 Uwe Schulzweida
* Erroneous behaviour setting GRIB2 bitsPerValue (bug fix) [report: Florian Prill]
......
vlistDefVarName(vlistID, varID, name) -> cdiDefKeyString(vlistID, varID, CDI_KEY_NAME, name)
vlistInqVarName(vlistID, varID, name) -> cdiInqKeyString(vlistID, varID, CDI_KEY_NAME, name, &length) // partly obsolete
gridDefXname(gridID, xname) -> cdiDefKeyString(gridID, CDI_XAXIS, CDI_KEY_NAME, xname)
gridDefXlongname(gridID, xlongname) -> cdiDefKeyString(gridID, CDI_XAXIS, CDI_KEY_LONGNAME, xlongname)
gridDefXunits(gridID, xunits) -> cdiDefKeyString(gridID, CDI_XAXIS, CDI_KEY_UNITS, xunits)
......
......@@ -782,8 +782,9 @@ int cdfDefVar(stream_t *streamptr, int varID)
size_t chunks[4];
int ndims = cdfDefineDimsAndChunks(streamptr, varID, xid, yid, zid, gridsize, dimorder, dims, lchunk, chunks, axis, &iax);
char name[CDI_MAX_NAME]; name[0] = 0;
if ( vlistInqVarNamePtr(vlistID, varID) ) vlistInqVarName(vlistID, varID, name);
char name[CDI_MAX_NAME];
int length = CDI_MAX_NAME;
(void)cdiInqKeyString(vlistID, varID, CDI_KEY_NAME, name, &length);
char longname[CDI_MAX_NAME]; longname[0] = 0;
char stdname[CDI_MAX_NAME]; stdname[0] = 0;
......
......@@ -875,6 +875,8 @@ int cdiCopyKeys(int cdiID1, int varID1, int cdiID2, int varID2);
int cdiCopyKey(int cdiID1, int varID1, int key, int cdiID2);
int cdiDeleteKey(int cdiID, int varID, int key);
// GRID routines
// gridDefXname: Define the name of a X-axis
......
......@@ -208,6 +208,28 @@ void cdi_define_key(const cdi_key_t *keyp, cdi_keys_t *keysp)
}
int cdiDeleteKey(int cdiID, int varID, int key)
{
int status = -1;
cdi_keys_t *keysp = cdi_get_keysp(cdiID, varID);
xassert(keysp != NULL);
cdi_key_t *keyp = find_key(keysp, key);
if ( keyp != NULL ) // key in use
{
if ( keyp->length )
{
free(keyp->v.s);
keyp->v.s = NULL;
keyp->length = 0;
}
}
return status;
}
void cdiCopyVarKeys(const cdi_keys_t *keysp1, cdi_keys_t *keysp2)
{
for ( size_t keyid = 0; keyid < keysp1->nelems; keyid++ )
......
......@@ -3183,7 +3183,6 @@ void cdf_define_code_and_param(int vlistID, int varID)
if ( strStartsWith(name, "var") )
{
vlistDefVarCode(vlistID, varID, atoi(name+3));
// vlistDestroyVarName(vlistID, varID);
}
}
else if ( len > 4 && isdigit((int) name[4]) )
......@@ -3191,7 +3190,6 @@ void cdf_define_code_and_param(int vlistID, int varID)
if ( strStartsWith(name, "code") )
{
vlistDefVarCode(vlistID, varID, atoi(name+4));
// vlistDestroyVarName(vlistID, varID);
}
}
else if ( len > 5 && isdigit((int) name[5]) )
......@@ -3201,7 +3199,6 @@ void cdf_define_code_and_param(int vlistID, int varID)
int pnum = -1, pcat = 255, pdis = 255;
sscanf(name+5, "%d.%d.%d", &pnum, &pcat, &pdis);
vlistDefVarParam(vlistID, varID, cdiEncodeParam(pnum, pcat, pdis));
// vlistDestroyVarName(vlistID, varID);
}
}
}
......@@ -3222,7 +3219,7 @@ void cdf_define_institut_and_model_id(int vlistID, int varID)
tableInqEntry(cdiDefaultTableID, code, -1, name, longname, units);
if ( name[0] )
{
vlistDestroyVarName(vlistID, varID);
cdiDeleteKey(vlistID, varID, CDI_KEY_NAME);
vlistDestroyVarLongname(vlistID, varID);
vlistDestroyVarUnits(vlistID, varID);
......
......@@ -7,9 +7,6 @@
#include "cdi_int.h"
/*int TableDefine = 0; */ /* Define new table also if the entry already exist */
/* This is needed for createtable */
#include "tablepar.h"
#include "table.h"
......@@ -97,11 +94,8 @@ static void parTableInitEntry(int tableID)
static void tableGetPath(void)
{
char *path = getenv("TABLEPATH");
if ( path ) tablePath = strdupx(path);
/*
printf("tablePath = %s\n", tablePath);
*/
// printf("tablePath = %s\n", tablePath);
}
static void parTableFinalize(void)
......@@ -129,8 +123,7 @@ static void parTableInit(void)
ParTableInit = 1;
atexit(parTableFinalize);
if ( cdiPartabIntern )
tableDefault();
if ( cdiPartabIntern ) tableDefault();
tableGetPath();
}
......@@ -138,8 +131,8 @@ static void parTableInit(void)
static int tableNewEntry()
{
int tableID = 0;
static int init = 0;
static int init = 0;
if ( ! init )
{
for ( tableID = 0; tableID < parTableSize; tableID++ )
......@@ -147,16 +140,13 @@ static int tableNewEntry()
init = 1;
}
/*
Look for a free slot in parTable.
*/
// Look for a free slot in parTable.
for ( tableID = 0; tableID < parTableSize; tableID++ )
{
if ( ! parTable[tableID].used ) break;
}
if ( tableID == parTableSize )
Error("no more entries!");
if ( tableID == parTableSize ) Error("no more entries!");
parTable[tableID].used = true;
parTableNum++;
......@@ -239,7 +229,7 @@ decodeForm1(char *pline, char *name, char *longname, char *units)
static int
decodeForm2(char *pline, char *name, char *longname, char *units)
{
/* Format 2 : code | name | longname | units */
// Format 2 : code | name | longname | units
char *pend;
pline = strchr(pline, '|');
......@@ -345,9 +335,7 @@ int tableRead(const char *tablefile)
if ( len < 4 ) continue;
while ( isspace((int) *pline) ) pline++;
id = atoi(pline);
/*
if ( id > 255 ) id -= 256;
*/
// if ( id > 255 ) id -= 256;
if ( id == 0 ) continue;
while ( isdigit((int) *pline) ) pline++;
......@@ -573,11 +561,9 @@ int tableInqModel(int tableID)
static void partabCheckID(int item)
{
if ( item < 0 || item >= parTableSize )
Error("item %d undefined!", item);
if ( item < 0 || item >= parTableSize ) Error("item %d undefined!", item);
if ( ! parTable[item].name )
Error("item %d name undefined!", item);
if ( ! parTable[item].name ) Error("item %d name undefined!", item);
}
......@@ -585,8 +571,7 @@ const char *tableInqNamePtr(int tableID)
{
const char *tablename = NULL;
if ( CDI_Debug )
Message("tableID = %d", tableID);
if ( CDI_Debug ) Message("tableID = %d", tableID);
if ( ! ParTableInit ) parTableInit();
......
......@@ -229,7 +229,7 @@ void vlist_delete(vlist_t *vlistptr)
for ( int varID = 0; varID < nvars; varID++ )
{
if ( vars[varID].levinfo ) Free(vars[varID].levinfo);
if ( vars[varID].name ) Free(vars[varID].name);
//if ( vars[varID].name ) Free(vars[varID].name);
if ( vars[varID].longname ) Free(vars[varID].longname);
if ( vars[varID].stdname ) Free(vars[varID].stdname);
if ( vars[varID].units ) Free(vars[varID].units);
......@@ -293,7 +293,7 @@ void cdiVlistDestroy_(int vlistID)
static
void var_copy_entries(var_t *var2, var_t *var1)
{
if ( var1->name ) var2->name = strdupx(var1->name);
//if ( var1->name ) var2->name = strdupx(var1->name);
if ( var1->longname ) var2->longname = strdupx(var1->longname);
if ( var1->stdname ) var2->stdname = strdupx(var1->stdname);
if ( var1->units ) var2->units = strdupx(var1->units);
......@@ -841,15 +841,21 @@ void vlistMerge(int vlistID2, int vlistID1)
if ( nvars1 == nvars2 )
{
char name1[CDI_MAX_NAME], name2[CDI_MAX_NAME];
for ( varID = 0; varID < nvars2; varID++ )
{
size_t ngp1 = gridInqSize(vars1[varID].gridID);
size_t ngp2 = gridInqSize(vars2[varID].gridID);
if ( ngp1 != ngp2 ) break;
if ( vars1[varID].name && vars2[varID].name )
int length = CDI_MAX_NAME;
(void)cdiInqKeyString(vlistID1, varID, CDI_KEY_NAME, name1, &length);
length = CDI_MAX_NAME;
(void)cdiInqKeyString(vlistID2, varID, CDI_KEY_NAME, name2, &length);
if ( *name1 && *name2 )
{
if ( strcmp(vars1[varID].name, vars2[varID].name) != 0 ) break;
if ( strcmp(name1, name2) != 0 ) break;
}
else
{
......@@ -1089,9 +1095,10 @@ int vlistNtsteps(int vlistID)
static
void vlistPrintKernel(vlist_t *vlistptr, FILE *fp)
{
fprintf ( fp, "#\n# vlistID %d\n#\n", vlistptr->self);
const int vlistID = vlistptr->self;
fprintf ( fp, "#\n# vlistID %d\n#\n", vlistID);
int nvars = vlistptr->nvars;
const int nvars = vlistptr->nvars;
fprintf(fp, "nvars : %d\n"
"ngrids : %d\n"
......@@ -1115,7 +1122,9 @@ void vlistPrintKernel(vlist_t *vlistptr, FILE *fp)
int zaxisID = vlistptr->vars[varID].zaxisID;
int subtypeID = vlistptr->vars[varID].subtypeID;
int tsteptype = vlistptr->vars[varID].tsteptype;
const char *name = vlistptr->vars[varID].name;
char name[CDI_MAX_NAME];
int length = CDI_MAX_NAME;
(void)cdiInqKeyString(vlistID, varID, CDI_KEY_NAME, name, &length);
const char *longname = vlistptr->vars[varID].longname;
const char *units = vlistptr->vars[varID].units;
int flag = vlistptr->vars[varID].flag;
......@@ -1125,7 +1134,7 @@ void vlistPrintKernel(vlist_t *vlistptr, FILE *fp)
cdiParamToString(param, paramstr, sizeof(paramstr));
fprintf(fp, "%6d %-8s %6d %6d %6d %6d %5d %6d %-8s %s [%s]\n",
varID, paramstr, gridID, zaxisID, subtypeID, tsteptype, flag, iorank,
name?name:"", longname?longname:"", units?units:"");
name, longname?longname:"", units?units:"");
}
fputs("\n"
......
......@@ -66,7 +66,6 @@ typedef struct
int xyz;
bool missvalused; // true if missval is defined
bool lvalidrange;
char *name;
char *longname;
char *stdname;
char *units;
......@@ -122,7 +121,6 @@ vlist_t *vlist_to_pointer(int vlistID);
void cdiVlistMakeInternal(int vlistID);
void cdiVlistMakeImmutable(int vlistID);
void vlistCheckVarID(const char *caller, int vlistID, int varID);
void vlistDestroyVarName(int vlistID, int varID);
void vlistDestroyVarLongname(int vlistID, int varID);
void vlistDestroyVarStdname(int vlistID, int varID);
void vlistDestroyVarUnits(int vlistID, int varID);
......
......@@ -35,7 +35,6 @@ void vlistvarInitEntry(int vlistID, int varID)
vlistptr->vars[varID].missval = CDI_default_missval;
vlistptr->vars[varID].addoffset = 0.0;
vlistptr->vars[varID].scalefactor = 1.0;
vlistptr->vars[varID].name = NULL;
vlistptr->vars[varID].longname = NULL;
vlistptr->vars[varID].stdname = NULL;
vlistptr->vars[varID].units = NULL;
......@@ -388,23 +387,19 @@ int vlistInqVarCode(int vlistID, int varID)
int code = pnum;
if ( pdis != 255 ) code = -varID-1; // GRIB2 Parameter
if ( code < 0 && vlistptr->vars[varID].tableID != -1 && vlistptr->vars[varID].name != NULL )
int tableID = vlistptr->vars[varID].tableID;
if (code < 0 && tableID != -1)
{
tableInqParCode(vlistptr->vars[varID].tableID, vlistptr->vars[varID].name, &code);
char name[CDI_MAX_NAME];
int length = CDI_MAX_NAME;
(void)cdiInqKeyString(vlistID, varID, CDI_KEY_NAME, name, &length);
if (name[0]) tableInqParCode(tableID, name, &code);
}
return code;
}
const char *vlistInqVarNamePtr(int vlistID, int varID)
{
vlistCheckVarID(__func__, vlistID, varID);
const vlist_t *vlistptr = vlist_to_pointer(vlistID);
return vlistptr->vars[varID].name;
}
/*
@Function vlistInqVarName
@Title Get the name of a Variable
......@@ -428,16 +423,15 @@ otherwise the result is an empty string.
*/
void vlistInqVarName(int vlistID, int varID, char *name)
{
/*
int length = CDI_MAX_NAME;
(void)cdiInqKeyString(vlistID, varID, CDI_KEY_NAME, name, &length);
*/
vlist_t *vlistptr = vlist_to_pointer(vlistID);
if (!name[0])
{
vlistCheckVarID(__func__, vlistID, varID);
if ( vlistptr->vars[varID].name == NULL )
{
vlist_t *vlistptr = vlist_to_pointer(vlistID);
int param = vlistptr->vars[varID].param;
int pdis, pcat, pnum;
cdiDecodeParam(param, &pnum, &pcat, &pdis);
......@@ -445,17 +439,14 @@ void vlistInqVarName(int vlistID, int varID, char *name)
{
int code = pnum;
int tableID = vlistptr->vars[varID].tableID;
name[0] = 0;
tableInqEntry(tableID, code, -1, name, NULL, NULL);
if ( !name[0] ) sprintf(name, "var%d", code);
if (!name[0]) sprintf(name, "var%d", code);
}
else
{
sprintf(name, "param%d.%d.%d", pnum, pcat, pdis);
}
}
else
strcpy(name, vlistptr->vars[varID].name); //FIXME: This may overrun the provided buffer.
return;
}
......@@ -477,27 +468,26 @@ The memory for the returned string is allocated to fit the string via Malloc().
@EndFunction
*/
char* vlistCopyVarName(int vlistId, int varId)
char *vlistCopyVarName(int vlistID, int varID)
{
vlist_t* vlistptr = vlist_to_pointer(vlistId);
vlistCheckVarID(__func__, vlistId, varId);
// If a name is set in the variable description, use that.
char name[CDI_MAX_NAME];
int length = CDI_MAX_NAME;
(void)cdiInqKeyString(vlistID, varID, CDI_KEY_NAME, name, &length);
if (name[0]) return strdup(name);
//If a name is set in the variable description, use that.
{
const char* name = vlistptr->vars[varId].name;
if (name) return strdup(name);
}
vlistCheckVarID(__func__, vlistID, varID);
vlist_t *vlistptr = vlist_to_pointer(vlistID);
//Otherwise we check if we should use the table of parameter descriptions.
int param = vlistptr->vars[varId].param;
// Otherwise we check if we should use the table of parameter descriptions.
int param = vlistptr->vars[varID].param;
int discipline, category, number;
cdiDecodeParam(param, &number, &category, &discipline);
char *result = NULL;
if (discipline == 255)
{
int tableId = vlistptr->vars[varId].tableID;
char name[CDI_MAX_NAME]; name[0] = 0;
tableInqEntry(tableId, number, -1, name, NULL, NULL);
int tableID = vlistptr->vars[varID].tableID;
tableInqEntry(tableID, number, -1, name, NULL, NULL);
if ( name[0] )
result = strdup(name);
else
......@@ -842,22 +832,7 @@ The function @func{vlistDefVarName} defines the name of a variable.
*/
void vlistDefVarName(int vlistID, int varID, const char *name)
{
//(void)cdiDefKeyString(vlistID, varID, CDI_KEY_NAME, name);
vlist_t *vlistptr = vlist_to_pointer(vlistID);
vlistCheckVarID(__func__, vlistID, varID);
if ( name )
{
if ( vlistptr->vars[varID].name )
{
Free(vlistptr->vars[varID].name);
vlistptr->vars[varID].name = NULL;
}
vlistptr->vars[varID].name = strdupx(name);
reshSetStatus(vlistID, &vlistOps, RESH_DESYNC_IN_USE);
}
(void)cdiDefKeyString(vlistID, varID, CDI_KEY_NAME, name);
}
/*
......@@ -1233,18 +1208,6 @@ int vlistInqVarTimave(int vlistID, int varID)
}
void vlistDestroyVarName(int vlistID, int varID)
{
vlist_t *vlistptr = vlist_to_pointer(vlistID);
if ( vlistptr->vars[varID].name )
{
Free(vlistptr->vars[varID].name);
vlistptr->vars[varID].name = NULL;
reshSetStatus(vlistID, &vlistOps, RESH_DESYNC_IN_USE);
}
}
void vlistDestroyVarLongname(int vlistID, int varID)
{
vlist_t *vlistptr = vlist_to_pointer(vlistID);
......@@ -1666,7 +1629,7 @@ int vlistVarCompare(vlist_t *a, int varIDA, vlist_t *b, int varIDB)
| FCMP(datatype) | FCMP(timetype) | FCMP(tsteptype) | FCMP(timave)
| FCMP(chunktype) | FCMP(xyz) | FCMP2(gridID) | FCMP2(zaxisID)
| FCMP2(instID) | FCMP2(modelID) | FCMP2(tableID) | FCMP(missvalused)
| FCMPFLT(missval) | FCMPFLT(addoffset) | FCMPFLT(scalefactor) | FCMPSTR(name)
| FCMPFLT(missval) | FCMPFLT(addoffset) | FCMPFLT(scalefactor)
| FCMPSTR(longname) | FCMPSTR(stdname) | FCMPSTR(units) | FCMPSTR(extra)
| FCMP(comptype) | FCMP(complevel) | FCMP(lvalidrange)
| FCMPFLT(validrange[0]) | FCMPFLT(validrange[1]);
......
......@@ -20,8 +20,6 @@ int vlistInqVarIOrank ( int, int );
void cdiVlistCreateVarLevInfo(vlist_t *vlistptr, int varID);
const char *vlistInqVarNamePtr(int vlistID, int varID);
#endif
/*
* Local Variables:
......
......@@ -11,7 +11,6 @@ enum {
VLISTVAR_PACK_INT_IDX_GRIDID,
VLISTVAR_PACK_INT_IDX_ZAXISID,
VLISTVAR_PACK_INT_IDX_TIMETYPE,
VLISTVAR_PACK_INT_IDX_NAMESZ,
VLISTVAR_PACK_INT_IDX_LONGNAMESZ,
VLISTVAR_PACK_INT_IDX_STDNAMESZ,
VLISTVAR_PACK_INT_IDX_UNITSSZ,
......@@ -39,8 +38,6 @@ int vlistVarGetPackSize(vlist_t *p, int varID, void *context)
var_t *var = p->vars + varID;
int varsize = serializeGetSize(vlistvarNint, CDI_DATATYPE_INT, context)
+ serializeGetSize(vlistvar_ndbls, CDI_DATATYPE_FLT64, context);
if (var->name)
varsize += serializeGetSize((int)strlen(var->name), CDI_DATATYPE_TXT, context);
if (var->longname)
varsize += serializeGetSize((int)strlen(var->longname), CDI_DATATYPE_TXT, context);
if (var->stdname)
......@@ -62,13 +59,12 @@ void vlistVarPack(vlist_t *p, int varID, char * buf, int size, int *position, vo
{
double dtempbuf[vlistvar_ndbls];
var_t *var = p->vars + varID;
int tempbuf[vlistvarNint], namesz, longnamesz, stdnamesz, unitssz, extralen;
int tempbuf[vlistvarNint], longnamesz, stdnamesz, unitssz, extralen;
tempbuf[VLISTVAR_PACK_INT_IDX_FLAG] = var->flag;
tempbuf[VLISTVAR_PACK_INT_IDX_GRIDID] = var->gridID;
tempbuf[VLISTVAR_PACK_INT_IDX_ZAXISID] = var->zaxisID;
tempbuf[VLISTVAR_PACK_INT_IDX_TIMETYPE] = var->timetype;
tempbuf[VLISTVAR_PACK_INT_IDX_NAMESZ] = namesz = var->name?(int)strlen(var->name):0;
tempbuf[VLISTVAR_PACK_INT_IDX_LONGNAMESZ] = longnamesz = var->longname?(int)strlen(var->longname):0;
tempbuf[VLISTVAR_PACK_INT_IDX_STDNAMESZ] = stdnamesz = var->stdname?(int)strlen(var->stdname):0;
tempbuf[VLISTVAR_PACK_INT_IDX_UNITSSZ] = unitssz = var->units?(int)strlen(var->units):0;
......@@ -90,8 +86,6 @@ void vlistVarPack(vlist_t *p, int varID, char * buf, int size, int *position, vo
dtempbuf[2] = var->addoffset;
serializePack(tempbuf, vlistvarNint, CDI_DATATYPE_INT, buf, size, position, context);
serializePack(dtempbuf, vlistvar_ndbls, CDI_DATATYPE_FLT64,buf, size, position, context);
if (namesz)
serializePack(var->name, namesz, CDI_DATATYPE_TXT, buf, size, position, context);
if (longnamesz)
serializePack(var->longname, longnamesz, CDI_DATATYPE_TXT, buf, size, position, context);
if (stdnamesz)
......@@ -142,20 +136,12 @@ void vlistVarUnpack(int vlistID, char * buf, int size, int *position,
namespaceAdaptKey ( tempbuf[VLISTVAR_PACK_INT_IDX_GRIDID], originNamespace ),
namespaceAdaptKey ( tempbuf[VLISTVAR_PACK_INT_IDX_ZAXISID], originNamespace ),
tempbuf[VLISTVAR_PACK_INT_IDX_TIMETYPE]);
if (tempbuf[VLISTVAR_PACK_INT_IDX_NAMESZ] || tempbuf[VLISTVAR_PACK_INT_IDX_LONGNAMESZ] ||
if (tempbuf[VLISTVAR_PACK_INT_IDX_LONGNAMESZ] ||
tempbuf[VLISTVAR_PACK_INT_IDX_STDNAMESZ] || tempbuf[VLISTVAR_PACK_INT_IDX_UNITSSZ] ||
tempbuf[VLISTVAR_PACK_INT_IDX_EXTRALEN])
varname = (char *)Malloc((size_t)imax(imax(imax(imax(tempbuf[VLISTVAR_PACK_INT_IDX_NAMESZ],
tempbuf[VLISTVAR_PACK_INT_IDX_LONGNAMESZ]),
varname = (char *)Malloc((size_t)imax(imax(imax(tempbuf[VLISTVAR_PACK_INT_IDX_LONGNAMESZ],
tempbuf[VLISTVAR_PACK_INT_IDX_STDNAMESZ]),
tempbuf[VLISTVAR_PACK_INT_IDX_UNITSSZ]), tempbuf[VLISTVAR_PACK_INT_IDX_EXTRALEN]) + 1);
if (tempbuf[VLISTVAR_PACK_INT_IDX_NAMESZ])
{
serializeUnpack(buf, size, position,
varname, tempbuf[VLISTVAR_PACK_INT_IDX_NAMESZ], CDI_DATATYPE_TXT, context);
varname[tempbuf[VLISTVAR_PACK_INT_IDX_NAMESZ]] = '\0';
vlistDefVarName(vlistID, newvar, varname);
}
if (tempbuf[VLISTVAR_PACK_INT_IDX_LONGNAMESZ])
{
serializeUnpack(buf, size, position,
......
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