Ignore:
Timestamp:
Apr 14, 2010, 1:36:24 PM (16 years ago)
Author:
Frederik Heber <heber@…>
Children:
5a84ee
Parents:
4283d0
Message:

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@…>

File:
1 edited

Legend:

Unmodified
Added
Removed
  • util/src/average.cpp

    r4283d0 r35bab2  
    1313        double avg, dev;
    1414        double tmp;
    15         int j, zaehler;
    16   int firstcol, lastcol;
    17         double ecut;
     15        int zaehler;
     16  int col;
    1817        int flag;
     18        int skiplines=0;
    1919        string zeile;
    2020        stringstream line;
     
    2222        cout << ESPACKVersion << endl;
    2323
    24         if (argc < 4) {
    25                 cout << "Usage: " << argv[0] << " <firstCol> <lastCol> <X> <File>\n";
     24        if (argc <= 2) {
     25                cout << "Usage: " << argv[0] << " <column (zero-based)> <data file> [#skip initial lines]\n";
    2626                return(1);
    2727        }
    28         line.str(argv[1]);
    29   line >> firstcol;
    30   line.clear();
    31   line.str(argv[2]);
    32   line >> lastcol;
    33   line.clear();
    34   line.str(argv[3]);
    35   line >> ecut;
    36   cout << "Going from " << firstcol << " to " << lastcol << " with Ecut of " << ecut << "." << endl;
     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        }
    3740
    3841        // get average
    3942        avg = 0.;
    4043        zaehler=0;
    41         ifstream test(argv[4]);
    42         if (test == NULL) { cout << "Can't open File " << argv[4] << "\n"; return(255); }
     44        ifstream test(argv[2]);
     45        if (test == NULL) { cout << "Can't open File " << argv[2] << "\n"; return(255); }
    4346        flag=1;
    44         //cout << "Looking for " << ecut << " \n";
    4547        while (getline(test, zeile, '\n')) {
    46                 //cout << zeile;
    47                 istringstream input(zeile);
    48                 input >> ws >> tmp;                     
    49                 if (tmp==(double)ecut) {        // found correct line!
    50                         for (j=2;j<=lastcol;j++)
    51                                 if (!input.eof()) {
    52                                         input >> ws >> tmp;
    53           if (j >= firstcol) {
    54             avg += tmp;
    55             zaehler++;
    56           }
    57         }
    58                 }
     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    }
    5964        }
    60   test.clear();
    61         test.seekg(ios::beg);
    62         if (zaehler != 0) avg /= zaehler;       
     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);
    6373
    6474        // get deviation
     
    6676        zaehler=0;
    6777        flag=1;
    68         while (getline(test, zeile, '\n')) {
    69                 istringstream input(zeile);
    70                 input >> ws >> tmp;                     
    71                 if (tmp==(double)ecut) {        // found correct line!
    72                         for (j=2;j<=lastcol;j++)
    73                                 if (!input.eof()) {
    74                                         input >> ws >> tmp;
    75           if (j >= firstcol) {
    76             dev += (tmp - avg)*(tmp - avg);
    77             zaehler++;
    78           }
    79         }
    80                 }
    81         }
     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  }
    8292        test.close();
    83         //dev = 1/(zaehler)*dev;
    84         if (dev != 0) dev /= zaehler;
     93        if (zaehler != 0) dev /= zaehler;
    8594                       
    8695        cout << setprecision(8) << avg << "\t" << setprecision(8) << sqrt(dev) << "\n";
Note: See TracChangeset for help on using the changeset viewer.