16 static int QaaRecNum = -1;
26 static unsigned char *
flags;
41 static float *bbp_qaa;
42 static float *adg_qaa;
43 static float *aph_qaa;
45 static int ib410, ib440, ib490, ib555, ib670;
46 static int do_decomp = 1;
50 static int qaa_ran(
int recnum) {
61 static void qaa_alloc(int32_t
npix, int32_t
nbands) {
62 static int32_t currentNumPix = -1;
64 if (
npix > currentNumPix) {
65 if (currentNumPix != -1) {
73 atot = (
float*) calloc(
npix*
nbands,
sizeof (
float));
74 aph = (
float*) calloc(
npix*
nbands,
sizeof (
float));
75 adg = (
float*) calloc(
npix*
nbands,
sizeof (
float));
76 bb = (
float*) calloc(
npix*
nbands,
sizeof (
float));
77 flags = (
unsigned char*) calloc(
npix,
sizeof (
unsigned char));
83 static void qaa_pixel_alloc(
int nbands) {
85 fwave = (
float*) calloc(
nbands,
sizeof (
float));
86 bbw = (
float*) calloc(
nbands,
sizeof (
float));
87 aw = (
float*) calloc(
nbands,
sizeof (
float));
88 Rrs = (
float*) calloc(
nbands,
sizeof (
float));
89 rrs = (
float*) calloc(
nbands,
sizeof (
float));
90 u = (
float*) calloc(
nbands,
sizeof (
float));
91 a_qaa = (
float*) calloc(
nbands,
sizeof (
float));
92 bb_qaa = (
float*) calloc(
nbands,
sizeof (
float));
93 bbp_qaa = (
float*) calloc(
nbands,
sizeof (
float));
94 adg_qaa = (
float*) calloc(
nbands,
sizeof (
float));
95 aph_qaa = (
float*) calloc(
nbands,
sizeof (
float));
101 static void run_qaa(l2str *l2rec) {
102 static int firstCall = 1;
104 l1str *
l1rec = l2rec->l1rec;
108 unsigned char flags_qaa;
119 printf(
"QAA v6 processing for %d bands\n", nbandsVis);
121 qaa_pixel_alloc(nbandsVis);
123 for (ib = 0; ib < nbandsVis; ib++)
124 fwave[ib] =
l1rec->l1file->fwave[ib];
128 if (w[1] < 0 || w[2] < 0 || w[3] < 0 || w[4] < 0) {
129 printf(
"qaa: algorithm coefficients not provided for this sensor.\n");
139 if (ib440 < 0 || ib490 < 0 || ib555 < 0 || ib670 < 0) {
140 printf(
"get_qaa: missing minimum required wavelengths "
141 "(need 440,490,555,670).\n");
142 printf(
"get_qaa: qaa_wave[1] =%d, qaa_wave[2] =%d, "
143 "qaa_wave[3] = %d, qaa_wave[4] = %d.\n",
144 w[1], w[2], w[3], w[4]);
152 printf(
"get_qaa: incompatible sensor wavelengths for aph/adg (need 410).\n");
155 printf(
"QAA v6 bands: (%d) %d nm, (%d) %d nm, (%d) %d nm, (%d) %d nm, (%d) %d nm\n",
156 ib410, w[0], ib440, w[1], ib490, w[2], ib555, w[3], ib670, w[4]);
157 printf(
"QAA v6 wav :");
158 for (ib = 0; ib < nbandsVis; ib++)
159 printf(
" %10.6f", fwave[ib]);
162 printf(
"QAA v6 aw :");
163 for (ib = 0; ib < nbandsVis; ib++)
164 printf(
" %10.6f", aw[ib]);
167 printf(
"QAA v6 bbw :");
168 for (ib = 0; ib < nbandsVis; ib++)
169 printf(
" %10.6f", bbw[ib]);
172 qaa_init(ib410, ib440, ib490, ib555, ib670);
177 qaa_alloc(
l1rec->npix, nbandsVis);
179 for (ip = 0; ip <
l1rec->npix; ip++) {
182 for (ib = 0; ib < nbandsVis; ib++) {
183 ipb = ip * nbandsVis + ib;
191 if (!
l1rec->mask[ip]) {
194 for (
i = 0;
i < nbandsVis;
i++)
195 Rrs[
i] = l2rec->Rrs[ip *
l1rec->l1file->nbands +
i];
198 qaaf_v6(nbandsVis, fwave, Rrs, aw, bbw, rrs,
u, a_qaa, bb_qaa, &flags_qaa);
200 qaaf_decomp(nbandsVis, fwave, rrs, a_qaa, aw, adg_qaa, aph_qaa,
205 for (ib = 0; ib < nbandsVis; ib++) {
207 ipb = ip * nbandsVis + ib;
209 if (isfinite(bb_qaa[ib]))
210 bb[ipb] = bb_qaa[ib];
216 if (isfinite(a_qaa[ib]))
217 atot[ipb] = a_qaa[ib];
223 if (isfinite(adg_qaa[ib]))
224 adg[ipb] = adg_qaa[ib];
230 if (isfinite(aph_qaa[ib]))
231 aph[ipb] = aph_qaa[ib];
238 if (atot[ipb] > 0.0) atot[ipb] =
MAX(atot[ipb], aw [ib]*1.05);
239 if (bb [ipb] > 0.0) bb [ipb] =
MAX(bb [ipb], bbw[ib]*1.05);
241 flags[ip] = flags_qaa;
246 QaaRecNum =
l1rec->iscan;
254 if (!qaa_ran(l2rec->l1rec->iscan))
260 void get_qaa(l2str *l2rec, l2prodstr *
p,
float prod[]) {
261 int prodID =
p->cat_ix;
265 if (!qaa_ran(l2rec->l1rec->iscan))
268 for (ip = 0; ip < l2rec->l1rec->npix; ip++) {
270 ipb = ip * nbandsVis + ib;
276 prod[ip] = atot[ipb];
278 prod[ip] =
p->badData;
285 prod[ip] =
p->badData;
292 prod[ip] =
p->badData;
299 prod[ip] =
p->badData;
303 if ((bb[ipb] - bbw[ib]) > 0.0)
304 prod[ip] = bb[ipb] - bbw[ib];
306 prod[ip] =
p->badData;
311 prod[ip] = bb[ipb] * 53.56857 + 0.00765;
313 prod[ip] =
p->badData;
317 if (bb[ipb] > 0.0 && atot[ipb] > 0.0)
318 prod[ip] = bb[ipb] * 53.56857 + 0.00765 + atot[ipb];
320 prod[ip] =
p->badData;
324 printf(
"-E- %s line %d : erroneous product ID %d passed to get_qaa().\n",
325 __FILE__, __LINE__, prodID);
338 if (!qaa_ran(l2rec->l1rec->iscan))
341 for (ip = 0; ip < l2rec->l1rec->npix; ip++) {
342 for (ib = 0; ib < nbandsVis; ib++) {
343 l2rec->a [ip * l2rec->l1rec->l1file->nbands + ib] = atot[ip * nbandsVis + ib];
344 l2rec->bb[ip * l2rec->l1rec->l1file->nbands + ib] = bb [ip * nbandsVis + ib];
354 if (!qaa_ran(l2rec->l1rec->iscan))
356 for (ip = 0; ip < l2rec->l1rec->npix; ip++) {
357 ipb = nbandsVis * ip + ib440;
358 adg_ref[ip] = adg[ipb];
359 bbp_ref[ip] = bb[ipb] - bbw[ib440];
367 int get_bbp_qaa(l2str *l2rec,
int ip,
float tab_wave[],
float tab_bbp[],
int tab_nwave) {
370 if (!qaa_ran(l2rec->l1rec->iscan))
373 for (ib = 0; ib < nbandsVis; ib++) {
374 ipb = ip * nbandsVis + ib;
375 bbp_qaa[ib] = bb[ipb] - bbw[ib];
380 for (ib = 0; ib < tab_nwave; ib++) {
381 tab_bbp[ib] =
linterp(fwave, bbp_qaa, nbandsVis, tab_wave[ib]);