1 | // C++ Interface: STSideBandSep
|
---|
2 | //
|
---|
3 | // Description:
|
---|
4 | // A class to invoke sideband separation of Scantable
|
---|
5 | //
|
---|
6 | // Author: Kana Sugimoto <kana.sugi@nao.ac.jp>, (C) 2012
|
---|
7 | //
|
---|
8 | // Copyright: See COPYING file that comes with this distribution
|
---|
9 | //
|
---|
10 | //
|
---|
11 |
|
---|
12 | // STL
|
---|
13 | #include <ctype.h>
|
---|
14 |
|
---|
15 | // cascore
|
---|
16 | #include <casa/OS/File.h>
|
---|
17 | #include <casa/Logging/LogIO.h>
|
---|
18 | #include <casa/Quanta/QuantumHolder.h>
|
---|
19 |
|
---|
20 | #include <measures/Measures/MFrequency.h>
|
---|
21 | #include <measures/Measures/MCFrequency.h>
|
---|
22 |
|
---|
23 | #include <tables/Tables/TableRecord.h>
|
---|
24 | #include <tables/Tables/TableVector.h>
|
---|
25 |
|
---|
26 | // asap
|
---|
27 | #include "STSideBandSep.h"
|
---|
28 |
|
---|
29 | using namespace std ;
|
---|
30 | using namespace casa ;
|
---|
31 | using namespace asap ;
|
---|
32 |
|
---|
33 | //#ifndef KS_DEBUG
|
---|
34 | //#define KS_DEBUG
|
---|
35 | //#endif
|
---|
36 |
|
---|
37 | namespace asap {
|
---|
38 |
|
---|
39 | // constructors
|
---|
40 | STSideBandSep::STSideBandSep()
|
---|
41 | : sigIfno_(0), ftol_(-1),
|
---|
42 | loTime_(-1), lo1Freq_(-1), loDir_("")
|
---|
43 | {
|
---|
44 | #ifdef KS_DEBUG
|
---|
45 | cout << "Default constructor STSideBandSep()" << endl;
|
---|
46 | #endif
|
---|
47 | // Default LO frame is TOPO
|
---|
48 | loFrame_ = MFrequency::TOPO;
|
---|
49 | };
|
---|
50 |
|
---|
51 | STSideBandSep::~STSideBandSep()
|
---|
52 | {
|
---|
53 | #ifdef KS_DEBUG
|
---|
54 | cout << "Destructor ~STSideBandSep()" << endl;
|
---|
55 | #endif
|
---|
56 | };
|
---|
57 |
|
---|
58 |
|
---|
59 | void STSideBandSep::setFrequency(const unsigned int ifno, const double freqtol, string frame)
|
---|
60 | {
|
---|
61 | #ifdef KS_DEBUG
|
---|
62 | cout << "STSideBandSep::setFrequency" << endl;
|
---|
63 | cout << "IFNO = " << ifno << endl;
|
---|
64 | cout << "freq tol = " << freqtol << " [Hz]" << endl;
|
---|
65 | cout << "frame = " << frame << endl;
|
---|
66 | #endif
|
---|
67 | };
|
---|
68 |
|
---|
69 | // Temporal function to set scantable of image sideband
|
---|
70 | void STSideBandSep::setImageTable(const ScantableWrapper &s)
|
---|
71 | {
|
---|
72 | #ifdef KS_DEBUG
|
---|
73 | cout << "STSideBandSep::setImageTable" << endl;
|
---|
74 | cout << "got image table nrow = " << s.nrow() << endl;
|
---|
75 | #endif
|
---|
76 | imgTab_p = s.getCP();
|
---|
77 | AlwaysAssert(!imgTab_p.null(),AipsError);
|
---|
78 |
|
---|
79 | };
|
---|
80 |
|
---|
81 | //
|
---|
82 | void STSideBandSep::setLO1(const double lo1, string frame, double reftime, string refdir)
|
---|
83 | {
|
---|
84 | lo1Freq_ = lo1;
|
---|
85 | MFrequency::getType(loFrame_, frame);
|
---|
86 | loTime_ = reftime;
|
---|
87 | loDir_ = refdir;
|
---|
88 |
|
---|
89 | #ifdef KS_DEBUG
|
---|
90 | cout << "STSideBandSep::setLO1" << endl;
|
---|
91 | if (lo1Freq_ > 0.)
|
---|
92 | cout << "lo1 = " << lo1Freq_ << " [Hz] (" << frame << ")" << endl;
|
---|
93 | if (loTime_ > 0.)
|
---|
94 | cout << "ref time = " << loTime_ << " [day]" << endl;
|
---|
95 | if (!loDir_.empty())
|
---|
96 | cout << "ref direction = " << loDir_ << " [day]" << endl;
|
---|
97 | #endif
|
---|
98 | };
|
---|
99 |
|
---|
100 | void STSideBandSep::setLO1Asdm(string asdmname)
|
---|
101 | {
|
---|
102 | // Check for existance of the asdm.
|
---|
103 | if (!checkFile(asdmname)) {
|
---|
104 | throw(AipsError("File does not exist"));
|
---|
105 | }
|
---|
106 | if (asdmname[(asdmname.size()-1)] == '/')
|
---|
107 | asdmname = asdmname.substr(0,(asdmname.size()-2));
|
---|
108 |
|
---|
109 | asdmName_ = asdmname;
|
---|
110 |
|
---|
111 | #ifdef KS_DEBUG
|
---|
112 | cout << "STSideBandSep::setLO1Asdm" << endl;
|
---|
113 | cout << "asdm name = " << asdmName_ << endl;
|
---|
114 | #endif
|
---|
115 | };
|
---|
116 |
|
---|
117 | void STSideBandSep::solveImageFreqency()
|
---|
118 | {
|
---|
119 | #ifdef KS_DEBUG
|
---|
120 | cout << "STSideBandSep::solveImageFrequency" << endl;
|
---|
121 | #endif
|
---|
122 | LogIO os(LogOrigin("STSideBandSep","solveImageFreqency()", WHERE));
|
---|
123 | os << "Start calculating frequencies of image side band" << LogIO::POST;
|
---|
124 |
|
---|
125 | if (imgTab_p.null())
|
---|
126 | throw AipsError("STSideBandSep::solveImageFreqency - an image side band scantable should be set first");
|
---|
127 |
|
---|
128 | // Check for the availability of LO1
|
---|
129 | os << "Looking for LO1. lo1Freq_ = " << lo1Freq_ << LogIO::POST;
|
---|
130 | if (lo1Freq_ > 0.) {
|
---|
131 | os << "Using user defined LO1 frequency." << LogIO::POST;
|
---|
132 | } else if (!asdmName_.empty() > 0) {
|
---|
133 | // ASDM name is set.
|
---|
134 | os << "Using user defined ASDM: " << asdmName_ << LogIO::POST;
|
---|
135 | if (!getLo1FromAsdm(asdmName_)) {
|
---|
136 | throw AipsError("Failed to get LO1 frequency from ASDM");
|
---|
137 | }
|
---|
138 | } else {
|
---|
139 | // Try getting ASDM name from scantable header
|
---|
140 | os << "Try getting information from scantable header" << LogIO::POST;
|
---|
141 | if (!getLo1FromTab(imgTab_p)) {
|
---|
142 | throw AipsError("Failed to get LO1 frequency from asis table");
|
---|
143 | }
|
---|
144 | }
|
---|
145 | // LO1 should now be ready.
|
---|
146 | if (lo1Freq_ < 0.)
|
---|
147 | throw(AipsError("Got negative LO1 Frequency"));
|
---|
148 |
|
---|
149 | // The code assumes that imgTab_p has only an IF and only a FREQ_ID
|
---|
150 | // is associated to an IFNO
|
---|
151 | // TODO: More complete Procedure would be
|
---|
152 | // 1. Get freq IDs associated to sigIfno_
|
---|
153 | // 2. Get freq information of the freq IDs
|
---|
154 | // 3. For each freqIDs, get freq infromation in TOPO and an LO1
|
---|
155 | // frequency and calculate image band frequencies.
|
---|
156 | STFrequencies freqTab_ = imgTab_p->frequencies();
|
---|
157 | // get the base frame of table
|
---|
158 | const MFrequency::Types tabframe = freqTab_.getFrame(true);
|
---|
159 | TableVector<uInt> freqIdVec( imgTab_p->table(), "FREQ_ID" );
|
---|
160 | // assuming single freqID per IFNO
|
---|
161 | uInt freqid = freqIdVec(0);
|
---|
162 | double refpix, refval, increment ;
|
---|
163 | freqTab_.getEntry(refpix, refval, increment, freqid);
|
---|
164 | //MFrequency sigrefval = MFrequency(MVFrequency(refval),tabframe);
|
---|
165 | // get freq infromation of sigIfno_ in loFrame_
|
---|
166 | const MPosition mp = imgTab_p->getAntennaPosition();
|
---|
167 | MEpoch me;
|
---|
168 | MDirection md;
|
---|
169 | if (loTime_ < 0.)
|
---|
170 | me = imgTab_p->getEpoch(-1);
|
---|
171 | else
|
---|
172 | me = MEpoch(MVEpoch(loTime_));
|
---|
173 | if (loDir_.empty()) {
|
---|
174 | ArrayColumn<Double> srcdirCol_;
|
---|
175 | srcdirCol_.attach(imgTab_p->table(), "SRCDIRECTION");
|
---|
176 | // Assuming J2000 and SRCDIRECTION in unit of rad
|
---|
177 | Quantum<Double> srcra = Quantum<Double>(srcdirCol_(0)(IPosition(1,0)), "rad");
|
---|
178 | Quantum<Double> srcdec = Quantum<Double>(srcdirCol_(0)(IPosition(1,1)), "rad");
|
---|
179 | md = MDirection(srcra, srcdec, MDirection::J2000);
|
---|
180 | //imgTab_p->getDirection(0);
|
---|
181 | } else {
|
---|
182 | // parse direction string
|
---|
183 | string::size_type pos0 = loDir_.find(" ");
|
---|
184 |
|
---|
185 | if (pos0 == string::npos) {
|
---|
186 | throw AipsError("bad string format in LO1 direction");
|
---|
187 | }
|
---|
188 | string::size_type pos1 = loDir_.find(" ", pos0+1);
|
---|
189 | String sepoch, sra, sdec;
|
---|
190 | if (pos1 != string::npos) {
|
---|
191 | sepoch = loDir_.substr(0, pos0);
|
---|
192 | sra = loDir_.substr(pos0+1, pos1-pos0);
|
---|
193 | sdec = loDir_.substr(pos1+1);
|
---|
194 | }
|
---|
195 | MDirection::Types epoch;
|
---|
196 | MDirection::getType(epoch, sepoch);
|
---|
197 | QuantumHolder qh ;
|
---|
198 | String err ;
|
---|
199 | qh.fromString( err, sra);
|
---|
200 | Quantum<Double> ra = qh.asQuantumDouble() ;
|
---|
201 | qh.fromString( err, sdec ) ;
|
---|
202 | Quantum<Double> dec = qh.asQuantumDouble() ;
|
---|
203 | md = MDirection(ra.getValue("rad"), dec.getValue("rad"),epoch);
|
---|
204 | }
|
---|
205 | // Summary (LO1)
|
---|
206 | Vector<Double> dirvec = md.getAngle(Unit(String("rad"))).getValue();
|
---|
207 | os << "[LO1 settings]" << LogIO::POST;
|
---|
208 | os << "- Frequency: " << lo1Freq_ << " [Hz] ("
|
---|
209 | << MFrequency::showType(loFrame_) << ")" << LogIO::POST;
|
---|
210 | os << "- Reference time: " << me.get(Unit(String("d"))).getValue()
|
---|
211 | << " [day]" << LogIO::POST;
|
---|
212 | os << "- Reference direction: [" << dirvec[0] << ", " << dirvec[1]
|
---|
213 | << "] (" << md.getRefString() << ") " << LogIO::POST;
|
---|
214 | //
|
---|
215 | MeasFrame mframe( me, mp, md );
|
---|
216 | MFrequency::Convert toloframe(loFrame_, MFrequency::Ref(tabframe, mframe));
|
---|
217 | MFrequency::Convert tobframe(tabframe, MFrequency::Ref(loFrame_, mframe));
|
---|
218 | // convert refval to loFrame_
|
---|
219 | double sigrefval;
|
---|
220 | if (tabframe == loFrame_)
|
---|
221 | sigrefval = refval;
|
---|
222 | else
|
---|
223 | sigrefval = toloframe(Quantum<Double>(refval, "Hz")).get("Hz").getValue();
|
---|
224 |
|
---|
225 | //Summary (signal)
|
---|
226 | os << "[Signal side band]" << LogIO::POST;
|
---|
227 | os << "- IFNO: " << imgTab_p->getIF(0) << " (FREQ_ID = " << freqid << ")"
|
---|
228 | << LogIO::POST;
|
---|
229 | os << "- Reference value: " << refval << " [Hz] ("
|
---|
230 | << MFrequency::showType(tabframe) << ") = "
|
---|
231 | << sigrefval << " [Hz] (" << MFrequency::showType(loFrame_)
|
---|
232 | << ")" << LogIO::POST;
|
---|
233 | os << "- Reference pixel: " << refpix << LogIO::POST;
|
---|
234 | os << "- Increment: " << increment << " [Hz]" << LogIO::POST;
|
---|
235 | // calculate image band incr and refval in loFrame_
|
---|
236 | Double imgincr = -increment;
|
---|
237 | Double imgrefval = 2 * lo1Freq_ - sigrefval;
|
---|
238 | Double imgrefval_tab = imgrefval;
|
---|
239 | // convert imgrefval back to table base frame
|
---|
240 | if (tabframe != loFrame_)
|
---|
241 | imgrefval = tobframe(Quantum<Double>(imgrefval, "Hz")).get("Hz").getValue();
|
---|
242 | // set new frequencies table
|
---|
243 | uInt fIDnew = freqTab_.addEntry(refpix, imgrefval, imgincr);
|
---|
244 | // update freq_ID in table.
|
---|
245 | freqIdVec = fIDnew;
|
---|
246 | // Summary (Image side band)
|
---|
247 | os << "[Signal side band]" << LogIO::POST;
|
---|
248 | os << "- IFNO: " << imgTab_p->getIF(0) << " (FREQ_ID = " << freqIdVec(0)
|
---|
249 | << ")" << LogIO::POST;
|
---|
250 | os << "- Reference value: " << imgrefval << " [Hz] ("
|
---|
251 | << MFrequency::showType(tabframe) << ") = "
|
---|
252 | << imgrefval_tab << " [Hz] (" << MFrequency::showType(loFrame_)
|
---|
253 | << ")" << LogIO::POST;
|
---|
254 | os << "- Reference pixel: " << refpix << LogIO::POST;
|
---|
255 | os << "- Increment: " << imgincr << " [Hz]" << LogIO::POST;
|
---|
256 | };
|
---|
257 |
|
---|
258 | Bool STSideBandSep::checkFile(const string name, string type)
|
---|
259 | {
|
---|
260 | File file(name);
|
---|
261 | if (!file.exists()){
|
---|
262 | return false;
|
---|
263 | } else if (type.empty()) {
|
---|
264 | return true;
|
---|
265 | } else {
|
---|
266 | // Check for file type
|
---|
267 | switch (tolower(type[0])) {
|
---|
268 | case 'f':
|
---|
269 | return file.isRegular(True);
|
---|
270 | case 'd':
|
---|
271 | return file.isDirectory(True);
|
---|
272 | case 's':
|
---|
273 | return file.isSymLink();
|
---|
274 | default:
|
---|
275 | throw AipsError("Invalid file type. Available types are 'file', 'directory', and 'symlink'.");
|
---|
276 | }
|
---|
277 | }
|
---|
278 | };
|
---|
279 |
|
---|
280 | bool STSideBandSep::getLo1FromAsdm(const string asdmname)
|
---|
281 | {
|
---|
282 | // Check for relevant tables.
|
---|
283 | string spwname = asdmname + "/SpectralWindow.xml";
|
---|
284 | string recname = asdmname + "/Receiver.xml";
|
---|
285 | if (!checkFile(spwname) || !checkFile(recname)) {
|
---|
286 | throw(AipsError("Could not find subtables in ASDM"));
|
---|
287 | }
|
---|
288 |
|
---|
289 | return false;
|
---|
290 |
|
---|
291 | };
|
---|
292 |
|
---|
293 |
|
---|
294 | bool STSideBandSep::getLo1FromTab(CountedPtr< Scantable > &scantab)
|
---|
295 | {
|
---|
296 | Table& tab = scantab->table();
|
---|
297 | // Check for relevant tables.
|
---|
298 | const String spwname = tab.keywordSet().asString("ASDM_SPECTRALWINDOW");
|
---|
299 | const String recname = tab.keywordSet().asString("ASDM_RECEIVER");
|
---|
300 | if (!checkFile(spwname,"directory") || !checkFile(recname,"directory")) {
|
---|
301 | throw(AipsError("Could not find subtables in MS"));
|
---|
302 | }
|
---|
303 | // get
|
---|
304 |
|
---|
305 | return false;
|
---|
306 |
|
---|
307 | };
|
---|
308 |
|
---|
309 |
|
---|
310 | } //namespace asap
|
---|