felix@1791: package de.intevation.flys.exports;
felix@1791: 
felix@1791: import org.apache.log4j.Logger;
felix@1791: 
felix@1791: import org.jfree.data.xy.XYSeries;
felix@1791: 
sascha@3076: import de.intevation.flys.artifacts.model.WKms;
sascha@3076: import de.intevation.flys.artifacts.model.WQKms;
sascha@3076: import de.intevation.flys.artifacts.model.WWQQ;
felix@1791: 
felix@1791: /**
felix@1791:  * Helper to create and modify StyledXYSeries.
felix@1791:  */
felix@1791: public class StyledSeriesBuilder {
felix@1791: 
felix@2601:     /**
felix@2601:      * JFreeChart and the area calculation will fail if we use Double.INFINITY
felix@2601:      * or Double.MAX_VALUE (probably because these are really used in
felix@2601:      * calculations). We define and use a more handy value instead.
felix@2601:      */
felix@2601:     final static double BIG_DOUBLE_VALUE = 1234567d;
felix@2601: 
felix@2571:     private static final Logger logger = Logger.getLogger
felix@2571:         (StyledSeriesBuilder.class);
felix@1791: 
felix@1791: 
felix@1791:     /**
felix@1791:      * Trivial, hidden constructor.
felix@1791:      */
sascha@1886:     private StyledSeriesBuilder() {
sascha@1886:     }
felix@1791: 
felix@1791: 
felix@1791:     /**
felix@1791:      * Add points to series.
felix@1791:      *
felix@1791:      * @param series Series to add points to.
felix@1791:      * @param points Points to add to series, points[0] to 1st dim, points[1]
felix@1791:      *               to 2nd dim.
felix@2686:      * @param skipNANs if true, skip NAN values in points parameter.
felix@1791:      */
felix@2686:     public static void addPoints(XYSeries series, double[][] points, boolean skipNANs) {
felix@1983:         if (points == null || points.length <= 1) {
felix@1975:             return;
felix@1975:         }
felix@1791:         double [] xPoints = points[0];
felix@1791:         double [] yPoints = points[1];
felix@1791:         for (int i = 0; i < xPoints.length; i++) {
felix@2686:             if (skipNANs &&
felix@2686:                 (Double.isNaN(xPoints[i]) || Double.isNaN(yPoints[i]))) {
felix@2571:                 logger.warn ("Skipping NaN in StyledSeriesBuilder.");
felix@2571:                 continue;
felix@2571:             }
felix@1791:             series.add(xPoints[i], yPoints[i], false);
felix@1791:         }
felix@1791:     }
felix@1791: 
felix@1791: 
felix@1791:     /**
felix@1791:      * Add points to series (km to 1st dim, w to 2nd dim).
felix@1791:      *
felix@1791:      * @param series Series to add points to.
felix@3269:      * @param wkms WKms to add to series.
felix@1791:      */
felix@1791:     public static void addPoints(XYSeries series, WKms wkms) {
felix@2156:         if (wkms == null) {
felix@2156:             return;
felix@2156:         }
felix@2156: 
felix@1791:         int size = wkms.size();
felix@1791: 
felix@1791:         for (int i = 0; i < size; i++) {
felix@1791:             series.add(wkms.getKm(i), wkms.getW(i), false);
felix@1791:         }
felix@1791:     }
felix@1791: 
felix@3395: 
felix@3395:     /**
felix@3395:      * Add points to dataset with an offset (shift all points by given amount).
felix@3395:      * @param series series to add data to.
felix@3395:      * @param wkms WKms of which the Ws will be shifted.
felix@3395:      * @param off the offset.
felix@3395:      */
felix@3394:     public static void addUpperBand(XYSeries series, WKms wkms, double off) {
felix@3394:         if (wkms == null) {
felix@3394:             return;
felix@3394:         }
felix@3394: 
felix@3394:         int size = wkms.size();
felix@3394: 
felix@3394:         for (int i = 0; i < size; i++) {
felix@3394:             series.add(wkms.getKm(i), wkms.getW(i)+off, false);
felix@3394:         }
felix@3394:     }
felix@3394: 
felix@3395: 
felix@3395:     /**
felix@3395:      * Add points to dataset with an offset (shift all points 'down' by given
felix@3395:      * amount).
felix@3395:      * @param series series to add data to.
felix@3395:      * @param wkms WKms of which the Ws will be shifted.
felix@3395:      * @param off the offset.
felix@3395:      */
felix@3394:     public static void addLowerBand(XYSeries series, WKms wkms, double off) {
felix@3394:         addUpperBand(series, wkms, -off);
felix@3394:     }
felix@1812: 
felix@3395: 
felix@1791:     /**
felix@1942:      * Add points to series (km to 1st dim, q to 2nd dim).
felix@1791:      *
felix@1791:      * @param series Series to add points to.
felix@3269:      * @param wqkms WQKms to add to series.
felix@1791:      */
felix@1791:     public static void addPointsKmQ(XYSeries series, WQKms wqkms) {
felix@2156:         if (wqkms == null) {
felix@2156:             return;
felix@2156:         }
felix@1791: 
felix@2156:         int size = wqkms.size();
felix@2156: 
felix@2156:         for (int i = 0; i < size; i++) {
felix@2156:             series.add(wqkms.getKm(i), wqkms.getQ(i), false);
felix@2156:         }
felix@1791:     }
felix@1812: 
felix@1812: 
felix@1812:     /**
felix@3176:      * Add points to series (km to 1st dim, q to 2nd dim), adding points
felix@3176:      * to achieve a step-like curve.
felix@3176:      *
felix@3176:      * @param series Series to add points to.
felix@3269:      * @param wqkms WQKms to add to series.
felix@3176:      */
felix@3176:     public static void addStepPointsKmQ(XYSeries series, WQKms wqkms) {
felix@3176:         if (wqkms == null) {
felix@3176:             return;
felix@3176:         }
felix@3176: 
felix@3176:         int size = wqkms.size();
felix@3176: 
felix@3176:         for (int i = 0; i < size; i++) {
felix@3176:             if (i==0) {
felix@3176:                 series.add(wqkms.getKm(i), wqkms.getQ(i), false);
felix@3176:             }
felix@3176:             else {
felix@3176:                 //Add two points.
felix@3176:                 double halveX = (wqkms.getKm(i-1) + wqkms.getKm(i)) / 2d;
bjoern@4438:                 series.add(halveX, wqkms.getQ(i-1), false);
bjoern@4438:                 series.add(halveX, wqkms.getQ(i), false);
felix@3176:             }
felix@3176:             if (i == size-1) {
felix@3176:                 series.add(wqkms.getKm(i), wqkms.getQ(i), false);
felix@3176:             }
felix@3176:         }
felix@3176:     }
felix@3176: 
felix@3176: 
felix@3176:     /**
felix@1812:      * Add points to series (q to 1st dim, w to 2nd dim).
felix@1812:      *
felix@1812:      * @param series Series to add points to.
felix@3284:      * @param wqkms WQKms to add to series.
felix@1812:      */
felix@1812:     public static void addPointsQW(XYSeries series, WQKms wqkms) {
felix@2156:         if (wqkms == null) {
felix@2156:             return;
felix@2156:         }
felix@2156: 
felix@1812:         int size = wqkms.size();
felix@1812: 
felix@1812:         for (int i = 0; i < size; i++) {
bjoern@4438:             series.add(wqkms.getQ(i), wqkms.getW(i), false);
felix@1812:         }
felix@1812:     }
felix@2601: 
felix@2601: 
felix@2601:     /**
felix@2601:      * Add points to series (q to 1st dim, w to 2nd dim).
felix@2601:      *
felix@2601:      * @param series Series to add points to.
felix@4365:      * @param qs the Qs to add, assumed same length than ws.
felix@4365:      * @param ws the Ws to add, assumed same length than qs.
felix@4364:      */
felix@4364:     public static void addPointsQW(XYSeries series, double[] qs, double ws[]) {
felix@4364:         if (ws == null || qs == null) {
felix@4364:             return;
felix@4364:         }
felix@4364: 
felix@4364:         int size = qs.length;
felix@4364: 
felix@4364:         for (int i = 0; i < size; i++) {
bjoern@4438:             series.add(qs[i], ws[i], false);
felix@4364:         }
felix@4364:     }
felix@4364: 
felix@4364: 
felix@4364:     /**
felix@4364:      * Add points to series (q to 1st dim, w to 2nd dim).
felix@4364:      *
felix@4364:      * @param series Series to add points to.
felix@3284:      * @param wwqq WWQQ to add to series.
felix@2601:      */
felix@2601:     public static void addPoints(XYSeries series, WWQQ wwqq) {
felix@2601:         if (wwqq == null) {
felix@2601:             return;
felix@2601:         }
felix@2601: 
felix@2601:         int size = wwqq.size();
felix@2601: 
felix@2601:         for (int i = 0; i < size; i++) {
bjoern@4438:             series.add(wwqq.getW1(i), wwqq.getW2(i), false);
felix@2601:         }
felix@2601:     }
felix@2601: 
felix@2601: 
felix@2601:     /**
felix@2601:      * Create a Series such that an infinitely big area can be filled
felix@2601:      * between the newly created and the given series.
felix@2601:      */
felix@2601:     public static XYSeries createGroundAtInfinity(XYSeries series) {
felix@2601:         XYSeries ground = new XYSeries(series.getKey() + /** TODO rand + */ "INF");
felix@2601:         ground.add(series.getMinX(), -BIG_DOUBLE_VALUE);
felix@2601:         ground.add(series.getMaxX(), -BIG_DOUBLE_VALUE);
felix@2601:         return ground;
felix@2601:     }
felix@2601: 
felix@2601: 
felix@2601:     /**
felix@2601:      * Create a Series such that an infinitely big area can be filled
felix@2601:      * between the newly created and the given series.
felix@2601:      */
felix@2601:     public static XYSeries createCeilingAtInfinity(XYSeries series) {
felix@2601:         XYSeries ground = new XYSeries(series.getKey() + /** TODO rand + */ "INF");
felix@2601:         ground.add(series.getMinX(), BIG_DOUBLE_VALUE);
felix@2601:         ground.add(series.getMaxX(), BIG_DOUBLE_VALUE);
felix@2601:         return ground;
felix@2601:     }
felix@1791: }
felix@1791: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :