changeset 5501:9a14eac637ec

New Artifact and static state for sq relation.
author Raimund Renkert <rrenkert@intevation.de>
date Thu, 28 Mar 2013 15:17:02 +0100
parents 700ac898ab0b
children 566eea137330
files flys-artifacts/doc/conf/artifacts/sqrelation.xml flys-artifacts/doc/conf/conf.xml flys-artifacts/src/main/java/de/intevation/flys/artifacts/SQRelationArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/access/StaticSQRelationAccess.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/sq/SQStaticState.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/sq/StaticSQRelationFacet.java
diffstat 6 files changed, 384 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/doc/conf/artifacts/sqrelation.xml	Thu Mar 28 15:17:02 2013 +0100
@@ -0,0 +1,58 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<artifact name="staticsqrelation">
+    <states>
+         <state id="state.sqrelation.static" description="state.sqrelation.static" state="de.intevation.flys.artifacts.states.sq.SQStaticState">
+            <data name="river" type="String" />
+            <data name="station" type="String" />
+            <outputmodes>
+                <outputmode name="sq_relation_a" description="output.sq_relation" type="chart">
+                    <facets>
+                        <facet name="sq_a_curve" description="A facet for sq curve"/>
+                        <facet name="sq_relation_a.manualpoints" />
+                    </facets>
+                </outputmode>
+                <outputmode name="sq_relation_b" description="output.sq_relation" type="chart">
+                    <facets>
+                        <facet name="sq_b_curve" description="A facet for sq curve"/>
+                        <facet name="sq_relation_b.manualpoints" />
+                    </facets>
+                </outputmode>
+                <outputmode name="sq_relation_c" description="output.sq_relation" type="chart">
+                    <facets>
+                        <facet name="sq_c_curve" description="A facet for sq curve"/>
+                        <facet name="sq_relation_c.manualpoints" />
+                    </facets>
+                </outputmode>
+                <outputmode name="sq_relation_d" description="output.sq_relation" type="chart">
+                    <facets>
+                        <facet name="sq_d_curve" description="A facet for sq curve"/>
+                        <facet name="sq_relation_d.manualpoints" />
+                    </facets>
+                </outputmode>
+                <outputmode name="sq_relation_e" description="output.sq_relation" type="chart">
+                    <facets>
+                        <facet name="sq_e_curve" description="A facet for sq curve"/>
+                        <facet name="sq_relation_e.manualpoints" />
+                    </facets>
+                </outputmode>
+                <outputmode name="sq_relation_f" description="output.sq_relation" type="chart">
+                    <facets>
+                        <facet name="sq_relation_f.manualpoints" />
+                        <facet name="sq_f_curve" description="A facet for sq curve"/>
+                    </facets>
+                </outputmode>
+                <outputmode name="sq_overview" description="output.sq_overview" type="overview">
+                    <facets>
+                        <facet name="sq_chart_overview" description="A facet for sq chart overview"/>
+                    </facets>
+                </outputmode>
+                <outputmode name="sqrelation_export" description="output.sqrelation_export" mime-type="text/plain" type="export">
+                    <facets>
+                        <facet name="csv" description="facet.sqrelation_export.csv" />
+                        <facet name="pdf" description="facet.sqrelation_export.pdf" />
+                    </facets>
+                </outputmode>
+            </outputmodes>
+         </state>
+    </states>
+</artifact>
--- a/flys-artifacts/doc/conf/conf.xml	Thu Mar 28 15:15:33 2013 +0100
+++ b/flys-artifacts/doc/conf/conf.xml	Thu Mar 28 15:17:02 2013 +0100
@@ -122,6 +122,9 @@
             <artifact-factory name="bedheight" description="Factory to create an artifact used in minfo datacage."
                 ttl="3600000"
                 artifact="de.intevation.flys.artifacts.BedHeightsArtifact">de.intevation.artifactdatabase.DefaultArtifactFactory</artifact-factory>
