comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/fixings/FixAnalysisCalculation.java @ 3434:1a636be7612b

FixA: Moved more common calculation code into base class. flys-artifacts/trunk@5097 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 22 Jul 2012 09:49:56 +0000
parents 7f7ab030a4e3
children 262e7d7e58fe
comparison
equal deleted inserted replaced
3433:c3fb41e73ffb 3434:1a636be7612b
32 import de.intevation.flys.utils.DoubleUtil; 32 import de.intevation.flys.utils.DoubleUtil;
33 import de.intevation.flys.utils.KMIndex; 33 import de.intevation.flys.utils.KMIndex;
34 34
35 import java.util.ArrayList; 35 import java.util.ArrayList;
36 import java.util.Date; 36 import java.util.Date;
37 import java.util.HashMap;
38 import java.util.List; 37 import java.util.List;
39 import java.util.Map;
40 38
41 import org.apache.commons.math.stat.descriptive.moment.StandardDeviation; 39 import org.apache.commons.math.stat.descriptive.moment.StandardDeviation;
42 40
43 import org.apache.log4j.Logger; 41 import org.apache.log4j.Logger;
44 42
106 104
107 if (eventColumns.size() < 2) { 105 if (eventColumns.size() < 2) {
108 addProblem("fix.too.less.data.columns"); 106 addProblem("fix.too.less.data.columns");
109 return new CalculationResult(this); 107 return new CalculationResult(this);
110 } 108 }
111
112 double [] kms = DoubleUtil.explode(from, to, step / 1000.0);
113 109
114 final double [] qs = new double[eventColumns.size()]; 110 final double [] qs = new double[eventColumns.size()];
115 final double [] ws = new double[qs.length]; 111 final double [] ws = new double[qs.length];
116 final boolean [] interpolated = new boolean[ws.length]; 112 final boolean [] interpolated = new boolean[ws.length];
117 113
145 new Parameters( 141 new Parameters(
146 StringUtils.join(STANDARD_COLUMNS, parameterNames)); 142 StringUtils.join(STANDARD_COLUMNS, parameterNames));
147 143
148 boolean invalid = false; 144 boolean invalid = false;
149 145
146 double [] kms = DoubleUtil.explode(from, to, step / 1000.0);
147
150 if (debug) { 148 if (debug) {
151 log.debug("number of kms: " + kms.length); 149 log.debug("number of kms: " + kms.length);
152 } 150 }
153 151
154 KMIndex<QW []> outliers = new KMIndex<QW []>(); 152 KMIndex<QW []> outliers = new KMIndex<QW []>();
221 analysisPeriods); 219 analysisPeriods);
222 220
223 return new CalculationResult(fr, this); 221 return new CalculationResult(fr, this);
224 } 222 }
225 223
226 protected String toString(String [] parameterNames, double [] values) { 224 @Override
227 StringBuilder sb = new StringBuilder(); 225 protected Filter createFilter() {
228 for (int i = 0; i < parameterNames.length; ++i) { 226 Filter ids = super.createFilter();
229 if (i > 0) sb.append(", ");
230 sb.append(parameterNames[i]).append(": ").append(values[i]);
231 }
232 return sb.toString();
233 }
234
235 protected List<Column> getEventColumns(FixingsOverview overview) {
236
237 FixingsColumnFactory fcf = FixingsColumnFactory.getInstance();
238
239 IdsFilter ids = new IdsFilter(events);
240 DateRangeFilter rdf = new DateRangeFilter( 227 DateRangeFilter rdf = new DateRangeFilter(
241 referencePeriod.getFrom(), 228 referencePeriod.getFrom(),
242 referencePeriod.getTo()); 229 referencePeriod.getTo());
243 Filter filter = new AndFilter().add(rdf).add(ids); 230 return new AndFilter().add(rdf).add(ids);
244 231 }
245 List<Fixing.Column> metas = overview.filter(null, filter);
246
247 List<Column> columns = new ArrayList<Column>(metas.size());
248
249 for (Fixing.Column meta: metas) {
250
251 FixingsColumn data = fcf.getColumnData(meta);
252 if (data == null) {
253 addProblem("fix.cannot.load.data");
254 }
255 else {
256 columns.add(new Column(meta, data));
257 }
258 }
259
260 return columns;
261 }
262
263 232
264 protected KMIndex<AnalysisPeriod []> calculateAnalysisPeriods( 233 protected KMIndex<AnalysisPeriod []> calculateAnalysisPeriods(
265 Function function, 234 Function function,
266 Parameters parameters, 235 Parameters parameters,
267 FixingsOverview overview 236 FixingsOverview overview
423 } 392 }
424 393
425 return results; 394 return results;
426 } 395 }
427 396
428 /** Helper class to bundle the meta information of a column
429 * and the real data.
430 */
431 protected static class Column {
432
433 protected Fixing.Column meta;
434 protected FixingsColumn data;
435
436 public Column() {
437 }
438
439 public Column(Fixing.Column meta, FixingsColumn data) {
440 this.meta = meta;
441 this.data = data;
442 }
443
444 public Date getDate() {
445 return meta.getStartTime();
446 }
447
448 public String getDescription() {
449 return meta.getDescription();
450 }
451
452 public boolean getQW(
453 double km,
454 double [] qs,
455 double [] ws,
456 int index
457 ) {
458 qs[index] = data.getQ(km);
459 return data.getW(km, ws, index);
460 }
461
462 public boolean getQW(double km, double [] wq) {
463 data.getW(km, wq, 0);
464 if (Double.isNaN(wq[0])) return false;
465 wq[1] = data.getQ(km);
466 return !Double.isNaN(wq[1]);
467 }
468 } // class Column
469
470
471 /**
472 * Helper class to find the data belonging to meta info more quickly.
473 */
474 public static class ColumnCache {
475
476 protected Map<Integer, Column> columns;
477
478 public ColumnCache() {
479 columns = new HashMap<Integer, Column>();
480 }
481
482 public Column getColumn(Fixing.Column meta) {
483 Integer key = meta.getId();
484 Column column = columns.get(key);
485 if (column == null) {
486 FixingsColumn data = FixingsColumnFactory
487 .getInstance()
488 .getColumnData(meta);
489 if (data != null) {
490 column = new Column(meta, data);
491 columns.put(key, column);
492 }
493 }
494 return column;
495 }
496 } // class ColumnCache
497
498 /** Fetch meta and data columns for analysis periods. */ 397 /** Fetch meta and data columns for analysis periods. */
499 protected Column [][] getAnalysisColumns(FixingsOverview overview) { 398 protected Column [][] getAnalysisColumns(FixingsOverview overview) {
500 399
501 boolean debug = log.isDebugEnabled(); 400 boolean debug = log.isDebugEnabled();
502 if (debug) { 401 if (debug) {
514 for (int i = 0; i < columns.length; ++i) { 413 for (int i = 0; i < columns.length; ++i) {
515 414
516 // Construct filter for period. 415 // Construct filter for period.
517 DateRange period = analysisPeriods[i]; 416 DateRange period = analysisPeriods[i];
518 417
519 AndFilter filter = new AndFilter();
520
521 DateRangeFilter dateRangeFilter = 418 DateRangeFilter dateRangeFilter =
522 new DateRangeFilter(period.getFrom(), period.getTo()); 419 new DateRangeFilter(period.getFrom(), period.getTo());
523 420
524 filter.add(dateRangeFilter); 421 Filter filter = new AndFilter()
525 filter.add(sectorRangeFilter); 422 .add(dateRangeFilter)
423 .add(sectorRangeFilter);
526 424
527 List<Fixing.Column> metaCols = overview.filter(range, filter); 425 List<Fixing.Column> metaCols = overview.filter(range, filter);
528 426
529 if (debug) { 427 if (debug) {
530 log.debug("number of filtered columns: " + metaCols.size()); 428 log.debug("number of filtered columns: " + metaCols.size());

http://dive4elements.wald.intevation.org