OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
mgiop.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <math.h>
3 #include "l12_proto.h"
4 #include "chl.h"
5 #include <stdio.h>
6 #include "giop.h"
7 
8 static float badval = BAD_FLT;
9 
10 void get_mgiop(l2str *l2rec, l2prodstr *p, float prod[]) {
11  static int firstCall = 1;
12  int prodID = p->cat_ix;
13  int ib = p->prod_ix;
14 
15  l1str *l1rec = l2rec->l1rec;
16  int32 nwave = l1rec->l1file->nbands;
17  int32 npix = l1rec->npix;
18 
19  int32_t ip, ipb, ibb;
20  int isdg, ieta, ipar;
21  int npar = 3;
22 
23  float def_sdg[] = {0.01, 0.015, 0.02};
24  float def_eta[] = {0.0, 0.33, 0.67, 1.0};
25 
26  static int32_t *nval;
27  static float *schl;
28  static float *sbbp;
29  static float *sadg;
30  static float *saph;
31  static float *snoc;
32  static float *sdia;
33  static float **spar;
34 
35  // pointers from giop.c
36  static float *chl;
37  static float *adg;
38  static float *bbp;
39  static float *aph;
40  static float **fit_par;
41 
42  if (firstCall) {
43 
44  firstCall = 0;
45 
46  // get number of eigenvalues (= # aph + 1 eta + 1 Sdg)
47  //npar = table_column_count(input->giop_aph_file)+1; // memory leak here?
48  if (strcmp(input->giop_aph_file, "aph_nas"))
49  npar = 4;
50 
51  if ((aph = calloc(npix * nwave, sizeof (float))) == NULL) {
52  printf("-E- %s line %d : error allocating memory for NAS.\n",
53  __FILE__, __LINE__);
54  exit(1);
55  }
56  if ((adg = calloc(npix * nwave, sizeof (float))) == NULL) {
57  printf("-E- %s line %d : error allocating memory for NAS.\n",
58  __FILE__, __LINE__);
59  exit(1);
60  }
61  if ((bbp = calloc(npix * nwave, sizeof (float))) == NULL) {
62  printf("-E- %s line %d : error allocating memory for NAS.\n",
63  __FILE__, __LINE__);
64  exit(1);
65  }
66  if ((chl = calloc(npix, sizeof (double))) == NULL) {
67  printf("-E- %s line %d : error allocating memory for NAS.\n",
68  __FILE__, __LINE__);
69  exit(1);
70  }
71  if ((fit_par = allocate2d_float(npix, npar)) == NULL) {
72  printf("-E- %s line %d : error allocating memory for NAS.\n",
73  __FILE__, __LINE__);
74  exit(1);
75  }
76 
77  if ((schl = calloc(npix, sizeof (double))) == NULL) {
78  printf("-E- %s line %d : error allocating memory for NAS.\n",
79  __FILE__, __LINE__);
80  exit(1);
81  }
82  if ((snoc = calloc(npix, sizeof (double))) == NULL) {
83  printf("-E- %s line %d : error allocating memory for NAS.\n",
84  __FILE__, __LINE__);
85  exit(1);
86  }
87  if ((sdia = calloc(npix, sizeof (double))) == NULL) {
88  printf("-E- %s line %d : error allocating memory for NAS.\n",
89  __FILE__, __LINE__);
90  exit(1);
91  }
92  if ((nval = calloc(npix, sizeof (double))) == NULL) {
93  printf("-E- %s line %d : error allocating memory for NAS.\n",
94  __FILE__, __LINE__);
95  exit(1);
96  }
97  if ((sbbp = calloc(npix * nwave, sizeof (float))) == NULL) {
98  printf("-E- %s line %d : error allocating memory for NAS.\n",
99  __FILE__, __LINE__);
100  exit(1);
101  }
102  if ((sadg = calloc(npix * nwave, sizeof (float))) == NULL) {
103  printf("-E- %s line %d : error allocating memory for NAS.\n",
104  __FILE__, __LINE__);
105  exit(1);
106  }
107  if ((saph = calloc(npix * nwave, sizeof (float))) == NULL) {
108  printf("-E- %s line %d : error allocating memory for NAS.\n",
109  __FILE__, __LINE__);
110  exit(1);
111  }
112  if ((spar = allocate2d_float(npix, npar)) == NULL) {
113  printf("-E- %s line %d : error allocating memory for NAS.\n",
114  __FILE__, __LINE__);
115  exit(1);
116  }
117 
118 
119  // set bbp spectral shape to user defined power-law
120  // set aph spectral shape to tabulated
121  //input->giop_bbp_opt=1;
122  //input->giop_aph_opt=0;
123 
124  // use NAS-specific tabulated aph
125  //parse_file_name("$OCDATAROOT/common/aph_nas_early.txt", tmp_file);
126  //strcpy(input->giop_aph_file, tmp_file);
127 
128  // further restrict rrsdiff_max and enable iteration
129  //input->giop_rrs_diff=0.1;
130  input->giop_iterate = 1;
131  }
132 
133  // initialize arrays and counters
134 
135  for (ip = 0; ip < npix; ip++) {
136 
137  nval[ip] = 0;
138  schl[ip] = 0.0;
139  sdia[ip] = 0.0;
140  snoc[ip] = 0.0;
141  chl[ip] = badval;
142 
143  ipb = ip*nwave;
144  for (ibb = 0; ibb < nwave; ibb++) {
145  sbbp[ipb + ibb] = 0.0;
146  saph[ipb + ibb] = 0.0;
147  sadg[ipb + ibb] = 0.0;
148  bbp[ipb + ibb] = badval;
149  aph[ipb + ibb] = badval;
150  adg[ipb + ibb] = badval;
151  }
152 
153  for (ipar = 0; ipar < npar; ipar++) {
154  fit_par[ip][ipar] = badval;
155  spar[ip][ipar] = 0;
156  }
157  }
158 
159 
160  // loop through three Sdg and four eta
161 
162  for (isdg = 0; isdg < 3; isdg++) for (ieta = 0; ieta < 4; ieta++) {
163 
164  input->giop_bbp_s = def_eta[ieta];
165  input->giop_adg_s = def_sdg[isdg];
166 
167  run_giop(l2rec);
168 
169  chl = giop_get_chl_pointer();
170  adg = giop_get_adg_pointer();
171  aph = giop_get_aph_pointer();
172  bbp = giop_get_bbp_pointer();
173  fit_par = giop_get_fitpar_pointer();
174 
175  for (ip = 0; ip < l1rec->npix; ip++) {
176 
177  ipb = ip * nwave + ib;
178 
179  if (chl[ip] > 0.005 && chl[ip] < 200.0) {
180 
181  nval[ip]++;
182 
183  schl[ip] += chl[ip];
184  sdia[ip] += fit_par[ip][0];
185  snoc[ip] += fit_par[ip][1];
186 
187  sadg[ipb] += adg[ipb];
188  saph[ipb] += aph[ipb];
189  sbbp[ipb] += bbp[ipb];
190 
191  for (ipar = 0; ipar < npar; ipar++) {
192  spar[ip][ipar] += fit_par[ip][ipar];
193  }
194 
195  }
196  }
197  }
198 
199 
200  for (ip = 0; ip < npix; ip++) {
201 
202  // flag and skip if pixel already masked
203 
204  if (l1rec->mask[ip]) {
205  l1rec->flags[ip] |= PRODFAIL;
206  continue;
207  }
208 
209  ipb = ip * nwave + ib;
210 
211  switch (prodID) {
212 
213  case CAT_aph_mgiop:
214  prod[ip] = (float) saph[ipb] / nval[ip];
215  break;
216 
217  case CAT_adg_mgiop:
218  prod[ip] = (float) sadg[ipb] / nval[ip];
219  break;
220 
221  case CAT_bbp_mgiop:
222  prod[ip] = (float) sbbp[ipb] / nval[ip];
223  break;
224 
225  case CAT_chl_mgiop:
226  prod[ip] = (float) schl[ip] / nval[ip];
227  break;
228 
229  case CAT_npix_mgiop:
230  prod[ip] = (int) nval[ip];
231  break;
232 
233  case CAT_crat_mgiop:
234  prod[ip] = (float) fabs(snoc[ip] / nval[ip]) / fabs(sdia[ip] / nval[ip]);
235  break;
236 
237  case CAT_fitpar_mgiop:
238  prod[ip] = (float) spar[ip][ib] / nval[ip];
239  break;
240 
241  default:
242  printf("-E- %s line %d : erroneous product ID %d passed to GIOP.\n",
243  __FILE__, __LINE__, prodID);
244  exit(1);
245  }
246  }
247 
248  return;
249 
250 }
251 
#define CAT_npix_mgiop
Definition: l2prod.h:269
#define NULL
Definition: decode_rs.h:63
float * giop_get_aph_pointer()
Definition: giop.c:2782
read l1rec
#define PRODFAIL
Definition: l2_flags.h:41
float * giop_get_adg_pointer()
Definition: giop.c:2768
float * giop_get_chl_pointer()
Definition: giop.c:2761
instr * input
#define CAT_aph_mgiop
Definition: l2prod.h:268
#define CAT_bbp_mgiop
Definition: l2prod.h:266
#define CAT_fitpar_mgiop
Definition: l2prod.h:271
void get_mgiop(l2str *l2rec, l2prodstr *p, float prod[])
Definition: mgiop.c:10
#define CAT_adg_mgiop
Definition: l2prod.h:267
float * giop_get_bbp_pointer()
Definition: giop.c:2775
#define CAT_crat_mgiop
Definition: l2prod.h:270
#define BAD_FLT
Definition: jplaeriallib.h:19
#define fabs(a)
Definition: misc.h:93
float ** giop_get_fitpar_pointer()
Definition: giop.c:2789
void run_giop(l2str *l2rec)
Definition: giop.c:1616
float ** allocate2d_float(size_t h, size_t w)
Allocate a two-dimensional array of type float of a given size.
Definition: allocate2d.c:125
int npix
Definition: get_cmp.c:27
float p[MODELMAX]
Definition: atrem_corl1.h:131
#define CAT_chl_mgiop
Definition: l2prod.h:265