view flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DischargeTables.java @ 155:fbbb22e1e206

Changed the background color of discharge curves to white. flys-artifacts/trunk@1597 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 28 Mar 2011 16:36:13 +0000
parents 03362976fa0f
children ea3a438a7021
line wrap: on
line source
package de.intevation.flys.artifacts.model;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Arrays;

import java.io.Serializable;

import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Query;

import org.apache.log4j.Logger;

import de.intevation.flys.backend.SessionFactoryProvider;

import de.intevation.flys.model.Gauge;
import de.intevation.flys.model.DischargeTable;
import de.intevation.flys.model.DischargeTableValue;

public class DischargeTables
implements   Serializable
{
    private static Logger log = Logger.getLogger(DischargeTables.class);

    protected List<String> gaugeNames;

    protected String riverName;

    protected double scale;

    protected Map<String, double [][]> values;

    public DischargeTables() {
    }

    public DischargeTables(String riverName, String [] gaugeNames) {
        this(riverName, Arrays.asList(gaugeNames));
    }

    public DischargeTables(String riverName, List<String> gaugeNames) {
        scale           = Double.NaN;
        this.riverName  = riverName;
        this.gaugeNames = gaugeNames;
    }

    public Map<String, double [][]> getValues(double scale) {
        if (values == null || scale != this.scale) {
            values = loadValues(scale);
            this.scale = scale;
        }
        return values;
    }

    protected Map<String, double [][]> loadValues(double scale) {
        Map<String, double [][]> values = new HashMap<String, double [][]>();

        SessionFactory sf = SessionFactoryProvider.getSessionFactory();
        Session session = sf.openSession();

        try {
            Query gaugeQuery = session.createQuery(
                "from Gauge where name=:gauge and river.name=:river");
            gaugeQuery.setParameter("river", riverName);

            for (String gaugeName: gaugeNames) {
                gaugeQuery.setParameter("gauge", gaugeName);
                List<Gauge> gauges = gaugeQuery.list();
                if (gauges.isEmpty()) {
                    log.warn(
                        "no gauge '"+gaugeName+"' at river '"+riverName+"'");
                    continue;
                }
                Gauge gauge = gauges.get(0);

                List<DischargeTable> tables = gauge.getDischargeTables();

                if (tables.isEmpty()) {
                    log.warn(
                        "no discharge table for gauge '" + gaugeName + "'");
                    continue;
                }

                // TODO: Filter by time interval
                DischargeTable table = tables.get(0);

                List<DischargeTableValue> dtvs =
                    table.getDischargeTableValues();

                double [][] vs = new double[2][dtvs.size()];

                int idx = 0;
                for (DischargeTableValue dtv: dtvs) {
                    vs[0][idx] = dtv.getQ().doubleValue() * scale;
                    vs[1][idx] = dtv.getW().doubleValue() * scale;
                    ++idx;
                }

                values.put(gaugeName, vs);
            }

            return values;
        }
        finally {
            session.close();
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org