-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.c
More file actions
289 lines (263 loc) · 7.76 KB
/
analysis.c
File metadata and controls
289 lines (263 loc) · 7.76 KB
1
2
3
4
5
6
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "calculations.h"
//constants
const char* ROOT_FILE_NAME = "graph.C";
const char BEGINNING[];
const char END[];
const int NUM_RTDS = 4;
const int TEMP_MARKER_STYLES[4] = {20, 20, 20, 20};
const int TEMP_MARKER_COLORS[4] = {2, 3, 4, 28};
//Command line options
//Default behavior: display temperature only
// -v display voltages
// -t display temperatures
// -p display pressures
// -o display resistance (always calculated, never from file)
// -f display fraction of pressure/temperature
// -r recalculate all data from voltages (use this when a new equation has been implemented)
// Note: doing so does not alter the original file
//User must also specify a file with data
static int pID;
struct sample
{
char timestamp[10];
double rtd[4];
double rtd_v[4];
double rtd_r[4]; //resistance
double pressure;
double pressure_v;
};
int main(int argc, char** argv)
{
//file access
char file_name[100] = "";
for(int i=1; i<argc; ++i)
{
if(argv[i][0] != '-')
{
strcpy(file_name, argv[i]);
break;
}
}
if(strcmp(file_name, "")==0)
{
printf("Please name a file to display.\n");
return -1;
}
//flags
int c;
extern char *optarg;
extern int optind, optopt, opterr;
bool disp_temperature = false;
bool disp_pressure = false;
bool disp_voltage = false;
bool disp_fraction = false;
bool disp_resistance = false;
bool recalculate = false;
while ((c = getopt(argc, argv, ":otpvfr")) != -1) {
switch(c) {
case 't':
disp_temperature = true;
break;
case 'p':
disp_pressure = true;
break;
case 'v':
disp_voltage = true;
break;
case 'f':
disp_fraction = true;
break;
case 'r':
recalculate = true;
break;
case 'o':
disp_resistance = true;
break;
case '?':
printf("unknown arg %c\n", optopt);
break;
}
}
//Default no options setting
if(!disp_temperature && !disp_pressure && !disp_fraction && !disp_resistance)
disp_temperature = true;
FILE* f = fopen(file_name, "r");
if(f == NULL)
printf("Could not open file %s.\n", file_name);
//ignore first line
char mark = getc(f);
while(mark != '\n') {
mark = getc(f);
}
int samples_len = 10;
struct sample* samples;
samples = (struct sample*)malloc(sizeof(struct sample)*samples_len);
//read data
int n = 0; //number of datums so far in sample array
while(!feof(f))
{
//extend array size
if(n==samples_len)
{
struct sample* new_samples;
new_samples = (struct sample*)malloc(sizeof(struct sample)*samples_len*2);
for(int i=0; i<n; ++i)
new_samples[i] = samples[i];
free(samples);
samples = new_samples;
samples_len *= 2;
}
//append data
struct sample s;
fscanf(f,
"%s %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
s.timestamp,
s.rtd, s.rtd+1, s.rtd+2, s.rtd+3,
&s.pressure,
s.rtd_v, s.rtd_v+1, s.rtd_v+2, s.rtd_v+3,
&s.pressure_v);
for(int i=0; i<NUM_RTDS; ++i)
s.rtd_r[i] = resistance(s.rtd_v[i], i);
samples[n] = s;
++n;
}
fclose(f);
if(recalculate)
{
for(int i=0; i<n; ++i)
{
for(int j=0; j<NUM_RTDS; ++j)
{
samples[i].rtd[j] = temperature(samples[i].rtd_v[j], j);
}
samples[i].pressure = pressure(samples[i].pressure_v);
}
}
f = fopen(ROOT_FILE_NAME, "w");
if(f==NULL)
{
printf("Error opening file to write \"%s\"\n", ROOT_FILE_NAME);
return 1;
}
fprintf(f, BEGINNING);
//input arrays here
fprintf(f, "const Int_t n = %d;\n", n);
if(disp_temperature || disp_resistance)
{
for(int i=0; i<NUM_RTDS; ++i)
{
fprintf(f, "Double_t x%d[] = {", i);
for(int j=0; j<n; ++j)
{
//Make this better once you implement timestamps
fprintf(f, "%d", j);
if(j!=n-1)
fprintf(f, ",");
}
fprintf(f, "};\n");
fprintf(f, "Double_t y%d[] = {", i);
for(int j=0; j<n; ++j)
{
if(disp_voltage)
fprintf(f, "%f", samples[j].rtd_v[i]);
else if(disp_resistance)
fprintf(f, "%f", samples[j].rtd_r[i]);
else
fprintf(f, "%f", samples[j].rtd[i]);
if(j!=n-1)
fprintf(f, ",");
}
fprintf(f, "};\n");
fprintf(f, "TGraph *gr%d = new TGraph(n,x%d,y%d);\n", i, i, i);
fprintf(f, "gr%d->SetMarkerColor(%d);\n", i, TEMP_MARKER_COLORS[i]);
fprintf(f, "gr%d->SetMarkerStyle(%d);\n", i, TEMP_MARKER_STYLES[i]);
fprintf(f, "mg->Add(gr%d);\n", i);
}
}
if(disp_fraction)
{
for(int i=0; i<NUM_RTDS; ++i)
{
fprintf(f, "Double_t x%d[] = {", i);
for(int j=0; j<n; ++j)
{
//Make this better once you implement timestamps
fprintf(f, "%d", j);
if(j!=n-1)
fprintf(f, ",");
}
fprintf(f, "};\n");
fprintf(f, "Double_t y%d[] = {", i);
for(int j=0; j<n; ++j)
{
fprintf(f, "%f", samples[j].pressure / samples[j].rtd[i]);
if(j!=n-1)
fprintf(f, ",");
}
fprintf(f, "};\n");
fprintf(f, "TGraph *gr%d = new TGraph(n,x%d,y%d);\n", i, i, i);
fprintf(f, "gr%d->SetMarkerColor(%d);\n", i, TEMP_MARKER_COLORS[i]);
fprintf(f, "gr%d->SetMarkerStyle(%d);\n", i, TEMP_MARKER_STYLES[i]);
fprintf(f, "mg->Add(gr%d);\n", i);
}
}
if(disp_pressure)
{
fprintf(f, "Double_t xp[] = {");
for(int j=0; j<n; ++j)
{
//Make this better once you implement timestamps
fprintf(f, "%d", j);
if(j!=n-1)
fprintf(f, ",");
}
fprintf(f, "};\n");
fprintf(f, "Double_t yp[] = {");
for(int j=0; j<n; ++j)
{
if(disp_voltage)
fprintf(f, "%f", samples[j].pressure_v);
else
fprintf(f, "%f", samples[j].pressure);
if(j!=n-1)
fprintf(f, ",");
}
fprintf(f, "};\n");
fprintf(f, "TGraph *grp = new TGraph(n,xp,yp);\n");
fprintf(f, "grp->SetMarkerColor(%d);\n", 6);
fprintf(f, "grp->SetMarkerStyle(%d);\n", 20);
fprintf(f, "mg->Add(grp);\n");
}
fprintf(f, END);
fclose(f);
char root_command[30] = "root -l ";
strcat(root_command, ROOT_FILE_NAME);
strcat(root_command, " &> /dev/null");
//system(root_command);
//signal(SIGINT, int_handler);
pID = fork();
if(pID==0)
{
printf("Initializing root. This may take a moment.\n");
system(root_command);
printf("Root terminated.\n");
exit(0);
}
return 0;
}
const char BEGINNING[] =
"#include<unistd.h>\n"
"void graph() {\n"
"TCanvas *c1 = new TCanvas(\"c1\", \"Graph title\", 10, 40, 700, 450);\n"
"c1->SetGrid();\n"
"TMultiGraph *mg = new TMultiGraph();\n";
const char END[] =
"mg->Draw(\"ALP\");\n"
"c1->Update();\n"
"}\n";