Created
September 30, 2015 02:10
-
-
Save aolinto/7b2fc0302877e88b5673 to your computer and use it in GitHub Desktop.
R function to calculate lunar phases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lunar<- function(Y,M,D) { | |
# http://www.paulsadowski.com/wsh/moonphase.htm | |
# necessita do pacote cars | |
P2<-2*3.14159 | |
YY<-Y-as.integer((12-M)/10) | |
MM=M+9 | |
if (MM>=12) MM<-MM-12 | |
K1=as.integer(365.25*(YY+4712)) | |
K2=as.integer(30.6*MM+.5) | |
K3=as.integer(as.integer((YY/100)+49)*.75)-38 | |
# J é a data às 12h UT do dia em questão | |
J<-K1+K2+D+59 | |
if(J>2299160) J<-J-K3 | |
# Calcula a fase sinódica | |
V<-(J-2451550.1)/29.530588853 | |
V<-V-as.integer(V) | |
if(V<0) V<-V+1 | |
IP<-V | |
# Idade da Lua em dias | |
AG<-IP*29.53 | |
IP<-IP*P2 # Converte fase em radianos | |
# Calcula a distância | |
V<-(J-2451562.2)/27.55454988 | |
V<-V-as.integer(V) | |
if(V<0) V<-V+1 | |
DP<-V | |
DP<-DP*P2 # Converte em radianos | |
DI<-60.4-3.3*cos(DP)-.6*cos(2*IP-DP)-.5*cos(2*IP) | |
# Calcula a Latitude | |
V<-(J-2451565.2)/27.212220817 | |
V<-V-as.integer(V) | |
if(V<0) V<-V+1 | |
NP<-V | |
NP<-NP*P2 # Converte em radianos | |
LA<-5.1*sin(NP) | |
# Calcula a Longitude | |
V<-(J-2451555.8)/27.321582241 | |
# Normaliza valores para o intervalo de 0 a 1 | |
V<-V-as.integer(V) | |
if(V<0) V<-V+1 | |
RP<-V | |
LO<-360*RP+6.3*sin(DP)+1.3*sin(2*IP-DP)+.7*sin(2*IP) | |
# fases em inglês | |
# http://home.hiwaay.net/~krcool/Astro/moon/moonphase/ | |
Phase<-c("nova","crescente concava","quarto crescente","crescente convexa","cheia","minguante convexa","quarto minguante","minguante concava") | |
ThisPhase<-recode(as.integer(AG),"c(0,29)=1;c(1,2,3,4,5,6)=2;c(7)=3;c(8,9,10,11,12,13)=4;c(14)=5;c(15,16,17,18,19,20,21)=6;c(22)=7;c(23,24,25,26,27,28)=8") | |
message("Idade da lua (dias): ", as.integer(AG)) | |
message("Fase da lua: ",Phase[ThisPhase]) | |
message("Distância (raio): ",round(DI,2)) | |
message("Latitude Elíptica (graus): ",round(LA,2)) | |
message("Longitude Elíptica (graus): ",round(LO,2)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
library(car)
lunar(2011,8,31)
Idade da lua (dias): 2
Fase da lua: crescente concava
Distância (raio): 56.67
Latitude Elíptica (graus): -4.8
Longitude Elíptica (graus): 190.11