38#include "eris_nix_gain_linearity.h"
39#include "eris_nix_utils.h"
46gain_linearity * engl_gain_linearity_create(cpl_imagelist *,
55double * engl_repack(
const cpl_imagelist *)
77void engl_gain_linearity_delete(gain_linearity * gain_lin) {
80 cpl_imagelist_delete(gain_lin->lin_coeffs);
81 cpl_free(gain_lin->ordered_lin_coeffs);
82 cpl_mask_delete(gain_lin->bpm);
83 cpl_propertylist_delete(gain_lin->plist);
104gain_linearity * engl_gain_linearity_load_from_frameset(
const cpl_frameset * frameset,
105 const char * gain_tag,
106 const char * coeffs_tag,
107 const char * bpm_tag,
109 cpl_frameset * used) {
111 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
113 const cpl_frame * linbpmframe = NULL;
114 const cpl_frame * lincoeffsframe = NULL;
115 const cpl_frame * gainframe = NULL;
116 cpl_imagelist * lin_coeffs = NULL;
117 cpl_mask * lin_bpm = NULL;
118 double * ordered_lin_coeffs = NULL;
119 cpl_propertylist * plist = NULL;
120 double saturation_limit = -1.0;
121 gain_linearity * result = NULL;
125 cpl_ensure(frameset, CPL_ERROR_NULL_INPUT, NULL);
126 cpl_ensure(gain_tag, CPL_ERROR_NULL_INPUT, NULL);
127 cpl_ensure(coeffs_tag, CPL_ERROR_NULL_INPUT, NULL);
128 cpl_ensure(bpm_tag, CPL_ERROR_NULL_INPUT, NULL);
132 gainframe = cpl_frameset_find_const(frameset, gain_tag);
133 lincoeffsframe = cpl_frameset_find_const(frameset, coeffs_tag);
134 linbpmframe = cpl_frameset_find_const(frameset, bpm_tag);
137 cpl_ensure(lincoeffsframe && linbpmframe && gainframe,
138 CPL_ERROR_DATA_NOT_FOUND, NULL);
139 }
else if (!(lincoeffsframe && linbpmframe && gainframe)) {
140 cpl_msg_warning(cpl_func,
"gain/linearity information not available");
143 if (lincoeffsframe && linbpmframe && gainframe) {
147 plist = cpl_propertylist_load(cpl_frame_get_filename(gainframe), 0);
148 double gain_data = cpl_propertylist_get_double(plist,
"ESO QC GAIN");
149 double gain_err = 0.0;
150 hdrl_value gain = (hdrl_value){gain_data, gain_err};
154 for (
int ip = 1; ip < 100; ip++) {
155 char * pname = cpl_sprintf(
"ESO PRO REC1 PARAM%-2d NAME", ip);
156 if (cpl_propertylist_has(plist, pname)) {
157 if (strstr(cpl_propertylist_get_string(plist, pname),
158 "saturation_limit")) {
159 char * vname = cpl_sprintf(
"ESO PRO REC1 PARAM%-2d VALUE", ip);
160 const char * sat_limit = cpl_propertylist_get_string(plist, vname);
161 sscanf(sat_limit,
"%lf", &saturation_limit);
166 if (saturation_limit > 0.0)
break;
168 if (saturation_limit < 0.0) {
169 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
170 "failed to read saturation_limit");
172 cpl_msg_info(cpl_func,
"gain/linearity saturation limit = %6.1f",
180 lin_coeffs = cpl_imagelist_load(
181 cpl_frame_get_filename(lincoeffsframe), CPL_TYPE_FLOAT, 0);
185 lin_bpm = cpl_mask_load(cpl_frame_get_filename(linbpmframe), 0, 0);
190 ordered_lin_coeffs = engl_repack(lin_coeffs);
194 result = engl_gain_linearity_create(lin_coeffs, lin_bpm,
195 ordered_lin_coeffs, gain,
196 saturation_limit, plist);
200 cpl_frame * dup_lincoeffsframe = cpl_frame_duplicate(lincoeffsframe);
201 cpl_frameset_insert(used, dup_lincoeffsframe);
202 cpl_frame * dup_linbpmframe = cpl_frame_duplicate(linbpmframe);
203 cpl_frameset_insert(used, dup_linbpmframe);
204 cpl_frame * dup_gainframe = cpl_frame_duplicate(gainframe);
205 cpl_frameset_insert(used, dup_gainframe);
207 if (cpl_error_get_code() != CPL_ERROR_NONE) {
208 engl_gain_linearity_delete(result);
238gain_linearity * engl_gain_linearity_create(cpl_imagelist * lin_coeffs,
240 double * ordered_lin_coeffs,
241 const hdrl_value gain,
242 const double saturation_limit,
243 cpl_propertylist * plist) {
245 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
247 cpl_ensure(lin_coeffs && lin_bpm && plist, CPL_ERROR_NULL_INPUT, NULL);
249 gain_linearity * result = cpl_malloc(
sizeof(gain_linearity));
250 result->lin_coeffs = lin_coeffs;
251 result->bpm = lin_bpm;
252 result->ordered_lin_coeffs = ordered_lin_coeffs;
254 result->saturation_limit = saturation_limit;
255 result->plist = plist;
282gain_linearity * engl_gain_linearity_test(
const cpl_size nx,
288 const hdrl_value gain,
289 const double saturation) {
291 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
293 cpl_imagelist * coeffs_imagelist = cpl_imagelist_new();
294 cpl_image * c0 = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
295 cpl_image_fill_window(c0, 1, 1, nx, ny, coeff0);
296 cpl_imagelist_set(coeffs_imagelist, c0, 0);
297 cpl_image * c1 = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
298 cpl_image_fill_window(c1, 1, 1, nx, ny, coeff1);
299 cpl_imagelist_set(coeffs_imagelist, c1, 1);
300 cpl_image * c2 = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
301 cpl_image_fill_window(c2, 1, 1, nx, ny, coeff2);
302 cpl_imagelist_set(coeffs_imagelist, c2, 2);
303 cpl_image * c3 = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
304 cpl_image_fill_window(c3, 1, 1, nx, ny, coeff3);
305 cpl_imagelist_set(coeffs_imagelist, c3, 3);
307 cpl_mask * bpm = cpl_mask_new(nx, ny);
308 cpl_propertylist * lin_plist = cpl_propertylist_new();
310 double * ordered_coeffs = engl_repack(coeffs_imagelist);
312 gain_linearity * result = engl_gain_linearity_create(coeffs_imagelist,
319 if (cpl_error_get_code() != CPL_ERROR_NONE) {
320 engl_gain_linearity_delete(result);
341double * engl_repack(
const cpl_imagelist * lin_coeffs) {
343 const cpl_size fit_order = cpl_imagelist_get_size(lin_coeffs);
345 cpl_image_get_size_x(cpl_imagelist_get_const(lin_coeffs, 0));
347 cpl_image_get_size_y(cpl_imagelist_get_const(lin_coeffs, 0));
348 double * result = NULL;
351 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
352 cpl_ensure(cpl_image_get_type(cpl_imagelist_get_const(lin_coeffs, 0)) ==
353 CPL_TYPE_FLOAT, CPL_ERROR_UNSUPPORTED_MODE, NULL);
355 result = cpl_malloc(fit_order * nx * ny *
sizeof(*result));
359 for (cpl_size ipos=0 ; ipos < nx * ny ; ipos++) {
360 const cpl_size ordered_pos = ipos * fit_order + j;
361 result[ordered_pos] = 0.0;
365 for (; j < fit_order; j++) {
366 const cpl_image * fit_coeff_image = cpl_imagelist_get_const(lin_coeffs,
368 const float * fit_coeff_data = cpl_image_get_data_float_const(
370 for (cpl_size ipos=0 ; ipos < nx * ny ; ipos++) {
371 const cpl_size ordered_pos = ipos * fit_order + j;
372 result[ordered_pos] = fit_coeff_data[ipos];