This repository has been archived by the owner on Apr 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
number_source.C
276 lines (258 loc) · 7.5 KB
/
number_source.C
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// number_source.C -- Extract a single number from a time series.
//
// Copyright 2005 Per Abrahamsen and KVL.
//
// This file is part of Daisy.
//
// Daisy is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// Daisy is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser Public License for more details.
//
// You should have received a copy of the GNU Lesser Public License
// along with Daisy; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#define BUILD_DLL
#include "number.h"
#include "block_model.h"
#include "source.h"
#include "assertion.h"
#include "librarian.h"
#include "treelog.h"
#include "frame.h"
#include <sstream>
#include <memory>
struct NumberSource : public Number
{
const std::unique_ptr<Source> source;
const std::unique_ptr<const Time> begin;
const std::unique_ptr<const Time> end;
enum { uninitialized, error, is_missing, has_value } state;
double val;
symbol title () const
{
daisy_assert (state != uninitialized);
return source->title ();
}
void tick (const Units&, const Scope&, Treelog&)
{ }
bool missing (const Scope&) const
{
daisy_assert (state != uninitialized);
return state != has_value;
}
double value (const Scope&) const
{
daisy_assert (state == has_value);
return val;
}
symbol dimension (const Scope&) const
{
daisy_assert (state != uninitialized);
return symbol (source->dimension ());
}
// Create.
virtual void initialize_derived (Treelog& msg) = 0;
bool initialize (const Units& units, const Scope& scope, Treelog& msg)
{
TREELOG_MODEL (msg);
msg.touch ();
daisy_assert (state == uninitialized);
daisy_assert (source.get ());
if (!source->load (msg))
state = error;
else
initialize_derived (msg);
return state != error;
}
bool check (const Units&, const Scope&, Treelog&) const
{
daisy_assert (state != uninitialized);
return state != error;
}
NumberSource (const BlockModel& al)
: Number (al),
source (Librarian::build_item<Source> (al, "source")),
begin (al.check ("begin") ? new Time (al.submodel ("begin")) : NULL),
end (al.check ("end") ? new Time (al.submodel ("end")) : NULL),
state (uninitialized),
val (-42.42e42)
{ }
};
static struct NumberSourceSyntax : public DeclareBase
{
NumberSourceSyntax ()
: DeclareBase (Number::component, "source",
"Extract information from a time series.")
{ }
void load_frame (Frame& frame) const
{
frame.declare_object ("source", Source::component, "\
The time series we want to extract a number from.");
frame.declare_submodule ("begin", Attribute::OptionalConst,
"Ignore values before or at this date.",
Time::load_syntax);
frame.declare_submodule ("end", Attribute::OptionalConst,
"Ignore values after this date.", Time::load_syntax);
}
} NumberSource_syntax;
struct NumberSourceUnique : public NumberSource
{
void initialize_derived (Treelog& msg)
{
const std::vector<Time>& time = source->time ();
const size_t size = time.size ();
int count = 0;
for (size_t i = 0; i < size; i++)
if ((!begin.get () || time[i] > *begin)
&& (!end.get () || time[i] <= *end))
{
val = source->value ()[i];
count++;
}
if (count == 1U)
state = has_value;
else if (count == 0U)
{
msg.warning ("Got zero elements, expected one");
state = is_missing;
}
else
{
std::ostringstream tmp;
tmp << "Got " << count << " elements, expected 1";
msg.error (tmp.str ());
state = error;
}
}
NumberSourceUnique (const BlockModel& al)
: NumberSource (al)
{ }
};
static struct NumberSourceUniqueSyntax : public DeclareModel
{
Model* make (const BlockModel& al) const
{ return new NumberSourceUnique (al); }
NumberSourceUniqueSyntax ()
: DeclareModel (Number::component, "source_unique", "source",
"Find unique number in time series.")
{ }
void load_frame (Frame&) const
{ }
} NumberSourceUnique_syntax;
struct NumberSourceAverage : public NumberSource
{
void initialize_derived (Treelog& msg)
{
const std::vector<Time>& time = source->time ();
const size_t size = time.size ();
int count = 0;
val = 0.0;
for (size_t i = 0; i < size; i++)
if ((!begin.get () || time[i] > *begin)
&& (!end.get () || time[i] <= *end))
{
val += source->value ()[i];
count++;
}
if (count == 0U)
{
msg.warning ("Can't take average of zero elements");
state = is_missing;
}
else
{
val /= (count + 0.0);
state = has_value;
}
}
NumberSourceAverage (const BlockModel& al)
: NumberSource (al)
{ }
};
static struct NumberSourceAverageSyntax : public DeclareModel
{
Model* make (const BlockModel& al) const
{ return new NumberSourceAverage (al); }
NumberSourceAverageSyntax ()
: DeclareModel (Number::component, "source_average", "source",
"Find average number in time series.")
{ }
void load_frame (Frame&) const
{ }
} NumberSourceAverage_syntax;
struct NumberSourceSum : public NumberSource
{
void initialize_derived (Treelog&)
{
const std::vector<Time>& time = source->time ();
const size_t size = time.size ();
val = 0.0;
state = has_value;
for (size_t i = 0; i < size; i++)
if ((!begin.get () || time[i] > *begin)
&& (!end.get () || time[i] <= *end))
val += source->value ()[i];
}
NumberSourceSum (const BlockModel& al)
: NumberSource (al)
{ }
};
static struct NumberSourceSumSyntax : public DeclareModel
{
Model* make (const BlockModel& al) const
{ return new NumberSourceSum (al); }
NumberSourceSumSyntax ()
: DeclareModel (Number::component, "source_sum", "source",
"Calculate the sum of the values in a time series.")
{ }
void load_frame (Frame&) const
{ }
} NumberSourceSum_syntax;
struct NumberSourceIncrease : public NumberSource
{
void initialize_derived (Treelog& msg)
{
const std::vector<Time>& time = source->time ();
const std::vector<double>& value = source->value ();
const size_t size = time.size ();
daisy_assert (value.size () == size);
state = has_value;
if (size < 2)
{
msg.warning ("Need two elements to make a difference");
val = 0;
return;
}
double first = value[0];
double last = value[0];
for (size_t i = 1; i < size; i++)
{
if (begin.get () && time[i] < *begin)
first = value[i];
if (!end.get () || time[i] <= *end)
last = value[i];
}
val = last - first;
}
NumberSourceIncrease (const BlockModel& al)
: NumberSource (al)
{ }
};
static struct NumberSourceIncreaseSyntax : public DeclareModel
{
Model* make (const BlockModel& al) const
{ return new NumberSourceIncrease (al); }
NumberSourceIncreaseSyntax ()
: DeclareModel (Number::component, "source_increase", "source",
"Find increase in value during time series.")
{ }
void load_frame (Frame&) const
{ }
} NumberSourceIncrease_syntax;
// number_source.C ends here