All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.epics.graphene.rrdtool.TimeSeriesMulti Maven / Gradle / Ivy

There is a newer version: 0.5
Show newest version
/**
 * Copyright (C) 2012 University of Michigan
 * All rights reserved. Use is subject to license terms.
 */
package org.epics.graphene.rrdtool;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.epics.util.array.CircularBufferDouble;
import org.epics.util.array.ListDouble;
import org.epics.util.time.Timestamp;

/**
 *
 * @author carcassi
 */
public class TimeSeriesMulti {
    
    private List time;
    private Map values;

    public TimeSeriesMulti(List time, Map  values) {
        this.time = time;
        this.values = values;
    }
    
    public List getTime() {
        return time;
    }
    
    public Map getValues() {
        return values;
    }
    
    public static TimeSeriesMulti synchronizeSeries(Map seriesMap) {
        int[] offsets = new int[seriesMap.size()];
        Timestamp[] timestamps = new Timestamp[seriesMap.size()];
        boolean done = false;
        
        List finalTimes = new ArrayList<>();
        Map data = new HashMap<>();
        List buffers = new ArrayList<>();
        List series = new ArrayList<>();
        for (Map.Entry entry : seriesMap.entrySet()) {
            String name = entry.getKey();
            TimeSeries timeSeries = entry.getValue();
            CircularBufferDouble buffer = new CircularBufferDouble(1000000);
            data.put(name, buffer);
            buffers.add(buffer);
            series.add(timeSeries);
        }
                
        while (!done) {
            // The next time is going to be the maximum between all the
            // next time, because we want all values to change
            for (int i = 0; i < timestamps.length; i++) {
                timestamps[i] = series.get(i).getTime().get(offsets[i]);
            }
            Timestamp nextTime = Collections.max(Arrays.asList(timestamps));
            
            // Advance all the offsets to a time that is nextTime or later
            for (int i = 0; i < offsets.length; i++) {
                List times = series.get(i).getTime();
                while((offsets[i] != times.size() - 1) && (times.get(offsets[i]).compareTo(nextTime) < 0)) {
                    offsets[i]++;
                }
            }
            
            // Add next values and increment
            finalTimes.add(nextTime);
            boolean hasIncremented = false;
            for (int i = 0; i < offsets.length; i++) {
                buffers.get(i).addDouble(series.get(i).getValues().getDouble(offsets[i]));
                if (offsets[i] < series.get(i).getValues().size() - 1) {
                    hasIncremented = true;
                    offsets[i]++;
                }
            }
            
            // If no increment, we are done
            if (!hasIncremented) {
                done = true;
            }
        }
        return new TimeSeriesMulti(finalTimes, data);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy