Alexandria  2.27.0
SDC-CH common library for the Euclid project
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
serialize.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2022 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /*
20  * @file serialize.h
21  * @author nikoapos
22  */
23 
24 #ifndef SOM_SERIALIZE_H
25 #define SOM_SERIALIZE_H
26 
29 #include "SOM/Distance.h"
30 #include "SOM/serialization/SOM.h"
31 #include <CCfits/CCfits>
32 #include <boost/archive/binary_iarchive.hpp>
33 #include <boost/archive/binary_oarchive.hpp>
34 #include <iostream>
35 
36 namespace Euclid {
37 namespace SOM {
38 
39 template <typename OArchive, typename DistFunc>
40 void somExport(std::ostream& out, const SOM<DistFunc>& som) {
41  // Do NOT delete this pointer!!! It points to the actual som
42  const SOM<DistFunc>* ptr = &som;
43  OArchive boa{out};
44  boa << ptr;
45 }
46 
47 template <typename DistFunc>
48 void somBinaryExport(std::ostream& out, const SOM<DistFunc>& som) {
49  somExport<boost::archive::binary_oarchive>(out, som);
50 }
51 
52 template <typename IArchive, typename DistFunc = Distance::L2>
54  IArchive bia{in};
55  // Do NOT delete manually this pointer. It is wrapped with a unique_ptr later.
56  SOM<DistFunc>* ptr;
57  bia >> ptr;
58  std::unique_ptr<SOM<DistFunc>> smart_ptr{ptr};
59  // We move out to the result the som pointed by the pointer. The unique_ptr
60  // will delete the (now empty) pointed object
61  return std::move(*smart_ptr);
62 }
63 
64 template <typename DistFunc = Distance::L2>
66  return somImport<boost::archive::binary_iarchive, DistFunc>(in);
67 }
68 
69 template <typename DistFunc>
70 void somFitsExport(const std::string& filename, const SOM<DistFunc>& som) {
71 
72  // Create the output file and the array HDU
73  int n_axes = 3;
74  std::size_t x;
75  std::size_t y;
76  std::tie(x, y) = som.getSize();
77  long ax_sizes[3] = {long(x), long(y), long(som.getDimensions())};
78  CCfits::FITS fits(filename, DOUBLE_IMG, n_axes, ax_sizes);
79 
80  // Write in the header the DistFunc type
81  fits.pHDU().addKey("DISTFUNC", typeid(DistFunc).name(), "");
82 
83  // Create a valarray with the SOM data
84  std::size_t total_size = x * y * som.getDimensions();
85  std::valarray<double> data(total_size);
86  int i = 0;
87  for (std::size_t w_i = 0; w_i < som.getDimensions(); ++w_i) {
88  for (auto& w_arr : som) {
89  data[i] = w_arr[w_i];
90  ++i;
91  }
92  }
93  fits.pHDU().write(1, total_size, data);
94 }
95 
96 template <typename DistFunc = Distance::L2>
98 
99  CCfits::FITS fits(filename, CCfits::Read);
100 
101  // Check that the type of the DistFunc is correct
102  std::string dist_func_type;
103  fits.pHDU().readKey("DISTFUNC", dist_func_type);
104  if (dist_func_type != typeid(DistFunc).name()) {
105  throw Elements::Exception() << "Incompatible DistFunc parameter. File contains SOM with " << dist_func_type
106  << " and is read as " << typeid(DistFunc).name();
107  }
108 
109  // Get the dimensions of the data in the file
110  if (fits.pHDU().axes() != 3) {
111  throw Elements::Exception() << "Data array in file " << filename << " does not have 3 dimensions";
112  }
113  std::size_t dim = fits.pHDU().axis(2);
114  std::size_t x = fits.pHDU().axis(0);
115  std::size_t y = fits.pHDU().axis(1);
116 
117  // Read the data from the file
119  fits.pHDU().read(data);
120 
121  // Copy the data in a SOM object
122  SOM<DistFunc> result{dim, x, y};
123  int i = 0;
124  for (std::size_t w_i = 0; w_i < dim; ++w_i) {
125  for (auto w_arr : result) {
126  w_arr[w_i] = data[i];
127  ++i;
128  }
129  }
130 
131  return result;
132 }
133 
134 } // namespace SOM
135 } // namespace Euclid
136 
137 #endif /* SOM_SERIALIZE_H */
void somBinaryExport(std::ostream &out, const SOM< DistFunc > &som)
Definition: serialize.h:48
void somExport(std::ostream &out, const SOM< DistFunc > &som)
Definition: serialize.h:40
T tie(T...args)
void somFitsExport(const std::string &filename, const SOM< DistFunc > &som)
Definition: serialize.h:70
STL class.
STL class.
SOM< DistFunc > somImport(std::istream &in)
Definition: serialize.h:53
STL class.
T move(T...args)
STL class.
SOM< DistFunc > somFitsImport(const std::string &filename)
Definition: serialize.h:97
SOM< DistFunc > somBinaryImport(std::istream &in)
Definition: serialize.h:65
STL class.