Retreiving current Lunar Phase

Would anyone here happen to know how I might retrieve the current lunar phase using ColdFusion?  I was thinking of perhaps finding and setting up some kind of database for this, but I'm now thinking that this might not even be necessary.  

Anyone know how I might achieve this programatically?  I've already looked in to using the DateLib UDF, -- but it doesn't seem to handle lunar phases.  Surely, this type of functionality can't be that complicated.  Anyone care to lend a hand?

Thanks,
 - Yvan
IDEASDesignAsked:
Who is Participating?
 
wytcomCommented:
Here is a function that gives the phase of the moon.  Input is a datetime in UTC.  Output is full, last quarter, new, or first quarter.

I tested it against the usno data and found that it produces good results over at least a 10 year period.  (They only show 10 years of data)

<!--- index.cfm   Dan Kelly   060418 --->

<cfif isDefined("form.dateMoon")>
  <cfset dateMoon = form.dateMoon>
<cfelse>
  <cfset dateMoon=now()>
</cfif>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "TR/html4/loose.dtd">
<html>
<head>
<title>Moon Phase per Date</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

<form name="formMoon" id="formMoon" method="post" action="index.cfm" >

Date:<input type="text" name="dateMoon" value="<cfoutput>#dateMoon#</cfoutput>" >
Phase:<cfoutput>#getPhase(dateMoon)#</cfoutput>
<input type="submit" value="Submit">

</form>
</body>
</html>

<cffunction name="getPhase" returntype="string">
      <cfargument name="dateMoon" type="date" required="yes">
      <cfset dateFull = createDateTime(2000, 1, 21, 4, 40, 0)>
      <cfset periodSeconds = 29.530589*86400>
      <cfset futureSeconds = dateDiff("s", dateFull, arguments.dateMoon)>
      <cfset phase = (futureSeconds mod periodSeconds)/periodSeconds>
      <cfif (phase gte .875) or (phase lt .125)><cfset result="full: #phase#">
      <cfelseif (phase gte .125) and (phase lt .375)><cfset result="last quarter: #phase#">
      <cfelseif (phase gte .375) and (phase lt .625)><cfset result="new: #phase#">
      <cfelse><cfset result="first quarter: #phase#"></cfif>
      <cfreturn result>
</cffunction>
0
 
danrosenthalCommented:
Here is a link: http://en.wikipedia.org/wiki/Full_moon_cycle

Looks like there is a new moon approx. every 29.53058885 days but it is a complicated formula if you want to be exact due to the elliptical orbits.
0
 
IDEASDesignAuthor Commented:
Well, since I'd like to retrieve the other lunar phases as well (not just the full moon phase), .. it seems that I might require some kind of database for this.   Anyone else have any suggestions?

- Yvan
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
JeffHowdenCommented:
The following may help:

http://aa.usno.navy.mil/
http://aa.usno.navy.mil/data/docs/RS_OneDay.html

Perhaps you can find something there that you can request, parse, and query for the needed info.
0
 
wytcomCommented:
You can combine the ideas given:  Check the accurate data given at the unno sites against a quick calculation using the given lunar period.  

Starting at a date-time of a full moon, you get another full moon every period (29.53058885).  

You will get the new moon at period/2 + period + 2*period ...

You will get a first quarter moon at period/4 + period + 2*period ...

You will get a third quarter moon at 3*period/4 + period + 2*period ...

I haven't done this calculation and checked it against the usno data but I would be surprised if it is not very accurate.  Keep in mind that you will be calculating a date and time of day, but that all you really need is the date of the moon phase.  This gives you lots of room to accept some deviation due to details.
0
 
JeffHowdenCommented:
Keep in mind, however that the accuracy of these calculations will be based on the timezone used for the origin of time.  For all others, the accuracy will be any number of hours incorrect.
0
 
RCorfmanCommented:
I don't know exactly how to do this, but this library looks like it has the support you are looking for.
http://www.mondtag.de.tt/
It is opensource and runs in the JRE. If you are using cf MX, you should be able to install the class library and use it... not something I have expertise in doing, other than to know this is entirely possible.
0
 
IDEASDesignAuthor Commented:
I appreciate all the feedback you folks have been giving me. Although I do understand the calculations that have been provided, I suspect that this approach would be a bit too complicated, considering that I'd have to map the span of days to the actual calendar dates.  And in examining the behavior of the Windows executable for the mondtag library, I do not believe that the JAR file equivalient of this would be able to achieve the type of functionionality I am looking for.  

What would be most ideal is if there was some kind of web service available that would allow me to pass a specific calendar date to it, and have it return the name of the lunar phase to me (New Moon, First Quarter, Full Moon, or Last Quarter) .  Anyone here know if something like this exists?  

- Yvan

P.S. The point that was brought up regarding how the timezone effects the lunar phases is perfectly valid.  However, at the point in time, I do not need that level of accuaracy. Thank you for mentioning it, however.  
0
 
IDEASDesignAuthor Commented:
Perfect!  Thank you so much!  This is exactly what I was looking for.  

You win :)

- Yvan
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.