changeset 3056:0b5a7a2c3724

Try to workaround some exception in FixWQCurveFacet and FixWQCurveGenerator flys-artifacts/trunk@4632 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Christian Lins <christian.lins@intevation.de>
date Sun, 10 Jun 2012 11:12:38 +0000
parents 1fbf8462f808
children febc39e77672
files flys-artifacts/src/main/java/de/intevation/flys/artifacts/FixationArtifactAccess.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/fixings/FixWQCurveFacet.java flys-artifacts/src/main/java/de/intevation/flys/exports/fixings/FixWQCurveGenerator.java
diffstat 4 files changed, 67 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FixationArtifactAccess.java	Sat Jun 09 13:46:04 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FixationArtifactAccess.java	Sun Jun 10 11:12:38 2012 +0000
@@ -400,9 +400,7 @@
 
     public double getCurrentKm() {
         //TODO: get the current km.
-        //return this.from.doubleValue();
-        //FIXME
-        return 13.37;
+        return getFrom().doubleValue();
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java	Sat Jun 09 13:46:04 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java	Sun Jun 10 11:12:38 2012 +0000
@@ -34,6 +34,7 @@
                 return i;
             }
         }
+        log.debug("columnIndex: " + name + " not found in columnNames");
         return -1;
     }
 
@@ -121,6 +122,11 @@
         return binarySearch(columnIndex(columnName), value);
     }
 
+    /**
+     * Performes a binary search in the column identified by its
+     * index.
+     * @return Index of found element or negative insertion point (shifted by one)
+     */
     public int binarySearch(int columnIndex, double value) {
         TDoubleArrayList column = columns[columnIndex];
         return column.binarySearch(value);
@@ -131,18 +137,22 @@
     }
 
     public int binarySearch(int columnIndex, double value, double epsilon) {
-        if (epsilon < 0d) epsilon = -epsilon;
+        if (epsilon < 0d)
+            epsilon = -epsilon;
         double vl = value - epsilon;
         double vh = value + epsilon;
 
         TDoubleArrayList column = columns[columnIndex];
         int lo = 0, hi = column.size()-1;
         while (hi >= lo) {
-            int mid = (lo+hi) >> 1;
+            int mid = (lo + hi) >> 1;
             double v = column.getQuick(mid);
-            if      (v < vl) hi = mid - 1;
-            else if (v > vh) lo = mid + 1;
-            else             return mid;
+            if (v < vl) 
+                hi = mid - 1;
+            else if (v > vh) 
+                lo = mid + 1;
+            else
+                return mid;
         }
 
         return -1;
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/fixings/FixWQCurveFacet.java	Sat Jun 09 13:46:04 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/fixings/FixWQCurveFacet.java	Sun Jun 10 11:12:38 2012 +0000
@@ -20,7 +20,6 @@
 
 import de.intevation.flys.artifacts.states.DefaultState.ComputeType;
 
-
 /**
  * Facet to show the W|Q values.
  *
@@ -69,12 +68,21 @@
             FixResult result = (FixResult) res.getData();
 
             double km = access.getCurrentKm();
+            logger.debug("FixWQCurveFacet.getData: km = " + km);
 
             String function = access.getFunction();
             Function ff = FunctionFactory.getInstance().getFunction(function);
 
             Parameters params = result.getParameters();
-            int row = 0; //params.binarySearch("km", km, Math.pow(10, -4)); //FIXME: search failes as access.getCurrentKm() always returns 13.37...
+            String[] columnNames = params.getColumnNames();
+            for(String columnName : columnNames) {
+                logger.debug("FixWQCurveFacet.getData: columnName = '" + columnName + "'");
+            }
+            int row = params.binarySearch("km", km /*, Math.pow(10, -4)*/); //FIXME Use epsilon as soon access.getCurrentKm() is fixed
+            if(row < 0) {
+                row = -row - 1;
+                logger.debug("getData: no direct hit in params.binarySearch");
+            }
             String[] paramNames = ff.getParameterNames();
             int[] paramInd = params.columnIndices(paramNames);
             double[] coeffs = new double[paramNames.length];
@@ -83,7 +91,8 @@
             de.intevation.flys.artifacts.math.Function mf =
                 ff.instantiate(coeffs);
 
