Obs_Calibration_Vector.cpp 9.35 KB
Newer Older
Achim Morschhauser's avatar
Achim Morschhauser committed
1
2
3
4
5
6
// C++ headers
#include <string>           // String
#include <iostream>         // String streams
#include <fstream>          // File operations
#include <unordered_map>

Achim Morschhauser's avatar
Achim Morschhauser committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <Obs_Calibration_Vector.hpp>
#include <stdio.h>

Obs_Calibration_Vector::Obs_Calibration_Vector(double offset, double scale){

	for (int i=0;i<3;i++){
		this->offset[i]=0.0;
		this->scale[i]=1.0;
	}

	this->offset[0] = offset;
	this->scale[0]  = scale;

	if (offset==0 && scale==1){
		do_cal=0;
	} else {
		do_cal=1;
	}

}

Obs_Calibration_Vector::Obs_Calibration_Vector(double* offset, double* scale){

	for (int i=0;i<3;i++){
		this->offset[i]=offset[i];
		this->scale[i]=scale[i];
		if (do_cal==0 && (offset[i]!=0 || scale[i]!=1)){
			do_cal=1;
		}
	}

}

Obs_Calibration_Vector::Obs_Calibration_Vector(
		double scale_X, double scale_Y, double scale_Z,
        double offset_X, double offset_Y, double offset_Z){

	do_cal=0;

	this->scale[0]=scale_X;
	this->scale[1]=scale_Y;
	this->scale[2]=scale_Z;
	this->offset[0]=offset_X;
	this->offset[1]=offset_Y;
	this->offset[2]=offset_Z;

	for (int i=0; i<3; i++){
		if (do_cal==0 && (offset[i]!=0 || scale[i]!=1)){
			do_cal=1;
			break;
		}
	}

}

/****************************************************************************
 ****************************************************************************
 *																			*
 * 				PUBLIC METHODS												*
 *																			*
 ****************************************************************************
 ***************************************************************************/

/****************************************************************************
 *                                                                                                                                                      *
 * Calibrate vector data                                                                                                        *
 *                                                                                                                                                      *
 ***************************************************************************/
int Obs_Calibration_Vector::calibrate(data_obs_vector* vdata){

        if (do_cal){

                double data[5];

                vdata->get_data(data);

                for (int i=0;i<3;i++){
                        //printf("SCALE: %f ",data[i]);
                        data[i]=data[i]*scale[i]+offset[i];
                        //printf("%f %f\n",data[i],scale[i]);
                }

                vdata->set_data(data);

        }

        return(0);



}


/****************************************************************************
 *                                                                                                                                                      *
 * Calibrate vector data                                                                                                        *
 *                                                                                                                                                      *
 ***************************************************************************/
int Obs_Calibration_Vector::calibrate(data_obs_scalar* sdata){

        if (do_cal){

                double data;

                data=sdata->get_value();

                data=data*scale[0]+offset[0];

                sdata->set_value(data);

        }

        return(0);

}




/****************************************************************************
 *																		 	*
 * Calibration constants for the ADC													*
 *																			*
 ***************************************************************************/
Achim Morschhauser's avatar
Achim Morschhauser committed
131
132
133
134
135
136
137
138
139
140
141
142
int Obs_Calibration_Vector::get_adc_calibrate(std::string config,
        std::string &offset, std::string &scale){

    std::unordered_map<std::string, struct adc_cal>::iterator it;
    struct adc_cal cal;
    
    it = map_ADC.find(config);
    
    if (it != map_ADC.end()) {
        cal     = it->second;
        scale   = cal.scale;
        offset  = cal.offset;
Achim Morschhauser's avatar
Achim Morschhauser committed
143
    } else {
144
145
        scale  = "";
        offset = "";
Achim Morschhauser's avatar
Achim Morschhauser committed
146
        return(-1);
Achim Morschhauser's avatar
Achim Morschhauser committed
147
148
    }
    
Achim Morschhauser's avatar
Achim Morschhauser committed
149
    return(0);
Achim Morschhauser's avatar
Achim Morschhauser committed
150
151
152
153
154
155
156
157

}

/****************************************************************************
 *																		 	*
 * Calibration constants for the ADC													*
 *																			*
 ***************************************************************************/
Achim Morschhauser's avatar
Achim Morschhauser committed
158
159
int Obs_Calibration_Vector::add_adc_calibrate(std::string config, 
        std::string offset, std::string scale){
Achim Morschhauser's avatar
Achim Morschhauser committed
160

Achim Morschhauser's avatar
Achim Morschhauser committed
161
162
163
164
165
    struct adc_cal cal;
    
    cal.offset = offset;
    cal.scale  = scale;
    
Achim Morschhauser's avatar
Achim Morschhauser committed
166
167
168
169
170
171
172
    // Check if already exists
    
    if (map_ADC.count(config)>0) {
        map_ADC[config]=cal;
    } else {
        std::cerr << "Calibration for " << config << " overwritten.";
    }
Achim Morschhauser's avatar
Achim Morschhauser committed
173
174

    return(0);
Achim Morschhauser's avatar
Achim Morschhauser committed
175
176

}
Achim Morschhauser's avatar
Achim Morschhauser committed
177
178
179
180
181
182
183
184
185

