Line data Source code
1 : //# MSContinuumSubtractor.cc: Subtract continuum from spectral line data
2 : //# Copyright (C) 2004
3 : //# Associated Universities, Inc. Washington DC, USA.
4 : //#
5 : //# This library is free software; you can redistribute it and/or modify it
6 : //# under the terms of the GNU Library General Public License as published by
7 : //# the Free Software Foundation; either version 2 of the License, or (at your
8 : //# option) any later version.
9 : //#
10 : //# This library is distributed in the hope that it will be useful, but WITHOUT
11 : //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 : //# License for more details.
14 : //#
15 : //# You should have received a copy of the GNU Library General Public License
16 : //# along with this library; if not, write to the Free Software Foundation,
17 : //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 : //#
19 : //# Correspondence concerning AIPS++ should be addressed as follows:
20 : //# Internet email: casa-feedback@nrao.edu.
21 : //# Postal address: AIPS++ Project Office
22 : //# National Radio Astronomy Observatory
23 : //# 520 Edgemont Road
24 : //# Charlottesville, VA 22903-2475 USA
25 : //#
26 : //# $Id$
27 : //#
28 :
29 : #include <spectrallines/Splatalogue/SearchEngine.h>
30 :
31 : #include <casacore/casa/Containers/Record.h>
32 : #include <casacore/casa/IO/FiledesIO.h>
33 :
34 : #include <casacore/casa/OS/File.h>
35 : #include <imageanalysis/ImageAnalysis/ImageInputProcessor.h>
36 : #include <casacore/tables/Tables/ScalarColumn.h>
37 : #include <casacore/tables/TaQL/TableParse.h>
38 : #include <fcntl.h>
39 : #include <memory>
40 :
41 : #include <iostream>
42 : using namespace std;
43 :
44 : using namespace casacore;
45 : namespace casa {
46 :
47 0 : SearchEngine::SearchEngine(
48 : const SplatalogueTable* const table, const Bool list,
49 : const String& logfile, const Bool append
50 0 : ) : _log(new LogIO), _table(table), _logfile(logfile),
51 0 : _list(list), _append(append){
52 0 : if (!logfile.empty()) {
53 0 : OutputDestinationChecker::OutputStruct logfile;
54 0 : logfile.label = "logfile";
55 0 : logfile.outputFile = &_logfile;
56 0 : logfile.required = true;
57 0 : logfile.replaceable = true;
58 0 : vector<OutputDestinationChecker::OutputStruct> output(1);
59 0 : output[0] = logfile;
60 0 : OutputDestinationChecker::checkOutputs(&output, *_log);
61 0 : }
62 0 : }
63 :
64 0 : SearchEngine::~SearchEngine() {
65 0 : delete _log;
66 0 : }
67 :
68 0 : SplatalogueTable* SearchEngine::search(
69 : const String& resultsTableName, const Double freqLow, const Double freqHigh,
70 : const Vector<String>& species, const Bool recommendedOnly,
71 : const Vector<String>& chemNames, const Vector<String>& qns,
72 : const Double intensityLow, const Double intensityHigh,
73 : const Double smu2Low, const Double smu2High,
74 : const Double logaLow, const Double logaHigh,
75 : const Double elLow, const Double elHigh,
76 : const Double euLow, const Double euHigh,
77 : const Bool includeRRLs, const Bool onlyRRLs
78 : ) const {
79 0 : LogOrigin origin("SearchEngine", __FUNCTION__);
80 0 : *_log << origin;
81 0 : if (! resultsTableName.empty()) {
82 0 : File tab(resultsTableName);
83 0 : if (! tab.canCreate()) {
84 0 : *_log << "Cannot create table " << resultsTableName << LogIO::EXCEPTION;
85 : }
86 0 : }
87 0 : ostringstream query;
88 0 : query << "SELECT FROM " << _table->tableName();
89 0 : query << " WHERE ";
90 :
91 0 : query << "(" << _getBetweenClause(SplatalogueTable::FREQUENCY, freqLow, freqHigh) << ")";
92 0 : if (species.size() > 0) {
93 0 : query << " AND (" << SplatalogueTable::SPECIES << " IN (";
94 0 : for (uInt i=0; i<species.size(); i++) {
95 0 : query << "'" << species[i] << "'";
96 0 : if (i != species.size() - 1) {
97 0 : query << ", ";
98 : }
99 : }
100 0 : query << "))";
101 : }
102 :
103 0 : if (recommendedOnly) {
104 0 : query << " AND (" << SplatalogueTable::RECOMMENDED << ")";
105 : }
106 :
107 0 : if (chemNames.size() > 0) {
108 0 : query << " AND (" << SplatalogueTable::CHEMICAL_NAME << " IN (";
109 0 : for (uInt i=0; i<chemNames.size(); i++) {
110 0 : query << "'" << chemNames[i] << "'";
111 0 : if (i != chemNames.size() - 1) {
112 0 : query << ", ";
113 : }
114 : }
115 0 : query << "))";
116 : }
117 0 : if (qns.size() > 0) {
118 0 : query << " AND (" << SplatalogueTable::QUANTUM_NUMBERS << " IN (";
119 0 : for (uInt i=0; i<qns.size(); i++) {
120 0 : query << "'" << qns[i] << "'";
121 0 : if (i != qns.size() - 1) {
122 0 : query << ", ";
123 : }
124 : }
125 0 : query << "))";
126 : }
127 0 : String rrlPortion = (includeRRLs || onlyRRLs)
128 : ? "LINELIST = 'Recomb'"
129 0 : : "";
130 0 : ostringstream nonRRLPortion;
131 0 : if (! onlyRRLs) {
132 0 : nonRRLPortion << "(LINELIST != 'Recomb')";
133 0 : if (intensityLow < intensityHigh) {
134 0 : nonRRLPortion << " AND " << _getBetweenClause(
135 : SplatalogueTable::INTENSITY, intensityLow, intensityHigh
136 0 : );
137 : }
138 0 : if (smu2Low < smu2High) {
139 0 : nonRRLPortion << " AND " << _getBetweenClause(
140 : SplatalogueTable::SMU2, smu2Low, smu2High
141 0 : );
142 : }
143 0 : if (logaLow < logaHigh) {
144 0 : nonRRLPortion << " AND " << _getBetweenClause(
145 : SplatalogueTable::LOGA, logaLow, logaHigh
146 0 : );
147 : }
148 0 : if (elLow < elHigh) {
149 0 : nonRRLPortion << " AND " << _getBetweenClause(
150 : SplatalogueTable::EL, elLow, elHigh
151 0 : );
152 : }
153 0 : if (euLow < euHigh) {
154 0 : nonRRLPortion << " AND " << _getBetweenClause(
155 : SplatalogueTable::EU, euLow, euHigh
156 0 : );
157 : }
158 : }
159 0 : if (onlyRRLs) {
160 0 : query << " AND " << rrlPortion;
161 : }
162 0 : else if (includeRRLs) {
163 0 : query << " AND ((" << rrlPortion << ") OR (" << nonRRLPortion.str() << "))";
164 : }
165 : else {
166 0 : query << " AND " << nonRRLPortion.str();
167 : }
168 0 : query << " ORDER BY " << SplatalogueTable::FREQUENCY;
169 :
170 0 : Table resTable = _runQuery(query.str());
171 0 : std::unique_ptr<SplatalogueTable> resSplatTable(new SplatalogueTable(resTable));
172 0 : if (!resultsTableName.empty()) {
173 0 : resSplatTable->rename(resultsTableName, Table::NewNoReplace);
174 0 : resSplatTable->flush(true, true);
175 : }
176 0 : if (_list) {
177 0 : _logIt(resSplatTable->list());
178 : }
179 0 : return resSplatTable.release();
180 0 : }
181 :
182 0 : Vector<String> SearchEngine::uniqueSpecies() const {
183 0 : String query = "SELECT UNIQUE(" + SplatalogueTable::SPECIES
184 0 : + ") FROM " + _table->tableName() + " ORDER BY " + SplatalogueTable::SPECIES;
185 : // Table resTable = tableCommand(query).table();
186 0 : Table resTable = _runQuery(query);
187 :
188 0 : String logString;
189 0 : ScalarColumn<String> species(resTable, SplatalogueTable::SPECIES);
190 0 : Vector<String> vSpecies(species.nrow());
191 0 : for (uInt i=0; i<species.nrow(); i++) {
192 0 : vSpecies[i] = species(i);
193 0 : logString += species(i) + "\n";
194 : }
195 0 : _logIt(logString);
196 0 : return vSpecies;
197 0 : }
198 :
199 0 : Vector<String> SearchEngine::uniqueChemicalNames() const {
200 0 : String query = "SELECT UNIQUE(" + SplatalogueTable::CHEMICAL_NAME
201 0 : + ") FROM " + _table->tableName() + " ORDER BY " + SplatalogueTable::CHEMICAL_NAME;
202 : // Table resTable = tableCommand(query).table();
203 0 : Table resTable = _runQuery(query);
204 :
205 0 : ScalarColumn<String> chemNames(resTable, SplatalogueTable::CHEMICAL_NAME);
206 0 : Vector<String> vChemNames(chemNames.nrow());
207 0 : String logString;
208 0 : for (uInt i=0; i<chemNames.nrow(); i++) {
209 0 : vChemNames[i] = chemNames(i);
210 0 : logString += chemNames(i) + "\n";
211 : }
212 0 : _logIt(logString);
213 0 : return vChemNames;
214 0 : }
215 :
216 0 : String SearchEngine::_getBetweenClause(
217 : const String& col, const Double low, const Double high
218 : ) const {
219 0 : ostringstream os;
220 0 : os << col << " BETWEEN " << low << " AND " << high;
221 0 : return os.str();
222 0 : }
223 :
224 0 : Table SearchEngine::_runQuery(const String& query) const {
225 0 : String tablename = _table->tableName();
226 0 : File file(tablename);
227 0 : Bool dump = false;
228 0 : String queryCopy = query;
229 0 : if (! file.exists()) {
230 0 : *_log << LogIO::NORMAL << "Flushing a copy of " << tablename << " to disk so it can be queried" << LogIO::POST;
231 0 : Path newName = File::newUniqueName(".");
232 0 : uInt pos = query.find(tablename);
233 0 : uInt length = tablename.length();
234 0 : tablename = newName.absoluteName();
235 0 : _table->deepCopy(tablename, Table::Scratch, true, Table::AipsrcEndian, false);
236 0 : dump = true;
237 0 : cout << "query " << query << endl;
238 0 : cout << "new table " << tablename << endl;
239 0 : String begin = query.substr(0, pos);
240 0 : String end = query.substr(pos+length, query.length());
241 0 : queryCopy = begin + tablename + end;
242 0 : cout << "new query " << queryCopy << endl;
243 :
244 0 : }
245 0 : Table resTable = tableCommand(queryCopy).table();
246 0 : if (dump) {
247 0 : Table t(tablename);
248 0 : t.markForDelete();
249 : //*_log << LogIO::NORMAL << "Removing temporary disk copy " << tablename << LogIO::POST;
250 : //Table::deleteTable(tablename, true);
251 0 : }
252 0 : return resTable;
253 0 : }
254 :
255 0 : void SearchEngine::_logIt(const String& logString) const {
256 0 : LogOrigin origin("SearchEngine", __FUNCTION__);
257 0 : *_log << origin << logString << LogIO::POST;
258 0 : if (! _list) {
259 0 : return;
260 : }
261 0 : if (! _logfile.empty()) {
262 0 : File log(_logfile);
263 0 : switch (File::FileWriteStatus status = log.getWriteStatus()) {
264 0 : case File::OVERWRITABLE:
265 0 : if (_append) {
266 0 : Int fd = open(_logfile.c_str(), O_RDWR | O_APPEND);
267 0 : FiledesIO fio(fd, _logfile.c_str());
268 0 : fio.write(logString.length(), logString.c_str());
269 0 : FiledesIO::close(fd);
270 0 : *_log << LogIO::NORMAL << "Appended results to file "
271 0 : << _logfile << LogIO::POST;
272 0 : }
273 : // no break here to fall through to the File::CREATABLE block if logFileAppend is false
274 : case File::CREATABLE:
275 0 : if (status == File::CREATABLE || ! _append) {
276 : // can fall through from previous case block so status can be File::OVERWRITABLE
277 0 : String action = (status == File::OVERWRITABLE) ? "Overwrote" : "Created";
278 0 : Int fd = FiledesIO::create(_logfile.c_str());
279 0 : FiledesIO fio (fd, _logfile.c_str());
280 0 : fio.write(logString.length(), logString.c_str());
281 0 : FiledesIO::close(fd);
282 0 : *_log << LogIO::NORMAL << action << " file "
283 0 : << _logfile << " with new log file"
284 0 : << LogIO::POST;
285 0 : }
286 0 : break;
287 0 : default:
288 : // checks to see if the log file is not creatable or not writeable should have already been
289 : // done and if so _logFileName set to the empty string so this method wouldn't be called in
290 : // those cases.
291 0 : *_log << "Programming logic error. This block should never be reached" << LogIO::EXCEPTION;
292 : }
293 0 : }
294 :
295 0 : }
296 :
297 :
298 : } //# NAMESPACE CASA - END
299 :
|