Commit 31bf5054 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

cleanup memory allocation and use field_malloc()

parent 81de265e
......@@ -231,6 +231,7 @@ void *Timstat(void *argument)
gridsize = vlistGridsizeMax(vlistID1);
if ( vlistNumber(vlistID1) != CDI_REAL ) gridsize *= 2;
field_init(&field);
field.ptr = (double *) malloc(gridsize*sizeof(double));
vars1 = field_malloc(vlistID1, FIELD_PTR);
......
......@@ -113,11 +113,17 @@ void *Timstat3(void *argument)
for ( i = 0; i < NIN; ++i ) reached_eof[i] = 0;
for ( i = 0; i < NIN; ++i )
in[i].ptr = (double *) malloc(gridsize*sizeof(double));
{
field_init(&in[i]);
in[i].ptr = (double *) malloc(gridsize*sizeof(double));
}
for ( i = 0; i < NOUT; ++i )
out[i].ptr = (double *) malloc(gridsize*sizeof(double));
{
field_init(&out[i]);
out[i].ptr = (double *) malloc(gridsize*sizeof(double));
}
for ( iw = 0; iw < NFWORK; ++iw )
fwork[iw] = (field_t **) malloc(nvars*sizeof(field_t *));
for ( iw = 0; iw < NIWORK; ++iw )
......@@ -140,6 +146,7 @@ void *Timstat3(void *argument)
{
for ( iw = 0; iw < NFWORK; ++iw )
{
field_init(&fwork[iw][varID][levelID]);
fwork[iw][varID][levelID].grid = gridID;
fwork[iw][varID][levelID].nmiss = 0;
fwork[iw][varID][levelID].missval = missval;
......
......@@ -184,6 +184,10 @@ void *Trms(void *argument)
vardata2[varID] = (double *) malloc(gridsize*nlevel*sizeof(double));
}
field_init(&field1);
field_init(&field2);
field_init(&field3);
lim = vlistGridsizeMax(vlistID1);
field1.weight = NULL;
if ( needWeights )
......
......@@ -197,8 +197,7 @@ void *Tstepcount(void *argument)
}
}
for ( tsID = 0; tsID < nts; tsID++ )
field_free(vars[tsID], vlistID1);
for ( tsID = 0; tsID < nts; tsID++ ) field_free(vars[tsID], vlistID1);
if ( vars ) free(vars);
......
......@@ -108,6 +108,10 @@ void *Varrms(void *argument)
vardata2[varID] = (double *) malloc(gridsize*nlevel*sizeof(double));
}
field_init(&field1);
field_init(&field2);
field_init(&field2);
lim = vlistGridsizeMax(vlistID1);
field1.weight = NULL;
if ( needWeights )
......
......@@ -93,6 +93,7 @@ void *Vertstat(void *argument)
gridsize = vlistGridsizeMax(vlistID1);
field_init(&field);
field.ptr = (double *) malloc(gridsize*sizeof(double));
vars1 = (field_t *) malloc(nvars*sizeof(field_t));
......@@ -106,6 +107,8 @@ void *Vertstat(void *argument)
gridsize = gridInqSize(gridID);
missval = vlistInqVarMissval(vlistID1, varID);
field_init(&vars1[varID]);
field_init(&samp1[varID]);
vars1[varID].grid = gridID;
vars1[varID].nsamp = 0;
vars1[varID].nmiss = 0;
......@@ -117,6 +120,7 @@ void *Vertstat(void *argument)
samp1[varID].ptr = NULL;
if ( operfunc == func_std || operfunc == func_var )
{
field_init(&vars2[varID]);
vars2[varID].grid = gridID;
vars2[varID].nmiss = 0;
vars2[varID].missval = missval;
......
......@@ -108,6 +108,9 @@ void *Wct(void *argument)
vlistCompare(vlistID1, vlistID2, CMP_DIM);
gridsize = vlistGridsizeMax(vlistID1);
field_init(&field1);
field_init(&field2);
field1.ptr = (double *) malloc(gridsize*sizeof(double));
field2.ptr = (double *) malloc(gridsize*sizeof(double));
......
......@@ -782,6 +782,9 @@ void *YAR(void *argument)
operatorInputArg("grid description file or name");
gridID2 = cdoDefineGrid(operatorArgv()[0]);
field_init(&field1);
field_init(&field2);
streamID1 = streamOpenRead(cdoStreamName(0));
vlistID1 = streamInqVlist(streamID1);
......
......@@ -70,6 +70,8 @@ void *Ydayarith(void *argument)
gridsize = vlistGridsizeMax(vlistID1);
field_init(&field1);
field_init(&field2);
field1.ptr = (double *) malloc(gridsize*sizeof(double));
field2.ptr = (double *) malloc(gridsize*sizeof(double));
......
......@@ -51,7 +51,6 @@ void *Ydaypctl(void *argument)
int *recVarID, *recLevelID;
int vdates1[NDAY], vtimes1[NDAY];
int vdates2[NDAY], vtimes2[NDAY];
double missval;
field_t **vars1[NDAY];
field_t field;
double pn;
......@@ -105,6 +104,7 @@ void *Ydaypctl(void *argument)
recLevelID = (int *) malloc(nrecords*sizeof(int));
gridsize = vlistGridsizeMax(vlistID1);
field_init(&field);
field.ptr = (double *) malloc(gridsize*sizeof(double));
tsID = 0;
......@@ -136,26 +136,15 @@ void *Ydaypctl(void *argument)
if ( vars1[dayoy] == NULL )
{
vars1[dayoy] = (field_t **) malloc(nvars*sizeof(field_t *));
vars1[dayoy] = field_malloc(vlistID1, FIELD_PTR);
hsets[dayoy] = hsetCreate(nvars);
for ( varID = 0; varID < nvars; varID++ )
{
gridID = vlistInqVarGrid(vlistID1, varID);
gridsize = gridInqSize(gridID);
nlevels = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
missval = vlistInqVarMissval(vlistID1, varID);
vars1[dayoy][varID] = (field_t *) malloc(nlevels*sizeof(field_t));
hsetCreateVarLevels(hsets[dayoy], varID, nlevels, gridID);
for ( levelID = 0; levelID < nlevels; levelID++ )
{
vars1[dayoy][varID][levelID].grid = gridID;
vars1[dayoy][varID][levelID].nmiss = 0;
vars1[dayoy][varID][levelID].missval = missval;
vars1[dayoy][varID][levelID].ptr = (double *) malloc(gridsize*sizeof(double));
}
}
}
......@@ -263,14 +252,7 @@ void *Ydaypctl(void *argument)
{
if ( vars1[dayoy] != NULL )
{
for ( varID = 0; varID < nvars; varID++ )
{
nlevels = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
for ( levelID = 0; levelID < nlevels; levelID++ )
free(vars1[dayoy][varID][levelID].ptr);
free(vars1[dayoy][varID]);
}
free(vars1[dayoy]);
field_free(vars1[dayoy], vlistID1);
hsetDestroy(hsets[dayoy]);
}
}
......
......@@ -113,6 +113,7 @@ void *Ydaystat(void *argument)
recLevelID = (int *) malloc(nrecords*sizeof(int));
gridsize = vlistGridsizeMax(vlistID1);
field_init(&field);
field.ptr = (double *) malloc(gridsize*sizeof(double));
tsID = 0;
......
......@@ -91,6 +91,7 @@ void *Yearmonstat(void *argument)
gridsize = vlistGridsizeMax(vlistID1);
field_init(&field);
field.ptr = (double *) malloc(gridsize*sizeof(double));
vars1 = field_malloc(vlistID1, FIELD_PTR);
......
......@@ -97,6 +97,8 @@ void *Yhourarith(void *argument)
gridsize = vlistGridsizeMax(vlistID1);
field_init(&field1);
field_init(&field2);
field1.ptr = (double *) malloc(gridsize*sizeof(double));
field2.ptr = (double *) malloc(gridsize*sizeof(double));
......
......@@ -138,6 +138,7 @@ void *Yhourstat(void *argument)
recLevelID = (int *) malloc(nrecords*sizeof(int));
gridsize = vlistGridsizeMax(vlistID1);
field_init(&field);
field.ptr = (double *) malloc(gridsize*sizeof(double));
tsID = 0;
......
......@@ -70,6 +70,8 @@ void *Ymonarith(void *argument)
gridsize = vlistGridsizeMax(vlistID1);
field_init(&field1);
field_init(&field2);
field1.ptr = (double *) malloc(gridsize*sizeof(double));
field2.ptr = (double *) malloc(gridsize*sizeof(double));
......
......@@ -49,7 +49,6 @@ void *Ymonpctl(void *argument)
int *recVarID, *recLevelID;
int vdates1[NMONTH], vtimes1[NMONTH];
int vdates2[NMONTH], vtimes2[NMONTH];
double missval;
field_t **vars1[NMONTH];
field_t field;
double pn;
......@@ -103,6 +102,7 @@ void *Ymonpctl(void *argument)
recLevelID = (int *) malloc(nrecords*sizeof(int));
gridsize = vlistGridsizeMax(vlistID1);
field_init(&field);
field.ptr = (double *) malloc(gridsize*sizeof(double));
tsID = 0;
......@@ -128,26 +128,15 @@ void *Ymonpctl(void *argument)
if ( vars1[month] == NULL )
{
vars1[month] = (field_t **) malloc(nvars*sizeof(field_t *));
vars1[month] = field_malloc(vlistID1, FIELD_PTR);
hsets[month] = hsetCreate(nvars);
for ( varID = 0; varID < nvars; varID++ )
{
gridID = vlistInqVarGrid(vlistID1, varID);
gridsize = gridInqSize(gridID);
nlevels = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
missval = vlistInqVarMissval(vlistID1, varID);
vars1[month][varID] = (field_t *) malloc(nlevels*sizeof(field_t));
hsetCreateVarLevels(hsets[month], varID, nlevels, gridID);
for ( levelID = 0; levelID < nlevels; levelID++ )
{
vars1[month][varID][levelID].grid = gridID;
vars1[month][varID][levelID].nmiss = 0;
vars1[month][varID][levelID].missval = missval;
vars1[month][varID][levelID].ptr = (double *) malloc(gridsize*sizeof(double));
}
}
}
......@@ -249,14 +238,7 @@ void *Ymonpctl(void *argument)
{
if ( vars1[month] != NULL )
{
for ( varID = 0; varID < nvars; varID++ )
{
nlevels = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
for ( levelID = 0; levelID < nlevels; levelID++ )
free(vars1[month][varID][levelID].ptr);
free(vars1[month][varID]);
}
free(vars1[month]);
field_free(vars1[month], vlistID1);
hsetDestroy(hsets[month]);
}
}
......
......@@ -130,6 +130,7 @@ void *Ymonstat(void *argument)
recLevelID = (int *) malloc(nrecords*sizeof(int));
gridsize = vlistGridsizeMax(vlistID1);
field_init(&field);
field.ptr = (double *) malloc(gridsize*sizeof(double));
tsID = 0;
......
......@@ -50,7 +50,6 @@ void *Yseaspctl(void *argument)
int *recVarID, *recLevelID;
int vdates1[NSEAS], vtimes1[NSEAS];
int vdates2[NSEAS], vtimes2[NSEAS];
double missval;
field_t **vars1[NSEAS];
field_t field;
double pn;
......@@ -106,6 +105,7 @@ void *Yseaspctl(void *argument)
recLevelID = (int *) malloc(nrecords*sizeof(int));
gridsize = vlistGridsizeMax(vlistID1);
field_init(&field);
field.ptr = (double *) malloc(gridsize*sizeof(double));
tsID = 0;
......@@ -149,26 +149,15 @@ void *Yseaspctl(void *argument)
if ( vars1[seas] == NULL )
{
vars1[seas] = (field_t **) malloc(nvars*sizeof(field_t *));
vars1[seas] = field_malloc(vlistID1, FIELD_PTR);
hsets[seas] = hsetCreate(nvars);
for ( varID = 0; varID < nvars; varID++ )
{
gridID = vlistInqVarGrid(vlistID1, varID);
gridsize = gridInqSize(gridID);
nlevels = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
missval = vlistInqVarMissval(vlistID1, varID);
vars1[seas][varID] = (field_t *) malloc(nlevels*sizeof(field_t));
hsetCreateVarLevels(hsets[seas], varID, nlevels, gridID);
for ( levelID = 0; levelID < nlevels; levelID++ )
{
vars1[seas][varID][levelID].grid = gridID;
vars1[seas][varID][levelID].nmiss = 0;
vars1[seas][varID][levelID].missval = missval;
vars1[seas][varID][levelID].ptr = (double *) malloc(gridsize*sizeof(double));
}
}
}
......@@ -275,14 +264,7 @@ void *Yseaspctl(void *argument)
{
if ( vars1[seas] != NULL )
{
for ( varID = 0; varID < nvars; varID++ )
{
nlevels = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
for ( levelID = 0; levelID < nlevels; levelID++ )
free(vars1[seas][varID][levelID].ptr);
free(vars1[seas][varID]);
}
free(vars1[seas]);
field_free(vars1[seas], vlistID1);
hsetDestroy(hsets[seas]);
}
}
......
......@@ -67,7 +67,6 @@ void *Yseasstat(void *argument)
int i;
int varID;
int recID;
int gridID;
int vdate, vtime;
int year, month, day, seas;
int nrecs, nrecords;
......@@ -81,7 +80,6 @@ void *Yseasstat(void *argument)
int nvars, nlevel;
int *recVarID, *recLevelID;
date_time_t datetime[NSEAS];
double missval;
field_t **vars1[NSEAS], **vars2[NSEAS], **samp1[NSEAS];
field_t field;
int season_start;
......@@ -131,6 +129,7 @@ void *Yseasstat(void *argument)
recLevelID = (int *) malloc(nrecords*sizeof(int));
gridsize = vlistGridsizeMax(vlistID1);
field_init(&field);
field.ptr = (double *) malloc(gridsize*sizeof(double));
tsID = 0;
......@@ -165,42 +164,10 @@ void *Yseasstat(void *argument)
if ( vars1[seas] == NULL )
{
vars1[seas] = (field_t **) malloc(nvars*sizeof(field_t *));
samp1[seas] = (field_t **) malloc(nvars*sizeof(field_t *));
vars1[seas] = field_malloc(vlistID1, FIELD_PTR);
samp1[seas] = field_malloc(vlistID1, FIELD_NONE);
if ( operfunc == func_std || operfunc == func_var )
vars2[seas] = (field_t **) malloc(nvars*sizeof(field_t *));
for ( varID = 0; varID < nvars; varID++ )
{
gridID = vlistInqVarGrid(vlistID1, varID);
gridsize = gridInqSize(gridID);
nlevel = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
missval = vlistInqVarMissval(vlistID1, varID);
vars1[seas][varID] = (field_t *) malloc(nlevel*sizeof(field_t));
samp1[seas][varID] = (field_t *) malloc(nlevel*sizeof(field_t));
if ( operfunc == func_std || operfunc == func_var )
vars2[seas][varID] = (field_t *) malloc(nlevel*sizeof(field_t));
for ( levelID = 0; levelID < nlevel; levelID++ )
{
vars1[seas][varID][levelID].grid = gridID;
vars1[seas][varID][levelID].nmiss = 0;
vars1[seas][varID][levelID].missval = missval;
vars1[seas][varID][levelID].ptr = (double *) malloc(gridsize*sizeof(double));
samp1[seas][varID][levelID].grid = gridID;
samp1[seas][varID][levelID].nmiss = 0;
samp1[seas][varID][levelID].missval = missval;
samp1[seas][varID][levelID].ptr = NULL;
if ( operfunc == func_std || operfunc == func_var )
{
vars2[seas][varID][levelID].grid = gridID;
vars2[seas][varID][levelID].nmiss = 0;
vars2[seas][varID][levelID].missval = missval;
vars2[seas][varID][levelID].ptr = (double *) malloc(gridsize*sizeof(double));
}
}
}
vars2[seas] = field_malloc(vlistID1, FIELD_PTR);
}
for ( recID = 0; recID < nrecs; recID++ )
......@@ -343,23 +310,8 @@ void *Yseasstat(void *argument)
{
if ( vars1[seas] != NULL )
{
for ( varID = 0; varID < nvars; varID++ )
{
nlevel = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
for ( levelID = 0; levelID < nlevel; levelID++ )
{
free(vars1[seas][varID][levelID].ptr);
if ( samp1[seas][varID][levelID].ptr ) free(samp1[seas][varID][levelID].ptr);
if ( operfunc == func_std || operfunc == func_var ) free(vars2[seas][varID][levelID].ptr);
}
free(vars1[seas][varID]);
free(samp1[seas][varID]);
if ( operfunc == func_std || operfunc == func_var ) free(vars2[seas][varID]);
}
free(vars1[seas]);
free(samp1[seas]);
field_free(vars1[seas], vlistID1);
field_free(samp1[seas], vlistID1);
if ( operfunc == func_std || operfunc == func_var ) free(vars2[seas]);
}
}
......
......@@ -140,6 +140,8 @@ void *Zonstat(void *argument)
nlatmax = gridInqYsize(gridID1); /* max nlat ? */
lim = vlistGridsizeMax(vlistID1);
field_init(&field2);
field_init(&field2);
field1.ptr = (double *) malloc(lim*sizeof(double));
field2.ptr = (double *) malloc(nlatmax*sizeof(double));
field2.grid = gridID2;
......
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