24 #include "muse_raman.h" 50 cpl_ensure(aProcessing, CPL_ERROR_NULL_INPUT, NULL);
52 MUSE_TAG_RAMAN_LINES, 0, CPL_FALSE);
53 cpl_errorstate es = cpl_errorstate_get();
54 cpl_frame *frame = cpl_frameset_get_position(frames, 0);
56 cpl_frameset_delete(frames);
57 cpl_errorstate_set(es);
58 cpl_msg_debug(__func__,
"No raman lines found in input frameset!");
61 const char *fn = cpl_frame_get_filename(frame);
64 cpl_msg_warning(__func__,
"Could not load raman lines from \"%s\"", fn);
65 cpl_frameset_delete(frames);
71 cpl_msg_info(__func__,
"Loaded raman lines from \"%s\"", fn);
74 cpl_msg_indent_more();
75 const char *unit = cpl_table_get_column_unit(lines,
"flux");
77 cpl_msg_warning(__func__,
"No flux unit given!");
78 }
else if (strcmp(unit,
"erg/(s cm^2 arcsec^2)") == 0) {
79 cpl_msg_info(__func__,
"Scaling flux by 1e20.");
80 cpl_table_multiply_scalar(lines,
"flux", 1e20);
81 cpl_table_set_column_unit(lines,
"flux",
82 "10**(-20)*erg/(s cm^2 arcsec^2)");
83 }
else if (strcmp(unit,
"10**(-20)*erg/(s cm^2 arcsec^2)") != 0) {
84 cpl_msg_warning(__func__,
"Unsupported flux unit \"%s\".", unit);
86 cpl_msg_indent_less();
90 cpl_frameset_delete(frames);
118 cpl_error_code rc[n_slices];
119 #pragma omp parallel for default(none) \ 120 shared(aLsfCube, aRamanLines, aGroup, aFlux, n_slices, \ 122 for (i_slice = 0; i_slice < n_slices; i_slice++) {
128 if (!aLsfCube[ifu-1]) {
129 cpl_msg_warning(__func__,
"No LSF params for slice #%i.%i. Ignoring in " 130 "Raman simulation.", ifu, slice);
131 rc[i_slice] = CPL_ERROR_NONE;
135 cpl_image *lsfImage = cpl_imagelist_get(aLsfCube[ifu-1]->img, slice-1);
137 int n_lines = cpl_table_get_nrow(aRamanLines);
138 rc[i_slice] = CPL_ERROR_NONE;
139 for (i_line = 0; i_line < n_lines; i_line++) {
140 if (cpl_table_get(aRamanLines,
"group", i_line, NULL) != aGroup) {
143 double wavelength = cpl_table_get(aRamanLines,
"lambda", i_line, NULL);
144 double flux = cpl_table_get(aRamanLines,
"flux", i_line, NULL) * aFlux;
146 lsfImage, aLsfCube[ifu-1]->wcs);
147 if (res != CPL_ERROR_NONE) {
155 for (i_slice = 0; i_slice < n_slices; i_slice++) {
156 if (rc[i_slice] != CPL_ERROR_NONE) {
160 return CPL_ERROR_NONE;
202 aRetval = cpl_array_new(cpl_array_get_size(lsf), CPL_TYPE_DOUBLE);
204 cpl_array_fill_window(aRetval, 0, cpl_array_get_size(aRetval), 0);
207 tmp = cpl_array_duplicate(xpos);
208 cpl_array_multiply(tmp, xpos);
209 cpl_array_multiply_scalar(tmp, cpl_array_get(aPar, 0, NULL));
210 cpl_array_add(aRetval, tmp);
211 cpl_array_delete(tmp);
213 tmp = cpl_array_duplicate(xpos);
214 cpl_array_multiply(tmp, ypos);
215 cpl_array_multiply_scalar(tmp, cpl_array_get(aPar, 1, NULL));
216 cpl_array_add(aRetval, tmp);
217 cpl_array_delete(tmp);
219 tmp = cpl_array_duplicate(ypos);
220 cpl_array_multiply(tmp, ypos);
221 cpl_array_multiply_scalar(tmp, cpl_array_get(aPar, 2, NULL));
222 cpl_array_add(aRetval, tmp);
223 cpl_array_delete(tmp);
225 tmp = cpl_array_duplicate(xpos);
226 cpl_array_multiply_scalar(tmp, cpl_array_get(aPar, 3, NULL));
227 cpl_array_add(aRetval, tmp);
228 cpl_array_delete(tmp);
230 tmp = cpl_array_duplicate(ypos);
231 cpl_array_multiply_scalar(tmp, cpl_array_get(aPar, 4, NULL));
232 cpl_array_add(aRetval, tmp);
233 cpl_array_delete(tmp);
235 cpl_array_add_scalar(aRetval, 1);
238 cpl_array_multiply(aRetval, lsf);
240 cpl_array_unwrap(xpos);
241 cpl_array_unwrap(ypos);
242 cpl_array_unwrap(lsf);
267 cpl_ensure(aImage && aPar, CPL_ERROR_NULL_INPUT, NULL);
268 cpl_ensure(cpl_array_get_size(aPar) == 5 + MUSE_RAMAN_RANGES,
269 CPL_ERROR_ILLEGAL_INPUT, NULL);
275 cpl_msg_warning(__func__,
"No Raman image simulated: %s",
276 cpl_error_get_message());
278 cpl_error_set(__func__, cpl_error_get_code());
283 image->
header = cpl_propertylist_duplicate(aImage->
header);
284 image->
data = cpl_image_duplicate(aImage->
data);
286 cpl_image_accept_all(image->
data);
289 const double *p = cpl_array_get_data_double_const(aPar);
290 float *data = cpl_image_get_data_float(image->
data);
291 int i, nx = cpl_image_get_size_x(image->
data),
292 ny = cpl_image_get_size_y(image->
data);
293 for (i = 0; i < nx; i++) {
295 for (j = 0; j < ny; j++) {
301 data[i + j*nx] = p[0] * x*x + p[1] * x*y + p[2] * y*y + p[3] * x
312 } muse_raman_fit_struct;
324 static cpl_error_code
327 muse_raman_fit_struct *fitData = aData;
330 for (i = 0; i < MUSE_RAMAN_RANGES; i++) {
331 if (!fitData->pt[i]) {
335 cpl_array_wrap_double(cpl_array_get_data_double(aRetval) + i_row,
336 cpl_table_get_nrow(fitData->pt[i]->table));
339 cpl_array_multiply_scalar(a, cpl_array_get(aPar, 5 + i, NULL));
342 cpl_array_subtract(a, data);
343 cpl_array_unwrap(data);
344 cpl_array_subtract_scalar(a, cpl_array_get_mean(a));
346 i_row += cpl_table_get_nrow(fitData->pt[i]->table);
349 return CPL_ERROR_NONE;
379 muse_raman_fit_struct fitData;
381 for (i = 0; i < MUSE_RAMAN_RANGES; i++) {
382 fitData.pt[i] = aPixtable[i];
385 cpl_array *param = cpl_array_new(5 + MUSE_RAMAN_RANGES, CPL_TYPE_DOUBLE);
387 cpl_array_set(param, 0, 0.);
388 cpl_array_set(param, 1, 0.);
389 cpl_array_set(param, 2, 0.);
390 cpl_array_set(param, 3, 0.);
391 cpl_array_set(param, 4, 0.);
393 for (i = 0; i < MUSE_RAMAN_RANGES; i++) {
394 cpl_array_set(param, 5 + i, 0.);
398 int debug = getenv(
"MUSE_DEBUG_LSF_FIT")
399 && atoi(getenv(
"MUSE_DEBUG_LSF_FIT")) > 0;
406 for (i = 0; i < MUSE_RAMAN_RANGES; i++) {
410 n_row += cpl_table_get_nrow(aPixtable[i]->table);
412 cpl_msg_info(__func__,
"Starting Raman line fit");
415 if (r != CPL_ERROR_NONE) {
416 cpl_msg_warning(__func__,
"Raman line fit failed: %s",
417 cpl_error_get_message());
#define MUSE_PIXTABLE_XPOS
void muse_pixtable_extracted_delete(muse_pixtable **aPixtables)
Delete a pixel table array.
unsigned short muse_pixtable_origin_get_slice(uint32_t aOrigin)
Get the slice number from the encoded 32bit origin number.
cpl_array * muse_raman_simulate(muse_pixtable *aPixtable, cpl_array *aPar, cpl_array *aRetval)
Apply the LSF and spatial Raman light distribution.
cpl_size muse_pixtable_extracted_get_size(muse_pixtable **aPixtables)
Get the size of an array of extracted pixel tables.
A structure containing a spatial two-axis WCS.
muse_pixtable ** muse_pixtable_extracted_get_slices(muse_pixtable *aPixtable)
Extract one pixel table per IFU and slice.
cpl_image * data
the data extension
cpl_error_code muse_cpl_optimize_lvmq(void *aData, cpl_array *aPar, int aSize, muse_cpl_evaluate_func *aFunction, muse_cpl_optimize_control_t *aCtrl)
Minimize a function with the Levenberg-Marquardt algorithm.
cpl_table * muse_raman_lines_load(muse_processing *aProcessing)
Load the raman lines file.
Data cube/stacked image list containing the LSF for one IFU.
Structure definition of MUSE three extension FITS file.
cpl_table * table
The pixel table.
cpl_propertylist * header
the FITS header
cpl_table * muse_cpltable_load(const char *aFile, const char *aExtension, const muse_cpltable_def aDefinition[])
Load a table from disk (and check against definition).
#define MUSE_PIXTABLE_DATA
cpl_error_code muse_lsf_apply_slice(muse_pixtable *aPixtable, double aWavelength, double aFlux, cpl_image *aLsfImage, muse_wcs *aLsfWCS)
Add line spread function to a MUSE slice of a pixtable.
cpl_array * muse_raman_fit(muse_pixtable *aPixtable[MUSE_RAMAN_RANGES])
Fit raman lines to an array of pixtables.
muse_wcs * muse_wcs_new(cpl_propertylist *aHeader)
Create a new WCS structure from a given FITS header.
Structure definition of MUSE pixel table.
static cpl_error_code muse_raman_eval(void *aData, cpl_array *aPar, cpl_array *aRetval)
Evaluate a given parameter set for the Raman fit.
cpl_array * muse_cpltable_extract_column(cpl_table *aTable, const char *aColumn)
Create an array from a section of a column.
Optimization control parameters.
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.
#define MUSE_PIXTABLE_ORIGIN
unsigned short muse_pixtable_origin_get_ifu(uint32_t aOrigin)
Get the IFU number from the encoded 32bit origin number.
cpl_error_code muse_raman_add_lsf(muse_pixtable *aPixtable, cpl_table *aRamanLines, int aGroup, double aFlux, muse_lsf_cube **aLsfCube)
Add a Raman line profile to pixtable.
static void muse_wcs_projplane_from_pixel_fast(muse_wcs *aWCS, double aX, double aY, double *aXOut, double *aYOut)
Convert from pixel coordinates to projection plane coordinates.
muse_image * muse_image_new(void)
Allocate memory for a new muse_image object.
muse_image * muse_raman_simulate_image(const muse_image *aImage, const cpl_array *aPar)
Simulate an image of the parameters of the spatial Raman light fit.
cpl_frameset * muse_frameset_find(const cpl_frameset *aFrames, const char *aTag, unsigned char aIFU, cpl_boolean aInvert)
return frameset containing data from an IFU/channel with a certain tag
#define MUSE_PIXTABLE_YPOS