00001 #include "sadie.h"
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 short
00014 ROISTATS (char *name,
00015 short nbnd,
00016 long nval,
00017 PIXEL * gval,
00018 short inc,
00019 short nlev,
00020
00021 PIXEL * gmin,
00022
00023 PIXEL * gmax,
00024
00025 double *mean,
00026
00027 double *dev,
00028
00029 long *zero,
00030
00031 double *cov,
00032
00033 double *cor
00034
00035
00036 )
00037 {
00038 register long i, j, k;
00039 register PIXEL p, q;
00040 char msg[SLEN];
00041 long maxval, npts, *hist = 0;
00042 double factor, *sum = 0, *sumsq = 0;
00043 PIXEL gmin_all, gmax_all;
00044 PIXEL **arrays = NULL;
00045 char minlbl[20], maxlbl[20];
00046 int option = NORM;
00047
00048 if (TIMES)
00049 TIMING (T_ROISTATS);
00050 if (NAMES)
00051 {
00052 MESSAGE ('I', "");
00053 MESSAGE ('I', "ROISTATS");
00054 MESSAGE ('I', "");
00055 sprintf (msg, " Input region: %s", name);
00056 MESSAGE ('I', msg);
00057 sprintf (msg, " Pixel subsample increment: %d", inc);
00058 MESSAGE ('I', msg);
00059 if (nlev > 0)
00060 {
00061 sprintf (msg, " Histogram resolution: %d", nlev);
00062 MESSAGE ('I', msg);
00063 }
00064 MESSAGE ('I', " ...............");
00065 }
00066
00067
00068 if (nbnd <= 0)
00069 {
00070 MESSAGE ('E', " Number of bands must be greater than zero.");
00071 goto the_end;
00072 }
00073 else if (nval <= 0)
00074 {
00075 MESSAGE ('E',
00076 " Number of values must be greater than or equal to zero.");
00077 goto the_end;
00078 }
00079 else if (inc <= 0)
00080 {
00081 MESSAGE ('E', " Increment must be greater than zero.");
00082 goto the_end;
00083 }
00084 else if (nlev < 0)
00085 {
00086 MESSAGE ('E',
00087 " Number of histogram bins must be greater than or equal to zero.");
00088 goto the_end;
00089 }
00090
00091
00092 if (!(sum = (double *) malloc (nbnd * sizeof (double))))
00093 {
00094 MESSAGE ('E', " Memory request failed.");
00095 goto the_end;
00096 }
00097 if (!(sumsq = (double *) malloc (nbnd * nbnd * sizeof (double))))
00098 {
00099 MESSAGE ('E', " Memory request failed.");
00100 goto the_end;
00101 }
00102
00103
00104 for (i = 0; i < nbnd; i++)
00105 {
00106 gmin[i] = gval[i * nval];
00107 gmax[i] = gval[i * nval];
00108 zero[i] = 0L;
00109 sum[i] = 0.0;
00110 for (j = 0; j < nbnd; j++)
00111 {
00112 sumsq[i * nbnd + j] = 0.0;
00113 }
00114 }
00115
00116
00117 for (i = 0; i < nbnd; i += 1)
00118 {
00119 for (j = 0; j < nval; j += inc)
00120 {
00121 p = gval[i * nval + j];
00122 gmin[i] = min (gmin[i], p);
00123 gmax[i] = max (gmax[i], p);
00124 zero[i] += (long) (p == (PIXEL) 0);
00125 sum[i] += (double) p;
00126 sumsq[i * nbnd + i] += (double) (p * p);
00127 }
00128 }
00129 for (i = 0; i < nbnd - 1; i += 1)
00130 {
00131 for (j = i + 1; j < nbnd; j += 1)
00132 {
00133 for (k = 0; k < nval; k += inc)
00134 {
00135 p = gval[i * nval + k];
00136 q = gval[j * nval + k];
00137 sumsq[i * nbnd + j] = sumsq[j * nbnd + i] += (double) (p * q);
00138 }
00139 }
00140 }
00141
00142
00143 for (i = 0; i < nbnd; i++)
00144 {
00145 mean[i] = sum[i] / (double) (nval / inc + (nval % inc ? 1 : 0));
00146 dev[i] =
00147 sqrt (sumsq[i * nbnd + i] /
00148 (double) (nval / inc + (nval % inc ? 1 : 0)) -
00149 mean[i] * mean[i]);
00150 }
00151
00152
00153 MESSAGE ('I', "");
00154 MESSAGE ('I',
00155 " Band Minimum Maximum Mean Deviation Zero/Non-Zero");
00156 for (i = 0; i < nbnd; i++)
00157 {
00158 sprintf (msg, "%5ld%12.4e%12.4e%12.4e%12.4e%10ld/%ld", i + 1,
00159 (double) gmin[i], (double) gmax[i], mean[i], dev[i], zero[i],
00160 nval - zero[i]);
00161 MESSAGE ('I', msg);
00162 }
00163
00164 if (nlev > 0)
00165 {
00166
00167 arrays = (PIXEL **) malloc (nbnd * sizeof (PIXEL *));
00168 if (!arrays)
00169 {
00170 MESSAGE ('E', " Error allocating memory for plotting!");
00171 goto the_end;
00172 }
00173 for (i = 0; i < nbnd; i++)
00174 {
00175 arrays[i] = NULL;
00176 arrays[i] = (PIXEL *) malloc (nlev * sizeof (PIXEL));
00177 if (!arrays[i])
00178 {
00179 MESSAGE ('E', " Error allocating memory for plotting!");
00180 goto the_end;
00181 }
00182 }
00183
00184
00185 if (!(hist = (long *) malloc (nbnd * nlev * sizeof (long))))
00186 {
00187 MESSAGE ('E', " Memory request failed.");
00188 goto the_end;
00189 }
00190
00191
00192 for (i = 0; i < nbnd * nlev; i++)
00193 {
00194 hist[i] = 0L;
00195 }
00196 p = gmin[0];
00197 q = gmax[0];
00198 for (i = 1; i < nbnd; i++)
00199 {
00200 p = min (p, gmin[i]);
00201 q = max (q, gmax[i]);
00202 }
00203 factor = (double) nlev / (double) (q - p + 1);
00204
00205
00206 for (i = 0; i < nbnd; i++)
00207 {
00208 for (j = 0; j < nval; j += inc)
00209 {
00210 if (gval[i * nval + j] <= p)
00211 {
00212 hist[i * nlev + 0] += 1L;
00213 }
00214 else if (p < gval[i * nval + j] && gval[i * nval + j] < q)
00215 {
00216 hist[i * nlev +
00217 (short) ((double) (gval[i * nval + j] - p) *
00218 factor)] += 1L;
00219 }
00220 else
00221 {
00222 hist[i * nlev + nlev - 1] += 1L;
00223 }
00224 }
00225 }
00226
00227
00228 for (maxval = hist[0], j = 0; j < nbnd * nlev; j++)
00229 {
00230 maxval = max (maxval, hist[j]);
00231 }
00232
00233 gmin_all = gmin[0];
00234 gmax_all = gmax[0];
00235 for (i = 0; i < nbnd; i++)
00236 {
00237 gmin_all = min (gmin_all, gmin[i]);
00238 gmax_all = max (gmax_all, gmax[i]);
00239 }
00240
00241
00242 MESSAGE ('I', "");
00243 sprintf (msg, " Histogram has %d levels between %.4e and %.4e", nlev, p,
00244 q);
00245 MESSAGE ('I', msg);
00246
00247
00248 for (i = 0; i < nbnd; i++)
00249 {
00250 for (j = 0; j < nlev; j++)
00251 {
00252 arrays[i][j] = (PIXEL) hist[i * nlev + j];
00253 }
00254 }
00255
00256 sprintf (minlbl, "%.2f", gmin_all);
00257 sprintf (maxlbl, "%.2f", gmax_all);
00258
00259 PLOT (arrays, nbnd, nlev, minlbl, maxlbl, option);
00260
00261 }
00262
00263 if (cov != NULL)
00264 {
00265
00266
00267 for (i = 0; i < nbnd; i++)
00268 {
00269 for (j = 0; j < nbnd; j++)
00270 {
00271 cov[i * nbnd + j] =
00272 (sumsq[i * nbnd + j] -
00273 sum[i] * sum[j] / (double) (nval / inc +
00274 (nval % inc ? 1 : 0))) /
00275 ((double) (nval / inc + (nval % inc ? 1 : 0) - 1));
00276 }
00277 }
00278
00279
00280 MESSAGE ('I', "");
00281 MESSAGE ('I', " Covariance Matrix");
00282 for (i = 0; i < nbnd; i++)
00283 {
00284 for (j = k = 0, msg[k++] = ' '; j < nbnd; j += 1, k = strlen (msg))
00285 {
00286 sprintf (msg + k, "%12.4e", cov[i * nbnd + j]);
00287 }
00288 MESSAGE ('I', msg);
00289 }
00290 }
00291
00292 if (cov != NULL && cor != NULL)
00293 {
00294
00295
00296 for (i = 0; i < nbnd; i++)
00297 {
00298 for (j = 0; j < nbnd; j++)
00299 {
00300 cor[i * nbnd + j] =
00301 cov[i * nbnd +
00302 j] / sqrt (cov[i * nbnd + i] * cov[j * nbnd + j]);
00303 }
00304 }
00305
00306
00307 MESSAGE ('I', "");
00308 MESSAGE ('I', " Correlation Matrix");
00309 for (i = 0; i < nbnd; i++)
00310 {
00311 for (j = k = 0, msg[k++] = ' '; j < nbnd; j += 1, k = strlen (msg))
00312 {
00313 sprintf (msg + k, "%12.4e", cor[i * nbnd + j]);
00314 }
00315 MESSAGE ('I', msg);
00316 }
00317 }
00318
00319 the_end:
00320
00321 if (arrays)
00322 {
00323 for (i = 0; i < nbnd; i++)
00324 {
00325 if (arrays[i])
00326 free (arrays[i]);
00327 arrays[i] = NULL;
00328 }
00329 free (arrays);
00330 arrays = NULL;
00331 }
00332
00333 if (hist)
00334 free (hist);
00335 if (sum)
00336 free (sum);
00337 if (sumsq)
00338 free (sumsq);
00339 if (TIMES)
00340 TIMING (T_EXIT);
00341 }