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