16 #define NEG_FLAG -9.1E6
18 int main(
int argc,
char *argv[])
43 int32_t fid, sdfid, c_fid, c_sdfid;
44 int32_t
nbins, c_nbins, wtchk;
48 clock_t
val = 0, val3 = 0;
50 val = (
float) clock() / CLOCKS_PER_SEC;
60 if (argc != 4 && argc != 3) {
61 printf(
"\n***** Usage: l3stat_chk <l3 bin file> <control file> ");
62 printf(
"[<climatology bin file>]\n\n");
63 printf(
" <l3 bin file> is level 3 bin data file\n");
64 printf(
" <control file> is the file specifying thresholds\n");
65 printf(
" [<climatology bin file>] full name of optional clim bin file ");
66 printf(
"\n\nOn exit, $status will be set to: \n\n 0 - no problems,"
67 "\n 1 - program error, ");
68 printf(
"\n 2 - data problem, \n 3 - both program and data problem\n");
73 printf(
"\n\n# Statistical Check of Dataset '%s'\n\n", argv[1]);
80 if ((fid = Hopen(argv[1], DFACC_RDONLY, 0)) < 0) {
81 printf(
"****** l3stat_chk: Failed on the Hopen of \n '%s'\n", argv[1]);
87 if ((c_fid = Hopen(argv[3], DFACC_RDONLY, 0)) < 0) {
88 printf(
"****** l3stat_chk: Failed on the Hopen of \n '%s'\n", argv[3]);
99 if (c_fid != 0) Vstart(c_fid);
105 if ((sdfid = SDstart(argv[1], DFACC_RDONLY)) < 0) {
106 printf(
"******* l3stat_chk: Failure at SDstart of \n'%s'\n", argv[1]);
113 if ((c_sdfid = SDstart(argv[3], DFACC_RDONLY)) < 0) {
114 printf(
"******* l3stat_chk: Failure at SDstart of \n'%s'\n", argv[3]);
129 if ((
read_cntldata(argv[2], databinchk, grschk, statchk, &wtchk, climchk))
139 if (
l3file(sdfid, c_sdfid, &
nbins, &c_nbins, ptype) < 0) {
148 if (databinchk[0].param && (strcmp(ptype,
"scene") != 0))
151 if (c_fid != 0) Hclose(c_fid);
162 if (c_fid != 0) Hclose(c_fid);
193 val3 = (
float) clock() / CLOCKS_PER_SEC;
194 printf(
"\n\n# Time took for %s = %d secs\n", argv[0], (
int) (val3 -
val));
229 int32_t
read_cntldata(
char *cntl_file, cntl_str *databinchk, cntl_str *grschk,
230 cntl_str *statchk, int32_t *wtchk, clim_str *climchk) {
233 int32_t
i, param, nflds = 0;
235 printf(
"\n# Reading control file '%s'\n\n", cntl_file);
240 if ((fid = fopen(cntl_file,
"r")) ==
NULL) {
241 printf(
"\n****read_cntldata: Unable to open control file:\n '%s'\n",
252 databinchk[0].param = databinchk[0].err_thresh = 0;
253 databinchk[0].low_thresh = databinchk[0].high_thresh = 0;
255 grschk[
i].param = statchk[
i].param = 0;
256 grschk[
i].err_thresh = statchk[
i].err_thresh = 0;
257 grschk[
i].low_thresh = statchk[
i].low_thresh = 0;
258 grschk[
i].high_thresh = statchk[
i].high_thresh = 0;
259 climchk[
i].param = 0;
260 climchk[
i].thresh1H = climchk[
i].thresh2H = climchk[
i].thresh3H = 0;
261 climchk[
i].thresh1L = climchk[
i].thresh2L = climchk[
i].thresh3L = 0;
267 while (fgets(
line, 500, fid) !=
NULL) {
269 for (
i = 0;
i < 500 && (
line[
i] ==
' ' ||
line[
i] ==
'\t');
i++);
272 if (
i < 500 &&
line[
i] ==
'#') {
279 if (strncmp(&
line[
i],
"L3DAT", 5) == 0) {
280 if ((nflds = sscanf(
line,
"%s %f %f",
str, &databinchk[0].low_thresh,
281 &databinchk[0].high_thresh)) != 3)
283 databinchk[0].param = 1;
284 }
else if (strncmp(&
line[
i],
"L3WT", 4) == 0)
286 else if (strncmp(&
line[
i],
"L3GCHK", 6) == 0) {
287 param = atoi(&
line[6]);
288 if (param < 1 || param >
NPARAMS) {
289 printf(
"********read_cntldata: Error in parameter number.");
290 printf(
" Parameter # read = %d", param);
294 if ((nflds = sscanf(
line,
"%s %f %f %f",
str,
295 &grschk[param - 1].err_thresh, &grschk[param - 1].low_thresh,
296 &grschk[param - 1].high_thresh)) != 4)
298 grschk[param - 1].param = 1;
299 }
else if (strncmp(&
line[
i],
"L3STAT", 6) == 0) {
300 param = atoi(&
line[6]);
301 if (param < 1 || param >
NPARAMS) {
302 printf(
"********read_cntldata: Error in param number.");
303 printf(
" Parameter # read = %d", param);
307 statchk[param - 1].param = 1;
308 }
else if (strncmp(&
line[
i],
"L3CCHK", 6) == 0) {
309 param = atoi(&
line[6]);
310 if (param < 1 || param >
NPARAMS) {
311 printf(
"********read_cntldata: Error in param number.");
312 printf(
" Parameter # read = %d", param);
316 if ((nflds = sscanf(
line,
"%s %f %f %f %f %f %f",
str,
317 &climchk[param - 1].thresh1L, &climchk[param - 1].thresh1H,
318 &climchk[param - 1].thresh2L, &climchk[param - 1].thresh2H,
319 &climchk[param - 1].thresh3L, &climchk[param - 1].thresh3H)) < 7)
321 climchk[param - 1].param = 1;
347 int32_t
l3file(int32_t sdfid, int32_t c_sdfid, int32_t *
nbins, int32_t *c_nbins,
358 if (strcmp(
title,
"SeaWiFS Level-3 Binned Data") != 0) {
359 printf(
"l3file: Data file is not level 3 bin file");
364 if ((
rdattr(sdfid,
"Data Bins", (VOIDP)
nbins)) < 0)
368 if ((
rdattr(c_sdfid,
"Data Bins", (VOIDP) c_nbins)) < 0)
372 if ((
rdattr(sdfid,
"Product Type", ptype)) < 0)
375 printf(
"\n# Data containing bins = %d", *
nbins);
376 printf(
"\n\n# Data containing bins in climatology file = %d", *c_nbins);
406 if ((
rdattr(sdfid,
"Percent Data Bins", &pctbins)) < 0)
409 if (pctbins > databinchk[0].high_thresh)
411 else if (pctbins < databinchk[0].low_thresh)
418 printf(
"\n\n#Mnemonic Flag %%data bins Err_low_thr Err_high_thr");
419 printf(
"\n#----------------------------------------------------\n");
421 printf(
"\nL3DAT %8d %12.6f %12.6f %12.6f", flag, pctbins,
422 databinchk[0].low_thresh, databinchk[0].high_thresh);
452 int32_t vsref, vsid,
val = 0, flag = 0;
453 int32_t
start = 0, elts = 0, ret;
454 float32 wt_buf[
BUFSZ];
457 if ((vsref = VSfind(fid,
"BinList")) < 0) {
458 printf(
"\nchk_weight: VSfind failed for vdata 'BinList'");
464 if ((vsid = VSattach(fid, vsref,
"r")) < 0) {
465 printf(
"\nchk_weight: VSattach failed for vdata 'BinList'");
499 printf(
"\n\n#Mnemonic Flag Value ");
500 printf(
"\n#-----------------------\n");
501 printf(
"\nL3WT %d %d", flag,
val);
504 printf(
"\n\n****Suspending further check..");
505 printf(
" %d bins have been found with weight set to 0.",
val);
553 int32_t c_nbins, cntl_str *grschk, cntl_str *statchk,
555 int32_t
i, ci,
p,
j, done, c_done, rd_flag, rd_c_flag;
556 int32_t
st, c_st, elts, c_elts;
557 int32_t prev_c_st = -1;
558 int32_t binlist_id, c_binlist_id, proceed = 0;
566 float64 c_xbar = 0, c_sd = 0;
568 float64 loparm[
NPARAMS] = {1.e10, 1.e10, 1.e10, 1.e10, 1.e10, 1.e10,
569 1.e10, 1.e10, 1.e10, 1.e10, 1.e10};
570 float64 hiparm[
NPARAMS] = {-1.e10, -1.e10, -1.e10, -1.e10, -1.e10,
571 -1.e10, -1.e10, -1.e10, -1.e10, -1.e10, -1.e10};
572 float64 negflag[
NPARAMS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
573 float64 num1Lstd[
NPARAMS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
574 float64 num1Hstd[
NPARAMS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
575 float64 num2Lstd[
NPARAMS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
576 float64 num2Hstd[
NPARAMS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
577 float64 num3Lstd[
NPARAMS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
578 float64 num3Hstd[
NPARAMS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
579 float64 value1[
NPARAMS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
580 float64 value2[
NPARAMS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
581 float64 sumxbar[
NPARAMS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
582 float64 sumsd[
NPARAMS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
584 printf(
"\n\n# Doing gross, statistical and climatology checks\n");
591 if (grschk[
p].param || statchk[
p].param || climchk[
p].param)
602 binlist_id =
get_vsid(fid,
"BinList");
603 if (c_fid != 0)c_binlist_id =
get_vsid(c_fid,
"BinList");
620 rd_flag = rd_c_flag = 1;
624 elts = c_elts =
BUFSZ;
638 if ((
rdvdata(binlist_id,
"bin_num",
st, elts,
639 (
unsigned char *) binno)) < 0)
641 if ((
rdvdata(binlist_id,
"weights",
st, elts,
642 (
unsigned char *) wts)) < 0)
644 if ((
rdvdata(binlist_id,
"nscenes",
st, elts,
645 (
unsigned char *) scenes)) < 0)
650 (
unsigned char *)
data)) < 0)
654 for (
j = 0;
j < elts;
j++) {
656 sumxbar[
p] += xbar[
p][
j];
657 sumsd[
p] += xbar[
p][
j] * xbar[
p][
j];
661 if (xbar[
p][
j] < loparm[
p]) loparm[
p] = xbar[
p][
j];
662 if (xbar[
p][
j] > hiparm[
p]) hiparm[
p] = xbar[
p][
j];
668 if (xbar[
p][
j] < grschk[
p].low_thresh)
670 if (xbar[
p][
j] > grschk[
p].high_thresh) {
673 printf(
"\n#indx= %d,sum= %f,sumxx= %f,wts= %f,mean= %f",
687 if ((
rdvdata(c_binlist_id,
"bin_num", c_st, c_elts,
688 (
unsigned char *) c_binno)) < 0)
692 while (
i < elts && ci < c_elts) {
693 if (binno[
i] < c_binno[ci])
695 else if (binno[
i] > c_binno[ci])
699 if (prev_c_st != c_st) {
702 if ((
rdvdata(c_binlist_id,
"weights", c_st, c_elts,
703 (
unsigned char *) c_wts)) < 0)
705 if ((
rdvdata(c_binlist_id,
"nscenes", c_st, c_elts,
706 (
unsigned char *) c_scenes)) < 0)
710 (
unsigned char *) c_data[
p])) < 0)
717 c_xbar = c_data[
p][ci][0] / c_wts[ci];
718 c_sd =
calc_sd(c_xbar, c_wts[ci], c_scenes[ci],
724 if (xbar[
p][
i] < (c_xbar - 3 * c_sd)) {
728 }
else if (xbar[
p][
i] < (c_xbar - 2 * c_sd)) {
731 }
else if (xbar[
p][
i] < (c_xbar - c_sd))
733 if (xbar[
p][
i] > (c_xbar + 3 * c_sd)) {
737 }
else if (xbar[
p][
i] > (c_xbar + 2 * c_sd)) {
740 }
else if (xbar[
p][
i] > (c_xbar + c_sd))
752 if (ci == c_elts && c_elts != 0) {
754 if (c_st + c_elts > c_nbins)
755 c_elts = c_nbins - c_st;
766 if ((
i == elts && elts != 0) || c_done) {
776 VSdetach(binlist_id);
777 if (c_fid != 0) VSdetach(c_binlist_id);
788 VSdetach(param_vsid[
p]);
789 if (c_fid != 0) VSdetach(c_param_vsid[
p]);
792 if (c_fid != 0)
pr_clim_results(clim_file, climchk, npts, num1Lstd, num1Hstd,
793 num2Lstd, num2Hstd, num3Lstd, num3Hstd);
820 printf(
"\n\nSuccessful check, no statistical errors\n");
823 printf(
"\n\nFailure of statistical check due to program error\n");
825 printf(
"\n\nFailure of statistical check due to data error\n");
853 int32_t
rdattr(int32_t sdfid,
char *attr_name,
void *buf) {
856 if ((attrnum = SDfindattr(sdfid, attr_name)) < 0) {
857 printf(
"\n****rdattr: Failure in SDfindattr while trying to read %s",
863 if ((SDreadattr(sdfid, attrnum, buf)) < 0) {
864 printf(
"\n****rdattr: Failure in SDreadattr while trying to read %s",
881 printf(
"\n*****Expecting %d value(s) for - %s, but, read %d value(s). \n",
917 if (
rdvdata(vsid, fld_name,
start, elts, (
unsigned char *) wt_buf) < 0)
920 for (
i = 0;
i < elts;
i++)
955 if ((vsref = VSfind(fid, vs_name)) < 0) {
956 printf(
"\nget_vsid: VSfind failed for vdata '%s'", vs_name);
962 if ((vsid = VSattach(fid, vsref,
"r")) < 0) {
963 printf(
"\nget_vsid: VSattach failed for vdata '%s'", vs_name);
990 float64
calc_sd(float64 xmean, float32 wts,
int16 nseg, float32 sumxx) {
995 sd = (sumxx / wts) - xmean * xmean;
999 sd = sqrt(sd * ww / (ww - nseg));
1032 int32_t
p, flag, failcode = 0;
1039 printf(
"\n\n#Mnemonic Flag %%<Low %%>High Err_thr ");
1040 printf(
"low_thr high_thr");
1041 printf(
"\n#-----------------------------------------------------------");
1042 printf(
"-------------------\n");
1045 if (grschk[
p].param) {
1047 v1 = value1[
p] /
nbins * 100;
1049 if (v1 > grschk[
p].err_thresh)
1051 else if (
v2 > grschk[
p].err_thresh)
1054 printf(
"\nL3GCHK%d %5d %11.6f %12.6f %12.6f %12.6f %12.6f",
1055 p + 1, flag, v1,
v2, grschk[
p].err_thresh,
1056 grschk[
p].low_thresh, grschk[
p].high_thresh);
1093 float64 *sumsd, float64 *loparm, float64 *hiparm) {
1094 int32_t
p, flag = 0;
1095 float64 sum,
sumsq, xbar, sd;
1102 printf(
"#Mnemonic flag nbins xbar sd Low High ");
1103 printf(
"\n#----------------------------------------------------\n");
1106 if (statchk[
p].param) {
1107 sum = sumxbar[
p] /
nbins;
1110 sd = sqrt(
sumsq - (sum * sum));
1112 printf(
"\nL3STAT%d %5d %7d %12.9f %12.9f %f %f",
p + 1, flag,
nbins,
1113 xbar, sd, loparm[
p], hiparm[
p]);
1150 float64 *num1Hstd, float64 *num1Lstd, float64 *num2Hstd,
1151 float64 *num2Lstd, float64 *num3Hstd, float64 *num3Lstd) {
1152 int32_t
p, failcode = 0;
1153 int32_t flag1_H, flag2_H, flag3_H;
1154 int32_t flag1_L, flag2_L, flag3_L;
1159 pct1Lstd[
p] = pct2Lstd[
p] = pct3Lstd[
p] = 0;
1160 pct1Hstd[
p] = pct2Hstd[
p] = pct3Hstd[
p] = 0;
1167 printf(
"\n\nCLIM_FILE_USED: %s", clim_file);
1168 printf(
"\n\nCLIM_NPTS: %f\n", npts);
1170 printf(
"#Mnemonic flag #std dev %%outside %%thresh ");
1171 printf(
"\n#----------------------------------------------------\n");
1174 if (climchk[
p].param) {
1176 pct1Hstd[
p] = 100 * num1Hstd[
p] / npts;
1177 pct1Lstd[
p] = 100 * num1Lstd[
p] / npts;
1178 pct2Hstd[
p] = 100 * num2Hstd[
p] / npts;
1179 pct2Lstd[
p] = 100 * num2Lstd[
p] / npts;
1180 pct3Hstd[
p] = 100 * num3Hstd[
p] / npts;
1181 pct3Lstd[
p] = 100 * num3Lstd[
p] / npts;
1184 flag1_H = flag1_L = flag2_H = flag2_L = flag3_H = flag3_L = 0;
1186 if (pct3Lstd[
p] > climchk[
p].thresh3L)
1188 if (pct2Lstd[
p] > climchk[
p].thresh2L)
1190 if (pct1Lstd[
p] > climchk[
p].thresh1L)
1193 if (pct3Hstd[
p] > climchk[
p].thresh3H)
1195 if (pct2Hstd[
p] > climchk[
p].thresh2H)
1197 if (pct1Hstd[
p] > climchk[
p].thresh1H)
1200 if (flag1_H || flag1_L || flag2_H || flag2_L || flag3_H || flag3_L)
1203 printf(
"\n\nL3CCHK%d %6d %10s %12.6lf %12.6f",
1204 p + 1, flag1_L,
"LO_1", pct1Lstd[
p], climchk[
p].thresh1L);
1205 printf(
"\nL3CCHK%d %6d %10s %12.6lf %12.6f",
1206 p + 1, flag1_H,
"HI_1", pct1Hstd[
p], climchk[
p].thresh1H);
1207 printf(
"\nL3CCHK%d %6d %10s %12.6f %12.6f",
1208 p + 1, flag2_L,
"LO_2", pct2Lstd[
p], climchk[
p].thresh2L);
1209 printf(
"\nL3CCHK%d %6d %10s %12.6f %12.6f",
1210 p + 1, flag2_H,
"HI_2", pct2Hstd[
p], climchk[
p].thresh2H);
1211 printf(
"\nL3CCHK%d %6d %10s %12.6f %12.6f",
1212 p + 1, flag3_L,
"LO_3", pct3Lstd[
p], climchk[
p].thresh3L);
1213 printf(
"\nL3CCHK%d %6d %10s %12.6f %12.6f",
1214 p + 1, flag3_H,
"HI_3", pct3Hstd[
p], climchk[
p].thresh3H);