view flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/MovingAverage.java @ 5122:5ba502e78e05 dami

Fix import of hws_* on Oracle The OCI driver needs a field named OGR_FID as the first element of a table, otherwise it gets confused and tries to add the fid twice in the insert statement leading to the "too many values" error.
author Andre Heinecke <aheinecke@intevation.de>
date Thu, 28 Feb 2013 10:09:31 +0100
parents d35d316049e8
children fb9892036bd6
line wrap: on
line source
package de.intevation.flys.artifacts.math;

import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;


public class MovingAverage
{

    public static double[][] simple(double[][] values, double radius) {
        TreeMap<Double, Double> map = toMap(values);
        int N = map.size();
        double [] xs = new double[N];
        double [] ys = new double[N];
        int ndx = 0;
        for (double x: map.keySet()) {
            SortedMap<Double, Double> range =
                map.subMap(x-radius, true, x+radius, true);
            double avg = 0d;
            for (double v: range.values()) {
                avg += v;
            }
            avg /= range.size();
            xs[ndx] = x;
            ys[ndx] = avg;
            ndx++;
        }
        return new double [][] { xs, ys };
    }

    public static double[][] weighted(double[][] values, double radius) {
        TreeMap<Double, Double> map = toMap(values);
        int N = map.size();
        double [] xs = new double[N];
        double [] ys = new double[N];
        int ndx = 0;
        double _1radius = 1d/radius;
        for (double x: map.keySet()) {
            double avg = 0d;
            double weights = 0d;
            for (Map.Entry<Double, Double> e:
                map.subMap(x-radius, false, x+radius, false).entrySet()
            ) {
                double weight = 1d - Math.abs(x - e.getKey())*_1radius;
                avg += weight*e.getValue();
                weights += weight;
            }
            avg /= weights;
            xs[ndx] = x;
            ys[ndx] = avg;
            ndx++;
        }
        return new double [][] { xs, ys };
    }

    private static TreeMap<Double, Double> toMap(double[][] values) {
        TreeMap<Double, Double> map = new TreeMap<Double, Double>();
        double [] xs = values[0];
        double [] ys = values[1];
        for (int i = 0; i < xs.length; i++) {
            map.put(xs[i], ys[i]);
        }
        return map;
    }
}

http://dive4elements.wald.intevation.org