diff --git a/src/main/java/eu/m724/wtapi/provider/Providers.java b/src/main/java/eu/m724/wtapi/provider/Providers.java index efc1823..0dfe691 100644 --- a/src/main/java/eu/m724/wtapi/provider/Providers.java +++ b/src/main/java/eu/m724/wtapi/provider/Providers.java @@ -3,7 +3,7 @@ package eu.m724.wtapi.provider; import eu.m724.wtapi.provider.exception.NoSuchProviderException; import eu.m724.wtapi.provider.thunder.ThunderProvider; import eu.m724.wtapi.provider.thunder.impl.blitzortung.BlitzortungProvider; -import eu.m724.wtapi.provider.twilight.ApproximateTwilightTimeProvider; +import eu.m724.wtapi.provider.twilight.impl.approximate.ApproximateTwilightTimeProvider; import eu.m724.wtapi.provider.twilight.TwilightTimeProvider; import eu.m724.wtapi.provider.weather.WeatherProvider; import eu.m724.wtapi.provider.weather.impl.openweathermap.OpenWeatherMapProvider; diff --git a/src/main/java/eu/m724/wtapi/provider/twilight/impl/approximate/ApproximateTwilightTimeCache.java b/src/main/java/eu/m724/wtapi/provider/twilight/impl/approximate/ApproximateTwilightTimeCache.java new file mode 100644 index 0000000..4187eea --- /dev/null +++ b/src/main/java/eu/m724/wtapi/provider/twilight/impl/approximate/ApproximateTwilightTimeCache.java @@ -0,0 +1,9 @@ +package eu.m724.wtapi.provider.twilight.impl.approximate; + +import eu.m724.wtapi.provider.twilight.TwilightTimeCache; + +import java.time.LocalDate; + +public record ApproximateTwilightTimeCache(LocalDate date, double equationOfTime, + double declination) implements TwilightTimeCache { +} diff --git a/src/main/java/eu/m724/wtapi/provider/twilight/ApproximateTwilightTimeProvider.java b/src/main/java/eu/m724/wtapi/provider/twilight/impl/approximate/ApproximateTwilightTimeProvider.java similarity index 64% rename from src/main/java/eu/m724/wtapi/provider/twilight/ApproximateTwilightTimeProvider.java rename to src/main/java/eu/m724/wtapi/provider/twilight/impl/approximate/ApproximateTwilightTimeProvider.java index 790fc11..663d73b 100644 --- a/src/main/java/eu/m724/wtapi/provider/twilight/ApproximateTwilightTimeProvider.java +++ b/src/main/java/eu/m724/wtapi/provider/twilight/impl/approximate/ApproximateTwilightTimeProvider.java @@ -1,7 +1,9 @@ -package eu.m724.wtapi.provider.twilight; +package eu.m724.wtapi.provider.twilight.impl.approximate; import eu.m724.wtapi.object.Coordinates; import eu.m724.wtapi.object.Twilight; +import eu.m724.wtapi.provider.twilight.CacheableTwilightTimeProvider; +import eu.m724.wtapi.provider.twilight.TwilightTimeCache; import java.time.Duration; import java.time.LocalDate; @@ -9,29 +11,16 @@ import java.time.LocalDate; import static java.lang.Math.*; /** - * Calculates approximate sunrise and sunset times - * Usually off by a few minutes, except the Poles which, during polar days, are up to few hours inaccurate - * Adapted from: https://gml.noaa.gov/grad/solcalc/solareqns.PDF + * Calculates approximate sunrise and sunset times
+ * Usually off by a few minutes, except the Poles which, during polar days, are up to few hours inaccurate
+ * I don't know why, I just copied stuff and maybe transformed a bit, I don't actually understand the topic
+ * Source: https://gml.noaa.gov/grad/solcalc/solareqns.PDF */ -public class ApproximateTwilightTimeProvider extends TwilightTimeProvider { +public class ApproximateTwilightTimeProvider extends CacheableTwilightTimeProvider { @Override - public Twilight calculateTwilightTime(LocalDate date, Coordinates coordinates) { - int dayOfYear = date.getDayOfYear() - 1; // -1 because we have to start from zero - // 2 * PI / 365 = 0.01721420632103996 - double fractionalYear = 0.01721420632103996 * dayOfYear; - - double equationOfTime = 229.18 * (0.000075 - + 0.001868 * cos(fractionalYear) - - 0.032077 * sin(fractionalYear) - - 0.014615 * cos(2 * fractionalYear) - - 0.040849 * sin(2 * fractionalYear)); - double declination = 0.006918 - - 0.399912 * cos(fractionalYear) - + 0.070257 * sin(fractionalYear) - - 0.006758 * cos(2 * fractionalYear) - + 0.000907 * sin(2 * fractionalYear) - - 0.002697 * cos(3 * fractionalYear) - + 0.00148 * sin(3 * fractionalYear); + public Twilight calculateTwilightTime(ApproximateTwilightTimeCache cache, Coordinates coordinates) { + double declination = cache.declination(); + double equationOfTime = cache.equationOfTime(); double latRad = toRadians(coordinates.latitude); // 90.833 deg = 1.5853349194640094 rad @@ -53,4 +42,30 @@ public class ApproximateTwilightTimeProvider extends TwilightTimeProvider { Duration.ofMillis((long) (sunset * 60000)) ); } + + @Override + public ApproximateTwilightTimeCache initializeCache(LocalDate date) { + int dayOfYear = date.getDayOfYear() - 1; // -1 because we have to start from zero + // 2 * PI / 365 = 0.01721420632103996 + double fractionalYear = 0.01721420632103996 * dayOfYear; + + double equationOfTime = 229.18 * (0.000075 + + 0.001868 * cos(fractionalYear) + - 0.032077 * sin(fractionalYear) + - 0.014615 * cos(2 * fractionalYear) + - 0.040849 * sin(2 * fractionalYear)); + double declination = 0.006918 + - 0.399912 * cos(fractionalYear) + + 0.070257 * sin(fractionalYear) + - 0.006758 * cos(2 * fractionalYear) + + 0.000907 * sin(2 * fractionalYear) + - 0.002697 * cos(3 * fractionalYear) + + 0.00148 * sin(3 * fractionalYear); + + return new ApproximateTwilightTimeCache( + date, + equationOfTime, + declination + ); + } } diff --git a/src/test/java/eu/m724/wtapi/twilight/ApproximateTwilightTimeTest.java b/src/test/java/eu/m724/wtapi/twilight/ApproximateTwilightTimeTest.java index b15d861..0578096 100644 --- a/src/test/java/eu/m724/wtapi/twilight/ApproximateTwilightTimeTest.java +++ b/src/test/java/eu/m724/wtapi/twilight/ApproximateTwilightTimeTest.java @@ -2,12 +2,13 @@ package eu.m724.wtapi.twilight; import eu.m724.wtapi.object.Coordinates; import eu.m724.wtapi.object.Twilight; -import eu.m724.wtapi.provider.twilight.ApproximateTwilightTimeProvider; +import eu.m724.wtapi.provider.twilight.impl.approximate.ApproximateTwilightTimeProvider; import eu.m724.wtapi.provider.twilight.TwilightTimeProvider; import org.junit.Test; import java.time.LocalDate; +// TODO also test cached public class ApproximateTwilightTimeTest { /** * Acceptable discrepancy in minutes