Skip to content

Instantly share code, notes, and snippets.

@Ruffo324
Created February 1, 2021 20:05
Show Gist options
  • Save Ruffo324/4e73d75dce399a16923ebcf1c598ba8d to your computer and use it in GitHub Desktop.
Save Ruffo324/4e73d75dce399a16923ebcf1c598ba8d to your computer and use it in GitHub Desktop.
IFFT Filter - Only run applet if its dark outside
/**
* IFFT Filter script, to execute an applet only if it's dark outside. Works position based.
* Just change the lat long below to your wanted location.
*/
const pos = {lat: 52.XYZ, long: 7.XYZ};
// Just placeholder for codecompletion!
// let Meta:any;
// let MakerWebhooks:any;
let d = Meta.currentUserTime.toDate();
let gettingBrigthOutside = sunrise(d, pos.lat, pos.long, 'official');
let gettingDarkOutside = sunset(d, pos.lat, pos.long, 'civil');
if(d >= gettingBrigthOutside && d <= gettingDarkOutside)
MakerWebhooks.makeWebRequest.skip();
// ==========================================================================================
// AWESOME LOGIC BELOW, BIG THANKS TO https://github.com/medmunds.
// Original source: http://jsfiddle.net/xzmut2es/ i just modified some parts,
// and made it typescript compatible!
// ==========================================================================================
function solar_event(date: Date, latitude: number, longitude: number, rising: boolean, zenith: any) {
var year = date.getUTCFullYear(),
month = date.getUTCMonth() + 1,
day = date.getUTCDate();
var floor = Math.floor,
degtorad = (deg: number) => Math.PI * deg / 180,
radtodeg = (rad: number) => 180 * rad / Math.PI,
sin = (deg: number) => Math.sin(degtorad(deg)),
cos = (deg: number) => Math.cos(degtorad(deg)),
tan = (deg: number) => Math.tan(degtorad(deg)),
asin = (x: number) => radtodeg(Math.asin(x)),
acos = (x: number) => radtodeg(Math.acos(x)),
atan = (x: number) => radtodeg(Math.atan(x)),
modpos = (x: number, m: number) => ((x % m) + m) % m;
// 1. first calculate the day of the year
var N1 = floor(275 * month / 9),
N2 = floor((month + 9) / 12),
N3 = (1 + floor((year - 4 * floor(year / 4) + 2) / 3)),
N = N1 - (N2 * N3) + day - 30;
// 2. convert the longitude to hour value and calculate an approximate time
var lngHour = longitude / 15,
t = N + (((rising ? 6 : 18) - lngHour) / 24);
// 3. calculate the Sun's mean anomaly
var M = (0.9856 * t) - 3.289;
// 4. calculate the Sun's true longitude
var L = M + (1.916 * sin(M)) + (0.020 * sin(2 * M)) + 282.634;
L = modpos(L, 360); // NOTE: L potentially needs to be adjusted into the range [0,360) by adding/subtracting 360
// 5a. calculate the Sun's right ascension
var RA = atan(0.91764 * tan(L));
RA = modpos(RA, 360); // NOTE: RA potentially needs to be adjusted into the range [0,360) by adding/subtracting 360
// 5b. right ascension value needs to be in the same quadrant as L
var Lquadrant = (floor(L / 90)) * 90,
RAquadrant = (floor(RA / 90)) * 90;
RA = RA + (Lquadrant - RAquadrant);
// 5c. right ascension value needs to be converted into hours
RA = RA / 15;
// 6. calculate the Sun's declination
var sinDec = 0.39782 * sin(L),
cosDec = cos(asin(sinDec));
// 7a. calculate the Sun's local hour angle
var cosH = (cos(zenith) - (sinDec * sin(latitude))) / (cosDec * cos(latitude));
var H: number;
if (cosH > 1) {
return undefined; // the sun never rises on this location (on the specified date)
} else if (cosH < -1) {
return undefined; // the sun never sets on this location (on the specified date)
}
// 7b. finish calculating H and convert into hours
if (rising) {
H = 360 - acos(cosH);
} else {
H = acos(cosH);
}
H = H / 15;
// 8. calculate local mean time of rising/setting
var T = H + RA - (0.06571 * t) - 6.622;
// 9. adjust back to UTC
var UT = T - lngHour;
UT = modpos(UT, 24); // NOTE: UT potentially needs to be adjusted into the range [0,24) by adding/subtracting 24
console.log(UT);
var hours = floor(UT),
minutes = Math.round(60 * (UT - hours));
var result = new Date(Date.UTC(year, month - 1, day, hours, minutes))
return result;
}
type types = 'official'|'civil'|'nautical'|'astronomical';
var zeniths: {[t in types] : number} = {
'official': 90.833333,
'civil': 96,
'nautical': 102,
'astronomical': 108
};
function sunrise(date: Date, latitude: number, longitude: number, type: types): Date {
var zenith = zeniths[type] || zeniths['official'];
return solar_event(date, latitude, longitude, true, zenith);
}
function sunset(date: Date, latitude: number, longitude: number, type: types): Date {
var zenith = zeniths[type] || zeniths['official'];
return solar_event(date, latitude, longitude, false, zenith);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment