11 static double pixwt = 1.0;
12 static int detfac = 1;
17 static int firstCall = 1;
20 if ((xcal_wave = (
int *) calloc(nwave,
sizeof (
int))) ==
NULL) {
21 printf(
"-E- : Error allocating memory to run_soa_sma\n");
26 for (iw = 0; iw <
nband; iw++)
27 if (xcal_wave[iw] == wave)
35 static m_array *rvs, *m12, *m13;
39 static int firstCall = 1;
40 int32_t ic, ip,
im,
id, it, it1, it2;
41 int32_t ncoef = 0, ntime = 0, ndet = 0, nmside = 0;
44 nwave =
l1rec->l1file->nbands;
45 if ((m12 = (m_array *) calloc(nwave,
sizeof (m_array))) ==
NULL) {
46 printf(
"-E- : Error allocating memory to dray_for_i\n");
49 if ((m13 = (m_array *) calloc(nwave,
sizeof (m_array))) ==
NULL) {
50 printf(
"-E- : Error allocating memory to dray_for_i\n");
53 if ((rvs = (m_array *) calloc(nwave,
sizeof (m_array))) ==
NULL) {
54 printf(
"-E- : Error allocating memory to dray_for_i\n");
75 double ap, ap2, ap3, ap4, ap5, x1, x2;
79 file = (
char *) calloc(FILENAME_MAX,
sizeof (
char));
81 name = (
char *) calloc(H4_MAX_NC_NAME,
sizeof (
char));
83 sdsname = (
char *) calloc(H4_MAX_NC_NAME,
sizeof (
char));
87 int32_t dims[H4_MAX_VAR_DIMS];
90 int32_t
start[5] = {0, 0, 0, 0, 0};
91 int32_t end [5] = {1, 1, 1, 1, 1};
95 xcal_wave[bandnum] = wave;
97 sprintf(
file,
"%s%s%d%s",
l1_input->xcal_file,
"_", wave,
".hdf");
99 printf(
"Loading XCAL rvs and polarization sensitivities from %s\n",
file);
101 sd_id = SDstart(
file, DFACC_RDONLY);
103 fprintf(
stderr,
"-E- %s line %d: SDstart(%s, %d) failed.\n",
104 __FILE__, __LINE__,
file, DFACC_RDONLY);
108 status = SDreadattr(sd_id, SDfindattr(sd_id,
"Number of Coefficients"), &ncoef);
110 printf(
"-E- %s Line %d: Error reading global attribute %s from %s.\n",
111 __FILE__, __LINE__,
"Number of Coefficients",
file);
115 printf(
"-E- %s Line %d: mismatch in ncoef\n", __FILE__, __LINE__);
119 status = SDreadattr(sd_id, SDfindattr(sd_id,
"Number of Detectors"), &ndet);
121 printf(
"-E- %s Line %d: Error reading global attribute %s from %s.\n",
122 __FILE__, __LINE__,
"Number of Detectors",
file);
126 printf(
"-E- %s Line %d: mismatch in ndet\n", __FILE__, __LINE__);
130 status = SDreadattr(sd_id, SDfindattr(sd_id,
"Number of Mirror Sides"), &nmside);
132 printf(
"-E- %s Line %d: Error reading global attribute %s from %s.\n",
133 __FILE__, __LINE__,
"Number of Mirror Sides",
file);
137 printf(
"-E- %s Line %d: mismatch in nmside\n", __FILE__, __LINE__);
141 status = SDreadattr(sd_id, SDfindattr(sd_id,
"Length of Time Series ndates"), &ntime);
143 printf(
"-E- %s Line %d: Error reading global attribute %s from %s.\n",
144 __FILE__, __LINE__,
"Length of Time Series ndates",
file);
148 printf(
"-E- %s Line %d: mismatch in ntime\n", __FILE__, __LINE__);
154 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
158 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
159 __FILE__, __LINE__, sdsname,
file);
162 status = SDendaccess(sds_id);
167 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
171 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
172 __FILE__, __LINE__, sdsname,
file);
175 status = SDendaccess(sds_id);
179 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
180 for (ic = 0; ic < ncoef; ic++)
for (
id = 0;
id < ndet;
id++)
for (
im = 0;
im < nmside;
im++)
for (it = 0; it < ntime; it++) {
187 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
188 __FILE__, __LINE__, sdsname,
file);
192 status = SDendaccess(sds_id);
195 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
196 for (ic = 0; ic < ncoef; ic++)
for (
id = 0;
id < ndet;
id++)
for (
im = 0;
im < nmside;
im++)
for (it = 0; it < ntime; it++) {
203 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
204 __FILE__, __LINE__, sdsname,
file);
208 status = SDendaccess(sds_id);
211 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
212 for (ic = 0; ic < ncoef; ic++)
for (
id = 0;
id < ndet;
id++)
for (
im = 0;
im < nmside;
im++)
for (it = 0; it < ntime; it++) {
219 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
220 __FILE__, __LINE__, sdsname,
file);
224 status = SDendaccess(sds_id);
229 for (it = 0; it < ntime; it++) {
231 sectab[it] =
yds2unix(yrtab[it], dytab[it], 0.0);
236 printf(
"-E- %s line %d: time field mismatch %d %d\n", __FILE__, __LINE__, it, ntime);
240 sec =
l1rec->scantime;
241 for (it = 0; it < ntime; it++) {
242 if (sectab[it] > sec)
break;
246 else if (it == ntime)
247 it2 = it1 = ntime - 1;
249 it2 =
MAX(
MIN(it, ntime - 1), 1);
253 for (
im = 0;
im < nmside;
im++)
for (
id = 0;
id < ndet;
id++) {
255 for (ic = 0; ic < ncoef; ic++) {
256 rvsc1[ic] = rvstab[ic][
id][
im][it1];
257 m12c1[ic] = m12tab[ic][
id][
im][it1];
258 m13c1[ic] = m13tab[ic][
id][
im][it1];
259 rvsc2[ic] = rvstab[ic][
id][
im][it2];
260 m12c2[ic] = m12tab[ic][
id][
im][it2];
261 m13c2[ic] = m13tab[ic][
id][
im][it2];
263 for (ic = ncoef; ic <
XTNORDER; ic++) {
275 wt = (sec - sectab[it1]) / (sectab[it2] - sectab[it1]);
290 for (ip = 0; ip <
l1rec->npix; ip++) {
298 x1 = rvsc1[0] + ap * rvsc1[1] + ap2 * rvsc1[2] + ap3 * rvsc1[3] + ap4 * rvsc1[4] + ap5 * rvsc1[5];
299 x2 = rvsc2[0] + ap * rvsc2[1] + ap2 * rvsc2[2] + ap3 * rvsc2[3] + ap4 * rvsc2[4] + ap5 * rvsc2[5];
300 rvs[bandnum][
im][
id][ip] = x1 + wt * (x2 - x1);
302 x1 = m12c1[0] + ap * m12c1[1] + ap2 * m12c1[2] + ap3 * m12c1[3] + ap4 * m12c1[4] + ap5 * m12c1[5];
303 x2 = m12c2[0] + ap * m12c2[1] + ap2 * m12c2[2] + ap3 * m12c2[3] + ap4 * m12c2[4] + ap5 * m12c2[5];
304 m12[bandnum][
im][
id][ip] = x1 + wt * (x2 - x1);
306 x1 = m13c1[0] + ap * m13c1[1] + ap2 * m13c1[2] + ap3 * m13c1[3] + ap4 * m13c1[4] + ap5 * m13c1[5];
307 x2 = m13c2[0] + ap * m13c2[1] + ap2 * m13c2[2] + ap3 * m13c2[3] + ap4 * m13c2[4] + ap5 * m13c2[5];
308 m13[bandnum][
im][
id][ip] = x1 + wt * (x2 - x1);
316 if (
l1rec->mside > 1)
320 case XRVS:
return (&rvs[bandnum][
l1rec->mside][
l1rec->detnum / detfac][0]);
322 case XM12:
return (&m12[bandnum][
l1rec->mside][
l1rec->detnum / detfac][0]);
324 case XM13:
return (&m13[bandnum][
l1rec->mside][
l1rec->detnum / detfac][0]);
327 printf(
"-E- %s line %d: bad xcal type specified\n", __FILE__, __LINE__);
339 double *fpm_gains =
NULL;
343 int linenum, isca, ib, isb;
345 if ((fp = fopen(fpm_file,
"r")) ==
NULL)
347 printf(
"-E- %s line %d: Cannot open fpm xcal file %s\n",__FILE__,__LINE__, fpm_file);
350 if ((
line = calloc(bufsz,
sizeof(
char))) ==
NULL)
352 printf(
"-E- %s line %d: Cannot allocate memory for line buffer\n",__FILE__,__LINE__);
358 while((llen = getline(&
line, &bufsz, fp)) != -1)
364 printf(
"-E- %s line %d: Missing FPM numbers in the first line of fpm xcal file %s\n",__FILE__,__LINE__, fpm_file);
369 sscanf(
line,
"%*[^=]= %d", &nsca);
379 printf(
"-E- %s line %d: Missing FPM numbers in the first line of fpm xcal file %s\n",__FILE__,__LINE__, fpm_file);
385 fpm_gains = (
double *)calloc(
nbands, nsca*
sizeof(
double) );
386 if ( fpm_gains ==
NULL )
388 printf(
"-E- %s line %d: Cannot allocate memory for fpm gain buffer\n",__FILE__,__LINE__);
397 if ((ptr = strtok(
line,
",")) !=
NULL)
399 for (ib = 0; ib<
nbands; ib++)
402 fpm_gains[isb] = atof(ptr);
403 ptr = strtok(
NULL,
",");