31#include "moo_combine_pair.h"
32#include "moo_sub_sky_stare.h"
33#include "moo_fibres_table.h"
36#include "moo_badpix.h"
38#include "moo_skycorr.h"
39#include "moo_products.h"
52static moo_sci_single *
53_moo_create_sci2d_single(moo_rbn_single *nod_rbn_single,
54 moo_rbn_single *objb_rbn_single,
55 moo_rbn_single *obja_rbn_single,
56 cpl_table *nod_fibret,
60 moo_sci_single *sci_single = NULL;
62 cpl_errorstate prestate = cpl_errorstate_get();
64 cpl_ensure(nod_rbn_single != NULL, CPL_ERROR_NULL_INPUT, NULL);
65 cpl_ensure(objb_rbn_single != NULL, CPL_ERROR_NULL_INPUT, NULL);
66 cpl_ensure(target_table != NULL, CPL_ERROR_NULL_INPUT, NULL);
68 int nb_target = cpl_table_get_nrow(target_table->table);
76 cpl_propertylist_copy_property(sci_single->header,
77 nod_rbn_single->header,
79 cpl_propertylist_copy_property(sci_single->header,
80 nod_rbn_single->header,
82 cpl_propertylist_copy_property(sci_single->header,
83 nod_rbn_single->header, MOO_PFITS_CD1_1);
84 cpl_propertylist_copy_property(sci_single->header,
85 nod_rbn_single->header, MOO_PFITS_CD1_2);
86 cpl_propertylist_copy_property(sci_single->header,
87 nod_rbn_single->header,
89 cpl_propertylist_copy_property(sci_single->header,
90 nod_rbn_single->header,
92 cpl_propertylist_copy_property(sci_single->header,
93 nod_rbn_single->header, MOO_PFITS_BUNIT);
94 cpl_propertylist_copy_property(sci_single->header,
95 nod_rbn_single->header,
97 cpl_propertylist_copy_property(sci_single->header,
98 nod_rbn_single->header,
100 cpl_propertylist_copy_property(sci_single->header,
101 nod_rbn_single->header, MOO_PFITS_CD2_1);
102 cpl_propertylist_copy_property(sci_single->header,
103 nod_rbn_single->header, MOO_PFITS_CD2_2);
104 cpl_propertylist_copy_property(sci_single->header,
105 nod_rbn_single->header,
107 cpl_propertylist_copy_property(sci_single->header,
108 nod_rbn_single->header,
110 cpl_propertylist_copy_property(sci_single->header,
111 nod_rbn_single->header,
113 cpl_propertylist_copy_property(sci_single->header,
114 nod_rbn_single->header,
116 int nx = hdrl_image_get_size_x(nod_rbn_single->image);
118 sci_single->image = hdrl_image_new(nx, nb_target);
119 sci_single->sky = cpl_image_new(nx, nb_target, CPL_TYPE_DOUBLE);
120 sci_single->qual = cpl_image_new(nx, nb_target, CPL_TYPE_INT);
122 cpl_image *sci_img = hdrl_image_get_image(sci_single->image);
124 cpl_image_get_bpm(sci_img);
126 cpl_image *sci_err_img = hdrl_image_get_error(sci_single->image);
127 cpl_image *sci_qual_img = sci_single->qual;
129 cpl_image *objb_rbn_img = hdrl_image_get_image(objb_rbn_single->image);
130 cpl_image *obja_rbn_img = NULL;
132 if (obja_rbn_single != NULL) {
133 obja_rbn_img = hdrl_image_get_image(obja_rbn_single->image);
136 cpl_image *nod_rbn_img = hdrl_image_get_image(nod_rbn_single->image);
137 cpl_image *nod_rbn_err_img =
138 hdrl_image_get_error(nod_rbn_single->image);
139 cpl_image *nod_rbn_qual_img = nod_rbn_single->qual;
141 const char *transname[] = { MOO_TARGET_TABLE_TRANSRI,
142 MOO_TARGET_TABLE_TRANSYJ,
143 MOO_TARGET_TABLE_TRANSH };
144 const char *ptransname[] = { MOO_TARGET_TABLE_PTRANSRI,
145 MOO_TARGET_TABLE_PTRANSYJ,
146 MOO_TARGET_TABLE_PTRANSH };
148 cpl_table_get_data_float(target_table->table,
149 transname[nod_rbn_single->type]);
151 cpl_table_get_data_float(target_table->table,
152 ptransname[nod_rbn_single->type]);
153 int *indexrbnt = cpl_table_get_data_int(target_table->table,
154 MOO_TARGET_TABLE_INDEXRBN);
156 cpl_table_get_data_int(target_table->table,
157 MOO_TARGET_TABLE_PAIREDINDEXRBN);
158 char **obstypet = cpl_table_get_data_string(target_table->table,
159 MOO_TARGET_TABLE_OBSTYPE);
161 for (
int i = 1; i <= nb_target; i++) {
163 int indexrbn = indexrbnt[idx];
165 cpl_table_set_int(target_table->table, MOO_TARGET_TABLE_INDEXTARG,
167 cpl_table_set_int(target_table->table, MOO_TARGET_TABLE_NBSKY, idx,
169 cpl_table_set_int(target_table->table,
170 MOO_TARGET_TABLE_MAX_DIST_SLIT, idx, 0);
171 cpl_table_set_float(target_table->table,
172 MOO_TARGET_TABLE_MAX_DIST_SKY, idx, 0);
173 cpl_table_set_string(target_table->table,
174 MOO_TARGET_TABLE_SUBSKYMODE, idx,
175 MOO_TARGET_TABLE_SUBSKY_NOD);
177 cpl_table_select_all(nod_fibret);
178 cpl_table_and_selected_int(nod_fibret, MOO_FIBRES_TABLE_INDEXRBN,
179 CPL_EQUAL_TO, indexrbn);
180 cpl_array *sel = cpl_table_where_selected(nod_fibret);
181 int ft_idx = cpl_array_get_cplsize(sel, 0, NULL);
182 moo_target_table_set_snr(target_table, idx, nod_fibret, ft_idx);
183 cpl_array_delete(sel);
193#if (__GNUC__ == 9) && (__GNUC_MINOR__ < 3)
194#pragma omp parallel shared(nb_target, indexrbnt, pindexrbnt, transt, ptranst, \
195 obstypet, target_table, nod_rbn_single, \
196 objb_rbn_img, obja_rbn_img, sci_single, \
197 nod_rbn_qual_img, sci_qual_img, nod_rbn_img, \
198 sci_img, nod_rbn_err_img, sci_err_img, nx, \
201#pragma omp parallel default(none) \
202 shared(nb_target, indexrbnt, pindexrbnt, transt, ptranst, obstypet, \
203 target_table, nod_rbn_single, objb_rbn_img, obja_rbn_img, \
204 sci_single, nod_rbn_qual_img, sci_qual_img, nod_rbn_img, \
205 sci_img, nod_rbn_err_img, sci_err_img, nx, ispaired)
210 for (
int i = 1; i <= nb_target; i++) {
212 int indexrbn = indexrbnt[idx];
213 char *obstype = obstypet[idx];
215 if ((strcmp(obstype, MOO_OBSTYPE_OBSPNOD) == 0) ||
216 (strcmp(obstype, MOO_OBSTYPE_OBSPNODSTARE) == 0)) {
217 for (
int j = 1; j <= nx; j++) {
220 cpl_image_get(obja_rbn_img, j, indexrbn, &rej);
221 cpl_image_set(sci_single->sky, j, i, sky_val);
223 cpl_image_get(nod_rbn_img, j, indexrbn, &rej);
225 cpl_image_set(sci_img, j, i, NAN);
226 cpl_image_set(sci_err_img, j, i, NAN);
227 cpl_image_reject(sci_img, j, i);
230 double sci_flux = -rbn_img_val;
231 cpl_image_set(sci_img, j, i, sci_flux);
233 cpl_image_get(nod_rbn_err_img, j, indexrbn,
235 double err = rbn_err_val;
236 cpl_image_set(sci_err_img, j, i, err);
238 double rbn_qual_val =
239 cpl_image_get(nod_rbn_qual_img, j, indexrbn, &rej);
240 cpl_image_set(sci_qual_img, j, i, rbn_qual_val);
243 else if (strcmp(obstype, MOO_OBSTYPE_OBSSWITCH) == 0) {
246 cpl_image *sky_img = objb_rbn_img;
249 sky_img = obja_rbn_img;
251 for (
int j = 1; j <= nx; j++) {
255 cpl_image_get(sky_img, j, indexrbn, &rej);
256 cpl_image_set(sci_single->sky, j, i, sky_val);
258 double nod_rbn_img_val =
260 cpl_image_get(nod_rbn_img, j, indexrbn, &rej);
261 double nod_rbn_qual_val =
262 cpl_image_get(nod_rbn_qual_img, j, indexrbn, &rerr);
263 double nod_rbn_err_val =
264 cpl_image_get(nod_rbn_err_img, j, indexrbn, &rerr);
267 cpl_image_reject(sci_img, j, i);
268 nod_rbn_img_val = NAN;
269 nod_rbn_err_val = NAN;
271 cpl_image_set(sci_img, j, i, nod_rbn_img_val);
272 cpl_image_set(sci_err_img, j, i, nod_rbn_err_val);
273 cpl_image_set(sci_qual_img, j, i, nod_rbn_qual_val);
277 for (
int j = 1; j <= nx; j++) {
281 cpl_image_get(objb_rbn_img, j, indexrbn, &rej);
282 cpl_image_set(sci_single->sky, j, i, sky_val);
284 cpl_image_get(nod_rbn_img, j, indexrbn, &rej);
286 cpl_image_set(sci_img, j, i, NAN);
287 cpl_image_set(sci_err_img, j, i, NAN);
288 cpl_image_reject(sci_img, j, i);
291 double sci_flux = rbn_img_val;
292 cpl_image_set(sci_img, j, i, sci_flux);
294 cpl_image_get(nod_rbn_err_img, j, indexrbn,
296 double err = rbn_err_val;
297 cpl_image_set(sci_err_img, j, i, err);
299 double rbn_qual_val =
300 cpl_image_get(nod_rbn_qual_img, j, indexrbn, &rej);
301 cpl_image_set(sci_qual_img, j, i, rbn_qual_val);
310 if (!cpl_errorstate_is_equal(prestate)) {
311 cpl_error_set_message(__func__, CPL_ERROR_ILLEGAL_OUTPUT,
312 "Error in combine pair %s",
313 nod_rbn_single->extname);
320static moo_sci_single *
321_moo_combine_pair_2d_single(moo_rbn_single *nod_rbn_single,
322 moo_rbn_single *objb_rbn_single,
323 moo_rbn_single *obja_rbn_single,
324 cpl_table *nod_fibret,
327 moo_sci_single *sci_single = NULL;
329 cpl_errorstate prestate = cpl_errorstate_get();
331 cpl_ensure(nod_rbn_single != NULL, CPL_ERROR_NULL_INPUT, NULL);
332 cpl_ensure(objb_rbn_single != NULL, CPL_ERROR_NULL_INPUT, NULL);
333 cpl_ensure(target_table != NULL, CPL_ERROR_NULL_INPUT, NULL);
338 cpl_propertylist_copy_property(sci_single->header, nod_rbn_single->header,
340 cpl_propertylist_copy_property(sci_single->header, nod_rbn_single->header,
342 cpl_propertylist_copy_property(sci_single->header, nod_rbn_single->header,
344 cpl_propertylist_copy_property(sci_single->header, nod_rbn_single->header,
346 cpl_propertylist_copy_property(sci_single->header, nod_rbn_single->header,
348 cpl_propertylist_copy_property(sci_single->header, nod_rbn_single->header,
350 cpl_propertylist_copy_property(sci_single->header, nod_rbn_single->header,
352 cpl_propertylist_copy_property(sci_single->header, nod_rbn_single->header,
354 cpl_propertylist_copy_property(sci_single->header, nod_rbn_single->header,
356 cpl_propertylist_copy_property(sci_single->header, nod_rbn_single->header,
358 cpl_propertylist_copy_property(sci_single->header, nod_rbn_single->header,
360 cpl_propertylist_copy_property(sci_single->header, nod_rbn_single->header,
362 cpl_propertylist_copy_property(sci_single->header, nod_rbn_single->header,
364 cpl_propertylist_copy_property(sci_single->header, nod_rbn_single->header,
366 cpl_propertylist_copy_property(sci_single->header, nod_rbn_single->header,
368 cpl_propertylist_copy_property(sci_single->header, nod_rbn_single->header,
370 int nx = hdrl_image_get_size_x(nod_rbn_single->image);
371 int nb_target = cpl_table_get_nrow(target_table->table);
373 sci_single->image = hdrl_image_new(nx, nb_target);
374 sci_single->sky = cpl_image_new(nx, nb_target, CPL_TYPE_DOUBLE);
375 sci_single->qual = cpl_image_new(nx, nb_target, CPL_TYPE_INT);
377 cpl_image *sci_img = hdrl_image_get_image(sci_single->image);
379 cpl_image_get_bpm(sci_img);
381 cpl_image *sci_err_img = hdrl_image_get_error(sci_single->image);
382 cpl_image *sci_qual_img = sci_single->qual;
384 cpl_image *objb_rbn_img = hdrl_image_get_image(objb_rbn_single->image);
385 cpl_image *obja_rbn_img = hdrl_image_get_image(obja_rbn_single->image);
387 cpl_image *nod_rbn_img = hdrl_image_get_image(nod_rbn_single->image);
388 cpl_image *nod_rbn_err_img = hdrl_image_get_error(nod_rbn_single->image);
389 cpl_image *nod_rbn_qual_img = nod_rbn_single->qual;
391 const char *transname[] = { MOO_TARGET_TABLE_TRANSRI,
392 MOO_TARGET_TABLE_TRANSYJ,
393 MOO_TARGET_TABLE_TRANSH };
394 const char *ptransname[] = { MOO_TARGET_TABLE_PTRANSRI,
395 MOO_TARGET_TABLE_PTRANSYJ,
396 MOO_TARGET_TABLE_PTRANSH };
397 float *transt = cpl_table_get_data_float(target_table->table,
398 transname[nod_rbn_single->type]);
399 float *ptranst = cpl_table_get_data_float(target_table->table,
400 ptransname[nod_rbn_single->type]);
402 cpl_table_get_data_int(target_table->table, MOO_TARGET_TABLE_INDEXRBN);
403 int *pindexrbnt = cpl_table_get_data_int(target_table->table,
404 MOO_TARGET_TABLE_PAIREDINDEXRBN);
405 char **obstypet = cpl_table_get_data_string(target_table->table,
406 MOO_TARGET_TABLE_OBSTYPE);
408 cpl_table_get_data_int(target_table->table, MOO_TARGET_TABLE_NBSKY);
409 int *maxdistslitt = cpl_table_get_data_int(target_table->table,
410 MOO_TARGET_TABLE_MAX_DIST_SLIT);
412 cpl_table_get_data_float(target_table->table,
413 MOO_TARGET_TABLE_MAX_DIST_SKY);
415 cpl_table_get_data_int(target_table->table, MOO_TARGET_TABLE_PNBSKY);
417 cpl_table_get_data_int(target_table->table,
418 MOO_TARGET_TABLE_PMAX_DIST_SLIT);
419 float *pmaxdistskyt =
420 cpl_table_get_data_float(target_table->table,
421 MOO_TARGET_TABLE_PMAX_DIST_SKY);
423 for (
int i = 1; i <= nb_target; i++) {
424 int indexrbn = indexrbnt[i - 1];
425 int pindexrbn = pindexrbnt[i - 1];
427 cpl_table_set_int(target_table->table, MOO_TARGET_TABLE_INDEXTARG,
429 cpl_table_set_string(target_table->table, MOO_TARGET_TABLE_SUBSKYMODE,
430 i - 1, MOO_TARGET_TABLE_SUBSKY_NOD);
431 char *obstype = obstypet[i - 1];
433 if (strcmp(obstype, MOO_OBSTYPE_OBSPNODSTARE) == 0) {
434 indexrbn = pindexrbn;
436 pmaxdistslitt[i - 1] = 0;
437 pmaxdistskyt[i - 1] = 0;
441 maxdistslitt[i - 1] = 0;
442 maxdistskyt[i - 1] = 0;
444 cpl_table_select_all(nod_fibret);
445 cpl_table_and_selected_int(nod_fibret, MOO_FIBRES_TABLE_INDEXRBN,
446 CPL_EQUAL_TO, indexrbn);
447 cpl_array *sel = cpl_table_where_selected(nod_fibret);
448 int ft_idx = cpl_array_get_cplsize(sel, 0, NULL);
449 if (strcmp(obstype, MOO_OBSTYPE_OBSPNODSTARE) == 0) {
450 moo_target_table_set_ft_psnr(target_table, i - 1, nod_fibret,
454 moo_target_table_set_snr(target_table, i - 1, nod_fibret, ft_idx);
456 cpl_array_delete(sel);
457 if (strcmp(obstype, MOO_OBSTYPE_OBSSWITCH) == 0) {
458 cpl_table_select_all(nod_fibret);
459 cpl_table_and_selected_int(nod_fibret, MOO_FIBRES_TABLE_INDEXRBN,
460 CPL_EQUAL_TO, pindexrbn);
461 cpl_array *sel2 = cpl_table_where_selected(nod_fibret);
462 ft_idx = cpl_array_get_cplsize(sel2, 0, NULL);
463 cpl_array_delete(sel2);
464 moo_target_table_set_ft_psnr(target_table, i - 1, nod_fibret,
466 cpl_table_set_int(target_table->table, MOO_TARGET_TABLE_PNBSKY,
468 cpl_table_set_int(target_table->table,
469 MOO_TARGET_TABLE_PMAX_DIST_SLIT, i - 1, 0);
470 cpl_table_set_float(target_table->table,
471 MOO_TARGET_TABLE_PMAX_DIST_SKY, i - 1, 0);
483#if (__GNUC__ == 9) && (__GNUC_MINOR__ < 3)
484#pragma omp parallel shared(nb_target, indexrbnt, pindexrbnt, transt, ptranst, \
485 obstypet, target_table, nod_rbn_single, \
486 objb_rbn_img, obja_rbn_img, sci_single, \
487 nod_rbn_qual_img, sci_qual_img, nod_rbn_img, \
488 sci_img, nod_rbn_err_img, sci_err_img, nx)
490#pragma omp parallel default(none) \
491 shared(nb_target, indexrbnt, pindexrbnt, transt, ptranst, obstypet, \
492 target_table, nod_rbn_single, objb_rbn_img, obja_rbn_img, \
493 sci_single, nod_rbn_qual_img, sci_qual_img, nod_rbn_img, \
494 sci_img, nod_rbn_err_img, sci_err_img, nx)
499 for (
int i = 1; i <= nb_target; i++) {
500 int indexrbn = indexrbnt[i - 1];
501 int pindexrbn = pindexrbnt[i - 1];
502 char *obstype = obstypet[i - 1];
503 float trans = transt[i - 1];
504 float ptrans = ptranst[i - 1];
506 if (strcmp(obstype, MOO_OBSTYPE_OBSPNOD) == 0 ||
507 strcmp(obstype, MOO_OBSTYPE_OBSPNODSTARE) == 0) {
508 if (strcmp(obstype, MOO_OBSTYPE_OBSPNODSTARE) == 0) {
509 indexrbn = pindexrbn;
511 for (
int j = 1; j <= nx; j++) {
514 cpl_image_get(obja_rbn_img, j, indexrbn, &rej);
515 cpl_image_set(sci_single->sky, j, i, sky_val);
517 cpl_image_get(nod_rbn_img, j, indexrbn, &rej);
519 cpl_image_set(sci_img, j, i, NAN);
520 cpl_image_set(sci_err_img, j, i, NAN);
521 cpl_image_reject(sci_img, j, i);
524 double sci_flux = -rbn_img_val;
525 cpl_image_set(sci_img, j, i, sci_flux);
527 cpl_image_get(nod_rbn_err_img, j, indexrbn, &rej);
528 double err = rbn_err_val;
529 cpl_image_set(sci_err_img, j, i, err);
531 double rbn_qual_val =
532 cpl_image_get(nod_rbn_qual_img, j, indexrbn, &rej);
533 cpl_image_set(sci_qual_img, j, i, rbn_qual_val);
536 else if (strcmp(obstype, MOO_OBSTYPE_OBSSWITCH) == 0) {
537 double transsum = trans + ptrans;
538 for (
int j = 1; j <= nx; j++) {
540 double sci_flux = NAN;
541 double sci_err = NAN;
543 double obja_sky_val =
544 cpl_image_get(obja_rbn_img, j, pindexrbn, &rej);
545 double objb_sky_val =
546 cpl_image_get(objb_rbn_img, j, indexrbn, &prej);
548 (ptrans * obja_sky_val + trans * objb_sky_val) /
550 cpl_image_set(sci_single->sky, j, i, sky_val);
552 double nod_rbn_img_val =
553 cpl_image_get(nod_rbn_img, j, indexrbn, &rej);
554 double pnod_rbn_img_val =
555 cpl_image_get(nod_rbn_img, j, pindexrbn, &prej);
558 cpl_image_reject(sci_img, j, i);
560 int nod_rbn_qual_val =
561 (int)cpl_image_get(nod_rbn_qual_img, j, indexrbn,
563 int pnod_rbn_qual_val =
564 (int)cpl_image_get(nod_rbn_qual_img, j, pindexrbn,
566 int sci_qual_val = nod_rbn_qual_val | pnod_rbn_qual_val;
567 cpl_image_set(sci_qual_img, j, i, (
double)sci_qual_val);
570 sci_flux = -2 * pnod_rbn_img_val;
571 double pnod_rbn_err_val =
572 cpl_image_get(nod_rbn_err_img, j, pindexrbn, &rerr);
573 sci_err = 2 * pnod_rbn_err_val;
576 (int)cpl_image_get(nod_rbn_qual_img, j, pindexrbn,
578 cpl_image_set(sci_qual_img, j, i, (
double)sci_qual_val);
581 sci_flux = 2 * nod_rbn_img_val;
582 double nod_rbn_err_val =
583 cpl_image_get(nod_rbn_err_img, j, indexrbn, &rerr);
584 sci_err = 2 * nod_rbn_err_val;
587 (int)cpl_image_get(nod_rbn_qual_img, j, indexrbn,
589 cpl_image_set(sci_qual_img, j, i, (
double)sci_qual_val);
592 sci_flux = (nod_rbn_img_val * trans -
593 pnod_rbn_img_val * ptrans) /
595 double nod_rbn_err_val =
596 cpl_image_get(nod_rbn_err_img, j, indexrbn, &rerr);
597 double pnod_rbn_err_val =
598 cpl_image_get(nod_rbn_err_img, j, pindexrbn, &rerr);
599 sci_err = sqrt((trans * trans * nod_rbn_err_val *
601 ptrans * ptrans * pnod_rbn_err_val *
605 int nod_rbn_qual_val =
606 (int)cpl_image_get(nod_rbn_qual_img, j, indexrbn,
608 int pnod_rbn_qual_val =
609 (int)cpl_image_get(nod_rbn_qual_img, j, pindexrbn,
611 int sci_qual_val = nod_rbn_qual_val | pnod_rbn_qual_val;
612 cpl_image_set(sci_qual_img, j, i, (
double)sci_qual_val);
614 cpl_image_set(sci_img, j, i, sci_flux);
615 cpl_image_set(sci_err_img, j, i, sci_err);
619 for (
int j = 1; j <= nx; j++) {
622 cpl_image_get(objb_rbn_img, j, indexrbn, &rej);
623 cpl_image_set(sci_single->sky, j, i, sky_val);
625 cpl_image_get(nod_rbn_img, j, indexrbn, &rej);
627 cpl_image_set(sci_img, j, i, NAN);
628 cpl_image_set(sci_err_img, j, i, NAN);
629 cpl_image_reject(sci_img, j, i);
632 double sci_flux = rbn_img_val;
633 cpl_image_set(sci_img, j, i, sci_flux);
635 cpl_image_get(nod_rbn_err_img, j, indexrbn, &rej);
636 double err = rbn_err_val;
637 cpl_image_set(sci_err_img, j, i, err);
639 double rbn_qual_val =
640 cpl_image_get(nod_rbn_qual_img, j, indexrbn, &rej);
641 cpl_image_set(sci_qual_img, j, i, rbn_qual_val);
649 if (!cpl_errorstate_is_equal(prestate)) {
650 cpl_error_set_message(__func__, CPL_ERROR_ILLEGAL_OUTPUT,
651 "Error in combine pair %s",
652 nod_rbn_single->extname);
659static cpl_propertylist *
660_moo_create_primary_header(cpl_propertylist *rbn_primary_header)
662 cpl_propertylist *primary_header = NULL;
664 primary_header = cpl_propertylist_new();
665 cpl_propertylist_copy_property(primary_header, rbn_primary_header,
667 cpl_propertylist_copy_property(primary_header, rbn_primary_header,
669 cpl_propertylist_copy_property_regexp(primary_header, rbn_primary_header,
671 cpl_propertylist_copy_property_regexp(primary_header, rbn_primary_header,
673 cpl_propertylist_copy_property(primary_header, rbn_primary_header,
676 return primary_header;
680_moo_combine_pair_2d(moo_rbn *nod_rbn,
684 const char *filename)
688 cpl_ensure(nod_rbn != NULL, CPL_ERROR_NULL_INPUT, NULL);
689 cpl_ensure(objb_rbn != NULL, CPL_ERROR_NULL_INPUT, NULL);
690 cpl_ensure(target_table != NULL, CPL_ERROR_NULL_INPUT, NULL);
694 cpl_errorstate prestate = cpl_errorstate_get();
696 unsigned int badpix_level =
701 sci->filename = filename;
702 sci->primary_header = _moo_create_primary_header(nod_rbn->primary_header);
704 cpl_msg_info(__func__,
"Combine pair (2D)");
711 for (
int i = 1; i < 3; i++) {
716 if (obja_rbn_single != NULL) {
717 cpl_propertylist *header =
723 exptimeA[i] = ndit * dit;
725 if (objb_rbn_single != NULL) {
726 cpl_propertylist *header =
732 exptimeB[i] = ndit * dit;
736 moo_target_table_set_exptimes(sci_target_table, exptimeA, exptimeB);
739 sci->target_table = sci_target_table;
741 cpl_msg_indent_more();
743 for (
int i = 0; i < 3; i++) {
744 moo_sci_single *sci_single = NULL;
745 moo_rbn_single *nod_rbn_single =
747 moo_rbn_single *objb_rbn_single =
749 moo_rbn_single *obja_rbn_single =
751 if (nod_rbn_single != NULL) {
752 moo_try_check(sci_single = _moo_combine_pair_2d_single(
753 nod_rbn_single, objb_rbn_single, obja_rbn_single,
754 nod_ft, sci_target_table),
762 cpl_msg_indent_less();
764 if (!cpl_errorstate_is_equal(prestate)) {
765 cpl_error_set_message(__func__, CPL_ERROR_ILLEGAL_OUTPUT,
766 "Error in combine pair (2D)");
807 const cpl_frame *solflux_frame,
808 const cpl_frame *airglow_group_frame,
809 const cpl_frame *airglow_var_frame,
810 moo_combine_pair_params *combine_pair_params,
811 moo_sub_sky_stare_params *sky_params,
812 const char *filename,
813 moo_products *products,
814 const cpl_frame *ref_frame)
816 moo_sci *sci1d = NULL;
817 moo_sci *psci1d = NULL;
818 char *sci1d_name = NULL;
819 char *psci1d_name = NULL;
820 char *sci2d_name = NULL;
821 char *psci2d_name = NULL;
822 moo_sci *sci2d = NULL;
823 moo_sci *psci2d = NULL;
824 moo_sci *cpair_sci = NULL;
826 cpl_ensure(nod_rbn != NULL, CPL_ERROR_NULL_INPUT, NULL);
827 cpl_ensure(sky_rbn != NULL, CPL_ERROR_NULL_INPUT, NULL);
828 cpl_ensure(target_table != NULL, CPL_ERROR_NULL_INPUT, NULL);
830 cpl_errorstate prestate = cpl_errorstate_get();
832 cpl_msg_info(__func__,
"Combine pair using optimal:%d",
833 combine_pair_params->optimal);
835 if (combine_pair_params->optimal == 1) {
836 cpl_msg_info(__func__,
"Substract sky (1D)");
837 sci1d_name = cpl_sprintf(
"1D_%s", filename);
839 moo_try_check(sci1d =
841 solflux_frame, airglow_group_frame,
842 airglow_var_frame, sky_params,
843 sci1d_name, MOO_SCI1D_NOT_PAIRED),
845 if (products != NULL) {
847 CPL_FRAME_LEVEL_TEMPORARY,
"1D",
848 sci1d_name, ref_frame),
851#if MOO_DEBUG_COMBINEPAIR
853 char *testname = cpl_sprintf(
"CPAIR_1D_%s", filename);
859 cpl_msg_info(__func__,
"Substract sky paired-fibre (1D)");
861 psci1d_name = cpl_sprintf(
"P1D_%s", filename);
863 moo_try_check(psci1d =
865 solflux_frame, airglow_group_frame,
866 airglow_var_frame, sky_params,
867 psci1d_name, MOO_SCI1D_PAIRED),
869 if (products != NULL) {
871 CPL_FRAME_LEVEL_TEMPORARY,
"P1D",
872 psci1d_name, ref_frame),
875#if MOO_DEBUG_COMBINEPAIR
877 char *testname = cpl_sprintf(
"CPAIR_P1D_%s", filename);
882 sci2d_name = cpl_sprintf(
"2D_%s", filename);
883 psci2d_name = cpl_sprintf(
"2D_PAIRED_%s", filename);
885 psci2d = moo_create_sci2d(nod_rbn, sky_rbn, obj_rbn, target_table,
886 MOO_SCI2D_PAIRED, psci2d_name);
887 sci2d = moo_create_sci2d(nod_rbn, sky_rbn, obj_rbn, target_table,
888 MOO_SCI2D_NOT_PAIRED, sci2d_name);
889 if (products != NULL) {
891 CPL_FRAME_LEVEL_TEMPORARY,
"2D",
892 sci2d_name, ref_frame),
895 CPL_FRAME_LEVEL_TEMPORARY,
"2D",
896 psci2d_name, ref_frame),
899 cpl_msg_info(
"test",
"combine all");
900 cpair_sci = moo_combine_pair_sci(target_table, sci1d, psci1d, sci2d,
904 moo_try_check(cpair_sci =
905 _moo_combine_pair_2d(nod_rbn, sky_rbn, obj_rbn,
906 target_table, filename),
911 cpl_free(sci1d_name);
912 cpl_free(psci1d_name);
913 cpl_free(sci2d_name);
914 cpl_free(psci2d_name);
919 if (!cpl_errorstate_is_equal(prestate)) {
920 cpl_error_set_message(__func__, CPL_ERROR_ILLEGAL_OUTPUT,
921 "Error in sky subtraction");
929moo_create_sci2d(moo_rbn *nod_rbn,
934 const char *filename)
938 cpl_ensure(nod_rbn != NULL, CPL_ERROR_NULL_INPUT, NULL);
939 cpl_ensure(target_table != NULL, CPL_ERROR_NULL_INPUT, NULL);
943 cpl_errorstate prestate = cpl_errorstate_get();
944 unsigned int badpix_level =
947 cpl_msg_info(__func__,
"Create SCI (2D) ispaired:%d", ispaired);
948 cpl_table_unselect_all(target_table->table);
952 cpl_table_or_selected_string(target_table->table,
953 MOO_TARGET_TABLE_OBSTYPE, CPL_EQUAL_TO,
954 MOO_OBSTYPE_OBSSWITCH);
955 cpl_table_and_selected_string(target_table->table,
956 MOO_TARGET_TABLE_OBSTYPE,
958 MOO_OBSTYPE_OBSSWITCHSTARE);
960 cpl_table_or_selected_string(target_table->table,
961 MOO_TARGET_TABLE_OBSTYPE, CPL_EQUAL_TO,
963 cpl_table_or_selected_string(target_table->table,
964 MOO_TARGET_TABLE_OBSTYPE, CPL_EQUAL_TO,
965 MOO_OBSTYPE_SKYRESNOD);
969 cpl_table_or_selected_string(target_table->table,
970 MOO_TARGET_TABLE_OBSTYPE, CPL_EQUAL_TO,
971 MOO_OBSTYPE_OBSSWITCH);
972 cpl_table_and_selected_string(target_table->table,
973 MOO_TARGET_TABLE_OBSTYPE,
975 MOO_OBSTYPE_OBSSWITCHSTARE);
977 cpl_table_or_selected_string(target_table->table,
978 MOO_TARGET_TABLE_OBSTYPE, CPL_EQUAL_TO,
979 MOO_OBSTYPE_OBSPNOD);
981 cpl_table *ttable = cpl_table_extract_selected(target_table->table);
984 sci->filename = filename;
985 sci->primary_header = _moo_create_primary_header(nod_rbn->primary_header);
988 sci_target_table->primary_header = cpl_propertylist_new();
989 sci_target_table->table = ttable;
990 sci->target_table = sci_target_table;
1000 for (
int i = 1; i < 3; i++) {
1005 if (obja_rbn_single != NULL) {
1006 cpl_propertylist *header =
1012 exptimeA[i] = ndit * dit;
1016 moo_target_table_set_exptimes(sci_target_table, exptimeA, exptimeB);
1020 cpl_msg_indent_more();
1022 for (
int i = 0; i < 3; i++) {
1023 moo_sci_single *sci_single = NULL;
1024 moo_rbn_single *nod_rbn_single = NULL;
1025 moo_rbn_single *obja_rbn_single = NULL;
1026 moo_rbn_single *objb_rbn_single = NULL;
1031 if (nod_rbn_single != NULL) {
1032 if (obja_rbn != NULL) {
1037 moo_try_check(sci_single = _moo_create_sci2d_single(
1038 nod_rbn_single, objb_rbn_single, obja_rbn_single,
1039 nod_ft, sci_target_table, ispaired),
1048 cpl_msg_indent_less();
1050 if (!cpl_errorstate_is_equal(prestate)) {
1051 cpl_error_set_message(__func__, CPL_ERROR_ILLEGAL_OUTPUT,
1052 "Error in combine pair (2D)");
1059static moo_sci_single *
1061 moo_sci_single *sci1d,
1062 cpl_table *sci1d_table,
1063 moo_sci_single *psci1d,
1064 cpl_table *psci1d_table,
1065 moo_sci_single *sci2d,
1066 cpl_table *sci2d_table,
1067 moo_sci_single *psci2d,
1068 cpl_table *psci2d_table)
1070 moo_sci_single *sci_single = NULL;
1071 moo_sci_single *main_sci_single = NULL;
1072 int *sci2d_index = NULL;
1073 int *sci1d_index = NULL;
1074 int *psci2d_index = NULL;
1075 int *psci1d_index = NULL;
1077 cpl_image *sci_img = NULL;
1078 cpl_image *sci_err_img = NULL;
1079 cpl_image *sci_qual_img = NULL;
1080 cpl_image *sci_sky = NULL;
1082 cpl_errorstate prestate = cpl_errorstate_get();
1084 cpl_ensure(target_table != NULL, CPL_ERROR_NULL_INPUT, NULL);
1086 if (sci1d != NULL) {
1087 main_sci_single = sci1d;
1089 else if (sci2d != NULL) {
1090 main_sci_single = sci2d;
1092 else if (psci1d != NULL) {
1093 main_sci_single = psci1d;
1095 else if (psci2d != NULL) {
1096 main_sci_single = psci2d;
1099 if (main_sci_single == NULL) {
1106 cpl_propertylist_copy_property(sci_single->header, main_sci_single->header,
1108 cpl_propertylist_copy_property(sci_single->header, main_sci_single->header,
1110 cpl_propertylist_copy_property(sci_single->header, main_sci_single->header,
1112 cpl_propertylist_copy_property(sci_single->header, main_sci_single->header,
1114 cpl_propertylist_copy_property(sci_single->header, main_sci_single->header,
1116 cpl_propertylist_copy_property(sci_single->header, main_sci_single->header,
1118 cpl_propertylist_copy_property(sci_single->header, main_sci_single->header,
1120 cpl_propertylist_copy_property(sci_single->header, main_sci_single->header,
1122 cpl_propertylist_copy_property(sci_single->header, main_sci_single->header,
1124 cpl_propertylist_copy_property(sci_single->header, main_sci_single->header,
1126 cpl_propertylist_copy_property(sci_single->header, main_sci_single->header,
1128 cpl_propertylist_copy_property(sci_single->header, main_sci_single->header,
1130 cpl_propertylist_copy_property(sci_single->header, main_sci_single->header,
1132 cpl_propertylist_copy_property(sci_single->header, main_sci_single->header,
1133 MOO_PFITS_WAVELMIN);
1134 cpl_propertylist_copy_property(sci_single->header, main_sci_single->header,
1135 MOO_PFITS_WAVELMAX);
1136 cpl_propertylist_copy_property(sci_single->header, main_sci_single->header,
1138 int nx = hdrl_image_get_size_x(main_sci_single->image);
1139 int nb_target = cpl_table_get_nrow(target_table->table);
1141 sci_single->image = hdrl_image_new(nx, nb_target);
1142 sci_single->sky = cpl_image_new(nx, nb_target, CPL_TYPE_DOUBLE);
1143 sci_single->qual = cpl_image_new(nx, nb_target, CPL_TYPE_INT);
1145 sci_img = hdrl_image_get_image(sci_single->image);
1147 cpl_image_get_bpm(sci_img);
1149 sci_err_img = hdrl_image_get_error(sci_single->image);
1150 sci_qual_img = sci_single->qual;
1151 sci_sky = sci_single->sky;
1153 const char *transname[] = { MOO_TARGET_TABLE_TRANSRI,
1154 MOO_TARGET_TABLE_TRANSYJ,
1155 MOO_TARGET_TABLE_TRANSH };
1156 const char *ptransname[] = { MOO_TARGET_TABLE_PTRANSRI,
1157 MOO_TARGET_TABLE_PTRANSYJ,
1158 MOO_TARGET_TABLE_PTRANSH };
1159 const char *exptimenames[] = { MOO_TARGET_TABLE_EXPTIMERI,
1160 MOO_TARGET_TABLE_EXPTIMEYJ,
1161 MOO_TARGET_TABLE_EXPTIMEH };
1162 const char *exptimename = exptimenames[sci_single->type];
1164 float *transt = cpl_table_get_data_float(target_table->table,
1165 transname[sci_single->type]);
1166 float *ptranst = cpl_table_get_data_float(target_table->table,
1167 ptransname[sci_single->type]);
1170 cpl_table_get_data_int(target_table->table, MOO_TARGET_TABLE_INDEXRBN);
1171 int *pindexrbnt = cpl_table_get_data_int(target_table->table,
1172 MOO_TARGET_TABLE_PAIREDINDEXRBN);
1173 char **obstypet = cpl_table_get_data_string(target_table->table,
1174 MOO_TARGET_TABLE_OBSTYPE);
1175 sci2d_index = cpl_calloc(nb_target,
sizeof(
int));
1176 sci1d_index = cpl_calloc(nb_target,
sizeof(
int));
1178 psci2d_index = cpl_calloc(nb_target,
sizeof(
int));
1179 psci1d_index = cpl_calloc(nb_target,
sizeof(
int));
1181 for (
int i = 1; i <= nb_target; i++) {
1182 double exptime = NAN;
1184 sci2d_index[idx] = -1;
1185 psci2d_index[idx] = -1;
1186 sci1d_index[idx] = -1;
1188 cpl_table_set_int(target_table->table, MOO_TARGET_TABLE_INDEXTARG, idx,
1191 int indexrbn = indexrbnt[idx];
1192 int pindexrbn = pindexrbnt[idx];
1193 char *obstype = obstypet[idx];
1195 if (strcmp(obstype, MOO_OBSTYPE_OBSNOD) == 0 ||
1196 strcmp(obstype, MOO_OBSTYPE_SKYRESNOD) == 0) {
1197 cpl_table_select_all(sci2d_table);
1199 int size = cpl_table_and_selected_int(sci2d_table,
1200 MOO_TARGET_TABLE_INDEXRBN,
1201 CPL_EQUAL_TO, indexrbn);
1202 cpl_ensure(size == 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
1203 cpl_array *sel = cpl_table_where_selected(sci2d_table);
1205 int tidx = cpl_array_get_cplsize(sel, 0, NULL);
1207 cpl_array_delete(sel);
1210 cpl_table_get_double(sci2d_table, exptimename, tidx, NULL);
1211 sci2d_index[idx] = tidx;
1213 else if (strcmp(obstype, MOO_OBSTYPE_OBSPNOD) == 0) {
1214 cpl_table_select_all(psci2d_table);
1216 int size = cpl_table_and_selected_int(psci2d_table,
1217 MOO_TARGET_TABLE_INDEXRBN,
1218 CPL_EQUAL_TO, indexrbn);
1219 cpl_ensure(size == 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
1220 cpl_array *sel = cpl_table_where_selected(psci2d_table);
1222 int tidx = cpl_array_get_cplsize(sel, 0, NULL);
1224 cpl_array_delete(sel);
1225 psci2d_index[idx] = tidx;
1227 else if (strcmp(obstype, MOO_OBSTYPE_OBSSWITCH) == 0) {
1228 cpl_table_select_all(sci2d_table);
1230 int size = cpl_table_and_selected_int(sci2d_table,
1231 MOO_TARGET_TABLE_INDEXRBN,
1232 CPL_EQUAL_TO, indexrbn);
1233 cpl_ensure(size == 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
1234 cpl_array *sel = cpl_table_where_selected(sci2d_table);
1236 int tidx = cpl_array_get_cplsize(sel, 0, NULL);
1238 cpl_array_delete(sel);
1239 sci2d_index[idx] = tidx;
1241 cpl_table_select_all(psci2d_table);
1243 size = cpl_table_and_selected_int(psci2d_table,
1244 MOO_TARGET_TABLE_INDEXRBN,
1245 CPL_EQUAL_TO, pindexrbn);
1246 cpl_ensure(size == 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
1247 sel = cpl_table_where_selected(psci2d_table);
1249 tidx = cpl_array_get_cplsize(sel, 0, NULL);
1250 cpl_array_delete(sel);
1252 psci2d_index[idx] = tidx;
1254 else if (strcmp(obstype, MOO_OBSTYPE_OBSSWITCHSTARE) == 0) {
1255 cpl_table_select_all(sci1d_table);
1257 int size = cpl_table_and_selected_int(sci1d_table,
1258 MOO_TARGET_TABLE_INDEXRBN,
1259 CPL_EQUAL_TO, indexrbn);
1260 cpl_ensure(size == 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
1261 cpl_array *sel = cpl_table_where_selected(sci1d_table);
1263 int tidx = cpl_array_get_cplsize(sel, 0, NULL);
1265 cpl_array_delete(sel);
1266 sci1d_index[idx] = tidx;
1268 cpl_table_select_all(psci1d_table);
1270 size = cpl_table_and_selected_int(psci1d_table,
1271 MOO_TARGET_TABLE_INDEXRBN,
1272 CPL_EQUAL_TO, pindexrbn);
1273 cpl_ensure(size == 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
1274 sel = cpl_table_where_selected(psci1d_table);
1276 tidx = cpl_array_get_cplsize(sel, 0, NULL);
1277 cpl_array_delete(sel);
1279 psci1d_index[idx] = tidx;
1281 else if (strcmp(obstype, MOO_OBSTYPE_OBSNODSTARE) == 0) {
1282 cpl_table_select_all(sci2d_table);
1284 int size = cpl_table_and_selected_int(sci2d_table,
1285 MOO_TARGET_TABLE_INDEXRBN,
1286 CPL_EQUAL_TO, indexrbn);
1287 cpl_ensure(size == 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
1288 cpl_array *sel = cpl_table_where_selected(sci2d_table);
1290 int tidx = cpl_array_get_cplsize(sel, 0, NULL);
1292 cpl_array_delete(sel);
1293 sci2d_index[idx] = tidx;
1295 cpl_table_select_all(psci1d_table);
1297 size = cpl_table_and_selected_int(psci1d_table,
1298 MOO_TARGET_TABLE_INDEXRBN,
1299 CPL_EQUAL_TO, pindexrbn);
1300 cpl_ensure(size == 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
1301 sel = cpl_table_where_selected(psci1d_table);
1303 tidx = cpl_array_get_cplsize(sel, 0, NULL);
1304 cpl_array_delete(sel);
1306 psci1d_index[idx] = tidx;
1308 else if (strcmp(obstype, MOO_OBSTYPE_OBSPNODSTARE) == 0) {
1309 cpl_table_select_all(psci2d_table);
1311 int size = cpl_table_and_selected_int(psci2d_table,
1312 MOO_TARGET_TABLE_INDEXRBN,
1313 CPL_EQUAL_TO, pindexrbn);
1314 cpl_ensure(size == 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
1315 cpl_array *sel = cpl_table_where_selected(psci2d_table);
1317 int tidx = cpl_array_get_cplsize(sel, 0, NULL);
1319 cpl_array_delete(sel);
1320 psci2d_index[idx] = tidx;
1322 cpl_table_select_all(sci1d_table);
1324 size = cpl_table_and_selected_int(sci1d_table,
1325 MOO_TARGET_TABLE_INDEXRBN,
1326 CPL_EQUAL_TO, indexrbn);
1327 cpl_ensure(size == 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
1328 sel = cpl_table_where_selected(sci1d_table);
1330 tidx = cpl_array_get_cplsize(sel, 0, NULL);
1331 cpl_array_delete(sel);
1333 sci1d_index[idx] = tidx;
1335 else if (strcmp(obstype, MOO_OBSTYPE_OBSSTARE) == 0 ||
1336 strcmp(obstype, MOO_OBSTYPE_SKYRESSTARE) == 0) {
1337 cpl_table_select_all(sci1d_table);
1339 int size = cpl_table_and_selected_int(sci1d_table,
1340 MOO_TARGET_TABLE_INDEXRBN,
1341 CPL_EQUAL_TO, indexrbn);
1342 cpl_ensure(size == 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
1343 cpl_array *sel = cpl_table_where_selected(sci1d_table);
1345 int tidx = cpl_array_get_cplsize(sel, 0, NULL);
1347 cpl_table_get_double(sci1d_table, exptimename, tidx, NULL);
1348 cpl_array_delete(sel);
1349 sci1d_index[idx] = tidx;
1351 else if (strcmp(obstype, MOO_OBSTYPE_OBSPSTARE) == 0 ||
1352 strcmp(obstype, MOO_OBSTYPE_SKYRESPSTARE) == 0) {
1353 cpl_table_select_all(psci1d_table);
1355 int size = cpl_table_and_selected_int(psci1d_table,
1356 MOO_TARGET_TABLE_INDEXRBN,
1357 CPL_EQUAL_TO, indexrbn);
1358 cpl_ensure(size == 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
1359 cpl_array *sel = cpl_table_where_selected(psci1d_table);
1361 int tidx = cpl_array_get_cplsize(sel, 0, NULL);
1363 cpl_array_delete(sel);
1364 psci1d_index[idx] = tidx;
1366 cpl_table_set_double(target_table->table, exptimename, idx, exptime);
1376#if (__GNUC__ == 9) && (__GNUC_MINOR__ < 3)
1377#pragma omp parallel shared(nb_target, indexrbnt, pindexrbnt, transt, ptranst, \
1378 obstypet, target_table, sci2d, psci2d, sci1d, \
1379 psci1d, sci_single, sci_qual_img, sci_img, \
1380 sci_err_img, sci_sky, nx, sci2d_index, \
1381 psci2d_index, sci1d_index, psci1d_index)
1383#pragma omp parallel default(none) \
1384 shared(nb_target, indexrbnt, pindexrbnt, transt, ptranst, obstypet, \
1385 target_table, sci2d, psci2d, sci1d, psci1d, sci_single, \
1386 sci_qual_img, sci_img, sci_err_img, sci_sky, nx, sci2d_index, \
1387 psci2d_index, sci1d_index, psci1d_index)
1392 for (
int i = 1; i <= nb_target; i++) {
1394 cpl_table_set_int(target_table->table, MOO_TARGET_TABLE_INDEXTARG,
1397 char *obstype = obstypet[idx];
1398 float trans = transt[idx];
1399 float ptrans = ptranst[idx];
1400 if (strcmp(obstype, MOO_OBSTYPE_OBSNOD) == 0 ||
1401 strcmp(obstype, MOO_OBSTYPE_SKYRESNOD) == 0) {
1402 int tidx = sci2d_index[idx] + 1;
1404 cpl_image *fluxes = hdrl_image_get_image(himg);
1406 cpl_image *errs = hdrl_image_get_error(himg);
1409 for (
int j = 1; j <= nx; j++) {
1411 double err = cpl_image_get(errs, j, tidx, &rej);
1412 double flux = cpl_image_get(fluxes, j, tidx, &rej);
1413 double sky = cpl_image_get(skyimg, j, tidx, &rej);
1414 double qual = cpl_image_get(qualimg, j, tidx, &rej);
1415 cpl_image_set(sci_img, j, i, flux);
1416 cpl_image_set(sci_err_img, j, i, err);
1417 cpl_image_set(sci_sky, j, i, sky);
1418 cpl_image_set(sci_qual_img, j, i, qual);
1421 else if (strcmp(obstype, MOO_OBSTYPE_OBSPNOD) == 0) {
1422 int tidx = psci2d_index[idx] + 1;
1424 cpl_image *fluxes = hdrl_image_get_image(himg);
1426 cpl_image *errs = hdrl_image_get_error(himg);
1429 for (
int j = 1; j <= nx; j++) {
1431 double err = cpl_image_get(errs, j, tidx, &rej);
1432 double flux = cpl_image_get(fluxes, j, tidx, &rej);
1433 double sky = cpl_image_get(skyimg, j, tidx, &rej);
1434 double qual = cpl_image_get(qualimg, j, tidx, &rej);
1435 cpl_image_set(sci_img, j, i, flux);
1436 cpl_image_set(sci_err_img, j, i, err);
1437 cpl_image_set(sci_sky, j, i, sky);
1438 cpl_image_set(sci_qual_img, j, i, qual);
1441 else if (strcmp(obstype, MOO_OBSTYPE_OBSSWITCH) == 0) {
1442 int tidx = sci2d_index[idx] + 1;
1443 int ptidx = psci2d_index[idx] + 1;
1445 double transsum = trans + ptrans;
1447 cpl_image *fluxes = hdrl_image_get_image(himg);
1449 cpl_image *errs = hdrl_image_get_error(himg);
1453 cpl_image *pfluxes = hdrl_image_get_image(phimg);
1455 cpl_image *perrs = hdrl_image_get_error(phimg);
1458 for (
int j = 1; j <= nx; j++) {
1460 double err1 = cpl_image_get(errs, j, tidx, &rej1);
1461 double flux1 = cpl_image_get(fluxes, j, tidx, &rej1);
1462 double sky1 = cpl_image_get(skyimg, j, tidx, &rej1);
1463 int qual1 = (int)cpl_image_get(qualimg, j, tidx, &rej1);
1466 double err2 = cpl_image_get(perrs, j, ptidx, &rej2);
1467 double flux2 = cpl_image_get(pfluxes, j, ptidx, &rej2);
1468 double sky2 = cpl_image_get(pskyimg, j, ptidx, &rej2);
1469 int qual2 = (int)cpl_image_get(pqualimg, j, ptidx, &rej2);
1471 int qual = qual1 | qual2;
1476 if (!rej1 && !rej2) {
1477 flux = (trans * flux1 + ptrans * flux2) / transsum * 2;
1480 sqrt((trans * err1 * err1 + ptrans * err2 * err2) /
1483 sky = (trans * sky1 + ptrans * sky2) / transsum * 2;
1486 cpl_image_set(sci_img, j, i, flux);
1487 cpl_image_set(sci_err_img, j, i, err);
1488 cpl_image_set(sci_sky, j, i, sky);
1489 cpl_image_set(sci_qual_img, j, i, qual);
1492 else if (strcmp(obstype, MOO_OBSTYPE_OBSNODSTARE) == 0) {
1493 int tidx = sci2d_index[idx] + 1;
1494 int ptidx = psci1d_index[idx] + 1;
1496 double transsum = trans + ptrans;
1499 cpl_image *fluxes = hdrl_image_get_image(himg);
1501 cpl_image *errs = hdrl_image_get_error(himg);
1505 cpl_image *pfluxes = hdrl_image_get_image(phimg);
1507 cpl_image *perrs = hdrl_image_get_error(phimg);
1510 for (
int j = 1; j <= nx; j++) {
1512 double err1 = cpl_image_get(errs, j, tidx, &rej1);
1513 double flux1 = cpl_image_get(fluxes, j, tidx, &rej1);
1514 double sky1 = cpl_image_get(skyimg, j, tidx, &rej1);
1515 int qual1 = (int)cpl_image_get(qualimg, j, tidx, &rej1);
1518 double err2 = cpl_image_get(perrs, j, ptidx, &rej2);
1519 double flux2 = cpl_image_get(pfluxes, j, ptidx, &rej2);
1520 double sky2 = cpl_image_get(pskyimg, j, ptidx, &rej2);
1521 int qual2 = (int)cpl_image_get(pqualimg, j, ptidx, &rej2);
1522 int qual = qual1 | qual2;
1537 (trans * flux1 + ptrans * flux2) / transsum * 2;
1539 err = sqrt((trans * err1 * err1 +
1540 ptrans * err2 * err2) /
1543 sky = (trans * sky1 + ptrans * sky2) / transsum * 2;
1552 cpl_image_set(sci_img, j, i, flux);
1553 cpl_image_set(sci_err_img, j, i, err);
1554 cpl_image_set(sci_sky, j, i, sky);
1555 cpl_image_set(sci_qual_img, j, i, qual);
1558 else if (strcmp(obstype, MOO_OBSTYPE_OBSPNODSTARE) == 0) {
1559 int ptidx = psci2d_index[idx] + 1;
1560 int tidx = sci1d_index[idx] + 1;
1562 double transsum = trans + ptrans;
1564 cpl_image *fluxes = hdrl_image_get_image(himg);
1566 cpl_image *errs = hdrl_image_get_error(himg);
1570 cpl_image *pfluxes = hdrl_image_get_image(phimg);
1572 cpl_image *perrs = hdrl_image_get_error(phimg);
1575 for (
int j = 1; j <= nx; j++) {
1577 double err1 = cpl_image_get(errs, j, tidx, &rej1);
1578 double flux1 = cpl_image_get(fluxes, j, tidx, &rej1);
1579 double sky1 = cpl_image_get(skyimg, j, tidx, &rej1);
1580 int qual1 = (int)cpl_image_get(qualimg, j, tidx, &rej1);
1583 double err2 = cpl_image_get(perrs, j, ptidx, &rej2);
1584 double flux2 = cpl_image_get(pfluxes, j, ptidx, &rej2);
1585 double sky2 = cpl_image_get(pskyimg, j, ptidx, &rej2);
1586 int qual2 = (int)cpl_image_get(pqualimg, j, ptidx, &rej2);
1587 int qual = qual1 | qual2;
1602 (trans * flux1 + ptrans * flux2) / transsum * 2;
1604 err = sqrt((trans * err1 * err1 +
1605 ptrans * err2 * err2) /
1608 sky = (trans * sky1 + ptrans * sky2) / transsum * 2;
1617 cpl_image_set(sci_img, j, i, flux);
1618 cpl_image_set(sci_err_img, j, i, err);
1619 cpl_image_set(sci_sky, j, i, sky);
1620 cpl_image_set(sci_qual_img, j, i, qual);
1623 else if (strcmp(obstype, MOO_OBSTYPE_OBSSWITCHSTARE) == 0) {
1624 int ptidx = psci1d_index[idx] + 1;
1625 int tidx = sci1d_index[idx] + 1;
1627 double transsum = trans + ptrans;
1629 cpl_image *fluxes = hdrl_image_get_image(himg);
1631 cpl_image *errs = hdrl_image_get_error(himg);
1635 cpl_image *pfluxes = hdrl_image_get_image(phimg);
1637 cpl_image *perrs = hdrl_image_get_error(phimg);
1640 for (
int j = 1; j <= nx; j++) {
1642 double err1 = cpl_image_get(errs, j, tidx, &rej1);
1643 double flux1 = cpl_image_get(fluxes, j, tidx, &rej1);
1644 double sky1 = cpl_image_get(skyimg, j, tidx, &rej1);
1645 int qual1 = (int)cpl_image_get(qualimg, j, tidx, &rej1);
1648 double err2 = cpl_image_get(perrs, j, ptidx, &rej2);
1649 double flux2 = cpl_image_get(pfluxes, j, ptidx, &rej2);
1650 double sky2 = cpl_image_get(pskyimg, j, ptidx, &rej2);
1651 int qual2 = (int)cpl_image_get(pqualimg, j, ptidx, &rej2);
1652 int qual = qual1 | qual2;
1667 (trans * flux1 + ptrans * flux2) / transsum * 2;
1669 err = sqrt((trans * err1 * err1 +
1670 ptrans * err2 * err2) /
1673 sky = (trans * sky1 + ptrans * sky2) / transsum * 2;
1682 cpl_image_set(sci_img, j, i, flux);
1683 cpl_image_set(sci_err_img, j, i, err);
1684 cpl_image_set(sci_sky, j, i, sky);
1685 cpl_image_set(sci_qual_img, j, i, qual);
1688 else if (strcmp(obstype, MOO_OBSTYPE_OBSSTARE) == 0 ||
1689 strcmp(obstype, MOO_OBSTYPE_SKYRESSTARE) == 0) {
1690 int tidx = sci1d_index[idx] + 1;
1692 cpl_image *fluxes = hdrl_image_get_image(himg);
1694 cpl_image *errs = hdrl_image_get_error(himg);
1697 for (
int j = 1; j <= nx; j++) {
1699 double err = cpl_image_get(errs, j, tidx, &rej);
1700 double flux = cpl_image_get(fluxes, j, tidx, &rej);
1701 double sky = cpl_image_get(skyimg, j, tidx, &rej);
1702 double qual = cpl_image_get(qualimg, j, tidx, &rej);
1703 cpl_image_set(sci_img, j, i, flux);
1704 cpl_image_set(sci_err_img, j, i, err);
1705 cpl_image_set(sci_sky, j, i, sky);
1706 cpl_image_set(sci_qual_img, j, i, qual);
1709 else if (strcmp(obstype, MOO_OBSTYPE_OBSPSTARE) == 0 ||
1710 strcmp(obstype, MOO_OBSTYPE_SKYRESPSTARE) == 0) {
1711 int tidx = psci1d_index[idx] + 1;
1713 cpl_image *fluxes = hdrl_image_get_image(himg);
1715 cpl_image *errs = hdrl_image_get_error(himg);
1718 for (
int j = 1; j <= nx; j++) {
1720 double err = cpl_image_get(errs, j, tidx, &rej);
1721 double flux = cpl_image_get(fluxes, j, tidx, &rej);
1722 double sky = cpl_image_get(skyimg, j, tidx, &rej);
1723 double qual = cpl_image_get(qualimg, j, tidx, &rej);
1724 cpl_image_set(sci_img, j, i, flux);
1725 cpl_image_set(sci_err_img, j, i, err);
1726 cpl_image_set(sci_sky, j, i, sky);
1727 cpl_image_set(sci_qual_img, j, i, qual);
1735 cpl_free(sci2d_index);
1736 cpl_free(sci1d_index);
1737 cpl_free(psci2d_index);
1738 cpl_free(psci1d_index);
1739 if (!cpl_errorstate_is_equal(prestate)) {
1751 const char *filename)
1753 moo_sci *sci = NULL;
1754 moo_sci *main_sci = NULL;
1755 cpl_table *sci1d_table = NULL;
1756 cpl_table *psci1d_table = NULL;
1757 cpl_table *sci2d_table = NULL;
1758 cpl_table *psci2d_table = NULL;
1760 cpl_ensure(target_table != NULL, CPL_ERROR_NULL_INPUT, NULL);
1762 cpl_errorstate prestate = cpl_errorstate_get();
1764 unsigned int badpix_level =
1767 if (psci1d != NULL) {
1771 if (psci2d != NULL) {
1775 if (sci2d != NULL) {
1780 if (sci1d != NULL) {
1785 if (main_sci == NULL) {
1790 sci->filename = filename;
1791 sci->primary_header = cpl_propertylist_duplicate(main_sci->primary_header);
1793 cpl_msg_info(__func__,
"Combine pair of SCI files");
1794 sci->target_table = target_table;
1796 cpl_msg_indent_more();
1798 for (
int i = 0; i < 3; i++) {
1799 moo_sci_single *sci_single = NULL;
1800 moo_sci_single *sci1d_single = NULL;
1801 moo_sci_single *psci1d_single = NULL;
1802 moo_sci_single *sci2d_single = NULL;
1803 moo_sci_single *psci2d_single = NULL;
1805 if (sci1d != NULL) {
1808 if (psci1d != NULL) {
1811 if (sci2d != NULL) {
1814 if (psci2d != NULL) {
1817 moo_try_check(sci_single = _moo_combine_pair_sci_single(
1818 target_table, sci1d_single, sci1d_table,
1819 psci1d_single, psci1d_table, sci2d_single,
1820 sci2d_table, psci2d_single, psci2d_table),
1827 cpl_msg_indent_less();
1829 if (!cpl_errorstate_is_equal(prestate)) {
1830 cpl_error_set_message(__func__, CPL_ERROR_ILLEGAL_OUTPUT,
1831 "Error in combine pair (2D)");
#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.
cpl_propertylist * moo_rbn_single_get_header(moo_rbn_single *self)
Get header of rbn single.
moo_rbn_single * moo_rbn_load_single(moo_rbn *self, moo_detector_type type, unsigned int level)
Load the type part in RBN and return it.
moo_rbn_single * moo_rbn_get_single(moo_rbn *self, moo_detector_type type)
Get a RBN single from RBN.
cpl_table * moo_rbn_get_fibre_table(moo_rbn *self)
Get the FIBRE TABLE in RBN.
moo_sci_single * moo_sci_single_new(moo_detector_type type)
Create a new moo_sci_single.
void moo_sci_single_delete(moo_sci_single *self)
Delete a 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_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.
moo_sci_single * moo_sci_load_single(moo_sci *self, moo_detector_type type, int level)
Load the type part in SCI and return it.
void moo_sci_delete(moo_sci *self)
Delete a moo_sci.
moo_sci * moo_sci_new(void)
Create a new moo_sci.
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.
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_target_table * moo_target_table_new(void)
Create a new moo_target_table.
moo_target_table * moo_target_table_duplicate(moo_target_table *self)
Duplicate an existing target_table.
int moo_target_table_xswitch_paired(moo_target_table *self)
get the index of the object in the target table sort by indexrbn
moo_sci * moo_combine_pair(moo_rbn *nod_rbn, moo_rbn *sky_rbn, moo_rbn *obj_rbn, moo_target_table *target_table, moo_f2f *f2f, const cpl_frame *solflux_frame, const cpl_frame *airglow_group_frame, const cpl_frame *airglow_var_frame, moo_combine_pair_params *combine_pair_params, moo_sub_sky_stare_params *sky_params, const char *filename, moo_products *products, const cpl_frame *ref_frame)
Combine the extracted spectra from a same objec.
moo_sci * moo_sub_sky_stare(moo_rbn *obja_rbn, moo_target_table *target_table, moo_rbn *objb_rbn, moo_f2f *f2f, const cpl_frame *solflux_frame, const cpl_frame *airglow_group_frame, const cpl_frame *airglow_var_frame, moo_sub_sky_stare_params *params, const char *filename, int ispaired)
This function subtracts the sky in wavelength calibrated and extracted science frames obtain in Stare...
int moo_pfits_get_ndit(const cpl_propertylist *plist)
find out the ESO DET NDIT value
double moo_pfits_get_dit(const cpl_propertylist *plist)
find out the DIT value
cpl_error_code moo_pfits_append_hduclass_data(cpl_propertylist *plist, moo_detector_type type, int ntas)
Set the HDUCLASS DATA Keyword.
double moo_pfits_get_exptime(const cpl_propertylist *plist)
find out the EXPTIME value
cpl_frame * moo_products_add_sci(moo_products *self, moo_sci *sci, cpl_frame_level level, const char *tag, const char *filename, const cpl_frame *inherit_frame)
create a product from a SCI object
the different type of detectors