Commit bc0b2661 authored by Thomas Jahns's avatar Thomas Jahns 🤸
Browse files

Defer creation of levinfo until needed.

parent 6559e6f4
......@@ -294,7 +294,7 @@ void vlistCopy(int vlistID2, int vlistID1)
if ( vlistptr1->vars )
{
int nvars = vlistptr1->nvars;
int nlevs, varID;
int varID;
//vlistptr2->varsAllocated = nvars;
vlistptr2->vars = (var_t *) malloc(vlistptr2->varsAllocated*sizeof(var_t));
......@@ -345,10 +345,13 @@ void vlistCopy(int vlistID2, int vlistID1)
vlistptr2->vars[varID].atts.nelems = 0;
vlistCopyVarAtts(vlistID1, varID, vlistID2, varID);
nlevs = vlistptr1->vars[varID].nlevs;
vlistptr2->vars[varID].levinfo = (levinfo_t *) malloc(nlevs*sizeof(levinfo_t));
memcpy(vlistptr2->vars[varID].levinfo,
vlistptr1->vars[varID].levinfo, nlevs*sizeof(levinfo_t));
if ( vlistptr1->vars[varID].levinfo )
{
int nlevs = vlistptr1->vars[varID].nlevs;
vlistptr2->vars[varID].levinfo = (levinfo_t *) malloc(nlevs*sizeof(levinfo_t));
memcpy(vlistptr2->vars[varID].levinfo,
vlistptr1->vars[varID].levinfo, nlevs*sizeof(levinfo_t));
}
}
}
}
......@@ -396,10 +399,11 @@ void vlistClearFlag(int vlistID)
for ( varID = 0; varID < vlistptr->nvars; varID++ )
{
vlistptr->vars[varID].flag = FALSE;
for ( levID = 0; levID < vlistptr->vars[varID].nlevs; levID++ )
{
vlistptr->vars[varID].levinfo[levID].flag = FALSE;
}
if ( vlistptr->vars[varID].levinfo )
for ( levID = 0; levID < vlistptr->vars[varID].nlevs; levID++ )
{
vlistptr->vars[varID].levinfo[levID].flag = FALSE;
}
}
}
......@@ -595,8 +599,9 @@ void vlistCopyFlag(int vlistID2, int vlistID1)
nlevs = vlistptr1->vars[varID].nlevs;
nlevs2 = 0;
for ( levID = 0; levID < nlevs; levID++ )
if ( vlistptr1->vars[varID].levinfo[levID].flag ) nlevs2++;
if ( vlistptr1->vars[varID].levinfo )
for ( levID = 0; levID < nlevs; levID++ )
if ( vlistptr1->vars[varID].levinfo[levID].flag ) nlevs2++;
vlistptr2->vars[varID2].levinfo = (levinfo_t *) malloc(nlevs2*sizeof(levinfo_t));
......@@ -613,6 +618,8 @@ void vlistCopyFlag(int vlistID2, int vlistID1)
zaxisID = vlistptr1->vars[varID].zaxisID;
levels = (double *) malloc(nlevs2*sizeof(double));
levID2 = 0;
if (!vlistptr1->vars[varID].levinfo)
cdiVlistCreateVarLevInfo(vlistptr1, varID);
for ( levID = 0; levID < nlevs; ++levID )
if ( vlistptr1->vars[varID].levinfo[levID].flag )
{
......@@ -780,8 +787,11 @@ void vlistCat(int vlistID2, int vlistID1)
vlistptr2->vars[varID2].units = strdupx(vlistptr1->vars[varID].units);
nlevs = vlistptr1->vars[varID].nlevs;
vlistptr2->vars[varID2].levinfo = (levinfo_t *) malloc(nlevs*sizeof(levinfo_t));
memcpy(vlistptr2->vars[varID2].levinfo, vlistptr1->vars[varID].levinfo, nlevs*sizeof(levinfo_t));
if (vlistptr1->vars[varID].levinfo)
{
vlistptr2->vars[varID2].levinfo = (levinfo_t *) malloc(nlevs*sizeof(levinfo_t));
memcpy(vlistptr2->vars[varID2].levinfo, vlistptr1->vars[varID].levinfo, nlevs*sizeof(levinfo_t));
}
if ( vlistptr1->vars[varID].ensdata )
{
......@@ -907,12 +917,16 @@ void vlistMerge(int vlistID2, int vlistID1)
/*
fprintf(stderr, "var %d %d %d %d %d\n", varID, nlevs1, nlevs2, nlevs, sizeof(levinfo_t));
*/
vlistptr2->vars[varID].levinfo =
(levinfo_t *) realloc(vlistptr2->vars[varID].levinfo, nlevs*sizeof(levinfo_t));
memcpy(vlistptr2->vars[varID].levinfo+nlevs2,
vlistptr1->vars[varID].levinfo, nlevs1*sizeof(levinfo_t));
if (vlistptr1->vars[varID].levinfo)
{
vlistptr2->vars[varID].levinfo =
xrealloc(vlistptr2->vars[varID].levinfo, nlevs*sizeof(levinfo_t));
memcpy(vlistptr2->vars[varID].levinfo+nlevs2,
vlistptr1->vars[varID].levinfo, nlevs1*sizeof(levinfo_t));
}
else
cdiVlistCreateVarLevInfo(vlistptr1, varID);
for ( levID = 0; levID < nlevs1; levID++ )
{
vlistptr1->vars[varID].levinfo[levID].mlevelID = nlevs2 + levID;
......@@ -1203,11 +1217,17 @@ vlistPrintKernel(vlist_t *vlistptr, FILE * fp )
dtype = vlistptr->vars[varID].datatype;
for ( levID = 0; levID < nlevs; levID++ )
{
flevID = vlistptr->vars[varID].levinfo[levID].flevelID;
mlevID = vlistptr->vars[varID].levinfo[levID].mlevelID;
index = vlistptr->vars[varID].levinfo[levID].index;
flag = vlistptr->vars[varID].levinfo[levID].flag;
levinfo_t li;
if (vlistptr->vars[varID].levinfo)
li = vlistptr->vars[varID].levinfo[levID];
else
li = DEFAULT_LEVINFO(levID);
flevID = li.flevelID;
mlevID = li.mlevelID;
index = li.index;
flag = li.flag;
level = zaxisInqLevel(zaxisID, levID);
fprintf(fp, "%6d %6d %6d %6d %6d %6d %6d %6d %5d %.9g\n",
varID, levID, fvarID, flevID, mvarID, mlevID, index,
dtype, flag, level);
......@@ -1594,19 +1614,15 @@ void vlistChangeZaxisIndex(int vlistID, int index, int zaxisID)
vlistptr->vars[varID].zaxisID = zaxisID;
nlevs = zaxisInqSize(zaxisID);
if ( nlevs != vlistptr->vars[varID].nlevs )
if ( vlistptr->vars[varID].levinfo
&& nlevs != vlistptr->vars[varID].nlevs )
{
vlistptr->vars[varID].nlevs = nlevs;
vlistptr->vars[varID].levinfo = (levinfo_t *) realloc(vlistptr->vars[varID].levinfo,
nlevs*sizeof(levinfo_t));
for ( levID = 0; levID < nlevs; levID++ )
{
vlistptr->vars[varID].levinfo[levID].flevelID = levID;
vlistptr->vars[varID].levinfo[levID].mlevelID = levID;
vlistptr->vars[varID].levinfo[levID].index = -1;
vlistptr->vars[varID].levinfo[levID].flag = FALSE;
}
vlistptr->vars[varID].levinfo[levID] = DEFAULT_LEVINFO(levID);
}
}
}
......@@ -1646,19 +1662,15 @@ void vlistChangeZaxis(int vlistID, int zaxisID1, int zaxisID2)
vlistptr->vars[varID].zaxisID = zaxisID2;
nlevs = zaxisInqSize(zaxisID2);
if ( nlevs != vlistptr->vars[varID].nlevs )
if ( vlistptr->vars[varID].levinfo
&& nlevs != vlistptr->vars[varID].nlevs )
{
vlistptr->vars[varID].nlevs = nlevs;
vlistptr->vars[varID].levinfo = (levinfo_t *) realloc(vlistptr->vars[varID].levinfo,
nlevs*sizeof(levinfo_t));
for ( levID = 0; levID < nlevs; levID++ )
{
vlistptr->vars[varID].levinfo[levID].flevelID = levID;
vlistptr->vars[varID].levinfo[levID].mlevelID = levID;
vlistptr->vars[varID].levinfo[levID].index = -1;
vlistptr->vars[varID].levinfo[levID].flag = FALSE;
}
vlistptr->vars[varID].levinfo[levID] = DEFAULT_LEVINFO(levID);
}
}
}
......
......@@ -47,6 +47,8 @@ typedef struct
}
levinfo_t;
#define DEFAULT_LEVINFO(levID) \
(levinfo_t){ .flag = 0, .index = -1, .flevelID = levID, .mlevelID = levID}
typedef struct
{
......
......@@ -200,8 +200,6 @@ vlistDestroy(vlistID);
int vlistDefVar(int vlistID, int gridID, int zaxisID, int tsteptype)
{
int varID;
int nlevs;
int levID;
int index;
vlist_t *vlistptr;
......@@ -229,19 +227,7 @@ int vlistDefVar(int vlistID, int gridID, int zaxisID, int tsteptype)
vlistptr->vars[varID].tsteptype = TSTEP_INSTANT;
}
nlevs = zaxisInqSize(zaxisID);
vlistptr->vars[varID].levinfo = (levinfo_t *) malloc(nlevs*sizeof(levinfo_t));
for ( levID = 0; levID < nlevs; levID++ )
{
vlistptr->vars[varID].levinfo[levID].flag = 0;
vlistptr->vars[varID].levinfo[levID].index = -1;
vlistptr->vars[varID].levinfo[levID].flevelID = levID;
vlistptr->vars[varID].levinfo[levID].mlevelID = levID;
}
vlistptr->vars[varID].nlevs = nlevs;
vlistptr->vars[varID].nlevs = zaxisInqSize(zaxisID);;
for ( index = 0; index < vlistptr->ngrids; index++ )
if ( gridID == vlistptr->gridIDs[index] ) break;
......@@ -272,6 +258,20 @@ int vlistDefVar(int vlistID, int gridID, int zaxisID, int tsteptype)
return (varID);
}
void
cdiVlistCreateVarLevInfo(vlist_t *vlistptr, int varID)
{
xassert(varID >= 0 && varID < vlistptr->nvars
&& vlistptr->vars[varID].levinfo == NULL);
int zaxisID = vlistptr->vars[varID].zaxisID;
int nlevs = zaxisInqSize(zaxisID);
vlistptr->vars[varID].levinfo = malloc(nlevs * sizeof(levinfo_t));
for (int levID = 0; levID < nlevs; levID++ )
vlistptr->vars[varID].levinfo[levID] = DEFAULT_LEVINFO(levID);
}
/*
@Function vlistDefVarParam
@Title Define the parameter number of a Variable
......@@ -1533,6 +1533,14 @@ void vlistDefFlag(int vlistID, int varID, int levID, int flag)
vlistptr = vlist_to_pointer(vlistID);
vlistptr->vars[varID].flag = flag;
levinfo_t li = DEFAULT_LEVINFO(levID);
if (vlistptr->vars[varID].levinfo)
;
else if (flag != li.flag)
cdiVlistCreateVarLevInfo(vlistptr, varID);
else
return;
vlistptr->vars[varID].levinfo[levID].flag = flag;
}
......@@ -1543,7 +1551,13 @@ int vlistInqFlag(int vlistID, int varID, int levID)
vlistptr = vlist_to_pointer(vlistID);
return (vlistptr->vars[varID].levinfo[levID].flag);
if (vlistptr->vars[varID].levinfo)
return (vlistptr->vars[varID].levinfo[levID].flag);
else
{
levinfo_t li = DEFAULT_LEVINFO(levID);
return li.flag;
}
}
......@@ -1594,7 +1608,7 @@ int vlistFindLevel(int vlistID, int fvarID, int flevelID)
}
}
return (levelID);
return (levelID);
}
......@@ -1604,7 +1618,7 @@ int vlistMergedVar(int vlistID, int varID)
vlistptr = vlist_to_pointer(vlistID);
return (vlistptr->vars[varID].mvarID);
return (vlistptr->vars[varID].mvarID);
}
......@@ -1614,7 +1628,13 @@ int vlistMergedLevel(int vlistID, int varID, int levelID)
vlistptr = vlist_to_pointer(vlistID);
return (vlistptr->vars[varID].levinfo[levelID].mlevelID);
if (vlistptr->vars[varID].levinfo)
return vlistptr->vars[varID].levinfo[levelID].mlevelID;
else
{
levinfo_t li = DEFAULT_LEVINFO(levelID);
return li.mlevelID;
}
}
......@@ -1630,7 +1650,14 @@ void vlistDefIndex(int vlistID, int varID, int levelID, int index)
vlistptr = vlist_to_pointer(vlistID);
vlistptr->vars[varID].levinfo[levelID].index = index;
levinfo_t li = DEFAULT_LEVINFO(levelID);
if (vlistptr->vars[varID].levinfo)
;
else if (index != li.index)
cdiVlistCreateVarLevInfo(vlistptr, varID);
else
return;
vlistptr->vars[varID].levinfo[levelID].index = index;
}
......@@ -1640,7 +1667,13 @@ int vlistInqIndex(int vlistID, int varID, int levelID)
vlistptr = vlist_to_pointer(vlistID);
return (vlistptr->vars[varID].levinfo[levelID].index);
if (vlistptr->vars[varID].levinfo)
return (vlistptr->vars[varID].levinfo[levelID].index);
else
{
levinfo_t li = DEFAULT_LEVINFO(levelID);
return li.index;
}
}
......@@ -2101,7 +2134,7 @@ void vlistVarPack(vlist_t *p, int varID, char * buf, int size, int *position,
{
double dtempbuf[vlistvar_ndbls];
var_t *var = p->vars + varID;
int tempbuf[vlistvar_nints], namesz, longnamesz, stdnamesz, unitssz, i;
int tempbuf[vlistvar_nints], namesz, longnamesz, stdnamesz, unitssz;
tempbuf[0] = var->flag;
tempbuf[1] = var->gridID;
......@@ -2143,13 +2176,23 @@ void vlistVarPack(vlist_t *p, int varID, char * buf, int size, int *position,
buf, size, position, context);
{
int levbuf[var->nlevs][4];
for (i = 0; i < var->nlevs; ++i)
{
levbuf[i][0] = var->levinfo[i].flag;
levbuf[i][1] = var->levinfo[i].index;
levbuf[i][2] = var->levinfo[i].mlevelID;
levbuf[i][3] = var->levinfo[i].flevelID;
}
if (var->levinfo)
for (int levID = 0; levID < var->nlevs; ++levID)
{
levbuf[levID][0] = var->levinfo[levID].flag;
levbuf[levID][1] = var->levinfo[levID].index;
levbuf[levID][2] = var->levinfo[levID].mlevelID;
levbuf[levID][3] = var->levinfo[levID].flevelID;
}
else
for (int levID = 0; levID < var->nlevs; ++levID)
{
levinfo_t li = DEFAULT_LEVINFO(levID);
levbuf[levID][0] = li.flag;
levbuf[levID][1] = li.index;
levbuf[levID][2] = li.mlevelID;
levbuf[levID][3] = li.flevelID;
}
serializePack(levbuf, var->nlevs * 4, DATATYPE_INT,
buf, size, position, context);
}
......@@ -2170,6 +2213,7 @@ void vlistVarUnpack(int vlistID, char * buf, int size, int *position,
int tempbuf[vlistvar_nints];
int newvar;
char *varname = NULL;
vlist_t *vlistptr = vlist_to_pointer(vlistID);
serializeUnpack(buf, size, position,
tempbuf, vlistvar_nints, DATATYPE_INT, context);
serializeUnpack(buf, size, position,
......@@ -2229,9 +2273,10 @@ void vlistVarUnpack(int vlistID, char * buf, int size, int *position,
vlistDefVarCompLevel(vlistID, newvar, tempbuf[17]);
{
int levbuf[tempbuf[18]][4];
var_t *var = vlist_to_pointer(vlistID)->vars + newvar;
var_t *var = vlistptr->vars + newvar;
int nlevs=tempbuf[18], i, flagSetLev = 0;
xassert(nlevs == var->nlevs);
cdiVlistCreateVarLevInfo(vlistptr, newvar);
serializeUnpack(buf, size, position,
levbuf, nlevs * 4, DATATYPE_INT, context);
for (i = 0; i < nlevs; ++i)
......
......@@ -20,6 +20,8 @@ void vlistVarUnpack(int vlistID,
void vlistDefVarIOrank ( int, int, int );
int vlistInqVarIOrank ( int, int );
void cdiVlistCreateVarLevInfo(vlist_t *vlistptr, int varID);
#endif
/*
* Local Variables:
......
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