SatDump 2.0.0-alpha-76a915210
Loading...
Searching...
No Matches
image_product.h
Go to the documentation of this file.
1#pragma once
2
7
9#include "image/image.h"
10#include "product.h"
11#include "utils/stats.h"
12
13#include "common/physics_constants.h"
14#include "image/calibration_units.h" // TODOREWORK MOVE!!!! + Conversion
15
16// TODOREWORK MOVE
17#include "common/calibration.h"
18#include "utils/unit_parser.h"
19#include <string>
20
21namespace satdump
22{
23 namespace products
24 {
41 class ImageProduct : public Product
42 {
43 public:
59 {
60 int abs_index = -1;
61 std::string filename;
62 std::string channel_name;
63 image::Image image;
64 int bit_depth = 16;
66 double wavenumber = -1;
67 std::string calibration_type = "";
68 };
69
70 std::vector<ImageHolder> images;
71
72 bool save_as_matrix = false;
73
74 public:
79 void set_proj_cfg(nlohmann::json cfg)
80 {
81 contents["projection_cfg"] = cfg;
82 if (cfg.contains("tle") && cfg["tle"].contains("name"))
83 if (!has_product_source())
84 set_product_source(cfg["tle"]["name"]);
85 if (cfg.contains("timestamps") && !has_product_timestamp())
86 set_product_timestamp(get_median(cfg["timestamps"].get<std::vector<double>>()));
87 }
88
95 void set_proj_cfg_tle_timestamps(nlohmann::json cfg, nlohmann::json tle, nlohmann::json timestamps)
96 {
97 cfg["tle"] = tle;
98 cfg["timestamps"] = timestamps;
99 set_proj_cfg(cfg);
100 }
101
107 nlohmann::json get_proj_cfg(int channel)
108 {
109 // TODO CHANNEL SPECIFICS
110 auto cfg = contents["projection_cfg"];
111 if (channel != -1)
112 {
113 cfg["transform"] = get_channel_image(channel).ch_transform;
114 cfg["width"] = get_channel_image(channel).image.width();
115 cfg["height"] = get_channel_image(channel).image.height();
116 }
118 cfg["proj_timestamp"] = get_product_timestamp();
119 return cfg;
120 }
121
126 bool has_proj_cfg() { return contents.contains("projection_cfg"); }
127
128 public:
129 void set_calibration(std::string calibrator, nlohmann::json cfg)
130 {
131 contents["calibration"] = cfg;
132 contents["calibration"]["calibrator"] = calibrator;
133 }
134
135 std::pair<std::string, nlohmann::json> get_calibration() { return {contents["calibration"]["calibrator"], contents["calibration"]}; }
136
141 bool has_calibration() { return contents.contains("calibration"); }
142
143 // TODOREWORK DOCUMENT
144 nlohmann::json get_calibration_raw() { return contents.contains("calibration") ? contents["calibration"] : nlohmann::json(); }
145
146 public:
153 {
154 for (auto &img : images)
155 if (img.abs_index == index)
156 return img;
157 throw satdump_exception("Product Channel Index " + std::to_string(index) + " is not present!");
158 }
159
166 {
167 for (auto &img : images)
168 if (img.channel_name == name)
169 return img;
170 throw satdump_exception("Product Channel Name " + name + " is not present!");
171 }
172
179 {
180 double best = 1e40;
181 ImageHolder *out = nullptr;
182 for (auto &img : images)
183 {
184 if (img.wavenumber != -1 && best > abs(img.wavenumber - wavenumber))
185 {
186 out = &img;
187 best = abs(img.wavenumber - wavenumber);
188 }
189 }
190 if (out == nullptr)
191 throw satdump_exception("Product Channel Close to Wavelength " + std::to_string(wavenumber) + " is not present!");
192 return *out;
193 }
194
201 {
202 double val = 0;
203
204 if (parseUnitFromString(str, val, UNIT_METER))
205 val = freq_to_wavenumber(SPEED_OF_LIGHT_M_S / val);
206 else if (parseUnitFromString(str, val, UNIT_HERTZ))
207 val = freq_to_wavenumber(val);
208 else
209 throw satdump_exception("Couldn't parse unit and value from " + str);
210
212 }
213
219 int get_channel_image_idx(std::string name)
220 {
221 for (int i = 0; i < (int)images.size(); i++)
222 if (images[i].channel_name == name)
223 return i;
224 throw satdump_exception("Product Channel Name " + name + " is not present!");
225 }
226
233 inline int get_raw_channel_val(int idx, int x, int y)
234 {
235 auto &i = images[idx];
236 int depth_diff = i.bit_depth - i.image.depth();
237 if (depth_diff > 0)
238 return i.image.get(0, x, y) << depth_diff;
239 else
240 return i.image.get(0, x, y) >> -depth_diff;
241 }
242
248 void set_channel_wavenumber(int index, double wavenumber)
249 {
250 for (auto &img : images)
251 if (img.abs_index == index)
252 {
253 img.wavenumber = wavenumber;
254 return;
255 }
256 throw satdump_exception("Product Channel Index " + std::to_string(index) + " is not present!");
257 }
258
264 void set_channel_frequency(int index, double frequency) { set_channel_wavenumber(index, freq_to_wavenumber(frequency)); }
265
271 double get_channel_wavenumber(int index)
272 {
273 for (auto &img : images)
274 if (img.abs_index == index)
275 return img.wavenumber;
276 throw satdump_exception("Product Channel Index " + std::to_string(index) + " is not present!");
277 }
278
284 double get_channel_frequency(int index) { return wavenumber_to_freq(get_channel_wavenumber(index)); }
285
292 void set_channel_unit(int index, std::string type_or_unit)
293 {
294 // TODOREWORK, may want to enforce unit automatically?
295 for (auto &img : images)
296 if (img.abs_index == index)
297 {
298 img.calibration_type = type_or_unit;
299 return;
300 }
301 throw satdump_exception("Product Channel Index " + std::to_string(index) + " is not present!");
302 }
303
304 public:
305 bool d_no_not_save_images = false;
306 bool d_no_not_load_images = false;
307 virtual void save(std::string directory);
308 virtual void load(std::string file);
309
310 ImageProduct() { type = "image"; }
311 virtual ~ImageProduct();
312 };
313 } // namespace products
314} // namespace satdump
Class handling 2D transforms, mainly intended for image channels. This covers a simple but pretty imp...
Definition channel_transform.h:39
ChannelTransform & init_none()
Initialize a NONE transform.
Definition channel_transform.cpp:10
Definition image.h:17
size_t height() const
Returns image height.
Definition image.h:216
size_t width() const
Returns image width.
Definition image.h:210
SatDump image product class.
Definition image_product.h:42
double get_channel_wavenumber(int index)
Get channel wavenumber.
Definition image_product.h:271
void set_channel_unit(int index, std::string type_or_unit)
Set channel calibration unit.
Definition image_product.h:292
bool has_calibration()
Check if calibration info is present.
Definition image_product.h:141
ImageHolder & get_channel_image_by_unitstr(std::string str)
Get image channel by unit string. Returns the closest one.
Definition image_product.h:200
virtual void load(std::string file)
Load the product. This should refer to the product.cbor file.
Definition image_product.cpp:79
void set_channel_wavenumber(int index, double wavenumber)
Set channel wavenumber.
Definition image_product.h:248
int get_raw_channel_val(int idx, int x, int y)
Get raw channel count.
Definition image_product.h:233
double get_channel_frequency(int index)
Get channel frequency.
Definition image_product.h:284
bool has_proj_cfg()
Check if geo projection info is present.
Definition image_product.h:126
void set_channel_frequency(int index, double frequency)
Set channel frequency (internally, wavenumber)
Definition image_product.h:264
ImageHolder & get_channel_image_by_wavenumber(double wavenumber)
Get image channel by wavenumber. Returns the closest one.
Definition image_product.h:178
int get_channel_image_idx(std::string name)
Get image channel raw ID by name.
Definition image_product.h:219
ImageHolder & get_channel_image(std::string name)
Get image channel by name.
Definition image_product.h:165
void set_proj_cfg_tle_timestamps(nlohmann::json cfg, nlohmann::json tle, nlohmann::json timestamps)
Set geo projection config in the product, with a TLE and timestamps.
Definition image_product.h:95
ImageHolder & get_channel_image(int index)
Get image channel by absolute ID.
Definition image_product.h:152
virtual void save(std::string directory)
Save the product. Depending on the type this will save a product.cbor and other files in the same dir...
Definition image_product.cpp:18
nlohmann::json get_proj_cfg(int channel)
Get geo projection config in the product, if present.
Definition image_product.h:107
void set_proj_cfg(nlohmann::json cfg)
Set geo projection config in the product.
Definition image_product.h:79
double get_product_timestamp()
Get the product timestamp.
Definition product.h:63
void set_product_source(std::string source)
Set product source, optional. This is meant to contextualize where this product is from,...
Definition product.h:69
void set_product_timestamp(double timestamp)
Set product timestamp, optional. This is usually the rough creation time / acquisition time.
Definition product.h:51
bool has_product_source()
Check if a product source is present.
Definition product.h:75
bool has_product_timestamp()
Check if a product timestamp is present.
Definition product.h:57
Core Image class.
Core Product implementation.
Math/Statistics functions.
Struct holding both the image and some metadata.
Definition image_product.h:59