LSSTApplications  21.0.0+1b62c9342b,21.0.0+45a059f35e,21.0.0-1-ga51b5d4+ceb9cf20a3,21.0.0-2-g103fe59+4d13aff7ba,21.0.0-2-g1367e85+571a348718,21.0.0-2-g2909d54+45a059f35e,21.0.0-2-g45278ab+1b62c9342b,21.0.0-2-g4bc9b9f+ebfe466dad,21.0.0-2-g5242d73+571a348718,21.0.0-2-g54e2caa+07cebfb09d,21.0.0-2-g66bcc37+0b2c5d3971,21.0.0-2-g7f82c8f+08f1f55c36,21.0.0-2-g8dde007+5d1b9cb3f5,21.0.0-2-g8f08a60+73884b2cf5,21.0.0-2-ga326454+08f1f55c36,21.0.0-2-ga63a54e+458e82fbcd,21.0.0-2-gc738bc1+8c4731df06,21.0.0-2-gde069b7+5a8f2956b8,21.0.0-2-ge17e5af+571a348718,21.0.0-2-ge712728+cfa36ee5f9,21.0.0-2-gecfae73+e597808034,21.0.0-2-gfc62afb+571a348718,21.0.0-20-g4449a12+6d1341e0f3,21.0.0-22-gf0532904+1cd928f0c5,21.0.0-3-g4c5b185+c3794955c6,21.0.0-3-g6d51c4a+0b2c5d3971,21.0.0-3-g8076721+5adeb471db,21.0.0-3-gaa929c8+01f4b7cfca,21.0.0-3-gd222c45+afc8332dbe,21.0.0-4-g1383c07+0b2c5d3971,21.0.0-4-g3300ddd+1b62c9342b,21.0.0-4-g5873dc9+9a92674037,21.0.0-4-g8a80011+bd904b6426,21.0.0-5-gcff38f6+844b7f7b93,21.0.0-6-g463d161+18af5fb57b,21.0.0-6-gd3283ba+01f4b7cfca,21.0.0-8-g19111d86+8234efb485,21.0.0-9-g7bed000b9+c7d3cce47e,w.2021.04
LSSTDataManagementBasePackage
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
Persistable.h
ModuleImporter.h
Mechanism for safely importing Python modules from C++; should not be included except by its own impl...
lsst::afw::table::io::Persistable::getPythonModule
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
lsst::afw::table::io::PersistableFactory::lookup
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
lsst::afw::table::io::Persistable::writeFits
void writeFits(std::string const &fileName, std::string const &mode="w") const
Write the object to a regular FITS file.
Definition: Persistable.cc:24
std::string
STL class.
std::shared_ptr< Persistable >
lsst::afw::table::io::OutputArchive::put
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...
Definition: OutputArchive.cc:180
lsst::afw::table::io::OutputArchiveHandle
An object passed to Persistable::write to allow it to persist itself.
Definition: OutputArchive.h:118
lsst::afw::fits::Fits
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:297
lsst.pex::exceptions::NotFoundError
Reports attempts to access elements using an invalid key.
Definition: Runtime.h:151
fits.h
lsst::afw
Definition: imageAlgorithm.dox:1
lsst.pex.config.history.format
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
lsst::afw::fits::MemFileManager
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:121
lsst::afw::geom.transform.transformContinued.name
string name
Definition: transformContinued.py:32
lsst::afw::table::io::Persistable::isPersistable
virtual bool isPersistable() const noexcept
Return true if this particular object can be persisted using afw::table::io.
Definition: Persistable.h:102
end
int end
Definition: BoundedField.cc:105
lsst::afw::table::io::PersistableFactory::PersistableFactory
PersistableFactory(std::string const &name)
Constructor for the factory.
Definition: Persistable.cc:74
lsst::base::ModuleImporter::import
static bool import(std::string const &name)
Import the given Python module, and return true if successful.
Definition: ModuleImporter.cc:49
lsst::afw::table::io::Persistable::getPersistenceName
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
Definition: Persistable.cc:34
lsst::afw::fits::Fits::AUTO_CHECK
@ AUTO_CHECK
Definition: fits.h:308
lsst::afw::fits::Fits::AUTO_CLOSE
@ AUTO_CLOSE
Definition: fits.h:307
lsst::afw::table::io::OutputArchive::writeFits
void writeFits(fits::Fits &fitsfile) const
Write the archive to an already-open FITS object.
Definition: OutputArchive.cc:203
lsst.pex::exceptions::LogicError
Reports errors in the logical structure of the program.
Definition: Runtime.h:46
lsst::afw::table::io::Persistable::write
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
Definition: Persistable.cc:38
lsst.pipe.tasks.cli.cmd.commands.str
str
Definition: commands.py:50
lsst::afw::table::io::PersistableFactory
A base class for factory classes used to reconstruct objects from records.
Definition: Persistable.h:228
lsst::afw::table::io::OutputArchive
A multi-catalog archive object used to save table::io::Persistable objects.
Definition: OutputArchive.h:34
std::map
STL class.
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
lsst::afw::table::io::InputArchive::readFits
static InputArchive readFits(fits::Fits &fitsfile)
Read an object from an already open FITS object.
Definition: InputArchive.cc:186
InputArchive.h
lsst::meas::modelfit.psf.psfContinued.module
module
Definition: psfContinued.py:42
OutputArchive.h