-
Notifications
You must be signed in to change notification settings - Fork 4
/
histogram.cpp
65 lines (59 loc) · 1.91 KB
/
histogram.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*****************************************************
Evaluate histograms of solute levels. TWS December 07.
Version 2.0, May 1, 2010.
Version 3.0, May 17,2011.
******************************************************/
#define _CRT_SECURE_NO_DEPRECATE
#include <math.h>
#include "nrutil.h"
#include <stdio.h>
#include <string.h>
void histogram(const char fname[])
{
extern int mxx, myy, mzz, nnt, nnv, nseg, nnod, nsp;
extern float *pmin, *pmax, *pmeant, *pref, **pt;
int i, j, itp, isp, nctop, ncbot, nstat, nstatm = 101;
float step, dev;
float *po2samp, *stat, *cumu, *mstat;
FILE *ofp;
po2samp = vector(1, nstatm);
stat = vector(1, nstatm);
cumu = vector(1, nstatm);
mstat = vector(1, nstatm);
ofp = fopen(fname, "w");
for (isp = 1; isp <= nsp; isp++) {
step = pref[isp] / 100;
nctop = pmax[isp] / step + 1.;
if (nctop > 100) nctop = 100;
ncbot = pmin[isp] / step;
if (ncbot <= 2) ncbot = 0;
nstat = nctop - ncbot + 1;
dev = 0.;
if (nstat > nstatm) printf("*** nstatm too small in histogram\n");
for (i = 1; i <= nstat; i++) {
po2samp[i] = step * (i - 1 + ncbot);
mstat[i] = 0;
}
for (itp = 1; itp <= nnt; itp++) {
dev += SQR(pmeant[isp] - pt[itp][isp]);
for (j = 1; j <= nstat; j++) if (pt[itp][isp] <= po2samp[j]) {
mstat[j] = mstat[j] + 1;
goto binned;
}
binned:;
}
dev = sqrt(dev / nnt);
for (i = 1; i <= nstat; i++) stat[i] = mstat[i] * 100. / nnt;
cumu[1] = stat[1];
for (i = 2; i <= nstat; i++) cumu[i] = cumu[i - 1] + stat[i];
fprintf(ofp, "Histogram data for solute %i\n", isp);
fprintf(ofp, "value %% cumul. %%\n");
for (i = 1; i <= nstat; i++) fprintf(ofp, "%g %7.2f %7.2f\n", po2samp[i], stat[i], cumu[i]);
fprintf(ofp, "Mean = %f deviation = %f min = %f max = %f\n", pmeant[isp], dev, pmin[isp], pmax[isp]);
}
fclose(ofp);
free_vector(po2samp, 1, nstatm);
free_vector(stat, 1, nstatm);
free_vector(cumu, 1, nstatm);
free_vector(mstat, 1, nstatm);
}