changeset 1938:1d991c91285b

Refactored the code of the 'berechnete Abflusskurve' to be reusable in the 'Bezugslinienverfahren'. flys-artifacts/trunk@3319 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 25 Nov 2011 17:27:40 +0000
parents f07d64d5cbe1
children 2730d17df021
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTable.java
diffstat 2 files changed, 129 insertions(+), 108 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Nov 25 15:50:31 2011 +0000
+++ b/flys-artifacts/ChangeLog	Fri Nov 25 17:27:40 2011 +0000
@@ -1,3 +1,10 @@
+2011-11-25	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/WstValueTable.java:
+	  Refactored the code for the "berechnete Abflusskurve" to enable
+	  the "Bezugslinienverfahren" to use the same code paths. It also
+	  removes a good deal of already existing code duplication.
+
 2011-11-25	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/model/WstValueTable.java(findQsForW):
@@ -25,7 +32,7 @@
 
 	* src/main/java/de/intevation/flys/exports/XYChartGenerator.java:
 	  (createAxes, removeEmptyRangeAxes): Survive empty datasets map, create
-					      primary axis.
+	  primary axis.
 	  (recoverEmptyPlot): New.
 
 2011-11-25  Felix Wolfsteller <felix.wolfsteller@intevation.de>
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTable.java	Fri Nov 25 15:50:31 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTable.java	Fri Nov 25 17:27:40 2011 +0000
@@ -166,10 +166,103 @@
             }
         }
 
-        public double [][] interpolateWQ(
+        public static final class SplineFunction {
+
+            public PolynomialSplineFunction spline;
+            public double []                splineQs;
+            public double []                splineWs;
+
+            public SplineFunction(
+                PolynomialSplineFunction spline,
+                double []                splineQs, 
+                double []                splineWs
+            ) {
+                this.spline   = spline;
+                this.splineQs = splineQs;
+                this.splineWs = splineWs;
+            }
+
+            public double [][] sample(
+                int         numSamples, 
+                double      km, 
+                Calculation errors
+            ) {
+                double q1 = splineQs[0], qN = splineQs[splineQs.length-1];
+                double minQ = Math.min(q1, qN);
+                double maxQ = Math.max(q1, qN);
+
+                double [] outWs = new double[numSamples];
+                double [] outQs = new double[numSamples];
+
+                Arrays.fill(outWs, Double.NaN);
+                Arrays.fill(outQs, Double.NaN);
+
+                double stepWidth = (maxQ - minQ)/numSamples;
+
+                try {
+                    double q = minQ;
+                    for (int i = 0; i < outWs.length; ++i, q += stepWidth) {
+                        outWs[i] = spline.value(outQs[i] = q);
+                    }
+                }
+                catch (ArgumentOutsideDomainException aode) {
+                    if (errors != null) {
+                        // TODO: I18N
+                        errors.addProblem(km, "spline interpolation failed");
+                    }
+                    log.error("spline interpolation failed.", aode);
+                }
+
+                return new double [][] { outWs, outQs };
+            }
+        } // class SplineFunction
+
+        public SplineFunction createSpline(
+            WstValueTable table,
+            Calculation   errors
+        ) {
+            int W = ws.length;
+
+            if (W < 1) {
+                if (errors != null) {
+                    // TODO: I18N
+                    errors.addProblem(km, "no ws found");
+                }
+                return null;
+            }
+
+            double [] splineQs = new double[W];
+
+            for (int i = 0; i < W; ++i) {
+                double sq = table.getQIndex(i, km);
+                if (Double.isNaN(sq) && errors != null) {
+                    // TODO: I18N
+                    errors.addProblem(
+                        km, "no q found in " + (i+1) + " column");
+                }
+                splineQs[i] = sq;
+            }
+
+            try {
+                SplineInterpolator interpolator = new SplineInterpolator();
+                PolynomialSplineFunction spline =
+                    interpolator.interpolate(splineQs, ws);
+
+                return new SplineFunction(spline, splineQs, ws);
+            }
+            catch (MathIllegalArgumentException miae) {
+                if (errors != null) {
+                    // TODO: I18N
+                    errors.addProblem(km, "spline creation failed");
+                }
+                log.error("spline creation failed", miae);
+            }
+            return null;
+        }
+
+        public SplineFunction createSpline(
             Row           other,
             double        km,
-            int           steps,
             WstValueTable table,
             Calculation   errors
         ) {
@@ -180,16 +273,13 @@
                     // TODO: I18N
                     errors.addProblem("no ws found");
                 }
-                return new double[2][0];
+                return null;
             }
 
             double factor = Linear.factor(km, this.km, other.km);
 
