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