Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/exports/minfo/SedimentLoadExporter.java @ 8134:dfcc96deebd8
Make SedimentloadExporter work with the new result scheme.
Pretty much untested.
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Mon, 25 Aug 2014 20:02:28 +0200 |
parents | 963ede7b32bb |
children | cda45ed151ed |
comparison
equal
deleted
inserted
replaced
8133:45b1d71110c3 | 8134:dfcc96deebd8 |
---|---|
8 | 8 |
9 package org.dive4elements.river.exports.minfo; | 9 package org.dive4elements.river.exports.minfo; |
10 | 10 |
11 import java.util.LinkedList; | 11 import java.util.LinkedList; |
12 import java.util.List; | 12 import java.util.List; |
13 import java.util.TreeSet; | 13 import java.util.TreeMap; |
14 | 14 |
15 import java.io.OutputStream; | 15 import java.io.OutputStream; |
16 import java.io.IOException; | 16 import java.io.IOException; |
17 | 17 |
18 import java.text.NumberFormat; | 18 import java.text.NumberFormat; |
19 | 19 |
20 import org.apache.log4j.Logger; | 20 import org.apache.log4j.Logger; |
21 | 21 |
22 import org.dive4elements.river.artifacts.access.SedimentLoadAccess; | 22 import org.dive4elements.river.artifacts.access.SedimentLoadAccess; |
23 import org.dive4elements.river.artifacts.model.CalculationResult; | 23 import org.dive4elements.river.artifacts.model.CalculationResult; |
24 import org.dive4elements.river.artifacts.model.minfo.SedimentLoadLSData; | 24 import org.dive4elements.river.artifacts.model.minfo.SedimentLoadDataResult.Fraction; |
25 import org.dive4elements.river.artifacts.model.minfo.SedimentLoadFraction; | 25 import org.dive4elements.river.artifacts.model.minfo.SedimentLoadDataResult; |
26 import org.dive4elements.river.artifacts.model.minfo.SedimentLoadResult; | |
27 | 26 |
28 import org.dive4elements.river.artifacts.D4EArtifact; | 27 import org.dive4elements.river.artifacts.D4EArtifact; |
29 | 28 |
30 import org.dive4elements.river.exports.AbstractExporter; | 29 import org.dive4elements.river.exports.AbstractExporter; |
31 | 30 |
69 "export.sedimentload_ls.csv.header.suspsediment"; | 68 "export.sedimentload_ls.csv.header.suspsediment"; |
70 | 69 |
71 public static final String CSV_TOTAL = | 70 public static final String CSV_TOTAL = |
72 "export.sedimentload_ls.csv.header.total"; | 71 "export.sedimentload_ls.csv.header.total"; |
73 | 72 |
73 /* Header is: suspended_sediment, susp_sand, susp_sand_bed, sand, | |
74 * fine_middle, coarse, total */ | |
75 private static final String[] FRACTION_ORDER = { | |
76 "suspended_sediment", | |
77 "susp_sand", | |
78 "susp_sand_bed", | |
79 "sand", | |
80 "fine_middle", | |
81 "coarse", | |
82 "total" | |
83 }; | |
74 | 84 |
75 /** Collected results. */ | 85 /** Collected results. */ |
76 private SedimentLoadResult[] results; | 86 private SedimentLoadDataResult result; |
77 | 87 |
78 /** Empty constructor. */ | 88 /** Empty constructor. */ |
79 public SedimentLoadExporter() { | 89 public SedimentLoadExporter() { |
80 results = new SedimentLoadResult[0]; | |
81 } | 90 } |
82 | 91 |
83 /** Process all stored data and write csv. */ | 92 /** Process all stored data and write csv. */ |
84 @Override | 93 @Override |
85 protected void writeCSVData(CSVWriter writer) throws IOException { | 94 protected void writeCSVData(CSVWriter writer) throws IOException { |
95 if (result == null) { | |
96 return; | |
97 } | |
86 writeCSVHeader(writer); | 98 writeCSVHeader(writer); |
87 | 99 |
88 for (SedimentLoadResult result: results) { | 100 /* Prepare the values. The order of the fractions is given by the |
89 String years = (result.getEndYear() == 0) | 101 * header and thus static. */ |
90 ? result.getStartYear() + " " | 102 |
91 : result.getStartYear() + "-" + result.getEndYear(); | 103 /* The result is ordered by the peridods. For each period there is |
92 SedimentLoadLSData load = result.getLoad(); | 104 * then a map of km - fractions pairs which are the actual result. */ |
93 // Put load.getName()+load.getDescription()}); somewhere? | 105 |
94 for (double km: new TreeSet<Double>(load.getKms())) { | 106 TreeMap <String, TreeMap <Double, Double[]>> result_map = |
95 SedimentLoadFraction fraction = load.getFraction(km); | 107 new TreeMap<String, TreeMap<Double, Double[]>>(); |
96 writeRecord(writer, km, years, fraction); | 108 int i = 0; |
109 for (String name: FRACTION_ORDER) { | |
110 for (Fraction fract: result.getFractionsByName(name)) { | |
111 String period = fract.getPeriod(); | |
112 TreeMap<Double, Double[]> cur_map; | |
113 if (result_map.containsKey(period)) { | |
114 cur_map = result_map.get(period); | |
115 } else { | |
116 cur_map = new TreeMap<Double, Double[]>(); | |
117 result_map.put(period, cur_map); | |
118 } | |
119 double[][] values = fract.getData(); | |
120 for (int j = 0; j < values[0].length; j++) { | |
121 Double km = values[0][j]; | |
122 Double val = values[1][j]; | |
123 Double[] old = cur_map.get(km); | |
124 if (old == null) { | |
125 old = new Double[FRACTION_ORDER.length]; | |
126 for (int k = 0; k < old.length; k++) { | |
127 old [k] = Double.NaN; | |
128 } | |
129 } | |
130 old [i] = val; | |
131 cur_map.put(km, old); | |
132 } | |
133 } | |
134 } | |
135 for (String period: result_map.keySet()) { | |
136 TreeMap<Double, Double[]> cur_map = result_map.get(period); | |
137 for (Double km: cur_map.keySet()) { | |
138 writeRecord(writer, km, period, cur_map.get(km)); | |
97 } | 139 } |
98 } | 140 } |
99 } | 141 } |
100 | 142 |
101 | 143 |
118 /** Write a line. */ | 160 /** Write a line. */ |
119 private void writeRecord( | 161 private void writeRecord( |
120 CSVWriter writer, | 162 CSVWriter writer, |
121 double km, | 163 double km, |
122 String years, | 164 String years, |
123 SedimentLoadFraction fraction | 165 Double[] fractions |
124 ) { | 166 ) { |
125 // year, total, susp sed, susp sandbed suspsand, sand, finemiddle, coarse | 167 // year, total, susp sed, susp sandbed suspsand, sand, finemiddle, coarse |
126 NumberFormat kmf = Formatter.getCalculationKm(context.getMeta()); | 168 NumberFormat kmf = Formatter.getCalculationKm(context.getMeta()); |
127 NumberFormat valf = Formatter.getFormatter(context.getMeta(), 0, 2); | 169 NumberFormat valf = Formatter.getFormatter(context.getMeta(), 0, 2); |
128 writer.writeNext(new String[] { | 170 writer.writeNext(new String[] { |
129 kmf.format(km), | 171 kmf.format(km), |
130 years, | 172 years, |
131 numberToString(valf, fraction.getSuspSediment()), | 173 numberToString(valf, fractions[0]), |
132 numberToString(valf, fraction.getSuspSand()), | 174 numberToString(valf, fractions[1]), |
133 numberToString(valf, fraction.getSuspSandBed()), | 175 numberToString(valf, fractions[2]), |
134 numberToString(valf, fraction.getSand()), | 176 numberToString(valf, fractions[3]), |
135 numberToString(valf, fraction.getFineMiddle()), | 177 numberToString(valf, fractions[4]), |
136 numberToString(valf, fraction.getCoarse()), | 178 numberToString(valf, fractions[5]), |
137 nonZeroToString(valf, fraction.getTotal()) | 179 nonZeroToString(valf, fractions[6]) |
138 }); | 180 }); |
139 } | 181 } |
140 | 182 |
141 /** Writes i18ned header for csv file/stream. */ | 183 /** Writes i18ned header for csv file/stream. */ |
142 protected void writeCSVHeader(CSVWriter writer) { | 184 protected void writeCSVHeader(CSVWriter writer) { |
143 logger.debug("writeCSVHeader()"); | 185 logger.debug("writeCSVHeader()"); |
144 | 186 |
145 List<String> header = new LinkedList<String>(); | 187 List<String> header = new LinkedList<String>(); |
146 if (results != null) { | 188 SedimentLoadAccess access = |
147 SedimentLoadAccess access = | 189 new SedimentLoadAccess((D4EArtifact) master); |
148 new SedimentLoadAccess((D4EArtifact) master); | 190 |
149 | 191 String unit = " [" + msg("state.minfo." + |
150 String unit = " [" + msg("state.minfo." + | 192 access.getUnit(), "translation missing") + "]"; |
151 access.getUnit(), "translation missing") + "]"; | 193 |
152 | 194 header.add(msg(CSV_KM, "km")); |
153 header.add(msg(CSV_KM, "km")); | 195 header.add(msg(CSV_YEAR, "Jahr")); |
154 header.add(msg(CSV_YEAR, "Jahr")); | 196 header.add(msg(CSV_SUSP_SEDIMENT, "Schwebst.") + unit); |
155 header.add(msg(CSV_SUSP_SEDIMENT, "Schwebst.") + unit); | 197 header.add(msg(CSV_SUSP_SAND, "Susp.Sand") + unit); |
156 header.add(msg(CSV_SUSP_SAND, "Susp.Sand") + unit); | 198 header.add(msg(CSV_SUSP_SAND_BB, "Susp.Sand(BB)") + unit); |
157 header.add(msg(CSV_SUSP_SAND_BB, "Susp.Sand(BB)") + unit); | 199 header.add(msg(CSV_SAND, "Sand") + unit); |
158 header.add(msg(CSV_SAND, "Sand") + unit); | 200 header.add(msg(CSV_FINEMIDDLE, "Kies(f+m)") + unit); |
159 header.add(msg(CSV_FINEMIDDLE, "Kies(f+m)") + unit); | 201 header.add(msg(CSV_COARSE, "Kies(g)") + unit); |
160 header.add(msg(CSV_COARSE, "Kies(g)") + unit); | 202 header.add(msg(CSV_TOTAL, "Gesamt") + unit); |
161 header.add(msg(CSV_TOTAL, "Gesamt") + unit); | |
162 } | |
163 writer.writeNext(header.toArray(new String[header.size()])); | 203 writer.writeNext(header.toArray(new String[header.size()])); |
164 } | 204 } |
165 | 205 |
166 /** Store data internally, accepting only SedimentLoadResults[] in | 206 /** Store data internally, accepting only SedimentLoadResults[] in |
167 * calculationresults data. */ | 207 * calculationresults data. */ |
169 protected void addData(Object data) { | 209 protected void addData(Object data) { |
170 if (!(data instanceof CalculationResult)) { | 210 if (!(data instanceof CalculationResult)) { |
171 logger.warn("Invalid data type."); | 211 logger.warn("Invalid data type."); |
172 return; | 212 return; |
173 } | 213 } |
174 Object[] d = (Object[])((CalculationResult)data).getData(); | 214 Object d = ((CalculationResult)data).getData(); |
175 | 215 |
176 if (!(d instanceof SedimentLoadResult[])) { | 216 if (!(d instanceof SedimentLoadDataResult)) { |
177 logger.warn("Invalid result object."); | 217 logger.warn("Invalid result object."); |
178 return; | 218 return; |
179 } | 219 } |
180 logger.debug("addData: Data added."); | 220 logger.debug("addData: Data added."); |
181 results = (SedimentLoadResult[])d; | 221 result = (SedimentLoadDataResult)d; |
182 } | 222 } |
183 | 223 |
184 /** Write PDF to outputstream (not implemented yet). */ | 224 /** Write PDF to outputstream (not implemented yet). */ |
185 @Override | 225 @Override |
186 protected void writePDF(OutputStream out) { | 226 protected void writePDF(OutputStream out) { |