OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
L3File.h
Go to the documentation of this file.
1 #ifndef L3File_h
2 #define L3File_h
3 
4 #include <hdf_bin.h>
5 #include <vector>
6 #include <deque>
7 #include <list>
8 #include <stdint.h>
9 
10 #include <boost/geometry.hpp>
11 #include <boost/geometry/geometries/point_xy.hpp>
12 #include <boost/geometry/geometries/polygon.hpp>
13 #include <boost/geometry/geometries/box.hpp>
14 #include <boost/foreach.hpp>
15 
16 #include <L3Shape.h>
17 
18 namespace l3 {
19 const float missingPixelValue = -32767.0;
20 
21 typedef boost::geometry::model::d2::point_xy<float> Point_t;
22 typedef boost::geometry::model::polygon<Point_t> Polygon_t;
23 typedef boost::geometry::model::box<Point_t> Box_t;
24 
25 template<typename Geometry>
26 inline float calcIntersectionArea(Box_t &box, Geometry &geo) {
27  std::deque<Geometry> outPolygons;
28  boost::geometry::intersection(box, geo, outPolygons);
29  float theArea = 0.0;
30 
31  BOOST_FOREACH(Geometry const& p, outPolygons) {
32  theArea += boost::geometry::area(p);
33  }
34  return theArea;
35 }
36 
37 template<>
38 inline float calcIntersectionArea<Box_t>(Box_t &box, Box_t &geo) {
39  Box_t outBox;
40  boost::geometry::intersection(box, geo, outBox);
41  return boost::geometry::area(outBox);
42 }
43 
44 class L3Row;
45 class L3File;
46 
50 class L3Bin {
51  friend class L3Row;
52  friend class L3File;
53  friend class L3FileSMI;
54 
55 private:
56  static const uint8_t qualityUnused = 255;
57 
58  void internalAllocate(int32_t numProducts);
59  void internalCopy(const L3Bin &bin);
60  void internalAdd(const L3Bin &bin);
61  void internalInit(int32_t numProducts);
62  void checkProdId(int32_t prodId) const;
63 
64 protected:
65  int32_t numProducts;
66  int64_t binNum;
67  int64_t recordNum;
68  int32_t nobs;
69  int32_t nscenes;
70  uint8_t quality;
71  float timeRec; //< timeRec/nobs = observation time (sec since 1993 TAI)
72  float weights;
73  float *sums;
74  float *sumSquares;
75 
76 public:
77  L3Bin(int32_t numProducts = 1);
78  L3Bin(const L3Bin &bin);
79  ~L3Bin();
80 
81  void clear();
82 
83  L3Bin& operator=(const L3Bin &bin);
84  L3Bin& operator+=(const L3Bin &bin);
85 
86  void addWeighted(const L3Bin &bin, float weighting);
87 
88  template<typename Geometry1, typename Geometry2>
89  inline void addWeighted(const L3Bin &bin, Geometry1 &box, Geometry2 &geo) {
90  float theArea = calcIntersectionArea(box, geo);
91  if (theArea > 0.0)
92  addWeighted(bin, theArea);
93  }
94 
95  void setNumProducts(int32_t numProducts);
96  int32_t getNumProducts() const;
97  int64_t getBinNum() const;
98  int32_t getNobs() const;
99  int32_t getNscenes() const;
100  int64_t getRecordNum() const;
101  float getObsTime() const;
102  float getWeights() const;
103  uint8_t getQuality() const;
104 
105  float getSum(int32_t prodId = 0) const;
106  float getSumSquares(int32_t prodId = 0) const;
107 
108  float getMean(int32_t prodId = 0) const;
109  float getVariance(int32_t prodId = 0) const;
110  float getStdev(int32_t prodId = 0) const;
111 
112 };
113 
117 class L3Row {
118  friend class L3File;
119  friend class L3FileSMI;
120 
121 protected:
122  int32_t row;
123  int32_t numProducts;
124  int64_t numBins;
125  std::vector<L3Bin*> binArray;
126  int64_t lastBin; //< bin index found by getBin last time
127 
128 public:
129  L3Row(int32_t row, int64_t numBins, int32_t numProducts);
130  ~L3Row();
131 
132  int32_t getRow() const;
133  int32_t getNumProducts() const;
134  int64_t getNumBins() const;
135  void setNumBins(int64_t numBins);
136  L3Bin* getBin(int64_t binNum);
137  L3Bin* getBinByIndex(int32_t index);
138 };
139 
143 class L3File {
144 protected:
147  std::list<L3Row*> rowList;
149  int64_t *baseRecord;
150  int32_t *extentbin;
151  float* sumBuffer;
153  uint8_t* qualityBuffer;
154  size_t *prodMap;
155  size_t numProds; // number of active Products
156  std::vector<std::string> activeProdNameList;
157 
158  virtual L3Row* readRow(int32_t row);
159 
160  virtual int initRecordLookup();
161 
170  template<typename Geometry>
171  inline bool addBinsFromRow(float lat0, float lon0, Geometry geo,
172  bool areaWeighted) {
173  bool foundIntersect = false;
174  int32_t row0, col0;
175  shape->latlon2rowcol(lat0, lon0, row0, col0);
176 
177  int32_t col = col0;
178  float lat, lon;
179 
180  float deltaLat = 90.0 / shape->getNumRows();
181  float deltaLon = 180.0 / shape->getNumCols(row0);
182 
183  // look right
184  bool binIntersects = true;
185  while (binIntersects) {
186  if (col > shape->getNumCols(row0))
187  break;
188  shape->rowcol2latlon(row0, col, lat, lon);
189  Point_t pMin(lon - deltaLon, lat - deltaLat);
190  Point_t pMax(lon + deltaLon, lat + deltaLat);
191  Box_t box(pMin, pMax);
192  binIntersects = boost::geometry::intersects(geo, box);
193  if (binIntersects) {
194  foundIntersect = true;
195  L3Bin* tmpBin = getBin(row0, col);
196  if (tmpBin) {
197  if (areaWeighted) {
198  outBin.addWeighted(*tmpBin, box, geo);
199  } else
200  outBin += *tmpBin;
201  }
202  col++;
203  }
204  }
205 
206  // look left
207  col = col0 - 1;
208  binIntersects = true;
209  while (binIntersects) {
210  if (col < 0)
211  break;
212  shape->rowcol2latlon(row0, col, lat, lon);
213  Point_t pMin(lon - deltaLon, lat - deltaLat);
214  Point_t pMax(lon + deltaLon, lat + deltaLat);
215  Box_t box(pMin, pMax);
216  binIntersects = boost::geometry::intersects(geo, box);
217  if (binIntersects) {
218  foundIntersect = true;
219  L3Bin* tmpBin = getBin(row0, col);
220  if (tmpBin) {
221  if (areaWeighted) {
222  outBin.addWeighted(*tmpBin, box, geo);
223  } else
224  outBin += *tmpBin;
225  }
226  col--;
227  }
228  }
229 
230  return foundIntersect;
231  }
232 
233 public:
234  L3File();
235  virtual ~L3File();
236 
237  virtual int64_t rowbin2record(int32_t row, int64_t bin);
238  virtual int64_t rowcol2record(int32_t row, int32_t col);
239  virtual int64_t latlon2record(float lat, float lon);
240  virtual int64_t bin2record(int64_t bin);
241 
242  virtual void clearCache();
243  virtual void setNumCacheRows(int32_t numRows);
244  virtual bool open(const char* fileName);
245  virtual void close();
246  virtual meta_l3bType* getMetaData();
247  virtual int32_t getNumProducts();
248  virtual std::string getProductName(size_t index = 0);
249  virtual bool setActiveProductList(const char* prodStr);
250  virtual int32_t getNumActiveProducts();
251  virtual std::string getActiveProductName(size_t index = 0);
252  virtual int32_t getNumRows();
253  virtual L3Row* getRow(int32_t row);
254  virtual L3Bin* getBin(int32_t row, int32_t col);
255  virtual L3Bin* getClosestBin(float lat, float lon);
256 
265  template<typename Geometry>
266  inline L3Bin* getBinsInside(Geometry geo, bool areaWeighted = false) {
267  outBin.clear();
268 
269  // get the center of the geometry
270  Point_t geoCenter;
271  boost::geometry::centroid(geo, geoCenter);
272 
273  // find r,c of closest bin
274  float geoLat = constrainLat(geoCenter.y());
275  float geoLon = constrainLon(geoCenter.x());
276  geoCenter.y(geoLat);
277  geoCenter.x(geoLon);
278 
279  int32_t geoRow, geoCol; // note row,col are 0 based
280  float tmpFloat;
281  shape->latlon2rowcol(geoLat, geoLon, geoRow, geoCol);
282  shape->rowcol2latlon(geoRow, geoCol, geoLat, tmpFloat); // adjust lat on the row center
283 
284  float deltaLat = 180.0 / shape->getNumRows();
285  float lat = geoLat;
286  bool binFound = true;
287 
288  // look up
289  while (binFound) {
290  if (lat > 90.0)
291  break;
292  binFound = addBinsFromRow(lat, geoLon, geo, areaWeighted);
293  lat = lat + deltaLat;
294  }
295 
296  // look down
297  binFound = true;
298  lat = geoLat - deltaLat;
299  while (binFound) {
300  if (lat < -90.0)
301  break;
302  binFound = addBinsFromRow(lat, geoLon, geo, areaWeighted);
303  lat = lat - deltaLat;
304  }
305 
306  if (outBin.nobs == 0)
307  return NULL;
308  else
309  return &outBin;
310  }
311 
312  virtual Hdf::hdf_bin* getHdfBinObject() const;
313  virtual bool hasQuality();
314  virtual void setQualityProcessing(bool val);
315  virtual bool getQualityProcessing() const;
316 
317  virtual L3Shape* getShape() const {
318  return shape;
319  }
320 
321 };
322 
323 } // namespace l3
324 
325 #endif
virtual void close()
Definition: L3File.cpp:443
double constrainLat(double lat)
Definition: L3Shape.cpp:13
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
~L3Bin()
Definition: L3File.cpp:24
boost::geometry::model::box< Point_t > Box_t
Definition: L3File.h:23
virtual L3Bin * getClosestBin(float lat, float lon)
Definition: L3File.cpp:739
std::vector< std::string > activeProdNameList
array of active product names
Definition: L3File.h:156
int64_t numBins
Definition: L3File.h:124
L3Bin & operator=(const L3Bin &bin)
Definition: L3File.cpp:132
float * sumSquares
Definition: L3File.h:74
virtual bool hasQuality()
Definition: L3File.cpp:788
virtual int32_t getNumCols(int32_t row) const =0
float calcIntersectionArea< Box_t >(Box_t &box, Box_t &geo)
Definition: L3File.h:38
Hdf::hdf_bin * binObj
Definition: L3File.h:146
uint8_t * qualityBuffer
buffer for the quality (2*nrows)
Definition: L3File.h:153
float getSumSquares(int32_t prodId=0) const
Definition: L3File.cpp:253
int32_t * extentbin
save the number of records in each row
Definition: L3File.h:150
#define NULL
Definition: decode_rs.h:63
L3Bin * getBin(int64_t binNum)
Definition: L3File.cpp:335
virtual std::string getActiveProductName(size_t index=0)
Definition: L3File.cpp:632
int32_t nscenes
Definition: L3File.h:69
std::list< L3Row * > rowList
Definition: L3File.h:147
virtual bool open(const char *fileName)
Definition: L3File.cpp:430
std::vector< L3Bin * > binArray
Definition: L3File.h:125
int64_t * baseRecord
first record index of each row (0 based)
Definition: L3File.h:149
boost::geometry::model::d2::point_xy< float > Point_t
Definition: L3File.h:21
virtual int64_t latlon2record(float lat, float lon)
Definition: L3File.cpp:562
bool addBinsFromRow(float lat0, float lon0, Geometry geo, bool areaWeighted)
Definition: L3File.h:171
virtual L3Bin * getBin(int32_t row, int32_t col)
Definition: L3File.cpp:761
int32_t row
Definition: L3File.h:122
float * lat
int32_t numProducts
Definition: L3File.h:65
int32_t nobs
Definition: L3File.h:68
double constrainLon(double lon)
Definition: L3Shape.cpp:26
virtual void setNumCacheRows(int32_t numRows)
Definition: L3File.cpp:420
float getWeights() const
Definition: L3File.cpp:226
virtual L3Shape * getShape() const
Definition: L3File.h:317
@ string
L3Bin * getBinByIndex(int32_t index)
Definition: L3File.cpp:381
virtual int32_t getNumRows()
Definition: L3File.cpp:726
virtual bool setActiveProductList(const char *prodStr)
Definition: L3File.cpp:591
virtual void latlon2rowcol(double lat, double lon, int32_t &row, int32_t &col) const =0
virtual ~L3File()
Definition: L3File.cpp:403
L3Bin & operator+=(const L3Bin &bin)
Definition: L3File.cpp:137
virtual int32_t getNumProducts()
Definition: L3File.cpp:581
int32_t getNumProducts() const
Definition: L3File.cpp:178
virtual std::string getProductName(size_t index=0)
Definition: L3File.cpp:587
uint8_t quality
Definition: L3File.h:70
virtual void setQualityProcessing(bool val)
Definition: L3File.cpp:795
virtual L3Row * getRow(int32_t row)
Definition: L3File.cpp:695
virtual int32_t getNumActiveProducts()
Definition: L3File.cpp:628
virtual int64_t rowcol2record(int32_t row, int32_t col)
Definition: L3File.cpp:550
virtual void rowcol2latlon(int32_t row, int32_t col, double &lat, double &lon) const =0
L3Bin outBin
accumulation bin for lookups
Definition: L3File.h:152
float weights
Definition: L3File.h:72
uint8_t getQuality() const
Definition: L3File.cpp:234
virtual void clearCache()
Definition: L3File.cpp:411
int32_t getRow() const
Definition: L3File.cpp:311
L3Bin * getBinsInside(Geometry geo, bool areaWeighted=false)
Definition: L3File.h:266
virtual meta_l3bType * getMetaData()
Definition: L3File.cpp:475
const float missingPixelValue
Definition: L3File.h:19
virtual L3Row * readRow(int32_t row)
Definition: L3File.cpp:636
L3Bin(int32_t numProducts=1)
Definition: L3File.cpp:16
float getObsTime() const
Definition: L3File.cpp:218
int64_t getNumBins() const
Definition: L3File.cpp:319
int64_t getBinNum() const
Definition: L3File.cpp:186
int64_t binNum
Definition: L3File.h:66
void addWeighted(const L3Bin &bin, Geometry1 &box, Geometry2 &geo)
Definition: L3File.h:89
float timeRec
Definition: L3File.h:71
int numCacheRows
Definition: L3File.h:148
int32_t getNobs() const
Definition: L3File.cpp:194
size_t numProds
Definition: L3File.h:155
void clear()
Definition: L3File.cpp:29
void setNumBins(int64_t numBins)
Definition: L3File.cpp:323
int64_t getRecordNum() const
Definition: L3File.cpp:210
float getMean(int32_t prodId=0) const
Definition: L3File.cpp:263
virtual int32_t getNumRows() const
Definition: L3Shape.cpp:55
int32_t getNumProducts() const
Definition: L3File.cpp:315
float getStdev(int32_t prodId=0) const
Definition: L3File.cpp:288
float getVariance(int32_t prodId=0) const
Definition: L3File.cpp:273
float calcIntersectionArea(Box_t &box, Geometry &geo)
Definition: L3File.h:26
void setNumProducts(int32_t numProducts)
Definition: L3File.cpp:163
virtual Hdf::hdf_bin * getHdfBinObject() const
Definition: L3File.cpp:784
float * lon
Definition: L3File.cpp:10
L3Row(int32_t row, int64_t numBins, int32_t numProducts)
Definition: L3File.cpp:296
int64_t recordNum
Definition: L3File.h:67
float * sums
Definition: L3File.h:73
int32_t getNscenes() const
Definition: L3File.cpp:202
virtual bool getQualityProcessing() const
Definition: L3File.cpp:811
msiBandIdx val
Definition: l1c_msi.cpp:34
size_t * prodMap
index map used to map my product index into binObj's index
Definition: L3File.h:154
void addWeighted(const L3Bin &bin, float weighting)
Definition: L3File.cpp:142
int32_t numProducts
Definition: L3File.h:123
virtual int64_t rowbin2record(int32_t row, int64_t bin)
Definition: L3File.cpp:529
float p[MODELMAX]
Definition: atrem_corl1.h:131
float getSum(int32_t prodId=0) const
Definition: L3File.cpp:243
virtual int initRecordLookup()
Definition: L3File.cpp:486
boost::geometry::model::polygon< Point_t > Polygon_t
Definition: L3File.h:22
float * sumBuffer
buffer for the sums (sum, sumSq, 2*nrows, numProd)
Definition: L3File.h:151
virtual int64_t bin2record(int64_t bin)
Definition: L3File.cpp:575
L3Shape * shape
Definition: L3File.h:145
int64_t lastBin
Definition: L3File.h:126