-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatafile.cpp
More file actions
419 lines (385 loc) · 8.67 KB
/
datafile.cpp
File metadata and controls
419 lines (385 loc) · 8.67 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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/***********************************************************************
* Source Code:
* Source code for datafile.cpp
* Author:
* Alexander Marvin
* Summary:
* Reads file in, using filename from user, and stores data in local
* memory. Data is read from the table.
************************************************************************/
#include <iostream>
#include <unistd.h>
#include <iomanip>
#include <fstream>
#include <vector>
#include <string>
#include <cstring>
#include "datafile.h"
using namespace std;
//typedef
typedef map<double,double>::iterator mIter;
datafile::datafile()
{
char cwd[255];
getcwd(cwd, 255); //get current working directory
directory = cwd; //place char array in string directory
altitude = 0;
frequency = 0.00001;
//cout << "I've been initialized, yay" << endl;
}
datafile::datafile(string s) : filename(s)
{
char cwd[255];
getcwd(cwd, 255);
directory = cwd;
altitude = 0;
frequency = 0.00001;
//cout << "I've been initialized with some value: " << filename << endl;
}
datafile::datafile(double alt)
{
char cwd[255];
getcwd(cwd, 255);
directory = cwd;
altitude = alt;
frequency = 0.00001;
}
datafile::datafile(string s, double alt) : filename(s)
{
char cwd[255];
getcwd(cwd, 255);
directory = cwd;
altitude = alt;
frequency = 0.00001;
}
datafile::datafile(string s, string t) : filename(s), directory(t)
{
altitude = 0;
frequency = 0.00001;
};
datafile::datafile(string s, string t, double alt) : filename(s), directory(t)
{
altitude = alt;
frequency = 0.00001;
};
string datafile::getFilename()
{
if (filename.empty())
{
cerr << "No filename yet.";
return filename;
}
else
return filename;
}
string datafile::getFullPath()
{
if (filename.empty())
throw("There is no filename");
return directory + "/" + filename;
}
void datafile::displayData()
{
map<double, double>::iterator it;
//display header
cout << setw(8) << "Altitude ";
cout << setw(8) << "Temperature ";
if (!pressure.empty())
cout << setw(8) << "Pressure ";
if (!humidity.empty())
cout << setw(8) << "Humidity";
cout << endl;
//loop through values
for (it = temperature.begin(); it != temperature.end(); it++)
{
double temp = it->first;
cout << setw(8) << it->first << " ";
cout << setw(8) << it->second << " ";
if (!pressure.empty())
cout << " " << setw(8) << pressure[temp];
if (!humidity.empty())
cout << " " << setw(8) << humidity[temp];
cout << endl;
}
}
void datafile::setDirectory(string d)
{
directory = d;
}
void datafile::setFilename(string f)
{
filename = f;
}
bool datafile::readFile(bool hasHead)
{
//check to see if has filename
if (filename.empty())
{
cerr << "There is no filename";
throw("No filename");
}
//initialize
string filepath;
double temp;
double temp2;
ifstream fin;
//open file
filepath = getFullPath();
fin.open(filepath.c_str());
//check to see if it opened
if (fin.fail())
{
cout << "File opening has failed\n";
return false;
}
//read file
//ignore first line if has header
if (hasHead)
{
fin.ignore(256,'\n');
}
//read in data
while (!fin.eof())
{
fin >> temp;
fin >> temp2;
pressure.insert(std::pair<double,double>(temp2, temp));
fin >> temp;
temperature.insert(std::pair<double,double>(temp2, temp));
fin >> temp;
humidity.insert(std::pair<double,double>(temp2, temp));
}
//close file
fin.close();
return true;
}
bool datafile::readCsvFile(bool hasHead )
{
//check to see if has filename
if (filename.empty())
{
cerr << "There is no filename";
throw("No filename");
}
//initialize
double temp;
double temp2;
string filepath;
ifstream fin;
//open file
filepath = getFullPath();
fin.open(filepath.c_str());
//check to see if it opened
if (fin.fail())
{
cout << "File opening has failed" << endl;
return false;
}
//read file
//ignore first line if has header
if (hasHead)
{
fin.ignore(256,'\n'); //ignore first line
}
//read in data
do
{
//read first item on line
fin >> temp;
//cout << "temp2: " << temp2 << endl;
fin.ignore(1,'\n'); //ignore newline character
//read second item on line
fin >> temp2;
//place in map
temperature.insert(std::pair<double,double>(temp,temp2));
fin.ignore(1,'\n'); //ignore comma
}
while(!fin.eof());
//close file
fin.close();
return true;
}
bool datafile::convertFile(const char filename[] )
{
cout << "Testing readFile\n";
//initialization
char temp = '\0';
char prev = '\0';
ifstream fin;
ofstream fout;
char filename2[255];
//now create file name for output file
strcpy(filename2, filename);
convertFileOutName(filename2);
//cout << "filename: " << filename << endl;
//cout << "filename2: " << filename2 << endl;
//open files
fin.open(filename);
if (fin.fail())
{
cout << "Unable to open your file" << endl;
return false;
}
fout.open(filename2);
if (fout.fail())
{
cout << "Operation has failed" << endl;
}
//read file
while (fin.get(temp))
{
if (temp == ',' || temp == '"')
{
fout << "\"";
fout << temp;
fout << "\"";
}
else
{
if (endWord(temp, prev, fin.peek()) && fin.tellg() > 1)
fout << ",";
fout << temp;
}
prev = temp;
}
//close file
cout << endl;
fin.close();
fout.close();
return true;
}
double datafile::getTemperature()
{//get value from map
if (temperature.empty())
{
cerr << "File is not read in!\n";
return -1;
}
//first create map iterator
map<double,double>::iterator it;
//get temperature
it = temperature.find(altitude);
if (it == temperature.end()) //if point not found, return -1
{
return interpolate(1);
}
else
{
return temperature[altitude]; //if found return value
}
}
double datafile::getPressure()
{ //retrieve value from map
if (pressure.empty())
{
cerr << "File is not read in!\n";
return -1;
}
//allocate iterator
map<double,double>::iterator it;
//get pressure
//cout << "getPressure running!\n";
it = pressure.find(altitude);
if (it == pressure.end()) //if point not found, return -1
return interpolate(2);
else
{
return pressure[altitude]; //if found return value
}
}
double datafile::getHumidity()
{ //retrieve value from map
if (humidity.empty())
{
cerr << "File is not read in!\n";
return -1;
}
//allocate iterator
map<double, double>::iterator it;
//get humidity
it = humidity.find(altitude);
if (it == humidity.end()) //if point not found, return -1
return interpolate(3);
else
{
return humidity[altitude];
}
}
double datafile::interpolate(int type)
{ //linear interpolation between two points
//cout << "Interpolate running!\n";
map<double, double> * mptr;
//set mptr to right map
switch(type)
{
case 1: //temperature
mptr = &temperature;
break;
case 2: //pressure
mptr = &pressure;
break;
case 3: //humidity
mptr = &humidity;
break;
}
//do boundary check
//need to be changed to deal with proper
//interpolation at boundaries.
map<double, double>::iterator it;
if (altitude < 0)
{
cerr << "Altitude out of range!\n" << endl;
it = mptr->begin();
return it->second;
}
it = mptr->end();
it--;
if (altitude > it->first)
{
return it->second;
}
//find upper and lower points to interpolate
mIter upper = mptr->upper_bound(altitude);
mIter lower = mptr->upper_bound(altitude);
lower--;
//calculate point
double xa = lower->first; //x of first point
double ya = lower->second; //y of first point
double xb = upper->first; //x of second point
double yb = upper->second; //y of second point
//linear interpolation formula from
//https://en.wikipedia.org/wiki/Linear_interpolation
return ya + (yb - ya)*(altitude - xa)/(xb - xa);
}
bool datafile::endWord(char presChar, char prevChar, char futChar)
{
if ( presChar == ' ' && prevChar != ' ' && prevChar != '\n')
return true;
else
return false;
}
void datafile::convertFileOutName(char str[])
{
int length = strlen(str);
int i;
for ( i = 0; i < length && str[i] != '.'; i++);
if (i <= 251 && i != length)
{
//cout << "Period is at " << i << endl;
//cout << "Length is: " << length << endl;
str[i] = '\0'; //erase ending
}
else if (i > 251)
{
str[251] = '\0';
}
else if (i <= 251)
{}
//who cares
else
{
cerr << "An error has occurred";
throw;
}
strcat(str, ".csv");
}