File: histogram.c

package info (click to toggle)
plotmtv 1.4.1-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 4,024 kB
  • ctags: 5,006
  • sloc: ansic: 51,179; makefile: 1,976; fortran: 1,277; sh: 510; csh: 439
file content (254 lines) | stat: -rw-r--r-- 7,337 bytes parent folder | download | duplicates (6)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 * histogram.c - procedures to build and maintain a histogram
 *               data structure
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include "CNplot.h"

/*
 * HISTOGRAM DATA STRUCTURE
 *    A histogram is used to store data for a histogram plot.
 *    The data consists of points (x only) that are sorted into
 *    bins then plotted.
 */

#define NBINS    10
#define MAXBINS 1000

/*
 * Given a histogram dataset, sort the data into bins and create curves
 */
void CNsort_histogram(dptr, verbose)
CNdatasetptr dptr;
int          verbose;
{
   CNpointptr P;
   CNcurveptr Cptr;
   double     x1, y1, x2, y2;
   double     xmin, xmax, binwidth, binstart;
   double     dpower;
   int        power;
   int        bins[MAXBINS];
   int        bmax;
   int        i;
   int        curveID=0;

   /* Error-check */
   if (dptr == NULL) {
      (void) fprintf(stderr,"CNsort_histogram: Error! NULL dataset!\n");
      return;
   }
   if (dptr->datatype != CN_HISTOGRAM) {
      (void) fprintf(stderr,
                     "CNsort_histogram: Error! Need a HISTOGRAM dataset!\n");
      return;
   }
   if (dptr->histogram == NULL) {
      (void) fprintf(stderr,
                     "CNsort_histogram: Error! NULL HISTOGRAM structure!\n");
      return;
   }

   /* Get rid of the curvelist in the dataset first */
   CNdelete_curve_list(&(dptr->curvehead), &(dptr->curvetail));

   /* Get the binwidth and binstart - special treatment for xmin=xmax */
   xmin = dptr->histogram->xmin;
   xmax = dptr->histogram->xmax;
   if ((xmax - xmin) < CN_SMALL) {
      /* Just put in one bar */

      /* Set binwidth */
      binwidth = 0.0;
      if ((dptr->data_pr.flag2 & CNbinwidth) != 0) {
         binwidth = dptr->data_pr.binwidth;
      } else {
         /* The binwidth is on the order of the mean */
         dpower   = CNlog10(xmin);
         power    = (dpower > -CN_SMALL) ? (int)dpower : (int)dpower - 1;
         binwidth = CNround_down(xmin,power);
      }
      if (binwidth < CN_SMALL) binwidth = 0.1;

      /* Get the binstart */
      if ((dptr->data_pr.flag2 & CNbinstart) != 0) {
         binstart = dptr->data_pr.binstart;
      } else {
         binstart = xmin - 0.5*binwidth;
      }

      /* Set xmin, xmax to the size of 3 bars */
      xmin = xmin - 1.5*binwidth;
      xmax = xmax + 1.5*binwidth;

   } else {
      /* Default of 10 bars */

      /* Set the binwidth */
      if ((dptr->data_pr.flag2 & CNbinwidth) != 0) {
         binwidth = dptr->data_pr.binwidth;
         if ((binwidth <= 0.0) || ((xmax-xmin)/binwidth > MAXBINS)) 
            binwidth = (xmax - xmin)/NBINS;
      } else {
         binwidth = (xmax - xmin)/NBINS;
      }

      /* If the binwidth is too small then adjust it */
      if (binwidth < CN_SMALL) binwidth = 0.1;

      /* Get the binstart */
      if ((dptr->data_pr.flag2 & CNbinstart) != 0) {
         binstart = dptr->data_pr.binstart;
      } else {
         dpower   = CNlog10(xmax-xmin);
         power    = (dpower > -CN_SMALL) ? (int)dpower : (int)dpower - 1;
         binstart = CNround_down(xmin,power);
      }
   }

   /* Put the binwidth, binstart values back */
   dptr->data_pr.binwidth = binwidth;
   dptr->data_pr.binstart = binstart;

   /* Print info */
   if (verbose) {
      (void) fprintf(stdout,"Histogram:\n");
      (void) fprintf(stdout,"xmin      = %g\n",xmin);
      (void) fprintf(stdout,"xmax      = %g\n",xmax);
      (void) fprintf(stdout,"bin width = %g\n",binwidth);
      (void) fprintf(stdout,"bin start = %g\n",binstart);
   }

   /* Initialize the bins */
   for (i=0; i<MAXBINS; i++) bins[i] = 0; 

   /* Fill the bins */
   for (P=dptr->histogram->pointhead; P!=NULL; P=P->next) {
      i = (int)((P->x - binstart)/binwidth);
      if (i>=0 && i<MAXBINS) bins[i]++;
   }

   /* Create the curves */
   bmax = 0;
   for (i=0; i<MAXBINS; i++) {
      if (bins[i] > 0) {

         /* Boundary */
         x1 = binstart + (i  )*binwidth;
         x2 = binstart + (i+1)*binwidth;
         y1 = -1000.0;
         y2 = (double) bins[i];
         /*
         (void) printf("x1=%g x2=%g y1=%g y2=%g\n",x1,x2,y1,y2);
          */

         /* Min/Max */
         if (bins[i] > bmax) bmax = bins[i];
         if (x1 < xmin) xmin = x1;
         if (x2 > xmax) xmax = x2;

         /* Allocate a curve data-structure */
         Cptr = CNinsert_curve(&(dptr->curvehead), &(dptr->curvetail),
                               curveID++);
         if (Cptr!=NULL) {
            (void) CNinsert_point(&(Cptr->pointhead), &(Cptr->pointtail),
                                  x1,y1,0.0,0);
            (void) CNinsert_point(&(Cptr->pointhead), &(Cptr->pointtail),
                                  x1,y2,0.0,0);
            (void) CNinsert_point(&(Cptr->pointhead), &(Cptr->pointtail),
                                  x2,y2,0.0,0);
            (void) CNinsert_point(&(Cptr->pointhead), &(Cptr->pointtail),
                                  x2,y1,0.0,0);
            (void) CNinsert_point(&(Cptr->pointhead), &(Cptr->pointtail),
                                  x1,y1,0.0,0);
            /* Set the properties */
            Cptr->curv_pr.filltype = dptr->histogram->filltype;
            Cptr->curv_pr.flag     = Cptr->curv_pr.flag & CNfilltype;
            Cptr->curv_pr.fillcolor= dptr->histogram->fillcolor;
            Cptr->curv_pr.flag     = Cptr->curv_pr.flag & CNfillcolor;
            Cptr->curv_pr.linecolor= 0;
            Cptr->curv_pr.flag     = Cptr->curv_pr.flag & CNlinecolor;
         }
      }
   }

   /* Reset the dataset boundaries */
   dptr->bymin         = 0.0;
   dptr->bymax         = (double) (bmax+1);
   dptr->plot_pr.vymin = 0.0;
   dptr->plot_pr.vymax = (double) (bmax+1);
   dptr->bxmin         = xmin;
   dptr->bxmax         = xmax;
   dptr->plot_pr.vxmin = xmin;
   dptr->plot_pr.vxmax = xmax;

   /* Print info */
   if (verbose) {
      (void) fprintf(stdout, "Generated %d new curves\n",
                     CNcount_curves(dptr->curvehead, dptr->curvetail));
   }
}
        



