OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
hist.c
Go to the documentation of this file.
1 #include <math.h>
2 #include <libgen.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include "hist_proto.h"
6 
7 #define NPARM 12
8 #define NBIN 100
9 
10 static float hmin[NPARM] = {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.8, 0.};
11 static float hmax[NPARM] = {5., 4., 4., 3., 2., 2., 1., 1., 0.5, 20.,
12  1.3, 0.5};
13 static float step[NPARM];
14 static int nhi[NPARM], nlo[NPARM], nbin[NPARM][NBIN];
15 char bfile[412];
16 
17 void h_init(char *file)
18 /*******************************************************************
19 
20  h_init
21 
22  purpose: initialize the histogram accumulation
23 
24  Returns type: void none
25 
26  Parameters: (in calling order)
27  Type Name I/O Description
28  ---- ---- --- -----------
29  char * file I level-3 file name
30  we'll add _bhist to
31  make binary output file
32 
33  Modification history:
34  Programmer Date Description of change
35  ---------- ---- ---------------------
36  W. Robinson 26-Nov-1997 Original development
37 
38  *******************************************************************/ {
39  int i, j;
40  /*
41  * initialize the bin arrays
42  */
43  for (i = 0; i < NPARM; i++) {
44  nhi[i] = 0;
45  nlo[i] = 0;
46  step[i] = (hmax[i] - hmin[i]) / NBIN;
47  for (j = 0; j < NBIN; j++) {
48  nbin[i][j] = 0.;
49  }
50  }
51  /*
52  * create the name of the binary histogram file
53  */
54  /* for tests
55  strcpy( bfile, basename( file ) );
56  */
57  strcpy(bfile, file);
58  strcat(bfile, "_bhist");
59 }
60 
61 void h_accum(float value, int parm_num)
62 /*******************************************************************
63 
64  h_accum
65 
66  purpose: accumulate the histogram parameters
67  Returns type: void none
68 
69  Parameters: (in calling order)
70  Type Name I/O Description
71  ---- ---- --- -----------
72  float value I value to accumulate
73  int parm_num I number of the parameter that
74  is being accumulated
75  Modification history:
76  Programmer Date Description of change
77  ---------- ---- ---------------------
78  W. Robinson 26-Nov-1997 Original development
79 
80  *******************************************************************/ {
81  int bin;
82 
83  if (value < hmin[parm_num]) {
84  nlo[parm_num]++;
85  } else if (value > hmax[parm_num]) {
86  nhi[parm_num]++;
87  } else {
88  /*
89  * find the bin that this value is added to
90  */
91  bin = (value - hmin[parm_num]) / step[parm_num];
92  if (bin > (NBIN - 1)) bin = NBIN - 1;
93 
94  nbin[parm_num][bin]++;
95  }
96 }
97 
98 void h_out(int nbins, double *sum, double *sumsq, double *loparm, double *hiparm)
99 /*******************************************************************
100 
101  h_out
102 
103  purpose: output the results
104  Returns type: void none
105 
106  Parameters: (in calling order)
107  Type Name I/O Description
108  ---- ---- --- -----------
109  int nbins I # observations
110  double * sum I sum for 12 parameters
111  double * sumsq I sum of squares for 12 params
112  double * loparm I low parameter for 12 params
113  double * hiparm I high parameter for 12 params
114  is being accumulated
115  Modification history:
116  Programmer Date Description of change
117  ---------- ---- ---------------------
118  W. Robinson 26-Nov-1997 Original development
119 
120  *******************************************************************/ {
121  FILE *stream;
122  int bins, i;
123  float mean[NPARM], sd[NPARM], lo[NPARM], hi[NPARM];
124  /*
125  * open the file
126  */
127  if ((stream = fopen(bfile, "w")) == NULL) {
128  printf("\n\nNOTE, failure to open binary histogram file\n'%s'\n",
129  bfile);
130  printf("No action will be taken but this is a problem\n");
131  } else {
132  bins = NBIN;
133  /*
134  * write the # bins and the file name in first 416 bytes
135  */
136  fwrite(&bins, sizeof ( int), 1, stream);
137  fwrite(bfile, sizeof ( char), 412, stream);
138 
139  /*
140  * then, write the parameter info
141  */
142  fwrite(hmin, sizeof ( float), NPARM, stream);
143  fwrite(hmax, sizeof ( float), NPARM, stream);
144  fwrite(nlo, sizeof ( int), NPARM, stream);
145  fwrite(nhi, sizeof ( int), NPARM, stream);
146  fwrite(nbin, sizeof ( int), (NPARM * NBIN), stream);
147  /*
148  * compute the mean, sd
149  */
150  for (i = 0; i < NPARM; i++) {
151  if (nbins > 0) {
152  mean[i] = sum[i] / nbins;
153  sd[i] = sumsq[i] / nbins - mean[i] * mean[i];
154  if (sd[i] > 0.) {
155  sd[i] = sqrt(sd[i]);
156  } else
157  sd[i] = 0;
158  } else {
159  mean[i] = 0;
160  sd[i] = 0;
161  }
162 
163  lo[i] = loparm[i];
164  hi[i] = hiparm[i];
165  }
166  fwrite(mean, sizeof ( float), NPARM, stream);
167  fwrite(sd, sizeof ( float), NPARM, stream);
168  fwrite(lo, sizeof ( float), NPARM, stream);
169  fwrite(hi, sizeof ( float), NPARM, stream);
170  /*
171  * and close the file
172  */
173  fclose(stream);
174  }
175 }
int32 value
Definition: Granule.c:1235
int j
Definition: decode_rs.h:73
float mean(float *xs, int sample_size)
Definition: numerical.c:81
#define NULL
Definition: decode_rs.h:63
unsigned long long sumsq(signed short *in, int cnt)
void h_init(char *file)
Definition: hist.c:17
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude resolving resolving GSFcd00179 Corrected handling of fill values for[Sensor|Solar][Zenith|Azimuth] resolving MODxl01751 Changed to validate LUT version against a value retrieved from the resolving MODxl02056 Changed to calculate Solar Diffuser angles without adjustment for estimated post launch changes in the MODIS orientation relative to incidentally resolving defects MODxl01766 Also resolves MODxl01947 Changed to ignore fill values in SCI_ABNORM and SCI_STATE rather than treating them as resolving MODxl01780 Changed to use spacecraft ancillary data to recognise when the mirror encoder data is being set by side A or side B and to change calculations accordingly This removes the need for seperate LUTs for Side A and Side B data it makes the new LUTs incompatible with older versions of the and vice versa Also resolves MODxl01685 A more robust GRing algorithm is being which will create a non default GRing anytime there s even a single geolocated pixel in a granule Removed obsolete messages from seed file
Definition: HISTORY.txt:413
void h_accum(float value, int parm_num)
Definition: hist.c:61
void h_out(int nbins, double *sum, double *sumsq, double *loparm, double *hiparm)
Definition: hist.c:98
char bfile[412]
Definition: hist.c:15
#define NBIN
Definition: hist.c:8
#define NPARM
Definition: hist.c:7
int i
Definition: decode_rs.h:71
How many dimensions is the output array Default is Not sure if anything above will work correctly strcpy(l2prod->title, "no title yet")