-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWind.cpp
180 lines (158 loc) · 4.01 KB
/
Wind.cpp
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
#include "Wind.h"
#include "units.h"
#include "math.h"
using namespace std;
Wind::Wind( Now *_now, Database &_db ):
now(_now),
dbg("Wind"),
db( _db )
{
restore();
}
Wind::~Wind()
{
dump();
}
void Wind::restore()
{
dbResult result = db.query("select * from windData;");
for ( dbResult::iterator iter = result.begin(); iter!=result.end(); ++iter )
{
time_t t = iter->second.asInteger("time");
float speed = iter->second.asDouble("speed");
pair< double, double > pr( iter->second.asDouble("dirSin"),
iter->second.asDouble("dirCos"));
windSpeed[t] = speed;
windDirection[t] = pr;
}
dbg.printf(NOTICE, "Restored %d wind values\n", result.size());
}
void Wind::dump()
{
time_t t;
float speed;
float dirSin;
float dirCos;
dbg.printf(NOTICE, "Dumping %d wind values to database\n", windSpeed.size());
db.query("BEGIN TRANSACTION;");
db.query("DELETE FROM windData;");
for ( windSpeed_t::iterator iter = windSpeed.begin();
iter != windSpeed.end(); ++iter )
{
t = iter->first;
speed = iter->second;
dirSin = windDirection[t].first;
dirCos = windDirection[t].second;
db.query("insert into windData ( time, speed, dirSin, dirCos ) VALUES ( %d, %f, %f, %f );", (int)t, speed, dirSin, dirCos );
}
db.query("COMMIT;");
}
void Wind::update( double speed, double direction )
{
pair<double, double> pr( sin( deg2rad( direction )),
cos( deg2rad( direction )));
windDirection[now->unixtime()] = pr ;
windSpeed[now->unixtime()] = speed;
for ( windSpeed_t::iterator iter = windSpeed.begin();
iter != windSpeed.end(); )
{
if ( iter->first <= now->unixtime() - MAX_WIND_DURATION )
{
windSpeed.erase(iter++);
}
else
{
++iter;
}
}
for ( windDirection_t::iterator iter = windDirection.begin();
iter != windDirection.end(); )
{
if ( iter->first <= now->unixtime() - MAX_WIND_DURATION )
{
windDirection.erase(iter++);
}
else
{
++iter;
}
}
}
int Wind::getAverageDirection( time_t duration )
{
int rval = 0;
double sinSum = 0;
double cosSum = 0;
for ( windDirection_t::iterator iter = windDirection.begin();
iter != windDirection.end();
++iter )
{
if ( iter->first > now->unixtime() - duration )
{
sinSum += iter->second.first;
cosSum += iter->second.second;
}
}
rval = (int)rad2deg( atan2( sinSum, cosSum ));
if ( rval < 0 )
rval += 360;
return rval;
}
double Wind::getAverageSpeed( time_t duration )
{
double rval = 0;
int count = 0;
for ( windSpeed_t::iterator iter = windSpeed.begin();
iter != windSpeed.end();
++iter )
{
if ( iter->first > now->unixtime() - duration )
{
rval += iter->second;
count ++;
}
}
return rval / count;
}
int Wind::getDirectionAtMaxSpeed( time_t duration )
{
double maxSpeed = -1;
time_t timeAtMax = 0;
int rval;
for ( windSpeed_t::iterator iter = windSpeed.begin();
iter != windSpeed.end();
++iter )
{
if ( iter->first > now->unixtime() - duration )
{
if ( iter->second > maxSpeed )
{
maxSpeed = iter->second;
timeAtMax = iter->first;
}
}
}
dbg.printf(DEBUG, "timeAtMax: %d\n", timeAtMax );
dbg.printf(DEBUG, "maxSpeed: %f\n", maxSpeed );
rval = (int)rad2deg( atan2( windDirection[ timeAtMax ].first, windDirection[ timeAtMax ].second ));
if ( rval < 0 )
rval += 360;
return rval;
}
double Wind::getMaxSpeed( time_t duration )
{
double rval = -1;
for ( windSpeed_t::iterator iter = windSpeed.begin();
iter != windSpeed.end();
++iter )
{
if ( iter->first > now->unixtime() - duration )
{
if ( iter->second > rval )
{
rval = iter->second;
}
}
}
return rval;
}