Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/org/dive4elements/river/artifacts/model/sq/SQRelationCalculation.java @ 5831:bd047b71ab37
Repaired internal references
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 12:06:39 +0200 |
parents | flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQRelationCalculation.java@aaf810d4ec82 |
children |
comparison
equal
deleted
inserted
replaced
5830:160f53ee0870 | 5831:bd047b71ab37 |
---|---|
1 package org.dive4elements.river.artifacts.model.sq; | |
2 | |
3 import org.dive4elements.river.artifacts.access.SQRelationAccess; | |
4 | |
5 import org.dive4elements.river.artifacts.math.fitting.Function; | |
6 import org.dive4elements.river.artifacts.math.fitting.FunctionFactory; | |
7 | |
8 import org.dive4elements.river.artifacts.model.Calculation; | |
9 import org.dive4elements.river.artifacts.model.CalculationResult; | |
10 import org.dive4elements.river.artifacts.model.DateRange; | |
11 import org.dive4elements.river.artifacts.model.Parameters; | |
12 | |
13 import org.dive4elements.river.backend.SedDBSessionHolder; | |
14 | |
15 import java.util.ArrayList; | |
16 import java.util.List; | |
17 | |
18 import org.apache.log4j.Logger; | |
19 | |
20 public class SQRelationCalculation extends Calculation { | |
21 | |
22 private static final Logger log = | |
23 Logger.getLogger(SQRelationCalculation.class); | |
24 | |
25 public static final String SQ_FUNCTION_NAME = "sq-pow"; | |
26 | |
27 protected String river; | |
28 protected double location; | |
29 protected DateRange period; | |
30 protected double outliers; | |
31 private String method; | |
32 | |
33 public SQRelationCalculation() { | |
34 } | |
35 | |
36 public SQRelationCalculation(SQRelationAccess access) { | |
37 | |
38 String river = access.getRiver(); | |
39 Double location = access.getLocation(); | |
40 DateRange period = access.getPeriod(); | |
41 Double outliers = access.getOutliers(); | |
42 String method = access.getOutlierMethod(); | |
43 | |
44 //river = "Rhein"; | |
45 | |
46 if (river == null) { | |
47 // TODO: i18n | |
48 addProblem("sq.missing.river"); | |
49 } | |
50 | |
51 if (location == null) { | |
52 // TODO: i18n | |
53 addProblem("sq.missing.location"); | |
54 } | |
55 | |
56 if (period == null) { | |
57 // TODO: i18n | |
58 addProblem("sq.missing.periods"); | |
59 } | |
60 | |
61 if (outliers == null) { | |
62 // TODO: i18n | |
63 addProblem("sq.missing.outliers"); | |
64 } | |
65 | |
66 if (method == null) { | |
67 //TODO: i18n | |
68 addProblem("sq.missing.method"); | |
69 } | |
70 | |
71 if (!hasProblems()) { | |
72 this.river = river; | |
73 this.location = location; | |
74 this.period = period; | |
75 this.outliers = outliers; | |
76 this.method = method; | |
77 } | |
78 } | |
79 | |
80 | |
81 public CalculationResult calculate() { | |
82 log.debug("SQRelationCalculation.calculate"); | |
83 | |
84 if (hasProblems()) { | |
85 return new CalculationResult(this); | |
86 } | |
87 | |
88 SedDBSessionHolder.acquire(); | |
89 try { | |
90 return internalCalculate(); | |
91 } | |
92 finally { | |
93 SedDBSessionHolder.release(); | |
94 } | |
95 } | |
96 | |
97 protected CalculationResult internalCalculate() { | |
98 | |
99 Function function = FunctionFactory | |
100 .getInstance() | |
101 .getFunction(SQ_FUNCTION_NAME); | |
102 | |
103 if (function == null) { | |
104 log.error("No '" + SQ_FUNCTION_NAME + "' function found."); | |
105 // TODO: i18n | |
106 addProblem("sq.missing.sq.function"); | |
107 } | |
108 | |
109 Measurements measurements = | |
110 MeasurementFactory.getMeasurements(river, location, period); | |
111 | |
112 SQFractionResult [] fractionResults = | |
113 new SQFractionResult[SQResult.NUMBER_FRACTIONS]; | |
114 | |
115 for (int i = 0; i < fractionResults.length; ++i) { | |
116 List<SQ> sqs = measurements.getSQs(i); | |
117 | |
118 SQFractionResult fractionResult; | |
119 | |
120 List<SQFractionResult.Iteration> iterations = | |
121 doFitting(function, sqs); | |
122 | |
123 if (iterations == null) { | |
124 // TODO: i18n | |
125 addProblem("sq.fitting.failed." + i); | |
126 fractionResult = new SQFractionResult(); | |
127 } | |
128 else { | |
129 fractionResult = new SQFractionResult( | |
130 sqs.toArray(new SQ[sqs.size()]), | |
131 iterations); | |
132 } | |
133 | |
134 fractionResults[i] = fractionResult; | |
135 } | |
136 | |
137 return new CalculationResult( | |
138 new SQResult[] { new SQResult(location, fractionResults) }, | |
139 this); | |
140 } | |
141 | |
142 protected List<SQFractionResult.Iteration> doFitting( | |
143 final Function function, | |
144 List<SQ> sqs | |
145 ) { | |
146 final List<SQFractionResult.Iteration> iterations = | |
147 new ArrayList<SQFractionResult.Iteration>(); | |
148 | |
149 boolean success = new Fitting(function, outliers).fit( | |
150 sqs, | |
151 method, | |
152 new Fitting.Callback() { | |
153 @Override | |
154 public void afterIteration( | |
155 double [] coeffs, | |
156 SQ [] measurements, | |
157 SQ [] outliers, | |
158 double standardDeviation, | |
159 double chiSqr | |
160 ) { | |
161 Parameters parameters = createParameters( | |
162 function.getParameterNames(), | |
163 coeffs, | |
164 standardDeviation, | |
165 chiSqr); | |
166 iterations.add(new SQFractionResult.Iteration( | |
167 parameters, | |
168 measurements, | |
169 outliers)); | |
170 } | |
171 }); | |
172 | |
173 return success ? iterations : null; | |
174 } | |
175 | |
176 public static final Parameters createParameters( | |
177 String [] names, | |
178 double [] values, | |
179 double standardDeviation, | |
180 double chiSqr | |
181 ) { | |
182 String [] columns = new String[names.length + 2]; | |
183 columns[0] = "chi_sqr"; | |
184 columns[1] = "std_dev"; | |
185 System.arraycopy(names, 0, columns, 2, names.length); | |
186 Parameters parameters = new Parameters(columns); | |
187 int row = parameters.newRow(); | |
188 parameters.set(row, names, values); | |
189 parameters.set(row, "chi_sqr", chiSqr); | |
190 parameters.set(row, "std_dev", standardDeviation); | |
191 return parameters; | |
192 } | |
193 } | |
194 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |