Line data Source code
1 : //# tSubImage.cc: Test program for class SubImage
2 : //# Copyright (C) 1998,1999,2000,2001,2003
3 : //# Associated Universities, Inc. Washington DC, USA.
4 : //#
5 : //# This program is free software; you can redistribute it and/or modify it
6 : //# under the terms of the GNU General Public License as published by the Free
7 : //# Software Foundation; either version 2 of the License, or (at your option)
8 : //# any later version.
9 : //#
10 : //# This program 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 General Public License for
13 : //# more details.
14 : //#
15 : //# You should have received a copy of the GNU General Public License along
16 : //# with this program; if not, write to the Free Software Foundation, Inc.,
17 : //# 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 : #include <imageanalysis/ImageAnalysis/ImageMaskedPixelReplacer.h>
29 :
30 : #include <imageanalysis/ImageAnalysis/SubImageFactory.h>
31 :
32 : namespace casa {
33 :
34 : template <class T>
35 : const casacore::String ImageMaskedPixelReplacer<T>::_class = "ImageMaskedPixelReplacer";
36 :
37 : template <class T>
38 0 : ImageMaskedPixelReplacer<T>::ImageMaskedPixelReplacer(
39 : const SPIIT image,
40 : const casacore::Record *const ®ion,
41 : const casacore::String& mask
42 : ) : ImageTask<T>(
43 : image, "", region, "", "", "", mask, "", false
44 : ),
45 0 : _image(image) {
46 0 : this->_construct();
47 0 : }
48 :
49 : template <class T>
50 0 : void ImageMaskedPixelReplacer<T>::replace(
51 : const casacore::String& expr, casacore::Bool updateMask, casacore::Bool verbose
52 : ) {
53 0 : casacore::LogOrigin lor(_class, __func__);
54 0 : *this->_getLog() << lor;
55 0 : ThrowIf(
56 : expr.empty(),
57 : "You must specify an expression"
58 : );
59 :
60 : // Modify in place by writing to the image passed to the constructor.
61 0 : std::shared_ptr<casacore::SubImage<T> > subImage = SubImageFactory<T>::createSubImageRW(
62 0 : *_image, *this->_getRegion(), this->_getMask(),
63 0 : (verbose ? this->_getLog().get() : 0),
64 0 : casacore::AxesSpecifier(), this->_getStretch(), true
65 : );
66 0 : ThrowIf(
67 : ! subImage->isWritable(),
68 : "This image is not writable. It is probably "
69 : "a reference or expression virtual image"
70 : );
71 0 : ThrowIf(
72 : ! subImage->isMasked() && ! subImage->hasPixelMask(),
73 : "Selected region of image has no mask"
74 : );
75 0 : casacore::Array<casacore::Bool> mymask(subImage->shape(), true);
76 0 : if (subImage->isMasked()) {
77 0 : mymask = mymask && subImage->getMask();
78 : }
79 0 : if (subImage->hasPixelMask()) {
80 0 : mymask && subImage->pixelMask().get();
81 : }
82 0 : ThrowIf(
83 : allTrue(mymask),
84 : "Mask for selected region has no bad pixels"
85 : );
86 0 : casacore::Block<casacore::LatticeExprNode> temps;
87 0 : casacore::Record tempRegions;
88 0 : casacore::PtrBlock<const casacore::ImageRegion*> tempRegs;
89 0 : _makeRegionBlock(tempRegs, tempRegions);
90 0 : casacore::LatticeExprNode node = casacore::ImageExprParse::command(expr, temps, tempRegs);
91 : // Delete the ImageRegions (by using an empty casacore::Record).
92 0 : _makeRegionBlock(tempRegs, casacore::Record());
93 : // Create the LEL expression we need. It's like replace(lattice, pixels)
94 : // where pixels is an expression itself.
95 0 : casacore::LatticeExprNode node2 = casacore::replace(*subImage, node);
96 : // Do it
97 0 : subImage->copyData(casacore::LatticeExpr<T> (node2));
98 :
99 : // Update the mask if desired
100 0 : if (updateMask) {
101 0 : casacore::Lattice<casacore::Bool>& mask = subImage->pixelMask();
102 0 : casacore::LatticeExprNode node(iif(!mask, true, mask));
103 0 : casacore::LatticeExpr<casacore::Bool> expr(node);
104 0 : mask.copyData(expr);
105 0 : }
106 0 : this->addHistory(lor, "Replaced values of masked pixels by " + expr);
107 0 : }
108 :
109 0 : template <class T>void ImageMaskedPixelReplacer<T>::_makeRegionBlock(
110 : casacore::PtrBlock<const casacore::ImageRegion*>& imageRegions,
111 : const casacore::Record& regions
112 : ) {
113 0 : for (casacore::uInt j = 0; j < imageRegions.nelements(); j++) {
114 0 : delete imageRegions[j];
115 : }
116 0 : imageRegions.resize(0, true, true);
117 0 : casacore::uInt nreg = regions.nfields();
118 0 : if (nreg > 0) {
119 0 : imageRegions.resize(nreg);
120 0 : imageRegions.set(static_cast<casacore::ImageRegion*> (0));
121 0 : for (casacore::uInt i = 0; i < nreg; i++) {
122 0 : imageRegions[i] = casacore::ImageRegion::fromRecord(regions.asRecord(i), "");
123 : }
124 : }
125 0 : }
126 :
127 : template <class T>
128 0 : String ImageMaskedPixelReplacer<T>::getClass() const {
129 0 : return _class;
130 : }
131 :
132 : }
133 :
134 :
|