5 int scan_cvt(in_rec_struc *in_rec, out_rec_struc *out_rec)
27 static int cvt_mode = -1;
30 static int npixin, npixout;
31 static float *cvt_lat, *cvt_lon, *cvt_senz, *cvt_sena, *cvt_solz, *cvt_sola,
33 int ibnd, ilin, olin, opix, nxfr, nxfrb, ipix_st, ag_rng, num, iag, ipix;
34 float theta1, theta2, theta, phi1, phi2, phi, sum;
35 static unsigned char *cvt_bnd_q[
MAX_BND];
36 unsigned char uc_val, ua_gain_vals[] = {4, 0};
37 unsigned char ua_sat_vals[] = {0, 8};
38 int v_unsat, v_sat, ag_sat_vals[] = {8, 0, 8, 4};
43 if (in_rec->lat ==
NULL) {
50 for (ibnd = 0; ibnd < out_rec->nbnd; ibnd++) {
51 free(cvt_bnd_lt[ibnd]);
52 free(cvt_bnd_q[ibnd]);
64 if ((in_rec->scn_fmt == 1) && (out_rec->scn_fmt == 0))
66 else if ((in_rec->scn_fmt == 2) && (out_rec->scn_fmt == 0))
68 else if ((in_rec->scn_fmt == 2) && (out_rec->scn_fmt == 1))
79 npixin = in_rec->npix;
80 npixout = out_rec->npix;
81 if ((cvt_lat = (
float *)
82 malloc(out_rec->npix * out_rec->ndet_scan * sizeof (
float)))
84 printf(
"%s, %d: Unable to allocate storage for lat conversion\n",
88 out_rec->lat = cvt_lat;
89 if ((cvt_lon = (
float *)
90 malloc(out_rec->npix * out_rec->ndet_scan * sizeof (
float)))
92 printf(
"%s, %d: Unable to allocate storage for lon conversion\n",
96 out_rec->lon = cvt_lon;
97 if ((cvt_senz = (
float *)
98 malloc(out_rec->npix * out_rec->ndet_scan * sizeof (
float)))
100 printf(
"%s, %d: Unable to allocate storage for senz conversion\n",
104 out_rec->senz = cvt_senz;
105 if ((cvt_sena = (
float *)
106 malloc(out_rec->npix * out_rec->ndet_scan * sizeof (
float)))
108 printf(
"%s, %d: Unable to allocate storage for sena conversion\n",
112 out_rec->sena = cvt_sena;
113 if ((cvt_solz = (
float *)
114 malloc(out_rec->npix * out_rec->ndet_scan * sizeof (
float)))
116 printf(
"%s, %d: Unable to allocate storage for solz conversion\n",
120 out_rec->solz = cvt_solz;
121 if ((cvt_sola = (
float *)
122 malloc(out_rec->npix * out_rec->ndet_scan * sizeof (
float)))
124 printf(
"%s, %d: Unable to allocate storage for sola conversion\n",
128 out_rec->sola = cvt_sola;
129 for (ibnd = 0; ibnd < out_rec->nbnd; ibnd++) {
130 if ((cvt_bnd_lt[ibnd] = (
float *)
131 malloc(out_rec->npix * out_rec->ndet_scan * sizeof (
float)))
134 "%s, %d: Unable to allocate storage for bnd_lt[%d] conversion\n",
135 __FILE__, __LINE__, ibnd);
138 out_rec->bnd_lt[ibnd] = cvt_bnd_lt[ibnd];
141 if ((cvt_bnd_q[ibnd] = (
unsigned char *)
142 malloc(out_rec->npix * out_rec->ndet_scan *
143 sizeof (
unsigned char))) ==
NULL) {
145 "%s, %d: Unable to allocate storage for bnd_q[%d] conversion\n",
146 __FILE__, __LINE__, ibnd);
149 out_rec->bnd_q[ibnd] = cvt_bnd_q[ibnd];
162 if (out_rec->scn_fmt == 0) {
163 for (ibnd = 0; ibnd < in_rec->nbnd; ibnd++) {
164 for (ilin = 0; ilin < in_rec->ndet_scan; ilin++) {
165 for (ipix = 0; ipix < in_rec->npix; ipix++) {
166 *(out_rec->qual1_m[ibnd] + ipix + in_rec->npix * ilin) =
167 *(in_rec->dn_sat[ibnd] + ipix + in_rec->npix * ilin) * 4;
176 for (ibnd = 0; ibnd < in_rec->nbnd; ibnd++) {
177 for (ilin = 0; ilin < in_rec->ndet_scan; ilin++) {
178 for (ipix = 0; ipix < in_rec->npix; ipix++) {
179 uc_val = ua_gain_vals[ *(in_rec->gain_bit[ibnd] + ipix +
180 in_rec->npix * ilin) ];
181 uc_val = uc_val | ua_sat_vals[ (
int) *(in_rec->dn_sat[ibnd] + ipix +
182 in_rec->npix * ilin) ];
183 *(out_rec->qual1_m[ibnd] + ipix + in_rec->npix * ilin) = uc_val;
189 }
else if (cvt_mode == 2) {
193 nxfr = npixout *
sizeof (
float);
194 nxfrb = npixout *
sizeof (
unsigned char);
195 for (olin = 0, ilin = in_rec->margin[0]; olin <
NDET; olin++, ilin++) {
196 memcpy((
void *) (cvt_lat + npixout * olin),
197 (
void *) (in_rec->lat + npixin * ilin + in_rec->margin[1]), nxfr);
198 memcpy((
void *) (cvt_lon + npixout * olin),
199 (
void *) (in_rec->lon + npixin * ilin + in_rec->margin[1]), nxfr);
200 memcpy((
void *) (cvt_sena + npixout * olin),
201 (
void *) (in_rec->sena + npixin * ilin + in_rec->margin[1]), nxfr);
202 memcpy((
void *) (cvt_senz + npixout * olin),
203 (
void *) (in_rec->senz + npixin * ilin + in_rec->margin[1]), nxfr);
204 memcpy((
void *) (cvt_sola + npixout * olin),
205 (
void *) (in_rec->sola + npixin * ilin + in_rec->margin[1]), nxfr);
206 memcpy((
void *) (cvt_solz + npixout * olin),
207 (
void *) (in_rec->solz + npixin * ilin + in_rec->margin[1]), nxfr);
208 for (ibnd = 0; ibnd < out_rec->nbnd; ibnd++) {
209 memcpy((
void *) (cvt_bnd_lt[ibnd] + npixout * olin),
210 (
void *) (in_rec->bnd_lt[ibnd] + npixin * ilin + in_rec->margin[1]),
212 memcpy((
void *) (cvt_bnd_q[ibnd] + npixout * olin),
213 (
void *) (in_rec->bnd_q[ibnd] + npixin * ilin + in_rec->margin[1]),
218 for (ipix = in_rec->margin[1], opix = 0;
219 ipix < in_rec->
npix - in_rec->margin[1]; ipix++, opix++) {
220 uc_val = ua_gain_vals[ *(in_rec->gain_bit[ibnd] + ipix +
221 in_rec->npix * ilin) ];
222 uc_val = uc_val | ua_sat_vals[ (
int) *(in_rec->dn_sat[ibnd] + ipix +
223 in_rec->npix * ilin) ];
224 *(out_rec->qual1_m[ibnd] + opix + npixout * olin) = uc_val;
228 }
else if (cvt_mode == 1) {
241 for (olin = 0, ilin = in_rec->margin[0]; olin < out_rec->ndet_scan;
243 for (opix = 0; opix < npixout; opix++) {
244 if ((opix >= 0) && (opix <= 639)) {
245 ipix_st = opix + in_rec->margin[1];
247 }
else if ((opix >= 640) && (opix <= 1007)) {
248 ipix_st = 640 + in_rec->margin[1] + (opix - 640) * 2;
250 }
else if ((opix >= 1008) && (opix <= 2191)) {
251 ipix_st = 1376 + in_rec->margin[1] + (opix - 1008) * 3;
253 }
else if ((opix >= 2192) && (opix <= 2559)) {
254 ipix_st = 4928 + in_rec->margin[1] + (opix - 2192) * 2;
256 }
else if ((opix >= 2560) && (opix <= 3199)) {
257 ipix_st = 5664 + in_rec->margin[1] + opix - 2560;
268 *(cvt_lat + npixout * olin + opix) =
269 *(in_rec->lat + ipix_st + npixin * ilin);
270 *(cvt_lon + npixout * olin + opix) =
271 *(in_rec->lon + ipix_st + npixin * ilin);
272 *(cvt_senz + npixout * olin + opix) =
273 *(in_rec->senz + ipix_st + npixin * ilin);
274 *(cvt_sena + npixout * olin + opix) =
275 *(in_rec->sena + ipix_st + npixin * ilin);
276 *(cvt_solz + npixout * olin + opix) =
277 *(in_rec->solz + ipix_st + npixin * ilin);
278 *(cvt_sola + npixout * olin + opix) =
279 *(in_rec->sola + ipix_st + npixin * ilin);
280 for (ibnd = 0; ibnd < out_rec->nbnd; ibnd++) {
281 *(cvt_bnd_lt[ibnd] + npixout * olin + opix) =
282 *(in_rec->bnd_lt[ibnd] + ipix_st + npixin * ilin);
283 *(cvt_bnd_q[ibnd] + npixout * olin + opix) =
284 *(in_rec->bnd_q[ibnd] + ipix_st + npixin * ilin);
290 *(out_rec->qual1_m[ibnd] + opix + npixout * olin) =
291 *(in_rec->dn_sat[ibnd] + ipix_st + npixin * ilin);
301 for (ibnd = 0; ibnd < out_rec->nbnd; ibnd++) {
306 for (iag = 0; iag < ag_rng; iag++) {
308 (*(in_rec->bnd_lt[ibnd] + ilin * npixin + ipix_st + iag) > 0)
309 && (*(in_rec->bnd_q[ibnd] + ilin * npixin + ipix_st + iag) == 0)) {
311 *(in_rec->bnd_lt[ibnd] + ilin * npixin + ipix_st + iag);
317 if (*(in_rec->dn_sat[ibnd] + ilin + npixin + ipix_st + iag)
319 if (*(in_rec->dn_sat[ibnd] + ilin + npixin + ipix_st + iag)
324 *(cvt_bnd_lt[ibnd] + npixout * olin + opix) = sum / num;
325 *(cvt_bnd_q[ibnd] + npixout * olin + opix) = 0;
327 *(cvt_bnd_lt[ibnd] + npixout * olin + opix) = -32767.;
328 *(cvt_bnd_q[ibnd] + npixout * olin + opix) = 2;
331 *(out_rec->qual1_m[ibnd] + npixout * olin + opix) =
332 ag_sat_vals[ v_sat + v_unsat ];
337 theta1 = *(in_rec->lat + ilin * npixin + ipix_st);
338 theta2 = *(in_rec->lat + ilin * npixin + ipix_st + 1);
339 phi1 = *(in_rec->lon + ilin * npixin + ipix_st);
340 phi2 = *(in_rec->lon + ilin * npixin + ipix_st + 1);
341 ang_avg(theta1, phi1, theta2, phi2, &theta, &phi);
342 *(cvt_lat + npixout * olin + opix) = theta;
343 *(cvt_lon + npixout * olin + opix) = phi;
345 theta1 = *(in_rec->senz + ilin * npixin + ipix_st);
346 theta2 = *(in_rec->senz + ilin * npixin + ipix_st + 1);
347 phi1 = *(in_rec->sena + ilin * npixin + ipix_st);
348 phi2 = *(in_rec->sena + ilin * npixin + ipix_st + 1);
349 ang_avg(theta1, phi1, theta2, phi2, &theta, &phi);
350 *(cvt_senz + npixout * olin + opix) = theta;
351 *(cvt_sena + npixout * olin + opix) = phi;
353 theta1 = *(in_rec->solz + ilin * npixin + ipix_st);
354 theta2 = *(in_rec->solz + ilin * npixin + ipix_st + 1);
355 phi1 = *(in_rec->sola + ilin * npixin + ipix_st);
356 phi2 = *(in_rec->sola + ilin * npixin + ipix_st + 1);
357 ang_avg(theta1, phi1, theta2, phi2, &theta, &phi);
358 *(cvt_solz + npixout * olin + opix) = theta;
359 *(cvt_sola + npixout * olin + opix) = phi;
365 for (ibnd = 0; ibnd < out_rec->nbnd; ibnd++) {
370 for (iag = 0; iag < ag_rng; iag++) {
372 (*(in_rec->bnd_lt[ibnd] + ilin * npixin + ipix_st + iag) > 0)
373 && (*(in_rec->bnd_q[ibnd] + ilin * npixin + ipix_st + iag) == 0)) {
375 *(in_rec->bnd_lt[ibnd] + ilin * npixin + ipix_st + iag);
381 if (*(in_rec->dn_sat[ibnd] + ilin + npixin + ipix_st + iag)
383 if (*(in_rec->dn_sat[ibnd] + ilin + npixin + ipix_st + iag)
388 *(cvt_bnd_lt[ibnd] + npixout * olin + opix) = sum / num;
389 *(cvt_bnd_q[ibnd] + npixout * olin + opix) = 0;
391 *(cvt_bnd_lt[ibnd] + npixout * olin + opix) = -32767.;
392 *(cvt_bnd_q[ibnd] + npixout * olin + opix) = 2;
395 *(out_rec->qual1_m[ibnd] + npixout * olin + opix) =
396 ag_sat_vals[ v_sat + v_unsat ];
399 *(cvt_lat + npixout * olin + opix) =
400 *(in_rec->lat + ilin * npixin + ipix_st + 1);
401 *(cvt_lon + npixout * olin + opix) =
402 *(in_rec->lon + ilin * npixin + ipix_st + 1);
403 *(cvt_senz + npixout * olin + opix) =
404 *(in_rec->senz + ilin * npixin + ipix_st + 1);
405 *(cvt_sena + npixout * olin + opix) =
406 *(in_rec->sena + ilin * npixin + ipix_st + 1);
407 *(cvt_solz + npixout * olin + opix) =
408 *(in_rec->solz + ilin * npixin + ipix_st + 1);
409 *(cvt_sola + npixout * olin + opix) =
410 *(in_rec->sola + ilin * npixin + ipix_st + 1);
419 int ang_avg(
float theta1,
float phi1,
float theta2,
float phi2,
420 float *theta,
float *phi)
449 float v1[2],
v2[2],
v[2];
464 v[0] = (v1[0] +
v2[0]) / 2.;
465 v[1] = (v1[1] +
v2[1]) / 2.;
468 *theta = asin(sqrt(
v[0] *
v[0] +
v[1] *
v[1])) *
rad2deg;