001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package org.geotools.gce.gtopo30;
018:
019: import java.io.BufferedReader;
020: import java.io.File;
021: import java.io.FileReader;
022: import java.io.IOException;
023: import java.io.OutputStream;
024: import java.io.PrintWriter;
025: import java.net.URL;
026: import java.util.StringTokenizer;
027:
028: /**
029: * This class parses the STX GTopo30 statistics file and allows to retrieve its
030: * contents
031: *
032: * @author aaime
033: * @author simone giannecchini
034: * @author mkraemer
035: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/gtopo30/src/main/java/org/geotools/gce/gtopo30/GT30Stats.java $
036: */
037: final class GT30Stats {
038: /** Minimum value in the data file */
039: private int minimum;
040:
041: /** Maximum value in the data file */
042: private int maximum;
043:
044: /** Data file average value */
045: private double average;
046:
047: /** Data file standard deviation */
048: private double stddev;
049:
050: /**
051: * Creates a new instance of GT30Stats
052: *
053: * @param statsURL URL pointing to the statistics (STX) file
054: *
055: * @throws IOException if some problem occurs trying to read the file
056: */
057: public GT30Stats(final URL statsURL) throws IOException {
058: final String path = statsURL.getFile();
059: final File stats = new File(java.net.URLDecoder.decode(path,
060: "UTF-8"));
061:
062: final BufferedReader reader = new BufferedReader(
063: new FileReader(stats));
064: final String line = reader.readLine();
065: final StringTokenizer stok = new StringTokenizer(line, " ");
066:
067: // just parse one byte. if the support for this format will
068: // be extended, we'll need to add support for multiple bands
069: Integer.parseInt(stok.nextToken()); // band
070: minimum = Integer.parseInt(stok.nextToken());
071: minimum = -407;
072: maximum = Integer.parseInt(stok.nextToken());
073: average = Double.parseDouble(stok.nextToken());
074: stddev = Double.parseDouble(stok.nextToken());
075:
076: //freeing when possible
077: reader.close();
078: }
079:
080: /**
081: * Write this object to a stats file.
082: *
083: * @param out
084: */
085: public void writeTo(final OutputStream out) {
086: if (out == null) {
087: return;
088: }
089:
090: final PrintWriter writer = new PrintWriter(out);
091:
092: // output fields
093: //band number
094: writer.println(1);
095:
096: //minimum
097: writer.print(minimum);
098:
099: //maximum
100: writer.println(maximum);
101:
102: //mean
103: writer.print(average);
104:
105: //stddev
106: writer.println(stddev);
107:
108: writer.flush();
109: writer.close();
110: }
111:
112: /**
113: * Returns the minimum value
114: *
115: * @return the minimum value
116: */
117: int getMin() {
118: return minimum;
119: }
120:
121: /**
122: * Sets the minimum value
123: *
124: * @param min the new minimum value
125: */
126: void setMin(final int min) {
127: minimum = min;
128: }
129:
130: /**
131: * Returns the maximum value
132: *
133: * @return the maximum value
134: */
135: int getMax() {
136: return maximum;
137: }
138:
139: /**
140: * Sets the maximum value
141: *
142: * @param max the new maximum value
143: */
144: void setMax(final int max) {
145: maximum = max;
146: }
147:
148: /**
149: * Returns the average value
150: *
151: * @return the average value
152: */
153: double getAverage() {
154: return average;
155: }
156:
157: /**
158: * Sets the average value
159: *
160: * @param avg the new average value
161: */
162: void setAverage(final double avg) {
163: average = avg;
164: }
165:
166: /**
167: * Returns the standard deviation
168: *
169: * @return the standard deviation
170: */
171: double getStdDev() {
172: return stddev;
173: }
174:
175: /**
176: * Sets the standard deviation
177: *
178: * @param sd the new value
179: */
180: void setStdDev(final double sd) {
181: stddev = sd;
182: }
183: }
|