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

refactor field_malloc

parent 3886b947
......@@ -270,7 +270,6 @@ void *Timstat(void *argument)
{
streamReadRecord(streamID1, vars1[varID][levelID].ptr, &nmiss);
vars1[varID][levelID].nmiss = nmiss;
if ( nmiss > 0 || samp1[varID][levelID].ptr )
{
if ( samp1[varID][levelID].ptr == NULL )
......@@ -288,7 +287,6 @@ void *Timstat(void *argument)
streamReadRecord(streamID1, field.ptr, &field.nmiss);
field.grid = vars1[varID][levelID].grid;
field.missval = vars1[varID][levelID].missval;
if ( field.nmiss > 0 || samp1[varID][levelID].ptr )
{
if ( samp1[varID][levelID].ptr == NULL )
......
......@@ -20,10 +20,13 @@
double var_to_std(double rvar, double missval);
#define FIELD_NONE 0
#define FIELD_ALL 1
#define FIELD_PTR 2
#define FILED_WGT 3
enum field_flag {
FIELD_NONE = 1,
FIELD_PTR = 2,
FIELD_WGT = 4,
FIELD_PTR2 = 8,
FIELD_ALL = FIELD_PTR | FIELD_WGT
};
#define FADD(x,y) (DBL_IS_EQUAL((x),missval1) || DBL_IS_EQUAL((y),missval2) ? missval1 : (x)+(y))
......
......@@ -90,7 +90,6 @@ void faradd(field_t *field1, field_t field2)
const int nmiss1 = field1->nmiss;
const double missval1 = field1->missval;
double *array1 = field1->ptr;
/* double *weight1 = field1->weight; */
const int grid2 = field2.grid;
const int nmiss2 = field2.nmiss;
const double missval2 = field2.missval;
......@@ -127,7 +126,6 @@ void farsum(field_t *field1, field_t field2)
const int nmiss1 = field1->nmiss;
const double missval1 = field1->missval;
double *array1 = field1->ptr;
/* double *weight1 = field1->weight; */
const int grid2 = field2.grid;
const int nmiss2 = field2.nmiss;
const double missval2 = field2.missval;
......@@ -170,7 +168,6 @@ void farsumw(field_t *field1, field_t field2, double w)
const int nmiss1 = field1->nmiss;
const double missval1 = field1->missval;
double *array1 = field1->ptr;
/* double *weight1 = field1->weight; */
const int grid2 = field2.grid;
const int nmiss2 = field2.nmiss;
const double missval2 = field2.missval;
......@@ -267,7 +264,6 @@ void farsumq(field_t *field1, field_t field2)
const int nmiss1 = field1->nmiss;
const double missval1 = field1->missval;
double *array1 = field1->ptr;
/* double *weight1 = field1->weight; */
const int grid2 = field2.grid;
const int nmiss2 = field2.nmiss;
const double missval2 = field2.missval;
......@@ -311,7 +307,6 @@ void farsumqw(field_t *field1, field_t field2, double w)
const int nmiss1 = field1->nmiss;
const double missval1 = field1->missval;
double *array1 = field1->ptr;
/* double *weight1 = field1->weight; */
const int grid2 = field2.grid;
const int nmiss2 = field2.nmiss;
const double missval2 = field2.missval;
......@@ -968,7 +963,6 @@ void farcount(field_t *field1, field_t field2)
const int nmiss1 = field1->nmiss;
const double missval1 = field1->missval;
double *array1 = field1->ptr;
/* double *weight1 = field1->weight; */
const int grid2 = field2.grid;
const int nmiss2 = field2.nmiss;
const double missval2 = field2.missval;
......
......@@ -16,28 +16,22 @@ void field_init(field_t *field)
field_t **field_allocate(int vlistID, int ptype, int init)
{
int nvars, nlevel;
int varID, zaxisID, levelID;
int gridID, gridsize;
int nwpv; // number of words per value; real:1 complex:2
double missval;
field_t **field;
int nvars = vlistNvars(vlistID);
nvars = vlistNvars(vlistID);
field_t **field = (field_t **) Malloc(nvars*sizeof(field_t *));
field = (field_t **) Malloc(nvars*sizeof(field_t *));
for ( varID = 0; varID < nvars; ++varID )
for ( int varID = 0; varID < nvars; ++varID )
{
nwpv = vlistInqNWPV(vlistID, varID);
gridID = vlistInqVarGrid(vlistID, varID);
gridsize = gridInqSize(gridID);
zaxisID = vlistInqVarZaxis(vlistID, varID);
nlevel = zaxisInqSize(zaxisID);
missval = vlistInqVarMissval(vlistID, varID);
int nwpv = vlistInqNWPV(vlistID, varID); // number of words per value; real:1 complex:2
int gridID = vlistInqVarGrid(vlistID, varID);
int gridsize = gridInqSize(gridID);
int zaxisID = vlistInqVarZaxis(vlistID, varID);
int nlevel = zaxisInqSize(zaxisID);
double missval = vlistInqVarMissval(vlistID, varID);
field[varID] = (field_t*) Malloc(nlevel*sizeof(field_t));
for ( levelID = 0; levelID < nlevel; ++levelID )
for ( int levelID = 0; levelID < nlevel; ++levelID )
{
field_init(&field[varID][levelID]);
......@@ -49,13 +43,13 @@ field_t **field_allocate(int vlistID, int ptype, int init)
field[varID][levelID].ptr = NULL;
field[varID][levelID].weight = NULL;
if ( ptype == FIELD_ALL || ptype == FIELD_PTR )
if ( ptype & FIELD_PTR )
{
field[varID][levelID].ptr = (double*) Malloc(nwpv*gridsize*sizeof(double));
if ( init ) memset(field[varID][levelID].ptr, 0, nwpv*gridsize*sizeof(double));
}
if ( ptype == FIELD_ALL || ptype == FIELD_PTR )
if ( ptype & FIELD_WGT )
{
field[varID][levelID].weight = (double*) Malloc(nwpv*gridsize*sizeof(double));
if ( init ) memset(field[varID][levelID].weight, 0, nwpv*gridsize*sizeof(double));
......@@ -63,33 +57,29 @@ field_t **field_allocate(int vlistID, int ptype, int init)
}
}
return (field);
return field;
}
field_t **field_malloc(int vlistID, int ptype)
{
return (field_allocate(vlistID, ptype, 0));
return field_allocate(vlistID, ptype, 0);
}
field_t **field_calloc(int vlistID, int ptype)
{
return (field_allocate(vlistID, ptype, 1));
return field_allocate(vlistID, ptype, 1);
}
void field_free(field_t **field, int vlistID)
{
int nvars, nlevel;
int varID, levelID;
nvars = vlistNvars(vlistID);
for ( varID = 0; varID < nvars; ++varID )
int nvars = vlistNvars(vlistID);
for ( int varID = 0; varID < nvars; ++varID )
{
nlevel = zaxisInqSize(vlistInqVarZaxis(vlistID, varID));
for ( levelID = 0; levelID < nlevel; ++levelID )
int nlevel = zaxisInqSize(vlistInqVarZaxis(vlistID, varID));
for ( int levelID = 0; levelID < nlevel; ++levelID )
{
if ( field[varID][levelID].ptr ) Free(field[varID][levelID].ptr);
if ( field[varID][levelID].weight ) Free(field[varID][levelID].weight);
......
Supports Markdown
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