Skip to content

Trapezoid area for time bin calculations

Daniel Lyons requested to merge trapezoid-time-bin-calculations into main

There is considerable in-code documentation as well as a notebook file, which will show you the non-interactive charts, that may help make this make sense, but the gist of it is this.

The difference between the duration of the observation and the window between the start and end times creates a trapezoid of pressure. The slope of the sides increases to 1 where the duration and the allowable window are the same, and flattens out as the range gets wider and the duration shorter. We can think of creating the pressure bins as basically computing the area of vertical slices of this shape. It turns out that vertical slices of linear functions also form trapezoids, so we are actually creating smaller vertical trapezoids out of a large horizontal trapezoid. Where the boundaries of the bin don't match up perfectly, we can just find the overlap of the bin with these regions and add the areas there where it is simple. The total will equal the time pressure in a given bin.

The idea here comes from calculus but you don't actually need any real calculus to implement it, and you won't find any calculus in this MR, just linear functions and trapezoid areas.

Part of the math gets a little gross dealing with wraparound (observations that start in the evening and complete in the morning) but it is handled in a completely general way, and there is some other grossness associated with floats that are "near" other floats, which can happen when the order of operations produces a float that "means" the same thing as another float with a different representation.

I have tried to comment the code so that it can be understood without referencing the notebook. Please let me know if you find it relatively easy to follow or if you see anything that seems out of place without a useful comment.

Merge request reports