1 | //
|
---|
2 | // C++ Implementation: STCalTsys
|
---|
3 | //
|
---|
4 | // Description:
|
---|
5 | //
|
---|
6 | //
|
---|
7 | // Author: Takeshi Nakazato <takeshi.nakazato@nao.ac.jp> (C) 2012
|
---|
8 | //
|
---|
9 | // Copyright: See COPYING file that comes with this distribution
|
---|
10 | //
|
---|
11 | //
|
---|
12 |
|
---|
13 | #include <vector>
|
---|
14 |
|
---|
15 | #include <casa/Arrays/ArrayMath.h>
|
---|
16 | #include <casa/Logging/LogIO.h>
|
---|
17 | #include <tables/Tables/ScalarColumn.h>
|
---|
18 |
|
---|
19 | #include "STSelector.h"
|
---|
20 | #include "STCalTsys.h"
|
---|
21 | #include "STDefs.h"
|
---|
22 | #include <atnf/PKSIO/SrcType.h>
|
---|
23 |
|
---|
24 | using namespace std;
|
---|
25 | using namespace casa;
|
---|
26 |
|
---|
27 | namespace asap {
|
---|
28 | STCalTsys::STCalTsys(CountedPtr<Scantable> &s, vector<int> &iflist)
|
---|
29 | : STCalibration(s, "TSYS"),
|
---|
30 | iflist_(iflist),
|
---|
31 | tsysspw_(),
|
---|
32 | do_average_(false)
|
---|
33 | {
|
---|
34 | applytable_ = new STCalTsysTable(*s);
|
---|
35 | }
|
---|
36 |
|
---|
37 | STCalTsys::STCalTsys(CountedPtr<Scantable> &s, Record &iflist, bool average)
|
---|
38 | : STCalibration(s, "TSYS"),
|
---|
39 | iflist_(),
|
---|
40 | tsysspw_(iflist),
|
---|
41 | do_average_(average)
|
---|
42 | {
|
---|
43 | iflist_.resize(tsysspw_.nfields());
|
---|
44 | for (uInt i = 0; i < tsysspw_.nfields(); ++i) {
|
---|
45 | iflist_[i] = std::atoi(tsysspw_.name(i).c_str());
|
---|
46 | }
|
---|
47 | applytable_ = new STCalTsysTable(*s);
|
---|
48 | }
|
---|
49 |
|
---|
50 | void STCalTsys::setupSelector(const STSelector &sel)
|
---|
51 | {
|
---|
52 | // sel is an selector object that is associated with
|
---|
53 | // input Scantable
|
---|
54 | sel_ = sel;
|
---|
55 | //cout << "original: " << endl << sel_.print() << endl;
|
---|
56 | vector<int> ifnos = sel_.getIFs();
|
---|
57 | if (ifnos.size() > 0) {
|
---|
58 | int nif = 0;
|
---|
59 | int nifOrg = iflist_.size();
|
---|
60 | vector<int> iflistNew(iflist_);
|
---|
61 | for (int i = 0; i < nifOrg; i++) {
|
---|
62 | if (find(ifnos.begin(), ifnos.end(), iflist_[i]) != ifnos.end()) {
|
---|
63 | iflistNew[nif] = iflist_[i];
|
---|
64 | ++nif;
|
---|
65 | }
|
---|
66 | }
|
---|
67 | if (nif == 0) {
|
---|
68 | LogIO os(LogOrigin("STCalTsys", "setupSelector", WHERE));
|
---|
69 | os << LogIO::SEVERE << "Selection contains no data." << LogIO::EXCEPTION;
|
---|
70 | }
|
---|
71 | sel_.setIFs(iflistNew);
|
---|
72 | }
|
---|
73 | else {
|
---|
74 | sel_.setIFs(iflist_);
|
---|
75 | }
|
---|
76 |
|
---|
77 | // intent selection
|
---|
78 | ROScalarColumn<Int> srctypeCol(scantable_->table(), "SRCTYPE");
|
---|
79 | Vector<Int> srctype = srctypeCol.getColumn();
|
---|
80 | uInt calOffCount = 0;
|
---|
81 | uInt calOnCount = 0;
|
---|
82 | for (size_t i = 0; i < srctype.nelements(); ++i) {
|
---|
83 | if (srctype[i] == static_cast<Int>(SrcType::PONCAL)) {
|
---|
84 | calOnCount++;
|
---|
85 | }
|
---|
86 | else if (srctype[i] == static_cast<Int>(SrcType::POFFCAL)) {
|
---|
87 | calOffCount++;
|
---|
88 | }
|
---|
89 | }
|
---|
90 | //cout << "srctype = " << srctype << endl;
|
---|
91 | //cout << "calOnCount, calOffCount = " << calOnCount << "," << calOffCount << endl;
|
---|
92 | if (calOnCount == 0 || calOffCount == 0) {
|
---|
93 | LogIO os(LogOrigin("STCalTsys", "setupSelector", WHERE));
|
---|
94 | os << LogIO::SEVERE << "Input scantable doesn't have ATMCal intents" << LogIO::EXCEPTION;
|
---|
95 | }
|
---|
96 |
|
---|
97 | vector<int> intents = sel_.getTypes();
|
---|
98 | if (count(intents.begin(), intents.end(), static_cast<Int>(SrcType::POFFCAL)) == 0) {
|
---|
99 | intents.push_back(static_cast<Int>(SrcType::POFFCAL));
|
---|
100 | sel_.setTypes(intents);
|
---|
101 | }
|
---|
102 |
|
---|
103 | //cout << "modified: " << endl << sel_.print() << endl;
|
---|
104 | }
|
---|
105 |
|
---|
106 | void STCalTsys::appenddata(uInt scanno, uInt cycleno,
|
---|
107 | uInt beamno, uInt ifno, uInt polno,
|
---|
108 | uInt freqid, Double time, Float elevation,
|
---|
109 | const Vector<Float> &any_data,
|
---|
110 | const Vector<uChar> &channel_flag)
|
---|
111 | {
|
---|
112 | STCalTsysTable *p = dynamic_cast<STCalTsysTable *>(&(*applytable_));
|
---|
113 | if (do_average_ && tsysspw_.isDefined(String::toString(ifno))) {
|
---|
114 | LogIO os(LogOrigin("STCalTsys", "appenddata", WHERE));
|
---|
115 | Vector<Float> averaged_data(any_data.size());
|
---|
116 | Vector<uChar> averaged_flag(any_data.size(), 0);
|
---|
117 | Float averaged_value = 0.0;
|
---|
118 | size_t num_value = 0;
|
---|
119 | Vector<Double> channel_range = tsysspw_.asArrayDouble(String::toString(ifno));
|
---|
120 | os << LogIO::DEBUGGING << "do averaging: channel range for IFNO " << ifno << " is " << channel_range << LogIO::POST;
|
---|
121 | for (uInt i = 1; i < channel_range.size(); i += 2) {
|
---|
122 | size_t start = (size_t)channel_range[i-1];
|
---|
123 | size_t end = std::min((size_t)channel_range[i] + 1, averaged_data.size());
|
---|
124 | os << LogIO::DEBUGGING << "start=" << start << ", end=" << end << LogIO::POST;
|
---|
125 | float sum_per_segment = 0.0;
|
---|
126 | size_t count = 0;
|
---|
127 | for (size_t j = start; j < end; ++j) {
|
---|
128 | if (channel_flag[j] == 0) {
|
---|
129 | sum_per_segment += any_data[j];
|
---|
130 | count += 1;
|
---|
131 | }
|
---|
132 | }
|
---|
133 | averaged_value += sum_per_segment;
|
---|
134 | num_value += count;
|
---|
135 | }
|
---|
136 | averaged_value /= (Float)num_value;
|
---|
137 | averaged_data = averaged_value;
|
---|
138 | os << LogIO::DEBUGGING << "averaged_data = " << averaged_data << LogIO::POST;
|
---|
139 | os << LogIO::DEBUGGING << "any_data = " << any_data << LogIO::POST;
|
---|
140 | p->appenddata(scanno, cycleno, beamno, ifno, polno,
|
---|
141 | freqid, time, elevation, averaged_data,
|
---|
142 | averaged_flag);
|
---|
143 | }
|
---|
144 | else {
|
---|
145 | p->appenddata(scanno, cycleno, beamno, ifno, polno,
|
---|
146 | freqid, time, elevation, any_data,
|
---|
147 | channel_flag);
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | }
|
---|