casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ComponentListImage.h
Go to the documentation of this file.
1 //# ComponentListImage.h
2 //# Copyright (C) 1994,1995,1996,1997,1998,1999,2000,2001,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 //# $Id$
27 
28 #ifndef IMAGES_COMPONENTlISTIMAGE_H
29 #define IMAGES_COMPONENTlISTIMAGE_H
30 
31 
32 //# Includes
33 #include <casacore/casa/aips.h>
40 
42 
43 #include <set>
44 
45 namespace casa {
46 
47 // <summary>
48 // Read, store, and manipulate an astronomical image based on a component list.
49 // </summary>
50 
51 // <prerequisite>
52 // <li> <linkto class=CoordinateSystem>CoordinateSystem</linkto>
53 // <li> <linkto class=ImageInterface>ImageInterface</linkto>
54 // <li> <linkto class=Lattice>Lattice</linkto>
55 // <li> <linkto class=LatticeIterator>LatticeIterator</linkto>
56 // <li> <linkto class=LatticeNavigator>LatticeNavigator</linkto>
57 // <li> <linkto class=ImageRegion>ImageRegion</linkto>
58 // </prerequisite>
59 
60 // <etymology>
61 // An Image comprised of a ComponentList.
62 // </etymology>
63 
64 // <synopsis>
65 // A ComponentListImage is an image based on a ComponentList table. Upon construction,
66 // the pixel values are not known, and are computed on the fly when doGetSlice() is called.
67 //
68 // AXES
69 // A ComponetListImage must have 2, 3, or 4 axes. At least two of these axes must be associated
70 // with a direction coordinate. Optionally, a ComponentListImage can also have a frequency and/or
71 // polarization axis. The maximum length of the polarization axis is four pixels, and the
72 // associated polarization values are constrained to be in the set of stokes parameters I, Q, U,
73 // and/or V.
74 //
75 // UNITS
76 // A ComponentListImage must have units of Jy/pixel, and setUnit() will thrown an exception if
77 // directed to set the brightness unit to something other than this.
78 //
79 // BEAMS
80 // ComponentListImages do not support beams, and setImageInfo() will throw an exception if beam
81 // information is included in the passed object. One can of course create another type of image
82 // using the pixel values of a ComponentListImage and modify metadata of the
83 // non-ComponentListImage to their heart's content.
84 //
85 // CACHING PIXEL VALUES
86 // The pixel values can be cached for the life of the object for fast retrieval if they
87 // are needed again. In this case, a TemporaryImage is created to hold the pixel values,
88 // The associated mask of this TempImage indicates if the corresponding pixel values have
89 // been computed or not; pixel values are computed as needed. In addition, the world
90 // coordinate values of all the direction and frequency pixels in the image are cached if
91 // caching is requested, as these computations can be expensive.
92 //
93 // CACHING OF POINT SOURCE PIXEL POSITIONS
94 // Computing samples for point source sky components on a pixel by pixel basis can be expensive
95 // (cf CAS-5688), and in comparison, caching their values for later use takes relatively little
96 // memory (I estimate 16 bytes * nchan * nstokes per point source). For
97 // this reason, if the associated component list contains point sources, the pixel coordinates
98 // of all the point sources are computed in a highly optimized way on the first call to doGetSlice()
99 // and are cached for later use; computing their values on a pixel by pixel basis is not done. Pixel
100 // coordinates for point sources which fall outside the image boundaries are not cached.
101 //
102 // MODIFYING PIXEL VALUES
103 // Pixel values are computed from sky components; one cannot set pixel values explicitly. Any method
104 // that ultimately calls doPutSlice will throw an exception. Note however, that masks can be added,
105 // removed, and modified in the usual ways.
106 // </synopsis>
107 
108 // <example>
109 
110 // </example>
111 
112 // <motivation>
113 // Kumar Golap is the motivation. See CAS-5837 and CAS-7309. The idea being that an image which can
114 // be represented by sky components takes a small fraction of the space to store compared to a
115 // PagedImage with identical pixel values.
116 // </motivation>
117 
118 class ComponentListImage: public casacore::ImageInterface<casacore::Float> {
119 public:
120 
122 
123  // <group>
124  // Create a new ComponentListImage from a component list, coordinate system, and shape.
125  // The input component list will be copied (via the ComponentList::copy() method, so
126  // that the constructed object has a unique ComponentList that is not referenced by
127  // any other objects. Exceptions are thrown if the shape has fewer than 2 or more than
128  // 4 elements, the number of pixel axes in csys is not equal to the number of elements
129  // in shape, or if csys does not have a direction coordinate. The brightness units are
130  // automatically set to Jy/pixel.
131 
132  // This constructor creates a persistent ComponentListImage of name tableName.
133  // If tableName is empty, an exception is thrown.
135  const ComponentList& compList, const casacore::CoordinateSystem& csys,
136  const casacore::IPosition& shape, const casacore::String& tableName,
137  const casacore::Bool doCache=casacore::True
138  );
139 
140  // This constructor creates a temporary ComponentListImage.
142  const ComponentList& compList, const casacore::CoordinateSystem& csys,
144  );
145  // </group>
146 
147  // Construct an object by reading a persistent ComponentListImage from disk.
148  // By default the image's default mask is used if it exists.
149  explicit ComponentListImage(
150  const casacore::String& filename,
151  const casacore::Bool doCache=casacore::True,
153  );
154 
155  // Copy constructor uses reference semantics.
157 
159 
160  // Assignment operator using reference semantics.
162 
163  // <group>
164  // These override the methods in Lattice and always throw an exception because
165  // there is not a general way to do this for a component list.
166  void apply (casacore::Float (*function)(casacore::Float));
167  void apply (casacore::Float (*function)(const casacore::Float&));
169  // </group>
170 
171  // Create an ImageInterface clone using reference semantics.
173 
174  // get a const reference to the underlying ComponentList
175  const ComponentList& componentList() const;
176 
178  casacore::Array<casacore::Bool>& buffer, const casacore::Slicer& section
179  );
180 
181  // This method overrides that in Lattice and always throw exceptions as there is not
182  // general way to do this for component lists.
184 
185  bool doGetSlice(
186  casacore::Array<casacore::Float>& buffer, const casacore::Slicer& section
187  );
188 
189  // This method overrides that in Lattice and always throws an exception as there is not
190  // general way to do this for component lists.
191  void doPutSlice (
192  const casacore::Array<casacore::Float>& buffer,
193  const casacore::IPosition& where, const casacore::IPosition& stride
194  );
195 
196  // Return the internal Table object to the RegionHandler.
197  static casacore::Table& getTable (void* imagePtr, casacore::Bool writable);
198 
199  // Get a pointer the default pixelmask object used with this image.
200  // It returns nullptr if no default pixelmask is used.
201  const casacore::LatticeRegion* getRegionPtr() const;
202 
204 
205  casacore::String imageType() const;
206 
207  casacore::Bool isMasked() const;
208 
209  // returns True if there is a persistent table associated with this object.
210  casacore::Bool isPaged() const;
211 
212  // returns True if there is a persistent table associated with this object.
214 
215  // Overrides the LatticeBase method. The method refers to the Lattice
216  // characteristic and therefore always returns False.
217  casacore::Bool isWritable() const;
218 
219  // If there is no persistent table associated with this object, returns
220  // "Temporary ComponentListImage". Returns the name of the table otherwise.
221  casacore::String name(bool stripPath=false) const;
222 
223  casacore::Bool ok() const;
224 
225  // Open an existing ComponentListImage. This gets registered with ImageOpener. It simply calls and
226  // returns the result of new ComponentListImage(name).
228 
229  // Throws an exception is there is no pixel mask.
231 
232  // In general, clients shouldn't need to call this. This is for registering
233  // how to open a ComponentListImage with casacore ImageOpener.
234  static void registerOpenFunction();
235 
236  // overrides ImageInterface method
237  void removeRegion(
238  const casacore::String& name,
240  casacore::Bool throwIfUnknown=casacore::True
241  );
242 
243  void resize(const casacore::TiledShape& newShape);
244 
245  // Overrides Lattice method. Always throws exception; there is no way to
246  // represent with a single component a source with constant intensity everywhere.
247  void set(const casacore::Float& value);
248 
249  // controls if pixel values, directions, and frequencies are cached. If doCache is false,
250  // existing cache values are destroyed if they exist.
251  void setCache(casacore::Bool doCache);
252 
253  // Set the coordinate system. Flush the new coordinate system to disk if
254  // the table exists is writable.
256 
257  void setDefaultMask(const casacore::String& regionName);
258 
260 
262 
263  // An exception is thrown if newUnits are anything but Jy/pixel.
264  casacore::Bool setUnits (const casacore::Unit& newUnits);
265 
266  // return the shape of the image.
267  casacore::IPosition shape() const;
268 
269  // Overrides ImageInterface method.
271 
272 protected:
273  //Overrides Lattice. Always throws exception; arbitrary math cannot be represented with components.
274  void handleMath(const casacore::Lattice<casacore::Float>& from, int oper);
275 
276 private:
277 
278  enum PtFound {
282  };
283 
285 
287  std::shared_ptr<casacore::LatticeRegion> _mask = nullptr;
294  // yes it really does need to be initialized here (or in the constructor)
295  // because initializing to nullptr will have undesirable effects when copying.
296  // The copied image will initialize this, but since it is a nullptr in the original,
297  // it doesn't get initialized in the original. Trust me, you don't want to deal
298  // with this issue.
299  using PtStoreType = std::map<
300  std::pair<casacore::uInt, casacore::uInt>,
302  >;
303  std::shared_ptr<PtStoreType> _ptSourcePixelVals = std::shared_ptr<PtStoreType>(
304  new PtStoreType()
305  );
310  // Yes this really has to be a shared_ptr so that _ptSourcePixelVals can be computed
311  // outside the constructor and copying the image will work properly
312  std::shared_ptr<casacore::Bool> _computedPtSources = std::shared_ptr<casacore::Bool>(
314  );
316  std::shared_ptr<casacore::TempImage<casacore::Float>> _valueCache = nullptr;
317 
318  void _applyMask (const casacore::String& maskName);
319 
320  void _applyMaskSpecifier (const casacore::MaskSpecifier& spec);
321 
323 
324  // improve performance by caching point source pixel values. This is always done;
325  // the value of _cache has no effect on it.
327 
328  void _deleteCache();
329 
330  void _fillBuffer(
331  casacore::Array<casacore::Float>& buffer, const casacore::IPosition& chunkShape,
332  const casacore::IPosition& secStart, casacore::uInt nFreqs,
333  const casacore::Cube<casacore::Double>& pixelVals
334  ) const;
335 
337  casacore::Cube<casacore::Double>& values, const casacore::IPosition& pixelPosition,
338  const casacore::DirectionCoordinate& dirCoord, const SkyComponent& point,
340  ) const;
341 
344  const casacore::DirectionCoordinate& dirCoord, const SkyComponent& point,
346  ) const;
347 
348  void _findPointSoures(
349  std::vector<casacore::uInt>& foundPtSourceIdx, casacore::uInt& nInside,
350  casacore::uInt& nOutside, const std::set<casacore::uInt>& pointSourceIdx
351  );
352 
354 
355  void _getDirValsDoCache(
357  const casacore::IPosition& secStart, casacore::uInt endLong,
358  casacore::uInt endLat, const casacore::DirectionCoordinate& dirCoord
359  );
360 
361  void _getDirValsNoCache(
363  const casacore::IPosition& secStart, casacore::uInt endLong,
364  casacore::uInt endLat, const casacore::DirectionCoordinate& dirCoord
365  ) const;
366 
367  void _getFreqValsDoCache(
369  const casacore::IPosition& secStart, casacore::uInt nFreqs,
370  const casacore::SpectralCoordinate& specCoord
371  );
372 
373  void _getFreqValsNoCache(
375  const casacore::IPosition& secStart, casacore::uInt nFreqs,
376  const casacore::SpectralCoordinate& specCoord
377  ) const;
378 
379  void _initCache();
380 
381  void _openLogTable();
382 
383  void _reopenRW();
384 
385  void _resize(const casacore::TiledShape& shape);
386 
387  void _restoreAll(const casacore::TableRecord& rec);
388 
389  void _restoreImageInfo(const casacore::TableRecord& rec);
390 
391  void _restoreMiscInfo(const casacore::TableRecord& rec);
392 
393  void _restoreUnits(const casacore::TableRecord& rec);
394 
395 };
396 
397 }
398 
399 #endif
casacore::MVAngle _pixelLongSize
A Vector of integers, for indexing into Array&lt;T&gt; objects.
Definition: IPosition.h:119
casacore::IPosition shape() const
return the shape of the image.
casacore::MFrequency _defaultFreq
A 1-D Specialization of the Array class.
int Int
Definition: aipstype.h:50
Map a domain object into a range object via operator().
Definition: Functional.h:122
casacore::Vector< casacore::Int > _pixelToIQUV
void _getFreqValsDoCache(casacore::Vector< casacore::MVFrequency > &freqVals, const casacore::IPosition &secStart, casacore::uInt nFreqs, const casacore::SpectralCoordinate &specCoord)
casacore::Bool isMasked() const
Has the object really a mask? The default implementation returns True if the MaskedLattice has a regi...
Main interface class to a read/write table.
Definition: Table.h:153
static void registerOpenFunction()
In general, clients shouldn&#39;t need to call this.
PtFound _findPixel(casacore::Cube< casacore::Double > &values, const casacore::IPosition &pixelPosition, const casacore::DirectionCoordinate &dirCoord, const SkyComponent &point, const casacore::Vector< casacore::MVFrequency > &freqValues) const
void _fillBuffer(casacore::Array< casacore::Float > &buffer, const casacore::IPosition &chunkShape, const casacore::IPosition &secStart, casacore::uInt nFreqs, const casacore::Cube< casacore::Double > &pixelVals) const
void _restoreImageInfo(const casacore::TableRecord &rec)
casacore::Matrix< std::shared_ptr< casacore::MVDirection > > _dirVals
casacore::Bool setCoordinateInfo(const casacore::CoordinateSystem &coords)
Set the coordinate system.
void set(const casacore::Float &value)
Overrides Lattice method.
void _restoreUnits(const casacore::TableRecord &rec)
const ComponentList & componentList() const
get a const reference to the underlying ComponentList
std::map< std::pair< casacore::uInt, casacore::uInt >, casacore::Matrix< casacore::Float > > PtStoreType
yes it really does need to be initialized here (or in the constructor) because initializing to nullpt...
casacore::Bool isPersistent() const
returns True if there is a persistent table associated with this object.
A non-templated, abstract base class for array-like objects.
Definition: LatticeBase.h:80
void _applyMaskSpecifier(const casacore::MaskSpecifier &spec)
Read, store, and manipulate an astronomical image based on a component list.
void _resize(const casacore::TiledShape &shape)
void copyData(const casacore::Lattice< casacore::Float > &)
This method overrides that in Lattice and always throw exceptions as there is not general way to do t...
casacore::Bool isPaged() const
returns True if there is a persistent table associated with this object.
A 2-D Specialization of the Array class.
casacore::Bool setUnits(const casacore::Unit &newUnits)
An exception is thrown if newUnits are anything but Jy/pixel.
ComponentListImage & operator=(const ComponentListImage &other)
Assignment operator using reference semantics.
bool doGetSlice(casacore::Array< casacore::Float > &buffer, const casacore::Slicer &section)
The functions (in the derived classes) doing the actual work.
void doPutSlice(const casacore::Array< casacore::Float > &buffer, const casacore::IPosition &where, const casacore::IPosition &stride)
This method overrides that in Lattice and always throws an exception as there is not general way to d...
void removeRegion(const casacore::String &name, casacore::RegionHandler::GroupType=casacore::RegionHandler::Any, casacore::Bool throwIfUnknown=casacore::True)
overrides ImageInterface method
defines physical units
Definition: Unit.h:189
A Measure: wave characteristics.
Definition: MFrequency.h:161
std::shared_ptr< casacore::LatticeRegion > _mask
Define the shape and tile shape.
Definition: TiledShape.h:99
void _restoreMiscInfo(const casacore::TableRecord &rec)
casacore::IPosition _shape
casacore::Bool ok() const
Check class invariants.
A base class for astronomical images.
casacore::Bool setMiscInfo(const casacore::RecordInterface &newInfo)
const casacore::Lattice< casacore::Bool > & pixelMask() const
Throws an exception is there is no pixel mask.
Interconvert pixel positions and directions (e.g. RA/DEC).
Class to specify which mask to use in an image.
Definition: MaskSpecifier.h:69
void _restoreAll(const casacore::TableRecord &rec)
casacore::String name(bool stripPath=false) const
If there is no persistent table associated with this object, returns &quot;Temporary ComponentListImage&quot;.
ComponentListImage(const ComponentList &compList, const casacore::CoordinateSystem &csys, const casacore::IPosition &shape, const casacore::String &tableName, const casacore::Bool doCache=casacore::True)
Create a new ComponentListImage from a component list, coordinate system, and shape.
static casacore::LatticeBase * openCLImage(const casacore::String &name, const casacore::MaskSpecifier &)
Open an existing ComponentListImage.
casacore::MeasRef< casacore::MDirection > _dirRef
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
casacore::Bool doGetMaskSlice(casacore::Array< casacore::Bool > &buffer, const casacore::Slicer &section)
The function (in the derived classes) doing the actual work.
void setCache(casacore::Bool doCache)
controls if pixel values, directions, and frequencies are cached.
casacore::Vector< std::shared_ptr< casacore::MVFrequency > > _freqVals
void _cacheCoordinateInfo(const casacore::CoordinateSystem &csys)
casacore::String imageType() const
Get the image type (returns name of derived class).
float Float
Definition: aipstype.h:54
static casacore::Table & getTable(void *imagePtr, casacore::Bool writable)
Return the internal Table object to the RegionHandler.
std::shared_ptr< casacore::Bool > _computedPtSources
Yes this really has to be a shared_ptr so that _ptSourcePixelVals can be computed outside the constru...
void resize(const casacore::TiledShape &newShape)
Function which changes the shape of the image (N.B.
void _applyMask(const casacore::String &maskName)
const Bool False
Definition: aipstype.h:44
static const casacore::String IMAGE_TYPE
A hierarchical collection of named fields of various types.
Definition: TableRecord.h:182
PtFound _findPixelIn3x3Box(casacore::IPosition &pixelPosition, casacore::Cube< casacore::Double > &values, const casacore::DirectionCoordinate &dirCoord, const SkyComponent &point, const casacore::Vector< casacore::MVFrequency > &freqValues) const
std::shared_ptr< PtStoreType > _ptSourcePixelVals
Specify which elements to extract from an n-dimensional array.
Definition: Slicer.h:289
Interconvert pixel and frequency values.
casacore::Bool hasPixelMask() const
Does the lattice have a pixelmask? The default implementation returns False.
casacore::Bool setImageInfo(const casacore::ImageInfo &info)
void _getDirValsNoCache(casacore::Vector< casacore::MVDirection > &dirVals, const casacore::IPosition &secStart, casacore::uInt endLong, casacore::uInt endLat, const casacore::DirectionCoordinate &dirCoord) const
void _getDirValsDoCache(casacore::Vector< casacore::MVDirection > &dirVals, const casacore::IPosition &secStart, casacore::uInt endLong, casacore::uInt endLat, const casacore::DirectionCoordinate &dirCoord)
A class for manipulating groups of components.
void _findPointSoures(std::vector< casacore::uInt > &foundPtSourceIdx, casacore::uInt &nInside, casacore::uInt &nOutside, const std::set< casacore::uInt > &pointSourceIdx)
A component of a model of the sky.
Definition: SkyComponent.h:130
void apply(casacore::Float(*function)(casacore::Float))
These override the methods in Lattice and always throw an exception because there is not a general wa...
std::shared_ptr< casacore::TempImage< casacore::Float > > _valueCache
String: the storage and methods of handling collections of characters.
Definition: String.h:223
casacore::MVAngle _pixelLatSize
An optionally strided region in a Lattice.
Definition: LatticeRegion.h:74
void setDefaultMask(const casacore::String &regionName)
Set the default pixelmask to the mask with the given name (which has to exist in the &quot;masks&quot; group)...
casacore::ImageInterface< casacore::Float > * cloneII() const
Create an ImageInterface clone using reference semantics.
void _computePointSourcePixelValues()
improve performance by caching point source pixel values.
casacore::Vector< casacore::MVFrequency > _getAllFreqValues(casacore::uInt nFreqs)
Abstract base class for Record classes.
void _getFreqValsNoCache(casacore::Vector< casacore::MVFrequency > &freqVals, const casacore::IPosition &secStart, casacore::uInt nFreqs, const casacore::SpectralCoordinate &specCoord) const
void useMask(casacore::MaskSpecifier=casacore::MaskSpecifier())
Overrides ImageInterface method.
casacore::Bool isWritable() const
Overrides the LatticeBase method.
Miscellaneous information related to an image.
Definition: ImageInfo.h:92
Class to handle angle type conversions and I/O.
Definition: MVAngle.h:245
void handleMath(const casacore::Lattice< casacore::Float > &from, int oper)
Overrides Lattice.
const Bool True
Definition: aipstype.h:43
GroupType
Define the possible group types (regions or masks).
Definition: RegionHandler.h:99
Interconvert pixel and world coordinates.
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
unsigned int uInt
Definition: aipstype.h:51
const casacore::LatticeRegion * getRegionPtr() const
Get a pointer the default pixelmask object used with this image.
casacore::MeasRef< casacore::MFrequency > _freqRef