25 static int first_time = 0, rvs_note = 0;
26 static float dn_max = 4095.;
27 static float dn_trans_pct = 0.83;
30 int ilin, idet, ismp, ipix, ibnd, igain, iham, dn_sat;
32 float c0, c1, c2, a0, a1, a2, dark, lt, aoi, rvs_val, dn, big_dn;
34 static vir_gain_struc
gain;
35 static vir_rvs_struc rvs;
40 iham = in_rec->ham_side;
42 if (first_time == 0) {
49 printf(
"%s, %d: # of HAM sides in gain struct (%d) != 2\n", __FILE__,
51 printf(
"\tgain file: %s\n", ctl->count_decal_gain_file);
56 "%s, %d: # of detectors in gain struct (%d) != the VIIRS # (%d)\n",
57 __FILE__, __LINE__,
gain.ndet,
NDET);
58 printf(
"\tgain file: %s\n", ctl->count_decal_gain_file);
61 if (
gain.ngain != 2) {
62 printf(
"%s, %d: # of gain states in gain struct (%d) != 2\n", __FILE__,
64 printf(
"\tgain file: %s\n", ctl->count_decal_gain_file);
69 "%s, %d: # of bands in gain struct (%d) not >= # VIS / NIR bands (%d)\n",
71 printf(
"\tgain file: %s\n", ctl->count_decal_gain_file);
81 printf(
"%s, %d: # of HAM sides in RVS struct (%d) != 2\n", __FILE__,
83 printf(
"\trvs file: %s\n", ctl->count_decal_rvs_file);
86 if (rvs.ndet !=
NDET) {
88 "%s, %d: # of detectors in RVS struct (%d) != the VIIRS # (%d)\n",
89 __FILE__, __LINE__, rvs.ndet,
NDET);
90 printf(
"\trvs file: %s\n", ctl->count_decal_rvs_file);
94 printf(
"%s, %d: # of bands in RVS struct (%d) not >= # needed # (%d)\n",
95 __FILE__, __LINE__, rvs.nbnd,
nbnd);
96 printf(
"\trvs file: %s\n", ctl->count_decal_rvs_file);
101 min_rvs[ibnd] = 999.;
102 max_rvs[ibnd] = -999.;
111 for (ipix = 0; ipix <
npix; ipix++) {
113 ismp = ipix - in_rec->margin[1];
117 if ((rvs_note == 0) && ((ipix % 200) == 0)) {
120 printf(
"%s, %d: info, sample: %d, scan angle: %f, aoi: %f\n",
121 __FILE__, __LINE__, ipix, scn_ang, aoi);
124 for (ilin = 0; ilin < in_rec->ndet_scan; ilin++) {
126 idet = ilin - in_rec->margin[0];
127 if (idet < 0) idet = 0;
134 if (*(in_rec->bnd_q[ibnd] + ipix +
npix * ilin) == 0) {
138 lt = *(in_rec->bnd_lt[ibnd] + ipix +
npix * ilin);
141 dark = *(
gain.dark + ibnd +
gain.nbnd * (igain +
gain.ngain *
142 (idet +
gain.ndet * iham)));
146 c0 = *(
gain.c0 + ibnd +
gain.nbnd * (igain +
gain.ngain *
147 (idet +
gain.ndet * iham)));
148 c1 = *(
gain.c1 + ibnd +
gain.nbnd * (igain +
gain.ngain *
149 (idet +
gain.ndet * iham)));
150 c2 = *(
gain.c2 + ibnd +
gain.nbnd * (igain +
gain.ngain *
151 (idet +
gain.ndet * iham)));
153 a0 = *(rvs.a0 + ibnd + rvs.nbnd * (idet + rvs.ndet * iham));
154 a1 = *(rvs.a1 + ibnd + rvs.nbnd * (idet + rvs.ndet * iham));
155 a2 = *(rvs.a2 + ibnd + rvs.nbnd * (idet + rvs.ndet * iham));
157 rvs_val = a0 + a1 * aoi + a2 * aoi * aoi;
162 if (rvs_val < min_rvs[ibnd])
163 min_rvs[ibnd] = rvs_val;
164 if (rvs_val > max_rvs[ibnd])
165 max_rvs[ibnd] = rvs_val;
171 dn = (lt * rvs_val - c0) / c1;
173 dn = (-c1 + sqrt(c1 * c1 - 4. * c2 * (c0 - lt * rvs_val)))
179 if (
gain.gain_ct[ibnd] == 2) {
180 if (dn > (dn_max * dn_trans_pct)) {
182 dark = *(
gain.dark + ibnd +
gain.nbnd * (igain +
gain.ngain *
183 (idet +
gain.ndet * iham)));
187 c0 = *(
gain.c0 + ibnd +
gain.nbnd * (igain +
gain.ngain *
188 (idet +
gain.ndet * iham)));
189 c1 = *(
gain.c1 + ibnd +
gain.nbnd * (igain +
gain.ngain *
190 (idet +
gain.ndet * iham)));
191 c2 = *(
gain.c2 + ibnd +
gain.nbnd * (igain +
gain.ngain *
192 (idet +
gain.ndet * iham)));
197 dn = (lt * rvs_val - c0) / c1;
199 dn = (-c1 + sqrt(c1 * c1 - 4. * c2 * (c0 - lt * rvs_val)))
206 if (big_dn > dn_max) {
216 if (big_dn > dn_max) {
225 if (ctl->count_dark_opt == 0)
236 *(in_rec->dn[ibnd] + ipix +
npix * ilin) = dn;
237 *(in_rec->dn_sat[ibnd] + ipix +
npix * ilin) = dn_sat;
238 *(in_rec->gain_bit[ibnd] + ipix +
npix * ilin) = igain;
247 printf(
"\n\n%s, %d:info\n", __FILE__, __LINE__);
248 printf(
"\nDe-calibration RVS min and max / band:\n");
249 printf(
"(file: %s\n", ctl->count_decal_rvs_file);
250 printf(
"\nM band Min RVS Max RVS\n");
252 printf(
" %3d %9.6f %9.6f\n", ibnd + 1,
253 min_rvs[ibnd], max_rvs[ibnd]);
254 printf(
"----------------------------------------\n\n");