Compare commits

..

3 commits

Author SHA1 Message Date
c670ba88bd
New feature: sunrise and sunset calculation 2024-09-24 18:34:07 +02:00
d2e812a70e
this can be final 2024-09-24 18:33:52 +02:00
0ef7fc22f1
more IDEA files 2024-09-24 18:33:39 +02:00
9 changed files with 319 additions and 1 deletions

View file

@ -11,6 +11,11 @@
<option name="name" value="724rocks" /> <option name="name" value="724rocks" />
<option name="url" value="https://git.724.rocks/api/packages/Minecon724/maven" /> <option name="url" value="https://git.724.rocks/api/packages/Minecon724/maven" />
</remote-repository> </remote-repository>
<remote-repository>
<option name="id" value="m724" />
<option name="name" value="m724" />
<option name="url" value="https://git.m724.eu/api/packages/Minecon724/maven" />
</remote-repository>
<remote-repository> <remote-repository>
<option name="id" value="central" /> <option name="id" value="central" />
<option name="name" value="Maven Central repository" /> <option name="name" value="Maven Central repository" />

124
.idea/uiDesigner.xml Normal file
View file

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

View file

@ -5,7 +5,7 @@ package eu.m724.wtapi.object;
* contains fields latitude and longitude * contains fields latitude and longitude
*/ */
public class Coordinates { public class Coordinates {
public double latitude, longitude; public final double latitude, longitude;
public String country, city; // TODO should it stay here? public String country, city; // TODO should it stay here?
public Coordinates(double latitude, double longitude) { public Coordinates(double latitude, double longitude) {

View file

@ -0,0 +1,27 @@
package eu.m724.wtapi.object;
import java.time.Duration;
import java.time.LocalDate;
public class Twilight {
/**
* The date this object contains times for
*/
public final LocalDate date;
/**
* Time of sunrise relative to UTC midnight of the date
*/
public final Duration sunrise;
/**
* Time of sunset relative to UTC midnight of the date
*/
public final Duration sunset;
public Twilight(LocalDate date, Duration sunrise, Duration sunset) {
this.date = date;
this.sunrise = sunrise;
this.sunset = sunset;
}
}

View file

@ -0,0 +1,51 @@
package eu.m724.wtapi.provider.twilight;
import eu.m724.wtapi.object.Coordinates;
import eu.m724.wtapi.object.Twilight;
import java.time.Duration;
import java.time.LocalDate;
import static java.lang.Math.*;
public class SimpleTwilightTimeProvider extends TwilightTimeProvider {
@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);
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(
date,
Duration.ofMillis((long) (sunrise * 60000)),
Duration.ofMillis((long) (sunset * 60000))
);
}
}

View file

@ -0,0 +1,22 @@
package eu.m724.wtapi.provider.twilight;
import eu.m724.wtapi.object.Coordinates;
import eu.m724.wtapi.object.Twilight;
import java.time.LocalDate;
// TODO make this an interface? also consider new name
/**
* Twilight refers to sunset and sunrise time<br>
* Is it the correct term? I don't know
*/
public abstract class TwilightTimeProvider {
/**
* Calculates sunrise and sunset for provided coordinates
*
* @param date UTC date
* @param coordinates Coordinates of the observer
* @return {@link Twilight}
*/
public abstract Twilight calculateTwilightTime(LocalDate date, Coordinates coordinates);
}

View file

@ -0,0 +1,46 @@
package eu.m724.wtapi.twilight;
import eu.m724.wtapi.object.Coordinates;
import eu.m724.wtapi.object.Twilight;
import eu.m724.wtapi.provider.twilight.SimpleTwilightTimeProvider;
import eu.m724.wtapi.provider.twilight.TwilightTimeProvider;
import org.junit.Test;
import java.time.LocalDate;
public class ApproximateTwilightTimeTest {
/**
* Acceptable discrepancy in minutes
*/
public static final int ACCEPTABLE_DIFFERENCE = 10;
@Test
public void approximateTest() {
TwilightTimeProvider provider = new SimpleTwilightTimeProvider();
testLocation(provider, 26, 6, 2023, 53.123394, 23.0864867, 122, 1139);
testLocation(provider, 13, 11, 2040, 45.432427, -122.3899276, 907, 1481);
testLocation(provider, 23, 3, 2021, 55.5024161, 9.6801853, 315, 1061);
testLocation(provider, 6, 8,1990, -72.012117, 2.5240873, 600, 832);
// TODO this is broken so fix
//testLocation(provider, 6, 9,2021, 82.498665, -62.3458366, 74, 1351); // date not on purpose as this was the first sunset in that location. first sunset since 5th april
}
private void testLocation(TwilightTimeProvider provider, int day, int month, int year, double latitude, double longitude, int actualSunrise, int actualSunset) {
LocalDate date = LocalDate.of(year, month, day);
Coordinates coordinates = new Coordinates(latitude, longitude);
Twilight twilight = provider.calculateTwilightTime(date, coordinates);
System.out.println();
System.out.println(date);
System.out.println(coordinates.latitude + " " + coordinates.longitude);
System.out.println("Calculated sunrise: " + date.atStartOfDay().plus(twilight.sunrise));
System.out.println("Actual sunrise: " + date.atStartOfDay().plusMinutes(actualSunrise));
assert Math.abs(twilight.sunrise.toMinutes() - actualSunrise) < ACCEPTABLE_DIFFERENCE;
System.out.println("Calculated sunset: " + date.atStartOfDay().plus(twilight.sunset));
System.out.println("Actual sunset: " + date.atStartOfDay().plusMinutes(actualSunset));
assert Math.abs(twilight.sunset.toMinutes() - actualSunset) < ACCEPTABLE_DIFFERENCE;
}
}

View file

@ -0,0 +1,20 @@
package eu.m724.wtapi.twilight;
import eu.m724.wtapi.object.Coordinates;
import eu.m724.wtapi.object.Twilight;
import eu.m724.wtapi.provider.twilight.TwilightTimeProvider;
import java.time.Duration;
import java.time.LocalDate;
public class MockTwilightTimeProvider extends TwilightTimeProvider {
@Override
public Twilight calculateTwilightTime(LocalDate date, Coordinates coordinates) {
int time = (int) (coordinates.latitude + coordinates.longitude);
return new Twilight(
date,
Duration.ofMinutes(-time),
Duration.ofMinutes(time)
);
}
}

View file

@ -0,0 +1,23 @@
package eu.m724.wtapi.twilight;
import eu.m724.wtapi.object.Coordinates;
import eu.m724.wtapi.object.Twilight;
import eu.m724.wtapi.provider.twilight.TwilightTimeProvider;
import org.junit.Test;
import java.time.LocalDate;
public class MockTwilightTimeTest {
@Test
public void testTwilight() {
TwilightTimeProvider provider = new MockTwilightTimeProvider();
LocalDate date = LocalDate.of(2077, 4, 20);
Coordinates coordinates = new Coordinates(52.4796012, 62.1847245);
Twilight twilight = provider.calculateTwilightTime(date, coordinates);
assert twilight.date.equals(date);
assert twilight.sunrise.getSeconds() == -6840;
assert twilight.sunset.getSeconds() == 6840;
}
}