X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=altoslib%2FAltosTimeSeries.java;h=5cf46c9ab888038ca2dd937aafdbcb1d340b2839;hp=30b24d82bcf3503fe6f059e7ae022c512f700994;hb=5c4e473ef5d13da9c2f356702cb8767b55aa2137;hpb=222158581887b5f9e8b9843d14321c313fa023fa diff --git a/altoslib/AltosTimeSeries.java b/altoslib/AltosTimeSeries.java index 30b24d82..5cf46c9a 100644 --- a/altoslib/AltosTimeSeries.java +++ b/altoslib/AltosTimeSeries.java @@ -16,11 +16,15 @@ package org.altusmetrum.altoslib_11; import java.util.*; -public class AltosTimeSeries implements Iterable { +public class AltosTimeSeries implements Iterable, Comparable { public String label; public AltosUnits units; ArrayList values; + public int compareTo(AltosTimeSeries other) { + return label.compareTo(other.label); + } + public void add(AltosTimeValue tv) { values.add(tv); } @@ -38,7 +42,7 @@ public class AltosTimeSeries implements Iterable { if (v0.time == v1.time) return (v0.value + v1.value) / 2; - return (v0.value * (v1.time - t) + v1.value * (t - v0.time)) / v1.time - v0.time; + return (v0.value * (v1.time - t) + v1.value * (t - v0.time)) / (v1.time - v0.time); } private int after_index(double time) { @@ -59,12 +63,18 @@ public class AltosTimeSeries implements Iterable { /* Compute a value for an arbitrary time */ public double value(double time) { int after = after_index(time); - if (after == 0) - return values.get(0).value; - if (after == values.size()) - return values.get(after - 1).value; + double ret; - return lerp(values.get(after-1), values.get(after), time); + if (after == 0) + ret = values.get(0).value; + else if (after == values.size()) + ret = values.get(after - 1).value; + else { + AltosTimeValue b = values.get(after-1); + AltosTimeValue a = values.get(after); + ret = lerp(b, a, time); + } + return ret; } /* Find the value just before an arbitrary time */ @@ -85,6 +95,16 @@ public class AltosTimeSeries implements Iterable { return values.get(after).value; } + public double time_of(double value) { + double last = AltosLib.MISSING; + for (AltosTimeValue v : values) { + if (v.value >= value) + return v.time; + last = v.time; + } + return last; + } + public int size() { return values.size(); } @@ -93,24 +113,69 @@ public class AltosTimeSeries implements Iterable { return values.iterator(); } - public double max() { - double max = AltosLib.MISSING; + public AltosTimeValue max() { + AltosTimeValue max = null; + for (AltosTimeValue tv : values) + if (max == null || tv.value > max.value) + max = tv; + return max; + } + + public AltosTimeValue max(double start_time, double end_time) { + AltosTimeValue max = null; for (AltosTimeValue tv : values) { - if (max == AltosLib.MISSING || tv.value > max) - max = tv.value; + if (start_time <= tv.time && tv.time <= end_time) + if (max == null || tv.value > max.value) + max = tv; } return max; } - public double min() { - double min = AltosLib.MISSING; + public AltosTimeValue min() { + AltosTimeValue min = null; for (AltosTimeValue tv : values) { - if (min == AltosLib.MISSING || tv.value < min) - min = tv.value; + if (min == null || tv.value < min.value) + min = tv; } return min; } + public AltosTimeValue min(double start_time, double end_time) { + AltosTimeValue min = null; + for (AltosTimeValue tv : values) { + if (start_time <= tv.time && tv.time <= end_time) + if (min == null || tv.value < min.value) + min = tv; + } + return min; + } + + public double average() { + double total = 0; + int count = 0; + for (AltosTimeValue tv : values) { + total += tv.value; + count++; + } + if (count == 0) + return AltosLib.MISSING; + return total / count; + } + + public double average(double start_time, double end_time) { + double total = 0; + int count = 0; + for (AltosTimeValue tv : values) { + if (start_time <= tv.time && tv.time <= end_time) { + total += tv.value; + count++; + } + } + if (count == 0) + return AltosLib.MISSING; + return total / count; + } + public AltosTimeSeries integrate(AltosTimeSeries integral) { double value = 0.0; double pvalue = 0.0; @@ -126,7 +191,6 @@ public class AltosTimeSeries implements Iterable { } pvalue = v.value; time = v.time; -// System.out.printf("%g %g %g\n", time, v.value, value); integral.add(time, value); } @@ -205,9 +269,11 @@ public class AltosTimeSeries implements Iterable { double j_right = j == right ? right_time : values.get(j+1).time; double interval = (j_right - j_left) / 2.0; double coeff = filter_coeff(j_time - center_time, width) * interval; + double value = values.get(j).value; + double partial = value * coeff; total_coeff += coeff; - total_value += coeff * values.get(j).value; + total_value += partial; } } if (total_coeff != 0.0)