33 #include "muse_cplwrappers.h"
34 #include "muse_data_format_z.h"
35 #include "muse_quality.h"
36 #include "muse_mask.h"
37 #include "muse_pfits.h"
56 {
"lambda", CPL_TYPE_DOUBLE,
"Angstrom",
"%7.2f",
"wavelength", CPL_TRUE},
57 {
"flux", CPL_TYPE_DOUBLE,
"erg/(s cm^2 arcsec^2)",
"%e",
"Flux", CPL_TRUE},
58 { NULL, 0, NULL, NULL, NULL, CPL_FALSE }
71 unsigned count = cpl_imagelist_get_size(aCube->
data);
73 double crval3 = cpl_propertylist_get_float(aCube->
header,
"CRVAL3");
74 double crpix3 = cpl_propertylist_get_float(aCube->
header,
"CRPIX3");
75 double cdelt3 = cpl_propertylist_get_float(aCube->
header,
"CD3_3");
77 cpl_mask *nmask = cpl_mask_duplicate(aMask);
79 for (i = 0; i < count; i++) {
80 cpl_table_set(spectrum,
"lambda", i, crval3 + (i + 1 - crpix3) * cdelt3);
82 cpl_image *img = cpl_imagelist_get(aCube->
data, i);
83 cpl_mask *bpm = cpl_image_get_bpm(img);
84 cpl_mask_or(bpm, nmask);
85 if (aCube->
dq != NULL) {
86 cpl_image *dq = cpl_imagelist_get(aCube->
dq, i);
87 cpl_mask *dq_mask = cpl_mask_threshold_image_create(dq, -0.5, 0.5);
88 cpl_mask_not(dq_mask);
89 cpl_mask_or(bpm, dq_mask);
90 cpl_mask_delete(dq_mask);
92 double dev = cpl_image_get_stdev(img);
93 double mean = cpl_image_get_mean(img);
94 cpl_table_set(spectrum,
"data", i, mean);
95 cpl_table_set(spectrum,
"stat", i, dev / sqrt(cpl_mask_count(bpm)));
96 cpl_table_set(spectrum,
"dq", i, (cpl_mask_count(bpm) < 3));
98 cpl_mask_delete(nmask);
120 const char *aQCPrefix)
122 cpl_ensure(aImage, CPL_ERROR_NULL_INPUT, NULL);
126 double t0 = cpl_image_get_min(aImage->
data);
128 cpl_msg_info(__func__,
"Creating sky mask for pixels between minimum (%g) and"
129 " threshold (%g)", t0, t1);
133 selectedRegions->
mask = cpl_mask_threshold_image_create(aImage->
data, t0, t1);
134 cpl_mask_not(selectedRegions->
mask);
135 cpl_mask_or(selectedRegions->
mask, cpl_image_get_bpm(aImage->
data));
136 cpl_mask_not(selectedRegions->
mask);
139 selectedRegions->
header = cpl_propertylist_duplicate(aImage->
header);
140 char keyword[KEYWORD_LENGTH];
141 snprintf(keyword, KEYWORD_LENGTH,
"%s THRESHOLD", aQCPrefix);
142 cpl_propertylist_append_double(selectedRegions->
header, keyword, t1);
144 return selectedRegions;
171 cpl_ensure(aLambda, CPL_ERROR_NULL_INPUT, NULL);
172 cpl_ensure(aLsf, CPL_ERROR_NULL_INPUT, NULL);
180 cpl_array_unwrap(linesLambda);
181 cpl_array_unwrap(linesFlux);
183 cpl_array *sensitivity = cpl_array_duplicate(aLambda);
184 cpl_array_subtract_scalar(sensitivity, aLsf->
lambda_ref);
187 cpl_array_multiply(spectrum, sensitivity);
189 cpl_array_delete(sensitivity);
209 float *lbda = cpl_table_get_data_float(aPixtable->
table, MUSE_PIXTABLE_LAMBDA);
210 float *data = cpl_table_get_data_float(aPixtable->
table, MUSE_PIXTABLE_DATA);
211 int *dq = cpl_table_get_data_int(aPixtable->
table, MUSE_PIXTABLE_DQ);
213 cpl_size spec_nrows = cpl_table_get_nrow(aSpectrum);
214 double *spec_data = cpl_table_get_data_double(aSpectrum,
"data");
215 double *spec_stat = cpl_table_get_data_double(aSpectrum,
"stat");
216 double *spec_std = cpl_malloc(spec_nrows *
sizeof(
double));
218 for (i_spec = 0; i_spec < spec_nrows; i_spec++) {
219 spec_std[i_spec] = sqrt(spec_stat[i_spec]);
223 cpl_array *asel = cpl_table_where_selected(aPixtable->
table);
224 const cpl_size *sel = cpl_array_get_data_cplsize_const(asel);
225 cpl_size isel, nsel = cpl_array_get_size(asel);
226 cpl_size n_cosmic = 0;
227 for (isel = 0; isel < nsel; isel++) {
228 cpl_size i_row = sel[isel];
229 if ((dq[i_row] != EURO3D_GOODPIXEL)) {
232 double l = lbda[i_row];
234 if ((i_spec < spec_nrows - 1) && (spec_data[i_spec] < spec_data[i_spec+1])) {
237 if (spec_data[i_spec] + 5 * spec_std[i_spec] < data[i_row]) {
238 dq[i_row] = EURO3D_COSMICRAY;
242 cpl_msg_info(__func__,
"Marked %"CPL_SIZE_FORMAT
" pixels of %"CPL_SIZE_FORMAT
243 " as cosmic", n_cosmic, nsel);
245 cpl_array_delete(asel);
265 const cpl_table *aContinuum, cpl_propertylist *aHeader)
267 cpl_ensure_code(aProcessing && aContinuum && aHeader, CPL_ERROR_NULL_INPUT);
270 CPL_FRAME_TYPE_TABLE);
271 cpl_ensure_code(frame, CPL_ERROR_ILLEGAL_INPUT);
272 const char *filename = cpl_frame_get_filename(frame);
273 cpl_error_code rc = cpl_propertylist_save(aHeader, filename, CPL_IO_CREATE);
275 "CONTINUUM", muse_fluxspectrum_def);
276 if (rc == CPL_ERROR_NONE) {
277 cpl_frameset_insert(aProcessing->
outframes, frame);
279 cpl_frame_delete(frame);
Structure definition of a MUSE datacube.
cpl_image * data
the data extension
cpl_table * muse_sky_spectrum_from_cube(muse_datacube *aCube, const cpl_mask *aMask)
Create a spectrum out of a cube by applying a mask.
muse_mask * muse_mask_new(void)
Allocate memory for a new muse object.
muse_mask * muse_sky_create_skymask(muse_image *aImage, double aFraction, const char *aQCPrefix)
Select spaxels to be considered as sky.
cpl_error_code muse_cplarray_poly1d(cpl_array *aArray, const cpl_array *aCoeff)
Apply a polynomial to an array.
Structure definition of MUSE three extension FITS file.
cpl_table * table
The pixel table.
cpl_propertylist * header
the FITS header
cpl_array * muse_lsf_spectrum_get_lines(const cpl_array *aLambda, const cpl_array *aLinesLambda, const cpl_array *aLinesFlux, const muse_lsf_params *aLsf)
Set the lines spectrum from the lines table and the detector LSF.
void muse_sky_mark_cosmic(cpl_table *aSpectrum, muse_pixtable *aPixtable)
Mark all pixel above a certain limit as COSMIC.
cpl_table * muse_cpltable_new(const muse_cpltable_def *aDef, cpl_size aLength)
Create an empty table according to the specified definition.
cpl_array * sensitivity
Relative detector sensitivity parametrization.
Structure definition of MUSE pixel table.
cpl_frame * muse_processing_new_frame(muse_processing *aProcessing, int aIFU, cpl_propertylist *aHeader, const char *aTag, cpl_frame_type aType)
Create a new frame for a result file.
cpl_array * muse_cpltable_extract_column(cpl_table *aTable, const char *aColumn)
Create an array from a section of a column.
double muse_cplimage_get_percentile(const cpl_image *aImage, double aFraction)
Get the percentile of an image.
cpl_imagelist * data
the cube containing the actual data values
const muse_cpltable_def muse_fluxspectrum_def[]
Definition of the flux spectrum table structure.
cpl_imagelist * dq
the optional cube containing the bad pixel status
cpl_size muse_cpltable_find_sorted(const cpl_table *aTable, const char *aColumn, double aValue)
Find a row in a table.
cpl_propertylist * header
the FITS header
double lambda_ref
Reference wavelength for polynomial parametrizations.
Handling of "mask" files.
cpl_error_code muse_image_reject_from_dq(muse_image *aImage)
Reject pixels of a muse_image depending on its DQ data.
cpl_propertylist * header
the FITS header
Definition of a cpl table structure.
cpl_array * muse_sky_apply_lsf(const cpl_array *aLambda, const cpl_table *aLines, const muse_lsf_params *aLsf)
Apply the LSF parameters to a spectrum.
cpl_mask * mask
The mask data.
Structure definition of detector (slice) parameters.
cpl_error_code muse_cpltable_append_file(const cpl_table *aTable, const char *aFile, const char *aExtension, const muse_cpltable_def aDefinition[])
Save a table to disk (into a FITS extension)
cpl_error_code muse_sky_save_continuum(muse_processing *aProcessing, const cpl_table *aContinuum, cpl_propertylist *aHeader)
Save sky continuum table to file.