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  int64_t getFirstBinNum();
139  int64_t getLastBinNum();
140 };
141 
145 class L3File {
146 protected:
149  std::list<L3Row*> rowList;
151  int64_t *baseRecord;
152  int32_t *extentbin;
153  float* sumBuffer;
155  uint8_t* qualityBuffer;
156  size_t *prodMap;
157  size_t numProds; // number of active Products
158  std::vector<std::string> activeProdNameList;
159 
160  virtual L3Row* readRow(int32_t row);
161 
162  virtual int initRecordLookup();
163 
172  template<typename Geometry>
173  inline bool addBinsFromRow(float lat0, float lon0, Geometry geo,
174  bool areaWeighted) {
175  bool foundIntersect = false;
176  int32_t row0, col0;
177  shape->latlon2rowcol(lat0, lon0, row0, col0);
178 
179  int32_t col = col0;
180  float lat, lon;
181 
182  float deltaLat = 90.0 / shape->getNumRows();
183  float deltaLon = 180.0 / shape->getNumCols(row0);
184 
185  // look right
186  bool binIntersects = true;
187  while (binIntersects) {
188  if (col > shape->getNumCols(row0))
189  break;
190  shape->rowcol2latlon(row0, col, lat, lon);
191  Point_t pMin(lon - deltaLon, lat - deltaLat);
192  Point_t pMax(lon + deltaLon, lat + deltaLat);
193  Box_t box(pMin, pMax);
194  binIntersects = boost::geometry::intersects(geo, box);
195  if (binIntersects) {
196  foundIntersect = true;
197  L3Bin* tmpBin = getBin(row0, col);
198  if (tmpBin) {
199  if (areaWeighted) {
200  outBin.addWeighted(*tmpBin, box, geo);
201  } else
202  outBin += *tmpBin;
203  }
204  col++;
205  }
206  }
207 
208  // look left
209  col = col0 - 1;
210  binIntersects = true;
211  while (binIntersects) {
212  if (col < 0)
213  break;
214  shape->rowcol2latlon(row0, col, lat, lon);
215  Point_t pMin(lon - deltaLon, lat - deltaLat);
216  Point_t pMax(lon + deltaLon, lat + deltaLat);
217  Box_t box(pMin, pMax);
218  binIntersects = boost::geometry::intersects(geo, box);
219  if (binIntersects) {
220  foundIntersect = true;
221  L3Bin* tmpBin = getBin(row0, col);
222  if (tmpBin) {
223  if (areaWeighted) {
224  outBin.addWeighted(*tmpBin, box, geo);
225  } else
226  outBin += *tmpBin;
227  }
228  col--;
229  }
230  }
231 
232  return foundIntersect;
233  }
234 
235 public:
236  L3File();
237  virtual ~L3File();
238 
239  virtual int64_t rowbin2record(int32_t row, int64_t bin);
240  virtual int64_t rowcol2record(int32_t row, int32_t col);
241  virtual int64_t latlon2record(float lat, float lon);
242  virtual int64_t bin2record(int64_t bin);
243 
244  virtual void clearCache();
245  virtual void setNumCacheRows(int32_t numRows);
246  virtual bool open(const char* fileName);
247  virtual void close();
248  virtual meta_l3bType* getMetaData();
249  virtual int32_t getNumProducts();
250  virtual std::string getProductName(size_t index = 0);
251  virtual bool setActiveProductList(const char* prodStr);
252  virtual int32_t getNumActiveProducts();
253  virtual std::string getActiveProductName(size_t index = 0);
254  virtual int32_t getNumRows();
255  virtual L3Row* getRow(int32_t row);
256  virtual L3Bin* getBin(int32_t row, int32_t col);
257  virtual L3Bin* getClosestBin(float lat, float lon);
258 
267  template<typename Geometry>
268  inline L3Bin* getBinsInside(Geometry geo, bool areaWeighted = false) {
269  outBin.clear();
270 
271  // get the center of the geometry
272  Point_t geoCenter;
273  boost::geometry::centroid(geo, geoCenter);
274 
275  // find r,c of closest bin
276  float geoLat = constrainLat(geoCenter.y());
277  float geoLon = constrainLon(geoCenter.x());
278  geoCenter.y(geoLat);
279  geoCenter.x(geoLon);
280 
281  int32_t geoRow, geoCol; // note row,col are 0 based
282  float tmpFloat;
283  shape->latlon2rowcol(geoLat, geoLon, geoRow, geoCol);
284  shape->rowcol2latlon(geoRow, geoCol, geoLat, tmpFloat); // adjust lat on the row center
285 
286  float deltaLat = 180.0 / shape->getNumRows();
287  float lat = geoLat;
288  bool binFound = true;
289 
290  // look up
291  while (binFound) {
292  if (lat > 90.0)
293  break;
294  binFound = addBinsFromRow(lat, geoLon, geo, areaWeighted);
295  lat = lat + deltaLat;
296  }
297 
298  // look down
299  binFound = true;
300  lat = geoLat - deltaLat;
301  while (binFound) {
302  if (lat < -90.0)
303  break;
304  binFound = addBinsFromRow(lat, geoLon, geo, areaWeighted);
305  lat = lat - deltaLat;
306  }
307 
308  if (outBin.nobs == 0)
309  return NULL;
310  else
311  return &outBin;
312  }
313 
314  virtual Hdf::hdf_bin* getHdfBinObject() const;
315  virtual bool hasQuality();
316  virtual void setQualityProcessing(bool val);
317  virtual bool getQualityProcessing() const;
318  virtual int64_t getBaseBin(int32_t row) const;
319  virtual int32_t getRowExtent(int32_t row) const;
320 
321  virtual L3Shape* getShape() const {
322  return shape;
323  }
324 
325 };
326 
327 } // namespace l3
328 
329 #endif
virtual void close()
Definition: L3File.cpp:467
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
virtual int32_t getRowExtent(int32_t row) const
Get the extent of bins in a row.
Definition: L3File.cpp:857
boost::geometry::model::box< Point_t > Box_t
Definition: L3File.h:23
virtual L3Bin * getClosestBin(float lat, float lon)
Definition: L3File.cpp:763
std::vector< std::string > activeProdNameList
array of active product names
Definition: L3File.h:158
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:812
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:148
uint8_t * qualityBuffer
buffer for the quality (2*nrows)
Definition: L3File.h:155
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:152
#define NULL
Definition: decode_rs.h:63
L3Bin * getBin(int64_t binNum)
Definition: L3File.cpp:336
virtual std::string getActiveProductName(size_t index=0)
Definition: L3File.cpp:656
int32_t nscenes
Definition: L3File.h:69
std::list< L3Row * > rowList
Definition: L3File.h:149
virtual bool open(const char *fileName)
Definition: L3File.cpp:454
std::vector< L3Bin * > binArray
Definition: L3File.h:125
int64_t * baseRecord
first record index of each row (0 based)
Definition: L3File.h:151
boost::geometry::model::d2::point_xy< float > Point_t
Definition: L3File.h:21
virtual int64_t latlon2record(float lat, float lon)
Definition: L3File.cpp:586
bool addBinsFromRow(float lat0, float lon0, Geometry geo, bool areaWeighted)
Definition: L3File.h:173
virtual L3Bin * getBin(int32_t row, int32_t col)
Definition: L3File.cpp:785
int32_t row
Definition: L3File.h:122
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:444
float getWeights() const
Definition: L3File.cpp:226
virtual L3Shape * getShape() const
Definition: L3File.h:321
@ string
L3Bin * getBinByIndex(int32_t index)
Definition: L3File.cpp:382
virtual int32_t getNumRows()
Definition: L3File.cpp:750
virtual bool setActiveProductList(const char *prodStr)
Definition: L3File.cpp:615
virtual void latlon2rowcol(double lat, double lon, int32_t &row, int32_t &col) const =0
virtual ~L3File()
Definition: L3File.cpp:427
L3Bin & operator+=(const L3Bin &bin)
Definition: L3File.cpp:137
virtual int32_t getNumProducts()
Definition: L3File.cpp:605
int32_t getNumProducts() const
Definition: L3File.cpp:178
virtual std::string getProductName(size_t index=0)
Definition: L3File.cpp:611
uint8_t quality
Definition: L3File.h:70
virtual void setQualityProcessing(bool val)
Definition: L3File.cpp:819
virtual L3Row * getRow(int32_t row)
Definition: L3File.cpp:719
virtual int32_t getNumActiveProducts()
Definition: L3File.cpp:652
virtual int64_t rowcol2record(int32_t row, int32_t col)
Definition: L3File.cpp:574
virtual void rowcol2latlon(int32_t row, int32_t col, double &lat, double &lon) const =0
L3Bin outBin
accumulation bin for lookups
Definition: L3File.h:154
float weights
Definition: L3File.h:72
uint8_t getQuality() const
Definition: L3File.cpp:234
virtual void clearCache()
Definition: L3File.cpp:435
int32_t getRow() const
Definition: L3File.cpp:312
L3Bin * getBinsInside(Geometry geo, bool areaWeighted=false)
Definition: L3File.h:268
virtual meta_l3bType * getMetaData()
Definition: L3File.cpp:499
const float missingPixelValue
Definition: L3File.h:19
virtual L3Row * readRow(int32_t row)
Definition: L3File.cpp:660
L3Bin(int32_t numProducts=1)
Definition: L3File.cpp:16
float getObsTime() const
Definition: L3File.cpp:218
int64_t getNumBins() const
Definition: L3File.cpp:320
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:150
int32_t getNobs() const
Definition: L3File.cpp:194
size_t numProds
Definition: L3File.h:157
void clear()
Definition: L3File.cpp:29
void setNumBins(int64_t numBins)
Definition: L3File.cpp:324
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:316
float getStdev(int32_t prodId=0) const
Definition: L3File.cpp:288
int64_t getLastBinNum()
Return the bin number for the last valid bin in the row.
Definition: L3File.cpp:404
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
int64_t getFirstBinNum()
Return the bin number for the first valid bin in the row.
Definition: L3File.cpp:392
virtual Hdf::hdf_bin * getHdfBinObject() const
Definition: L3File.cpp:808
Definition: L3File.cpp:10
L3Row(int32_t row, int64_t numBins, int32_t numProducts)
Definition: L3File.cpp:297
virtual int64_t getBaseBin(int32_t row) const
return the base bin number for a row
Definition: L3File.cpp:847
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:835
size_t * prodMap
index map used to map my product index into binObj's index
Definition: L3File.h:156
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:553
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:510
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:153
virtual int64_t bin2record(int64_t bin)
Definition: L3File.cpp:599
L3Shape * shape
Definition: L3File.h:147
int64_t lastBin
Definition: L3File.h:126