-            double [] splineQ = new double[W];
-            double [] splineW = new double[W];
-
-            double minQ =  Double.MAX_VALUE;
-            double maxQ = -Double.MAX_VALUE;
+            double [] splineQs = new double[W];
+            double [] splineWs = new double[W];
 
             for (int i = 0; i < W; ++i) {
                 double wws = Linear.weight(factor, ws[i], other.ws[i]);
@@ -204,22 +294,18 @@
                         errors.addProblem(km, "cannot find w or q");
                     }
                 }
-                else {
-                    if (wqs < minQ) minQ = wqs;
-                    if (wqs > maxQ) maxQ = wqs;
-                }
 
-                splineW[i] = wws;
-                splineQ[i] = wqs;
+                splineWs[i] = wws;
+                splineQs[i] = wqs;
             }
 
-            double stepWidth = (maxQ - minQ)/steps;
-
             SplineInterpolator interpolator = new SplineInterpolator();
-            PolynomialSplineFunction spline;
 
             try {
-                spline = interpolator.interpolate(splineQ, splineW);
+                PolynomialSplineFunction spline =
+                    interpolator.interpolate(splineQs, splineWs);
+
+                return new SplineFunction(spline, splineQs, splineWs);
             }
             catch (MathIllegalArgumentException miae) {
                 if (errors != null) {
@@ -227,30 +313,23 @@
                     errors.addProblem(km, "spline creation failed");
                 }
                 log.error("spline creation failed", miae);
-                return new double[2][0];
             }
 
-            double [] outWs = new double[steps];
-            double [] outQs = new double[steps];
-
-            Arrays.fill(outWs, Double.NaN);
-            Arrays.fill(outQs, Double.NaN);
+            return null;
+        }
 
-            try {
-                double q = minQ;
-                for (int i = 0; i < outWs.length; ++i, q += stepWidth) {
-                    outWs[i] = spline.value(outQs[i] = q);
-                }
-            }
-            catch (ArgumentOutsideDomainException aode) {
-                if (errors != null) {
-                    // TODO: I18N
-                    errors.addProblem(km, "spline interpolation failed");
-                }
-                log.error("spline interpolation failed", aode);
-            }
+        public double [][] interpolateWQ(
+            Row           other,
+            double        km,
+            int           steps,
+            WstValueTable table,
+            Calculation   errors
+        ) {
+            SplineFunction sf = createSpline(other, km, table, errors);
 
-            return new double [][] { outWs, outQs };
+            return sf != null
+                ? sf.sample(steps, km, errors)
+                : new double[2][0];
         }
 
 
@@ -259,76 +338,11 @@
             WstValueTable table,
             Calculation   errors
         ) {
-            int W = ws.length;
-
-            if (W < 1) {
-                if (errors != null) {
-                    // TODO: I18N
-                    errors.addProblem(km, "no ws found");
-                }
-                return new double[2][0];
-            }
-
-            double [] splineQ = new double[W];
-
-            double minQ =  Double.MAX_VALUE;
-            double maxQ = -Double.MAX_VALUE;
-
-            for (int i = 0; i < W; ++i) {
-                double sq = table.getQIndex(i, km);
-                if (Double.isNaN(sq)) {
-                    if (errors != null) {
-                        // TODO: I18N
-                        errors.addProblem(
-                            km, "no q found in " + (i+1) + " column");
-                    }
-                }
-                else {
-                    if (sq < minQ) minQ = sq;
-                    if (sq > maxQ) maxQ = sq;
-                }
-                splineQ[i] = sq;
-            }
-
-            double stepWidth = (maxQ - minQ)/steps;
-
-            SplineInterpolator interpolator = new SplineInterpolator();
+            SplineFunction sf = createSpline(table, errors);
 
-            PolynomialSplineFunction spline;
-
-            try {
-                spline = interpolator.interpolate(splineQ, ws);
-            }
-            catch (MathIllegalArgumentException miae) {
-                if (errors != null) {
-                    // TODO: I18N
-                    errors.addProblem(km, "spline creation failed");
-                }
-                log.error("spline creation failed", miae);
-                return new double[2][0];
-            }
-
-            double [] outWs = new double[steps];
-            double [] outQs = new double[steps];
-
-            Arrays.fill(outWs, Double.NaN);
-            Arrays.fill(outQs, Double.NaN);
-
-            try {
-                double q = minQ;
-                for (int i = 0; i < outWs.length; ++i, q += stepWidth) {
-                    outWs[i] = spline.value(outQs[i] = q);
-                }
-            }
-            catch (ArgumentOutsideDomainException aode) {
-                if (errors != null) {
-                    // TODO: I18N
-                    errors.addProblem(km, "spline interpolation failed");
-                }
-                log.error("spline interpolation failed.", aode);
-            }
-
-            return new double [][] { outWs, outQs };
+            return sf != null
+                ? sf.sample(steps, km, errors)
+                : new double[2][0];
         }
 
 

http://dive4elements.wald.intevation.org