Obs_Calibration_Vector.cpp 8.99 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
144
    } else {
        return(-1);
Achim Morschhauser's avatar
Achim Morschhauser committed
145
146
    }
    
Achim Morschhauser's avatar
Achim Morschhauser committed
147
    return(0);
Achim Morschhauser's avatar
Achim Morschhauser committed
148
149
150
151
152
153
154
155

}

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

Achim Morschhauser's avatar
Achim Morschhauser committed
159
160
161
162
163
    struct adc_cal cal;
    
    cal.offset = offset;
    cal.scale  = scale;
    
Achim Morschhauser's avatar
Achim Morschhauser committed
164
165
166
167
168
169
170
    // 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
171
172

    return(0);
Achim Morschhauser's avatar
Achim Morschhauser committed
173
174

}
Achim Morschhauser's avatar
Achim Morschhauser committed
175
176
177
178
179
180
181
182
183

/****************************************************************************
 *
 * 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
184
      * stored. Cannot simply append, as duplicates need to be treated.
Achim Morschhauser's avatar
Achim Morschhauser committed
185
186
187
188
      * SOLUTION: Save whole file in string before writing. Then, rewrite
      * file. Comment out duplicates instead of overwriting.
      */
     
Achim Morschhauser's avatar
Achim Morschhauser committed
189
190
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
    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
235
236
     return(0);
 }
Achim Morschhauser's avatar
Achim Morschhauser committed
237
238
239
240
241
242
243
244
 
 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
245
246
247
248
249
250
251
252
253
254
255
        
/****************************************************************************
 *
 * 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
256
257
    std::string     config; // configuration string used as key
    struct adc_cal  cal;    // calibration constants used as value
Achim Morschhauser's avatar
Achim Morschhauser committed
258
259
260
261
262
263
264
    
    // 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
265
266
        // Parse from line
        parse_adc_calibration(line,config,cal);
Achim Morschhauser's avatar
Achim Morschhauser committed
267
268
269
270
271
272
273
274
275
276
277
278
        // Save key-value pair
        map_ADC[config]=cal;
      }
      file.close();
    } else {
      std::cout << "Unable to open file: " << filename << std::endl;
    }


    return 0;
     
 }
Achim Morschhauser's avatar
Achim Morschhauser committed
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
 
 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;
     
    // Find comment token ("#")
    pos_c=line.find('#');
    comment=line.substr(pos_c);
    // Proceed with uncommented part
    line=line.substr(0,pos_c);
    if (line.size()==0) return(-1);
    // 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);
    std::cout << "Read ADC: " << config << " " << cal.offset << 
            " " << cal.scale << std::endl;
     
     return(0);
     
 }

  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));
      
  }