Weather dashboard
https://natronics.org/weather/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
3.4 KiB
124 lines
3.4 KiB
var d3 = require('d3')
|
|
|
|
export function readxml(xmldoc) {
|
|
const forcast = xmldoc.childNodes[0]
|
|
const data = forcast.children[1]
|
|
|
|
// Time
|
|
const times = data.getElementsByTagName('time-layout')[0]
|
|
let startTimes = []
|
|
for (let time of times.getElementsByTagName('start-valid-time')) {
|
|
const date = new Date(time.textContent)
|
|
startTimes.push(date)
|
|
}
|
|
let endTimes = []
|
|
for (let time of times.getElementsByTagName('end-valid-time')) {
|
|
const date = new Date(time.textContent)
|
|
endTimes.push(date)
|
|
}
|
|
|
|
// Temperature
|
|
let temperatures = []
|
|
for (let temp_type of data.getElementsByTagName('temperature')) {
|
|
if (temp_type.attributes.type.nodeValue === 'hourly') {
|
|
for (let temp of temp_type.getElementsByTagName('value')) {
|
|
const degC = (parseFloat(temp.textContent) - 32) * (5.0/9.0)
|
|
temperatures.push(degC)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Cloud Cover
|
|
let cloudCover = []
|
|
const clouds = data.getElementsByTagName('cloud-amount')[0]
|
|
for (let cloud of clouds.getElementsByTagName('value')) {
|
|
cloudCover.push(parseFloat(cloud.textContent))
|
|
}
|
|
|
|
// Precipitation
|
|
let precip = []
|
|
const probabilityOfPrecipitation = data.getElementsByTagName('probability-of-precipitation')[0]
|
|
for (let prop of probabilityOfPrecipitation.getElementsByTagName('value')) {
|
|
precip.push(parseFloat(prop.textContent))
|
|
}
|
|
let precipQPF = []
|
|
const hourlyQPF = data.getElementsByTagName('hourly-qpf')[0]
|
|
for (let qpf of hourlyQPF.getElementsByTagName('value')) {
|
|
if (qpf.getAttribute('xsi:nil') === "true") {
|
|
precipQPF.push(0)
|
|
} else {
|
|
precipQPF.push(parseFloat(qpf.textContent))
|
|
}
|
|
}
|
|
|
|
// Map
|
|
const nElements = startTimes.length
|
|
|
|
let forcastData = []
|
|
let cloudData = []
|
|
let precipData = []
|
|
let precipQPFData = []
|
|
cloudData.push({
|
|
date: startTimes[0],
|
|
coverage: 0
|
|
})
|
|
precipData.push({
|
|
date: startTimes[0],
|
|
probability: 0
|
|
})
|
|
precipQPFData.push({
|
|
date: startTimes[0],
|
|
qpf: 0
|
|
})
|
|
for (var i = 0; i < nElements; i++) {
|
|
const midtimeScale = d3.scaleTime()
|
|
.domain([0, 1])
|
|
.range([startTimes[i], endTimes[i]])
|
|
const midpointTime = midtimeScale(0.5)
|
|
forcastData.push({
|
|
date: midpointTime,
|
|
temp: temperatures[i]
|
|
})
|
|
cloudData.push({
|
|
date: midpointTime,
|
|
coverage: cloudCover[i]
|
|
})
|
|
precipData.push({
|
|
date: startTimes[i],
|
|
probability: precip[i]
|
|
})
|
|
precipData.push({
|
|
date: endTimes[i],
|
|
probability: precip[i]
|
|
})
|
|
precipQPFData.push({
|
|
date: startTimes[i],
|
|
qpf: precipQPF[i]
|
|
})
|
|
precipQPFData.push({
|
|
date: endTimes[i],
|
|
qpf: precipQPF[i]
|
|
})
|
|
}
|
|
cloudData.push({
|
|
date: endTimes[endTimes.length - 1],
|
|
coverage: 0
|
|
})
|
|
precipData.push({
|
|
date: endTimes[endTimes.length - 1],
|
|
probability: 0
|
|
})
|
|
precipQPFData.push({
|
|
date: endTimes[endTimes.length - 1],
|
|
qpf: 0
|
|
})
|
|
|
|
return {
|
|
startTimes: startTimes,
|
|
endTimes: endTimes,
|
|
temperature: forcastData,
|
|
clouds: cloudData,
|
|
precip: precipData,
|
|
precipQPF: precipQPFData,
|
|
}
|
|
}
|