MOONS Pipeline Reference Manual 0.13.1
moo_combine_sky.c
1/*
2 * This file is part of the MOONS Pipeline
3 * Copyright (C) 2002-2016 European Southern Observatory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24/*-----------------------------------------------------------------------------
25 Includes
26 -----------------------------------------------------------------------------*/
27#include <math.h>
28#include <string.h>
29#include <cpl.h>
30#include <hdrl.h>
31#include "moo_utils.h"
32#include "moo_dfs.h"
33#include "moo_pfits.h"
34#include "moo_qc.h"
35#include "moo_badpix.h"
36#include "moo_combine_sky.h"
37/*----------------------------------------------------------------------------*/
42/*----------------------------------------------------------------------------*/
43
46/*-----------------------------------------------------------------------------
47 Function codes
48 -----------------------------------------------------------------------------*/
49
50/*----------------------------------------------------------------------------*/
62/*----------------------------------------------------------------------------*/
63static cpl_image *
64_moo_qual_fromlist(cpl_imagelist *list)
65{
66 cpl_ensure(list, CPL_ERROR_NULL_INPUT, NULL);
67
68 int size = cpl_imagelist_get_size(list);
69 cpl_ensure(size > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
70
71 const cpl_image *qimg = cpl_imagelist_get_const(list, 0);
72
73 if (size > 1) {
74 int k;
75
76 int nx = cpl_image_get_size_x(qimg);
77 int ny = cpl_image_get_size_y(qimg);
78 const int *qdata = cpl_image_get_data_int_const(qimg);
79
80 for (k = 1; k < size; k++) {
81 cpl_image *qimg2 = cpl_imagelist_get(list, k);
82 int *qdata2 = cpl_image_get_data_int(qimg2);
83 for (int i = 0; i < nx * ny; i++) {
84 if (qdata[i] != qdata2[i]) {
85 cpl_msg_error(__func__,
86 "QUAL of first frame and %d are not equal",
87 k);
88 }
89 }
90 }
91 }
92 return cpl_image_duplicate(qimg);
93}
94
95static moo_rbn_single *
96_moo_collapse_median(hdrl_imagelist *list,
97 cpl_imagelist *quallist,
99{
100 moo_rbn_single *single = NULL;
101 int j;
102 cpl_ensure(list != NULL, CPL_ERROR_ILLEGAL_INPUT, NULL);
103
104 int size = hdrl_imagelist_get_size(list);
105
106 if (size > 0) {
107 hdrl_image *res = NULL;
108 cpl_image *contrib = NULL;
109 hdrl_imagelist_collapse_median(list, &res, &contrib);
110
111 for (j = 0; j < size; j++) {
112 hdrl_imagelist_unset(list, 0);
113 }
114
115 cpl_image *qual = _moo_qual_fromlist(quallist);
116
117 single = moo_rbn_single_new(type);
118 single->qual = qual;
119 single->image = res;
120
121 for (j = 0; j < size; j++) {
122 cpl_imagelist_unset(quallist, 0);
123 }
124
125 cpl_image_delete(contrib);
126 }
127
128 return single;
129}
130
131static cpl_error_code
132_moo_copy_header_kw(cpl_propertylist *res_header,
133 const cpl_propertylist *orig_header)
134{
135 cpl_ensure_code(res_header != NULL, CPL_ERROR_NULL_INPUT);
136 cpl_ensure_code(orig_header != NULL, CPL_ERROR_NULL_INPUT);
137
138 cpl_propertylist_copy_property_regexp(res_header, orig_header, "ESO DET *",
139 0);
140 cpl_propertylist_copy_property_regexp(res_header, orig_header, "HDU*", 0);
141 cpl_propertylist_copy_property(res_header, orig_header, MOO_PFITS_BUNIT);
142 cpl_propertylist_copy_property(res_header, orig_header, MOO_PFITS_ERRDATA);
143 cpl_propertylist_copy_property(res_header, orig_header, MOO_PFITS_QUALDATA);
144 cpl_propertylist_copy_property(res_header, orig_header, MOO_PFITS_CRPIX1);
145 cpl_propertylist_copy_property(res_header, orig_header, MOO_PFITS_CRPIX2);
146 cpl_propertylist_copy_property(res_header, orig_header, MOO_PFITS_CRVAL1);
147 cpl_propertylist_copy_property(res_header, orig_header, MOO_PFITS_CRVAL2);
148 cpl_propertylist_copy_property(res_header, orig_header, MOO_PFITS_CTYPE1);
149 cpl_propertylist_copy_property(res_header, orig_header, MOO_PFITS_CTYPE2);
150 cpl_propertylist_copy_property(res_header, orig_header, MOO_PFITS_CD1_1);
151 cpl_propertylist_copy_property(res_header, orig_header, MOO_PFITS_CD1_2);
152 cpl_propertylist_copy_property(res_header, orig_header, MOO_PFITS_CUNIT1);
153 cpl_propertylist_copy_property(res_header, orig_header, MOO_PFITS_CD2_1);
154 cpl_propertylist_copy_property(res_header, orig_header, MOO_PFITS_CD2_2);
155
156 return cpl_error_get_code();
157}
158
159static cpl_error_code
160_moo_combine_sky_single(moo_rbnlist *rbnlist,
161 moo_rbn *orig_rbn,
163 unsigned int badpix_level,
164 moo_rbn *res)
165{
166 cpl_error_code status = CPL_ERROR_NONE;
167 moo_rbn_single *single = NULL;
168 moo_rbn_single *orig_single = NULL;
169
170 moo_rbnlist_load_single(rbnlist, type, badpix_level);
171 hdrl_imagelist *list = moo_rbnlist_get_image(rbnlist, type);
172 cpl_imagelist *qlist = moo_rbnlist_get_single_qual(rbnlist, type);
173
174 orig_single = moo_rbn_get_single(orig_rbn, type);
175 single = _moo_collapse_median(list, qlist, type);
176
177 if (single != NULL) {
178 cpl_propertylist *header = NULL;
179 moo_try_check(header = moo_rbn_single_get_header(orig_single), " ");
180 moo_try_check(_moo_copy_header_kw(single->header, header), " ");
181 }
182moo_try_cleanup:
183 moo_rbn_set_single(res, single);
184 cpl_imagelist_unwrap(qlist);
185 hdrl_imagelist_unwrap(list);
186 moo_rbnlist_free_single(rbnlist, type);
187 return status;
188}
189/*----------------------------------------------------------------------------*/
206/*----------------------------------------------------------------------------*/
207moo_rbn *
208moo_combine_sky(moo_rbnlist *rbnlist)
209{
210 moo_rbn *res = NULL;
211 cpl_ensure(rbnlist != NULL, CPL_ERROR_NULL_INPUT, NULL);
212 cpl_ensure(rbnlist->size > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
213
214 cpl_errorstate prestate = cpl_errorstate_get();
215
216 cpl_msg_info(__func__, "Combine sky of %d frames", (int)rbnlist->size);
217
218 int badpix_level = MOO_BADPIX_GOOD;
219
220 moo_rbn *orig_rbn = moo_rbnlist_get(rbnlist, 0);
221 res = moo_rbn_new();
222 res->primary_header = cpl_propertylist_duplicate(orig_rbn->primary_header);
223 moo_qc_set_ncom(res->primary_header, rbnlist->size);
224 cpl_errorstate prev_state = cpl_errorstate_get();
225 res->fibre_table = cpl_table_duplicate(moo_rbn_get_fibre_table(orig_rbn));
226
227 if (!cpl_errorstate_is_equal(prev_state)) {
228 cpl_msg_info(__func__, "DET input files do not have a fibre table.");
229 cpl_errorstate_set(prev_state);
230 }
231
232 for (int i = 0; i < 3; i++) {
233 moo_try_check(_moo_combine_sky_single(rbnlist, orig_rbn, i,
234 badpix_level, res),
235 " ");
236 }
237
238moo_try_cleanup:
239 if (!cpl_errorstate_is_equal(prestate)) {
240 cpl_msg_error(__func__, "Error in remove CRH");
241 cpl_errorstate_dump(prestate, CPL_FALSE, cpl_errorstate_dump_one);
242 moo_rbn_delete(res);
243 res = NULL;
244 }
245 return res;
246}
#define MOO_BADPIX_GOOD
Definition: moo_badpix.h:36
enum _moo_detector_type_ moo_detector_type
The type code type.
Definition: moo_detector.h:64
cpl_error_code moo_rbnlist_free_single(const moo_rbnlist *self, moo_detector_type type)
Free the type part for all DET in the detlist.
Definition: moo_rbnlist.c:137
cpl_propertylist * moo_rbn_single_get_header(moo_rbn_single *self)
Get header of rbn single.
moo_rbn_single * moo_rbn_single_new(moo_detector_type type)
Create a new moo_rbn_single.
cpl_error_code moo_rbn_set_single(moo_rbn *self, moo_rbn_single *single)
assign moo_rbn_single structure in moo_rbn structure
Definition: moo_rbn.c:184
void moo_rbn_delete(moo_rbn *self)
Delete a moo_rbn.
Definition: moo_rbn.c:120
moo_rbn_single * moo_rbn_get_single(moo_rbn *self, moo_detector_type type)
Get a RBN single from RBN.
Definition: moo_rbn.c:325
cpl_table * moo_rbn_get_fibre_table(moo_rbn *self)
Get the FIBRE TABLE in RBN.
Definition: moo_rbn.c:397
moo_rbn * moo_rbn_new(void)
Create a new moo_rbn.
Definition: moo_rbn.c:67
cpl_error_code moo_rbnlist_load_single(const moo_rbnlist *self, moo_detector_type type, int level)
Load the type part for all RBN in the rbnlist.
Definition: moo_rbnlist.c:104
hdrl_imagelist * moo_rbnlist_get_image(const moo_rbnlist *self, moo_detector_type type)
Get the all the images of the type part in the rbnlist.
Definition: moo_rbnlist.c:212
cpl_imagelist * moo_rbnlist_get_single_qual(const moo_rbnlist *self, moo_detector_type type)
Get the type QUAL part for all RBN in the rbnlist.
Definition: moo_rbnlist.c:294
moo_rbn * moo_rbnlist_get(moo_rbnlist *self, int i)
Get the RBN at the position i in the list.
Definition: moo_rbnlist.c:186
moo_rbn * moo_combine_sky(moo_rbnlist *rbnlist)
Combine RBN sky frames.
cpl_error_code moo_qc_set_ncom(cpl_propertylist *plist, int val)
Set the QC.NCOM value.
Definition: moo_qc.c:149