33 p = malloc(m *
sizeof (
float *));
35 for (
i = 0;
i < m;
i++) {
36 p[
i ] = malloc(n *
sizeof (
float **));
37 for (
j = 0;
j < n;
j++) {
38 p[
i ][
j ] = malloc(o *
sizeof (
float ***));
48 p = malloc(l *
sizeof (
float *));
50 for (
i = 0;
i < l;
i++) {
51 p[
i ] = malloc(m *
sizeof (
float **));
52 for (
j = 0;
j < m;
j++) {
53 p[
i ][
j ] = malloc(n *
sizeof (
float ***));
54 for (
k = 0;
k < n;
k++) {
55 p[
i ][
j ][
k ] = malloc(o *
sizeof (
float ****));
68 p = malloc(m *
sizeof (int32_t *));
70 for (
i = 0;
i < m;
i++) {
71 p[
i ] = malloc(n *
sizeof (int32_t **));
72 for (
j = 0;
j < n;
j++) {
73 p[
i ][
j ] = malloc(o *
sizeof (int32_t ***));
97 static short ftype[
NK];
99 static short gain3corr;
100 static float ***radcor;
101 static int32_t *k1_epoch;
103 static int num_k1_epoch;
105 static float ***g3corr;
106 static int32_t *gr_epoch;
108 static int num_gr_epoch;
110 static float ***cnts2rad;
111 static int32_t ***det_off;
113 static float ***fptempcor;
114 static int32_t *k3_epoch;
116 static int num_k3_epoch;
118 static float ***scanmod;
119 static int32_t *k4_epoch;
121 static int num_k4_epoch;
123 static float ****msidecor;
124 static int32_t *ms_epoch;
126 static int num_ms_epoch;
128 static float ***dark_restore;
129 static int32_t *dkrest_epoch;
130 static int num_dkrest_epoch;
131 static int haveDarkRestore = 0;
135 static float g_f[
NBAND][1024];
139 static int32_t ref_min;
140 static short prev_tdi[
NBAND] = {-1, -1, -1, -1, -1, -1, -1, -1};
141 static short prev_gain[
NBAND] = {-1, -1, -1, -1, -1, -1, -1, -1};
142 static int32_t prev_syear;
143 static int32_t prev_sday;
144 static int32_t prev_smsec;
145 static double ref_jsec;
154 static int firstCall = 1;
155 char name [H4_MAX_NC_NAME] =
"";
156 char sdsname[H4_MAX_NC_NAME] =
"";
162 int32 dims[H4_MAX_VAR_DIMS];
163 int32 nattrs, attr_index,
count, num_type;
165 int32 start4[4] = {0, 0, 0, 0};
166 int32
start[3] = {0, 0, 0};
174 if (!firstCall)
return;
177 sd_id = SDstart(cal_path, DFACC_RDONLY);
179 fprintf(
stderr,
"-E- %s line %d: SDstart(%s, %d) failed.\n",
180 __FILE__, __LINE__, cal_path, DFACC_RDONLY);
186 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
190 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
191 __FILE__, __LINE__, sdsname, cal_path);
194 status = SDendaccess(sds_id);
198 strcpy(sdsname,
"radiometric_coef");
199 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
205 r = (
float *) malloc(m *
sizeof (
float *));
207 attr_index = SDfindattr(sds_id,
"k1_epochs");
208 status = SDattrinfo(sds_id, attr_index, attr_name, &num_type, &
count);
209 num_k1_epoch =
count;
210 k1_epoch = (int32_t *) malloc(
count *
sizeof (int32_t *));
211 status = SDreadattr(sds_id, attr_index, k1_epoch);
221 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
222 __FILE__, __LINE__, sdsname, cal_path);
225 status = SDendaccess(sds_id);
226 for (
i = 0;
i < dims[0];
i++) {
227 for (
j = 0;
j < dims[1];
j++) {
228 for (
k = 0;
k < dims[2];
k++) {
229 m =
i * dims[1] * dims[2] +
j * dims[2] +
k;
230 radcor[
i][
j][
k] =
r[m];
239 strcpy(sdsname,
"dark_counts");
240 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
248 r = (
float *) malloc(m *
sizeof (
float *));
250 attr_index = SDfindattr(sds_id,
"dn0_epochs");
251 status = SDattrinfo(sds_id, attr_index, attr_name, &num_type, &
count);
252 num_dkrest_epoch =
count;
253 dkrest_epoch = (int32_t *) malloc(
count *
sizeof (int32_t *));
254 status = SDreadattr(sds_id, attr_index, dkrest_epoch);
257 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
258 __FILE__, __LINE__, sdsname, cal_path);
261 status = SDendaccess(sds_id);
262 for (
i = 0;
i < dims[0];
i++) {
263 for (
j = 0;
j < dims[1];
j++) {
264 for (
k = 0;
k < dims[2];
k++) {
265 m =
i * dims[1] * dims[2] +
j * dims[2] +
k;
266 dark_restore[
i][
j][
k] =
r[m];
277 strcpy(sdsname,
"gainratio_coef");
278 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
282 printf(
"\nGain 3 correction applied.\n");
288 r = (
float *) malloc(m *
sizeof (
float *));
290 attr_index = SDfindattr(sds_id,
"gr_epochs");
291 status = SDattrinfo(sds_id, attr_index, attr_name, &num_type, &
count);
292 num_gr_epoch =
count;
293 gr_epoch = (int32_t *) malloc(
count *
sizeof (int32_t *));
294 status = SDreadattr(sds_id, attr_index, gr_epoch);
303 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
304 __FILE__, __LINE__, sdsname, cal_path);
307 status = SDendaccess(sds_id);
308 for (
i = 0;
i < dims[0];
i++) {
309 for (
j = 0;
j < dims[1];
j++) {
310 for (
k = 0;
k < dims[2];
k++) {
311 m =
i * dims[1] * dims[2] +
j * dims[2] +
k;
312 g3corr[
i][
j][
k] =
r[m];
324 strcpy(sdsname,
"counts_to_radiance");
325 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
331 r = (
float *) malloc(m *
sizeof (
float *));
335 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
336 __FILE__, __LINE__, sdsname, cal_path);
339 status = SDendaccess(sds_id);
340 for (
i = 0;
i < dims[0];
i++) {
341 for (
j = 0;
j < dims[1];
j++) {
342 for (
k = 0;
k < dims[2];
k++) {
343 m =
i * dims[1] * dims[2] +
j * dims[2] +
k;
344 cnts2rad[
k][
j][
i] =
r[m];
354 strcpy(sdsname,
"detector_offsets");
355 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
361 dr = (int32_t *) malloc(m *
sizeof (int32_t *));
365 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
366 __FILE__, __LINE__, sdsname, cal_path);
369 status = SDendaccess(sds_id);
370 for (
i = 0;
i < dims[0];
i++) {
371 for (
j = 0;
j < dims[1];
j++) {
372 for (
k = 0;
k < dims[2];
k++) {
373 m =
i * dims[1] * dims[2] +
j * dims[2] +
k;
374 det_off[
k][
j][
i] = dr[m];
383 strcpy(sdsname,
"temperature_coef");
384 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
390 r = (
float *) malloc(m *
sizeof (
float *));
392 attr_index = SDfindattr(sds_id,
"k3_epochs");
393 status = SDattrinfo(sds_id, attr_index, attr_name, &num_type, &
count);
394 num_k3_epoch =
count;
395 k3_epoch = (int32_t *) malloc(
count *
sizeof (int32_t *));
396 status = SDreadattr(sds_id, attr_index, k3_epoch);
405 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
406 __FILE__, __LINE__, sdsname, cal_path);
409 status = SDendaccess(sds_id);
410 for (
i = 0;
i < dims[0];
i++) {
411 for (
j = 0;
j < dims[1];
j++) {
412 for (
k = 0;
k < dims[2];
k++) {
413 m =
i * dims[1] * dims[2] +
j * dims[2] +
k;
414 fptempcor[
i][
j][
k] =
r[m];
423 strcpy(sdsname,
"scan_mod_coef");
424 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
430 r = (
float *) malloc(m *
sizeof (
float *));
432 attr_index = SDfindattr(sds_id,
"k4_epochs");
433 status = SDattrinfo(sds_id, attr_index, attr_name, &num_type, &
count);
434 num_k4_epoch =
count;
435 k4_epoch = (int32_t *) malloc(
count *
sizeof (int32_t *));
436 status = SDreadattr(sds_id, attr_index, k4_epoch);
445 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
446 __FILE__, __LINE__, sdsname, cal_path);
449 status = SDendaccess(sds_id);
450 for (
i = 0;
i < dims[0];
i++) {
451 for (
j = 0;
j < dims[1];
j++) {
452 for (
k = 0;
k < dims[2];
k++) {
453 m =
i * dims[1] * dims[2] +
j * dims[2] +
k;
454 scanmod[
i][
j][
k] =
r[m];
463 strcpy(sdsname,
"mirror_side_coef");
464 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
466 msidecor =
alloc4d_float(dims[0], dims[1], dims[2], dims[3]);
470 r = (
float *) malloc(m *
sizeof (
float *));
472 attr_index = SDfindattr(sds_id,
"ms_epochs");
473 status = SDattrinfo(sds_id, attr_index, attr_name, &num_type, &
count);
474 num_ms_epoch =
count;
475 ms_epoch = (int32_t *) malloc(
count *
sizeof (int32_t *));
476 status = SDreadattr(sds_id, attr_index, ms_epoch);
483 status = SDreaddata(sds_id, start4,
NULL, dims,
r);
485 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
486 __FILE__, __LINE__, sdsname, cal_path);
489 status = SDendaccess(sds_id);
490 for (
i = 0;
i < dims[0];
i++) {
491 for (
j = 0;
j < dims[1];
j++) {
492 for (
k = 0;
k < dims[2];
k++) {
493 for (l = 0; l < dims[3]; l++) {
494 m =
i * dims[1] * dims[2] * dims[3] +
j * dims[2] * dims[3] +
k * dims[3] + l;
495 msidecor[
i][
j][
k][l] =
r[m];
505 strcpy(sdsname,
"fp_temp");
506 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
511 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
512 __FILE__, __LINE__, sdsname, cal_path);
515 status = SDendaccess(sds_id);
519 strcpy(sdsname,
"TDI_list");
520 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
525 printf(
"-E- %s Line %d: Error reading SDS %s from %s.\n",
526 __FILE__, __LINE__, sdsname, cal_path);
529 status = SDendaccess(sds_id);
533 attr_index = SDfindattr(sd_id,
"Reference Year");
536 printf(
"-E- %s Line %d: Error reading Reference Year from %s.\n",
537 __FILE__, __LINE__, cal_path);
540 attr_index = SDfindattr(sd_id,
"Reference Day");
543 printf(
"-E- %s Line %d: Error reading Reference Day from %s.\n",
544 __FILE__, __LINE__, cal_path);
547 attr_index = SDfindattr(sd_id,
"Reference Minute");
548 status = SDreadattr(sd_id, attr_index, &ref_min);
550 printf(
"-E- %s Line %d: Error reading Reference Minute from %s.\n",
551 __FILE__, __LINE__, cal_path);
569 float correction_factor;
574 correction_factor = 1.0 / (coef[0] -
575 coef[1] * (1.0 - exp(-coef[2] * param)) -
576 coef[3] * (1.0 - exp(-coef[4] * param)));
580 correction_factor = 1.0 / (coef[0] + coef[1] * param + coef[2] * pow(param, 2));
584 correction_factor = 1.0 / (coef[0] -
585 coef[1] * (1.0 - exp(-coef[2] * param)) -
590 return correction_factor;
624 epochs = dkrest_epoch;
625 cnt = num_dkrest_epoch;
628 for (
i = 0;
i < cnt;
i++) {
629 if (jsec > (
double) epochs[
i]) {
646 float32 loc_slopes[4];
647 float32 slopes[
NBAND][4][4];
648 int32 cnts[
NBAND][4][4];
653 for (
j = 0;
j < 4;
j++)
654 for (
k = 0;
k < 4;
k++) {
655 slopes[
i][
j][
k] = cnts2rad[
i][
j][
k];
656 cnts[
i][
j][
k] = det_off[
i][
j][
k];
660 for (
j = 0;
j < 4;
j++)
661 dets[
j] = tdi_list[
j][
tdi[
i]] - 1;
664 scnts[
k] = 1023 - cnts[
i][
j][dets[
k]];
665 srads[
k] = scnts[
k] * slopes[
i][
j][dets[
k]];
666 loc_slopes[
k] = slopes[
i][
j][dets[
k]];
672 for (
k = 1;
k < 5;
k++)
673 rads[
i][
j][
k] = srads[oindex[
k - 1]];
676 counts[
i][
j][1] = (scnts[oindex[0]] +
677 srads[oindex[0]] / loc_slopes[oindex[1]] +
678 srads[oindex[0]] / loc_slopes[oindex[2]] +
679 srads[oindex[0]] / loc_slopes[oindex[3]]) / 4.0;
681 counts[
i][
j][2] = (scnts[oindex[0]] + scnts[oindex[1]] +
682 srads[oindex[1]] / loc_slopes[oindex[2]] +
683 srads[oindex[1]] / loc_slopes[oindex[3]]) / 4.0;
686 counts[
i][
j][3] = (scnts[oindex[0]] + scnts[oindex[1]] +
688 srads[oindex[2]] / loc_slopes[oindex[3]]) / 4.0;
690 counts[
i][
j][4] = (scnts[oindex[0]] + scnts[oindex[1]] +
691 scnts[oindex[2]] + scnts[oindex[3]]) / 4.0;
700 int32_t
l1b_rad(
int syear,
int sday, int32_t smsec, int32_t msec,
701 char *
dtype, int32_t nsta, int32_t ninc, int32_t
npix,
703 short *scan_temp,
float *inst_temp,
int mside,
704 short *
l1a_data,
float *l1b_data, cal_mod_struc *cal_mod) {
707 int l1_data_lo, l1_data_hi;
712 float k1, gcorr, k3, k4, mirror;
713 int16 k1_ftype, gr_ftype, k4_ftype, ms_ftype;
714 int epoch_idx[
NCOEF];
717 float cal_offset = 0.;
718 int gain_flag, tdi_flag;
720 short count1, count2;
722 int called_calc_knees = 0;
725 delta_t = (jsec - (
double) ref_jsec) / 86400.;
733 if (scan_temp[
band] < 0)
735 if (scan_temp[
band] > 255)
736 scan_temp[
band] = 255;
763 if (
syear != prev_syear ||
sday != prev_sday
764 ||
smsec != prev_smsec || tdi_flag) {
773 called_calc_knees = 1;
784 if (called_calc_knees || gain_flag) {
785 called_calc_knees = 0;
789 for (knee = 1; knee <= 4; knee++) {
823 coef[
i] = radcor[epoch_idx[0]][
i][
band];
831 coef[
i] = g3corr[epoch_idx[4]][
i][
band];
839 if (cal_mod->flag == 1) {
840 k1 = cal_mod->gain[
band] * k1;
841 }
else if (cal_mod->flag == 2) {
842 cal_offset = cal_mod->offset[
band];
844 else if (cal_mod->flag == 3) {
845 k1 = cal_mod->gain[
band] * k1;
846 cal_offset = cal_mod->offset[
band];
848 cal_mod->gain[
band] = k1;
849 cal_mod->offset[
band] = cal_offset;
854 coef[
i] = fptempcor[epoch_idx[1]][
i][
band];
857 k3 = (1.0 + coef[0]*(fp_temp[
band][scan_temp[
band]] - coef[1]));
864 if (
mside == 1) mirror = 1.0 / mirror;
868 coef[
i] = scanmod[epoch_idx[3]][
i][
band];
874 if ((strcmp(
dtype,
"SOL") != 0) && (strcmp(
dtype,
"TDI") != 0) &&
875 (strcmp(
dtype,
"IGC") != 0))
881 if (haveDarkRestore) {
884 dark = ceil(dark_restore[dkidx][gn[
band]][
band]);
885 dark_ratio = (
float) dark - dark_restore[dkidx][gn[
band]][
band];
896 if (l1_data_lo > 1022)
899 l1_data_hi = l1_data_lo + 1;
901 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);