teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: andre@8094: // TODO Aheinecke 15.8.2014 andre@8094: // This class was formerly known as SedimentLoadFacet. andre@8094: // This class could be a base for the calculated sediment loads. andre@8094: // If there is another facet for them remove this andre@8094: // class altogether. andre@8094: // andre@8094: // The new SedimentLoadFacet is a new StaticFacet which only wraps andre@8094: // static data from the database. andre@8094: teichmann@5831: package org.dive4elements.river.artifacts.model.minfo; teichmann@5778: teichmann@5831: import org.dive4elements.artifactdatabase.state.Facet; teichmann@5778: teichmann@5831: import org.dive4elements.artifacts.Artifact; teichmann@5831: import org.dive4elements.artifacts.CallContext; andre@8125: import org.dive4elements.artifacts.CallMeta; teichmann@5778: andre@8125: import org.dive4elements.river.artifacts.resources.Resources; teichmann@5867: import org.dive4elements.river.artifacts.D4EArtifact; teichmann@5778: teichmann@5831: import org.dive4elements.river.artifacts.model.CalculationResult; teichmann@5831: import org.dive4elements.river.artifacts.model.DataFacet; teichmann@5831: andre@8125: import org.dive4elements.river.artifacts.model.minfo.SedimentLoadDataResult.Fraction; andre@8125: teichmann@5831: import org.dive4elements.river.artifacts.states.DefaultState.ComputeType; teichmann@5831: felix@5645: import java.util.List; felix@5645: teichmann@5778: import org.apache.log4j.Logger; felix@5645: rrenkert@4372: felix@5647: /** Facet to access various sediment loads. */ andre@8094: public class SedimentLoadDataFacet rrenkert@4372: extends DataFacet rrenkert@4372: { felix@5645: /** Very own logger. */ andre@8094: private static Logger logger = Logger.getLogger(SedimentLoadDataFacet.class); rrenkert@4372: andre@8125: private static final String BASE_NAME = "sedimentload"; felix@5645: andre@8125: public String fraction; felix@5647: tom@8146: public String period; tom@8146: andre@8094: public SedimentLoadDataFacet() { rrenkert@4372: } rrenkert@4372: andre@8125: public SedimentLoadDataFacet(int idx, String fraction_name, String unit, tom@8146: String periodString, andre@8125: ComputeType type, String stateId, String hash, CallContext context) { andre@8125: super(idx, /*name*/"", ""/*description*/, type, hash, stateId); andre@8125: andre@8125: this.fraction = fraction_name; tom@8146: this.period = periodString; andre@8125: andre@8125: String typeUnit; /* Gnah someone should unify unit strings,... */ andre@8125: String i18nUnit; andre@8125: if (unit != null && unit.equals("m3/a")) { andre@8125: typeUnit = "m3a"; andre@8125: i18nUnit = "m\u00b3/a"; andre@8125: } else { andre@8125: typeUnit = "ta"; andre@8125: i18nUnit = "t/a"; andre@8125: } andre@8125: andre@8125: name = BASE_NAME + "." + typeUnit + "." + fraction_name; andre@8125: andre@8125: CallMeta meta = context.getMeta(); tom@8142: /* descriptions of real calculation results have to be distinguished tom@8142: from simple fractions from the database */ tom@8142: String isCalculated = fraction_name.equals("total") || tom@8142: fraction_name.equals("bed_load") || tom@8172: fraction_name.equals("bed_load_susp_sand") ? "calc." : ""; andre@8125: description = Resources.getMsg( andre@8125: meta, tom@8142: "facet.sedimentload." + isCalculated + fraction_name, tom@8146: new Object[] { periodString, i18nUnit }); andre@8125: andre@8125: /* Is this necessary? */ andre@8125: metaData.put("X", "chart.longitudinal.section.xaxis.label"); andre@8125: metaData.put("Y", ""); rrenkert@4372: } rrenkert@4372: felix@6679: @Override rrenkert@4372: public Object getData(Artifact artifact, CallContext context) { andre@8125: logger.debug("Get data for sediment load with fraction: " + fraction); rrenkert@4372: teichmann@5867: D4EArtifact flys = (D4EArtifact) artifact; rrenkert@4372: andre@8125: CalculationResult res = (CalculationResult) flys.compute(context, hash, stateId, type, false); andre@8125: Object payload = res.getData(); rrenkert@4372: andre@8125: if (!(payload instanceof SedimentLoadDataResult)) { andre@8125: logger.error("Invalid result!"); felix@5645: } felix@5645: andre@8125: SedimentLoadDataResult sdResult = (SedimentLoadDataResult) payload; felix@6382: tom@8146: List fractions = sdResult.getFractionsByNameAndPeriod( tom@8146: fraction, period tom@8146: ); andre@8132: if (fractions == null) { tom@8146: logger.warn("No result for fraction '" + fraction + tom@8146: "' and period '" + period + "'"); andre@8132: return null; felix@6382: } felix@6382: andre@8132: if (fractions.size() > 1) { tom@8146: logger.error("Multiple results for fraction '" + fraction + tom@8146: "' and period '" + period + "'"); andre@8132: } andre@8132: andre@8132: return fractions.get(0).getData(); felix@5645: andre@8125: /* Are gaps neccessary now? felix@5653: for (int x = 0; x < values[0].length-1; x++) { felix@5653: // Introduce gaps where no data in measurement station. felix@5653: if (Math.abs(values[0][x+1] - values[0][x]) > 3*EPSILON felix@5653: && values[1][x+1] != values[1][x]) { felix@5653: values[0][x] = Double.NaN; felix@5653: values[1][x] = Double.NaN; felix@5645: } felix@5645: } felix@5653: andre@8125: return values; */ felix@5648: } felix@5648: rrenkert@4372: /** Copy deeply. */ rrenkert@4372: @Override rrenkert@4372: public Facet deepCopy() { andre@8094: SedimentLoadDataFacet copy = new SedimentLoadDataFacet(); rrenkert@4372: copy.set(this); rrenkert@4372: copy.type = type; rrenkert@4372: copy.hash = hash; rrenkert@4372: copy.stateId = stateId; andre@8125: copy.fraction = fraction; rrenkert@4372: return copy; rrenkert@4372: } rrenkert@4372: } rrenkert@4372: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :