OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
read_l3bin.cpp
Go to the documentation of this file.
1 // =====================================================================
2 // read_l3bin - level-3 bin reader for l2gen
3 // B. Franz, NASA/OBPG, March 2013
4 // =====================================================================
5 
6 #include <stdio.h>
7 #include <math.h>
8 #include <iostream>
9 #include <sstream>
10 #include <list>
11 #include "hdf_bin.h"
12 #include <strings.h>
13 
14 #include "read_l3bin.h"
15 #include "l12_proto.h"
16 
17 using namespace std;
18 
27 extern "C" int32_t read_l3bin(char *file, l3binstr *l3bin, int32_t nbands) {
28  int32_t i, j, k, iprod, band;
29 
30  // Open input binfile and to get bin dimension
31  cout << endl << "Opening " << file << " for reading" << endl;
32  Hdf::hdf_bin *binfile;
33  binfile = Hdf::openBinObject(file);
34  if (binfile == NULL) {
35  fprintf(stderr, "-E- Unable to open %s for reading.\n", file);
36  exit(EXIT_FAILURE);
37  }
38  int32_t nbins = binfile->n_data_records;
39  int32_t nrows = binfile->nrows;
40  l3bin->sensorID = sensorName2SensorId(binfile->meta_l3b.sensor_name); //binfile->meta_l3b.sensorID;
41  char **prodlist;
42  int32 nprods = binfile->query(&prodlist);
43 
44  // Allocate space for output and work space for i/o
45  l3bin->data = (float **) malloc(nbins * sizeof (float *));
46  for (i = 0; i < nbins; i++) {
47  if ((l3bin->data[i] = (float *) calloc(nbands, sizeof (float))) == NULL) {
48  printf("-E- Cannot allocate memory to hold L3 data\n");
49  exit(EXIT_FAILURE);
50  }
51  }
52  l3bin->nobs = (int32_t *) calloc(nbins, sizeof (int32_t));
53  l3bin->nscenes = (int32_t *) calloc(nbins, sizeof (int32_t));
54  l3bin->chl = (float *) calloc(nbins, sizeof (float));
55  l3bin->tau = (float *) calloc(nbins, sizeof (float));
56  l3bin->bins = (int32_t *) calloc(nbins, sizeof (int32_t));
57  l3bin->basebin = (int32_t *) calloc(nrows, sizeof (int32_t));
58  l3bin->numbin = (int32_t *) calloc(nrows, sizeof (int32_t));
59  l3bin->wavelengths = (float *) calloc(nbands, sizeof(float));
60 
61  float **inData;
62  if ((l3bin->prods = (char **) malloc(nprods * sizeof (char *))) == NULL) {
63  printf("-E- Error allocating memory to the L3 product list .\n");
64  exit(EXIT_FAILURE);
65  }
66  if ((inData = (float **) malloc(nprods * sizeof (float *))) == NULL) {
67  printf("-E- Error allocating memory to the L3 input data array.\n");
68  exit(EXIT_FAILURE);
69  }
70  for (i = 0; i < nprods; i++) {
71  if ((inData[i] = (float *) calloc(2 * MAXPIX, sizeof (float))) == NULL) {
72  printf("-E- Error allocating memory to the L3 input data array.\n");
73  exit(EXIT_FAILURE);
74  }
75  if ((l3bin->prods[i] = (char *) calloc(32, sizeof (char))) == NULL) {
76  printf("-E- Error allocating memory to the L3 product list.\n");
77  exit(EXIT_FAILURE);
78  }
79  }
80 
81  char buf[FILENAME_MAX];
82  char prodstr[L1_PRODSTRLEN] = "";
83  char *wavelengthStr;
84  j = 0;
85  l3bin->hasRrs = 0;
86  l3bin->nwave = 0;
87  for (i = 0; i < nprods; i++) {
88  if ((strcmp(prodlist[i], "chlor_a") == 0) ||
89  (strncmp(prodlist[i], "aot", 3) == 0) ||
90  (strncmp(prodlist[i], "tau", 3) == 0) ||
91  (strncmp(prodlist[i], "nLw", 3) == 0) ||
92  (strncmp(prodlist[i], "Rrs", 3) == 0)) {
93  strcpy(l3bin->prods[j++], prodlist[i]);
94  l3bin->nprods++;
95  if ((strncmp(prodlist[i], "nLw", 3) == 0) ||
96  (strncmp(prodlist[i], "Rrs", 3) == 0)) {
97  strcpy(buf,prodlist[i]);
98  wavelengthStr = strtok(buf, "_");
99  wavelengthStr = strtok(NULL,"\0");
100  l3bin->wavelengths[l3bin->nwave] = (float) atof(wavelengthStr);
101  l3bin->nwave++;
102  if (strncmp(prodlist[i], "Rrs", 3) == 0)
103  l3bin->hasRrs = 1;
104  }
105  strcat(prodstr, prodlist[i]);
106  if (i < nprods - 1)
107  strcat(prodstr, ",");
108  }
109  }
110  binfile->read(prodstr);
111 
112  // Save info to return struct
113  l3bin->file = strdup(file);
114  l3bin->nbins = nbins;
115  l3bin->nrows = nrows;
116  int bindx = 0;
117 
118  for (int krow = 0; krow < nrows; krow++) {
119  int basebin = binfile->get_basebin(krow);
120  int numbin = binfile->get_numbin(krow);
121  l3bin->basebin[krow] = basebin;
122  l3bin->numbin[krow] = numbin;
123  binfile->readBinIndex(krow);
124 
125  int ext = binfile->get_ext();
126  int beg = binfile->get_beg();
127  // if the row has no filled bins, skip it
128  if (beg == 0)
129  continue;
130 
131  int status = binfile->readBinList(ext);
132  // if something went horribly wrong...
133  if (status == -1) {
134  printf("Unable to read bin numbers...: %d\n", ext);
135  exit(EXIT_FAILURE);
136  }
137 
138  // fill the input data array with the necessary input data
139  iprod = 0;
140 
141  for (int j = 0; j < binfile->nprod(); j++) {
142 
143  if (binfile->active_data_prod[j] == true) {
144  binfile->get_prodname(j, buf);
145  if (strcmp(prodlist[j], buf) == 0) {
146  binfile->readSums(inData[iprod], ext, j);
147  iprod++;
148  }
149  }
150  }
151 
152  if (binfile->isHDF5 || binfile->isCDF4)
153  binfile->setDataPtr(ext);
154 
155  // Apply the weights to get the mean from the sum
156  for (k = 0; k < ext; k++) {
157  float weight = binfile->get_weights(k);
158  band = 0;
159  for (iprod = 0; iprod < l3bin->nprods; iprod++) {
160  if (strcmp(l3bin->prods[iprod], "chlor_a") == 0) {
161  l3bin->chl[bindx] = inData[iprod][2 * k] / weight;
162  } else if ((strncmp(l3bin->prods[iprod], "tau", 3) == 0) ||
163  (strncmp(l3bin->prods[iprod], "aot", 3) == 0)) {
164  l3bin->tau[bindx] = inData[iprod][2 * k] / weight;
165  } else if ((strncmp(l3bin->prods[iprod], "nLw", 3) == 0) ||
166  (strncmp(l3bin->prods[iprod], "Rrs", 3) == 0)) {
167  l3bin->data[bindx][band++] = inData[iprod][2 * k] / weight;
168  }
169  }
170  l3bin->bins[bindx] = binfile->get_bin_num(k);
171  l3bin->nobs[bindx] = binfile->get_nobs(k);
172  l3bin->nscenes[bindx] = binfile->get_nscenes(k);
173  bindx++;
174  }
175 
176  }
177  // Clean-up
178  binfile->close();
179  for (i = 0; i < nprods; i++)
180  free(inData[i]);
181  free(inData);
182 
183  return (nbins);
184 }
185 
virtual int64_t get_bin_num(int kbin)=0
virtual int get_nobs(int kbin)=0
const int bindx[3]
Definition: DbLutNetcdf.cpp:28
int j
Definition: decode_rs.h:73
int status
Definition: l1_czcs_hdf.c:32
virtual int readSums(float *sums, int32_t nbins_to_read, int iprod)=0
#define NULL
Definition: decode_rs.h:63
hdf_bin * openBinObject(const char *binFileName)
Definition: bin_io.cpp:32
PARAM_TYPE_NONE Default value No parameter is buried in the product name name_prefix is case insensitive string compared to the product name PARAM_TYPE_VIS_WAVE The visible wavelength bands from the sensor are buried in the product name The product name is compared by appending and name_suffix ie aph_412_giop where prod_ix will be set to PARAM_TYPE_IR_WAVE same search method as PARAM_TYPE_VIS_WAVE except only wavelength above are looped through but prod_ix is still based ie aph_2_giop for the second band
int sensorName2SensorId(const char *name)
Definition: sensorInfo.c:268
virtual int close()=0
virtual int64_t get_numbin(int irow)
Definition: hdf_bin.h:72
bool active_data_prod[MAXNVDATA]
Definition: hdf_bin.h:121
virtual int64_t get_beg()=0
virtual int get_nscenes(int kbin)=0
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
int32_t prodlist(int32_t sensorID, int32_t evalmask, const char *inprod, const char *defprod, char outprod[L1_MAXPROD][32])
virtual int get_ext()=0
virtual int query()
Definition: bin_io.cpp:2764
int32_t krow
Definition: l2bin.cpp:90
int32_t read_l3bin(char *file, l3binstr *l3bin, int32_t nbands)
Definition: read_l3bin.cpp:27
virtual int64_t get_basebin(int irow)
Definition: hdf_bin.h:76
char * strdup(const char *)
int32_t nrows
Definition: hdf_bin.h:119
virtual int32_t nprod()
Definition: hdf_bin.h:108
#define L1_PRODSTRLEN
Definition: filehandle.h:19
meta_l3bType meta_l3b
Definition: hdf_bin.h:131
int32 nrows
bool isCDF4
Definition: hdf_bin.h:125
virtual int setDataPtr(int nbins_to_read)=0
virtual int get_prodname(int iprod, char *prodname)
Definition: bin_io.cpp:2802
int32_t nbands
#define MAXPIX
Definition: l1.h:50
bool isHDF5
Definition: hdf_bin.h:124
virtual int read(char *product_list)
Definition: bin_io.cpp:358
int32_t n_data_records
Definition: hdf_bin.h:122
virtual int readBinIndex(int row_num_to_read)=0
virtual int readBinList(int nbins_to_read)=0
char sensor_name[SM_ATTRSZ]
Definition: meta_l3b.h:19
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
virtual float get_weights(int kbin)=0