# HG changeset patch # User Raimund Renkert # Date 1348133688 0 # Node ID a1c79d84e3cd10d9629ebf809a445ad13208c71e # Parent 633fbb61a0ccf32bc69b63ace4ca66091b02b320 New calcultion in MINFO: Bed height difference calculation. flys-artifacts/trunk@5538 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 633fbb61a0cc -r a1c79d84e3cd flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Thu Sep 20 09:31:18 2012 +0000 +++ b/flys-artifacts/ChangeLog Thu Sep 20 09:34:48 2012 +0000 @@ -1,3 +1,16 @@ +2012-09-20 Raimund Renkert + + * src/main/java/de/intevation/flys/artifacts/model/minfo/BedDiffCalculation.java: + New. Calcuation for bed height differences. + + * src/main/java/de/intevation/flys/artifacts/model/minfo/BedDifferencesResult.java, + src/main/java/de/intevation/flys/artifacts/model/minfo/BedDiffEpochResult.java, + src/main/java/de/intevation/flys/artifacts/model/minfo/BedDiffYearResult.java: + New. Bed height calculation results. + + * src/main/java/de/intevation/flys/artifacts/access/BedDifferencesAccess.java: + New. Access object to get bed height difference parameters from artifact. + 2012-09-20 Raimund Renkert * src/main/java/de/intevation/flys/artifacts/model/minfo/BedHeightFactory.java: diff -r 633fbb61a0cc -r a1c79d84e3cd flys-artifacts/src/main/java/de/intevation/flys/artifacts/access/BedDifferencesAccess.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/access/BedDifferencesAccess.java Thu Sep 20 09:34:48 2012 +0000 @@ -0,0 +1,46 @@ +package de.intevation.flys.artifacts.access; + +import de.intevation.artifacts.CallContext; +import de.intevation.flys.artifacts.FLYSArtifact; +import de.intevation.flys.artifacts.model.minfo.BedHeight; +import de.intevation.flys.utils.FLYSUtils; +import de.intevation.flys.utils.StringUtil; + + +public class BedDifferencesAccess +extends RiverAccess +{ + + private String yearEpoch; + private String[] diffs; + + private CallContext context; + + public BedDifferencesAccess(FLYSArtifact artifact, CallContext context) { + super(artifact); + this.context = context; + } + + public String getYearEpoch() { + return yearEpoch; + } + + public FLYSArtifact[][] getDifferenceArtifacts() { + FLYSArtifact[][] artifacts = new FLYSArtifact[diffs.length][2]; + for (int i = 0; i < diffs.length; i++) { + String[] diff = diffs[i].split("#"); + String diff1 = StringUtil.unbracket(diff[0]); + String diff2 = StringUtil.unbracket(diff[1]); + String[] diff1parts = diff1.split(";"); + String[] diff2parts = diff2.split(";"); + artifacts[i][0] = FLYSUtils.getArtifact(diff1parts[0], context); + artifacts[i][1] = FLYSUtils.getArtifact(diff2parts[1], context); + } + return artifacts; + } + + public static int getHeightId(FLYSArtifact artifact) { + Access a = new Access(artifact); + return a.getInteger("height_id"); + } +} diff -r 633fbb61a0cc -r a1c79d84e3cd flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/BedDiffCalculation.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/BedDiffCalculation.java Thu Sep 20 09:34:48 2012 +0000 @@ -0,0 +1,163 @@ +package de.intevation.flys.artifacts.model.minfo; + +import gnu.trove.TDoubleArrayList; + +import java.util.LinkedList; +import java.util.List; + +import org.apache.log4j.Logger; + +import de.intevation.flys.artifacts.FLYSArtifact; +import de.intevation.flys.artifacts.access.BedDifferencesAccess; +import de.intevation.flys.artifacts.model.Calculation; +import de.intevation.flys.artifacts.model.CalculationResult; + + +public class BedDiffCalculation +extends Calculation +{ + + private static final Logger logger = Logger + .getLogger(BedQualityCalculation.class); + + protected String river; + protected String yearEpoch; + protected FLYSArtifact[][] artifacts; + + public BedDiffCalculation() { + } + + public CalculationResult calculate(BedDifferencesAccess access) { + logger.info("BedQualityCalculation.calculate"); + + String river = access.getRiver(); + String yearEpoch = access.getYearEpoch(); + FLYSArtifact[][] artifacts = access.getDifferenceArtifacts(); + + if (river == null) { + // TODO: i18n + addProblem("minfo.missing.river"); + } + + if (yearEpoch == null) { + addProblem("minfo.missing.year_epoch"); + } + + if (artifacts == null) { + addProblem("minfo.missing.differences"); + } + + if (!hasProblems()) { + this.river = river; + this.yearEpoch = yearEpoch; + this.artifacts = artifacts; + return internalCalculate(); + } + + return new CalculationResult(); + } + + private CalculationResult internalCalculate() { + List results = + new LinkedList(); + + if (yearEpoch.equals("year")) { + for (int i = 0; i < artifacts.length; i++) { + BedHeight[] pair = + getHeightPair(artifacts[i][0], artifacts[i][1], "single"); + BedDifferencesResult res = calculateYearDifference(pair); + results.add(res); + } + } + if (yearEpoch.equals("epoch")) { + for (int i = 0; i < artifacts.length; i++) { + BedHeight[] pair = + getHeightPair(artifacts[i][0], artifacts[i][1], "epoch"); + BedDifferencesResult res = calculateEpochDifference(pair); + results.add(res); + } + } + + return new CalculationResult( + results.toArray(new BedDifferencesResult[results.size()]), this); + } + + private BedHeight[] getHeightPair( + FLYSArtifact art1, + FLYSArtifact art2, + String type + ) { + int id1 = BedDifferencesAccess.getHeightId(art1); + int id2 = BedDifferencesAccess.getHeightId(art2); + + BedHeight[] heights = new BedHeight[2]; + heights[0] = BedHeightFactory.getHeight(type, id1, 0); + heights[1] = BedHeightFactory.getHeight(type, id2, 0); + return heights; + } + + private BedDiffEpochResult calculateEpochDifference(BedHeight[] pair) { + + TDoubleArrayList stations = pair[0].getStations(); + TDoubleArrayList diffRes = new TDoubleArrayList(); + TDoubleArrayList kms = new TDoubleArrayList(); + TDoubleArrayList morphs = new TDoubleArrayList(); + for (int i = 0; i < stations.size(); i++) { + if (!Double.isNaN(pair[0].getHeight(stations.get(i))) && + !Double.isNaN(pair[1].getHeight(stations.get(i)))) { + double hDiff = + pair[0].getHeight(stations.get(i)) - + pair[1].getHeight(stations.get(i)); + diffRes.add(hDiff); + kms.add(stations.get(i)); + if (pair[0].getMorphWidth(i) > pair[1].getMorphWidth(i)) { + morphs.add(pair[0].getMorphWidth(i)); + } + else { + morphs.add(pair[1].getMorphWidth(i)); + } + } + } + return new BedDiffEpochResult(kms, diffRes, morphs); + } + + private BedDiffYearResult calculateYearDifference(BedHeight[] pair) { + + TDoubleArrayList stations = pair[0].getStations(); + TDoubleArrayList diffRes = new TDoubleArrayList(); + TDoubleArrayList kms = new TDoubleArrayList(); + TDoubleArrayList morphs = new TDoubleArrayList(); + TDoubleArrayList heights = new TDoubleArrayList(); + TDoubleArrayList gap = new TDoubleArrayList(); + BedHeightSingle s1 = (BedHeightSingle)pair[0]; + BedHeightSingle s2 = (BedHeightSingle)pair[1]; + int range = s1.getYear() - s2.getYear(); + if (range < 0) { + range = range * -1; + } + for (int i = 0; i < stations.size(); i++) { + if (!Double.isNaN(pair[0].getHeight(stations.get(i))) && + !Double.isNaN(pair[1].getHeight(stations.get(i)))) { + double hDiff = + pair[0].getHeight(stations.get(i)) - + pair[1].getHeight(stations.get(i)); + diffRes.add(hDiff); + kms.add(stations.get(i)); + if (pair[0].getMorphWidth(i) > pair[1].getMorphWidth(i)) { + morphs.add(pair[0].getMorphWidth(i)); + } + else { + morphs.add(pair[1].getMorphWidth(i)); + } + if (pair[0].getDataGap(i) > pair[1].getDataGap(i)) { + gap.add(pair[0].getDataGap(i)); + } + else { + gap.add(pair[1].getDataGap(i)); + } + heights.add(hDiff / range); + } + } + return new BedDiffYearResult(kms, diffRes, morphs, heights, gap); + } +} diff -r 633fbb61a0cc -r a1c79d84e3cd flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/BedDiffEpochResult.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/BedDiffEpochResult.java Thu Sep 20 09:34:48 2012 +0000 @@ -0,0 +1,21 @@ +package de.intevation.flys.artifacts.model.minfo; + +import gnu.trove.TDoubleArrayList; + + +public class BedDiffEpochResult +extends BedDifferencesResult +{ + + public BedDiffEpochResult () { + super(); + } + + public BedDiffEpochResult ( + TDoubleArrayList kms, + TDoubleArrayList differences, + TDoubleArrayList morphWidth + ) { + super(kms, differences, morphWidth); + } +} diff -r 633fbb61a0cc -r a1c79d84e3cd flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/BedDiffYearResult.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/BedDiffYearResult.java Thu Sep 20 09:34:48 2012 +0000 @@ -0,0 +1,38 @@ +package de.intevation.flys.artifacts.model.minfo; + +import gnu.trove.TDoubleArrayList; + + +public class BedDiffYearResult +extends BedDifferencesResult +{ + + protected TDoubleArrayList bedHeights; + protected TDoubleArrayList dataGap; + + public BedDiffYearResult () { + super(); + this.bedHeights = new TDoubleArrayList(); + this.dataGap = new TDoubleArrayList(); + } + + public BedDiffYearResult( + TDoubleArrayList kms, + TDoubleArrayList differences, + TDoubleArrayList morphWidth, + TDoubleArrayList bedHeights, + TDoubleArrayList dataGap + ) { + super(kms, differences, morphWidth); + this.bedHeights = bedHeights; + this.dataGap = dataGap; + } + + public TDoubleArrayList getBedHeights() { + return this.bedHeights; + } + + public TDoubleArrayList getDataGap() { + return this.dataGap; + } +} diff -r 633fbb61a0cc -r a1c79d84e3cd flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/BedDifferencesResult.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/BedDifferencesResult.java Thu Sep 20 09:34:48 2012 +0000 @@ -0,0 +1,43 @@ +package de.intevation.flys.artifacts.model.minfo; + +import gnu.trove.TDoubleArrayList; + +import java.io.Serializable; + + +public class BedDifferencesResult +implements Serializable +{ + + protected TDoubleArrayList kms; + protected TDoubleArrayList differences; + protected TDoubleArrayList morphWidth; + + public BedDifferencesResult () { + kms = new TDoubleArrayList(); + differences = new TDoubleArrayList(); + morphWidth = new TDoubleArrayList(); + } + + public BedDifferencesResult( + TDoubleArrayList kms, + TDoubleArrayList differences, + TDoubleArrayList morpWidth + ) { + this.kms = kms; + this.differences = differences; + this.morphWidth = morpWidth; + } + + public TDoubleArrayList getKms() { + return this.kms; + } + + public TDoubleArrayList getDifferences() { + return this.differences; + } + + public TDoubleArrayList getMorphWidth() { + return this.morphWidth; + } +}