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