MUSE Pipeline Reference Manual  2.1.1
muse_bias.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-2014 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 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 
26 /*----------------------------------------------------------------------------*
27  * Includes *
28  *----------------------------------------------------------------------------*/
29 #include <stdio.h>
30 #include <float.h>
31 #include <math.h>
32 #include <string.h>
33 #include <cpl.h>
34 #include <muse.h>
35 #include "muse_bias_z.h"
36 
37 /*---------------------------------------------------------------------------*
38  * Functions code *
39  *---------------------------------------------------------------------------*/
40 
41 /* Explicitely specify the parameters for the cpl_flux_get_noise_window() *
42  * function so that we can control, how many pixels are involved which is *
43  * necessary to know for the determination of the variance. *
44  * Make the boxes a bit larger than the default to use more pixels for *
45  * the determination and hence also get smaller error estimates. */
46 #define RON_HALFSIZE 9
47 #define RON_NSAMPLES 100
48 
49 /*---------------------------------------------------------------------------*/
59 /*---------------------------------------------------------------------------*/
60 static cpl_error_code
61 muse_bias_qc_header(muse_image *aImage, cpl_bivector *aRON,
62  muse_imagelist *aList)
63 {
64  cpl_ensure_code(aImage && aRON && aList, CPL_ERROR_NULL_INPUT);
65  cpl_msg_debug(__func__, "Adding QC parameters");
66 
67  /* copy saturation numbers from the input images */
68  unsigned int k;
69  for (k = 0; k < muse_imagelist_get_size(aList); k++) {
70  char *keyword = cpl_sprintf(QC_BIAS_PREFIXi" "QC_BASIC_NSATURATED, k+1);
71  int nsaturated = cpl_propertylist_get_int(muse_imagelist_get(aList, k)->header,
72  MUSE_HDR_TMP_NSAT);
73  cpl_propertylist_update_int(aImage->header, keyword, nsaturated);
74  cpl_free(keyword);
75  } /* for k (all images in list) */
76 
77  /* propagate read-out noise and per-quadrant medians */
78  cpl_vector *vmedians = cpl_vector_new(aList->size);
79  const double *ron = cpl_bivector_get_x_data_const(aRON),
80  *ronerr = cpl_bivector_get_y_data_const(aRON);
81  unsigned char n;
82  for (n = 1; n <= 4; n++) {
83  /* get the image window for this quadrant */
84  cpl_size *window = muse_quadrants_get_window(aImage, n);
85 
86  /* save read-out noise and its error in the respective FITS keywords */
87  char keyword[KEYWORD_LENGTH];
88  snprintf(keyword, KEYWORD_LENGTH, QC_BIAS_MASTER_RON, n);
89  cpl_propertylist_append_float(aImage->header, keyword, ron[n-1]);
90 
91  snprintf(keyword, KEYWORD_LENGTH, QC_BIAS_MASTER_RONERR, n);
92  cpl_propertylist_append_float(aImage->header, keyword, ronerr[n-1]);
93 
94  snprintf(keyword, KEYWORD_LENGTH, QC_BIAS_MASTERn_PREFIX, n);
95  unsigned stats = CPL_STATS_MEDIAN | CPL_STATS_MEAN | CPL_STATS_STDEV
96  | CPL_STATS_MIN | CPL_STATS_MAX;
98  keyword, stats, window[0],
99  window[2], window[1], window[3]);
100 
101  /* add slopes for the quadrant */
102  cpl_vector *slopes = muse_cplimage_slope_window(aImage->data, window);
103  snprintf(keyword, KEYWORD_LENGTH, QC_BIAS_MASTER_SLOPEX, n);
104  cpl_propertylist_append_float(aImage->header, keyword,
105  cpl_vector_get(slopes, 0));
106  snprintf(keyword, KEYWORD_LENGTH, QC_BIAS_MASTER_SLOPEY, n);
107  cpl_propertylist_append_float(aImage->header, keyword,
108  cpl_vector_get(slopes, 1));
109  cpl_vector_delete(slopes);
110 
111  cpl_free(window);
112 
113  /* propagate per-quadrant medians from individual images to master image */
114  for (k = 0; k < aList->size; k++) {
115  snprintf(keyword, KEYWORD_LENGTH, MUSE_HDR_TMP_QUADnMED, n);
116  double val = cpl_propertylist_get_float(muse_imagelist_get(aList, k)->header,
117  keyword);
118  cpl_vector_set(vmedians, k, val);
119  } /* for k (all images in list) */
120  snprintf(keyword, KEYWORD_LENGTH, QC_BIAS_LEVELi_MEAN, n);
121  cpl_propertylist_update_float(aImage->header, keyword,
122  cpl_vector_get_mean(vmedians));
123  snprintf(keyword, KEYWORD_LENGTH, QC_BIAS_LEVELi_STDEV, n);
124  cpl_propertylist_update_float(aImage->header, keyword,
125  cpl_vector_get_stdev(vmedians));
126  snprintf(keyword, KEYWORD_LENGTH, QC_BIAS_LEVELi_MEDIAN, n);
127  cpl_propertylist_update_float(aImage->header, keyword,
128  cpl_vector_get_median(vmedians));
129  } /* for n (quadrants) */
130  cpl_vector_delete(vmedians);
131  return CPL_ERROR_NONE;
132 } /* muse_bias_qc_header() */
133 
134 /*---------------------------------------------------------------------------*/
144 /*---------------------------------------------------------------------------*/
145 int
146 muse_bias_compute(muse_processing *aProcessing, muse_bias_params_t *aParams)
147 {
149  "muse.muse_bias");
150  muse_imagelist *images = muse_basicproc_load(aProcessing, aParams->nifu, bpars);
152  cpl_ensure(images, cpl_error_get_code(), -1);
153 
154  cpl_bivector *bron = muse_imagelist_compute_ron(images,
155  RON_HALFSIZE,
156  RON_NSAMPLES);
157 
158  muse_combinepar *cpars = muse_combinepar_new(aProcessing->parameters,
159  "muse.muse_bias");
160  muse_image *masterimage = muse_combine_images(cpars, images);
161  muse_combinepar_delete(cpars);
162  if (!masterimage) {
163  cpl_msg_error(__func__, "Combining input frames failed!");
164  muse_imagelist_delete(images);
165  cpl_bivector_delete(bron);
166  return -1;
167  }
168  cpl_propertylist_erase_regexp(masterimage->header, MUSE_WCS_KEYS, 0);
169 
170  muse_bias_qc_header(masterimage, bron, images);
171  muse_imagelist_delete(images);
172  cpl_bivector_delete(bron);
173 
174  int nbad = muse_quality_bad_columns(masterimage, aParams->losigmabadpix,
175  aParams->hisigmabadpix);
176  cpl_propertylist_append_int(masterimage->header, QC_BIAS_MASTER_NBADPIX,
177  nbad);
178 
179  muse_basicproc_qc_saturated(masterimage, QC_BIAS_MASTER_PREFIX);
180  int rc = muse_processing_save_image(aProcessing, aParams->nifu,
181  masterimage, MUSE_TAG_MASTER_BIAS);
182  muse_image_delete(masterimage);
183  return rc == CPL_ERROR_NONE ? 0 : -1;
184 } /* muse_bias_compute() */
muse_imagelist * muse_basicproc_load(muse_processing *aProcessing, unsigned char aIFU, muse_basicproc_params *aBPars)
Load the raw input files from disk and do basic processing.
Structure definition for a collection of muse_images.
void muse_image_delete(muse_image *aImage)
Deallocate memory associated to a muse_image object.
Definition: muse_image.c:85
cpl_size * muse_quadrants_get_window(const muse_image *aImage, unsigned char aQuadrant)
Determine the data window of a given quadrant on the CCD.
cpl_image * data
the data extension
Definition: muse_image.h:46
double hisigmabadpix
High sigma to find bright columns in the combined bias.
Definition: muse_bias_z.h:88
int muse_quality_bad_columns(muse_image *aBias, double aLow, double aHigh)
Find bad columns (in a master bias).
Definition: muse_quality.c:193
void muse_imagelist_delete(muse_imagelist *aList)
Free the memory of the MUSE image list.
muse_basicproc_params * muse_basicproc_params_new(cpl_parameterlist *aParameters, const char *aPrefix)
Create a new structure of basic processing parameters.
muse_image * muse_combine_images(muse_combinepar *aCPars, muse_imagelist *aImages)
Combine several images into one.
Definition: muse_combine.c:741
Structure definition of MUSE three extension FITS file.
Definition: muse_image.h:40
int nifu
IFU to handle. If set to 0, all IFUs are processed serially. If set to -1, all IFUs are processed in ...
Definition: muse_bias_z.h:50
void muse_basicproc_params_delete(muse_basicproc_params *aBPars)
Free a structure of basic processing parameters.
cpl_propertylist * header
the FITS header
Definition: muse_image.h:72
unsigned int muse_imagelist_get_size(muse_imagelist *aList)
Return the number of stored images.
void muse_combinepar_delete(muse_combinepar *aCPars)
Clear the combination parameters.
Definition: muse_combine.c:715
double losigmabadpix
Low sigma to find dark columns in the combined bias.
Definition: muse_bias_z.h:85
#define MUSE_WCS_KEYS
regular expression for WCS properties
Definition: muse_wcs.h:48
muse_image * muse_imagelist_get(muse_imagelist *aList, unsigned int aIdx)
Get the muse_image of given list index.
cpl_error_code muse_basicproc_stats_append_header_window(cpl_image *aImage, cpl_propertylist *aHeader, const char *aPrefix, unsigned aStats, int aX1, int aY1, int aX2, int aY2)
Compute image statistics of an image window and add them to a header.
muse_combinepar * muse_combinepar_new(cpl_parameterlist *aParameters, const char *aPrefix)
Create a new set of combination parameters.
Definition: muse_combine.c:672
cpl_vector * muse_cplimage_slope_window(const cpl_image *aImage, const cpl_size *aWindow)
Compute slopes of an image, both horizontally and vertically.
int muse_processing_save_image(muse_processing *aProcessing, int aIFU, muse_image *aImage, const char *aTag)
Save a computed MUSE image to disk.
cpl_bivector * muse_imagelist_compute_ron(muse_imagelist *aList, int aHalfsize, int aNSamples)
Compute the read-out noise from bias images in an imagelist.
Structure of basic processing parameters.
unsigned int size
Structure to hold the parameters of the muse_bias recipe.
Definition: muse_bias_z.h:48
cpl_parameterlist * parameters
cpl_error_code muse_basicproc_qc_saturated(muse_image *aImage, const char *aPrefix)
Add QC parameter about saturated pixels to a muse_image.