Line data Source code
1 : //# SynthesisUtilMethods.h: Imager functionality sits here;
2 : //# Copyright (C) 1996,1997,1998,1999,2000,2001,2002,2003
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 : //#
25 : //# $Id$
26 :
27 : #ifndef SYNTHESIS_SYNTHESISUTILS_H
28 : #define SYNTHESIS_SYNTHESISUTILS_H
29 :
30 : #include <casacore/casa/aips.h>
31 : #include <casacore/casa/OS/Timer.h>
32 : #include <casacore/casa/Containers/Record.h>
33 : #include <casacore/ms/MeasurementSets/MeasurementSet.h>
34 : #include <casacore/casa/Arrays/IPosition.h>
35 : #include <casacore/casa/Quanta/Quantum.h>
36 : #include <casacore/casa/Quanta/QuantumHolder.h>
37 : #include <casacore/measures/Measures/MDirection.h>
38 : #include <casacore/casa/Quanta/MVAngle.h>
39 : #include <casacore/measures/Measures/MFrequency.h>
40 : #include <casacore/casa/Utilities/DataType.h>
41 : #include <stdcasa/StdCasa/CasacSupport.h>
42 : #include <casacore/coordinates/Coordinates/Projection.h>
43 : #include <casacore/coordinates/Coordinates/DirectionCoordinate.h>
44 : #include <casacore/coordinates/Coordinates/SpectralCoordinate.h>
45 : #include <casacore/coordinates/Coordinates/CoordinateSystem.h>
46 : #include <casacore/scimath/Mathematics/GaussianBeam.h>
47 :
48 : #include <msvis/MSVis/VisibilityIterator.h>
49 : #include <msvis/MSVis/VisibilityIterator2.h>
50 : //#include <synthesis/ImagerObjects/TmpSwitch.h>
51 :
52 : namespace casa { //# NAMESPACE CASA - BEGIN
53 :
54 : class SynthesisParams;
55 : class SynthesisParamsImage;
56 : class SynthesisParamsSelect;
57 :
58 : class SynthesisUtilMethods
59 : {
60 : public:
61 : enum VBSTATES {NOVALIDROWS=-1};
62 : // Default constructor
63 :
64 : SynthesisUtilMethods();
65 : ~SynthesisUtilMethods();
66 :
67 : // All functions here are stand-alone, self-contained methods.
68 :
69 :
70 : // Partitioning syntax for Selection parameters
71 : //
72 : // casacore::Input casacore::Record (example) :
73 : // { 'ms0' : { 'msname':xxx1, 'spw':yyy } ,
74 : // 'ms1' : { 'msname':xxx2, 'spw':yyy } }
75 : //
76 : // Output casacore::Record (example for partitioning on spw) :
77 : // { '0' : { 'ms0' : { 'msname':xxx1, 'spw':yyy1 } ,
78 : // 'ms1' : { 'msname':xxx2, 'spw':yyy1 } }
79 : // '1' : { 'ms0' : { 'msname':xxx1, 'spw':yyy2 } ,
80 : // 'ms1' : { 'msname':xxx2, 'spw':yyy2 } } }
81 :
82 : // casacore::Data partitioning rules for CONTINUUM imaging
83 : casacore::Record continuumDataPartition(casacore::Record &selpars, const casacore::Int npart);
84 :
85 : // casacore::Data partitioning rules for CUBE imaging
86 : //uniform contiguous partition in frequency step
87 : // Note that the spw selection will have a "-1"
88 : //if there is no overlap in the data selection provided and
89 : // frequency range provided
90 : static casacore::Record cubeDataPartition(const casacore::Record &selpars, const casacore::Int npart, const casacore::Double freqBeg, const casacore::Double freqEnd, const casacore::MFrequency::Types eltype=casacore::MFrequency::LSRK);
91 :
92 : // freqBeg and freqEnd are frequency range of the sub image cubes defined in frame set here
93 : // number of partions is obviously the length of freqBeg and freqEnd
94 : // Use this for non uniform width of imge frequencies
95 : static casacore::Record cubeDataPartition(const casacore::Record& selpars, const casacore::Vector<casacore::Double>& freqBeg, const casacore::Vector<casacore::Double>& freqEnd, const casacore::MFrequency::Types frame=casacore::MFrequency::LSRK);
96 :
97 : //casacore::CoordinateSystem and number of channels of original cube is passed in
98 : //Output record is the ms's and data selection for each part.
99 : // also the casacore::Vector of outCsys and outnChan are the coordinatesystems
100 : // and nchannel of the sub cube for each part.
101 : // The image is divided in n part along spectral channel
102 : //now if one of the sub cube has no match the the spw selection will have a "-1"
103 : //for that part. The caller will have to deal with that for load balancing etc..
104 : // Output casacore::Record (example for partitioning on spw) :
105 : // { '0' : { 'ms0' : { 'msname':xxx1, 'spw': '0:5~10' } ,
106 : // 'ms1' : { 'msname':xxx2, 'spw':'0:20~25' },
107 : // 'nchan': 6,
108 : // 'coordsys': { A record of the coordinatesystem of subcube 0}}
109 : // '1' : { 'ms0' : { 'msname':xxx1, 'spw':'0:9~14' } ,
110 : // 'ms1' : { 'msname':xxx2, 'spw':'0:24~29' },
111 : // 'nchan':6,
112 : // 'coordsys': { A record of the coordinatesystem of subcube 1} }
113 : // }
114 : // the coordsys keyed record can be use in casacore::CoordinateSystem:;restore to reconstrucr
115 : // it is the same as the parameter outCsys
116 : static casacore::Record cubeDataImagePartition(const casacore::Record & selpars, const casacore::CoordinateSystem&
117 : incsys, const casacore::Int npart, const casacore::Int nchannel,
118 : casacore::Vector<casacore::CoordinateSystem>& outCsys,
119 : casacore::Vector<casacore::Int>& outnChan);
120 :
121 : // Image cube partitioning rules for CUBE imaging
122 : casacore::Record cubeImagePartition(casacore::Record &impars, casacore::Int npart);
123 :
124 : // Validate the supplied VB. This currently only checks for the
125 : // existence of at least one non-flagged row and returns the index
126 : // of the first non-flagged row found. Otherwise returns
127 : // SynthesisUtilMethods::NOVALIDROWS. This static method is called
128 : // in the vis-iter loops to block invalid VBs from flowing down
129 : // the pipes.
130 : static casacore::Int validate(const VisBuffer& vb);
131 : static casacore::Int validate(const vi::VisBuffer2& vb);
132 :
133 :
134 : static casacore::Int getOptimumSize(const casacore::Int npix);
135 :
136 : static casacore::Bool fitPsfBeam(const casacore::String& imagename="", const casacore::Int nterms=1, const casacore::Float psfcutoff=0.35);
137 :
138 : ///The following 4 functions are for mtmfs via cube.
139 : //convert from/to cube to/from Taylor term images
140 : static casacore::Bool taylorCoeffsToCube(const casacore::String& cubename="",const casacore::String& mtname="", const casacore::Int nterms=1, const casacore::String& reffreq="");
141 : static casacore::Bool cubeToTaylorSum(const casacore::String& cubename="",const casacore::String& mtname="", const casacore::Int nterms=1, const casacore::String& reffreq="", const casacore::Int imtype=0, const casacore::Float pblimit=0.2);
142 : ///convert from/to cube pb to reference freq pb on the images
143 : static casacore::Bool removeFreqDepPB(const casacore::String& cubename="", const casacore::String& mtname="", const casacore::Float pblimit=0.2);
144 : static casacore::Bool applyFreqDepPB(const casacore::String& cubename="", const casacore::String& mtname="", const casacore::Float pblimit=0.2);
145 :
146 : static void getResource(casacore::String label="", casacore::String fname="");
147 :
148 : // return comprehensible direction string from given MDirection object
149 : static casacore::String asComprehensibleDirectionString(casacore::MDirection const &direction);
150 :
151 : // Advise the chanselection needed for the frequency range or
152 : // give the frequency range for a give spwselection
153 : // You need to specify the field_id for which this calculation is
154 : // being done.
155 : // getFreqRange=true then the freqrange in the frame and spwselection you choose is
156 : // returned in freqStart and freqEnd.
157 : casacore::Bool adviseChanSel(casacore::Double& freqStart,
158 : casacore::Double& freqEnd,
159 : const casacore::Double& freqStep,
160 : const casacore::MFrequency::Types& freqframe,
161 : casacore::Vector<casacore::Int>& spw,
162 : casacore::Vector<casacore::Int>& start,
163 : casacore::Vector<casacore::Int>& nchan,
164 : const casacore::String& msname="",
165 : const casacore::String& ephemtab="",
166 : const casacore::Int fieldid=0,
167 : const casacore::Bool getFreqRange=false,
168 : const casacore::String spwselection="");
169 :
170 :
171 :
172 : static casacore::Record fillWeightRecord(const casacore::String& type="natural",
173 : const casacore::String& rmode="norm",
174 : const casacore::Quantity& noise=casacore::Quantity(0.0, "Jy"),
175 : const casacore::Double robust=0.0,
176 : const casacore::Quantity& fieldofview=casacore::Quantity(0.0, "arcsec"),
177 : const casacore::Int npixels=0,
178 : const casacore::Bool multiField=false,
179 : const casacore::Bool useCubeBriggs=false,
180 : const casacore::String& filtertype=casacore::String("Gaussian"),
181 : const casacore::Quantity& filterbmaj=casacore::Quantity(0.0,"deg"),
182 : const casacore::Quantity& filterbmin=casacore::Quantity(0.0,"deg"),
183 : const casacore::Quantity& filterbpa=casacore::Quantity(0.0,"deg"), const casacore::Double& fracBW=0.0);
184 : static void getFromWeightRecord( casacore::String& type,casacore::String& rmode,
185 : casacore::Quantity& noise,casacore::Double& robust,
186 : casacore::Quantity& fieldofview, casacore::Int& npixels,
187 : casacore::Bool& multiField,casacore::Bool& useCubeBriggs,
188 : casacore::String& filtertype,
189 : casacore::Quantity& filterbmaj,
190 : casacore::Quantity& filterbmin,
191 : casacore::Quantity& filterbpa, casacore::Double& fracBW, const casacore::Record& inrec);
192 :
193 :
194 : protected:
195 : static casacore::String mergeSpwSel(const casacore::Vector<casacore::Int>& fspw, const casacore::Vector<casacore::Int>& fstart, const casacore::Vector<casacore::Int>& fnchan, const casacore::Matrix<casacore::Int>& spwsel);
196 :
197 : static casacore::Vector<casacore::uInt> primeFactors(casacore::uInt n, casacore::Bool douniq=true);
198 :
199 : private:
200 : static casacore::String makeResourceFilename(int pid);
201 :
202 : static casacore::String g_hostname;
203 : static casacore::String g_startTimestamp;
204 : static const casacore::String g_enableOptMemProfile;
205 :
206 : static casacore::Int parseProcStatusLine(const std::string &str);
207 :
208 : };
209 :
210 : class SynthesisParams
211 : {
212 : public:
213 17527 : SynthesisParams(){};
214 17524 : virtual ~SynthesisParams(){};
215 : ///As there is no state to copy
216 4327 : virtual SynthesisParams& operator= (const SynthesisParams& /*other*/){ return *this;};
217 : virtual void fromRecord(const casacore::Record &inrec)=0;
218 : virtual void setDefaults()=0;
219 : virtual casacore::String verify()const =0 ;
220 : virtual casacore::Record toRecord() const =0;
221 : protected:
222 : // All return strings are error messages. Null if valid.
223 : casacore::String readVal(const casacore::Record &rec, casacore::String id, casacore::String& val) const ;
224 : casacore::String readVal(const casacore::Record &rec, casacore::String id, casacore::Int& val) const ;
225 : casacore::String readVal(const casacore::Record &rec, casacore::String id, casacore::Float& val) const;
226 : casacore::String readVal(const casacore::Record &rec, casacore::String id, casacore::Bool& val) const ;
227 : casacore::String readVal(const casacore::Record &rec, casacore::String id, casacore::Vector<casacore::Int>& val) const;
228 : casacore::String readVal(const casacore::Record &rec, casacore::String id, casacore::Vector<casacore::Float>& val) const ;
229 : casacore::String readVal(const casacore::Record &rec, casacore::String id, casacore::Vector<casacore::String>& val) const ;
230 : casacore::String stringToQuantity(casacore::String instr, casacore::Quantity& qa) const;
231 : casacore::String stringToMDirection(casacore::String instr, casacore::MDirection& md) const ;
232 : casacore::String readVal(const casacore::Record &rec, casacore::String id, casacore::Quantity& val) const;
233 : casacore::String readVal(const casacore::Record &rec, casacore::String id, casacore::MDirection& val) const ;
234 : // Others..
235 : casacore::String MDirectionToString(casacore::MDirection val) const;
236 : casacore::String QuantityToString(casacore::Quantity val) const;
237 : casacore::String recordQMToString(const casacore::Record &rec) const ;
238 : };
239 :
240 : class SynthesisParamsSelect : public SynthesisParams
241 : {
242 : public:
243 :
244 : SynthesisParamsSelect();
245 : SynthesisParamsSelect(const SynthesisParamsSelect& other);
246 : ~SynthesisParamsSelect();
247 :
248 : //copy semantics
249 : virtual SynthesisParamsSelect& operator=(const SynthesisParamsSelect& other);
250 : void fromRecord(const casacore::Record &inrec);
251 : void setDefaults();
252 : casacore::String verify() const;
253 : casacore::Record toRecord() const;
254 :
255 : casacore::String msname, spw, freqbeg, freqend;
256 : casacore::MFrequency::Types freqframe;
257 : casacore::String field, antenna, timestr, scan, obs, state, uvdist,taql,intent;
258 : casacore::Bool usescratch,readonly,incrmodel;
259 :
260 : casacore::String datacolumn;
261 :
262 : };
263 :
264 :
265 :
266 : class SynthesisParamsImage: public SynthesisParams
267 : {
268 : public:
269 :
270 : SynthesisParamsImage();
271 : ~SynthesisParamsImage();
272 :
273 : void fromRecord(const casacore::Record &inrec);
274 : void setDefaults();
275 :
276 : //need to explicitly declare an = operator if private variables contains arrays
277 : SynthesisParamsImage& operator=(const SynthesisParamsImage& other);
278 :
279 : casacore::String verify() const;
280 : casacore::Record toRecord()const ;
281 :
282 :
283 : // Generate casacore::Coordinate System
284 : casacore::CoordinateSystem buildCoordinateSystem(ROVisibilityIterator* rvi);
285 :
286 : casacore::CoordinateSystem buildCoordinateSystem(vi::VisibilityIterator2& vi2, const std::map<casacore::Int, std::map<casacore::Int, casacore::Vector<casacore::Int> > >& chansel, casacore::Block<const casacore::MeasurementSet *> mss);
287 :
288 :
289 : casacore::CoordinateSystem buildCoordinateSystemCore(casacore::MeasurementSet& msobj,
290 : casacore::Vector<casacore::Int> spwids, casacore::Int fld,
291 : casacore::Double freqmin, casacore::Double freqmax,
292 : casacore::Double datafstart, casacore::Double datafend);
293 :
294 : casacore::Vector<casacore::Int> decideNPolPlanes(const casacore::String& stokes) const;
295 : casacore::IPosition shp() const;
296 : casacore::Bool getImFreq(casacore::Vector<casacore::Double>& ChanFreq, casacore::Vector<casacore::Double>& ChanWidth,
297 : casacore::Double& refPix, casacore::String& specmode,
298 : const casacore::MEpoch& obsEpoch, const casacore::MPosition& obsPosition,
299 : const casacore::Vector<casacore::Double>& dataChanFreqs, const casacore::Vector<casacore::Double>& dataFreqRes,
300 : const casacore::MFrequency::Types& dataFrame, const casacore::Quantity& qrestfreq,
301 : const casacore::Double& freqmin, const casacore::Double& freqmax,
302 : const casacore::MDirection& phaseCenter );
303 : casacore::Double getCubeImageStartFreq();
304 : casacore::String findSpecMode(const casacore::String& mode) const;
305 : casacore::String MDopToVelString(casacore::Record &rec);
306 : casacore::Record getcsys() const;
307 : // check consistency of image parameters when csys record exists and update
308 : // accordingly based on csys record
309 : casacore::Record updateParams(const casacore::Record &impar);
310 : //get the moving source direction in frame requested
311 : casacore::MDirection getMovingSourceDir(const casacore::MeasurementSet& ms, const casacore::MEpoch& refEp, const casacore::MPosition& refpos, const casacore::MDirection::Types outframe);
312 :
313 : // Sky coordinates
314 : casacore::String imageName, stokes;
315 : casacore::Vector<casacore::String> startModel;
316 : casacore::Vector<casacore::Int> imsize;
317 : casacore::Vector<casacore::Quantity> cellsize;
318 : casacore::Projection projection;
319 : casacore::Bool useNCP;
320 : casacore::MDirection phaseCenter;
321 : casacore::Int phaseCenterFieldId;
322 : casacore::MPosition obslocation;
323 :
324 : // Stokes info
325 : casacore::Bool pseudoi;
326 :
327 : // Spectral coordinates ( TT : Add other params here )
328 : casacore::Int nchan, nTaylorTerms, chanStart, chanStep;
329 : casacore::Quantity freqStart, freqStep, refFreq, velStart, velStep;
330 : casacore::MFrequency::Types freqFrame;
331 : casacore::MFrequency mFreqStart, mFreqStep;
332 : casacore::MRadialVelocity mVelStart, mVelStep;
333 : casacore::Vector<casacore::Quantity> restFreq;
334 : casacore::String start, step, frame, veltype, mode, reffreq, sysvel, sysvelframe;
335 : casacore::Quantity sysvelvalue;
336 :
337 : // private variable to store ref frame defined in casacore::Quantity or casacore::Measure
338 : // in start or step parameters and veltype from measure (e.g. casacore::MDoppler)
339 : casacore::String qmframe, mveltype;
340 : casacore::String tststr;
341 : // for holding quantity or measure records
342 : casacore::Record startRecord, stepRecord, reffreqRecord, sysvelRecord, restfreqRecord;
343 : // optional coordsys record
344 : casacore::Record csysRecord, csys;
345 : casacore::Vector<casacore::Int> imshape;
346 :
347 : //freqframe coversion?
348 : casacore::Bool freqFrameValid;
349 :
350 : casacore::Bool overwrite;
351 :
352 : casacore::String deconvolver;
353 : //moving source
354 : // Moving phase center ?
355 : casacore::Quantity distance;
356 : casacore::MDirection trackDir;
357 : casacore::Bool trackSource;
358 : casacore::String movingSource;
359 :
360 :
361 : };
362 :
363 :
364 : class SynthesisParamsGrid: public SynthesisParams
365 : {
366 : public:
367 :
368 : SynthesisParamsGrid();
369 : ~SynthesisParamsGrid();
370 :
371 : void fromRecord(const casacore::Record &inrec);
372 : void setDefaults();
373 : casacore::String verify() const;
374 : casacore::Record toRecord() const;
375 :
376 : casacore::String imageName;
377 :
378 : // FTMachine setup
379 : casacore::String gridder, ftmachine, convFunc, vpTable;
380 : casacore::Int wprojplanes;
381 : casacore::Bool useDoublePrec, useAutoCorr;
382 : casacore::Float padding;
383 :
384 : // Facets for gridding.
385 : casacore::Int facets;
386 :
387 : // casacore::Cube chunks for gridding
388 : casacore::Int chanchunks;
389 :
390 : // Spectral axis interpolation
391 : casacore::String interpolation;
392 :
393 : // Moving phase center ?
394 : casacore::Quantity distance;
395 : casacore::MDirection trackDir;
396 : casacore::Bool trackSource;
397 : casacore::String movingSource;
398 :
399 : // For wb-aprojection ftm.
400 : casacore::Bool aTermOn, psTermOn,mTermOn,wbAWP,usePointing, doPBCorr, conjBeams;
401 : casacore::String cfCache;
402 : casacore::Float computePAStep, rotatePAStep;
403 :
404 : // For single-dish imaging
405 : casacore::String pointingDirCol;
406 : casacore::String convertFirst;
407 : casacore::Float skyPosThreshold;
408 : casacore::Vector<casacore::Float> pointingOffsetSigDev;
409 : /* std::vector<float> pointingOffsetSigDev; */
410 : casacore::Int convSupport;
411 : casacore::Quantity truncateSize;
412 : casacore::Quantity gwidth;
413 : casacore::Quantity jwidth;
414 : casacore::Float minWeight;
415 : casacore::Bool clipMinMax;
416 :
417 : // Mapper Type.
418 : casacore::String mType;
419 :
420 : };
421 :
422 :
423 : class SynthesisParamsDeconv: public SynthesisParams
424 : {
425 : public:
426 :
427 : SynthesisParamsDeconv();
428 : ~SynthesisParamsDeconv();
429 :
430 : void fromRecord(const casacore::Record &inrec);
431 : void setDefaults();
432 : casacore::String verify() const;
433 : casacore::Record toRecord() const;
434 :
435 : casacore::String imageName, algorithm;
436 : casacore::Vector<casacore::String> startModel;
437 : casacore::Int deconvolverId; // maybe remove ? It's only to tag summary info.
438 : casacore::Int nTaylorTerms;
439 : casacore::Vector<casacore::Float> scales;
440 : casacore::Float scalebias;
441 : casacore::String maskString;
442 : casacore::String maskType;
443 : casacore::Vector<casacore::String> maskList;
444 : casacore::String autoMaskAlgorithm;
445 : casacore::Float pbMask;
446 : casacore::String maskThreshold;
447 : casacore::String maskResolution;
448 : casacore::Float fracOfPeak;
449 : casacore::Float maskResByBeam;
450 : casacore::Float sidelobeThreshold;
451 : casacore::Float noiseThreshold;
452 : casacore::Float lowNoiseThreshold;
453 : casacore::Float negativeThreshold;
454 : casacore::Float smoothFactor;
455 : casacore::Float minBeamFrac;
456 : casacore::Float cutThreshold;
457 : casacore::Int growIterations;
458 : casacore::Bool doGrowPrune;
459 : casacore::Float minPercentChange;
460 : casacore::Bool verbose;
461 : casacore::Bool fastnoise;
462 : casacore::Float nsigma;
463 : int nMask;
464 : bool autoAdjust;
465 : casacore::Float fusedThreshold;
466 : casacore::String specmode;
467 : casacore::Int largestscale;
468 : // task deconvolve needs to tell siimagestore that we don't need to check for the sumwt image
469 : casacore::Bool noRequireSumwt = casacore::False;
470 :
471 : // return dictionary size control
472 : casacore::Bool fullsummary;
473 :
474 : casacore::Bool interactive;
475 :
476 : casacore::GaussianBeam restoringbeam;
477 : casacore::String usebeam;
478 :
479 : };
480 :
481 :
482 : } //# NAMESPACE CASA - END
483 :
484 : #endif
|