LSSTApplications  16.0-1-gce273f5+20,16.0-10-g1758552,16.0-10-gc1446dd+21,16.0-12-g569485f+3,16.0-13-g5f20d24+1,16.0-13-g80874fd+2,16.0-13-gb122224+12,16.0-14-g08f9460+4,16.0-14-g8a3b804,16.0-14-ga5060d2,16.0-15-g77ef378+7,16.0-18-gdf247dd+2,16.0-18-ge18fa5b,16.0-18-ge4151178,16.0-2-g0febb12+16,16.0-2-g9d5294e+46,16.0-2-gc6e0ed0+5,16.0-23-ge8a9b866+3,16.0-3-g404ea43+13,16.0-3-gbc759ec+19,16.0-3-gcfd6c53+44,16.0-3-ge00e371,16.0-4-g03cf288+35,16.0-4-g13a27c5+21,16.0-4-g5f3a788+15,16.0-4-ga3eb747+5,16.0-5-g1991253+21,16.0-5-g1e9226d+3,16.0-5-g6a53317,16.0-5-g865efd9+23,16.0-5-gb3f8a4b+53,16.0-5-gd0f1235+10,16.0-52-gad2f36c79,16.0-7-g6043bfc+9,16.0-7-gd2eeba5+3,16.0-7-gde5bd64+3,16.0-8-g0e813a6+1,16.0-9-g52c50f7,16.0-9-g73415e6,master-g5768c874b9+5,w.2018.41
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
T empty(T... args)
PersistableFactory(std::string const &name)
Constructor for the factory.
Definition: Persistable.cc:74
An object passed to Persistable::write to allow it to persist itself.
A base class for factory classes used to reconstruct objects from records.
Definition: Persistable.h:228
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:296
STL class.
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
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
Definition: Persistable.cc:38
STL class.
A multi-catalog archive object used to save table::io::Persistable objects.
Definition: OutputArchive.h:34
Reports attempts to access elements using an invalid key.
Definition: Runtime.h:151
void writeFits(std::string const &fileName, std::string const &mode="w") const
Write the object to a regular FITS file.
Definition: Persistable.cc:24
void writeFits(fits::Fits &fitsfile) const
Write the archive to an already-open FITS object.
A base class for image defects.
Definition: cameraGeom.dox:3
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
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:120
Mechanism for safely importing Python modules from C++; should not be included except by its own impl...
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:129
Reports errors in the logical structure of the program.
Definition: Runtime.h:46
void setHdu(int hdu, bool relative=false)
Set the current HDU.
Definition: fits.cc:492
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
static InputArchive readFits(fits::Fits &fitsfile)
Read an object from an already open FITS object.
int put(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...
static bool import(std::string const &name)
Import the given Python module, and return true if successful.
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 getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
Definition: Persistable.cc:34
A multi-catalog archive object used to load table::io::Persistable objects.
Definition: InputArchive.h:31
int end
std::shared_ptr< Persistable > get(int id) const
Load the Persistable with the given ID and return it.