-
Notifications
You must be signed in to change notification settings - Fork 0
/
usgs-details.html
320 lines (282 loc) · 11.4 KB
/
usgs-details.html
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<!DOCTYPE html>
<html lang="en">
<head>
<style type="text/css">
/* League Gothic Condensed */
@font-face {
font-family: 'League Gothic';
src: url('assets/font/leaguegothic-regular-webfont.eot');
src: url('assets/font/leaguegothic-regular-webfont.eot?#iefix') format('embedded-opentype'),
url('assets/font/leaguegothic-regular-webfont.woff') format('woff'),
url('assets/font/leaguegothic-regular-webfont.ttf') format('truetype'),
url('media_al/font/leaguegothic-regular-webfont.svg#league_gothic_condensed-Rg') format('svg');
font-weight: normal;
font-style: normal;
}
/* League Gothic Condensed Italic */
@font-face {
font-family: 'League Gothic';
src: url('assets/font/leaguegothic-italic-webfont.eot');
src: url('assets/font/leaguegothic-italic-webfont.eot?#iefix') format('embedded-opentype'),
url('assets/font/leaguegothic-italic-webfont.woff') format('woff'),
url('assets/font/leaguegothic-italic-webfont.ttf') format('truetype'),
url('assets/font/leaguegothic-italic-webfont.svg#league_gothic_condensed_itaRg') format('svg');
font-weight: normal;
font-style: italic;
}
body {
font-family: 'Gill Sans', 'Gill Sans MT', 'Myriad Pro', 'DejaVu Sans Condensed', Helvetica, Arial, sans-serif;
}
.container {
height: 700px;
background-color: #000000;
margin-top: 240px;
color: #ffffff;
text-align: center;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
/* For Sensor Page Background image*/
.container[name='sensor']{
background-image: url("assets/Sensor_Illustration_opt1.png");
background-repeat: no-repeat;
background-size: contain;
height: 400px;
margin-left: 141px;
margin-top: 150px;
opacity: 0.8;
}
#header,
#subtext,
#sensor-data {
max-width: 300px;
margin: 0 auto;
}
#header:not(:empty):before {
content: '';
display: block;
height: 1px;
background-color: white;
width: 100px;
margin: 9px auto 15px;
}
#value,
#units {
font-family: 'League Gothic', 'Gill Sans', 'Gill Sans MT', 'Myriad Pro', 'DejaVu Sans Condensed', Helvetica, Arial, sans-serif;
}
#value {
font-size: 76px;
}
#units {
font-size: 36px;
}
#header {
font-size: 28px;
margin-bottom: 10px;
}
#subtext {
font-size: 24px;
font-style: italic;
}
</style>
</head>
<body>
<div class="container">
<div id="sensor-data">
<span id="value"></span>
<span id="units"></span>
</div>
<div id="header"></div>
<div id="subtext"></div>
</div>
<script type="text/javascript">
// String constants
var VERY_LOW = 'Very Low',
LOW = 'Low',
MODERATE = 'Moderate',
HIGH = 'High',
VERY_HIGH = 'Very High',
PRIMARY_SENSOR_URL = // Schuylkill River gage usgs.
'https://waterservices.usgs.gov/nwis/iv/?format=json&sites=01474500',
BACKUP_SENSOR_URL = // Delaware River gage
'https://waterservices.usgs.gov/nwis/iv/?format=json&sites=014670261';
// Metrics configuration
var METRICS = {
temperature: {
code: '00010',
units: '°F',
transform: function(c) {
var f = c * 9 / 5 + 32;
return Math.round(f * 10) / 10;
},
fallback: [
42, 32, 42, 51, 61, 71, 80, 90, 80, 71, 61, 51
],
display: 'temperature',
},
ph: {
code: '00400',
units: '',
transform: function(ph) { return ph; }, // Identity
fallback: [
5.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 7.5, 7, 6.5, 6
],
display: 'pH',
},
dissolvedOxygen: {
code: '00300',
units: 'mg/L',
transform: function(dO) { return dO; }, // Identity
fallback: [
10, 12, 10, 8, 6, 4, 2, 1, 2, 4, 6, 8
],
display: 'oxygen',
},
salinity: {
code: '00095',
units: 'ppm',
transform: function(uScm) {
var ppm = uScm / 1.56;
return Math.round(ppm);
},
fallback: [
848, 1000, 848, 697, 545, 393, 242, 90, 242, 393, 545, 697
],
display: 'salinity',
},
turbidity: {
code: '63680',
units: '',
transform: function(t) {
if (t <= 10) { return VERY_LOW; } else
if (t <= 17) { return LOW; } else
if (t <= 40) { return MODERATE; } else
if (t <= 240) { return HIGH; } else
{ return VERY_HIGH; }
},
fallback: [
VERY_LOW, VERY_LOW, LOW, LOW, MODERATE, MODERATE, HIGH, VERY_HIGH, HIGH, HIGH, MODERATE, LOW
],
display: 'turbidity',
},
};
// Hat tip https://stackoverflow.com/a/901144/6995854
// Utility function to get query parameter
function getQueryParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) {
return null;
}
if (!results[2]) {
return '';
}
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
// Utility function to generate USGS request URL for a metric
function urlForMetric(metric, sensorUrl) {
return sensorUrl + '¶meterCd=' + metric.code;
}
// Function to capture results of USGS and populate values
function handleXHRResponse(metric, isBackupSensor) {
return function() {
var value = metric.fallback[new Date().getMonth()],
units = metric.units,
isFallback = true;
if (this.status === 200) {
var json = JSON.parse(this.responseText);
try {
var noDataValue = json.value.timeSeries[0].variable.noDataValue;
var apiValue = json.value.timeSeries[0].values[0].value[0].value;
var apiUnits = json.value.timeSeries[0].variable.unit.unitCode;
console.log('From API: ' + apiValue + ' ' + apiUnits);
console.log('No data value: ' + noDataValue);
if (Number(apiValue) === Number(noDataValue)) {
throw new Error('apiValue ' + apiValue + ' is noDataValue ' + noDataValue);
}
value = metric.transform(apiValue);
isFallback = false;
} catch (ex) {
console.warn('Error while parsing result: ' + ex.message);
}
} else {
console.warn('Error while fetching USGS data: ' + this.status);
console.log('Using fallback values');
}
return renderData(value, units, metric, isFallback, isBackupSensor);
};
}
// Handle XHR errors by using fallback data
function handleXHRError(metric) {
return function() {
var value = metric.fallback[new Date().getMonth()],
units = metric.units,
isFallback = true;
console.warn('Error while fetching USGS data; using fallback values');
return renderData(value, units, metric, isFallback);
}
}
// Create header text for live and fallback values
function renderHeader(metric, isFallback, isBackupSensor) {
if (isFallback) {
return 'Typical ' + metric.display + ' for the Schuylkill';
}
if (isBackupSensor) {
return 'Current ' + metric.display;
}
return 'Current ' + metric.display
}
// Create subtext for live and fallback values
function renderSubtext(isFallback, isBackupSensor) {
if (isFallback) {
return 'A common sensor reading for this month';
}
if (isBackupSensor) {
return 'From a sensor in the Delaware';
}
return 'Reading from a nearby sensor'
}
// Write live or fallback values to the document
function renderData(value, units, metric, isFallback, isBackupSensor) {
document.getElementById('value').innerText = value;
document.getElementById('units').innerText = units;
document.getElementById('header').innerText =
renderHeader(metric, isFallback, isBackupSensor);
document.getElementById('subtext').innerText =
renderSubtext(isFallback, isBackupSensor);
}
// Function to fetch values from USGS sensor given a metric and sensorId
function fetchDataForMetric(metric, sensorId) {
var url = urlForMetric(metric, sensorId);
var xhr = new XMLHttpRequest();
var isBackupSensor = sensorId === BACKUP_SENSOR_URL;
console.log('Fetching data from sensor ' + sensorId + ': ' + url);
xhr.addEventListener('load', handleXHRResponse(metric, isBackupSensor));
if (sensorId === PRIMARY_SENSOR_URL) {
xhr.addEventListener('error', function() {
return fetchDataForMetric(metric, BACKUP_SENSOR_URL);
});
} else {
xhr.addEventListener('error', handleXHRError(metric));
}
xhr.open('GET', url);
xhr.send();
}
// Get metric from Query Parameter and fetch and populate values
window.onload = function() {
var metric = METRICS[getQueryParameterByName('metric')];
if (!metric) {
console.warn('Could not find metric for ' + metric);
// Sensor lacks metrics, so giving its div.container a name of "sensor"
// in order to apply some specific styles.
var container = document.getElementsByClassName('container');
container[0].setAttribute('name', 'sensor');
return;
}
fetchDataForMetric(metric, PRIMARY_SENSOR_URL);
};
</script>
</body>
</html>