Find Methode : Moon Phase

Anglais

Hello, I’m looking for a method of all done because I can t do it, which gives me the moon phase, whatever the Maniere I would adapt the code, I’m looking for a method, a source that allows me to know the Moonphases … thank you for your help

Français :

bonjour, je cherche une méthode toutes faite car j’y arrive pas, qui me donne la phase de la lune, quelque soit la maniere j’adapterait le code, je cherche une méthode, un source qui me permet de connaitre les phases de lune… merci d’avance pour votre aide

I found this document:
https://www.subsystems.us/uploads/9/8/9/4/98948044/moonphase.pdf
and wrote the following Xojo function:

Function MoonPhase(dt As Date) as Double
  'Returns phase of moon as a decimal value between 0 and 1
  'Zero indicates new moon, 0.5 indicates full moon
  dim y,m,d,a,b,c,e,f As Integer
  dim jd,dn,nm,pm As Double
  y = dt.Year
  m = dt.Month
  d = dt.Day
  a = y\\100
  b = a\\4
  c = 2-a+b
  e = 365.25*(y+4716)
  f = 30.6001*(M+1)
  jd = c+d+e+f-1524.5
  dn = jd-2451549.5
  nm = dn/29.53
  pm = nm-(nm\\1)
  Return pm
End Function

You’ve probably just done his homework for him :wink:

Could be, but I won’t be there to help with the final exam. :slight_smile:

I like b(ees), Bees are cool and often forgotten.

Hello :slight_smile:

No, I am developing a personal software, I have tried the function, it will return never 0.5 or 0 … so I have rounded the result by making a round (moonphase * 100)/100 … It gives me the right Moon and not all the Moon, lag of one day or not … I converted two different sources of Java from the same, and not even accurate problem

français :

non c’est pour moi, je dévelope un logiciel personnel, j’ai essayé la fonction, elle ne retourne jamai 0.5 ou 0… donc j’ai arrondi le resultat en faisant un round(moonphase*100)/100… elle me donne pas les bonnes lune et pas toutes les lune, décalage de un jour ou pas… j’ai convertie deux sources different de provenance de java et même problème pas exacte

To verify the result

http://www.w33kn.com/#calendar

Yes, that is true, unless the change of phase of the moon happens to occur exactly at midnight.

So no source available has your knowledge that actually works?

this one gives 23 days for today, which seems correct

Project: moonphase

Window1
Class Window1
Inherits Window
Events
Sub Open()
dim d as new Date
TFDay.Text = str( d.Day) TFMonth.Text = str( d.Month) TFYear.Text = str( d.Year)
End Sub

Methods

Sub Calculate()
dim d,m,y as Integer d = val(TFDay.Text)
m = val(TFMonth.Text) y = val(TFYear.Text)
dim ph as Double = MoonAge( d,m,y) TFPhase.Text = format( ph, "00.00")
End Sub

Function JulianDate(d as Integer, m as Integer, y as Integer) As Integer 
dim mm, yy as Integer
dim k1, k2, k3 as Integer
dim j as Integer
yy = y - Floor((12 - m) / 10) mm = m + 9
if (mm >= 12) then mm = mm - 12
k1 = floor((365.25 * (yy + 4712)))
k2 = floor((30.6001 * mm + 0.5))
k3 = floor(floor((yy / 100) + 49) * 0.75) - 38
// 'j' for dates in Julian calendar:
j = k1 + k2 + d + 59
// For Gregorian calendar:
if (j > 2299160) then j = j - k3 // 'j' is the Julian date at 12h UT (Universal Time) return j
End Function

Function MoonAge(d as Integer, m as Integer, y as Integer) As Double 
dim j as Integer = JulianDate(d, m, y)
Dim ip As Double
Dim ag As Double
 //Calculate the approximate phase of the moon
ip = (j + 4.867) / 29.53059 ip = ip - Floor(ip)
//After several trials I've seen to add the following lines, //which gave the result was not bad
if(ip < 0.5) then
ag = ip * 29.53059 + 29.53059 / 2 else
ag = ip * 29.53059 - 29.53059 / 2 end if
   // Moon's age in days
ag = Floor(ag) + 1
return ag 
End Function

Window1 Control Label1:

Window1 Control TFDay:
Sub TextChange() Calculate
End Sub

Window1 Control Label2:

Window1 Control TFMonth:
Sub TextChange() Calculate
End Sub

Window1 Control Label3:

Window1 Control TFYear:
Sub TextChange() Calculate
End Sub

Window1 Control Label4:
 Window1 Control TFPhase: 
End Class

The formula works. I’ve tested it. It appears that the problem is in how you are trying to use it.

From the testing that I did, the one that I posted is also correct. However, it gives the moon phase as of midnight at the beginning of the specifed day. If the full moon begins several hours before this time, or several hours after this time, then it will give a number slightly higher or slightly lower than 0.5. It won’t give a constant fixed number for the duration of the full moon or any other phase. If you need a fixed number you can round the value. For example you can multiply the value by 8 and then round it to the nearest integer. That will give you 8 values that cover new moon crescents, quarters, gibous moon, full moon, etc. You can also increase precision by including the number of hours since midnight as a decimal fraction in the ‘day’ variable.

