view artifacts/src/main/java/org/dive4elements/river/artifacts/model/fixings/FixResult.java @ 6877:2d96d8240e3e

FixA: Make dates of analysis periods unique, too. TODO: Remap the indices like the reference interval.
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 21 Aug 2013 01:09:25 +0200
parents af13ceeba52a
children 8efef772a488
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.artifacts.model.fixings;

import org.apache.log4j.Logger;
import org.dive4elements.river.artifacts.model.Parameters;

import org.dive4elements.river.utils.KMIndex;

import gnu.trove.TIntIntHashMap;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.TreeMap;
import java.util.TreeSet;

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

    protected Parameters      parameters;
    protected KMIndex<QWD []> referenced;
    protected KMIndex<QWI []> outliers;

    public FixResult() {
    }

    public FixResult(
        Parameters      parameters,
        KMIndex<QWD []> referenced,
        KMIndex<QWI []> outliers
    ) {
        this.parameters = parameters;
        this.referenced = referenced;
        this.outliers   = outliers;
    }

    public KMIndex<QWD []> getReferenced() {
        return referenced;
    }

    public void setReferenced(KMIndex<QWD []> referenced) {
        this.referenced = referenced;
    }

    public void makeReferenceEventsDatesUnique() {
        DateUniqueMaker dum = new DateUniqueMaker();
        for (KMIndex.Entry<QWD []> entry: referenced) {
            for (QWD ref: entry.getValue()) {
                dum.makeUnique(ref);
            }
        }
    }

    public Collection<Integer> getReferenceEventsIndices() {
        TreeMap<Date, Integer> dates = new TreeMap<Date, Integer>();
        for (KMIndex.Entry<QWD []> entry: referenced) {
            for (QWD value: entry.getValue()) {
                dates.put(value.date, value.index);
            }
        }
        return dates.values();
    }

    public void remapReferenceIndicesToRank() {
        Collection<Integer> referenceIndices = getReferenceEventsIndices();
        int index = 0;
        TIntIntHashMap map = new TIntIntHashMap();
        boolean debug = log.isDebugEnabled();
        for (Integer refId: referenceIndices) {
            if (debug) {
                log.debug("map " + refId + " -> " + index);
            }
            map.put(refId, index);
            ++index;
        }

        // Two passes: If there are shared references do not
        // remap them twice. In the first pass all indices are
        // mapped to negative values (assuming the original data
        // is all positive). If a negative value is found ignore
        // it because it was remapped before.

        for (KMIndex.Entry<QWD []> entry: referenced) {
            for (QWD value: entry.getValue()) {
                if (value.index >= 0) {
                    if (map.containsKey(value.index)) {
                        value.index = -(map.get(value.index) + 1);
                    }
                    else {
                        log.warn("Could not remap index: " + value.index);
                    }
                }
            }
        }

        // In the second pass all indices are turned to positive
        // values again.
        for (KMIndex.Entry<QWD []> entry: referenced) {
            for (QWD value: entry.getValue()) {
                if (value.index < 0) {
                    value.index = -(value.index + 1);
                }
            }
        }
    }

    public Collection<Date> getReferenceEventsDates() {
        TreeSet<Date> dates = new TreeSet<Date>();
        for (KMIndex.Entry<QWD []> entry: referenced) {
            for (QWD qwd: entry.getValue()) {
                dates.add(qwd.date);
            }
        }
        return dates;
    }


    public KMIndex<QWI []> getOutliers() {
        return outliers;
    }

    public void setOutliers(KMIndex<QWI []> outliers) {
        this.outliers = outliers;
    }

    public Parameters getParameters() {
        return parameters;
    }

    public void setParameters(Parameters parameters) {
        this.parameters = parameters;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org