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, CPL_FALSE, 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, nifu);
219 MUSE_TAG_TRACE_TABLE, nifu);
225 if (binx != 1 || biny != 1) {
227 imunbinned->
data = cpl_image_new(kMuseOutputXRight, kMuseOutputYTop,
230 cpl_image *wavemap =
muse_wave_map(imunbinned, wavecaltable, tracetable);
233 if (binx != 1 || biny != 1) {
235 cpl_msg_info(__func__,
"Rebinning wavelength map %dx%d", binx, biny);
236 cpl_image *binned = cpl_image_rebin(wavemap, 1, 1, binx, biny);
237 cpl_image_delete(wavemap);
239 wavemap = cpl_image_divide_scalar_create(binned, binx * biny);
240 cpl_image_delete(binned);
244 cpl_mask *mask = cpl_mask_threshold_image_create(wavemap, wmin, wmax);
247 header = cpl_propertylist_new();
248 if (binx == 1 && biny == 1) {
249 muse_qi_mask_wavecal_polys(header, wavecaltable, tracetable);
256 int nx = cpl_mask_get_size_x(mask),
257 ny = cpl_mask_get_size_y(mask);
259 cpl_image *labeled = cpl_image_labelise_mask_create(mask, &ndet);
261 cpl_apertures *apertures = cpl_apertures_new_from_image(labeled, labeled);
262 int napertures = cpl_apertures_get_size(apertures);
266 cpl_matrix *mlabels = cpl_matrix_new(2, napertures);
268 for (n = 1; n <= napertures; n++) {
269 cpl_matrix_set(mlabels, 0, n - 1,
270 cpl_apertures_get_left(apertures, n));
271 cpl_matrix_set(mlabels, 1, n - 1, n);
273 cpl_matrix_sort_columns(mlabels, 1);
275 cpl_matrix_dump(mlabels, stdout);
279 for (m = 1; m <= ndet; m++) {
280 n = cpl_matrix_get(mlabels, 1, m - 1);
282 int x1 = cpl_apertures_get_left(apertures, n),
283 x2 = cpl_apertures_get_right(apertures, n),
284 y1 = cpl_apertures_get_bottom(apertures, n),
285 y2 = cpl_apertures_get_top(apertures, n);
286 if (x1 == x2 || y1 == y2) {
290 for (i = x1; i <= x2; i++) {
292 for (j = y1; j <= y2; j++) {
293 cpl_mask_set(mask, i, j, CPL_BINARY_0);
296 cpl_msg_debug(__func__,
"Excluding aperture [%d:%d,%d:%d]", x1, x2, y1, y2);
305 if (x1 > nx/2+32/binx) x1 += 64 / binx;
306 if (x2 > nx/2+32/binx) x2 += 64 / binx;
307 if (y1 > ny/2+32/biny) y1 += 64 / biny;
308 if (y2 > ny/2+32/biny) y2 += 64 / biny;
310 int xc = (x1 + x2) / 2,
314 cpl_msg_debug(__func__,
"Aperture %2d Slice %2d: [%d:%d,%d:%d] center %d,%d size %dx%d",
315 n, m, x1, x2, y1, y2, xc, yc, w, h);
317 char keyword[KEYWORD_LENGTH];
318 snprintf(keyword, KEYWORD_LENGTH,
"ESO DET SLICE%d XSTART", nslice);
319 cpl_propertylist_append_int(header, keyword, x1);
320 cpl_propertylist_set_comment(header, keyword,
"[pix] Start position of the slice along X");
321 snprintf(keyword, KEYWORD_LENGTH,
"ESO DET SLICE%d YSTART", nslice);
322 cpl_propertylist_append_int(header, keyword, y1);
323 cpl_propertylist_set_comment(header, keyword,
"[pix] Start position of the slice along Y");
324 snprintf(keyword, KEYWORD_LENGTH,
"ESO DET SLICE%d XEND", nslice);
325 cpl_propertylist_append_int(header, keyword, x2);
326 cpl_propertylist_set_comment(header, keyword,
"[pix] End position of the slice along X");
327 snprintf(keyword, KEYWORD_LENGTH,
"ESO DET SLICE%d YEND", nslice);
328 cpl_propertylist_append_int(header, keyword, y2);
329 cpl_propertylist_set_comment(header, keyword,
"[pix] End position of the slice along Y");
332 cpl_image_delete(labeled);
333 cpl_apertures_delete(apertures);
334 cpl_matrix_delete(mlabels);
335 if (nslice - 1 != kMuseSlicesPerCCD) {
336 cpl_msg_warning(__func__,
"Found %d slices (%d apertures) instead of %d",
337 nslice - 1, napertures, kMuseSlicesPerCCD);
341 cpl_mask *untrimmed = cpl_mask_new(nx + 4*32/binx, ny + 4*32/biny);
342 cpl_mask *m1 = cpl_mask_extract(mask, 1, 1, nx/2-1, ny/2-1),
343 *m2 = cpl_mask_extract(mask, 1, ny/2, nx/2-1, ny),
344 *m3 = cpl_mask_extract(mask, nx/2, 1, nx, ny/2-1),
345 *m4 = cpl_mask_extract(mask, nx/2, ny/2, nx, ny);
346 cpl_mask_copy(untrimmed, m1, 32/binx+1, 32/biny+1);
347 cpl_mask_copy(untrimmed, m2, 32/binx+1, ny/2+3*32/biny);
348 cpl_mask_copy(untrimmed, m3, nx/2+3*32/binx, 32/biny+1);
349 cpl_mask_copy(untrimmed, m4, nx/2+3*32/binx, ny/2+3*32/biny);
354 cpl_mask_delete(mask);
360 cpl_propertylist_copy_property_regexp(header, image->
header,
361 "ESO DET (CHIP |OUT)|EXTNAME", 0);
364 char *extname = cpl_sprintf(
"CHAN%02d", nifu);
365 cpl_propertylist_append_string(header,
"EXTNAME", extname);
368 cpl_mask_save(mask, fn, header, CPL_IO_EXTEND);
369 cpl_propertylist_delete(header);
370 cpl_image_delete(wavemap);
371 cpl_mask_delete(mask);
372 cpl_table_delete(tracetable);
373 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
muse_imagelist * muse_imagelist_new(void)
Create a new (empty) MUSE image list.
cpl_table * muse_table_load(muse_processing *aProcessing, const char *aTag, unsigned char aIFU)
load a table according to its tag and IFU/channel number
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...