27 #define BAD_CACO3 BAD_FLT
31 static float bbstr = 1.628;
32 static float caco3min = 1e-5;
33 static float caco3hi = 0.0005;
34 static float fixedbbstar = 1.28;
46 static int firstCall = 1;
47 static int maxiter = 10;
48 static float ftrans = 6.179;
50 static float wave[3] = {670., 760., 870.};
58 static float fw1, fw2;
60 static float oobswf[3][8] = {
61 {0.000313529, 0.000770558, 0.00152194, 0.000155573,
62 0.00116455, 0.0, 0.000445433, 0.000124172},
63 {0.000201709, 6.96143e-05, 7.00147e-06, 2.28957e-07,
64 4.17788e-05, 0.00159814, 0.0, 0.00536827},
65 {0.000463807, 8.54003e-05, 2.47401e-05, 0.000755890,
66 0.00587073, 0.00021686, 0.0111331, 0.0}
72 float bbc_cclth, r8_cclth, aeps_cclth;
73 float bbcinit, bbctol;
76 float *awptr, *bbwptr;
81 l1str *
l1rec = l2rec->l1rec;
88 for (
i = 0;
i < 3;
i++) {
95 b68diff = wave[2] - wave[0];
96 b78diff = wave[1] - wave[0];
99 fw1 = pow(wave[0] / wave[1], 1.35);
100 fw2 = pow(wave[0] / wave[2], 1.35);
108 awptr = &
l1rec->sw_a_avg[ipb];
109 bbwptr = &
l1rec->sw_bb_avg[ipb];
111 for (
i = 0;
i < 3;
i++) {
112 aw [
i] = awptr [bx[
i]];
113 bbw [
i] = bbwptr[bx[
i]];
125 if ((
l1rec->flags[ip] & caco3_msk) != 0) {
129 if ((rhoaw = (
float *) calloc(nwave,
sizeof (
float))) ==
NULL) {
130 printf(
"-E- : Error allocating memory to rhoaw\n");
136 for (ib = 0; ib < nwave; ib++) {
137 ipb = nwave * ip + ib;
138 rhoaw[ib] = ((l2rec->l1rec->Lt[ipb] /
l1rec->tg_sol[ipb] /
l1rec->tg_sen[ipb] -
l1rec->tLf[ipb]
141 for (
i = 0;
i < 3;
i++) {
142 rho[
i] = rhoaw[bx[
i]];
143 for (ib = 0; ib < nwave; ib++) {
144 rho[
i] -= rhoaw[ib] * oobswf[
i][ib];
149 for (
i = 0;
i < 3;
i++) {
151 ipb = nwave * ip + ib;
152 rho[
i] = ((l2rec->l1rec->Lt[ipb] /
l1rec->tg_sol[ipb] /
l1rec->tg_sen[ipb] -
l1rec->tLf[ipb]
165 for (
i = 0;
i < 3;
i++) {
166 ipb = nwave * ip + bx[
i];
171 while (bbctol > 5. && numiter < maxiter) {
175 bbc[1] = bbc[0] * fw1;
176 bbc[2] = bbc[0] * fw2;
179 r8_cclth = rho[2] - (bbw[2] + bbc[2]) / (aw[2] + bbw[2] + bbc[2]) / ftrans *
t[2];
181 if ((r8_cclth > 0.09) || (r8_cclth < 0)) {
188 aeps_cclth = log((rho[1] - (bbw[1] + bbc[1]) / (aw[1] + bbw[1] + bbc[1]) / ftrans *
t[1]) / r8_cclth) / b78diff;
190 if (aeps_cclth > 0.4) {
197 bbc[0] = (rho[0] - r8_cclth * exp(aeps_cclth * b68diff)) /
t[0] * (aw[0] + bbw[0] + bbc[0]) * ftrans - bbw[0];
199 if ((bbc[0] <= 0) || isnan(bbc[0])) {
205 bbctol =
fabs((bbcinit - bbc[0]) / bbcinit)*100.;
212 bbc_cclth = bbc[0] / pow((546. / wave[0]), 1.35) - bbw546;
215 caco3 = bbc_cclth / bbstr;
216 newbbstar = pow(10,(0.2007476*log10(caco3)*log10(caco3) + 1.033187*log10(caco3) + 1.069821));
218 if (newbbstar < fixedbbstar) {
219 newbbstar = fixedbbstar;
222 caco3 = caco3*bbstr/newbbstar;
244 static int firstCall = 1;
245 static int bandShift = 0;
249 typedef float tbb_t[
N550];
251 static int32_t ib443;
252 static int32_t ib550;
255 int i443 = 0, i550 = 0;
257 int32_t nwave,
i, nc;
262 l1str *
l1rec = l2rec->l1rec;
278 printf(
"-E- %s line %d: No picfile specified.\n", __FILE__, __LINE__);
281 printf(
"Loading PIC 2-band algorithm table %s\n",
filename);
287 printf(
"Assuming PIC table is for 443nm and 555nm.\n");
291 if (ib443 < 0 || ib550 < 0) {
292 printf(
"-E- %s line %d: required bands not available PIC\n",
299 fprintf(
stderr,
"-E- %s line %d: unable to open %s for reading\n",
306 while (fgets(
line, 80, fp)) {
307 if (
line[0] !=
'#' &&
line[0] !=
'\n') {
313 for (
i = 0;
i < nc;
i++)
318 for (i443 = 0; i443 <
N443; i443++)
319 for (i550 = 0; i550 <
N550; i550++) {
320 fscanf(fp,
"%f %f %f %f\n", &
x, &
y, &
a, &
b);
323 tbb [i443][i550] =
a;
332 if (
l1rec->mask[ip]) {
336 x443 = l2rec->nLw[ip * nwave + ib443];
337 x550 = l2rec->nLw[ip * nwave + ib550];
350 x550 *= Rrs555 / l2rec->Rrs[ip * nwave + ib550];
355 if (x443 < t443[
i]) {
360 if (x443 >= t443[
N443-1])
364 if (x550 < t550[
i]) {
369 if (x550 >= t550[
N550-1])
386 if (tbb[i443 - 1][i550 - 1] > 998.9 || tbb[i443 ][i550 - 1] > 998.9 ||
387 tbb[i443 - 1][i550 ] > 998.9 || tbb[i443 ][i550 ] > 998.9) {
392 bb1 = tbb[i443 - 1][i550 - 1] + (x443 - t443[i443 - 1])*
393 (tbb[i443][i550 - 1] - tbb[i443 - 1][i550 - 1]) / (t443[i443] - t443[i443 - 1]);
395 bb2 = tbb[i443 - 1][i550 ] + (x443 - t443[i443 - 1])*
396 (tbb[i443][i550 ] - tbb[i443 - 1][i550 ]) / (t443[i443] - t443[i443 - 1]);
398 bb = bb1 + (x550 - t550[i550 - 1]) * (bb2 - bb1) / (t550[i550] - t550[i550 - 1]);
403 newbbstar = pow(10,(0.2007476*log10(caco3)*log10(caco3) + 1.033187*log10(caco3) + 1.069821));
405 if (newbbstar < fixedbbstar) {
406 newbbstar = fixedbbstar;
409 caco3 = caco3*bbstr/newbbstar;
427 int32_t shallowDepth = 30;
429 turbid = ((l2rec->l1rec->flags[ip] &
TURBIDW) != 0);
430 shallow = (
abs(l2rec->l1rec->dem[ip]) < shallowDepth);
433 if (turbid & shallow) {
440 if (caco3 < caco3hi) {
451 caco3 =
MAX(caco3, caco3min);
464 int nwave = l2rec->l1rec->l1file->nbands;
465 static int32_t ibRed;
466 static int32_t ibGreen;
467 static int firstCall = 1;
470 ibRed =
windex(667., l2rec->l1rec->l1file->fwave, nwave);
471 ibGreen =
windex(550., l2rec->l1rec->l1file->fwave, nwave);
475 float RrsRed = l2rec->Rrs[ip * nwave + ibRed];
476 float RrsGreen = l2rec->Rrs[ip * nwave + ibGreen];
477 if (RrsRed >= 0.0 && RrsGreen >= 0.0) {
478 caco3 = 1.3055 * (RrsGreen - RrsRed) - 0.00188;
495 int nwave = l2rec->l1rec->l1file->nbands;
496 static int32_t ibNIR;
497 static int32_t ibRed;
498 static int32_t ibGreen;
499 static float wvlratio;
500 static int firstCall = 1;
504 ibNIR =
windex(869., l2rec->l1rec->l1file->fwave, nwave);
507 ibNIR =
windex(748., l2rec->l1rec->l1file->fwave, nwave);
509 ibRed =
windex(667., l2rec->l1rec->l1file->fwave, nwave);
510 ibGreen =
windex(550., l2rec->l1rec->l1file->fwave, nwave);
511 wvlratio = (l2rec->l1rec->l1file->fwave[ibRed] -
512 l2rec->l1rec->l1file->fwave[ibGreen]) /
513 (l2rec->l1rec->l1file->fwave[ibNIR] -
514 l2rec->l1rec->l1file->fwave[ibGreen]);
518 float RrsNIR = l2rec->Rrs[ip * nwave + ibNIR];
521 float RrsRed = l2rec->Rrs[ip * nwave + ibRed];
522 float RrsGreen = l2rec->Rrs[ip * nwave + ibGreen];
523 if (RrsRed >= 0.0 && RrsGreen >= 0.0) {
524 CI = RrsRed - (RrsGreen + (wvlratio * (RrsNIR - RrsGreen)));
526 caco3 = -0.8013 * CI - 0.00076;
528 caco3 = -1.3764 * CI - 0.00071;
541 void calcite(l2str *l2rec, l2prodstr *
p,
float prod[]) {
544 for (ip = 0; ip < l2rec->l1rec->npix; ip++) {
565 printf(
"Error: %s : Unknown product specifier: %d\n", __FILE__,
p->cat_ix);
571 l2rec->l1rec->flags[ip] |=
PRODFAIL;