Skip to content

Commit

Permalink
Merge pull request #11 from SimpleAstronomy/develop
Browse files Browse the repository at this point in the history
Fix bug with finding a full moon
  • Loading branch information
dustmachine authored May 4, 2019
2 parents b1233a7 + daa4c5b commit 29795c3
Show file tree
Hide file tree
Showing 22 changed files with 539 additions and 439 deletions.
23 changes: 0 additions & 23 deletions .classpath

This file was deleted.

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/target/
/bin/
.DS_Store
.classpath
.settings/
>>>>>>> master
3 changes: 0 additions & 3 deletions .settings/com.springsource.sts.maven.prefs

This file was deleted.

2 changes: 0 additions & 2 deletions .settings/org.eclipse.jdt.core.prefs

This file was deleted.

4 changes: 0 additions & 4 deletions .settings/org.eclipse.wst.common.project.facet.core.xml

This file was deleted.

8 changes: 0 additions & 8 deletions .settings/org.maven.ide.eclipse.prefs

This file was deleted.

8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# simple-astronomy-lib
A simple and free astronomy library for calculating moon phase.
A simple and free astronomy library for calculating moon phases.

A simple (and free) astronomy library for calculating moon phase, solar eclipses, etc. This is intended to be used as a library within other projects, thus the list of dependencies will remain as small as possible.