/*
 * HISTOGRAM DATA STRUCTURE
 *    A histogram is used to store data for a histogram plot.
 *    The data consists of points (x only) that are sorted into
 *    bins then plotted.
 */
CNhistogramptr CNmake_histogram(xmin, xmax, pointhead, pointtail)
double     xmin, xmax;
CNpointptr pointhead, pointtail;
{
   CNhistogramptr newptr;
   unsigned int size = sizeof(CNhistogram);

   if ((newptr = (CNhistogramptr)malloc(size))!=NULL) {
      newptr->xmin      = xmin;
      newptr->xmax      = xmax;
      newptr->filltype  = CN_FILL_SOLID;
      newptr->fillcolor = 5;
      /* Linked lists */
      newptr->pointhead = pointhead;
      newptr->pointtail = pointtail;
   }
   return(newptr);
}


/*
 * Delete histogram 
 */
void CNdelete_histogram(Sptr)
CNhistogramptr Sptr;
{
   /* delete all the points in Dptr */
   CNdelete_point_list(&(Sptr->pointhead),&(Sptr->pointtail));

   /* Now delete Sptr */
   free ((char*)Sptr);
}


/*
 * Print info on the histogram
 */
void CNprint_histogram(Sptr)
CNhistogramptr Sptr;
{
   int npoints;

   /* Count the contents of the slice */
   npoints = CNcount_points(Sptr->pointhead,Sptr->pointtail);

   /* Print the contents of the slice */
   (void) fprintf(stdout,"Histogram: \n");
   (void) fprintf(stdout,"   No of points = %d\n", npoints);
}