5 #include <boost/date_time/gregorian/gregorian_types.hpp>
6 #include <boost/date_time/posix_time/posix_time.hpp>
7 #include <boost/cast.hpp>
15 MtuFormat::MtuFormat()
19 MtuFormat::~MtuFormat()
25 unsigned char byte1, byte2, byte3;
31 datavalue = ByteToInt(byte1, byte2, byte3);
32 CurrChannel.
GetData().push_back(datavalue);
35 int MtuFormat::ByteToInt(
unsigned char first,
unsigned char second,
38 const char highbit = 0x80;
45 value = -(last * 65536 + second * 256 + first + 1);
48 value = last * 65536 + second * 256 + first;
52 void MtuFormat::ReadRecord(ifstream &infile)
54 unsigned char CurrentTag[
tagsize];
60 infile.read((
char *) CurrentTag,
tagsize);
63 startsec = CurrentTag[0];
64 startmin = CurrentTag[1];
65 starthr = CurrentTag[2];
66 startday = CurrentTag[3];
67 startmonth = CurrentTag[4];
68 startyear = CurrentTag[5];
69 startdow = CurrentTag[6];
70 startcentury = CurrentTag[7];
71 serialnumber = CurrentTag[9] * 256 + CurrentTag[8];
72 nscans = CurrentTag[11] * 256 + CurrentTag[10];
73 nchannels = CurrentTag[12];
74 taglength = CurrentTag[13];
75 status = CurrentTag[14];
76 saturationflags = CurrentTag[15];
77 futurereserved = CurrentTag[16];
78 samplelength = CurrentTag[17];
79 sampledenom = boost::numeric_cast<
double>(CurrentTag[19] * 256
81 sampleunit = CurrentTag[20];
82 clockstatus = CurrentTag[21];
83 clockerror = 16777216 * CurrentTag[25] + 65536 * CurrentTag[24]
84 + 256 * CurrentTag[23] + CurrentTag[22];
85 for (
int i = 0; i < 6; ++i)
86 reserved[i] = CurrentTag[i + 26];
99 sampleenum = 3600.0 * 24.0;
102 const double samplerate = sampledenom / sampleenum;
103 Hx.SetSamplerate(samplerate);
104 Hy.SetSamplerate(samplerate);
105 Hz.SetSamplerate(samplerate);
106 Ex.SetSamplerate(samplerate);
107 Ey.SetSamplerate(samplerate);
108 recordlength = nscans * nchannels;
110 TimeSeries::ttime basetime(boost::gregorian::date(startcentury
111 * 100 + startyear, startmonth, startday),
112 boost::posix_time::time_duration(starthr, startmin, startsec));
114 buffer =
new char[recordlength * samplelength];
115 infile.read(buffer, recordlength * samplelength);
116 currentbyte = buffer;
117 for (
int i = 0; i < nscans; ++i)
119 t.push_back(basetime + boost::posix_time::microseconds(
120 boost::numeric_cast<unsigned int>(i * 1000000.0
122 ReadValue(Ex, currentbyte);
123 currentbyte += samplelength;
124 ReadValue(Ey, currentbyte);
125 currentbyte += samplelength;
126 ReadValue(Hx, currentbyte);
127 currentbyte += samplelength;
128 ReadValue(Hy, currentbyte);
129 currentbyte += samplelength;
130 ReadValue(Hz, currentbyte);
131 currentbyte += samplelength;
137 void MtuFormat::GetData(
const std::string filename)
139 ifstream infile(filename.c_str(), ios::binary);
142 while (infile.good())
153 void MtuFormat::GetData()
158 void MtuFormat::MakeGood()
164 void MtuFormat::WriteNum(
double number, ofstream &outfile)
167 unsigned char middle;
170 unsigned int intnumber;
172 intnumber = int(abs(floor(number)));
174 intnumber = ~int(abs(floor(number))) + 1;
175 high = intnumber / 65536;
176 middle = (intnumber % 65536) / 256;
177 low = intnumber % 256;
182 outfile.write(buffer, 3);
185 void MtuFormat::UpDateHeader(
unsigned char *&header,
const int firstscan)
187 tm currtime = boost::posix_time::to_tm(t.at(firstscan));
188 header[0] = boost::numeric_cast<
char>(currtime.tm_sec);
189 header[1] = boost::numeric_cast<
char>(currtime.tm_min);
190 header[2] = boost::numeric_cast<
char>(currtime.tm_hour);
191 boost::gregorian::greg_year_month_day currdate =
192 t.at(firstscan).date().year_month_day();
193 header[3] = boost::numeric_cast<
char>(currdate.day.as_number());
194 header[4] = boost::numeric_cast<
char>(currdate.month.as_number());
195 header[5] = boost::numeric_cast<
char>(currdate.year % 100);
196 header[6] = boost::numeric_cast<
char>(currtime.tm_wday);
197 header[7] = boost::numeric_cast<
char>(currdate.year / 100);
200 void MtuFormat::WriteData(
const std::string filename)
205 unsigned char *header =
new unsigned char[
tagsize];
207 header[8] = serialnumber % 256;
208 header[9] = serialnumber / 256;
209 header[10] = nscans % 256;
210 header[11] = nscans / 256;
211 header[12] = nchannels;
212 header[13] = taglength;
214 header[15] = saturationflags;
215 header[16] = futurereserved;
216 header[17] = samplelength;
217 header[18] = boost::numeric_cast<
unsigned int>(sampledenom) % 256;
218 header[19] = boost::numeric_cast<
unsigned int>(sampledenom) / 256;
219 header[20] = sampleunit;
220 header[21] = clockstatus;
221 header[22] = clockerror % 256;
222 header[23] = (clockerror % 65536) / 256;
223 header[24] = (clockerror % 16777216) / 65536;
224 header[25] = clockerror / 16777216;
225 for (
int i = 0; i < 6; ++i)
226 header[i + 26] = reserved[i];
227 nrecords = Size() / nscans;
229 ofstream outfile(filename.c_str());
230 for (
int i = 0; i < nrecords; ++i)
232 UpDateHeader(header, firstscan);
233 outfile.write((
char *) header,
tagsize);
234 for (
int j = 0; j < nscans; ++j)
237 WriteNum(Ex.GetData().at(firstscan + j), outfile);
238 WriteNum(Ey.GetData().at(firstscan + j), outfile);
239 WriteNum(Hx.GetData().at(firstscan + j), outfile);
240 WriteNum(Hy.GetData().at(firstscan + j), outfile);
241 WriteNum(Hz.GetData().at(firstscan + j), outfile);
252 this->TimeSeries::operator=(source);
259 this->TimeSeries::operator=(source);
std::vector< double > & GetData()
Access for data vector, for ease of use and efficiency we return a reference.
This class is the base class for all classes dealing with MT time series.
TimeSeriesComponent is the base storage class for all types of time series data.
The basic exception class for all errors that arise in gplib.