OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
DtAlgorithm.h
Go to the documentation of this file.
1 /*
2  * DtAlgorithm.h
3  *
4  * Created on: Nov 3, 2016
5  * Author: ssander2
6  */
7 
8 #ifndef INCLUDE_DTALGORITHM_H_
9 #define INCLUDE_DTALGORITHM_H_
10 
11 #include <DDAlgorithm.h>
12 #include "DtLutNetcdf.h"
13 
14 using namespace std;
15 
17  D488, // m03
18  D550, // m04
19  D670, // m05
20  D865, // m07
21  D1240, // m08
22  D1610, // m10
23  D2250, // m11
24  D412, // m01
25  D1380, // m09
26  D11000, // m15
28 };
29 
31  DL488, // m03
32  DL550, // m04
33  DL670, // m05
34  DL2250, // m11
36 };
37 
39  CONTN=1,
45 };
46 
47 
48 class DtAlgorithm : public DDAlgorithm
49 {
50  friend class DtCloudMaskLand;
51  friend class DtCloudMaskOcean;
52  friend class DtSedimentMask;
53 public:
54 
55 // static definitions
56  static constexpr int LINE = 1;
57  static constexpr int GRIDX = LINE;
58  static constexpr int GRIDY = LINE;
59  static constexpr int NUM_CASES_SMALL = 4;
60  static constexpr int NUM_CASES_BIG = 5;
61  static constexpr int NUM_LUTS = 4;
62  static constexpr int NUM_LATS = 180;
63  static constexpr int NUM_LONS = 360;
64 
65 // angle limits
66  static constexpr float MINMTHET0=0.0;
67  static constexpr float MAXMTHET0=80.0;
68  static constexpr float MINMTHET=0.0;
69  static constexpr float MAXMTHET=72.0;
70  static constexpr float MINMPHI=0.0;
71  static constexpr float MAXMPHI=180.0;
72 
73  static constexpr float MAXTAU=5.0;
74  static constexpr float MINTAU=-0.01;
75 
76  static constexpr int NUM_RFL_BANDS = 10;
77  static constexpr int DT_OWAVES = 7;
78  static constexpr int DT_LWAVES = 3;
79  static constexpr int MODIS_WAVES = 7;
80  static constexpr int VIIRS_WAVES = 3;
81  static const float wind_[WIND_LUT_ENTRIES];
82  static const float pressure_[P_LEVELS];
83 
84  enum SIZES {
87  };
88 
89  enum STATS {
91  IAVG
92  };
93 
95  unsigned char cmask_;
96 
97  float rfld_[NUM_RFL_BANDS];
98 
102  DtAlgorithm();
103 
107  virtual ~DtAlgorithm ();
108 
112  virtual int initialize( map<string, ddata*> imap );
113 
117  virtual vector<string> get_products() { return {};};
118 
122  virtual map<string, ddata*> process(vector<size_t> start, vector<size_t> count,
123  map<string, ddata*> imap);
124 
129  int compute_gas_correction();
130 
135  int fit_line( float x[], float y[], float sig[], int ndata,
136  float& A, float& B );
143  float glint_refl_;
144  float ndvi_;
145 
146 protected:
147 
148  // LUT angle-based indices
149  int SZA_0_;
150  int SZA_1_;
151  int THE_0_;
152  int THE_1_;
153  int PHI_0_;
154  int PHI_1_;
155 
159  int interp_extrap( int num, float xin,
160  float x[], float y[], float& yout );
164  int mean_std( int n, float* data, float& mean, float& sdev );
165 
170  int mean_std_weighted( int n, float* data, float& mean,
171  float& sdev, float* weight );
172 
176  int sort_index( int numPts, float array[], int index[] );
177 
182  int sort_inplace( int numPts, float array1[], float array2[] );
183 
187  int set_byte( short val, short bitPos, short& target);
188 
192  int compute_glint_angle();
193 
197  int compute_scatter_angle(float& scat_angle);
198 
199 };
200 
201 class DtCloudMaskOcean;
202 
203 class DtAlgOcean : public DtAlgorithm
204 {
205 public:
206 
207 // algorithm thresholds
208  static constexpr float THRHLD550 = 0.0025;
209  static constexpr float THRHLD470 = 0.4;
210  static constexpr float THRHLD138_1 = 0.03;
211  static constexpr float THRHLD138_2 = 0.005;
212  static constexpr float THRHLD_DUST = 0.75;
213  static constexpr float THRHLD_CIRRUS = 0.03;
214  static constexpr float Threshold_LSQ_Error = 3.7;
215 
216  static constexpr float GLINT_ANGLE_THRESHOLD = 40.0;
217 // static constexpr float GLINT_ANGLE_THRESHOLD = 30.0;
218  static constexpr float LOW_THRESHOLD = -0.05;
219  static constexpr float HIGH_THRESHOLD = 5.0;
220  static constexpr float GLINT_REFL_THRESHOLD = 0.01570796326; //0.005*PI;
221 
222  static constexpr int NUMCASES = 4;
223  static constexpr int NUMCASEB = 5;
224  static constexpr int NWAV = 7;
225  static constexpr int NAOT = 6;
226  static constexpr int NTH0 = 11;
227  static constexpr int NTHET = 16;
228  static constexpr int NPHI = 16;
229  static constexpr int NUM_ARRAY_ELEMENTS = 77;
230  static constexpr int NUM_SIZES = 2;
231  static constexpr int NUM_STATS = 2;
232 
233 // data from LUTs
236 
237 // global working data
238 // float scatter_angle_[NUMCASES*NUMCASEB];
239 
240 // interpolate_rayleigh values based on reported angles
241  float refl_rayl_[NWAV];
242 
243 // interpolate_angle
244  float refl_big_[NWAV][NUM_CASES_BIG][NAOT];
245  float refl_small_[NWAV][NUM_CASES_SMALL][NAOT];
246 
247 // interpolate_sza_albedo, interpolate_tau_albedo
248  float albedo_R_small_tau_[NWAV][NUM_CASES_SMALL][NAOT];
249  float albedo_R_big_tau_[NWAV][NUM_CASES_BIG][NAOT];
250  float albedo_T_small_tau_[NWAV][NUM_CASES_SMALL][NAOT];
251  float albedo_T_big_tau_[NWAV][NUM_CASES_BIG][NAOT];
252 
253 // compute_average
254  float refl_[NWAV];
255  float sdev_[NWAV];
256  int numData_[NWAV];
257  int good_pixels_[NWAV];
258 
259 // results by case
260  float tau_[NWAV][NUMCASES*NUMCASEB];
261  float tau_small_[NWAV][NUMCASES*NUMCASEB];
262  float tau_big_[NWAV][NUMCASES*NUMCASEB];
263  float backscatter_[NWAV][NUMCASES*NUMCASEB];
264  float assym_[NWAV][NUMCASES*NUMCASEB];
265  float refl_flux_[NWAV][NUMCASES*NUMCASEB];
266  float trans_flux_[NWAV][NUMCASES*NUMCASEB];
267  float angstrom_exp_[NUM_STATS][NUMCASES*NUMCASEB];
268  float solution_index_[NUM_SIZES][NUMCASES*NUMCASEB];
269  float ccn_[NUMCASES*NUMCASEB];
270  float eff_radius_[NUMCASES*NUMCASEB];
271  float eff_variance_[NUMCASES*NUMCASEB];
272  float mass_con_ocean_[NUMCASES*NUMCASEB];
273  float xmin_[2][NUMCASES*NUMCASEB];
274  float funmin_[2][NUMCASES*NUMCASEB];
275  float tau_X55_[2][NUMCASES*NUMCASEB];
276  float error_[2][NWAV][NUMCASES*NUMCASEB];
277 
278 // results best and averaged
279  float refl_avg_[NWAV];
280  float sdev_avg_[NWAV];
281  float tau_avg_[NUM_STATS][NWAV];
282  float tau_small_avg_[NUM_STATS][NWAV];
283  float tau_big_avg_[NUM_STATS][NWAV];
284  float backscatter_avg_[NUM_STATS][NWAV];
285  float assym_avg_[NUM_STATS][NWAV];
286  float refl_flux_avg_[NUM_STATS][NWAV];
287  float trans_flux_avg_[NUM_STATS][NWAV];
288  float angstrom_exp_avg_[NUM_STATS][NUM_SIZES];
289  float solution_index_avg_[NUM_STATS][NUM_SIZES];
290  float ccn_avg_[NUM_STATS];
291  float eff_radius_avg_[NUM_STATS];
292  float eff_variance_avg_[NUM_STATS];
293  float mass_con_ocean_avg_[NUM_STATS];
294  float xmin_avg_[NUM_STATS];
295  float funmin_avg_[NUM_STATS];
296  float tau_X55_avg_[NUM_STATS];
297 
298 // quality control
299  short qcontrol_;
305  short quality_to_pass_[2];
306  short quality_flag_[12];
307 
308 // Intermediate output data per cell
309  float sds_refl_[NWAV];
310  float sds_refl_sdev_[NWAV];
311  short sds_numPixels_[NWAV];
312  float sds_tau_best_[NWAV];
313  float sds_tau_avg_[NWAV];
314  float sds_tau_big_best_[NWAV];
315  float sds_tau_big_avg_[NWAV];
316  float sds_tau_small_best_[NWAV];
317  float sds_tau_small_avg_[NWAV];
318  float sds_assy_best_[NWAV];
319  float sds_assy_avg_[NWAV];
320  float sds_back_best_[NWAV];
321  float sds_back_avg_[NWAV];
322  float sds_reff_best_[NWAV];
323  float sds_reff_avg_[NWAV];
324  float sds_tranf_best_[NWAV];
325  float sds_tranf_avg_[NWAV];
328  float sds_Small_Weighting_[NUM_SIZES];
329  float sds_Least_Error_[NUM_SIZES];
330  float sds_tau_X55_[NUM_SIZES];
331  float sds_EffRad_[NUM_SIZES];
332  float sds_EffVar_[NUM_SIZES];
333  short sds_Sol_Index_Small_[NUM_SIZES];
334  short sds_Sol_Index_Large_[NUM_SIZES];
335  float sds_Angs_Coeff1_[NUM_SIZES];
336  float sds_Angs_Coeff2_[NUM_SIZES];
337  float sds_Mass_Conc_[NUM_SIZES];
338  float sds_CCN_[NUM_SIZES];
339  float sds_AOT_model_[NUMCASES+NUMCASEB];
341 
345  DtAlgOcean();
346 
350  ~DtAlgOcean ();
351 
355  int initialize( map<string, ddata*> imap );
356 
360  vector<string> get_products() { return {"cloud_mask", "quality_flag", "aerosol_type",
361  "l2_flags", "scattering_angle", "fmf_550", "angstrom", "aot_380",
362  "aot_490", "aot_550", "aot_670","aot_865", "aot_1240", "aot_1610", "aot_2250"};};
363 
367  map<string, ddata*> process(vector<size_t> start, vector<size_t> count,
368  map<string, ddata*> imap);
369 
370 protected:
371 
375  int index_geometry( float sza, float azim, float phi);
376 
381  int interpolate_rayleigh();
382 
387  int interpolate_angles();
388 
394  int compute_tau_flux( int iBig, int iSmall, int iSol );
395 
403  int compute_avg_refl();
404 
409  int compute_average_to_500m();
410 
414  int store_reflectance();
415 
419  int store_output();
420 
424  int assign_quality();
425 
429  int set_fill_out();
430 
434  int average_output();
435 
439  int compute_minimum(int iBig, int iSmall, int iSol);
440 
444  int compute_minimum_baseline(int iBig, int iSmall, int iSol);
445 
449  float fun_tau( float xmin, int iBig, int iSmall, int iSol );
450 };
451 
452 class DtCloudMaskLand;
453 
454 class DtAlgLand : public DtAlgorithm
455 {
456 public:
457 
458 // algorithm constants
459  static constexpr int NLTHET0 = 11;
460  static constexpr int NLTHE = 15;
461  static constexpr int NLPHI = 16;
462  static constexpr int NLWAV = 4;
463  static constexpr int NLTAU = 7;
464  static constexpr int NLTABLE = 5;
465  static constexpr int NLETA = 13;
466  static constexpr int NLSIZE = 2;
467  static constexpr int DTABLE = 5;
468 
469  static constexpr float THR213MIN_1 = 0.01;
470  static constexpr float THR213MAX_1 = 0.25;
471  static constexpr float THR213MIN_2 = 0.25;
472  static constexpr float THR213MAX_2 = 0.25;
473  static constexpr float PRESSURE_P0 = 1013.0;
474  static constexpr float DLAT = 0.5;
475  static constexpr float DLON = 0.5;
476 
477  static constexpr int QA_LAND = 5;
478 
479 // data from LUTs
482 
483  int season_;
485  int mtable_[NLSIZE];
486 
487 // interpolate_
488  float refl_ray_nl_[NLWAV];
489  float opth_nl_[NLSIZE][NLWAV][NLTAU];
490  float int_nl_[NLSIZE][NLWAV][NLTAU];
491  float fd_nl_[NLSIZE][NLWAV][NLTAU];
492  float t_nl_[NLSIZE][NLWAV][NLTAU];
493  float fdt_nl_[NLSIZE][NLWAV][NLTAU];
494  float sbar_nl_[NLSIZE][NLWAV][NLTAU];
495 // average
496  float refl_inter_[NLWAV][GRIDX*GRIDY];
497  float refl_[NLWAV]; // mean reflectance
498  float sdev_[NLWAV]; // reflectance standard deviation
499  short error_;
500 // retrieve_first, retrieve_second
501  float rho_star_[NLSIZE][NLWAV][NLTAU];
502  float rho_star_tot_[NLWAV][NLTAU];
503  float rho_S212_[NLSIZE][NLTAU];
504  float rho_S212_tot_[NLTAU];
505  float errwave_[NLWAV];
506  float rho_S466_;
507  float yint_466_;
508  float slope_466_;
509  float rho_S644_;
510  float yint_644_;
511  float slope_644_;
512  short eta_flag_;
513 // results
514  float aot_d_[NLWAV]; // tau corrected
515  float aot_f_[NLWAV]; // tau fine (small)
516  float aot_c_[NLWAV]; // tau coarse (big)
517  float rho_sfc_[NLWAV]; // surface reflectance
518  float eta_; // dust weighting
519  float err644_; // fitting error
520  float masscon_; // mass concentration
521  float angstrom_; // angs coefficient
522  float ndvi_; // ndvi
523  short iaer_; // aerosol type
524  int good_pixels_[NLWAV]; // good pixels
525  int num_pixels_used_; // = good_pixels_[W659]
526 
527 // program and quality control
528  short iproc_;
529  short ifinish_;
530  float thresh_min_;
531  float thresh_max_;
534  short fail_ret_;
535  short quality_flag_for_joint_[2];
538 
539 // Intermediate output data per cell
540  short sds_qcontrol_[QA_LAND];
548  float sds_ndvi_;
549  float sds_numpixels_[NLWAV];
550  float sds_tau_corrected_[NLWAV];
551  float sds_refl_[NLWAV];
552  float sds_refl_std_[NLWAV];
553  float sds_tau_small_[NLWAV];
554  float sds_tau_big_[NLWAV];
555  float sds_surface_reflectance_[NLWAV];
556 
560  DtAlgLand();
561 
565  ~DtAlgLand ();
566 
570  int initialize( map<string, ddata*> imap );
571 
575  vector<string> get_products() { return {"cloud_mask", "quality_flag", "aerosol_type",
576  "l2_flags", "scattering_angle", "fmf_550", "angstrom", "aot_380",
577  "aot_490", "aot_550", "aot_670", "aot_2250"};};
578 
595  map<string, ddata*> process(vector<size_t> start, vector<size_t> count,
596  map<string, ddata*> imap);
597 
598 protected:
599 
603  int index_geometry( float sza, float azim, float phi);
604 
609 // int select_lut( int iSize, int iTable );
610 
615  int interpolate_rayleigh();
616 
621  int interpolate_angle();
622 
628  int interpolate_elevation();
629 
637  int compute_average(size_t iy, size_t ix);
638 
646  int compute_cloudmask_ndvi(size_t iy, size_t ix,
647  int& iCldRed, int& iCldBlue);
648 
652  int simulate_toa();
653 
659  int retrieve_first();
660 
667  int retrieve_second();
668 
672  int assign_quality();
673 
677  int store_output();
678 
682  int fill_values();
683 
684 };
685 
686 #endif
float err644_
Definition: DtAlgorithm.h:519
@ MAXDLBAND
Definition: DtAlgorithm.h:35
float thresh_max_
Definition: DtAlgorithm.h:531
@ D412
Definition: DtAlgorithm.h:24
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
short return_quality_cirrus_
Definition: DtAlgorithm.h:532
DtCloudMaskLand * cm_
Definition: DtAlgorithm.h:481
@ D865
Definition: DtAlgorithm.h:20
short qcontrol_cirrus_
Definition: DtAlgorithm.h:302
short error_
Definition: DtAlgorithm.h:499
float angstrom_
Definition: DtAlgorithm.h:521
float slope_644_
Definition: DtAlgorithm.h:511
float sds_scat_angle_
Definition: DtAlgorithm.h:542
@ SMOKE
Definition: DtAlgorithm.h:41
short iproc_
Definition: DtAlgorithm.h:528
short qcontrol_exclude_
Definition: DtAlgorithm.h:300
float mean(float *xs, int sample_size)
Definition: numerical.c:81
@ D2250
Definition: DtAlgorithm.h:23
DTLBANDS_ENUM
Definition: DtAlgorithm.h:30
int IBIG
Definition: dtland.py:33
float sds_Tau_Land_Ocean_img_
Definition: DtAlgorithm.h:327
float thresh_min_
Definition: DtAlgorithm.h:530
float sds_dust_weighting_
Definition: DtAlgorithm.h:547
void initialize(int pixref_flag, int blkref_flag)
Definition: Usds.c:1371
float sds_Tau_Land_Ocean_
Definition: DtAlgorithm.h:326
@ D11000
Definition: DtAlgorithm.h:26
float PRESSURE_P0
Definition: dtland.py:45
unsigned char cmask_
Definition: DtAlgorithm.h:95
vector< string > get_products()
Definition: DtAlgorithm.h:360
int num_pixels_used_
Definition: DtAlgorithm.h:525
float ndvi_
Definition: DtAlgorithm.h:522
@ D550
Definition: DtAlgorithm.h:18
@ MAXDBAND
Definition: DtAlgorithm.h:27
short quality_dust_flag_off_glint_
Definition: DtAlgorithm.h:304
@ DL670
Definition: DtAlgorithm.h:33
@ D670
Definition: DtAlgorithm.h:19
float glint_refl_
Definition: DtAlgorithm.h:143
@ BACKG
Definition: DtAlgorithm.h:40
float masscon_
Definition: DtAlgorithm.h:520
float slope_466_
Definition: DtAlgorithm.h:508
float glint_angle_
Definition: DtAlgorithm.h:142
short qcontrol_special_
Definition: DtAlgorithm.h:537
@ DL2250
Definition: DtAlgorithm.h:34
@ D488
Definition: DtAlgorithm.h:17
float eta_
Definition: DtAlgorithm.h:518
short iaer_
Definition: DtAlgorithm.h:523
short eta_flag_
Definition: DtAlgorithm.h:512
virtual vector< string > get_products()
Definition: DtAlgorithm.h:117
float sds_cloud_fraction_
Definition: DtAlgorithm.h:545
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude data
Definition: HISTORY.txt:356
short ifinish_
Definition: DtAlgorithm.h:529
DtCloudMaskOcean * cm_
Definition: DtAlgorithm.h:235
float yint_644_
Definition: DtAlgorithm.h:510
short success_ret_
Definition: DtAlgorithm.h:533
short fail_ret_
Definition: DtAlgorithm.h:534
short sds_aerosol_type_
Definition: DtAlgorithm.h:541
float sds_angs_coeff_
Definition: DtAlgorithm.h:546
short sds_land_ocean_quality_
Definition: DtAlgorithm.h:340
float rho_S466_
Definition: DtAlgorithm.h:506
short quality_flag_for_retr_
Definition: DtAlgorithm.h:536
DTBANDS_ENUM
Definition: DtAlgorithm.h:16
short qcontrol_special_
Definition: DtAlgorithm.h:301
short quality_dust_flag_glint_
Definition: DtAlgorithm.h:303
dtGasCorrectionLUT gc_lut_
Definition: DtAlgorithm.h:94
dtOceanAerosolLUT lut_
Definition: DtAlgorithm.h:234
@ D1610
Definition: DtAlgorithm.h:22
@ CONTN
Definition: DtAlgorithm.h:39
dtLandAerosolLUT lut_
Definition: DtAlgorithm.h:480
float sds_mass_conc_
Definition: DtAlgorithm.h:544
float yint_466_
Definition: DtAlgorithm.h:507
AEROSOL_TYPE
Definition: DtAlgorithm.h:38
float cloud_fraction_
Definition: DtAlgorithm.h:140
short qcontrol_
Definition: DtAlgorithm.h:299
float scatter_angle_
Definition: DtAlgorithm.h:141
@ URBAN
Definition: DtAlgorithm.h:42
@ MAX_AERO
Definition: DtAlgorithm.h:44
@ DL488
Definition: DtAlgorithm.h:31
float rho_S644_
Definition: DtAlgorithm.h:509
float scatter_angle_
Definition: DtAlgorithm.h:484
@ D1240
Definition: DtAlgorithm.h:21
msiBandIdx val
Definition: l1c_msi.cpp:34
float sds_fitting_error_
Definition: DtAlgorithm.h:543
@ COARSE
Definition: DtAlgorithm.h:43
float sds_ndvi_
Definition: DtAlgorithm.h:548
vector< string > get_products()
Definition: DtAlgorithm.h:575
@ D1380
Definition: DtAlgorithm.h:25
@ DL550
Definition: DtAlgorithm.h:32
int count
Definition: decode_rs.h:79