changeset 8169:856701739443

Handle missing years in bed height differences without assuming an unknown year to be BC 1.
author "Tom Gottfried <tom@intevation.de>"
date Mon, 01 Sep 2014 09:43:55 +0200
parents 75a6ac0c73ef
children 363b82ecf29f
files artifacts/doc/conf/meta-data.xml artifacts/src/main/java/org/dive4elements/river/artifacts/BedHeightsArtifact.java artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedDiffCalculation.java artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedDiffYearResult.java artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedHeightSingleData.java artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/DifferencesState.java
diffstat 6 files changed, 91 insertions(+), 79 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/doc/conf/meta-data.xml	Fri Aug 29 17:00:26 2014 +0200
+++ b/artifacts/doc/conf/meta-data.xml	Mon Sep 01 09:43:55 2014 +0200
@@ -1813,19 +1813,19 @@
               <dc:when test="dc:group-key() = $b1year">
                 <height factory="bedheight"
                         target_out="{$out}"
-                        ids="bedheight-single-{$b1id}-{$b1year}#bedheight-single-{$b2id}-{$b2year}"
+                        ids="bedheight-single-{$b1id}#bedheight-single-{$b2id}"
                         info="{$b1desc}#{$b2desc}"
                         description="{$b1desc} - {$b2desc}"/>
               </dc:when>
               <dc:when test="dc:group-key() = 'sonstige'">
                 <height factory="bedheight"
                         target_out="{$out}"
-                        ids="bedheight-single-{$b1id}-{$b1year}#bedheight-single-{$b2id}-{$b2year}"
+                        ids="bedheight-single-{$b1id}#bedheight-single-{$b2id}"
                         info="{$b1desc}#{$b2desc}"
                         description="{$b1desc} - {$b2desc}"/>
                 <height factory="bedheight"
                         target_out="{$out}"
