source: branches/fitshead-branch/Utils/cpghist_log.c @ 1441

Last change on this file since 1441 was 86, checked in by Matthew Whiting, 18 years ago

Large commit, mostly removing dud commented code, and adding comments and
documentation to the existing code. No new features.

File size: 2.1 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <cpgplot.h>
4#include <math.h>
5void cpghistlog(int npts, float *data, float datamin, float datamax, int nbin, int pgflag)
6{
7  /**
8   * cpghistlog(npts, data, datamin, datamax, nbin, pgflag)
9   *
10   *  Works exactly as for cpghist, except the y-axis is a logarithmic scale.
11   *
12   */
13
14
15  int i,bin;
16  float *num;
17  float maxNum,binSize,x1,x2,y1,y2,older,newer,fraction;
18  float MINCOUNT=-0.2;
19
20  num = (float *)calloc(nbin,sizeof(float));
21
22  cpgbbuf();
23
24  // HOW MANY VALUES IN EACH BIN?
25  for(i=0; i<npts; i++){
26    fraction = (data[i] - datamin) / (datamax - datamin);
27    bin = (int)( floor(fraction*nbin) );
28    if((bin>=0)&&(bin<nbin)) num[bin]+=1.;
29  }
30  for(i=0; i<nbin;i++){
31    if(num[i]>0) num[i] = log10(num[i]);
32    else num[i] = MINCOUNT;
33  }
34  maxNum = num[0];
35  for(i=1; i<nbin; i++) if(num[i]>maxNum) maxNum = num[i];
36  binSize = (datamax - datamin) / (float)nbin;
37
38  // BOUNDARIES OF PLOT
39  x1 = datamin;
40  x2 = datamax;
41  y1 = MINCOUNT;
42  y2 = ceil(maxNum);
43
44  // DEFINE ENVIRONMENT IF NECESSARY
45  if(pgflag%2 == 0) cpgenv(x1,x2,y1,y2,0,20);
46
47  // DRAW HISTOGRAM
48  if(pgflag/2 == 0){
49    older = 0.;
50    x2 = datamin;
51    cpgmove(datamin,MINCOUNT);
52    for(i=0;i<nbin;i++){
53      newer = num[i];
54      x1 = x2;
55      x2 = datamin + i*binSize;
56      if(newer!=MINCOUNT){
57        if(newer<=older){
58          cpgmove(x1,newer);
59          cpgdraw(x2,newer);
60        }
61        else{
62          cpgmove(x1,older);
63          cpgdraw(x1,newer);
64          cpgdraw(x2,newer);
65        }
66      }
67      cpgdraw(x2,MINCOUNT);
68      older=newer;
69    }
70  }
71  else if(pgflag/2 == 1){
72    older = MINCOUNT;
73    x2 = datamin;
74    for(i=0;i<nbin;i++){
75      newer = num[i];
76      x1 = x2;
77      x2 = datamin + i*binSize;
78      if(newer!=MINCOUNT) cpgrect(x1,x2,0.,newer);     
79    }
80  }
81  else if(pgflag/2 == 2){
82    older = 0.;
83    cpgmove(datamin,MINCOUNT);
84    x2 = datamin;
85    for(i=0;i<nbin;i++){
86      newer = num[i];
87      x1 = x2;
88      x2 = datamin + i*binSize;
89      if((newer==MINCOUNT)&&(older==MINCOUNT)) cpgmove(x2,MINCOUNT);
90      else {
91        cpgdraw(x1,newer);
92        if(newer==MINCOUNT) cpgmove(x2,newer);
93        else cpgdraw(x2,newer);
94      }
95      older = newer;
96    }
97  }
98
99  cpgebuf();
100   
101}
Note: See TracBrowser for help on using the repository browser.