lsst.afw
g3a5ebb7d8a+28b83bf6a5
Toggle main menu visibility
Loading...
Searching...
No Matches
src
table
io
Persistable.cc
Go to the documentation of this file.
1
// -*- lsst-c++ -*-
2
3
#include <map>
4
5
#include "
lsst/base/ModuleImporter.h
"
6
#include "
lsst/afw/table/io/Persistable.h
"
7
#include "
lsst/afw/table/io/OutputArchive.h
"
8
#include "
lsst/afw/table/io/InputArchive.h
"
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
34
std::string
Persistable::getPersistenceName
()
const
{
return
std::string
(); }
35
36
std::string
Persistable::getPythonModule
()
const
{
return
std::string
(); }
37
38
void
Persistable::write
(
OutputArchiveHandle
&)
const
{
39
assert(!
isPersistable
());
40
throw
LSST_EXCEPT
(
pex::exceptions::LogicError
,
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) {
51
fits::Fits
fitsfile(manager,
"r"
,
fits::Fits::AUTO_CLOSE
|
fits::Fits::AUTO_CHECK
);
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
65
using
RegistryMap =
std::map<std::string, const PersistableFactory *>
;
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
76
PersistableFactory
const
&
PersistableFactory::lookup
(
std::string
const
&name,
std::string
const
&module) {
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
(
83
pex::exceptions::NotFoundError
,
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()) {
92
throw
LSST_EXCEPT
(
93
pex::exceptions::LogicError
,
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
(
103
pex::exceptions::LogicError
,
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
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
InputArchive.h
ModuleImporter.h
OutputArchive.h
Persistable.h
std::string
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:242
lsst::afw::fits::Fits::AUTO_CHECK
@ AUTO_CHECK
Definition
fits.h:253
lsst::afw::fits::Fits::AUTO_CLOSE
@ AUTO_CLOSE
Definition
fits.h:252
lsst::afw::fits::MemFileManager
Lifetime-management for memory that goes into FITS memory files.
Definition
fits.h:126
lsst::afw::table::io::InputArchive::readFits
static InputArchive readFits(fits::Fits &fitsfile)
Read an object from an already open FITS object.
Definition
InputArchive.cc:183
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:201
lsst::afw::table::io::OutputArchive::writeFits
void writeFits(fits::Fits &fitsfile) const
Write the archive to an already-open FITS object.
Definition
OutputArchive.cc:224
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::PersistableFactory::PersistableFactory
PersistableFactory(std::string const &name)
Constructor for the factory.
Definition
Persistable.cc:74
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
lsst::afw::table::io::Persistable::OutputArchiveHandle
io::OutputArchiveHandle OutputArchiveHandle
Definition
Persistable.h:108
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
lsst::afw::table::io::Persistable::OutputArchive
friend class io::OutputArchive
Definition
Persistable.h:145
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::Persistable::InputArchive
friend class io::InputArchive
Definition
Persistable.h:146
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::table::io::Persistable::write
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
Definition
Persistable.cc:38
lsst::base::ModuleImporter::import
static bool import(std::string const &name)
lsst::pex::exceptions::LogicError
lsst::pex::exceptions::NotFoundError
std::string::empty
T empty(T... args)
fits.h
std::map
lsst::afw::table::io
Definition
tablePersistence.dox:3
lsst::afw::table
Definition
table.dox:3
lsst::afw
Definition
imageAlgorithm.dox:1
lsst
std::shared_ptr
Generated on
for lsst.afw by
1.17.0