Repository navigation
Expand file tree
/
Copy pathmoon.js
More file actions
111 lines (101 loc) · 3.57 KB
/
Copy pathmoon.js
File metadata and controls
111 lines (101 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* Source: https://observablehq.com/@forresto/the-moon-now
*
* Alternate API worth looking at, returns JSON with image paths and more moon info...
* https://svs.gsfc.nasa.gov/api/dialamoon/2025-10-10T23:00
*/
export function getMoonImageURLs(date = new Date(), southern = false) {
const moon_domain = "https://svs.gsfc.nasa.gov";
const moon_year = date.getFullYear();
const moon_info = {
"2022": {
northern: {
jpg: "/vis/a000000/a004900/a004955/frames/730x730_1x1_30p/",
tif: "/vis/a000000/a004900/a004955/frames/5760x3240_16x9_30p/plain/",
},
southern: {
jpg: "/vis/a000000/a004900/a004956/frames/730x730_1x1_30p/",
tif: "/vis/a000000/a004900/a004956/frames/5760x3240_16x9_30p/plain/",
},
febdays: 28,
nimages: 8760,
},
"2023": {
northern: {
jpg: "/vis/a000000/a005000/a005048/frames/730x730_1x1_30p/",
tif: "/vis/a000000/a005000/a005048/frames/5760x3240_16x9_30p/plain/",
},
southern: {
jpg: "/vis/a000000/a005000/a005049/frames/730x730_1x1_30p/",
tif: "/vis/a000000/a005000/a005049/frames/5760x3240_16x9_30p/plain/",
},
febdays: 28,
nimages: 8760,
},
"2024": {
northern: {
jpg: "/vis/a000000/a005100/a005187/frames/730x730_1x1_30p/",
tif: "/vis/a000000/a005100/a005187/frames/5760x3240_16x9_30p/plain/",
},
southern: {
jpg: "/vis/a000000/a005100/a005188/frames/730x730_1x1_30p/",
tif: "/vis/a000000/a005100/a005188/frames/5760x3240_16x9_30p/plain/",
},
febdays: 29,
nimages: 8760,
},
"2025": {
northern: {
jpg: "/vis/a000000/a005400/a005415/frames/730x730_1x1_30p/",
tif: "/vis/a000000/a005400/a005415/frames/5760x3240_16x9_30p/plain/",
},
southern: {
jpg: "/vis/a000000/a005400/a005416/frames/730x730_1x1_30p/",
tif: "/vis/a000000/a005400/a005416/frames/5760x3240_16x9_30p/plain/",
},
febdays: 28,
nimages: 8760,
},
"2026": {
northern: {
jpg: "/vis/a000000/a005500/a005587/frames/730x730_1x1_30p/",
tif: "/vis/a000000/a005500/a005587/frames/5760x3240_16x9_30p/plain/",
},
southern: {
jpg: "/vis/a000000/a005500/a005588/frames/730x730_1x1_30p/",
tif: "/vis/a000000/a005500/a005588/frames/5760x3240_16x9_30p/plain/",
},
febdays: 28,
nimages: 8760,
},
};
if (!moon_info[moon_year]) {
throw new Error(
"We only have image paths for 2022-2026 for now. See NASA's site for images back to 2011: https://svs.gsfc.nasa.gov/gallery/moonphase/",
);
}
/*
======================================================================
get_moon_imagenum()
Initialize the frame number. If the current date is within the year
moon_year, the frame number is the (rounded) number of hours since the
start of the year. Otherwise it's 1.
====================================================================== */
const year = date.getUTCFullYear();
const { moon_nimages } = moon_info[year];
const janone = Date.UTC(year, 0, 1, 0, 0, 0);
let moon_imagenum = 1 + Math.round((date.getTime() - janone) / 3600000.0);
if (moon_imagenum > moon_nimages) {
moon_imagenum = moon_nimages;
}
const filename = "moon." + String(moon_imagenum).padStart(4, "0");
function moon_image_url(filetype) {
const moon_path =
moon_info[moon_year][southern ? "southern" : "northern"][filetype];
return moon_domain + moon_path + filename + "." + filetype;
}
return {
jpg: moon_image_url("jpg"),
tif: moon_image_url("tif"),
};
}