/****************************************************************************
 *
 * Save the calibration constants to file
 *
 ***************************************************************************/
 int Obs_Calibration_Vector::save_adc_calibrate(std::string filename) {
     
     /* PROBLEM: Save the comments in ADC file when new configurations are
Achim Morschhauser's avatar
Achim Morschhauser committed
186
      * stored. Cannot simply append, as duplicates need to be treated.
Achim Morschhauser's avatar
Achim Morschhauser committed
187
188
189
190
      * SOLUTION: Save whole file in string before writing. Then, rewrite
      * file. Comment out duplicates instead of overwriting.
      */
     
Achim Morschhauser's avatar
Achim Morschhauser committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
    std::ifstream  file_buf(filename);
    std::ofstream  file_out;
    std::string    line, config, comment;
    
    size_t pos_c;
    
    struct adc_cal cal;
     
    // Copy the hash map
    // The copy will be used to track what has been written to file
    std::unordered_map<std::string, struct adc_cal> map_ADC_cpy(map_ADC);
     
    // Open file as new blank file
    file_out.open(filename,std::ios::out | std::ios::trunc);
    
    // Read from buffered file
      while ( getline (file_buf,line) )  {
          
        // Parse from line
          
        if (parse_adc_calibration(line,config,cal,comment) < 0){
        
            file_out << line << std::endl;
        
        } else {
            
            // Configuration exists and is overwritten
            if (map_ADC_cpy.count(config)>0) {
                file_out << "#" << line << std::endl;
                cal=map_ADC_cpy[config];
                file_out << config << "," << cal.offset << "," << 
                            cal.scale << ";" << comment << std::endl;

            // Configuration doesn't exist
            } else {
                file_out << line << std::endl;
            }
        
        }
        
      }
    
     file_out.close();
    
     
     
Achim Morschhauser's avatar
Achim Morschhauser committed
237
238
     return(0);
 }
Achim Morschhauser's avatar
Achim Morschhauser committed
239
240
241
242
243
244
245
246
 
 int Obs_Calibration_Vector::save_adc_calibrate() {
 
     if (file_ADC.empty()) return(-1);
     
     return(save_adc_calibrate(file_ADC));
     
 }
Achim Morschhauser's avatar
Achim Morschhauser committed
247
248
249
250
251
252
253
254
255
256
257
        
/****************************************************************************
 *
 * Read the calibration constants to file
 *
 ***************************************************************************/
 int Obs_Calibration_Vector::read_adc_calibrate(std::string filename) {
     
    std::ifstream  file;
    std::string    line;
    
Achim Morschhauser's avatar
Achim Morschhauser committed
258
259
    std::string     config; // configuration string used as key
    struct adc_cal  cal;    // calibration constants used as value
Achim Morschhauser's avatar
Achim Morschhauser committed
260
261
262
263
264
265
266
    
    // Open file
    file.open(filename,std::ios::out);
    
    // Read from file
    if (file.is_open()) {
      while ( getline (file,line) )  {
Achim Morschhauser's avatar
Achim Morschhauser committed
267
        // Parse from line
Achim Morschhauser's avatar
Achim Morschhauser committed
268
        if (parse_adc_calibration(line,config,cal)==0) {
269
270
            // Save key-value pair
            map_ADC[config]=cal;
Achim Morschhauser's avatar
Achim Morschhauser committed
271
272
273
            std::cout << "Read ADC: " << config << " " << cal.offset << 
            " " << cal.scale << std::endl;
        }
Achim Morschhauser's avatar
Achim Morschhauser committed
274
275
276
277
278
279
280
281
282
283
      }
      file.close();
    } else {
      std::cout << "Unable to open file: " << filename << std::endl;
    }


    return 0;
     
 }
Achim Morschhauser's avatar
Achim Morschhauser committed
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
 
 int Obs_Calibration_Vector::read_adc_calibrate() {
     
     if (file_ADC.empty()) return(-1);
     
     return(read_adc_calibrate(file_ADC));
     
 }
 
 
 /****************************************************************************
 *
 * Parse an ADC calibration line
 *
 ***************************************************************************/
 int Obs_Calibration_Vector::parse_adc_calibration(std::string line, 
         std::string& config, struct adc_cal& cal, std::string& comment) {
         
    size_t pos_c;
303
304
    
    comment="";
Achim Morschhauser's avatar
Achim Morschhauser committed
305
306
     
    // Find comment token ("#")
307
    if (line.length()<=0) return(-1);
Achim Morschhauser's avatar
Achim Morschhauser committed
308
    pos_c=line.find('#');
309
310
311
312
    if (pos_c < std::string::npos){
        comment=line.substr(pos_c);
        line=line.substr(0,pos_c);
    }
Achim Morschhauser's avatar
Achim Morschhauser committed
313
    if (line.size()==0) return(-1);
314
    std::cerr << "PARSE: " << line << std::endl;
Achim Morschhauser's avatar
Achim Morschhauser committed
315
316
317
318
319
320
321
322
323
324
    // Find configuration string
    pos_c=line.find(',');
    config=line.substr(0,pos_c);
    // Find calibration string
    line=line.substr(pos_c+1);
    pos_c=line.find(',');
    cal.offset = line.substr(0,pos_c);
    line=line.substr(pos_c+1);
    pos_c=line.find(';');
    cal.scale  = line.substr(0,pos_c);
Achim Morschhauser's avatar
Achim Morschhauser committed
325
    return(0);
Achim Morschhauser's avatar
Achim Morschhauser committed
326
327
328
329
330
331
332
333
334
 }

  int Obs_Calibration_Vector::parse_adc_calibration(std::string line, 
         std::string& config, struct adc_cal& cal) {
      
      std::string comment;
      
      return(parse_adc_calibration(line,config,cal,comment));
      
Achim Morschhauser's avatar
Achim Morschhauser committed
335
336
337
338
339
340
341
342
343
344
  }
  
  int Obs_Calibration_Vector::set_adc_calibrate(std::string filename){
      
      file_ADC=filename;
      
      read_adc_calibrate(filename);
      
      return(0);
      
Achim Morschhauser's avatar
Achim Morschhauser committed
345
  }