sascha@2182: package de.intevation.flys.artifacts.model;
sascha@2182: 
sascha@2182: import de.intevation.flys.utils.DataUtil;
teichmann@4821: import de.intevation.flys.utils.DoubleUtil;
sascha@2182: 
sascha@2182: import gnu.trove.TDoubleArrayList;
sascha@2182: 
sascha@2182: import org.apache.log4j.Logger;
sascha@2182: 
sascha@2182: public class W
sascha@2182: extends      NamedObjectImpl
sascha@2182: {
sascha@2182:     private static Logger log = Logger.getLogger(W.class);
sascha@2182: 
sascha@2182:     protected TDoubleArrayList ws;
sascha@2182: 
sascha@2182:     public W() {
sascha@2226:         ws = new TDoubleArrayList();
sascha@2182:     }
sascha@2182: 
sascha@2182:     public W(String name) {
sascha@2182:         super(name);
sascha@2226:         ws = new TDoubleArrayList();
sascha@2182:     }
sascha@2182: 
sascha@2182:     public W(int capacity) {
sascha@2182:         this(capacity, "");
sascha@2182:     }
sascha@2182: 
sascha@2182:     public W(int capacity, String name) {
sascha@2182:         super(name);
sascha@2182:         ws = new TDoubleArrayList(capacity);
sascha@2182:     }
sascha@2182: 
sascha@2182:     public void add(double value) {
sascha@2182:         ws.add(value);
sascha@2182:     }
sascha@2182: 
sascha@2182:     public int size() {
sascha@2182:         return ws.size();
sascha@2182:     }
sascha@2182: 
sascha@2182:     public double getW(int idx) {
sascha@2182:         return ws.getQuick(idx);
sascha@2182:     }
sascha@2182: 
sascha@2182:     public double [] getWs() {
sascha@2182:         return ws.toNativeArray();
sascha@2182:     }
sascha@2182: 
sascha@2182:     public double [] get(int idx) {
sascha@2182:         return get(idx, new double [1]);
sascha@2182:     }
sascha@2182: 
sascha@2182:     public double [] get(int idx, double [] dst) {
sascha@2182:         dst[0] = ws.getQuick(idx);
sascha@2182:         return dst;
sascha@2182:     }
sascha@2182: 
sascha@2198:     public double minWs() {
sascha@2198:         return ws.min();
sascha@2198:     }
sascha@2198: 
sascha@2182:     public void removeNaNs() {
teichmann@4821:         DoubleUtil.removeNaNs(new TDoubleArrayList [] { ws });
sascha@2182:     }
sascha@2182: 
sascha@2182:     public boolean guessWaterIncreasing() {
sascha@2182:         return guessWaterIncreasing(0.05f);
sascha@2182:     }
sascha@2182: 
sascha@2182:     public boolean guessWaterIncreasing(float factor) {
sascha@2182:         return DataUtil.guessWaterIncreasing(ws, factor);
sascha@2182:     }
sascha@2182: 
sascha@2182:     public int [] longestIncreasingWRangeIndices() {
sascha@2182:         return longestIncreasingWRangeIndices(new int[2]);
sascha@2182:     }
sascha@2182: 
sascha@2182:     public int [] longestIncreasingWRangeIndices(int [] bounds) {
sascha@2182: 
sascha@2182:         int N = size();
sascha@2182:         int start = 0;
sascha@2182:         int stop  = 0;
sascha@2182: 
sascha@2182:         double lastW = Double.MAX_VALUE;
sascha@2182: 
sascha@2182:         for (int i = 0; i < N; ++i) {
sascha@2182:             double v = ws.getQuick(i);
sascha@2182:             if (v <= lastW) {
sascha@2182:                 if (stop-start > bounds[1]-bounds[0]) {
sascha@2182:                     bounds[0] = start;
sascha@2182:                     bounds[1] = stop;
sascha@2182:                     if (log.isDebugEnabled()) {
sascha@2182:                         log.debug("new range: " +
sascha@2182:                             bounds[0] + " - " + bounds[1] + " (" +
sascha@2182:                             ws.getQuick(bounds[0]) + ", " +
sascha@2182:                             ws.getQuick(bounds[1]) + ")");
sascha@2182: 
sascha@2182:                     }
sascha@2182:                 }
sascha@2182:                 start = stop = i;
sascha@2182:             }
sascha@2182:             else {
sascha@2182:                 stop = i;
sascha@2182:             }
sascha@2182:             lastW = v;
sascha@2182:         }
sascha@2182: 
sascha@2182:         if (stop-start > bounds[1]-bounds[0]) {
sascha@2182:             bounds[0] = start;
sascha@2182:             bounds[1] = stop;
sascha@2182:             if (log.isDebugEnabled()) {
sascha@2182:                 log.debug("new range @end: " +
sascha@2182:                     bounds[0] + " - " + bounds[1] + " (" +
sascha@2182:                     ws.getQuick(bounds[0]) + ", " +
sascha@2182:                     ws.getQuick(bounds[1]) + ")");
sascha@2182:             }
sascha@2182:         }
sascha@2182: 
sascha@2182:         return bounds;
sascha@2182:     }
sascha@2182: }
sascha@2182: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :