MUSE Pipeline Reference Manual  2.5.2
muse_postproc.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) 2005-2017 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
20  * 02110-1301, USA.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 /*----------------------------------------------------------------------------*
28  * Includes *
29  *----------------------------------------------------------------------------*/
30 #include <string.h>
31 #include <strings.h>
32 
33 #include "muse_postproc.h"
34 #include "muse_instrument.h"
35 
36 #include "muse_astro.h"
37 #include "muse_dar.h"
38 #include "muse_idp.h"
39 #include "muse_imagelist.h"
40 #include "muse_pfits.h"
41 #include "muse_phys.h"
42 #include "muse_quality.h"
43 #include "muse_raman.h"
44 #ifdef USE_LSF_PARAMS
45 # include "muse_sky_old.h"
46 #endif
47 #include "muse_utils.h"
48 
49 #undef CREATE_IDP
50 
51 /*----------------------------------------------------------------------------*/
58 /*----------------------------------------------------------------------------*/
59 
62 /*----------------------------------------------------------------------------*/
74 /*----------------------------------------------------------------------------*/
77 {
78  muse_postproc_properties *prop = cpl_calloc(1, sizeof(muse_postproc_properties));
79  switch (aType) {
80  case MUSE_POSTPROC_SCIPOST:
81  case MUSE_POSTPROC_STANDARD:
82  case MUSE_POSTPROC_ASTROMETRY:
83  prop->type = aType;
84  break;
85  default:
86  cpl_msg_error(__func__, "No such setup known: %d", aType);
87  cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT);
88  cpl_free(prop);
89  return NULL;
90  }
91  /* rvtype of 0 is already MUSE_RVCORRECT_NONE */
92  return prop;
93 } /* muse_postproc_properties_new() */
94 
95 /*----------------------------------------------------------------------------*/
101 /*----------------------------------------------------------------------------*/
102 void
104 {
105  if (!aProp) {
106  return;
107  }
108  cpl_table_delete(aProp->exposures);
109  muse_table_delete(aProp->response);
110  muse_table_delete(aProp->telluric);
111  cpl_table_delete(aProp->extinction);
112  cpl_propertylist_delete(aProp->wcs);
114 #ifdef USE_LSF_PARAMS
116 #endif
117  cpl_table_delete(aProp->raman_lines);
118  cpl_table_delete(aProp->sky_lines);
119  cpl_table_delete(aProp->sky_continuum);
120  muse_mask_delete(aProp->sky_mask);
121  cpl_frame_delete(aProp->refframe);
122  cpl_table_delete(aProp->tellregions);
123  cpl_free(aProp);
124 } /* muse_postproc_properties_delete() */
125 
126 /*----------------------------------------------------------------------------*/
133 /*----------------------------------------------------------------------------*/
134 cpl_boolean
135 muse_postproc_check_save_param(const char *aSave, const char *aValid)
136 {
137  cpl_ensure(aSave, CPL_ERROR_NULL_INPUT, CPL_FALSE);
138  if (strlen(aSave) < 4) { /* less than "cube"! */
139  cpl_error_set_message(__func__, CPL_ERROR_DATA_NOT_FOUND,
140  "no (valid) save option given!");
141  return CPL_FALSE;
142  }
143  /* now compare all given output products against the allowed options */
144  cpl_boolean allvalid = CPL_TRUE;
145  cpl_array *asave = muse_cplarray_new_from_delimited_string(aSave, ","),
146  *avalid = muse_cplarray_new_from_delimited_string(aValid, ",");
147  int i, j,
148  ns = cpl_array_get_size(asave),
149  nv = cpl_array_get_size(avalid);
150  for (i = 0; i < ns; i++) {
151  cpl_boolean valid = CPL_FALSE;
152  for (j = 0; j < nv; j++) { /* go through all valid options */
153  if (!strcmp(cpl_array_get_string(asave, i),
154  cpl_array_get_string(avalid, j))) {
155  valid = CPL_TRUE;
156  }
157  } /* for j (all valid strings) */
158  if (!valid) { /* this string was apparently not valid! */
159  cpl_error_set_message(__func__, CPL_ERROR_ILLEGAL_INPUT,
160  "save option %d (%s) is not valid!", i + 1,
161  cpl_array_get_string(asave, i));
162  allvalid = CPL_FALSE;
163  }
164  } /* for i (all given strings) */
165  cpl_array_delete(asave);
166  cpl_array_delete(avalid);
167  return allvalid;
168 } /* muse_scipost_check_save_param() */
169 
170 /*----------------------------------------------------------------------------*/
185 /*----------------------------------------------------------------------------*/
187 muse_postproc_get_resampling_type(const char *aResampleString)
188 {
189  cpl_ensure(aResampleString, CPL_ERROR_NULL_INPUT, MUSE_RESAMPLE_WEIGHTED_DRIZZLE);
190  if (!strncmp(aResampleString, "nearest", 8)) {
191  return MUSE_RESAMPLE_NEAREST;
192  }
193  if (!strncmp(aResampleString, "linear", 7)) {
195  }
196  if (!strncmp(aResampleString, "quadratic", 10)) {
198  }
199  if (!strncmp(aResampleString, "renka", 6)) {
201  }
202  if (!strncmp(aResampleString, "drizzle", 8)) {
204  }
205  if (!strncmp(aResampleString, "lanczos", 8)) {
207  }
208  return MUSE_RESAMPLE_WEIGHTED_DRIZZLE; /* catch all fallback */
209 } /* muse_postproc_get_resampling_type() */
210 
211 /*----------------------------------------------------------------------------*/
226 /*----------------------------------------------------------------------------*/
228 muse_postproc_get_cr_type(const char *aCRTypeString)
229 {
230  cpl_ensure(aCRTypeString, CPL_ERROR_NULL_INPUT, MUSE_RESAMPLING_CRSTATS_IRAF);
231  if (!strncmp(aCRTypeString, "iraf", 5)) {
233  }
234  if (!strncmp(aCRTypeString, "mean", 5)) {
236  }
237  if (!strncmp(aCRTypeString, "median", 7)) {
239  }
240  return MUSE_RESAMPLING_CRSTATS_MEDIAN; /* catch all fallback */
241 } /* muse_postproc_get_cr_type() */
242 
243 /*----------------------------------------------------------------------------*/
257 /*----------------------------------------------------------------------------*/
259 muse_postproc_get_cube_format(const char *aFormatString)
260 {
261  cpl_ensure(aFormatString, CPL_ERROR_NULL_INPUT, MUSE_CUBE_TYPE_FITS);
262  if (!strncmp(aFormatString, "Cube", 5)) {
263  return MUSE_CUBE_TYPE_FITS;
264  }
265  if (!strncmp(aFormatString, "Euro3D", 7)) {
266  return MUSE_CUBE_TYPE_EURO3D;
267  }
268  if (!strncmp(aFormatString, "xCube", 6)) {
269  return MUSE_CUBE_TYPE_FITS_X;
270  }
271  if (!strncmp(aFormatString, "xEuro3D", 8)) {
273  }
274  if (!strncmp(aFormatString, "sdpCube", 8)) {
275  return MUSE_CUBE_TYPE_SDP;
276  }
277  return MUSE_CUBE_TYPE_FITS; /* catch all fallback */
278 } /* muse_postproc_get_cube_format() */
279 
280 /*----------------------------------------------------------------------------*/
294 /*----------------------------------------------------------------------------*/
296 muse_postproc_get_weight_type(const char *aWeightString)
297 {
298  cpl_ensure(aWeightString, CPL_ERROR_NULL_INPUT, MUSE_XCOMBINE_EXPTIME);
299  if (!strncmp(aWeightString, "exptime", 8)) {
300  return MUSE_XCOMBINE_EXPTIME;
301  }
302  if (!strncmp(aWeightString, "fwhm", 5)) {
303  return MUSE_XCOMBINE_FWHM;
304  }
305  if (!strncmp(aWeightString, "none", 5)) {
306  return MUSE_XCOMBINE_NONE;
307  }
308  return MUSE_XCOMBINE_EXPTIME; /* catch all fallback */
309 } /* muse_postproc_get_weight_type() */
310 
311 /*----------------------------------------------------------------------------*/
336 /*----------------------------------------------------------------------------*/
337 cpl_table *
338 muse_postproc_load_nearest(const cpl_propertylist *aHeader,
339  const cpl_frame *aFrame, float aWarnLimit,
340  float aErrLimit, double *aRA, double *aDEC)
341 {
342  cpl_ensure(aHeader && aFrame, CPL_ERROR_NULL_INPUT, NULL);
343  cpl_errorstate state = cpl_errorstate_get();
344  double raref = muse_pfits_get_ra(aHeader),
345  decref = muse_pfits_get_dec(aHeader);
346  cpl_ensure(cpl_errorstate_is_equal(state), CPL_ERROR_DATA_NOT_FOUND, NULL);
347  cpl_msg_debug(__func__, "reference coordinates: RA = %e deg, DEC =%e deg",
348  raref, decref);
349  if (aRA) {
350  *aRA = raref;
351  }
352  if (aDEC) {
353  *aDEC = decref;
354  }
355 
356  const char *fn = cpl_frame_get_filename(aFrame);
357  cpl_propertylist *header;
358  double dmin = FLT_MAX; /* minimum distance yet found in deg */
359  int iext, inearest = -1, next = cpl_fits_count_extensions(fn);
360  for (iext = 1; iext <= next; iext++) {
361  header = cpl_propertylist_load(fn, iext);
362  const char *extname = cpl_propertylist_get_string(header, "EXTNAME");
363  double ra = muse_pfits_get_ra(header),
364  dec = muse_pfits_get_dec(header),
365  d = muse_astro_angular_distance(ra, dec, raref, decref);
366  cpl_msg_debug(__func__, "extension %d [%s]: RA = %e deg, DEC = %e deg --> "
367  "d = %e deg", iext, extname, ra, dec, d);
368  if (d < dmin) {
369  dmin = d;
370  inearest = iext;
371  }
372  cpl_propertylist_delete(header);
373  } /* for iext */
374  /* warn for distances larger than x arcsec */
375  if (dmin * 3600. > aErrLimit) {
376  char *msg = cpl_sprintf("Distance of nearest reference table to observed "
377  "position is %.2f arcmin, certainly a wrong "
378  "reference object!", dmin * 60.);
379  cpl_msg_error(__func__, "%s", msg);
380  cpl_error_set_message(__func__, CPL_ERROR_INCOMPATIBLE_INPUT, "%s", msg);
381  cpl_free(msg);
382  return NULL;
383  }
384  if (dmin * 3600. > aWarnLimit) {
385  cpl_msg_warning(__func__, "Distance of nearest reference table to observed "
386  "position is %.2f arcmin! (Wrong reference object?)",
387  dmin * 60.);
388  }
389  header = cpl_propertylist_load(fn, inearest);
390  const char *extname = cpl_propertylist_get_string(header, "EXTNAME");
391  cpl_msg_info(__func__, "Loading \"%s[%s]\" (distance %.1f arcsec)", fn,
392  extname, dmin * 3600.);
393  cpl_propertylist_delete(header);
394  cpl_table *table = cpl_table_load(fn, inearest, 1);
395  return table;
396 } /* muse_postproc_load_nearest() */
397 
398 /*----------------------------------------------------------------------------*/
414 /*----------------------------------------------------------------------------*/
415 cpl_error_code
417  const muse_table *aResponse)
418 {
419  cpl_ensure_code(aPt && aPt->header, CPL_ERROR_NULL_INPUT);
420  if (!aResponse) {
421  return CPL_ERROR_NONE;
422  }
423  /* cross-check the header of the response curve against *
424  * the flat-field spectrum status of the pixel table */
425  cpl_boolean respff = cpl_propertylist_has(aResponse->header,
426  MUSE_HDR_FLUX_FFCORR),
427  ptff = cpl_propertylist_has(aPt->header, MUSE_HDR_PT_FFCORR);
428  if (respff == ptff || !ptff) {
429  /* same state for both means we don't need to do anything, same if *
430  * the pixel table was not corrected for ffspec */
431  return CPL_ERROR_NONE;
432  }
433 
434  cpl_msg_warning(__func__, "Adapt pixel table to %s for backward compatibility"
435  ": revert correction by flat-field spectrum!",
436  MUSE_TAG_STD_RESPONSE);
437 
438  /* so we need to divide again by the flat-field spectrum */
439  cpl_array *lambdas = muse_cpltable_extract_column(aPt->ffspec,
441  *ffdata = muse_cpltable_extract_column(aPt->ffspec,
443  muse_pixtable_spectrum_apply(aPt, lambdas, ffdata,
444  MUSE_PIXTABLE_OPERATION_DIVIDE);
445  cpl_array_unwrap(lambdas);
446  cpl_array_unwrap(ffdata);
447  cpl_table_delete(aPt->ffspec);
448  aPt->ffspec = NULL;
449  cpl_propertylist_erase(aPt->header, MUSE_HDR_PT_FFCORR);
450  cpl_msg_info(__func__, "Pixel table now convolved with flat-field spectrum "
451  "again, removed %s keyword from header.", MUSE_HDR_PT_FFCORR);
452  return CPL_ERROR_NONE;
453 } /* muse_postproc_revert_ffspec_maybe() */
454 
455 /*----------------------------------------------------------------------------*/
467 /*----------------------------------------------------------------------------*/
468 muse_image *
469 muse_postproc_whitelight(muse_pixtable *aPixtable, double aCRSigma)
470 {
471  cpl_ensure(aPixtable, CPL_ERROR_NULL_INPUT, NULL);
472 
473  cpl_boolean usegrid = getenv("MUSE_COLLAPSE_PIXTABLE")
474  && atoi(getenv("MUSE_COLLAPSE_PIXTABLE")) > 0;
478  if (aCRSigma > 0.) {
480  params->crsigma = aCRSigma;
481  }
482  muse_pixgrid *grid = NULL;
483  muse_datacube *cube = muse_resampling_cube(aPixtable, params,
484  usegrid ? &grid : NULL);
485  if (cube == NULL) {
486  cpl_msg_error(__func__, "Could not create cube for whitelight image");
488  muse_pixgrid_delete(grid);
489  return NULL;
490  }
491  muse_image *image = NULL;
492  muse_table *fwhite = muse_table_load_filter(NULL, "white");
493  if (usegrid) {
495  image = muse_resampling_collapse_pixgrid(aPixtable, grid,
496  cube, fwhite, params);
497  } else {
498  image = muse_datacube_collapse(cube, fwhite);
499  }
501  muse_pixgrid_delete(grid);
502  muse_datacube_delete(cube);
503  muse_table_delete(fwhite);
504  return image;
505 } /* muse_postproc_whitelight() */
506 
507 /*----------------------------------------------------------------------------*/
533 /*----------------------------------------------------------------------------*/
534 static cpl_error_code
535 muse_postproc_run_autocalib(muse_pixtable *aPT, muse_postproc_properties *aProp,
536  const cpl_table *aOffsets,
538  muse_datacube **aCube, muse_image **aWhite)
539 {
540  cpl_ensure_code(aPT && aProp, CPL_ERROR_NULL_INPUT);
541 
542  /* spectra/pixel selection for self-calibration */
543  cpl_size nrows = muse_pixtable_get_nrow(aPT);
544  if (aProp->sky_mask) {
545  cpl_msg_info(__func__, "Preparing masking for self-calibration using input "
546  "mask.");
547  cpl_errorstate state = cpl_errorstate_get();
548  cpl_table_select_all(aPT->table);
549  cpl_size nbefore = cpl_table_count_selected(aPT->table);
550  muse_pixtable_and_selected_mask(aPT, aProp->sky_mask, aProp->wcs, aOffsets);
551  cpl_size nafter = cpl_table_count_selected(aPT->table);
552  if (!cpl_errorstate_is_equal(state)) {
553  if (nafter < nbefore) {
554  cpl_msg_warning(__func__, "An error occurred during pixel selection "
555  "using %s: %s", MUSE_TAG_SKY_MASK,
556  cpl_error_get_message());
557  /* Since some selection happened, this was likely successful, *
558  * even though some errors occurred. Swallow the error to not *
559  * cause more confusion. */
560  cpl_errorstate_set(state);
561  } else {
562  cpl_msg_error(__func__, "Selecting sky pixels using %s failed: %s",
563  MUSE_TAG_SKY_MASK, cpl_error_get_message());
564  return cpl_error_get_code();
565  } /* else */
566  } /* if error state */
567  } else {
568  /* Create white light image and sky mask */
569  cpl_msg_info(__func__, "Intermediate resampling to produce white-light "
570  "image to create auto calibration mask");
571  cpl_msg_indent_more();
574  params->crsigma = aProp->skymodel_params.crsigmac;
575  muse_datacube *cube = muse_resampling_cube(aPT, params, NULL);
577  if (!cube) {
578  cpl_msg_error(__func__, "Could not create cube for white-light image");
579  return CPL_ERROR_ILLEGAL_OUTPUT;
580  }
581  muse_table *fwhite = muse_table_load_filter(NULL, "white");
582  muse_image *whitelight = muse_datacube_collapse(cube, fwhite);
583  muse_table_delete(fwhite);
584  cpl_msg_indent_less();
585 
586  muse_mask *skymask = muse_autocalib_create_mask(whitelight, 5.0,
587  /* hardcode only sensible prefix */ "ESO QC SCIPOST");
588  cpl_table_select_all(aPT->table);
589  muse_pixtable_and_selected_mask(aPT, skymask, NULL, NULL);
590  if (aACalOut) {
591  aACalOut->mask = skymask;
592  } else {
593  muse_mask_delete(skymask);
594  }
595  cpl_size nsel = cpl_table_count_selected(aPT->table);
596  cpl_msg_info(__func__, "Mask selected %"CPL_SIZE_FORMAT" pixels (%.2f %%) ",
597  nsel, 100.*nsel/nrows);
598 
599  /* save output data, if possible */
600  if (aCube) {
601  *aCube = cube;
602  } else {
603  muse_datacube_delete(cube);
604  }
605  if (aWhite) {
606  *aWhite = whitelight;
607  } else {
608  muse_image_delete(whitelight);
609  }
610  } /* else: use data itself to create mask */
611 
612  if (getenv("MUSE_DEBUG_AUTOCALIB")) {
613  cpl_msg_debug(__func__, "Saving pixel table mask");
614  nrows = muse_pixtable_get_nrow(aPT);
615  cpl_table_new_column(aPT->table, "MASK", CPL_TYPE_INT);
616  for (cpl_size n = 0; n < nrows; n++) {
617  cpl_table_set_int(aPT->table, "MASK", n,
618  cpl_table_is_selected(aPT->table, n) == 0 ? 1 : 0);
619  } /* for all pixel table rows */
620  } /* if debug autocalib */
621 
622  muse_table **factors = aACalOut ? &(aACalOut->table) : NULL;
623  cpl_error_code rc = muse_autocalib_slice_median(aPT, 15.0, factors);
624  /* clear the selection in the pixel table before returning */
625  cpl_table_select_all(aPT->table);
626  return rc;
627 } /* muse_postproc_run_autocalib() */
628 
629 /*----------------------------------------------------------------------------*/
660 /*----------------------------------------------------------------------------*/
661 cpl_error_code
663  const cpl_table *aOffsets,
664  muse_datacube **aRamanCube)
665 {
666  if (aRamanCube) {
667  *aRamanCube = NULL; /* NULL output input pointer for the failure cases */
668  }
669  cpl_ensure_code(aPT && aProp, CPL_ERROR_NULL_INPUT);
670  if (aProp->raman_width < kMuseSliceSlitWidthA) {
671  cpl_msg_error(__func__, "Raman signal modelling not done, given width "
672  "parameter only %.2f Angstrom (should be %.2f at least)!",
673  aProp->raman_width, kMuseSliceSlitWidthA);
674  return cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT);
675  }
676  if (!aProp->raman_lines) {
677  cpl_msg_error(__func__, "Raman signal modelling not done, %s missing!",
678  MUSE_TAG_RAMAN_LINES);
679  return cpl_error_set(__func__, CPL_ERROR_DATA_NOT_FOUND);
680  }
681  if (!aProp->lsf_cube) {
682  cpl_msg_error(__func__, "Raman signal modelling not done, %s missing!",
683  MUSE_TAG_LSF_PROFILE);
684  return cpl_error_set(__func__, CPL_ERROR_DATA_NOT_FOUND);
685  }
686 
687  /* central wavelengths of Raman peaks */
688  double wl[MUSE_RAMAN_RANGES] = {
689  6484., /* O2 */
690  6827. /* N2 */
691  };
692  float llo = cpl_propertylist_get_float(aPT->header, MUSE_HDR_PT_LLO),
693  lhi = cpl_propertylist_get_float(aPT->header, MUSE_HDR_PT_LHI);
694  if (llo > wl[1] || lhi < wl[0]) {
695  cpl_msg_info(__func__, "Available wavelengths (%.3f ... %.3f) do not cover "
696  "Raman features, not trying to correct them.", llo, lhi);
697  return CPL_ERROR_NONE;
698  }
699 
700  double cputime = cpl_test_get_cputime(),
701  walltime = cpl_test_get_walltime();
702 
703  /* Adding these columns can take a minute or more, better output something. */
704  cpl_msg_debug(__func__, "Adding temporary pixel table columns...");
705  /* The temporary "lsf" column holds the combined line spread *
706  * function for the Raman spectrum on both wavelength (6484 and *
707  * 6827), including the side lines. */
708  cpl_msg_debug(__func__, "... lsf ...");
709  cpl_table_new_column(aPT->table, "lsf", CPL_TYPE_DOUBLE);
710  cpl_table_fill_column_window_double(aPT->table, "lsf", 0,
711  cpl_table_get_nrow(aPT->table), 0);
712  /* The temporary "raman" column holds the simulated Raman *
713  * illumination, to be subtracted from the "data" column. */
714  cpl_msg_debug(__func__, "... raman ...");
715  cpl_table_new_column(aPT->table, "raman", CPL_TYPE_DOUBLE);
716  cpl_table_fill_column_window_double(aPT->table, "raman", 0,
717  cpl_table_get_nrow(aPT->table), 0);
718  cpl_msg_debug(__func__, "... done.");
719 
720  /* Create mini-pixtables containing just the wavelength range to fit. */
721  muse_pixtable *pt[MUSE_RAMAN_RANGES];
722  muse_image *images[MUSE_RAMAN_RANGES];
723  muse_mask *sky_maskr[MUSE_RAMAN_RANGES];
724  int i;
725  for (i = 0; i < MUSE_RAMAN_RANGES; i++) {
726  double lo = wl[i] - aProp->raman_width * 0.5,
727  hi = wl[i] + aProp->raman_width * 0.5;
728  pt[i] = muse_pixtable_extract_wavelength(aPT, lo, hi);
729  if (!pt[i] || (pt[i] && muse_pixtable_get_nrow(pt[i]) == 0)) {
730  cpl_msg_warning(__func__, "Raman correction for %s feature not possible, "
731  "%.3f..%.3f Angstrom is outside the wavelength range!",
732  i == 0 ? "O2" : "N2", lo, hi);
733  muse_pixtable_delete(pt[i]);
734  pt[i] = NULL;
735  images[i] = NULL;
736  sky_maskr[i] = NULL;
737  continue;
738  }
739 
740  /* Create sky masks: one for each wavelength range */
741  cpl_msg_info(__func__, "Intermediate resampling to produce white-light image"
742  " for Raman correction (%s feature, %.3f..%.3f Angstrom):",
743  i == 0 ? "O2" : "N2", lo, hi);
744  cpl_msg_indent_more();
745  images[i] = muse_postproc_whitelight(pt[i], aProp->skymodel_params.crsigmac);
746  cpl_msg_indent_less();
747  if (!images[i]) {
748  /* should not occur, since the pixel table is checked above */
749  sky_maskr[i] = NULL;
750  continue;
751  }
752  sky_maskr[i] = muse_sky_create_skymask(images[i], aProp->skymodel_params.ignore,
753  aProp->skymodel_params.fraction,
754  "ESO QC SCIPOST");
755  if (aRamanCube) {
756  /* not used a real white filter, and the unit is irrelevant here */
757  cpl_propertylist_erase_regexp(images[i]->header, "ESO DRS MUSE", 0);
758  cpl_propertylist_update_string(images[i]->header, "BUNIT", "");
759  /* the DQ extension is not needed, use NANs instead */
760  muse_image_reject_from_dq(images[i]);
761  cpl_image_fill_rejected(images[i]->data, NAN);
762  cpl_image_delete(images[i]->dq);
763  images[i]->dq = NULL;
764  } else {
765  muse_image_delete(images[i]);
766  }
767  } /* for i (both Raman ranges) */
768 
769  /* check if any one of the two Raman features can be used at all */
770  if (!sky_maskr[0] && !sky_maskr[1]) {
771  /* no correction is possible, clean up and return */
772  cpl_msg_info(__func__, "Neither O2 nor N2 features can be analysed, not "
773  "carrying out Raman correction.");
774  for (i = 0; i < MUSE_RAMAN_RANGES; i++) {
775  muse_image_delete(images[i]);
776  muse_pixtable_delete(pt[i]);
777  } /* for i (both Raman ranges) */
778  cpl_table_erase_column(aPT->table, "raman");
779  cpl_table_erase_column(aPT->table, "lsf");
780  return CPL_ERROR_NONE;
781  } /* no sky mask */
782 
783  /* Remove masked pixels of both ranges (and possibly an input sky mask) *
784  * from both mini pixel tables, and then remove all bad pixels as well. */
785  for (i = 0; i < MUSE_RAMAN_RANGES; i++) {
786  if (!pt[i] || !sky_maskr[i]) {
787  continue;
788  }
789  int j;
790  cpl_table_select_all(pt[i]->table);
791  for (j = 0; j < MUSE_RAMAN_RANGES; j++) {
792  if (!pt[j] || !sky_maskr[j]) {
793  continue;
794  }
795  muse_pixtable_and_selected_mask(pt[i], sky_maskr[j], NULL, NULL);
796  }
797  /* select pixels of an overall sky mask, possibly using its WCS */
798  if (aProp->sky_mask) {
799  muse_pixtable_and_selected_mask(pt[i], aProp->sky_mask, aProp->wcs,
800  aOffsets);
801  }
802  cpl_table_not_selected(pt[i]->table);
803  cpl_table_erase_selected(pt[i]->table);
804 
805  /* remove remaining bad pixels, including the ones that were *
806  * marked as cosmic rays during the white-light image creation */
807  cpl_table_and_selected_int(pt[i]->table, MUSE_PIXTABLE_DQ,
808  CPL_NOT_EQUAL_TO, EURO3D_GOODPIXEL);
809  cpl_table_erase_selected(pt[i]->table);
810  } /* for i (both Raman ranges) */
811 
812  /* Clean up the masks if that don't get propagated out. */
813  for (i = 0; !aRamanCube && i < MUSE_RAMAN_RANGES; i++) {
814  muse_mask_delete(sky_maskr[i]);
815  } /* for i (both Raman ranges) */
816 
817  /* Add the LSF column to each pixtable, and do the fit. */
818  for (i = 0; i < MUSE_RAMAN_RANGES; i++) {
819  if (!pt[i]) {
820  continue;
821  }
822  muse_raman_add_lsf(pt[i], aProp->raman_lines, i, 1.0, aProp->lsf_cube);
823  } /* for i (both Raman ranges) */
824 
825  /* Do the 2D fit for the Raman signal, to both wavelength ranges. */
826  cpl_array *param = muse_raman_fit(pt);
827 
828  /* Now we can clean up both mini pixel tables. */
829  for (i = 0; i < MUSE_RAMAN_RANGES; i++) {
830  muse_pixtable_delete(pt[i]);
831  } /* for i (both Raman ranges) */
832 
833  /* Compute the (mean) flux of the two Raman features. */
834  /* first get the unit, to see if we have to scale the fluxes */
835  const char *colunit = cpl_table_get_column_unit(aProp->raman_lines, "flux");
836  char *unit = NULL;
837  double fscale = 1.;
838  if (colunit) {
839  if (strstr(colunit, "10**(-20)") || strstr(colunit, "10^(-20)")) {
840  fscale = 1e-20;
841  char *p = strstr(colunit, "erg");
842  unit = cpl_sprintf("%s", p);
843  } else {
844  /* seems that there is no scale */
845  unit = cpl_sprintf("%s", colunit);
846  }
847  } /* if colunit */
848  double totflux[MUSE_RAMAN_RANGES];
849  for (i = 0; i < MUSE_RAMAN_RANGES; i++) {
850  /* flux in arbitrary units from the fit */
851  totflux[i] = cpl_array_get(param, 5 + i, NULL) * fscale;
852 
853  /* multiply by total flux for the feature from the table */
854  cpl_table_select_all(aProp->raman_lines);
855  cpl_table_and_selected_int(aProp->raman_lines, "group", CPL_EQUAL_TO, i);
856  cpl_table *t = cpl_table_extract_selected(aProp->raman_lines);
857  double sum = cpl_table_get_nrow(t) * cpl_table_get_column_mean(t, "flux");
858  cpl_table_delete(t);
859  totflux[i] *= sum;
860  } /* for i (both Raman ranges) */
861  cpl_table_select_all(aProp->raman_lines);
862 
863  /* debug: dump the parameters */
864  cpl_msg_info(__func__, "spatial: %g x^2 %+g xy %+g y^2 %+g x %+g y +1",
865  cpl_array_get(param, 0, NULL), cpl_array_get(param, 1, NULL),
866  cpl_array_get(param, 2, NULL), cpl_array_get(param, 3, NULL),
867  cpl_array_get(param, 4, NULL));
868  cpl_msg_info(__func__, "O2 flux = %g, N2 flux = %g %s", totflux[0], totflux[1],
869  unit);
870 
871  if (aRamanCube) {
872  /* Reconstruct an image of the Raman light distribution as well. */
873  muse_image *fits[MUSE_RAMAN_RANGES];
874  for (i = 0; i < MUSE_RAMAN_RANGES; i++) {
875  if (!images[i]) {
876  fits[i] = NULL;
877  } else {
878  fits[i] = muse_raman_simulate_image(images[i], param);
879  }
880  } /* for i (both Raman ranges) */
881 
882  /* Now, to be able to saving everything into one file, use a datacube. *
883  * As the "cube", use the fitted Raman light distribution in arbitrary *
884  * units. */
885  *aRamanCube = cpl_calloc(1, sizeof(muse_datacube));
886  (*aRamanCube)->data = cpl_imagelist_new();
887  if (fits[0]) {
888  cpl_imagelist_set((*aRamanCube)->data, cpl_image_duplicate(fits[0]->data), 0);
889  (*aRamanCube)->header = cpl_propertylist_duplicate(fits[0]->header);
890  } else {
891  cpl_imagelist_set((*aRamanCube)->data, cpl_image_duplicate(fits[1]->data), 0);
892  (*aRamanCube)->header = cpl_propertylist_duplicate(fits[1]->header);
893  }
894 
895  /* write some QC parameters */
896  cpl_propertylist_append_double((*aRamanCube)->header, QC_POST_RAMAN_SP_XX,
897  cpl_array_get(param, 0, NULL));
898  cpl_propertylist_append_double((*aRamanCube)->header, QC_POST_RAMAN_SP_XY,
899  cpl_array_get(param, 1, NULL));
900  cpl_propertylist_append_double((*aRamanCube)->header, QC_POST_RAMAN_SP_YY,
901  cpl_array_get(param, 2, NULL));
902  cpl_propertylist_append_double((*aRamanCube)->header, QC_POST_RAMAN_SP_X,
903  cpl_array_get(param, 3, NULL));
904  cpl_propertylist_append_double((*aRamanCube)->header, QC_POST_RAMAN_SP_Y,
905  cpl_array_get(param, 4, NULL));
906  cpl_propertylist_append_float((*aRamanCube)->header, QC_POST_RAMAN_FLUXO2,
907  totflux[0]);
908  cpl_propertylist_append_float((*aRamanCube)->header, QC_POST_RAMAN_FLUXN2,
909  totflux[1]);
910 
911  /* To be able to use the datacube structure for saving, *
912  * convert the sky masks into images. */
913  muse_image *masks[MUSE_RAMAN_RANGES];
914  for (i = 0; i < MUSE_RAMAN_RANGES; i++) {
915  if (!sky_maskr[i]) {
916  masks[i] = NULL;
917  continue;
918  }
919  masks[i] = muse_image_new();
920  masks[i]->header = cpl_propertylist_duplicate(sky_maskr[i]->header);
921  masks[i]->data = cpl_image_new_from_mask(sky_maskr[i]->mask);
922  muse_mask_delete(sky_maskr[i]);
923 
924  /* Also scale both Raman images using the flux of the Raman lines. */
925  if (!fits[i]) {
926  continue;
927  }
928  cpl_image_multiply_scalar(fits[i]->data, totflux[i]);
929  cpl_propertylist_update_string(fits[i]->header, "BUNIT",
930  kMuseFluxUnitString);
931  } /* for i (both Raman ranges) */
932 
933  (*aRamanCube)->recimages = muse_imagelist_new();
934  (*aRamanCube)->recnames = cpl_array_new(6, CPL_TYPE_STRING);
935  cpl_errorstate state = cpl_errorstate_get();
936  muse_imagelist_set((*aRamanCube)->recimages, images[0], 0);
937  muse_imagelist_set((*aRamanCube)->recimages, masks[0], 1);
938  muse_imagelist_set((*aRamanCube)->recimages, images[1], 2);
939  muse_imagelist_set((*aRamanCube)->recimages, masks[1], 3);
940  muse_imagelist_set((*aRamanCube)->recimages, fits[0], 4);
941  muse_imagelist_set((*aRamanCube)->recimages, fits[1], 5);
942  if (!cpl_errorstate_is_equal(state)) {
943  cpl_msg_debug(__func__, "Some images for the Raman cube were not saved...");
944  /* swallow the errors */
945  cpl_errorstate_set(state);
946  }
947  cpl_array_set_string((*aRamanCube)->recnames, 0, "RAMAN_IMAGE_O2");
948  cpl_array_set_string((*aRamanCube)->recnames, 1, "SKY_MASK_O2");
949  cpl_array_set_string((*aRamanCube)->recnames, 2, "RAMAN_IMAGE_N2");
950  cpl_array_set_string((*aRamanCube)->recnames, 3, "SKY_MASK_N2");
951  cpl_array_set_string((*aRamanCube)->recnames, 4, "RAMAN_FIT_O2");
952  cpl_array_set_string((*aRamanCube)->recnames, 5, "RAMAN_FIT_N2");
953  } /* if aRamanCube */
954  cpl_free(unit);
955 
956  /* Subtract the raman light from the pixel table. *
957  * This is a bit tricky: since we have only a global simulation function *
958  * (independent of the group), but the groups differ by flux, we need to *
959  * put the group individual flux into the lsf column. */
960  for (i = 0; i < MUSE_RAMAN_RANGES; i++) {
961  double flux = cpl_array_get(param, 5 + i, NULL);
962  muse_raman_add_lsf(aPT, aProp->raman_lines, i, flux, aProp->lsf_cube);
963  }
964  cpl_msg_info(__func__, "Simulate Raman light with given parameters");
965  cpl_array *raman = muse_cpltable_extract_column(aPT->table, "raman");
966  muse_raman_simulate(aPT, param, raman);
967  cpl_array_delete(param);
968  cpl_array_unwrap(raman);
969  cpl_msg_info(__func__, "Subtract Raman light");
970  cpl_table_subtract_columns(aPT->table, MUSE_PIXTABLE_DATA, "raman");
971 
972  /* Clean the extra pixel table columns up again. */
973  cpl_table_erase_column(aPT->table, "raman");
974  cpl_table_erase_column(aPT->table, "lsf");
975 
976  cpl_msg_debug(__func__, "Raman correction took %gs (CPU time), %gs (wall-clock)",
977  cpl_test_get_cputime() - cputime,
978  cpl_test_get_walltime() - walltime);
979 
980  return CPL_ERROR_NONE;
981 } /* muse_postproc_correct_raman() */
982 
983 /*----------------------------------------------------------------------------*/
1029 /*----------------------------------------------------------------------------*/
1030 void *
1032  unsigned int aIndex,
1034  muse_datacube **aRamanCube,
1035  muse_postproc_sky_outputs *aSkyOut,
1036  const cpl_table *aOffsets)
1037 {
1038  cpl_ensure(aProp && aProp->exposures, CPL_ERROR_NULL_INPUT, NULL);
1039  cpl_ensure((int)aIndex < cpl_table_get_nrow(aProp->exposures),
1040  CPL_ERROR_ILLEGAL_INPUT, NULL);
1041  cpl_msg_info(__func__, "Starting to process exposure %d (DATE-OBS=%s)",
1042  aIndex + 1, cpl_table_get_string(aProp->exposures, "DATE-OBS",
1043  aIndex));
1044  cpl_ensure(aProp->exposures, CPL_ERROR_NULL_INPUT, NULL);
1045 
1046  cpl_table *exposure = cpl_table_extract(aProp->exposures, aIndex, 1);
1048  aProp->lambdamin,
1049  aProp->lambdamax);
1050  cpl_table_delete(exposure);
1051  if (!pt) {
1052  return NULL;
1053  }
1054  /* erase pre-existing QC parameters */
1055  cpl_propertylist_erase_regexp(pt->header, "ESO QC ", 0);
1056 
1057  /* Override RA, DEC and DROT POSANG if required for the superflat */
1058  if (getenv("MUSE_SUPERFLAT_POS")) {
1059  cpl_array *values = muse_cplarray_new_from_delimited_string(
1060  getenv("MUSE_SUPERFLAT_POS"), ",");
1061  int nval = cpl_array_get_size(values);
1062  if (nval != 3) {
1063  cpl_msg_error(__func__, "MUSE_SUPERFLAT_POS needs 3 parameters, only"
1064  "%d were found.", nval);
1065  cpl_array_delete(values);
1066  }
1067  double ra = atof(cpl_array_get_string(values, 0));
1068  cpl_propertylist_set_double(pt->header, "RA", ra);
1069  double dec = atof(cpl_array_get_string(values, 1));
1070  cpl_propertylist_set_double(pt->header, "DEC", dec);
1071  double drot = atof(cpl_array_get_string(values, 2));
1072  cpl_propertylist_set_double(pt->header, "ESO INS DROT POSANG", drot);
1073  cpl_msg_info(__func__, "Overriding RA=%f, DEC=%f, DROT=%f", ra, dec, drot);
1074  cpl_array_delete(values);
1075  } /* if superflat positioning */
1076 
1077  /* see if we need to revert the flat-field spectrum correction, *
1078  * to match pixel table and response curve */
1080 
1081  /* atmospheric refraction correction */
1082  cpl_boolean labdata = CPL_FALSE;
1083  cpl_errorstate prestate = cpl_errorstate_get();
1084  double airmass = muse_astro_airmass(pt->header);
1085  double ra = muse_pfits_get_ra(pt->header),
1086  dec = muse_pfits_get_dec(pt->header);
1087  /* try to recognize a lab exposure by missing RA/DEC or weird airmass */
1088  if (!cpl_errorstate_is_equal(prestate) || airmass < 1.) {
1089  cpl_msg_warning(__func__, "This seems to be lab data (RA=%f DEC=%f, "
1090  "airmass=%f), not doing any DAR correction!", ra, dec,
1091  airmass);
1092  cpl_errorstate_dump(prestate, CPL_FALSE, NULL);
1093  cpl_errorstate_set(prestate);
1094  labdata = CPL_TRUE;
1095  }
1096  cpl_error_code rc = CPL_ERROR_NONE;
1097  if (!labdata && muse_pfits_get_mode(pt->header) <= MUSE_MODE_WFM_AO_N) {
1098  cpl_msg_debug(__func__, "WFM detected: starting DAR correction");
1099  rc = muse_dar_correct(pt, aProp->lambdaref);
1100  cpl_msg_debug(__func__, "DAR correction returned rc=%d: %s", rc,
1101  rc != CPL_ERROR_NONE ? cpl_error_get_message() : "");
1102  } /* if not labdata but WFM */
1103  /* check and possibly correct the DAR quality, if requested to do so */
1104  if (aProp->darcheck != MUSE_POSTPROC_DARCHECK_NONE) {
1105  cpl_boolean dorefine = aProp->darcheck == MUSE_POSTPROC_DARCHECK_CORRECT;
1106  double maxshift = 0;
1107  rc = muse_dar_check(pt, &maxshift, dorefine, NULL);
1108  if (rc != CPL_ERROR_NONE) {
1109  cpl_msg_warning(__func__, "Maximum %s shift %f\'\' (rc = %d: %s)",
1110  dorefine ? "corrected" : "detected", maxshift, rc,
1111  cpl_error_get_message());
1112  } else {
1113  cpl_msg_info(__func__, "Maximum %s shift %f\'\'",
1114  dorefine ? "corrected" : "detected", maxshift);
1115  }
1116  } /* if DAR checking */
1117 
1118  /* do flux calibration (before sky subtraction!), if possible */
1119  if (aProp->type != MUSE_POSTPROC_STANDARD) {
1120  if (aProp->response) {
1121  rc = muse_flux_calibrate(pt, aProp->response, aProp->extinction,
1122  aProp->telluric);
1123  if (rc != CPL_ERROR_NONE && rc != CPL_ERROR_UNSUPPORTED_MODE) {
1124  /* for a more serious error, output a warning at least *
1125  * (the data may still be usable) */
1126  cpl_msg_warning(__func__, "Flux calibration returned rc=%d: %s", rc,
1127  cpl_error_get_message());
1128  } else {
1129  /* for no error or unsupported mode (an already flux *
1130  * calibrated table!) a debug message seems more appropriate */
1131  cpl_msg_debug(__func__, "Flux calibration returned rc=%d: %s", rc,
1132  rc != CPL_ERROR_NONE ? cpl_error_get_message_default(rc) : "");
1133  }
1134  } else {
1135  cpl_msg_info(__func__, "Skipping flux calibration (no %s curve)",
1136  MUSE_TAG_STD_RESPONSE);
1137  } /* else */
1138  } /* if not POSTPROC_STANDARD */
1139 
1140  /* prepare objects for cube and white-light image, in case these *
1141  * are created already for the self-calibration procedure */
1142  muse_datacube *cube = NULL;
1143  muse_image *whitelight = NULL;
1144 
1145  /* run self calibration using partitioned per-slice sky spectra */
1146  if (aProp->autocalib == MUSE_POSTPROC_AUTOCALIB_DEEPFIELD) {
1147  muse_postproc_run_autocalib(pt, aProp, aOffsets, aACalOut, &cube,
1148  &whitelight);
1149  } /* if autocalib */
1150 
1151  /* Raman line correction for AO modes */
1152  if (muse_pfits_get_mode(pt->header) >= MUSE_MODE_WFM_AO_E &&
1153  aProp->raman_lines) {
1154  muse_postproc_correct_raman(pt, aProp, aOffsets, aRamanCube);
1155  }
1156 
1157  /* prepare sky (lines and) continuum for sky subtraction */
1158  cpl_table *sky_cont = NULL,
1159  *sky_lines = NULL;
1160  if ((aProp->skymethod == MUSE_POSTPROC_SKYMETHOD_MODEL && aProp->sky_lines) ||
1161  (aProp->skymethod == MUSE_POSTPROC_SKYMETHOD_SIMPLE)) {
1162  /* Process optional sky mask */
1163  muse_pixtable *sky_pt = muse_pixtable_duplicate(pt);
1164  if (aProp->sky_mask) {
1165  cpl_table_select_all(sky_pt->table);
1166  muse_pixtable_and_selected_mask(sky_pt, aProp->sky_mask, aProp->wcs, aOffsets);
1167  }
1168  /* Create white light image and sky mask, if still needed */
1169  if (!whitelight) {
1170  if (!cube) {
1171  cpl_msg_info(__func__, "Intermediate resampling to produce white-light image"
1172  " for sky-spectrum creation:");
1173  cpl_msg_indent_more();
1176  params->crsigma = aProp->skymodel_params.crsigmac;
1177  cube = muse_resampling_cube(sky_pt, params, NULL);
1179  if (!cube) {
1180  cpl_msg_error(__func__, "Could not create cube for whitelight image");
1181  muse_pixtable_delete(sky_pt);
1183  return NULL;
1184  } /* if still no cube */
1185  } /* if no cube */
1186  muse_table *fwhite = muse_table_load_filter(NULL, "white");
1187  whitelight = muse_datacube_collapse(cube, fwhite);
1188  muse_table_delete(fwhite);
1189  cpl_msg_indent_less();
1190  } /* if no whitelight image */
1191  muse_mask *sky_mask = muse_sky_create_skymask(whitelight,
1192  aProp->skymodel_params.ignore,
1193  aProp->skymodel_params.fraction,
1194  /* hardcode only sensible prefix */ "ESO QC SCIPOST");
1195 
1196  /* Apply mask and create spectrum, possibly with one CR-reject iteration */
1197  cpl_table_select_all(sky_pt->table);
1198  muse_pixtable_and_selected_mask(sky_pt, sky_mask, aProp->wcs, aOffsets);
1199  cpl_table *spectrum =
1200  muse_resampling_spectrum_iterate(sky_pt, aProp->skymodel_params.sampling,
1201  0., aProp->skymodel_params.crsigmas, 1);
1202  if (aProp->skymethod == MUSE_POSTPROC_SKYMETHOD_MODEL) {
1203  /* Fit sky line spectrum */
1204  cpl_array *lambda = muse_cpltable_extract_column(spectrum, "lambda");
1205  double lambda_low = cpl_array_get_min(lambda),
1206  lambda_high = cpl_array_get_max(lambda);
1207  sky_lines = cpl_table_duplicate(aProp->sky_lines);
1208  muse_sky_lines_set_range(sky_lines, lambda_low-5, lambda_high+5);
1209 
1210  /* do the fit, ignoring possible errors */
1211  prestate = cpl_errorstate_get();
1212  if (aProp->lsf_cube) {
1213  cpl_image *lsfImage = muse_lsf_average_cube_all(aProp->lsf_cube, sky_pt);
1214  muse_wcs *lsfWCS = muse_lsf_cube_get_wcs_all(aProp->lsf_cube);
1215  /* Filter the image with a rectangle to model the spectrum binning */
1216  muse_lsf_fold_rectangle(lsfImage, lsfWCS, aProp->skymodel_params.sampling);
1217 
1218  muse_sky_lines_fit(spectrum, sky_lines, lsfImage, lsfWCS);
1219  if (!cpl_errorstate_is_equal(prestate)) {
1220  cpl_errorstate_dump(prestate, CPL_FALSE, NULL);
1221  cpl_errorstate_set(prestate);
1222  }
1223 
1224  /* Create continuum */
1225  if (!aProp->sky_continuum) {
1226  cpl_msg_info(__func__, "No sky continuum given, create it from the data");
1227  sky_cont = muse_sky_continuum_create(spectrum, sky_lines, lsfImage, lsfWCS,
1228  aProp->skymodel_params.csampling);
1229  } else {
1230  cpl_msg_info(__func__, "Using sky continuum given as input");
1231  }
1232 
1233  /* clean up */
1234  cpl_image_delete(lsfImage);
1235 #ifdef USE_LSF_PARAMS
1236  } else if (aProp->lsf_params) { /* Old LSF params code */
1237  /* do the fit, ignoring possible errors */
1238  muse_sky_lines_fit_old(spectrum, sky_lines);
1239  if (!cpl_errorstate_is_equal(prestate)) {
1240  cpl_errorstate_dump(prestate, CPL_FALSE, NULL);
1241  cpl_errorstate_set(prestate);
1242  }
1243 
1244  if (!aProp->sky_continuum) {
1245  cpl_msg_info(__func__, "No sky continuum given, create it from the data");
1246  muse_sky_subtract_lines_old(sky_pt, sky_lines, aProp->lsf_params);
1247  sky_cont = muse_resampling_spectrum(sky_pt,
1248  aProp->skymodel_params.csampling);
1249  cpl_table_erase_column(sky_cont, "stat");
1250  cpl_table_erase_column(sky_cont, "dq");
1251  cpl_table_name_column(sky_cont, "data", "flux");
1252  }
1253 #endif
1254  }
1255  cpl_array_unwrap(lambda);
1256  } else {
1257  /* simple subtraction: pretend that the spectrum is a *
1258  * continuum and use that function for direct subtraction */
1259  cpl_table *spec2 = cpl_table_duplicate(spectrum);
1260  cpl_table_name_column(spec2, "data", "flux");
1261  rc = muse_sky_subtract_continuum(pt, spec2);
1262  cpl_table_delete(spec2);
1263  } /* else: simple */
1264  if (aSkyOut) {
1265  aSkyOut->mask = sky_mask;
1266  aSkyOut->spectrum = spectrum;
1267  } else {
1268  muse_mask_delete(sky_mask);
1269  cpl_table_delete(spectrum);
1270  }
1271  muse_pixtable_delete(sky_pt);
1272  } /* if MUSE_POSTPROC_SKYMETHOD_MODEL or MUSE_POSTPROC_SKYMETHOD_SIMPLE */
1273  if (aSkyOut) {
1274  aSkyOut->image = whitelight;
1275  } else {
1276  muse_image_delete(whitelight);
1277  }
1278  muse_datacube_delete(cube);
1279 
1280  /* In the following we do the sky subtraction, if we have the products *
1281  * for it. The SKYMETHOD_NONE here reflects the subtract-model option *
1282  * of the muse_scipost recipe. */
1283  if ((aProp->skymethod == MUSE_POSTPROC_SKYMETHOD_NONE) ||
1284  (aProp->skymethod == MUSE_POSTPROC_SKYMETHOD_MODEL)) {
1285  if (aProp->sky_continuum) {
1286  cpl_msg_debug(__func__, "doing sky continuum subtraction with input "
1287  "spectrum");
1288  rc = muse_sky_subtract_continuum(pt, aProp->sky_continuum);
1289  } else if (sky_cont) {
1290  cpl_msg_debug(__func__, "doing sky continuum subtraction with created "
1291  "spectrum");
1292  rc = muse_sky_subtract_continuum(pt, sky_cont);
1293  }
1294  if (sky_lines) {
1295  cpl_msg_debug(__func__, "doing sky line subtraction with created list");
1296  if (aProp->lsf_cube != NULL) {
1297  rc = muse_sky_subtract_lines(pt, sky_lines, aProp->lsf_cube);
1298 #ifdef USE_LSF_PARAMS
1299  } else if (aProp->lsf_params != NULL) {
1300  rc = muse_sky_subtract_lines_old(pt, sky_lines, aProp->lsf_params);
1301 #endif
1302  }
1303  } else if (aProp->sky_lines) {
1304  cpl_msg_debug(__func__, "doing sky line subtraction with input list");
1305  if (aProp->lsf_cube != NULL) {
1306  rc = muse_sky_subtract_lines(pt, aProp->sky_lines, aProp->lsf_cube);
1307 #ifdef USE_LSF_PARAMS
1308  } else if (aProp->lsf_params != NULL) {
1309  rc = muse_sky_subtract_lines_old(pt, aProp->sky_lines, aProp->lsf_params);
1310 #endif
1311  }
1312  }
1313  } /* if sky modeling */
1314  /* either transfer the sky lines and continuum out or clean *
1315  * them up; both should still work, if one or both are NULL */
1316  if (aSkyOut) {
1317  aSkyOut->lines = sky_lines;
1318  aSkyOut->continuum = sky_cont;
1319  } else {
1320  cpl_table_delete(sky_lines);
1321  cpl_table_delete(sky_cont);
1322  }
1323 
1324  /* carry out radial velocity correction, if specified */
1325  muse_rvcorrect(pt, aProp->rvtype);
1326 
1327  /* do flux response computation and return */
1328  if (aProp->type == MUSE_POSTPROC_STANDARD) {
1329  double raref, decref;
1330  cpl_table *reftable = muse_postproc_load_nearest(pt->header,
1331  aProp->refframe, 20., 60.,
1332  &raref, &decref);
1333  if (!reftable) {
1334  cpl_msg_error(__func__, "Correct %s could not be loaded, observed "
1335  "target not listed?", MUSE_TAG_STD_FLUX_TABLE);
1337  return NULL;
1338  }
1339  if (muse_flux_reference_table_check(reftable) != CPL_ERROR_NONE) {
1340  cpl_msg_error(__func__, "%s does not have a recognized format!",
1341  MUSE_TAG_STD_FLUX_TABLE);
1342  cpl_table_delete(reftable);
1344  return NULL;
1345  }
1346 
1347  prestate = cpl_errorstate_get();
1349  rc = muse_flux_integrate_std(pt, aProp->profile, flux);
1350  if (flux->intimage) {
1351  cpl_msg_debug(__func__, "Flux integration found %"CPL_SIZE_FORMAT" stars",
1352  cpl_image_get_size_y(flux->intimage->data));
1353  }
1355  if (airmass < 0) {
1356  cpl_msg_warning(__func__, "Invalid airmass derived (%.3e), resetting to "
1357  " zero", airmass);
1358  airmass = 0.; /* set to zero to leave out uncertain extinction term */
1359  }
1360  flux->raref = raref;
1361  flux->decref = decref;
1362  rc = muse_flux_response_compute(flux, aProp->select, airmass, reftable,
1363  aProp->tellregions, aProp->extinction);
1364  cpl_table_delete(reftable);
1365  rc = muse_flux_get_response_table(flux, aProp->smooth);
1366  rc = muse_flux_get_telluric_table(flux);
1367  rc = muse_flux_compute_qc(flux);
1368 
1369  if (!cpl_errorstate_is_equal(prestate)) {
1370  cpl_msg_warning(__func__, "The following errors happened while computing "
1371  "the flux calibration (rc = %d/%s):", rc,
1372  cpl_error_get_message_default(rc));
1373  cpl_errorstate_dump(prestate, CPL_FALSE, muse_cplerrorstate_dump_some);
1374  }
1375  return flux;
1376  } /* if MUSE_POSTPROC_STANDARD */
1377 
1378  /* do astrometry computation and return */
1379  if (aProp->type == MUSE_POSTPROC_ASTROMETRY) {
1380  cpl_table *reftable = muse_postproc_load_nearest(pt->header,
1381  aProp->refframe, 20., 60.,
1382  NULL, NULL);
1383  if (!reftable) {
1384  cpl_msg_error(__func__, "Correct %s could not be loaded, observed "
1385  "target not listed?", MUSE_TAG_ASTROMETRY_REFERENCE);
1387  return NULL;
1388  }
1389  prestate = cpl_errorstate_get();
1390 
1392  wcs->crpix1 = aProp->crpix1; /* set center of rotation */
1393  wcs->crpix2 = aProp->crpix2;
1394  rc = muse_wcs_locate_sources(pt, fabsf(aProp->detsigma), aProp->centroid,
1395  wcs);
1396  if (aProp->detsigma < 0.) {
1397  /* loop through possible detection sigmas to find the optimal solution */
1398  rc = muse_wcs_optimize_solution(wcs, aProp->detsigma, aProp->centroid,
1399  reftable, aProp->radius, aProp->faccuracy,
1400  aProp->niter, aProp->rejsigma);
1401  } else {
1402  /* use just the given detection sigma for the solution */
1403  rc = muse_wcs_solve(wcs, reftable, aProp->radius, aProp->faccuracy,
1404  aProp->niter, aProp->rejsigma);
1405  if (wcs->wcs) { /* set the keyword that muse_wcs_solve() does not save */
1406  cpl_propertylist_update_float(wcs->wcs, MUSE_HDR_WCS_DETSIGMA,
1407  aProp->detsigma);
1408  cpl_propertylist_set_comment(wcs->wcs, MUSE_HDR_WCS_DETSIGMA,
1409  MUSE_HDR_WCS_DETSIGMA_C_ONE);
1410  }
1411  }
1412  cpl_table_delete(reftable);
1413  cpl_msg_debug(__func__, "Solving astrometry (initial radius %.1f pix, "
1414  "initial relative accuracy %.1f, %d iterations with rejection"
1415  " sigma %.2f) returned rc=%d: %s", aProp->radius,
1416  aProp->faccuracy, aProp->niter, aProp->rejsigma,
1417  rc, rc != CPL_ERROR_NONE ? cpl_error_get_message() : "");
1419 
1420  if (!cpl_errorstate_is_equal(prestate)) {
1421  cpl_msg_warning(__func__, "The following errors happened while computing "
1422  "the astrometric solution (rc = %d/%s):", rc,
1423  cpl_error_get_message());
1424  cpl_errorstate_dump(prestate, CPL_FALSE, muse_cplerrorstate_dump_some);
1425  }
1426  return wcs;
1427  } /* if MUSE_POSTPROC_ASTROMETRY */
1428 
1429  if (aProp->astrometry) {
1430  cpl_propertylist *wcs = aProp->wcs;
1431  if (!wcs) {
1432  /* create WCS, trying to find spaxel scale for the instrument mode */
1433  const char *mode = muse_pfits_get_insmode(pt->header);
1434  cpl_msg_warning(__func__, "Using default MUSE %cFM astrometry, output world"
1435  " coordinates will be inaccurate!", mode[0]);
1436  wcs = muse_wcs_create_default(pt->header);
1437  }
1438  rc = muse_wcs_project_tan(pt, wcs);
1439  cpl_msg_debug(__func__, "Astrometry correction returned rc=%d: %s", rc,
1440  rc != CPL_ERROR_NONE ? cpl_error_get_message() : "");
1441  if (!aProp->wcs) { /* if this was not input, we need to free again */
1442  cpl_propertylist_delete(wcs);
1443  }
1444  } /* if astrometry */
1445 
1446  return pt;
1447 } /* muse_postproc_process_exposure() */
1448 
1449 /*----------------------------------------------------------------------------*/
1461 /*----------------------------------------------------------------------------*/
1462 void
1463 muse_postproc_offsets_scale(muse_pixtable *aPT, const cpl_table *aOffsets,
1464  const char *aPurpose)
1465 {
1466  if (!aPT || !aOffsets) {
1467  return;
1468  }
1469  if (!aPT->header) {
1470  return;
1471  }
1472  cpl_msg_info(__func__, "Applying offsets to %s...", aPurpose);
1473  cpl_msg_indent_more();
1474  double ra = muse_pfits_get_ra(aPT->header),
1475  dec = muse_pfits_get_dec(aPT->header),
1476  *offsets = muse_xcombine_find_offsets(aOffsets,
1478  char keyword[KEYWORD_LENGTH], comment[KEYWORD_LENGTH];
1479  if (offsets) {
1480  if (isfinite(offsets[0]) && isfinite(offsets[1])) {
1481  ra -= offsets[0];
1482  dec -= offsets[1];
1483  cpl_msg_debug(__func__, "Applying coordinate offsets to exposure: %e/%e"
1484  " deg", offsets[0], offsets[1]);
1485  /* update RA/DEC in the header of the pixel table */
1486  cpl_errorstate state = cpl_errorstate_get();
1487  cpl_propertylist_update_double(aPT->header, "RA", ra);
1488  if (!cpl_errorstate_is_equal(state)) {
1489  cpl_errorstate_set(state);
1490  cpl_propertylist_update_float(aPT->header, "RA", ra);
1491  }
1492  cpl_propertylist_set_comment(aPT->header, "RA", "offset applied!");
1493  state = cpl_errorstate_get();
1494  cpl_propertylist_update_double(aPT->header, "DEC", dec);
1495  if (!cpl_errorstate_is_equal(state)) {
1496  cpl_errorstate_set(state);
1497  cpl_propertylist_update_float(aPT->header, "DEC", dec);
1498  }
1499  cpl_propertylist_set_comment(aPT->header, "DEC", "offset applied!");
1500  /* store in the header of the pixel table */
1501  snprintf(keyword, KEYWORD_LENGTH, MUSE_HDR_OFFSETi_DRA, 1);
1502  snprintf(comment, KEYWORD_LENGTH, MUSE_HDR_OFFSETi_DRA_C, offsets[0] * 3600.);
1503  cpl_propertylist_append_double(aPT->header, keyword, offsets[0]);
1504  cpl_propertylist_set_comment(aPT->header, keyword, comment);
1505  snprintf(keyword, KEYWORD_LENGTH, MUSE_HDR_OFFSETi_DDEC, 1);
1506  snprintf(comment, KEYWORD_LENGTH, MUSE_HDR_OFFSETi_DDEC_C, offsets[1] * 3600.);
1507  cpl_propertylist_append_double(aPT->header, keyword, offsets[1]);
1508  cpl_propertylist_set_comment(aPT->header, keyword, comment);
1509  } /* if spatial offsets */
1510  if (isnormal(offsets[2])) { /* valid flux scale */
1511  /* get and apply the scale */
1512  cpl_msg_debug(__func__, "Scaling flux of exposure by %g.", offsets[2]);
1513  muse_pixtable_flux_multiply(aPT, offsets[2]);
1514  /* store in the header of the pixel table */
1515  snprintf(keyword, KEYWORD_LENGTH, MUSE_HDR_FLUX_SCALEi, 1);
1516  cpl_propertylist_append_double(aPT->header, keyword, offsets[2]);
1517  cpl_propertylist_set_comment(aPT->header, keyword, MUSE_HDR_FLUX_SCALEi_C);
1518  } /* if scale */
1519  /* store date of changed exposure in the header of the output pixel table */
1520  snprintf(keyword, KEYWORD_LENGTH, MUSE_HDR_OFFSETi_DATEOBS, 1);
1521  snprintf(comment, KEYWORD_LENGTH, MUSE_HDR_OFFSETi_DATEOBS_C, 1);
1522  cpl_propertylist_append_string(aPT->header, keyword,
1524  cpl_propertylist_set_comment(aPT->header, keyword, comment);
1525  } /* if offsets */
1526  cpl_free(offsets);
1527  cpl_msg_indent_less();
1528 } /* muse_postproc_offsets_scale() */
1529 
1530 /*---------------------------------------------------------------------------*/
1548 /*---------------------------------------------------------------------------*/
1549 cpl_propertylist *
1551 {
1552  cpl_ensure(aProcessing, CPL_ERROR_NULL_INPUT, NULL);
1553 
1554  cpl_frameset *fwcs = muse_frameset_find(aProcessing->inframes,
1555  MUSE_TAG_OUTPUT_WCS, 0, CPL_FALSE);
1556  if (!fwcs || cpl_frameset_get_size(fwcs) <= 0) {
1557  /* not an error, quietly return NULL */
1558  cpl_frameset_delete(fwcs);
1559  return NULL;
1560  }
1561  cpl_frame *frame = cpl_frameset_get_position(fwcs, 0);
1562  const char *fn = cpl_frame_get_filename(frame);
1563  cpl_propertylist *header = NULL;
1564  int iext, next = cpl_fits_count_extensions(fn);
1565  for (iext = 0; iext <= next; iext++) {
1566  header = cpl_propertylist_load(fn, iext);
1567  cpl_errorstate es = cpl_errorstate_get();
1568  cpl_wcs *wcs = cpl_wcs_new_from_propertylist(header);
1569  if (!cpl_errorstate_is_equal(es)) {
1570  cpl_errorstate_set(es);
1571  }
1572  if (!wcs) {
1573  cpl_propertylist_delete(header);
1574  header = NULL;
1575  continue; /* try the next extension */
1576  }
1577  /* need 2D or 3D WCS to have something useful */
1578  int naxis = cpl_wcs_get_image_naxis(wcs);
1579  cpl_boolean valid = naxis == 2 || naxis == 3;
1580  /* need RA---TAN and DEC--TAN for axes 1 and 2 */
1581  const cpl_array *ctypes = cpl_wcs_get_ctype(wcs);
1582  if (valid && cpl_array_get_string(ctypes, 0)) {
1583  valid = valid && !strncmp(cpl_array_get_string(ctypes, 0), "RA---TAN", 9);
1584  }
1585  if (valid && cpl_array_get_string(ctypes, 1)) {
1586  valid = valid && !strncmp(cpl_array_get_string(ctypes, 1), "DEC--TAN", 9);
1587  }
1588  /* need AWAV or AWAV-LOG for axis 3 */
1589  if (valid && naxis == 3 && cpl_array_get_string(ctypes, 2)) {
1590  const char *ctype3 = cpl_array_get_string(ctypes, 2);
1591  valid = valid
1592  && (!strncmp(ctype3, "AWAV", 5) || !strncmp(ctype3, "AWAV-LOG", 9) ||
1593  !strncmp(ctype3, "WAVE", 5) || !strncmp(ctype3, "WAVE-LOG", 9));
1594  }
1595  if (valid) { /* check for unsupported PCi_j keywords */
1596  /* still needs to be done on the input header not on the derives WCS! */
1597  cpl_propertylist *pc = cpl_propertylist_new();
1598  cpl_propertylist_copy_property_regexp(pc, header, "^PC[0-9]+_[0-9]+", 0);
1599  valid = cpl_propertylist_get_size(pc) == 0;
1600  cpl_propertylist_delete(pc);
1601  }
1602  cpl_wcs_delete(wcs);
1603  if (valid) {
1604  cpl_msg_debug(__func__, "Apparently valid header %s found in extension %d"
1605  " of \"%s\"", MUSE_TAG_OUTPUT_WCS, iext, fn);
1606  muse_processing_append_used(aProcessing, frame, CPL_FRAME_GROUP_CALIB, 1);
1607  break;
1608  }
1609  cpl_propertylist_delete(header);
1610  header = NULL;
1611  } /* for iext (all extensions) */
1612  if (!header) {
1613  cpl_msg_warning(__func__, "No valid headers for %s found in \"%s\"",
1614  MUSE_TAG_OUTPUT_WCS, fn);
1615  }
1616  cpl_frameset_delete(fwcs);
1617  return header;
1618 } /* muse_postproc_cube_load_output_wcs() */
1619 
1620 /*---------------------------------------------------------------------------*/
1638 /*---------------------------------------------------------------------------*/
1639 cpl_error_code
1641  muse_pixtable *aPixtable,
1642  muse_cube_type aFormat,
1643  muse_resampling_params *aParams,
1644  const char *aFilter)
1645 {
1646  cpl_ensure_code(aProcessing && aPixtable && aParams, CPL_ERROR_NULL_INPUT);
1647  cpl_ensure_code(aFormat == MUSE_CUBE_TYPE_EURO3D ||
1648  aFormat == MUSE_CUBE_TYPE_FITS ||
1649  aFormat == MUSE_CUBE_TYPE_EURO3D_X ||
1650  aFormat == MUSE_CUBE_TYPE_FITS_X ||
1651  aFormat == MUSE_CUBE_TYPE_SDP, CPL_ERROR_ILLEGAL_INPUT);
1652 
1653  /* convert cube from air to vacuum wavelengths, if needed */
1654  if (aParams->tlambda == MUSE_RESAMPLING_DISP_WAVE ||
1655  aParams->tlambda == MUSE_RESAMPLING_DISP_WAVE_LOG) {
1656  /* convert using standard air (and Ciddor method) */
1658  }
1659 
1660  /* create cube and cast to generic pointer to save code duplication */
1661  void *cube = NULL;
1662  muse_pixgrid *grid = NULL;
1663  if (aFormat == MUSE_CUBE_TYPE_EURO3D || aFormat == MUSE_CUBE_TYPE_EURO3D_X) {
1664  cpl_msg_info(__func__, "Resampling to final cube follows, output format "
1665  "\"Euro3D\"");
1666  cpl_msg_indent_more();
1667  muse_euro3dcube *e3d = muse_resampling_euro3d(aPixtable, aParams);
1668  cpl_msg_indent_less();
1669  cpl_ensure_code(e3d, cpl_error_get_code());
1670  cube = e3d;
1671  } else if (aFormat == MUSE_CUBE_TYPE_FITS ||
1672  aFormat == MUSE_CUBE_TYPE_FITS_X || aFormat == MUSE_CUBE_TYPE_SDP) {
1673  cpl_msg_info(__func__, "Resampling to final cube follows, output format "
1674  "\"FITS cube\"");
1675  cpl_msg_indent_more();
1676  muse_datacube *fits = muse_resampling_cube(aPixtable, aParams, &grid);
1677  cpl_msg_indent_less();
1678  cpl_ensure_code(fits, cpl_error_get_code());
1679  muse_postproc_qc_fwhm(aProcessing, fits);
1680  cube = fits;
1681  }
1682 
1683  cpl_array *filters = muse_cplarray_new_from_delimited_string(aFilter, ",");
1684  int i, ipos = 0, nfilters = cpl_array_get_size(filters);
1685  for (i = 0; i < nfilters; i++) {
1686  /* try to find and load the filter from a table */
1687  muse_table *filter = muse_table_load_filter(aProcessing,
1688  cpl_array_get_string(filters, i));
1689  if (!filter) {
1690  continue;
1691  }
1692 
1693  /* if we got a filter table, do the collapsing */
1694  muse_image *fov = NULL;
1695  if (aFormat == MUSE_CUBE_TYPE_EURO3D || aFormat == MUSE_CUBE_TYPE_EURO3D_X) {
1696  fov = muse_euro3dcube_collapse(cube, filter);
1697  } else if (aFormat == MUSE_CUBE_TYPE_FITS ||
1698  aFormat == MUSE_CUBE_TYPE_FITS_X || aFormat == MUSE_CUBE_TYPE_SDP) {
1699  if (getenv("MUSE_COLLAPSE_PIXTABLE") &&
1700  atoi(getenv("MUSE_COLLAPSE_PIXTABLE")) > 0) {
1701  /* create collapsed image directly from pixel table/grid */
1702  fov = muse_resampling_collapse_pixgrid(aPixtable, grid,
1703  (muse_datacube *)cube,
1704  filter, aParams);
1705  } else {
1706  fov = muse_datacube_collapse(cube, filter);
1707  }
1708  }
1709  muse_table_delete(filter);
1710 
1711  if (aFormat == MUSE_CUBE_TYPE_EURO3D_X || aFormat == MUSE_CUBE_TYPE_FITS_X) {
1712  if (!((muse_datacube *)cube)->recimages) {
1713  /* create empty structures before first entry */
1714  ((muse_datacube *)cube)->recimages = muse_imagelist_new();
1715  ((muse_datacube *)cube)->recnames = cpl_array_new(0, CPL_TYPE_STRING);
1716  }
1717  /* append image to cube/Euro3D structure */
1718  muse_imagelist_set(((muse_datacube *)cube)->recimages, fov,
1719  muse_imagelist_get_size(((muse_datacube *)cube)->recimages));
1720  cpl_array_set_size(((muse_datacube *)cube)->recnames, ipos+1);
1721  cpl_array_set_string(((muse_datacube *)cube)->recnames, ipos,
1722  cpl_array_get_string(filters, i));
1723  } else {
1724  /* we want NANs instead of DQ in the output images */
1725  muse_image_dq_to_nan(fov);
1726  /* duplicate cube header to be able to use its WCS info as the base */
1727  muse_utils_copy_modified_header(fov->header, fov->header, "OBJECT",
1728  cpl_array_get_string(filters, i));
1729  cpl_propertylist_update_string(fov->header, MUSE_HDR_FILTER,
1730  cpl_array_get_string(filters, i));
1731  if (aFormat == MUSE_CUBE_TYPE_SDP) {
1732  if (muse_idp_properties_update_fov(fov) != CPL_ERROR_NONE) {
1733  cpl_msg_warning(__func__, "Writing IDP keywords to field-of-view "
1734  "image failed!");
1735  }
1736  }
1737  muse_processing_save_image(aProcessing, -1, fov, MUSE_TAG_IMAGE_FOV);
1738  muse_image_delete(fov);
1739  }
1740  ipos++;
1741  } /* for i (all filters) */
1742  cpl_array_delete(filters);
1743  muse_pixgrid_delete(grid);
1744 
1745 #ifdef CREATE_IDP
1746  /* Collect IDP properties from the raw data frames and the data cube *
1747  * header */
1748  muse_idp_properties *properties =
1749  muse_idp_properties_collect(aProcessing, ((muse_datacube *)cube)->header,
1750  MUSE_TAG_CUBE_FINAL);
1751 
1752  /* Write formatted IDP properties to the data cube product header so *
1753  * that they are saved to the product file in the next step */
1754  muse_idp_properties_update((muse_datacube *)cube)->header, properties);
1755 #endif
1756 
1757  cpl_error_code rc = CPL_ERROR_NONE;
1758  if (aFormat == MUSE_CUBE_TYPE_EURO3D || aFormat == MUSE_CUBE_TYPE_EURO3D_X) {
1759  /* save the data and clean up */
1760  rc = muse_processing_save_cube(aProcessing, -1, cube, MUSE_TAG_CUBE_FINAL,
1762  muse_euro3dcube_delete(cube);
1763  } else if (aFormat == MUSE_CUBE_TYPE_FITS || aFormat == MUSE_CUBE_TYPE_FITS_X ||
1764  aFormat == MUSE_CUBE_TYPE_SDP) {
1765  /* convert/remove the DQ cube */
1767  /* save the cube and clean up */
1768  if (aFormat == MUSE_CUBE_TYPE_SDP) {
1769  rc = muse_processing_save_cube(aProcessing, -1, cube, MUSE_TAG_CUBE_FINAL,
1771  } else {
1772  rc = muse_processing_save_cube(aProcessing, -1, cube, MUSE_TAG_CUBE_FINAL,
1774  }
1775  muse_datacube_delete(cube);
1776  }
1777 #ifdef CREATE_IDP
1778  muse_idp_properties_delete(properties);
1779 #endif
1780  return rc;
1781 } /* muse_postproc_cube_resample_and_collapse() */
1782 
1783 /*---------------------------------------------------------------------------*/
1806 /*---------------------------------------------------------------------------*/
1807 cpl_error_code
1809 {
1810  cpl_ensure_code(aProcessing && aCube, CPL_ERROR_NULL_INPUT);
1811 
1812  /* determine which FITS keyword prefix to take */
1813  const char *prefix = "";
1814  if (!strncmp(aProcessing->name, "muse_scipost", 13)) {
1815  prefix = QC_POST_PREFIX_SCIPOST;
1816  } else if (!strncmp(aProcessing->name, "muse_exp_combine", 17)) {
1817  prefix = QC_POST_PREFIX_EXPCOMBINE;
1818  } else if (!strncmp(aProcessing->name, "muse_standard", 14)) {
1819  prefix = QC_POST_PREFIX_STANDARD;
1820  } else if (!strncmp(aProcessing->name, "muse_astrometry", 16)) {
1821  prefix = QC_POST_PREFIX_ASTROMETRY;
1822  } else {
1823  cpl_msg_info(__func__, "Recipe \"%s\" found, not generating QC1 keywords",
1824  aProcessing->name);
1825  return CPL_ERROR_NONE;
1826  }
1827 
1828  /* get image from central plane of cube and use it to do object detection *
1829  * and FWHM estimation */
1830  int plane = cpl_imagelist_get_size(aCube->data) / 2;
1831  cpl_image *cim = cpl_imagelist_get(aCube->data, plane);
1832  double dsigmas[] = {/*50., 30., 10., 8., 6.,*/ 5., 4., 3. };
1833  int ndsigmas = sizeof(dsigmas) / sizeof(double);
1834  cpl_vector *vsigmas = cpl_vector_wrap(ndsigmas, dsigmas);
1835  cpl_size isigma = -1;
1836  cpl_errorstate prestate = cpl_errorstate_get();
1837  cpl_apertures *apertures = cpl_apertures_extract(cim, vsigmas, &isigma);
1838  cpl_vector_unwrap(vsigmas);
1839 
1840  /* add the first QC parameters, no matter what the detection did */
1841  char keyword[KEYWORD_LENGTH];
1842  cpl_boolean loglambda = !strncmp(muse_pfits_get_ctype(aCube->header, 3),
1843  "AWAV-LOG", 9);
1844  double crpix3 = muse_pfits_get_crpix(aCube->header, 3),
1845  cd33 = muse_pfits_get_cd(aCube->header, 3, 3),
1846  crval3 = muse_pfits_get_crval(aCube->header, 3),
1847  lambda = loglambda
1848  ? crval3 * exp((plane + 1. - crpix3) * cd33 / crval3)
1849  : (plane + 1. - crpix3) * cd33 + crval3;
1850  snprintf(keyword, KEYWORD_LENGTH, QC_POST_LAMBDA, prefix);
1851  cpl_propertylist_update_float(aCube->header, keyword, lambda);
1852 
1853  /* now see, if we detected anything */
1854  if (!apertures || !cpl_errorstate_is_equal(prestate)) {
1855  /* set up number and a fake entry (source number zero and negative FWHMs) */
1856  snprintf(keyword, KEYWORD_LENGTH, QC_POST_NDET, prefix);
1857  cpl_propertylist_update_int(aCube->header, keyword, 0); /* zero! */
1858 
1859  snprintf(keyword, KEYWORD_LENGTH, QC_POST_POSX, prefix, 0);
1860  cpl_propertylist_update_float(aCube->header, keyword, -1.);
1861  snprintf(keyword, KEYWORD_LENGTH, QC_POST_POSY, prefix, 0);
1862  cpl_propertylist_update_float(aCube->header, keyword, -1.);
1863  snprintf(keyword, KEYWORD_LENGTH, QC_POST_FWHMX, prefix, 0);
1864  cpl_propertylist_update_float(aCube->header, keyword, -1.);
1865  snprintf(keyword, KEYWORD_LENGTH, QC_POST_FWHMY, prefix, 0);
1866  cpl_propertylist_update_float(aCube->header, keyword, -1.);
1867  /* swallow the cpl_aperture error, which is too cryptic for users */
1868  cpl_errorstate_set(prestate);
1869  cpl_msg_warning(__func__, "No sources found for FWHM measurement down to "
1870  "%.1f sigma limit on plane %d, QC parameters will not "
1871  "contain useful information", dsigmas[ndsigmas-1], plane+1);
1872  return CPL_ERROR_DATA_NOT_FOUND;
1873  } /* if no detections */
1874  int ndet = cpl_apertures_get_size(apertures);
1875  snprintf(keyword, KEYWORD_LENGTH, QC_POST_NDET, prefix);
1876  cpl_propertylist_update_int(aCube->header, keyword, ndet); /* zero! */
1877 
1878  /* get some kind of WCS for conversion of FWHM from pixels to arcsec, *
1879  * by default assume WFM and x=RA and y=DEC */
1880  double cd11 = kMuseSpaxelSizeX_WFM, cd12 = 0., cd21 = 0.,
1881  cd22 = kMuseSpaxelSizeY_WFM;
1882  cpl_errorstate es = cpl_errorstate_get();
1883  cpl_wcs *wcs = cpl_wcs_new_from_propertylist(aCube->header);
1884  if (!cpl_errorstate_is_equal(es)) {
1885  cpl_errorstate_set(es); /* swallow possible errors from creating the WCS */
1886  }
1887  if (!wcs || !strncasecmp(muse_pfits_get_ctype(aCube->header, 1), "PIXEL", 5)) {
1888  if (muse_pfits_get_mode(aCube->header) == MUSE_MODE_NFM_AO_N) { /* NFM scaling */
1889  cd11 = kMuseSpaxelSizeX_NFM;
1890  cd22 = kMuseSpaxelSizeY_NFM;
1891  }
1892  } else {
1893  const cpl_matrix *cd = cpl_wcs_get_cd(wcs);
1894  /* take the absolute and scale by 3600 to get positive arcseconds */
1895  cd11 = fabs(cpl_matrix_get(cd, 0, 0)) * 3600.,
1896  cd12 = fabs(cpl_matrix_get(cd, 0, 1)) * 3600.,
1897  cd21 = fabs(cpl_matrix_get(cd, 1, 0)) * 3600.,
1898  cd22 = fabs(cpl_matrix_get(cd, 1, 1)) * 3600.;
1899  }
1900  cpl_wcs_delete(wcs); /* rely on internal NULL check */
1901 
1902  /* Compute FWHM of all apertures; collect them *
1903  * in an image for statistical computations */
1904  cpl_image *statimage = cpl_image_new(ndet, 2, CPL_TYPE_DOUBLE);
1905 #if 0
1906  printf("index RA_FWHM DEC_FWHM\n");
1907 #endif
1908  int n, nbad = 0;
1909  for (n = 1; n <= ndet; n++) {
1910  cpl_size xcen = lround(cpl_apertures_get_centroid_x(apertures, n)),
1911  ycen = lround(cpl_apertures_get_centroid_y(apertures, n));
1912  double x, y;
1913  cpl_errorstate state = cpl_errorstate_get();
1914  cpl_image_get_fwhm(cim, xcen, ycen, &x, &y);
1915  const char *fwhmcomment = NULL;
1916  if (x < 0 || y < 0 || !cpl_errorstate_is_equal(state)) {
1917  /* something's weird with this source */
1918  x = -1.;
1919  y = -1.;
1920  /* do not count these in the statistics */
1921  cpl_image_reject(statimage, n, 1);
1922  cpl_image_reject(statimage, n, 2);
1923  fwhmcomment = "[arcsec] failure determining FWHM";
1924  nbad++;
1925  /* suppress the error state */
1926  cpl_errorstate_set(state);
1927  } else {
1928  /* linear WCS scaling */
1929  x = cd11 * x + cd12 * y;
1930  y = cd22 * y + cd21 * x;
1931  cpl_image_set(statimage, n, 1, x);
1932  cpl_image_set(statimage, n, 2, y);
1933  }
1934 #if 0
1935  printf("%4d %f %f\n", n, x, y);
1936 #endif
1937 
1938  /* generate and write FITS keywords */
1939  snprintf(keyword, KEYWORD_LENGTH, QC_POST_POSX, prefix, n);
1940  cpl_propertylist_update_float(aCube->header, keyword, xcen);
1941  snprintf(keyword, KEYWORD_LENGTH, QC_POST_POSY, prefix, n);
1942  cpl_propertylist_update_float(aCube->header, keyword, ycen);
1943  snprintf(keyword, KEYWORD_LENGTH, QC_POST_FWHMX, prefix, n);
1944  cpl_propertylist_update_float(aCube->header, keyword, x);
1945  if (fwhmcomment) {
1946  cpl_propertylist_set_comment(aCube->header, keyword, fwhmcomment);
1947  }
1948  snprintf(keyword, KEYWORD_LENGTH, QC_POST_FWHMY, prefix, n);
1949  cpl_propertylist_update_float(aCube->header, keyword, y);
1950  if (fwhmcomment) {
1951  cpl_propertylist_set_comment(aCube->header, keyword, fwhmcomment);
1952  }
1953  } /* for n (aperture number) */
1954 #if 0
1955  fflush(stdout);
1956 #endif
1957  cpl_apertures_delete(apertures);
1958 
1959  /* add simple FWHM statistics as well */
1960  snprintf(keyword, KEYWORD_LENGTH, QC_POST_FWHM_NVALID, prefix);
1961  cpl_propertylist_update_int(aCube->header, keyword, ndet - nbad);
1962  cpl_errorstate state = cpl_errorstate_get();
1963  cpl_stats *s = cpl_stats_new_from_image(statimage,
1964  CPL_STATS_MEDIAN | CPL_STATS_MAD);
1965  cpl_boolean error = !cpl_errorstate_is_equal(state);
1966  cpl_errorstate_set(state); /* swallow the error again */
1967  snprintf(keyword, KEYWORD_LENGTH, QC_POST_FWHM_MEDIAN, prefix);
1968  if (error || ndet < 3) {
1969  cpl_propertylist_update_float(aCube->header, keyword, 0.);
1970  } else {
1971  cpl_propertylist_update_float(aCube->header, keyword, cpl_stats_get_median(s));
1972  }
1973  snprintf(keyword, KEYWORD_LENGTH, QC_POST_FWHM_MAD, prefix);
1974  if (error || ndet < 3) {
1975  cpl_propertylist_update_float(aCube->header, keyword, 0.);
1976  } else {
1977  cpl_propertylist_update_float(aCube->header, keyword, cpl_stats_get_mad(s));
1978  }
1979  cpl_stats_delete(s);
1980  cpl_image_delete(statimage);
1981  cpl_msg_info(__func__, "Computed FWHM QC parameters for %d of %d source%s "
1982  "found down to the %.1f sigma threshold on plane %d",
1983  ndet - nbad, ndet, ndet == 1 ? "" : "s", dsigmas[isigma],
1984  plane + 1);
1985 
1986  return CPL_ERROR_NONE;
1987 } /* muse_postproc_qc_fwhm() */
1988 
cpl_error_code muse_sky_lines_fit(cpl_table *, cpl_table *, cpl_image *, muse_wcs *)
Fit all entries of the pixel table to the master sky.
cpl_table * muse_sky_continuum_create(cpl_table *, cpl_table *, cpl_image *, muse_wcs *, double)
Create a continuum spectrum.
#define MUSE_PIXTABLE_DQ
Definition: muse_pixtable.h:49
muse_image * muse_resampling_collapse_pixgrid(muse_pixtable *aPixtable, muse_pixgrid *aGrid, muse_datacube *aCube, const muse_table *aFilter, muse_resampling_params *aParams)
Integrate a pixel table / pixel grid along the wavelength direction.
#define MUSE_PIXTABLE_FFDATA
Definition: muse_pixtable.h:62
Structure definition of a MUSE datacube.
Definition: muse_datacube.h:48
muse_rvcorrect_type rvtype
Definition: muse_postproc.h:99
cpl_propertylist * muse_postproc_cube_load_output_wcs(muse_processing *aProcessing)
Find a file with a usable output WCS in the input frameset.
muse_autocalib_type autocalib
muse_postproc_properties * muse_postproc_properties_new(muse_postproc_type aType)
Create a post-processing properties object.
Definition: muse_postproc.c:76
muse_flux_selection_type select
cpl_error_code muse_flux_compute_qc(muse_flux_object *aFluxObj)
Compute QC parameters, related to on-sky throughput.
Definition: muse_flux.c:2864
void muse_image_delete(muse_image *aImage)
Deallocate memory associated to a muse_image object.
Definition: muse_image.c:85
double muse_pfits_get_cd(const cpl_propertylist *aHeaders, unsigned int aAxisI, unsigned int aAxisJ)
find out the WCS coordinate at the reference point
Definition: muse_pfits.c:446
const char * muse_pfits_get_insmode(const cpl_propertylist *aHeaders)
find out the observation mode
Definition: muse_pfits.c:1383
muse_image * intimage
Definition: muse_flux.h:83
muse_pixtable * muse_pixtable_duplicate(muse_pixtable *aPixtable)
Make a copy of the pixtanle.
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
double muse_pfits_get_crval(const cpl_propertylist *aHeaders, unsigned int aAxis)
find out the WCS coordinate at the reference point
Definition: muse_pfits.c:423
#define MUSE_PIXTABLE_FFLAMBDA
Definition: muse_pixtable.h:61
A structure containing a spatial two-axis WCS.
Definition: muse_wcs.h:105
muse_wcs_centroid_type centroid
const char * name
cpl_table * muse_resampling_spectrum_iterate(muse_pixtable *aPixtable, double aBinwidth, float aLo, float aHi, unsigned char aIter)
Iteratively resample selected pixels of a pixel table into spectrum.
double muse_pfits_get_ra(const cpl_propertylist *aHeaders)
find out the right ascension
Definition: muse_pfits.c:267
cpl_error_code muse_postproc_correct_raman(muse_pixtable *aPT, muse_postproc_properties *aProp, const cpl_table *aOffsets, muse_datacube **aRamanCube)
Set up and run Raman-light removal.
void muse_postproc_offsets_scale(muse_pixtable *aPT, const cpl_table *aOffsets, const char *aPurpose)
Apply spatial offsets and flux scale to a pixel table (of a single exposure)
cpl_image * data
the data extension
Definition: muse_image.h:46
cpl_error_code muse_rvcorrect(muse_pixtable *aPixtable, muse_rvcorrect_type aType)
Correct the wavelengths of all pixels of a given pixel table for radial velocity shift.
cpl_size muse_pixtable_get_nrow(const muse_pixtable *aPixtable)
get the number of rows within the pixel table
void muse_mask_delete(muse_mask *aMask)
Deallocate memory associated to a muse_mask object.
Definition: muse_mask.c:78
#define MUSE_HDR_PT_LHI
FITS header keyword contains the upper limit of the data in spectral direction.
muse_xcombine_types muse_postproc_get_weight_type(const char *aWeightString)
Select correct weighting type for weight string.
cpl_error_code muse_dar_correct(muse_pixtable *aPixtable, double aLambdaRef)
Correct the pixel coordinates of all pixels of a given pixel table for differential atmospheric refra...
Definition: muse_dar.c:139
cpl_error_code muse_postproc_revert_ffspec_maybe(muse_pixtable *aPt, const muse_table *aResponse)
Revert correction of on-sky data with the flat-field spectrum.
cpl_image * muse_lsf_average_cube_all(muse_lsf_cube **aLsfCube, muse_pixtable *aPixtable)
Create an average image from all LSF cubes.
muse_resampling_crstats_type muse_postproc_get_cr_type(const char *aCRTypeString)
Select correct cosmic ray rejection type for crtype string.
muse_datacube * muse_resampling_cube(muse_pixtable *aPixtable, muse_resampling_params *aParams, muse_pixgrid **aGrid)
Resample a pixel table onto a regular grid structure representing a FITS NAXIS=3 datacube.
const char * muse_pfits_get_dateobs(const cpl_propertylist *aHeaders)
find out the date of observations
Definition: muse_pfits.c:364
void muse_datacube_delete(muse_datacube *aCube)
Deallocate memory associated to a muse_datacube object.
cpl_error_code muse_dar_check(muse_pixtable *aPixtable, double *aMaxShift, cpl_boolean aCorrect, muse_datacube **aCube)
check that the continuum of objects in the frame is well-aligned perpendicular to the spatial axis...
Definition: muse_dar.c:472
#define MUSE_HDR_PT_FFCORR
cpl_error_code muse_flux_reference_table_check(cpl_table *aTable)
Check and/or adapt the standard flux reference table format.
Definition: muse_flux.c:170
cpl_error_code muse_postproc_qc_fwhm(muse_processing *aProcessing, muse_datacube *aCube)
Compute QC1 parameters for datacubes and save them in the FITS header.
WCS object to store data needed while computing the astrometric solution.
Definition: muse_wcs.h:70
The pixel grid.
Definition: muse_pixgrid.h:65
Structure definition of MUSE three extension FITS file.
Definition: muse_image.h:40
muse_flux_object * muse_flux_object_new(void)
Allocate memory for a new muse_flux_object object.
Definition: muse_flux.c:68
cpl_error_code muse_flux_calibrate(muse_pixtable *aPixtable, const muse_table *aResponse, const cpl_table *aExtinction, const muse_table *aTelluric)
Convert the input pixel table from counts to fluxes.
Definition: muse_flux.c:3094
cpl_error_code muse_postproc_cube_resample_and_collapse(muse_processing *aProcessing, muse_pixtable *aPixtable, muse_cube_type aFormat, muse_resampling_params *aParams, const char *aFilter)
High level function to resample to a datacube and collapse that to an image of the field of view and ...
cpl_table * table
The pixel table.
muse_flux_profile_type profile
void muse_lsf_params_delete_all(muse_lsf_params **aParams)
Delete an allocated array of muse_lsf_params structure.
muse_postproc_skymethod skymethod
cpl_propertylist * header
the FITS header
Definition: muse_image.h:72
muse_lsf_params ** lsf_params
#define MUSE_PIXTABLE_DATA
Definition: muse_pixtable.h:48
unsigned int muse_imagelist_get_size(muse_imagelist *aList)
Return the number of stored images.
cpl_error_code muse_wcs_project_tan(muse_pixtable *aPixtable, const cpl_propertylist *aWCS)
Carry out a gnomonic projection of a pixel table into native spherical coordinates.
Definition: muse_wcs.c:1353
muse_resampling_crstats_type crtype
cpl_image * dq
the data quality extension
Definition: muse_image.h:56
double muse_pfits_get_crpix(const cpl_propertylist *aHeaders, unsigned int aAxis)
find out the WCS reference point
Definition: muse_pfits.c:401
cpl_error_code muse_flux_get_telluric_table(muse_flux_object *aFluxObj)
Get the table of the telluric correction.
Definition: muse_flux.c:2758
void muse_pixgrid_delete(muse_pixgrid *aGrid)
Delete a pixel grid and remove its memory.
Definition: muse_pixgrid.c:575
cpl_array * muse_raman_fit(muse_pixtable *aPixtable[MUSE_RAMAN_RANGES])
Fit raman lines to an array of pixtables.
Definition: muse_raman.c:377
cpl_array * muse_cplarray_new_from_delimited_string(const char *aString, const char *aDelim)
Convert a delimited string into an array of strings.
Structure definition of MUSE pixel table.
Flux object to store data needed while computing the flux calibration.
Definition: muse_flux.h:78
cpl_error_code muse_datacube_convert_dq(muse_datacube *aCube)
Convert the DQ extension of a datacube to NANs in DATA and STAT.
muse_cube_type muse_postproc_get_cube_format(const char *aFormatString)
Select correct cube format for format string.
cpl_error_code muse_sky_subtract_lines_old(muse_pixtable *aPixtable, cpl_table *aLines, muse_lsf_params **aLsfParams)
Subtract sky lines from a pixtable.
Definition: muse_sky_old.c:532
cpl_array * muse_cpltable_extract_column(cpl_table *aTable, const char *aColumn)
Create an array from a section of a column.
double * muse_xcombine_find_offsets(const cpl_table *aOffsets, const char *aDateObs)
Get offsets and scale from table row with matching DATE-OBS entry.
cpl_boolean muse_postproc_check_save_param(const char *aSave, const char *aValid)
Check the –save parameter contents against allowed options.
cpl_error_code muse_autocalib_slice_median(muse_pixtable *aPixtable, double aClip, muse_table **aFactors)
Correct the background level of the slices (port from MPDAF).
cpl_error_code muse_flux_response_compute(muse_flux_object *aFluxObj, muse_flux_selection_type aSelect, double aAirmass, const cpl_table *aReference, const cpl_table *aTellBands, const cpl_table *aExtinct)
Compare measured flux distribution over wavelength with calibrated stellar fluxes and derive instrume...
Definition: muse_flux.c:2070
muse_resampling_crstats_type
Cosmic ray rejection statistics type.
cpl_propertylist * muse_wcs_create_default(const cpl_propertylist *aHeader)
Create FITS headers containing a default (relative) WCS.
Definition: muse_wcs.c:1184
muse_resampling_params * muse_resampling_params_new(muse_resampling_type aMethod)
Create the resampling parameters structure.
cpl_error_code muse_processing_save_cube(muse_processing *aProcessing, int aIFU, void *aCube, const char *aTag, muse_cube_type aType)
Save a MUSE datacube to disk.
cpl_error_code muse_wcs_solve(muse_wcs_object *aWCSObj, cpl_table *aReference, float aRadius, float aFAccuracy, int aIter, float aThresh)
Find the world coordinate solution for a given field using coordinates of detected sources and coordi...
Definition: muse_wcs.c:671
muse_wcs_object * muse_wcs_object_new(void)
Allocate memory for a new muse_wcs_object object.
Definition: muse_wcs.c:71
cpl_table * ffspec
A flat-field spectrum.
muse_flux_smooth_type smooth
muse_cube_type
cpl_propertylist * wcs
Structure definition of the post-processing properties.
Definition: muse_postproc.h:92
void muse_cplerrorstate_dump_some(unsigned aCurrent, unsigned aFirst, unsigned aLast)
Dump some CPL errors.
void muse_euro3dcube_delete(muse_euro3dcube *aEuro3D)
Deallocate memory associated to a muse_euro3dcube object.
muse_table * muse_table_load_filter(muse_processing *aProcessing, const char *aFilterName)
Load a table for a given filter name.
Definition: muse_utils.c:799
void muse_postproc_properties_delete(muse_postproc_properties *aProp)
Free memory taken by a post-processing properties object and all its components.
cpl_table * muse_postproc_load_nearest(const cpl_propertylist *aHeader, const cpl_frame *aFrame, float aWarnLimit, float aErrLimit, double *aRA, double *aDEC)
Load the calibration from a multi-table FITS file that is nearest on the sky.
cpl_imagelist * data
the cube containing the actual data values
Definition: muse_datacube.h:76
cpl_error_code muse_sky_subtract_continuum(muse_pixtable *, cpl_table *)
Subtract sky continuum from pixel table.
cpl_error_code muse_sky_lines_set_range(cpl_table *, double, double)
Limit the lines in the table to a wavelength range.
Structure definition of a Euro3D datacube.
Definition: muse_datacube.h:97
double muse_pfits_get_dec(const cpl_propertylist *aHeaders)
find out the declination
Definition: muse_pfits.c:285
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.
cpl_error_code muse_pixtable_and_selected_mask(muse_pixtable *aPixtable, const muse_mask *aMask, const cpl_propertylist *aWCS, const cpl_table *aOffsets)
Select all pixels where the spatial positions are enabled in the given mask.
Structure to store a table together with a property list.
Definition: muse_table.h:43
cpl_error_code muse_flux_get_response_table(muse_flux_object *aFluxObj, muse_flux_smooth_type aSmooth)
Get the table of the standard star response function.
Definition: muse_flux.c:2644
muse_image * muse_postproc_whitelight(muse_pixtable *aPixtable, double aCRSigma)
Create a white-light image of the field of view.
muse_pixtable * muse_pixtable_extract_wavelength(muse_pixtable *aPixtable, double aLow, double aHigh)
Create a new pixtable with values within the selected wavelengths.
void muse_table_delete(muse_table *aTable)
Deallocate memory associated to a muse_table object.
Definition: muse_table.c:80
muse_resampling_dispersion_type tlambda
#define MUSE_HDR_PT_LLO
FITS header keyword contains the lower limit of the data in spectral direction.
muse_lsf_cube ** lsf_cube
cpl_propertylist * header
the header
Definition: muse_table.h:56
const char * muse_pfits_get_ctype(const cpl_propertylist *aHeaders, unsigned int aAxis)
find out the WCS axis type string
Definition: muse_pfits.c:469
void * muse_postproc_process_exposure(muse_postproc_properties *aProp, unsigned int aIndex, muse_postproc_autocal_outputs *aACalOut, muse_datacube **aRamanCube, muse_postproc_sky_outputs *aSkyOut, const cpl_table *aOffsets)
Merge and process pixel tables from one exposure.
muse_postproc_type
Type of per-exposure processing to run.
Definition: muse_postproc.h:59
cpl_error_code muse_flux_integrate_std(muse_pixtable *aPixtable, muse_flux_profile_type aProfile, muse_flux_object *aFluxObj)
Reconstruct a cube, detect the standard star, and integrate its flux.
Definition: muse_flux.c:1362
cpl_propertylist * header
the FITS header
Definition: muse_datacube.h:57
int muse_processing_save_image(muse_processing *aProcessing, int aIFU, muse_image *aImage, const char *aTag)
Save a computed MUSE image to disk.
muse_pixtable * muse_pixtable_load_merge_channels(cpl_table *aExposureList, double aLambdaMin, double aLambdaMax)
Load and merge the pixel tables of the 24 MUSE sub-fields.
cpl_error_code muse_wcs_optimize_solution(muse_wcs_object *aWCSObj, float aDetSigma, muse_wcs_centroid_type aCentroid, cpl_table *aReference, float aRadius, float aFAccuracy, int aIter, float aRejSigma)
Find an optimal astrometry solution given a detection image.
Definition: muse_wcs.c:990
muse_resampling_type muse_postproc_get_resampling_type(const char *aResampleString)
Select correct resampling type for resample string.
muse_mask * muse_autocalib_create_mask(muse_image *aImage, double aNSigma, const char *aQCPrefix)
Create Select spaxels to be considered as sky for autocalibration.
cpl_error_code muse_phys_air_to_vacuum(muse_pixtable *aPixtable, unsigned int aFlags)
Convert a pixel table from air to vacuum wavelengths.
Definition: muse_phys.c:261
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
Handling of "mask" files.
Definition: muse_mask.h:43
cpl_error_code muse_image_reject_from_dq(muse_image *aImage)
Reject pixels of a muse_image depending on its DQ data.
Definition: muse_image.c:863
void muse_lsf_cube_delete_all(muse_lsf_cube **aLsfCube)
Delete all LSF cubes.
muse_imagelist * muse_imagelist_new(void)
Create a new (empty) MUSE image list.
muse_wcs * muse_lsf_cube_get_wcs_all(muse_lsf_cube **aLsfCubes)
Get the world coordinate representative of all LSF cubes.
cpl_frameset * inframes
muse_mask * muse_sky_create_skymask(muse_image *, double, double, const char *)
Select spaxels to be considered as sky.
cpl_error_code muse_sky_subtract_lines(muse_pixtable *, cpl_table *, muse_lsf_cube **)
Subtract sky lines from a pixtable.
muse_postproc_type type
Definition: muse_postproc.h:93
muse_resampling_type
Resampling types.
cpl_error_code muse_pixtable_spectrum_apply(muse_pixtable *aPixtable, const cpl_array *aLambdas, const cpl_array *aFlux, muse_pixtable_operation aOperation)
Apply a spectrum given by two arrays with an operation to a pixel table.
cpl_error_code muse_lsf_fold_rectangle(cpl_image *aLsfImage, const muse_wcs *aWCS, double aBinWidth)
Filter an LSF image with a rectangle to model spectrum binning.
muse_euro3dcube * muse_resampling_euro3d(muse_pixtable *aPixtable, muse_resampling_params *aParams)
Resample a pixel table onto a regular grid structure representing a Euro3D format file...
Resampling parameters.
Structure definition of the post-processing output sky data.
void muse_resampling_params_delete(muse_resampling_params *aParams)
Delete a resampling parameters structure.
muse_resampling_type method
muse_image * muse_datacube_collapse(muse_datacube *aCube, const muse_table *aFilter)
Integrate a FITS NAXIS=3 datacube along the wavelength direction.
muse_image * muse_image_new(void)
Allocate memory for a new muse_image object.
Definition: muse_image.c:66
cpl_table * muse_resampling_spectrum(muse_pixtable *aPixtable, double aBinwidth)
Resample the selected pixels of a pixel table into a spectrum.
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
Structure definition of the post-processing output auto-calibration.
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
cpl_error_code muse_sky_lines_fit_old(cpl_table *aSpectrum, cpl_table *aLines)
Fit all entries of the pixel table to the master sky.
Definition: muse_sky_old.c:436
muse_sky_params skymodel_params
void muse_pixtable_delete(muse_pixtable *aPixtable)
Deallocate memory associated to a pixel table object.
muse_postproc_darcheck darcheck
Definition: muse_postproc.h:98
muse_xcombine_types
Xposure combination types.
Definition: muse_xcombine.h:44
cpl_error_code muse_imagelist_set(muse_imagelist *aList, muse_image *aImage, unsigned int aIdx)
Set the muse_image of given list index.
double muse_astro_angular_distance(double aRA1, double aDEC1, double aRA2, double aDEC2)
Compute angular distance in the sky between two positions.
Definition: muse_astro.c:496
muse_ins_mode muse_pfits_get_mode(const cpl_propertylist *aHeaders)
find out the observation mode
Definition: muse_pfits.c:1352
cpl_error_code muse_utils_copy_modified_header(cpl_propertylist *aH1, cpl_propertylist *aH2, const char *aKeyword, const char *aString)
Copy a modified header keyword from one header to another.
Definition: muse_utils.c:2570
cpl_error_code muse_wcs_locate_sources(muse_pixtable *aPixtable, float aSigma, muse_wcs_centroid_type aCentroid, muse_wcs_object *aWCSObj)
Determine the centers of stars on an astrometric exposure.
Definition: muse_wcs.c:496
muse_image * muse_euro3dcube_collapse(muse_euro3dcube *aEuro3D, const muse_table *aFilter)
Integrate a Euro3D datacube along the wavelength direction.
cpl_error_code muse_image_dq_to_nan(muse_image *aImage)
Convert pixels flagged in the DQ extension to NANs in DATA (and STAT, if present).
Definition: muse_image.c:904
double muse_astro_airmass(cpl_propertylist *aHeader)
Derive the effective airmass of an observation from information in a FITS header. ...
Definition: muse_astro.c:331
cpl_propertylist * header
The FITS header.
cpl_error_code muse_pixtable_flux_multiply(muse_pixtable *aPixtable, double aScale)
Scale the flux of a pixel table with correct treatment of variance.