1 | // |
---|
2 | // C++ Implementation: CalibrationManager |
---|
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 | #include <assert.h> |
---|
13 | |
---|
14 | #include <casa/Arrays/Vector.h> |
---|
15 | #include <casa/Exceptions/Error.h> |
---|
16 | #include <casa/Utilities/Assert.h> |
---|
17 | #include <casa/Utilities/Regex.h> |
---|
18 | #include <casa/BasicSL/String.h> |
---|
19 | #include <tables/Tables/Table.h> |
---|
20 | #include <tables/Tables/ScalarColumn.h> |
---|
21 | #include <measures/TableMeasures/ScalarMeasColumn.h> |
---|
22 | |
---|
23 | |
---|
24 | #include "CalibrationManager.h" |
---|
25 | #include "Scantable.h" |
---|
26 | #include "STCalTsys.h" |
---|
27 | #include "STCalSkyPSAlma.h" |
---|
28 | |
---|
29 | using namespace casa; |
---|
30 | using namespace std; |
---|
31 | |
---|
32 | namespace asap { |
---|
33 | |
---|
34 | CalibrationManager::CalibrationManager() |
---|
35 | : target_(0), |
---|
36 | calmode_(""), |
---|
37 | spwlist_(0) |
---|
38 | { |
---|
39 | applicator_ = new STApplyCal(); |
---|
40 | } |
---|
41 | |
---|
42 | CalibrationManager::~CalibrationManager() |
---|
43 | { |
---|
44 | } |
---|
45 | |
---|
46 | void CalibrationManager::setScantable(ScantableWrapper &s) |
---|
47 | { |
---|
48 | os_.origin(LogOrigin("CalibrationManager","setScantable",WHERE)); |
---|
49 | os_ << LogIO::DEBUGGING << "set scantable object." << LogIO::POST; |
---|
50 | target_ = s.getCP(); |
---|
51 | } |
---|
52 | |
---|
53 | void CalibrationManager::setScantableByName(const string &s) |
---|
54 | { |
---|
55 | os_.origin(LogOrigin("CalibrationManager","setScantableAsName",WHERE)); |
---|
56 | os_ << LogIO::DEBUGGING << "set scantable " << s << "." << LogIO::POST; |
---|
57 | // always plain table |
---|
58 | target_ = new Scantable(s, Table::Plain); |
---|
59 | } |
---|
60 | |
---|
61 | void CalibrationManager::addSkyTable(const string &c) |
---|
62 | { |
---|
63 | os_.origin(LogOrigin("CalibrationManager","addSkyTable",WHERE)); |
---|
64 | os_ << LogIO::DEBUGGING << "add STCalSkyTable " << c << "." << LogIO::POST; |
---|
65 | skytables_.push_back(new STCalSkyTable(c)); |
---|
66 | } |
---|
67 | |
---|
68 | void CalibrationManager::addTsysTable(const string &c) |
---|
69 | { |
---|
70 | os_.origin(LogOrigin("CalibrationManager","addTsysTable",WHERE)); |
---|
71 | os_ << LogIO::DEBUGGING << "add STCalTsysTable " << c << "." << LogIO::POST; |
---|
72 | tsystables_.push_back(new STCalTsysTable(c)); |
---|
73 | } |
---|
74 | |
---|
75 | void CalibrationManager::setMode(const string &mode) |
---|
76 | { |
---|
77 | os_.origin(LogOrigin("CalibrationManager","setMode",WHERE)); |
---|
78 | os_ << LogIO::DEBUGGING << "set calibration mode to " << mode << "." << LogIO::POST; |
---|
79 | calmode_ = mode; |
---|
80 | calmode_.upcase(); |
---|
81 | } |
---|
82 | |
---|
83 | void CalibrationManager::setTimeInterpolation(const string &interp, int order) |
---|
84 | { |
---|
85 | os_.origin(LogOrigin("CalibrationManager","setTimeInterpolation",WHERE)); |
---|
86 | os_ << LogIO::DEBUGGING << "set interpolation method for time axis to " << interp << "." << LogIO::POST; |
---|
87 | applicator_->setTimeInterpolation(stringToInterpolationEnum(interp), |
---|
88 | order); |
---|
89 | } |
---|
90 | |
---|
91 | void CalibrationManager::setFrequencyInterpolation(const string &interp, int order) |
---|
92 | { |
---|
93 | os_.origin(LogOrigin("CalibrationManager","setFrequencyInterpolation",WHERE)); |
---|
94 | os_ << LogIO::DEBUGGING << "set interpolation method for frequency axis to " << interp << "." << LogIO::POST; |
---|
95 | applicator_->setFrequencyInterpolation(stringToInterpolationEnum(interp), |
---|
96 | order); |
---|
97 | } |
---|
98 | |
---|
99 | void CalibrationManager::setTsysTransfer(unsigned int from, |
---|
100 | const vector<unsigned int> &to) |
---|
101 | { |
---|
102 | os_.origin(LogOrigin("CalibrationManager","setTsysTransfer",WHERE)); |
---|
103 | os_ << LogIO::DEBUGGING << "associate Tsys IFNO " << from << " with science IFNO ["; |
---|
104 | for (size_t i = 0; i < to.size() ; i++) { |
---|
105 | os_ << to[i]; |
---|
106 | if (i == to.size() - 1) |
---|
107 | os_ << "]."; |
---|
108 | else |
---|
109 | os_ << ", "; |
---|
110 | } |
---|
111 | os_ << LogIO::POST; |
---|
112 | Vector<uInt> v(to); |
---|
113 | applicator_->setTsysTransfer(from, v); |
---|
114 | } |
---|
115 | |
---|
116 | void CalibrationManager::setTsysSpw(const vector<int> &spwlist) |
---|
117 | { |
---|
118 | os_.origin(LogOrigin("CalibrationManager","setTsysSpw",WHERE)); |
---|
119 | os_ << LogIO::DEBUGGING << "set IFNO for Tsys calibration to ["; |
---|
120 | for (size_t i = 0; i < spwlist.size() ; i++) { |
---|
121 | os_ << spwlist[i]; |
---|
122 | if (i == spwlist.size() - 1) |
---|
123 | os_ << "]."; |
---|
124 | else |
---|
125 | os_ << ", "; |
---|
126 | } |
---|
127 | os_ << LogIO::POST; |
---|
128 | spwlist_ = spwlist; |
---|
129 | } |
---|
130 | |
---|
131 | void CalibrationManager::resetCalSetup() |
---|
132 | { |
---|
133 | os_.origin(LogOrigin("CalibrationManager","resetCalSetup",WHERE)); |
---|
134 | os_ << LogIO::DEBUGGING << "reset all calibration settings except target data ." << LogIO::POST; |
---|
135 | applicator_->reset(); |
---|
136 | calmode_ = ""; |
---|
137 | spwlist_.clear(); |
---|
138 | } |
---|
139 | |
---|
140 | void CalibrationManager::reset() |
---|
141 | { |
---|
142 | os_.origin(LogOrigin("CalibrationManager","reset",WHERE)); |
---|
143 | os_ << LogIO::DEBUGGING << "reset all calibration settings." << LogIO::POST; |
---|
144 | applicator_->completeReset(); |
---|
145 | calmode_ = ""; |
---|
146 | spwlist_.clear(); |
---|
147 | } |
---|
148 | |
---|
149 | void CalibrationManager::calibrate() |
---|
150 | { |
---|
151 | os_.origin(LogOrigin("CalibrationManager","calibrate",WHERE)); |
---|
152 | os_ << LogIO::DEBUGGING << "start calibration with mode " << calmode_ << "." << LogIO::POST; |
---|
153 | //assert(!target_.null()); |
---|
154 | assert_<AipsError>(!target_.null(), "You have to set target scantable first."); |
---|
155 | if (calmode_ == "TSYS") { |
---|
156 | //assert(spwlist_.size() > 0); |
---|
157 | assert_<AipsError>(spwlist_.size() > 0, "You have to set list of IFNOs for ATM calibration."); |
---|
158 | STCalTsys cal(target_, spwlist_); |
---|
159 | cal.calibrate(); |
---|
160 | tsystables_.push_back(cal.applytable()); |
---|
161 | } |
---|
162 | else if (calmode_ == "PS") { |
---|
163 | // will match DV01-25, DA41-65, PM01-04, CM01-12 |
---|
164 | Regex reant("^(DV(0[1-9]|1[0-9]|2[0-5])|DA(4[1-9]|5[0-9]|6[0-5])|PM0[1-4]|CM(0[1-9]|1[1,2]))$"); |
---|
165 | const String antname = target_->getAntennaName(); |
---|
166 | if (reant.match(antname.c_str(), antname.size()) != String::npos) { |
---|
167 | os_ << LogIO::DEBUGGING << "ALMA specific position-switch calibration." << LogIO::POST; |
---|
168 | STCalSkyPSAlma cal(target_); |
---|
169 | cal.calibrate(); |
---|
170 | skytables_.push_back(cal.applytable()); |
---|
171 | } |
---|
172 | else { |
---|
173 | String msg = "Calibration type " + calmode_ + " for antenna " + antname + " is not supported."; |
---|
174 | os_.origin(LogOrigin("CalibrationManager","calibrate",WHERE)); |
---|
175 | os_ << LogIO::SEVERE << msg << LogIO::POST; |
---|
176 | throw AipsError(msg); |
---|
177 | } |
---|
178 | } |
---|
179 | else { |
---|
180 | String msg = "Calibration type " + calmode_ + " is not supported."; |
---|
181 | os_.origin(LogOrigin("CalibrationManager","calibrate",WHERE)); |
---|
182 | os_ << LogIO::SEVERE << msg << LogIO::POST; |
---|
183 | throw AipsError(msg); |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | void CalibrationManager::apply(bool insitu, bool filltsys) |
---|
188 | { |
---|
189 | os_.origin(LogOrigin("CalibrationManager","apply",WHERE)); |
---|
190 | os_ << LogIO::DEBUGGING << "apply calibration to the data." << LogIO::POST; |
---|
191 | applicator_->setTarget(target_); |
---|
192 | for (size_t i = 0; i < tsystables_.size() ; i++) |
---|
193 | applicator_->push(dynamic_cast<STCalTsysTable*>(&(*tsystables_[i]))); |
---|
194 | for (size_t i = 0; i < skytables_.size(); i++) |
---|
195 | applicator_->push(dynamic_cast<STCalSkyTable*>(&(*skytables_[i]))); |
---|
196 | applicator_->apply(insitu, filltsys); |
---|
197 | } |
---|
198 | |
---|
199 | void CalibrationManager::saveCaltable(const string &name) |
---|
200 | { |
---|
201 | os_.origin(LogOrigin("CalibrationManager","saveCaltable",WHERE)); |
---|
202 | if (calmode_ == "TSYS") { |
---|
203 | //assert(tsystables_.size() > 0); |
---|
204 | assert_<AipsError>(tsystables_.size() > 0, "Tsys table list is empty."); |
---|
205 | os_ << LogIO::DEBUGGING << "save latest STCalTsysTable as " << name << "." << LogIO::POST; |
---|
206 | tsystables_[tsystables_.size()-1]->save(name); |
---|
207 | } |
---|
208 | else { |
---|
209 | //assert(skytables_.size() > 0); |
---|
210 | assert_<AipsError>(skytables_.size() > 0, "Sky table list is empty."); |
---|
211 | os_ << LogIO::DEBUGGING << "save latest STCalSkyTable as " << name << "." << LogIO::POST; |
---|
212 | skytables_[skytables_.size()-1]->save(name); |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | void CalibrationManager::split(const string &name) |
---|
217 | { |
---|
218 | os_.origin(LogOrigin("CalibrationManager","split",WHERE)); |
---|
219 | os_ << LogIO::DEBUGGING << "split science data and save them to " << name << "." << LogIO::POST; |
---|
220 | applicator_->save(name); |
---|
221 | } |
---|
222 | |
---|
223 | STCalEnum::InterpolationType CalibrationManager::stringToInterpolationEnum(const string &s) |
---|
224 | { |
---|
225 | String itype(s); |
---|
226 | itype.upcase(); |
---|
227 | const Char *c = itype.c_str(); |
---|
228 | String::size_type len = itype.size(); |
---|
229 | Regex nearest("^NEAREST(NEIGHBOR)?$"); |
---|
230 | Regex linear("^LINEAR$"); |
---|
231 | Regex spline("^(C(UBIC)?)?SPLINE$"); |
---|
232 | Regex poly("^POLY(NOMIAL)?$"); |
---|
233 | if (nearest.match(c, len) != String::npos) { |
---|
234 | return STCalEnum::NearestInterpolation; |
---|
235 | } |
---|
236 | else if (linear.match(c, len) != String::npos) { |
---|
237 | return STCalEnum::LinearInterpolation; |
---|
238 | } |
---|
239 | else if (spline.match(c, len) != String::npos) { |
---|
240 | return STCalEnum::CubicSplineInterpolation; |
---|
241 | } |
---|
242 | else if (poly.match(c, len) != String::npos) { |
---|
243 | return STCalEnum::PolynomialInterpolation; |
---|
244 | } |
---|
245 | |
---|
246 | os_.origin(LogOrigin("CalibrationManager","stringToInterpolationEnum",WHERE)); |
---|
247 | os_ << LogIO::WARN << "Interpolation type " << s << " is not available. Use default interpolation method." << LogIO::POST; |
---|
248 | return STCalEnum::DefaultInterpolation; |
---|
249 | } |
---|
250 | |
---|
251 | } |
---|