OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
l3despeckle.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <math.h>
3 #include <iostream>
4 #include <sstream>
5 #include <list>
6 #include "hdf4_bin.h"
7 
8 /*
9  typedef struct binListStruct {
10  int32 bin_num;
11  int16 nobs;
12  int16 nscenes;
13  int16 time_rec;
14  float32 weights;
15  uchar8 sel_cat;
16  int32 flags_set;
17  float32 lat;
18  float32 lon;
19  } binListStructure;
20 
21  */
22 
23 using namespace std;
24 
25 int main(int argc, char* argv[]) {
26  char ptime[17];
27  char buf[1024];
28 
29  get_time(ptime);
30 
31  // Open input binfile
32  static Hdf::hdf4_bin input_binfile;
33  input_binfile.open(argv[1]); //, H5F_ACC_RDONLY);
34 
35  // Get sigma value
36  float32 sigma = atof(argv[2]);
37 
38  // Alternative product array
39  char **prod_array;
40  // int nprod = input_binfile.query( &prod_array);
41  // for (size_t i=0; i<nprod; i++)
42  //cout << i << " " << prod_array[i] << endl;
43 
44  // Get number of input file bins (data_records)
45  int32 n_input_bins = input_binfile.n_data_records;
46 
47  // Get total number of data elements for two nLw fields & allocate buffer
48  int n_data_elem = input_binfile.read(argv[3]);
49  float32 *inData = (float32 *) calloc(n_data_elem, sizeof (float32));
50  float32 *inVar = (float32 *) calloc(n_data_elem, sizeof (float32));
51 
52  // Allocate BinList structure buffer
53  Hdf::binListStruct *inBinList =
54  (Hdf::binListStruct *) calloc(n_input_bins, sizeof (Hdf::binListStruct));
55 
56  // Read BinList & Data
57  int32 nread = input_binfile.read(inData, inVar, inBinList);
58 
59  // Allocate bins to copy, means, std dev
60  int32 *binsSpeckled = (int32 *) calloc(n_input_bins, sizeof (int32));
61  float32 *meansSpeckled = (float32 *) calloc(n_input_bins, sizeof (float32));
62  float32 *sdevsSpeckled = (float32 *) calloc(n_input_bins, sizeof (float32));
63 
64  // Open 9km near bin datafile
65  FILE *fp;
66  int32 n_index, n_near_bins, *index, *near_bins;
67 
68  char *tmp_str;
69  if ((tmp_str = getenv("OCDATAROOT")) == NULL) {
70  printf("OCDATAROOT environment variable is not defined.\n");
71  return (1);
72  }
73  strcpy(buf, tmp_str);
74  strcat(buf, "/common/9km_near.dat");
75 
76  fp = fopen(buf, "rb");
77  if (fp == NULL) {
78  cout << buf << " not found." << endl;
79  exit(1);
80  }
81 
82  fread(&n_index, sizeof (int32), 1, fp);
83  fread(&n_near_bins, sizeof (int32), 1, fp);
84  index = (int32 *) calloc(n_index, sizeof (int32));
85  near_bins = (int32 *) calloc(n_near_bins, sizeof (int32));
86 
87  fread(index, sizeof (int32), n_index, fp);
88  fread(near_bins, sizeof (int32), n_near_bins, fp);
89  fclose(fp);
90 
91  // Compute bin_arr (bin # to entry in inBinList[].bin_num)
92  int32 totbins = input_binfile.totbins;
93  int32 *bin_arr = (int32 *) calloc(totbins, sizeof (int32));
94  for (size_t i = 0; i < totbins; i++) bin_arr[i] = -1;
95  for (size_t i = 0; i < n_input_bins; i++) {
96  int32 bin_zerobased = inBinList[i].bin_num - 1;
97  bin_arr[bin_zerobased] = i;
98  }
99 
100  // Main Loop
101  float32 cen_val, cen_var;
102  float32 neigh_val[32];
103  float32 wgt_val[32];
104  int32 n_speckle_bins = 0;
105  for (size_t i = 0; i < n_input_bins; i++) {
106  int32 bin_zerobased = inBinList[i].bin_num - 1;
107  int32 n_close_bins = index[bin_zerobased + 1] - index[bin_zerobased];
108  int32 idx = index[bin_zerobased];
109 
110  int32 k = 0;
111  for (size_t j = 0; j < n_close_bins; j++) {
112  int32 ibin = near_bins[idx + j]; // 1-based
113  if (bin_arr[ibin - 1] != -1) {
114  if (ibin == inBinList[i].bin_num) {
115  cen_val = inData[i];
116  cen_var = inVar[i];
117  } else {
118  neigh_val[k] = inData[bin_arr[ibin - 1]];
119  wgt_val[k] = inBinList[bin_arr[ibin - 1]].weights;
120 
121  k++;
122  }
123  } // if close bin found
124  } // j-loop (n_close_bins)
125 
126  bool keep = true;
127  float32 mean, stdev;
128  if (k >= 2) {
129  float32 sum = 0;
130  float32 sum2 = 0;
131  float32 sumw = 0;
132  float32 sumw2 = 0;
133  for (size_t j = 0; j < k; j++) {
134  sum += neigh_val[j] * wgt_val[j];
135  sum2 += neigh_val[j] * neigh_val[j] * wgt_val[j];
136  sumw += wgt_val[j];
137  sumw2 += wgt_val[j] * wgt_val[j];
138  }
139  mean = sum / sumw;
140  stdev = sqrt((sum2 * sumw - sum * sum) / (sumw * sumw - sumw2));
141  if (fabs(cen_val - mean) / stdev > sigma) {
142  keep = false;
143  // printf("Speckle Bin: %7d %3d %3d %8.5f %8.5f %8.5f %8.5f %7.2f\n",
144  // inBinList[i].bin_num, inBinList[i].nobs, inBinList[i].nscenes,
145  // cen_val, sqrt(cen_var), mean, stdev,
146  // fabs(cen_val - mean) / stdev);
147 
148  // This is the entry number within the BinList (0-based),
149  // NOT the bin number itself which is 1-based.
150  binsSpeckled[n_speckle_bins] = i;
151  meansSpeckled[n_speckle_bins] = mean;
152  sdevsSpeckled[n_speckle_bins++] = stdev;
153  }
154  }
155 
156  } // i-loop (n_input_bins)
157 
158  cout << n_input_bins << " " << n_speckle_bins << endl;
159 
160 
161  // Open speckled bins output file (binary)
162  FILE *fp_out;
163  fp_out = fopen(argv[4], "wb");
164  fwrite(&n_speckle_bins, sizeof (int32), 1, fp_out);
165  for (size_t i = 0; i < n_speckle_bins; i++) {
166  int32 binnum = inBinList[binsSpeckled[i]].bin_num;
167  fwrite(&binnum, sizeof (int32), 1, fp_out);
168  fwrite(&meansSpeckled[i], sizeof (float32), 1, fp_out);
169  fwrite(&sdevsSpeckled[i], sizeof (float32), 1, fp_out);
170  }
171 
172  free(inData);
173  free(inVar);
174  free(inBinList);
175  free(binsSpeckled);
176  free(meansSpeckled);
177  free(sdevsSpeckled);
178  cout << "Normal Completion" << endl << endl;
179 
180  return 0;
181 }
182 
an array had not been initialized Several spelling and grammar corrections were which is read from the appropriate MCF the above metadata values were hard coded A problem calculating the average background DN for SWIR bands when the moon is in the space view port was corrected The new algorithm used to calculate the average background DN for all reflective bands when the moon is in the space view port is now the same as the algorithm employed by the thermal bands For non SWIR changes in the averages are typically less than Also for non SWIR the black body DNs remain a backup in case the SV DNs are not available For SWIR the changes in computed averages were larger because the old which used the black body suffered from contamination by the micron leak As a consequence of the if SV DNs are not available for the SWIR the EV pixels will not be the granule time is used to identify the appropriate tables within the set given for one LUT the first two or last two tables respectively will be used for the interpolation If there is only one LUT in the set of it will be treated as a constant LUT The manner in which Earth View data is checked for saturation was changed Previously the raw Earth View DNs and Space View DNs were checked against the lookup table values contained in the table dn_sat The change made is to check the raw Earth and Space View DNs to be sure they are less than the maximum saturation value and to check the Space View subtracted Earth View dns against a set of values contained in the new lookup table dn_sat_ev The metadata configuration and ASSOCIATEDINSTRUMENTSHORTNAME from the MOD02HKM product The same metatdata with extensions and were removed from the MOD021KM and MOD02OBC products ASSOCIATEDSENSORSHORTNAME was set to MODIS in all products These changes are reflected in new File Specification which users may consult for exact the pow functions were eliminated in Emissive_Cal and Emissive bands replaced by more efficient code Other calculations throughout the code were also made more efficient Aside from a few round off there was no difference to the product The CPU time decreased by about for a day case and for a night case A minor bug in calculating the uncertainty index for emissive bands was corrected The frame index(0-based) was previously being used the frame number(1-based) should have been used. There were only a few minor changes to the uncertainty index(maximum of 1 digit). 3. Some inefficient arrays(Sigma_RVS_norm_sq) were eliminated and some code lines in Preprocess_L1A_Data were moved into Process_OBCEng_Emiss. There were no changes to the product. Required RAM was reduced by 20 MB. Now
int32_t bin_num
Definition: bin_util.h:55
int main(int argc, char *argv[])
Definition: l3despeckle.cpp:25
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
void get_time(char *pr_time)
Definition: get_time.c:28
int open(const char *l3b_filename)
Definition: bin_io.cpp:233
#define fabs(a)
Definition: misc.h:93
int read(float *data, binListStruct *binList)
Definition: bin_io.cpp:590
int64_t totbins
Definition: hdf_bin.h:118
int32_t n_data_records
Definition: hdf_bin.h:122
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")
int k
Definition: decode_rs.h:73