LSST Applications g042eb84c57+b3bbdc4f1a,g04e9c324dd+8c5ae1fdc5,g121cf07809+de4bf23b53,g134cb467dc+0cbace0124,g199a45376c+0ba108daf9,g1fd858c14a+1dc7511062,g210f2d0738+9c061127b9,g262e1987ae+2d37dc3994,g29ae962dfc+d9a9135c6e,g2cef7863aa+aef1011c0b,g35bb328faa+8c5ae1fdc5,g3fd5ace14f+f525e742cf,g47891489e3+a82ffe86f0,g4d44eb3520+3036bb6e09,g4d7b6aa1c5+9c061127b9,g53246c7159+8c5ae1fdc5,g56a1a4eaf3+f4bcfd44dd,g64539dfbff+9c061127b9,g67b6fd64d1+a82ffe86f0,g67fd3c3899+9c061127b9,g6985122a63+a82ffe86f0,g6eac29cc47+0486cfe5cd,g74acd417e5+b6fc5cdc3f,g786e29fd12+668abc6043,g87389fa792+8856018cbb,g89139ef638+a82ffe86f0,g8d7436a09f+f523aa4ebd,g8ea07a8fe4+f5f58b89a4,g90f42f885a+b932d389a5,g97be763408+ef481f73f1,g99822b682c+47dc04c1e3,ga41d0fce20+fba9b6fb88,gbf99507273+8c5ae1fdc5,gd7ef33dd92+a82ffe86f0,gdab6d2f7ff+b6fc5cdc3f,ge410e46f29+a82ffe86f0,geaed405ab2+af3bcf2cab,gf9a733ac38+8c5ae1fdc5,w.2025.37
LSST Data Management Base Package
Loading...
Searching...
No Matches
Persistable.cc
Go to the documentation of this file.
1// -*- lsst-c++ -*-
2
3#include <map>
4
9#include "lsst/afw/fits.h"
10
11namespace lsst {
12namespace afw {
13namespace table {
14namespace io {
15
16// ----- Persistable ----------------------------------------------------------------------------------------
17
18void Persistable::writeFits(fits::Fits &fitsfile) const {
19 OutputArchive archive;
20 archive.put(this);
21 archive.writeFits(fitsfile);
22}
23
24void Persistable::writeFits(std::string const &fileName, std::string const &mode) const {
26 writeFits(fitsfile);
27}
28
29void Persistable::writeFits(fits::MemFileManager &manager, std::string const &mode) const {
31 writeFits(fitsfile);
32}
33
35
37
39 assert(!isPersistable());
41 "afw::table-based persistence is not supported for this object.");
42}
43
44std::shared_ptr<Persistable> Persistable::_readFits(std::string const &fileName, int hdu) {
46 fitsfile.setHdu(hdu);
47 return _readFits(fitsfile);
48}
49
50std::shared_ptr<Persistable> Persistable::_readFits(fits::MemFileManager &manager, int hdu) {
52 fitsfile.setHdu(hdu);
53 return _readFits(fitsfile);
54}
55
56std::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
63namespace {
64
66
67RegistryMap &getRegistry() {
68 static RegistryMap instance;
69 return instance;
70}
71
72} // namespace
73
74PersistableFactory::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(
84 (boost::format(
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()) {
94 (boost::format(
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
#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:308
Lifetime-management for memory that goes into FITS memory files.
Definition fits.h:125
static InputArchive readFits(fits::Fits &fitsfile)
Read an object from an already open FITS object.
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.
static PersistableFactory const & lookup(std::string const &name, std::string const &module="")
Return the factory that has been registered with the given name.
PersistableFactory(std::string const &name)
Constructor for the factory.
void writeFits(std::string const &fileName, std::string const &mode="w") const
Write the object to a regular FITS file.
io::OutputArchiveHandle OutputArchiveHandle
virtual bool isPersistable() const noexcept
Return true if this particular object can be persisted using afw::table::io.
virtual std::string getPythonModule() const
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
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