35#include "moo_params.h"
36#include "moo_badpix.h"
37#include "moo_single.h"
39#include "moo_detlist.h"
41#include "moo_detector.h"
43#include "moo_prepare.h"
58_moo_prepare_get_qual_extension(
const char *filename,
64 cpl_image *rqual = NULL;
65 cpl_image *qual = NULL;
68 int nx = cpl_image_get_size_x(data);
69 int ny = cpl_image_get_size_y(data);
71 if (filename != NULL) {
74 int qnx = cpl_image_get_size_x(rqual);
75 int qny = cpl_image_get_size_y(rqual);
77 if (qnx == nx && qny == ny) {
78 qual = cpl_image_duplicate(rqual);
83 cpl_image_delete(rqual);
86 qual = cpl_image_new(nx, ny, CPL_TYPE_INT);
93_moo_prepare_image_output(cpl_image *idata, moo_output out)
95 cpl_ensure_code(idata != NULL, CPL_ERROR_NULL_INPUT);
100 double gain = out.gain;
102 int size_x = cpl_image_get_size_x(idata);
104 double *data = cpl_image_get_data_double(idata);
106 for (
int j = y; j < (y + ny); j++) {
107 for (
int i = x; i < (x + nx); i++) {
108 data[i + j * size_x] = MOO_TO_ELECTRON(data[i + j * size_x], gain);
112 return CPL_ERROR_NONE;
116_moo_prepare_error_output(cpl_image *ierr,
121 cpl_ensure_code(ierr != NULL, CPL_ERROR_NULL_INPUT);
126 double ron = out.ron;
127 double *err = cpl_image_get_data_double(ierr);
128 int size_x = cpl_image_get_size_x(ierr);
129 const double *data = cpl_image_get_data_double_const(idata);
132 const double *biasdata = cpl_image_get_data_double_const(ibias);
133 for (
int j = y; j < (y + ny); j++) {
134 for (
int i = x; i < (x + nx); i++) {
135 int idx = i + j * size_x;
136 err[idx] = sqrt(ron * ron + fabs(data[idx] - biasdata[idx]));
141 for (
int j = y; j < (y + ny); j++) {
142 for (
int i = x; i < (x + nx); i++) {
143 int idx = i + j * size_x;
149 return CPL_ERROR_NONE;
153_moo_prepare_single_adu(moo_single *single,
const char *
const qual_filename_rp)
155 cpl_image *data = NULL;
156 cpl_image *rdata = NULL;
157 cpl_image *err = NULL;
158 cpl_image *qual_rp = NULL;
159 moo_outputs *outputs = NULL;
161 cpl_ensure_code(single != NULL, CPL_ERROR_NULL_INPUT);
163 const char *extname = single->extname;
164 int num = single->ntas;
166 cpl_propertylist *header = moo_single_get_header(single);
168 cpl_propertylist_update_string(header, MOO_PFITS_BUNIT,
169 MOO_PREPARE_ADU_BUNIT);
171 outputs = moo_outputs_load(header, single->type);
176 moo_outputs_get_det_size(outputs, &nx, &ny);
178 moo_try_check(rdata =
180 extname, CPL_TYPE_DOUBLE),
183 cpl_msg_info(
"moo_prepare",
" Creating %s", extname);
184 cpl_msg_info(
"moo_prepare",
" Creating ERR_%s", extname);
186 err = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
187 single->image = hdrl_image_create(data, err);
188 cpl_msg_info(
"moo_prepare",
" Creating QUAL_%s", extname);
189 moo_try_check(qual_rp = _moo_prepare_get_qual_extension(qual_filename_rp,
193 single->qual = qual_rp;
196 cpl_image_delete(rdata);
197 cpl_image_delete(data);
198 cpl_image_delete(err);
199 moo_outputs_delete(outputs);
200 return CPL_ERROR_NONE;
204_moo_prepare_single(moo_single *single,
205 const char *
const qual_filename_nl,
206 const char *
const masterbias_filename)
208 cpl_image *data = NULL;
209 cpl_image *err = NULL;
210 cpl_image *biasdata = NULL;
211 cpl_image *qual_nl = NULL;
212 moo_outputs *outputs = NULL;
214 cpl_ensure_code(single != NULL, CPL_ERROR_NULL_INPUT);
216 const char *extname = single->extname;
218 cpl_propertylist *header = moo_single_get_header(single);
219 if (!cpl_propertylist_has(header, MOO_PFITS_CDELT1)) {
221 cpl_propertylist_append_double(header, MOO_PFITS_CDELT1, cd1_1);
223 if (!cpl_propertylist_has(header, MOO_PFITS_CDELT2)) {
225 cpl_propertylist_append_double(header, MOO_PFITS_CDELT2, cd2_2);
227 cpl_propertylist_update_string(header, MOO_PFITS_BUNIT,
228 MOO_PREPARE_COUNT_BUNIT);
230 outputs = moo_outputs_load(header, single->type);
233 data = hdrl_image_get_image(image);
234 err = hdrl_image_get_error(image);
236 cpl_msg_info(
"moo_prepare",
" Creating %s", extname);
238 for (
int i = 0; i < outputs->nb; i++) {
240 moo_pfits_set_det_outi_x(header, i + 1, outputs->outputs[i].dx);
241 moo_pfits_set_det_outi_y(header, i + 1, outputs->outputs[i].dy);
244 moo_pfits_set_det_chip_outi_x(header, i + 1,
245 outputs->outputs[i].dx);
246 moo_pfits_set_det_chip_outi_y(header, i + 1,
247 outputs->outputs[i].dy);
249 _moo_prepare_image_output(data, outputs->outputs[i]);
252 cpl_msg_info(
"moo_prepare",
" Creating ERR_%s", extname);
255 if (masterbias_filename != NULL) {
257 extname, CPL_TYPE_DOUBLE);
261 int nx = cpl_image_get_size_x(data);
262 int ny = cpl_image_get_size_y(data);
263 biasdata = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
266 for (
int i = 0; i < outputs->nb; i++) {
267 _moo_prepare_error_output(err, data, biasdata, outputs->outputs[i]);
270 cpl_msg_info(
"moo_prepare",
" Creating QUAL_%s", extname);
271 moo_try_check(qual_nl = _moo_prepare_get_qual_extension(qual_filename_nl,
276 cpl_image_or(single->qual, single->qual, qual_nl);
278 if (single->type ==
MOO_TYPE_RI && masterbias_filename != NULL) {
282 cpl_image_or(single->qual, single->qual, bqual);
283 cpl_image_delete(bqual);
287 if (biasdata != NULL) {
288 cpl_image_delete(biasdata);
290 cpl_image_delete(qual_nl);
291 moo_outputs_delete(outputs);
292 return CPL_ERROR_NONE;
325 const char *
const badpixmask_rp,
326 const char *
const badpixmask_nl,
327 const cpl_frame *masterbias,
328 const cpl_frame *cube_frame,
329 moo_prepare_params *params)
331 moo_det *dres = NULL;
333 const char *masterbias_filename = NULL;
335 cpl_errorstate prestate = cpl_errorstate_get();
337 cpl_ensure(rawframe != NULL, CPL_ERROR_NULL_INPUT, NULL);
339 const char *filename = cpl_frame_get_filename(rawframe);
340 const char *tag = cpl_frame_get_tag(rawframe);
341 cpl_frame_group group = cpl_frame_get_group(rawframe);
342 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
343 cpl_ensure(tag != NULL, CPL_ERROR_NULL_INPUT, NULL);
344 cpl_ensure(group == CPL_FRAME_GROUP_RAW, CPL_ERROR_ILLEGAL_INPUT, NULL);
347#if DEBUG_PREPARE_ADU_LIN
352 cpl_msg_info(__func__,
"Preparing %s", filename);
354 cpl_msg_indent_more();
355 if (badpixmask_nl != NULL) {
356 cpl_msg_info(__func__,
"Using BP_MAP_NL %s", badpixmask_nl);
359 if (masterbias != NULL) {
360 masterbias_filename = cpl_frame_get_filename(masterbias);
361 cpl_msg_info(__func__,
"Using MASTER_BIAS %s", masterbias_filename);
363 for (
int i = 0; i < 3; i++) {
364 for (
int j = 1; j <= 2; j++) {
366 if (single != NULL) {
367 int ignored = params->ignore_detector[i + (j - 1) * 3];
369 moo_try_check(_moo_prepare_single(single, badpixmask_nl,
370 masterbias_filename),
377 cpl_msg_indent_less();
378#if DEBUG_PREPARE_ADU_LIN
384 if (!cpl_errorstate_is_equal(prestate)) {
387 cpl_msg_error(__func__,
"Error in prepare");
388 cpl_errorstate_dump(prestate, CPL_FALSE, cpl_errorstate_dump_one);
389 cpl_errorstate_set(prestate);
395_moo_create_primary_header(cpl_propertylist *header,
const char *filename)
397 cpl_error_code status = CPL_ERROR_NONE;
431 const char *
const badpixmask_rp,
432 moo_prepare_params *params)
434 moo_det *dres = NULL;
437 cpl_errorstate prestate = cpl_errorstate_get();
439 cpl_ensure(rawframe != NULL, CPL_ERROR_NULL_INPUT, NULL);
441 const char *filename = cpl_frame_get_filename(rawframe);
442 const char *tag = cpl_frame_get_tag(rawframe);
443 cpl_frame_group group = cpl_frame_get_group(rawframe);
444 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
445 cpl_ensure(tag != NULL, CPL_ERROR_NULL_INPUT, NULL);
446 cpl_ensure(group == CPL_FRAME_GROUP_RAW, CPL_ERROR_ILLEGAL_INPUT, NULL);
449 _moo_create_primary_header(dres->primary_header, filename);
451 cpl_msg_info(__func__,
"Preparing in ADU %s", filename);
452 cpl_msg_indent_more();
453 if (badpixmask_rp != NULL) {
454 cpl_msg_info(__func__,
"Using BP_MAP_RP %s", badpixmask_rp);
457 for (
int i = 0; i < 3; i++) {
458 for (
int j = 1; j <= 2; j++) {
460 int ignored = params->ignore_detector[i + (j - 1) * 3];
461 if (single != NULL) {
462 cpl_propertylist *header = moo_single_get_header(single);
464 ignored = ignored || !live;
466 ignored = ignored || !clive;
468 moo_try_check(_moo_prepare_single_adu(single,
474 moo_det_set_single(dres, i, j, NULL);
480 cpl_msg_indent_less();
490 if (!cpl_errorstate_is_equal(prestate)) {
493 cpl_msg_error(__func__,
"Error in prepare");
494 cpl_errorstate_dump(prestate, CPL_FALSE, cpl_errorstate_dump_one);
495 cpl_errorstate_set(prestate);
moo_det * moo_det_create(const cpl_frame *frame)
Create a new moo_det from the given DET frame.
void moo_det_save(moo_det *self, const char *filename)
Save a moo_det to a FITS file.
void moo_det_delete(moo_det *self)
Delete a moo_det.
moo_single * moo_det_get_single(moo_det *self, moo_detector_type type, int num)
Get the type part in DET and return it.
const char * moo_detector_get_extname(moo_detector_type type, int ntas)
Get the extension name of a detector.
enum _moo_detector_type_ moo_detector_type
The type code type.
cpl_image * moo_fits_load_extension_image(const char *filename, const char *name, const char *detectorname, cpl_type type)
Load an image from FITS file.
hdrl_image * moo_single_get_image(moo_single *self)
Get the IMAGE part (DATA,ERR) of single DET.
moo_det * moo_prepare(const cpl_frame *rawframe, const char *const badpixmask_rp, const char *const badpixmask_nl, const cpl_frame *masterbias, const cpl_frame *cube_frame, moo_prepare_params *params)
This function transforms RAW frames in DET frames attaching the default bad pixel map and an error im...
moo_det * moo_prepare_adu(const cpl_frame *rawframe, const char *const badpixmask_rp, moo_prepare_params *params)
This function transforms RAW frames in DET ADU frames attaching the default bad pixel.
cpl_error_code moo_correct_detlin(moo_det *det, const cpl_frame *cube_frame)
Apply the detector linearity correction.
double moo_pfits_get_cd1_1(const cpl_propertylist *plist)
find out the CD1_1 value
int moo_pfits_get_det_chip_ny(const cpl_propertylist *plist)
find out the ESO DET CHIP NY value
int moo_pfits_get_det_chip_live(const cpl_propertylist *plist)
find out the ESO DET CHIP LIVE value
cpl_error_code moo_pfits_append_hduclass_data(cpl_propertylist *plist, moo_detector_type type, int ntas)
Set the HDUCLASS DATA Keyword.
int moo_pfits_get_live(const cpl_propertylist *plist)
find out the ESO DET LIVE value
int moo_pfits_get_det_chip_nx(const cpl_propertylist *plist)
find out the ESO DET CHIP NX value
cpl_image * moo_outputs_create_det(moo_outputs *self, cpl_image *raw, int nx, int ny, moo_detector_type dtype)
Create the DET data from RAW header and DATA.
double moo_pfits_get_cd2_2(const cpl_propertylist *plist)
find out the CD2_2 value
cpl_error_code moo_qc_set_is_linearcor(cpl_propertylist *plist, cpl_boolean val)
Set the QC.IS.LINEARCOR value.
cpl_error_code moo_qc_set_frame_raw1(cpl_propertylist *plist, const char *val)
Set the QC.FRAME.RAW1 value.
cpl_error_code moo_qc_set_is_p2pcor(cpl_propertylist *plist, cpl_boolean val)
Set the QC.IS.P2PCOR value.