+            <artifact-factory name="staticsqrelation" description="Factory to create an artifact that generates sq relations from db."
+                ttl="3600000"
+                artifact="de.intevation.flys.artifacts.SQRelationArtifact">de.intevation.artifactdatabase.DefaultArtifactFactory</artifact-factory>
 
             <artifact-factory name="gaugedischargecurve" description="Factory to create an artifact to show a discharge curve for a gauge."
                 ttl="3600000"
@@ -251,6 +254,7 @@
         <artifact name="gaugedischarge" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="${artifacts.config.dir}/artifacts/gaugedischarge.xml" />
         <artifact name="gaugedischargecurve" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="${artifacts.config.dir}/artifacts/gaugedischargecurve.xml" />
         <artifact name="qsector" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="${artifacts.config.dir}/artifacts/qsector.xml" />
+        <artifact name="staticsqrelation" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="${artifacts.config.dir}/artifacts/sqrelation.xml" />
     </artifacts>
 
     <modules>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/SQRelationArtifact.java	Thu Mar 28 15:17:02 2013 +0100
@@ -0,0 +1,158 @@
+package de.intevation.flys.artifacts;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.w3c.dom.Document;
+
+import de.intevation.artifactdatabase.data.DefaultStateData;
+import de.intevation.artifactdatabase.state.DefaultOutput;
+import de.intevation.artifactdatabase.state.Facet;
+import de.intevation.artifacts.ArtifactFactory;
+import de.intevation.artifacts.ArtifactNamespaceContext;
+import de.intevation.artifacts.CallMeta;
+import de.intevation.artifacts.common.utils.XMLUtils;
+import de.intevation.flys.artifacts.model.sq.StaticSQRelation;
+import de.intevation.flys.artifacts.resources.Resources;
+import de.intevation.flys.artifacts.states.StaticState;
+import de.intevation.flys.artifacts.states.sq.SQStaticState;
+
+
+public class SQRelationArtifact
+extends AbstractStaticStateArtifact
+{
+    private static final Logger logger =
+        Logger.getLogger(SQRelationArtifact.class);
+
+    public static final String XPATH_RIVER = "/art:action/art:river/@art:name";
+    public static final String XPATH_STATION =
+        "/art:action/art:measurement_station/@art:number";
+    public static final String NAME = "staticsqrelation";
+    public static final String STATIC_STATE_NAME = "state.sqrelation.static";
+    public static final String UIPROVIDER = "static_sqrelation";
+    public static final String SQ_RELATION_OUT_A = "sq_relation_a";
+    public static final String SQ_RELATION_OUT_B = "sq_relation_b";
+    public static final String SQ_RELATION_OUT_C = "sq_relation_c";
+    public static final String SQ_RELATION_OUT_D = "sq_relation_d";
+    public static final String SQ_RELATION_OUT_E = "sq_relation_e";
+    public static final String SQ_RELATION_OUT_F = "sq_relation_f";
+
+
+    @Override
+    public void setup(
+        String          identifier,
+        ArtifactFactory factory,
+        Object          context,
+        CallMeta        callmeta,
+        Document        data
+    ) {
+        logger.debug("SQRelationArtifact.setup()");
+
+        String river = XMLUtils.xpathString(
+            data,
+            XPATH_RIVER,
+            ArtifactNamespaceContext.INSTANCE);
+        String station = XMLUtils.xpathString(
+            data,
+            XPATH_STATION,
+            ArtifactNamespaceContext.INSTANCE);
+
+        addData(
+            "river",
+            new DefaultStateData(
+                "river",
+                Resources.getMsg(callmeta, "static.sq.river", "Rivername"),
+                "String",
+                river));
+        addData(
+            "station",
+            new DefaultStateData(
+                "station",
+                Resources.getMsg(callmeta, "static.sq.station", "Station"),
+                "String",
+                station));
+        super.setup(identifier, factory, context, callmeta, data);
+    }
+
+    @Override
+    protected void initStaticState() {
+        StaticState state = new SQStaticState(STATIC_STATE_NAME);
+
+        List<Facet> fs = new ArrayList<Facet>();
+        state.staticCompute(fs, this);
+
+        if (hasParameter(StaticSQRelation.Parameter.A, fs)) {
+            DefaultOutput outputA = new DefaultOutput(
+                SQ_RELATION_OUT_A,
+                "output.static.sqrelation.a",
+                "image/png",
+                fs,
+                "chart");
+            state.addOutput(outputA);
+        }
+        if (hasParameter(StaticSQRelation.Parameter.B, fs)) {
+            DefaultOutput outputB = new DefaultOutput(
+                SQ_RELATION_OUT_B,
+                "output.static.sqrelation.b",
+                "image/png",
+                fs,
+                "chart");
+            state.addOutput(outputB);
+        }
+        if (hasParameter(StaticSQRelation.Parameter.C, fs)) {
+            DefaultOutput outputC = new DefaultOutput(
+                SQ_RELATION_OUT_C,
+                "output.static.sqrelation.c",
+                "image/png",
+                fs,
+                "chart");
+            state.addOutput(outputC);
+        }
+        if (hasParameter(StaticSQRelation.Parameter.D, fs)) {
+            DefaultOutput outputD = new DefaultOutput(
+                SQ_RELATION_OUT_D,
+                "output.static.sqrelation.d",
+                "image/png",
+                fs,
+                "chart");
+            state.addOutput(outputD);
+        }
+        if (hasParameter(StaticSQRelation.Parameter.E, fs)) {
+            DefaultOutput outputE = new DefaultOutput(
+                SQ_RELATION_OUT_E,
+                "output.static.sqrelation.e",
+                "image/png",
+                fs,
+                "chart");
+            state.addOutput(outputE);
+        }
+        if (hasParameter(StaticSQRelation.Parameter.F, fs)) {
+            DefaultOutput outputF = new DefaultOutput(
+                SQ_RELATION_OUT_F,
+                "output.static.sqrelation.f",
+                "image/png",
+                fs,
+                "chart");
+            state.addOutput(outputF);
+        }
+        addFacets(STATIC_STATE_NAME, fs);
+        state.setUIProvider(UIPROVIDER);
+        setStaticState(state);
+    }
+
+    @Override
+    public String getName() {
+        return NAME;
+    }
+
+    private boolean hasParameter(StaticSQRelation.Parameter p, List<Facet> fs) {
+        for (Facet f : fs) {
+            if (f.getName().equals("sq_" +
+                p.toString().toLowerCase() + "_curve")) {
+                return true;
+            }
+        }
+        return false;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/access/StaticSQRelationAccess.java	Thu Mar 28 15:17:02 2013 +0100
@@ -0,0 +1,31 @@
+package de.intevation.flys.artifacts.access;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.flys.artifacts.FLYSArtifact;
+
+
+public class StaticSQRelationAccess
+extends RiverAccess
+{
+    /** The logger that is used in this state. */
+    private static final Logger logger =
+        Logger.getLogger(StaticSQRelationAccess.class);
+
+    private String measurementStation;
+
+    public StaticSQRelationAccess(FLYSArtifact artifact) {
+        super(artifact);
+    }
+
+    /** Get measurement station */
+    public String getMeasurementStation() {
+        if (measurementStation == null) {
+            measurementStation = getString("station");
+        }
+        if (logger.isDebugEnabled()) {
+            logger.debug("measurement station: '" + measurementStation + "'");
+        }
+        return measurementStation;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/sq/SQStaticState.java	Thu Mar 28 15:17:02 2013 +0100
@@ -0,0 +1,84 @@
+package de.intevation.flys.artifacts.states.sq;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.List;
+
+import javax.swing.text.DateFormatter;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.artifactdatabase.state.Facet;
+import de.intevation.flys.artifacts.FLYSArtifact;
+import de.intevation.flys.artifacts.access.StaticSQRelationAccess;
+import de.intevation.flys.artifacts.model.sq.StaticSQContainer;
+import de.intevation.flys.artifacts.model.sq.StaticSQFactory;
+import de.intevation.flys.artifacts.model.sq.StaticSQRelation;
+import de.intevation.flys.artifacts.states.StaticState;
+
+
+public class SQStaticState
+extends StaticState
+{
+    private static final Logger log =
+        Logger.getLogger(SQStaticState.class);
+
+    public SQStaticState() {
+        super();
+    }
+
+    public SQStaticState(String name) {
+        super(name);
+    }
+
+    @Override
+    public Object staticCompute(
+        List<Facet> facets,
+        FLYSArtifact artifact
+    ) {
+        StaticSQRelationAccess access = new StaticSQRelationAccess(artifact);
+
+        String river = access.getRiver();
+        String measurementStation = access.getMeasurementStation();
+
+        int ms = -1;
+        try {
+            ms = Integer.valueOf(measurementStation);
+        }
+        catch (NumberFormatException nfe) {
+            log.error("Unparseable measurement station: " + measurementStation);
+            return null;
+        }
+
+        StaticSQContainer sqRelations =
+            StaticSQFactory.getSQRelations(river, ms);
+
+        for (int i = 0; i < StaticSQRelation.Parameter.values().length; i++) {
+            StaticSQRelation.Parameter p =
+                StaticSQRelation.Parameter.values()[i];
+
+            List<StaticSQRelation> relations =
+                sqRelations.getRelationsByParameter(p);
+            if (relations.size() > 0) {
+                int count = 0;
+
+                for (StaticSQRelation relation : relations) {
+                    String name = "sq_" + p.toString().toLowerCase() + "_curve";
+                    DateFormat df =
+                        DateFormat.getDateInstance(DateFormat.SHORT);
+                    String desc = p.toString() + ": " +
+                        df.format(relation.getStartTime()) + " - " +
+                        df.format(relation.getStopTime());
+                    facets.add(new StaticSQRelationFacet(
+                        count,
+                        name,
+                        desc,
+                        relation));
+                    count++;
+                }
+            }
+        }
+        return null;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/sq/StaticSQRelationFacet.java	Thu Mar 28 15:17:02 2013 +0100
@@ -0,0 +1,49 @@
+package de.intevation.flys.artifacts.states.sq;
+
+import de.intevation.artifactdatabase.state.DefaultFacet;
+import de.intevation.artifactdatabase.state.Facet;
+import de.intevation.artifacts.Artifact;
+import de.intevation.artifacts.CallContext;
+import de.intevation.flys.artifacts.math.fitting.Function;
+import de.intevation.flys.artifacts.math.fitting.FunctionFactory;
+import de.intevation.flys.artifacts.model.sq.SQFunction;
+import de.intevation.flys.artifacts.model.sq.StaticSQRelation;
+
+
+public class StaticSQRelationFacet
+extends DefaultFacet
+implements Facet
+{
+    public static final String FUNCTION = "sq-pow";
+
+    private StaticSQRelation relation;
+
+
+    public StaticSQRelationFacet(
+        int ndx,
+        String name,
+        String description,
+        StaticSQRelation relation) {
+        super(ndx, name, description);
+        this.relation = relation;
+    }
+
+    @Override
+    public Object getData(Artifact artifact, CallContext context) {
+        double qmax = relation.getQmax();
+        double[] coeffs = new double[] {relation.getA(), relation.getB()};
+        Function func = FunctionFactory.getInstance().getFunction(FUNCTION);
+        de.intevation.flys.artifacts.math.Function function =
+            func.instantiate(coeffs);
+        SQFunction sqf = new SQFunction(function, 0, qmax);
+        return sqf;
+    }
+
+    @Override
+    public Facet deepCopy() {
+        StaticSQRelationFacet copy =
+            new StaticSQRelationFacet(index, name, description, relation);
+        copy.set(this);
+        return copy;
+    }
+}

http://dive4elements.wald.intevation.org