Expand All @@ -8,7 +8,11 @@ Initial code based on the book [_Practical Astronomy with your Calculator_](http
NOTE: additional collaborators welcome

### Current Functionality
* Moon Phase - find date for next phase of the moon (full or new)
Find dates for Moon Phases
* full moon
* new moon
* first quarter
* last quarter

### Getting Started / Example
For a brief page on how to use this library, read the [Getting Started page](https://github.com/dustmachine/simple-astronomy-lib/blob/wiki/GettingStartedExample.md)
Expand Down
7 changes: 2 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

<groupId>com.bradsbrain</groupId>
<artifactId>SimpleAstronomyLib</artifactId>
<version>0.2.0</version>
<version>0.3.0</version>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<version>4.4</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -45,8 +45,6 @@
</execution>
</executions>
</plugin>
<!-- FIXME: Damon, fix javadoc errors in the source. -->
<!--
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
Expand All @@ -59,7 +57,6 @@
</execution>
</executions>
</plugin>
-->
</plugins>
</build>

Expand Down
48 changes: 14 additions & 34 deletions src/main/java/com/bradsbrain/simpleastronomy/BaseUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,20 @@
*/
package com.bradsbrain.simpleastronomy;

import java.text.DateFormat;
import static java.text.DateFormat.SHORT;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public final class BaseUtils {

public static double exactDaysSince(Calendar myCal, double epoch) {
public static double exactDaysSince(ZonedDateTime myCal, double epoch) {
return JulianDate.makeJulianDateUsingMyModified(myCal) - epoch;
}




public static Calendar getSafeLocalCopy(long millis) {
// safe local copy
Calendar myCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
myCal.clear();
myCal.setTimeInMillis(millis);
return myCal;
}

public static double adjustTo360Range(double valToAdjust) {
double howMany = Math.floor(valToAdjust / 360);
double adjustedValue = valToAdjust - (howMany * 360);
return adjustedValue;
return valToAdjust - (howMany * 360);
}

public static double sinDegrees(double angleInDegrees) {
Expand All @@ -61,29 +47,23 @@ public static double useLessPrecision(double d, int precision) {
/**
* Useful date-to-string formatting which I found myself using a lot
*
* @param moonDate
* @param moonDate the date to format
* @return the date in GMT timezone
*/
public static String formatDateForGMT(Date moonDate) {
DateFormat sdf = SimpleDateFormat.getDateInstance(SHORT);
((SimpleDateFormat) sdf).applyPattern("yyyy-MM-dd");
((SimpleDateFormat) sdf).setTimeZone(TimeZone.getTimeZone("GMT"));
return sdf.format(moonDate);
public static String formatDateForGMT(ZonedDateTime moonDate) {
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return moonDate.withZoneSameInstant(ZoneOffset.UTC).format(df);
}

/**
* Useful date-to-string formatting which I found myself using a lot
*
* @param moonDate
* @param moonDate date to format
* @param tz the timezone with which to format the date
* @return the date in whatever timezone is default
*/
public static String formatDateAsShortDateLocalTime(Date moonDate, TimeZone tz) {
DateFormat sdf = SimpleDateFormat.getDateInstance(SHORT);
sdf.setTimeZone(tz);
((SimpleDateFormat) sdf).applyPattern("yyyy-MM-dd");
return sdf.format(moonDate);
public static String formatDateAsShortDateLocalTime(ZonedDateTime moonDate, ZoneId tz) {
return moonDate.withZoneSameInstant(tz).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
}



}
26 changes: 15 additions & 11 deletions src/main/java/com/bradsbrain/simpleastronomy/FullMoonFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@
*/
public class FullMoonFinder implements MoonFinder {

private static final int _360 = 360;

private static final double ROTATE_ANGLE = 179.9;
private static final double _360 = 360;

/**
* This value is close to 180, but the calculate yield more accurate results
* compared to observatories with this value than with a higher value.
*/
private static final double ROTATE_ANGLE = 179.95;

private static final double FULL_MOON_HALF_ANGLE = _360/ 2;

/** {@inheritDoc} */
public boolean isMoonBefore(double angle, double unused) {
double usefulAngle = (angle + ROTATE_ANGLE) % _360;
return usefulAngle < FULL_MOON_HALF_ANGLE;
}

private static final double FULL_MOON_HALF_ANGLE = _360/ 2;

/** {@inheritDoc} */
public boolean isMoonBefore(double angle, double unused) {
double usefulAngle = (angle + ROTATE_ANGLE) % _360;

return FULL_MOON_HALF_ANGLE > usefulAngle;
}
}
25 changes: 13 additions & 12 deletions src/main/java/com/bradsbrain/simpleastronomy/JulianDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/
package com.bradsbrain.simpleastronomy;

import java.util.Calendar;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

/**
* Heavily based on procedures as documented in the
Expand All @@ -30,19 +31,19 @@ public class JulianDate {
* This method is a simplification of formula in Section 3 of PAwyC3.
* We'll assume we're only talking about Gregorian Calendar dates because
* really, we don't care a whole lot about past dates.
*
* @param cal
* @return
*
* @param cal the input date
* @return the julian date
*/
public static Double makeJulianDateUsingMyModified(Calendar cal) {
Calendar myCal = BaseUtils.getSafeLocalCopy(cal.getTimeInMillis());
public static Double makeJulianDateUsingMyModified(ZonedDateTime cal) {
cal = cal.withZoneSameInstant(ZoneOffset.UTC);
// step 1
int year = myCal.get(Calendar.YEAR);
int month = myCal.get(Calendar.MONTH) + 1; // fix the January=0
double day = myCal.get(Calendar.DAY_OF_MONTH);
double hour = myCal.get(Calendar.HOUR_OF_DAY) / 24.0;
double minute = myCal.get(Calendar.MINUTE) / 24.0 / 60.0;
double second = myCal.get(Calendar.SECOND) / 24.0 / 60.0 / 60.0;
int year = cal.getYear();
int month = cal.getMonthValue();
double day = cal.getDayOfMonth();
double hour = cal.getHour() / 24.0;
double minute = cal.getMinute() / 24.0 / 60.0;
double second = cal.getSecond() / 24.0 / 60.0 / 60.0;

// step 2
if (month <= 2) {
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/bradsbrain/simpleastronomy/MoonFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
*/
public interface MoonFinder {

/**
* Determines if the desired moon type is earlier or later than the given information.
*
* @param angle a moon angle
* @param percent a moon visible percentage
* @return true if the moon is earlier in time than the supplied information
*/
boolean isMoonBefore(double angle, double percent);
/**
* Determines if the desired moon type is earlier or later than the given information.
*
* @param angle a moon angle
* @param percent a moon visible percentage
* @return true if the moon is earlier in time than the supplied information
*/
boolean isMoonBefore(double angle, double percent);

}
Loading

0 comments on commit 29795c3

Please sign in to comment.