Commit 0300b798 authored by Fabian Wachsmann's avatar Fabian Wachsmann
Browse files

Try to calc cei by bootstrapping histo

parent f885e7e1
......@@ -328,7 +328,6 @@ AC_CONFIG_FILES([
test/Timselpctl.test
test/Timstat.test
test/Timstat2.test
test/UserInput.test
test/Varsstat.test
test/Vertint.test
test/Vertstat.test
......
This diff is collapsed.
......@@ -170,7 +170,7 @@ Seaspctl(void *process)
cdoReadRecord(streamID1, vars1[varID][levelID].vec.data(), &nmiss);
vars1[varID][levelID].nmiss = nmiss;
hset.addVarLevelValues(varID, levelID, vars1[varID][levelID]);
hset.addSubVarLevelValues(varID, levelID, vars1[varID][levelID], true);
}
nsets++;
......
......@@ -158,7 +158,7 @@ timpctl(int operatorID)
cdoReadRecord(streamID1, vars1[varID][levelID].vec.data(), &nmiss);
vars1[varID][levelID].nmiss = nmiss;
hset.addVarLevelValues(varID, levelID, vars1[varID][levelID]);
hset.addSubVarLevelValues(varID, levelID, vars1[varID][levelID], true);
}
nsets++;
......
......@@ -186,7 +186,7 @@ Timselpctl(void *process)
cdoReadRecord(streamID1, vars1[varID][levelID].vec.data(), &nmiss);
vars1[varID][levelID].nmiss = nmiss;
hset.addVarLevelValues(varID, levelID, vars1[varID][levelID]);
hset.addSubVarLevelValues(varID, levelID, vars1[varID][levelID], true);
}
tsID++;
......
......@@ -171,7 +171,7 @@ Ydaypctl(void *process)
cdoReadRecord(streamID1, vars1[dayoy][varID][levelID].vec.data(), &nmiss);
vars1[dayoy][varID][levelID].nmiss = nmiss;
hsets[dayoy].addVarLevelValues(varID, levelID, vars1[dayoy][varID][levelID]);
hsets[dayoy].addSubVarLevelValues(varID, levelID, vars1[dayoy][varID][levelID], true);
}
nsets[dayoy]++;
......
......@@ -198,7 +198,7 @@ Ydrunpctl(void *process)
const auto nlevels = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
for (levelID = 0; levelID < nlevels; levelID++)
for (inp = 0; inp < ndates; inp++) hsets[dayoy].addVarLevelValues(varID, levelID, vars1[inp][varID][levelID]);
for (inp = 0; inp < ndates; inp++) hsets[dayoy].addSubVarLevelValues(varID, levelID, vars1[inp][varID][levelID], true);
}
datetime[ndates] = datetime[0];
......
......@@ -171,7 +171,7 @@ Ymonpctl(void *process)
cdoReadRecord(streamID1, vars1[month][varID][levelID].vec.data(), &nmiss);
vars1[month][varID][levelID].nmiss = nmiss;
hsets[month].addVarLevelValues(varID, levelID, vars1[month][varID][levelID]);
hsets[month].addSubVarLevelValues(varID, levelID, vars1[month][varID][levelID], true);
}
nsets[month]++;
......
......@@ -177,7 +177,7 @@ Yseaspctl(void *process)
cdoReadRecord(streamID1, vars1[seas][varID][levelID].vec.data(), &nmiss);
vars1[seas][varID][levelID].nmiss = nmiss;
hsets[seas].addVarLevelValues(varID, levelID, vars1[seas][varID][levelID]);
hsets[seas].addSubVarLevelValues(varID, levelID, vars1[seas][varID][levelID], true);
}
nsets[seas]++;
......
......@@ -62,6 +62,15 @@ histBinValue(Histogram &hist, double value)
if (bin >= 0 && bin < hist.nbins) INT_PTR(hist.ptr)[bin]++;
}
static void
histBinSubValue(Histogram &hist, double value)
{
assert(hist.step > 0);
const int bin = cdo::min((int) ((value - hist.min) / hist.step), hist.nbins - 1);
if (bin >= 0 && bin < hist.nbins && INT_PTR(hist.ptr)[bin] > 0) INT_PTR(hist.ptr)[bin]--;
}
static void
histBin(Histogram &hist)
{
......@@ -124,6 +133,50 @@ histAddValue(Histogram &hist, double value)
return 0;
}
static void
histRemoveValue(Histogram &hist, double value)
{
int i = 0;
for ( i = 0; i < hist.nsamp; i++ )
{
if ( IS_EQUAL(DBL_PTR(hist.ptr)[i], value) )
{
if ( i != hist.nsamp-1 )
DBL_PTR(hist.ptr)[i] = DBL_PTR(hist.ptr)[hist.nsamp-1];
break;
}
}
if ( i == hist.nsamp )
cdoWarning("'%f' not found in histogram!", value);
else
hist.nsamp--;
}
static int
histSubValue(Histogram &hist, double value)
{
assert(hist.nbins > 0);
// 2011-08-01 Uwe Schulzweida: added check for rounding errors
if (value < hist.min && (hist.min - value) < 1e5) value = hist.min;
if (value > hist.max && (value - hist.max) < 1e5) value = hist.max;
if (IS_EQUAL(hist.min, hist.max)) return 0;
if (value < hist.min || value > hist.max) return 1;
if (hist.nsamp < DBL_CAPACITY(hist.nbins))
{
histRemoveValue(hist, value);
}
else if (hist.nsamp > DBL_CAPACITY(hist.nbins))
{
histBinSubValue(hist, value);
hist.nsamp--;
}
return 0;
}
static double
histGetPercentile(const Histogram &hist, double p)
{
......@@ -220,7 +273,7 @@ HistogramSet::defVarLevelBounds(int varID, int levelID, const Field &field1, con
}
void
HistogramSet::addVarLevelValues(int varID, int levelID, const Field &field)
HistogramSet::addSubVarLevelValues(int varID, int levelID, const Field &field, bool lisAdd)
{
const auto &array = field.vec;
assert(!array.empty());
......@@ -237,19 +290,33 @@ HistogramSet::addVarLevelValues(int varID, int levelID, const Field &field)
int nign = 0;
if (field.nmiss)
if ( lisAdd )
{
for (size_t i = 0; i < nhists; i++)
if (!DBL_IS_EQUAL(array[i], field.missval)) nign += histAddValue(hists[i], array[i]);
if (field.nmiss)
{
for (size_t i = 0; i < nhists; i++)
if (!DBL_IS_EQUAL(array[i], field.missval)) nign += histAddValue(hists[i], array[i]);
}
else
{
for (size_t i = 0; i < nhists; i++) nign += histAddValue(hists[i], array[i]);
}
}
else
{
for (size_t i = 0; i < nhists; i++) nign += histAddValue(hists[i], array[i]);
if (field.nmiss)
{
for (size_t i = 0; i < nhists; i++)
if (!DBL_IS_EQUAL(array[i], field.missval)) nign += histSubValue(hists[i], array[i]);
}
else
{
for (size_t i = 0; i < nhists; i++) nign += histSubValue(hists[i], array[i]);
}
}
if (nign) cdoWarning("%d out of %d grid values are out of bounds and have been ignored (%s)", nign, nhists, __func__);
}
void
HistogramSet::Reset(int varID, int levelID)
{
......
......@@ -79,7 +79,7 @@ public:
void createVarLevels(int varID, int nlevels, size_t nhists);
void defVarLevelBounds(int varID, int levelID, const Field &field1, const Field &field2);
void addVarLevelValues(int varID, int levelID, const Field &field);
void addSubVarLevelValues(int varID, int levelID, const Field &field, bool lisAdd);
void getVarLevelPercentiles(Field &field, int varID, int levelID, double p);
void Reset(int varID, int levelID);
};
......
......@@ -539,7 +539,7 @@ static void
printZaxisReferenceInfo(const int zaxisID)
{
int number = 0;
cdiInqKeyInt(zaxisID, CDI_GLOBAL, CDI_KEY_NUMBEROFVGRIDUSED, &number);
/* cdiInqKeyInt(zaxisID, CDI_GLOBAL, CDI_KEY_NUMBEROFVGRIDUSED, &number)*/
if (number > 0)
{
fprintf(stdout, "%33s : ", "zaxis");
......
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