35 #include "muse_ampl_z.h"
69 static const char *muse_ampl_help =
70 "This recipe combines several separate amplifier images (flat-fields with special FITS headers containing pico amplifier measurements) into one master image file and computes the instrumental throughput per IFU (and slice). Processing trims the raw data and records the overscan statistics, subtracts the bias (taking account of the overscan, if overscan is not &none&), and optionally, the dark from each raw input image, converts them from adu to count, scales them according to their exposure time, and combines them using input parameters. To compute the throughput the image is converted into a pixel table, the flux is then integrated over the given filter band, and the ratio of the expected flux (FITS header INS.AMPL2.CURR) to measured flux is taken, in the same units. If a geometry table was given as input, the relative area of the IFUs is taken into account when computing the flux per unit area. The resulting ratio is the instrument efficiency (throughput) and saved as QC parameters for the whole input image and per slice in the output pixel table.";
72 static const char *muse_ampl_help_esorex =
73 "\n\nInput frames for raw frame tag \"AMPL\":\n"
74 "\n Frame tag Type Req #Fr Description"
75 "\n -------------------- ---- --- --- ------------"
76 "\n AMPL raw Y >=3 Special raw flat-field taken with pico-amplifier readings in the FITS header"
77 "\n MASTER_BIAS calib Y 1 Master bias"
78 "\n MASTER_DARK calib . 1 Master dark"
79 "\n BADPIX_TABLE calib . 1 Known bad pixels"
80 "\n GEOMETRY_TABLE calib . 1 Relative positions of the slices in the field of view"
81 "\n FILTER_LIST calib Y 1 Filter definitions; here, it has to contain the filter curve for the filter given in INS.AMPL2.FILTER"
82 "\n TRACE_TABLE calib Y 1 Tracing table for all slices"
83 "\n WAVECAL_TABLE calib Y 1 Wavelength calibration table"
84 "\n\nProduct frames for raw frame tag \"AMPL\":\n"
85 "\n Frame tag Level Description"
86 "\n -------------------- -------- ------------"
87 "\n MASTER_AMPL final Combined master AMPL image, written if --savemaster=true"
88 "\n TABLE_AMPL final Output table with computations for each CCD pixel, written if --savetable=true."
89 "\n AMPL_CONVOLVED final Combined and convolved master AMPL image";
100 static cpl_recipeconfig *
101 muse_ampl_new_recipeconfig(
void)
103 cpl_recipeconfig *recipeconfig = cpl_recipeconfig_new();
107 cpl_recipeconfig_set_tag(recipeconfig, tag, 3, -1);
108 cpl_recipeconfig_set_input(recipeconfig, tag,
"MASTER_BIAS", 1, 1);
109 cpl_recipeconfig_set_input(recipeconfig, tag,
"MASTER_DARK", -1, 1);
110 cpl_recipeconfig_set_input(recipeconfig, tag,
"BADPIX_TABLE", -1, 1);
111 cpl_recipeconfig_set_input(recipeconfig, tag,
"GEOMETRY_TABLE", -1, 1);
112 cpl_recipeconfig_set_input(recipeconfig, tag,
"FILTER_LIST", 1, 1);
113 cpl_recipeconfig_set_input(recipeconfig, tag,
"TRACE_TABLE", 1, 1);
114 cpl_recipeconfig_set_input(recipeconfig, tag,
"WAVECAL_TABLE", 1, 1);
115 cpl_recipeconfig_set_output(recipeconfig, tag,
"MASTER_AMPL");
116 cpl_recipeconfig_set_output(recipeconfig, tag,
"TABLE_AMPL");
117 cpl_recipeconfig_set_output(recipeconfig, tag,
"AMPL_CONVOLVED");
133 static cpl_error_code
134 muse_ampl_prepare_header(
const char *aFrametag, cpl_propertylist *aHeader)
136 cpl_ensure_code(aFrametag, CPL_ERROR_NULL_INPUT);
137 cpl_ensure_code(aHeader, CPL_ERROR_NULL_INPUT);
138 if (!strcmp(aFrametag,
"MASTER_AMPL")) {
141 "Number of saturated pixels in raw image i in input list");
144 "Number of saturated pixels in output master image");
145 }
else if (!strcmp(aFrametag,
"TABLE_AMPL")) {
148 "[ph] Integrated number of photons detected on the CCD");
151 "[W] Integrated power detected on the CCD");
154 "[%] Channel throughput compared to photodiode a");
157 "[%] Estimated error of throughput compared to photodiode 2");
160 "[%] Slice j throughput compared to photodiode 2");
161 }
else if (!strcmp(aFrametag,
"AMPL_CONVOLVED")) {
164 "Number of saturated pixels in raw image i in input list");
167 "Number of saturated pixels in output master image");
170 "[ph] Integrated number of photons detected on the CCD");
173 "[W] Integrated power detected on the CCD");
176 "[%] Channel throughput compared to photodiode a");
179 "[%] Estimated error of throughput compared to photodiode 2");
182 "[%] Slice j throughput compared to photodiode 2");
184 cpl_msg_warning(__func__,
"Frame tag %s is not defined", aFrametag);
185 return CPL_ERROR_ILLEGAL_INPUT;
187 return CPL_ERROR_NONE;
200 static cpl_frame_level
201 muse_ampl_get_frame_level(
const char *aFrametag)
204 return CPL_FRAME_LEVEL_NONE;
206 if (!strcmp(aFrametag,
"MASTER_AMPL")) {
207 return CPL_FRAME_LEVEL_FINAL;
209 if (!strcmp(aFrametag,
"TABLE_AMPL")) {
210 return CPL_FRAME_LEVEL_FINAL;
212 if (!strcmp(aFrametag,
"AMPL_CONVOLVED")) {
213 return CPL_FRAME_LEVEL_FINAL;
215 return CPL_FRAME_LEVEL_NONE;
229 muse_ampl_get_frame_mode(
const char *aFrametag)
234 if (!strcmp(aFrametag,
"MASTER_AMPL")) {
237 if (!strcmp(aFrametag,
"TABLE_AMPL")) {
240 if (!strcmp(aFrametag,
"AMPL_CONVOLVED")) {
258 muse_ampl_create(cpl_plugin *aPlugin)
262 if (cpl_plugin_get_type(aPlugin) == CPL_PLUGIN_TYPE_RECIPE) {
263 recipe = (cpl_recipe *)aPlugin;
271 muse_ampl_new_recipeconfig(),
272 muse_ampl_prepare_header,
273 muse_ampl_get_frame_level,
274 muse_ampl_get_frame_mode);
279 cpl_msg_set_time_on();
283 recipe->parameters = cpl_parameterlist_new();
288 p = cpl_parameter_new_range(
"muse.muse_ampl.nifu",
290 "IFU to handle. If set to 0, all IFUs are processed serially. If set to -1, all IFUs are processed in parallel.",
295 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG,
"nifu");
296 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"nifu");
298 cpl_parameterlist_append(recipe->parameters, p);
301 p = cpl_parameter_new_value(
"muse.muse_ampl.overscan",
303 "If this is \"none\", stop when detecting discrepant overscan levels (see ovscsigma), for \"offset\" it assumes that the mean overscan level represents the real offset in the bias levels of the exposures involved, and adjusts the data accordingly; for \"vpoly\", a polynomial is fit to the vertical overscan and subtracted from the whole quadrant.",
305 (
const char *)
"vpoly");
306 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG,
"overscan");
307 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"overscan");
309 cpl_parameterlist_append(recipe->parameters, p);
312 p = cpl_parameter_new_value(
"muse.muse_ampl.ovscreject",
314 "This influences how values are rejected when computing overscan statistics. Either no rejection at all (\"none\"), rejection using the DCR algorithm (\"dcr\"), or rejection using an iterative constant fit (\"fit\").",
316 (
const char *)
"dcr");
317 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG,
"ovscreject");
318 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"ovscreject");
320 cpl_parameterlist_append(recipe->parameters, p);
323 p = cpl_parameter_new_value(
"muse.muse_ampl.ovscsigma",
325 "If the deviation of mean overscan levels between a raw input image and the reference image is higher than |ovscsigma x stdev|, stop the processing. If overscan=\"vpoly\", this is used as sigma rejection level for the iterative polynomial fit. Has no effect for overscan=\"offset\".",
328 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG,
"ovscsigma");
329 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"ovscsigma");
331 cpl_parameterlist_append(recipe->parameters, p);
334 p = cpl_parameter_new_value(
"muse.muse_ampl.ovscignore",
336 "The number of pixels of the overscan adjacent to the data region of the CCD that are ignored when computing statistics or fits.",
339 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG,
"ovscignore");
340 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"ovscignore");
342 cpl_parameterlist_append(recipe->parameters, p);
345 p = cpl_parameter_new_enum(
"muse.muse_ampl.combine",
347 "Type of combination to use",
349 (
const char *)
"sigclip",
351 (
const char *)
"average",
352 (
const char *)
"median",
353 (
const char *)
"minmax",
354 (
const char *)
"sigclip");
355 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG,
"combine");
356 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"combine");
358 cpl_parameterlist_append(recipe->parameters, p);
361 p = cpl_parameter_new_value(
"muse.muse_ampl.nlow",
363 "Number of minimum pixels to reject with minmax",
366 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG,
"nlow");
367 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"nlow");
369 cpl_parameterlist_append(recipe->parameters, p);
372 p = cpl_parameter_new_value(
"muse.muse_ampl.nhigh",
374 "Number of maximum pixels to reject with minmax",
377 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG,
"nhigh");
378 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"nhigh");
380 cpl_parameterlist_append(recipe->parameters, p);
383 p = cpl_parameter_new_value(
"muse.muse_ampl.nkeep",
385 "Number of pixels to keep with minmax",
388 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG,
"nkeep");
389 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"nkeep");
391 cpl_parameterlist_append(recipe->parameters, p);
394 p = cpl_parameter_new_value(
"muse.muse_ampl.lsigma",
396 "Low sigma for pixel rejection with sigclip",
399 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG,
"lsigma");
400 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"lsigma");
402 cpl_parameterlist_append(recipe->parameters, p);
405 p = cpl_parameter_new_value(
"muse.muse_ampl.hsigma",
407 "High sigma for pixel rejection with sigclip",
410 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG,
"hsigma");
411 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"hsigma");
413 cpl_parameterlist_append(recipe->parameters, p);
416 p = cpl_parameter_new_value(
"muse.muse_ampl.fbeam",
418 "Factor to describe the widening of the beam from the focal plane to photo diode 2.",
421 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG,
"fbeam");
422 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"fbeam");
424 cpl_parameterlist_append(recipe->parameters, p);
427 p = cpl_parameter_new_value(
"muse.muse_ampl.temp",
429 "Lamp temperature [K] used to create the black body function.",
432 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG,
"temp");
433 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"temp");
435 cpl_parameterlist_append(recipe->parameters, p);
438 p = cpl_parameter_new_value(
"muse.muse_ampl.savemaster",
440 "Save the processed and combined master image before any concolution is done.",
443 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG,
"savemaster");
444 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"savemaster");
446 cpl_parameterlist_append(recipe->parameters, p);
449 p = cpl_parameter_new_value(
"muse.muse_ampl.savetable",
451 "Save the table with all the processed pixel values.",
454 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG,
"savetable");
455 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"savetable");
457 cpl_parameterlist_append(recipe->parameters, p);
477 cpl_ensure_code(aParams, CPL_ERROR_NULL_INPUT);
478 cpl_ensure_code(aParameters, CPL_ERROR_NULL_INPUT);
481 p = cpl_parameterlist_find(aParameters,
"muse.muse_ampl.nifu");
482 cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
483 aParams->
nifu = cpl_parameter_get_int(p);
485 p = cpl_parameterlist_find(aParameters,
"muse.muse_ampl.overscan");
486 cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
487 aParams->
overscan = cpl_parameter_get_string(p);
489 p = cpl_parameterlist_find(aParameters,
"muse.muse_ampl.ovscreject");
490 cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
491 aParams->
ovscreject = cpl_parameter_get_string(p);
493 p = cpl_parameterlist_find(aParameters,
"muse.muse_ampl.ovscsigma");
494 cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
495 aParams->
ovscsigma = cpl_parameter_get_double(p);
497 p = cpl_parameterlist_find(aParameters,
"muse.muse_ampl.ovscignore");
498 cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
499 aParams->
ovscignore = cpl_parameter_get_int(p);
501 p = cpl_parameterlist_find(aParameters,
"muse.muse_ampl.combine");
502 cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
503 aParams->
combine_s = cpl_parameter_get_string(p);
505 (!strcasecmp(aParams->
combine_s,
"average")) ? MUSE_AMPL_PARAM_COMBINE_AVERAGE :
506 (!strcasecmp(aParams->
combine_s,
"median")) ? MUSE_AMPL_PARAM_COMBINE_MEDIAN :
507 (!strcasecmp(aParams->
combine_s,
"minmax")) ? MUSE_AMPL_PARAM_COMBINE_MINMAX :
508 (!strcasecmp(aParams->
combine_s,
"sigclip")) ? MUSE_AMPL_PARAM_COMBINE_SIGCLIP :
509 MUSE_AMPL_PARAM_COMBINE_INVALID_VALUE;
510 cpl_ensure_code(aParams->
combine != MUSE_AMPL_PARAM_COMBINE_INVALID_VALUE,
511 CPL_ERROR_ILLEGAL_INPUT);
513 p = cpl_parameterlist_find(aParameters,
"muse.muse_ampl.nlow");
514 cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
515 aParams->
nlow = cpl_parameter_get_int(p);
517 p = cpl_parameterlist_find(aParameters,
"muse.muse_ampl.nhigh");
518 cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
519 aParams->
nhigh = cpl_parameter_get_int(p);
521 p = cpl_parameterlist_find(aParameters,
"muse.muse_ampl.nkeep");
522 cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
523 aParams->
nkeep = cpl_parameter_get_int(p);
525 p = cpl_parameterlist_find(aParameters,
"muse.muse_ampl.lsigma");
526 cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
527 aParams->
lsigma = cpl_parameter_get_double(p);
529 p = cpl_parameterlist_find(aParameters,
"muse.muse_ampl.hsigma");
530 cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
531 aParams->
hsigma = cpl_parameter_get_double(p);
533 p = cpl_parameterlist_find(aParameters,
"muse.muse_ampl.fbeam");
534 cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
535 aParams->
fbeam = cpl_parameter_get_double(p);
537 p = cpl_parameterlist_find(aParameters,
"muse.muse_ampl.temp");
538 cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
539 aParams->
temp = cpl_parameter_get_double(p);
541 p = cpl_parameterlist_find(aParameters,
"muse.muse_ampl.savemaster");
542 cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
543 aParams->
savemaster = cpl_parameter_get_bool(p);
545 p = cpl_parameterlist_find(aParameters,
"muse.muse_ampl.savetable");
546 cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
547 aParams->
savetable = cpl_parameter_get_bool(p);
561 muse_ampl_exec(cpl_plugin *aPlugin)
563 if (cpl_plugin_get_type(aPlugin) != CPL_PLUGIN_TYPE_RECIPE) {
566 cpl_recipe *recipe = (cpl_recipe *)aPlugin;
567 cpl_msg_set_threadid_on();
569 cpl_frameset *usedframes = cpl_frameset_new(),
570 *outframes = cpl_frameset_new();
572 muse_ampl_params_fill(¶ms, recipe->parameters);
574 cpl_errorstate prestate = cpl_errorstate_get();
576 if (params.
nifu < -1 || params.
nifu > kMuseNumIFUs) {
577 cpl_msg_error(__func__,
"Please specify a valid IFU number (between 1 and "
578 "%d), 0 (to process all IFUs consecutively), or -1 (to "
579 "process all IFUs in parallel) using --nifu.", kMuseNumIFUs);
584 if (params.
nifu > 0) {
587 rc = muse_ampl_compute(proc, ¶ms);
588 cpl_frameset_join(usedframes, proc->
usedFrames);
591 }
else if (params.
nifu < 0) {
592 int *rcs = cpl_calloc(kMuseNumIFUs,
sizeof(
int));
594 #pragma omp parallel for default(none) \
595 shared(outframes, params, rcs, recipe, usedframes)
596 for (nifu = 1; nifu <= kMuseNumIFUs; nifu++) {
602 int *rci = rcs + (nifu - 1);
603 *rci = muse_ampl_compute(proc, pars);
604 if (rci && cpl_error_get_code() == MUSE_ERROR_CHIP_NOT_LIVE) {
608 #pragma omp critical(muse_processing_used_frames)
609 cpl_frameset_join(usedframes, proc->
usedFrames);
610 #pragma omp critical(muse_processing_output_frames)
616 for (nifu = 1; nifu <= kMuseNumIFUs; nifu++) {
617 if (rcs[nifu-1] != 0) {
623 for (params.
nifu = 1; params.
nifu <= kMuseNumIFUs && !rc; params.
nifu++) {
626 rc = muse_ampl_compute(proc, ¶ms);
627 if (rc && cpl_error_get_code() == MUSE_ERROR_CHIP_NOT_LIVE) {
630 cpl_frameset_join(usedframes, proc->
usedFrames);
636 if (!cpl_errorstate_is_equal(prestate)) {
640 cpl_msg_set_level(CPL_MSG_INFO);
650 cpl_frameset_join(recipe->frames, usedframes);
651 cpl_frameset_join(recipe->frames, outframes);
652 cpl_frameset_delete(usedframes);
653 cpl_frameset_delete(outframes);
666 muse_ampl_destroy(cpl_plugin *aPlugin)
670 if (cpl_plugin_get_type(aPlugin) == CPL_PLUGIN_TYPE_RECIPE) {
671 recipe = (cpl_recipe *)aPlugin;
677 cpl_parameterlist_delete(recipe->parameters);
694 cpl_plugin_get_info(cpl_pluginlist *aList)
696 cpl_recipe *recipe = cpl_calloc(1,
sizeof *recipe);
697 cpl_plugin *plugin = &recipe->interface;
701 helptext = cpl_sprintf(
"%s%s", muse_ampl_help,
702 muse_ampl_help_esorex);
704 helptext = cpl_sprintf(
"%s", muse_ampl_help);
708 cpl_plugin_init(plugin, CPL_PLUGIN_API, MUSE_BINARY_VERSION,
709 CPL_PLUGIN_TYPE_RECIPE,
711 "Determine the instrumental throughput from exposures taken with the pico-amplifier / photo diode readings.",
719 cpl_pluginlist_append(aList, plugin);
void muse_processing_delete(muse_processing *aProcessing)
Free the muse_processing structure.
int nifu
IFU to handle. If set to 0, all IFUs are processed serially. If set to -1, all IFUs are processed in ...
Structure to hold the parameters of the muse_ampl recipe.
muse_cplframework_type muse_cplframework(void)
Return the CPL framework the recipe is run under.
int nhigh
Number of maximum pixels to reject with minmax.
double ovscsigma
If the deviation of mean overscan levels between a raw input image and the reference image is higher ...
double temp
Lamp temperature [K] used to create the black body function.
const char * muse_get_license(void)
Get the pipeline copyright and license.
muse_processing * muse_processing_new(const char *aRecipeName, cpl_recipe *aRecipe)
Create a new processing structure.
int nlow
Number of minimum pixels to reject with minmax.
int combine
Type of combination to use.
int ovscignore
The number of pixels of the overscan adjacent to the data region of the CCD that are ignored when com...
const char * overscan
If this is "none", stop when detecting discrepant overscan levels (see ovscsigma), for "offset" it assumes that the mean overscan level represents the real offset in the bias levels of the exposures involved, and adjusts the data accordingly; for "vpoly", a polynomial is fit to the vertical overscan and subtracted from the whole quadrant.
int savetable
Save the table with all the processed pixel values.
int nkeep
Number of pixels to keep with minmax.
cpl_frameset * outputFrames
void muse_cplerrorstate_dump_some(unsigned aCurrent, unsigned aFirst, unsigned aLast)
Dump some CPL errors.
void muse_processinginfo_delete(cpl_recipe *)
Clear all information from the processing info and from the recipe config.
double hsigma
High sigma for pixel rejection with sigclip.
const char * ovscreject
This influences how values are rejected when computing overscan statistics. Either no rejection at al...
double fbeam
Factor to describe the widening of the beam from the focal plane to photo diode 2.
const char * combine_s
Type of combination to use (as string)
cpl_error_code muse_cplframeset_erase_duplicate(cpl_frameset *aFrames)
Erase all duplicate frames from a frameset.
cpl_error_code muse_cplframeset_erase_all(cpl_frameset *aFrames)
Erase all frames in a frameset.
int savemaster
Save the processed and combined master image before any concolution is done.
void muse_processinginfo_register(cpl_recipe *, cpl_recipeconfig *, muse_processing_prepare_header_func *, muse_processing_get_frame_level_func *, muse_processing_get_frame_mode_func *)
Register extended functionalities for MUSE recipes.
double lsigma
Low sigma for pixel rejection with sigclip.
cpl_frameset * usedFrames
cpl_error_code muse_processing_prepare_property(cpl_propertylist *, const char *, cpl_type, const char *)
Prepare and check the specified property.