-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherProvider.js
More file actions
80 lines (67 loc) · 2.18 KB
/
WeatherProvider.js
File metadata and controls
80 lines (67 loc) · 2.18 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
var Self = function ($rootScope, $q, $timeout) {
var self = this
self.name = 'Weather'
self.$q = $q
self.$rootScope = $rootScope
self.$timeout = $timeout
//TODO Convention variable - find better way for providing consumers with choosen station data
self.loaded = {}
d3.csv('data/ghcn_units.csv', function (data) {
self.items = data
})
}
Self.prototype.stations = [{
id: 'USW00094728'
, name: 'New York'
}, {
id: 'USW00023234'
, name: 'San Francisco'
}, {
id: 'USW00014922'
, name: 'Minneapolis'
}]
Self.prototype.dateFormatter = d3.time.format('%Y%m%d')
Self.prototype.load = function (station) {
var self = this
if (!self.items) return setTimeout(function () { self.load(station) }, 100)
var deferred = self.$q.defer()
if (!station.data) {
d3.csv('data/' + station.id + '.csv', function (data) {
//Parse csv
data = d3.nest()
.key(function (d) { return d.date })
.rollup(function (leaves) {
return leaves.reduce(function (p, d, i, arr) { p[d.attr] = +d.value; return p}, {})
})
.entries(data)
//format data
for (var i = 0; i < data.length; i++) {
var result = data[i].values
result.date = self.dateFormatter.parse(data[i].key)
var sunr = i < data.length -1 ? result.TMAX - data[i+1].values.TMAX : 0
result.SUNR = Math.round((sunr + 200)/0.035)
self.items.forEach(function (item) {
//TODO not all missing values defaults to zero
if (result[item.name] === undefined) {
result[item.name] = 0
}
if (item.scale) result[item.name] = result[item.name] * +item.scale
})
data[i] = result
}
data = data.reverse()
station.data = data
self.loaded = data
self.$timeout(function () {
self.$rootScope.$broadcast('data-loaded', data, self.items)
}, 500)
deferred.resolve(data)
})
} else {
self.$rootScope.$broadcast('data-loaded', station.data, self.items)
deferred.resolve(station.data)
}
return deferred.promise
}
module.exports = angular.module('weather', [])
.service('WeatherProvider', ['$rootScope', '$q', '$timeout', Self])