# HG changeset patch # User Sascha L. Teichmann # Date 1303224508 0 # Node ID 67b3f54188aacfe0edfb127d8f3a1b5e2409aded # Parent bf4e12f1d0258438a0e8e316299fb8bc901a6eca Discharge tables: Sorting of q values was done wrong. flys-artifacts/trunk@1731 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r bf4e12f1d025 -r 67b3f54188aa flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Tue Apr 19 13:10:27 2011 +0000 +++ b/flys-artifacts/ChangeLog Tue Apr 19 14:48:28 2011 +0000 @@ -1,3 +1,8 @@ +2011-04-19 Sascha L. Teichmann + + * src/main/java/de/intevation/flys/artifacts/model/DischargeTables.java: + Sorting of q values was done wrong. + 2011-04-19 Ingo Weinzierl * src/main/java/de/intevation/flys/artifacts/model/AnnotationsFactory.java: diff -r bf4e12f1d025 -r 67b3f54188aa flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DischargeTables.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DischargeTables.java Tue Apr 19 13:10:27 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DischargeTables.java Tue Apr 19 14:48:28 2011 +0000 @@ -27,18 +27,6 @@ public static final int MASTER = 0; - public static final double EPSILON = 1e-5; - - public static Comparator Q_COMPARATOR = - new Comparator() { - public int compare(double [] a, double [] b) { - double d = a[0] - b[0]; - if (d < -EPSILON) return -1; - if (d > +EPSILON) return +1; - return 0; - } - }; - protected List gaugeNames; protected String riverName; @@ -137,19 +125,56 @@ List dtvs = table.getDischargeTableValues(); - double [][] vs = new double[2][dtvs.size()]; + final double [][] vs = new double[2][dtvs.size()]; + + boolean qSorted = true; + + double lastQ = -Double.MAX_VALUE; int idx = 0; for (DischargeTableValue dtv: dtvs) { - vs[0][idx] = dtv.getQ().doubleValue() * scale; + double q = dtv.getQ().doubleValue(); + vs[0][idx] = q * scale; vs[1][idx] = dtv.getW().doubleValue() * scale; ++idx; + + if (qSorted && lastQ > q) { + qSorted = false; + } + lastQ = q; } - // TODO: Do this db level. - Arrays.sort(vs, Q_COMPARATOR); + if (!qSorted) { + log.debug("need to sort by q values"); + // TODO: Do this db level. + // XXX: This is so ugly :-( + Integer [] indices = new Integer[vs[0].length]; + for (int i = 0; i < indices.length; ++i) { + indices[i] = i; + } - values.put(gaugeName, vs); + Arrays.sort(indices, new Comparator() { + public int compare(Integer a, Integer b) { + double va = vs[1][a]; + double vb = vs[1][b]; + double d = va - vb; + if (d < 0.0) return -1; + if (d > 0.0) return +1; + return 0; + } + }); + + double [][] vs2 = new double[2][vs[0].length]; + for (int i = 0; i < indices.length; ++i) { + vs2[0][i] = vs[0][indices[i]]; + vs2[1][i] = vs[0][indices[i]]; + } + values.put(gaugeName, vs2); + } + else { + values.put(gaugeName, vs); + } + } return values; @@ -157,10 +182,10 @@ public static boolean getWForQ( double [][] values, - double q, - double [] result + double q, + double [] result ) { - int index = Arrays.binarySearch(values, new double [] { q }, Q_COMPARATOR); + int index = Arrays.binarySearch(values[1], q); if (index >= 0) { result[0] = values[1][index]; return true;