LSSTApplications  16.0-10-g0ee56ad+5,16.0-11-ga33d1f2+5,16.0-12-g3ef5c14+3,16.0-12-g71e5ef5+18,16.0-12-gbdf3636+3,16.0-13-g118c103+3,16.0-13-g8f68b0a+3,16.0-15-gbf5c1cb+4,16.0-16-gfd17674+3,16.0-17-g7c01f5c+3,16.0-18-g0a50484+1,16.0-20-ga20f992+8,16.0-21-g0e05fd4+6,16.0-21-g15e2d33+4,16.0-22-g62d8060+4,16.0-22-g847a80f+4,16.0-25-gf00d9b8+1,16.0-28-g3990c221+4,16.0-3-gf928089+3,16.0-32-g88a4f23+5,16.0-34-gd7987ad+3,16.0-37-gc7333cb+2,16.0-4-g10fc685+2,16.0-4-g18f3627+26,16.0-4-g5f3a788+26,16.0-5-gaf5c3d7+4,16.0-5-gcc1f4bb+1,16.0-6-g3b92700+4,16.0-6-g4412fcd+3,16.0-6-g7235603+4,16.0-69-g2562ce1b+2,16.0-8-g14ebd58+4,16.0-8-g2df868b+1,16.0-8-g4cec79c+6,16.0-8-gadf6c7a+1,16.0-8-gfc7ad86,16.0-82-g59ec2a54a+1,16.0-9-g5400cdc+2,16.0-9-ge6233d7+5,master-g2880f2d8cf+3,v17.0.rc1
LSSTDataManagementBasePackage
Mask.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 /*
26  * LSST bitmasks
27  */
28 
29 #ifndef LSST_AFW_IMAGE_MASK_H
30 #define LSST_AFW_IMAGE_MASK_H
31 
32 #include <list>
33 #include <map>
34 #include <string>
35 
36 #include <memory>
37 
38 #include "lsst/base.h"
39 #include "lsst/daf/base/Citizen.h"
41 #include "lsst/pex/exceptions.h"
44 #include "lsst/afw/fitsDefaults.h"
45 
46 namespace lsst {
47 namespace afw {
48 namespace image {
49 
50 namespace detail {
51 class MaskDict; // forward declaration
52 }
53 
54 // all masks will initially be instantiated with the same pixel type
55 namespace detail {
57 struct Mask_tag : public detail::basic_tag {};
58 
60 } // namespace detail
61 
77 template <typename MaskPixelT = lsst::afw::image::MaskPixel>
78 class Mask : public ImageBase<MaskPixelT> {
79 public:
81 
83 
85  template <typename MaskPT = MaskPixelT>
88  typedef Mask<MaskPT> type;
89  };
90 
91  // Constructors
99  explicit Mask(unsigned int width, unsigned int height, MaskPlaneDict const& planeDefs = MaskPlaneDict());
108  explicit Mask(unsigned int width, unsigned int height, MaskPixelT initialValue,
109  MaskPlaneDict const& planeDefs = MaskPlaneDict());
117  MaskPlaneDict const& planeDefs = MaskPlaneDict());
125  explicit Mask(lsst::geom::Extent2I const& dimensions, MaskPixelT initialValue,
126  MaskPlaneDict const& planeDefs = MaskPlaneDict());
133  explicit Mask(lsst::geom::Box2I const& bbox, MaskPlaneDict const& planeDefs = MaskPlaneDict());
141  explicit Mask(lsst::geom::Box2I const& bbox, MaskPixelT initialValue,
142  MaskPlaneDict const& planeDefs = MaskPlaneDict());
143 
164  explicit Mask(std::string const& fileName, int hdu = fits::DEFAULT_HDU,
167  lsst::geom::Box2I const& bbox = lsst::geom::Box2I(), ImageOrigin origin = PARENT,
168  bool conformMasks = false, bool allowUnsafe=false);
169 
190  explicit Mask(fits::MemFileManager& manager, int hdu = fits::DEFAULT_HDU,
193  lsst::geom::Box2I const& bbox = lsst::geom::Box2I(), ImageOrigin origin = PARENT,
194  bool conformMasks = false, bool allowUnsafe=false);
195 
213  explicit Mask(fits::Fits& fitsfile,
216  lsst::geom::Box2I const& bbox = lsst::geom::Box2I(), ImageOrigin origin = PARENT,
217  bool conformMasks = false, bool allowUnsafe=false);
218 
219  // generalised copy constructor
220  template <typename OtherPixelT>
221  Mask(Mask<OtherPixelT> const& rhs, const bool deep)
222  : image::ImageBase<MaskPixelT>(rhs, deep), _maskDict(rhs._maskDict) {}
223 
230  Mask(const Mask& src, const bool deep = false);
231  Mask(Mask&& src);
232  ~Mask() override;
241  Mask(const Mask& src, const lsst::geom::Box2I& bbox, ImageOrigin const origin = PARENT,
242  const bool deep = false);
243 
244  explicit Mask(ndarray::Array<MaskPixelT, 2, 1> const& array, bool deep = false,
246 
247  void swap(Mask& rhs);
248  // Operators
249 
250  Mask& operator=(MaskPixelT const rhs);
251  Mask& operator=(const Mask& rhs);
252  Mask& operator=(Mask&& rhs);
253 
255  Mask& operator|=(Mask const& rhs);
257  Mask& operator|=(MaskPixelT const rhs);
258 
260  Mask& operator&=(Mask const& rhs);
262  Mask& operator&=(MaskPixelT const rhs);
263 
277  Mask subset(lsst::geom::Box2I const & bbox, ImageOrigin origin=PARENT) const {
278  return Mask(*this, bbox, origin, false);
279  }
280 
282  Mask operator[](lsst::geom::Box2I const & bbox) const {
283  return subset(bbox);
284  }
285 
287 
293  static MaskPixelT getPlaneBitMask(const std::vector<std::string>& names);
294 
296  Mask& operator^=(Mask const& rhs);
298  Mask& operator^=(MaskPixelT const rhs);
299 
306  typename ImageBase<MaskPixelT>::PixelReference operator()(int x, int y);
313  typename ImageBase<MaskPixelT>::PixelConstReference operator()(int x, int y) const;
321  bool operator()(int x, int y, int plane) const;
329  typename ImageBase<MaskPixelT>::PixelReference operator()(int x, int y, CheckIndices const& check);
337  typename ImageBase<MaskPixelT>::PixelConstReference operator()(int x, int y,
338  CheckIndices const& check) const;
347  bool operator()(int x, int y, int plane, CheckIndices const& check) const;
348 
356  void writeFits(std::string const& fileName,
359  std::string const& mode = "w") const;
360 
368  void writeFits(fits::MemFileManager& manager,
371  std::string const& mode = "w") const;
372 
379  void writeFits(fits::Fits& fitsfile, std::shared_ptr<lsst::daf::base::PropertySet const> metadata =
381 
390  void writeFits(std::string const& filename, fits::ImageWriteOptions const& options,
391  std::string const& mode = "w",
392  std::shared_ptr<daf::base::PropertySet const> header = nullptr) const;
393 
402  void writeFits(fits::MemFileManager& manager, fits::ImageWriteOptions const& options,
403  std::string const& mode = "w",
404  std::shared_ptr<daf::base::PropertySet const> header = nullptr) const;
405 
413  void writeFits(fits::Fits& fitsfile, fits::ImageWriteOptions const& options,
414  std::shared_ptr<daf::base::PropertySet const> header = nullptr) const;
415 
424  static Mask readFits(std::string const& filename, int hdu = fits::DEFAULT_HDU) {
425  return Mask<MaskPixelT>(filename, hdu);
426  }
427 
436  static Mask readFits(fits::MemFileManager& manager, int hdu = fits::DEFAULT_HDU) {
437  return Mask<MaskPixelT>(manager, hdu);
438  }
439 
441  static std::string interpret(MaskPixelT value);
442  std::string getAsString(int x, int y) { return interpret((*this)(x, y)); }
443 
444  // Mask Plane ops
445 
447  void clearAllMaskPlanes();
449  void clearMaskPlane(int plane);
453  void setMaskPlaneValues(const int plane, const int x0, const int x1, const int y);
460  static MaskPlaneDict parseMaskPlaneMetadata(std::shared_ptr<lsst::daf::base::PropertySet const> metadata);
461  //
462  // Operations on the mask plane dictionary
463  //
465  static void clearMaskPlaneDict();
466  static int addMaskPlane(const std::string& name);
467  static void removeMaskPlane(const std::string& name);
477  void removeAndClearMaskPlane(const std::string& name, bool const removeFromDefault = false);
478 
484  static int getMaskPlane(const std::string& name);
490  static MaskPixelT getPlaneBitMask(const std::string& name);
491 
492  static int getNumPlanesMax() { return 8 * sizeof(MaskPixelT); }
493  static int getNumPlanesUsed();
497  MaskPlaneDict const& getMaskPlaneDict() const;
499  void printMaskPlanes() const;
500 
504  static void addMaskPlanesToMetadata(std::shared_ptr<lsst::daf::base::PropertySet>);
505  //
506  // This one isn't static, it fixes up a given Mask's planes
521  void conformMaskPlanes(const MaskPlaneDict& masterPlaneDict);
522 
523 private:
524 
525  friend class MaskFitsReader;
526 
527  std::shared_ptr<detail::MaskDict> _maskDict; // our bitplane dictionary
528 
529  static std::shared_ptr<detail::MaskDict> _maskPlaneDict();
530  static int _setMaskPlaneDict(MaskPlaneDict const& mpd);
531  static const std::string maskPlanePrefix;
532 
541  static int addMaskPlane(std::string name, int plane);
542 
544  static int getMaskPlaneNoThrow(const std::string& name);
546  static MaskPixelT getBitMaskNoThrow(int plane);
552  static MaskPixelT getBitMask(int plane);
553 
557  void _initializePlanes(MaskPlaneDict const& planeDefs); // called by ctors
558 
559  //
560  // Make names in templatized base class visible (Meyers, Effective C++, Item 43)
561  //
564 
570  void checkMaskDictionaries(Mask const& other);
571 };
572 
573 template <typename PixelT>
574 void swap(Mask<PixelT>& a, Mask<PixelT>& b);
575 } // namespace image
576 } // namespace afw
577 } // namespace lsst
578 
579 #endif // LSST_AFW_IMAGE_MASK_H
A FITS reader class for Masks.
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:49
Mask(Mask< OtherPixelT > const &rhs, const bool deep)
Definition: Mask.h:221
table::Key< int > b
std::map< std::string, int > MaskPlaneDict
Definition: Mask.h:59
Options for writing an image to FITS.
Definition: fits.h:218
std::string getAsString(int x, int y)
Definition: Mask.h:442
static Mask readFits(std::string const &filename, int hdu=fits::DEFAULT_HDU)
Read a Mask from a regular FITS file.
Definition: Mask.h:424
int y
Definition: SpanSet.cc:49
table::Key< int > a
detail::MaskPlaneDict MaskPlaneDict
Definition: Mask.h:80
tuple options
Definition: lsstimport.py:47
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:296
The base class for all image classed (Image, Mask, MaskedImage, ...)
Definition: ImageBase.h:103
Mask< MaskPT > type
Return the desired type.
Definition: Mask.h:88
STL class.
A base class for image defects.
Mask operator[](lsst::geom::Box2I const &bbox) const
Return a subimage corresponding to the given box (interpreted as PARENT coordinates).
Definition: Mask.h:282
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:78
detail::Mask_tag image_category
Definition: Mask.h:82
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:120
A class used to request that array accesses be checked.
Definition: ImageBase.h:75
table::Box2IKey bbox
Definition: Detector.cc:166
double x
std::shared_ptr< RecordT > src
Definition: Match.cc:48
void swap(Image< PixelT > &a, Image< PixelT > &b)
Definition: Image.cc:465
static int getNumPlanesMax()
Definition: Mask.h:492
ItemVariant const * other
Definition: Schema.cc:56
Mask subset(lsst::geom::Box2I const &bbox, ImageOrigin origin=PARENT) const
Return a subimage corresponding to the given box.
Definition: Mask.h:277
A templated class to return this classes&#39; type (present in Image/Mask/MaskedImage) ...
Definition: Mask.h:86
static Mask readFits(fits::MemFileManager &manager, int hdu=fits::DEFAULT_HDU)
Read a Mask from a FITS RAM file.
Definition: Mask.h:436
An integer coordinate rectangle.
Definition: Box.h:54
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18
Basic LSST definitions.