18     static int firstCall = 1;
 
   21     static m_array *m12, *m13;
 
   25     static float *ang = 
NULL, margin_s;
 
   31     static int *have_xcal;
 
   37     float *delta_polcor=
NULL;
 
   38     float *dpol = 
l1rec->dpol;
 
   40     int32_t pixnum = 
l1rec->pixnum[ip];
 
   41     int32_t detnum = 
l1rec->detnum;
 
   52     float L_x, L_qp, L_up;
 
   54     int iagsm[] = {0, 640, 1376, 3152, 4928, 5664, 6304};
 
   55     int iagpx[] = {0, 640, 1008, 1600, 2192, 2560, 3200};
 
   56     int iseq, ix1, ix2, ipx, irng, step[] = {1, 2, 3, 3, 2, 1};
 
   57     char ix, attrname[50];
 
   58     double sind = 0.0003104;
 
   59     float rad_2_deg = 180. / acos(-1);
 
   60     int get_wt(
float, 
float *, 
int, 
float *, 
char *);
 
   62     if(
l1rec->uncertainty) {
 
   63         delta_polcor=
l1rec->uncertainty->derv_pol;
 
   69     for (ib = 0; ib < 
nbands; ib++) {
 
   73     if (
input->pol_opt == 0 || 
input->pol_opt > 99)
 
   88         char name [H4_MAX_NC_NAME] = 
"";
 
   89         char sdsname[H4_MAX_NC_NAME] = 
"";
 
   90         char file [FILENAME_MAX] = 
"";
 
   95         int32 dims[H4_MAX_VAR_DIMS];
 
   97         int32 
start[3] = {0, 0, 0};
 
   98         int32 
end [3] = {1, 1, 1};
 
  103         int32_t 
im, ia, 
id, ixb;
 
  106         if ((detfac = (
float *) calloc(
l1rec->l1file->nbands, sizeof (
float))) == 
NULL) {
 
  107             printf(
"-E- : Error allocating memory to dray_for_i\n");
 
  111         if ((have_xcal = (
int *) calloc(
l1rec->l1file->nbands, sizeof (
int))) == 
NULL) {
 
  112             printf(
"-E- : Error allocating memory to dray_for_i\n");
 
  116         for (ixb = 0; ixb < 
l1_input->xcal_nwave; ixb++) {
 
  119                     printf(
"-E- %sline %d: xcal wavelength %f does not match sensor\n",
 
  120                             __FILE__, __LINE__, 
l1_input->xcal_wave[ixb]);
 
  127         if ((m12 = (m_array *) calloc(
l1rec->l1file->nbands, sizeof (m_array))) == 
NULL) {
 
  128             printf(
"-E- : Error allocating memory to dray_for_i\n");
 
  131         if ((m13 = (m_array *) calloc(
l1rec->l1file->nbands, sizeof (m_array))) == 
NULL) {
 
  132             printf(
"-E- : Error allocating memory to dray_for_i\n");
 
  136         for (ib = 0; ib < 
nbands; ib++) {
 
  138             sprintf(
file, 
"%s%s%d%s", 
input->polfile, 
"_", 
l1rec->l1file->iwave[ib], 
".hdf");
 
  140             printf(
"Loading polarization file %s\n", 
file);
 
  143             sd_id = SDstart(
file, DFACC_RDONLY);
 
  145                 fprintf(
stderr, 
"-E- %s line %d: SDstart(%s, %d) failed.\n",
 
  146                         __FILE__, __LINE__, 
file, DFACC_RDONLY);
 
  151             status = SDreadattr(sd_id, SDfindattr(sd_id, 
"Number of Detectors"), &ndets);
 
  153                 printf(
"-E- %s Line %d:  Error reading global attribute %s from %s.\n",
 
  154                         __FILE__, __LINE__, 
"Number of Detectors", 
file);
 
  158             status = SDreadattr(sd_id, SDfindattr(sd_id, 
"Number of Mirror Sides"), &nmside);
 
  160                 printf(
"-E- %s Line %d:  Error reading global attribute %s from %s.\n",
 
  161                         __FILE__, __LINE__, 
"Number of Mirror Sides", 
file);
 
  171                     strcpy(attrname, 
"Number of Scan angles");
 
  172                     strcpy(sdsname, 
"scanangle");
 
  174                     strcpy(attrname, 
"Number of AOIs");
 
  179                 status = SDreadattr(sd_id, SDfindattr(sd_id, attrname), &nang);
 
  182                             "-E- %s Line %d:  Error reading global attribute %s from %s.\n",
 
  183                             __FILE__, __LINE__, attrname, 
file);
 
  187                 if ((ang = (
float *) malloc(nang * 
sizeof (
float))) == 
NULL) {
 
  188                     printf(
"-E- %s Line %d:  Error allocating memory for ang.\n",
 
  193                 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
 
  197                     printf(
"-E- %s Line %d:  Error reading SDS %s from %s.\n",
 
  198                             __FILE__, __LINE__, sdsname, 
file);
 
  201                     status = SDendaccess(sds_id);
 
  207             sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
 
  208             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);
 
  221             status = SDendaccess(sds_id);
 
  224             sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
 
  225             for (
im = 0; 
im < nmside; 
im++) 
for (ia = 0; ia < nang; ia++) 
for (
id = 0; 
id < ndets; 
id++) {
 
  231                             printf(
"-E- %s Line %d:  Error reading SDS %s from %s.\n",
 
  232                                     __FILE__, __LINE__, sdsname, 
file);
 
  236             status = SDendaccess(sds_id);
 
  245                 detfac[ib] = 
l1rec->l1file->ndets * 1.0 / ndets; 
 
  263             margin_s = 
l1rec->margin_s;
 
  264             if (
l1rec->scn_fmt == 0)
 
  267                 maxpix = 6304 + margin_s * 2;
 
  269             printf(
"-E- %s line %d: Mirror geometry unknown for %s\n",
 
  277         if ((pxwt = (
float *) malloc(maxpix * 
sizeof ( 
float))) == 
NULL) {
 
  278             printf(
"-E- %s Line %d:  Error allocating memory for weights.\n",
 
  282         if ((pxangix = (
char *) malloc(maxpix * 
sizeof ( 
char))) == 
NULL) {
 
  283             printf(
"-E- %s Line %d:  Error allocating memory for angle index.\n",
 
  292             if (
l1rec->scn_fmt == 0)  {
 
  293                 for (irng = 0; irng < 6; irng++) {
 
  296                     for (ipx = iagpx[ix1]; ipx < iagpx[ix2]; ipx++) {
 
  297                         iseq = ipx - iagpx[ix1];
 
  298                         zang = iagsm[ix1] + ((2 * iseq + 1) * step[irng] - 1.) / 2.;
 
  299                         zang = (zang - 3151.5) * sind * rad_2_deg;
 
  302                         get_wt(zang, ang, nang, &wt, &ix);
 
  308                 for (ipx = 0; ipx < maxpix; ipx++) {
 
  309                     zang = ((
float) ipx - 3151.5 - margin_s) * sind * rad_2_deg;
 
  310                     get_wt(zang, ang, nang, &wt, &ix);
 
  318             for (ipx = 0; ipx < maxpix; ipx++) {
 
  319                 zang = 10.5 + ipx / maxpix * (65.5 - 10.5);
 
  320                 get_wt(zang, ang, nang, &wt, &ix);
 
  334     for (ib = 0; ib < 
nbands; ib++) {
 
  337         idet = (int32_t) 
rint(detnum / detfac[ib]);
 
  340         if(
l1rec->uncertainty) {
 
  341             delta_polcor[ipb]= 1/ 
l1rec->tg_sol[ipb] / 
l1rec->tg_sen[ipb];
 
  349                 polcor[ipb] = 1.0 / (1.0 - dm12[ip] * L_qp / L_x - dm13[ip] * L_up / L_x);
 
  351                 for (iang = pxangix[pixnum]; iang <= (pxangix[pixnum] + 1); iang++) {
 
  352                     m1[iang] = 1.0 / (1.0
 
  353                             - m12[ib][
mside][iang][idet] * L_qp / L_x
 
  354                             - m13[ib][
mside][iang][idet] * L_up / L_x);
 
  356                     if(
l1rec->uncertainty) {
 
  357                         deriv_m1[iang]=-m1[iang]*m1[iang]*(m12[ib][
mside][iang][idet] * L_qp + m13[ib][
mside][iang][idet] * L_up)/(L_x*L_x);
 
  358                         deriv_m1[iang]*=delta_polcor[ipb];
 
  361                 polcor[ipb] = m1[(
int) pxangix[pixnum]] * (1. - pxwt[pixnum]) + m1[(
int) pxangix[pixnum] + 1] * pxwt[pixnum];
 
  362                 if(
l1rec->uncertainty) {
 
  363                     delta_polcor[ipb]=deriv_m1[(
int) pxangix[pixnum]] * (1. - pxwt[pixnum]) + deriv_m1[(
int) pxangix[pixnum] + 1] * pxwt[pixnum];
 
  366             dpol [ipb] = sqrt(pow(
l1rec->L_q[ipb], 2.0) + pow(
l1rec->L_u[ipb], 2.0)) / L_x;
 
  369             if (
input->pol_opt == 6 && ib != nir_l && ib != nir_s)
 
  379 int get_wt(
float zang, 
float *ang, 
int nang, 
float *wt, 
char *ix)
 
  404     else if (zang >= ang[nang - 1])
 
  407         for (iang = 1; iang < nang; iang++) {
 
  408             if (zang < ang[iang]) {
 
  415     *wt = (zang - ang[(
int) *ix]) / (ang[ix2] - ang[(
int) *ix]);