Little useless-useful R function – Full moon finder

[This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Another one from the series [1,2,3,4] of useless functions. This one is particularly useful when you have overcast weather. And you are feeling moody, feel slightly off-balance and that causes you behaving erratically.

The full moon function, or should we call it fool moon – due to it’s simplistic and approximate nature, calculates the the difference between the date (only date, no time, no long/lat coordinates) and Julian constant. Should you be using a different calendar, don’t run the function, just look out the window.

The function is written based on generalized equation for julian day numbers and months. Another one could be to calculate RMSE of the predicted values and realization of lunar behavior (lunatic start time). In this case – reversed engineering – you would use the the approximate date/time for the first new moon after that date if the synod period was constant. This number than obtained is only empirically proven by recursively solving for the new “possible date/time” of lunar behavior and calculate the prediction error. In order to minimize the RMSE value of the difference between the full moon dates/times predicted formula and the dates/times for the full moon over the next 10 years you get something like this.

And the formula is pretty useless as well (using epoch Unix time format and the sine function. But this is not wacky enough (with help of WolframAlpha):

nn <- 1604007055 #today - now
#idea of calculation / rough approx.
((
                1 * (nn/2551442844-0.228535)
+ 0.0059199782 * sin(nn/5023359217 + 3.1705094)
- 0.0038844129 * sin(nn/4374537912 + 2.0017265)
+ 0.0176727762 * sin(nn/3789212683 - 1.5388144)
- 0.0004144842 * sin(nn/1385559605 - 1.2363316))%%1)

Final version of R function is:

IsItFullMoon <- function(){
  #when date (conert to 113) is 14 -> is getting full, else empty, based on Hijri (Kuwait) Calendar
  da <- Sys.Date()
  julianConstant <- 2451549.5
  cycle <- 29.53
  y <- as.integer(format(da, format="%Y"))
  m <- as.integer(format(da, format="%m"))
  d <- as.integer(format(da, format="%d"))
  
  # If the month is January or February, subtract 1 from the year and add 12 to the month.
  if(m==1 | m==2) {
    y <- y-1
    m <- m + 12
  }
  
  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
  new_moon = jd - 2451549.5
  semi <- new_moon/29.53
  dec <- semi%%1
  if (dec*cycle>= 14.50 & dec*cycle <= 15.50){
    print("Full Moon. Yay!")
  } else {
    print("You should wait. Go back to sleep")
  }
}  
IsItFullMoon()

As of writing this blog post, the function returns:

and I think, I will just to that – go to sleep. As always, the wacky version of R code is available at Github.

Happy R-ing!!!

To leave a comment for the author, please follow the link and comment on their blog: R – TomazTsql.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)