14 static int nwts =
NWTS;
15 static int lastScanRun = -1;
18 void fuzzy_func_v3(
float *rrs,
float **urrs,
float ***y3inv,
int nclass,
int nwts,
int df,
double *outdata);
25 static int firstCall = 1;
27 static int nrrs =
NRRS;
30 static float rrs[
NRRS];
35 static float ***y3inv;
42 int32_t ip, ib, ic, ipb;
44 l1str *
l1rec = l2rec->l1rec;
50 int32 sd_id, sds_id,
i,
j,
k;
51 int32
dims1[2], start1[2], edge1[2];
52 int32
dims2[3], start2[3], edge2[3];
53 char fname[FILENAME_MAX];
54 char sdsname[H4_MAX_NC_NAME] =
"";
59 if (strlen(fname) == 0) {
60 printf(
"-E- %s line %d: No owtfile specified.\n", __FILE__, __LINE__);
63 printf(
"\nLoading optical class data from %s\n", fname);
64 sd_id = SDstart(fname, DFACC_RDWR);
67 if ((wave = (
float *) calloc(
l1file->nbands, sizeof (
float))) ==
NULL) {
68 printf(
"-E- : Error allocating memory to run_owt\n");
72 switch (
l1file->sensorID) {
90 printf(
"%s Line %d: No classification data available for this sensor.\n", __FILE__, __LINE__);
95 for (ib = 0; ib < nrrs; ib++)
99 urrs =
matrix(1, nrrs, 1, nclass);
100 y3inv =
f3tensor(1, nclass, 1, nrrs, 1, nrrs);
103 printf(
"-E- %s line %d : error allocating memory for optical classes.\n",
109 printf(
"-E- %s line %d : error allocating memory for optical classes.\n",
113 owtd = (
float *) calloc(
l1rec->npix, sizeof (
float));
115 printf(
"-E- %s line %d : error allocating memory for optical classes.\n",
124 edge1 [0] =
dims1[0];
125 edge1 [1] =
dims1[1];
127 strcpy(sdsname,
"class_means");
128 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
129 status = SDreaddata(sds_id, start1,
NULL, edge1, (VOIDP) rrs_means);
131 printf(
"-E- %s: Error reading SDS %s from %s.\n", __FILE__, sdsname, fname);
135 status = SDendaccess(sds_id);
144 edge2 [0] =
dims2[0];
145 edge2 [1] =
dims2[1];
146 edge2 [2] =
dims2[2];
148 strcpy(sdsname,
"class_covariance");
149 sds_id = SDselect(sd_id, SDnametoindex(sd_id, sdsname));
150 status = SDreaddata(sds_id, start2,
NULL, edge2, (VOIDP) rrs_cov);
152 printf(
"-E- %s: Error reading SDS %s from %s.\n", __FILE__, sdsname, fname);
156 status = SDendaccess(sds_id);
159 for (ic = 0; ic < nclass; ic++)
160 for (ib = 0; ib < nrrs; ib++) {
161 urrs[ib + 1][ic + 1] = rrs_means[ib * nclass + ic];
168 for (
i = 0;
i < nclass;
i++)
for (
j = 0;
j < nrrs;
j++)
169 printf(
"urrs %d %d %f\n",
j + 1,
i + 1, urrs[
j + 1][
i + 1]);
171 for (
i = 0;
i < nclass;
i++)
for (
j = 0;
j < nrrs;
j++)
for (
k = 0;
k < nrrs;
k++)
172 printf(
"y3inv %d %d %d %f\n",
i + 1,
j + 1,
k + 1, y3inv[
i + 1][
j + 1][
k + 1]);
176 for (ip = 0; ip <
l1rec->npix; ip++) {
178 ipb = ip *
l1file->nbands;
182 for (ic = 0; ic < nclass; ic++) {
183 owt [ip][ic] = badval;
184 owtn[ip][ic] = badval;
189 for (ib = 0; ib < nrrs; ib++) {
191 if (rrs[ib] < 0)
status++;
192 if (
DEBUG_OWT) printf(
"\nrrs %d %f %f", ib, rrs[ib], l2rec->Rrs[ipb +
bindx[ib]]);
198 fuzzy_func_v3(rrs, urrs, y3inv, nclass, nwts, nrrs, outclass);
202 for (ic = 0; ic < nwts; ic++) {
203 if (outclass[ic] < 1e-35)
205 owt[ip][ic] = (
float) outclass[ic];
206 owt_sum += owt[ip][ic];
207 if (
DEBUG_OWT) printf(
"\n%d %d %f", ip, ic, outclass[ic]);
208 if (owt[ip][ic] > owt_max) {
209 owt_max = owt[ip][ic];
213 for (ic = 0; ic < nwts; ic++) {
214 if (outclass[ic] >= 0.0)
215 owtn[ip][ic] = (
float) outclass[ic] / owt_sum;
225 lastScanRun =
l1rec->iscan;
242 float chl_error(
char *fname,
float wts[],
int nwts,
int dclass) {
243 static int firstCall = 1;
246 static float min_sum_wts = 1e-6;
248 float perc, rms,
bias;
258 if (strlen(fname) == 0) {
259 printf(
"-E- %s line %d: No owtchlerrfile specified.\n", __FILE__, __LINE__);
262 printf(
"\nLoading chl error table from %s\n", fname);
264 if ((fp = fopen(fname,
"r")) ==
NULL) {
266 "-E- %s line %d: unable to open %s for reading\n",
267 __FILE__, __LINE__, fname);
271 if ((erec = (errstr *) malloc(nwts *
sizeof (errstr))) ==
NULL) {
272 printf(
"-E- %s line %d: error allocating space for error rec.\n", __FILE__, __LINE__);
277 while (fgets(
line, 80, fp)) {
278 if (
line[0] ==
'#' ||
line[0] ==
'\n')
280 sscanf(
line,
"%d %f %f %f\n", &clss, &perc, &rms, &
bias);
281 erec[ic].perc = perc;
282 erec[ic].bias =
bias;
285 if (ic > nwts)
break;
289 printf(
"-E- %s line %d: Number of weights (%d) does not match number of error records (%d) in %s\n",
290 __FILE__, __LINE__, nwts, ic, fname);
298 if (erec[dclass - 1].perc > 0.0) {
302 for (ic = 0; ic < nwts; ic++) {
303 if (wts[ic] >= 0.0 && erec[ic].perc > 0.0) {
304 perc += (wts[ic] * erec[ic].perc);
305 bias += (wts[ic] * erec[ic].perc);
306 rms += (wts[ic] * erec[ic].perc);
312 if (sum_wts > min_sum_wts)
326 int classID =
p->prod_ix - 1;
327 int prodID =
p->cat_ix;
331 l1str *
l1rec = l2rec->l1rec;
333 if (classID < 0 || classID > nwts) {
334 printf(
"%s line %d: there is no class member %d\n", __FILE__, __LINE__, classID + 1);
338 if (
l1rec->iscan != lastScanRun)
343 for (ip = 0; ip <
l1rec->npix; ip++)
344 *fptr++ = owt[ip][classID];
347 for (ip = 0; ip <
l1rec->npix; ip++)
348 *fptr++ = owtn[ip][classID];
351 for (ip = 0; ip <
l1rec->npix; ip++)
355 for (ip = 0; ip <
l1rec->npix; ip++)
356 *fptr++ =
chl_error(
input->owtchlerrfile, &owt[ip][0], nwts, owtd[ip]);