10 static int32_t *w =
NULL;
11 static float *
a =
NULL;
22 if (w[0] < 0 || w[1] < 0) {
23 printf(
"chl_oc2: algorithm coefficients not provided for this sensor.\n");
28 if (ib1 < 0 || ib2 < 0) {
29 printf(
"chl_oc2: incompatible sensor wavelengths for this algorithm\n");
37 if (Rrs1 > 0.0 && Rrs2 > 0.0 && Rrs1 / Rrs2 <= 10.0) {
39 if (rat > minrat && rat < maxrat) {
42 pow(10.0, (
a[0] + rat * (
a[1] + rat * (
a[2] + rat * (
a[3] + rat *
a[4])))));
43 chl = (chl > chlmin ? chl : chlmin);
44 chl = (chl < chlmax ? chl : chlmax);
52 static int32_t *w =
NULL;
53 static float *
a =
NULL;
59 float Rrs1, Rrs2, Rrs3, Rrsmax;
61 float *dRrs, *dchl,*covariance_matrix, tmpderiv;
63 uncertainty_t *uncertainty=l2rec->l1rec->uncertainty;
68 dRrs=uncertainty->dRrs;
69 dchl=&uncertainty->dchl;
70 if(
input->proc_uncertainty==2)
71 covariance_matrix=uncertainty->covaraince_matrix;
73 covariance_matrix=uncertainty->pixel_covariance;
77 nbands=l2rec->l1rec->l1file->nbands;
80 if (w[0] < 0 || w[1] < 0 || w[2] < 0) {
81 printf(
"chl_oc3: algorithm coefficients not provided for this sensor.\n");
87 if (ib1 < 0 || ib2 < 0 || ib3 < 0) {
88 printf(
"chl_oc3: incompatible sensor wavelengths for this algorithm\n");
97 minRrs =
MIN(Rrs1, Rrs2);
99 if (Rrs3 > 0.0 && Rrs2 > 0.0 && minRrs > -0.001) {
100 rat =
MAX(Rrs1, Rrs2) / Rrs3;
101 if (rat > minrat && rat < maxrat) {
104 pow(10.0, (
a[0] + rat * (
a[1] + rat * (
a[2] + rat * (
a[3] + rat *
a[4])))));
114 tmpderiv= chl* (
a[1]+2*
a[2]*rat+3*
a[3]*rat*rat+4*
a[4]*rat*rat*rat);
116 *dchl=pow(tmpderiv,2)*(pow(dRrs[ibmax]/Rrsmax,2)+ pow(dRrs[ib3]/Rrs3,2));
117 *dchl-=2*pow(tmpderiv,2)/(Rrsmax*Rrs3)*covariance_matrix[ibmax*
nbands+ib3];
118 *dchl=sqrt(*dchl+chl*fit_unc*chl*fit_unc);
121 chl = (chl > chlmin ? chl : chlmin);
122 chl = (chl < chlmax ? chl : chlmax);
125 if(
fabs(chl-chlmin)<0.0000001 ||
fabs(chl-chlmax)<0.0000001)
135 static float a[] = {0.2515, -2.3798, 1.5823, -0.6372, -0.5692};
141 float Rrs1, Rrs2, Rrs3;
149 if (ib1 < 0 || ib2 < 0 || ib3 < 0) {
150 printf(
"chl_oc3: incompatible sensor wavelengths for this algorithm\n");
159 minRrs =
MIN(Rrs1, Rrs2);
161 if (Rrs3 > 0.0 && Rrs2 > 0.0 && minRrs > -0.001) {
163 rat =
MAX(Rrs1, Rrs2) / Rrs3;
164 if (rat > minrat && rat < maxrat) {
167 pow(10.0, (
a[0] + rat * (
a[1] + rat * (
a[2] + rat * (
a[3] + rat *
a[4])))));
168 chl = (chl > chlmin ? chl : chlmin);
169 chl = (chl < chlmax ? chl : chlmax);
177 static int32_t *w =
NULL;
178 static float *
a =
NULL;
186 float Rrs1, Rrs2, Rrs3, Rrs4, Rrsmax;
189 float *dRrs, *dchl,*covariance_matrix, tmpderiv;
190 uncertainty_t *uncertainty=l2rec->l1rec->uncertainty;
195 dRrs=uncertainty->dRrs;
196 dchl=&uncertainty->dchl;
197 if(
input->proc_uncertainty==2)
198 covariance_matrix=uncertainty->covaraince_matrix;
200 covariance_matrix=uncertainty->pixel_covariance;
204 nbands=l2rec->l1rec->l1file->nbands;
207 if (w[0] < 0 || w[1] < 0 || w[2] < 0 || w[3] < 0) {
208 printf(
"chl_oc4: algorithm coefficients not provided for this sensor.\n");
215 if (ib1 < 0 || ib2 < 0 || ib3 < 0 || ib4 < 0) {
216 printf(
"chl_oc4: incompatible sensor wavelengths for this algorithm\n");
226 minRrs =
MIN(Rrs1, Rrs2);
228 if (Rrs4 > 0.0 && Rrs3 > 0.0 && (Rrs2 > 0.0 || Rrs1 * Rrs2 > 0.0) && minRrs > -0.001) {
229 rat =
MAX(
MAX(Rrs1, Rrs2), Rrs3) / Rrs4;
230 if (rat > minrat && rat < maxrat) {
233 pow(10.0, (
a[0] + rat * (
a[1] + rat * (
a[2] + rat * (
a[3] + rat *
a[4])))));
236 if(Rrs1>=Rrs2 && Rrs1>=Rrs3){
240 else if(Rrs2>=Rrs1 && Rrs2>=Rrs3){
248 tmpderiv= chl* (
a[1]+2*
a[2]*rat+3*
a[3]*rat*rat+4*
a[4]*rat*rat*rat);
249 *dchl=pow(tmpderiv,2)*(pow(dRrs[ibmax]/Rrsmax,2)+ pow(dRrs[ib4]/Rrs4,2));
250 *dchl-=2*pow(tmpderiv,2)/(Rrsmax*Rrs4)*covariance_matrix[ibmax*
nbands+ib4];
251 *dchl=sqrt(*dchl+chl*fit_unc*chl*fit_unc);
254 chl = (chl > chlmin ? chl : chlmin);
255 chl = (chl < chlmax ? chl : chlmax);
257 if(
fabs(chl-chlmin)<0.0000001 ||
fabs(chl-chlmax)<0.0000001)
267 static float w[] = {443, 555, 670};
268 static float a[] = {-0.4287, 230.47};
275 float Rrs1, Rrs2, Rrs3;
280 uncertainty_t *uncertainty=l2rec->l1rec->uncertainty;
281 float *dRrs, *dchl, *covariance_matrix, cov_term=0;
284 dRrs=uncertainty->dRrs;
285 dchl=&uncertainty->dchl;
286 if(
input->proc_uncertainty==2)
287 covariance_matrix=uncertainty->covaraince_matrix;
289 covariance_matrix=uncertainty->pixel_covariance;
293 nbands=l2rec->l1rec->l1file->nbands;
300 if (ib1 < 0 || ib2 < 0 || ib3 < 0) {
301 printf(
"chl_hu: incompatible sensor wavelengths for this algorithm\n");
302 printf(
"chl_hu: %d %d %d\n", ib1, ib2, ib3);
305 printf(
"chl_hu: using %7.2f %7.2f %7.2f\n", l2rec->l1rec->l1file->fwave[ib1], l2rec->l1rec->l1file->fwave[ib2], l2rec->l1rec->l1file->fwave[ib3]);
329 if (Rrs3 >
BAD_FLT + 1 && Rrs2 > 0.0 && Rrs1 > 0.0) {
332 Rrs2 =
conv_rrs_to_555(Rrs2, l2rec->l1rec->l1file->fwave[ib2],dRrs[ib2], &dRrs2);
336 ci =
MIN(Rrs2 - (Rrs1 + (w[1] - w[0]) / (w[2] - w[0])*(Rrs3 - Rrs1)), 0.0);
338 if(uncertainty && ci<0){
339 dci=sqrt( dRrs2*dRrs2 + pow((w[2]-w[1])*dRrs[ib1]/(w[2]-w[0]),2) + pow((w[1]-w[0])*dRrs[ib3]/(w[2]-w[0]),2) );
340 cov_term=(w[1]-w[2])/(w[2]-w[0])*covariance_matrix[ib1*
nbands+ib2];
341 cov_term+=(w[0]-w[1])/(w[2]-w[0])*covariance_matrix[ib2*
nbands+ib3];
342 cov_term+=(w[1]-w[2])/(w[2]-w[0])*(w[0]-w[1])/(w[2]-w[0])*covariance_matrix[ib1*
nbands+ib3];
343 dci=sqrt(dci*dci+cov_term);
347 chl = (
float) pow(10.0,
a[0] +
a[1] * ci);
349 *dchl=chl*log(10)*
a[1]*dci;
351 *dchl=sqrt(*dchl**dchl+fit_unc*chl*fit_unc*chl);
355 chl = (chl > chlmin ? chl : chlmin);
356 chl = (chl < chlmax ? chl : chlmax);
363 static float t1 = 0.25;
364 static float t2 = 0.35;
365 static float w[] = {443, 555, 670};
366 static float a[] = {-0.4909, 191.6590};
367 static float dcoef1[]={0.0203,6.303};
368 static float dcoef2[]={0.0388, 0.169, 0.691, 1.552, 0.906};
377 float Rrs1, Rrs2, Rrs3;
383 fit_unc=fit_unc* (chl1 * (t2 - chl1) / (t2 - t1)+ chl2 * (chl1 - t1) / (t2 - t1));
385 uncertainty_t *uncertainty=l2rec->l1rec->uncertainty;
386 float *dRrs, *covariance_matrix,deriv[5]={0,0,0,0,0};
388 static int32_t *w_ocx =
NULL;
389 static float *a_ocx =
NULL;
396 float deriv_chl1,deriv_chl2;
397 deriv_chl1=(t2-2*chl1+chl2)/(t2-t1);
398 deriv_chl2=(chl1-t1)/(t2-t1);
401 dRrs=uncertainty->dRrs;
402 if(
input->proc_uncertainty==2)
403 covariance_matrix=uncertainty->covaraince_matrix;
405 covariance_matrix=uncertainty->pixel_covariance;
409 nbands=l2rec->l1rec->l1file->nbands;
416 if (ib1 < 0 || ib2 < 0 || ib3 < 0) {
417 printf(
"chl_hu: incompatible sensor wavelengths for this algorithm\n");
418 printf(
"chl_hu: %d %d %d\n", ib1, ib2, ib3);
422 w_ocx =
input->chloc3w;
423 a_ocx =
input->chloc3c;
424 if (w_ocx[0] < 0 || w_ocx[1] < 0 || w_ocx[2] < 0) {
425 printf(
"chl_oc3: algorithm coefficients not provided for this sensor.\n");
431 if (ib1 < 0 || ib4 < 0 || ib5 < 0) {
432 printf(
"chl_oc3: incompatible sensor wavelengths for this algorithm\n");
449 if (Rrs3 >
BAD_FLT + 1 && Rrs2 > 0.0 && Rrs1 > 0.0) {
452 Rrs2 =
conv_rrs_to_555(Rrs2, l2rec->l1rec->l1file->fwave[ib2],dRrs[ib2], &dRrs2);
456 ci =
MIN(Rrs2 - (Rrs1 + (w[1] - w[0]) / (w[2] - w[0])*(Rrs3 - Rrs1)), 0.0);
458 if(uncertainty && ci<0){
459 deriv[0]=chl1*log(10)*
a[1]*(w[1]-w[2])/(w[2]-w[0]);
460 deriv[1]=chl1*log(10)*
a[1];
461 deriv[2]=chl1*log(10)*
a[1]*(w[0]-w[1])/(w[2]-w[0]);
471 minRrs =
MIN(Rrs1, Rrs4);
473 if (Rrs5 > 0.0 && Rrs4 > 0.0 && minRrs > -0.001) {
474 rat =
MAX(Rrs1, Rrs4) / Rrs5;
475 if (rat > minrat && rat < maxrat) {
477 chl = (
float)pow(10.0, (a_ocx[0] + rat * (a_ocx[1] + rat * (a_ocx[2] + rat * (a_ocx[3] + rat * a_ocx[4])))));
480 tmpderiv= chl* (a_ocx[1]+2*a_ocx[2]*rat+3*a_ocx[3]*rat*rat+4*a_ocx[4]*rat*rat*rat);
482 deriv[0]=deriv_chl1*deriv[0]+deriv_chl2*tmpderiv/Rrs1;
485 deriv[3]=deriv_chl2*tmpderiv/Rrs4;
487 deriv[4]=-deriv_chl2*tmpderiv/Rrs5;
489 deriv[1]*=deriv_chl1;
490 deriv[2]*=deriv_chl1;
502 bindex[0]=ib1;bindex[1]=ib2;bindex[2]=ib3;
503 bindex[3]=ib4;bindex[4]=ib5;
505 chl+=pow(deriv[
i]*dRrs[bindex[
i]],2);
510 if(bindex[
j]>=bindex[
i])
511 chl+=2*deriv[
i]*deriv[
j]*covariance_matrix[bindex[
i]*
nbands+bindex[
j]];
513 chl+=2*deriv[
i]*deriv[
j]*covariance_matrix[bindex[
j]*
nbands+bindex[
i]];
520 for(
int icoef=0;icoef<5;icoef++)
521 dmodel+=(pow(rat,2*icoef)*pow(dcoef2[icoef],2));
522 dmodel*=pow(deriv_chl2*chl2*log(10),2);
524 dmodel+=pow(deriv_chl1*chl1*log(10),2)*(dcoef1[0]*dcoef1[0]+pow(ci*dcoef1[1],2));
526 return (sqrt(chl+fit_unc*fit_unc));
532 static float t1 = 0.25;
533 static float t2 = 0.35;
539 uncertainty_t *uncertainty=l2rec->l1rec->uncertainty;
542 dchl=&uncertainty->dchl;
554 chl = chl1 * (t2 - chl1) / (t2 - t1)
555 + chl2 * (chl1 - t1) / (t2 - t1);
566 static float chl_lo = 0.35;
567 static float chl_hi = 20.0;
569 static float c_modisa[6] = {1.030e+00, 7.668e-02, 4.152e-01, 5.335e-01, -5.040e-01, 1.265e-01};
570 static float c_meris [6] = {1.0, 0.0, 0.0, 0.0, 0.0};
571 static float c_null [6] = {1.0, 0.0, 0.0, 0.0, 0.0};
583 switch (l2rec->l1rec->l1file->sensorID) {
607 printf(
"%s Line %d: need a default chlorophyll algorithm for this sensor\n",
613 chl =
MAX(
MIN(chl_hi, chl1), chl_lo);
615 rat =
c[0] + lchl * (
c[1] + lchl *
c[2] + lchl * (
c[3] + lchl * (
c[4] + lchl *
c[5])));
634 float chl1, chl2, chl3, chl4, chl5, chl6, chl7, Z;
635 float nLw1, nLw2, nLw3;
637 static int ib1 = -1, ib2 = -1, ib3 = -1;
648 if (ib1 < 0 || ib2 < 0 || ib3 < 0) {
649 printf(
"chl_abi is not compatible with this sensor\n");
652 printf(
"Calculating chl_abi using %7.2f %7.2f %7.2f\n",
653 l2rec->l1rec->l1file->fwave[ib1],
654 l2rec->l1rec->l1file->fwave[ib2],
655 l2rec->l1rec->l1file->fwave[ib3]);
666 chl1 = ((nLw2 / nLw3) - nLw1) / ((nLw2 / nLw3) + nLw1);
667 chl2 = pow(10.0, chl1);
668 chl3 = (((nLw1 * nLw2) / 47.0)*((nLw1 * nLw2) / (nLw3 * nLw3)));
670 chl5 = 0.1403 * (pow(chl4, (-0.572)));
671 chl6 = pow(chl5, 1.056);
673 chl7 = chl6 / (chl6 + Z);
681 switch (l2rec->l1rec->l1file->sensorID) {
700 switch (l2rec->l1rec->l1file->sensorID) {
740 printf(
"%s Line %d: need a default chlorophyll algorithm for this sensor\n",
749 void get_chl(l2str *l2rec,
int prodnum,
float prod[]) {
753 int32_t
nbands = l2rec->l1rec->l1file->nbands;
754 int32_t
npix = l2rec->l1rec->npix;
759 for (ip = 0; ip <
npix; ip++) {
769 prod[ip] =
chl_oc2(l2rec, &l2rec->Rrs[ipb]);
772 prod[ip] =
chl_oc3(l2rec, &l2rec->Rrs[ipb]);
775 prod[ip] =
chl_oc3c(l2rec, &l2rec->Rrs[ipb]);
778 prod[ip] =
chl_oc4(l2rec, &l2rec->Rrs[ipb]);
781 prod[ip] =
chl_hu(l2rec, &l2rec->Rrs[ipb]);
784 prod[ip] =
chl_oci(l2rec, &l2rec->Rrs[ipb]);
787 prod[ip] =
chl_cdr(l2rec, &l2rec->Rrs[ipb]);
790 prod[ip] =
chl_abi(l2rec, &l2rec->nLw[ipb]);
793 printf(
"Error: %s : Unknown product specifier: %d\n", __FILE__, prodnum);
798 if (prod[ip] == chlbad)
799 l2rec->l1rec->flags[ip] |=
PRODFAIL;