You should also be aware that these are approximate formulae, but should be accurate to the nearest day. For the most accurate calculations, you need to take into account your latitude, longitude, and time zone. These calculations become extremely complex.

Also, there isn’t necessarily one single formula that’s suitable for every application.

The function returns a number between 1 and 30, full white moon 30 and full Moon 15, but it is not precisely because Moon is not a the right date, it would require a calculation method that gives the percentage of the moon between 0 and 100% with presision

Hamery, if you want Robert’s code to return a number from 0 to 29.5, just change the last line, from:

Return pm

to

Return pm * 29.53

Note: Code still missing step 2) from the PDF (about January and February)

you did not tell us the precision you need.
I start thinking you don’t search for xojo code to calculate the moon phases,
but rather a source code that is precise enough to suit your need.
I think you’ve better ask this question first on an astronomer forum,
then when you have the algorithm with the wanted precision ,
come here and we can source it with xojo.

Okay, I’m gonna do this, thanks for your help, this forum is super…

Jean-Yves Pochez tu est franais, je le suis galement je vie a Nantes(44000)… je pense que ton ide n’est pas mauvaise, je vais voir si je trouve un forum serieux d’astrologie et voir si je peux piquer un source assez prci, j’ai un source que j’ai converti de java a Xojo, mais sur les 12 lune noire de 2018 il ce plante sur une des lune de un jour, donc ce code n’est pas bon, j’ai essayez different code trouver et il manque tous de prcision et donne des dates qui ne sont pas les bonnes a un jour prs… ou alors c’est moi qui ne sais pas exploiter correctement le rsultat… est ce que tu veux que je te mette le code ici pour que tu jette un oeil dessus, pour m’aider a ce qu’il retourn le poucentage de la phase de lune ? a oui je dbute sur Xojo, j’tait dvelopeur de 1982 2001 sur d’autre logiciel et je viens de reprendre le dvelopement cel fait 2 semaine avec Xojo qui est super :slight_smile:

Jean-Yves Pochez, c’est ton source qui donne le meilleur rsultat, en faite c’est le manque de prsision dans le resultat de 1 a 30 qui fait quelque erreur de jour, de un jour seulement et pas a tout les coup, si il retournait un pourcentage ce serait mieux je vais voir dans le source si je trouve comment faire, je vous tiens au courant de la suite…

Jean - Yves Pochez, it’s your source that gives the best result, in fact is the lack of Presion in the result of 1 a 30 that makes some day error, one day only and not suddenly, if he returned a percentage it would be better I go to the source if I find out how to do it, I would keep you posted on the sequel…

je ne vois pas trop quoi faire pour qu’il me retourne un pourcentage sur 100% avec la prcision qui va avec…

vous voulez bien m’aider ?

je vous met les source modifier a ma faon…

dim ph as Double = MoonAge( d2,m,y) rem demande de pourcentage de la lune


methode MoonAge(d as Integer, m as Integer, y as Integer) as Double

dim j as Integer = JulianDate(d, m, y)
Dim ip As Double
Dim ag As Double
//Calculate the approximate phase of the moon
ip = (j + 4.867) / 29.53059
ip = ip - Floor(ip)
//After several trials I’ve seen to add the following lines, //which gave the result was not bad
if(ip < 0.5) then
ag = ip * 29.53059 + 29.53059 / 2
else
ag = ip * 29.53059 - 29.53059 / 2
end if
// Moon’s age in days

'ag = Floor(ag) + 1

ag = round(ag) + 1
return ag


methode JulianDate(d as Integer, m as Integer, y as Integer) as Integer

dim mm, yy as Integer
dim k1, k2, k3 as Integer
dim j as Integer
yy = y - Floor((12 - m) / 10)
mm = m + 9
if (mm >= 12) then mm = mm - 12
k1 = floor((365.25 * (yy + 4712)))
k2 = floor((30.6001 * mm + 0.5))
k3 = floor(floor((yy / 100) + 49) * 0.75) - 38
// ‘j’ for dates in Julian calendar:
j = k1 + k2 + d + 59
// For Gregorian calendar:
if (j > 2299160) then j = j - k3 // ‘j’ is the Julian date at 12h UT (Universal Time)
return j

@Hamery Eric
Please write in English in the General forum.
There is a French forum here: https://forum.xojo.com/conversations/francais
And you can also write a direct message to Jean-Yves by clicking on his name.

Bonne journée :wink:

[quote=403954:@Jeremie Leroy]@Hamery Eric
Please write in English in the General forum.
There is a French forum here: https://forum.xojo.com/conversations/francais
And you can also write a direct message to Jean-Yves by clicking on his name.

Bonne journée ;)[/quote]

ok sorry :smiley: