32 #include "muse_qi_mask_z.h" 50 muse_qi_mask_wavecal_polys(cpl_propertylist *aHeader, cpl_table *aWC,
53 if (!aHeader || !aWC || !aTT) {
54 cpl_error_set_message(__func__, CPL_ERROR_NULL_INPUT,
"Could not create per" 55 "-slice wavelength polynomials in the FITS header!");
60 const unsigned int kSteps = 50;
63 cpl_matrix *pos = cpl_matrix_new(1, kMuseSlicesPerCCD * kSteps);
64 cpl_vector *val = cpl_vector_new(kMuseSlicesPerCCD * kSteps),
65 *ycenters = cpl_vector_new(kMuseSlicesPerCCD);
67 unsigned short nslice;
68 for (nslice = 1; nslice <= kMuseSlicesPerCCD; nslice++) {
72 double x = cpl_polynomial_eval_1d(pt[MUSE_TRACE_CENTER], kMuseOutputYTop/2,
76 cpl_polynomial *pconst = cpl_polynomial_new(1);
78 cpl_polynomial_set_coeff(pconst, &pows, x);
80 cpl_polynomial *pwcen = cpl_polynomial_extract(pw, 0, pconst);
81 double ycen = cpl_polynomial_eval_1d(pwcen, kMuseOutputYTop/2, NULL);
82 cpl_vector_set(ycenters, nslice - 1, ycen);
83 cpl_polynomial_delete(pconst);
84 cpl_polynomial_delete(pw);
88 double c0 = cpl_polynomial_get_coeff(pwcen, &pows);
89 cpl_polynomial_set_coeff(pwcen, &pows, c0 - ycen);
93 iy <= (unsigned)kMuseOutputYTop;
94 iy += ((unsigned)kMuseOutputYTop) / (kSteps - 1)) {
95 cpl_matrix_set(pos, 0, ipos, iy);
96 cpl_vector_set(val, ipos++, cpl_polynomial_eval_1d(pwcen, iy, NULL));
101 cpl_polynomial *fit = cpl_polynomial_new(1);
103 cpl_polynomial_fit(fit, pos, NULL, val, NULL, CPL_TRUE, NULL, &maxdeg);
105 cpl_vector *res = cpl_vector_duplicate(val);
107 cpl_vector_fill_polynomial_fit_residual(res, val, NULL, fit, pos, &chisq);
108 const double mse = cpl_vector_product(res, res)
109 / cpl_vector_get_size(res);
110 cpl_vector_delete(res);
111 cpl_msg_debug(__func__,
"MSE=%g ChiSq=%g", mse, chisq);
113 cpl_vector_delete(val);
114 cpl_matrix_delete(pos);
117 char keyword[KEYWORD_LENGTH];
119 for (nslice = 1; nslice <= kMuseSlicesPerCCD; nslice++) {
120 snprintf(keyword, KEYWORD_LENGTH,
"ESO DET WLEN SLICE%hu POLY0", nslice);
122 double zeropoint = cpl_polynomial_get_coeff(fit, &j)
123 + cpl_vector_get(ycenters, nslice - 1);
124 cpl_propertylist_append_float(aHeader, keyword, zeropoint);
125 cpl_propertylist_set_comment(aHeader, keyword,
126 "[Angstrom] Slice-specific zero-point");
128 cpl_vector_delete(ycenters);
131 for (j = 1; j <= 2; j++) {
132 snprintf(keyword, KEYWORD_LENGTH,
"ESO DET WLEN POLY%d", (
int)j);
133 cpl_propertylist_append_float(aHeader, keyword,
134 cpl_polynomial_get_coeff(fit, &j));
136 snprintf(comment, 99,
"[Angstrom] Common %s-order polynomial coefficient",
137 j == 1 ?
"1st" :
"2nd");
138 cpl_propertylist_set_comment(aHeader, keyword, comment);
140 cpl_polynomial_delete(fit);
163 cpl_msg_info(__func__,
"Creating mask for full MUSE wavelength range " 164 "(%.1f...%.1f)", wmin, wmax);
168 cpl_frame *frame = cpl_frameset_find(aProcessing->
inframes, MUSE_TAG_BIAS);
174 cpl_propertylist *header = cpl_propertylist_new();
175 cpl_propertylist_append_string(header,
"OBJECT",
"Mask full MUSE range");
176 cpl_propertylist_append_float(header,
"ESO DET FRS WMIN", wmin);
177 cpl_propertylist_set_comment(header,
"ESO DET FRS WMIN",
"[Angstrom] Minimum wavelength");
178 cpl_propertylist_append_float(header,
"ESO DET FRS WMAX", wmax);
179 cpl_propertylist_set_comment(header,
"ESO DET FRS WMAX",
"[Angstrom] Maximum wavelength");
181 header, MUSE_TAG_MASK_IMAGE);
182 cpl_propertylist_delete(header);
185 frame = cpl_frameset_find(aProcessing->
outframes, MUSE_TAG_MASK_IMAGE);
186 const char *fn = cpl_frame_get_filename(frame);
189 int n1 = aParams->
nifu ? aParams->
nifu : 1,
190 n2 = aParams->
nifu ? aParams->
nifu : kMuseNumIFUs,
192 for (nifu = n1; nifu <= n2; nifu++) {
194 cpl_errorstate prestate = cpl_errorstate_get();
198 cpl_msg_debug(__func__,
"succeeded to load %u raw images, using first one",
202 cpl_msg_warning(__func__,
"failed to load raw image, assuming 1x1 binning");
206 image->
data = cpl_image_new(kMuseOutputXRight, kMuseOutputYTop,
208 image->
header = cpl_propertylist_new();
209 cpl_propertylist_append_int(image->
header,
"ESO DET BINX", 1);
210 cpl_propertylist_append_int(image->
header,
"ESO DET BINY", 1);
217 MUSE_TAG_WAVECAL_TABLE,
220 MUSE_TAG_TRACE_TABLE, nifu);
226 if (binx != 1 || biny != 1) {
228 imunbinned->
data = cpl_image_new(kMuseOutputXRight, kMuseOutputYTop,
231 cpl_image *wavemap =
muse_wave_map(imunbinned, wavecaltable, tracetable);
234 if (binx != 1 || biny != 1) {
236 cpl_msg_info(__func__,
"Rebinning wavelength map %dx%d", binx, biny);
237 cpl_image *binned = cpl_image_rebin(wavemap, 1, 1, binx, biny);
238 cpl_image_delete(wavemap);
240 wavemap = cpl_image_divide_scalar_create(binned, binx * biny);
241 cpl_image_delete(binned);
245 cpl_mask *mask = cpl_mask_threshold_image_create(wavemap, wmin, wmax);
248 header = cpl_propertylist_new();
249 if (binx == 1 && biny == 1) {
250 muse_qi_mask_wavecal_polys(header, wavecaltable, tracetable);
257 int nx = cpl_mask_get_size_x(mask),
258 ny = cpl_mask_get_size_y(mask);
260 cpl_image *labeled = cpl_image_labelise_mask_create(mask, &ndet);
262 cpl_apertures *apertures = cpl_apertures_new_from_image(labeled, labeled);
263 int napertures = cpl_apertures_get_size(apertures);
267 cpl_matrix *mlabels = cpl_matrix_new(2, napertures);
269 for (n = 1; n <= napertures; n++) {
270 cpl_matrix_set(mlabels, 0, n - 1,
271 cpl_apertures_get_left(apertures, n));
272 cpl_matrix_set(mlabels, 1, n - 1, n);
274 cpl_matrix_sort_columns(mlabels, 1);
276 cpl_matrix_dump(mlabels, stdout);
280 for (m = 1; m <= ndet; m++) {
281 n = cpl_matrix_get(mlabels, 1, m - 1);
283 int x1 = cpl_apertures_get_left(apertures, n),
284 x2 = cpl_apertures_get_right(apertures, n),
285 y1 = cpl_apertures_get_bottom(apertures, n),
286 y2 = cpl_apertures_get_top(apertures, n);
287 if (x1 == x2 || y1 == y2) {
291 for (i = x1; i <= x2; i++) {
293 for (j = y1; j <= y2; j++) {
294 cpl_mask_set(mask, i, j, CPL_BINARY_0);
297 cpl_msg_debug(__func__,
"Excluding aperture [%d:%d,%d:%d]", x1, x2, y1, y2);
306 if (x1 > nx/2+32/binx) x1 += 64 / binx;
307 if (x2 > nx/2+32/binx) x2 += 64 / binx;
308 if (y1 > ny/2+32/biny) y1 += 64 / biny;
309 if (y2 > ny/2+32/biny) y2 += 64 / biny;
311 int xc = (x1 + x2) / 2,
315 cpl_msg_debug(__func__,
"Aperture %2d Slice %2d: [%d:%d,%d:%d] center %d,%d size %dx%d",
316 n, m, x1, x2, y1, y2, xc, yc, w, h);
318 char keyword[KEYWORD_LENGTH];
319 snprintf(keyword, KEYWORD_LENGTH,
"ESO DET SLICE%d XSTART", nslice);
320 cpl_propertylist_append_int(header, keyword, x1);
321 cpl_propertylist_set_comment(header, keyword,
"[pix] Start position of the slice along X");
322 snprintf(keyword, KEYWORD_LENGTH,
"ESO DET SLICE%d YSTART", nslice);
323 cpl_propertylist_append_int(header, keyword, y1);
324 cpl_propertylist_set_comment(header, keyword,
"[pix] Start position of the slice along Y");
325 snprintf(keyword, KEYWORD_LENGTH,
"ESO DET SLICE%d XEND", nslice);
326 cpl_propertylist_append_int(header, keyword, x2);
327 cpl_propertylist_set_comment(header, keyword,
"[pix] End position of the slice along X");
328 snprintf(keyword, KEYWORD_LENGTH,
"ESO DET SLICE%d YEND", nslice);
329 cpl_propertylist_append_int(header, keyword, y2);
330 cpl_propertylist_set_comment(header, keyword,
"[pix] End position of the slice along Y");
333 cpl_image_delete(labeled);
334 cpl_apertures_delete(apertures);
335 cpl_matrix_delete(mlabels);
336 if (nslice - 1 != kMuseSlicesPerCCD) {
337 cpl_msg_warning(__func__,
"Found %d slices (%d apertures) instead of %d",
338 nslice - 1, napertures, kMuseSlicesPerCCD);
342 cpl_mask *untrimmed = cpl_mask_new(nx + 4*32/binx, ny + 4*32/biny);
343 cpl_mask *m1 = cpl_mask_extract(mask, 1, 1, nx/2-1, ny/2-1),
344 *m2 = cpl_mask_extract(mask, 1, ny/2, nx/2-1, ny),
345 *m3 = cpl_mask_extract(mask, nx/2, 1, nx, ny/2-1),
346 *m4 = cpl_mask_extract(mask, nx/2, ny/2, nx, ny);
347 cpl_mask_copy(untrimmed, m1, 32/binx+1, 32/biny+1);
348 cpl_mask_copy(untrimmed, m2, 32/binx+1, ny/2+3*32/biny);
349 cpl_mask_copy(untrimmed, m3, nx/2+3*32/binx, 32/biny+1);
350 cpl_mask_copy(untrimmed, m4, nx/2+3*32/binx, ny/2+3*32/biny);
355 cpl_mask_delete(mask);
361 cpl_propertylist_copy_property_regexp(header, image->
header,
362 "ESO DET (CHIP |OUT)|EXTNAME", 0);
365 char *extname = cpl_sprintf(
"CHAN%02d", nifu);
366 cpl_propertylist_append_string(header,
"EXTNAME", extname);
369 cpl_mask_save(mask, fn, header, CPL_IO_EXTEND);
370 cpl_propertylist_delete(header);
371 cpl_image_delete(wavemap);
372 cpl_mask_delete(mask);
373 cpl_table_delete(tracetable);
374 cpl_table_delete(wavecaltable);
muse_imagelist * muse_basicproc_load(muse_processing *aProcessing, unsigned char aIFU, muse_basicproc_params *aBPars)
Load the raw input files from disk and do basic processing.
cpl_polynomial ** muse_trace_table_get_polys_for_slice(const cpl_table *aTable, const unsigned short aSlice)
construct polynomial from the trace table entry for the given slice
Structure definition for a collection of muse_images.
void muse_image_delete(muse_image *aImage)
Deallocate memory associated to a muse_image object.
cpl_polynomial * muse_wave_table_get_poly_for_slice(const cpl_table *aTable, const unsigned short aSlice)
Construct polynomial from the wavelength calibration table entry for the given slice.
cpl_image * data
the data extension
void muse_imagelist_delete(muse_imagelist *aList)
Free the memory of the MUSE image list.
Structure definition of MUSE three extension FITS file.
cpl_propertylist * header
the FITS header
unsigned int muse_imagelist_get_size(muse_imagelist *aList)
Return the number of stored images.
void muse_trace_polys_delete(cpl_polynomial *aPolys[])
Delete the multi-polynomial array created in relation to tracing.
cpl_error_code muse_processing_save_header(muse_processing *aProcessing, int aIFU, cpl_propertylist *aHeader, const char *aTag)
Save a FITS header to disk.
int muse_pfits_get_biny(const cpl_propertylist *aHeaders)
find out the binning factor in y direction
muse_image * muse_imagelist_get(muse_imagelist *aList, unsigned int aIdx)
Get the muse_image of given list index.
void muse_cplerrorstate_dump_some(unsigned aCurrent, unsigned aFirst, unsigned aLast)
Dump some CPL errors.
cpl_image * muse_wave_map(muse_image *aImage, const cpl_table *aWave, const cpl_table *aTrace)
Write out a wavelength map for visual checks.
void muse_processing_append_used(muse_processing *aProcessing, cpl_frame *aFrame, cpl_frame_group aGroup, int aDuplicate)
Add a frame to the set of used frames.
Structure to hold the parameters of the muse_qi_mask recipe.
int muse_pfits_get_binx(const cpl_propertylist *aHeaders)
find out the binning factor in x direction
cpl_table * muse_processing_load_ctable(muse_processing *aProcessing, const char *aTag, unsigned char aIFU)
Load a CPL table according to its tag and IFU/channel number.
muse_imagelist * muse_imagelist_new(void)
Create a new (empty) MUSE image list.
muse_image * muse_image_new(void)
Allocate memory for a new muse_image object.
cpl_error_code muse_imagelist_set(muse_imagelist *aList, muse_image *aImage, unsigned int aIdx)
Set the muse_image of given list index.
int nifu
IFU to handle. If set to 0, all IFUs are processed serially, which is the recommendation for this rec...