|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2026 Contributors to the openHAB project |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information. |
| 6 | + * |
| 7 | + * This program and the accompanying materials are made available under the |
| 8 | + * terms of the Eclipse Public License 2.0 which is available at |
| 9 | + * http://www.eclipse.org/legal/epl-2.0 |
| 10 | + * |
| 11 | + * SPDX-License-Identifier: EPL-2.0 |
| 12 | + */ |
| 13 | +package org.openhab.binding.astro.internal.calc; |
| 14 | + |
| 15 | +import static org.openhab.binding.astro.internal.util.MathUtils.*; |
| 16 | + |
| 17 | +import java.time.Instant; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Set; |
| 20 | + |
| 21 | +import org.eclipse.jdt.annotation.NonNullByDefault; |
| 22 | +import org.openhab.binding.astro.internal.model.EclipseKind; |
| 23 | +import org.openhab.binding.astro.internal.model.Position; |
| 24 | +import org.openhab.binding.astro.internal.util.DateTimeUtils; |
| 25 | + |
| 26 | +/** |
| 27 | + * Calculates the eclipses for the astro object |
| 28 | + * |
| 29 | + * @author Gerhard Riegler - Initial contribution |
| 30 | + * @author Gaël L'hopital - Extracted from MoonCalc |
| 31 | + */ |
| 32 | +@NonNullByDefault |
| 33 | +public abstract class EclipseCalc extends AstroCalc { |
| 34 | + public record Eclipse(EclipseKind kind, double when) { |
| 35 | + public LocalizedEclipse withPosition(Position position) { |
| 36 | + return new LocalizedEclipse(this, position.getElevationAsDouble()); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public record LocalizedEclipse(Eclipse eclipse, double elevation) { |
| 41 | + public Instant when() { |
| 42 | + return DateTimeUtils.jdToInstant(eclipse.when); |
| 43 | + } |
| 44 | + |
| 45 | + public EclipseKind kind() { |
| 46 | + return eclipse.kind; |
| 47 | + } |
| 48 | + |
| 49 | + public boolean matches(EclipseKind otherKind) { |
| 50 | + return eclipse.kind.equals(otherKind); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public List<Eclipse> getNextEclipses(double startJd) { |
| 55 | + return validEclipses().stream().map(ek -> new Eclipse(ek, calculate(startJd, ek))).toList(); |
| 56 | + } |
| 57 | + |
| 58 | + public double calculate(double midnightJd, EclipseKind eclipse) { |
| 59 | + double tz = 0; |
| 60 | + double eclipseJd = 0; |
| 61 | + do { |
| 62 | + double k = varK(midnightJd, tz); |
| 63 | + tz += 1; |
| 64 | + eclipseJd = getEclipse(Math.floor(k) + getJDAjust(), eclipse); |
| 65 | + } while (eclipseJd <= midnightJd); |
| 66 | + return eclipseJd; |
| 67 | + } |
| 68 | + |
| 69 | + protected abstract double getJDAjust(); |
| 70 | + |
| 71 | + protected abstract double astroAdjust(EclipseKind ek, double e, double m, double m1, double g, double u, double jd); |
| 72 | + |
| 73 | + protected abstract Set<EclipseKind> validEclipses(); |
| 74 | + |
| 75 | + /** |
| 76 | + * Calculates the eclipse. |
| 77 | + */ |
| 78 | + protected double getEclipse(double kMod, EclipseKind eclipse) { |
| 79 | + double t = kMod / 1236.85; |
| 80 | + double f = varF(kMod, t); |
| 81 | + |
| 82 | + if (sinDeg(Math.abs(f)) > .36) { |
| 83 | + return 0; |
| 84 | + } |
| 85 | + |
| 86 | + double o = varO(kMod, t); |
| 87 | + double f1 = f - .02665 * sinDeg(o); |
| 88 | + double a1 = 299.77 + .107408 * kMod - .009173 * t * t; |
| 89 | + double e = varE(t); |
| 90 | + double m = varM(kMod, t); |
| 91 | + double m1 = varM1(kMod, t); |
| 92 | + double p = .207 * e * sinDeg(m) + .0024 * e * sinDeg(2 * m) - .0392 * sinDeg(m1) + .0116 * sinDeg(2 * m1) |
| 93 | + - .0073 * e * sinDeg(m1 + m) + .0067 * e * sinDeg(m1 - m) + .0118 * sinDeg(2 * f1); |
| 94 | + double q = 5.2207 - .0048 * e * cosDeg(m) + .002 * e * cosDeg(2 * m) - .3299 * cosDeg(m1) |
| 95 | + - .006 * e * cosDeg(m1 + m) + .0041 * e * cosDeg(m1 - m); |
| 96 | + double g = (p * cosDeg(f1) + q * sinDeg(f1)) * (1 - .0048 * cosDeg(Math.abs(f1))); |
| 97 | + double u = .0059 + .0046 * e * cosDeg(m) - .0182 * cosDeg(m1) + .0004 * cosDeg(2 * m1) - .0005 * cosDeg(m + m1); |
| 98 | + double jd = varJde(kMod, t); |
| 99 | + jd += .0161 * sinDeg(2 * m1) - .0097 * sinDeg(2 * f1) + .0073 * e * sinDeg(m1 - m) - .005 * e * sinDeg(m1 + m) |
| 100 | + - .0023 * sinDeg(m1 - 2 * f1) + .0021 * e * sinDeg(2 * m); |
| 101 | + jd += .0012 * sinDeg(m1 + 2 * f1) + .0006 * e * sinDeg(2 * m1 + m) - .0004 * sinDeg(3 * m1) |
| 102 | + - .0003 * e * sinDeg(m + 2 * f1) + .0003 * sinDeg(a1) - .0002 * e * sinDeg(m - 2 * f1) |
| 103 | + - .0002 * e * sinDeg(2 * m1 - m) - .0002 * sinDeg(o); |
| 104 | + return astroAdjust(eclipse, e, m, m1, g, u, jd); |
| 105 | + } |
| 106 | +} |
0 commit comments