32 p = malloc(m *
sizeof (
float *));
34 for (
i = 0;
i < m;
i++) {
35 p[
i ] = malloc(n *
sizeof (
float **));
36 for (
j = 0;
j < n;
j++) {
37 p[
i ][
j ] = malloc(o *
sizeof (
float ***));
47 p = malloc(l *
sizeof (
float *));
49 for (
i = 0;
i < l;
i++) {
50 p[
i ] = malloc(m *
sizeof (
float **));
51 for (
j = 0;
j < m;
j++) {
52 p[
i ][
j ] = malloc(n *
sizeof (
float ***));
53 for (
k = 0;
k < n;
k++) {
54 p[
i ][
j ][
k ] = malloc(o *
sizeof (
float ****));
67 p = malloc(m *
sizeof (int32_t *));
69 for (
i = 0;
i < m;
i++) {
70 p[
i ] = malloc(n *
sizeof (int32_t **));
71 for (
j = 0;
j < n;
j++) {
72 p[
i ][
j ] = malloc(o *
sizeof (int32_t ***));
96 static short ftype[
NK];
98 static short gain3corr;
99 static float ***radcor;
100 static int32_t *k1_epoch;
102 static int num_k1_epoch;
104 static float ***g3corr;
105 static int32_t *gr_epoch;
107 static int num_gr_epoch;
109 static float ***cnts2rad;
110 static int32_t ***det_off;
112 static float ***fptempcor;
113 static int32_t *k3_epoch;
115 static int num_k3_epoch;
117 static float ***scanmod;
118 static int32_t *k4_epoch;
120 static int num_k4_epoch;
122 static float ****msidecor;
123 static int32_t *ms_epoch;
125 static int num_ms_epoch;
127 static float ***dark_restore;
128 static int32_t *dkrest_epoch;
129 static int num_dkrest_epoch;
130 static int haveDarkRestore = 0;
136 static float g_f[
NBAND][1024];
140 static int32_t ref_min;
141 static short prev_tdi[
NBAND] = {-1, -1, -1, -1, -1, -1, -1, -1};
142 static short prev_gain[
NBAND] = {-1, -1, -1, -1, -1, -1, -1, -1};
143 static int32_t prev_syear;
144 static int32_t prev_sday;
145 static int32_t prev_smsec;
146 static double ref_jsec;
155 static int firstCall = 1;
156 char name [H4_MAX_NC_NAME] =
"";
157 char sdsname[H4_MAX_NC_NAME] =
"";
163 int32 dims[H4_MAX_VAR_DIMS];
164 int32 nattrs, attr_index,
count, num_type;
166 int32 start4[4] = {0, 0, 0, 0};
167 int32
start[3] = {0, 0, 0};
175 if (!firstCall)
return;
178 sd_id = SDstart(cal_path, DFACC_RDONLY);
180 fprintf(
stderr,
"-E- %s line %d: SDstart(%s, %d) failed.\n",
181 __FILE__, __LINE__, cal_path, DFACC_RDONLY);
187 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
191 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
192 __FILE__, __LINE__, sdsname, cal_path);
195 status = SDendaccess(sds_id);
199 strcpy(sdsname,
"radiometric_coef");
200 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
206 r = (
float *) malloc(m *
sizeof (
float *));
208 attr_index = SDfindattr(sds_id,
"k1_epochs");
209 status = SDattrinfo(sds_id, attr_index, attr_name, &num_type, &
count);
210 num_k1_epoch =
count;
211 k1_epoch = (int32_t *) malloc(
count *
sizeof (int32_t *));
212 status = SDreadattr(sds_id, attr_index, k1_epoch);
222 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
223 __FILE__, __LINE__, sdsname, cal_path);
226 status = SDendaccess(sds_id);
227 for (
i = 0;
i < dims[0];
i++) {
228 for (
j = 0;
j < dims[1];
j++) {
229 for (
k = 0;
k < dims[2];
k++) {
230 m =
i * dims[1] * dims[2] +
j * dims[2] +
k;
231 radcor[
i][
j][
k] =
r[m];
240 strcpy(sdsname,
"dark_counts");
241 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
249 r = (
float *) malloc(m *
sizeof (
float *));
251 attr_index = SDfindattr(sds_id,
"dn0_epochs");
252 status = SDattrinfo(sds_id, attr_index, attr_name, &num_type, &
count);
253 num_dkrest_epoch =
count;
254 dkrest_epoch = (int32_t *) malloc(
count *
sizeof (int32_t *));
255 status = SDreadattr(sds_id, attr_index, dkrest_epoch);
258 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
259 __FILE__, __LINE__, sdsname, cal_path);
262 status = SDendaccess(sds_id);
263 for (
i = 0;
i < dims[0];
i++) {
264 for (
j = 0;
j < dims[1];
j++) {
265 for (
k = 0;
k < dims[2];
k++) {
266 m =
i * dims[1] * dims[2] +
j * dims[2] +
k;
267 dark_restore[
i][
j][
k] =
r[m];
278 strcpy(sdsname,
"gainratio_coef");
279 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
283 printf(
"\nGain 3 correction applied.\n");
289 r = (
float *) malloc(m *
sizeof (
float *));
291 attr_index = SDfindattr(sds_id,
"gr_epochs");
292 status = SDattrinfo(sds_id, attr_index, attr_name, &num_type, &
count);
293 num_gr_epoch =
count;
294 gr_epoch = (int32_t *) malloc(
count *
sizeof (int32_t *));
295 status = SDreadattr(sds_id, attr_index, gr_epoch);
304 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
305 __FILE__, __LINE__, sdsname, cal_path);
308 status = SDendaccess(sds_id);
309 for (
i = 0;
i < dims[0];
i++) {
310 for (
j = 0;
j < dims[1];
j++) {
311 for (
k = 0;
k < dims[2];
k++) {
312 m =
i * dims[1] * dims[2] +
j * dims[2] +
k;
313 g3corr[
i][
j][
k] =
r[m];
325 strcpy(sdsname,
"counts_to_radiance");
326 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
332 r = (
float *) malloc(m *
sizeof (
float *));
336 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
337 __FILE__, __LINE__, sdsname, cal_path);
340 status = SDendaccess(sds_id);
341 for (
i = 0;
i < dims[0];
i++) {
342 for (
j = 0;
j < dims[1];
j++) {
343 for (
k = 0;
k < dims[2];
k++) {
344 m =
i * dims[1] * dims[2] +
j * dims[2] +
k;
345 cnts2rad[
k][
j][
i] =
r[m];
355 strcpy(sdsname,
"detector_offsets");
356 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
362 dr = (int32_t *) malloc(m *
sizeof (int32_t *));
366 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
367 __FILE__, __LINE__, sdsname, cal_path);
370 status = SDendaccess(sds_id);
371 for (
i = 0;
i < dims[0];
i++) {
372 for (
j = 0;
j < dims[1];
j++) {
373 for (
k = 0;
k < dims[2];
k++) {
374 m =
i * dims[1] * dims[2] +
j * dims[2] +
k;
375 det_off[
k][
j][
i] = dr[m];
384 strcpy(sdsname,
"temperature_coef");
385 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
391 r = (
float *) malloc(m *
sizeof (
float *));
393 attr_index = SDfindattr(sds_id,
"k3_epochs");
394 status = SDattrinfo(sds_id, attr_index, attr_name, &num_type, &
count);
395 num_k3_epoch =
count;
396 k3_epoch = (int32_t *) malloc(
count *
sizeof (int32_t *));
397 status = SDreadattr(sds_id, attr_index, k3_epoch);
406 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
407 __FILE__, __LINE__, sdsname, cal_path);
410 status = SDendaccess(sds_id);
411 for (
i = 0;
i < dims[0];
i++) {
412 for (
j = 0;
j < dims[1];
j++) {
413 for (
k = 0;
k < dims[2];
k++) {
414 m =
i * dims[1] * dims[2] +
j * dims[2] +
k;
415 fptempcor[
i][
j][
k] =
r[m];
424 strcpy(sdsname,
"scan_mod_coef");
425 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
431 r = (
float *) malloc(m *
sizeof (
float *));
433 attr_index = SDfindattr(sds_id,
"k4_epochs");
434 status = SDattrinfo(sds_id, attr_index, attr_name, &num_type, &
count);
435 num_k4_epoch =
count;
436 k4_epoch = (int32_t *) malloc(
count *
sizeof (int32_t *));
437 status = SDreadattr(sds_id, attr_index, k4_epoch);
446 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
447 __FILE__, __LINE__, sdsname, cal_path);
450 status = SDendaccess(sds_id);
451 for (
i = 0;
i < dims[0];
i++) {
452 for (
j = 0;
j < dims[1];
j++) {
453 for (
k = 0;
k < dims[2];
k++) {
454 m =
i * dims[1] * dims[2] +
j * dims[2] +
k;
455 scanmod[
i][
j][
k] =
r[m];
464 strcpy(sdsname,
"mirror_side_coef");
465 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
467 msidecor =
alloc4d_float(dims[0], dims[1], dims[2], dims[3]);
471 r = (
float *) malloc(m *
sizeof (
float *));
473 attr_index = SDfindattr(sds_id,
"ms_epochs");
474 status = SDattrinfo(sds_id, attr_index, attr_name, &num_type, &
count);
475 num_ms_epoch =
count;
476 ms_epoch = (int32_t *) malloc(
count *
sizeof (int32_t *));
477 status = SDreadattr(sds_id, attr_index, ms_epoch);
484 status = SDreaddata(sds_id, start4,
NULL, dims,
r);
486 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
487 __FILE__, __LINE__, sdsname, cal_path);
490 status = SDendaccess(sds_id);
491 for (
i = 0;
i < dims[0];
i++) {
492 for (
j = 0;
j < dims[1];
j++) {
493 for (
k = 0;
k < dims[2];
k++) {
494 for (l = 0; l < dims[3]; l++) {
495 m =
i * dims[1] * dims[2] * dims[3] +
j * dims[2] * dims[3] +
k * dims[3] + l;
496 msidecor[
i][
j][
k][l] =
r[m];
506 strcpy(sdsname,
"fp_temp");
507 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
512 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
513 __FILE__, __LINE__, sdsname, cal_path);
516 status = SDendaccess(sds_id);
520 strcpy(sdsname,
"TDI_list");
521 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
526 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
527 __FILE__, __LINE__, sdsname, cal_path);
530 status = SDendaccess(sds_id);
534 attr_index = SDfindattr(sd_id,
"Reference Year");
537 printf(
"-E- %s Line %d: Error reading Reference Year from %s.\n",
538 __FILE__, __LINE__, cal_path);
541 attr_index = SDfindattr(sd_id,
"Reference Day");
544 printf(
"-E- %s Line %d: Error reading Reference Day from %s.\n",
545 __FILE__, __LINE__, cal_path);
548 attr_index = SDfindattr(sd_id,
"Reference Minute");
549 status = SDreadattr(sd_id, attr_index, &ref_min);
551 printf(
"-E- %s Line %d: Error reading Reference Minute from %s.\n",
552 __FILE__, __LINE__, cal_path);
570 float correction_factor;
575 correction_factor = 1.0 / (coef[0] -
576 coef[1] * (1.0 - exp(-coef[2] * param)) -
577 coef[3] * (1.0 - exp(-coef[4] * param)));
581 correction_factor = 1.0 / (coef[0] + coef[1] * param + coef[2] * pow(param, 2));
585 correction_factor = 1.0 / (coef[0] -
586 coef[1] * (1.0 - exp(-coef[2] * param)) -
591 return correction_factor;
625 epochs = dkrest_epoch;
626 cnt = num_dkrest_epoch;
629 for (
i = 0;
i < cnt;
i++) {
630 if (jsec > (
double) epochs[
i]) {
647 float32 loc_slopes[4];
648 float32 slopes[
NBAND][4][4];
649 int32 cnts[
NBAND][4][4];
654 for (
j = 0;
j < 4;
j++)
655 for (
k = 0;
k < 4;
k++) {
656 slopes[
i][
j][
k] = cnts2rad[
i][
j][
k];
657 cnts[
i][
j][
k] = det_off[
i][
j][
k];
661 for (
j = 0;
j < 4;
j++)
662 dets[
j] = tdi_list[
j][
tdi[
i]] - 1;
665 scnts[
k] = 1023 - cnts[
i][
j][dets[
k]];
666 srads[
k] = scnts[
k] * slopes[
i][
j][dets[
k]];
667 loc_slopes[
k] = slopes[
i][
j][dets[
k]];
673 for (
k = 1;
k < 5;
k++)
674 rads[
i][
j][
k] = srads[oindex[
k - 1]];
677 counts[
i][
j][1] = (scnts[oindex[0]] +
678 srads[oindex[0]] / loc_slopes[oindex[1]] +
679 srads[oindex[0]] / loc_slopes[oindex[2]] +
680 srads[oindex[0]] / loc_slopes[oindex[3]]) / 4.0;
682 counts[
i][
j][2] = (scnts[oindex[0]] + scnts[oindex[1]] +
683 srads[oindex[1]] / loc_slopes[oindex[2]] +
684 srads[oindex[1]] / loc_slopes[oindex[3]]) / 4.0;
687 counts[
i][
j][3] = (scnts[oindex[0]] + scnts[oindex[1]] +
689 srads[oindex[2]] / loc_slopes[oindex[3]]) / 4.0;
691 counts[
i][
j][4] = (scnts[oindex[0]] + scnts[oindex[1]] +
692 scnts[oindex[2]] + scnts[oindex[3]]) / 4.0;
702 char *
dtype, int32_t nsta, int32_t ninc, int32_t
npix,
704 short *scan_temp,
float *inst_temp,
int mside,
705 short *
l1a_data,
float *l1b_data, cal_mod_struc *cal_mod) {
708 int l1_data_lo, l1_data_hi;
713 float k1, gcorr, k3, k4, mirror;
714 int16 k1_ftype, gr_ftype, k4_ftype, ms_ftype;
715 int epoch_idx[
NCOEF];
718 float cal_offset = 0.;
719 int gain_flag, tdi_flag;
721 short count1, count2;
723 int called_calc_knees = 0;
726 delta_t = (jsec - (
double) ref_jsec) / 86400.;
734 if (scan_temp[
band] < 0)
736 if (scan_temp[
band] > 255)
737 scan_temp[
band] = 255;
764 if (
syear != prev_syear ||
sday != prev_sday
765 ||
smsec != prev_smsec || tdi_flag) {
774 called_calc_knees = 1;
785 if (called_calc_knees || gain_flag) {
786 called_calc_knees = 0;
790 for (knee = 1; knee <= 4; knee++) {
824 coef[
i] = radcor[epoch_idx[0]][
i][
band];
832 coef[
i] = g3corr[epoch_idx[4]][
i][
band];
840 if (cal_mod->flag == 1) {
841 k1 = cal_mod->gain[
band] * k1;
842 }
else if (cal_mod->flag == 2) {
843 cal_offset = cal_mod->offset[
band];
845 else if (cal_mod->flag == 3) {
846 k1 = cal_mod->gain[
band] * k1;
847 cal_offset = cal_mod->offset[
band];
849 cal_mod->gain[
band] = k1;
850 cal_mod->offset[
band] = cal_offset;
855 coef[
i] = fptempcor[epoch_idx[1]][
i][
band];
858 k3 = (1.0 + coef[0]*(fp_temp[
band][scan_temp[
band]] - coef[1]));
865 if (
mside == 1) mirror = 1.0 / mirror;
869 coef[
i] = scanmod[epoch_idx[3]][
i][
band];
875 if ((strcmp(
dtype,
"SOL") != 0) && (strcmp(
dtype,
"TDI") != 0) &&
876 (strcmp(
dtype,
"IGC") != 0))
882 if (haveDarkRestore) {
885 dark = ceil(dark_restore[dkidx][gn[
band]][
band]);
886 dark_ratio = (
float) dark - dark_restore[dkidx][gn[
band]][
band];
897 if (l1_data_lo > 1022)
900 l1_data_hi = l1_data_lo + 1;
902 l1b_data[
band *
npix +
pixel] = (k1 * gcorr * k3 * k4 * mirror * (g_f[
band][l1_data_lo]*(1 - dark_ratio) + g_f[
band][l1_data_hi] * dark_ratio) + cal_offset);