Parcourir la source

i.pca: change output back to DCELL for no rescaling, update manual, copy color table from input band for filtering

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@53388 15284696-431f-4ddb-bdfa-cd5b030d7da7
Markus Metz il y a 12 ans
Parent
commit
96df7652be
4 fichiers modifiés avec 45 ajouts et 52 suppressions
  1. 21 35
      imagery/i.pca/i.pca.html
  2. 1 1
      imagery/i.pca/local_proto.h
  3. 10 8
      imagery/i.pca/main.c
  4. 13 8
      imagery/i.pca/support.c

+ 21 - 35
imagery/i.pca/i.pca.html

@@ -12,41 +12,23 @@ reading the input raster map layers. When the rescale
 option is used, the output files are rescaled to fit the
 option is used, the output files are rescaled to fit the
 min,max range.
 min,max range.
 
 
-<h2>OPTIONS</h2>
-
-<h3>Parameters:</h3>
-<dl>
-<dt><b>input=</b><em>name,name</em>[<em>,name,name</em>,...] 
-<dd>Name of two or more input raster map layers.
-
-<dt><b>output=</b><em>name</em> 
-<dd>The output raster map layer name to which suffixes are
-added.  Each output raster map layer is assigned this
-user-specified <em>name</em> with a numerical (.1, .2, ...
-.n) suffix.
-
-<dt><b>rescale=</b><em>min,max</em> 
-<dd>The optional output category range. (Default: 0,255) If
-rescale=0,0,  no rescaling is performed on output files.
-
-<dd>If output is rescaled, the output raster will be of type CELL. If 
-the output is not rescaled, the output raster will be of type DCELL.
+<p>
+If the output is not rescaled (<em>rescale=0,0</em>, the output raster 
+maps will be of type DCELL, otherwise the output raster maps will be of 
+type CELL.
 
 
-<dt><b>-n</b> <em>normalize input raster maps</em> 
-<dd>By default, the values of the input raster maps are centered for each 
+<p>By default, the values of the input raster maps are centered for each 
 map separately with <em>x - mean</em>. With <em>-n</em>, the input raster 
 map separately with <em>x - mean</em>. With <em>-n</em>, the input raster 
 maps are normalized for each map separately with <em>(x - mean) / stddev</em>. 
 maps are normalized for each map separately with <em>(x - mean) / stddev</em>. 
 Normalizing is highly recommended when the input raster maps have different
 Normalizing is highly recommended when the input raster maps have different
 units, e.g. represent different environmental parameters. 
 units, e.g. represent different environmental parameters. 
 
 
-<dt><b>-f</b> <em>output will be filtered input bands</em> 
-<dd>This option, together with the <em>percent</em> option, can be used 
-to remove noise from input bands. Input bands will be recalculated from 
-a subset of the principal components. The subset is selected by using 
-only the most important (highest eigenvalue) principal components which 
-explain together <em>percent</em> percent variance observed in the 
-input bands.
-</dl>
+<p>The <em>-f</em> flag, together with the <em>percent</em> option, can 
+be used to remove noise from input bands. Input bands will be 
+recalculated from a subset of the principal components (inverse PCA). 
+The subset is selected by using only the most important (highest 
+eigenvalue) principal components which explain together <em>percent</em> 
+percent variance observed in the input bands.
 
 
 
 
 <h2>NOTES</h2>
 <h2>NOTES</h2>
@@ -60,16 +42,20 @@ history files. View with <em>r.info</em>.
 
 
 <h2>EXAMPLE</h2>
 <h2>EXAMPLE</h2>
 
 
-Using the Spearfish Imagery sample dataset
+Using Landsat imagery in the North Carolina sample dataset:
 
 
 <div class="code"><pre>
 <div class="code"><pre>
-i.pca in=spot.ms.1,spot.ms.2,spot.ms.3 out=spot_pca
+i.pca in=lsat7_2002_10,lsat7_2002_20,lsat7_2002_30,lsat7_2002_40,lsat7_2002_50,lsat7_2002_70 \
+    out=lsat7_2002_pca
 
 
-r.info -h spot_pca.1
+r.info -h lsat7_2002_pca.1
    Eigen values, (vectors), and [percent importance]:
    Eigen values, (vectors), and [percent importance]:
-   PC1   1159.75 ( 0.6237, 0.6503, 0.4337) [88.38%]
-   PC2    146.50 ( 0.2403, 0.3685,-0.8981) [11.16%]
-   PC3      5.97 (-0.7438, 0.6643, 0.0735) [ 0.45%]
+   PC1   4334.35 ( 0.2824, 0.3342, 0.5092,-0.0087, 0.5264, 0.5217) [83.04%]
+   PC2    588.31 ( 0.2541, 0.1885, 0.2923,-0.7428,-0.5110,-0.0403) [11.27%]
+   PC3    239.22 ( 0.3801, 0.3819, 0.2681, 0.6238,-0.4000,-0.2980) [ 4.58%]
+   PC4     32.85 ( 0.1752,-0.0191,-0.4053, 0.1593,-0.4435, 0.7632) [ 0.63%]
+   PC5     20.73 (-0.6170,-0.2514, 0.6059, 0.1734,-0.3235, 0.2330) [ 0.40%]
+   PC6      4.08 (-0.5475, 0.8021,-0.2282,-0.0607,-0.0208, 0.0252) [ 0.08%]
 </pre></div>
 </pre></div>
 
 
 
 

+ 1 - 1
imagery/i.pca/local_proto.h

@@ -2,6 +2,6 @@
 #define __LOCAL_PROTO_H__
 #define __LOCAL_PROTO_H__
 
 
 /* support.c */
 /* support.c */
-void write_support(int, char *, double **, double *);
+void write_support(int, char *, char *, double **, double *);
 
 
 #endif
 #endif

+ 10 - 8
imagery/i.pca/main.c

@@ -171,7 +171,7 @@ int main(int argc, char *argv[])
     if (!calc_mu_cov(inp_fd, covar, mu, stddev, bands))
     if (!calc_mu_cov(inp_fd, covar, mu, stddev, bands))
 	G_fatal_error(_("No non-null values"));
 	G_fatal_error(_("No non-null values"));
 
 
-    G_math_d_copy(covar[0], eigmat[0], bands*bands);
+    G_math_d_copy(covar[0], eigmat[0], bands * bands);
     G_debug(1, "Calculating eigenvalues and eigenvectors...");
     G_debug(1, "Calculating eigenvalues and eigenvectors...");
     G_math_eigen(eigmat, eigval, bands);
     G_math_eigen(eigmat, eigval, bands);
 
 
@@ -217,15 +217,18 @@ int main(int argc, char *argv[])
 
 
     /* write colors and history to output */
     /* write colors and history to output */
     for (i = 0; i < bands; i++) {
     for (i = 0; i < bands; i++) {
-	char outname[80];
+	char outname[GNAME_MAX];
+
+	/* close input files */
+	Rast_unopen(inp_fd[i]);
 
 
 	sprintf(outname, "%s.%d", opt_out->answer, i + 1);
 	sprintf(outname, "%s.%d", opt_out->answer, i + 1);
 
 
 	/* write colors and history to file */
 	/* write colors and history to file */
-	write_support(bands, outname, eigmat, eigval);
-
-	/* close input files */
-	Rast_unopen(inp_fd[i]);
+	if (flag_filt->answer)
+	    write_support(bands, opt_in->answers[i], outname, eigmat, eigval);
+	else
+	    write_support(bands, NULL, outname, eigmat, eigval);
     }
     }
     
     
     /* free memory */
     /* free memory */
@@ -395,7 +398,7 @@ write_pca(double **eigmat, double *mu, double *stddev,
     int rows = Rast_window_rows();
     int rows = Rast_window_rows();
     int cols = Rast_window_cols();
     int cols = Rast_window_cols();
     /* why CELL_TYPE when scaling output ? */
     /* why CELL_TYPE when scaling output ? */
-    int outmap_type = (scale) ? CELL_TYPE : FCELL_TYPE;
+    int outmap_type = (scale) ? CELL_TYPE : DCELL_TYPE;
     int outcell_mapsiz = Rast_cell_size(outmap_type);
     int outcell_mapsiz = Rast_cell_size(outmap_type);
     int *out_fd = (int *) G_malloc(bands * sizeof(int));
     int *out_fd = (int *) G_malloc(bands * sizeof(int));
     DCELL **inbuf = (DCELL **) G_malloc(bands * sizeof(DCELL *));
     DCELL **inbuf = (DCELL **) G_malloc(bands * sizeof(DCELL *));
@@ -500,7 +503,6 @@ write_pca(double **eigmat, double *mu, double *stddev,
 			}
 			}
 		    }
 		    }
 
 
