nidas  v1.2-1520
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 
30 #include <nidas/core/Resampler.h>
31 #include <nidas/core/SampleTag.h>
32 
33 #define SYNC_RECORD_ID 3
34 #define SYNC_RECORD_HEADER_ID 2
35 
36 namespace nidas {
37 
38 namespace util {
39 class LogContext;
40 }
41 
42 namespace core {
43 class Variable;
44 class SampleTracer;
45 }
46 
47 namespace dynld { namespace raf {
48 
49 using namespace nidas::core;
50 
51 class Aircraft;
52 
53 class SyncInfo;
54 
56 {
185 public:
186 
188 
189  virtual ~SyncRecordSource();
190 
205  static void
206  selectVariablesFromProject(Project* project,
207  std::list<const Variable*>& variables);
208 
212  static void
213  selectVariablesFromSensor(DSMSensor* sensor,
214  std::list<const Variable*>& variables);
215 
217 
218  SampleSource* getProcessedSampleSource() { return &_source; }
219 
223  std::list<const SampleTag*> getSampleTags() const
224  {
225  return _source.getSampleTags();
226  }
227 
232  {
233  return _source.getSampleTagIterator();
234  }
235 
239  void addSampleClient(SampleClient* client) throw()
240  {
241  _source.addSampleClient(client);
242  }
243 
244  void removeSampleClient(SampleClient* client) throw()
245  {
246  _source.removeSampleClient(client);
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 
259  void removeSampleClientForTag(SampleClient* client,const SampleTag*) throw()
260  {
261  _source.removeSampleClient(client);
262  }
263 
264  int getClientCount() const throw()
265  {
266  return _source.getClientCount();
267  }
268 
273  void flush() throw();
274 
275  const SampleStats& getSampleStats() const
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 
349 private:
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  {
373  _source.addSampleTag(tag);
374  }
375 
376  void removeSampleTag(const SampleTag* tag) throw ()
377  {
378  _source.removeSampleTag(tag);
379  }
380 
381  void createHeader(std::ostream&) throw();
382 
383  void
384  sendSyncRecord();
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 
411  SampleTag _syncRecordHeaderSampleTag;
412 
413  SampleTag _syncRecordDataSampleTag;
414 
415  size_t _recSize;
416 
417  dsm_time_t _syncHeaderTime;
418 
419  dsm_time_t _syncTime[2];
420 
424  int _current;
425 
426  int _halfMaxUsecsPerSample;
427 
428  SampleT<double>* _syncRecord[2];
429 
430  double* _dataPtr[2];
431 
432  size_t _unrecognizedSamples;
433 
434  std::ostringstream _headerStream;
435 
436  const Aircraft* _aircraft;
437 
438  bool _initialized;
439 
440  int _unknownSampleType;
441 
442  unsigned int _badLaterSamples;
443 
446 
448  SyncRecordSource& operator=(const SyncRecordSource&);
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 
464 class SyncInfo
465 {
466 public:
467 
471  SyncInfo(dsm_sample_id_t i, float r, SyncRecordSource* srs);
472 
473 #ifdef EXPLICIT_SYNCINFO_COPY_ASSIGN
474 
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 
491  dsm_sample_id_t id;
492 
496  float rate;
497 
503  int nSlots;
504 
509  int dtUsec;
510 
511 private:
515  int islot;
516 
520  int irec;
521 
522 public:
523 
524  int getSlotIndex() const { return islot; }
525 
526  bool incrementSlot();
527 
528  bool decrementSlot();
529 
530  bool checkNonIntRateIncrement();
531 
532  void computeSlotIndex(const Sample* samp);
533 
534  int getRecordIndex() const { return irec; }
535 
537  {
538  irec = _srs->nextRecordIndex(irec);
539  }
540 
542  {
543  incrementRecord();
544  }
545 
549  std::vector<size_t> varLengths;
550 
556  size_t sampleLength;
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 
610  int minDiff;
611 
632  int skipMod;
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 
662 private:
663 
664  SyncRecordSource* _srs;
665 
666 };
667 
668 }}} // namespace nidas namespace dynld namespace raf
669 
670 #endif
Interface for a resampler, simply a SampleClient and a SampleSource.
Definition: Resampler.h:39
SampleSource * getProcessedSampleSource()
Several objects in NIDAS can be both a SampleSource of raw Samples and processed Samples.
Definition: SyncRecordSource.h:218
void decrementRecord()
Definition: SyncRecordSource.h:541
unsigned int nLateSamp
How many successive samples have been later than their slot time by more than TDIFF_CHECK_USEC.
Definition: SyncRecordSource.h:655
std::list< const SampleTag * > getSampleTags() const
Get the output SampleTags.
Definition: SyncRecordSource.h:223
SampleTagIterator getSampleTagIterator() const
Implementation of SampleSource::getSampleTagIterator().
Definition: SyncRecordSource.h:231
unsigned int dsm_sample_id_t
Definition: Sample.h:63
dsm_sample_id_t id
Definition: SyncRecordSource.h:491
unsigned int total
Definition: SyncRecordSource.h:586
unsigned int discarded
Definition: SyncRecordSource.h:576
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:61
Class describing a sampled variable.
Definition: Variable.h:46
Pure virtual interface for a source of Samples.
Definition: SampleSource.h:48
unsigned int outOfSlotMax
Once nEarlySamp or nLateSamp exceed this value, adjust the slot index.
Definition: SyncRecordSource.h:660
unsigned int nskips
Definition: SyncRecordSource.h:582
Parameters needed for each sample to assemble and write a sync record.
Definition: SyncRecordSource.h:464
int getClientCount() const
How many SampleClients are currently in my list.
Definition: SyncRecordSource.h:264
void incrementRecord()
Definition: SyncRecordSource.h:536
A source of samples.
Definition: SampleSourceSupport.h:47
void addSampleClientForTag(SampleClient *client, const SampleTag *)
Add a Client for a given SampleTag.
Definition: SyncRecordSource.h:253
int minDiff
The minimum difference between the sample time tags and their corresponding slot times is computed ov...
Definition: SyncRecordSource.h:610
float rate
Sampling rate of the sample.
Definition: SyncRecordSource.h:496
int getRecordIndex() const
Definition: SyncRecordSource.h:534
Pure virtual interface of a client of Samples.
Definition: SampleClient.h:38
Definition: SyncRecordSource.h:55
int getSlotIndex() const
Definition: SyncRecordSource.h:524
std::vector< size_t > varLengths
Number of values for each variable in the sample.
Definition: SyncRecordSource.h:549
int irec
Index of current sync record for this sample.
Definition: SyncRecordSource.h:520
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
int dtUsec
Number of microseconds per sample, 1000000/rate, rounded to an integer.
Definition: SyncRecordSource.h:509
int skipModCount
Sample counter used for non-integral rates.
Definition: SyncRecordSource.h:637
std::vector< size_t > varSRIndex
Indices of the each variable in the sync record array of doubles.
Definition: SyncRecordSource.h:574
A typed Sample, with data of type DataT.
Definition: Sample.h:393
SyncRecordSource * _srs
Definition: SyncRecordSource.h:664
void removeSampleTag(const SampleTag *tag)
Definition: SyncRecordSource.h:376
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
static unsigned int ncopy
Definition: SyncRecordSource.h:478
Interface to a data sample.
Definition: Sample.h:189
bool overWritten
Definition: SyncRecordSource.h:578
DSMSensor provides the basic support for reading, processing and distributing samples from a sensor a...
Definition: DSMSensor.h:87
SampleSource * getRawSampleSource()
Several objects in NIDAS can be both a SampleSource of raw Samples and processed Samples.
Definition: SyncRecordSource.h:216
int skipMod
skipMod provides a way to insert NaNs in sync records with non-integral rates.
Definition: SyncRecordSource.h:632
Class for iterating over the SampleTags of a Project, Site, DSMConfig, or a SampleSource.
Definition: NidsIterators.h:217
#define USECS_PER_MSEC
Definition: types.h:115
unsigned int noverWritten
Definition: SyncRecordSource.h:580
Class describing a group of variables that are sampled and handled together.
Definition: SampleTag.h:87
Aircraft is a sub-class of a measurement Site.
Definition: Aircraft.h:42
int islot
Index of next slot for the sample in the current sync record.
Definition: SyncRecordSource.h:515
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 removeSampleClientForTag(SampleClient *client, const SampleTag *)
Remove a SampleClient for a given SampleTag from this SampleSource.
Definition: SyncRecordSource.h:259
A source of samples.
Definition: SampleStats.h:41
void addSampleClient(SampleClient *client)
Implementation of SampleSource::addSampleClient().
Definition: SyncRecordSource.h:239
Definition: Project.h:60
SampleTracer uses a Logger to log messages about samples as they are encountered in the code...
Definition: SampleTracer.h:22
int minDiffInit
See the comment below about minDiff.
Definition: SyncRecordSource.h:602
void removeSampleClient(SampleClient *client)
Remove a SampleClient from this SampleSource.
Definition: SyncRecordSource.h:244
std::list< const Variable * > variables
Variables in the sample.
Definition: SyncRecordSource.h:568
void addSampleTag(const SampleTag *tag)
Add a SampleTag to this SampleSource.
Definition: SampleSourceSupport.cc:60
static unsigned int nassign
Definition: SyncRecordSource.h:484