Uncertainty Propagation

The Observer{T} element type is only constrained to Real, so measurement values flow through every algorithm and solar positions work with Measurements.jl. See the Automatic Differentiation guide for the other thing that genericity buys, and the Numeric Precision guide for the supported number types themselves.

Give the coordinates an uncertainty and it reaches every returned angle:

using SolarPositionusing Datesusing Measurementsobs = Observer(52.35888 ± 0.01, 4.88185 ± 0.01)  # Van Gogh Museum, Amsterdampos = solar_position(obs, DateTime(2023, 6, 21, 12))
SolPos(azimuth=188.399 ± 0.019°, elevation=60.8792 ± 0.0099°, zenith=29.1208 ± 0.0099°)

Correlations

Results that share an input stay consistent. Zenith is derived from elevation, so their sum carries no uncertainty at all:

pos.elevation + pos.zenith

\[90.0 \pm 0.0\]

The vectorized, in-place, and table interfaces all carry uncertainties through unchanged:

times = collect(DateTime(2023, 6, 21, 10):Hour(2):DateTime(2023, 6, 21, 14))solar_position(obs, times).elevation
3-element Vector{Measurements.Measurement{Float64}}:
 55.1321 ± 0.0084
 60.8792 ± 0.0099
 50.9926 ± 0.0076
using DataFramessolar_position(DataFrame(datetime = times), obs)
3×4 DataFrame
Rowdatetimeazimuthelevationzenith
DateTimeMeasurem…Measurem…Measurem…
12023-06-21T10:00:00136.191±0.01755.1321±0.008434.8679±0.0084
22023-06-21T12:00:00188.399±0.01960.8792±0.009929.1208±0.0099
32023-06-21T14:00:00235.526±0.01650.9926±0.007639.0074±0.0076

How much does a coordinate error matter?

A GPS fix carries its own uncertainty, so a natural question is how much of it reaches the computed angles.

m_per_deg_lat = 111_320.0m_per_deg_lon = 111_320.0 * cosd(52.35888)sigma_m = 10 .^ range(-2, 3; length = 60)angles = [    solar_position(        Observer(52.35888 ± (s / m_per_deg_lat), 4.88185 ± (s / m_per_deg_lon)),        DateTime(2024, 6, 21, 12), SPA(), NoRefraction(),    ) for s in sigma_m]sigma_elev = [Measurements.uncertainty(p.elevation) for p in angles]sigma_azim = [Measurements.uncertainty(p.azimuth) for p in angles]

Plotted against the accuracy the algorithms themselves claim:

using CairoMakieblue, orange = "#0072B2", "#E69F00"fig = Figure(size = (700, 460))ax = Axis(    fig[1, 1];    xscale = log10, yscale = log10,    xlabel = "horizontal position uncertainty (m)",    ylabel = "resulting angular uncertainty (°)",    title = "Coordinate uncertainty against algorithm accuracy",    xgridcolor = (:gray, 0.2), ygridcolor = (:gray, 0.2),)# a typical consumer GNSS fix, roughly 3 to 10 mvspan!(ax, 3, 10; color = (:gray, 0.12))text!(ax, 5.5, 2.0e-7; text = "consumer\nGNSS", align = (:center, :bottom),    color = :gray, fontsize = 11)hlines!(ax, [0.0003]; xmax = 0.9, color = (:gray, 0.7), linestyle = :dash)hlines!(ax, [0.0083]; xmax = 0.9, color = (:gray, 0.7), linestyle = :dash)text!(ax, 0.012, 0.0003; text = "SPA ±0.0003°", align = (:left, :bottom),    color = :gray, fontsize = 11)text!(ax, 0.012, 0.0083; text = "PSA ±0.0083°", align = (:left, :bottom),    color = :gray, fontsize = 11)lines!(ax, sigma_m, sigma_azim; color = orange, linewidth = 2, label = "azimuth")lines!(ax, sigma_m, sigma_elev; color = blue, linewidth = 2, label = "elevation")text!(ax, 1300, sigma_azim[end]; text = "azimuth", color = orange,    align = (:left, :center), fontsize = 12)text!(ax, 1300, sigma_elev[end]; text = "elevation", color = blue,    align = (:left, :center), fontsize = 12)xlims!(ax, 0.008, 5000)axislegend(ax; position = :rb, framevisible = false)fig
Example block output

Refraction parameters

Refraction models accept pressure and temperature with uncertainties in the same way:

exact = Observer(52.35888, 4.88185)low = DateTime(2023, 12, 21, 8)  # a low winter sun, where refraction is largestsolar_position(exact, low, PSA(), HUGHES(101325.0 ± 500.0, 12.0 ± 2.0))
ApparentSolPos(azimuth=131.60591634226506 ± 0.0°, elevation=0.560174802251171 ± 0.0°, zenith=89.43982519774883 ± 0.0°,
    apparent_elevation=0.967 ± 0.0035°, apparent_zenith=89.033 ± 0.0035°

The geometric angles come back exact, since they do not depend on the atmosphere.

Sunrise and sunset

A DateTime cannot carry an uncertainty, so transit_sunrise_sunset returns the nominal times instead:

transit_sunrise_sunset(Observer(52.35888 ± 5.0, 4.88185 ± 5.0), Date(2023, 6, 21)).sunrise
2023-06-21T03:18:05

transit_sunrise_sunset_seconds keeps the observer's element type instead, giving seconds since midnight UTC:

transit_sunrise_sunset_seconds(obs, Date(2023, 6, 21))
TransitSunriseSunset{Measurements.Measurement{Float64}}(42134.7 ± 2.4, 11885.5 ± 4.3, 72384.2 ± 4.3)

The next_/previous_ helpers return a DateTime and drop the uncertainty in the same way.