Mercurial > dive4elements > river
changeset 8085:75e62ea0e307
Added the broken calculation state of the new sediment load calc.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Mon, 11 Aug 2014 17:18:36 +0200 |
parents | 640fffb0ad60 |
children | 7ede7904c8a5 |
files | artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/SedimentLoadDataCalculate.java |
diffstat | 1 files changed, 625 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/SedimentLoadDataCalculate.java Mon Aug 11 17:18:36 2014 +0200 @@ -0,0 +1,625 @@ +/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde + * Software engineering by Intevation GmbH + * + * This file is Free Software under the GNU AGPL (>=v3) + * and comes with ABSOLUTELY NO WARRANTY! Check out the + * documentation coming with Dive4Elements River for details. + */ + +package org.dive4elements.river.artifacts.states.minfo; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.apache.log4j.Logger; +import org.dive4elements.artifactdatabase.state.Facet; +import org.dive4elements.artifactdatabase.state.FacetActivity; +import org.dive4elements.artifacts.Artifact; +import org.dive4elements.artifacts.CallContext; +import org.dive4elements.artifacts.CallMeta; +import org.dive4elements.river.artifacts.D4EArtifact; +import org.dive4elements.river.artifacts.access.SedimentLoadAccess; +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.SedimentLoadDataCalculation; +import org.dive4elements.river.artifacts.model.minfo.SedimentLoadLSData; +import org.dive4elements.river.artifacts.model.minfo.SedimentLoadFacet; +import org.dive4elements.river.artifacts.model.minfo.SedimentLoadFactory; +import org.dive4elements.river.artifacts.model.minfo.SedimentLoadResult; +import org.dive4elements.river.artifacts.model.minfo.SedimentLoadUnknownFacet; +import org.dive4elements.river.artifacts.resources.Resources; +import org.dive4elements.river.artifacts.states.DefaultState; + +/** State in which Sediment Load(s) are calculated/retrieved. */ +public class SedimentLoadDataCalculate +extends DefaultState +implements FacetTypes +{ + + private static final long serialVersionUID = 1L; + + private static final Logger logger = Logger + .getLogger(SedimentLoadCalculate.class); + + public static final String I18N_FACET_SEDIMENTLOAD_COARSE = "facet.sedimentload.coarse"; + public static final String I18N_FACET_SEDIMENTLOAD_SAND = "facet.sedimentload.sand"; + public static final String I18N_FACET_SEDIMENTLOAD_FINE_MIDDLE = "facet.sedimentload.fine_middle"; + public static final String I18N_FACET_SEDIMENTLOAD_SUSPSAND = "facet.sedimentload.susp_sand"; + public static final String I18N_FACET_SEDIMENTLOAD_SUSPSANDBED = "facet.sediemntload.susp_sand_bed"; + public static final String I18N_FACET_SEDIMENTLOAD_SUSPSEDIMENT = "facet.sedimentload.susp_sediment"; + public static final String I18N_FACET_SEDIMENTLOAD_TOTAL_LOAD = "facet.sedimentload.total_load"; + public static final String I18N_FACET_SEDIMENTLOAD_TOTAL = "facet.sedimentload.total"; + + static { + // Active/deactivate facets. + FacetActivity.Registry.getInstance().register( + "minfo", + new FacetActivity() { + @Override + public Boolean isInitialActive( + Artifact artifact, + Facet facet, + String output + ) { + return null; + //XXX: This is all broken. + /* + String name = facet.getName(); + if (name.equals(SEDIMENT_LOAD_TA_COARSE) || + name.equals(SEDIMENT_LOAD_TA_FINEMIDDLE) || + name.equals(SEDIMENT_LOAD_TA_SAND) || + name.equals(SEDIMENT_LOAD_TA_SUSP_SAND) || + name.equals(SEDIMENT_LOAD_TA_SUSP_SEDIMENT) || + name.equals(SEDIMENT_LOAD_TA_SUSP_SAND_BED) || + name.equals(SEDIMENT_LOAD_M3A_COARSE) || + name.equals(SEDIMENT_LOAD_M3A_FINEMIDDLE) || + name.equals(SEDIMENT_LOAD_M3A_SAND) || + name.equals(SEDIMENT_LOAD_M3A_SUSP_SAND) || + name.equals(SEDIMENT_LOAD_M3A_SUSP_SEDIMENT) || + name.equals(SEDIMENT_LOAD_M3A_SUSP_SAND_BED)){ + return Boolean.FALSE; + } + else if (name.equals(SEDIMENT_LOAD_TA_UNKNOWN) + || name.equals(SEDIMENT_LOAD_M3A_UNKNOWN)) { + D4EArtifact d4e = (D4EArtifact)artifact; + SedimentLoadUnknownFacet f = + (SedimentLoadUnknownFacet) + d4e.getNativeFacet(facet, null); + SedimentLoadLSData load = + (SedimentLoadLSData)f.getData(artifact, null); + SedimentLoadAccess access = + new SedimentLoadAccess(d4e); + List<int[]> dates = new ArrayList<int[]>(); + if (access.getYearEpoch().equals("year")) { + dates.add(access.getPeriod()); + } + else { + int[][] epochs = access.getEpochs(); + for (int i = 0; i < epochs.length; i++) { + dates.add(epochs[i]); + } + } + for (int[] date: dates) { + try { + Date s = + DateUtil.getStartDateFromYear(date[0]); + Date e = + DateUtil.getEndDateFromYear(date[1]); + if (!(s.after(load.getEnd()) || + e.before(load.getStart()))) { + return Boolean.TRUE; + } + } + catch (IllegalArgumentException iae) { + return Boolean.FALSE; + } + } + return Boolean.FALSE; + } + else { + return null; + } + */ + } + }); + } + + public SedimentLoadDataCalculate() { + } + + @Override + public Object computeAdvance(D4EArtifact artifact, String hash, + CallContext context, List<Facet> facets, Object old) { + logger.debug("SedimentLoadDataCalculate.computeAdvance"); + + List<Facet> newFacets = new ArrayList<Facet>(); + + SedimentLoadAccess access = new SedimentLoadAccess(artifact); + + CalculationResult res = old instanceof CalculationResult + ? (CalculationResult) old + : new SedimentLoadDataCalculation().calculate(access); + + if (facets == null || res == null) { + return res; + } + + /* XXX: THIS IS ALL BROKEN + + SedimentLoadResult[] results = (SedimentLoadResult[]) res.getData(); + + if (results == null || results.length == 0) { + logger.warn("Calculation computed no results!"); + return res; + } + + String type = access.getYearEpoch(); + if (type.equals("year")) { + generateYearFacets(context, newFacets, results, getID(), hash, access); + } + else if (type.equals("epoch")) { + generateEpochFacets(context, newFacets, results, getID(), hash, access); + } + else if (type.equals("off_epoch")) { + generateOffEpochFacets(context, newFacets, results, getID(), hash, access); + } + logger.debug("Created " + newFacets.size() + " new Facets."); + + String river = access.getRiverName(); + SedimentLoadLSData[] unknown = + SedimentLoadFactory.getSedimentLoadUnknown(river, + access.getUnit().replace("_per_","/"), type); + + boolean isUnitTA = access.getUnit().startsWith("t"); + + if (res.getReport().hasProblems()) { + newFacets.add(new ReportFacet(ComputeType.ADVANCE, hash, id)); + } + + for (int i = 0; i < unknown.length; i++) { + newFacets.add(new SedimentLoadUnknownFacet( + i, + (isUnitTA)? SEDIMENT_LOAD_TA_UNKNOWN:SEDIMENT_LOAD_M3A_UNKNOWN, + unknown[i].getDescription(), + ComputeType.ADVANCE, + getID(), + hash)); + } + + newFacets.add( + new DataFacet(CSV, "CSV data", ComputeType.ADVANCE, hash, id)); + + facets.addAll(newFacets); + + */ + + return res; + } + + /* + private String facetNameInfo(SedimentLoadResult result, String unit) { + return " - " + result.getStartYear() + " " + unit; + } + + protected void generateYearFacets(CallContext context, + List<Facet> newFacets, + SedimentLoadResult[] results, + String stateId, + String hash, + SedimentLoadAccess access + ) { + logger.debug("SedimentLoadCalculate.generateFacets " + access.getUnit()); + + CallMeta meta = context.getMeta(); + boolean isUnitTA = access.getUnit().startsWith("t"); + String unit = (isUnitTA) + ? Resources.getMsg(context.getMeta(), "state.minfo.sediment.load.t_per_a", "(t/a)") + : Resources.getMsg(context.getMeta(), "state.minfo.sediment.load.m3_per_a", "(m^3/a)"); + +// newFacets.add(new DataFacet(CSV, "CSV data", ComputeType.ADVANCE, hash, id)); + for (int idx = 0; idx < results.length; idx++) { + SedimentLoadResult res = results[idx]; + if (res.hasCoarseData()) { + newFacets.add(new SedimentLoadFacet( + idx, + (isUnitTA) + ?SEDIMENT_LOAD_TA_COARSE + :SEDIMENT_LOAD_M3A_COARSE, + Resources.getMsg( + meta, + I18N_FACET_SEDIMENTLOAD_COARSE, + I18N_FACET_SEDIMENTLOAD_COARSE) + + facetNameInfo(res, unit), + ComputeType.ADVANCE, + stateId, + hash)); + } + if (res.hasSandData()) { + newFacets.add(new SedimentLoadFacet( + idx, + (isUnitTA) + ?SEDIMENT_LOAD_TA_SAND + :SEDIMENT_LOAD_M3A_SAND, + Resources.getMsg( + meta, + I18N_FACET_SEDIMENTLOAD_SAND, + I18N_FACET_SEDIMENTLOAD_SAND) + + facetNameInfo(res, unit), + ComputeType.ADVANCE, + stateId, + hash)); + } + if (res.hasFineMiddleData()) { + newFacets.add(new SedimentLoadFacet( + idx, + (isUnitTA) + ?SEDIMENT_LOAD_TA_FINEMIDDLE + :SEDIMENT_LOAD_M3A_FINEMIDDLE, + Resources.getMsg( + meta, + I18N_FACET_SEDIMENTLOAD_FINE_MIDDLE, + I18N_FACET_SEDIMENTLOAD_FINE_MIDDLE) + + facetNameInfo(res, unit), + ComputeType.ADVANCE, + stateId, + hash)); + } + if (res.hasSuspSandData()) { + newFacets.add(new SedimentLoadFacet( + idx, + (isUnitTA) + ?SEDIMENT_LOAD_TA_SUSP_SAND + :SEDIMENT_LOAD_M3A_SUSP_SAND, + Resources.getMsg( + meta, + I18N_FACET_SEDIMENTLOAD_SUSPSAND, + I18N_FACET_SEDIMENTLOAD_SUSPSAND) + + facetNameInfo(res, unit), + ComputeType.ADVANCE, + stateId, + hash)); + } + if (res.hasSuspSandBedData()) { + newFacets.add(new SedimentLoadFacet( + idx, + (isUnitTA) + ?SEDIMENT_LOAD_TA_SUSP_SAND_BED + :SEDIMENT_LOAD_M3A_SUSP_SAND_BED, + Resources.getMsg( + meta, + I18N_FACET_SEDIMENTLOAD_SUSPSANDBED, + I18N_FACET_SEDIMENTLOAD_SUSPSANDBED) + + facetNameInfo(res, unit), + ComputeType.ADVANCE, + stateId, + hash)); + } + if (res.hasSuspSedimentData()) { + newFacets.add(new SedimentLoadFacet( + idx, + (isUnitTA) + ?SEDIMENT_LOAD_TA_SUSP_SEDIMENT + :SEDIMENT_LOAD_M3A_SUSP_SEDIMENT, + Resources.getMsg( + meta, + I18N_FACET_SEDIMENTLOAD_SUSPSEDIMENT, + I18N_FACET_SEDIMENTLOAD_SUSPSEDIMENT) + + facetNameInfo(res, unit), + ComputeType.ADVANCE, + stateId, + hash)); + + } + if (res.hasTotalData()) { + newFacets.add(new SedimentLoadFacet( + idx, + (isUnitTA) + ?SEDIMENT_LOAD_TA_TOTAL + :SEDIMENT_LOAD_M3A_TOTAL, + Resources.getMsg( + meta, + I18N_FACET_SEDIMENTLOAD_TOTAL, + I18N_FACET_SEDIMENTLOAD_TOTAL) + + facetNameInfo(res, unit), + ComputeType.ADVANCE, + stateId, + hash)); + } + } + } + + private String epochFacetNameInfo(SedimentLoadResult result, String unit) { + return " - " + result.getStartYear() + "-" + result.getEndYear() + " " + unit; + } + + protected void generateEpochFacets( + CallContext context, + List<Facet> newFacets, + SedimentLoadResult[] results, + String stateId, + String hash, + SedimentLoadAccess access + ) { + logger.debug("SedimentLoadCalculate.generateEpochFacets "); + + CallMeta meta = context.getMeta(); + boolean isUnitTA = access.getUnit().startsWith("t"); + String unit = (isUnitTA) + ? Resources.getMsg(context.getMeta(), "state.minfo.sediment.load.t_per_a", "(t/a)") + : Resources.getMsg(context.getMeta(), "state.minfo.sediment.load.m3_per_a", "(m^3/a)"); + +// newFacets.add(new DataFacet(CSV, "CSV data", ComputeType.ADVANCE, hash, id)); + for (int idx = 0; idx < results.length; idx++) { + SedimentLoadResult res = results[idx]; + if (res.hasCoarseData()) { + newFacets.add(new SedimentLoadFacet( + idx, + (isUnitTA) + ?SEDIMENT_LOAD_TA_COARSE + :SEDIMENT_LOAD_M3A_COARSE, + Resources.getMsg( + meta, + I18N_FACET_SEDIMENTLOAD_COARSE, + I18N_FACET_SEDIMENTLOAD_COARSE) + + epochFacetNameInfo(res,unit), + ComputeType.ADVANCE, + stateId, + hash)); + } + if (res.hasSandData()) { + newFacets.add(new SedimentLoadFacet( + idx, + (isUnitTA) + ?SEDIMENT_LOAD_TA_SAND + :SEDIMENT_LOAD_M3A_SAND, + Resources.getMsg( + meta, + I18N_FACET_SEDIMENTLOAD_SAND, + I18N_FACET_SEDIMENTLOAD_SAND) + + epochFacetNameInfo(res,unit), + ComputeType.ADVANCE, + stateId, + hash)); + } + if (res.hasFineMiddleData()) { + newFacets.add(new SedimentLoadFacet( + idx, + (isUnitTA) + ?SEDIMENT_LOAD_TA_FINEMIDDLE + :SEDIMENT_LOAD_M3A_FINEMIDDLE, + Resources.getMsg( + meta, + I18N_FACET_SEDIMENTLOAD_FINE_MIDDLE, + I18N_FACET_SEDIMENTLOAD_FINE_MIDDLE) + + epochFacetNameInfo(res,unit), + ComputeType.ADVANCE, + stateId, + hash)); + } + if (res.hasSuspSandData()) { + newFacets.add(new SedimentLoadFacet( + idx, + (isUnitTA) + ?SEDIMENT_LOAD_TA_SUSP_SAND + :SEDIMENT_LOAD_M3A_SUSP_SAND, + Resources.getMsg( + meta, + I18N_FACET_SEDIMENTLOAD_SUSPSAND, + I18N_FACET_SEDIMENTLOAD_SUSPSAND) + + epochFacetNameInfo(res,unit), + ComputeType.ADVANCE, + stateId, + hash)); + } + if (res.hasSuspSandBedData()) { + newFacets.add(new SedimentLoadFacet( + idx, + (isUnitTA) + ?SEDIMENT_LOAD_TA_SUSP_SAND_BED + :SEDIMENT_LOAD_M3A_SUSP_SAND, + Resources.getMsg( + meta, + I18N_FACET_SEDIMENTLOAD_SUSPSANDBED, + I18N_FACET_SEDIMENTLOAD_SUSPSANDBED) + + epochFacetNameInfo(res,unit), + ComputeType.ADVANCE, + stateId, + hash)); + } + if (res.hasSuspSedimentData()) { + newFacets.add(new SedimentLoadFacet( + idx, + (isUnitTA) + ?SEDIMENT_LOAD_TA_SUSP_SEDIMENT + :SEDIMENT_LOAD_M3A_SUSP_SEDIMENT, + Resources.getMsg( + meta, + I18N_FACET_SEDIMENTLOAD_SUSPSEDIMENT, + I18N_FACET_SEDIMENTLOAD_SUSPSEDIMENT) + + epochFacetNameInfo(res,unit), + ComputeType.ADVANCE, + stateId, + hash)); + + } + if (res.hasTotalLoadData()) { + newFacets.add(new SedimentLoadFacet( + idx, + (isUnitTA) + ?SEDIMENT_LOAD_TA_TOTAL_LOAD + :SEDIMENT_LOAD_M3A_TOTAL_LOAD, + Resources.getMsg( + meta, + I18N_FACET_SEDIMENTLOAD_TOTAL_LOAD, + I18N_FACET_SEDIMENTLOAD_TOTAL_LOAD) + + epochFacetNameInfo(res,unit), + ComputeType.ADVANCE, + stateId, + hash)); + } + if (res.hasTotalData()) { + newFacets.add(new SedimentLoadFacet( + idx, + (isUnitTA) + ?SEDIMENT_LOAD_TA_TOTAL + :SEDIMENT_LOAD_M3A_TOTAL, + Resources.getMsg( + meta, + I18N_FACET_SEDIMENTLOAD_TOTAL, + I18N_FACET_SEDIMENTLOAD_TOTAL) + + epochFacetNameInfo(res,unit), + ComputeType.ADVANCE, + stateId, + hash)); + } + } + } + + protected void generateOffEpochFacets( + CallContext context, + List<Facet> newFacets, + SedimentLoadResult[] results, + String stateId, + String hash, + SedimentLoadAccess access + ) { + logger.debug("SedimentLoadCalculate.generateOffEpochFacets"); + + CallMeta meta = context.getMeta(); + boolean isUnitTA = access.getUnit().startsWith("t"); + String unit = (isUnitTA) + ? Resources.getMsg(context.getMeta(), "state.minfo.sediment.load.t_per_a", "(t/a)") + : Resources.getMsg(context.getMeta(), "state.minfo.sediment.load.m3_per_a", "(m^3/a)"); + +// newFacets.add(new DataFacet(CSV, "CSV data", ComputeType.ADVANCE, hash, id)); + for (int idx = 0; idx < results.length; idx++) { + SedimentLoadResult res = results[idx]; + if (res.hasCoarseData()) { + newFacets.add(new SedimentLoadFacet( + idx, + (isUnitTA) + ?SEDIMENT_LOAD_TA_COARSE + :SEDIMENT_LOAD_M3A_COARSE, + Resources.getMsg( + meta, + I18N_FACET_SEDIMENTLOAD_COARSE, + I18N_FACET_SEDIMENTLOAD_COARSE) + + epochFacetNameInfo(res,unit), + ComputeType.ADVANCE, + stateId, + hash)); + } + if (res.hasSandData()) { + newFacets.add(new SedimentLoadFacet( + idx, + (isUnitTA) + ?SEDIMENT_LOAD_TA_SAND + :SEDIMENT_LOAD_M3A_SAND, + Resources.getMsg( + meta, + I18N_FACET_SEDIMENTLOAD_SAND, + I18N_FACET_SEDIMENTLOAD_SAND) + + epochFacetNameInfo(res,unit), + ComputeType.ADVANCE, + stateId, + hash)); + } + if (res.hasFineMiddleData()) { + newFacets.add(new SedimentLoadFacet( + idx, + (isUnitTA) + ?SEDIMENT_LOAD_TA_FINEMIDDLE + :SEDIMENT_LOAD_M3A_FINEMIDDLE, + Resources.getMsg( + meta, + I18N_FACET_SEDIMENTLOAD_FINE_MIDDLE, + I18N_FACET_SEDIMENTLOAD_FINE_MIDDLE) + + epochFacetNameInfo(res,unit), + ComputeType.ADVANCE, + stateId, + hash)); + } + if (res.hasSuspSandData()) { + newFacets.add(new SedimentLoadFacet( + idx, + (isUnitTA) + ?SEDIMENT_LOAD_TA_SUSP_SAND + :SEDIMENT_LOAD_M3A_SUSP_SAND, + Resources.getMsg( + meta, + I18N_FACET_SEDIMENTLOAD_SUSPSAND, + I18N_FACET_SEDIMENTLOAD_SUSPSAND) + + epochFacetNameInfo(res,unit), + ComputeType.ADVANCE, + stateId, + hash)); + } + if (res.hasSuspSandBedData()) { + newFacets.add(new SedimentLoadFacet( + idx, + (isUnitTA) + ?SEDIMENT_LOAD_TA_SUSP_SAND_BED + :SEDIMENT_LOAD_M3A_SUSP_SAND_BED, + Resources.getMsg( + meta, + I18N_FACET_SEDIMENTLOAD_SUSPSANDBED, + I18N_FACET_SEDIMENTLOAD_SUSPSANDBED) + + epochFacetNameInfo(res,unit), + ComputeType.ADVANCE, + stateId, + hash)); + } + if (res.hasSuspSedimentData()) { + newFacets.add(new SedimentLoadFacet( + idx, + (isUnitTA) + ?SEDIMENT_LOAD_TA_SUSP_SEDIMENT + :SEDIMENT_LOAD_M3A_SUSP_SEDIMENT, + Resources.getMsg( + meta, + I18N_FACET_SEDIMENTLOAD_SUSPSEDIMENT, + I18N_FACET_SEDIMENTLOAD_SUSPSEDIMENT) + + epochFacetNameInfo(res,unit), + ComputeType.ADVANCE, + stateId, + hash)); + + } + if (res.hasTotalLoadData()) { + newFacets.add(new SedimentLoadFacet( + idx, + (isUnitTA) + ?SEDIMENT_LOAD_TA_TOTAL_LOAD + :SEDIMENT_LOAD_M3A_TOTAL_LOAD, + Resources.getMsg( + meta, + I18N_FACET_SEDIMENTLOAD_TOTAL_LOAD, + I18N_FACET_SEDIMENTLOAD_TOTAL_LOAD) + + epochFacetNameInfo(res,unit), + ComputeType.ADVANCE, + stateId, + hash)); + } + if (res.hasTotalData()) { + newFacets.add(new SedimentLoadFacet( + idx, + (isUnitTA) + ?SEDIMENT_LOAD_TA_TOTAL + :SEDIMENT_LOAD_M3A_TOTAL, + Resources.getMsg( + meta, + I18N_FACET_SEDIMENTLOAD_TOTAL, + I18N_FACET_SEDIMENTLOAD_TOTAL) + + epochFacetNameInfo(res,unit), + ComputeType.ADVANCE, + stateId, + hash)); + } + } + } +*/ +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :