qse  0.5.0
qsescanaverager.cpp
Go to the documentation of this file.
1 #include "qsescanaverager.h"
2 #include "qsedataset.h"
3 #include "qsescan.h"
4 
5 #include <stdio.h>
6 
8 (QseDataSet* inData, QseDataSet* outData, const QString& name, QObject* parent)
9  : QseEXAFSOperator(inData, outData, name, parent),
10  m_Result(NULL),
11  m_Totals(NULL),
12  m_NScans(0),
13  m_LookupIndex(0)
14 {
15 }
16 
18 {
19 }
20 
22 {
23  m_OutputData -> setHeader(m_InputData -> header());
24  m_OutputData -> setFileName(m_InputData -> fileName()+"_avg");
25 
26  m_Result = m_OutputData -> appendScan();
27  m_Totals = new QseScan("totals");
28 // m_Totals = m_OutputData -> appendScan();
29  m_NScans = 0;
30 }
31 
33 {
34  QseScan *scan = m_InputData -> scan(i);
35 
36  if (scan) {
37  if (m_NScans == 0) {
38  m_Result -> setName(scan->name()+"_avg");
39  m_Result -> setScanCommand(scan->scanCommand()+"_avg");
40  m_Result -> setScanDateTime(QDateTime::currentDateTime());
41  m_Result -> mergeHeaders(scan->header());
42 
43  m_Totals -> setName(scan->name()+"_totals");
44  m_Totals -> setScanCommand(scan->scanCommand()+"_totals");
45  m_Totals -> setScanDateTime(QDateTime::currentDateTime());
46  m_Totals -> mergeHeaders(scan->header());
47 
48  int ncols = scan -> columnCount();
49 
50  for (int i=0; i<ncols; i++) {
51  QseColumn *col = scan -> column(i);
52  QString nam = col->name();
53 
54  QseColumn *res = m_Result -> appendColumn(nam, col);
55  QseColumn *sum = m_Totals -> appendColumn(nam, col);
56 
57  sum -> assignment(1.0);
58  }
59  } else {
60  m_Result -> appendHeader("#C Averaged : "+scan->scanCommand());
61  m_Result -> mergeHeaders(scan->header());
62 
63  int ncols = scan -> columnCount();
64 
65  for (int i=0; i<ncols; i++) {
66  QseColumn *col = scan -> column(i);
67  QString nam = col->name();
68 
69  QseColumn *res = NULL;
70  QseColumn *sum = NULL;
71 
72  if (m_LookupIndex) {
73  res = m_Result -> column(i);
74  sum = m_Totals -> column(i);
75  } else {
76  res = m_Result -> column(nam);
77  sum = m_Totals -> column(nam);
78  }
79 
80  if (res == NULL) {
81  res = m_Result -> appendColumn(nam, col);
82  if (sum != NULL) {
83  printf("Problem: sum != NULL when res == NULL\n");
84  }
85  sum = m_Totals -> appendColumn(nam, col);
86  sum -> assignment(1.0);
87  } else {
88  if (sum == NULL) {
89  printf("Problem: sum == NULL when res != NULL\n");
90  sum = m_Totals -> appendColumn(nam, col);
91  sum -> assignment(1.0);
92  }
93 
94  int nRowsIn = col -> rowCount();
95  int nRowsOut = res -> rowCount();
96 
97  if (nRowsOut < nRowsIn) {
98  res -> setRowCount(nRowsIn);
99  sum -> setRowCount(nRowsIn);
100  }
101 
102  for (int r=0; r<nRowsIn; r++) {
103  double vres = res->data(r);
104  double vsum = sum->data(r);
105 
106  res->setData(r, vres+col->data(r));
107  sum->setData(r, vsum+1.0);
108  }
109  }
110  }
111  }
112 
113  m_NScans += 1;
114  }
115 }
116 
118 {
119  int ncols = m_Result -> columnCount();
120 
121  for (int i = 0; i<ncols; i++) {
122  QseColumn *res = m_Result->column(i);
123  QseColumn *tot = m_Totals->column(i);
124 
125  res->division(tot);
126  }
127 
128  delete m_Totals;
129 
130  m_Result = NULL;
131  m_Totals = NULL;
132 
133  m_OutputData -> emitOperationCompleted();
134 }