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 | #include "STCalSkyOtfAlma.h" |
---|
29 | |
---|
30 | using namespace casa; |
---|
31 | using namespace std; |
---|
32 | |
---|
33 | namespace asap { |
---|
34 | |
---|
35 | CalibrationManager::CalibrationManager() |
---|
36 | : target_(0), |
---|
37 | calmode_(""), |
---|
38 | spwlist_(0), |
---|
39 | spwlist_withrange_(), |
---|
40 | do_average_(false) |
---|
41 | { |
---|
42 | applicator_ = new STApplyCal(); |
---|
43 | } |
---|
44 | |
---|
45 | CalibrationManager::~CalibrationManager() |
---|
46 | { |
---|
47 | } |
---|
48 | |
---|
49 | void CalibrationManager::setScantable(ScantableWrapper &s) |
---|
50 | { |
---|
51 | os_.origin(LogOrigin("CalibrationManager","setScantable",WHERE)); |
---|
52 | os_ << LogIO::DEBUGGING << "set scantable object." << LogIO::POST; |
---|
53 | target_ = s.getCP(); |
---|
54 | } |
---|
55 | |
---|
56 | void CalibrationManager::setScantableByName(const string &s) |
---|
57 | { |
---|
58 | os_.origin(LogOrigin("CalibrationManager","setScantableAsName",WHERE)); |
---|
59 | os_ << LogIO::DEBUGGING << "set scantable " << s << "." << LogIO::POST; |
---|
60 | // always plain table |
---|
61 | target_ = new Scantable(s, Table::Plain); |
---|
62 | } |
---|
63 | |
---|
64 | void CalibrationManager::addApplyTable(const string &c) |
---|
65 | { |
---|
66 | STCalEnum::CalType caltype = STApplyTable::getCalType(c); |
---|
67 | if (caltype == STCalEnum::CalTsys) { |
---|
68 | addTsysTable(c); |
---|
69 | } |
---|
70 | else if (caltype != STCalEnum::NoType) { |
---|
71 | // should be sky table |
---|
72 | addSkyTable(c); |
---|
73 | } |
---|
74 | else { |
---|
75 | os_.origin(LogOrigin("CalibrationManager","addCalTable",WHERE)); |
---|
76 | os_ << LogIO::WARN << "Table " << c << " is not ApplyTable." << LogIO::POST ; |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | void CalibrationManager::addSkyTable(const string &c) |
---|
81 | { |
---|
82 | os_.origin(LogOrigin("CalibrationManager","addSkyTable",WHERE)); |
---|
83 | os_ << LogIO::DEBUGGING << "add STCalSkyTable " << c << "." << LogIO::POST; |
---|
84 | skytables_.push_back(new STCalSkyTable(c)); |
---|
85 | } |
---|
86 | |
---|
87 | void CalibrationManager::addTsysTable(const string &c) |
---|
88 | { |
---|
89 | os_.origin(LogOrigin("CalibrationManager","addTsysTable",WHERE)); |
---|
90 | os_ << LogIO::DEBUGGING << "add STCalTsysTable " << c << "." << LogIO::POST; |
---|
91 | tsystables_.push_back(new STCalTsysTable(c)); |
---|
92 | } |
---|
93 | |
---|
94 | void CalibrationManager::setMode(const string &mode) |
---|
95 | { |
---|
96 | os_.origin(LogOrigin("CalibrationManager","setMode",WHERE)); |
---|
97 | os_ << LogIO::DEBUGGING << "set calibration mode to " << mode << "." << LogIO::POST; |
---|
98 | calmode_ = mode; |
---|
99 | calmode_.upcase(); |
---|
100 | } |
---|
101 | |
---|
102 | void CalibrationManager::setTimeInterpolation(const string &interp, int order) |
---|
103 | { |
---|
104 | os_.origin(LogOrigin("CalibrationManager","setTimeInterpolation",WHERE)); |
---|
105 | os_ << LogIO::DEBUGGING << "set interpolation method for time axis to " << interp << "." << LogIO::POST; |
---|
106 | applicator_->setTimeInterpolation(stringToInterpolationEnum(interp), |
---|
107 | order); |
---|
108 | } |
---|
109 | |
---|
110 | void CalibrationManager::setFrequencyInterpolation(const string &interp, int order) |
---|
111 | { |
---|
112 | os_.origin(LogOrigin("CalibrationManager","setFrequencyInterpolation",WHERE)); |
---|
113 | os_ << LogIO::DEBUGGING << "set interpolation method for frequency axis to " << interp << "." << LogIO::POST; |
---|
114 | applicator_->setFrequencyInterpolation(stringToInterpolationEnum(interp), |
---|
115 | order); |
---|
116 | } |
---|
117 | |
---|
118 | void CalibrationManager::setTsysTransfer(unsigned int from, |
---|
119 | const vector<unsigned int> &to) |
---|
120 | { |
---|
121 | os_.origin(LogOrigin("CalibrationManager","setTsysTransfer",WHERE)); |
---|
122 | os_ << LogIO::DEBUGGING << "associate Tsys IFNO " << from << " with science IFNO ["; |
---|
123 | for (size_t i = 0; i < to.size() ; i++) { |
---|
124 | os_ << to[i]; |
---|
125 | if (i == to.size() - 1) |
---|
126 | os_ << "]."; |
---|
127 | else |
---|
128 | os_ << ", "; |
---|
129 | } |
---|
130 | os_ << LogIO::POST; |
---|
131 | Vector<uInt> v(to); |
---|
132 | applicator_->setTsysTransfer(from, v); |
---|
133 | } |
---|
134 | |
---|
135 | void CalibrationManager::setTsysSpw(const vector<int> &spwlist) |
---|
136 | { |
---|
137 | os_.origin(LogOrigin("CalibrationManager","setTsysSpw",WHERE)); |
---|
138 | os_ << LogIO::DEBUGGING << "set IFNO for Tsys calibration to ["; |
---|
139 | for (size_t i = 0; i < spwlist.size() ; i++) { |
---|
140 | os_ << spwlist[i]; |
---|
141 | if (i == spwlist.size() - 1) |
---|
142 | os_ << "]."; |
---|
143 | else |
---|
144 | os_ << ", "; |
---|
145 | } |
---|
146 | os_ << LogIO::POST; |
---|
147 | spwlist_ = spwlist; |
---|
148 | } |
---|
149 | |
---|
150 | void CalibrationManager::setTsysSpwWithRange(const Record &spwlist, bool average) |
---|
151 | { |
---|
152 | os_.origin(LogOrigin("CalibrationManager","setTsysSpw",WHERE)); |
---|
153 | os_ << LogIO::DEBUGGING << "set IFNO for Tsys calibration to " << LogIO::POST; |
---|
154 | spwlist.print(os_.output()); |
---|
155 | os_ << LogIO::DEBUGGING << LogIO::POST; |
---|
156 | os_ << LogIO::DEBUGGING << ((average) ? "with averaging" : "without averaging") << LogIO::POST; |
---|
157 | spwlist_withrange_ = spwlist; |
---|
158 | do_average_ = average; |
---|
159 | } |
---|
160 | |
---|
161 | void CalibrationManager::resetCalSetup() |
---|
162 | { |
---|
163 | os_.origin(LogOrigin("CalibrationManager","resetCalSetup",WHERE)); |
---|
164 | os_ << LogIO::DEBUGGING << "reset all calibration settings except target data ." << LogIO::POST; |
---|
165 | applicator_->reset(); |
---|
166 | calmode_ = ""; |
---|
167 | spwlist_.clear(); |
---|
168 | spwlist_withrange_ = Record(); |
---|
169 | do_average_ = false; |
---|
170 | } |
---|
171 | |
---|
172 | void CalibrationManager::reset() |
---|
173 | { |
---|
174 | os_.origin(LogOrigin("CalibrationManager","reset",WHERE)); |
---|
175 | os_ << LogIO::DEBUGGING << "reset all calibration settings." << LogIO::POST; |
---|
176 | applicator_->completeReset(); |
---|
177 | calmode_ = ""; |
---|
178 | spwlist_.clear(); |
---|
179 | spwlist_withrange_ = Record(); |
---|
180 | do_average_ = false; |
---|
181 | } |
---|
182 | |
---|
183 | void CalibrationManager::calibrate() |
---|
184 | { |
---|
185 | os_.origin(LogOrigin("CalibrationManager","calibrate",WHERE)); |
---|
186 | os_ << LogIO::DEBUGGING << "start calibration with mode " << calmode_ << "." << LogIO::POST; |
---|
187 | //assert(!target_.null()); |
---|
188 | assert_<AipsError>(!target_.null(), "You have to set target scantable first."); |
---|
189 | if (calmode_ == "TSYS") { |
---|
190 | //assert(spwlist_.size() > 0); |
---|
191 | if (spwlist_withrange_.empty()) { |
---|
192 | assert_<AipsError>(spwlist_.size() > 0, "You have to set list of IFNOs for ATM calibration."); |
---|
193 | STCalTsys cal(target_, spwlist_); |
---|
194 | cal.calibrate(); |
---|
195 | tsystables_.push_back(cal.applytable()); |
---|
196 | } |
---|
197 | else { |
---|
198 | STCalTsys cal(target_, spwlist_withrange_, do_average_); |
---|
199 | cal.calibrate(); |
---|
200 | tsystables_.push_back(cal.applytable()); |
---|
201 | } |
---|
202 | } |
---|
203 | else if (calmode_ == "PS") { |
---|
204 | // // will match DV01-25, DA41-65, PM01-04, CM01-12 |
---|
205 | // 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]))$"); |
---|
206 | // const String antname = target_->getAntennaName(); |
---|
207 | // if (reant.match(antname.c_str(), antname.size()) != String::npos) { |
---|
208 | if (isAlmaAntenna()) { |
---|
209 | os_ << LogIO::DEBUGGING << "ALMA specific position-switch calibration." << LogIO::POST; |
---|
210 | STCalSkyPSAlma cal(target_); |
---|
211 | cal.calibrate(); |
---|
212 | skytables_.push_back(cal.applytable()); |
---|
213 | } |
---|
214 | else { |
---|
215 | String msg = "Calibration type " + calmode_ + " for non-ALMA antenna " + target_->getAntennaName() + " is not supported."; |
---|
216 | os_.origin(LogOrigin("CalibrationManager","calibrate",WHERE)); |
---|
217 | os_ << LogIO::SEVERE << msg << LogIO::POST; |
---|
218 | throw AipsError(msg); |
---|
219 | } |
---|
220 | } |
---|
221 | else if (calmode_ == "OTF" || calmode_ == "OTFRASTER") { |
---|
222 | if (isAlmaAntenna()) { |
---|
223 | os_ << LogIO::DEBUGGING << "ALMA specific position-switch calibration." << LogIO::POST; |
---|
224 | STCalSkyOtfAlma cal(target_, (calmode_ == "OTFRASTER")); |
---|
225 | if (!options_.empty()) |
---|
226 | cal.setOption(options_); |
---|
227 | cal.calibrate(); |
---|
228 | skytables_.push_back(cal.applytable()); |
---|
229 | } |
---|
230 | else { |
---|
231 | String msg = "Calibration type " + calmode_ + " for non-ALMA antenna " + target_->getAntennaName() + " is not supported."; |
---|
232 | os_.origin(LogOrigin("CalibrationManager","calibrate",WHERE)); |
---|
233 | os_ << LogIO::SEVERE << msg << LogIO::POST; |
---|
234 | throw AipsError(msg); |
---|
235 | } |
---|
236 | } |
---|
237 | else { |
---|
238 | String msg = "Calibration type " + calmode_ + " is not supported."; |
---|
239 | os_.origin(LogOrigin("CalibrationManager","calibrate",WHERE)); |
---|
240 | os_ << LogIO::SEVERE << msg << LogIO::POST; |
---|
241 | throw AipsError(msg); |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | void CalibrationManager::apply(bool insitu, bool filltsys) |
---|
246 | { |
---|
247 | os_.origin(LogOrigin("CalibrationManager","apply",WHERE)); |
---|
248 | os_ << LogIO::DEBUGGING << "apply calibration to the data." << LogIO::POST; |
---|
249 | applicator_->setTarget(target_); |
---|
250 | for (size_t i = 0; i < tsystables_.size() ; i++) |
---|
251 | applicator_->push(dynamic_cast<STCalTsysTable*>(&(*tsystables_[i]))); |
---|
252 | for (size_t i = 0; i < skytables_.size(); i++) |
---|
253 | applicator_->push(dynamic_cast<STCalSkyTable*>(&(*skytables_[i]))); |
---|
254 | applicator_->apply(insitu, filltsys); |
---|
255 | } |
---|
256 | |
---|
257 | void CalibrationManager::saveCaltable(const string &name) |
---|
258 | { |
---|
259 | os_.origin(LogOrigin("CalibrationManager","saveCaltable",WHERE)); |
---|
260 | if (calmode_ == "TSYS") { |
---|
261 | //assert(tsystables_.size() > 0); |
---|
262 | assert_<AipsError>(tsystables_.size() > 0, "Tsys table list is empty."); |
---|
263 | os_ << LogIO::DEBUGGING << "save latest STCalTsysTable as " << name << "." << LogIO::POST; |
---|
264 | tsystables_[tsystables_.size()-1]->save(name); |
---|
265 | } |
---|
266 | else { |
---|
267 | //assert(skytables_.size() > 0); |
---|
268 | assert_<AipsError>(skytables_.size() > 0, "Sky table list is empty."); |
---|
269 | os_ << LogIO::DEBUGGING << "save latest STCalSkyTable as " << name << "." << LogIO::POST; |
---|
270 | skytables_[skytables_.size()-1]->save(name); |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | void CalibrationManager::split(const string &name) |
---|
275 | { |
---|
276 | os_.origin(LogOrigin("CalibrationManager","split",WHERE)); |
---|
277 | os_ << LogIO::DEBUGGING << "split science data and save them to " << name << "." << LogIO::POST; |
---|
278 | applicator_->save(name); |
---|
279 | } |
---|
280 | |
---|
281 | STCalEnum::InterpolationType CalibrationManager::stringToInterpolationEnum(const string &s) |
---|
282 | { |
---|
283 | String itype(s); |
---|
284 | itype.upcase(); |
---|
285 | const Char *c = itype.c_str(); |
---|
286 | String::size_type len = itype.size(); |
---|
287 | Regex nearest("^NEAREST(NEIGHBOR)?$"); |
---|
288 | Regex linear("^LINEAR$"); |
---|
289 | Regex spline("^(C(UBIC)?)?SPLINE$"); |
---|
290 | Regex poly("^POLY(NOMIAL)?$"); |
---|
291 | if (nearest.match(c, len) != String::npos) { |
---|
292 | return STCalEnum::NearestInterpolation; |
---|
293 | } |
---|
294 | else if (linear.match(c, len) != String::npos) { |
---|
295 | return STCalEnum::LinearInterpolation; |
---|
296 | } |
---|
297 | else if (spline.match(c, len) != String::npos) { |
---|
298 | return STCalEnum::CubicSplineInterpolation; |
---|
299 | } |
---|
300 | else if (poly.match(c, len) != String::npos) { |
---|
301 | return STCalEnum::PolynomialInterpolation; |
---|
302 | } |
---|
303 | |
---|
304 | os_.origin(LogOrigin("CalibrationManager","stringToInterpolationEnum",WHERE)); |
---|
305 | os_ << LogIO::WARN << "Interpolation type " << s << " is not available. Use default interpolation method." << LogIO::POST; |
---|
306 | return STCalEnum::DefaultInterpolation; |
---|
307 | } |
---|
308 | |
---|
309 | Bool CalibrationManager::isAlmaAntenna() |
---|
310 | { |
---|
311 | assert_<AipsError>(!target_.null(), "You have to set target scantable first."); |
---|
312 | // will match DV01-25, DA41-65, PM01-04, CM01-12 |
---|
313 | 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[0-2]))$"); |
---|
314 | const String antname = target_->getAntennaName(); |
---|
315 | return (reant.match(antname.c_str(), antname.size()) != String::npos); |
---|
316 | } |
---|
317 | |
---|
318 | } |
---|