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 {
18 // If there is something pending at this point, flush before opening for read
19 if(item.collector != null)
20 item.collector.flush();
22 return sample(item.iter, from, end, timeWindow, timeStep, resample);
28 public static TDoubleArrayList sample( StreamIterator iter, double from, double end, double timeWindow, double timeStep, boolean resample ) throws HistoryException, IOException {
29 return sample(iter, from, end, timeWindow, timeStep, resample, 0.0);
32 public static TDoubleArrayList sample( StreamIterator iter, double from, double end, double timeWindow, double timeStep, boolean resample, Double sampleFrom ) throws HistoryException, IOException {
34 ExportInterpolation numberInterpolation = ExportInterpolation.LINEAR_INTERPOLATION;
36 double startTime = from;
37 if(sampleFrom != null) {
38 // This option can be used do define the offset of sampling. Samples will be sampleFrom + n * timeStep
39 startTime = sampleFrom;
42 TDoubleArrayList result = new TDoubleArrayList();
44 if(iter.isEmpty()) return result;
46 double allFrom = iter.getFirstTime();
47 double allEnd = 10e10;//iter.getLastTime();
49 if(from > (allEnd + timeStep)) {
50 from = allEnd-timeWindow;
54 // System.err.println("sample " + from + " " + end);
56 // System.err.println("fgag");
59 boolean hasAnyValues = allFrom != Double.MAX_VALUE && allEnd != -Double.MAX_VALUE;
61 // Make intersection of actual data range (allFrom, allEnd) and requested data (from, end)
62 double _from = Double.MAX_VALUE, _end = -Double.MAX_VALUE;
64 _from = Math.max(allFrom, from);
65 _end = Math.min(allEnd, end);
69 // System.err.println("=> no values");
73 // Iterate until endTime is met for all variables
78 // 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
84 // time = startTime + n*timeStep
86 // Sampling based on given startTime and timeStep
89 // Find the first sample time that contains data
90 double n = Math.max(0, Math.ceil((_from-startTime) / timeStep));
91 time = startTime + n*timeStep;
95 // Start sampling from startTime but make sure that it is not less than _from
96 if(startTime > _from) time = startTime;
103 // Must convert double times to String when initializing BigDecimal.
104 // Otherwise BigDecimal will pick up inaccuracies from beyond 15 precise digits
105 // thus making a mess of the time step calculations.
107 BigDecimal bigTime = new BigDecimal(String.valueOf(time));
108 BigDecimal bigTimeStep = new BigDecimal(String.valueOf(timeStep));
110 //System.err.println("=> goto " + time);
112 if(!iter.gotoTime(time)) {
113 //System.err.println("=> no sample found at " + time);
117 //time = iter.getValueBand().getTimeDouble();
119 boolean ignore = Math.abs(time-from) < 1e-6;
123 //System.err.println("process " + time + " " + iter.getValueBand());
127 // Check for valid value
128 if ( iter.hasValidValue() ) {
132 // System.err.println("Add time : " + time);
136 Object value = iter.getValueBand().getValue();
137 //System.err.print("Add value : " + value);
138 if (value instanceof Number) {
139 if (value instanceof Float || value instanceof Double) {
140 switch (numberInterpolation) {
141 case PREVIOUS_SAMPLE:
144 // System.err.println(" previous .. done!");
145 result.add(((Number) value).doubleValue());
150 case LINEAR_INTERPOLATION:
151 if (time != iter.getValueBand().getTimeDouble() && iter.hasNext()) {
154 int currentIndex = iter.getIndex();
155 ValueBand band = iter.getValueBand();
156 //double t1 = band.getTimeDouble();
157 Number v1 = (Number) value;
158 double t12 = band.getEndTimeDouble();
160 double t2 = iter.getValueBand().getTimeDouble();
161 Number v2 = (Number) iter.getValueBand().getValue();
162 iter.gotoIndex(currentIndex);
164 double vs = v1.doubleValue();
166 vs = HistoryExportUtil.biglerp(t12, v1.doubleValue(), t2, v2.doubleValue(), time);
169 // System.err.println(" linear .. done!");
174 // Exact timestamp match, or last sample.
175 // Don't interpolate nor extrapolate.
177 // System.err.println(" else .. done!");
178 result.add(((Number) value).doubleValue());
183 throw new UnsupportedOperationException("Unsupported interpolation: " + numberInterpolation);
186 throw new IllegalStateException("Value is not a number " + value);
188 } else if (value instanceof Boolean) {
190 result.add( (Boolean)value ? 1.0: 0.0);
192 throw new IllegalStateException("Value is not a number " + value);
198 // Read next values, and the following times
199 if ( timeStep>0.0 ) {
200 bigTime = bigTime.add(bigTimeStep);
201 time = bigTime.doubleValue();
203 // Get smallest end time that is larger than current time
204 Double nextTime = null;
205 // System.out.println("time = "+time);
206 if(!iter.hasNext()) break;
207 Double itemNextTime = iter.getNextTime( time );
208 // System.err.println(" "+i.label+" nextTime="+itemNextTime);
209 if ( nextTime == null || ( nextTime > itemNextTime && !itemNextTime.equals( time ) ) ) nextTime = itemNextTime;
210 if ( nextTime == null || nextTime.equals( time ) ) break;
214 boolean hasMore = false;
216 iter.proceedToTime(time);
217 if(HistoryExportUtil.contains(iter, time)) hasMore = true;
221 } while (time<=_end);
223 //System.err.println("=> " + Arrays.toString(result.toArray()));
229 public synchronized static TDoubleArrayList sample(HistorySamplerItem2 item, double end, double timeWindow, int maxSamples, boolean resample) throws HistoryException, IOException {
231 // If there is something pending at this point, flush before opening for read
232 if (item.collector != null)
233 item.collector.flush();
234 double pixelsPerSecond = (double) maxSamples / timeWindow;
235 item.open(pixelsPerSecond);
236 return sample(item.iter, end, timeWindow, maxSamples, resample);
242 private static TDoubleArrayList sample(StreamIterator iter, double end, double timeWindow, int maxSamples, boolean resample) throws HistoryException, IOException {
243 //System.out.println("sample: " + end + ", " + timeWindow + ", " + maxSamples + ", " + resample);
244 ExportInterpolation numberInterpolation = ExportInterpolation.LINEAR_INTERPOLATION;
247 double startTime = -1e10;
249 TDoubleArrayList result = new TDoubleArrayList();
253 double allFrom = iter.getFirstTime();
254 double allEnd = iter.getLastTime();
255 //System.out.println("data available [" + allFrom + " .. " + allEnd + "]");
257 double from = allEnd-timeWindow;
259 //System.out.println("sample between [" + from + " .. " + end + "]");
262 boolean hasAnyValues = allFrom != Double.MAX_VALUE && allEnd != -Double.MAX_VALUE;
264 //System.err.println("=> no values");
268 // Make intersection of actual data range (allFrom, allEnd) and requested data (from, end)
269 double _from = Math.max(allFrom, from);
270 double _end = Math.min(allEnd, end);
272 // Iterate until endTime is met for all variables
276 // 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
280 // time = startTime + n*timeStep
281 // Sampling based on given startTime and timeStep
283 // Find the first sample time that contains data
284 double n = Math.max(0, Math.ceil((_from-startTime) / timeStep));
285 time = startTime + n*timeStep;
287 // Start sampling from startTime but make sure that it is not less than _from
288 if(startTime > _from) time = startTime;
292 // Must convert double times to String when initializing BigDecimal.
293 // Otherwise BigDecimal will pick up inaccuracies from beyond 15 precise digits
294 // thus making a mess of the time step calculations.
295 BigDecimal bigTime = new BigDecimal(String.valueOf(time));
296 BigDecimal bigTimeStep = new BigDecimal(String.valueOf(timeStep));
298 //System.err.println("=> goto " + time);
300 if(!iter.gotoTime(time)) {
301 //System.err.println("=> no sample found at " + time);
305 //time = iter.getValueBand().getTimeDouble();
307 boolean ignore = Math.abs(time-from) < 1e-6;
311 //System.err.println("process " + time + " " + iter.getValueBand());
315 // Check for valid value
316 if ( iter.hasValidValue() ) {
320 //System.err.println("Add time : " + time);
324 Object value = iter.getValueBand().getValue();
325 //System.err.print("Add value : " + value);
326 if (value instanceof Number) {
327 if (value instanceof Float || value instanceof Double) {
328 switch (numberInterpolation) {
329 case PREVIOUS_SAMPLE:
332 // System.err.println(" previous .. done!");
333 result.add(((Number) value).doubleValue());
338 case LINEAR_INTERPOLATION:
339 if (time != iter.getValueBand().getTimeDouble() && iter.hasNext()) {
342 int currentIndex = iter.getIndex();
343 ValueBand band = iter.getValueBand();
344 //double t1 = band.getTimeDouble();
345 Number v1 = (Number) value;
346 double t12 = band.getEndTimeDouble();
348 double t2 = iter.getValueBand().getTimeDouble();
349 Number v2 = (Number) iter.getValueBand().getValue();
350 iter.gotoIndex(currentIndex);
352 double vs = v1.doubleValue();
354 vs = HistoryExportUtil.biglerp(t12, v1.doubleValue(), t2, v2.doubleValue(), time);
357 //System.err.println(" linear .. done!");
362 // Exact timestamp match, or last sample.
363 // Don't interpolate nor extrapolate.
365 //System.err.println(" else .. done!");
366 result.add(((Number) value).doubleValue());
371 throw new UnsupportedOperationException("Unsupported interpolation: " + numberInterpolation);
374 throw new IllegalStateException("Value is not a number " + value);
376 } else if (value instanceof Boolean) {
378 result.add( (Boolean)value ? 1.0: 0.0);
380 throw new IllegalStateException("Value is not a number " + value);
386 // Read next values, and the following times
387 if ( timeStep>0.0 ) {
388 bigTime = bigTime.add(bigTimeStep);
389 time = bigTime.doubleValue();
391 // Get smallest end time that is larger than current time
392 Double nextTime = null;
393 // System.out.println("time = "+time);
394 if(!iter.hasNext()) break;
395 Double itemNextTime = iter.getNextTime( time );
396 // System.err.println(" "+i.label+" nextTime="+itemNextTime);
397 if ( nextTime == null || ( nextTime > itemNextTime && !itemNextTime.equals( time ) ) ) nextTime = itemNextTime;
398 if ( nextTime == null || nextTime.equals( time ) ) break;
402 boolean hasMore = false;
404 iter.proceedToTime(time);
405 if(HistoryExportUtil.contains(iter, time)) hasMore = true;
409 } while (time<=_end);
411 //System.err.println("=> " + Arrays.toString(result.toArray()));