35 #include <gsl/gsl_errno.h>
36 #include <gsl/gsl_vector.h>
37 #include <gsl/gsl_blas.h>
38 #include <gsl/gsl_multifit_nlin.h>
39 #include <gsl/gsl_linalg.h>
40 #include <gsl/gsl_poly.h>
48 static int32_t LastRecNum = -1;
50 static float bbp_s_max = 5.0;
51 static float bbp_s_min = 0.0;
52 static float adg_s_max = 0.025;
53 static float adg_s_min = 0.01;
54 static float chl_max = 10.0;
55 static float chl_min = 0.03;
91 static float *rrsdiff;
93 static float **fit_par;
95 static int16 max_npar;
98 static int allocateRrsRaman = 0;
102 for (
i = 0;
i < m; ++
i) {
110 for (
i = 0;
i < m; ++
i) {
129 printf(
"\nLoading default basis vector from %s\n",
file);
134 printf(
"-E- %s line %d: error opening (%s) file", __FILE__, __LINE__,
file);
143 xtab = &
table[nrow * 0];
145 for (ivec = 0; ivec < ncol - 1; ivec++) {
146 ytab = &
table[nrow * (ivec + 1)];
147 for (
i = 0;
i < nx;
i++) {
168 for (ivec = 0; ivec <
g->aph_nvec; ivec++) {
169 g->par[ipar] = chl /
g->aph_nvec;
174 for (ivec = 0; ivec <
g->adg_nvec; ivec++) {
180 switch (
g->bbp_opt) {
190 for (ivec = 0; ivec <
g->bbp_nvec; ivec++) {
191 g->par[ipar] = 0.001;
199 if (ipar !=
g->npar &&
g->fit_opt !=
SVDSIOP) {
200 printf(
"-E- %s Line %d: number of GIOP fit parameters (%d) does not equal number of initialized parameter (%d)\n",
201 __FILE__, __LINE__,
g->npar, ipar);
216 float aw[],
float bbw[]) {
220 float def_aph_w = 443.0;
221 float def_aph_s = 0.5;
222 float def_adg_w = 443.0;
223 float def_adg_s = 0.02061;
224 float def_bbp_w = 443.0;
225 float def_bbp_s = 1.03373;
226 float def_grd[] = {0.0949, 0.0794};
230 if (
input->giop_wave[0] > 0) {
231 for (iw = 0; iw < nwave; iw++) {
232 if (
input->giop_wave[iw] <= 0)
break;
239 g->nwave =
MIN(
windex(671., wave, nwave) + 1, nwave);
240 for (iw = 0; iw <
g->nwave; iw++)
246 for (iw = 0; iw <
g->nwave; iw++) {
247 g->wave[iw] = wave[
g->bindx[iw]];
248 g->aw [iw] = aw [
g->bindx[iw]];
249 g->bbw [iw] = bbw [
g->bindx[iw]];
254 if (
input->giop_rrs_unc[0] > 0) {
256 for (iw = 0; iw < nwave; iw++) {
257 if (
input->giop_rrs_unc[iw] <= 0)
break;
258 g->wts[iw] = 1.0 / pow(
input->giop_rrs_unc[iw], 2);
260 if (iw !=
g->nwave) {
261 printf(
"-E- %s line %d: number of giop_rrs_unc (%d) must equal number of giop_wave (%d)",
262 __FILE__, __LINE__, iw,
g->nwave);
267 for (iw = 0; iw <
g->nwave; iw++)
274 if (
input->giop_maxiter > 0)
275 g->maxiter =
input->giop_maxiter;
281 if (
input->giop_fit_opt > 0)
282 g->fit_opt =
input->giop_fit_opt;
288 if (
input->giop_rrs_opt > 0)
289 g->rrs_opt =
input->giop_rrs_opt;
296 if (
input->giop_grd[0] > -999.0) {
297 g->grd[0] =
input->giop_grd[0];
298 g->grd[1] =
input->giop_grd[1];
300 g->grd[0] = def_grd[0];
301 g->grd[1] = def_grd[1];
325 if (
input->giop_aph_opt > 0)
326 g->aph_opt =
input->giop_aph_opt;
328 if (
input->giop_aph_w > 0.0)
329 g->aph_w =
input->giop_aph_w;
331 g->aph_w = def_aph_w;
333 if (
input->giop_aph_s > -999.0)
334 g->aph_s =
input->giop_aph_s;
336 g->aph_s = def_aph_s;
343 if (
input->giop_adg_opt != 1)
344 g->adg_opt =
input->giop_adg_opt;
348 if (
input->giop_adg_w > 0.0)
349 g->adg_w =
input->giop_adg_w;
351 g->adg_w = def_adg_w;
353 if (
input->giop_adg_s > -999.0)
354 g->adg_s =
input->giop_adg_s;
356 g->adg_s = def_adg_s;
359 if (
input->giop_acdom_opt != 1)
360 g->acdom_opt =
input->giop_acdom_opt;
365 if (
input->giop_anap_opt != 1)
366 g->anap_opt =
input->giop_anap_opt;
374 if (
input->giop_bbp_opt != 1)
375 g->bbp_opt =
input->giop_bbp_opt;
379 if (
input->giop_bbp_w > 0.0)
380 g->bbp_w =
input->giop_bbp_w;
382 g->bbp_w = def_bbp_w;
384 if (
input->giop_bbp_s > -999.0)
385 g->bbp_s =
input->giop_bbp_s;
387 g->bbp_s = def_bbp_s;
390 if (
input->giop_bbph_opt != 1)
391 g->bbph_opt =
input->giop_bbph_opt;
396 if (
input->giop_bbnap_opt != 1)
397 g->bbnap_opt =
input->giop_bbnap_opt;
404 switch (
g->aph_opt) {
413 switch (
g->adg_opt) {
422 switch (
g->acdom_opt) {
431 switch (
g->anap_opt) {
440 switch (
g->bbp_opt) {
453 switch (
g->bbph_opt) {
462 switch (
g->bbnap_opt) {
476 g->npar =
g->aph_nvec +
g->adg_nvec +
g->bbp_nvec;
482 g->aph_tab_nw = nwave;
483 g->aph_tab_w = (
float *) calloc(nwave,
sizeof (
float));
486 g->adg_tab_nw = nwave;
487 g->adg_tab_w = (
float *) calloc(nwave,
sizeof (
float));
490 g->acdom_tab_nw = nwave;
491 g->acdom_tab_w = (
float *) calloc(nwave,
sizeof (
float));
494 g->anap_tab_nw = nwave;
495 g->anap_tab_w = (
float *) calloc(nwave,
sizeof (
float));
498 g->bbp_tab_nw = nwave;
499 g->bbp_tab_w = (
float *) calloc(nwave,
sizeof (
float));
502 g->bbph_tab_nw = nwave;
503 g->bbph_tab_w = (
float *) calloc(nwave,
sizeof (
float));
506 g->bbnap_tab_nw = nwave;
507 g->bbnap_tab_w = (
float *) calloc(nwave,
sizeof (
float));
512 for (iw = 0; iw < nwave; iw++) {
513 g->aph_tab_w[iw] = wave[iw];
514 g->adg_tab_w[iw] = wave[iw];
515 g->acdom_tab_w[iw] = wave[iw];
516 g->anap_tab_w[iw] = wave[iw];
517 g->bbp_tab_w[iw] = wave[iw];
518 g->bbph_tab_w[iw] = wave[iw];
519 g->bbnap_tab_w[iw] = wave[iw];
524 switch (
g->aph_opt) {
530 switch (
g->adg_opt) {
536 switch (
g->bbp_opt) {
541 switch (
g->acdom_opt) {
546 switch (
g->anap_opt) {
551 switch (
g->bbph_opt) {
556 switch (
g->bbnap_opt) {
580 void giop_model(giopstr *
g,
double par[],
int nwave,
float wave[],
float aw[],
float bbw[],
581 float foq[],
float aph[],
float adg[],
float bbp[],
582 double rrs[],
double **dfdpar,
double **parstar) {
584 int iw, iwtab, ivec, ipar;
595 if ((aphstar = (
float *) calloc(nwave,
sizeof (
float))) ==
NULL) {
596 printf(
"-E- %s line %d : error allocating memory for GIOP:giop_model.\n",
600 if ((adgstar = (
float *) calloc(nwave,
sizeof (
float))) ==
NULL) {
601 printf(
"-E- %s line %d : error allocating memory for GIOP:giop_model.\n",
605 if ((bbpstar = (
float *) calloc(nwave,
sizeof (
float))) ==
NULL) {
606 printf(
"-E- %s line %d : error allocating memory for GIOP:giop_model.\n",
611 for (iw = 0; iw < nwave; iw++) {
620 switch (
g->aph_opt) {
622 aphstar[ipar] = exp(-1. * pow(wave[iw] -
g->aph_w, 2) / (2 * pow(
g->aph_s, 2)));
623 aph[iw] = par[ipar] * aphstar[ipar];
624 aph[iw + nwave] = par[ipar +
g->npar] * aphstar[ipar];
630 for (ivec = 0; ivec <
g->aph_nvec; ivec++) {
631 iwtab =
windex(wave[iw],
g->aph_tab_w,
g->aph_tab_nw);
632 aphstar[ipar] =
g->aph_tab_s[ivec][iwtab];
633 aph[iw] += par[ipar] * aphstar[ipar];
634 aph[iw + nwave] += par[ipar +
g->npar] * aphstar[ipar];
640 switch (
g->adg_opt) {
644 adgstar[ipar] = exp(-
g->adg_s * (wave[iw] -
g->adg_w));
645 adg[iw] = par[ipar] * adgstar[ipar];
646 adg[iw + nwave] = par[ipar +
g->npar] * adgstar[ipar];
652 for (ivec = 0; ivec <
g->adg_nvec; ivec++) {
653 iwtab =
windex(wave[iw],
g->adg_tab_w,
g->adg_tab_nw);
654 adgstar[ipar] =
g->adg_tab_s[ivec][iwtab];
655 adg[iw] += par[ipar] * adgstar[ipar];
656 adg[iw + nwave] += par[ipar +
g->npar] * adgstar[ipar];
662 switch (
g->bbp_opt) {
665 iwtab =
windex(wave[iw],
g->bbp_tab_w,
g->bbp_tab_nw);
666 bbpstar[ipar] =
g->bbp_tab_s[0][iwtab];
667 bbp[iw] = bbpstar[ipar];
675 bbpstar[ipar] = pow((
g->bbp_w / wave[iw]),
g->bbp_s);
676 bbp[iw] = par[ipar] * bbpstar[ipar];
677 bbp[iw + nwave] = par[ipar +
g->npar] * bbpstar[ipar];
683 for (ivec = 0; ivec <
g->bbp_nvec; ivec++) {
684 iwtab =
windex(wave[iw],
g->bbp_tab_w,
g->bbp_tab_nw);
685 bbpstar[ipar] =
g->bbp_tab_s[ivec][iwtab];
686 bbp[iw] += par[ipar] * bbpstar[ipar];
687 bbp[iw + nwave] += par[ipar +
g->npar] * bbpstar[ipar];
693 a = aw [iw] + aph[iw] + adg[iw];
694 bb = bbw[iw] + bbp[iw];
697 switch (
g->rrs_opt) {
699 rrs[iw] =
g->grd[0] *
x +
g->grd[1] * pow(
x, 2);
700 dfdx =
g->grd[0] + 2 *
g->grd[1] *
x;
703 rrs[iw] = foq[iw] *
x;
708 if (dfdpar !=
NULL) {
713 dxdb =
x / bb + dxda;
715 switch (
g->aph_opt) {
717 for (ivec = 0; ivec <
g->aph_nvec; ivec++) {
718 dfdpar[iw][ipar] = dfdx * dxda * aphstar[ipar];
724 switch (
g->adg_opt) {
726 for (ivec = 0; ivec <
g->adg_nvec; ivec++) {
727 dfdpar[iw][ipar] = dfdx * dxda * adgstar[ipar];
733 switch (
g->bbp_opt) {
738 for (ivec = 0; ivec <
g->bbp_nvec; ivec++) {
739 dfdpar[iw][ipar] = dfdx * dxdb * bbpstar[ipar];
746 if (parstar !=
NULL) {
750 switch (
g->aph_opt) {
752 for (ivec = 0; ivec <
g->aph_nvec; ivec++) {
753 parstar[iw][ipar] = aphstar[ipar];
759 switch (
g->adg_opt) {
761 for (ivec = 0; ivec <
g->adg_nvec; ivec++) {
762 parstar[iw][ipar] = adgstar[ipar];
768 switch (
g->bbp_opt) {
773 for (ivec = 0; ivec <
g->bbp_nvec; ivec++) {
774 parstar[iw][ipar] = bbpstar[ipar];
793 float foq[],
float aph[],
float adg[],
float bbp[],
float acdom[],
float anap[],
float bbph[],
float bbnap[],
794 double rrs[],
double **dfdpar,
double **parstar) {
796 int iw, iwtab, idx443;
816 if ((aphstar = (
float *) calloc(nwave,
sizeof (
float))) ==
NULL) {
817 printf(
"-E- %s line %d : error allocating memory for GIOP:giop_model.\n",
821 if ((acdomstar = (
float *) calloc(nwave,
sizeof (
float))) ==
NULL) {
822 printf(
"-E- %s line %d : error allocating memory for GIOP:giop_model.\n",
826 if ((anapstar = (
float *) calloc(nwave,
sizeof (
float))) ==
NULL) {
827 printf(
"-E- %s line %d : error allocating memory for GIOP:giop_model.\n",
831 if ((bbphstar = (
float *) calloc(nwave,
sizeof (
float))) ==
NULL) {
832 printf(
"-E- %s line %d : error allocating memory for GIOP:giop_model.\n",
836 if ((bbnapstar = (
float *) calloc(nwave,
sizeof (
float))) ==
NULL) {
837 printf(
"-E- %s line %d : error allocating memory for GIOP:giop_model.\n",
844 switch (
g->fit_opt) {
846 if (
g->aph_nvec !=
g->acdom_nvec ||
g->aph_nvec !=
g->anap_nvec
847 ||
g->aph_nvec !=
g->bbph_nvec ||
g->aph_nvec !=
g->bbnap_nvec
848 ||
g->acdom_nvec !=
g->anap_nvec ||
g->acdom_nvec !=
g->bbph_nvec
849 ||
g->acdom_nvec !=
g->bbnap_nvec ||
g->anap_nvec !=
g->bbph_nvec
850 ||
g->anap_nvec !=
g->bbnap_nvec ||
g->bbph_nvec !=
g->bbnap_nvec) {
852 printf(
"-E- %s line %d: SIOP tables must have same number of columns.\n",
859 idx443 =
windex(443.,
g->acdom_tab_w,
g->acdom_tab_nw);
861 for (iw = 0; iw < nwave; iw++) {
868 iwtab =
windex(wave[iw],
g->aph_tab_w,
g->aph_tab_nw);
869 aphstar[iw] =
g->aph_tab_s[isiop][iwtab];
870 aph[iw] = par[0] * aphstar[iw];
871 aph[iw + nwave] = par[
g->npar] * aphstar[iw];
874 iwtab =
windex(wave[iw],
g->acdom_tab_w,
g->acdom_tab_nw);
875 acdomstar[iw] =
g->acdom_tab_s[isiop][iwtab];
876 acdom443 =
g->acdom_tab_s[isiop][idx443];
877 acdom[iw] = par[1]*(acdomstar[iw] / acdom443);
878 acdom[iw + nwave] = par[1 +
g->npar]*(aphstar[iw] / acdom443);
880 iwtab =
windex(wave[iw],
g->anap_tab_w,
g->anap_tab_nw);
881 anapstar[iw] =
g->anap_tab_s[isiop][iwtab];
882 anap[iw] = par[2] * anapstar[iw];
883 anap[iw + nwave] = par[2 +
g->npar] * aphstar[iw];
885 adg[iw] = par[1] * acdomstar[iw] + par[2] * anapstar[iw];
886 adg[iw + nwave] = par[1 +
g->npar] * acdomstar[iw] + par[2 +
g->npar] * anapstar[iw];
888 iwtab =
windex(wave[iw],
g->bbph_tab_w,
g->bbph_tab_nw);
889 bbphstar[iw] =
g->bbph_tab_s[isiop][iwtab];
890 bbph[iw] = par[0] * bbphstar[iw];
891 bbph[iw + nwave] = par[
g->npar] * bbphstar[iw];
893 iwtab =
windex(wave[iw],
g->bbnap_tab_w,
g->bbnap_tab_nw);
894 bbnapstar[iw] =
g->bbnap_tab_s[isiop][iwtab];
895 bbnap[iw] = par[2] * bbnapstar[iw];
896 bbnap[iw + nwave] = par[2 +
g->npar] * bbnapstar[iw];
898 bbp[iw] = par[0] * bbphstar[iw] + par[2] * bbnapstar[iw];
899 bbp[iw + nwave] = par[
g->npar] * bbphstar[iw] + par[2 +
g->npar] * bbnapstar[iw];
901 a = aw [iw] + aph[iw] + adg[iw];
902 bb = bbw[iw] + bbp[iw];
905 switch (
g->rrs_opt) {
907 rrs[iw] =
g->grd[0] *
x +
g->grd[1] * pow(
x, 2);
908 dfdx =
g->grd[0] + 2 *
g->grd[1] *
x;
911 rrs[iw] = foq[iw] *
x;
916 if (dfdpar !=
NULL) {
919 dxdb =
x / bb + dxda;
921 dfdpar[iw][0] = dfdx * dxda * aphstar[iw];
922 dfdpar[iw][1] = dfdx * dxda * acdomstar[iw];
923 dfdpar[iw][2] = dfdx * dxdb * anapstar[iw];
924 dfdpar[iw][3] = dfdx * dxdb * bbphstar[iw];
925 dfdpar[iw][4] = dfdx * dxdb * bbnapstar[iw];
930 if (parstar !=
NULL) {
932 parstar[iw][0] = aphstar[iw];
933 parstar[iw][1] = acdomstar[iw];
934 parstar[iw][2] = anapstar[iw];
935 parstar[iw][3] = bbphstar[iw];
936 parstar[iw][4] = bbnapstar[iw];
958 giopstr *
g = (giopstr *) (ambdata->
meta);
960 giop_model(
g, par,
g->nwave,
g->wave,
g->aw,
g->bbw,
g->foq, aph1, adg1, bbp1, ambdata->
yfit,
NULL,
NULL);
962 ambdata->
merit = 0.0;
963 for (iw = 0; iw <
g->nwave; iw++) {
964 ambdata->
merit += pow((ambdata->
y[iw] - ambdata->
yfit[iw]), 2) * ambdata->
wgt[iw];
967 return (ambdata->
merit);
976 double Rrs_fit[],
int16 *itercnt) {
980 static float tol = 1.e-6;
984 static int firstCall = 1;
989 ambdata.
niter =
g->maxiter;
994 ambdata.
yfit = Rrs_fit;
997 if (firstCall == 1) {
999 if ((
init = (
double *) calloc(
g->nwave * (
g->nwave + 1), sizeof (
double))) ==
NULL) {
1000 printf(
"-E- %s line %d : error allocating memory for GIOP:gio_model.\n",
1001 __FILE__, __LINE__);
1007 for (
j = 0;
j <
g->npar + 1;
j++)
1008 for (
i = 0;
i <
g->npar;
i++)
1011 for (
i = 0;
i <
g->npar;
i++) {
1012 init[
g->npar +
i * (
g->npar + 1)] +=
g->len[
i];
1020 if (ambdata.
niter >=
g->maxiter)
1023 for (
i = 0;
i <
g->npar;
i++) {
1024 par[
i] =
init[
g->npar * isml +
i];
1027 *itercnt = ambdata.
niter;
1039 double *
y = ((fitstr *)
data)->y;
1040 double *w = ((fitstr *)
data)->w;
1041 float *aw = ((fitstr *)
data)->g->aw;
1042 float *bbw = ((fitstr *)
data)->g->bbw;
1043 float *foq = ((fitstr *)
data)->g->foq;
1044 float *wave = ((fitstr *)
data)->g->wave;
1045 size_t nwave = ((fitstr *)
data)->g->nwave;
1046 size_t npar = ((fitstr *)
data)->g->npar;
1047 giopstr *
g = ((fitstr *)
data)->g;
1057 if ((par = (
double *) calloc(nwave,
sizeof (
double))) ==
NULL) {
1058 printf(
"-E- %s line %d : error allocating memory for GIOP:giop_lm_fdf.\n",
1059 __FILE__, __LINE__);
1064 for (ipar = 0; ipar < npar; ipar++) {
1065 par[ipar] = gsl_vector_get(parv, ipar);
1067 if ((yfit = (
double *) calloc(nwave,
sizeof (
double))) ==
NULL) {
1068 printf(
"-E- %s line %d : error allocating memory for GIOP:giop_lm_fdf.\n",
1069 __FILE__, __LINE__);
1072 if ((dydpar = (
double **) calloc(nwave,
sizeof (
double *))) ==
NULL) {
1073 printf(
"-E- %s line %d : error allocating memory for GIOP:giop_lm_fdf.\n",
1074 __FILE__, __LINE__);
1077 for (iw = 0; iw < nwave; iw++)
1078 if ((dydpar[iw] = (
double *) calloc(nwave,
sizeof (
double))) ==
NULL) {
1079 printf(
"-E- %s line %d : error allocating memory for GIOP:giop_lm_fdf.\n",
1080 __FILE__, __LINE__);
1085 giop_model(
g, par, nwave, wave, aw, bbw, foq, aph1, adg1, bbp1, yfit, dydpar,
NULL);
1088 for (iw = 0; iw < nwave; iw++) {
1091 sigma = sqrt(1. / w[iw]);
1095 gsl_vector_set(
f, iw, (yfit[iw] -
y[iw]) / sigma);
1099 for (ipar = 0; ipar < npar; ipar++)
1100 gsl_matrix_set(
J, iw, ipar, dydpar[iw][ipar] / sigma);
1129 size_t npar =
g->npar;
1131 static gsl_multifit_function_fdf
func;
1132 const gsl_multifit_fdfsolver_type *
t;
1133 gsl_multifit_fdfsolver *
s;
1136 gsl_matrix *
J = gsl_matrix_alloc(
g->nwave, npar);
1137 gsl_matrix *cov = gsl_matrix_alloc(npar, npar);
1156 t = gsl_multifit_fdfsolver_lmsder;
1157 s = gsl_multifit_fdfsolver_alloc(
t,
g->nwave,
g->npar);
1160 x = gsl_vector_view_array(par, npar);
1161 gsl_multifit_fdfsolver_set(
s, &
func, &
x.vector);
1165 for (iter = 0; iter <
g->maxiter; iter++) {
1166 gsl_multifit_fdfsolver_iterate(
s);
1167 if (gsl_multifit_test_delta(
s->dx,
s->x, 1e-4, 1e-4) == GSL_SUCCESS) {
1175 gsl_multifit_fdfsolver_jac(
s,
J);
1176 gsl_multifit_covar(
J, 0.0, cov);
1179 sum = gsl_blas_dnrm2(
s->f);
1181 *chi = pow(sum, 2.0) / dof;
1184 c = sum / sqrt(dof);
1189 for (ipar = 0; ipar < npar; ipar++) {
1190 par[ipar] = gsl_vector_get(
s->x, ipar);
1191 par[ipar + npar] = sqrt(gsl_matrix_get(cov, ipar, ipar)) *
c;
1195 gsl_multifit_fdfsolver_free(
s);
1196 gsl_matrix_free(cov);
1213 size_t nwave =
g->nwave;
1214 size_t npar =
g->npar;
1221 gsl_matrix *A = gsl_matrix_alloc(nwave, npar);
1222 gsl_matrix *V = gsl_matrix_alloc(npar, npar);
1223 gsl_vector *S = gsl_vector_alloc(npar);
1224 gsl_vector *W = gsl_vector_alloc(npar);
1225 gsl_vector *
x = gsl_vector_alloc(npar);
1226 gsl_vector *
b = gsl_vector_alloc(nwave);
1228 if ((rrs_fit = (
double *) calloc(nwave,
sizeof (
double))) ==
NULL) {
1229 printf(
"-E- %s line %d : error allocating memory for GIOP:fit_giop_svd.\n",
1230 __FILE__, __LINE__);
1233 if ((parstar = (
double **) calloc(nwave,
sizeof (
double *))) ==
NULL) {
1234 printf(
"-E- %s line %d : error allocating memory for GIOP:fit_giop_svd.\n",
1235 __FILE__, __LINE__);
1238 for (iw = 0; iw < nwave; iw++)
1239 if ((parstar[iw] = (
double *) calloc(nwave,
sizeof (
double))) ==
NULL) {
1240 printf(
"-E- %s line %d : error allocating memory for GIOP:fit_giop_svd.\n",
1241 __FILE__, __LINE__);
1246 giop_model(
g, par,
g->nwave,
g->wave,
g->aw,
g->bbw,
g->foq, aph1, adg1, bbp1, rrs_fit,
NULL, parstar);
1249 for (ipar = 0; ipar <
g->npar; ipar++)
1254 for (iw = 0; iw <
g->nwave; iw++) {
1257 switch (
g->rrs_opt) {
1262 if ((gsl_poly_solve_quadratic(a2, a1, a0, &u0, &u1) == 2) && u1 > 0.0)
1270 u = rrs[iw] /
g->foq[iw];
1274 gsl_vector_set(
b, iw, -(
u *
g->aw[iw] + (
u - 1.0) *
g->bbw[iw]));
1276 for (ipar = 0; ipar <
g->npar; ipar++) {
1277 if (ipar < (
g->aph_nvec +
g->adg_nvec))
1278 gsl_matrix_set(A, iw, ipar, parstar[iw][ipar] *
u);
1280 gsl_matrix_set(A, iw, ipar, parstar[iw][ipar]*(
u - 1.0));
1285 status = gsl_linalg_SV_decomp(A, V, S, W);
1286 status = gsl_linalg_SV_solve(A, V, S,
b,
x);
1290 for (ipar = 0; ipar <
g->npar; ipar++) {
1291 par[ipar] = gsl_vector_get(
x, ipar);
1334 size_t nwave =
g->nwave;
1335 size_t npar =
g->npar;
1336 size_t nvec =
g->aph_nvec;
1338 int iw, ipar, iv, smlIdx;
1343 double diffSq, diffSqSum, smlRmse, sumRrs;
1345 gsl_matrix *A = gsl_matrix_alloc(nwave, npar);
1346 gsl_matrix *V = gsl_matrix_alloc(npar, npar);
1347 gsl_vector *S = gsl_vector_alloc(npar);
1348 gsl_vector *W = gsl_vector_alloc(npar);
1349 gsl_vector *
x = gsl_vector_alloc(npar);
1350 gsl_vector *
b = gsl_vector_alloc(nwave);
1353 if ((rrs_fit = (
double *) calloc(nwave,
sizeof (
double))) ==
NULL) {
1354 printf(
"-E- %s line %d : error allocating memory for GIOP:fit_giop_svd.\n",
1355 __FILE__, __LINE__);
1358 if ((parstar = (
double **) calloc(nwave,
sizeof (
double *))) ==
NULL) {
1359 printf(
"-E- %s line %d : error allocating memory for GIOP:fit_giop_svd.\n",
1360 __FILE__, __LINE__);
1363 for (iw = 0; iw < nwave; iw++) {
1364 if ((parstar[iw] = (
double *) calloc(5,
sizeof (
double))) ==
NULL) {
1365 printf(
"-E- %s line %d : error allocating memory for GIOP:fit_giop_svd.\n",
1366 __FILE__, __LINE__);
1370 if ((parArr = (
double *) calloc(npar * nvec,
sizeof (
double *))) ==
NULL) {
1371 printf(
"-E- %s line %d : error allocating memory for GIOP:fit_giop_svd.\n",
1372 __FILE__, __LINE__);
1375 if ((
rmse = (
double *) calloc(nvec,
sizeof (
double *))) ==
NULL) {
1376 printf(
"-E- %s line %d : error allocating memory for GIOP:fit_giop_svd.\n",
1377 __FILE__, __LINE__);
1380 if ((badSolution = (
int *) calloc(nvec,
sizeof (
int *))) ==
NULL) {
1381 printf(
"-E- %s line %d : error allocating memory for GIOP:fit_giop_svd.\n",
1382 __FILE__, __LINE__);
1385 if ((parFit = (
double *) calloc(npar,
sizeof (
double *))) ==
NULL) {
1386 printf(
"-E- %s line %d : error allocating memory for GIOP:fit_giop_svd.\n",
1387 __FILE__, __LINE__);
1392 for (ipar = 0; ipar <
g->npar; ipar++)
1395 for (ipar = 0; ipar < 3; ipar++)
1400 for (iv = 0; iv < nvec; iv++)
1405 for (iv = 0; iv < nvec; iv++) {
1410 giop_model_iterate(
g, parFit,
g->nwave,
g->wave,
g->aw,
g->bbw,
g->foq, aph1, adg1, bbp1, acdom1, anap1, bbph1, bbnap1, rrs_fit,
NULL, parstar);
1413 for (iw = 0; iw <
g->nwave; iw++) {
1416 switch (
g->rrs_opt) {
1421 if ((gsl_poly_solve_quadratic(a2, a1, a0, &u0, &u1) == 2) && u1 > 0.0)
1429 u = rrs[iw] /
g->foq[iw];
1433 gsl_vector_set(
b, iw, -(
g->aw[iw] *
u +
g->bbw[iw]*(
u - 1.0)));
1440 gsl_matrix_set(A, iw, 0, parstar[iw][0] *
u + parstar[iw][3]*(
u - 1.0));
1441 gsl_matrix_set(A, iw, 1, parstar[iw][1] *
u);
1442 gsl_matrix_set(A, iw, 2, parstar[iw][2] *
u + parstar[iw][4]*(
u - 1.0));
1448 status = gsl_linalg_SV_decomp(A, V, S, W);
1449 status = gsl_linalg_SV_solve(A, V, S,
b,
x);
1452 for (ipar = 0; ipar < npar; ipar++) {
1456 if (gsl_vector_get(
x, ipar) < 0.0 ||
status != 0) {
1457 badSolution[iv] = 1;
1458 for (ipar = 0; ipar < npar; ipar++) {
1459 parArr[iv * npar + ipar] =
BAD_FLT;
1463 }
else if (gsl_vector_get(
x, ipar) >= 0.0) {
1464 badSolution[iv] = 0;
1465 parArr[iv * npar + ipar] = gsl_vector_get(
x, ipar);
1477 for (iv = 0; iv < nvec; iv++) {
1481 for (ipar = 0; ipar < npar; ipar++) {
1485 parFit[ipar] = parArr[iv * npar + ipar];
1490 giop_model_iterate(
g, parFit,
g->nwave,
g->wave,
g->aw,
g->bbw,
g->foq, aph1, adg1, bbp1, acdom1, anap1, bbph1, bbnap1, rrs_fit,
NULL, parstar);
1497 for (iw = 0; iw <
g->nwave; iw++) {
1498 diffSq = pow((rrs_fit[iw] - rrs[iw]), 2);
1499 diffSqSum += diffSq;
1504 if (badSolution[iv]) {
1508 rmse[iv] = pow((diffSqSum / (
g->nwave - 1)), 0.5);
1517 for (iv = 0; iv < nvec; iv++) {
1518 if (
rmse[iv] < smlRmse) {
1526 g->siopIdx = smlIdx;
1530 if (
rmse[smlIdx] != 999 && badSolution[smlIdx] != 1) {
1532 for (ipar = 0; ipar < npar; ipar++) {
1533 par[ipar] = parArr[smlIdx * npar + ipar];
1541 for (ipar = 0; ipar < npar; ipar++) {
1570 float32 chl = badval;
1575 switch (
g->aph_opt) {
1582 for (ipar = 0; ipar <
g->aph_nvec; ipar++) {
1584 *chl_err += par[ipar +
g->npar];
1599 return (
Rrs / (0.52 + 1.7 *
Rrs));
1608 return ( (0.52 * rrs_s) / (1.0 - 1.7 * rrs_s));
1617 static int firstCall = 1;
1618 static giopstr giopctl;
1619 static giopstr *
g = &giopctl;
1634 int32 ipar, ip, iw, ib, ipb, ipb2, ierr;
1637 l1str *
l1rec = l2rec->l1rec;
1639 float *wave =
l1rec->l1file->fwave;
1640 int32 nwave =
l1rec->l1file->nbands;
1652 if ((bbw = calloc(nwave,
sizeof (
float))) ==
NULL) {
1653 printf(
"-E- %s line %d : error allocating memory for GIOP:run_giop.\n",
1654 __FILE__, __LINE__);
1657 if ((aw = calloc(nwave,
sizeof (
float))) ==
NULL) {
1658 printf(
"-E- %s line %d : error allocating memory for GIOP:run_giop.\n",
1659 __FILE__, __LINE__);
1662 if ((foq = calloc(nwave,
sizeof (
float))) ==
NULL) {
1663 printf(
"-E- %s line %d : error allocating memory for GIOP:run_giop.\n",
1664 __FILE__, __LINE__);
1667 if ((aph1 = calloc(2 * nwave,
sizeof (
float))) ==
NULL) {
1668 printf(
"-E- %s line %d : error allocating memory for GIOP:run_giop.\n",
1669 __FILE__, __LINE__);
1672 if ((acdom1 = calloc(2 * nwave,
sizeof (
float))) ==
NULL) {
1673 printf(
"-E- %s line %d : error allocating memory for GIOP:run_giop.\n",
1674 __FILE__, __LINE__);
1677 if ((anap1 = calloc(2 * nwave,
sizeof (
float))) ==
NULL) {
1678 printf(
"-E- %s line %d : error allocating memory for GIOP:run_giop.\n",
1679 __FILE__, __LINE__);
1682 if ((adg1 = calloc(2 * nwave,
sizeof (
float))) ==
NULL) {
1683 printf(
"-E- %s line %d : error allocating memory for GIOP:run_giop.\n",
1684 __FILE__, __LINE__);
1687 if ((bbph1 = calloc(2 * nwave,
sizeof (
float))) ==
NULL) {
1688 printf(
"-E- %s line %d : error allocating memory for GIOP:run_giop.\n",
1689 __FILE__, __LINE__);
1692 if ((bbnap1 = calloc(2 * nwave,
sizeof (
float))) ==
NULL) {
1693 printf(
"-E- %s line %d : error allocating memory for GIOP:run_giop.\n",
1694 __FILE__, __LINE__);
1697 if ((bbp1 = calloc(2 * nwave,
sizeof (
float))) ==
NULL) {
1698 printf(
"-E- %s line %d : error allocating memory for GIOP:run_giop.\n",
1699 __FILE__, __LINE__);
1702 if ((
g->wave = (
float *) calloc(nwave,
sizeof (
float))) ==
NULL) {
1703 printf(
"-E- %s line %d : error allocating memory in init_iop_flag.\n",
1704 __FILE__, __LINE__);
1707 if ((
g->bindx = (
int *) calloc(nwave,
sizeof (
int))) ==
NULL) {
1708 printf(
"-E- %s line %d : error allocating memory in init_iop_flag.\n",
1709 __FILE__, __LINE__);
1712 if ((
g->aw = (
float *) calloc(nwave,
sizeof (
float))) ==
NULL) {
1713 printf(
"-E- %s line %d : error allocating memory in init_iop_flag.\n",
1714 __FILE__, __LINE__);
1717 if ((
g->bbw = (
float *) calloc(nwave,
sizeof (
float))) ==
NULL) {
1718 printf(
"-E- %s line %d : error allocating memory in init_iop_flag.\n",
1719 __FILE__, __LINE__);
1722 if ((
g->wts = (
float *) calloc(nwave,
sizeof (
float))) ==
NULL) {
1723 printf(
"-E- %s line %d : error allocating memory in init_iop_flag.\n",
1724 __FILE__, __LINE__);
1727 if ((
g->foq = (
float *) calloc(nwave,
sizeof (
float))) ==
NULL) {
1728 printf(
"-E- %s line %d : error allocating memory in init_iop_flag.\n",
1729 __FILE__, __LINE__);
1732 if ((
g->par = (
double *) calloc(nwave,
sizeof (
double))) ==
NULL) {
1733 printf(
"-E- %s line %d : error allocating memory in init_iop_flag.\n",
1734 __FILE__, __LINE__);
1737 if ((
g->len = (
double *) calloc(nwave,
sizeof (
double))) ==
NULL) {
1738 printf(
"-E- %s line %d : error allocating memory in init_iop_flag.\n",
1739 __FILE__, __LINE__);
1749 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1750 __FILE__, __LINE__);
1754 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1755 __FILE__, __LINE__);
1758 if ((mRrs = calloc(
npix * nwave,
sizeof (
float))) ==
NULL) {
1759 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1760 __FILE__, __LINE__);
1763 if ((
a = calloc(
npix * nwave * 2,
sizeof (
float))) ==
NULL) {
1764 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1765 __FILE__, __LINE__);
1768 if ((aph = calloc(
npix * nwave * 2,
sizeof (
float))) ==
NULL) {
1769 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1770 __FILE__, __LINE__);
1773 if ((acdom = calloc(
npix * nwave * 2,
sizeof (
float))) ==
NULL) {
1774 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1775 __FILE__, __LINE__);
1778 if ((anap = calloc(
npix * nwave * 2,
sizeof (
float))) ==
NULL) {
1779 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1780 __FILE__, __LINE__);
1783 if ((adg = calloc(
npix * nwave * 2,
sizeof (
float))) ==
NULL) {
1784 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1785 __FILE__, __LINE__);
1788 if ((bbph = calloc(
npix * nwave * 2,
sizeof (
float))) ==
NULL) {
1789 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1790 __FILE__, __LINE__);
1793 if ((bbnap = calloc(
npix * nwave * 2,
sizeof (
float))) ==
NULL) {
1794 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1795 __FILE__, __LINE__);
1798 if ((bbp = calloc(
npix * nwave * 2,
sizeof (
float))) ==
NULL) {
1799 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1800 __FILE__, __LINE__);
1803 if ((bb = calloc(
npix * nwave * 2,
sizeof (
float))) ==
NULL) {
1804 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1805 __FILE__, __LINE__);
1808 if ((chl = calloc(
npix * 2,
sizeof (
float))) ==
NULL) {
1809 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1810 __FILE__, __LINE__);
1813 if ((rrsdiff = calloc(
npix,
sizeof (
float))) ==
NULL) {
1814 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1815 __FILE__, __LINE__);
1818 if ((aph_s = calloc(
npix,
sizeof (
float))) ==
NULL) {
1819 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1820 __FILE__, __LINE__);
1823 if ((adg_s = calloc(
npix,
sizeof (
float))) ==
NULL) {
1824 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1825 __FILE__, __LINE__);
1828 if ((bbp_s = calloc(
npix,
sizeof (
float))) ==
NULL) {
1829 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1830 __FILE__, __LINE__);
1834 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1835 __FILE__, __LINE__);
1838 if ((siop_num = calloc(
npix,
sizeof (
int))) ==
NULL) {
1839 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1840 __FILE__, __LINE__);
1844 if ((chisqr = calloc(
npix,
sizeof (
float))) ==
NULL) {
1845 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1846 __FILE__, __LINE__);
1853 if (l2rec->Rrs_raman ==
NULL) {
1856 printf(
"No Raman scattering correction applied to Rrs. \n");
1859 allocateRrsRaman = 1;
1861 l1rec->l1file->nbands * sizeof (
float),
"Rrs_ram");
1866 if ((par = calloc(2 * nwave,
sizeof (
double))) ==
NULL) {
1867 printf(
"-E- %s line %d : error allocating memory for GIOP:run_giop.\n",
1868 __FILE__, __LINE__);
1895 if (allocateRrsRaman) {
1896 free(l2rec->Rrs_raman);
1898 l1rec->l1file->nbands * sizeof (
float),
"Rrs_ram");
1902 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1903 __FILE__, __LINE__);
1907 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1908 __FILE__, __LINE__);
1911 if ((mRrs = calloc(
npix * nwave,
sizeof (
float))) ==
NULL) {
1912 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1913 __FILE__, __LINE__);
1916 if ((
a = calloc(
npix * nwave * 2,
sizeof (
float))) ==
NULL) {
1917 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1918 __FILE__, __LINE__);
1921 if ((aph = calloc(
npix * nwave * 2,
sizeof (
float))) ==
NULL) {
1922 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1923 __FILE__, __LINE__);
1926 if ((acdom = calloc(
npix * nwave * 2,
sizeof (
float))) ==
NULL) {
1927 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1928 __FILE__, __LINE__);
1931 if ((anap = calloc(
npix * nwave * 2,
sizeof (
float))) ==
NULL) {
1932 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1933 __FILE__, __LINE__);
1936 if ((adg = calloc(
npix * nwave * 2,
sizeof (
float))) ==
NULL) {
1937 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1938 __FILE__, __LINE__);
1941 if ((bbph = calloc(
npix * nwave * 2,
sizeof (
float))) ==
NULL) {
1942 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1943 __FILE__, __LINE__);
1946 if ((bbnap = calloc(
npix * nwave * 2,
sizeof (
float))) ==
NULL) {
1947 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1948 __FILE__, __LINE__);
1951 if ((bbp = calloc(
npix * nwave * 2,
sizeof (
float))) ==
NULL) {
1952 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1953 __FILE__, __LINE__);
1956 if ((bb = calloc(
npix * nwave * 2,
sizeof (
float))) ==
NULL) {
1957 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1958 __FILE__, __LINE__);
1961 if ((chl = calloc(
npix * 2,
sizeof (
float))) ==
NULL) {
1962 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1963 __FILE__, __LINE__);
1966 if ((rrsdiff = calloc(
npix,
sizeof (
float))) ==
NULL) {
1967 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1968 __FILE__, __LINE__);
1971 if ((aph_s = calloc(
npix,
sizeof (
float))) ==
NULL) {
1972 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1973 __FILE__, __LINE__);
1976 if ((adg_s = calloc(
npix,
sizeof (
float))) ==
NULL) {
1977 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1978 __FILE__, __LINE__);
1981 if ((bbp_s = calloc(
npix,
sizeof (
float))) ==
NULL) {
1982 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1983 __FILE__, __LINE__);
1987 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1988 __FILE__, __LINE__);
1991 if ((siop_num = calloc(
npix,
sizeof (
int))) ==
NULL) {
1992 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1993 __FILE__, __LINE__);
1997 if ((chisqr = calloc(
npix,
sizeof (
float))) ==
NULL) {
1998 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
1999 __FILE__, __LINE__);
2005 if ((Rrs_a = calloc(nwave,
sizeof (
double))) ==
NULL) {
2006 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
2007 __FILE__, __LINE__);
2010 if ((Rrs_b = calloc(nwave,
sizeof (
double))) ==
NULL) {
2011 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
2012 __FILE__, __LINE__);
2015 if ((Rrs_f = calloc(nwave,
sizeof (
double))) ==
NULL) {
2016 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
2017 __FILE__, __LINE__);
2020 if ((wts = calloc(nwave,
sizeof (
double))) ==
NULL) {
2021 printf(
"-E- %s line %d : error allocating memory for GIOP.\n",
2022 __FILE__, __LINE__);
2028 if (
input->giop_iterate > 0) {
2029 g->adg_s =
input->giop_adg_s;
2030 g->bbp_s =
input->giop_bbp_s;
2034 for (ip = 0; ip <
l1rec->npix; ip++) {
2037 ipb2 = ip *
l1rec->l1file->nbands;
2038 ierr =
l1rec->npix * nwave + ipb;
2042 for (ib = 0; ib < nwave; ib++) {
2043 a [ipb + ib] = badval;
2044 bb [ipb + ib] = badval;
2045 aph [ipb + ib] = badval;
2046 adg [ipb + ib] = badval;
2047 acdom [ipb + ib] = badval;
2048 anap[ipb + ib] = badval;
2049 bbp [ipb + ib] = badval;
2050 bbph[ipb + ib] = badval;
2051 bbnap [ipb + ib] = badval;
2052 mRrs[ipb + ib] = badval;
2053 a [ierr + ib] = 0.0;
2054 bb [ierr + ib] = 0.0;
2055 aph [ierr + ib] = 0.0;
2056 adg [ierr + ib] = 0.0;
2057 acdom [ierr + ib] = 0.0;
2058 anap [ierr + ib] = 0.0;
2059 bbp [ierr + ib] = 0.0;
2060 bbph [ierr + ib] = 0.0;
2061 bbnap [ierr + ib] = 0.0;
2069 rrsdiff[ip] = badval;
2070 chisqr[ip] = badval;
2077 for (ipar = 0; ipar <
g->npar; ipar++)
2078 fit_par[ip][ipar] = badval;
2083 if (
l1rec->mask[ip]) {
2091 for (iw = 0; iw < nwave; iw++) {
2092 aw [iw] =
l1rec->sw_a [ipb2 + iw];
2093 bbw[iw] =
l1rec->sw_bb[ipb2 + iw];
2095 for (iw = 0; iw <
g->nwave; iw++) {
2096 g->aw [iw] = aw [
g->bindx[iw]];
2097 g->bbw[iw] = bbw [
g->bindx[iw]];
2104 aph_s[ip] =
g->aph_s;
2105 adg_s[ip] =
g->adg_s;
2106 bbp_s[ip] =
g->bbp_s;
2110 g->chl =
MAX(
MIN(l2rec->chl[ip], chl_max), chl_min);
2114 switch (
g->rrs_opt) {
2117 for (iw = 0; iw <
g->nwave; iw++) {
2118 g->foq[iw] = foq[
g->bindx[iw]];
2125 switch (
g->aph_opt) {
2128 for (iw = 0; iw < nwave; iw++) {
2129 g->aph_tab_w[iw] = wave[iw];
2135 g->aph_tab_nw = nwave;
2140 for (iw = 0; iw < nwave; iw++) {
2141 g->aph_tab_w[iw] = wave[iw];
2144 g->aph_tab_nw = nwave;
2151 switch (
g->adg_opt) {
2154 i1 =
windex(443.0, wave, nwave);
2155 i2 =
windex(550.0, wave, nwave);
2156 Rrs1 = l2rec->Rrs[ipb2 + i1] - l2rec->Rrs_raman[ipb2 + i1];
2157 Rrs2 = l2rec->Rrs[ipb2 + i2] - l2rec->Rrs_raman[ipb2 + i2];
2158 if (Rrs1 > 0.0 && Rrs2 > 0.0) {
2161 g->adg_s =
MAX(
MIN(0.015 + 0.002 / (0.6 + Rrs1 / Rrs2), adg_s_max), adg_s_min);
2162 }
else if (Rrs2 > 0.0) {
2163 g->adg_s = adg_s_min;
2172 i1 =
windex(412.0, wave, nwave);
2173 i2 =
windex(550.0, wave, nwave);
2174 Rrs1 = l2rec->Rrs[ipb2 + i1] - l2rec->Rrs_raman[ipb2 + i1];
2175 Rrs2 = l2rec->Rrs[ipb2 + i2] - l2rec->Rrs_raman[ipb2 + i2];
2176 if (Rrs1 > 0.0 && Rrs2 > 0.0) {
2177 g->adg_s =
MAX(
MIN(0.015 + 0.0038 * log10(Rrs1 / Rrs2), adg_s_max), adg_s_min);
2178 }
else if (Rrs2 > 0.0) {
2179 g->adg_s = adg_s_min;
2190 switch (
g->bbp_opt) {
2193 i1 =
windex(490.0, wave, nwave);
2194 i2 =
windex(550.0, wave, nwave);
2195 Rrs1 = l2rec->Rrs[ipb2 + i1] - l2rec->Rrs_raman[ipb2 + i1];
2196 Rrs2 = l2rec->Rrs[ipb2 + i2] - l2rec->Rrs_raman[ipb2 + i2];
2197 if (Rrs1 > 0.0 && Rrs2 > 0.0) {
2198 g->bbp_s =
MAX(
MIN(0.8 * (Rrs1 / Rrs2) + 0.2, bbp_s_max), bbp_s_min);
2199 }
else if (Rrs2 > 0.0) {
2200 g->bbp_s = bbp_s_min;
2209 i1 =
windex(443.0, wave, nwave);
2210 i2 =
windex(550.0, wave, nwave);
2211 Rrs1 = l2rec->Rrs[ipb2 + i1] - l2rec->Rrs_raman[ipb2 + i1];
2212 Rrs2 = l2rec->Rrs[ipb2 + i2] - l2rec->Rrs_raman[ipb2 + i2];
2213 if (Rrs1 > 0.0 && Rrs2 > 0.0) {
2216 g->bbp_s =
MAX(
MIN(2.0 * (1.0 - 1.2 * exp(-0.9 * Rrs1 / Rrs2)), bbp_s_max), bbp_s_min);
2217 }
else if (Rrs2 > 0.0) {
2218 g->bbp_s = bbp_s_min;
2227 if (l2rec->chl[ip] > 0.0) {
2228 g->bbp_s =
MAX(
MIN(1.0 - 0.768 * log10(
g->chl), bbp_s_max), bbp_s_min);
2237 if (l2rec->chl[ip] > 0.0) {
2238 g->bbp_s =
MAX(
MIN(0.5 * (0.3 - log10(
g->chl)), bbp_s_max), bbp_s_min);
2256 if (
get_bbp_las(l2rec, ip,
g->bbp_tab_w,
g->bbp_tab_s[0],
g->bbp_tab_nw) == 0) {
2264 if (
get_bbp_qaa(l2rec, ip,
g->bbp_tab_w,
g->bbp_tab_s[0],
g->bbp_tab_nw) == 0) {
2274 aph_s[ip] =
g->aph_s;
2275 adg_s[ip] =
g->adg_s;
2276 bbp_s[ip] =
g->bbp_s;
2285 for (iw = 0; iw <
g->nwave; iw++) {
2287 Rrs_a[iw] = l2rec->Rrs[ipb2 + ib] - l2rec->Rrs_raman[ipb2 + ib];
2289 wts [iw] =
g->wts[iw];
2290 if (Rrs_b[iw] > 0.0) {
2297 if (bndcnt < g->npar) {
2306 for (ipar = 0; ipar <
g->npar; ipar++) {
2307 par[ipar] =
g->par[ipar];
2308 par[ipar +
g->npar] = 0.0;
2313 switch (
g->fit_opt) {
2331 printf(
"%s Line %d: Unknown optimization method for GIOP %d\n",
2332 __FILE__, __LINE__,
g->fit_opt);
2342 if (itercnt >=
g->maxiter)
2349 for (ipar = 0; ipar <
g->npar; ipar++) {
2350 if (isfinite(par[ipar]))
2351 fit_par[ip][ipar] = par[ipar];
2358 switch (
g->fit_opt) {
2360 giop_model_iterate(
g, par,
g->nwave,
g->wave,
g->aw,
g->bbw,
g->foq, aph1, adg1, bbp1, acdom1, anap1, bbph1, bbnap1, Rrs_f,
NULL,
NULL);
2363 giop_model(
g, par,
g->nwave,
g->wave,
g->aw,
g->bbw,
g->foq, aph1, adg1, bbp1, Rrs_f,
NULL,
NULL);
2370 for (iw = 0; iw <
g->nwave; iw++) {
2371 if (!isfinite(Rrs_f[iw])) {
2384 for (iw = 0; iw <
g->nwave; iw++) {
2385 if (
g->wave[iw] >= 400 &&
g->wave[iw] <= 600) {
2386 if (
fabs(Rrs_b[iw]) > 1e-7 &&
fabs(Rrs_f[iw] - Rrs_b[iw]) > 1e-5)
2387 rrsdiff[ip] +=
fabs(Rrs_f[iw] - Rrs_b[iw]) /
fabs(Rrs_b[iw]);
2390 rrsdiff[ip] /=
g->nwave;
2391 if (rrsdiff[ip] >
input->giop_rrs_diff) {
2398 switch (
g->fit_opt) {
2400 giop_model_iterate(
g, par, nwave, wave, aw, bbw, foq, aph1, adg1, bbp1, acdom1, anap1, bbph1, bbnap1, Rrs_f,
NULL,
NULL);
2404 for (ib = 0; ib < nwave; ib++) {
2406 if (isfinite(aph1[ib])) {
2407 aph[ipb + ib] = aph1[ib];
2408 aph[ierr + ib] = aph1[ib + nwave];
2412 if (isfinite(acdom1[ib])) {
2413 acdom[ipb + ib] = acdom1[ib];
2414 acdom[ierr + ib] = acdom1[ib + nwave];
2418 if (isfinite(anap1[ib])) {
2419 anap[ipb + ib] = anap1[ib];
2420 anap[ierr + ib] = anap1[ib + nwave];
2424 if (isfinite(aph1[ib]) && isfinite(acdom1[ib]) && isfinite(anap1[ib])) {
2425 adg[ipb + ib] = acdom1[ib] + anap1[ib];
2426 adg[ierr + ib] = adg1[ib + nwave] + acdom1[ib + nwave];
2427 a[ipb + ib] = aw[ib] + aph1[ib] + acdom1[ib] + anap1[ib];
2428 a[ierr + ib] = aph1[ib + nwave] + adg1[ib + nwave] + acdom1[ib + nwave];
2432 if (isfinite(bbnap1[ib])) {
2433 bbnap [ipb + ib] = bbnap1[ib];
2434 bbnap [ierr + ib] = bbnap1[ib + nwave];
2438 if (isfinite(bbph1[ib])) {
2439 bbph [ipb + ib] = bbph1[ib];
2440 bbph [ierr + ib] = bbph1[ib + nwave];
2444 if (isfinite(bbph1[ib]) && isfinite(bbnap1[ib])) {
2445 bbp[ipb + ib] = bbph1[ib] + bbnap1[ib];
2446 bbp[ierr + ib] = bbph1[ib + nwave] + bbnap1[ib + nwave];
2447 bb [ipb + ib] = bbw[ib] + bbph1[ib] + bbnap1[ib];
2448 bb [ierr + ib] = bbw[ib + nwave] + bbph1[ib + nwave] + bbnap[ib + nwave];
2456 &bb[ipb], &bbp[ipb], &iopf[ip]);
2459 siop_num[ip] = 1 +
g->siopIdx;
2468 giop_model(
g, par, nwave, wave, aw, bbw, foq, aph1, adg1, bbp1, Rrs_f,
NULL,
NULL);
2470 for (ib = 0; ib < nwave; ib++) {
2474 if (isfinite(aph1[ib])) {
2475 aph[ipb + ib] = aph1[ib];
2476 aph[ierr + ib] = aph1[ib + nwave];
2480 if (isfinite(adg1[ib])) {
2481 adg[ipb + ib] = adg1[ib];
2482 adg[ierr + ib] = adg1[ib + nwave];
2486 if (isfinite(aph1[ib]) && isfinite(adg1[ib])) {
2487 a[ipb + ib] = aw[ib] + aph1[ib] + adg1[ib];
2488 a[ierr + ib] = aph1[ib + nwave] + adg1[ib + nwave];
2493 if (isfinite(bbp1[ib])) {
2494 bbp[ipb + ib] = bbp1[ib];
2495 bbp[ierr + ib] = bbp1[ib + nwave];
2496 bb [ipb + ib] = bbw[ib] + bbp1[ib];
2497 bb [ierr + ib] = bbp1[ib + nwave];
2505 &bb[ipb], &bbp[ipb], &iopf[ip]);
2521 for (ip = 0; ip <
l1rec->npix; ip++)
2525 LastRecNum =
l1rec->iscan;
2539 int prodID =
p->cat_ix;
2540 int ib =
p->prod_ix;
2542 int32_t ip, ipb, ierr;
2543 l1str *
l1rec = l2rec->l1rec;
2547 switch (
input->giop_fit_opt) {
2560 printf(
"-E- %s line %d : products acdom, anap, bbph and bbnap are only applicable with giop_fit_opt=SVDSIOP.\n",
2561 __FILE__, __LINE__);
2571 for (ip = 0; ip <
l1rec->npix; ip++) {
2573 ipb = ip *
l1rec->l1file->nbands + ib;
2574 ierr =
l1rec->npix *
l1rec->l1file->nbands + ipb;
2579 prod[ip] = (
float) mRrs[ipb];
2583 prod[ip] = (
float) aph[ipb];
2587 prod[ip] = (
float) adg[ipb];
2591 prod[ip] = (
float) bbp[ipb];
2595 prod[ip] = (
float)
a[ipb];
2599 prod[ip] = (
float) bb[ipb];
2603 prod[ip] = (
float) acdom[ipb];
2607 prod[ip] = (
float) anap[ipb];
2611 prod[ip] = (
float) bbph[ipb];
2615 prod[ip] = (
float) bbnap[ipb];
2619 prod[ip] = (
float) chl[ip];
2623 prod[ip] = (
int) siop_num[ip];
2627 prod[ip] = (
float) aph[ierr];
2631 prod[ip] = (
float) adg[ierr];
2635 prod[ip] = (
float) adg[ierr];
2639 prod[ip] = (
float) adg[ierr];
2643 prod[ip] = (
float) bbp[ierr];
2647 prod[ip] = (
float) bbph[ierr];
2651 prod[ip] = (
float) bbnap[ierr];
2655 prod[ip] = (
float)
a[ierr];
2659 prod[ip] = (
float) bb[ierr];
2667 prod[ip] = (
float) aph_s[ip];
2671 prod[ip] = (
float) adg_s[ip];
2675 prod[ip] = (
float) bbp_s[ip];
2679 prod[ip] = (
float) rrsdiff[ip];
2683 prod[ip] = (
float) chisqr[ip];
2687 if (ib >= max_npar) {
2688 printf(
"-E- %s line %d : output request for GIOP fit parameter %d exceeds number of fit parameters %d.\n",
2689 __FILE__, __LINE__, ib, max_npar);
2692 prod[ip] = (
float) fit_par[ip][ib];
2696 printf(
"-E- %s line %d : erroneous product ID %d passed to GIOP.\n",
2697 __FILE__, __LINE__, prodID);
2711 if (!
giop_ran(l2rec->l1rec->iscan))
2723 if (!
giop_ran(l2rec->l1rec->iscan))
2735 int32_t ib, ip, ipb, ipb2;
2737 int32_t
nbands = l2rec->l1rec->l1file->nbands;
2738 int32_t
npix = l2rec->l1rec->npix;
2740 if (!
giop_ran(l2rec->l1rec->iscan))
2743 for (ip = 0; ip <
npix; ip++) {
2744 for (ib = 0; ib <
nbands; ib++) {
2747 l2rec->a [ipb2] = (
float)
a[ipb];
2748 l2rec->bb[ipb2] = (
float) bb[ipb];