17 #include <sys/types.h>
24 #define STDMAXSCANS 48
30 #define near( x, y ) ( fabsf( x - y ) < 0.001 )
34 static char vfnames[
MAXBANDS][FILENAME_MAX];
35 static h5io_str geo_fid, sdr_fid[
MAXBANDS];
36 static h5io_str geo_dat_id[7], bnd_dat_id[2][
MAXBANDS];
38 static unsigned short *scl_rad;
39 static float *scale, *
offset;
40 static float *flt_rad;
41 static int64_t *u58_scn_st, u58_gran_st, u58_gran_en;
43 static unsigned char *viirs_qual2;
54 static float *
pos, *vel, *att;
55 static int16_t scn_fmt;
56 static int16_t margin[2];
61 static int32_t btype[] = {0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 1, 1, 1, 1};
62 static int32_t ivswir = 0;
63 static int32_t itherm = 0;
67 char tmp[FILENAME_MAX];
68 char dir[FILENAME_MAX];
69 char base[FILENAME_MAX];
72 if( access(
name, R_OK ) == 0 ) {
85 if((
dp = opendir(dir)) ==
NULL) {
86 fprintf(
stderr,
"%s:%d - cannot open directory: %s\n", __FILE__, __LINE__, dir);
91 while((entry = readdir(
dp)) !=
NULL) {
92 if(strncmp(entry->d_name,
base, 44) == 0){
94 sprintf(
name,
"%s/%s", dir, entry->d_name);
101 fprintf(
stderr,
"%s:%d - valid VIIRS file not found: %s\n", __FILE__, __LINE__,
name);
129 char *
path, temp[FILENAME_MAX],
base[FILENAME_MAX];
138 if ((fp = fopen(in_file,
"r")) ==
NULL) {
139 fprintf(
stderr,
"-E- %s, %d: input file %s open problem.\n",
140 __FILE__, __LINE__, in_file);
147 if (fscanf(fp,
"%s", vfnames[
i]) != 1) {
149 "-E- %s, %d: Failed to read VIIRS input file # %d from list file: %s/n",
150 __FILE__, __LINE__,
i, in_file);
164 path = dirname(in_file);
166 if (strncmp(
base,
"SVM", 3) != 0) {
168 "-E- %s, %d: Improper VIIRS file format for file base:\n",
175 "-E- %s, %d: Improper VIIRS file format for file base:\n",
181 sprintf(vfnames[
i],
"%s/SVM%2.2d%s",
path,
j,
base + 5);
184 printf(
"VIIRS filename for M%2.2d: %s\n",
j, vfnames[
i]);
218 char *in_calfile, sdr_cal_fil[FILENAME_MAX], *csave =
NULL;
219 char *aux_arr, *fildir;
220 int ncorr, icorr, f_ndim, f_dim_siz[20], f_sto_len,
i;
230 for (icorr = 0; icorr < ncorr; icorr++)
231 *(f_cal_corr + icorr) = 1.;
238 printf(
"\nVIIRS calibration starting\n");
239 if (*in_calfile == 0) {
241 printf(
"No cal file supplied, calibration remains unchanged\n");
245 printf(
"Input calibration from file: %s\n", in_calfile);
254 if (strstr(in_calfile,
"Unity") ==
NULL) {
256 printf(
"%s, %d - E - failed to retrieve input F table values\n",
261 printf(
"Input calibration is from the unity F table, Using unity\n");
269 if (
h5io_info(g_id,
"N_Aux_Filename", &f_class, &f_native_typ, &f_ndim,
270 f_dim_siz, &f_sto_len) != 0) {
271 printf(
"%s, %d - E - could not get info on N_Aux_Filename\n", __FILE__,
278 if ((aux_arr = (
char *) malloc(f_sto_len * f_dim_siz[0] *
sizeof (
char)))
280 printf(
"%s, %d - E - unable to allocate aux filename list array\n",
287 if (
h5io_rd_attr(g_id,
"N_Aux_Filename", aux_arr) != 0) {
288 printf(
"%s, %d - failed to read the N_Aux_Filename attribute\n",
295 for (
i = 0;
i < f_dim_siz[0];
i++) {
297 if (strstr((aux_arr +
i * f_sto_len),
"VIIRS-SDR-F-LUT_npp") !=
NULL)
298 csave = (aux_arr +
i * f_sto_len);
305 printf(
"%s ,%d: I: No F-LUT found, Looking for F-PREDICTED-LUT\n",
307 for (
i = 0;
i < f_dim_siz[0];
i++) {
309 if (strstr((aux_arr +
i * f_sto_len),
310 "VIIRS-SDR-F-PREDICTED-LUT_npp") !=
NULL)
311 csave = (aux_arr +
i * f_sto_len);
315 "%s, %d E - failed to find the F LUT name in N_Aux_Filename\n",
320 "%s, %d - A F-PREDICTED-LUT was found, but code is not set-up\n",
323 " to handle this kind of LUT. Set 'calfile=' in par file\n");
324 printf(
" to leave cal unchanged\n");
329 printf(
"\n\nThe F LUT found in the SDR is: %s\n", csave);
333 if (strstr(csave,
"Unity") !=
NULL) {
335 printf(
"Found a unity F table designation in the SDR\n");
341 if ((fildir = getenv(
"OCVARROOT")) ==
NULL) {
342 printf(
"-E- %s, %d: OCVARROOT env variable undefined.\n", __FILE__,
346 strcpy(sdr_cal_fil, fildir);
347 strcat(sdr_cal_fil,
"/viirsn/cal/EVAL/");
348 strcat(sdr_cal_fil, csave);
349 strcat(sdr_cal_fil,
".h5");
350 printf(
"F Lut file name derived from the SDR is: %s\n", sdr_cal_fil);
356 printf(
"-E- %s, %d: Failed to retrieve SDR F table values\n",
365 printf(
"Completed calibration using F LUT file(s)\n\n");
396 double f_lut_sel[2 * 16 * 16 * 2];
397 int64_t *tarr, del, dist, u58_t1, u58_t2;
401 h5io_str h5fid, dsid;
402 int f_ndim, f_dim_siz[20], f_sto_len, ntime, itim1, ncorr,
i;
408 printf(
"Reading in F LUT: %s\n",
file);
410 printf(
"- E - %s, %d: Unable to open the F LUT file: %s\n", __FILE__,
417 if (
h5io_set_ds(&h5fid,
"Beginning_Time_IET", &dsid) != 0) {
418 printf(
"- E - %s, %d: Unable to set dataset Beginning_Time_IET\n",
422 if (
h5io_info(&dsid,
NULL, &f_class, &f_native_typ, &f_ndim, f_dim_siz,
424 printf(
"- E - %s, %d: Unable to get info on Beginning_Time_IET\n",
428 ntime = f_dim_siz[0];
432 if ((tarr = (int64_t *) malloc(ntime *
sizeof (int64_t))) ==
NULL) {
433 printf(
"- E - %s, %d: Unable to allocate time storage\n", __FILE__,
438 printf(
"- E - %s, %d: Unable to read Beginning_Time_IET\n", __FILE__,
447 printf(
"Granule time is: %ld\n", (
long) u58_time);
448 if (u58_time < tarr[0]) {
450 "\n\n\n- W - %s, %d: WARNING, granule time below times in the F table\n",
453 printf(
"granule time of %ld is below table start time of %ld\n",
454 (
long) u58_time, (
long) tarr[0]);
455 }
else if (u58_time > *(tarr + ntime - 1)) {
457 "\n\n\n- W - %s, %d: WARNING, granule time higher than times in the F table\n",
460 printf(
"granule time of %ld is above table end time of %ld\n", (
long) u58_time,
461 (
long) tarr[ntime - 1]);
463 for (itim1 = 0; itim1 < ntime; itim1++)
464 if (*(tarr + itim1 + 1) > u58_time)
470 u58_t1 = *(tarr + itim1);
471 u58_t2 = *(tarr + itim1 + 1);
473 printf(
"itim1: %d, t1: %ld, t2: %ld\n", itim1, (
long) u58_t1, (
long) u58_t2);
478 if (
h5io_set_ds(&h5fid,
"VIIRS-SDR-F-LUT", &dsid) != 0) {
479 printf(
"- E - %s, %d: Unable to set dataset VIIRS-SDR-F-LUT\n",
483 if (
h5io_info(&dsid,
NULL, &f_class, &f_native_typ, &f_ndim, f_dim_siz,
485 printf(
"- E - %s, %d: Unable to get info on dataset VIIRS-SDR-F-LUT\n",
490 printf(
"- E - %s, %d: # dimensions of LUT dataset is not 5\n", __FILE__,
494 if ((f_dim_siz[0] != ntime) || (f_dim_siz[1] != 22) || (f_dim_siz[2] != 32)
495 || (f_dim_siz[3] != 3) || (f_dim_siz[4] != 2)) {
496 printf(
"- E - %s, %d: LUT dataset dimension sizes are unexpected\n",
516 printf(
"- E - %s, %d: Unable to read the VIIRS-SDR-F-LUT dataset\n",
524 del = u58_t2 - u58_t1;
525 dist = u58_time - u58_t1;
528 printf(
"Interp factor from t2: %f\n",
fact);
531 for (
i = 0;
i < ncorr;
i++)
532 *(f_cal_corr +
i) *= *(f_lut_sel +
i) * (1. -
fact)
533 + *(f_lut_sel +
i + ncorr) *
fact;
535 for (
i = 0;
i < ncorr;
i++)
536 *(f_cal_corr +
i) /= *(f_lut_sel +
i) * (1. -
fact)
537 + *(f_lut_sel +
i + ncorr) *
fact;
556 hid_t *base_grp, spaceid, attr_id;
559 &(
id->dat_id) : &(
id->grp_id);
560 if ((attr_id = H5Aopen_name(*base_grp, attr_name)) < 0)
563 if ((spaceid = H5Aget_space(attr_id)) < 0)
566 if(H5Sget_simple_extent_npoints(spaceid) != 1) {
567 printf(
"%s: # of elements of attribute: %s has to be 1\n", __FILE__, attr_name);
571 if (H5Aread(attr_id, H5T_NATIVE_INT,
val) < 0)
577 if (H5Sclose(spaceid) < 0)
579 if (H5Aclose(attr_id) < 0)
608 h5io_str g_id, id_tmp;
609 char g_path[100], geo_all_data_gnam[200], *band_dark, **grp_obj_nm;
610 char *geo_name[] = {
"Latitude",
"Longitude",
"SatelliteAzimuthAngle",
611 "SatelliteZenithAngle",
"SolarAzimuthAngle",
"SolarZenithAngle",
614 H5T_class_t h5_class;
616 int ndim, dim_siz[4], sto_len, n_obj, *grp_obj_typ;
618 unsigned char *cmp_vq2;
619 int16_t ua_scn_fmt, ua_ndets, ua_margin[2];
620 int32_t ua_npix, ua_nlin, attr_exist;
621 int32_t qual2_found = 0, ngran, dgran,
iscan, dk;
631 if(
file->geofile[0] == 0) {
633 file->geofile[0] =
'G';
634 file->geofile[1] =
'M';
635 file->geofile[2] =
'T';
636 file->geofile[3] =
'C';
637 file->geofile[4] =
'O';
646 for (ibnd = 0, jbnd = 1; ibnd <
MAXBANDS; ibnd++, jbnd++) {
648 if (
h5io_openr(vfnames[ibnd], 0, (sdr_fid + ibnd)) != 0) {
649 fprintf(
stderr,
"-E- %s Line %d: Failure to open %s\n", __FILE__,
650 __LINE__, vfnames[ibnd]);
656 sprintf(g_path,
"Data_Products/VIIRS-M%d-SDR/VIIRS-M%d-SDR_Aggr", jbnd,
658 if (
h5io_set_ds((sdr_fid + ibnd), g_path, &g_id) != 0) {
660 "-E- %s, %d: Failure to set aggregation dataset, band %d:\n",
661 __FILE__, __LINE__, ibnd);
662 fprintf(
stderr,
"name: %s\n", vfnames[ibnd]);
668 "-E- %s, %d: Unable to read the AggregateNumberGranules attribute\n",
670 fprintf(
stderr,
"band %d\n, name: %s\n", ibnd, vfnames[ibnd]);
674 fprintf(
stderr,
"-E- %s Line %d: Unable to close granule, ds %d\n",
675 __FILE__, __LINE__, ibnd);
686 if ((scale = (
float *) malloc(
MAXBANDS * ngran *
sizeof (
float)))
689 "-E- %s, %d: failure to allocate scale storage\n",
693 if ((
offset = (
float *) malloc(
MAXBANDS * ngran *
sizeof (
float)))
696 "-E- %s, %d: failure to allocate offset storage\n",
700 if ((nscan_inst = (
int *) malloc(ngran *
sizeof (
int))) ==
NULL) {
702 "-E- %s, %d: failure to allocate nscan_inst storage\n",
706 if ((nscan_stat = (
int *) malloc(ngran *
sizeof (
int))) ==
NULL) {
708 "-E- %s, %d: failure to allocate nscan_stat storage\n",
712 if ((band_dark = (
char *) calloc(ngran *
MAXBANDS,
sizeof (
char)))
715 "-E- %s, %d: failure to allocate band_dark storage\n",
719 if ((rad_fact = (
float *) malloc(ngran * 2 *
sizeof (
float))) ==
NULL) {
721 "-E- %s, %d: failure to allocate rad_fact storage\n",
726 if (dgran != ngran) {
728 "-E- %s, %d: Mismatch in AggregateNumberOfGranules\n",
730 fprintf(
stderr,
"band %d\n, name: %s\n", ibnd, vfnames[ibnd]);
739 for (igran = 0; igran < ngran; igran++) {
740 sprintf(g_path,
"Data_Products/VIIRS-M%d-SDR/VIIRS-M%d-SDR_Gran_%d",
742 if (
h5io_set_ds((sdr_fid + ibnd), g_path, &g_id) != 0) {
744 "-E- %s Line %d: Failure to set granule dataset, ds %d:\n",
745 __FILE__, __LINE__, ibnd);
746 fprintf(
stderr,
"name: %s", vfnames[ibnd]);
751 (
void *) (nscan_inst + igran)) != 0) {
753 "-E- %s Line %d: Unable to read the N_Number_Of_Scans ds attr, ds %d:\n",
754 __FILE__, __LINE__, ibnd);
755 fprintf(
stderr,
"name: %s", vfnames[ibnd]);
758 if (nscan_inst[igran] <= 0) {
760 "-E- %s, %d: N_Number_Of_Scans[%d]: %d invalid\n",
761 __FILE__, __LINE__, igran, nscan_inst[igran]);
765 nscan_inst_tot += nscan_inst[igran];
768 if ((igran == 0) && (ibnd == 0)) {
776 if ((gran_map = (
int *) malloc(
777 ngran * nscan_per_gran *
sizeof (
int))) ==
NULL) {
779 "-E- %s, %d: failure to allocate gran_map storage\n",
783 if ((scan_map = (
int *) malloc(
784 ngran * nscan_per_gran *
sizeof (
int))) ==
NULL) {
786 "-E- %s, %d: failure to allocate scan_map storage\n",
790 for (
i = 0;
i < ngran * nscan_per_gran;
i++) {
795 for (
i = 0;
i < nscan_inst[igran];
i++) {
796 scan_map[
iscan] = igran * nscan_per_gran +
i;
797 gran_map[
iscan++] = igran;
802 if (
h5io_rd_attr(&g_id,
"N_Beginning_Time_IET", (
void *) &u58_gran)
805 "-E- %s Line %d: Unable to read the N_Beginning_Time_IET ds attr, ds %d:\n",
806 __FILE__, __LINE__, ibnd);
807 fprintf(
stderr,
"name: %s", vfnames[ibnd]);
811 u58_gran_st = u58_gran;
812 else if ((u58_gran > 0) && (u58_gran < u58_gran_st))
813 u58_gran_st = u58_gran;
815 if (
h5io_rd_attr(&g_id,
"N_Ending_Time_IET", (
void *) &u58_gran)
818 "-E- %s Line %d: Unable to read the N_Ending_Time_IET ds attr, ds %d:\n",
819 __FILE__, __LINE__, ibnd);
820 fprintf(
stderr,
"name: %s", vfnames[ibnd]);
824 u58_gran_en = u58_gran;
825 else if (u58_gran > u58_gran_en)
826 u58_gran_en = u58_gran;
834 if ((ibnd == 0) && (igran == ngran - 1))
840 "-E- %s Line %d: Unable to close granule, ds %d\n",
841 __FILE__, __LINE__, ibnd);
847 sprintf(g_path,
"All_Data/VIIRS-M%d-SDR_All/NumberOfScans", jbnd);
848 if (
h5io_grab_ds((sdr_fid + ibnd), g_path, (
void *) nscan_stat) != 0) {
850 "-E- %s, %d: Unable to read the # scans from NumberOfScans, band %d\n",
851 __FILE__, __LINE__, ibnd);
854 for (igran = 0; igran < ngran; igran++) {
856 if (nscan_stat[igran] == -993) {
857 *(band_dark + ibnd +
MAXBANDS * igran) = 1;
859 if (nscan_stat[igran] != nscan_inst[igran]) {
861 "-E- %s, %d: dataset #scans: %d not = to granule # scans: %d \n",
862 __FILE__, __LINE__, nscan_stat[igran],
864 fprintf(
stderr,
"band: %d, granule: %d\n", ibnd, igran);
879 sprintf(g_path,
"All_Data/VIIRS-M%d-SDR_All/Radiance", jbnd);
880 if (
h5io_set_ds((sdr_fid + ibnd), g_path, &g_id) != 0) {
882 "-E- %s Line %d: Getting nscan_sto, Unable to open path\n",
887 dim_siz, &sto_len) != 0) {
889 "-E- %s Line %d: Getting nscan_sto, Unable to read dataset\n",
893 nscan_sto = dim_siz[0] /
NDET;
896 "-E- %s Line %d: Getting nscan_sto, Unable to close path\n",
901 printf(
"%s: total # inst scans: %d, dataset scans: %d\n",
902 __FILE__, nscan_inst_tot, nscan_sto);
903 if (nscan_sto < nscan_inst_tot) {
904 fprintf(
stderr,
"-E- %s,%d: # aggregate scans < inst scans\n",
909 if ((viirs_qual2 = malloc(nscan_sto *
sizeof (
unsigned char)))
912 "-E- %s Line %d: mem allocation error for viirs_qual2\n",
916 if ((cmp_vq2 = malloc(nscan_sto *
sizeof (
unsigned char))) ==
NULL) {
918 "-E- %s Line %d: mem allocation error for cmp_vq2\n",
926 sprintf(g_path,
"All_Data/VIIRS-M%d-SDR_All/QF2_SCAN_SDR", jbnd);
927 if (
h5io_grab_ds((sdr_fid + ibnd), g_path, (
void *) cmp_vq2) != 0) {
929 "-E- %s, %d: Unable to get QF2 for path %s, band %d\n",
930 __FILE__, __LINE__, g_path, ibnd);
937 nscan = nscan_inst_tot;
939 if (nscan_inst_tot !=
nscan) {
941 "-E- %s, %d: # inst scans: %d in band file: %s",
942 __FILE__, __LINE__, nscan_inst_tot, vfnames[ibnd]);
944 "is not the same as initially established (scans: %d)\n",
954 for (
i = 0;
i < ngran;
i++) {
955 if (*(band_dark + ibnd +
MAXBANDS *
i) == 1) {
961 if (qual2_found == 0) {
962 memcpy(viirs_qual2, cmp_vq2, nscan_sto);
964 for (
i = 0;
i < nscan_inst_tot;
i++) {
965 if ((*(viirs_qual2 + scan_map[
i]) & 2) != 0) {
966 file->sv_with_moon = 1;
967 printf(
"VIIRS_SV_LUNAR_INCURSION detected\n");
973 for (
i = 0;
i < nscan_inst_tot;
i++) {
974 if (*(viirs_qual2 + scan_map[
i])
975 != *(cmp_vq2 + scan_map[
i])) {
977 "-E- %s Line %d: QF2_SCAN_SDR mismatch, val# %d, bnd %d\n",
978 __FILE__, __LINE__,
i, ibnd);
988 if ((attr_exist =
h5io_attr_exist((sdr_fid + ibnd),
"Data Scan Format"))
991 "-E- %s Line %d: failure while checking for attr Data Scan Format, band: %d\n",
992 __FILE__, __LINE__, ibnd);
995 if (attr_exist == 1) {
1006 (
void *) &ua_scn_fmt) != 0) {
1008 "-E- %s Line %d: Unable to read the Data Scan Format attr, ds %d: name: %s\n",
1009 __FILE__, __LINE__, ibnd, vfnames[ibnd]);
1012 if (
h5io_rd_attr((sdr_fid + ibnd),
"Scan Margin (track, scan)",
1013 (
void *) ua_margin) != 0) {
1015 "-E- %s Line %d: Unable to read the Scan Margin attr, ds %d: name: %s\n",
1016 __FILE__, __LINE__, ibnd, vfnames[ibnd]);
1019 if (
h5io_rd_attr((sdr_fid + ibnd),
"Pixels per Scan Line",
1020 (
void *) &ua_npix) != 0) {
1022 "-E- %s Line %d: Unable to read the Pixels per Scan Line attr, ds %d: name: %s\n",
1023 __FILE__, __LINE__, ibnd, vfnames[ibnd]);
1026 if (
h5io_rd_attr((sdr_fid + ibnd),
"Number of Scan Lines",
1027 (
void *) &ua_nlin) != 0) {
1029 "-E- %s Line %d: Unable to read the Number of Scan Lines attr, ds %d: name: %s\n",
1030 __FILE__, __LINE__, ibnd, vfnames[ibnd]);
1034 "Number of Detectors per Scan", (
void *) &ua_ndets)
1037 "-E- %s Line %d: Unable to read the Number of Detectors per Scan attr, ds %d: name: %s\n",
1038 __FILE__, __LINE__, ibnd, vfnames[ibnd]);
1047 margin[0] = ua_margin[0];
1048 margin[1] = ua_margin[1];
1049 file->npix = ua_npix;
1051 file->ndets = ua_ndets;
1052 file->nscan = ua_nlin;
1053 scn_fmt = ua_scn_fmt;
1056 if ((margin[0] != ua_margin[0]) || (margin[1] != ua_margin[1])) {
1058 "-E- %s Line %d: margin mismatch on band: %d name: %s\n",
1059 __FILE__, __LINE__, ibnd, vfnames[ibnd]);
1062 if (
file->npix != ua_npix) {
1064 "-E- %s Line %d: ua npix mismatch on band: %d name: %s\n",
1065 __FILE__, __LINE__, ibnd, vfnames[ibnd]);
1068 if (
file->ndets != ua_ndets) {
1070 "-E- %s Line %d: ua ndets mismatch on band: %d name: %s\n",
1071 __FILE__, __LINE__, ibnd, vfnames[ibnd]);
1076 "-E- %s Line %d: ua # lines mismatch on band: %d name: %s\n",
1077 __FILE__, __LINE__, ibnd, vfnames[ibnd]);
1080 if (scn_fmt != ua_scn_fmt) {
1082 "-E- %s Line %d: ua scan format mismatch on band: %d name: %s\n",
1083 __FILE__, __LINE__, ibnd, vfnames[ibnd]);
1092 for (ids = 0; ids < 1; ids++) {
1093 if (btype[ibnd] ==
VSWIR || btype[ibnd] ==
CIRRUS)
1094 sprintf(g_path,
"All_Data/VIIRS-M%d-SDR_All/%s", jbnd,
1097 sprintf(g_path,
"All_Data/VIIRS-M%d-SDR_All/%s", jbnd,
1100 if (
h5io_set_ds((sdr_fid + ibnd), g_path, &(bnd_dat_id[ids][ibnd]))
1103 "-E- %s Line %d: Error setting dataset, band %d, ds %d\n",
1104 __FILE__, __LINE__, ibnd, ids);
1112 &h5_native_typ, &ndim, dim_siz, &sto_len) != 0) {
1114 "-E- %s Line %d: Error accessing radiance dataset info, band %d\n",
1115 __FILE__, __LINE__, ibnd);
1118 if (h5_class == H5T_INTEGER)
1119 *(sdr_band_typ + ibnd) = 0;
1121 *(sdr_band_typ + ibnd) = 1;
1127 if (*(sdr_band_typ + ibnd) == 0) {
1128 if (btype[ibnd] ==
VSWIR || btype[ibnd] ==
CIRRUS)
1129 sprintf(g_path,
"All_Data/VIIRS-M%d-SDR_All/ReflectanceFactors",
1132 sprintf(g_path,
"All_Data/VIIRS-M%d-SDR_All/RadianceFactors",
1135 if (
h5io_grab_ds((sdr_fid + ibnd), g_path, (
void *) rad_fact)
1138 "-E- %s Line %d: Unable to read the rad fact, ds %d\n",
1139 __FILE__, __LINE__, ibnd);
1142 for (igran = 0; igran < ngran; igran++) {
1143 *(scale + ibnd +
MAXBANDS * igran) = rad_fact[igran * 2];
1147 for (igran = 0; igran < ngran; igran++) {
1148 *(scale + ibnd +
MAXBANDS * igran) = 1.;
1158 if (qual2_found == 0) {
1160 "-E- %s Line %d: All M bands contain no data (NumberOfScans undefined for all bands)\n",
1161 __FILE__, __LINE__);
1168 "-E- %s Line %d: Unable to open the geolocation file: %s\n",
1169 __FILE__, __LINE__,
file->geofile);
1176 if (
h5io_set_grp(&geo_fid,
"Data_Products", &id_tmp) != 0) {
1178 "-E- %s, %d: Failure to set geolocation Data_Products group\n",
1179 __FILE__, __LINE__);
1180 fprintf(
stderr,
"name: %s\n",
file->geofile);
1185 "-E- %s Line %d: failed to get group Data_Products contents\n",
1186 __FILE__, __LINE__);
1189 if (strcmp(grp_obj_nm[0],
"VIIRS-MOD-GEO-TC") == 0) {
1192 fprintf(
stderr,
"-I- %s , %d: Geolocation is Terrain Corrected\n",
1193 __FILE__, __LINE__);
1194 }
else if (strcmp(grp_obj_nm[0],
"VIIRS-MOD-GEO") == 0) {
1197 fprintf(
stderr,
"-I- %s , %d: Geolocation is NOT Terrain Corrected\n",
1198 __FILE__, __LINE__);
1200 fprintf(
stderr,
"-E- %s, %d: Geo Data_Products has unknown sub-group\n",
1201 __FILE__, __LINE__);
1202 fprintf(
stderr,
"= %s\n", grp_obj_nm[0]);
1206 fprintf(
stderr,
"-E- %s, %d: failed to close group Data_Products\n",
1207 __FILE__, __LINE__);
1210 file->terrain_corrected = terr_corr;
1215 sprintf(g_path,
"Data_Products/VIIRS-MOD-GEO-TC/VIIRS-MOD-GEO-TC_Aggr");
1217 sprintf(g_path,
"Data_Products/VIIRS-MOD-GEO/VIIRS-MOD-GEO_Aggr");
1221 "-E- %s, %d: Failure to set geolocation aggregation dataset\n",
1222 __FILE__, __LINE__);
1223 fprintf(
stderr,
"name: %s\n",
file->geofile);
1226 if (
h5ReadAttrInt32(&g_id,
"AggregateNumberGranules", (
void *) &dgran) != 0) {
1228 "-E- %s, %d: Unable to read the AggregateNumberGranules attribute\n",
1229 __FILE__, __LINE__);
1230 fprintf(
stderr,
"name: %s\n",
file->geofile);
1234 fprintf(
stderr,
"-E- %s, %d: Unable to close geofile granule\n",
1235 __FILE__, __LINE__);
1239 if (dgran != ngran) {
1241 "-E- %s, %d: Mismatch found in # granules in geofile vs band files\n",
1242 __FILE__, __LINE__);
1243 fprintf(
stderr,
" geofile: %d, band files: %d\n", dgran, ngran);
1251 if (
h5io_set_grp(&geo_fid,
"All_Data", &id_tmp) != 0) {
1252 fprintf(
stderr,
"-E- %s Line %d: Unable to open group All_Data\n",
1253 __FILE__, __LINE__);
1258 "-E- %s Line %d: failed to get group All_Data contents\n",
1259 __FILE__, __LINE__);
1262 strcpy(geo_all_data_gnam, grp_obj_nm[0]);
1264 fprintf(
stderr,
"-E- %s Line %d: failed to close group All_Data\n",
1265 __FILE__, __LINE__);
1274 u58_scn_st = (int64_t *) malloc(nscan_sto *
sizeof (int64_t));
1275 sprintf(g_path,
"All_Data/%s/StartTime", geo_all_data_gnam);
1276 if (
h5io_grab_ds(&geo_fid, g_path, (
void *) u58_scn_st) != 0) {
1277 fprintf(
stderr,
"-E- %s, %d: Unable to read the Geo StartTime\n",
1278 __FILE__, __LINE__);
1281 for (
i = 0;
i < nscan_inst_tot;
i++) {
1283 if ((u58_scn_st[
iscan] < u58_gran_st)
1284 || (u58_scn_st[
iscan] > u58_gran_en)) {
1286 "-W- %s Line %d: scan start time on scan %d was outside granule range - repaired.\n",
1287 __FILE__, __LINE__,
i);
1288 u58_scn_st[
iscan] = u58_gran_st
1289 +
i * (u58_gran_en - u58_gran_st) / nscan_inst_tot;
1293 pos = (
float *) malloc(nscan_sto * 3 *
sizeof (
float));
1295 sprintf(g_path,
"All_Data/%s/SCPosition", geo_all_data_gnam);
1297 fprintf(
stderr,
"-E- %s Line %d: Unable to read the SCPosition\n",
1298 __FILE__, __LINE__);
1302 vel = (
float *) malloc(nscan_sto * 3 *
sizeof (
float));
1304 sprintf(g_path,
"All_Data/%s/SCVelocity", geo_all_data_gnam);
1305 if (
h5io_grab_ds(&geo_fid, g_path, (
void *) vel) != 0) {
1306 fprintf(
stderr,
"-E- %s Line %d: Unable to read the SCVelocity\n",
1307 __FILE__, __LINE__);
1311 att = (
float *) malloc(nscan_sto * 3 *
sizeof (
float));
1312 sprintf(g_path,
"All_Data/%s/SCAttitude", geo_all_data_gnam);
1314 if (
h5io_grab_ds(&geo_fid, g_path, (
void *) att) != 0) {
1315 fprintf(
stderr,
"-E- %s Line %d: Unable to read the SCAttitude\n",
1316 __FILE__, __LINE__);
1322 for (ids = 0; ids < 7; ids++) {
1323 sprintf(g_path,
"All_Data/%s/%s", geo_all_data_gnam, geo_name[ids]);
1324 if (
h5io_set_ds(&geo_fid, g_path, (geo_dat_id + ids)) != 0) {
1326 "-E- %s Line %d: Unable to set ds # %d in geolocation file\n",
1327 __FILE__, __LINE__, ids);
1334 if ((scl_rad = (
unsigned short *) malloc(
npix *
sizeof (
unsigned short)))
1336 fprintf(
stderr,
"-E- %s Line %d: scl_rad allocate failed\n", __FILE__,
1341 if ((flt_rad = (
float *) malloc(
npix *
sizeof (
float))) ==
NULL) {
1342 fprintf(
stderr,
"-E- %s Line %d: flt_rad allocate failed\n", __FILE__,
1383 static int32_t firstcall = 1, lastframe = -1;
1386 int32_t
i, ibnd, ipix, moon_affected;
1387 float *iptr, pos1[3], vel1[3], att1[3], sen_mat[3][3], coeff[10], rval;
1389 double dsec,
esdist, f_corr;
1390 static double mnorm[3];
1393 int32_t detnum, igran, dscan, ascan, aline;
1402 dscan = dline /
file->ndets;
1403 detnum = (dline % (
NDET + 2 * margin[0])) - margin[0];
1404 ascan = scan_map[dscan];
1405 aline = ascan * (
NDET + 2 * margin[0]) + detnum;
1406 igran = gran_map[dscan];
1412 if (detnum >
NDET - 1)
1414 double tai58 = u58_scn_st[ascan] / 1000000.0;
1421 for (ibnd = 0; ibnd < 7; ibnd++) {
1446 iptr =
l1rec->height;
1456 "-E- %s, %d: Failed read to geo line %d of band %d\n",
1457 __FILE__, __LINE__, aline, ibnd);
1462 if (ascan != lastframe) {
1466 mside = ((*(viirs_qual2 + ascan) & 1) == 1) ? 1 : 0;
1471 for (
i = 0;
i < 3;
i++) {
1472 pos1[
i] = *(
pos +
i + ascan * 3) / 1000.;
1473 vel1[
i] = *(vel +
i + ascan * 3) / 1000.;
1474 att1[
i] = *(att +
i + ascan * 3) / 3600.;
1476 ocorient_(pos1, vel1, att1, sen_mat, coeff);
1480 for (
i = 0;
i < 3;
i++)
1488 int32_t msec32 = (int32_t) (dsec * 1.e3);
1489 int32_t yr = (int32_t)
year;
1490 int32_t dy = (int32_t)
day;
1493 fsol = pow(1.0 /
esdist, 2);
1499 for (ipix = 0; ipix <
l1rec->npix; ipix++)
1500 *(
l1rec->pixnum + ipix) = ipix;
1518 for (ibnd = 0; ibnd <
MAXBANDS; ibnd++) {
1521 if (btype[ibnd] ==
VSWIR)
1522 l1rec->Fo[ivswir] = Fobar[ivswir] * fsol;
1524 moon_affected = ((*(viirs_qual2 + ascan) & 2) != 0) ? 1 : 0;
1526 f_corr = *(f_cal_corr +
mside +
N_MS * (detnum +
NDET * ibnd));
1528 if (sdr_band_typ[ibnd] == 0) {
1531 (
void *) scl_rad) != 0) {
1533 "-E- %s, %d: Failed to read line %d of band %d\n",
1534 __FILE__, __LINE__, aline, ibnd);
1537 for (ipix = 0; ipix <
l1rec->npix; ipix++) {
1541 if ((btype[ibnd] ==
VSWIR)
1549 if ((*(
l1rec->lat + ipix) < -90.)
1550 || (*(
l1rec->lat + ipix) > 90.)
1551 || (*(
l1rec->lon + ipix) < -180.)
1552 || (*(
l1rec->lon + ipix) > 180.)) {
1553 if (btype[ibnd] ==
VSWIR) {
1555 l1rec->navfail[ipix] = 1;
1556 }
else if (btype[ibnd] ==
THERM)
1558 else if (btype[ibnd] ==
CIRRUS)
1565 if (moon_affected == 1)
1566 l1rec->navwarn[ipix] = 1;
1571 switch (*(scl_rad + ipix)) {
1573 if (btype[ibnd] ==
VSWIR) {
1575 l1rec->hilt[ipix] = 1;
1576 }
else if (btype[ibnd] ==
THERM) {
1578 }
else if (btype[ibnd] ==
CIRRUS) {
1588 if (btype[ibnd] ==
VSWIR) {
1590 }
else if (btype[ibnd] ==
THERM) {
1592 }
else if (btype[ibnd] ==
CIRRUS) {
1597 if (btype[ibnd] ==
VSWIR) {
1599 l1rec->navfail[ipix] = 1;
1600 }
else if (btype[ibnd] ==
THERM)
1602 else if (btype[ibnd] ==
CIRRUS)
1606 if (btype[ibnd] ==
VSWIR)
1607 *(
l1rec->Lt +
nbands * ipix + ivswir) = (*(scl_rad
1608 + ipix) * *(scale + ibnd +
MAXBANDS * igran)
1610 *
l1rec->Fo[ivswir] * f_corr
1612 else if (btype[ibnd] ==
THERM)
1614 * (*(scl_rad + ipix)
1615 * *(scale + ibnd +
MAXBANDS * igran)
1618 else if (btype[ibnd] ==
CIRRUS)
1619 l1rec->rho_cirrus[ipix] = (*(scl_rad + ipix)
1620 * *(scale + ibnd +
MAXBANDS * igran)
1627 (
void *) flt_rad) != 0) {
1629 "-E- %s, %d: Failed to read scan %d of band %d\n",
1630 __FILE__, __LINE__, aline, ibnd);
1633 for (ipix = 0; ipix <
l1rec->npix; ipix++) {
1634 if ((btype[ibnd] ==
VSWIR)
1642 if ((*(
l1rec->lat + ipix) < -90.)
1643 || (*(
l1rec->lat + ipix) > 90.)
1644 || (*(
l1rec->lon + ipix) < -180.)
1645 || (*(
l1rec->lon + ipix) > 180.)) {
1646 if (btype[ibnd] ==
VSWIR) {
1648 l1rec->navfail[ipix] = 1;
1649 }
else if (btype[ibnd] ==
THERM)
1651 else if (btype[ibnd] ==
CIRRUS)
1658 if (moon_affected == 1)
1659 l1rec->navwarn[ipix] = 1;
1661 rval = *(flt_rad + ipix);
1663 if (btype[ibnd] ==
VSWIR) {
1665 l1rec->hilt[ipix] = 1;
1666 }
else if (btype[ibnd] ==
THERM) {
1668 }
else if (btype[ibnd] ==
CIRRUS) {
1677 if (btype[ibnd] ==
VSWIR) {
1679 }
else if (btype[ibnd] ==
THERM) {
1681 }
else if (btype[ibnd] ==
CIRRUS) {
1685 if (btype[ibnd] ==
VSWIR) {
1687 l1rec->navfail[ipix] = 1;
1688 }
else if (btype[ibnd] ==
THERM)
1690 else if (btype[ibnd] ==
CIRRUS)
1692 }
else if (btype[ibnd] ==
VSWIR)
1693 *(
l1rec->Lt +
nbands * ipix + ivswir) = rval * f_corr
1696 else if (btype[ibnd] ==
THERM)
1699 else if (btype[ibnd] ==
CIRRUS)
1700 l1rec->rho_cirrus[ipix] = rval * f_corr;
1703 switch (btype[ibnd]) {
1712 l1rec->detnum = (int32_t) detnum;
1713 l1rec->margin_s = margin[1];
1714 l1rec->scn_fmt = scn_fmt;
1719 for (ipix = 0; ipix <
l1rec->npix; ipix++)
1747 for (ict = 0; ict <
MAXBANDS; ict++) {
1748 if (
h5io_close(&(bnd_dat_id[0][ict])) != 0) {
1750 "-E- %s Line %d: Failed to close band data id #%d\n",
1751 __FILE__, __LINE__, ict);
1756 fprintf(
stderr,
"-E- %s Line %d: Failed to close band file %s\n",
1757 __FILE__, __LINE__, vfnames[ict]);
1764 for (ict = 0; ict < 7; ict++) {
1767 "-E- %s Line %d: Failed to close geo data id #%d\n",
1768 __FILE__, __LINE__, ict);
1774 fprintf(
stderr,
"-E- %s Line %d: Failed to close geo data file %s\n",
1775 __FILE__, __LINE__,
file->geofile);