Commit 198c1a45 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

replace check of pn by call to percentile_check_number()

parent 9e7ce17d
...@@ -55,7 +55,6 @@ void *Ensstat(void *argument) ...@@ -55,7 +55,6 @@ void *Ensstat(void *argument)
double missval; double missval;
double *array; double *array;
} ens_file_t; } ens_file_t;
int pn = 0;
cdoInitialize(argument); cdoInitialize(argument);
...@@ -75,13 +74,13 @@ void *Ensstat(void *argument) ...@@ -75,13 +74,13 @@ void *Ensstat(void *argument)
int argc = operatorArgc(); int argc = operatorArgc();
int nargc = argc; int nargc = argc;
double pn = 0;
if ( operfunc == func_pctl ) if ( operfunc == func_pctl )
{ {
operatorInputArg("percentile number"); operatorInputArg("percentile number");
pn = parameter2int(operatorArgv()[0]); pn = parameter2int(operatorArgv()[0]);
percentile_check_number(pn);
if ( pn < 1 || pn > 100 )
cdoAbort("Illegal argument: percentile number %d is not in the range 1..100!", pn);
argc--; argc--;
} }
......
...@@ -96,7 +96,6 @@ void *Fldstat(void *argument) ...@@ -96,7 +96,6 @@ void *Fldstat(void *argument)
int nmiss; int nmiss;
double sglval; double sglval;
field_t field; field_t field;
int pn = 0;
cdoInitialize(argument); cdoInitialize(argument);
...@@ -115,13 +114,12 @@ void *Fldstat(void *argument) ...@@ -115,13 +114,12 @@ void *Fldstat(void *argument)
int operfunc = cdoOperatorF1(operatorID); int operfunc = cdoOperatorF1(operatorID);
int needWeights = cdoOperatorF2(operatorID); int needWeights = cdoOperatorF2(operatorID);
double pn = 0;
if ( operfunc == func_pctl ) if ( operfunc == func_pctl )
{ {
operatorInputArg("percentile number"); operatorInputArg("percentile number");
pn = parameter2int(operatorArgv()[0]); pn = parameter2double(operatorArgv()[0]);
percentile_check_number(pn);
if ( pn < 1 || pn > 100 )
cdoAbort("Illegal argument: percentile number %d is not in the range 1..100!", pn);
} }
int useweights = TRUE; int useweights = TRUE;
......
...@@ -46,7 +46,6 @@ void *Merstat(void *argument) ...@@ -46,7 +46,6 @@ void *Merstat(void *argument)
int recID, nrecs; int recID, nrecs;
int varID, levelID; int varID, levelID;
int needWeights = FALSE; int needWeights = FALSE;
int pn = 0;
char varname[CDI_MAX_NAME]; char varname[CDI_MAX_NAME];
cdoInitialize(argument); cdoInitialize(argument);
...@@ -65,16 +64,13 @@ void *Merstat(void *argument) ...@@ -65,16 +64,13 @@ void *Merstat(void *argument)
int operatorID = cdoOperatorID(); int operatorID = cdoOperatorID();
int operfunc = cdoOperatorF1(operatorID); int operfunc = cdoOperatorF1(operatorID);
/* RQ */ double pn = 0;
if ( operfunc == func_pctl ) if ( operfunc == func_pctl )
{ {
operatorInputArg("percentile number"); operatorInputArg("percentile number");
pn = parameter2int(operatorArgv()[0]); pn = parameter2double(operatorArgv()[0]);
percentile_check_number(pn);
if ( pn < 1 || pn > 100 )
cdoAbort("Illegal argument: percentile number %d is not in the range 1..100!", pn);
} }
/* QR */
if ( operfunc == func_mean || operfunc == func_avg || if ( operfunc == func_mean || operfunc == func_avg ||
operfunc == func_var || operfunc == func_std || operfunc == func_var || operfunc == func_std ||
......
...@@ -51,12 +51,10 @@ void *Runpctl(void *argument) ...@@ -51,12 +51,10 @@ void *Runpctl(void *argument)
operatorInputArg("percentile number, number of timesteps"); operatorInputArg("percentile number, number of timesteps");
operatorCheckArgc(2); operatorCheckArgc(2);
int pn = parameter2int(operatorArgv()[0]); double pn = parameter2double(operatorArgv()[0]);
percentile_check_number(pn);
int ndates = parameter2int(operatorArgv()[1]); int ndates = parameter2int(operatorArgv()[1]);
if ( pn < 1 || pn > 100 )
cdoAbort("Illegal argument: percentile number %d is not in the range 1..100!", pn);
int streamID1 = streamOpenRead(cdoStreamName(0)); int streamID1 = streamOpenRead(cdoStreamName(0));
int vlistID1 = streamInqVlist(streamID1); int vlistID1 = streamInqVlist(streamID1);
......
...@@ -57,9 +57,7 @@ void *Seaspctl(void *argument) ...@@ -57,9 +57,7 @@ void *Seaspctl(void *argument)
operatorInputArg("percentile number"); operatorInputArg("percentile number");
double pn = parameter2double(operatorArgv()[0]); double pn = parameter2double(operatorArgv()[0]);
percentile_check_number(pn);
if ( !(pn > 0 && pn < 100) )
cdoAbort("Illegal argument: percentile number %g is not in the range 0..100!", pn);
season_start = get_season_start(); season_start = get_season_start();
......
...@@ -54,9 +54,7 @@ void timpctl(int operatorID) ...@@ -54,9 +54,7 @@ void timpctl(int operatorID)
operatorInputArg("percentile number"); operatorInputArg("percentile number");
double pn = parameter2double(operatorArgv()[0]); double pn = parameter2double(operatorArgv()[0]);
percentile_check_number(pn);
if ( !(pn >= 0 && pn <= 100) )
cdoAbort("Illegal argument: percentile number %g is not in the range 0..100!", pn);
int cmplen = DATE_LEN - cdoOperatorF2(operatorID); int cmplen = DATE_LEN - cdoOperatorF2(operatorID);
......
...@@ -55,14 +55,12 @@ void *Timselpctl(void *argument) ...@@ -55,14 +55,12 @@ void *Timselpctl(void *argument)
if ( nargc < 2 ) cdoAbort("Too few arguments! Need %d found %d.", 2, nargc); if ( nargc < 2 ) cdoAbort("Too few arguments! Need %d found %d.", 2, nargc);
double pn = parameter2double(operatorArgv()[0]); double pn = parameter2double(operatorArgv()[0]);
percentile_check_number(pn);
int ndates = parameter2int(operatorArgv()[1]); int ndates = parameter2int(operatorArgv()[1]);
int noffset = 0, nskip = 0; int noffset = 0, nskip = 0;
if ( nargc > 2 ) noffset = parameter2int(operatorArgv()[2]); if ( nargc > 2 ) noffset = parameter2int(operatorArgv()[2]);
if ( nargc > 3 ) nskip = parameter2int(operatorArgv()[3]); if ( nargc > 3 ) nskip = parameter2int(operatorArgv()[3]);
if ( !(pn > 0 && pn < 100) )
cdoAbort("Illegal argument: percentile number %g is not in the range 0..100!", pn);
if ( cdoVerbose ) cdoPrint("nsets = %d, noffset = %d, nskip = %d", ndates, noffset, nskip); if ( cdoVerbose ) cdoPrint("nsets = %d, noffset = %d, nskip = %d", ndates, noffset, nskip);
int streamID1 = streamOpenRead(cdoStreamName(0)); int streamID1 = streamOpenRead(cdoStreamName(0));
......
...@@ -54,17 +54,14 @@ void *Ydaypctl(void *argument) ...@@ -54,17 +54,14 @@ void *Ydaypctl(void *argument)
int vdates2[NDAY], vtimes2[NDAY]; int vdates2[NDAY], vtimes2[NDAY];
field_t **vars1[NDAY]; field_t **vars1[NDAY];
field_t field; field_t field;
double pn;
HISTOGRAM_SET *hsets[NDAY]; HISTOGRAM_SET *hsets[NDAY];
cdoInitialize(argument); cdoInitialize(argument);
cdoOperatorAdd("ydaypctl", func_pctl, 0, NULL); cdoOperatorAdd("ydaypctl", func_pctl, 0, NULL);
operatorInputArg("percentile number"); operatorInputArg("percentile number");
pn = parameter2double(operatorArgv()[0]); double pn = parameter2double(operatorArgv()[0]);
percentile_check_number(pn);
if ( !(pn > 0 && pn < 100) )
cdoAbort("Illegal argument: percentile number %g is not in the range 0..100!", pn);
for ( dayoy = 0; dayoy < NDAY; dayoy++ ) for ( dayoy = 0; dayoy < NDAY; dayoy++ )
{ {
......
...@@ -67,10 +67,8 @@ void *Ydrunpctl(void *argument) ...@@ -67,10 +67,8 @@ void *Ydrunpctl(void *argument)
operatorInputArg("percentile number, number of timesteps"); operatorInputArg("percentile number, number of timesteps");
operatorCheckArgc(2); operatorCheckArgc(2);
double pn = parameter2double(operatorArgv()[0]); double pn = parameter2double(operatorArgv()[0]);
percentile_check_number(pn);
int ndates = parameter2int(operatorArgv()[1]); int ndates = parameter2int(operatorArgv()[1]);
if ( !(pn > 0 && pn < 100) )
cdoAbort("Illegal argument: percentile number %g is not in the range 0..100!", pn);
for ( dayoy = 0; dayoy < NDAY; dayoy++ ) for ( dayoy = 0; dayoy < NDAY; dayoy++ )
{ {
......
...@@ -59,17 +59,14 @@ void *Ymonpctl(void *argument) ...@@ -59,17 +59,14 @@ void *Ymonpctl(void *argument)
int vdates2[NMONTH]; int vdates2[NMONTH];
field_t **vars1[NMONTH]; field_t **vars1[NMONTH];
field_t field; field_t field;
double pn;
HISTOGRAM_SET *hsets[NMONTH]; HISTOGRAM_SET *hsets[NMONTH];
cdoInitialize(argument); cdoInitialize(argument);
cdoOperatorAdd("ymonpctl", func_pctl, 0, NULL); cdoOperatorAdd("ymonpctl", func_pctl, 0, NULL);
operatorInputArg("percentile number"); operatorInputArg("percentile number");
pn = parameter2double(operatorArgv()[0]); double pn = parameter2double(operatorArgv()[0]);
percentile_check_number(pn);
if ( !(pn > 0 && pn < 100) )
cdoAbort("Illegal argument: percentile number %g is not in the range 0..100!", pn);
for ( month = 0; month < NMONTH; month++ ) for ( month = 0; month < NMONTH; month++ )
{ {
......
...@@ -61,9 +61,7 @@ void *Yseaspctl(void *argument) ...@@ -61,9 +61,7 @@ void *Yseaspctl(void *argument)
operatorInputArg("percentile number"); operatorInputArg("percentile number");
double pn = parameter2double(operatorArgv()[0]); double pn = parameter2double(operatorArgv()[0]);
percentile_check_number(pn);
if ( !(pn > 0 && pn < 100) )
cdoAbort("Illegal argument: percentile number %g is not in the range 0..100!", pn);
for ( seas = 0; seas < NSEAS; seas++ ) for ( seas = 0; seas < NSEAS; seas++ )
{ {
......
...@@ -46,7 +46,6 @@ void *Zonstat(void *argument) ...@@ -46,7 +46,6 @@ void *Zonstat(void *argument)
int index; int index;
int recID, nrecs; int recID, nrecs;
int varID, levelID; int varID, levelID;
int pn = 0;
cdoInitialize(argument); cdoInitialize(argument);
...@@ -65,13 +64,12 @@ void *Zonstat(void *argument) ...@@ -65,13 +64,12 @@ void *Zonstat(void *argument)
int operatorID = cdoOperatorID(); int operatorID = cdoOperatorID();
int operfunc = cdoOperatorF1(operatorID); int operfunc = cdoOperatorF1(operatorID);
double pn = 0;
if ( operfunc == func_pctl ) if ( operfunc == func_pctl )
{ {
operatorInputArg("percentile number"); operatorInputArg("percentile number");
pn = parameter2int(operatorArgv()[0]); pn = parameter2double(operatorArgv()[0]);
percentile_check_number(pn);
if ( pn < 1 || pn > 100 )
cdoAbort("Illegal argument: percentile number %d is not in the range 1..100!", pn);
} }
int streamID1 = streamOpenRead(cdoStreamName(0)); int streamID1 = streamOpenRead(cdoStreamName(0));
......
...@@ -532,7 +532,7 @@ void varrms(field_t field, field_t field2, field_t *field3) ...@@ -532,7 +532,7 @@ void varrms(field_t field, field_t field2, field_t *field3)
} }
/* RQ */ /* RQ */
double fldpctl(field_t field, const int p) double fldpctl(field_t field, const double pn)
{ {
const size_t len = field.size; const size_t len = field.size;
const int nmiss = field.nmiss; const int nmiss = field.nmiss;
...@@ -551,13 +551,13 @@ double fldpctl(field_t field, const int p) ...@@ -551,13 +551,13 @@ double fldpctl(field_t field, const int p)
if ( !DBL_IS_EQUAL(array[i], missval) ) if ( !DBL_IS_EQUAL(array[i], missval) )
array2[j++] = array[i]; array2[j++] = array[i];
pctl = percentile(array2, j, p); pctl = percentile(array2, j, pn);
Free(array2); Free(array2);
} }
else else
{ {
pctl = percentile(array, len, p); pctl = percentile(array, len, pn);
} }
} }
......
...@@ -88,7 +88,7 @@ double fldstd(field_t field); ...@@ -88,7 +88,7 @@ double fldstd(field_t field);
double fldstd1(field_t field); double fldstd1(field_t field);
double fldvar(field_t field); double fldvar(field_t field);
double fldvar1(field_t field); double fldvar1(field_t field);
double fldpctl(field_t field, const int k); double fldpctl(field_t field, const double pn);
void fldunm(field_t *field); void fldunm(field_t *field);
int fldhvs(field_t *field, const size_t nlevels); int fldhvs(field_t *field, const size_t nlevels);
......
#include <math.h> #include <math.h>
#include "util.h"
#include "percentiles.h" #include "percentiles.h"
#include "nth_element.h" #include "nth_element.h"
double percentile(double *array, unsigned len, double p)
double percentile(double *array, unsigned len, double pn)
{ {
double percentil; int element = (int)ceil(len*(pn/100.0))-1;
percentil = nth_element(array, len, (int)ceil(len*(p/100.0))-1); if ( element < 0 ) element = 0;
if ( element >= len ) element = len-1;
double percentil = nth_element(array, len, element);
return percentil; return percentil;
} }
void percentile_check_number(double pn)
{
if ( pn < 0 || pn > 100 )
cdoAbort("Illegal argument: percentile number %g is not in the range 0..100!", pn);
}
#ifndef _PERCENTILES_H #ifndef _PERCENTILES_H
#define _PERCENTILES_H #define _PERCENTILES_H
double percentile(double *array, unsigned len, double p); double percentile(double *array, unsigned len, double pn);
void percentile_check_number(double pn);
#endif /* _PERCENTILES_H */ #endif /* _PERCENTILES_H */
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
#ifndef _UTIL_H #ifndef _UTIL_H
#define _UTIL_H #define _UTIL_H
#include <stdio.h>
#include "percentiles.h"
/* dummy use of unused parameters to silence compiler warnings */ /* dummy use of unused parameters to silence compiler warnings */
#define UNUSED(x) (void)x #define UNUSED(x) (void)x
......
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