1 package org.simantics.history;
3 import gnu.trove.list.array.TDoubleArrayList;
5 import java.io.IOException;
6 import java.math.BigDecimal;
8 import org.simantics.history.csv.ExportInterpolation;
9 import org.simantics.history.util.HistoryExportUtil;
10 import org.simantics.history.util.StreamIterator;
11 import org.simantics.history.util.ValueBand;
13 public class HistorySampler {
15 public synchronized static TDoubleArrayList sample( HistorySamplerItem item, double from, double end, double timeWindow, double timeStep, boolean resample ) throws HistoryException, IOException {
19 if(item.collector != null)
20 item.collector.flush();
21 return sample(item.iter, from, end, timeWindow, timeStep, resample);
27 public static TDoubleArrayList sample( StreamIterator iter, double from, double end, double timeWindow, double timeStep, boolean resample ) throws HistoryException, IOException {
29 ExportInterpolation numberInterpolation = ExportInterpolation.LINEAR_INTERPOLATION;
31 double startTime = 0.0;
33 TDoubleArrayList result = new TDoubleArrayList();
35 if(iter.isEmpty()) return result;
37 double allFrom = iter.getFirstTime();
38 double allEnd = 10e10;//iter.getLastTime();
40 if(from > (allEnd + timeStep)) {
41 from = allEnd-timeWindow;
45 // System.err.println("sample " + from + " " + end);
47 // System.err.println("fgag");
50 boolean hasAnyValues = allFrom != Double.MAX_VALUE && allEnd != -Double.MAX_VALUE;
52 // Make intersection of actual data range (allFrom, allEnd) and requested data (from, end)
53 double _from = Double.MAX_VALUE, _end = -Double.MAX_VALUE;
55 _from = Math.max(allFrom, from);
56 _end = Math.min(allEnd, end);
60 // System.err.println("=> no values");
64 // Iterate until endTime is met for all variables
69 // If resample is false then all samples are reported as is. The code achieves this by setting startTime to _from and timeStep to 0.0
75 // time = startTime + n*timeStep
77 // Sampling based on given startTime and timeStep
80 // Find the first sample time that contains data
81 double n = Math.max(0, Math.ceil((_from-startTime) / timeStep));
82 time = startTime + n*timeStep;
86 // Start sampling from startTime but make sure that it is not less than _from
87 if(startTime > _from) time = startTime;
94 // Must convert double times to String when initializing BigDecimal.
95 // Otherwise BigDecimal will pick up inaccuracies from beyond 15 precise digits
96 // thus making a mess of the time step calculations.
98 BigDecimal bigTime = new BigDecimal(String.valueOf(time));
99 BigDecimal bigTimeStep = new BigDecimal(String.valueOf(timeStep));
101 //System.err.println("=> goto " + time);
103 if(!iter.gotoTime(time)) {
104 //System.err.println("=> no sample found at " + time);
108 //time = iter.getValueBand().getTimeDouble();
110 boolean ignore = Math.abs(time-from) < 1e-6;
114 //System.err.println("process " + time + " " + iter.getValueBand());
118 // Check for valid value
119 if ( iter.hasValidValue() ) {
123 // System.err.println("Add time : " + time);
127 Object value = iter.getValueBand().getValue();
128 //System.err.print("Add value : " + value);
129 if (value instanceof Number) {
130 if (value instanceof Float || value instanceof Double) {
131 switch (numberInterpolation) {
132 case PREVIOUS_SAMPLE:
135 // System.err.println(" previous .. done!");
136 result.add(((Number) value).doubleValue());
141 case LINEAR_INTERPOLATION:
142 if (time != iter.getValueBand().getTimeDouble() && iter.hasNext()) {
145 int currentIndex = iter.getIndex();
146 ValueBand band = iter.getValueBand();
147 //double t1 = band.getTimeDouble();
148 Number v1 = (Number) value;
149 double t12 = band.getEndTimeDouble();
151 double t2 = iter.getValueBand().getTimeDouble();
152 Number v2 = (Number) iter.getValueBand().getValue();
153 iter.gotoIndex(currentIndex);
155 double vs = v1.doubleValue();
157 vs = HistoryExportUtil.biglerp(t12, v1.doubleValue(), t2, v2.doubleValue(), time);
160 // System.err.println(" linear .. done!");
165 // Exact timestamp match, or last sample.
166 // Don't interpolate nor extrapolate.
168 // System.err.println(" else .. done!");
169 result.add(((Number) value).doubleValue());
174 throw new UnsupportedOperationException("Unsupported interpolation: " + numberInterpolation);
177 throw new IllegalStateException("Value is not a number " + value);
179 } else if (value instanceof Boolean) {
181 result.add( (Boolean)value ? 1.0: 0.0);
183 throw new IllegalStateException("Value is not a number " + value);
189 // Read next values, and the following times
190 if ( timeStep>0.0 ) {
191 bigTime = bigTime.add(bigTimeStep);
192 time = bigTime.doubleValue();
194 // Get smallest end time that is larger than current time
195 Double nextTime = null;
196 // System.out.println("time = "+time);
197 if(!iter.hasNext()) break;
198 Double itemNextTime = iter.getNextTime( time );
199 // System.err.println(" "+i.label+" nextTime="+itemNextTime);
200 if ( nextTime == null || ( nextTime > itemNextTime && !itemNextTime.equals( time ) ) ) nextTime = itemNextTime;
201 if ( nextTime == null || nextTime.equals( time ) ) break;
205 boolean hasMore = false;
207 iter.proceedToTime(time);
208 if(HistoryExportUtil.contains(iter, time)) hasMore = true;
212 } while (time<=_end);
214 //System.err.println("=> " + Arrays.toString(result.toArray()));