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