17 static int firstCall = 1;
20 static m_array *m12, *m13;
24 static float *ang =
NULL, margin_s;
30 static int *have_xcal;
36 float *dpol =
l1rec->dpol;
38 int32_t pixnum =
l1rec->pixnum[ip];
39 int32_t detnum =
l1rec->detnum;
50 float L_x, L_qp, L_up;
52 int iagsm[] = {0, 640, 1376, 3152, 4928, 5664, 6304};
53 int iagpx[] = {0, 640, 1008, 1600, 2192, 2560, 3200};
54 int iseq, ix1, ix2, ipx, irng, step[] = {1, 2, 3, 3, 2, 1};
55 char ix, attrname[50];
56 double sind = 0.0003104;
57 float rad_2_deg = 180. / acos(-1);
58 int get_wt(
float,
float *,
int,
float *,
char *);
63 for (ib = 0; ib <
nbands; ib++) {
67 if (
input->pol_opt == 0 ||
input->pol_opt > 99)
77 char name [H4_MAX_NC_NAME] =
"";
78 char sdsname[H4_MAX_NC_NAME] =
"";
79 char file [FILENAME_MAX] =
"";
84 int32 dims[H4_MAX_VAR_DIMS];
86 int32
start[3] = {0, 0, 0};
87 int32 end [3] = {1, 1, 1};
92 int32_t
im, ia,
id, ixb;
95 if ((detfac = (
float *) calloc(
l1rec->l1file->nbands, sizeof (
float))) ==
NULL) {
96 printf(
"-E- : Error allocating memory to dray_for_i\n");
100 if ((have_xcal = (
int *) calloc(
l1rec->l1file->nbands, sizeof (
int))) ==
NULL) {
101 printf(
"-E- : Error allocating memory to dray_for_i\n");
105 for (ixb = 0; ixb <
l1_input->xcal_nwave; ixb++) {
108 printf(
"-E- %sline %d: xcal wavelength %f does not match sensor\n",
109 __FILE__, __LINE__,
l1_input->xcal_wave[ixb]);
116 if ((m12 = (m_array *) calloc(
l1rec->l1file->nbands, sizeof (m_array))) ==
NULL) {
117 printf(
"-E- : Error allocating memory to dray_for_i\n");
120 if ((m13 = (m_array *) calloc(
l1rec->l1file->nbands, sizeof (m_array))) ==
NULL) {
121 printf(
"-E- : Error allocating memory to dray_for_i\n");
125 for (ib = 0; ib <
nbands; ib++) {
127 sprintf(
file,
"%s%s%d%s",
input->polfile,
"_",
l1rec->l1file->iwave[ib],
".hdf");
129 printf(
"Loading polarization file %s\n",
file);
132 sd_id = SDstart(
file, DFACC_RDONLY);
134 fprintf(
stderr,
"-E- %s line %d: SDstart(%s, %d) failed.\n",
135 __FILE__, __LINE__,
file, DFACC_RDONLY);
139 status = SDreadattr(sd_id, SDfindattr(sd_id,
"Number of Detectors"), &ndets);
141 printf(
"-E- %s Line %d: Error reading global attribute %s from %s.\n",
142 __FILE__, __LINE__,
"Number of Detectors",
file);
146 status = SDreadattr(sd_id, SDfindattr(sd_id,
"Number of Mirror Sides"), &nmside);
148 printf(
"-E- %s Line %d: Error reading global attribute %s from %s.\n",
149 __FILE__, __LINE__,
"Number of Mirror Sides",
file);
158 strcpy(attrname,
"Number of Scan angles");
159 strcpy(sdsname,
"scanangle");
161 strcpy(attrname,
"Number of AOIs");
164 status = SDreadattr(sd_id, SDfindattr(sd_id, attrname), &nang);
167 "-E- %s Line %d: Error reading global attribute %s from %s.\n",
168 __FILE__, __LINE__, attrname,
file);
172 if ((ang = (
float *) malloc(nang *
sizeof (
float))) ==
NULL) {
173 printf(
"-E- %s Line %d: Error allocating memory for ang.\n",
178 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
182 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
183 __FILE__, __LINE__, sdsname,
file);
186 status = SDendaccess(sds_id);
192 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
193 for (
im = 0;
im < nmside;
im++)
for (ia = 0; ia < nang; ia++)
for (
id = 0;
id < ndets;
id++) {
199 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
200 __FILE__, __LINE__, sdsname,
file);
205 status = SDendaccess(sds_id);
208 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
209 for (
im = 0;
im < nmside;
im++)
for (ia = 0; ia < nang; ia++)
for (
id = 0;
id < ndets;
id++) {
215 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
216 __FILE__, __LINE__, sdsname,
file);
220 status = SDendaccess(sds_id);
229 detfac[ib] =
l1rec->l1file->ndets * 1.0 / ndets;
247 margin_s =
l1rec->margin_s;
248 if (
l1rec->scn_fmt == 0)
251 maxpix = 6304 + margin_s * 2;
253 printf(
"-E- %s line %d: Mirror geometry unknown for %s\n",
261 if ((pxwt = (
float *) malloc(maxpix *
sizeof (
float))) ==
NULL) {
262 printf(
"-E- %s Line %d: Error allocating memory for weights.\n",
266 if ((pxangix = (
char *) malloc(maxpix *
sizeof (
char))) ==
NULL) {
267 printf(
"-E- %s Line %d: Error allocating memory for angle index.\n",
276 if (
l1rec->scn_fmt == 0) {
277 for (irng = 0; irng < 6; irng++) {
280 for (ipx = iagpx[ix1]; ipx < iagpx[ix2]; ipx++) {
281 iseq = ipx - iagpx[ix1];
282 zang = iagsm[ix1] + ((2 * iseq + 1) * step[irng] - 1.) / 2.;
283 zang = (zang - 3151.5) * sind * rad_2_deg;
286 get_wt(zang, ang, nang, &wt, &ix);
292 for (ipx = 0; ipx < maxpix; ipx++) {
293 zang = ((
float) ipx - 3151.5 - margin_s) * sind * rad_2_deg;
294 get_wt(zang, ang, nang, &wt, &ix);
302 for (ipx = 0; ipx < maxpix; ipx++) {
303 zang = 10.5 + ipx / maxpix * (65.5 - 10.5);
304 get_wt(zang, ang, nang, &wt, &ix);
318 for (ib = 0; ib <
nbands; ib++) {
321 idet = (int32_t)
rint(detnum / detfac[ib]);
331 polcor[ipb] = 1.0 / (1.0 - dm12[ip] * L_qp / L_x - dm13[ip] * L_up / L_x);
333 for (iang = pxangix[pixnum]; iang <= (pxangix[pixnum] + 1); iang++) {
334 m1[iang] = 1.0 / (1.0
335 - m12[ib][
mside][iang][idet] * L_qp / L_x
336 - m13[ib][
mside][iang][idet] * L_up / L_x);
338 polcor[ipb] = m1[(
int) pxangix[pixnum]] * (1. - pxwt[pixnum]) + m1[(
int) pxangix[pixnum] + 1] * pxwt[pixnum];
340 dpol [ipb] = sqrt(pow(
l1rec->L_q[ipb], 2.0) + pow(
l1rec->L_u[ipb], 2.0)) / L_x;
343 if (
input->pol_opt == 6 && ib != nir_l && ib != nir_s)
353 int get_wt(
float zang,
float *ang,
int nang,
float *wt,
char *ix)
378 else if (zang >= ang[nang - 1])
381 for (iang = 1; iang < nang; iang++) {
382 if (zang < ang[iang]) {
389 *wt = (zang - ang[(
int) *ix]) / (ang[ix2] - ang[(
int) *ix]);