Scraping http://en.tutiempo.net/climate and get avg. stats of all elements for every month in every year (starts from 1973 to 2003 in case). Actually generates html table, that then you can select and paste it directly to Libre Office or Microsoft Office packs and voala!
Really, not so big deal, but pretty much faster than making tons of clicks and scrolling for every month in every of these 30 years period.
'use strict'
var mzfs = require('mz/fs')
var cheerio = require('cheerio')
var months = require('months')
var config = require('./fastfingers-config.json')
var stationFilepath = './tmp/' + config.station + '.html'
var stationElements = './tmp/' + config.station + '-elements.html'
var obj = {}
var tmp = []
var res = []
var data = mzfs.readFile(stationFilepath, 'utf8')
var chee = data.then(function (html) {
return cheerio.load(html)
})
function req (year, month) {
return chee.then(function ($) {
var tds = $('table#m' + year + 'm tr:nth-child(' + (month + 1) + ') td')
var monthName = months.abbr[month - 1]
month++
$(tds).each(function (i, item) {
if (i === 0) {
tmp.push(monthName)
} else {
tmp.push($(item).text())
}
})
res.push(tmp)
tmp = []
if (month > 12) {
obj[year] = res
res = []
month = 1
year++
}
if (year === config.lastYear) {
return
}
return req(year, month)
})
}
var output = ''
function element (elnum) {
return req(config.firstYear, 1).then(function () {
var arr = Object.keys(obj)
var html = '<table>'
arr.forEach(function (year) {
var monts = obj[year]
html += '<tr>'
monts.forEach(function (item, i) {
html += '<td>' + item[elnum] + '</td>'
})
html += '</tr>'
})
html += '</table><br>'
return html
})
}
element(2)
.then(function (html) {
console.log('end2')
output += '<h1>TM</h1>'
output += html
return element(3)
})
.then(function (html) {
console.log('end3')
output += '<h1>Tm</h1>'
output += html
return element(4)
})
.then(function (html) {
console.log('end4')
output += '<h1>SLP</h1>'
output += html
return element(5)
})
.then(function (html) {
console.log('end5')
output += '<h1>H</h1>'
output += html
return element(8)
})
.then(function (html) {
console.log('end8')
output += '<h1>V</h1>'
output += html
return element(11)
})
.then(function (html) {
console.log('end11')
output += '<h1>RA</h1>'
output += html
return element(12)
})
.then(function (html) {
console.log('end12')
output += '<h1>SN</h1>'
output += html
return element(13)
})
.then(function (html) {
console.log('end13')
output += '<h1>TS</h1>'
output += html
return element(14)
})
.then(function (html) {
console.log('end14')
output += '<h1>FG</h1>'
output += html
return mzfs.writeFile(stationElements, output)
})
.then(function () {
console.log('Success! Copy-paste from `' + stationElements + '` now!')
})
.catch(console.error)
Then if you have one more task to create "Histogram for temperature of the wind for one station for 30 years period", so.. you'll need one more trick to get firstly every january from that 30 years and calculate it's average temperature, then every february from that 30 years and calculate it's average temperature and so on and so on...
// from previous
.then(function (html) {
var len = 12
var i = 0
var t = 0
var avg = 0
var $ = cheerio.load(html)
while (i < len) {
i++
$('table tr:nth-child(' + (i + 1) + ') td:nth-child(2)').each(function () {
t += Number($(this).text())
})
console.log('month: ' + i + ', avg: ' + (t / 30));
//=>
// "month: 1, avg: -5.9"
// "month: 2, avg: -3.5"
// "month: 3, avg: 1.6"
// "month: 4, avg: 7.4"
// "month: 5, avg: 13.4"
// "month: 6, avg: 19.5"
// "month: 7, avg: 22.9"
// "month: 8, avg: 22.1"
// "month: 9, avg: 17.6"
// "month: 10, avg: 10.9"
// "month: 11, avg: 3.8"
// "month: 12, avg: -2.8"
t = 0
}
})
Scraping http://en.tutiempo.net/climate and get avg. stats of all elements for every month in every year (starts from 1973 to 2003 in case). Actually generates html table, that then you can select and paste it directly to Libre Office or Microsoft Office packs and voala!
Really, not so big deal, but pretty much faster than making tons of clicks and scrolling for every month in every of these 30 years period.
Then if you have one more task to create "Histogram for temperature of the wind for one station for 30 years period", so.. you'll need one more trick to get firstly every january from that 30 years and calculate it's average temperature, then every february from that 30 years and calculate it's average temperature and so on and so on...
then write it to the paper :laughing: