nidas v1.2.3
SyncRecordSource.h
Go to the documentation of this file.
1// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2// vim: set shiftwidth=4 softtabstop=4 expandtab:
3/*
4 ********************************************************************
5 ** NIDAS: NCAR In-situ Data Acquistion Software
6 **
7 ** 2005, Copyright University Corporation for Atmospheric Research
8 **
9 ** This program is free software; you can redistribute it and/or modify
10 ** it under the terms of the GNU General Public License as published by
11 ** the Free Software Foundation; either version 2 of the License, or
12 ** (at your option) any later version.
13 **
14 ** This program is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ** GNU General Public License for more details.
18 **
19 ** The LICENSE.txt file accompanying this software contains
20 ** a copy of the GNU General Public License. If it is not found,
21 ** write to the Free Software Foundation, Inc.,
22 ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 **
24 ********************************************************************
25*/
26
27#ifndef NIDAS_CORE_SYNCRECORDSOURCE_H
28#define NIDAS_CORE_SYNCRECORDSOURCE_H
29
32
33#define SYNC_RECORD_ID 3
34#define SYNC_RECORD_HEADER_ID 2
35
36namespace nidas {
37
38namespace util {
39class LogContext;
40}
41
42namespace core {
43class Variable;
44class SampleTracer;
45}
46
47namespace dynld { namespace raf {
48
49using namespace nidas::core;
50
51class Aircraft;
52
53class SyncInfo;
54
56{
185public:
186
188
189 virtual ~SyncRecordSource();
190
205 static void
207 std::list<const Variable*>& variables);
208
212 static void
214 std::list<const Variable*>& variables);
215
217
219
223 std::list<const SampleTag*> getSampleTags() const
224 {
225 return _source.getSampleTags();
226 }
227
235
239 void addSampleClient(SampleClient* client) throw()
240 {
241 _source.addSampleClient(client);
242 }
243
244 void removeSampleClient(SampleClient* client) throw()
245 {
247 }
248
253 void addSampleClientForTag(SampleClient* client,const SampleTag*) throw()
254 {
255 // I only have one tag, so just call addSampleClient()
256 _source.addSampleClient(client);
257 }
258
260 {
262 }
263
264 int getClientCount() const throw()
265 {
266 return _source.getClientCount();
267 }
268
273 void flush() throw();
274
276 {
277 return _source.getSampleStats();
278 }
279
280 void connect(SampleSource* source) throw();
281
282 void disconnect(SampleSource* source) throw();
283
291 void init();
292
303 void sendSyncHeader() throw();
304
305 bool receive(const Sample*) throw();
306
307 // static const int NSYNCREC = 2;
308
309 void
310 preLoadCalibrations(dsm_time_t sampleTime) throw();
311
316 static const int NSLOT_LIMIT = 2;
317
321 static int nextRecordIndex(int i);
322
328 static int prevRecordIndex(int i);
329
334 bool prevRecord(SyncInfo& sinfo);
335
340 bool nextRecord(SyncInfo& sinfo);
341
342 static const int NSYNCREC = 2;
343
347 int computeSlotIndex(const Sample* samp, SyncInfo& sinfo);
348
349private:
350
354 bool checkTime(const Sample* samp, SyncInfo& sinfo, SampleTracer& stracer,
355 nidas::util::LogContext& lc, int warn_times);
356
357 void slog(SampleTracer& stracer, const std::string& msg,
358 const Sample* samp, const SyncInfo& sinfo);
359
360 void log(nidas::util::LogContext& lc, const std::string& msg,
361 const Sample* samp, const SyncInfo& sinfo);
362
363 void log(nidas::util::LogContext& lc, const std::string& msg,
364 const SyncInfo& sinfo);
365
367
371 void addSampleTag(const SampleTag* tag) throw ()
372 {
374 }
375
376 void removeSampleTag(const SampleTag* tag) throw ()
377 {
379 }
380
381 void createHeader(std::ostream&) throw();
382
383 void
385
386 void
387 allocateRecord(int irec, dsm_time_t timetag);
388
389 int advanceRecord(dsm_time_t timetag);
390
404 std::map<dsm_sample_id_t, SyncInfo> _syncInfo;
405
409 std::list<const Variable*> _variables;
410
412
414
415 size_t _recSize;
416
418
420
425
427
428 SampleT<double>* _syncRecord[2];
429
430 double* _dataPtr[2];
431
433
434 std::ostringstream _headerStream;
435
437
439
441
442 unsigned int _badLaterSamples;
443
446
449
450};
451
452/*
453 * Whether to define an explicit copy constructor
454 * and assignment op for SyncInfo. Some care is taken
455 * in the code to avoid copy and assignment. Define
456 * this to detect how often they are called.
457 */
458#define EXPLICIT_SYNCINFO_COPY_ASSIGN
459
465{
466public:
467
471 SyncInfo(dsm_sample_id_t i, float r, SyncRecordSource* srs);
472
473#ifdef EXPLICIT_SYNCINFO_COPY_ASSIGN
477 SyncInfo(const SyncInfo&);
478 static unsigned int ncopy;
479
483 SyncInfo& operator=(const SyncInfo&);
484 static unsigned int nassign;
485#endif
486
487 void addVariable(const Variable* var);
488
489 void advanceRecord(int last);
490
492
496 float rate;
497
504
510
511private:
515 int islot;
516
520 int irec;
521
522public:
523
524 int getSlotIndex() const { return islot; }
525
526 bool incrementSlot();
527
528 bool decrementSlot();
529
531
532 void computeSlotIndex(const Sample* samp);
533
534 int getRecordIndex() const { return irec; }
535
537 {
539 }
540
542 {
544 }
545
549 std::vector<size_t> varLengths;
550
557
564
568 std::list<const Variable*> variables;
569
574 std::vector<size_t> varSRIndex;
575
576 unsigned int discarded;
577
579
580 unsigned int noverWritten;
581
582 unsigned int nskips;
583
584 unsigned int skipped;
585
586 unsigned int total;
587
603
611
633
638
643 static const int TDIFF_CHECK_USEC = 2 * USECS_PER_MSEC;
644
649 unsigned int nEarlySamp;
650
655 unsigned int nLateSamp;
656
660 unsigned int outOfSlotMax;
661
662private:
663
665
666};
667
668}}} // namespace nidas namespace dynld namespace raf
669
670#endif
DSMSensor provides the basic support for reading, processing and distributing samples from a sensor a...
Definition DSMSensor.h:88
Definition Project.h:60
Interface for a resampler, simply a SampleClient and a SampleSource.
Definition Resampler.h:39
Pure virtual interface of a client of Samples.
Definition SampleClient.h:38
A source of samples.
Definition SampleSourceSupport.h:47
void removeSampleClient(SampleClient *c)
Remove a SampleClient from this SampleSource This will also remove a SampleClient if it has been adde...
Definition SampleSourceSupport.cc:88
void addSampleClient(SampleClient *c)
Add a SampleClient to this SampleSource.
Definition SampleSourceSupport.cc:80
SampleTagIterator getSampleTagIterator() const
Definition SampleSourceSupport.cc:75
int getClientCount() const
How many SampleClients are currently in my list.
Definition SampleSourceSupport.cc:136
void removeSampleTag(const SampleTag *tag)
Definition SampleSourceSupport.cc:67
const SampleStats & getSampleStats() const
Definition SampleSourceSupport.h:149
void addSampleTag(const SampleTag *tag)
Add a SampleTag to this SampleSource.
Definition SampleSourceSupport.cc:60
std::list< const SampleTag * > getSampleTags() const
What SampleTags am I a SampleSource for?
Definition SampleSourceSupport.cc:54
Pure virtual interface for a source of Samples.
Definition SampleSource.h:48
A source of samples.
Definition SampleStats.h:41
Class for iterating over the SampleTags of a Project, Site, DSMConfig, or a SampleSource.
Definition NidsIterators.h:218
Class describing a group of variables that are sampled and handled together.
Definition SampleTag.h:88
SampleTracer uses a Logger to log messages about samples as they are encountered in the code.
Definition SampleTracer.h:23
Interface to a data sample.
Definition Sample.h:190
Class describing a sampled variable.
Definition Variable.h:47
Aircraft is a sub-class of a measurement Site.
Definition Aircraft.h:42
Parameters needed for each sample to assemble and write a sync record.
Definition SyncRecordSource.h:465
int dtUsec
Number of microseconds per sample, 1000000/rate, rounded to an integer.
Definition SyncRecordSource.h:509
std::list< const Variable * > variables
Variables in the sample.
Definition SyncRecordSource.h:568
unsigned int discarded
Definition SyncRecordSource.h:576
void decrementRecord()
Definition SyncRecordSource.h:541
bool overWritten
Definition SyncRecordSource.h:578
std::vector< size_t > varLengths
Number of values for each variable in the sample.
Definition SyncRecordSource.h:549
int minDiff
The minimum difference between the sample time tags and their corresponding slot times is computed ov...
Definition SyncRecordSource.h:610
dsm_sample_id_t id
Definition SyncRecordSource.h:491
bool incrementSlot()
Definition SyncRecordSource.cc:183
void incrementRecord()
Definition SyncRecordSource.h:536
int getSlotIndex() const
Definition SyncRecordSource.h:524
int skipMod
skipMod provides a way to insert NaNs in sync records with non-integral rates.
Definition SyncRecordSource.h:632
static unsigned int nassign
Definition SyncRecordSource.h:484
static unsigned int ncopy
Definition SyncRecordSource.h:478
int skipModCount
Sample counter used for non-integral rates.
Definition SyncRecordSource.h:637
static const int TDIFF_CHECK_USEC
If sample time tag differs from the slot time by this much or more then increment nEarlySamp or nLate...
Definition SyncRecordSource.h:643
size_t sampleLength
Number of data values in one second: nSlots times the sum of the varLengths for the variables in the ...
Definition SyncRecordSource.h:556
int nSlots
Smallest integer not less than rate, computed as ceil(rate).
Definition SyncRecordSource.h:503
unsigned int nEarlySamp
How many successive samples have been earlier than their slot time by more than TDIFF_CHECK_USEC.
Definition SyncRecordSource.h:649
void computeSlotIndex(const Sample *samp)
Definition SyncRecordSource.cc:227
SyncRecordSource * _srs
Definition SyncRecordSource.h:664
SyncInfo & operator=(const SyncInfo &)
Assignment.
Definition SyncRecordSource.cc:132
bool decrementSlot()
Definition SyncRecordSource.cc:215
SyncInfo(dsm_sample_id_t i, float r, SyncRecordSource *srs)
Constructor.
Definition SyncRecordSource.cc:73
bool checkNonIntRateIncrement()
Definition SyncRecordSource.cc:197
int islot
Index of next slot for the sample in the current sync record.
Definition SyncRecordSource.h:515
int getRecordIndex() const
Definition SyncRecordSource.h:534
std::vector< size_t > varSRIndex
Indices of the each variable in the sync record array of doubles.
Definition SyncRecordSource.h:574
unsigned int outOfSlotMax
Once nEarlySamp or nLateSamp exceed this value, adjust the slot index.
Definition SyncRecordSource.h:660
int irec
Index of current sync record for this sample.
Definition SyncRecordSource.h:520
unsigned int noverWritten
Definition SyncRecordSource.h:580
size_t sampleSRIndex
Index of this sample in the sync record array of doubles.
Definition SyncRecordSource.h:563
unsigned int skipped
Definition SyncRecordSource.h:584
void addVariable(const Variable *var)
Definition SyncRecordSource.cc:164
unsigned int total
Definition SyncRecordSource.h:586
float rate
Sampling rate of the sample.
Definition SyncRecordSource.h:496
void advanceRecord(int last)
Definition SyncRecordSource.cc:173
unsigned int nskips
Definition SyncRecordSource.h:582
int minDiffInit
See the comment below about minDiff.
Definition SyncRecordSource.h:602
unsigned int nLateSamp
How many successive samples have been later than their slot time by more than TDIFF_CHECK_USEC.
Definition SyncRecordSource.h:655
Definition SyncRecordSource.h:56
SampleSourceSupport _source
Definition SyncRecordSource.h:366
void removeSampleClient(SampleClient *client)
Remove a SampleClient from this SampleSource.
Definition SyncRecordSource.h:244
std::map< dsm_sample_id_t, SyncInfo > _syncInfo
Info kept for each sample in order to assemble and write sync records.
Definition SyncRecordSource.h:404
void log(nidas::util::LogContext &lc, const std::string &msg, const Sample *samp, const SyncInfo &sinfo)
static const int NSLOT_LIMIT
Maximum number of delta-Ts allowed between a sample time tag and its slot in the sync record.
Definition SyncRecordSource.h:316
void slog(SampleTracer &stracer, const std::string &msg, const Sample *samp, const SyncInfo &sinfo)
Definition SyncRecordSource.cc:967
SampleT< double > * _syncRecord[2]
Definition SyncRecordSource.h:428
int advanceRecord(dsm_time_t timetag)
Definition SyncRecordSource.cc:682
unsigned int _badLaterSamples
Definition SyncRecordSource.h:442
void removeSampleClientForTag(SampleClient *client, const SampleTag *)
Remove a SampleClient for a given SampleTag from this SampleSource.
Definition SyncRecordSource.h:259
const Aircraft * _aircraft
Definition SyncRecordSource.h:436
void addSampleClient(SampleClient *client)
Implementation of SampleSource::addSampleClient().
Definition SyncRecordSource.h:239
std::ostringstream _headerStream
Definition SyncRecordSource.h:434
bool checkTime(const Sample *samp, SyncInfo &sinfo, SampleTracer &stracer, nidas::util::LogContext &lc, int warn_times)
Definition SyncRecordSource.cc:813
bool prevRecord(SyncInfo &sinfo)
If the previous sync record is non-null, do sinfo.decrementRecord().
Definition SyncRecordSource.cc:757
void allocateRecord(int irec, dsm_time_t timetag)
Definition SyncRecordSource.cc:656
void addSampleClientForTag(SampleClient *client, const SampleTag *)
Add a Client for a given SampleTag.
Definition SyncRecordSource.h:253
void createHeader(std::ostream &)
Definition SyncRecordSource.cc:469
dsm_time_t _syncTime[2]
Definition SyncRecordSource.h:419
bool nextRecord(SyncInfo &sinfo)
If the next sync record is non-null, do sinfo.incrementRecord().
Definition SyncRecordSource.cc:769
bool _initialized
Definition SyncRecordSource.h:438
void disconnect(SampleSource *source)
Definition SyncRecordSource.cc:454
int computeSlotIndex(const Sample *samp, SyncInfo &sinfo)
Which time slot should a sample be placed.
Definition SyncRecordSource.cc:793
int _unknownSampleType
Definition SyncRecordSource.h:440
SyncRecordSource()
SyncRecordSource builds "sync records" from a sample stream.
Definition SyncRecordSource.cc:233
void removeSampleTag(const SampleTag *tag)
Definition SyncRecordSource.h:376
int getClientCount() const
How many SampleClients are currently in my list.
Definition SyncRecordSource.h:264
static void selectVariablesFromProject(Project *project, std::list< const Variable * > &variables)
This method and selectVariablesFromSensor() are used to select the list of variables from a Project c...
Definition SyncRecordSource.cc:347
size_t _recSize
Definition SyncRecordSource.h:415
static const int NSYNCREC
Definition SyncRecordSource.h:342
SampleSource * getRawSampleSource()
Several objects in NIDAS can be both a SampleSource of raw Samples and processed Samples.
Definition SyncRecordSource.h:216
void init()
Create the SyncInfo objects for all samples and their variables that will go in the sync record.
Definition SyncRecordSource.cc:368
const SampleStats & getSampleStats() const
Definition SyncRecordSource.h:275
int _current
Index of current sync record.
Definition SyncRecordSource.h:424
void flush()
Implementation of Resampler::flush().
Definition SyncRecordSource.cc:628
static int nextRecordIndex(int i)
Return the index of the next sync record.
Definition SyncRecordSource.cc:746
static void selectVariablesFromSensor(DSMSensor *sensor, std::list< const Variable * > &variables)
See selectVariablesFromProject().
Definition SyncRecordSource.cc:297
SampleTag _syncRecordHeaderSampleTag
Definition SyncRecordSource.h:411
dsm_time_t _syncHeaderTime
Definition SyncRecordSource.h:417
void sendSyncRecord()
Definition SyncRecordSource.cc:638
size_t _unrecognizedSamples
Definition SyncRecordSource.h:432
std::list< const Variable * > _variables
List of all variables in the sync record.
Definition SyncRecordSource.h:409
SampleSource * getProcessedSampleSource()
Several objects in NIDAS can be both a SampleSource of raw Samples and processed Samples.
Definition SyncRecordSource.h:218
double * _dataPtr[2]
Definition SyncRecordSource.h:430
std::list< const SampleTag * > getSampleTags() const
Get the output SampleTags.
Definition SyncRecordSource.h:223
SampleTag _syncRecordDataSampleTag
Definition SyncRecordSource.h:413
void preLoadCalibrations(dsm_time_t sampleTime)
Definition SyncRecordSource.cc:565
bool receive(const Sample *)
Method called to pass a sample to this client.
Definition SyncRecordSource.cc:1040
void sendSyncHeader()
Generate and send a sync record header sample.
Definition SyncRecordSource.cc:602
void connect(SampleSource *source)
Connect the resampler to a source.
Definition SyncRecordSource.cc:434
virtual ~SyncRecordSource()
Definition SyncRecordSource.cc:258
SyncRecordSource & operator=(const SyncRecordSource &)
No assignment.
SyncRecordSource(const SyncRecordSource &)
No copying.
static int prevRecordIndex(int i)
Return the index of the previous sync record.
Definition SyncRecordSource.cc:752
void addSampleTag(const SampleTag *tag)
Add a SampleTag to this SampleSource.
Definition SyncRecordSource.h:371
SampleTagIterator getSampleTagIterator() const
Implementation of SampleSource::getSampleTagIterator().
Definition SyncRecordSource.h:231
int _halfMaxUsecsPerSample
Definition SyncRecordSource.h:426
The essential core classes of nidas.
Definition A2DConverter.h:31
long long dsm_time_t
Posix time in microseconds, the number of non-leap microseconds since 1970 Jan 1 00:00 UTC.
Definition Sample.h:62
unsigned int dsm_sample_id_t
Definition Sample.h:64
Root namespace for the NCAR In-Situ Data Acquisition Software.
Definition A2DConverter.h:31
#define USECS_PER_MSEC
Definition types.h:115