Mercurial > dive4elements > river
view artifacts/src/main/java/org/dive4elements/river/exports/fixings/FixWQCurveGenerator.java @ 6601:5ecc6d4d73f2
Add official fixings to Waterlevel CSV Export (issue1384)
This searches the collection for staticwqkms artifacts that
contain official data and adds that data to the export.
The data is filtered by the calculation range and sorted by the
calculation direction.
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Thu, 18 Jul 2013 13:16:33 +0200 |
parents | 6791411ed1fc |
children | 641f93a5fa9e |
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde * Software engineering by Intevation GmbH * * This file is Free Software under the GNU AGPL (>=v3) * and comes with ABSOLUTELY NO WARRANTY! Check out the * documentation coming with Dive4Elements River for details. */ package org.dive4elements.river.exports.fixings; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.text.DateFormat; import java.util.ArrayList; import java.util.List; import org.apache.log4j.Logger; import org.jfree.chart.JFreeChart; import org.jfree.chart.annotations.XYTextAnnotation; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.Marker; import org.jfree.chart.plot.ValueMarker; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.title.TextTitle; import org.jfree.data.xy.XYSeries; import org.jfree.ui.RectangleAnchor; import org.jfree.ui.RectangleInsets; import org.jfree.ui.TextAnchor; import org.w3c.dom.Document; import org.dive4elements.artifactdatabase.state.ArtifactAndFacet; import org.dive4elements.artifactdatabase.state.Facet; import org.dive4elements.river.artifacts.D4EArtifact; import org.dive4elements.river.artifacts.StaticWKmsArtifact; import org.dive4elements.river.artifacts.WINFOArtifact; import org.dive4elements.river.artifacts.access.FixAnalysisAccess; import org.dive4elements.river.artifacts.model.DateRange; import org.dive4elements.river.artifacts.model.FacetTypes; import org.dive4elements.river.artifacts.model.NamedDouble; import org.dive4elements.river.artifacts.model.QWDDateRange; import org.dive4elements.river.artifacts.model.WKms; import org.dive4elements.river.artifacts.model.WQKms; import org.dive4elements.river.artifacts.model.fixings.FixFunction; import org.dive4elements.river.artifacts.model.fixings.FixWQCurveFacet; import org.dive4elements.river.artifacts.model.fixings.QWD; import org.dive4elements.river.artifacts.model.fixings.QWI; import org.dive4elements.river.artifacts.resources.Resources; import org.dive4elements.river.exports.ChartGenerator; import org.dive4elements.river.exports.DischargeCurveGenerator; import org.dive4elements.river.exports.SyncNumberAxis; import org.dive4elements.river.exports.StyledSeriesBuilder; import org.dive4elements.river.jfree.CollisionFreeXYTextAnnotation; import org.dive4elements.river.jfree.RiverAnnotation; import org.dive4elements.river.jfree.JFreeUtil; import org.dive4elements.river.jfree.StickyAxisAnnotation; import org.dive4elements.river.jfree.StyledXYSeries; import org.dive4elements.river.model.Gauge; import org.dive4elements.river.model.River; import org.dive4elements.river.utils.RiverUtils; import org.dive4elements.river.utils.ThemeUtil; /** * Generator for WQ fixing charts. * @author <a href="mailto:christian.lins@intevation.de">Christian Lins</a> */ public class FixWQCurveGenerator extends FixChartGenerator implements FacetTypes { /** Private logger. */ private static Logger logger = Logger.getLogger(FixWQCurveGenerator.class); public static final String I18N_CHART_TITLE = "chart.fixings.wq.title"; public static final String I18N_CHART_SUBTITLE = "chart.fixings.wq.subtitle"; public static final String I18N_CHART_SUBTITLE1 = "chart.fixings.wq.subtitle1"; public static final String I18N_XAXIS_LABEL = "chart.fixings.wq.xaxis.label"; public static final String I18N_YAXIS_LABEL = "chart.fixings.wq.yaxis.label"; public static final String I18N_CHART_TITLE_DEFAULT = "Fixierungsanalyse"; public static final String I18N_XAXIS_LABEL_DEFAULT = "Q [m\u00B3/s]"; public static final String I18N_YAXIS_LABEL_DEFAULT = "W [NN + m]"; public static final double EPSILON = 0.001d; public static enum YAXIS { WCm(0), W(1); public int idx; private YAXIS(int c) { idx = c; } } /** Needed to access data to create subtitle. */ protected D4EArtifact artifact; // TODO dupe of ComputedDischargeCurveGenerator protected SyncNumberAxis secondYAxis; // TODO dupe of ComputedDischargeCurveGenerator protected NumberAxis firstYAxis; /** * Create Y (range) axis for given index, here with a special axis * that depends on other axis (does translation and scaling for * special case at gauge in cm). */ // TODO dupe of ComputedDischargeCurveGenerator @Override protected NumberAxis createYAxis(int index) { logger.debug("createYAxis: " + index); if (index == 1) { firstYAxis = super.createYAxis(1); if (secondYAxis != null) { secondYAxis.setProxyAxis(firstYAxis); } return firstYAxis; } YAxisWalker walker = getYAxisWalker(); Font labelFont = new Font( DEFAULT_FONT_NAME, Font.BOLD, getYAxisFontSize(index)); SyncNumberAxis axis = new SyncNumberAxis( walker.getId(index), getYAxisLabel(index), firstYAxis); axis.setAutoRangeIncludesZero(false); axis.setLabelFont(labelFont); axis.setTickLabelFont(labelFont); axis.setShift((double)-getCurrentGaugeDatum()); secondYAxis = axis; return axis; } /** Returns value != 0 if the current km is not at a gauge. */ public double getCurrentGaugeDatum() { if (context.getContextValue(CURRENT_KM) != null) { return DischargeCurveGenerator.getCurrentGaugeDatum( (Double) context.getContextValue(CURRENT_KM), (D4EArtifact) getMaster(), 1e-4); } else return 0d; } /** Overriden to show second axis also if no visible data present. */ @Override protected void adjustAxes(XYPlot plot) { super.adjustAxes(plot); if (getCurrentGaugeDatum() != 0d) { // Show the W[*m] axis even if there is no data. plot.setRangeAxis(1, createYAxis(YAXIS.W.idx)); } } /** Translate River annotations if not at gauge. */ public void translateRiverAnnotation(RiverAnnotation riverAnnotation) { if (getCurrentGaugeDatum() == 0d) { return; } logger.debug("Translate some river annotation."); double translate = getCurrentGaugeDatum(); double factor = 100d; for (StickyAxisAnnotation annotation: riverAnnotation.getAxisTextAnnotations()){ if (!annotation.atX()) { annotation.setPos((annotation.getPos() - translate)*factor); } } for (XYTextAnnotation annotation: riverAnnotation.getTextAnnotations()) { annotation.setY((annotation.getY() - translate)*factor); } } @Override public void doOut(ArtifactAndFacet aaf, Document doc, boolean visible) { logger.debug("doOut: " + aaf.getFacetName()); if (!prepareChartData(aaf, doc, visible)) { logger.warn("Unknown facet, name " + aaf.getFacetName()); } } /** * Return true if data could be handled, * to be overridden to add more handled data. */ public boolean prepareChartData(ArtifactAndFacet aaf, Document doc, boolean visible) { String name = aaf.getFacetName(); this.artifact = (D4EArtifact) aaf.getArtifact(); if(name.startsWith(FIX_SECTOR_AVERAGE_WQ)) { doSectorAverageOut(aaf, doc, visible); } else if(FIX_ANALYSIS_EVENTS_WQ.equals(name)) { doAnalysisEventsOut(aaf, doc, visible); } else if(FIX_REFERENCE_EVENTS_WQ.equals(name)) { doReferenceEventsOut(aaf, doc, visible); } else if(FIX_WQ_CURVE.equals(name)) { doWQCurveOut(aaf, doc, visible); } else if(FIX_OUTLIER.equals(name)) { doOutlierOut(aaf, doc, visible); } else if(QSECTOR.equals(name)) { doQSectorOut(aaf, doc, visible); } else if(FIX_EVENTS.equals(name)) { doEventsOut(aaf, doc, visible); } else if(/*STATIC_WKMS_INTERPOL.equals(name) ||*/ STATIC_WKMS_MARKS.equals(name) || STATIC_WKMS.equals(name) || HEIGHTMARKS_POINTS.equals(name) ) { doWAnnotations( aaf.getData(context), aaf, doc, visible); } else if (LONGITUDINAL_W.equals(name) || STATIC_WQ.equals(name) || STATIC_WKMS_INTERPOL.equals(name) || FIX_WQ_LS.equals(name)) { doWQOut(aaf.getData(context), aaf, doc, visible); } else if (name.equals(DISCHARGE_CURVE)) { logger.debug("diso " + name); doDischargeOut( (WINFOArtifact) aaf.getArtifact(), aaf.getData(context), aaf.getFacetDescription(), doc, visible); } else if (name.equals(MAINVALUES_W) || name.equals(MAINVALUES_Q)) { RiverAnnotation mainValues = (RiverAnnotation) aaf.getData(context); translateRiverAnnotation(mainValues); doAnnotations( mainValues, aaf, doc, visible); } else if (FacetTypes.IS.MANUALPOINTS(aaf.getFacetName())) { doPoints(aaf.getData(context), aaf, doc, visible, YAXIS.W.idx); } else { return false; } return true; } /** Add sector average points to chart. */ protected void doSectorAverageOut(ArtifactAndFacet aaf, Document doc, boolean visible) { logger.debug("doSectorAverageOut"); QWDDateRange qwdd = (QWDDateRange) aaf.getData(context); QWD qwd = qwdd != null ? qwdd.getQWD() : null; if(qwd != null) { addQWSeries(new QWD[] { qwd }, aaf, doc, visible); } else { logger.debug("doSectorAverageOut: qwd == null"); } } /** Add analysis event points to chart. */ protected void doAnalysisEventsOut(ArtifactAndFacet aaf, Document doc, boolean visible) { logger.debug("doAnalysisEventsOut"); QWD qwd = (QWD)aaf.getData(context); if(qwd != null) { XYSeries series = new StyledXYSeries(aaf.getFacetDescription(), doc); List<XYTextAnnotation> textAnnos = new ArrayList<XYTextAnnotation>(); DateFormat dateFormat = DateFormat.getDateInstance( DateFormat.SHORT); series.add(qwd.getQ(), qwd.getW()); XYTextAnnotation anno = new CollisionFreeXYTextAnnotation( dateFormat.format(qwd.getDate()), qwd.getQ(), qwd.getW()); textAnnos.add(anno); addAxisSeries(series, YAXIS.W.idx, visible); if(visible && ThemeUtil.parseShowPointLabel(doc)) { RiverAnnotation flysAnno = new RiverAnnotation(null, null, null, doc); flysAnno.setTextAnnotations(textAnnos); addAnnotations(flysAnno); } } else { logger.debug("doAnalysisEventsOut: qwds == null"); } } /** Add reference event points to chart. */ protected void doReferenceEventsOut(ArtifactAndFacet aaf, Document doc, boolean visible) { logger.debug("doReferenceEventsOut"); QWI qwd = (QWI)aaf.getData(context); if(qwd != null) { XYSeries series = new StyledXYSeries(aaf.getFacetDescription(), doc); List<XYTextAnnotation> textAnnos = new ArrayList<XYTextAnnotation>(); DateFormat dateFormat = DateFormat.getDateInstance( DateFormat.SHORT); series.add(qwd.getQ(), qwd.getW()); XYTextAnnotation anno = new CollisionFreeXYTextAnnotation( dateFormat.format(qwd.getDate()), qwd.getQ(), qwd.getW()); textAnnos.add(anno); addAxisSeries(series, YAXIS.W.idx, visible); if(visible && ThemeUtil.parseShowPointLabel(doc)) { RiverAnnotation flysAnno = new RiverAnnotation(null, null, null, doc); flysAnno.setTextAnnotations(textAnnos); addAnnotations(flysAnno); } } else { logger.debug("doReferenceEventsOut: qwds == null"); } } private void addPointFromWQKms(WQKms wqkms, String title, Document theme, boolean visible ) { XYSeries series = new StyledXYSeries(title, theme); Double ckm = (Double) context.getContextValue(CURRENT_KM); if (wqkms == null || wqkms.getKms().length == 0 || ckm == null) { logger.info("addPointFromWQKms: No event data to show."); return; } double[] kms = wqkms.getKms(); for (int i = 0 ; i< kms.length; i++) { if (Math.abs(kms[i] - ckm) <= EPSILON) { series.add(wqkms.getQ(i), wqkms.getW(i)); addAxisSeries(series, YAXIS.W.idx, visible); if(visible && ThemeUtil.parseShowPointLabel(theme)) { List<XYTextAnnotation> textAnnos = new ArrayList<XYTextAnnotation>(); XYTextAnnotation anno = new CollisionFreeXYTextAnnotation( title, wqkms.getQ(i), wqkms.getW(i)); textAnnos.add(anno); RiverAnnotation flysAnno = new RiverAnnotation(null, null, null, theme); flysAnno.setTextAnnotations(textAnnos); addAnnotations(flysAnno); } return; } } } protected void doEventsOut(ArtifactAndFacet aaf, Document doc, boolean visible) { logger.debug("doEventsOut"); // Find W/Q at km. addPointFromWQKms((WQKms) aaf.getData(context), aaf.getFacetDescription(), doc, visible); } protected void doWQCurveOut(ArtifactAndFacet aaf, Document doc, boolean visible) { logger.debug("doWQCurveOut"); FixWQCurveFacet facet = (FixWQCurveFacet)aaf.getFacet(); FixFunction func = (FixFunction)facet.getData( aaf.getArtifact(), context); if (func == null) { logger.warn("doWQCurveOut: Facet does not contain FixFunction"); return; } double maxQ = func.getMaxQ(); if (maxQ > 0) { StyledXYSeries series = JFreeUtil.sampleFunction2D( func.getFunction(), doc, aaf.getFacetDescription(), 500, // number of samples 0.0 , // start maxQ); // end double gaugeDatum = getCurrentGaugeDatum(); double factor = (gaugeDatum == 0d) ? 1d : 100d; if (gaugeDatum == 0d) { addAxisSeries(series, YAXIS.W.idx, visible); } else { StyledXYSeries series2 = JFreeUtil.sampleFunction2D( func.getFunction(), doc, aaf.getFacetDescription(), 500, // number of samples 0.0 , // start maxQ); // end addAxisSeries(series2, YAXIS.W.idx, false); // Use second axis at cm if at gauge. for (int i = 0; i < series.getItemCount(); i++) { series.updateByIndex(i, new Double(factor*(series.getY(i).doubleValue()-gaugeDatum))); } addAxisSeries(series, YAXIS.WCm.idx, visible); } } else { logger.warn("doWQCurveOut: maxQ <= 0"); } } protected void doOutlierOut(ArtifactAndFacet aaf, Document doc, boolean visible) { logger.debug("doOutlierOut"); QWI[] qws = (QWI[])aaf.getData(context); addQWSeries(qws, aaf, doc, visible); } /** Add markers for q sectors. */ protected void doQSectorOut(ArtifactAndFacet aaf, Document theme, boolean visible) { logger.debug("doQSectorOut"); if (!visible) { return; } Object qsectorsObj = aaf.getData(context); if (qsectorsObj == null || !(qsectorsObj instanceof List)) { logger.warn("No QSectors coming from data."); return; } List<?> qsectorsList = (List<?>) qsectorsObj; if (qsectorsList.size() == 0 || !(qsectorsList.get(0) instanceof NamedDouble)) { logger.warn("No QSectors coming from data."); return; } @SuppressWarnings("unchecked") List<NamedDouble> qsectors = (List<NamedDouble>) qsectorsList; for (NamedDouble qsector : qsectors) { if (Double.isNaN(qsector.getValue())) { continue; } Marker m = new ValueMarker(qsector.getValue()); m.setPaint(Color.black); float[] dashes = ThemeUtil.parseLineStyle(theme); int size = ThemeUtil.parseLineWidth(theme); BasicStroke stroke; if (dashes.length <= 1) { stroke = new BasicStroke(size); } else { stroke = new BasicStroke(size, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1.0f, dashes, 0.0f); } m.setStroke(stroke); if (ThemeUtil.parseShowLineLabel(theme)) { m.setLabel(qsector.getName()); m.setPaint(ThemeUtil.parseTextColor(theme)); m.setLabelFont(ThemeUtil.parseTextFont(theme)); } Color paint = ThemeUtil.parseLineColorField(theme); if (paint != null) { m.setPaint(paint); } m.setLabelAnchor(RectangleAnchor.TOP_LEFT); m.setLabelTextAnchor(TextAnchor.TOP_LEFT); m.setLabelOffset(new RectangleInsets(5, 5, 10, 10)); addDomainMarker(m); } } /** * Add W-Annotations to plot. * @param wqkms actual data (double[][]). * @param theme theme to use. */ protected void doWAnnotations( Object wqkms, ArtifactAndFacet aandf, Document theme, boolean visible ) { Facet facet = aandf.getFacet(); List<StickyAxisAnnotation> xy = new ArrayList<StickyAxisAnnotation>(); if (wqkms instanceof double[][]) { logger.debug("Got double[][]"); double [][] data = (double [][]) wqkms; for (int i = 0; i< data[0].length; i++) { xy.add(new StickyAxisAnnotation(aandf.getFacetDescription(), (float) data[1][i], StickyAxisAnnotation.SimpleAxis.Y_AXIS)); } doAnnotations(new RiverAnnotation(facet.getDescription(), xy), aandf, theme, visible); } else { // Assume its WKms. logger.debug("Got WKms"); WKms data = (WKms) wqkms; Double ckm = (Double) context.getContextValue(CURRENT_KM); double location = (ckm != null) ? ckm.doubleValue() : getRange()[0]; double w = StaticWKmsArtifact.getWAtKmLin(data, location); xy.add(new StickyAxisAnnotation(aandf.getFacetDescription(), (float) w, StickyAxisAnnotation.SimpleAxis.Y_AXIS)); doAnnotations(new RiverAnnotation(facet.getDescription(), xy), aandf, theme, visible); } } /** * Add series with discharge curve to diagram. */ protected void doDischargeOut( WINFOArtifact artifact, Object o, String description, Document theme, boolean visible) { WQKms wqkms = (WQKms) o; String gaugeName = wqkms.getName(); River river = RiverUtils.getRiver(artifact); if (river == null) { logger.debug("no river found"); return; } Gauge gauge = river.determineGaugeByName(gaugeName); if (gauge == null) { logger.debug("no gauge found"); return; } XYSeries series = new StyledXYSeries(description, theme); StyledSeriesBuilder.addPointsQW(series, wqkms); addAxisSeries(series, YAXIS.W.idx, visible); } /** * Add WQ Data to plot. * @param wqkms data as double[][] */ protected void doWQOut( Object wqkms, ArtifactAndFacet aaf, Document theme, boolean visible ) { logger.debug("FixWQCurveGenerator: doWQOut"); if (wqkms instanceof WQKms) { // TODO As in doEventsOut, the value-searching should // be delivered by the facet already (instead of in the Generator). logger.debug("FixWQCurveGenerator: doWQOut: WQKms"); addPointFromWQKms((WQKms) aaf.getData(context), aaf.getFacetDescription(), theme, visible); } else { logger.debug("FixWQCurveGenerator: doWQOut: double[][]"); double [][] data = (double [][]) wqkms; XYSeries series = new StyledXYSeries(aaf.getFacetDescription(), theme); StyledSeriesBuilder.addPoints(series, data, true); addAxisSeries(series, YAXIS.W.idx, visible); } } protected void addQWSeries( QWI [] qws, ArtifactAndFacet aaf, Document theme, boolean visible ) { if (qws == null) { return; } XYSeries series = new StyledXYSeries(aaf.getFacetDescription(), theme); List<XYTextAnnotation> textAnnos = new ArrayList<XYTextAnnotation>(qws.length); DateFormat dateFormat = DateFormat.getDateInstance( DateFormat.SHORT); for (QWI qw: qws) { series.add(qw.getQ(), qw.getW()); XYTextAnnotation anno = new CollisionFreeXYTextAnnotation( dateFormat.format(qw.getDate()), qw.getQ(), qw.getW()); textAnnos.add(anno); } addAxisSeries(series, YAXIS.W.idx, visible); if (visible && ThemeUtil.parseShowPointLabel(theme)) { RiverAnnotation flysAnno = new RiverAnnotation(null, null, null, theme); flysAnno.setTextAnnotations(textAnnos); addAnnotations(flysAnno); } } @Override protected String getChartTitle() { return Resources.format( context.getMeta(), I18N_CHART_TITLE, I18N_CHART_TITLE_DEFAULT, context.getContextValue(CURRENT_KM)); } @Override protected String getDefaultChartTitle() { return msg(I18N_CHART_TITLE, I18N_CHART_TITLE_DEFAULT); } @Override protected String getDefaultChartSubtitle() { FixAnalysisAccess access = new FixAnalysisAccess(artifact); DateRange dateRange = access.getDateRange(); DateRange refRange = access.getReferencePeriod(); if (dateRange != null && refRange != null) { return Resources.format( context.getMeta(), I18N_CHART_SUBTITLE, "", access.getRiver(), dateRange.getFrom(), dateRange.getTo(), refRange.getFrom(), refRange.getTo()); } return null; } @Override protected void addSubtitles(JFreeChart chart) { String defaultSubtitle = getDefaultChartSubtitle(); if (defaultSubtitle == null || defaultSubtitle.length() == 0) { return; } chart.addSubtitle(new TextTitle(defaultSubtitle)); StringBuilder buf = new StringBuilder(); // Add analysis periods as additional subtitle FixAnalysisAccess access = new FixAnalysisAccess(artifact); DateRange[] aperiods = access.getAnalysisPeriods(); buf.append(msg("fix.analysis.periods")); buf.append(": "); for(int n = 0; n < aperiods.length; n++) { buf.append( Resources.format( context.getMeta(), I18N_CHART_SUBTITLE1, "", aperiods[n].getFrom(), aperiods[n].getTo())); if(n + 1 < aperiods.length) { buf.append("; "); } } chart.addSubtitle(new TextTitle(buf.toString())); } @Override protected String getDefaultXAxisLabel() { return msg(I18N_XAXIS_LABEL, I18N_XAXIS_LABEL_DEFAULT); } @Override protected String getDefaultYAxisLabel(int pos) { D4EArtifact flys = (D4EArtifact) master; String unit = RiverUtils.getRiver(flys).getWstUnit().getName(); if (pos == 0) { unit = "cm"; } return msg(I18N_YAXIS_LABEL, I18N_YAXIS_LABEL_DEFAULT, new Object[] { unit }); } @Override protected ChartGenerator.YAxisWalker getYAxisWalker() { return new YAxisWalker() { @Override public int length() { return YAXIS.values().length; } @Override public String getId(int idx) { YAXIS[] yaxes = YAXIS.values(); return yaxes[idx].toString(); } }; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :