OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
DtAlgorithm.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  *
3  * NAME: DtAlgorithm.cpp
4  *
5  * DESCRIPTION: Object class that provides data structures and processes that
6  * compute cloud masks for a given DtDDProcess object class.
7  *
8  * Created on: November 3, 2016
9  * Author: Sam Anderson, DT
10  *
11  * Modified:
12  *
13  *******************************************************************************/
14 
15 #include "darktarget/DtAlgorithm.h"
16 
17 #include <new> // nothrow
18 #include <algorithm> // std::sort
19 #include <iostream> // std::cout
20 #include <functional> // std::bind
21 #include <vector>
22 
23 using namespace std;
24 
25 const float DtAlgorithm::wind_[WIND_LUT_ENTRIES] = {
26  2.0, 6.0, 10.0, 14.0 };
27 
28 const float DtAlgorithm::pressure_[P_LEVELS] = {
29  1000.0, 975.0, 950.0, 925.0, 900.0, 850.0, 800.0,
30  750.0, 700.0, 650.0, 600.0, 550.0, 500.0, 450.0, 400.0,
31  350.0, 300.0, 250.0, 200.0, 150.0, 100.0, 70.0, 50.0,
32  30.0, 20.0, 10.0 };
33 
34 /**************************************************************************
35  * NAME: DtAlgorithm()
36  *
37  * DESCRIPTION: Class Constructor
38  *
39  *************************************************************************/
40 
42 {
43  cloud_fraction_= 0;
44  scatter_angle_= 0;
45  glint_angle_= 0;
46  glint_refl_= 0;
47  ndvi_= 0;
48  SZA_0_= 0;
49  SZA_1_= 0;
50  THE_0_= 0;
51  THE_1_= 0;
52  PHI_0_= 0;
53  PHI_1_= 0;
54  cmask_ = 1;
55 }
56 
57 /**************************************************************************
58  * NAME: ~DtAlgorithm()
59  *
60  * DESCRIPTION: Class Destructor
61  *
62  *************************************************************************/
63 
65 {
66 }
67 
68 /**************************************************************************
69  * NAME: initialize()
70  *
71  * DESCRIPTION: Virtual function initializes data and object classes for
72  * process operations.
73  *
74  *************************************************************************/
75 
76 int DtAlgorithm::initialize( map<string, ddata*> imap )
77 {
78  int status = DTDB_SUCCESS;
79 
80  lines_ = static_cast<ddval<int>*>(imap["num_lines"])->val;
81  pixels_ = static_cast<ddval<int>*>(imap["num_pixels"])->val;
82  month_ = static_cast<ddval<int>*>(imap["start_month"])->val;
83  bgascorrect_ = static_cast<ddval<bool>*>(imap["bgascorrect"])->val;
84  bglintmask_ = static_cast<ddval<bool>*>(imap["bglintmask"])->val;
85  bcloudmask_ = static_cast<ddval<bool>*>(imap["bcloudmask"])->val;
86  btest_ = false;
87 
88  DtLutNetcdf* lutgen = new DtLutNetcdf();
89  status = lutgen->read_gas_correction_lut( gc_lut_);
90  delete lutgen;
91 
92  return status;
93 }
94 
95 
96 /**************************************************************************
97  * NAME: compute()
98  *
99  * DESCRIPTION: Virtual function executes process algorithm.
100  *
101  *************************************************************************/
102 
103 map<string, ddata*> DtAlgorithm::process(vector<size_t> start, vector<size_t> count,
104  map<string, ddata*> imap)
105 {
106  map<string, ddata*> omap;
107  return omap;
108 }
109 
110 /**************************************************************************
111  * NAME: compute_gascorrection()
112  *
113  * DESCRIPTION: Compute gas correction
114  *
115  *************************************************************************/
116 
118 {
119  int status = DTDB_SUCCESS;
120 
121  float RTrans_H2O[NUM_DT_BANDS];
122  float RTrans_O3[NUM_DT_BANDS];
123  float RTrans_CO2[NUM_DT_BANDS];
124  for (int iWave = 0; iWave < NUM_DT_BANDS; iWave++) {
125  RTrans_H2O[iWave] = 1.0;
126  RTrans_O3[iWave] = 1.0;
127  RTrans_CO2[iWave] = 1.0;
128  }
129 // Calculate geometric factor for 2-way transmission
130  float degrad = acos(-1.0)/180.0;
131  float G_factor = -1.0;
132  float G_factor_H2O = -1.0;
133  float G_factor_O3 = -1.0;
134  float G_factor_CO2 = -1.0;
135  if ((senz_ > 0.0) && (solz_ > 0.0)) {
136  float c_VZ = cos(degrad*senz_);
137  float c_SZ = cos(degrad*solz_);
138 // Calculate spherical geometry g_factor
139  float hh = 9.0; // 9 km atmos scale height
140  float r = 6371.0/hh;
141  G_factor = sqrt(pow(r*c_VZ,2.0) + 2.0*r + 1.0) - r*c_VZ
142  + sqrt(pow(r*c_SZ,2.0) + 2.0*r + 1) - r*c_SZ;
143 // Calculate Kasten and Young g_factors ( 1. / cosz + a1*z**a2 * (a3-z)**a4 )
144  G_factor_H2O =
145  (1.0/(c_VZ + 0.0311*pow(senz_,0.1) * pow((92.471-senz_),-1.3814)))
146  + (1.0/(c_SZ + 0.0311*pow(solz_,0.1) * pow((92.471-solz_),-1.3814)));
147  G_factor_O3 =
148  (1.0/(c_VZ + 268.45*pow(senz_,0.5) * pow((115.42-senz_),-3.2922)))
149  + (1.0/(c_SZ + 268.45*pow(solz_,0.5) * pow((115.42-solz_),-3.2922)));
150  G_factor_CO2 =
151  (1.0/(c_VZ + 0.4567*pow(senz_,0.07) * pow((96.4836-senz_),-1.6970)))
152  + (1.0/(c_SZ + 0.4567*pow(solz_,0.07) * pow((96.4836-solz_),-1.6970)));
153  }
154  float total_H20 = 0.0;
155  if (pwv_ > 0.0){
156  total_H20 = pwv_/10.0;
157  }
158  else {
159  total_H20 = 0.0;
160  }
161 // If NCEP water is available compute Water transmission else use OptH20 from clim.
162  if((total_H20 > 0.0) && (G_factor > 0.0)) {
163  float logcon = log(total_H20*G_factor_H2O);
164  float logcon2 = logcon*logcon;
165  for (int iWave = 0; iWave < NUM_DT_BANDS; iWave++) {
166  float exponent = gc_lut_.H2O_COEF[iWave][0] +
167  gc_lut_.H2O_COEF[iWave][1]*logcon +
168  gc_lut_.H2O_COEF[iWave][2]*logcon2;
169  float exp1 = exp(exponent);
170  RTrans_H2O[iWave]= exp(exp1);
171  }
172  }
173  else {
174  for (int iWave = 0; iWave < NUM_DT_BANDS; iWave++) {
175  RTrans_H2O[iWave] = exp(gc_lut_.OPT_H2O_CLIM[iWave]*G_factor_H2O);
176  }
177  }
178 // If NCEP Ozone is available compute Ozone transmission else use OptOzone from clim.
179  float total_O3 = oz_/1000.0;
180  if((total_O3 > 0.0) && (G_factor > 0.0)) {
181  for (int iWave = 0; iWave < NUM_DT_BANDS; iWave++) {
182  float exponent = total_O3*G_factor_O3;
183  RTrans_O3[iWave] = exp(gc_lut_.O3_COEF[iWave][0] +
184  gc_lut_.O3_COEF[iWave][1]*exponent);
185  }
186  }
187  else {
188  for (int iWave = 0; iWave < NUM_DT_BANDS; iWave++) {
189  RTrans_O3[iWave] = exp(gc_lut_.OPT_O3_CLIM[iWave]*G_factor_O3);
190  }
191  }
192 // compute rest of gases from cli.
193  for ( int iWave = 0; iWave < NUM_DT_BANDS; iWave++) {
194  RTrans_CO2[iWave] = exp(gc_lut_.OPT_CO2_CLIM[iWave]*G_factor_CO2);
195  }
196 // compute total transmission
197  for ( int iWave=0; iWave<NUM_DT_BANDS; iWave++) {
198  gasc_[iWave] = RTrans_H2O[iWave]*RTrans_O3[iWave]*RTrans_CO2[iWave];
199  }
200 
201  return status;
202 }
203 
204 /**************************************************************************
205  * NAME: mean_std()
206  *
207  * DESCRIPTION: Subroutine returns the mean and standard deviation of
208  * input data array
209  *
210  *************************************************************************/
211 
212 int DtAlgorithm::mean_std( int size, float* data, float& mean, float& sdev )
213 {
214  int status = DTDB_SUCCESS;
215 
216  float s = 0.0;
217  for ( int j=0; j<size; j++ ) {
218  s += data[j];
219  }
220  mean = s/size;
221  float var = 0;
222  for ( int j=0; j<size; j++ ) {
223  s = data[j] - mean;
224  float p = s*s;
225  var = var+p;
226  }
227  if (var > 0) {
228  var = var/(size-1);
229  sdev = sqrt(var);
230  }
231  else {
232  sdev = 0;
233  }
234 
235  return status;
236 }
237 
238 /**************************************************************************
239  * NAME: mean_std_weighted()
240  *
241  * DESCRIPTION: Subroutine returns the weighted mean and standard deviation of
242  * input data array subject to a weighting mask
243  *
244  *************************************************************************/
245 
246 int DtAlgorithm::mean_std_weighted( int size, float* data, float& mean,
247  float& sdev, float* weight )
248 {
249  int status = DTDB_SUCCESS;
250 
251  float umean = 0.0;
252  float* weighted_data;
253  float weight_sum = 0;
254  weighted_data = new (nothrow) float[size];
255  if (weighted_data == NULL) {
256  cout << "DtAlgorithm::mean_std_weight: Memory could not be allocated";
257  status = DTDB_FAIL;
258  }
259  else
260  {
261  for (int i=0; i < size; i++) {
262  weighted_data[i] = data[i]*weight[i];
263  weight_sum += weight[i];
264  }
265 
266  mean_std( size, weighted_data, umean, sdev );
267 
268  if ( weight_sum > 0.0 ) {
269  mean = umean * ((float)size) / weight_sum;
270  }
271  }
272  delete[] weighted_data;
273 
274  return status;
275 }
276 
277 /**************************************************************************
278  * NAME: interp_extrap()
279  *
280  * DESCRIPTION: Perform linear interpolation or extrapolation.
281  *
282  *************************************************************************/
283 
284 int DtAlgorithm::interp_extrap( int num, float xin,
285  float x[], float y[], float& yout)
286 {
287  int status = DTDB_FAIL;
288 
289  if (xin <= x[0]) {
290  // reverse extrapolation
291  yout = y[0]+(xin-x[0])*(y[1]-y[0])/(x[1]-x[0]);
292  status = DTDB_SUCCESS;
293  } else if (xin >= x[num-1]) {
294  // forward extrapolation
295  yout = y[num-2]+(xin-x[num-2])*(y[num-1]-y[num-2])/(x[num-1]-x[num-2]);
296  status = DTDB_SUCCESS;
297  } else {
298  // interpolation
299  for (int i=0; i < num-1; i++) {
300  if ((xin >= x[i]) && (xin <= x[i+1])) {
301  yout=y[i]+(xin-x[i])*(y[i+1]-y[i])/(x[i+1]-x[i]);
302  status = DTDB_SUCCESS;
303  break;
304  }
305  }
306  }
307 
308  return status;
309 }
310 
311 /**************************************************************************
312  * NAME: sort_index()
313  *
314  * DESCRIPTION: This subroutine finds the sorted index of an array.
315  *
316  *************************************************************************/
317 
318 bool compare_pair (pair<float,int> x, pair<float,int> y)
319 {
320  return (x.first < y.first);
321 }
322 
323 int DtAlgorithm::sort_index( int numPts, float array[], int index[])
324 {
325  int status = DTDB_SUCCESS;
326 
327  typedef pair<float,int> p;
328  vector< pair<float,int> > index_pair;
329 
330  for (int i = 0; i < numPts; i++) {
331  index_pair.push_back(make_pair(array[i], i));
332  }
333 
334  stable_sort(index_pair.begin(), index_pair.end(), compare_pair);
335 
336  for (int i = 0; i < numPts; i++) {
337  index[i] = index_pair[i].second;
338  }
339 
340  return status;
341 }
342 
343 /**************************************************************************
344  * NAME: sort_inplace()
345  *
346  * DESCRIPTION: This subroutine performs sort in place.
347  *
348  *************************************************************************/
349 
350 int DtAlgorithm::sort_inplace(int numPts, float array1[], float array2[])
351 {
352  int status = DTDB_SUCCESS;
353 
354  typedef pair<float,float> p;
355  vector< pair<float,float> > index_pair;
356 
357  for (int i = 0; i < numPts; i++) {
358  index_pair.push_back(make_pair(array1[i], array2[i]));
359  }
360 
361  stable_sort(index_pair.begin(), index_pair.end(), compare_pair);
362 
363  for (int i = 0; i < numPts; i++) {
364  array1[i] = index_pair[i].first;
365  array2[i] = index_pair[i].second;
366  }
367 
368  return status;
369 }
370 
371 /**************************************************************************
372  * NAME: fit_line()
373  *
374  * DESCRIPTION: Linear fit data in x and y arrays to produce straight line
375  * coefficients Y = A + BX.
376  *
377  *************************************************************************/
378 
379 int DtAlgorithm::fit_line( float x[], float y[], float sig[], int ndata,
380  float& A, float& B )
381 {
382  int status = DTDB_SUCCESS;
383 
384  float sx = 0.0;
385  float sy = 0.0;
386  float ST2 = 0.0;
387  float WT = 0.0;
388  float MWT = 0.0;
389  float ss = 0.0;
390 
391  if (MWT != 0) {
392  for (int i=0; i<ndata; i++) {
393  WT = 1.0/pow(sig[i],2.0);
394  ss += WT;
395  sx += x[i]*WT;
396  sy += y[i]*WT;
397  }
398  }
399  else {
400  for (int i=0; i<ndata; i++) {
401  sx += x[i];
402  sy += y[i];
403  }
404  ss = (float) ndata;
405  }
406  float sxOSS=sx/ss;
407  if (MWT != 0) {
408  for (int i=0; i<ndata; i++) {
409  float T = (x[i]-sxOSS)/sig[i];
410  ST2 += T*T;
411  B += T*y[i]/sig[i];
412  }
413  }
414  else {
415  for (int i=0; i<ndata; i++) {
416  float T = x[i]-sxOSS;
417  ST2 += T*T;
418  B += T*y[i];
419  }
420  }
421  B = B/ST2;
422  A = (sy-sx*B)/ss;
423 // float SIGA = sqrt((1.0 + sx*sx/(ss*ST2))/ss);
424 // float SIGB = sqrt(1.0/ST2);
425 
426  return status;
427 }
428 
429 /**************************************************************************
430  * NAME: set_byte()
431  *
432  * DESCRIPTION: Set bits in a quality control flag.
433  **************************************************************************/
434 
435 int DtAlgorithm::set_byte(short val, short bitPos, short& target)
436 {
437  int status = DTDB_SUCCESS;
438 
439  short temp = 0;
440  temp = val << bitPos;
441  target |= temp;
442 
443  return status;
444 }
445 
446 /**************************************************************************
447  * NAME: compute_glint_angle()
448  *
449  * DESCRIPTION: Compute the glint angle
450  *
451  *************************************************************************/
452 
454 {
455  int status = DTDB_SUCCESS;
456 
457  glint_angle_ = 0.0;
458  if((solz_> 0.0) && (senz_> 0.0) && (raa_> 0.0)) {
459  glint_angle_ = cos(solz_*DEGtoRAD)*cos(senz_*DEGtoRAD)
460  + sin(solz_*DEGtoRAD)*sin(senz_*DEGtoRAD)*cos(raa_*DEGtoRAD);
461  glint_angle_ = acos(glint_angle_)*RADtoDEG;
462  }
463 
464  double cc = DEGtoRAD;
465  float nr = 1.341;
466 
467  double zx = (sin(senz_*cc)*sin(raa_*cc))/
468  (cos(solz_*cc)+cos(senz_*cc));
469  double zy = (sin(solz_*cc)-sin(senz_*cc)*cos(raa_*cc))/
470  (cos(solz_*cc)+cos(senz_*cc));
471  double sigx = sqrt(0.003+0.00192*ws_);
472  double sigy = sqrt(0.00316*ws_);
473  double zeta = zx / sigx;
474  double eta = zy / sigy;
475  double p = (1.0/(2.0*M_PI*sigx*sigy))*exp(-0.5*(zeta*zeta + eta*eta));
476  double costwoomega = cos(senz_*cc)*cos(solz_*cc) -
477  sin(senz_*cc)*sin(solz_*cc)*cos(raa_*cc);
478  double cosbeta = (cos(solz_*cc)+cos(senz_*cc))/
479  (sqrt(2.0 + 2.0*costwoomega));
480  double w = 0.5 * acos(costwoomega);
481  double wp = asin(sin(w)/nr);
482  double a1 = sin(w - wp);
483  double b1 = sin(w+wp);
484  double c1 = tan(w-wp);
485  double d1 = tan(w+wp);
486  double R = 0.5*((a1*a1)/(b1*b1)+(c1*c1)/(d1*d1));
487  glint_refl_ = p*R/(4*cos(senz_*cc)*pow(cosbeta,4.0));
488 
489  return status;
490 }
491 
492 /**************************************************************************
493  * NAME: compute_scatter_angle()
494  *
495  * DESCRIPTION: Compute land scatter angle.
496  *
497  *************************************************************************/
498 
500 {
501  int status = DTDB_SUCCESS;
502 
503  scatter_angle = -cos(solz_*DEGtoRAD)*cos(senz_*DEGtoRAD)
504  +sin(solz_*DEGtoRAD)*sin(senz_*DEGtoRAD)*cos(raa_*DEGtoRAD);
505  scatter_angle = acos(scatter_angle)*RADtoDEG;
506 
507  return status;
508 }
509 
510 
511 
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
int r
Definition: decode_rs.h:73
int read_gas_correction_lut(dtGasCorrectionLUT &gc_lut)
int j
Definition: decode_rs.h:73
int status
Definition: l1_czcs_hdf.c:32
int sort_index(int numPts, float array[], int index[])
int sort_inplace(int numPts, float array1[], float array2[])
float mean(float *xs, int sample_size)
Definition: numerical.c:81
int compute_scatter_angle(float &scat_angle)
#define NULL
Definition: decode_rs.h:63
int compute_glint_angle()
constexpr int NUM_DT_BANDS
Definition: DtLutNetcdf.h:70
#define M_PI
Definition: pml_iop.h:15
static const float pressure_[P_LEVELS]
Definition: DtAlgorithm.h:82
int set_byte(short val, short bitPos, short &target)
int compute_gas_correction()
int mean_std_weighted(int n, float *data, float &mean, float &sdev, float *weight)
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude data
Definition: HISTORY.txt:356
instead the metadata field ProcessingEnvinronment is filled in from the output of a call to the POSIX compliant function uname from within the L1B code A small bug in L1B_Tables an incorrect comparison of RVS coefficients for TEBs to RVS coefficients for RSBs was being made This was replaced with a comparison between TEB coefficients This error never resulted in an incorrect RVS correction but did lead to recalculating the coefficients for each detector in a thermal band even if the coefficients were the same for all detectors To reduce to overall size of the reflective LUT HDF fill values were eliminated from all LUTs previously dimensioned where and where NUM_TIMES is the number of time dependent table pieces In Preprocess a small error where the trailing dropped scan counter was incremented when the leading dropped scan counter should have been was fixed This counter is internal only and is not yet used for any chiefly to casting of were added to make it LINUX compatible Output of code run on LINUX machines displays differences of at most scaled sector incalculable values of the Emissive calibration factor b1
Definition: HISTORY.txt:576
int interp_extrap(int num, float xin, float x[], float y[], float &yout)
static const float wind_[WIND_LUT_ENTRIES]
Definition: DtAlgorithm.h:81
virtual ~DtAlgorithm()
Definition: DtAlgorithm.cpp:64
virtual map< string, ddata * > process(vector< size_t > start, vector< size_t > count, map< string, ddata * > imap)
data_t s[NROOTS]
Definition: decode_rs.h:75
#define R
Definition: make_L3_v1.1.c:96
real, dimension(:,:), allocatable scatter_angle
int i
Definition: decode_rs.h:71
virtual int initialize(map< string, ddata * > imap)
Definition: DtAlgorithm.cpp:76
msiBandIdx val
Definition: l1c_msi.cpp:34
int fit_line(float x[], float y[], float sig[], int ndata, float &A, float &B)
int mean_std(int n, float *data, float &mean, float &sdev)
bool compare_pair(pair< float, int > x, pair< float, int > y)
float p[MODELMAX]
Definition: atrem_corl1.h:131
int count
Definition: decode_rs.h:79