OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
apply_despeckle.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 #include <timeutils.h>
8 
9 using namespace std;
10 
11 int main(int argc, char* argv[]) {
12  char ptime[17];
13  char buf[1024];
14 
15  get_time(ptime);
16 
17  // Open input binfile
18  static Hdf::hdf4_bin input_binfile;
19  input_binfile.open(argv[1]); //, H5F_ACC_RDONLY);
20 
21 
22  int32 n_input_bins = input_binfile.read(argv[3]);
23 
24  // Allocate BinList structure buffer
25  Hdf::binListStruct *inBinList =
26  (Hdf::binListStruct *) calloc(n_input_bins, sizeof (Hdf::binListStruct));
27 
28  // Allocate data buffer
29  float32 *inData = (float32 *) calloc(n_input_bins, sizeof (float32));
30 
31  // Read BinList & Data
32  n_input_bins = input_binfile.read(inData, inBinList);
33 
34  // Allocate bins to copy
35  int32 *binsToCopy = (int32 *) calloc(n_input_bins, sizeof (float32));
36 
37  // Open speckled bins datafile
38  FILE *fp;
39  int32 n_speckle_bins, *speckle_bins;
40  float32 *stdev, *mean;
41  fp = fopen(argv[2], "rb");
42  fread(&n_speckle_bins, sizeof (int32), 1, fp);
43  speckle_bins = (int32 *) calloc(n_speckle_bins, sizeof (int32));
44  mean = (float32 *) calloc(n_speckle_bins, sizeof (float32));
45  stdev = (float32 *) calloc(n_speckle_bins, sizeof (float32));
46  for (size_t i = 0; i < n_speckle_bins; i++) {
47  fread(&speckle_bins[i], sizeof (int32), 1, fp);
48  fread(&mean[i], sizeof (float32), 1, fp);
49  fread(&stdev[i], sizeof (float32), 1, fp);
50  }
51 
52 
53 
54  // Determine bins to copy
55  int32 lasti = 0;
56  int32 n_bins_removed = 0;
57  for (size_t j = 0; j < n_speckle_bins; j++) {
58  // if ( (j % 1000) == 0) printf("%d\n", j);
59  for (size_t i = lasti; i < n_input_bins; i++) {
60  if (inBinList[i].bin_num == speckle_bins[j]) {
61  if ((fabs(inData[i] - mean[j]) / stdev[j]) > 3.0) {
62  inBinList[i].bin_num = -1;
63  n_bins_removed++;
64  }
65  lasti = i + 1;
66  break;
67  }
68  if (inBinList[i].bin_num > speckle_bins[j]) {
69  break;
70  }
71  }
72  }
73 
74  int32 n_output_bins = 0;
75  for (size_t i = 0; i < n_input_bins; i++) {
76  if (inBinList[i].bin_num != -1) {
77  binsToCopy[n_output_bins++] = i;
78  }
79  }
80 
81  static Hdf::hdf4_bin output_binfile;
82 
83  // Copy metadata
84  output_binfile.meta_l3b = input_binfile.meta_l3b;
85  strcpy(output_binfile.meta_l3b.ptime, ptime);
86  strcat(output_binfile.meta_l3b.product_name, "_DS");
87  strcat(output_binfile.meta_l3b.soft_name, "|apply_despeckle");
88  strcat(output_binfile.meta_l3b.soft_ver, "|0.1");
89  strcat(output_binfile.meta_l3b.proc_con, "|apply_despeckle ");
90  strcat(output_binfile.meta_l3b.proc_con, argv[1]);
91  strcat(output_binfile.meta_l3b.proc_con, " ");
92  strcat(output_binfile.meta_l3b.proc_con, argv[2]);
93  strcat(output_binfile.meta_l3b.proc_con, " ");
94  strcat(output_binfile.meta_l3b.proc_con, argv[3]);
95  strcat(output_binfile.meta_l3b.proc_con, " ");
96  strcat(output_binfile.meta_l3b.proc_con, argv[4]);
97 
98  // Create output bin file
99  strcpy(buf, argv[4]);
100  output_binfile.create(buf, input_binfile.nrows);
101 
102  // Determine size of input file product list
103  char *fullprodlist = (char *) malloc(input_binfile.query());
104 
105  // Read input file product list
106  input_binfile.query(fullprodlist);
107 
108  // Copy specified bins
109  output_binfile.copy(fullprodlist, n_output_bins, binsToCopy,
110  inBinList, &input_binfile);
111 
112  // Close bin files
113  output_binfile.close();
114  input_binfile.close();
115 
116  free(fullprodlist);
117  free(inBinList);
118  free(binsToCopy);
119  free(speckle_bins);
120  free(mean);
121  free(stdev);
122 
123  printf("--Number of input bins: %8d\n", n_input_bins);
124  printf("--Number of bins removed: %8d\n", n_bins_removed);
125  printf("--Percent removed: %7.2f\n", n_bins_removed * 100. / n_input_bins);
126 
127  cout << "Normal Completion" << endl << endl;
128 
129  return 0;
130 }
131 
132 
char product_name[SM_ATTRSZ]
Definition: meta_l3b.h:16
int32_t bin_num
Definition: bin_util.h:55
int j
Definition: decode_rs.h:73
float mean(float *xs, int sample_size)
Definition: numerical.c:81
virtual int query()
Definition: bin_io.cpp:2764
void get_time(char *pr_time)
Definition: get_time.c:28
int create(const char *l3b_filename, int32_t nrows)
Definition: bin_io.cpp:160
int32_t nrows
Definition: hdf_bin.h:119
meta_l3bType meta_l3b
Definition: hdf_bin.h:131
int copy(char *product_list, int32_t nwrite, int32_t *binsToCopy, Hdf::binListStruct *inBinList, Hdf::hdf4_bin *input_binfile)
Definition: bin_io.cpp:673
int open(const char *l3b_filename)
Definition: bin_io.cpp:233
char proc_con[MD_ATTRSZ]
Definition: meta_l3b.h:35
int main(int argc, char *argv[])
#define fabs(a)
Definition: misc.h:93
char soft_ver[SM_ATTRSZ]
Definition: meta_l3b.h:33
int read(float *data, binListStruct *binList)
Definition: bin_io.cpp:590
char ptime[SM_ATTRSZ]
Definition: meta_l3b.h:34
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 close()
Definition: bin_io.cpp:951
char soft_name[SM_ATTRSZ]
Definition: meta_l3b.h:32