-
Notifications
You must be signed in to change notification settings - Fork 0
/
histogram.php
225 lines (181 loc) · 7.88 KB
/
histogram.php
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
<?php
class Histogram {
private $feed;
public function __construct($feed) {
$this->feed = $feed;
}
public function kwh_at_cop($id_elec,$id_heat,$start,$end,$histogram_div,$interval,$cop_min,$cop_max) {
$histogram_div = (float) $histogram_div;
$interval = (int) $interval;
$cop_min = (float) $cop_min;
$cop_max = (float) $cop_max;
$start = floor($start / $interval) * $interval;
$end = floor($end / $interval) * $interval;
$power_to_kwh = $interval / 3600000;
$elec_data = $this->feed->get_data($id_elec,$start,$end,$interval,1,"UTC","notime");
$heat_data = $this->feed->get_data($id_heat,$start,$end,$interval,1,"UTC","notime");
$npoints = count($elec_data);
$histogram = array();
$timer = microtime(true);
for ($i=0; $i<$npoints; $i++) {
$elec = $elec_data[$i];
$heat = $heat_data[$i];
if ($elec>0) {
$cop = $heat / $elec;
if ($cop>=$cop_min && $cop<=$cop_max) {
// calculate histogram allocation
$div = "".(floor($cop / $histogram_div) * $histogram_div);
// add to histogram
if (!isset($histogram[$div])) {
$histogram[$div] = 0;
}
$kwh_inc = $heat * $power_to_kwh;
$histogram[$div] += $kwh_inc;
}
}
}
return $this->format($histogram,$histogram_div,$timer);
}
function hwh_at_temperature($id_power,$id_temperature,$start,$end,$histogram_div,$interval,$temperature_min,$temperature_max) {
$histogram_div = (float) $histogram_div;
$interval = (int) $interval;
$temperature_min = (float) $temperature_min;
$temperature_max = (float) $temperature_max;
$start = floor($start / $interval) * $interval;
$end = floor($end / $interval) * $interval;
$power_to_kwh = $interval / 3600000;
$power_data = $this->feed->get_data($id_power,$start,$end,$interval,1,"UTC","notime");
$temperature_data = $this->feed->get_data($id_temperature,$start,$end,$interval,1,"UTC","notime");
$npoints = count($power_data);
$histogram = array();
$timer = microtime(true);
for ($i=0; $i<$npoints; $i++) {
$power = $power_data[$i];
$temperature = $temperature_data[$i];
// calculate histogram allocation
if ($temperature>=$temperature_min && $temperature<=$temperature_max) {
$div = "".(floor($temperature / $histogram_div) * $histogram_div);
// add to histogram
if (!isset($histogram[$div])) {
$histogram[$div] = 0;
}
$kwh_inc = $power * $power_to_kwh;
$histogram[$div] += $kwh_inc;
}
}
return $this->format($histogram,$histogram_div,$timer);
}
function hwh_at_flow_minus_outside($id_power,$id_flow,$id_outside,$start,$end,$histogram_div,$interval,$temperature_min,$temperature_max) {
$histogram_div = (float) $histogram_div;
$interval = (int) $interval;
$temperature_min = (float) $temperature_min;
$temperature_max = (float) $temperature_max;
$start = floor($start / $interval) * $interval;
$end = floor($end / $interval) * $interval;
$power_to_kwh = $interval / 3600000;
$power_data = $this->feed->get_data($id_power,$start,$end,$interval,1,"UTC","notime");
$flow_data = $this->feed->get_data($id_flow,$start,$end,$interval,1,"UTC","notime");
$outside_data = $this->feed->get_data($id_outside,$start,$end,$interval,1,"UTC","notime");
$npoints = count($power_data);
$histogram = array();
$timer = microtime(true);
for ($i=0; $i<$npoints; $i++) {
$power = $power_data[$i];
$flow = $flow_data[$i];
$outside = $outside_data[$i];
$temperature = $flow - $outside;
// calculate histogram allocation
if ($temperature>=$temperature_min && $temperature<=$temperature_max) {
$div = "".(floor($temperature / $histogram_div) * $histogram_div);
// add to histogram
if (!isset($histogram[$div])) {
$histogram[$div] = 0;
}
$kwh_inc = $power * $power_to_kwh;
$histogram[$div] += $kwh_inc;
}
}
return $this->format($histogram,$histogram_div,$timer);
}
public function flow_temp_curve($outsideT,$flowT,$heat,$start,$end,$div,$interval,$x_min,$x_max) {
$div = (float) $div;
$interval = (int) $interval;
$x_min = (float) $x_min;
$x_max = (float) $x_max;
$start = floor($start / $interval) * $interval;
$end = floor($end / $interval) * $interval;
$power_to_kwh = $interval / 3600000;
$outsideT_data = $this->feed->get_data($outsideT,$start,$end,$interval,1,"UTC","notime");
$flowT_data = $this->feed->get_data($flowT,$start,$end,$interval,1,"UTC","notime");
$heat_data = $this->feed->get_data($heat,$start,$end,$interval,1,"UTC","notime");
$npoints = count($outsideT_data);
$histogram = array();
$timer = microtime(true);
for ($i=0; $i<$npoints; $i++) {
$outsideT = $outsideT_data[$i];
$flowT = $flowT_data[$i];
$heat = $heat_data[$i];
// calculate histogram allocation
// if ($outsideT>=$x_min && $outsideT<=$x_max) {
$outside_div = "".(floor($outsideT / $div) * $div);
$flow_div = "".(floor($flowT / $div) * $div);
// add to histogram
if (!isset($histogram[$outside_div])) {
$histogram[$outside_div] = array();
}
if (!isset($histogram[$outside_div][$flow_div])) {
$histogram[$outside_div][$flow_div] = 0;
}
$kwh_inc = $heat * $power_to_kwh;
$histogram[$outside_div][$flow_div] += $kwh_inc;
// }
}
header('Content-Type: text/plain');
$out = "";
$data = array();
// convert to [[x,y,z]] format
foreach ($histogram as $outside_div => $row) {
foreach ($row as $flow_div => $kwh) {
$kwh = number_format($kwh,3,".","")*1;
$data[] = array($outside_div*1,$flow_div*1,$kwh);
$out .= "$outside_div\t$flow_div\t$kwh\n";
}
}
print $out;
die;
/*
// sort by outside temperature
ksort($histogram);
// sort by flow temperature
foreach ($histogram as $outside_div => $data) {
ksort($histogram[$outside_div]);
}*/
return $out;
}
public function format($histogram,$histogram_div,$timer) {
// find largest key
$keys = array_keys($histogram);
if (count($keys)) {
$min = min($keys);
$max = max($keys);
} else {
$min = 0;
$max = 0;
}
$output = array(
"min" => $min*1,
"max" => $max*1,
"div" => $histogram_div*1,
"data" => [],
"time" => number_format(microtime(true)-$timer,3,".","")
);
for ($i=$min; $i<=$max; $i+=$histogram_div) {
if (isset($histogram["".$i])) {
$output["data"][] = number_format($histogram["".$i],3,".","")*1;
} else {
$output["data"][] = 0;
}
}
return $output;
}
}