Compare commits

...

4 commits

Author SHA1 Message Date
158bd8f62b
Fix error
Made by accident, IDEA warned before committing, but as I always see some warnings there I just clicked without thinking
2024-09-24 18:59:51 +02:00
b5482b8962
Getting busy, move implementation to another package 2024-09-24 18:58:15 +02:00
7930e4dd0a
Add cache for time provider
The improvement is surely negligible, perhaps harming performance, so more of a gimmick
2024-09-24 18:57:55 +02:00
1f1a3260c0
IDEA wants another file 2024-09-24 18:56:31 +02:00
7 changed files with 94 additions and 26 deletions

View file

@ -8,5 +8,5 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="temurin-21" project-jdk-type="JavaSDK" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="graalvm-ce-17" project-jdk-type="JavaSDK" />
</project>

View file

@ -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;

View file

@ -0,0 +1,40 @@
package eu.m724.wtapi.provider.twilight;
import eu.m724.wtapi.object.Coordinates;
import eu.m724.wtapi.object.Twilight;
import java.time.LocalDate;
// TODO reconsider if this is necessary and doesn't actually harm performance
/**
* A {@link TwilightTimeProvider} that can cache some calculations to be faster
*
* @param <T> An implementation of {@link TwilightTimeCache} where you store whatever you need
*/
public abstract class CacheableTwilightTimeProvider<T extends TwilightTimeCache> extends TwilightTimeProvider {
/**
* Calculates sunrise and sunset for provided coordinates
*
* @param cache {@link T}, implementation of {@link TwilightTimeCache}
* @param coordinates Coordinates of the observer
* @return {@link Twilight}
* @see #initializeCache(LocalDate)
*/
public abstract Twilight calculateTwilightTime(T cache, Coordinates coordinates);
/**
* Initializes cache for provided date
*
* @param date The UTC date
* @return ready to use cache
*/
public abstract T initializeCache(LocalDate date);
@Override
public Twilight calculateTwilightTime(LocalDate date, Coordinates coordinates) {
return calculateTwilightTime(
initializeCache(date),
coordinates
);
}
}

View file

@ -0,0 +1,4 @@
package eu.m724.wtapi.provider.twilight;
public interface TwilightTimeCache {
}

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,8 @@
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 java.time.Duration;
import java.time.LocalDate;
@ -9,13 +10,40 @@ 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: <a href="https://gml.noaa.gov/grad/solcalc/solareqns.PDF">https://gml.noaa.gov/grad/solcalc/solareqns.PDF</a>
* 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<br>
* 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
public Twilight calculateTwilightTime(LocalDate date, Coordinates coordinates) {
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
double n1 = cos(1.5853349194640094) / (cos(latRad) * cos(declination));
double n2 = tan(latRad) * tan(declination);
double hourAngle = acos(n1 - n2);
double hourAngleDeg = 4 * toDegrees(hourAngle);
// sunrise = 720 - 4 * (coordinates.longitude + hourAngleDeg) - equationOfTime
// sunset = 720 - 4 * (coordinates.longitude - hourAngleDeg) - equationOfTime
double solarNoon = 720 - 4 * coordinates.longitude - equationOfTime;
double sunrise = solarNoon - hourAngleDeg;
double sunset = solarNoon + hourAngleDeg;
return new Twilight(
cache.date(),
Duration.ofMillis((long) (sunrise * 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;
@ -33,24 +61,10 @@ public class ApproximateTwilightTimeProvider extends TwilightTimeProvider {
- 0.002697 * cos(3 * fractionalYear)
+ 0.00148 * sin(3 * fractionalYear);
double latRad = toRadians(coordinates.latitude);
// 90.833 deg = 1.5853349194640094 rad
double n1 = cos(1.5853349194640094) / (cos(latRad) * cos(declination));
double n2 = tan(latRad) * tan(declination);
double hourAngle = acos(n1 - n2);
double hourAngleDeg = 4 * toDegrees(hourAngle);
// sunrise = 720 - 4 * (coordinates.longitude + hourAngleDeg) - equationOfTime
// sunset = 720 - 4 * (coordinates.longitude - hourAngleDeg) - equationOfTime
double solarNoon = 720 - 4 * coordinates.longitude - equationOfTime;
double sunrise = solarNoon - hourAngleDeg;
double sunset = solarNoon + hourAngleDeg;
return new Twilight(
return new ApproximateTwilightTimeCache(
date,
Duration.ofMillis((long) (sunrise * 60000)),
Duration.ofMillis((long) (sunset * 60000))
equationOfTime,
declination
);
}
}

View file

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