4 * Created on: Dec 7, 2010
26 template <class NumberDataType>
27 bool string_to_number(NumberDataType& result,
28 const string& string_param,
29 std::ios_base& (*format)(std::ios_base&))
31 std::istringstream iss(string_param);
32 return !(iss >> format >> result).fail();
35 Data::Data(int size) {
36 value_arr.resize(size);
39 Data::Data(int size, double default_value, string unit_param) {
42 value_arr.resize(size);
43 for (ii = 0; ii < size; ii++) {
44 value_arr[ii] = default_value;
49 Data::Data(vector<double> *vect_param) {
53 sz = vect_param->size();
54 //log_debug("Data(), value count: %d\n", size);
56 for (ii = 0; ii < sz; ii++) {
57 value_arr[ii] = vect_param->at(ii);
58 //log_debug("Data(), value[%d]: %f\n", ii, value_arr[ii]);
62 Data::Data(std::vector<double> *vector_param, string unit_param) {
66 sz = vector_param->size();
67 //log_debug("Data(), value count: %d\n", size);
69 for (ii = 0; ii < sz; ii++) {
70 value_arr[ii] = vector_param->at(ii);
71 //log_debug("Data(), value[%d]: %f\n", ii, value_arr[ii]);
76 Data::Data(vector<double> *vector_param,
82 size = vector_param->size();
83 //log_debug("Data(), value count: %d\n", size);
84 value_arr.resize(size);
85 for (ii = 0; ii < size; ii++) {
86 value_arr[ii] = vector_param->at(ii);
87 //log_debug("Data(), value[%d]: %f\n", ii, value_arr[ii]);
89 date_time.copy(date_param);
93 Data::Data(std::valarray<double> *val_arr_param, Date *date_param) {
97 sz = val_arr_param->size();
99 for (ii = 0; ii < sz; ii++) {
100 value_arr[ii] = (*val_arr_param)[ii];
102 date_time.copy(date_param);
108 Data *Data::clone() {
111 ret_val = new Data(&value_arr, &date_time);
115 void Data::printout() {
116 log_debug(" data: %s\n", to_string().c_str());
119 plp::Date Data::get_date() {
123 void Data::set_date(Date *date_param) {
124 date_time.copy(date_param);
127 string Data::get_unit() {
131 Data *Data::parse_string(const string& dataline) {
132 stringstream ss(dataline);
145 //log_debug("parse_string: %s\n", dataline.c_str());
146 while(getline(ss, item, '|')) {
149 date = W1Util::parse_date_str(item);
152 suc_flg = string_to_number<double>(val, item, dec);
154 //log_debug("adding number: %f\n", val);
160 ii = item.find_last_of(" ");
163 ((ii + 1) <= (sz - 1))) {
164 unit = item.substr(ii + 1);
170 ret_val = new Data(&v, &date, unit);
175 string Data::to_string(int dec_precision) {
180 ret_val = date_time.to_string();
181 if (value_arr.size() > 0) {
182 for (ii = 0; ii < value_arr.size(); ii++) {
183 out << "|" << fixed << setprecision(dec_precision) << value_arr[ii];
185 ret_val.append(out.str());
186 if (unit.empty() == false) {
188 ret_val.append(unit.c_str());
194 string Data::to_string() {
198 DataRange::DataRange() {
205 DataRange::DataRange(Data *data) {
209 column_count = data->value_arr.size();
210 unit = data->get_unit();
219 DataRange::DataRange(DataRange *datarange) {
224 if (datarange != NULL) {
229 DataRange::DataRange(int value_count_per_data_item, string unit_param) {
231 column_count = value_count_per_data_item;
236 DataRange::~DataRange() {
240 if (val_matrix != NULL) {
244 for (ii = 0; ii < date_list.size(); ii++) {
245 date = date_list.at(ii);
250 void DataRange::add(Data *data) {
257 if ((val_matrix == NULL) &&
258 (column_count == 0) &&
259 (unit.empty() == true)) {
260 column_count = data->value_arr.size();
261 unit = data->get_unit();
263 cnt = (row_count + 1) * column_count;
264 val_matrix = (double *)realloc(val_matrix, cnt * sizeof(double));
265 indx = row_count * column_count;
266 cnt = data->value_arr.size();
267 if (cnt != column_count) {
268 log_error("Error when adding data... Invalid data item count!\n");
269 if (cnt > column_count) {
273 for (ii = cnt; ii < column_count; ii++) {
274 val_matrix[indx + ii] = 0;
278 for (ii = 0; ii < cnt; ii++) {
279 val_matrix[indx + ii] = data->value_arr[ii];
282 for (int ii = 0; ii < ((row_count + 1) * column_count); ii++) {
283 log_debug("data_matrix[%d] = %f\n", ii, val_matrix[ii]);
286 date = data->get_date().clone();
287 date_list.push_back(date);
292 void DataRange::add(DataRange *datarange) {
297 if (datarange != NULL) {
298 cnt = datarange->get_count();
299 for (ii = 0; ii < cnt; ii++) {
300 dta = datarange->get(ii);
307 Data *DataRange::get(int row_index) {
316 if ((row_index >= 0) &&
317 (row_index < row_count)) {
318 start_indx = row_index * column_count;
319 for (ii = 0; ii < column_count; ii++) {
320 //val = val_matrix[0];
321 val = val_matrix[start_indx + ii];
322 //log_debug("val[%d] = %f\n", (start_indx + ii), val);
325 date = date_list.at(row_index);
326 ret_val = new Data(&vect, date, unit);
331 int DataRange::get_count() {
335 int DataRange::get_data_item_value_count() {
339 Data *DataRange::get_first() {
349 Data *DataRange::get_last() {
350 return get(row_count - 1);
353 void DataRange::printout() {
357 log_debug("---- DataRange, number of data items: %d\n ----", row_count);
358 for (ii = 0; ii < row_count; ii++) {
365 log_debug("---- DataRange printout done ----\n");