casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImageRegrid.h
Go to the documentation of this file.
1 //# ImageRegrid.h: Regrid Images
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: aips2-request@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 //#
27 //# $Id$
28 
29 #ifndef IMAGES_IMAGEREGRID_H
30 #define IMAGES_IMAGEREGRID_H
31 
32 #include <casacore/casa/aips.h>
39 #include <set>
40 
41 namespace casacore { //# NAMESPACE CASACORE - BEGIN
42 
43 template<class T> class MaskedLattice;
44 template<class T> class ImageInterface;
45 template<class T> class Lattice;
46 template<class T> class LatticeIterator;
47 template<class T> class Vector;
48 
49 class CoordinateSystem;
50 class DirectionCoordinate;
51 class Coordinate;
52 class ObsInfo;
53 class IPosition;
54 class Unit;
55 class ProgressMeter;
56 
57 // <summary>This regrids one image to match the coordinate system of another</summary>
58 
59 // <use visibility=export>
60 
61 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
62 // </reviewed>
63 
64 // <prerequisite>
65 // <li> <linkto class="ImageInterface">ImageInterface</linkto>
66 // <li> <linkto class="CoordinateSystem">CoordinateSystem</linkto>
67 // <li> <linkto class="Interpolate2D">Interpolate2D</linkto>
68 // <li> <linkto class="InterpolateArray1D">InterpolateArray1D</linkto>
69 // </prerequisite>
70 //
71 // <etymology>
72 // Regrids, or resamples, images.
73 // </etymology>
74 //
75 // <synopsis>
76 // This class enables you to regrid one image to the coordinate
77 // system of another. You can regrid any or all of the
78 // axes in the image. A range of interpolation schemes are available.
79 //
80 // It will cope with coordinate systems being in different orders
81 // (coordinate, world axes, pixel axes). The basic approach is to
82 // make a mapping from the input to the output coordinate systems,
83 // but the output CoordinateSystem order is preserved in the output
84 // image.
85 //
86 // Any DirectionCoordinate or LinearCoordinate holding exactly two axes
87 // is regridded in one pass with a 2-D interpolation scheme.
88 // All other axes are regridded in separate passes with a 1D interpolation
89 // scheme. This means that a LinearCoordinate holding say 3 axes
90 // where some of them are coupled will not be correctly regridded.
91 // StokesCoordinates cannot be regridded.
92 //
93 // Multiple passes are made through the data, and the output of
94 // each pass is the input of the next pass. The intermediate
95 // images are stored as TempImages which may be in memory or
96 // on disk, depending on their size.
97 //
98 // It can also simply insert this image into that one via
99 // an integer shift.
100 // </synopsis>
101 //
102 // <example>
103 //
104 // <srcblock>
105 // </srcblock>
106 // </example>
107 //
108 // <motivation>
109 // A common image analysis need is to regrid images, e.g. to compare
110 // images from different telescopes.
111 // </motivation>
112 //
113 // <thrown>
114 // <li> AipsError
115 // </thrown>
116 //
117 // <todo asof="1999/04/20">
118 // </todo>
119 
120 template <class T> class ImageRegrid
121 {
122 public:
123 
124  // Default constructor
125  ImageRegrid();
126 
127  // copy constructor (copy semantics)
128  ImageRegrid(const ImageRegrid &other);
129 
130  // destructor
131  ~ImageRegrid();
132 
133  // Assignment copy semantics)
134  ImageRegrid<T>& operator=(const ImageRegrid& other);
135 
136  // Regrid inImage onto the grid specified by outImage.
137  // If outImage has a writable mask, it will be updated in that
138  // output pixels at which the regridding failed will be masked bad (False)
139  // and the pixel value set to zero. Otherwise the output mask is not changed.
140  // Specify which pixel axes of outImage are to be
141  // regridded. The coordinate and axis order of outImage
142  // is preserved, regardless of where the relevant coordinates
143  // are in inImage.
144  //
145  // decimate only applies when replicate=False. it is
146  // the coordinate grid computation decimation FACTOR
147  // (e.g. nCoordGrid ~ nIn / decimate). 0 means no decimation
148  // (slowest and most accurate)
149  void regrid(ImageInterface<T>& outImage,
150  typename Interpolate2D::Method method,
151  const IPosition& whichOutPixelAxes,
152  const ImageInterface<T>& inImage,
153  Bool replicate=False, uInt decimate=0,
154  Bool showProgress=False, Bool forceRegrid=False,
155  Bool verbose=False);
156 
157 // Get and set the 2-D coordinate grid. After a call to function <src>regrid</src>
158 // in which coupled 2D coordinate (presently only DirectionCoordinate) is
159 // regridded, this coordinate grid will be available. It can be reused
160 // via the <src>set2DCoordinateGrid</src> function for another like plane
161 // (e.g. if you choose to regrid planes of a cube separately). When you provide
162 // the coordinate grid, it will no longer (for that 2D coordinate only) be
163 // computed internally, which may save a lot of time. Ordinarily, if you
164 // regridded many planes of a cube in one call to regrid, the coordinate grid
165 // is cached for you. To trigger successive calls to regrid to go back to
166 // internal computation, set zero length Cube and Matrix. <src>gridMask</src>
167 // is True for successfull coordinate conversions, and False otherwise.
168 // <group>
169  void get2DCoordinateGrid (Cube<Double>& grid, Matrix<Bool>& gridMask) const;
170  void set2DCoordinateGrid (const Cube<Double>& grid, const Matrix<Bool>& gridMask, Bool notify=False);
171 // </group>
172 //
173  // Inserts inImage into outImage. The alignment is done by
174  // placing the blc of inImage at the specified
175  // absolute pixel of the outImage (outPixelLocation). If
176  // the outPixelLocation vector is of zero length, then the images
177  // are aligned by their reference pixels. Only integral shifts are done
178  // in the aligment process. If outImage has a mask, it will be updated.
179  // Returns False if no overlap of images, in which case the
180  // output is not updated.
181  Bool insert(ImageInterface<T>& outImage,
182  const Vector<Double>& outPixelLocation,
183  const ImageInterface<T>& inImage);
184 
185  // Print out useful debugging information (level 0 is none,
186  // 1 is some, 2 is too much)
187  void showDebugInfo(Int level=0) {itsShowLevel = level;};
188 
189  // Enable/disable Measures Reference conversions
191 
192  // Helper function. We are regridding from cSysFrom to cSysTo for the
193  // specified pixel axes of cSyFrom. This function returns a CoordinateSystem which,
194  // for the pixel axes being regridded, copies the coordinates from cSysTo
195  // (if coordinate type present in cSysTo) or cSysFrom (coordinate
196  // type not present in cSysTo).
197  // For the axes not being regridded, it copies the coordinates from
198  // cSysFrom. This helps you build the cSys for function regrid.
199  // The ObsInfo from cSysFrom is copied to the output CoordinateSystem.
200  // If inShape has one or more elements it represenents the size of the
201  // image to be regridded. It this must have the same number of elements
202  // as the number of pixel axes in <src>cSysFrom</src>. If any of the values
203  // are unity (ie the axes are degenerate), and the corresponding axis in <src>csysFrom</src> is the only
204  // axis in its corresponding coordinate, this coordinate will not be replaced
205  // even if the axis is specified in <src>axes</src>.
206  // Upon return, <src>coordsToBeRegridded</src> will contain a list of the coordinates that will
207  // be regridded.
209  LogIO& os,
210  std::set<Coordinate::Type>& coordsToBeRegridded,
211  const CoordinateSystem& cSysTo,
212  const CoordinateSystem& cSysFrom,
213  const IPosition& axes,
214  const IPosition& inShape=IPosition(),
215  Bool giveStokesWarning=True
216  );
217 
218  private:
219 
222 //
225 //
229 //
230  // Check shape and axes. Exception if no good. If pixelAxes
231  // of length 0, set to all axes according to shape
232  void _checkAxes(IPosition& outPixelAxes,
233  const IPosition& inShape,
234  const IPosition& outShape,
235  const Vector<Int>& pixelAxisMap,
236  const CoordinateSystem& outCoords,
237  Bool verbose);
238 
239  // Find maps between coordinate systems
240  void findMaps (uInt nDim,
241  Vector<Int>& pixelAxisMap1,
242  Vector<Int>& pixelAxisMap2,
243  const CoordinateSystem& inCoords,
244  const CoordinateSystem& outCoords) const;
245 
246  // Find scale factor to conserve flux
247  Double findScaleFactor(const Unit& units,
248  const CoordinateSystem& inCoords,
249  const CoordinateSystem& outCoords,
250  Int inCoordinate, Int outCoordinate,
251  LogIO& os) const;
252 
253  // Regrid one Coordinate
254  void _regridOneCoordinate (LogIO& os, IPosition& outShape2,
255  Vector<Bool>& doneOutPixelAxes,
256  MaskedLattice<T>* &finalOutPtr,
257  MaskedLattice<T>* &inPtr,
258  MaskedLattice<T>* &outPtr,
259  CoordinateSystem& outCoords,
260  const CoordinateSystem& inCoords,
261  Int outPixelAxis,
262  const ImageInterface<T>& inImage,
263  const IPosition& outShape,
264  Bool replicate, uInt decimate,
265  Bool outIsMasked, Bool showProgress,
266  Bool forceRegrid,
267  typename Interpolate2D::Method method,
268  Bool verbose);
269 
270  // Regrid DirectionCoordinate or 2-axis LinearCoordinate
271  void regridTwoAxisCoordinate (LogIO& os, MaskedLattice<T>& outLattice,
272  const MaskedLattice<T>& inLattice,
273  const Unit& imageUnit,
274  const CoordinateSystem& inCoords,
275  const CoordinateSystem& outCoords,
276  Int inCoordinate, Int outCoordinate,
277  const Vector<Int> inPixelAxes,
278  const Vector<Int> outPixelAxes,
279  const Vector<Int> pixelAxisMap1,
280  const Vector<Int> pixelAxisMap2,
281  typename Interpolate2D::Method method,
282  Bool replicate, uInt decimate,
283  Bool showProgress);
284 
285  // Make regridding coordinate grid for this cursor.
286  void make2DCoordinateGrid (LogIO& os, Bool& allFail, Bool&missedIt,
287  Double& minInX, Double& minInY,
288  Double& maxInX, Double& maxInY,
289  Cube<Double>& in2DPos,
290  Matrix<Bool>& succeed,
291  const CoordinateSystem& inCoords,
292  const CoordinateSystem& outCoords,
293  Int inCoordinate, Int outCoordinate,
294  uInt xInAxis, uInt yInAxis,
295  uInt xOutAxis, uInt yOutAxis,
296  const IPosition& inPixelAxes,
297  const IPosition& outPixelAxes,
298  const IPosition& inShape,
299  const IPosition& outPos,
300  const IPosition& cursorShape,
301  uInt decimate=0);
302 
303  // Make replication coordinate grid for this cursor
304  void make2DCoordinateGrid (Cube<Double>& in2DPos,
305  Double& minInX, Double& minInY,
306  Double& maxInX, Double& maxInY,
307  const Vector<Double>& pixelScale,
308  uInt xInAxis, uInt yInAxis,
309  uInt xOutAxis, uInt yOutAxis,
310  uInt xInCorrAxis, uInt yInCorrAxis,
311  uInt xOutCorrAxis, uInt yOutCorrAxis,
312  const IPosition& outPos, const IPosition& cursorShape);
313 
314  // Make regridding coordinate grid for this axis
316  Vector<Bool>& failed,
317  Bool& allFailed,
318  Bool& allGood,
319  const Coordinate& inCoord,
320  const Coordinate& outCoord,
321  Int inAxisInCoordinate,
322  Int outAxisInCoordinate,
323  MFrequency::Convert& machine,
324  Bool useMachine);
325 
326 
327  // Make replication coordinate grid for this axis
329  typename NumericTraits<T>::BaseType pixelScale) const;
330 
331  // Regrid 1 axis
332  void regrid1D (MaskedLattice<T>& outLattice,
333  const MaskedLattice<T>& inLattice,
334  const Coordinate& inCoord,
335  const Coordinate& outCoord,
336  const Vector<Int>& inPixelAxes,
337  const Vector<Int>& outPixelAxes,
338  Int inAxisInCoordinate,
339  Int outAxisInCoordinate,
340  const Vector<Int> pixelAxisMap,
341  typename Interpolate2D::Method method,
342  MFrequency::Convert& machine,
343  Bool replicate,
344  Bool useMachine, Bool showProgress);
345 
346 //
347  void regrid2DMatrix(Lattice<T>& outCursor,
348  LatticeIterator<Bool>*& outMaskIterPtr,
349  const Interpolate2D& interp,
350  ProgressMeter*& pProgress,
351  Double& iPix,
352  uInt nDim,
353  uInt xInAxis, uInt yInAxis,
354  uInt xOutAxis, uInt yOutAxis,
355  Double scale,
356  Bool inIsMasked, Bool outIsMasked,
357  const IPosition& outPos,
358  const IPosition& outCursorShape,
359  const IPosition& inChunkShape,
360  const IPosition& inChunkBlc,
361  const IPosition& pixelAxisMap2,
362  Array<T>& inDataChunk,
363  Array<Bool>*& inMaskChunkPtr,
364  const Cube<Double>& pix2DPos,
365  const Matrix<Bool>& succeed);
366 
367  void findXYExtent (Bool& missedIt, Bool& allFailed,
368  Double& minInX, Double& minInY,
369  Double& maxInX, Double& maxInY,
370  Cube<Double>& in2DPos,
371  const Matrix<Bool>& succeed,
372  uInt xInAxis, uInt yInAxis,
373  uInt xOutAxis, uInt yOutAxis,
374  const IPosition& outPos,
375  const IPosition& outCursorShape,
376  const IPosition& inShape);
377 //
378  Bool minmax(Double &minX, Double &maxX, Double& minY, Double& maxY,
379  const Array<Double> &xData,
380  const Array<Double> &yData,
381  const Array<Bool>& mask);
382 };
383 
384 //# Declare extern templates for often used types.
385  extern template class ImageRegrid<Float>;
386 
387 } //# NAMESPACE CASACORE - END
388 
389 #ifndef CASACORE_NO_AUTO_TEMPLATES
390 #include <casacore/images/Images/ImageRegrid.tcc>
391 #endif //# CASACORE_NO_AUTO_TEMPLATES
392 #endif
393 
A Vector of integers, for indexing into Array&lt;T&gt; objects.
Definition: IPosition.h:119
Matrix< Bool > its2DCoordinateGridMask
Definition: ImageRegrid.h:224
int Int
Definition: aipstype.h:50
std::vector< double > Vector
Definition: ds9context.h:24
A read/write lattice iterator.
void make1DCoordinateGrid(Block< typename NumericTraits< T >::BaseType > &xOut, Vector< Bool > &failed, Bool &allFailed, Bool &allGood, const Coordinate &inCoord, const Coordinate &outCoord, Int inAxisInCoordinate, Int outAxisInCoordinate, MFrequency::Convert &machine, Bool useMachine)
Make regridding coordinate grid for this axis.
LatticeExprNode mask(const LatticeExprNode &expr)
This function returns the mask of the given expression.
void set2DCoordinateGrid(const Cube< Double > &grid, const Matrix< Bool > &gridMask, Bool notify=False)
Bool insert(ImageInterface< T > &outImage, const Vector< Double > &outPixelLocation, const ImageInterface< T > &inImage)
Inserts inImage into outImage.
A templated, abstract base class for array-like objects with masks.
ostream-like interface to creating log messages.
Definition: LogIO.h:167
void regrid2DMatrix(Lattice< T > &outCursor, LatticeIterator< Bool > *&outMaskIterPtr, const Interpolate2D &interp, ProgressMeter *&pProgress, Double &iPix, uInt nDim, uInt xInAxis, uInt yInAxis, uInt xOutAxis, uInt yOutAxis, Double scale, Bool inIsMasked, Bool outIsMasked, const IPosition &outPos, const IPosition &outCursorShape, const IPosition &inChunkShape, const IPosition &inChunkBlc, const IPosition &pixelAxisMap2, Array< T > &inDataChunk, Array< Bool > *&inMaskChunkPtr, const Cube< Double > &pix2DPos, const Matrix< Bool > &succeed)
Matrix< Bool > itsUser2DCoordinateGridMask
Definition: ImageRegrid.h:227
defines physical units
Definition: Unit.h:189
ImageRegrid< T > & operator=(const ImageRegrid &other)
Assignment copy semantics)
Cube< Double > its2DCoordinateGrid
Definition: ImageRegrid.h:223
A templated, abstract base class for array-like objects.
A base class for astronomical images.
Interface for converting between world and pixel coordinates.
Definition: Coordinate.h:139
void make2DCoordinateGrid(LogIO &os, Bool &allFail, Bool &missedIt, Double &minInX, Double &minInY, Double &maxInX, Double &maxInY, Cube< Double > &in2DPos, Matrix< Bool > &succeed, const CoordinateSystem &inCoords, const CoordinateSystem &outCoords, Int inCoordinate, Int outCoordinate, uInt xInAxis, uInt yInAxis, uInt xOutAxis, uInt yOutAxis, const IPosition &inPixelAxes, const IPosition &outPixelAxes, const IPosition &inShape, const IPosition &outPos, const IPosition &cursorShape, uInt decimate=0)
Make regridding coordinate grid for this cursor.
void findMaps(uInt nDim, Vector< Int > &pixelAxisMap1, Vector< Int > &pixelAxisMap2, const CoordinateSystem &inCoords, const CoordinateSystem &outCoords) const
Find maps between coordinate systems.
void get2DCoordinateGrid(Cube< Double > &grid, Matrix< Bool > &gridMask) const
Get and set the 2-D coordinate grid.
double Double
Definition: aipstype.h:55
void regrid1D(MaskedLattice< T > &outLattice, const MaskedLattice< T > &inLattice, const Coordinate &inCoord, const Coordinate &outCoord, const Vector< Int > &inPixelAxes, const Vector< Int > &outPixelAxes, Int inAxisInCoordinate, Int outAxisInCoordinate, const Vector< Int > pixelAxisMap, typename Interpolate2D::Method method, MFrequency::Convert &machine, Bool replicate, Bool useMachine, Bool showProgress)
Regrid 1 axis.
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
void regrid(ImageInterface< T > &outImage, typename Interpolate2D::Method method, const IPosition &whichOutPixelAxes, const ImageInterface< T > &inImage, Bool replicate=False, uInt decimate=0, Bool showProgress=False, Bool forceRegrid=False, Bool verbose=False)
Regrid inImage onto the grid specified by outImage.
Bool minmax(Double &minX, Double &maxX, Double &minY, Double &maxY, const Array< Double > &xData, const Array< Double > &yData, const Array< Bool > &mask)
void showDebugInfo(Int level=0)
Print out useful debugging information (level 0 is none, 1 is some, 2 is too much) ...
Definition: ImageRegrid.h:187
This regrids one image to match the coordinate system of another.
Definition: ImageRegrid.h:120
Visual indication of a tasks progress.
Definition: ProgressMeter.h:91
~ImageRegrid()
destructor
void regridTwoAxisCoordinate(LogIO &os, MaskedLattice< T > &outLattice, const MaskedLattice< T > &inLattice, const Unit &imageUnit, const CoordinateSystem &inCoords, const CoordinateSystem &outCoords, Int inCoordinate, Int outCoordinate, const Vector< Int > inPixelAxes, const Vector< Int > outPixelAxes, const Vector< Int > pixelAxisMap1, const Vector< Int > pixelAxisMap2, typename Interpolate2D::Method method, Bool replicate, uInt decimate, Bool showProgress)
Regrid DirectionCoordinate or 2-axis LinearCoordinate.
const Bool False
Definition: aipstype.h:44
template &lt;class T, class U&gt; class vector;
Definition: MSFlagger.h:37
void findXYExtent(Bool &missedIt, Bool &allFailed, Double &minInX, Double &minInY, Double &maxInX, Double &maxInY, Cube< Double > &in2DPos, const Matrix< Bool > &succeed, uInt xInAxis, uInt yInAxis, uInt xOutAxis, uInt yOutAxis, const IPosition &outPos, const IPosition &outCursorShape, const IPosition &inShape)
simple 1-D array
ImageRegrid()
Default constructor.
void disableReferenceConversions(Bool disable=True)
Enable/disable Measures Reference conversions.
Definition: ImageRegrid.h:190
void _checkAxes(IPosition &outPixelAxes, const IPosition &inShape, const IPosition &outShape, const Vector< Int > &pixelAxisMap, const CoordinateSystem &outCoords, Bool verbose)
Check shape and axes.
Cube< Double > itsUser2DCoordinateGrid
Definition: ImageRegrid.h:226
A two dimension interpolator for Matrices or Arrays.
Definition: Interpolate2D.h:97
Char BaseType
Numeric type.
void _regridOneCoordinate(LogIO &os, IPosition &outShape2, Vector< Bool > &doneOutPixelAxes, MaskedLattice< T > *&finalOutPtr, MaskedLattice< T > *&inPtr, MaskedLattice< T > *&outPtr, CoordinateSystem &outCoords, const CoordinateSystem &inCoords, Int outPixelAxis, const ImageInterface< T > &inImage, const IPosition &outShape, Bool replicate, uInt decimate, Bool outIsMasked, Bool showProgress, Bool forceRegrid, typename Interpolate2D::Method method, Bool verbose)
Regrid one Coordinate.
const Bool True
Definition: aipstype.h:43
Interconvert pixel and world coordinates.
Double findScaleFactor(const Unit &units, const CoordinateSystem &inCoords, const CoordinateSystem &outCoords, Int inCoordinate, Int outCoordinate, LogIO &os) const
Find scale factor to conserve flux.
static CoordinateSystem makeCoordinateSystem(LogIO &os, std::set< Coordinate::Type > &coordsToBeRegridded, const CoordinateSystem &cSysTo, const CoordinateSystem &cSysFrom, const IPosition &axes, const IPosition &inShape=IPosition(), Bool giveStokesWarning=True)
Helper function.
unsigned int uInt
Definition: aipstype.h:51
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42