4 int viirs_cal(ctl_struc *ctl, in_rec_struc *in_rec)
25 static int first_time = 0, rvs_note = 0;
26 int ilin, idet, ipix, ibnd, igain, iham;
28 float c0, c1, c2, a0, a1, a2, dark, dn, lt, aoi, rvs_val;
30 static vir_gain_struc
gain;
31 static vir_rvs_struc rvs;
36 iham = in_rec->ham_side;
38 if (first_time == 0) {
45 printf(
"%s, %d: # of HAM sides in gain struct (%d) != 2\n", __FILE__,
47 printf(
"\tgain file: %s\n", ctl->count_decal_gain_file);
52 "%s, %d: # of detectors in gain struct (%d) != the VIIRS # (%d)\n",
53 __FILE__, __LINE__,
gain.ndet,
NDET);
54 printf(
"\tgain file: %s\n", ctl->count_decal_gain_file);
57 if (
gain.ngain != 2) {
58 printf(
"%s, %d: # of gain states in gain struct (%d) != 2\n", __FILE__,
60 printf(
"\tgain file: %s\n", ctl->count_decal_gain_file);
64 printf(
"%s, %d: # of bands in gain struct (%d) not >= # needed # (%d)\n",
65 __FILE__, __LINE__,
gain.nbnd,
nbnd);
66 printf(
"\tgain file: %s\n", ctl->count_decal_gain_file);
76 printf(
"%s, %d: # of HAM sides in gain struct (%d) != 2\n", __FILE__,
78 printf(
"\trvs file: %s\n", ctl->count_decal_rvs_file);
81 if (rvs.ndet !=
NDET) {
83 "%s, %d: # of detectors in gain struct (%d) != the VIIRS # (%d)\n",
84 __FILE__, __LINE__, rvs.ndet,
NDET);
85 printf(
"\trvs file: %s\n", ctl->count_decal_rvs_file);
89 printf(
"%s, %d: # of bands in gain struct (%d) not >= # needed # (%d)\n",
90 __FILE__, __LINE__, rvs.nbnd,
nbnd);
91 printf(
"\trvs file: %s\n", ctl->count_decal_rvs_file);
97 max_rvs[ibnd] = -999.;
106 for (ipix = 0; ipix <
npix; ipix++) {
110 for (ilin = 0; ilin < in_rec->ndet_scan; ilin++) {
112 idet = ilin - in_rec->margin[0];
113 if (idet < 0) idet = 0;
120 if (*(in_rec->bnd_q[ibnd] + ipix +
npix * ilin) == 0) {
124 igain = *(in_rec->gain_bit[ibnd] + ipix +
npix * ilin);
125 dn = *(in_rec->dn[ibnd] + ipix +
npix * ilin);
129 c0 = *(
gain.c0 + ibnd +
gain.nbnd * (igain +
gain.ngain *
130 (idet +
gain.ndet * iham)));
131 c1 = *(
gain.c1 + ibnd +
gain.nbnd * (igain +
gain.ngain *
132 (idet +
gain.ndet * iham)));
133 c2 = *(
gain.c2 + ibnd +
gain.nbnd * (igain +
gain.ngain *
134 (idet +
gain.ndet * iham)));
136 a0 = *(rvs.a0 + ibnd + rvs.nbnd * (idet + rvs.ndet * iham));
137 a1 = *(rvs.a1 + ibnd + rvs.nbnd * (idet + rvs.ndet * iham));
138 a2 = *(rvs.a2 + ibnd + rvs.nbnd * (idet + rvs.ndet * iham));
140 rvs_val = a0 + a1 * aoi + a2 * aoi * aoi;
141 if ((rvs_val > 2.) || (rvs_val < .5)) {
142 printf(
"%s, %d: rvs val %f, odd at pix: %d, lin: %d, bnd: %d\n",
143 __FILE__, __LINE__, rvs_val, ipix, ilin, ibnd);
149 if (rvs_val < min_rvs[ibnd])
150 min_rvs[ibnd] = rvs_val;
151 if (rvs_val > max_rvs[ibnd])
152 max_rvs[ibnd] = rvs_val;
157 if (ctl->count_dark_opt == 1) {
158 dark = *(
gain.dark + ibnd +
gain.nbnd * (igain +
gain.ngain *
159 (idet +
gain.ndet * iham)));
162 lt = (c0 + c1 * dn + c2 * dn * dn) / rvs_val;
166 *(in_rec->bnd_lt[ibnd] + ipix +
npix * ilin) = lt;
177 printf(
"\n\n%s, %d: info:\n", __FILE__, __LINE__);
178 printf(
"\nCalibration RVS min and max / band:\n");
179 printf(
"(file: %s\n", ctl->count_cal_rvs_file);
180 printf(
"\nM band Min RVS Max RVS\n");
182 printf(
" %3d %9.6f %9.6f\n", ibnd + 1,
183 min_rvs[ibnd], max_rvs[ibnd]);
184 printf(
"----------------------------------------\n\n");