-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponents.js
222 lines (202 loc) · 7.82 KB
/
components.js
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Function to create a color scale for mapping data values to colors
function setColorScale()
{
return d3.scaleLinear()
.domain([0, (1/6), ((1/6)*2), ((1/6)*3), ((1/6)*4), ((1/6)*5), 1])
.range(['blue', 'teal', 'green', 'chartreuse', 'yellow', 'orange','red']);
}
// This function takes a data value as input and returns a color based on the value
export function setColor(value)
{
let color = ''
if (value <= 1)
{
const colorScale = setColorScale();
return colorScale(value); // sets weight of color based on scale
}
else if (value > 1)
{
return d3.color('darkred');
}
else if (value === 'inactive')
{
return d3.color('grey');
}
return color;
}
export function createColorLegend() {
const colorScale = setColorScale();
const colorLegend = L.control({ position: 'bottomleft' });
colorLegend.onAdd = function (map) {
const div = L.DomUtil.create('div', 'legend');
// create and append colorBar div
const colorBar = L.DomUtil.create('div', '', div);
colorBar.id = 'colorBar';
colorBar.style.backgroundImage = `linear-gradient(to right, ${colorScale(0)}, ${colorScale(1 / 6)}, ${colorScale((1 / 6) * 2)}, ${colorScale((1 / 6) * 3)}, ${colorScale((1 / 6) * 4)}, ${colorScale((1 / 6) * 5)}, ${colorScale(1)})`;
colorBar.style.width = '300px'
// colorBar.style.border = '1px dashed black';
// // add vertical dashed lines to colorBar
// const numLines = 20;
// const lineColor = 'black';
// const lineWidth = 1;
// const lineStyle = 'dashed';
// const lineSpacing = 3 / numLines;
// colorBar.style.backgroundImage = `repeating-linear-gradient(to right, transparent, transparent ${lineWidth}px, ${lineColor} ${lineWidth}px, ${lineColor} ${lineWidth + lineSpacing}px)`;
// create and append legendMarker div
const legendMarker = L.DomUtil.create('div', '', div);
legendMarker.id = 'legendMarker';
legendMarker.style.width = '300px'
for (let i = 0; i <= 5; i++) {
const value = (i / 5).toFixed(1);
const p = L.DomUtil.create('p', '', legendMarker);
p.innerText = value;
}
const AOD = L.DomUtil.create('div', '', div);
AOD.id = 'AOD';
AOD.style.display = 'block';
const currentTime = L.DomUtil.create('div', '', div);
currentTime.id = 'currentTime';
currentTime.style.display = 'block';
// div.style.margin = '30px';
div.style.padding = '10px';
return div;
};
return colorLegend;
}
// Function to update the current time label
// export function updateTime(date = null, time = null, previouslySet = false, daily = false, hourTolerance=1) {
export function updateTime(dateTime, daily = false) {
let year, month, day, previousHr, hour, bufferHr, minute;
let dayValidation,xz,xy;
if (dateTime.length === 2)
{
[year, month, day] = dateTime[0].map(Number);
[previousHr, hour, bufferHr, minute] = dateTime[1];
}
else if (dateTime.length === 3)
{
[year, month, day] = dateTime[1].map(Number);
[previousHr, hour, bufferHr, minute] = dateTime[2]
}
const currentTimeDiv = document.getElementById('currentTime');
if (!daily) {
const dateString = new Date(Date.UTC(year, month-1, parseInt(day)+1)).toLocaleString('en-US', {
month: 'long',
day: '2-digit',
year: 'numeric'
});
const timeString = `${previousHr}:${minute} — ${hour}:${minute}`;
currentTimeDiv.innerHTML = `<strong>${dateString}</strong> (${timeString}) <strong>UTC</strong>`;
} else if (daily) {
const dateString = new Date(Date.UTC(year, month-1, day+1)).toLocaleString('en-US', {
month: 'long',
day: '2-digit',
year: 'numeric'
});
currentTimeDiv.innerHTML = `${dateString} Daily Average`;
} else {
}
}
export function updateAOD(optical_dep)
{
optical_dep = optical_dep.split('_')[1]
const currentTimeDiv = document.getElementById('AOD');
currentTimeDiv.innerHTML = `Aerosol Optical Depth (${optical_dep})`; // aod string
currentTimeDiv.style.textAlign = 'center';
currentTimeDiv.style.fontSize = '14px';
}
export function getStartEndDateTime(dateTime = null, hourTolerance = 1, daily = null, previousDateTime=null)
{
let yesterday;
let year, month, day, previousYear, previousMonth, previousDay, previousHr, hour, bufferHr, minute;
if(!dateTime)
{
const now = new Date();
yesterday = new Date(Date.parse(now.toISOString()))
yesterday.setDate(now.getDate() - 1)
year = now.getUTCFullYear().toString().padStart(4, '0');
month = (now.getUTCMonth() + 1).toString().padStart(2, '0');
day = (now.getUTCDate()).toString().padStart(2, '0');
hour = now.getUTCHours().toString().padStart(2, '0');
minute = now.getUTCMinutes().toString().padStart(2, '0');
previousYear = yesterday.getUTCFullYear().toString().padStart(4, '0');
previousMonth = (yesterday.getUTCMonth() + 1).toString().padStart(2, '0');
previousDay = (yesterday.getUTCDate()).toString().padStart(2, '0');
}
else if (dateTime && daily)
{
const setTime = new Date(Date.parse(dateTime))
yesterday = new Date(Date.parse(dateTime))
yesterday.setDate(setTime.getDate() - 1)
year = setTime.getUTCFullYear().toString().padStart(4, '0');
month = (setTime.getUTCMonth() + 1).toString().padStart(2, '0');
day = (setTime.getUTCDate()).toString().padStart(2, '0');
previousYear = yesterday.getUTCFullYear().toString().padStart(4, '0');
previousMonth = (yesterday.getUTCMonth() + 1).toString().padStart(2, '0');
previousDay = (yesterday.getUTCDate()).toString().padStart(2, '0');
if (previousDateTime.length === 3)
{
hour = previousDateTime[2][1];
minute = previousDateTime[2][3];
}
else if (previousDateTime.length === 2)
{
hour = previousDateTime[1][1];
minute = previousDateTime[1][3];
}
}
else if (dateTime)
{
const setTime = new Date(Date.parse(dateTime))
yesterday = new Date(Date.parse(dateTime))
yesterday.setDate(setTime.getDate() - 1)
year = setTime.getUTCFullYear().toString().padStart(4, '0');
month = (setTime.getUTCMonth() + 1).toString().padStart(2, '0');
day = (setTime.getUTCDate()).toString().padStart(2, '0');
hour = setTime.getUTCHours().toString().padStart(2, '0');
minute = setTime.getUTCMinutes().toString().padStart(2, '0');
previousYear = yesterday.getUTCFullYear().toString().padStart(4, '0');
previousMonth = (yesterday.getUTCMonth() + 1).toString().padStart(2, '0');
previousDay = (yesterday.getUTCDate()).toString().padStart(2, '0');
}
// Normalize Hours back to 24-hour format
previousHr = (((parseInt(hour) - hourTolerance) % 24 + 24) % 24).toString().padStart(2, '0');
bufferHr = (((parseInt(hour) + 1) % 24 + 24) % 24).toString().padStart(2, '0');
if (hour==='00' || bufferHr < hour)
{
return [
[
previousYear,
previousMonth,
previousDay,
],
[
year,
month,
day,
],
[
previousHr,
hour,
bufferHr,
minute,
],
];
}
else
{
return [
[
year,
month,
day,
],
[
previousHr,
hour,
bufferHr,
minute,
],
];
}
}