Skip to content

Commit

Permalink
Add MoonPosition.getTrueAltitude()
Browse files Browse the repository at this point in the history
  • Loading branch information
shred committed Jan 17, 2024
1 parent 1f6bf23 commit 1d2d19c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/main/java/org/shredzone/commons/suncalc/MoonPosition.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ public class MoonPosition {

private final double azimuth;
private final double altitude;
private final double trueAltitude;
private final double distance;
private final double parallacticAngle;

private MoonPosition(double azimuth, double altitude, double distance, double parallacticAngle) {
private MoonPosition(double azimuth, double altitude, double trueAltitude, double distance, double parallacticAngle) {
this.azimuth = (toDegrees(azimuth) + 180.0) % 360.0;
this.altitude = toDegrees(altitude);
this.trueAltitude = toDegrees(trueAltitude);
this.distance = distance;
this.parallacticAngle = toDegrees(parallacticAngle);
}
Expand Down Expand Up @@ -83,20 +85,40 @@ public MoonPosition execute() {

double pa = atan2(sin(h), tan(phi) * cos(mc.getTheta()) - sin(mc.getTheta()) * cos(h));

return new MoonPosition(horizontal.getPhi(), horizontal.getTheta() + hRef, mc.getR(), pa);
return new MoonPosition(
horizontal.getPhi(),
horizontal.getTheta() + hRef,
horizontal.getTheta(),
mc.getR(),
pa);
}
}

/**
* Moon altitude above the horizon, in degrees.
* <p>
* {@code 0.0} means the moon's center is at the horizon, {@code 90.0} at the zenith
* (straight over your head).
* (straight over your head). Atmospheric refraction is taken into account.
*
* @see #getTrueAltitude()
*/
public double getAltitude() {
return altitude;
}

/**
* The true moon altitude above the horizon, in degrees.
* <p>
* {@code 0.0} means the moon's center is at the horizon, {@code 90.0} at the zenith
* (straight over your head).
*
* @see #getAltitude()
* @since 3.8
*/
public double getTrueAltitude() {
return trueAltitude;
}

/**
* Moon azimuth, in degrees, north-based.
* <p>
Expand Down Expand Up @@ -126,6 +148,7 @@ public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("MoonPosition[azimuth=").append(azimuth);
sb.append("°, altitude=").append(altitude);
sb.append("°, true altitude=").append(trueAltitude);
sb.append("°, distance=").append(distance);
sb.append(" km, parallacticAngle=").append(parallacticAngle);
sb.append("°]");
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/org/shredzone/commons/suncalc/MoonPositionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.shredzone.commons.suncalc.Locations.*;

import java.util.TimeZone;

import org.assertj.core.data.Offset;
import org.junit.Test;

Expand All @@ -36,6 +38,7 @@ public void testCologne() {
.execute();
assertThat(mp1.getAzimuth()).as("azimuth").isCloseTo(304.8, ERROR);
assertThat(mp1.getAltitude()).as("altitude").isCloseTo(-39.6, ERROR);
assertThat(mp1.getTrueAltitude()).as("trueAltitude").isCloseTo(-39.6, ERROR);
assertThat(mp1.getParallacticAngle()).as("pa").isCloseTo(32.0, ERROR);

MoonPosition mp2 = MoonPosition.compute()
Expand All @@ -45,6 +48,7 @@ public void testCologne() {
.execute();
assertThat(mp2.getAzimuth()).as("azimuth").isCloseTo(179.9, ERROR);
assertThat(mp2.getAltitude()).as("altitude").isCloseTo(25.3, ERROR);
assertThat(mp2.getTrueAltitude()).as("trueAltitude").isCloseTo(25.3, ERROR);
assertThat(mp2.getDistance()).as("distance").isCloseTo(394709.0, DISTANCE_ERROR);
assertThat(mp2.getParallacticAngle()).as("pa").isCloseTo(0.0, ERROR);
}
Expand All @@ -58,6 +62,7 @@ public void testAlert() {
.execute();
assertThat(mp1.getAzimuth()).as("azimuth").isCloseTo(257.5, ERROR);
assertThat(mp1.getAltitude()).as("altitude").isCloseTo(-10.9, ERROR);
assertThat(mp1.getTrueAltitude()).as("trueAltitude").isCloseTo(-10.9, ERROR);
assertThat(mp1.getParallacticAngle()).as("pa").isCloseTo(7.5, ERROR);

MoonPosition mp2 = MoonPosition.compute()
Expand All @@ -67,6 +72,7 @@ public void testAlert() {
.execute();
assertThat(mp2.getAzimuth()).as("azimuth").isCloseTo(179.8, ERROR);
assertThat(mp2.getAltitude()).as("altitude").isCloseTo(-5.7, ERROR);
assertThat(mp2.getTrueAltitude()).as("trueAltitude").isCloseTo(-5.7, ERROR);
assertThat(mp2.getDistance()).as("distance").isCloseTo(393609.0, DISTANCE_ERROR);
assertThat(mp2.getParallacticAngle()).as("pa").isCloseTo(0.0, ERROR);
}
Expand All @@ -80,6 +86,7 @@ public void testWellington() {
.execute();
assertThat(mp1.getAzimuth()).as("azimuth").isCloseTo(311.3, ERROR);
assertThat(mp1.getAltitude()).as("altitude").isCloseTo(55.1, ERROR);
assertThat(mp1.getTrueAltitude()).as("trueAltitude").isCloseTo(55.1, ERROR);
assertThat(mp1.getParallacticAngle()).as("pa").isCloseTo(144.2, ERROR);

MoonPosition mp2 = MoonPosition.compute()
Expand All @@ -89,6 +96,7 @@ public void testWellington() {
.execute();
assertThat(mp2.getAzimuth()).as("azimuth").isCloseTo(0.5, ERROR);
assertThat(mp2.getAltitude()).as("altitude").isCloseTo(63.9, ERROR);
assertThat(mp2.getTrueAltitude()).as("trueAltitude").isCloseTo(63.9, ERROR);
assertThat(mp2.getDistance()).as("distance").isCloseTo(396272.0, DISTANCE_ERROR);
assertThat(mp2.getParallacticAngle()).as("pa").isCloseTo(-179.6, ERROR);
}
Expand All @@ -102,6 +110,7 @@ public void testPuertoWilliams() {
.execute();
assertThat(mp1.getAzimuth()).as("azimuth").isCloseTo(199.4, ERROR);
assertThat(mp1.getAltitude()).as("altitude").isCloseTo(-52.7, ERROR);
assertThat(mp1.getTrueAltitude()).as("trueAltitude").isCloseTo(-52.7, ERROR);
assertThat(mp1.getParallacticAngle()).as("pa").isCloseTo(168.3, ERROR);

MoonPosition mp2 = MoonPosition.compute()
Expand All @@ -111,6 +120,7 @@ public void testPuertoWilliams() {
.execute();
assertThat(mp2.getAzimuth()).as("azimuth").isCloseTo(0.1, ERROR);
assertThat(mp2.getAltitude()).as("altitude").isCloseTo(16.3, ERROR);
assertThat(mp2.getTrueAltitude()).as("trueAltitude").isCloseTo(16.3, ERROR);
assertThat(mp2.getDistance()).as("distance").isCloseTo(369731.0, DISTANCE_ERROR);
assertThat(mp2.getParallacticAngle()).as("pa").isCloseTo(-179.9, ERROR);
}
Expand All @@ -124,6 +134,7 @@ public void testSingapore() {
.execute();
assertThat(mp1.getAzimuth()).as("azimuth").isCloseTo(240.6, ERROR);
assertThat(mp1.getAltitude()).as("altitude").isCloseTo(57.1, ERROR);
assertThat(mp1.getTrueAltitude()).as("trueAltitude").isCloseTo(57.1, ERROR);
assertThat(mp1.getParallacticAngle()).as("pa").isCloseTo(64.0, ERROR);

MoonPosition mp2 = MoonPosition.compute()
Expand All @@ -133,8 +144,24 @@ public void testSingapore() {
.execute();
assertThat(mp2.getAzimuth()).as("azimuth").isCloseTo(180.0, ERROR);
assertThat(mp2.getAltitude()).as("altitude").isCloseTo(74.1, ERROR);
assertThat(mp2.getTrueAltitude()).as("trueAltitude").isCloseTo(74.1, ERROR);
assertThat(mp2.getDistance()).as("distance").isCloseTo(395621.0, DISTANCE_ERROR);
assertThat(mp2.getParallacticAngle()).as("pa").isCloseTo(0.0, ERROR);
}

@Test
public void testBaghdad() {
MoonPosition mp1 = MoonPosition.compute()
.on(2002, 12, 4, 16, 57, 0)
.latitude(33, 20, 0.0)
.longitude(44, 25, 0.0)
.heightFt(100.0)
.timezone("Asia/Baghdad")
.execute();
assertThat(mp1.getAzimuth()).as("azimuth").isCloseTo(241.1, ERROR);
assertThat(mp1.getAltitude()).as("altitude").isCloseTo(1.2, ERROR);
assertThat(mp1.getTrueAltitude()).as("trueAltitude").isCloseTo(0.8, ERROR);
assertThat(mp1.getParallacticAngle()).as("pa").isCloseTo(52.8, ERROR);
}

}

0 comments on commit 1d2d19c

Please sign in to comment.