Estoy teniendo el siguiente problema:
Estoy migrando un proyecto de Javascript puro a Angular2, necesito migrar o ver la forma de incluir la siguiente librería SunCalc, intente migrarla pero en algún momento al adaptar el código algo se rompió, explícitamente sólo necesito la parte de calcular el porcentaje de iluminación de la luna en un día dado.
Dejo el servicio que estuve armando con la librería por si alguno puede corregirlo o me dice cómo importar directamente desde javascript.
moon-calc.service.ts
import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions, URLSearchParams } from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map';
const LUNAS = [
'MALA',
'REGULAR',
'BUENA',
'EXELENTE'
];
const COLORES_PIQUE = [
'p-mala',
'p-regular',
'p-buena',
'p-exelente'
];
const MONTHS = [
'ENERO',
'FEBRERO',
'MARZO',
'ABRIL',
'MAYO',
'JUNIO',
'JULIO',
'AGOSTO',
'SEPTIEMBRE',
'OCTUBRE',
'NOVIEMBRE',
'DICIEMBRE',
];
const DAYS_OF_MONTH = [
31,
28,
31,
30,
31,
30,
31,
31,
30,
31,
30,
31
];
const dayMs = 1000 * 60 * 60 * 24;
const J1970 = 2440588;
const J2000 = 2451545;
const rad = Math.PI / 180;
const e = rad * 23.4397; // obliquity of the Earth
/*
* Implements part of SunCalc.js
*/
@Injectable()
export class MoonCalcService {
constructor() {}
public calculateCalendar(month, year) {
let nd = this.getDaysOfMonth(month, year);
let fd = this.getFirstDay(month, year);
let week = new Array();
let weeks = new Array();
for (let i = 1; i <= fd; i++) {
week.push({ day: ''});
}
let k = 1;
for (let d = 1; d <= nd; d++) {
if (week.length == 7) {
weeks.push(week);
week = new Array();
k += 1;
}
// correcion porque $scope.month va de 0 a 11 en ves de 1 a 12
let day_ = year + '-' + (month + 1) + '-' + this.pad(d);
week.push({
day: this.pad(d),
type: COLORES_PIQUE[ this.fishingMoon(day_) ],
});
// console.log(day_, this.fishingMoon(day_));
}
while (week.length != 7) {
week.push({day:''});
}
weeks.push(week);
let bwe = [
{day: '', type: ''},
{day: '', type: ''},
{day: '', type: ''},
{day: '', type: ''},
{day: '', type: ''},
{day: '', type: ''},
{day: '', type: ''}
];
while (k < 6) {
weeks.push(bwe);
k += 1;
// console.log(k);
}
return weeks;
}
private getDaysOfMonth(month: number, year: number) {
if (month === 1 && year % 4 === 0) {
return 29; // febrero año bisiesto
} else {
return DAYS_OF_MONTH[month];
}
}
private getFirstDay(month: number, year: number) {
let d = new Date(year, month, 1);
if (d.getDay() === 0) {
return 6;
} else {
return d.getDay() - 1;
}
}
private toDays(date) {
return this.toJulian(date) - J2000;
}
private toJulian(date) {
return date.valueOf() / dayMs - 0.5 + J1970;
}
private rightAscension(l: number, b: number) {
let value = (Math.sin(l) * Math.cos(e) - Math.tan(b) * Math.sin(e), Math.cos(l));
return Math.atan(value);
}
private declination(l: number, b: number) {
return Math.asin(Math.sin(b) * Math.cos(e) + Math.cos(b) * Math.sin(e) * Math.sin(l));
}
private solarMeanAnomaly(d: number) {
return rad * (357.5291 + 0.98560028 * d);
}
private eclipticLongitude(M) {
let C = rad * (1.9148 * Math.sin(M) + 0.02 * Math.sin(2 * M) + 0.0003 * Math.sin(3 * M)); // equation of center
let P = rad * 102.9372; // perihelion of the Earth
return M + C + P + Math.PI;
}
private getMoonIllumination(date) {
let d = this.toDays(date);
let s = this.sunCoords(d);
let m = this.moonCoords(d);
console.log(d, s, m);
const sdist = 149598000; // distance from Earth to Sun in km
let phi = Math.acos(Math.sin(s.dec) * Math.sin(m.dec) +
Math.cos(s.dec) * Math.cos(m.dec) * Math.cos(s.ra - m.ra));
let _inc = (sdist * Math.sin(phi), m.dist - sdist * Math.cos(phi));
let inc = Math.atan(_inc);
let _angle = (Math.cos(s.dec) * Math.sin(s.ra - m.ra), Math.sin(s.dec) * Math.cos(m.dec) -
Math.cos(s.dec) * Math.sin(m.dec) * Math.cos(s.ra - m.ra));
console.log(_angle, Math.atan(_angle));
let angle = Math.atan(_angle);
return {
fraction: (1 + Math.cos(inc)) / 2,
phase: 0.5 + 0.5 * inc * (angle < 0 ? -1 : 1) / Math.PI,
angle: angle
};
}
private moonCoords(d) {
let L = rad * (218.316 + 13.176396 * d); // ecliptic longitude
let M = rad * (134.963 + 13.064993 * d); // mean anomaly
let F = rad * (93.272 + 13.229350 * d); // mean distance
let l = L + rad * 6.289 * Math.sin(M); // longitude
let b = rad * 5.128 * Math.sin(F); // latitude
let dt = 385001 - 20905 * Math.cos(M); // distance to the moon in km
return {
ra: this.rightAscension(l, b),
dec: this.declination(l, b),
dist: dt
};
}
private sunCoords(d): any {
let M = this.solarMeanAnomaly(d);
let L = this.eclipticLongitude(M);
return {
dec: this.declination(L, 0),
ra: this.rightAscension(L, 0)
};
}
private fishingMoon(fecha: string): number {
let date = new Date(fecha);
// correcion de un dia para coincidir con el calendario pesca estandar
let _date = date.setDate(date.getDate() + 1);
let p = this.getMoonIllumination(_date).phase;
console.log('phase', p);
if (p <= 0.25) {
return 3;
} else if (p <= 0.5) {
return 1;
} else if (p <= 0.75) {
return 0;
} else {
return 2;
}
}
private fishingMoonStatus(fecha: string): string {
return LUNAS[this.fishingMoon(fecha)];
}
private pad(s): string {
return parseInt(s, null) < 10 ? '0' + s : s;
}
}
Explicitamente solo necesito corregir getMoonIllumination()
. Desde ya muchas gracias. Alguna libreria alternativa seria otra solución.
Gracias