-
 		    /* the cell entry is complete */
 		    /* the cell entry is complete */
 		    if (scale && (pass == 1)) {
 		    if (scale && (pass == 1)) {
 			if (first)
 			if (first)

+ 13 - 8
imagery/i.pca/support.c

@@ -8,18 +8,24 @@
 static void write_history(int, char *, double **, double *);
 static void write_history(int, char *, double **, double *);
 
 
 
 
-void write_support(int bands, char *outname, double **eigmat, double *eigval)
+void write_support(int bands, char *inname, char *outname, double **eigmat, double *eigval)
 {
 {
     const char *mapset = G_mapset();
     const char *mapset = G_mapset();
     struct Colors colors;
     struct Colors colors;
     struct FPRange range;
     struct FPRange range;
     DCELL min, max;
     DCELL min, max;
 
 
-    /* make grey scale color table */
-    Rast_read_fp_range(outname, mapset, &range);
-    Rast_get_fp_range_min_max(&range, &min, &max);
+    if (inname) {
+	Rast_read_colors(inname, "", &colors);
+    }
+    else {
+	/* make grey scale color table */
+	Rast_read_fp_range(outname, mapset, &range);
+	Rast_get_fp_range_min_max(&range, &min, &max);
+
+	Rast_make_grey_scale_fp_colors(&colors, min, max);
 
 
-    Rast_make_grey_scale_fp_colors(&colors, min, max);
+    }
 
 
     if (Rast_map_is_fp(outname, mapset))
     if (Rast_map_is_fp(outname, mapset))
 	Rast_mark_colors_as_fp(&colors);
 	Rast_mark_colors_as_fp(&colors);
@@ -66,14 +72,13 @@ static void write_history(int bands, char *outname, double **eigmat, double *eig
 	Rast_append_history(&hist, tmpeigen);
 	Rast_append_history(&hist, tmpeigen);
 
 
 	/* write eigen values to screen */
 	/* write eigen values to screen */
-	if(first_map)
+	if (first_map)
 	    fprintf(stdout, "%s\n", tmpeigen);
 	    fprintf(stdout, "%s\n", tmpeigen);
     }
     }
 
 
-    Rast_command_history(&hist);
-
     /* only write to stderr the first time (this fn runs for every output map) */
     /* only write to stderr the first time (this fn runs for every output map) */
     first_map = FALSE;
     first_map = FALSE;
 
 
+    Rast_command_history(&hist);
     Rast_write_history(outname, &hist);
     Rast_write_history(outname, &hist);
 }
 }