nidas  v1.2-1520
CalFile.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  ** 2006, 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_CALFILE_H
28 #define NIDAS_CORE_CALFILE_H
29 
30 #include "DOMable.h"
31 #include <nidas/util/UTime.h>
32 #include <nidas/util/IOException.h>
34 
35 #include <vector>
36 #include <fstream>
37 
38 #include <regex.h>
39 
40 namespace nidas { namespace core {
41 
42 class DSMSensor;
43 
165 public:
166 
167  CalFile();
168 
174  CalFile(const CalFile&);
175 
181  CalFile& operator=(const CalFile&);
182 
186  ~CalFile();
187 
188  const std::string& getFile() const;
189 
193  void setFile(const std::string& val);
194 
200  const std::string& getPath() const;
201 
207  void setPath(const std::string& val);
208 
213  static std::vector<std::string> getAllPaths()
214  {
215  _staticMutex.lock();
216  std::vector<std::string> tmp = _allPaths;
218  return tmp;
219  }
220 
224  const std::string& getCurrentFileName() const
225  {
226  if (_include) return _include->getCurrentFileName();
227  return _currentFileName;
228  }
229 
235  void setName(const std::string& val)
236  {
237  _name = val;
238  }
239 
240  const std::string& getName() const
241  {
242  return _name;
243  }
244 
245  int getLineNumber() const
246  {
247  if (_include) return _include->getLineNumber();
248  return _nline;
249  }
250 
256  void open() throw(nidas::util::IOException);
257 
262  void close() throw();
263 
267  bool eof() const {
268  if (_include) return false;
269  return _eofState;
270  }
271 
272  const std::string& getTimeZone() const { return _timeZone; }
273 
279  void
280  setTimeZone(const std::string& val);
281 
282  const std::string& getDateTimeFormat() const
283  {
284  return _dateTimeFormat;
285  }
286 
292  void setDateTimeFormat(const std::string& val);
293 
302 
346  int readCF(nidas::util::UTime& time, float* data, int ndata,
347  std::vector<std::string>* fields=0)
348  throw(nidas::util::IOException,nidas::util::ParseException);
349 
357  const std::vector<std::string>&
358  getCurrentFields(nidas::util::UTime* time = 0);
359 
360  nidas::util::UTime
362  {
363  return _currentTime;
364  }
365 
374  float
375  getFloatField(int column, const std::vector<std::string>* fields = 0);
376 
393  int
394  getFields(int begin, int end, float* data,
395  const std::vector<std::string>* fields = 0);
396 
397  /*
398  * Return the value of the next time in the file.
399  * If there is no next record in the file, the returned value
400  * will be far off in the future.
401  */
403  {
404  return _nextTime;
405  }
406 
413  void setDSMSensor(const DSMSensor* val);
414 
415  const DSMSensor* getDSMSensor() const;
416 
417  void fromDOMElement(const xercesc::DOMElement* node)
419 
420 protected:
421 
422  nidas::util::UTime parseTime() throw(nidas::util::ParseException);
423 
433  void readLine() throw(nidas::util::IOException,nidas::util::ParseException);
434 
441  bool
443 
450  int
451  parseInclude();
452 
453  void openInclude(const std::string& name)
454  throw(nidas::util::IOException,nidas::util::ParseException);
455 
461  int
462  readCFInclude(nidas::util::UTime& time, float* data, int ndata,
463  std::vector<std::string>* fields_out);
464 
465 
466 private:
467 
477  nidas::util::UTime readTime()
478  throw(nidas::util::IOException,nidas::util::ParseException);
479 
480  int readCFNoLock(nidas::util::UTime& time, float* data, int ndata,
481  std::vector<std::string>* fields)
482  throw(nidas::util::IOException,nidas::util::ParseException);
483 
484  std::string _name;
485 
486  std::string _fileName;
487 
488  std::string _path;
489 
490  std::string _currentFileName;
491 
492  std::string _timeZone;
493 
494  bool _utcZone;
495 
496  std::string _dateTimeFormat;
497 
498  std::ifstream _fin;
499 
500  static const int INITIAL_CURLINE_LENGTH = 128;
501 
503 
504  char *_curline;
505 
506  int _curpos;
507 
508  bool _eofState;
509 
510  int _nline;
511 
512  nidas::util::UTime _nextTime;
513 
514  nidas::util::UTime _currentTime;
515  std::vector<std::string> _currentFields;
516 
520  nidas::util::UTime _includeTime;
521 
525  nidas::util::UTime _timeAfterInclude;
526 
530  nidas::util::UTime _timeFromInclude;
531 
533 
535 
536  static nidas::util::Mutex _staticMutex;
537 
538  static int _reUsers;
539 
540  static bool _reCompiled;
541 
542  static regex_t _dateFormatPreg;
543 
544  static regex_t _timeZonePreg;
545 
546  static regex_t _includePreg;
547 
548  static void freeREs();
549 
550  static void compileREs() throw(nidas::util::ParseException);
551 
552  static std::vector<std::string> _allPaths;
553 
554  nidas::util::Mutex _mutex;
555 };
556 
557 }} // namespace nidas namespace core
558 
559 #endif
nidas::util::UTime _timeFromInclude
Time stamp of last record in include file with time &lt;= _includeTime.
Definition: CalFile.h:530
A class for reading ASCII files containing a time series of calibration data.
Definition: CalFile.h:164
static std::vector< std::string > getAllPaths()
Return all the paths that have been set in all CalFile instances, in the order they were seen...
Definition: CalFile.h:213
void setPath(const std::string &val)
Set the search path to find the file, and any included files: one or more directory paths separated b...
Definition: CalFile.cc:207
A class for parsing, formatting and doing operations on time, based on Unix time conventions, where leap seconds are ignored, so that there are always 60 seconds in a minute, 3600 seconds in an hour and 86400 seconds in a day.
Definition: UTime.h:76
nidas::util::UTime _includeTime
Time stamp of include &quot;file&quot; record.
Definition: CalFile.h:520
std::string _fileName
Definition: CalFile.h:486
nidas::util::UTime readTime()
Read lines, parsing special comment lines and skipping other comments or blank lines, until a record line is found.
Definition: CalFile.cc:414
bool _eofState
Definition: CalFile.h:508
const std::string & getName() const
Definition: CalFile.h:240
static std::vector< std::string > _allPaths
Definition: CalFile.h:552
int getFields(int begin, int end, float *data, const std::vector< std::string > *fields=0)
Parse a range of columns from the fields vector as numbers and store them in the array data...
Definition: CalFile.cc:606
std::ifstream _fin
Definition: CalFile.h:498
void close()
Close file.
Definition: CalFile.cc:300
std::string _timeZone
Definition: CalFile.h:492
nidas::util::UTime getCurrentTime()
Definition: CalFile.h:361
nidas::util::UTime _nextTime
Definition: CalFile.h:512
bool _utcZone
Definition: CalFile.h:494
bool eof() const
Have we reached eof.
Definition: CalFile.h:267
nidas::util::UTime _timeAfterInclude
Time stamp of record after include &quot;file&quot;.
Definition: CalFile.h:525
const std::vector< std::string > & getCurrentFields(nidas::util::UTime *time=0)
Return the time and fields of the current record, the one last read with readCF().
Definition: CalFile.cc:319
nidas::util::UTime search(const nidas::util::UTime &tsearch)
Search forward in a file, returning the time of the last record in the file with a time less than or ...
Definition: CalFile.cc:346
void setDateTimeFormat(const std::string &val)
Set the format for reading the date &amp; time from the file.
Definition: CalFile.cc:250
int parseInclude()
If the current calfile record line is an include directive, parse the include filename, open it, and return 1.
Definition: CalFile.cc:444
static void freeREs()
Definition: CalFile.cc:98
Interface of an object that can be instantiated from a DOM element, via the fromDOMElement method...
Definition: DOMable.h:51
void setFile(const std::string &val)
Set the base name of the file to be opened.
Definition: CalFile.cc:195
static int _reUsers
Definition: CalFile.h:538
int getLineNumber() const
Definition: CalFile.h:245
int readCFInclude(nidas::util::UTime &time, float *data, int ndata, std::vector< std::string > *fields_out)
Internal version of readCF() which reads records from the current include file, if any...
Definition: CalFile.cc:480
void openInclude(const std::string &name)
Definition: CalFile.cc:774
static const int INITIAL_CURLINE_LENGTH
Definition: CalFile.h:500
void open()
Open the file.
Definition: CalFile.cc:266
void fromDOMElement(const xercesc::DOMElement *node)
Initialize myself from a xercesc::DOMElement.
Definition: CalFile.cc:795
int _nline
Definition: CalFile.h:510
nidas::util::UTime nextTime()
Definition: CalFile.h:402
static nidas::util::Mutex _staticMutex
Definition: CalFile.h:536
bool parseTimeComments()
Check the current line for special comments with timezone and datetime format settings.
Definition: CalFile.cc:733
const std::string & getCurrentFileName() const
Return the full file path of the current file.
Definition: CalFile.h:224
CalFile()
Definition: CalFile.cc:107
std::string _dateTimeFormat
Definition: CalFile.h:496
void lock()
Lock the Mutex.
Definition: ThreadSupport.h:206
static regex_t _includePreg
Definition: CalFile.h:546
const DSMSensor * getDSMSensor() const
Definition: CalFile.cc:245
void setTimeZone(const std::string &val)
Set the timezone for the dates &amp; times read from the file.
Definition: CalFile.cc:181
void setName(const std::string &val)
An instance of CalFile can have a name.
Definition: CalFile.h:235
int readCF(nidas::util::UTime &time, float *data, int ndata, std::vector< std::string > *fields=0)
Read the time and data from the current record, and return the number of values read.
Definition: CalFile.cc:433
std::string _name
Definition: CalFile.h:484
const std::string & getDateTimeFormat() const
Definition: CalFile.h:282
DSMSensor provides the basic support for reading, processing and distributing samples from a sensor a...
Definition: DSMSensor.h:87
static void compileREs()
Definition: CalFile.cc:65
int readCFNoLock(nidas::util::UTime &time, float *data, int ndata, std::vector< std::string > *fields)
Definition: CalFile.cc:534
nidas::util::Mutex _mutex
Definition: CalFile.h:554
char * _curline
Definition: CalFile.h:504
void setDSMSensor(const DSMSensor *val)
Set the DSMSensor associated with this CalFile.
Definition: CalFile.cc:240
Definition: IOException.h:37
const DSMSensor * _sensor
Definition: CalFile.h:534
void readLine()
Read forward to next non-comment line in CalFile.
Definition: CalFile.cc:673
static bool _reCompiled
Definition: CalFile.h:540
nidas::util::UTime parseTime()
Definition: CalFile.cc:387
float getFloatField(int column, const std::vector< std::string > *fields=0)
Convert the field at index column in the fields vector to a number, and return the number...
Definition: CalFile.cc:665
const std::string & getPath() const
Set the search path to find the file, and any included files: one or more directory paths separated b...
Definition: CalFile.cc:202
const std::string & getTimeZone() const
Definition: CalFile.h:272
~CalFile()
Closes the file if necessary.
Definition: CalFile.cc:167
std::string _path
Definition: CalFile.h:488
const std::string & getFile() const
Definition: CalFile.cc:190
std::string _currentFileName
Definition: CalFile.h:490
static regex_t _timeZonePreg
Definition: CalFile.h:544
CalFile & operator=(const CalFile &)
Assignment operator, like the copy constructor.
Definition: CalFile.cc:148
nidas::util::UTime _currentTime
Definition: CalFile.h:514
int _curpos
Definition: CalFile.h:506
void unlock()
Unlock the Mutex.
Definition: ThreadSupport.h:218
static regex_t _dateFormatPreg
Definition: CalFile.h:542
Definition: InvalidParameterException.h:35
Definition: ParseException.h:36
int _curlineLength
Definition: CalFile.h:502
CalFile * _include
Definition: CalFile.h:532
std::vector< std::string > _currentFields
Definition: CalFile.h:515