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