CNES / EleFits

A modern C++ API on top of CFitsIO
https://cnes.github.io/EleFits
Other
13 stars 5 forks source link

Implement Strategy #35

Closed kabasset closed 1 year ago

kabasset commented 1 year ago

A Strategy is a collection of Action = std::function<void(const Hdu&)>. It is indexed by some ActionTrigger, e.g. PostFileOpen, PreFileClose, PreHduInit, PostHduInit, PostFirstHduAccess (naming too rude, to be refined) and the HduCategory. Possibly, some ActionTag (e.g. int or string) could be used to add or remove actions.

Something like this must be feasible:

Strategy s;
s.add(Compress(Plio()), ActionTrigger::PreHduInit, HduCategory::IntImage);
s.add(Compress(HCompress()), ActionTrigger::PreHduInit, HduCategory::FloatImage);
s.add(ValidateChecksums(), ActionTrigger::PostFileOpen); // default category is Any
s.add(ValidateChecksums(), ActionTrigger::PostFirstHduAccess);
s.add(UpdateChecksums(), ActionTrigger::PreFileClose, HduCategory::Edited);
s.add([](const Hdu& hdu) {
        hdu.header().updateVersion(hdu.header().readVersion() + 1);
    }, ActionTrigger::PreFileClose, HduCategory::Edited);

MefFile f("file.fits", FileMode::Create, s); // default is empty strategy

At some point, shortcuts could be introduced, e.g. s.preHduInit(Compress(Plio()), HduCategory::IntImage);.

For implementation ease, tags could also be replaced with ids, e.g.:

auto plio = s.add(Compress(Plio()), ...);
...
f.disableAction(plio);
f.initImage(...);
f.enableAction(plio);

Given that such local behaviors should be used with care, such an extra complexity would probably be acceptable.

kabasset commented 1 year ago

Done for compression, with simplified CompressionStrategy, which is the main target of the issue.

kabasset commented 1 year ago

Done with simpler interface, yet to be optimized (avoid reading all headers that often).