DATRW++ library: seismic data I/O with multiple formats
thiesdl1line.cc
Go to the documentation of this file.
1 /*! \file thiesdl1line.cc
2  * \brief handle a ThiesDL1 data line (implementation)
3  *
4  * ----------------------------------------------------------------------------
5  *
6  * \author Thomas Forbriger
7  * \date 13/09/2011
8  *
9  * handle a ThiesDL1 data line (implementation)
10  *
11  * Copyright (c) 2011 by Thomas Forbriger (BFO Schiltach)
12  *
13  * ----
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27  * ----
28  *
29  * REVISIONS and CHANGES
30  * - 13/09/2011 V1.0 Thomas Forbriger
31  * - 25/10/2012 V1.1 resolved rounding error when deriving counts from
32  * sample values
33  *
34  * ============================================================================
35  */
36 #define DATRW_THIESDL1LINE_CC_VERSION \
37  "DATRW_THIESDL1LINE_CC V1.1"
38 
39 #include <sstream>
40 #include <cmath>
41 #include <datrwxx/thiesdl1line.h>
42 
43 namespace datrw {
44 
45  namespace thiesdl1 {
46 
47  namespace helper {
48 
49  const libtime::TRelativeTime chalfminute(0,0,0,30);
50  const libtime::TRelativeTime coneday(1);
51 
52  } // namespace helper
53 
55  this->Exception::report();
56  this->my_report();
57  } // void ExceptionInconsistentLine::report() const
58 
59  /*----------------------------------------------------------------------*/
60 
62  std::cerr << " DL1 data line: " << this->Mdataline;
63  } // void ExceptionInconsistentLine::my_report() const
64 
65  /*======================================================================*/
66 
67  const double DataLine::gain=0.1;
68 
69  /*----------------------------------------------------------------------*/
70 
71  DataLine::DataLine(const std::string& line)
72  {
73  Mline=line;
74  this->evalline();
75  } // DataLine::DataLine(const std::string& line)
76 
77  /*----------------------------------------------------------------------*/
78 
79 #define DL1_dlassert( C, M, L ) \
80  if (!(C)) { throw( ExceptionInconsistentLine( M , __FILE__, \
81  __LINE__, #C, L )); }
82 
83  typedef unsigned int Tuint;
84 
85  // example data line:
86  // 00.017 0.0 01.12.08 00:01
87  // 0000000000111111111122222222223
88  // 0123456789012345678901234567890
90  {
91  DL1_dlassert(Mline.length()==27, "unexpected length of data line", Mline);
92  // check for expected characters
93  const char cmismatch[]="character mismatch";
94  DL1_dlassert(Mline.substr(2,1)==".", cmismatch, Mline);
95  DL1_dlassert(Mline.substr(10,1)==".", cmismatch, Mline);
96  DL1_dlassert(Mline.substr(15,1)==".", cmismatch, Mline);
97  DL1_dlassert(Mline.substr(18,1)==".", cmismatch, Mline);
98  DL1_dlassert(Mline.substr(24,1)==":", cmismatch, Mline);
99  DL1_dlassert(Mline.substr(6,1)==" ", cmismatch, Mline);
100  DL1_dlassert(Mline.substr(12,1)==" ", cmismatch, Mline);
101  DL1_dlassert(Mline.substr(21,1)==" ", cmismatch, Mline);
102  // extract time and date
103  int year, month, day, hour, minute;
104  std::istringstream iss;
105  iss.clear();
106  iss.str(Mline.substr(13,2));
107  iss >> day;
108  iss.clear();
109  iss.str(Mline.substr(16,2));
110  iss >> month;
111  iss.clear();
112  iss.str(Mline.substr(19,2));
113  iss >> year;
114  iss.clear();
115  iss.str(Mline.substr(22,2));
116  iss >> hour;
117  iss.clear();
118  iss.str(Mline.substr(25,2));
119  iss >> minute;
120  if (year < 100) { year += 1900; }
121  if (year < 1970) { year += 100; }
122  libtime::TAbsoluteTime intime(year,month,day,hour,minute);
123  // check time and date
124  if (hour!=24)
125  {
126  DL1_dlassert(((intime.year()==year)
127  && (intime.month()==month)
128  && (intime.day()==day)
129  && (intime.hour()==hour)
130  && (intime.minute()==minute)),
131  "invalid date value", Mline);
132  }
133  else
134  {
135  DL1_dlassert((minute==0),
136  "invalid time value (expected: 24:00UT)", Mline);
137  libtime::TAbsoluteTime thedayonly(intime-helper::coneday);
138  DL1_dlassert(((thedayonly.year()==year)
139  && (thedayonly.month()==month)
140  && (thedayonly.day()==day)),
141  "invalid date value", Mline);
142  }
143  Mtime=intime-helper::chalfminute;
144  // check float value for time
145  double ftime=double(hour)+int(0.5+1000.*double(minute)/60.)/1000.;
146  double rftime;
147  iss.clear();
148  iss.str(Mline.substr(0,6));
149  iss >> rftime;
150  double diff=ftime-rftime;
151  if (diff<0) { diff=-diff; }
152  if (!(diff < 0.002))
153  {
154  /*
155  Logger(log_err) << "ftime: " << ftime
156  << " rftime: " << rftime
157  << " diff: " << diff;
158  */
159  }
160  DL1_dlassert(diff < 0.002,
161  "inconsistent floating point time values", Mline);
162  // read data value
163  iss.clear();
164  iss.str(Mline.substr(7,6));
165  iss >> this->Mvalue;
166  this->Mcounts=Tuint(nearbyint(Mvalue/DataLine::gain));
167  } // DataLine::evalline()
168 
169  } // namespace thiesdl1
170 
171 } // namespace datrw
172 
173 /* ----- END OF thiesdl1line.cc ----- */
unsigned int Mcounts
value of sample in counts
Definition: thiesdl1line.h:98
std::string Mline
literal data line as received from DL1
Definition: thiesdl1line.h:92
unsigned int Tuint
Definition: thiesdl1line.cc:83
static const double gain
gain value in mm/count
Definition: thiesdl1line.h:84
std::string line() const
return data line
Definition: thiesdl1line.h:75
#define DL1_dlassert(C, M, L)
Definition: thiesdl1line.cc:79
libtime::TAbsoluteTime Mtime
date and time of this sample
Definition: thiesdl1line.h:94
Root namespace of library.
Definition: aalibdatrwxx.cc:16
const libtime::TRelativeTime coneday(1)
double Mvalue
value of sample in mm precipitation
Definition: thiesdl1line.h:96
const libtime::TRelativeTime chalfminute(0, 0, 0, 30)
virtual void report() const
Screen report.
Definition: exception.cc:96
DataLine(const std::string &line)
swallow data line
Definition: thiesdl1line.cc:71
virtual void report() const
Screen report.
Definition: thiesdl1line.cc:54