comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/extreme/Curve.java @ 3733:893b2477208f

Some first models needed for the extreme waterlevels. flys-artifacts/trunk@5406 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sat, 08 Sep 2012 18:19:33 +0000
parents
children 976be312a84c
comparison
equal deleted inserted replaced
3732:fe29b0226faf 3733:893b2477208f
1 package de.intevation.flys.artifacts.model.extreme;
2
3 import de.intevation.flys.artifacts.math.Function;
4 import de.intevation.flys.artifacts.math.NaNFunction;
5 import de.intevation.flys.artifacts.math.UnivariateRealFunctionFunction;
6
7 import de.intevation.flys.artifacts.math.fitting.FunctionFactory;
8
9 import de.intevation.flys.artifacts.model.QW;
10
11 import java.io.Serializable;
12
13 import java.lang.ref.SoftReference;
14
15 import org.apache.commons.math.analysis.interpolation.SplineInterpolator;
16
17 import org.apache.commons.math.exception.MathIllegalArgumentException;
18
19 import org.apache.log4j.Logger;
20
21 public class Curve
22 implements Serializable, Function
23 {
24 private static Logger log = Logger.getLogger(Curve.class);
25
26 protected QW [] qws;
27 protected String function;
28 protected double [] coeffs;
29
30 // The spline is pretty heavy weight so cache it with a soft ref only.
31 protected transient SoftReference<Function> spline;
32 protected transient Function extrapolation;
33
34 public Curve() {
35 }
36
37 public Curve(QW [] qws, String function, double [] coeffs) {
38 this.qws = qws;
39 this.function = function;
40 this.coeffs = coeffs;
41 }
42
43 public QW [] getQWs() {
44 return qws;
45 }
46
47 public String getFunction() {
48 return function;
49 }
50
51 public double [] getCoeffs() {
52 return coeffs;
53 }
54
55 @Override
56 public double value(double x) {
57 if (x < qws[0].getQ()) return Double.NaN;
58 return (x <= qws[qws.length-1].getQ()
59 ? getSpline()
60 : getExtrapolation()).value(x);
61 }
62
63 protected synchronized Function getExtrapolation() {
64 if (extrapolation == null) {
65 de.intevation.flys.artifacts.math.fitting.Function
66 f = FunctionFactory.getInstance().getFunction(function);
67
68 extrapolation = f != null
69 ? f.instantiate(coeffs)
70 : NaNFunction.INSTANCE;
71 }
72 return extrapolation;
73 }
74
75 protected synchronized Function getSpline() {
76 Function sp;
77 if (spline != null) {
78 if ((sp = spline.get()) != null) {
79 return sp;
80 }
81 }
82 spline = new SoftReference(sp = createSpline());
83 return sp;
84 }
85
86 protected Function createSpline() {
87 SplineInterpolator interpolator = new SplineInterpolator();
88 double [] qs = new double[qws.length];
89 double [] ws = new double[qws.length];
90 for (int i = 0; i < qws.length; ++i) {
91 QW qw = qws[i];
92 qs[i] = qw.getQ();
93 ws[i] = qw.getW();
94 }
95 try {
96 return new UnivariateRealFunctionFunction(
97 interpolator.interpolate(qs, ws));
98 }
99 catch (MathIllegalArgumentException miae) {
100 log.debug("creation on spline failed", miae);
101 return NaNFunction.INSTANCE;
102 }
103 }
104 }
105 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org