-            double maxQ = 100; //getMaxQ(result, km); //FIXME
+            double maxQ = getMaxQ(result, km);
+            logger.debug("getData: maxQ = " + maxQ);
 
             FixFunction fix = new FixFunction(
                 ff.getName(),
@@ -105,29 +114,39 @@
 
         KMIndex<QW []> kmQWRef = result.getReferenced();
 
-        QW[] qwRef = kmQWRef.binarySearch(km).getValue();
-        if (qwRef != null) {
-            for (int i = 0; i < qwRef.length; i++) {
-                if (qwRef[i].getQ() > maxQ) {
-                    maxQ = qwRef[i].getQ();
+        KMIndex.Entry<QW []> qwEntry = kmQWRef.binarySearch(km);
+        if(qwEntry != null) {
+            QW[] qwRef = qwEntry.getValue();
+            if (qwRef != null) {
+                for (int i = 0; i < qwRef.length; i++) {
+                    if (qwRef[i].getQ() > maxQ) {
+                        maxQ = qwRef[i].getQ();
+                    }
                 }
             }
+        } else {
+            logger.debug("getMaxQ: qwEntry is null, that's odd...");
         }
 
         KMIndex<AnalysisPeriod []> kmQWDAna = result.getAnalysisPeriods();
-        AnalysisPeriod[] periods = kmQWDAna.binarySearch(km).getValue();
+        KMIndex.Entry<AnalysisPeriod []> periodsEntry = kmQWDAna.binarySearch(km);
 
-        if(periods != null) {
-            for (int i = 0; i < periods.length; i++) {
-                QWD[] qwdAna = periods[i].getQWDs();
-                if (qwdAna != null) {
-                    for (int j = 0; j < qwdAna.length; j++) {
-                        if (qwdAna[j].getQ() > maxQ) {
-                            maxQ = qwdAna[j].getQ();
+        if(periodsEntry != null) {
+            AnalysisPeriod[] periods = kmQWDAna.binarySearch(km).getValue();
+            if(periods != null) {
+                for (int i = 0; i < periods.length; i++) {
+                    QWD[] qwdAna = periods[i].getQWDs();
+                    if (qwdAna != null) {
+                        for (int j = 0; j < qwdAna.length; j++) {
+                            if (qwdAna[j].getQ() > maxQ) {
+                                maxQ = qwdAna[j].getQ();
+                            }
                         }
                     }
                 }
             }
+        } else {
+            logger.debug("getMaxQ: periodsEntry is null, that's odd...");
         }
         return maxQ;
     }
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/fixings/FixWQCurveGenerator.java	Sat Jun 09 13:46:04 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/fixings/FixWQCurveGenerator.java	Sun Jun 10 11:12:38 2012 +0000
@@ -3,6 +3,7 @@
 import de.intevation.artifactdatabase.state.ArtifactAndFacet;
 import de.intevation.flys.artifacts.model.FacetTypes;
 import de.intevation.flys.artifacts.model.WQDay;
+import de.intevation.flys.artifacts.model.fixings.FixFunction;
 import de.intevation.flys.artifacts.model.fixings.QW;
 import de.intevation.flys.exports.ChartGenerator;
 import de.intevation.flys.exports.XYChartGenerator;
@@ -54,30 +55,22 @@
     }
 
     @Override
-    public void doOut(ArtifactAndFacet bundle, Document doc, boolean visible) {
-        String name = bundle.getFacetName();
-/*        Object data;
-        try {
-            data = bundle.getData(context);
-        } catch(NullPointerException ex) { // FIXME it's crashing on every edge 
-            logger.error("doOut Exception catched", ex);
-            data = new Object();
-        }*/
+    public void doOut(ArtifactAndFacet aaf, Document doc, boolean visible) {
+        String name = aaf.getFacetName();
         logger.debug("doOut: " + name);
-        //logger.debug("doOut: data is instanceof " + data.toString());
-        
+
         if(FIX_SECTOR_AVERAGE_WQ.equals(name)) {
-            doSectorAverageOut(bundle, doc, visible);
+            doSectorAverageOut(aaf, doc, visible);
         } else if(FIX_ANALYSIS_EVENTS_WQ.equals(name)) {
-            doAnalysisEventsOut(bundle, doc, visible);
+            doAnalysisEventsOut(aaf, doc, visible);
         } else if(FIX_ANALYSIS_PERIODS_WQ.equals(name)) {
-            doAnalysisPeriodsOut(bundle, doc, visible);
+            doAnalysisPeriodsOut(aaf, doc, visible);
         } else if(FIX_REFERENCE_EVENTS_WQ.equals(name)) {
-            doReferenceEventsOut(bundle, doc, visible);
+            doReferenceEventsOut(aaf, doc, visible);
         } else if(FIX_WQ_CURVE.equals(name)) {
-            doWQOut(new QW(13.37, 42.23, "Unknown point", new Date()), bundle, doc, visible);
+            doWQCurveOut(aaf, doc, visible);
         } else if(FIX_OUTLIER.equals(name)) {
-            doOutlierOut(bundle, doc, visible);
+            doOutlierOut(aaf, doc, visible);
         } else {
             logger.warn("Unknown facet name " + name);
             return;
@@ -132,22 +125,14 @@
        addAxisSeries(series, 0, visible);
     }
 
-    protected void doWQOut(
-        QW            wqdays,
-        ArtifactAndFacet aaf,
-        Document         theme,
-        boolean          visible
-    ) {
-        logger.debug("doWQOut");
+    protected void doWQCurveOut(ArtifactAndFacet aaf, Document doc, boolean visible) {
+        logger.debug("doWQCurveOut");
 
-        XYSeries series = new StyledXYSeries(aaf.getFacetDescription(), theme);
-        java.util.Random rand = new java.util.Random(1000);
-        int size = 10; //wqdays.size();
-        for (int i = 0; i < size; i++) {
-            //int  day = wqdays.getDate(i);
-            //double q = wqdays.getQ(i);
+        FixFunction func = (FixFunction)aaf.getData(context);
 
-            series.add(rand.nextDouble(), rand.nextDouble());
+        XYSeries series = new StyledXYSeries(aaf.getFacetDescription(), doc);
+        for (int q = 0; q <= func.getMaxQ(); q += 1.0) {
+            series.add(q, func.getFunction().value(q));
         }
 
         addAxisSeries(series, 0, visible);

http://dive4elements.wald.intevation.org