Commit 78309fe6 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

Added operator yhourrange.

parent f489d6cd
......@@ -5,6 +5,7 @@
2017-06-01 Uwe Schulzweida
* New operator yhourrange: Multi-year hourly range (yhourmax-yhourmin)
* New operator ymonrange: Multi-year monthly range (ymonmax-ymonmin)
* New operator yseasrange: Multi-year seasonal range (yseasmax-yseasmin)
......
......@@ -18,6 +18,7 @@
/*
This module contains the following operators:
Yhourstat yhourrange Multi-year hourly range
Yhourstat yhourmin Multi-year hourly minimum
Yhourstat yhourmax Multi-year hourly maximum
Yhourstat yhoursum Multi-year hourly sum
......@@ -74,37 +75,39 @@ void *Yhourstat(void *argument)
int varID;
int nrecs;
int levelID;
int nsets[MAX_HOUR];
int houroy_nsets[MAX_HOUR];
int nmiss;
int vdates[MAX_HOUR], vtimes[MAX_HOUR];
field_type **vars1[MAX_HOUR], **vars2[MAX_HOUR], **samp1[MAX_HOUR];
cdoInitialize(argument);
cdoOperatorAdd("yhourmin", func_min, 0, NULL);
cdoOperatorAdd("yhourmax", func_max, 0, NULL);
cdoOperatorAdd("yhoursum", func_sum, 0, NULL);
cdoOperatorAdd("yhourmean", func_mean, 0, NULL);
cdoOperatorAdd("yhouravg", func_avg, 0, NULL);
cdoOperatorAdd("yhourvar", func_var, 0, NULL);
cdoOperatorAdd("yhourvar1", func_var1, 0, NULL);
cdoOperatorAdd("yhourstd", func_std, 0, NULL);
cdoOperatorAdd("yhourstd1", func_std1, 0, NULL);
cdoOperatorAdd("yhourrange", func_range, 0, NULL);
cdoOperatorAdd("yhourmin", func_min, 0, NULL);
cdoOperatorAdd("yhourmax", func_max, 0, NULL);
cdoOperatorAdd("yhoursum", func_sum, 0, NULL);
cdoOperatorAdd("yhourmean", func_mean, 0, NULL);
cdoOperatorAdd("yhouravg", func_avg, 0, NULL);
cdoOperatorAdd("yhourvar", func_var, 0, NULL);
cdoOperatorAdd("yhourvar1", func_var1, 0, NULL);
cdoOperatorAdd("yhourstd", func_std, 0, NULL);
cdoOperatorAdd("yhourstd1", func_std1, 0, NULL);
int operatorID = cdoOperatorID();
int operfunc = cdoOperatorF1(operatorID);
bool lrange = operfunc == func_range;
bool lmean = operfunc == func_mean || operfunc == func_avg;
bool lstd = operfunc == func_std || operfunc == func_std1;
bool lvarstd = operfunc == func_std || operfunc == func_var || operfunc == func_std1 || operfunc == func_var1;
int divisor = operfunc == func_std1 || operfunc == func_var1;
int divisor = operfunc == func_std1 || operfunc == func_var1;
for ( int houroy = 0; houroy < MAX_HOUR; ++houroy )
{
vars1[houroy] = NULL;
vars2[houroy] = NULL;
samp1[houroy] = NULL;
nsets[houroy] = 0;
houroy_nsets[houroy] = 0;
}
int streamID1 = streamOpenRead(cdoStreamName(0));
......@@ -149,7 +152,7 @@ void *Yhourstat(void *argument)
{
vars1[houroy] = field_malloc(vlistID1, FIELD_PTR);
samp1[houroy] = field_malloc(vlistID1, FIELD_NONE);
if ( lvarstd )
if ( lvarstd || lrange )
vars2[houroy] = field_malloc(vlistID1, FIELD_PTR);
}
......@@ -163,23 +166,31 @@ void *Yhourstat(void *argument)
recinfo[recID].levelID = levelID;
}
field_type *psamp1 = &samp1[houroy][varID][levelID];
field_type *pvars1 = &vars1[houroy][varID][levelID];
field_type *pvars2 = vars2[houroy] ? &vars2[houroy][varID][levelID] : NULL;
int nsets = houroy_nsets[houroy];
int gridsize = gridInqSize(vlistInqVarGrid(vlistID1, varID));
if ( nsets[houroy] == 0 )
if ( nsets == 0 )
{
streamReadRecord(streamID1, pvars1->ptr, &nmiss);
pvars1->nmiss = (size_t) nmiss;
if ( lrange )
{
pvars2->nmiss = pvars1->nmiss;
for ( int i = 0; i < gridsize; i++ )
pvars2->ptr[i] = pvars1->ptr[i];
}
if ( nmiss > 0 || samp1[houroy][varID][levelID].ptr )
if ( nmiss > 0 || psamp1->ptr )
{
if ( samp1[houroy][varID][levelID].ptr == NULL )
samp1[houroy][varID][levelID].ptr = (double*) Malloc(gridsize*sizeof(double));
if ( psamp1->ptr == NULL )
psamp1->ptr = (double*) Malloc(gridsize*sizeof(double));
for ( int i = 0; i < gridsize; i++ )
samp1[houroy][varID][levelID].ptr[i] = !DBL_IS_EQUAL(pvars1->ptr[i], pvars1->missval);
psamp1->ptr[i] = !DBL_IS_EQUAL(pvars1->ptr[i], pvars1->missval);
}
}
else
......@@ -189,18 +200,18 @@ void *Yhourstat(void *argument)
field.grid = pvars1->grid;
field.missval = pvars1->missval;
if ( field.nmiss > 0 || samp1[houroy][varID][levelID].ptr )
if ( field.nmiss > 0 || psamp1->ptr )
{
if ( samp1[houroy][varID][levelID].ptr == NULL )
if ( psamp1->ptr == NULL )
{
samp1[houroy][varID][levelID].ptr = (double*) Malloc(gridsize*sizeof(double));
psamp1->ptr = (double*) Malloc(gridsize*sizeof(double));
for ( int i = 0; i < gridsize; i++ )
samp1[houroy][varID][levelID].ptr[i] = nsets[houroy];
psamp1->ptr[i] = nsets;
}
for ( int i = 0; i < gridsize; i++ )
if ( !DBL_IS_EQUAL(field.ptr[i], pvars1->missval) )
samp1[houroy][varID][levelID].ptr[i]++;
psamp1->ptr[i]++;
}
if ( lvarstd )
......@@ -208,6 +219,11 @@ void *Yhourstat(void *argument)
farsumq(pvars2, field);
farsum(pvars1, field);
}
else if ( lrange )
{
farmin(pvars2, field);
farmax(pvars1, field);
}
else
{
farfun(pvars1, field, operfunc);
......@@ -215,7 +231,7 @@ void *Yhourstat(void *argument)
}
}
if ( nsets[houroy] == 0 && lvarstd )
if ( houroy_nsets[houroy] == 0 && lvarstd )
for ( int recID = 0; recID < maxrecs; recID++ )
{
int varID = recinfo[recID].varID;
......@@ -228,48 +244,47 @@ void *Yhourstat(void *argument)
farmoq(pvars2, *pvars1);
}
nsets[houroy]++;
houroy_nsets[houroy]++;
tsID++;
}
for ( int houroy = 0; houroy < MAX_HOUR; ++houroy )
if ( nsets[houroy] )
if ( houroy_nsets[houroy] )
{
if ( lmean )
for ( int recID = 0; recID < maxrecs; recID++ )
{
int varID = recinfo[recID].varID;
int levelID = recinfo[recID].levelID;
field_type *pvars1 = &vars1[houroy][varID][levelID];
if ( vlistInqVarTsteptype(vlistID1, varID) == TSTEP_CONSTANT ) continue;
if ( samp1[houroy][varID][levelID].ptr == NULL )
farcdiv(pvars1, (double)nsets[houroy]);
else
fardiv(pvars1, samp1[houroy][varID][levelID]);
}
else if ( lvarstd )
for ( int recID = 0; recID < maxrecs; recID++ )
{
int varID = recinfo[recID].varID;
int levelID = recinfo[recID].levelID;
field_type *pvars1 = &vars1[houroy][varID][levelID];
field_type *pvars2 = &vars2[houroy][varID][levelID];
if ( vlistInqVarTsteptype(vlistID1, varID) == TSTEP_CONSTANT ) continue;
if ( samp1[houroy][varID][levelID].ptr == NULL )
{
if ( lstd ) farcstd(pvars1, *pvars2, nsets[houroy], divisor);
else farcvar(pvars1, *pvars2, nsets[houroy], divisor);
}
else
{
if ( lstd ) farstd(pvars1, *pvars2, samp1[houroy][varID][levelID], divisor);
else farvar(pvars1, *pvars2, samp1[houroy][varID][levelID], divisor);
}
}
int nsets = houroy_nsets[houroy];
for ( int recID = 0; recID < maxrecs; recID++ )
{
int varID = recinfo[recID].varID;
int levelID = recinfo[recID].levelID;
field_type *psamp1 = &samp1[houroy][varID][levelID];
field_type *pvars1 = &vars1[houroy][varID][levelID];
field_type *pvars2 = vars2[houroy] ? &vars2[houroy][varID][levelID] : NULL;
if ( vlistInqVarTsteptype(vlistID1, varID) == TSTEP_CONSTANT ) continue;
if ( lmean )
{
if ( psamp1->ptr ) fardiv(pvars1, *psamp1);
else farcdiv(pvars1, (double)nsets);
}
else if ( lvarstd )
{
if ( psamp1->ptr )
{
if ( lstd ) farstd(pvars1, *pvars2, *psamp1, divisor);
else farvar(pvars1, *pvars2, *psamp1, divisor);
}
else
{
if ( lstd ) farcstd(pvars1, *pvars2, nsets, divisor);
else farcvar(pvars1, *pvars2, nsets, divisor);
}
}
else if ( lrange )
{
farsub(pvars1, *pvars2);
}
}
taxisDefVdate(taxisID2, vdates[houroy]);
taxisDefVtime(taxisID2, vtimes[houroy]);
......
......@@ -503,7 +503,7 @@ void *Samplegrid(void *argument); // "samplegrid", "subgrid"
#define YdrunpctlOperators {"ydrunpctl"}
#define YdrunstatOperators {"ydrunmin", "ydrunmax", "ydrunsum", "ydrunmean", "ydrunavg", "ydrunstd", "ydrunstd1", "ydrunvar", "ydrunvar1"}
#define YhourarithOperators {"yhouradd", "yhoursub", "yhourmul", "yhourdiv"}
#define YhourstatOperators {"yhourmin", "yhourmax", "yhoursum", "yhourmean", "yhouravg", "yhourstd", "yhourstd1", "yhourvar", "yhourvar1"}
#define YhourstatOperators {"yhourrange", "yhourmin", "yhourmax", "yhoursum", "yhourmean", "yhouravg", "yhourstd", "yhourstd1", "yhourvar", "yhourvar1"}
#define YmonarithOperators {"ymonadd", "ymonsub", "ymonmul", "ymondiv"}
#define YseasarithOperators {"yseasadd", "yseassub", "yseasmul", "yseasdiv"}
#define YmonpctlOperators {"ymonpctl"}
......
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