MUSE Pipeline Reference Manual  2.5.2
muse_raman.c
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set sw=2 sts=2 et cin: */
3 /*
4  * This file is part of the MUSE Instrument Pipeline
5  * Copyright (C) 2017-2018 European Southern Observatory
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21 
22 #include <string.h>
23 
24 #include "muse_raman.h"
25 #include <muse.h>
26 
27 /*----------------------------------------------------------------------------*/
35 /*----------------------------------------------------------------------------*/
36 
39 /*----------------------------------------------------------------------------*/
46 /*----------------------------------------------------------------------------*/
47 cpl_table *
49 {
50  cpl_ensure(aProcessing, CPL_ERROR_NULL_INPUT, NULL);
51  cpl_frameset *frames = muse_frameset_find(aProcessing->inframes,
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);
55  if (!frame) {
56  cpl_frameset_delete(frames);
57  cpl_errorstate_set(es); /* swallow the illegal input state */
58  cpl_msg_debug(__func__, "No raman lines found in input frameset!");
59  return NULL;
60  }
61  const char *fn = cpl_frame_get_filename(frame);
62  cpl_table *lines = muse_cpltable_load(fn, "LINES", muse_sky_lines_lines_def);
63  if (!lines) {
64  cpl_msg_warning(__func__, "Could not load raman lines from \"%s\"", fn);
65  cpl_frameset_delete(frames);
66  return NULL;
67  }
68 
69  /* Output message, no matter if unit conversions happen *
70  * (so that the following messages can be related to a file name) */
71  cpl_msg_info(__func__, "Loaded raman lines from \"%s\"", fn);
72  /* Convert tables with unscaled flux into ones with scaled flux. */
73  if (lines) {
74  cpl_msg_indent_more();
75  const char *unit = cpl_table_get_column_unit(lines, "flux");
76  if (!unit) {
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);
85  }
86  cpl_msg_indent_less();
87  } /* if lines */
88 
89  muse_processing_append_used(aProcessing, frame, CPL_FRAME_GROUP_CALIB, 1);
90  cpl_frameset_delete(frames);
91 
92  return lines;
93 } /* muse_raman_lines_load() */
94 
95 /*----------------------------------------------------------------------------*/
109 /*----------------------------------------------------------------------------*/
110 cpl_error_code
111 muse_raman_add_lsf(muse_pixtable *aPixtable, cpl_table *aRamanLines,
112  int aGroup, double aFlux, muse_lsf_cube **aLsfCube)
113 {
114  muse_pixtable **slice_pixtable = muse_pixtable_extracted_get_slices(aPixtable);
115 
116  cpl_size n_slices = muse_pixtable_extracted_get_size(slice_pixtable);
117  cpl_size i_slice;
118  cpl_error_code rc[n_slices];
119  #pragma omp parallel for default(none) \
120  shared(aLsfCube, aRamanLines, aGroup, aFlux, n_slices, \
121  slice_pixtable, rc)
122  for (i_slice = 0; i_slice < n_slices; i_slice++) {
123  muse_pixtable *slice_pt = slice_pixtable[i_slice];
124  uint32_t origin = cpl_table_get_int(slice_pt->table, MUSE_PIXTABLE_ORIGIN,
125  0, NULL);
126  int ifu = muse_pixtable_origin_get_ifu(origin);
127  int slice = muse_pixtable_origin_get_slice(origin);
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;
132  continue;
133  }
134 
135  cpl_image *lsfImage = cpl_imagelist_get(aLsfCube[ifu-1]->img, slice-1);
136  int i_line;
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) {
141  continue;
142  }
143  double wavelength = cpl_table_get(aRamanLines, "lambda", i_line, NULL);
144  double flux = cpl_table_get(aRamanLines, "flux", i_line, NULL) * aFlux;
145  cpl_error_code res = muse_lsf_apply_slice(slice_pt, wavelength, flux,
146  lsfImage, aLsfCube[ifu-1]->wcs);
147  if (res != CPL_ERROR_NONE) {
148  rc[i_slice] = res;
149  break;
150  }
151  }
152  }
153  muse_pixtable_extracted_delete(slice_pixtable);
154 
155  for (i_slice = 0; i_slice < n_slices; i_slice++) {
156  if (rc[i_slice] != CPL_ERROR_NONE) {
157  return rc[i_slice];
158  }
159  }
160  return CPL_ERROR_NONE;
161 } /* muse_raman_add_lsf() */
162 
163 /*----------------------------------------------------------------------------*/
187 /*----------------------------------------------------------------------------*/
188 cpl_array *
189 muse_raman_simulate(muse_pixtable *aPixtable, cpl_array *aPar,
190  cpl_array *aRetval)
191 {
192  cpl_array *xpos = muse_cpltable_extract_column(aPixtable->table,
194  cpl_array *ypos = muse_cpltable_extract_column(aPixtable->table,
196  cpl_array *lsf = muse_cpltable_extract_column(aPixtable->table, "lsf");
197 
198  /* Apply parameterization:
199  p[0]*xpos^2 + p[1]*xpos*ypos + p[2]*ypos^2 + p[3]*xpos + p[4]*ypos + 1
200  */
201  if (!aRetval) {
202  aRetval = cpl_array_new(cpl_array_get_size(lsf), CPL_TYPE_DOUBLE);
203  }
204  cpl_array_fill_window(aRetval, 0, cpl_array_get_size(aRetval), 0);
205  cpl_array *tmp;
206 
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);
212 
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);
218 
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);
224 
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);
229 
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);
234 
235  cpl_array_add_scalar(aRetval, 1);
236 
237  /* multiply with LSF */
238  cpl_array_multiply(aRetval, lsf);
239 
240  cpl_array_unwrap(xpos);
241  cpl_array_unwrap(ypos);
242  cpl_array_unwrap(lsf);
243 
244  return aRetval;
245 } /* muse_raman_simulate() */
246 
247 /*----------------------------------------------------------------------------*/
263 /*----------------------------------------------------------------------------*/
264 muse_image *
265 muse_raman_simulate_image(const muse_image *aImage, const cpl_array *aPar)
266 {
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);
270 
271  /* get the WCS */
272  muse_wcs *wcs = muse_wcs_new(aImage->header);
273  if (!wcs) {
274  /* not a critical error for the actual processing, so just warn */
275  cpl_msg_warning(__func__, "No Raman image simulated: %s",
276  cpl_error_get_message());
277  /* but still set the error code */
278  cpl_error_set(__func__, cpl_error_get_code());
279  return NULL;
280  }
281  /* create output image, just header plus data are enough here */
282  muse_image *image = muse_image_new();
283  image->header = cpl_propertylist_duplicate(aImage->header);
284  image->data = cpl_image_duplicate(aImage->data);
285  /* no bad pixels in the image we create here (they get intra/extrapolated) */
286  cpl_image_accept_all(image->data);
287 
288  /* get buffers for parmeters and image */
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++) {
294  int j;
295  for (j = 0; j < ny; j++) {
296  /* get location in WCS coordinates */
297  double x, y;
298  muse_wcs_projplane_from_pixel_fast(wcs, i+1, j+1, &x, &y);
299 
300  /* evaluate the polynomial */
301  data[i + j*nx] = p[0] * x*x + p[1] * x*y + p[2] * y*y + p[3] * x
302  + p[4] * y + 1.;
303  } /* for j */
304  } /* for i */
305  cpl_free(wcs);
306 
307  return image;
308 } /* muse_raman_simulate_image() */
309 
310 typedef struct {
311  muse_pixtable *pt[MUSE_RAMAN_RANGES];
312 } muse_raman_fit_struct;
313 
314 /*----------------------------------------------------------------------------*/
323 /*----------------------------------------------------------------------------*/
324 static cpl_error_code
325 muse_raman_eval(void *aData, cpl_array *aPar, cpl_array* aRetval)
326 {
327  muse_raman_fit_struct *fitData = aData;
328  int i;
329  cpl_size i_row = 0;
330  for (i = 0; i < MUSE_RAMAN_RANGES; i++) {
331  if (!fitData->pt[i]) {
332  continue;
333  }
334  cpl_array *a =
335  cpl_array_wrap_double(cpl_array_get_data_double(aRetval) + i_row,
336  cpl_table_get_nrow(fitData->pt[i]->table));
337 
338  muse_raman_simulate(fitData->pt[i], aPar, a);
339  cpl_array_multiply_scalar(a, cpl_array_get(aPar, 5 + i, NULL));
340  cpl_array *data = muse_cpltable_extract_column(fitData->pt[i]->table,
342  cpl_array_subtract(a, data);
343  cpl_array_unwrap(data);
344  cpl_array_subtract_scalar(a, cpl_array_get_mean(a));
345  cpl_array_unwrap(a);
346  i_row += cpl_table_get_nrow(fitData->pt[i]->table);
347  } /* for i (both Raman ranges) */
348 
349  return CPL_ERROR_NONE;
350 } /* muse_raman_eval() */
351 
352 /*----------------------------------------------------------------------------*/
375 /*----------------------------------------------------------------------------*/
376 cpl_array *
377 muse_raman_fit(muse_pixtable *aPixtable[MUSE_RAMAN_RANGES])
378 {
379  muse_raman_fit_struct fitData;
380  int i;
381  for (i = 0; i < MUSE_RAMAN_RANGES; i++) {
382  fitData.pt[i] = aPixtable[i];
383  }
384 
385  cpl_array *param = cpl_array_new(5 + MUSE_RAMAN_RANGES, CPL_TYPE_DOUBLE);
386  /* First quess. See muse_raman_polynomial() */
387  cpl_array_set(param, 0, 0.); /* x² coefficient */
388  cpl_array_set(param, 1, 0.); /* xy coefficient */
389  cpl_array_set(param, 2, 0.); /* y² coefficient */
390  cpl_array_set(param, 3, 0.); /* x coefficient */
391  cpl_array_set(param, 4, 0.); /* y coefficient */
392 
393  for (i = 0; i < MUSE_RAMAN_RANGES; i++) {
394  cpl_array_set(param, 5 + i, 0.); /* constant part i-th range*/
395  }
396 
397  /* do the fit, ignoring possible errors */
398  int debug = getenv("MUSE_DEBUG_LSF_FIT")
399  && atoi(getenv("MUSE_DEBUG_LSF_FIT")) > 0;
401  -1, -1, -1, -1, /* default ftol, xtol, gtol, maxiter */
402  debug
403  };
404 
405  cpl_size n_row = 0;
406  for (i = 0; i < MUSE_RAMAN_RANGES; i++) {
407  if (!aPixtable[i]) {
408  continue;
409  }
410  n_row += cpl_table_get_nrow(aPixtable[i]->table);
411  } /* for i (both Raman ranges) */
412  cpl_msg_info(__func__, "Starting Raman line fit");
413  cpl_error_code r = muse_cpl_optimize_lvmq(&fitData, param, n_row,
414  muse_raman_eval, &ctrl);
415  if (r != CPL_ERROR_NONE) {
416  cpl_msg_warning(__func__, "Raman line fit failed: %s",
417  cpl_error_get_message());
418  }
419 
420  return param;
421 } /* muse_raman_fit() */
422 
#define MUSE_PIXTABLE_XPOS
Definition: muse_pixtable.h:51
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.
Definition: muse_raman.c:189
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.
Definition: muse_wcs.h:105
muse_pixtable ** muse_pixtable_extracted_get_slices(muse_pixtable *aPixtable)
Extract one pixel table per IFU and slice.
cpl_image * data
the data extension
Definition: muse_image.h:46
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.
Definition: muse_raman.c:48
const muse_cpltable_def muse_sky_lines_lines_def[]
Data cube/stacked image list containing the LSF for one IFU.
Definition: muse_lsf.h:49
Structure definition of MUSE three extension FITS file.
Definition: muse_image.h:40
cpl_table * table
The pixel table.
cpl_propertylist * header
the FITS header
Definition: muse_image.h:72
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
Definition: muse_pixtable.h:48
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.
Definition: muse_raman.c:377
muse_wcs * muse_wcs_new(cpl_propertylist *aHeader)
Create a new WCS structure from a given FITS header.
Definition: muse_wcs.c:1897
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.
Definition: muse_raman.c:325
cpl_array * muse_cpltable_extract_column(cpl_table *aTable, const char *aColumn)
Create an array from a section of a column.
Optimization control parameters.
Definition: muse_optimize.h:32
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
Definition: muse_pixtable.h:54
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.
Definition: muse_raman.c:111
cpl_frameset * inframes
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.
Definition: muse_wcs.h:217
muse_image * muse_image_new(void)
Allocate memory for a new muse_image object.
Definition: muse_image.c:66
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.
Definition: muse_raman.c:265
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
Definition: muse_utils.c:160
#define MUSE_PIXTABLE_YPOS
Definition: muse_pixtable.h:52