raimund@3028: package de.intevation.flys.artifacts.model.fixings; raimund@3028: raimund@3028: import org.apache.log4j.Logger; raimund@3028: raimund@3028: import de.intevation.artifacts.Artifact; raimund@3028: import de.intevation.artifacts.CallContext; raimund@3028: raimund@3035: import de.intevation.flys.utils.KMIndex; raimund@3035: raimund@3028: import de.intevation.flys.artifacts.FLYSArtifact; raimund@3035: import de.intevation.flys.artifacts.FixationArtifactAccess; raimund@3035: raimund@3028: import de.intevation.flys.artifacts.model.FacetTypes; raimund@3028: import de.intevation.flys.artifacts.model.DataFacet; raimund@3035: import de.intevation.flys.artifacts.model.CalculationResult; raimund@3035: import de.intevation.flys.artifacts.model.Parameters; raimund@3035: raimund@3035: import de.intevation.flys.artifacts.math.fitting.FunctionFactory; raimund@3035: import de.intevation.flys.artifacts.math.fitting.Function; raimund@3028: raimund@3028: import de.intevation.flys.artifacts.states.DefaultState.ComputeType; raimund@3028: raimund@3028: /** raimund@3028: * Facet to show the W|Q values. raimund@3028: * raimund@3028: * @author Raimund Renkert raimund@3028: */ raimund@3028: public class FixWQCurveFacet raimund@3028: extends DataFacet raimund@3028: implements FacetTypes { raimund@3028: raimund@3028: /** House logger. */ raimund@3028: private static Logger logger = Logger.getLogger(FixWQCurveFacet.class); raimund@3028: raimund@3028: /** Trivial Constructor. */ raimund@3028: public FixWQCurveFacet() { raimund@3028: } raimund@3028: raimund@3028: raimund@3028: /** raimund@3028: * @param name raimund@3028: */ raimund@3028: public FixWQCurveFacet(String description) { raimund@3028: super(0, FIX_WQ_CURVE, description, ComputeType.ADVANCE, null, null); raimund@3028: } raimund@3028: raimund@3028: raimund@3028: /** raimund@3028: * Returns the data this facet requires. raimund@3028: * raimund@3028: * @param artifact the owner artifact. raimund@3028: * @param context the CallContext (ignored). raimund@3028: * raimund@3028: * @return the data. raimund@3028: */ raimund@3028: @Override raimund@3028: public Object getData(Artifact artifact, CallContext context) { raimund@3028: logger.debug("FixWQCurveFacet.getData"); raimund@3035: if (artifact instanceof FLYSArtifact) { raimund@3035: FLYSArtifact flys = (FLYSArtifact)artifact; raimund@3035: FixationArtifactAccess access = new FixationArtifactAccess(flys); raimund@3035: raimund@3035: CalculationResult res = raimund@3035: (CalculationResult) flys.compute(context, raimund@3035: ComputeType.ADVANCE, raimund@3035: false); raimund@3035: raimund@3035: FixResult result = (FixResult) res.getData(); raimund@3035: raimund@3035: double km = access.getCurrentKm(); christian@3056: logger.debug("FixWQCurveFacet.getData: km = " + km); raimund@3035: raimund@3035: String function = access.getFunction(); raimund@3035: Function ff = FunctionFactory.getInstance().getFunction(function); raimund@3035: raimund@3035: Parameters params = result.getParameters(); christian@3056: String[] columnNames = params.getColumnNames(); christian@3056: for(String columnName : columnNames) { christian@3056: logger.debug("FixWQCurveFacet.getData: columnName = '" + columnName + "'"); christian@3056: } christian@3056: int row = params.binarySearch("km", km /*, Math.pow(10, -4)*/); //FIXME Use epsilon as soon access.getCurrentKm() is fixed christian@3056: if(row < 0) { christian@3056: row = -row - 1; christian@3056: logger.debug("getData: no direct hit in params.binarySearch"); christian@3056: } raimund@3035: String[] paramNames = ff.getParameterNames(); raimund@3035: int[] paramInd = params.columnIndices(paramNames); raimund@3035: double[] coeffs = new double[paramNames.length]; raimund@3035: params.get(row, paramInd, coeffs); raimund@3035: raimund@3035: de.intevation.flys.artifacts.math.Function mf = raimund@3035: ff.instantiate(coeffs); raimund@3035: christian@3056: double maxQ = getMaxQ(result, km); christian@3056: logger.debug("getData: maxQ = " + maxQ); raimund@3035: raimund@3035: FixFunction fix = new FixFunction( raimund@3035: ff.getName(), raimund@3035: ff.getDescription(), raimund@3035: mf, raimund@3035: maxQ); raimund@3035: raimund@3035: return fix; raimund@3035: } raimund@3035: else { raimund@3035: logger.debug("Not an instance of FixationArtifact."); raimund@3035: return null; raimund@3035: } raimund@3028: } raimund@3028: raimund@3028: raimund@3035: protected double getMaxQ(FixResult result, double km) { raimund@3035: double maxQ = 0; raimund@3035: raimund@3035: KMIndex kmQWRef = result.getReferenced(); raimund@3035: christian@3056: KMIndex.Entry qwEntry = kmQWRef.binarySearch(km); christian@3056: if(qwEntry != null) { christian@3056: QW[] qwRef = qwEntry.getValue(); christian@3056: if (qwRef != null) { christian@3056: for (int i = 0; i < qwRef.length; i++) { christian@3056: if (qwRef[i].getQ() > maxQ) { christian@3056: maxQ = qwRef[i].getQ(); christian@3056: } raimund@3035: } raimund@3035: } christian@3056: } else { christian@3056: logger.debug("getMaxQ: qwEntry is null, that's odd..."); raimund@3035: } raimund@3035: raimund@3035: KMIndex kmQWDAna = result.getAnalysisPeriods(); christian@3056: KMIndex.Entry periodsEntry = kmQWDAna.binarySearch(km); raimund@3035: christian@3056: if(periodsEntry != null) { christian@3056: AnalysisPeriod[] periods = kmQWDAna.binarySearch(km).getValue(); christian@3056: if(periods != null) { christian@3056: for (int i = 0; i < periods.length; i++) { christian@3056: QWD[] qwdAna = periods[i].getQWDs(); christian@3056: if (qwdAna != null) { christian@3056: for (int j = 0; j < qwdAna.length; j++) { christian@3056: if (qwdAna[j].getQ() > maxQ) { christian@3056: maxQ = qwdAna[j].getQ(); christian@3056: } raimund@3035: } raimund@3035: } raimund@3035: } raimund@3035: } christian@3056: } else { christian@3056: logger.debug("getMaxQ: periodsEntry is null, that's odd..."); raimund@3035: } raimund@3035: return maxQ; raimund@3035: } raimund@3035: raimund@3028: /** raimund@3028: * Create a deep copy of this Facet. raimund@3028: * @return a deep copy. raimund@3028: */ raimund@3028: @Override raimund@3028: public FixWQCurveFacet deepCopy() { raimund@3028: FixWQCurveFacet copy = new FixWQCurveFacet(); raimund@3028: copy.set(this); raimund@3028: return copy; raimund@3028: } raimund@3028: } raimund@3028: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :