comparison artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/BedQualityState.java @ 8587:07c9ac22f611

(issue1755) Generalise BedQuality result handling The bedquality calculation now produces a result for each time period which has BedQualityResultValues for each specific result type. Formally this was split up in density, porosity and diameter classes with some bedload diameter classes mixed in for extra fun. The intent of this commit is to allow more shared code and generic access patterns to the BedQuality results.
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 18 Mar 2015 18:42:08 +0100
parents e9343e4c9ae0
children 36faef4f8acb
comparison
equal deleted inserted replaced
8586:19fde13e2db4 8587:07c9ac22f611
23 import org.dive4elements.river.artifacts.access.BedQualityAccess; 23 import org.dive4elements.river.artifacts.access.BedQualityAccess;
24 import org.dive4elements.river.artifacts.model.CalculationResult; 24 import org.dive4elements.river.artifacts.model.CalculationResult;
25 import org.dive4elements.river.artifacts.model.DataFacet; 25 import org.dive4elements.river.artifacts.model.DataFacet;
26 import org.dive4elements.river.artifacts.model.DateRange; 26 import org.dive4elements.river.artifacts.model.DateRange;
27 import org.dive4elements.river.artifacts.model.FacetTypes; 27 import org.dive4elements.river.artifacts.model.FacetTypes;
28 import org.dive4elements.river.artifacts.model.minfo.BedDensityTopFacet; 28 import org.dive4elements.river.artifacts.model.minfo.BedQualityDataFacet;
29 import org.dive4elements.river.artifacts.model.minfo.BedDensitySubFacet;
30 import org.dive4elements.river.artifacts.model.minfo.BedDiameterTopFacet;
31 import org.dive4elements.river.artifacts.model.minfo.BedDiameterDataFacet; 29 import org.dive4elements.river.artifacts.model.minfo.BedDiameterDataFacet;
32 import org.dive4elements.river.artifacts.model.minfo.BedDiameterResult; 30 import org.dive4elements.river.artifacts.model.minfo.BedloadDiameterDataFacet;
33 import org.dive4elements.river.artifacts.model.minfo.BedDiameterSubFacet;
34 import org.dive4elements.river.artifacts.model.minfo.BedParametersResult;
35 import org.dive4elements.river.artifacts.model.minfo.BedPorositySubFacet;
36 import org.dive4elements.river.artifacts.model.minfo.BedPorosityTopFacet;
37 import org.dive4elements.river.artifacts.model.minfo.BedQualityCalculation; 31 import org.dive4elements.river.artifacts.model.minfo.BedQualityCalculation;
38 import org.dive4elements.river.artifacts.model.minfo.BedQualityDiameterResult;
39 import org.dive4elements.river.artifacts.model.minfo.BedQualityResult; 32 import org.dive4elements.river.artifacts.model.minfo.BedQualityResult;
40 import org.dive4elements.river.artifacts.model.minfo.BedloadDiameterDataFacet; 33 import org.dive4elements.river.artifacts.model.minfo.BedQualityResultValue;
41 import org.dive4elements.river.artifacts.model.minfo.BedloadDiameterFacet;
42 import org.dive4elements.river.artifacts.model.minfo.BedloadDiameterResult;
43 import org.dive4elements.river.artifacts.resources.Resources; 34 import org.dive4elements.river.artifacts.resources.Resources;
44 import org.dive4elements.river.artifacts.states.DefaultState; 35 import org.dive4elements.river.artifacts.states.DefaultState;
45 36
37 /* TODO: Change data facets to live in the generalized data scheme and
38 * obsolute the obfuscated index magic. */
46 39
47 public class BedQualityState extends DefaultState implements FacetTypes { 40 public class BedQualityState extends DefaultState implements FacetTypes {
48 41
49 private static final long serialVersionUID = 1L; 42 private static final long serialVersionUID = 1L;
50 43
51 private static final Logger log = Logger 44 private static final Logger log = Logger
52 .getLogger(BedQualityState.class); 45 .getLogger(BedQualityState.class);
53 46
47 /* The suffix to append to interpol facets. */
48 public static final String I18N_INTERPOL_SUFFIX = "facet.bedquality.interpol.suffix";
49
50 /* I18n is in the pattern base.<name>.<type> with the optional suffix .data */
51 public static final String I18N_FACET_BED_BASE = "facet.bedquality.bed";
52
53 /* Data Layers */
54 public static final String I18N_FACET_BEDLOAD_DIAMETER_DATA = "facet.bedquality.bed.diameter.bedload.data";
55 public static final String I18N_FACET_BED_DIAMETER_DATA_TOPLAYER = "facet.bedquality.bed.diameter.toplayer.data";
56 public static final String I18N_FACET_BED_DIAMETER_DATA_SUBLAYER = "facet.bedquality.bed.diameter.sublayer.data";
54 public static final String I18N_TOPLAYER = "bedquality.toplayer"; 57 public static final String I18N_TOPLAYER = "bedquality.toplayer";
55 public static final String I18N_SUBLAYER = "bedquality.sublayer"; 58 public static final String I18N_SUBLAYER = "bedquality.sublayer";
56
57 /* Calculated layers */
58 public static final String I18N_FACET_BED_POROSITY_TOPLAYER = "facet.bedquality.bed.porosity.toplayer";
59 public static final String I18N_FACET_BED_POROSITY_SUBLAYER = "facet.bedquality.bed.porosity.sublayer";
60 public static final String I18N_FACET_BED_DENSITY_TOPLAYER = "facet.bedquality.bed.density.toplayer";
61 public static final String I18N_FACET_BED_DENSITY_SUBLAYER = "facet.bedquality.bed.density.sublayer";
62 public static final String I18N_FACET_BED_DIAMETER_TOPLAYER = "facet.bedquality.bed.diameter.toplayer";
63 public static final String I18N_FACET_BED_DIAMETER_SUBLAYER = "facet.bedquality.bed.diameter.sublayer";
64 public static final String I18N_FACET_BEDLOAD_DIAMETER = "facet.bedquality.bedload.diameter";
65
66 /* Data Layers */
67 public static final String I18N_FACET_BEDLOAD_DIAMETER_DATA = "facet.bedquality.bedload.diameter.data";
68 public static final String I18N_FACET_BED_DIAMETER_DATA_TOPLAYER = "facet.bedquality.bed.diameter.data.toplayer";
69 public static final String I18N_FACET_BED_DIAMETER_DATA_SUBLAYER = "facet.bedquality.bed.diameter.data.sublayer";
70 59
71 static { 60 static {
72 // Active/deactivate facets. 61 // Active/deactivate facets.
73 FacetActivity.Registry.getInstance().register( 62 FacetActivity.Registry.getInstance().register(
74 "minfo", 63 "minfo",
121 log.warn("Calculation computed no results!"); 110 log.warn("Calculation computed no results!");
122 return res; 111 return res;
123 } 112 }
124 113
125 generateFacets(context, newFacets, results, getID(), hash); 114 generateFacets(context, newFacets, results, getID(), hash);
115 generateDataFacets(context, newFacets, access, getID(), hash);
126 log.debug("Created " + newFacets.size() + " new Facets."); 116 log.debug("Created " + newFacets.size() + " new Facets.");
127 generateDataFacets(context, newFacets, access, getID(), hash);
128 facets.addAll(newFacets); 117 facets.addAll(newFacets);
129 118
130 return res; 119 return res;
120 }
121
122 private int generateIndex(String diameter) {
123 int d = 0;
124 if(diameter.equals("d10")) {
125 d = 1;
126 }
127 else if (diameter.equals("d16")) {
128 d = 2;
129 }
130 else if (diameter.equals("d20")) {
131 d = 3;
132 }
133 else if (diameter.equals("d25")) {
134 d = 4;
135 }
136 else if (diameter.equals("d30")) {
137 d = 5;
138 }
139 else if (diameter.equals("d40")) {
140 d = 6;
141 }
142 else if (diameter.equals("d50")) {
143 d = 7;
144 }
145 else if (diameter.equals("d60")) {
146 d = 8;
147 }
148 else if (diameter.equals("d70")) {
149 d = 9;
150 }
151 else if (diameter.equals("d75")) {
152 d = 10;
153 }
154 else if (diameter.equals("d80")) {
155 d = 11;
156 }
157 else if (diameter.equals("d84")) {
158 d = 12;
159 }
160 else if (diameter.equals("d90")) {
161 d = 13;
162 }
163 else if (diameter.equals("dmin")) {
164 d = 14;
165 }
166 else if (diameter.equals("dmax")) {
167 d = 15;
168 }
169 else if (diameter.equals("dm")) {
170 d = 16;
171 }
172 int ndx = d << 1;
173 return ndx;
131 } 174 }
132 175
133 private void generateDataFacets( 176 private void generateDataFacets(
134 CallContext context, 177 CallContext context,
135 List<Facet> newFacets, 178 List<Facet> newFacets,
141 List<DateRange> ranges = access.getDateRanges(); 184 List<DateRange> ranges = access.getDateRanges();
142 for (int i = 0, R = ranges.size(); i < R; i++) { 185 for (int i = 0, R = ranges.size(); i < R; i++) {
143 DateRange range = ranges.get(i); 186 DateRange range = ranges.get(i);
144 for (String diameter: diameters) { 187 for (String diameter: diameters) {
145 int ndxTop = generateIndex(diameter); 188 int ndxTop = generateIndex(diameter);
146 int ndxSub = ndxTop; // TODO: Is this correct? 189 int ndxSub = ndxTop;
147 ndxTop += 1; 190 ndxTop += 1;
148 ndxTop = ndxTop << 3; 191 ndxTop = ndxTop << 3;
149 ndxSub = ndxSub << 3; 192 ndxSub = ndxSub << 3;
150 ndxTop += i; 193 ndxTop += i;
151 ndxSub += i; 194 ndxSub += i;
200 hash)); 243 hash));
201 } 244 }
202 } 245 }
203 } 246 }
204 247
205 private int generateIndex(String diameter) { 248
206 int d = 0; 249 protected String getFacetName(BedQualityResultValue value) {
207 if(diameter.equals("d10")) { 250 /* basename + name or "diameter" + .type */
208 d = 1; 251 return BED_QUALITY_DATA_FACET + "." +
209 } 252 (value.isDiameterResult() ? "diameter" : value.getName()) + "." +
210 else if (diameter.equals("d16")) { 253 value.getType();
211 d = 2;
212 }
213 else if (diameter.equals("d20")) {
214 d = 3;
215 }
216 else if (diameter.equals("d25")) {
217 d = 4;
218 }
219 else if (diameter.equals("d30")) {
220 d = 5;
221 }
222 else if (diameter.equals("d40")) {
223 d = 6;
224 }
225 else if (diameter.equals("d50")) {
226 d = 7;
227 }
228 else if (diameter.equals("d60")) {
229 d = 8;
230 }
231 else if (diameter.equals("d70")) {
232 d = 9;
233 }
234 else if (diameter.equals("d75")) {
235 d = 10;
236 }
237 else if (diameter.equals("d80")) {
238 d = 11;
239 }
240 else if (diameter.equals("d84")) {
241 d = 12;
242 }
243 else if (diameter.equals("d90")) {
244 d = 13;
245 }
246 else if (diameter.equals("dmin")) {
247 d = 14;
248 }
249 else if (diameter.equals("dmax")) {
250 d = 15;
251 }
252 else if (diameter.equals("dm")) {
253 d = 16;
254 }
255 int ndx = d << 1;
256 return ndx;
257 } 254 }
258 255
259 protected void generateFacets(CallContext context, List<Facet> newFacets, 256 protected void generateFacets(CallContext context, List<Facet> newFacets,
260 BedQualityResult[] results, String stateId, String hash) { 257 BedQualityResult[] results, String stateId, String hash) {
261 log.debug("BedQualityState.generateFacets"); 258 log.debug("BedQualityState.generateFacets");
264 261
265 newFacets.add(new DataFacet(CSV, "CSV data", ComputeType.ADVANCE, hash, id)); 262 newFacets.add(new DataFacet(CSV, "CSV data", ComputeType.ADVANCE, hash, id));
266 for (int idx = 0; idx < results.length; idx++) { 263 for (int idx = 0; idx < results.length; idx++) {
267 BedQualityResult result = results[idx]; 264 BedQualityResult result = results[idx];
268 DateRange range = result.getDateRange(); 265 DateRange range = result.getDateRange();
269 BedDiameterResult[] bedDiameter = result.getBedResults(); 266 int i = 0;
270 for (int j = 0; j < bedDiameter.length; j++) { 267 for (BedQualityResultValue value: result.getValues()) {
271 newFacets.add(new BedDiameterTopFacet((idx << 8) + j, 268 newFacets.add(new BedQualityDataFacet((idx << 8) + i++,
272 BED_QUALITY_BED_DIAMETER_TOPLAYER, 269 getFacetName(value),
273 getLayerDescription( 270 getFacetDescription(meta, range, value),
274 meta, 271 ComputeType.ADVANCE,
275 bedDiameter[j], 272 stateId, hash, value.getName(), value.getType()));
276 range,
277 I18N_FACET_BED_DIAMETER_TOPLAYER,
278 true),
279 ComputeType.ADVANCE, stateId, hash));
280
281 newFacets.add(new BedDiameterSubFacet((idx << 8) +j,
282 BED_QUALITY_BED_DIAMETER_SUBLAYER,
283 getLayerDescription(
284 meta,
285 bedDiameter[j],
286 range,
287 I18N_FACET_BED_DIAMETER_SUBLAYER,
288 false),
289 ComputeType.ADVANCE, stateId, hash));
290 } 273 }
291 BedloadDiameterResult[] bedloadDiameter = result.getBedloadResults(); 274 }
292 for (int j = 0; j < bedloadDiameter.length; j++) { 275 }
293 newFacets.add(new BedloadDiameterFacet( 276
294 (idx << 8) + j, 277 protected String getFacetDescription(CallMeta meta,
295 BED_QUALITY_BEDLOAD_DIAMETER, 278 DateRange range,
296 createDiameterDescription( 279 BedQualityResultValue value) {
297 meta, bedloadDiameter[j]),
298 ComputeType.ADVANCE,
299 stateId,
300 hash));
301
302 }
303 if (bedDiameter.length > 0) {
304 BedParametersResult[] bedParameters = result.getParameters();
305 for (int j = 0; j < bedParameters.length; j++) {
306 newFacets.add(new BedPorosityTopFacet((idx << 8) + j,
307 BED_QUALITY_POROSITY_TOPLAYER,
308 getLayerDescription(
309 meta,
310 bedParameters[j],
311 range,
312 I18N_FACET_BED_POROSITY_TOPLAYER,
313 true),
314 ComputeType.ADVANCE, stateId, hash));
315
316 newFacets.add(new BedPorositySubFacet((idx << 8) + j,
317 BED_QUALITY_POROSITY_SUBLAYER,
318 getLayerDescription(
319 meta,
320 bedParameters[j],
321 range,
322 I18N_FACET_BED_POROSITY_SUBLAYER,
323 false),
324 ComputeType.ADVANCE, stateId, hash));
325
326 newFacets.add(new BedDensityTopFacet((idx << 8) + j,
327 BED_QUALITY_SEDIMENT_DENSITY_TOPLAYER,
328 getLayerDescription(
329 meta,
330 bedParameters[j],
331 range,
332 I18N_FACET_BED_DENSITY_TOPLAYER,
333 true),
334 ComputeType.ADVANCE, stateId, hash));
335
336 newFacets.add(new BedDensitySubFacet((idx << 8) + j,
337 BED_QUALITY_SEDIMENT_DENSITY_SUBLAYER,
338 getLayerDescription(
339 meta,
340 bedParameters[j],
341 range,
342 I18N_FACET_BED_DENSITY_SUBLAYER,
343 false),
344 ComputeType.ADVANCE, stateId, hash));
345 }
346 }
347 }
348 }
349
350 protected String getLayerDescription(CallMeta meta,
351 Object result, DateRange range, String i18n, boolean topLayer) {
352 Date from = range != null ? range.getFrom() : new Date(); 280 Date from = range != null ? range.getFrom() : new Date();
353 Date to = range != null ? range.getTo() : new Date(); 281 Date to = range != null ? range.getTo() : new Date();
354 282
355 String layer; 283 final String layerSuffix =
356 /* Maybe just a check if i18n ends with .sublayer? */ 284 Resources.getMsg(meta, "bedquality." + value.getType(), "");
357 if (topLayer) { 285
358 layer = Resources.getMsg(meta, I18N_TOPLAYER, I18N_TOPLAYER); 286 /* This could probably be unified with the facet name */
287 final String i18n = I18N_FACET_BED_BASE + "." +
288 (value.isDiameterResult() ? "diameter" : value.getName()) + "." +
289 value.getType();
290
291 if (value.isDiameterResult()) {
292 /* Include the diameter in the description */
293 return Resources.getMsg(meta, i18n, i18n, new Object[] {
294 value.getName().toUpperCase(), from, to, layerSuffix });
359 } else { 295 } else {
360 layer = Resources.getMsg(meta, I18N_SUBLAYER, I18N_SUBLAYER); 296 return Resources.getMsg(meta, i18n, i18n, new Object[] { from, to, layerSuffix });
361 } 297 }
362 if (result instanceof BedDiameterResult) {
363 return Resources.getMsg(meta, i18n, i18n, new Object[] { ((BedDiameterResult)result).getType(),
364 from, to, layer });
365 } else {
366 return Resources.getMsg(meta, i18n, i18n, new Object[] { from, to, layer });
367 }
368 }
369
370 protected String createDiameterDescription(CallMeta meta,
371 BedQualityDiameterResult result) {
372 return Resources.getMsg(meta, I18N_FACET_BEDLOAD_DIAMETER,
373 I18N_FACET_BEDLOAD_DIAMETER, new Object[] { result.getType() });
374 } 298 }
375 } 299 }
376 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org