Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/exports/MiddleBedHeightExporter.java @ 7693:c130196389df
(issue1596) Sort Export of Middle bedHeight
This simplyfies internal Data storage by removing
the ArrayList wrapper. Usually this is a list of size 1.
The data list of which each member is a distinct sounding
is then sorted by description or first indexed KM.
Afterwards the km index is mapped in a sorted map to
export it in order of km and not in oder of index
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Mon, 16 Dec 2013 12:26:52 +0100 |
parents | 410fbd6a6d01 |
children | 62befca02480 |
comparison
equal
deleted
inserted
replaced
7692:4508501cdde7 | 7693:c130196389df |
---|---|
10 | 10 |
11 import java.io.OutputStream; | 11 import java.io.OutputStream; |
12 import java.text.NumberFormat; | 12 import java.text.NumberFormat; |
13 import java.util.ArrayList; | 13 import java.util.ArrayList; |
14 import java.util.List; | 14 import java.util.List; |
15 import java.util.Collections; | |
16 import java.util.SortedMap; | |
17 import java.util.TreeMap; | |
15 | 18 |
16 import org.apache.log4j.Logger; | 19 import org.apache.log4j.Logger; |
17 | 20 |
18 import au.com.bytecode.opencsv.CSVWriter; | 21 import au.com.bytecode.opencsv.CSVWriter; |
19 | 22 |
59 | 62 |
60 public static final String CSV_LOCATIONS = | 63 public static final String CSV_LOCATIONS = |
61 "export.bedheight_middle.csv.header.locations"; | 64 "export.bedheight_middle.csv.header.locations"; |
62 | 65 |
63 | 66 |
64 protected List<MiddleBedHeightData[]> data; | 67 protected List<MiddleBedHeightData> data; |
65 | 68 |
66 public MiddleBedHeightExporter() { | 69 public MiddleBedHeightExporter() { |
67 data = new ArrayList<MiddleBedHeightData[]>(); | 70 data = new ArrayList<MiddleBedHeightData>(); |
68 } | 71 } |
69 | 72 |
70 @Override | 73 @Override |
71 protected void addData(Object d) { | 74 protected void addData(Object d) { |
72 if (d instanceof CalculationResult) { | 75 if (d instanceof CalculationResult) { |
73 d = ((CalculationResult) d).getData(); | 76 d = ((CalculationResult) d).getData(); |
74 | 77 |
75 if (d instanceof MiddleBedHeightData[]) { | 78 if (d instanceof MiddleBedHeightData[]) { |
76 logger.debug("Add new data of type MiddleBedHeightData"); | 79 logger.debug("Add new data of type MiddleBedHeightData"); |
77 data.add((MiddleBedHeightData[]) d); | 80 for (MiddleBedHeightData mD :(MiddleBedHeightData[]) d) { |
81 data.add(mD); | |
82 } | |
78 } | 83 } |
79 } | 84 } |
80 } | 85 } |
81 | 86 |
82 | 87 |
85 logger.info("MiddleBedHeightExporter.writeCSVData"); | 90 logger.info("MiddleBedHeightExporter.writeCSVData"); |
86 logger.debug("CSV gets " + data.size() + " MiddleBedHeightData objects."); | 91 logger.debug("CSV gets " + data.size() + " MiddleBedHeightData objects."); |
87 | 92 |
88 writeCSVHeader(writer); | 93 writeCSVHeader(writer); |
89 | 94 |
90 for (MiddleBedHeightData[] d: data) { | 95 Collections.sort(data); |
96 | |
97 for (MiddleBedHeightData d: data) { | |
91 data2CSV(writer, d); | 98 data2CSV(writer, d); |
92 } | 99 } |
93 } | 100 } |
94 | 101 |
95 | 102 |
107 msg(CSV_LOCATIONS, CSV_LOCATIONS) | 114 msg(CSV_LOCATIONS, CSV_LOCATIONS) |
108 }); | 115 }); |
109 } | 116 } |
110 | 117 |
111 | 118 |
112 protected void data2CSV(CSVWriter writer, MiddleBedHeightData[] mData) { | 119 protected void data2CSV(CSVWriter writer, MiddleBedHeightData data) { |
113 logger.debug("Add next MiddleBedHeightData to CSV"); | 120 logger.debug("Add next MiddleBedHeightData to CSV"); |
114 | 121 |
115 D4EArtifact flys = (D4EArtifact) master; | 122 D4EArtifact flys = (D4EArtifact) master; |
116 | 123 |
117 NumberFormat kmF = Formatter.getMiddleBedHeightKM(context); | 124 NumberFormat kmF = Formatter.getMiddleBedHeightKM(context); |
121 NumberFormat soundF = Formatter.getMiddleBedHeightSounding(context); | 128 NumberFormat soundF = Formatter.getMiddleBedHeightSounding(context); |
122 NumberFormat widthF = Formatter.getMiddleBedHeightWidth(context); | 129 NumberFormat widthF = Formatter.getMiddleBedHeightWidth(context); |
123 | 130 |
124 heightF.setMaximumFractionDigits(1); | 131 heightF.setMaximumFractionDigits(1); |
125 soundF.setMaximumFractionDigits(1); | 132 soundF.setMaximumFractionDigits(1); |
126 for (MiddleBedHeightData data: mData) { | |
127 for (int i = 0, n = data.size(); i < n; i++) { | |
128 int start = data.getStartYear(); | |
129 int end = data.getEndYear(); | |
130 | 133 |
131 if (start == end) { | 134 SortedMap <Double, Integer> kmIndexMap = new TreeMap<Double, Integer>(); |
132 String uncert = !Double.isNaN(data.getUncertainty(i)) ? | |
133 uncertF.format(data.getUncertainty(i)) : ""; | |
134 String gap = !Double.isNaN(data.getDataGap(i)) ? | |
135 gapF.format(data.getDataGap(i)) + "%" : ""; | |
136 String sound = !Double.isNaN(data.getSoundingWidth(i)) ? | |
137 soundF.format(data.getSoundingWidth(i)) : ""; | |
138 writer.writeNext(new String[] { | |
139 kmF.format(data.getKM(i)), | |
140 data.getDescription(), | |
141 heightF.format(data.getMiddleHeight(i)), | |
142 uncert, | |
143 gap, | |
144 sound, | |
145 widthF.format(data.getWidth(i)), | |
146 RiverUtils.getLocationDescription(flys, data.getKM(i)), | |
147 }); | |
148 } | |
149 else { | |
150 writer.writeNext(new String[] { | |
151 kmF.format(data.getKM(i)), | |
152 data.getDescription(), | |
153 heightF.format(data.getMiddleHeight(i)), | |
154 "", | |
155 "", | |
156 "", | |
157 "", | |
158 RiverUtils.getLocationDescription(flys, data.getKM(i)), | |
159 }); | |
160 } | |
161 | 135 |
136 for (int i = 0, n = data.size(); i < n; i++) { | |
137 kmIndexMap.put(data.getKM(i), i); | |
138 } | |
139 | |
140 for (int i: kmIndexMap.values()) { | |
141 int start = data.getStartYear(); | |
142 int end = data.getEndYear(); | |
143 | |
144 if (start == end) { | |
145 String uncert = !Double.isNaN(data.getUncertainty(i)) ? | |
146 uncertF.format(data.getUncertainty(i)) : ""; | |
147 String gap = !Double.isNaN(data.getDataGap(i)) ? | |
148 gapF.format(data.getDataGap(i)) + "%" : ""; | |
149 String sound = !Double.isNaN(data.getSoundingWidth(i)) ? | |
150 soundF.format(data.getSoundingWidth(i)) : ""; | |
151 writer.writeNext(new String[] { | |
152 kmF.format(data.getKM(i)), | |
153 data.getDescription(), | |
154 heightF.format(data.getMiddleHeight(i)), | |
155 uncert, | |
156 gap, | |
157 sound, | |
158 widthF.format(data.getWidth(i)), | |
159 RiverUtils.getLocationDescription(flys, data.getKM(i)), | |
160 }); | |
162 } | 161 } |
162 else { | |
163 writer.writeNext(new String[] { | |
164 kmF.format(data.getKM(i)), | |
165 data.getDescription(), | |
166 heightF.format(data.getMiddleHeight(i)), | |
167 "", | |
168 "", | |
169 "", | |
170 "", | |
171 RiverUtils.getLocationDescription(flys, data.getKM(i)), | |
172 }); | |
173 } | |
174 | |
163 } | 175 } |
164 } | 176 } |
165 | 177 |
166 | 178 |
167 @Override | 179 @Override |