Little useless-useful R function – Full moon finder

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!!!

Tagged with: , , , ,
Posted in Uncategorized
3 comments on “Little useless-useful R function – Full moon finder
  1. […] by data_admin [This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page […]

    Like

  2. […] Tomaz Kastrun has a not-so-useless function: […]

    Like

  3. […] article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here) […]

    Like

Leave a comment

Follow TomazTsql on WordPress.com
Programs I Use: SQL Search
Programs I Use: R Studio
Programs I Use: Plan Explorer
Rdeči Noski – Charity

Rdeči noski

100% of donations made here go to charity, no deductions, no fees. For CLOWNDOCTORS - encouraging more joy and happiness to children staying in hospitals (http://www.rednoses.eu/red-noses-organisations/slovenia/)

€2.00

Top SQL Server Bloggers 2018
TomazTsql

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Discover WordPress

A daily selection of the best content published on WordPress, collected for you by humans who love to read.

Revolutions

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Reeves Smith's SQL & BI Blog

A blog about SQL Server and the Microsoft Business Intelligence stack with some random Non-Microsoft tools thrown in for good measure.

SQL Server

for Application Developers

Business Analytics 3.0

Data Driven Business Models

SQL Database Engine Blog

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Search Msdn

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Data Until I Die!

Data for Life :)

Paul Turley's SQL Server BI Blog

sharing my experiences with the Microsoft data platform, Fabric, enterprise Power BI, SQL Server BI, Data Modeling, SSAS Design, SSRS, Dashboards & Visualization since 2009

Grant Fritchey

Intimidating Databases and Code

Madhivanan's SQL blog

A modern business theme

Alessandro Alpi's Blog

DevOps could be the disease you die with, but don’t die of.

Paul te Braak

Business Intelligence Blog

Sql Insane Asylum (A Blog by Pat Wright)

Information about SQL (PostgreSQL & SQL Server) from the Asylum.

Gareth's Blog

A blog about Life, SQL & Everything ...

SQLPam's Blog

Life changes fast and this is where I occasionally take time to ponder what I have learned and experienced. A lot of focus will be on SQL and the SQL community – but life varies.

William Durkin

William Durkin a blog on SQL Server, Replication, Performance Tuning and whatever else.

$hell Your Experience !!!

As aventuras de um DBA usando o Poder do $hell

Design a site like this with WordPress.com
Get started