-                        ids="bedheight-single-{$b2id}-{$b2year}#bedheight-single-{$b1id}-{$b1year}"
+                        ids="bedheight-single-{$b2id}#bedheight-single-{$b1id}"
                         info="{$b2desc}#{$b1desc}"
                         description="{$b2desc} - {$b1desc}"/>
               </dc:when>
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/BedHeightsArtifact.java	Fri Aug 29 17:00:26 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/BedHeightsArtifact.java	Mon Sep 01 09:43:55 2014 +0200
@@ -89,7 +89,7 @@
         if (code != null) {
             String [] parts = code.split("-");
 
-            if (parts.length >= 4) {
+            if (parts.length >= 3) {
                 // The setting is a bit complicated:
                 // This artifact can spawn epoch type bedheight facets,
                 // 'singlevalue'/singleyear bedheight facets or
@@ -104,7 +104,6 @@
                     facetType = BEDHEIGHT_SOUNDING_WIDTH;
                 }
                 addStringData("height_id", parts[2]);
-                addStringData("time", parts[3]);
                 addStringData("type", type);
                 String btype = type;
                 int hId = Integer.parseInt(parts[2]);
@@ -134,6 +133,9 @@
 
                 addFacets(STATIC_STATE_NAME, facets);
             }
+            else {
+                logger.error("Invalid datacage ID '" + code + "'");
+            }
         }
         super.setup(identifier, factory, context, callMeta, data, loadFacets);
     }
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedDiffCalculation.java	Fri Aug 29 17:00:26 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedDiffCalculation.java	Mon Sep 01 09:43:55 2014 +0200
@@ -35,45 +35,35 @@
     public CalculationResult calculate(BedDifferencesAccess access, CallContext context) {
         logger.info("BedDiffCalculation.calculate");
 
-        String river       = access.getRiverName();
-        int [][] heightIds = access.extractHeightIds(context);
+        this.river     = access.getRiverName();
+        this.heightIds = access.extractHeightIds(context);
 
-        if (river == null) {
-            // TODO: i18n
-            addProblem("minfo.missing.river");
+        BedDiffYearResult [] results = new BedDiffYearResult[heightIds.length];
+        for (int i = 0; i < heightIds.length; i++) {
+            BedHeightSingleData [] pair = getHeightPair(heightIds[i]);
+            if (pair[0].getYear() == null || pair[1].getYear() == null) {
+                addProblem("beddiff.missing.year");
+            }
+            results[i] = calculateYearDifference(pair);
         }
 
-        if (!hasProblems()) {
-            this.river     = river;
-            this.heightIds = heightIds;
-            return internalCalculate();
-        }
-
-        return new CalculationResult();
-    }
-
-    private CalculationResult internalCalculate() {
-        BedDiffYearResult [] results = new BedDiffYearResult[heightIds.length];
-
-        for (int i = 0; i < heightIds.length; i++) {
-            BedHeightData [] pair = getHeightPair(heightIds[i], "single");
-            results[i] = calculateYearDifference(pair);
-        }
         return new CalculationResult(results, this);
     }
 
     /** Get two BedHeights from factory. */
-    private static BedHeightData [] getHeightPair(int [] ids, String type) {
-        return new BedHeightData [] {
-            BedHeightFactory.getHeight(type, ids[0]),
-            BedHeightFactory.getHeight(type, ids[1])
+    private static BedHeightSingleData [] getHeightPair(int [] ids) {
+        return new BedHeightSingleData [] {
+            (BedHeightSingleData)BedHeightFactory.getHeight("single", ids[0]),
+            (BedHeightSingleData)BedHeightFactory.getHeight("single", ids[1])
         };
     }
 
-    private BedDiffYearResult calculateYearDifference(BedHeightData[] pair) {
+    private BedDiffYearResult calculateYearDifference(
+            BedHeightSingleData[] pair
+        ) {
         logger.debug("BedDiffCalculation.calculateYearDifference");
-        BedHeightSingleData s1 = (BedHeightSingleData) pair[0];
-        BedHeightSingleData s2 = (BedHeightSingleData) pair[1];
+        BedHeightSingleData s1 = pair[0];
+        BedHeightSingleData s2 = pair[1];
 
         TDoubleArrayList stations = s1.getStations();
         int size = stations.size();
@@ -88,7 +78,10 @@
         TDoubleArrayList morphs1   = new TDoubleArrayList(size);
         TDoubleArrayList morphs2   = new TDoubleArrayList(size);
 
-        int range = Math.abs(s1.getYear() - s2.getYear());
+        Integer range = null;
+        if (s1.getYear() != null && s2.getYear() != null) {
+            range = Math.abs(s1.getYear() - s2.getYear());
+        }
 
         for (int i = 0; i < size; i++) {
             double station = stations.getQuick(i);
@@ -110,7 +103,9 @@
                     s1.getDataGap(station),
                     s2.getDataGap(station)));
 
-                absolute.add((hDiff / range) * 100d);
+                if (range != null) {
+                    absolute.add((hDiff / range) * 100d);
+                }
                 heights1.add(h1);
                 heights2.add(h2);
                 morphs1.add(m1);
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedDiffYearResult.java	Fri Aug 29 17:00:26 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedDiffYearResult.java	Mon Sep 01 09:43:55 2014 +0200
@@ -15,25 +15,23 @@
 public class BedDiffYearResult
 extends BedDifferencesResult
 {
-    protected TDoubleArrayList bedHeights;
+    protected TDoubleArrayList diffsPerYear;
     protected TDoubleArrayList dataGap;
     protected TDoubleArrayList soundingWidth;
     protected TDoubleArrayList morphWidth1;
     protected TDoubleArrayList morphWidth2;
-    protected int start;
-    protected int end;
+    protected Integer start;
+    protected Integer end;
     protected String nameFirst;
     protected String nameSecond;
 
     public BedDiffYearResult () {
         super();
-        this.bedHeights = new TDoubleArrayList();
+        this.diffsPerYear = new TDoubleArrayList();
         this.dataGap = new TDoubleArrayList();
         this.soundingWidth = new TDoubleArrayList();
         this.morphWidth1 = new TDoubleArrayList();
         this.morphWidth2 = new TDoubleArrayList();
-        this.start = -1;
-        this.end = -1;
     }
 
     /**
@@ -41,7 +39,7 @@
      * @param differences the height differences
      * @param heights1 the heights
      * @param heights2 the other heights
-     * @param bedHeights the differences normalized per year in m.
+     * @param diffsPerYear the differences normalized per year in cm.
      */
     public BedDiffYearResult(
         TDoubleArrayList kms,
@@ -51,15 +49,15 @@
         TDoubleArrayList morphWidth1,
         TDoubleArrayList morphWidth2,
         TDoubleArrayList soundingWidth,
-        TDoubleArrayList bedHeights,
+        TDoubleArrayList diffsPerYear,
         TDoubleArrayList dataGap,
-        int start,
-        int end,
+        Integer start,
+        Integer end,
         String nameFirst,
         String nameSecond
     ) {
         super(kms, differences, heights1, heights2);
-        this.bedHeights = bedHeights;
+        this.diffsPerYear = diffsPerYear;
         this.dataGap = dataGap;
         this.soundingWidth = soundingWidth;
         this.morphWidth1 = morphWidth1;
@@ -71,7 +69,7 @@
     }
 
     public TDoubleArrayList getBedHeights() {
-        return this.bedHeights;
+        return this.diffsPerYear;
     }
 
     public TDoubleArrayList getDataGap() {
@@ -90,7 +88,7 @@
         return this.soundingWidth;
     }
 
-    public int getStart() {
+    public Integer getStart() {
         return this.start;
     }
 
@@ -102,7 +100,7 @@
         this.end = value;
     }
 
-    public int getEnd() {
+    public Integer getEnd() {
         return this.end;
     }
 
@@ -129,7 +127,7 @@
     }
 
     public void addBedHeights(double value) {
-        this.bedHeights.add(value);
+        this.diffsPerYear.add(value);
     }
 
     public double[][] getMorphWidth1Data() {
@@ -156,7 +154,7 @@
     public double[][] getHeightPerYearData() {
         return new double[][] {
             kms.toNativeArray(),
-            bedHeights.toNativeArray()
+            diffsPerYear.toNativeArray()
         };
     }
 
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedHeightSingleData.java	Fri Aug 29 17:00:26 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedHeightSingleData.java	Mon Sep 01 09:43:55 2014 +0200
@@ -14,14 +14,13 @@
 public class BedHeightSingleData
 extends BedHeightData
 {
-    protected int year;
+    protected Integer year;
     protected TDoubleArrayList data_gap;
     protected TDoubleArrayList soundingWidth;
     protected TDoubleArrayList width;
 
     public BedHeightSingleData() {
         super();
-        this.year = -1;
         data_gap = new TDoubleArrayList();
         soundingWidth = new TDoubleArrayList();
         width = new TDoubleArrayList();
@@ -29,7 +28,6 @@
 
     public BedHeightSingleData(String name) {
         super(name);
-        this.year = -1;
         data_gap = new TDoubleArrayList();
         soundingWidth = new TDoubleArrayList();
         width = new TDoubleArrayList();
@@ -41,16 +39,18 @@
         double gap,
         double sounding,
         double width,
-        int year
+        Integer year
     ) {
         super.add(value, station);
-        this.year = year;
+        if (year != null) {
+            this.year = year;
+        }
         this.data_gap.add(gap);
         this.soundingWidth.add(sounding);
         this.width.add(width);
     }
 
-    public int getYear() {
+    public Integer getYear() {
         return this.year;
     }
 
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/DifferencesState.java	Fri Aug 29 17:00:26 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/DifferencesState.java	Mon Sep 01 09:43:55 2014 +0200
@@ -20,9 +20,11 @@
 import org.dive4elements.artifacts.CallMeta;
 import org.dive4elements.river.artifacts.D4EArtifact;
 import org.dive4elements.river.artifacts.access.BedDifferencesAccess;
+import org.dive4elements.river.artifacts.model.Calculation;
 import org.dive4elements.river.artifacts.model.CalculationResult;
 import org.dive4elements.river.artifacts.model.DataFacet;
 import org.dive4elements.river.artifacts.model.FacetTypes;
+import org.dive4elements.river.artifacts.model.ReportFacet;
 import org.dive4elements.river.artifacts.model.minfo.BedDiffCalculation;
 import org.dive4elements.river.artifacts.model.minfo.BedDiffFacet;
 import org.dive4elements.river.artifacts.model.minfo.BedDiffHeightMinFacet;
@@ -98,6 +100,12 @@
 
         facets.addAll(newFacets);
 
+        Calculation report = res.getReport();
+        if (report != null && report.hasProblems()) {
+            facets.add(new ReportFacet(ComputeType.ADVANCE, hash, id));
+            logger.warn("Problems: " + report.problemsToString());
+        }
+
         return res;
     }
 
@@ -172,16 +180,31 @@
                     ComputeType.ADVANCE,
                     stateId,
                     hash));
-                newFacets.add(new BedDiffPerYearFacet(
-                    idx,
-                    BED_DIFFERENCE_HEIGHT_YEAR,
-                    createBedDiffAbsoluteDescription(
-                        meta,
-                        (BedDiffYearResult)results[idx],
-                        true),
-                    ComputeType.ADVANCE,
-                    stateId,
-                    hash));
+                if (((BedDiffYearResult)results[idx])
+                    .getHeightPerYearData()[1].length > 0) {
+                    /* Skip facets with data per year if there are none
+                       (because of missing start or end year) */
+                    newFacets.add(new BedDiffPerYearFacet(
+                        idx,
+                        BED_DIFFERENCE_HEIGHT_YEAR,
+                        createBedDiffAbsoluteDescription(
+                            meta,
+                            (BedDiffYearResult)results[idx],
+                            true),
+                        ComputeType.ADVANCE,
+                        stateId,
+                        hash));
+                    newFacets.add(new BedDiffPerYearFilterFacet(
+                        idx,
+                        BED_DIFFERENCE_HEIGHT_YEAR_FILTERED,
+                        createBedDiffAbsoluteDescription(
+                            meta,
+                            (BedDiffYearResult)results[idx],
+                            false),
+                        ComputeType.ADVANCE,
+                        stateId,
+                        hash));
+                }
                 newFacets.add(new BedDiffFilterFacet(
                     idx,
                     BED_DIFFERENCE_YEAR_FILTERED,
@@ -214,16 +237,6 @@
                     ComputeType.ADVANCE,
                     stateId,
                     hash));
-                newFacets.add(new BedDiffPerYearFilterFacet(
-                    idx,
-                    BED_DIFFERENCE_HEIGHT_YEAR_FILTERED,
-                    createBedDiffAbsoluteDescription(
-                        meta,
-                        (BedDiffYearResult)results[idx],
-                        false),
-                    ComputeType.ADVANCE,
-                    stateId,
-                    hash));
             }
         }
     }
@@ -255,13 +268,17 @@
         BedDiffYearResult result,
         boolean raw
     ) {
-        String range = result.getStart() + " - " + result.getEnd();
+        String start = result.getStart() != null ?
+            result.getStart().toString() : result.getNameFirst();
+        String end = result.getEnd() != null ?
+            result.getEnd().toString() : result.getNameSecond();
+        String range = start + " - " + end;
 
         String i18n = I18N_FACET_BED_DIFF_YEAR;
         if (raw) {
             i18n = I18N_FACET_BED_DIFF_YEAR_RAW;
         }
-        return Resources.getMsg(meta, i18n, i18n, new Object[] { range });
+        return Resources.getMsg(meta, i18n, new Object[] { range });
     }
 
     protected String createBedDiffSoundingDescription(

http://dive4elements.wald.intevation.org