Getting busy, move implementation to another package

This commit is contained in:
Minecon724 2024-09-24 18:58:15 +02:00
parent 7930e4dd0a
commit b5482b8962
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
4 changed files with 49 additions and 24 deletions

View file

@ -3,7 +3,7 @@ package eu.m724.wtapi.provider;
import eu.m724.wtapi.provider.exception.NoSuchProviderException; import eu.m724.wtapi.provider.exception.NoSuchProviderException;
import eu.m724.wtapi.provider.thunder.ThunderProvider; import eu.m724.wtapi.provider.thunder.ThunderProvider;
import eu.m724.wtapi.provider.thunder.impl.blitzortung.BlitzortungProvider; 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.twilight.TwilightTimeProvider;
import eu.m724.wtapi.provider.weather.WeatherProvider; import eu.m724.wtapi.provider.weather.WeatherProvider;
import eu.m724.wtapi.provider.weather.impl.openweathermap.OpenWeatherMapProvider; import eu.m724.wtapi.provider.weather.impl.openweathermap.OpenWeatherMapProvider;

View file

@ -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 {
}

View file

@ -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.Coordinates;
import eu.m724.wtapi.object.Twilight; 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.Duration;
import java.time.LocalDate; import java.time.LocalDate;
@ -9,29 +11,16 @@ import java.time.LocalDate;
import static java.lang.Math.*; import static java.lang.Math.*;
/** /**
* Calculates approximate sunrise and sunset times * Calculates approximate sunrise and sunset times<br>
* Usually off by a few minutes, except the Poles which, during polar days, are up to few hours inaccurate * Usually off by a few minutes, except the Poles which, during polar days, are up to few hours inaccurate<br>
* Adapted from: <a href="https://gml.noaa.gov/grad/solcalc/solareqns.PDF">https://gml.noaa.gov/grad/solcalc/solareqns.PDF</a> * I don't know why, I just copied stuff and maybe transformed a bit, I don't actually understand the topic<br>
* Source: <a href="https://gml.noaa.gov/grad/solcalc/solareqns.PDF">https://gml.noaa.gov/grad/solcalc/solareqns.PDF</a>
*/ */
public class ApproximateTwilightTimeProvider extends TwilightTimeProvider { public class ApproximateTwilightTimeProvider extends CacheableTwilightTimeProvider<ApproximateTwilightTimeCache> {
@Override @Override
public Twilight calculateTwilightTime(LocalDate date, Coordinates coordinates) { public Twilight calculateTwilightTime(ApproximateTwilightTimeCache cache, Coordinates coordinates) {
int dayOfYear = date.getDayOfYear() - 1; // -1 because we have to start from zero double declination = cache.declination();
// 2 * PI / 365 = 0.01721420632103996 double equationOfTime = cache.equationOfTime();
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);
double latRad = toRadians(coordinates.latitude); double latRad = toRadians(coordinates.latitude);
// 90.833 deg = 1.5853349194640094 rad // 90.833 deg = 1.5853349194640094 rad
@ -53,4 +42,30 @@ public class ApproximateTwilightTimeProvider extends TwilightTimeProvider {
Duration.ofMillis((long) (sunset * 60000)) 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
);
}
} }

View file

@ -2,12 +2,13 @@ package eu.m724.wtapi.twilight;
import eu.m724.wtapi.object.Coordinates; import eu.m724.wtapi.object.Coordinates;
import eu.m724.wtapi.object.Twilight; 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 eu.m724.wtapi.provider.twilight.TwilightTimeProvider;
import org.junit.Test; import org.junit.Test;
import java.time.LocalDate; import java.time.LocalDate;
// TODO also test cached
public class ApproximateTwilightTimeTest { public class ApproximateTwilightTimeTest {
/** /**
* Acceptable discrepancy in minutes * Acceptable discrepancy in minutes