|
Last change
on this file since 35bab2 was 35bab2, checked in by Frederik Heber <heber@…>, 16 years ago |
|
rewritten statistical tool average.
- average has been very specific to pcp (included ecut parameter).
- is now general: parses a column, may skip some initial rows, and evaluates average and mean deviation.
Signed-off-by: Frederik Heber <heber@…>
|
-
Property mode
set to
100644
|
|
File size:
1.9 KB
|
| Line | |
|---|
| 1 | using namespace std;
|
|---|
| 2 | #include <iostream>
|
|---|
| 3 | #include <iomanip>
|
|---|
| 4 | #include <fstream>
|
|---|
| 5 | #include <sstream>
|
|---|
| 6 | #include <math.h>
|
|---|
| 7 | #include <string>
|
|---|
| 8 |
|
|---|
| 9 | #include "version.h"
|
|---|
| 10 |
|
|---|
| 11 | int main(int argc, char **argv) {
|
|---|
| 12 |
|
|---|
| 13 | double avg, dev;
|
|---|
| 14 | double tmp;
|
|---|
| 15 | int zaehler;
|
|---|
| 16 | int col;
|
|---|
| 17 | int flag;
|
|---|
| 18 | int skiplines=0;
|
|---|
| 19 | string zeile;
|
|---|
| 20 | stringstream line;
|
|---|
| 21 |
|
|---|
| 22 | cout << ESPACKVersion << endl;
|
|---|
| 23 |
|
|---|
| 24 | if (argc <= 2) {
|
|---|
| 25 | cout << "Usage: " << argv[0] << " <column (zero-based)> <data file> [#skip initial lines]\n";
|
|---|
| 26 | return(1);
|
|---|
| 27 | }
|
|---|
| 28 | {
|
|---|
| 29 | line.str(argv[1]);
|
|---|
| 30 | line >> col;
|
|---|
| 31 | cout << "Using column " << col << "." << endl;
|
|---|
| 32 | line.clear();
|
|---|
| 33 | }
|
|---|
| 34 | if (argc > 3) {
|
|---|
| 35 | line.str(argv[3]);
|
|---|
| 36 | line >> skiplines;
|
|---|
| 37 | cout << "Will skip first " << skiplines << " lines." << endl;
|
|---|
| 38 | line.clear();
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | // get average
|
|---|
| 42 | avg = 0.;
|
|---|
| 43 | zaehler=0;
|
|---|
| 44 | ifstream test(argv[2]);
|
|---|
| 45 | if (test == NULL) { cout << "Can't open File " << argv[2] << "\n"; return(255); }
|
|---|
| 46 | flag=1;
|
|---|
| 47 | while (getline(test, zeile, '\n')) {
|
|---|
| 48 | if (skiplines) {
|
|---|
| 49 | skiplines--;
|
|---|
| 50 | continue;
|
|---|
| 51 | }
|
|---|
| 52 | line.clear();
|
|---|
| 53 | line.str(zeile);
|
|---|
| 54 | for (int j=0;j<=col;j++)
|
|---|
| 55 | if (!line.eof()) {
|
|---|
| 56 | line >> ws >> tmp;
|
|---|
| 57 | } else {
|
|---|
| 58 | flag=0;
|
|---|
| 59 | }
|
|---|
| 60 | if (flag) {
|
|---|
| 61 | avg += tmp;
|
|---|
| 62 | zaehler++;
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 | if (!flag) {
|
|---|
| 66 | cerr << "File does not contain " << col << " column(s)." << endl;
|
|---|
| 67 | exit(1);
|
|---|
| 68 | } else
|
|---|
| 69 | if (zaehler != 0) avg /= zaehler;
|
|---|
| 70 | // goto to beginning again
|
|---|
| 71 | test.clear();
|
|---|
| 72 | test.seekg(ios::beg);
|
|---|
| 73 |
|
|---|
| 74 | // get deviation
|
|---|
| 75 | dev = 0.;
|
|---|
| 76 | zaehler=0;
|
|---|
| 77 | flag=1;
|
|---|
| 78 | while (getline(test, zeile, '\n')) {
|
|---|
| 79 | if (skiplines) {
|
|---|
| 80 | skiplines--;
|
|---|
| 81 | continue;
|
|---|
| 82 | }
|
|---|
| 83 | line.clear();
|
|---|
| 84 | line.str(zeile);
|
|---|
| 85 | for (int j=0;j<=col;j++)
|
|---|
| 86 | line >> ws >> tmp;
|
|---|
| 87 | if (!line.eof()) {
|
|---|
| 88 | dev += (tmp - avg)*(tmp - avg);
|
|---|
| 89 | zaehler++;
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 | test.close();
|
|---|
| 93 | if (zaehler != 0) dev /= zaehler;
|
|---|
| 94 |
|
|---|
| 95 | cout << setprecision(8) << avg << "\t" << setprecision(8) << sqrt(dev) << "\n";
|
|---|
| 96 | return(0);
|
|---|
| 97 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.