LSST Applications  21.0.0+3c14b91618,21.0.0+9f51b1e3f7,21.0.0-1-ga51b5d4+6691386486,21.0.0-10-g2408eff+1a328412bf,21.0.0-10-g560fb7b+52fd22d7b4,21.0.0-10-g8d1d15d+2f9043cae0,21.0.0-10-gcf60f90+d15de71c48,21.0.0-11-g25eff31+d43066e4ef,21.0.0-15-g490e301a+a676f0d5cf,21.0.0-2-g103fe59+4758c8ef83,21.0.0-2-g1367e85+a9f57e981a,21.0.0-2-g45278ab+9f51b1e3f7,21.0.0-2-g5242d73+a9f57e981a,21.0.0-2-g7f82c8f+1bcc828e4f,21.0.0-2-g8f08a60+e6fd6d9ff9,21.0.0-2-ga326454+1bcc828e4f,21.0.0-2-gde069b7+66c51b65da,21.0.0-2-gecfae73+251b9830c3,21.0.0-2-gfc62afb+a9f57e981a,21.0.0-20-g09baf175d+e1e7d1c708,21.0.0-3-g357aad2+854c3902c3,21.0.0-3-g4be5c26+a9f57e981a,21.0.0-3-g65f322c+feaa1990e9,21.0.0-3-g7d9da8d+3c14b91618,21.0.0-3-ge02ed75+bda8df9b93,21.0.0-4-g591bb35+bda8df9b93,21.0.0-4-g65b4814+52fd22d7b4,21.0.0-4-g88306b8+656365ce3f,21.0.0-4-gccdca77+86bf7a300d,21.0.0-4-ge8a399c+b99e86088e,21.0.0-5-gd00fb1e+6a0dc09319,21.0.0-51-gd3b42663+bf9f0d1c8b,21.0.0-6-g2d4f3f3+9f51b1e3f7,21.0.0-7-g04766d7+ee2ae02087,21.0.0-7-g98eecf7+3609eddee2,21.0.0-7-gde99fe0+bda8df9b93,21.0.0-8-gd70ce6f+79e45e76e4,master-gac4afde19b+bda8df9b93,w.2021.10
LSST Data Management Base Package
Persistable.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 #include <map>
4 
9 #include "lsst/afw/fits.h"
10 
11 namespace lsst {
12 namespace afw {
13 namespace table {
14 namespace io {
15 
16 // ----- Persistable ----------------------------------------------------------------------------------------
17 
18 void Persistable::writeFits(fits::Fits &fitsfile) const {
19  OutputArchive archive;
20  archive.put(this);
21  archive.writeFits(fitsfile);
22 }
23 
24 void Persistable::writeFits(std::string const &fileName, std::string const &mode) const {
25  fits::Fits fitsfile(fileName, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
26  writeFits(fitsfile);
27 }
28 
29 void Persistable::writeFits(fits::MemFileManager &manager, std::string const &mode) const {
30  fits::Fits fitsfile(manager, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
31  writeFits(fitsfile);
32 }
33 
35 
37 
39  assert(!isPersistable());
41  "afw::table-based persistence is not supported for this object.");
42 }
43 
44 std::shared_ptr<Persistable> Persistable::_readFits(std::string const &fileName, int hdu) {
45  fits::Fits fitsfile(fileName, "r", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
46  fitsfile.setHdu(hdu);
47  return _readFits(fitsfile);
48 }
49 
50 std::shared_ptr<Persistable> Persistable::_readFits(fits::MemFileManager &manager, int hdu) {
52  fitsfile.setHdu(hdu);
53  return _readFits(fitsfile);
54 }
55 
56 std::shared_ptr<Persistable> Persistable::_readFits(fits::Fits &fitsfile) {
57  InputArchive archive = InputArchive::readFits(fitsfile);
58  return archive.get(1); // the first object saved always has id=1
59 }
60 
61 // ----- PersistableFactory ---------------------------------------------------------------------------------
62 
63 namespace {
64 
66 
67 RegistryMap &getRegistry() {
68  static RegistryMap instance;
69  return instance;
70 }
71 
72 } // namespace
73 
74 PersistableFactory::PersistableFactory(std::string const &name) { getRegistry()[name] = this; }
75 
77  RegistryMap::const_iterator i = getRegistry().find(name);
78  if (i == getRegistry().end()) {
79  if (!module.empty()) {
80  bool success = base::ModuleImporter::import(module);
81  if (!success) {
82  throw LSST_EXCEPT(
85  "PersistableFactory with name '%s' not found, and import of module "
86  "'%s' failed (possibly because Python calls were not available from C++).") %
87  name % module)
88  .str());
89  }
90  i = getRegistry().find(name);
91  if (i == getRegistry().end()) {
92  throw LSST_EXCEPT(
95  "PersistableFactory with name '%s' not found even after successful import "
96  "of module '%s'. Please report this as a bug in the persistence "
97  "implementation for this object.") %
98  name % module)
99  .str());
100  }
101  } else {
102  throw LSST_EXCEPT(
104  (boost::format(
105  "PersistableFactory with name '%s' not found, and no Python module to import "
106  "was provided. Please report this as a bug in the persistence implementation "
107  "for this object.") %
108  name)
109  .str());
110  }
111  }
112  return *i->second;
113 }
114 } // namespace io
115 } // namespace table
116 } // namespace afw
117 } // namespace lsst
int end
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
Mechanism for safely importing Python modules from C++; should not be included except by its own impl...
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:297
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:121
static InputArchive readFits(fits::Fits &fitsfile)
Read an object from an already open FITS object.
An object passed to Persistable::write to allow it to persist itself.
A multi-catalog archive object used to save table::io::Persistable objects.
Definition: OutputArchive.h:34
int put(std::shared_ptr< Persistable const > obj, bool permissive=false)
Save an object to the archive and return a unique ID that can be used to retrieve it from an InputArc...
void writeFits(fits::Fits &fitsfile) const
Write the archive to an already-open FITS object.
A base class for factory classes used to reconstruct objects from records.
Definition: Persistable.h:228
static PersistableFactory const & lookup(std::string const &name, std::string const &module="")
Return the factory that has been registered with the given name.
Definition: Persistable.cc:76
PersistableFactory(std::string const &name)
Constructor for the factory.
Definition: Persistable.cc:74
void writeFits(std::string const &fileName, std::string const &mode="w") const
Write the object to a regular FITS file.
Definition: Persistable.cc:24
virtual bool isPersistable() const noexcept
Return true if this particular object can be persisted using afw::table::io.
Definition: Persistable.h:102
virtual std::string getPythonModule() const
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
Definition: Persistable.cc:36
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
Definition: Persistable.cc:34
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
Definition: Persistable.cc:38
static bool import(std::string const &name)
Import the given Python module, and return true if successful.
Reports errors in the logical structure of the program.
Definition: Runtime.h:46
Reports attempts to access elements using an invalid key.
Definition: Runtime.h:151
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
A base class for image defects.
table::Key< std::string > name
Definition: Amplifier.cc:116