Outsourced Moon phase calculation to GsweMoonPhaseData

This commit is contained in:
Gergely Polonkai 2013-10-04 01:18:34 +02:00
parent 20a350eacc
commit 5dcd5cdd17
4 changed files with 87 additions and 51 deletions

View File

@ -45,6 +45,8 @@ gswe_moment_get_type
gswe_moon_phase_data_new
gswe_moon_phase_data_ref
gswe_moon_phase_data_unref
gswe_moon_phase_data_calculate_by_timestamp
gswe_moon_phase_data_calculate_by_jd
gswe_moon_phase_data_get_phase
gswe_moon_phase_data_get_illumination
GsweMoonPhaseData

View File

@ -32,8 +32,6 @@
* one given point on Earth at a given time.
*/
#define SYNODIC 29.53058867
#define GSWE_MOMENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), GSWE_TYPE_MOMENT, GsweMomentPrivate))
/**
@ -972,62 +970,16 @@ gswe_moment_get_quality_points(GsweMoment *moment, GsweQuality quality)
GsweMoonPhaseData *
gswe_moment_get_moon_phase(GsweMoment *moment, GError **err)
{
gdouble difference,
phase_percent,
jd,
jdb;
if (moment->priv->moon_phase_revision == moment->priv->revision) {
return moment->priv->moon_phase;
}
jd = gswe_timestamp_get_julian_day(moment->priv->timestamp, err);
gswe_moon_phase_data_calculate_by_timestamp(moment->priv->timestamp, err);
if ((err) && (*err)) {
return NULL;
if (!err || !*err) {
moment->priv->moon_phase_revision = moment->priv->revision;
}
jdb = gswe_timestamp_get_julian_day(gswe_full_moon_base_date, err);
if ((err) && (*err)) {
return NULL;
}
difference = (jd - jdb);
phase_percent = fmod((difference * 100) / SYNODIC, 100);
if (phase_percent < 0) {
phase_percent += 100.0;
}
if ((phase_percent < 0) || (phase_percent > 100)) {
g_error("Error during Moon phase calculation!");
}
moment->priv->moon_phase->illumination = (50.0 - fabs(phase_percent - 50.0)) * 2;
if (phase_percent == 0) {
moment->priv->moon_phase->phase = GSWE_MOON_PHASE_NEW;
} else if (phase_percent < 25) {
moment->priv->moon_phase->phase = GSWE_MOON_PHASE_WAXING_CRESCENT;
} else if (phase_percent == 25) {
moment->priv->moon_phase->phase = GSWE_MOON_PHASE_WAXING_HALF;
} else if (phase_percent < 50) {
moment->priv->moon_phase->phase = GSWE_MOON_PHASE_WAXING_GIBBOUS;
} else if (phase_percent == 50) {
moment->priv->moon_phase->phase = GSWE_MOON_PHASE_FULL;
} else if (phase_percent < 75) {
moment->priv->moon_phase->phase = GSWE_MOON_PHASE_WANING_GIBBOUS;
} else if (phase_percent == 75) {
moment->priv->moon_phase->phase = GSWE_MOON_PHASE_WANING_HALF;
} else if (phase_percent < 100) {
moment->priv->moon_phase->phase = GSWE_MOON_PHASE_WANING_CRESCENT;
} else {
moment->priv->moon_phase->phase = GSWE_MOON_PHASE_DARK;
}
moment->priv->moon_phase_revision = moment->priv->revision;
return gswe_moon_phase_data_ref(moment->priv->moon_phase);
}

View File

@ -15,10 +15,16 @@
* You should have received a copy of the GNU General Public License
* along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include "gswe-types.h"
#include "swe-glib-private.h"
#include "gswe-moon-phase-data.h"
#include "gswe-moon-phase-data-private.h"
#include "gswe-timestamp.h"
#define SYNODIC 29.53058867
/**
* SECTION:gswe-moon-phase-data
@ -82,6 +88,79 @@ gswe_moon_phase_data_unref(GsweMoonPhaseData *moon_phase_data)
}
}
/**
* gswe_moon_phase_data_calculate_by_timestamp:
* @moon_phase_data: a #GsweMoonPhaseData
* @timestamp: a #GsweTimestamp with a valid timestamp set
* @err: a #GError
*
* Calculates the moon at a given time, specified by @jd.
*/
void
gswe_moon_phase_data_calculate_by_jd(GsweMoonPhaseData *moon_phase_data, gdouble jd, GError **err)
{
gdouble jdb,
phase_percent;
jdb = gswe_timestamp_get_julian_day(gswe_full_moon_base_date, err);
if ((err) && (*err)) {
return;
}
if ((phase_percent = fmod(((jd - jdb) * 100) / SYNODIC, 100)) < 0) {
phase_percent += 100.0;
}
if ((phase_percent < 0) || (phase_percent > 100)) {
g_error("Error during Moon phase calculation!");
}
moon_phase_data->illumination = (50.0 - fabs(phase_percent - 50.0)) * 2;
if (phase_percent == 0) {
moon_phase_data->phase = GSWE_MOON_PHASE_NEW;
} else if (phase_percent < 25) {
moon_phase_data->phase = GSWE_MOON_PHASE_WAXING_CRESCENT;
} else if (phase_percent == 25) {
moon_phase_data->phase = GSWE_MOON_PHASE_WAXING_HALF;
} else if (phase_percent < 50) {
moon_phase_data->phase = GSWE_MOON_PHASE_WAXING_GIBBOUS;
} else if (phase_percent == 50) {
moon_phase_data->phase = GSWE_MOON_PHASE_FULL;
} else if (phase_percent < 75) {
moon_phase_data->phase = GSWE_MOON_PHASE_WANING_GIBBOUS;
} else if (phase_percent == 75) {
moon_phase_data->phase = GSWE_MOON_PHASE_WANING_HALF;
} else if (phase_percent < 100) {
moon_phase_data->phase = GSWE_MOON_PHASE_WANING_CRESCENT;
} else {
moon_phase_data->phase = GSWE_MOON_PHASE_DARK;
}
}
/**
* gswe_moon_phase_data_calculate_by_timestamp:
* @moon_phase_data: a #GsweMoonPhaseData
* @timestamp: a #GsweTimestamp with a valid timestamp set
* @err: a #GError
*
* Calculates the moon at a given time, specified by @timestamp.
*/
void
gswe_moon_phase_data_calculate_by_timestamp(GsweMoonPhaseData *moon_phase_data, GsweTimestamp *timestamp, GError **err)
{
gdouble jd;
jd = gswe_timestamp_get_julian_day(timestamp, err);
if (*err) {
return;
}
gswe_moon_phase_data_calculate_by_jd(moon_phase_data, jd, err);
}
/**
* gswe_moon_phase_data_get_phase:
* @moon_phase_data: (in): a GsweMoonPhaseData

View File

@ -39,6 +39,9 @@ GsweMoonPhaseData *gswe_moon_phase_data_new(void);
GsweMoonPhaseData *gswe_moon_phase_data_ref(GsweMoonPhaseData *moon_phase_data);
void gswe_moon_phase_data_unref(GsweMoonPhaseData *moon_phase_data);
void gswe_moon_phase_data_calculate_by_jd(GsweMoonPhaseData *moon_phase_data, gdouble jd, GError **err);
void gswe_moon_phase_data_calculate_by_timestamp(GsweMoonPhaseData *moon_phase_data, GsweTimestamp *timestamp, GError **err);
GsweMoonPhase gswe_moon_phase_data_get_phase(GsweMoonPhaseData *moon_phase_data);
gdouble gswe_moon_phase_data_get_illumination(GsweMoonPhaseData *moon_phase_data);