view flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/fixings/FixCalculation.java @ 3434:1a636be7612b

FixA: Moved more common calculation code into base class. flys-artifacts/trunk@5097 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 22 Jul 2012 09:49:56 +0000
parents da7cf0e3ccaa
children 262e7d7e58fe
line wrap: on
line source
package de.intevation.flys.artifacts.model.fixings;

import de.intevation.flys.artifacts.access.FixAccess;

import de.intevation.flys.artifacts.model.Calculation;
import de.intevation.flys.artifacts.model.FixingsColumn;
import de.intevation.flys.artifacts.model.FixingsColumnFactory;

import de.intevation.flys.artifacts.model.FixingsOverview.Fixing.Filter;

import de.intevation.flys.artifacts.model.FixingsOverview.Fixing;
import de.intevation.flys.artifacts.model.FixingsOverview.IdsFilter;

import de.intevation.flys.artifacts.model.FixingsOverview;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

public class FixCalculation
extends      Calculation
{
    private static Logger log = Logger.getLogger(FixCalculation.class);

    /** Helper class to bundle the meta information of a column
     *  and the real data.
     */
    protected static class Column {

        protected Fixing.Column meta;
        protected FixingsColumn data;

        public Column() {
        }

        public Column(Fixing.Column meta, FixingsColumn data) {
            this.meta = meta;
            this.data = data;
        }

        public Date getDate() {
            return meta.getStartTime();
        }

        public String getDescription() {
            return meta.getDescription();
        }

        public boolean getQW(
            double    km,
            double [] qs,
            double [] ws,
            int       index
        ) {
            qs[index] = data.getQ(km);
            return data.getW(km, ws, index);
        }

        public boolean getQW(double km, double [] wq) {
            data.getW(km, wq, 0);
            if (Double.isNaN(wq[0])) return false;
            wq[1] = data.getQ(km);
            return !Double.isNaN(wq[1]);
        }
    } // class Column

    /**
     * Helper class to find the data belonging to meta info more quickly.
     */
    protected static class ColumnCache {

        protected Map<Integer, Column> columns;

        public ColumnCache() {
            columns = new HashMap<Integer, Column>();
        }

        public Column getColumn(Fixing.Column meta) {
            Integer key = meta.getId();
            Column column = columns.get(key);
            if (column == null) {
                FixingsColumn data = FixingsColumnFactory
                    .getInstance()
                    .getColumnData(meta);
                if (data != null) {
                    column = new Column(meta, data);
                    columns.put(key, column);
                }
            }
            return column;
        }
    } // class ColumnCache


    protected String  river;
    protected double  from;
    protected double  to;
    protected double  step;
    protected boolean preprocessing;
    protected String  function;
    protected int []  events;
    protected int     qSectorStart;
    protected int     qSectorEnd;

    public FixCalculation() {
    }

    public FixCalculation(FixAccess access) {
        String       river           = access.getRiver();
        Double       from            = access.getFrom();
        Double       to              = access.getTo();
        Double       step            = access.getStep();
        String       function        = access.getFunction();
        int []       events          = access.getEvents();
        Integer      qSectorStart    = access.getQSectorStart();
        Integer      qSectorEnd      = access.getQSectorEnd();
        Boolean      preprocessing   = access.getPreprocessing();

        if (river == null) {
            addProblem("fix.missing.river");
        }

        if (from == null) {
            addProblem("fix.missing.from");
        }

        if (to == null) {
            addProblem("fix.missing.to");
        }

        if (step == null) {
            addProblem("fix.missing.step");
        }

        if (function == null) {
            addProblem("fix.missing.function");
        }

        if (events == null || events.length < 1) {
            addProblem("fix.missing.events");
        }

        if (qSectorStart == null) {
            addProblem("fix.missing.qstart.sector");
        }

        if (qSectorEnd == null) {
            addProblem("fix.missing.qend.sector");
        }

        if (preprocessing == null) {
            addProblem("fix.missing.preprocessing");
        }

        if (!hasProblems()) {
            this.river         = river;
            this.from          = from;
            this.to            = to;
            this.step          = step;
            this.function      = function;
            this.events        = events;
            this.qSectorStart  = qSectorStart;
            this.qSectorEnd    = qSectorEnd;
            this.preprocessing = preprocessing;
        }
    }

    protected static String toString(
        String [] parameterNames,
        double [] values
    ) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < parameterNames.length; ++i) {
            if (i > 0) sb.append(", ");
            sb.append(parameterNames[i]).append(": ").append(values[i]);
        }
        return sb.toString();
    }

    protected Filter createFilter() {
        return new IdsFilter(events);
    }

    protected List<Column> getEventColumns(FixingsOverview overview) {

        FixingsColumnFactory fcf = FixingsColumnFactory.getInstance();

        Filter filter = createFilter();

        List<Fixing.Column> metas = overview.filter(null, filter);

        List<Column> columns = new ArrayList<Column>(metas.size());

        for (Fixing.Column meta: metas) {

            FixingsColumn data = fcf.getColumnData(meta);
            if (data == null) {
                addProblem("fix.cannot.load.data");
            }
            else {
                columns.add(new Column(meta, data));
            }
        }

        return columns;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org