-
Notifications
You must be signed in to change notification settings - Fork 6
/
StripHistoryLANL.cc
283 lines (220 loc) · 6.99 KB
/
StripHistoryLANL.cc
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
/*************************************************************************\
* Copyright (c) 1994-2004 The University of Chicago, as Operator of Argonne
* National Laboratory.
* Copyright (c) 1997-2003 Southeastern Universities Research Association,
* as Operator of Thomas Jefferson National Accelerator Facility.
* Copyright (c) 1997-2002 Deutches Elektronen-Synchrotron in der Helmholtz-
* Gemelnschaft (DESY).
* This file is distributed subject to a Software License Agreement found
* in the file LICENSE that is included with this distribution.
\*************************************************************************/
#define NO_X11_HERE /* Albert */
extern "C" {
#include "StripHistory.h"
#include "StripDataSource.h"
}
#include <iostream.h>
#include "osiTime.h"
#include "osiTimeHelper.h"
#include <BinArchive.h>
#include <BinValueIterator.h>
#include <signal.h>
#include <math.h>
#define ARCHIVE_NAME_ENVIRONMENT_VAR "STRIP_ARCHIVE"
#define ARCHIVE_REQUEST_ENVIRONMENT_VAR "STRIP_VERBOSE"
/*USING_NAMESPACE_CHANARCH
#ifdef USE_NAMESPACE_STD
using namespace std;
#endif
*/
// AccessChanArch does the actual data acquisition from the archiver
int AccessChanArch(ArchiveI *,
const stdString &,
const osiTime &,
const osiTime &,
double *,
short *,
struct timeval *,
size_t);
// CountSamples finds out how many distinct data points we have in the requested range
// in order to allocate memory properly
size_t CountSamples(ArchiveI *,
const stdString &,
const osiTime &,
const osiTime &);
static bool VERBOSE = false;
/* StripHistoryInfo
*
* Contains instance data for the archive service.
*/
typedef struct _StripHistoryInfo
{
Strip strip;
ArchiveI *archiveI;
}
StripHistoryInfo;
/* StripHistory_init
*/
extern "C" StripHistory StripHistory_init (Strip strip)
{
StripHistoryInfo *shi = 0;
stdString ARCHIVE_NAME, VERBOSE_SWITCH;
ARCHIVE_NAME = getenv(ARCHIVE_NAME_ENVIRONMENT_VAR);
if(ARCHIVE_NAME.empty())
{
cout << "You must declare environment variable STRIP_ARCHIVE" << endl;
exit(1);
}
VERBOSE_SWITCH = getenv(ARCHIVE_REQUEST_ENVIRONMENT_VAR);
if(VERBOSE_SWITCH.empty())
{
cout << "You may want to declare environment variable STRIP_VERBOSE (=on or =off)" << endl;
}
else if(VERBOSE_SWITCH == "on")
{
cout << "Strip_History Verbose is turned on" << endl;
VERBOSE = true;
}
if (shi = (StripHistoryInfo *)malloc (sizeof(StripHistoryInfo)))
{
shi->strip = strip;
shi->archiveI = new BinArchive (ARCHIVE_NAME);
}
return (StripHistory)shi;
}
/* StripHistory_delete
*/
extern "C" void StripHistory_delete (StripHistory the_shi)
{
StripHistoryInfo *shi = (StripHistoryInfo *)the_shi;
delete shi->archiveI;
free (shi);
}
/* StripHistory_fetch
*/
extern "C" FetchStatus StripHistory_fetch (StripHistory the_shi,
char *name,
struct timeval *begin,
struct timeval *end,
StripHistoryResult *result,
StripHistoryCallback BOGUS(callback),
void *BOGUS(call_data))
{
StripHistoryInfo *shi = (StripHistoryInfo *)the_shi;
size_t samples;
int no_of_points;
osiTime t0 = osiTime(begin->tv_sec, (begin->tv_usec * 1000));
osiTime t1 = osiTime(end->tv_sec, (end->tv_usec * 1000));
samples = CountSamples(shi->archiveI, name, t0, t1);
if(VERBOSE == true)
{
cout << "Requested Channel: " << name << " " << t0 << " - " << t1
<< " (" << samples << ")\n";
}
struct timeval *times = new struct timeval[samples];
double *data = new double[samples];
short *status = new short[samples];
no_of_points = AccessChanArch(shi->archiveI, name, t0, t1, data, status, times, samples);
if(VERBOSE == true)
{
cout << "Requested Channel: " << name << " " << t0 << " - " << t1
<< "NoP= (" << no_of_points << ")\n";
}
/* remember the request range */
result->t0 = *begin;
result->t1 = *end;
if ((compare_times (begin, ×[no_of_points-1]) <= 0) &&
(compare_times (end, ×[0]) >= 0) &&
(compare_times (begin, end) <= 0))
{
result->times = times;
result->data = data;
result->status = status;
result->n_points = no_of_points;
result->fetch_stat = FETCH_DONE;
}
else
{
result->times = 0;
result->data = 0;
result->status = 0;
delete[] times;
delete[] data;
delete[] status;
result->fetch_stat = FETCH_NODATA;
}
return result->fetch_stat;
}
/* StripHistory_cancel
*/
extern "C" void StripHistory_cancel (StripHistory BOGUS(the_shi),
StripHistoryResult *BOGUS(result))
{
}
/* StripHistoryResult_release
*/
void StripHistoryResult_release (StripHistory BOGUS(the_shi),
StripHistoryResult *result)
{
delete[] result->data;
delete[] result->times;
delete[] result->status;
}
size_t CountSamples(ArchiveI *archiveI, const stdString &channel_name, const osiTime &start, const osiTime &end)
{
Archive archive (archiveI);
ChannelIterator channel(archive);
ValueIterator value(archive);
size_t chunk_size=0, chunk_cumulative=0;
archive.findChannelByName (channel_name, channel);
channel->getValueAfterTime (start, value);
while (value && (end == nullTime || value->getTime() < end))
{
chunk_size = value.determineChunk(end);
for(int i = 0; i < chunk_size; i++) ++value;
chunk_cumulative += chunk_size;
}
archive.detach();
chunk_cumulative += 2;
return chunk_cumulative;
}
int AccessChanArch(ArchiveI *archiveI, const stdString &channel_name, const osiTime &start, const osiTime &end, double *data, short *status, struct timeval *times, size_t samples)
{
Archive archive (archiveI);
ChannelIterator channel(archive);
ValueIterator value(archive);
int i = 0;
archive.findChannelByName (channel_name, channel);
channel->getValueAfterTime (start, value);
if(!value)
{
for(i = 0; i < 2; i++) status[i] &= ~DATASTAT_PLOTABLE;
times[0].tv_sec = start.getSec();
times[0].tv_usec = start.getUSec();
times[1].tv_sec = end.getSec();
times[1].tv_usec = start.getUSec();
archive.detach();
return 2;
}
while (value && (end == nullTime || value->getTime() < end) && i < (samples - 1))
{
if(value->isInfo())
status[i] &= ~DATASTAT_PLOTABLE;
else {
data[i] = value->getDouble();
status[i] |= DATASTAT_PLOTABLE;
}
times[i].tv_sec = value->getTime().getSec();
times[i].tv_usec = value->getTime().getUSec();
++value;
++i;
}
//pad end with a ~DATASTAT_PLOTABLE to avoid interpolation
status[i] &= ~DATASTAT_PLOTABLE;
times[i].tv_sec = end.getSec();
times[i].tv_usec = end.getUSec();
//########################
archive.detach();
return ++i;
}
#undef NO_X11_HERE /* Albert */