34#include "moo_badpix.h"
56_moo_coadd_target_table(moo_scilist *scilist)
60 cpl_ensure(scilist != NULL, CPL_ERROR_NULL_INPUT, NULL);
62 cpl_ensure(size > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
63 cpl_errorstate prestate = cpl_errorstate_get();
69 for (
int i = 1; i < size; i++) {
76 if (!cpl_errorstate_is_equal(prestate)) {
85_moo_create_pheader(cpl_propertylist *header, moo_scilist *scilist)
87 cpl_ensure_code(header != NULL, CPL_ERROR_NULL_INPUT);
88 cpl_ensure_code(scilist != NULL, CPL_ERROR_NULL_INPUT);
91 double exptime_sum = 0.0;
93 for (
int i = 0; i < size; i++) {
97 exptime_sum += exptime;
100 double pexptime = exptime_sum;
102 cpl_propertylist_append_double(header, MOO_PFITS_EXPTIME, pexptime);
103 cpl_propertylist_set_comment(header, MOO_PFITS_EXPTIME,
104 MOO_PFITS_EXPTIME_C);
106 cpl_propertylist *sci_primary_header = sci->primary_header;
107 cpl_propertylist_copy_property_regexp(header, sci_primary_header,
109 cpl_propertylist_copy_property(header, sci_primary_header,
111 return CPL_ERROR_NONE;
134moo_coadd(moo_scilist *scilist, moo_coadd_params *params,
const char *filename)
138 _moo_target *targets = NULL;
140 cpl_ensure(scilist != NULL, CPL_ERROR_NULL_INPUT, NULL);
141 cpl_ensure(params != NULL, CPL_ERROR_NULL_INPUT, NULL);
142 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
144 cpl_errorstate prestate = cpl_errorstate_get();
146 unsigned int badpix_level =
150 cpl_ensure(size > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
151 cpl_msg_info(__func__,
"Coadding %d frames with method %s", size,
155 cpl_frame *frame = cpl_frame_new();
156 cpl_frame_set_filename(frame, filename);
159 moo_sci_load(scif, badpix_level);
165 moo_sci_load(sci, badpix_level);
167 cpl_frame_delete(frame);
170 const char **targnames = NULL;
173 sci->filename = filename;
174 sci->primary_header = cpl_propertylist_new();
175 moo_try_check(_moo_create_pheader(sci->primary_header, scilist),
" ");
178 moo_try_check(sci_target_table = _moo_coadd_target_table(scilist),
" ");
179 sci->target_table = sci_target_table;
183 ntarget = cpl_table_get_nrow(sci_target_table->table);
185 moo_try_check(targnames = cpl_table_get_data_string_const(
186 sci_target_table->table, MOO_TARGET_TABLE_TARGNAME),
189 targets = cpl_calloc(ntarget,
sizeof(_moo_target));
191 for (
int i = 0; i < ntarget; i++) {
192 const char *targname = targnames[i];
194 targets[i].indexes = cpl_calloc(size,
sizeof(
int));
196 double exptimeri = 0;
197 double exptimeyj = 0;
200 for (
int j = 0; j < size; j++) {
205 targets[i].name = targname;
206 targets[i].indexes[j] = indextarg;
211 cpl_table_get_double(ttable->table,
212 MOO_TARGET_TABLE_EXPTIMERI, tindex,
215 cpl_table_get_double(ttable->table,
216 MOO_TARGET_TABLE_EXPTIMEYJ, tindex,
218 exptimeh += cpl_table_get_double(ttable->table,
219 MOO_TARGET_TABLE_EXPTIMEH,
223 moo_try_assure(nb > 0, CPL_ERROR_UNSPECIFIED,
"Target %s not found",
226 cpl_table_set_double(sci_target_table->table,
227 MOO_TARGET_TABLE_EXPTIMERI, i, exptimeri);
228 cpl_table_set_double(sci_target_table->table,
229 MOO_TARGET_TABLE_EXPTIMEYJ, i, exptimeyj);
230 cpl_table_set_double(sci_target_table->table,
231 MOO_TARGET_TABLE_EXPTIMEH, i, exptimeh);
234 cpl_msg_indent_more();
237 for (
int i = 0; i < 3; i++) {
238 moo_sci_single *sci_single = NULL;
241 if (first_sci_single != NULL) {
243 cpl_propertylist *first_header =
249 int nx = hdrl_image_get_size_x(fimage);
251 sci_single->image = hdrl_image_new(nx, ntarget);
252 cpl_propertylist_append(sci_single->header, first_header);
253 sci_single->sky = cpl_image_new(nx, ntarget, CPL_TYPE_DOUBLE);
254 sci_single->qual = cpl_image_new(nx, ntarget, CPL_TYPE_INT);
256 for (
int j = 0; j < ntarget; j++) {
257 _moo_target target = targets[j];
258 hdrl_imagelist *list = hdrl_imagelist_new();
259 cpl_imagelist *qlist = cpl_imagelist_new();
260 cpl_imagelist *slist = cpl_imagelist_new();
262 for (
int k = 0; k < size; k++) {
263 int idx = target.indexes[k];
264 moo_sci_single *current_single =
267 hdrl_image *current =
269 cpl_image *qcurrent =
271 cpl_image *scurrent =
274 int cnx = hdrl_image_get_size_x(current);
276 hdrl_image_extract(current, 1, idx, cnx, idx);
278 cpl_image_extract(qcurrent, 1, idx, cnx, idx);
280 cpl_image_extract(scurrent, 1, idx, cnx, idx);
282 int pos = hdrl_imagelist_get_size(list);
284 hdrl_imagelist_set(list, sub, pos);
285 cpl_imagelist_set(qlist, qsub, pos);
286 cpl_imagelist_set(slist, ssub, pos);
289 hdrl_image *out = NULL;
290 cpl_image *contrib = NULL;
291 cpl_image *skyout = NULL;
292 cpl_image *qout = NULL;
295 if (strcmp(params->method, MOO_COADD_METHOD_MEDIAN) == 0) {
296 hdrl_imagelist_collapse_median(list, &out, &contrib);
297 skyout = cpl_imagelist_collapse_median_create(slist);
300 else if (strcmp(params->method, MOO_COADD_METHOD_MEAN) ==
302 hdrl_imagelist_collapse_mean(list, &out, &contrib);
303 skyout = cpl_imagelist_collapse_create(slist);
307 hdrl_imagelist_collapse_sigclip(list,
311 &out, &contrib, NULL,
313 skyout = cpl_imagelist_collapse_sigclip_create(
314 slist, params->clip_kappa, params->clip_kappa, 0.7,
315 CPL_COLLAPSE_MEAN, NULL);
320 hdrl_image_copy(sci_single->image, out, 1, j + 1);
321 cpl_image_copy(sci_single->sky, skyout, 1, j + 1);
322 cpl_image_copy(sci_single->qual, qout, 1, j + 1);
323 hdrl_image_delete(out);
324 cpl_image_delete(qout);
325 cpl_image_delete(skyout);
326 cpl_image_delete(contrib);
328 hdrl_imagelist_delete(list);
329 cpl_imagelist_delete(qlist);
330 cpl_imagelist_delete(slist);
337 cpl_msg_indent_less();
343 if (targets != NULL) {
344 for (
int i = 0; i < ntarget; i++) {
345 cpl_free(targets[i].indexes);
349 if (!cpl_errorstate_is_equal(prestate)) {
350 cpl_msg_error(__func__,
"Error in coadding frames");
351 cpl_errorstate_dump(prestate, CPL_FALSE, cpl_errorstate_dump_one);
#define MOO_BADPIX_OUTSIDE_DATA_RANGE
#define MOO_BADPIX_CALIB_DEFECT
cpl_error_code moo_fits_create(const char *filename)
Create a new fits file with empty propertylist.
moo_sci_single * moo_sci_single_new(moo_detector_type type)
Create a new moo_sci_single.
cpl_image * moo_sci_single_get_sky(moo_sci_single *self)
Get sky of sci single.
hdrl_image * moo_sci_single_get_image(moo_sci_single *self)
Get image of SCI_SINGLE.
cpl_image * moo_sci_single_get_qual(moo_sci_single *self)
Get image of qual.
cpl_propertylist * moo_sci_single_get_header(moo_sci_single *self)
Get header of sci single.
cpl_error_code moo_sci_add_target_table(moo_sci *self, moo_target_table *table)
Add target table to SCI file and update moo_sci structure.
void moo_sci_delete(moo_sci *self)
Delete a moo_sci.
moo_sci * moo_sci_new(void)
Create a new moo_sci.
moo_sci * moo_sci_create(const cpl_frame *frame)
Create a new empty SCI filename.
cpl_error_code moo_sci_add_single(moo_sci *self, moo_detector_type type, moo_sci_single *single)
Add SCI_SINGLE extension to SCI file and update moo_sci structure.
moo_sci_single * moo_sci_get_single(moo_sci *self, moo_detector_type type)
Get the type part in SCI and return it.
void moo_sci_save(moo_sci *self, const char *filename)
Save a moo_sci to a FITS file.
moo_target_table * moo_sci_get_target_table(moo_sci *self)
Get the target table of SCI file.
moo_sci * moo_scilist_get(moo_scilist *self, int i)
Get the SCI at the position i in the list.
moo_sci_single * moo_scilist_get_single(moo_scilist *self, int i, moo_detector_type type)
Get the SCI SINGLE at the position i in the list for the given detector.
cpl_error_code moo_scilist_load_single(const moo_scilist *self, moo_detector_type type, int level)
Load the type part for all SCI in the scilist.
cpl_size moo_scilist_get_size(const moo_scilist *self)
Get the number of SCI in the scilist.
void moo_target_table_delete(moo_target_table *self)
Delete a moo_target_table.
moo_target_table * moo_target_table_duplicate(moo_target_table *self)
Duplicate an existing target_table.
cpl_error_code moo_target_table_merge(moo_target_table *self, moo_target_table *tomerge)
Merge two TARGET_TABLE.
int moo_target_table_find_index(moo_target_table *self, const char *targname)
find target indexspec in TARGET_TABLE
int moo_target_table_find_target(moo_target_table *self, const char *targname)
find target targindex in TARGET_TABLE
cpl_error_code moo_target_table_remove_coadd_cols(moo_target_table *self)
remove columns for coadd in target_table
moo_sci * moo_coadd(moo_scilist *scilist, moo_coadd_params *params, const char *filename)
Co-add reduced science frames in SCI format.
cpl_error_code moo_pfits_update_ncoadd(cpl_propertylist *plist, int v)
set the ESO PRO NCOADD value
double moo_pfits_get_exptime(const cpl_propertylist *plist)
find out the EXPTIME value
cpl_image * moo_imagelist_collapse_bitwiseor(cpl_imagelist *list, hdrl_imagelist *datalist)
get the QUAL resulting in a bitwise OR operation on the QUAL list
the different type of detectors