17 #include <sys/types.h>
24 #define STDMAXSCANS 48
30 #define near( x, y ) ( fabsf( x - y ) < 0.001 )
33 #define NA_FLOAT32_FILL -999.9f
34 #define NA_UINT16_FILL 65535
35 #define MISS_FLOAT32_FILL -999.8f
36 #define MISS_UINT16_FILL 65534
37 #define ONBOARD_PT_FLOAT32_FILL -999.7f
38 #define ONBOARD_PT_UINT16_FILL 65533
39 #define ONGROUND_PT_FLOAT32_FILL -999.6f
40 #define ONGROUND_PT_UINT16_FILL 65532
41 #define ERR_FLOAT32_FILL -999.5f
42 #define ERR_UINT16_FILL 65531
43 #define ELINT_FLOAT32_FILL -999.4f
44 #define ELINT_UINT16_FILL 65530
45 #define VDNE_FLOAT32_FILL -999.3f
46 #define VDNE_UINT16_FILL 65529
47 #define SOUB_FLOAT32_FILL -999.2f
48 #define SOUB_UINT16_FILL 65528
56 static char vfnames[
MAXBANDS][FILENAME_MAX];
57 static h5io_str geo_fid, sdr_fid[
MAXBANDS];
58 static h5io_str geo_dat_id[7], bnd_dat_id[2][
MAXBANDS];
60 static unsigned short *scl_rad;
61 static float *scale, *
offset;
62 static float *flt_rad;
63 static int64_t *u58_scn_st, u58_gran_st, u58_gran_en;
65 static unsigned char *viirs_qual2;
76 static float *
pos, *vel, *att;
77 static int16_t scn_fmt;
78 static int16_t margin[2];
83 static int32_t btype[] = {0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 1, 1, 1, 1};
84 static int32_t ivswir = 0;
85 static int32_t itherm = 0;
89 char tmp[FILENAME_MAX];
90 char dir[FILENAME_MAX];
91 char base[FILENAME_MAX];
94 if( access(
name, R_OK ) == 0 ) {
106 struct dirent *entry;
107 if((
dp = opendir(dir)) ==
NULL) {
108 fprintf(
stderr,
"%s:%d - cannot open directory: %s\n", __FILE__, __LINE__, dir);
113 while((entry = readdir(
dp)) !=
NULL) {
114 if(strncmp(entry->d_name,
base, 44) == 0){
116 sprintf(
name,
"%s/%s", dir, entry->d_name);
123 fprintf(
stderr,
"%s:%d - valid VIIRS file not found: %s\n", __FILE__, __LINE__,
name);
151 char *
path, temp[FILENAME_MAX],
base[FILENAME_MAX];
160 if ((fp = fopen(in_file,
"r")) ==
NULL) {
161 fprintf(
stderr,
"-E- %s, %d: input file %s open problem.\n",
162 __FILE__, __LINE__, in_file);
169 if (fscanf(fp,
"%s", vfnames[
i]) != 1) {
171 "-E- %s, %d: Failed to read VIIRS input file # %d from list file: %s/n",
172 __FILE__, __LINE__,
i, in_file);
186 path = dirname(in_file);
188 if (strncmp(
base,
"SVM", 3) != 0) {
190 "-E- %s, %d: Improper VIIRS file format for file base:\n",
197 "-E- %s, %d: Improper VIIRS file format for file base:\n",
203 sprintf(vfnames[
i],
"%s/SVM%2.2d%s",
path,
j,
base + 5);
206 printf(
"VIIRS filename for M%2.2d: %s\n",
j, vfnames[
i]);
240 char *in_calfile, sdr_cal_fil[FILENAME_MAX], *csave =
NULL;
241 char *aux_arr, *fildir;
242 int ncorr, icorr, f_ndim, f_dim_siz[20], f_sto_len,
i;
252 for (icorr = 0; icorr < ncorr; icorr++)
253 *(f_cal_corr + icorr) = 1.;
260 printf(
"\nVIIRS calibration starting\n");
261 if (*in_calfile == 0) {
263 printf(
"No cal file supplied, calibration remains unchanged\n");
267 printf(
"Input calibration from file: %s\n", in_calfile);
276 if (strstr(in_calfile,
"Unity") ==
NULL) {
278 printf(
"%s, %d - E - failed to retrieve input F table values\n",
283 printf(
"Input calibration is from the unity F table, Using unity\n");
291 if (
h5io_info(g_id,
"N_Aux_Filename", &f_class, &f_native_typ, &f_ndim,
292 f_dim_siz, &f_sto_len) != 0) {
293 printf(
"%s, %d - E - could not get info on N_Aux_Filename\n", __FILE__,
300 if ((aux_arr = (
char *) malloc(f_sto_len * f_dim_siz[0] *
sizeof (
char)))
302 printf(
"%s, %d - E - unable to allocate aux filename list array\n",
309 if (
h5io_rd_attr(g_id,
"N_Aux_Filename", aux_arr) != 0) {
310 printf(
"%s, %d - failed to read the N_Aux_Filename attribute\n",
317 for (
i = 0;
i < f_dim_siz[0];
i++) {
319 if (strstr((aux_arr +
i * f_sto_len),
"VIIRS-SDR-F-LUT_npp") !=
NULL)
320 csave = (aux_arr +
i * f_sto_len);
327 printf(
"%s ,%d: I: No F-LUT found, Looking for F-PREDICTED-LUT\n",
329 for (
i = 0;
i < f_dim_siz[0];
i++) {
331 if (strstr((aux_arr +
i * f_sto_len),
332 "VIIRS-SDR-F-PREDICTED-LUT_npp") !=
NULL)
333 csave = (aux_arr +
i * f_sto_len);
337 "%s, %d E - failed to find the F LUT name in N_Aux_Filename\n",
342 "%s, %d - A F-PREDICTED-LUT was found, but code is not set-up\n",
345 " to handle this kind of LUT. Set 'calfile=' in par file\n");
346 printf(
" to leave cal unchanged\n");
351 printf(
"\n\nThe F LUT found in the SDR is: %s\n", csave);
355 if (strstr(csave,
"Unity") !=
NULL) {
357 printf(
"Found a unity F table designation in the SDR\n");
363 if ((fildir = getenv(
"OCVARROOT")) ==
NULL) {
364 printf(
"-E- %s, %d: OCVARROOT env variable undefined.\n", __FILE__,
368 strcpy(sdr_cal_fil, fildir);
369 strcat(sdr_cal_fil,
"/viirsn/cal/EVAL/");
370 strcat(sdr_cal_fil, csave);
371 strcat(sdr_cal_fil,
".h5");
372 printf(
"F Lut file name derived from the SDR is: %s\n", sdr_cal_fil);
378 printf(
"-E- %s, %d: Failed to retrieve SDR F table values\n",
387 printf(
"Completed calibration using F LUT file(s)\n\n");
418 double f_lut_sel[2 * 16 * 16 * 2];
419 int64_t *tarr, del, dist, u58_t1, u58_t2;
423 h5io_str h5fid, dsid;
424 int f_ndim, f_dim_siz[20], f_sto_len, ntime, itim1, ncorr,
i;
430 printf(
"Reading in F LUT: %s\n",
file);
432 printf(
"- E - %s, %d: Unable to open the F LUT file: %s\n", __FILE__,
439 if (
h5io_set_ds(&h5fid,
"Beginning_Time_IET", &dsid) != 0) {
440 printf(
"- E - %s, %d: Unable to set dataset Beginning_Time_IET\n",
444 if (
h5io_info(&dsid,
NULL, &f_class, &f_native_typ, &f_ndim, f_dim_siz,
446 printf(
"- E - %s, %d: Unable to get info on Beginning_Time_IET\n",
450 ntime = f_dim_siz[0];
454 if ((tarr = (int64_t *) malloc(ntime *
sizeof (int64_t))) ==
NULL) {
455 printf(
"- E - %s, %d: Unable to allocate time storage\n", __FILE__,
460 printf(
"- E - %s, %d: Unable to read Beginning_Time_IET\n", __FILE__,
469 printf(
"Granule time is: %ld\n", (
long) u58_time);
470 if (u58_time < tarr[0]) {
472 "\n\n\n- W - %s, %d: WARNING, granule time below times in the F table\n",
475 printf(
"granule time of %ld is below table start time of %ld\n",
476 (
long) u58_time, (
long) tarr[0]);
477 }
else if (u58_time > *(tarr + ntime - 1)) {
479 "\n\n\n- W - %s, %d: WARNING, granule time higher than times in the F table\n",
482 printf(
"granule time of %ld is above table end time of %ld\n", (
long) u58_time,
483 (
long) tarr[ntime - 1]);
485 for (itim1 = 0; itim1 < ntime; itim1++)
486 if (*(tarr + itim1 + 1) > u58_time)
492 u58_t1 = *(tarr + itim1);
493 u58_t2 = *(tarr + itim1 + 1);
495 printf(
"itim1: %d, t1: %ld, t2: %ld\n", itim1, (
long) u58_t1, (
long) u58_t2);
500 if (
h5io_set_ds(&h5fid,
"VIIRS-SDR-F-LUT", &dsid) != 0) {
501 printf(
"- E - %s, %d: Unable to set dataset VIIRS-SDR-F-LUT\n",
505 if (
h5io_info(&dsid,
NULL, &f_class, &f_native_typ, &f_ndim, f_dim_siz,
507 printf(
"- E - %s, %d: Unable to get info on dataset VIIRS-SDR-F-LUT\n",
512 printf(
"- E - %s, %d: # dimensions of LUT dataset is not 5\n", __FILE__,
516 if ((f_dim_siz[0] != ntime) || (f_dim_siz[1] != 22) || (f_dim_siz[2] != 32)
517 || (f_dim_siz[3] != 3) || (f_dim_siz[4] != 2)) {
518 printf(
"- E - %s, %d: LUT dataset dimension sizes are unexpected\n",
538 printf(
"- E - %s, %d: Unable to read the VIIRS-SDR-F-LUT dataset\n",
546 del = u58_t2 - u58_t1;
547 dist = u58_time - u58_t1;
550 printf(
"Interp factor from t2: %f\n",
fact);
553 for (
i = 0;
i < ncorr;
i++)
554 *(f_cal_corr +
i) *= *(f_lut_sel +
i) * (1. -
fact)
555 + *(f_lut_sel +
i + ncorr) *
fact;
557 for (
i = 0;
i < ncorr;
i++)
558 *(f_cal_corr +
i) /= *(f_lut_sel +
i) * (1. -
fact)
559 + *(f_lut_sel +
i + ncorr) *
fact;
578 hid_t *base_grp, spaceid, attr_id;
581 &(
id->dat_id) : &(
id->grp_id);
582 if ((attr_id = H5Aopen_name(*base_grp, attr_name)) < 0)
585 if ((spaceid = H5Aget_space(attr_id)) < 0)
588 if(H5Sget_simple_extent_npoints(spaceid) != 1) {
589 printf(
"%s: # of elements of attribute: %s has to be 1\n", __FILE__, attr_name);
593 if (H5Aread(attr_id, H5T_NATIVE_INT,
val) < 0)
599 if (H5Sclose(spaceid) < 0)
601 if (H5Aclose(attr_id) < 0)
630 h5io_str g_id, id_tmp;
631 char g_path[100], geo_all_data_gnam[200], *band_dark, **grp_obj_nm;
632 char *geo_name[] = {
"Latitude",
"Longitude",
"SatelliteAzimuthAngle",
633 "SatelliteZenithAngle",
"SolarAzimuthAngle",
"SolarZenithAngle",
636 H5T_class_t h5_class;
638 int ndim, dim_siz[4], sto_len, n_obj, *grp_obj_typ;
640 unsigned char *cmp_vq2;
641 int16_t ua_scn_fmt, ua_ndets, ua_margin[2];
642 int32_t ua_npix, ua_nlin, attr_exist;
643 int32_t qual2_found = 0, ngran, dgran,
iscan, dk;
653 if(
file->geofile[0] == 0) {
655 file->geofile[0] =
'G';
656 file->geofile[1] =
'M';
657 file->geofile[2] =
'T';
658 file->geofile[3] =
'C';
659 file->geofile[4] =
'O';
668 for (ibnd = 0, jbnd = 1; ibnd <
MAXBANDS; ibnd++, jbnd++) {
670 if (
h5io_openr(vfnames[ibnd], 0, (sdr_fid + ibnd)) != 0) {
671 fprintf(
stderr,
"-E- %s Line %d: Failure to open %s\n", __FILE__,
672 __LINE__, vfnames[ibnd]);
678 sprintf(g_path,
"Data_Products/VIIRS-M%d-SDR/VIIRS-M%d-SDR_Aggr", jbnd,
680 if (
h5io_set_ds((sdr_fid + ibnd), g_path, &g_id) != 0) {
682 "-E- %s, %d: Failure to set aggregation dataset, band %d:\n",
683 __FILE__, __LINE__, ibnd);
684 fprintf(
stderr,
"name: %s\n", vfnames[ibnd]);
690 "-E- %s, %d: Unable to read the AggregateNumberGranules attribute\n",
692 fprintf(
stderr,
"band %d\n, name: %s\n", ibnd, vfnames[ibnd]);
696 fprintf(
stderr,
"-E- %s Line %d: Unable to close granule, ds %d\n",
697 __FILE__, __LINE__, ibnd);
708 if ((scale = (
float *) malloc(
MAXBANDS * ngran *
sizeof (
float)))
711 "-E- %s, %d: failure to allocate scale storage\n",
715 if ((
offset = (
float *) malloc(
MAXBANDS * ngran *
sizeof (
float)))
718 "-E- %s, %d: failure to allocate offset storage\n",
722 if ((nscan_inst = (
int *) malloc(ngran *
sizeof (
int))) ==
NULL) {
724 "-E- %s, %d: failure to allocate nscan_inst storage\n",
728 if ((nscan_stat = (
int *) malloc(ngran *
sizeof (
int))) ==
NULL) {
730 "-E- %s, %d: failure to allocate nscan_stat storage\n",
734 if ((band_dark = (
char *) calloc(ngran *
MAXBANDS,
sizeof (
char)))
737 "-E- %s, %d: failure to allocate band_dark storage\n",
741 if ((rad_fact = (
float *) malloc(ngran * 2 *
sizeof (
float))) ==
NULL) {
743 "-E- %s, %d: failure to allocate rad_fact storage\n",
748 if (dgran != ngran) {
750 "-E- %s, %d: Mismatch in AggregateNumberOfGranules\n",
752 fprintf(
stderr,
"band %d\n, name: %s\n", ibnd, vfnames[ibnd]);
761 for (igran = 0; igran < ngran; igran++) {
762 sprintf(g_path,
"Data_Products/VIIRS-M%d-SDR/VIIRS-M%d-SDR_Gran_%d",
764 if (
h5io_set_ds((sdr_fid + ibnd), g_path, &g_id) != 0) {
766 "-E- %s Line %d: Failure to set granule dataset, ds %d:\n",
767 __FILE__, __LINE__, ibnd);
768 fprintf(
stderr,
"name: %s", vfnames[ibnd]);
773 (
void *) (nscan_inst + igran)) != 0) {
775 "-E- %s Line %d: Unable to read the N_Number_Of_Scans ds attr, ds %d:\n",
776 __FILE__, __LINE__, ibnd);
777 fprintf(
stderr,
"name: %s", vfnames[ibnd]);
780 if (nscan_inst[igran] <= 0) {
782 "-E- %s, %d: N_Number_Of_Scans[%d]: %d invalid\n",
783 __FILE__, __LINE__, igran, nscan_inst[igran]);
787 nscan_inst_tot += nscan_inst[igran];
790 if ((igran == 0) && (ibnd == 0)) {
798 if ((gran_map = (
int *) malloc(
799 ngran * nscan_per_gran *
sizeof (
int))) ==
NULL) {
801 "-E- %s, %d: failure to allocate gran_map storage\n",
805 if ((scan_map = (
int *) malloc(
806 ngran * nscan_per_gran *
sizeof (
int))) ==
NULL) {
808 "-E- %s, %d: failure to allocate scan_map storage\n",
812 for (
i = 0;
i < ngran * nscan_per_gran;
i++) {
817 for (
i = 0;
i < nscan_inst[igran];
i++) {
818 scan_map[
iscan] = igran * nscan_per_gran +
i;
819 gran_map[
iscan++] = igran;
824 if (
h5io_rd_attr(&g_id,
"N_Beginning_Time_IET", (
void *) &u58_gran)
827 "-E- %s Line %d: Unable to read the N_Beginning_Time_IET ds attr, ds %d:\n",
828 __FILE__, __LINE__, ibnd);
829 fprintf(
stderr,
"name: %s", vfnames[ibnd]);
833 u58_gran_st = u58_gran;
834 else if ((u58_gran > 0) && (u58_gran < u58_gran_st))
835 u58_gran_st = u58_gran;
837 if (
h5io_rd_attr(&g_id,
"N_Ending_Time_IET", (
void *) &u58_gran)
840 "-E- %s Line %d: Unable to read the N_Ending_Time_IET ds attr, ds %d:\n",
841 __FILE__, __LINE__, ibnd);
842 fprintf(
stderr,
"name: %s", vfnames[ibnd]);
846 u58_gran_en = u58_gran;
847 else if (u58_gran > u58_gran_en)
848 u58_gran_en = u58_gran;
856 if ((ibnd == 0) && (igran == ngran - 1))
862 "-E- %s Line %d: Unable to close granule, ds %d\n",
863 __FILE__, __LINE__, ibnd);
869 sprintf(g_path,
"All_Data/VIIRS-M%d-SDR_All/NumberOfScans", jbnd);
870 if (
h5io_grab_ds((sdr_fid + ibnd), g_path, (
void *) nscan_stat) != 0) {
872 "-E- %s, %d: Unable to read the # scans from NumberOfScans, band %d\n",
873 __FILE__, __LINE__, ibnd);
876 for (igran = 0; igran < ngran; igran++) {
878 if (nscan_stat[igran] == -993) {
879 *(band_dark + ibnd +
MAXBANDS * igran) = 1;
881 if (nscan_stat[igran] != nscan_inst[igran]) {
883 "-E- %s, %d: dataset #scans: %d not = to granule # scans: %d \n",
884 __FILE__, __LINE__, nscan_stat[igran],
886 fprintf(
stderr,
"band: %d, granule: %d\n", ibnd, igran);
901 sprintf(g_path,
"All_Data/VIIRS-M%d-SDR_All/Radiance", jbnd);
902 if (
h5io_set_ds((sdr_fid + ibnd), g_path, &g_id) != 0) {
904 "-E- %s Line %d: Getting nscan_sto, Unable to open path\n",
909 dim_siz, &sto_len) != 0) {
911 "-E- %s Line %d: Getting nscan_sto, Unable to read dataset\n",
915 nscan_sto = dim_siz[0] /
NDET;
918 "-E- %s Line %d: Getting nscan_sto, Unable to close path\n",
923 printf(
"%s: total # inst scans: %d, dataset scans: %d\n",
924 __FILE__, nscan_inst_tot, nscan_sto);
925 if (nscan_sto < nscan_inst_tot) {
926 fprintf(
stderr,
"-E- %s,%d: # aggregate scans < inst scans\n",
931 if ((viirs_qual2 = malloc(nscan_sto *
sizeof (
unsigned char)))
934 "-E- %s Line %d: mem allocation error for viirs_qual2\n",
938 if ((cmp_vq2 = malloc(nscan_sto *
sizeof (
unsigned char))) ==
NULL) {
940 "-E- %s Line %d: mem allocation error for cmp_vq2\n",
948 sprintf(g_path,
"All_Data/VIIRS-M%d-SDR_All/QF2_SCAN_SDR", jbnd);
949 if (
h5io_grab_ds((sdr_fid + ibnd), g_path, (
void *) cmp_vq2) != 0) {
951 "-E- %s, %d: Unable to get QF2 for path %s, band %d\n",
952 __FILE__, __LINE__, g_path, ibnd);
959 nscan = nscan_inst_tot;
961 if (nscan_inst_tot !=
nscan) {
963 "-E- %s, %d: # inst scans: %d in band file: %s",
964 __FILE__, __LINE__, nscan_inst_tot, vfnames[ibnd]);
966 "is not the same as initially established (scans: %d)\n",
976 for (
i = 0;
i < ngran;
i++) {
977 if (*(band_dark + ibnd +
MAXBANDS *
i) == 1) {
983 if (qual2_found == 0) {
984 memcpy(viirs_qual2, cmp_vq2, nscan_sto);
986 for (
i = 0;
i < nscan_inst_tot;
i++) {
987 if ((*(viirs_qual2 + scan_map[
i]) & 2) != 0) {
988 file->sv_with_moon = 1;
989 printf(
"VIIRS_SV_LUNAR_INCURSION detected\n");
995 for (
i = 0;
i < nscan_inst_tot;
i++) {
996 if (*(viirs_qual2 + scan_map[
i])
997 != *(cmp_vq2 + scan_map[
i])) {
999 "-E- %s Line %d: QF2_SCAN_SDR mismatch, val# %d, bnd %d\n",
1000 __FILE__, __LINE__,
i, ibnd);
1010 if ((attr_exist =
h5io_attr_exist((sdr_fid + ibnd),
"Data Scan Format"))
1013 "-E- %s Line %d: failure while checking for attr Data Scan Format, band: %d\n",
1014 __FILE__, __LINE__, ibnd);
1017 if (attr_exist == 1) {
1028 (
void *) &ua_scn_fmt) != 0) {
1030 "-E- %s Line %d: Unable to read the Data Scan Format attr, ds %d: name: %s\n",
1031 __FILE__, __LINE__, ibnd, vfnames[ibnd]);
1034 if (
h5io_rd_attr((sdr_fid + ibnd),
"Scan Margin (track, scan)",
1035 (
void *) ua_margin) != 0) {
1037 "-E- %s Line %d: Unable to read the Scan Margin attr, ds %d: name: %s\n",
1038 __FILE__, __LINE__, ibnd, vfnames[ibnd]);
1041 if (
h5io_rd_attr((sdr_fid + ibnd),
"Pixels per Scan Line",
1042 (
void *) &ua_npix) != 0) {
1044 "-E- %s Line %d: Unable to read the Pixels per Scan Line attr, ds %d: name: %s\n",
1045 __FILE__, __LINE__, ibnd, vfnames[ibnd]);
1048 if (
h5io_rd_attr((sdr_fid + ibnd),
"Number of Scan Lines",
1049 (
void *) &ua_nlin) != 0) {
1051 "-E- %s Line %d: Unable to read the Number of Scan Lines attr, ds %d: name: %s\n",
1052 __FILE__, __LINE__, ibnd, vfnames[ibnd]);
1056 "Number of Detectors per Scan", (
void *) &ua_ndets)
1059 "-E- %s Line %d: Unable to read the Number of Detectors per Scan attr, ds %d: name: %s\n",
1060 __FILE__, __LINE__, ibnd, vfnames[ibnd]);
1069 margin[0] = ua_margin[0];
1070 margin[1] = ua_margin[1];
1071 file->npix = ua_npix;
1073 file->ndets = ua_ndets;
1074 file->nscan = ua_nlin;
1075 scn_fmt = ua_scn_fmt;
1078 if ((margin[0] != ua_margin[0]) || (margin[1] != ua_margin[1])) {
1080 "-E- %s Line %d: margin mismatch on band: %d name: %s\n",
1081 __FILE__, __LINE__, ibnd, vfnames[ibnd]);
1084 if (
file->npix != ua_npix) {
1086 "-E- %s Line %d: ua npix mismatch on band: %d name: %s\n",
1087 __FILE__, __LINE__, ibnd, vfnames[ibnd]);
1090 if (
file->ndets != ua_ndets) {
1092 "-E- %s Line %d: ua ndets mismatch on band: %d name: %s\n",
1093 __FILE__, __LINE__, ibnd, vfnames[ibnd]);
1098 "-E- %s Line %d: ua # lines mismatch on band: %d name: %s\n",
1099 __FILE__, __LINE__, ibnd, vfnames[ibnd]);
1102 if (scn_fmt != ua_scn_fmt) {
1104 "-E- %s Line %d: ua scan format mismatch on band: %d name: %s\n",
1105 __FILE__, __LINE__, ibnd, vfnames[ibnd]);
1114 for (ids = 0; ids < 1; ids++) {
1115 if (btype[ibnd] ==
VSWIR || btype[ibnd] ==
CIRRUS)
1116 sprintf(g_path,
"All_Data/VIIRS-M%d-SDR_All/%s", jbnd,
1119 sprintf(g_path,
"All_Data/VIIRS-M%d-SDR_All/%s", jbnd,
1122 if (
h5io_set_ds((sdr_fid + ibnd), g_path, &(bnd_dat_id[ids][ibnd]))
1125 "-E- %s Line %d: Error setting dataset, band %d, ds %d\n",
1126 __FILE__, __LINE__, ibnd, ids);
1134 &h5_native_typ, &ndim, dim_siz, &sto_len) != 0) {
1136 "-E- %s Line %d: Error accessing radiance dataset info, band %d\n",
1137 __FILE__, __LINE__, ibnd);
1140 if (h5_class == H5T_INTEGER)
1141 *(sdr_band_typ + ibnd) = 0;
1143 *(sdr_band_typ + ibnd) = 1;
1149 if (*(sdr_band_typ + ibnd) == 0) {
1150 if (btype[ibnd] ==
VSWIR || btype[ibnd] ==
CIRRUS)
1151 sprintf(g_path,
"All_Data/VIIRS-M%d-SDR_All/ReflectanceFactors",
1154 sprintf(g_path,
"All_Data/VIIRS-M%d-SDR_All/RadianceFactors",
1157 if (
h5io_grab_ds((sdr_fid + ibnd), g_path, (
void *) rad_fact)
1160 "-E- %s Line %d: Unable to read the rad fact, ds %d\n",
1161 __FILE__, __LINE__, ibnd);
1164 for (igran = 0; igran < ngran; igran++) {
1165 *(scale + ibnd +
MAXBANDS * igran) = rad_fact[igran * 2];
1169 for (igran = 0; igran < ngran; igran++) {
1170 *(scale + ibnd +
MAXBANDS * igran) = 1.;
1180 if (qual2_found == 0) {
1182 "-E- %s Line %d: All M bands contain no data (NumberOfScans undefined for all bands)\n",
1183 __FILE__, __LINE__);
1190 "-E- %s Line %d: Unable to open the geolocation file: %s\n",
1191 __FILE__, __LINE__,
file->geofile);
1198 if (
h5io_set_grp(&geo_fid,
"Data_Products", &id_tmp) != 0) {
1200 "-E- %s, %d: Failure to set geolocation Data_Products group\n",
1201 __FILE__, __LINE__);
1202 fprintf(
stderr,
"name: %s\n",
file->geofile);
1207 "-E- %s Line %d: failed to get group Data_Products contents\n",
1208 __FILE__, __LINE__);
1211 if (strcmp(grp_obj_nm[0],
"VIIRS-MOD-GEO-TC") == 0) {
1214 fprintf(
stderr,
"-I- %s , %d: Geolocation is Terrain Corrected\n",
1215 __FILE__, __LINE__);
1216 }
else if (strcmp(grp_obj_nm[0],
"VIIRS-MOD-GEO") == 0) {
1219 fprintf(
stderr,
"-I- %s , %d: Geolocation is NOT Terrain Corrected\n",
1220 __FILE__, __LINE__);
1222 fprintf(
stderr,
"-E- %s, %d: Geo Data_Products has unknown sub-group\n",
1223 __FILE__, __LINE__);
1224 fprintf(
stderr,
"= %s\n", grp_obj_nm[0]);
1228 fprintf(
stderr,
"-E- %s, %d: failed to close group Data_Products\n",
1229 __FILE__, __LINE__);
1232 file->terrain_corrected = terr_corr;
1237 sprintf(g_path,
"Data_Products/VIIRS-MOD-GEO-TC/VIIRS-MOD-GEO-TC_Aggr");
1239 sprintf(g_path,
"Data_Products/VIIRS-MOD-GEO/VIIRS-MOD-GEO_Aggr");
1243 "-E- %s, %d: Failure to set geolocation aggregation dataset\n",
1244 __FILE__, __LINE__);
1245 fprintf(
stderr,
"name: %s\n",
file->geofile);
1248 if (
h5ReadAttrInt32(&g_id,
"AggregateNumberGranules", (
void *) &dgran) != 0) {
1250 "-E- %s, %d: Unable to read the AggregateNumberGranules attribute\n",
1251 __FILE__, __LINE__);
1252 fprintf(
stderr,
"name: %s\n",
file->geofile);
1256 fprintf(
stderr,
"-E- %s, %d: Unable to close geofile granule\n",
1257 __FILE__, __LINE__);
1261 if (dgran != ngran) {
1263 "-E- %s, %d: Mismatch found in # granules in geofile vs band files\n",
1264 __FILE__, __LINE__);
1265 fprintf(
stderr,
" geofile: %d, band files: %d\n", dgran, ngran);
1273 if (
h5io_set_grp(&geo_fid,
"All_Data", &id_tmp) != 0) {
1274 fprintf(
stderr,
"-E- %s Line %d: Unable to open group All_Data\n",
1275 __FILE__, __LINE__);
1280 "-E- %s Line %d: failed to get group All_Data contents\n",
1281 __FILE__, __LINE__);
1284 strcpy(geo_all_data_gnam, grp_obj_nm[0]);
1286 fprintf(
stderr,
"-E- %s Line %d: failed to close group All_Data\n",
1287 __FILE__, __LINE__);
1296 u58_scn_st = (int64_t *) malloc(nscan_sto *
sizeof (int64_t));
1297 sprintf(g_path,
"All_Data/%s/StartTime", geo_all_data_gnam);
1298 if (
h5io_grab_ds(&geo_fid, g_path, (
void *) u58_scn_st) != 0) {
1299 fprintf(
stderr,
"-E- %s, %d: Unable to read the Geo StartTime\n",
1300 __FILE__, __LINE__);
1303 for (
i = 0;
i < nscan_inst_tot;
i++) {
1305 if ((u58_scn_st[
iscan] < u58_gran_st)
1306 || (u58_scn_st[
iscan] > u58_gran_en)) {
1308 "-W- %s Line %d: scan start time on scan %d was outside granule range - repaired.\n",
1309 __FILE__, __LINE__,
i);
1310 u58_scn_st[
iscan] = u58_gran_st
1311 +
i * (u58_gran_en - u58_gran_st) / nscan_inst_tot;
1315 pos = (
float *) malloc(nscan_sto * 3 *
sizeof (
float));
1317 sprintf(g_path,
"All_Data/%s/SCPosition", geo_all_data_gnam);
1319 fprintf(
stderr,
"-E- %s Line %d: Unable to read the SCPosition\n",
1320 __FILE__, __LINE__);
1324 vel = (
float *) malloc(nscan_sto * 3 *
sizeof (
float));
1326 sprintf(g_path,
"All_Data/%s/SCVelocity", geo_all_data_gnam);
1327 if (
h5io_grab_ds(&geo_fid, g_path, (
void *) vel) != 0) {
1328 fprintf(
stderr,
"-E- %s Line %d: Unable to read the SCVelocity\n",
1329 __FILE__, __LINE__);
1333 att = (
float *) malloc(nscan_sto * 3 *
sizeof (
float));
1334 sprintf(g_path,
"All_Data/%s/SCAttitude", geo_all_data_gnam);
1336 if (
h5io_grab_ds(&geo_fid, g_path, (
void *) att) != 0) {
1337 fprintf(
stderr,
"-E- %s Line %d: Unable to read the SCAttitude\n",
1338 __FILE__, __LINE__);
1344 for (ids = 0; ids < 7; ids++) {
1345 sprintf(g_path,
"All_Data/%s/%s", geo_all_data_gnam, geo_name[ids]);
1346 if (
h5io_set_ds(&geo_fid, g_path, (geo_dat_id + ids)) != 0) {
1348 "-E- %s Line %d: Unable to set ds # %d in geolocation file\n",
1349 __FILE__, __LINE__, ids);
1356 if ((scl_rad = (
unsigned short *) malloc(
npix *
sizeof (
unsigned short)))
1358 fprintf(
stderr,
"-E- %s Line %d: scl_rad allocate failed\n", __FILE__,
1363 if ((flt_rad = (
float *) malloc(
npix *
sizeof (
float))) ==
NULL) {
1364 fprintf(
stderr,
"-E- %s Line %d: flt_rad allocate failed\n", __FILE__,
1405 static int32_t firstcall = 1, lastframe = -1;
1408 int32_t
i, ibnd, ipix, moon_affected;
1409 float *iptr, pos1[3], vel1[3], att1[3], sen_mat[3][3], coeff[10], rval;
1411 double dsec,
esdist, f_corr;
1412 static double mnorm[3];
1415 int32_t detnum, igran, dscan, ascan, aline;
1424 dscan = dline /
file->ndets;
1425 detnum = (dline % (
NDET + 2 * margin[0])) - margin[0];
1426 ascan = scan_map[dscan];
1427 aline = ascan * (
NDET + 2 * margin[0]) + detnum;
1428 igran = gran_map[dscan];
1434 if (detnum >
NDET - 1)
1436 double tai58 = u58_scn_st[ascan] / 1000000.0;
1443 for (ibnd = 0; ibnd < 7; ibnd++) {
1447 if (
lonlat && (ibnd != 0) && (ibnd != 1) && (ibnd != 5))
1470 iptr =
l1rec->height;
1481 "-E- %s, %d: Failed read to geo line %d of band %d\n",
1482 __FILE__, __LINE__, aline, ibnd);
1491 if (ascan != lastframe) {
1495 mside = ((*(viirs_qual2 + ascan) & 1) == 1) ? 1 : 0;
1500 for (
i = 0;
i < 3;
i++) {
1501 pos1[
i] = *(
pos +
i + ascan * 3) / 1000.;
1502 vel1[
i] = *(vel +
i + ascan * 3) / 1000.;
1503 att1[
i] = *(att +
i + ascan * 3) / 3600.;
1505 ocorient_(pos1, vel1, att1, sen_mat, coeff);
1509 for (
i = 0;
i < 3;
i++)
1517 int32_t msec32 = (int32_t) (dsec * 1.e3);
1518 int32_t yr = (int32_t) year;
1519 int32_t dy = (int32_t)
day;
1522 fsol = pow(1.0 /
esdist, 2);
1528 for (ipix = 0; ipix <
l1rec->npix; ipix++)
1529 *(
l1rec->pixnum + ipix) = ipix;
1547 for (ibnd = 0; ibnd <
MAXBANDS; ibnd++) {
1550 if (btype[ibnd] ==
VSWIR)
1551 l1rec->Fo[ivswir] = Fobar[ivswir] * fsol;
1553 moon_affected = ((*(viirs_qual2 + ascan) & 2) != 0) ? 1 : 0;
1555 f_corr = *(f_cal_corr +
mside +
N_MS * (detnum +
NDET * ibnd));
1557 if (sdr_band_typ[ibnd] == 0) {
1560 (
void *) scl_rad) != 0) {
1562 "-E- %s, %d: Failed to read line %d of band %d\n",
1563 __FILE__, __LINE__, aline, ibnd);
1566 for (ipix = 0; ipix <
l1rec->npix; ipix++) {
1570 if ((btype[ibnd] ==
VSWIR)
1578 if ((*(
l1rec->lat + ipix) < -90.)
1579 || (*(
l1rec->lat + ipix) > 90.)
1580 || (*(
l1rec->lon + ipix) < -180.)
1581 || (*(
l1rec->lon + ipix) > 180.)) {
1582 if (btype[ibnd] ==
VSWIR) {
1584 l1rec->navfail[ipix] = 1;
1585 }
else if (btype[ibnd] ==
THERM)
1587 else if (btype[ibnd] ==
CIRRUS)
1594 if (moon_affected == 1)
1595 l1rec->navwarn[ipix] = 1;
1600 switch (*(scl_rad + ipix)) {
1602 if (btype[ibnd] ==
VSWIR) {
1604 l1rec->hilt[ipix] = 1;
1605 }
else if (btype[ibnd] ==
THERM) {
1607 }
else if (btype[ibnd] ==
CIRRUS) {
1617 if (btype[ibnd] ==
VSWIR) {
1619 }
else if (btype[ibnd] ==
THERM) {
1621 }
else if (btype[ibnd] ==
CIRRUS) {
1626 if (btype[ibnd] ==
VSWIR) {
1628 l1rec->navfail[ipix] = 1;
1629 }
else if (btype[ibnd] ==
THERM)
1631 else if (btype[ibnd] ==
CIRRUS)
1635 if (btype[ibnd] ==
VSWIR)
1636 *(
l1rec->Lt +
nbands * ipix + ivswir) = (*(scl_rad
1637 + ipix) * *(scale + ibnd +
MAXBANDS * igran)
1639 *
l1rec->Fo[ivswir] * f_corr
1641 else if (btype[ibnd] ==
THERM)
1643 * (*(scl_rad + ipix)
1644 * *(scale + ibnd +
MAXBANDS * igran)
1647 else if (btype[ibnd] ==
CIRRUS)
1648 l1rec->rho_cirrus[ipix] = (*(scl_rad + ipix)
1649 * *(scale + ibnd +
MAXBANDS * igran)
1656 (
void *) flt_rad) != 0) {
1658 "-E- %s, %d: Failed to read scan %d of band %d\n",
1659 __FILE__, __LINE__, aline, ibnd);
1662 for (ipix = 0; ipix <
l1rec->npix; ipix++) {
1663 if ((btype[ibnd] ==
VSWIR)
1671 if ((*(
l1rec->lat + ipix) < -90.)
1672 || (*(
l1rec->lat + ipix) > 90.)
1673 || (*(
l1rec->lon + ipix) < -180.)
1674 || (*(
l1rec->lon + ipix) > 180.)) {
1675 if (btype[ibnd] ==
VSWIR) {
1677 l1rec->navfail[ipix] = 1;
1678 }
else if (btype[ibnd] ==
THERM)
1680 else if (btype[ibnd] ==
CIRRUS)
1687 if (moon_affected == 1)
1688 l1rec->navwarn[ipix] = 1;
1690 rval = *(flt_rad + ipix);
1692 if (btype[ibnd] ==
VSWIR) {
1694 l1rec->hilt[ipix] = 1;
1695 }
else if (btype[ibnd] ==
THERM) {
1697 }
else if (btype[ibnd] ==
CIRRUS) {
1706 if (btype[ibnd] ==
VSWIR) {
1708 }
else if (btype[ibnd] ==
THERM) {
1710 }
else if (btype[ibnd] ==
CIRRUS) {
1714 if (btype[ibnd] ==
VSWIR) {
1716 l1rec->navfail[ipix] = 1;
1717 }
else if (btype[ibnd] ==
THERM)
1719 else if (btype[ibnd] ==
CIRRUS)
1721 }
else if (btype[ibnd] ==
VSWIR)
1722 *(
l1rec->Lt +
nbands * ipix + ivswir) = rval * f_corr
1725 else if (btype[ibnd] ==
THERM)
1728 else if (btype[ibnd] ==
CIRRUS)
1729 l1rec->rho_cirrus[ipix] = rval * f_corr;
1732 switch (btype[ibnd]) {
1741 l1rec->detnum = (int32_t) detnum;
1742 l1rec->margin_s = margin[1];
1743 l1rec->scn_fmt = scn_fmt;
1748 for (ipix = 0; ipix <
l1rec->npix; ipix++)
1776 for (ict = 0; ict <
MAXBANDS; ict++) {
1777 if (
h5io_close(&(bnd_dat_id[0][ict])) != 0) {
1779 "-E- %s Line %d: Failed to close band data id #%d\n",
1780 __FILE__, __LINE__, ict);
1785 fprintf(
stderr,
"-E- %s Line %d: Failed to close band file %s\n",
1786 __FILE__, __LINE__, vfnames[ict]);
1793 for (ict = 0; ict < 7; ict++) {
1796 "-E- %s Line %d: Failed to close geo data id #%d\n",
1797 __FILE__, __LINE__, ict);
1803 fprintf(
stderr,
"-E- %s Line %d: Failed to close geo data file %s\n",
1804 __FILE__, __LINE__,
file->geofile);