32 float BandShift(ccstr*, l2str*, int32_t,
float);
36 float Get_bb(l2str*, ccstr*, int32_t,
int,
char);
37 float Get_a(l2str*, ccstr*, int32_t,
int,
char);
39 void Setup(l2str*, ccstr*);
46 static int nband_vc = 7;
47 static float vc_bands[7] = {412., 443., 490., 510., 531., 555., 670.};
48 static int current_iscan = -1;
49 static int current_npix = -1;
50 static float *sh_prod_arr, *adg_ref, *bbp_ref;
54 int32_t ibvc =
p->prod_ix;
57 static int firstRun = 1;
66 if (l2rec->l1rec->iscan != current_iscan) {
67 current_iscan = l2rec->l1rec->iscan;
68 current_npix = l2rec->l1rec->npix;
71 CheckNULL(adg_ref = (
float*) malloc(current_npix *
sizeof (
float)),
72 "adg_ref", __LINE__, __FILE__);
75 CheckNULL(bbp_ref = (
float*) malloc(current_npix *
sizeof (
float)),
76 "bbp_ref", __LINE__, __FILE__);
77 if (sh_prod_arr !=
NULL)
79 CheckNULL(sh_prod_arr = (
float*) malloc(current_npix * nband_vc *
sizeof (
float))
80 ,
"sh_prod_arr", __LINE__, __FILE__);
84 for (ip = 0; ip < current_npix; ip++) {
85 for (ib = 0; ib < nband_vc; ib++) {
87 if (vcc.ibvc_bin[ib]) {
88 *(sh_prod_arr + ip * nband_vc + ib) =
BandShift(&vcc, l2rec, ip, vc_bands[ib]);
91 ipb = ip * l2rec->l1rec->l1file->nbands +
bindex_get((int32_t) vc_bands[ib]);
92 *(sh_prod_arr + ip * nband_vc + ib) = l2rec->Rrs[ipb];
98 for (ip = 0; ip < l2rec->l1rec->npix; ip++)
99 prod[ip] = *(sh_prod_arr + ip * nband_vc + ibvc);
103 void Setup(l2str *l2rec, ccstr *vccp) {
106 float lambda_i[4][5] = {
107 {510., 555., -1., -1., -1.},
108 {488., 488., 531., 547., 667.},
109 {510., 560., 560., 665., -1.},
110 {488., 488., 555., 555., 670.}
113 float lambda_o[4][5] = {
114 {531., 531., -1., -1., -1.},
115 {490., 510., 510., 555., 670.},
116 {531., 531., 555., 670., -1.},
117 {490., 510., 510., 531., 670.}
120 int ibvc_bin[4][7] = {
121 {0, 0, 0, 0, 1, 0, 0},
122 {0, 0, 1, 1, 0, 1, 1},
123 {0, 0, 0, 0, 1, 1, 1},
124 {0, 0, 1, 1, 1, 0, 1}
127 switch (l2rec->l1rec->l1file->sensorID) {
130 vccp->nBandPairs = 2;
131 vccp->greenband = 555;
138 vccp->nBandPairs = 5;
139 vccp->greenband = 547;
146 vccp->nBandPairs = 5;
147 vccp->greenband = 560;
153 vccp->nBandPairs = 5;
154 vccp->greenband = 555;
160 CheckNULL(vccp->ibvc_bin = (
int*) malloc(nband_vc *
sizeof (
int)),
"vccp->ibvc_bin", __LINE__, __FILE__);
161 CheckNULL(vccp->wvl_i = (
float*) malloc(vccp->nBandPairs * sizeof (
float)),
"vccp->wvl_i", __LINE__, __FILE__);
162 CheckNULL(vccp->wvl_o = (
float*) malloc(vccp->nBandPairs * sizeof (
float)),
"vccp->wvl_o", __LINE__, __FILE__);
163 CheckNULL(vccp->aw_i = (
float*) malloc(vccp->nBandPairs * sizeof (
float)),
"vccp->aw_i", __LINE__, __FILE__);
164 CheckNULL(vccp->aw_o = (
float*) malloc(vccp->nBandPairs * sizeof (
float)),
"vccp->aw_o", __LINE__, __FILE__);
165 CheckNULL(vccp->bbw_i = (
float*) malloc(vccp->nBandPairs * sizeof (
float)),
"vccp->aw_all", __LINE__, __FILE__);
166 CheckNULL(vccp->bbw_o = (
float*) malloc(vccp->nBandPairs * sizeof (
float)),
"vccp->bbw_o", __LINE__, __FILE__);
168 for (kb = 0; kb < vccp->nBandPairs; kb++) {
169 vccp->wvl_i[kb] = lambda_i[
iflag][kb];
170 vccp->wvl_o[kb] = lambda_o[
iflag][kb];
173 for (kb = 0; kb < nband_vc; kb++)
174 vccp->ibvc_bin[kb] = ibvc_bin[
iflag][kb];
176 get_aw_bbw(l2rec, vccp->wvl_i, vccp->nBandPairs, vccp->aw_i, vccp->bbw_i);
177 get_aw_bbw(l2rec, vccp->wvl_o, vccp->nBandPairs, vccp->aw_o, vccp->bbw_o);
178 get_aw_bbw(l2rec, &vccp->wvl_ref, 1, &vccp->aw_ref, &vccp->bbw_ref);
181 float BandInterp(ccstr* vcc,
float* rrs_sh,
int idx_0,
int num_in) {
188 float *wts, wts_sum = 0;
189 float rrs_sh_interp = 0;
191 CheckNULL(wts = (
float*) malloc(num_in *
sizeof (
float)),
"wts", __LINE__, __FILE__);
193 for (idx = 0; idx < num_in; idx++) {
195 wts[idx] = 1.0 /
fabs(vcc->wvl_o[ib] - vcc->wvl_i[ib]);
199 for (idx = 0; idx < num_in; idx++) {
200 rrs_sh_interp += (wts[idx] * rrs_sh[idx]) / (wts_sum);
204 return rrs_sh_interp;
207 float BandShift(ccstr* vccp, l2str *l2rec, int32_t ip,
float target_band) {
208 float g0 = 0.089, g1 = 0.1245;
209 float rrs_in, *rrs_e_f, rrs_f_i, rrs_f_f, rrs_e_temp, Rrs_e_f;
210 int ipb, ib, idx, t_num = 0, t_start;
212 float a_i, a_f, u_i, u_f;
215 for (ib = 0; ib < vccp->nBandPairs; ib++) {
216 if (vccp->wvl_o[ib] == target_band) {
224 printf(
"\n -E- line %d Unable to find target band,%f => t_num = 0 :( \n", __LINE__, target_band);
228 CheckNULL(rrs_e_f = (
float*) malloc(t_num *
sizeof (
float)),
"rrs_e_f", __LINE__, __FILE__);
230 for (idx = 0; idx < t_num; idx++) {
232 ipb = ip * l2rec->l1rec->l1file->nbands +
bindex_get((int32_t) vccp->wvl_i[ib]);
235 bb_i =
Get_bb(l2rec, vccp, ip, ib,
'i');
236 bb_f =
Get_bb(l2rec, vccp, ip, ib,
'f');
237 a_i =
Get_a(l2rec, vccp, ip, ib,
'i');
238 a_f =
Get_a(l2rec, vccp, ip, ib,
'f');
240 u_i = bb_i / (a_i + bb_i);
241 u_f = bb_f / (a_f + bb_f);
243 rrs_f_i = g0 * u_i + g1 * pow(u_i, 2);
244 rrs_f_f = g0 * u_f + g1 * pow(u_f, 2);
246 rrs_e_f[idx] = rrs_in * rrs_f_f / rrs_f_i;
250 rrs_e_temp =
BandInterp(vccp, rrs_e_f, t_start, t_num);
261 printf(
"\n --E-- pointer %s at line %d is %s not allocated :(\n", ptrName, lineNum,
filename);
266 float Get_bb(l2str *l2rec, ccstr *vccp, int32_t ip,
int vccidx,
char key) {
267 int refIdx, greenIdx, ipb;
268 float rrsRef, rrsGreen, eta, wavl;
272 ipb = ip * l2rec->l1rec->l1file->nbands + refIdx;
276 ipb = ip * l2rec->l1rec->l1file->nbands + greenIdx;
280 wavl = vccp->wvl_i[vccidx];
281 bbw = vccp->bbw_i[vccidx];
283 wavl = vccp->wvl_o[vccidx];
284 bbw = vccp->bbw_o[vccidx];
286 eta = 2. * (1 - 1.2 * exp(-0.9 * rrsRef / rrsGreen));
287 bbp = bbp_ref[ip] * pow((
REFWVL / wavl), eta);
292 float Get_a(l2str *l2rec, ccstr *vccp, int32_t ip,
int vccidx,
char key) {
294 int refIdx, greenIdx, ipb;
295 float rrsRef, rrsGreen, wavl,
s;
296 float aw, aph, adg,
a;
299 ipb = ip * l2rec->l1rec->l1file->nbands + refIdx;
302 greenIdx =
bindex_get((int32_t) vccp->greenband);
303 ipb = ip * l2rec->l1rec->l1file->nbands + greenIdx;
307 wavl = vccp->wvl_i[vccidx];
308 aw = vccp->aw_i[vccidx];
310 wavl = vccp->wvl_o[vccidx];
311 aw = vccp->aw_o[vccidx];
313 s = 0.015 + 0.002 / (0.6 + (rrsRef / rrsGreen));
314 adg = adg_ref[ip] * exp(-
s * (wavl -
REFWVL));
322 rrsBelow = rrsAbove / (0.52 + 1.7 * rrsAbove);
328 rrsAbove = 0.52 * rrsBelow / (1 - 1.7 * rrsBelow);