MUSE Pipeline Reference Manual  0.18.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  *
5  * This file is part of the MUSE Instrument Pipeline
6  * Copyright (C) 2005-2011 European Southern Observatory
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 /*----------------------------------------------------------------------------*
28  * Includes *
29  *----------------------------------------------------------------------------*/
30 #include <stdio.h>
31 #include <float.h>
32 #include <math.h>
33 #include <string.h>
34 #include <cpl.h>
35 #include <muse.h>
36 #include "muse_bias_z.h"
37 
38 /*---------------------------------------------------------------------------*
39  * Functions code *
40  *---------------------------------------------------------------------------*/
41 
42 /* Explicitely specify the parameters for the cpl_flux_get_noise_window() *
43  * function so that we can control, how many pixels are involved which is *
44  * necessary to know for the determination of the variance. *
45  * Make the boxes a bit larger than the default to use more pixels for *
46  * the determination and hence also get smaller error estimates. */
47 #define RON_HALFSIZE 9
48 #define RON_NSAMPLES 100
49 
50 /*---------------------------------------------------------------------------*/
58 /*---------------------------------------------------------------------------*/
59 static void
60 muse_bias_qc_header(muse_image *aImage, cpl_bivector *aRON,
61  muse_imagelist *aList)
62 {
63  cpl_msg_debug(__func__, "Adding QC parameters");
64 
65  /* copy saturation numbers from the input images */
66  unsigned int i;
67  for (i = 0; i < muse_imagelist_get_size(aList); i++) {
68  char *keyword = cpl_sprintf(QC_BIAS_PREFIXi" "QC_BASIC_NSATURATED, i+1);
69  int nsaturated = cpl_propertylist_get_int(muse_imagelist_get(aList, i)->header,
70  MUSE_HDR_TMP_NSAT);
71  cpl_propertylist_update_int(aImage->header, keyword, nsaturated);
72  cpl_free(keyword);
73  } /* for i (all images in list) */
74 
75  const double *ron = cpl_bivector_get_x_data_const(aRON),
76  *ronerr = cpl_bivector_get_y_data_const(aRON);
77  unsigned char n;
78  for (n = 1; n <= 4; n++) {
79  /* get the image window for this quadrant */
80  cpl_size *window = muse_quadrants_get_window(aImage, n);
81 
82  /* save read-out noise and its error in the respective FITS keywords */
83  char keyword[KEYWORD_LENGTH];
84  snprintf(keyword, KEYWORD_LENGTH, QC_BIAS_MASTER_RON, n);
85  cpl_propertylist_append_float(aImage->header, keyword, ron[n-1]);
86 
87  snprintf(keyword, KEYWORD_LENGTH, QC_BIAS_MASTER_RONERR, n);
88  cpl_propertylist_append_float(aImage->header, keyword, ronerr[n-1]);
89 
90  snprintf(keyword, KEYWORD_LENGTH, QC_BIAS_MASTERn_PREFIX, n);
91  unsigned stats = CPL_STATS_MEDIAN | CPL_STATS_MEAN | CPL_STATS_STDEV
92  | CPL_STATS_MIN | CPL_STATS_MAX;
94  keyword, stats, window[0],
95  window[2], window[1], window[3]);
96 
97  /* add slopes for the quadrant */
98  cpl_vector *slopes = muse_cplimage_slope_window(aImage->data, window);
99  snprintf(keyword, KEYWORD_LENGTH, QC_BIAS_MASTER_SLOPEX, n);
100  cpl_propertylist_append_float(aImage->header, keyword,
101  cpl_vector_get(slopes, 0));
102  snprintf(keyword, KEYWORD_LENGTH, QC_BIAS_MASTER_SLOPEY, n);
103  cpl_propertylist_append_float(aImage->header, keyword,
104  cpl_vector_get(slopes, 1));
105  cpl_vector_delete(slopes);
106 
107  cpl_free(window);
108  } /* for n (quadrants) */
109 } /* muse_bias_qc_header() */
110 
111 /*---------------------------------------------------------------------------*/
121 /*---------------------------------------------------------------------------*/
122 int
123 muse_bias_compute(muse_processing *aProcessing, muse_bias_params_t *aParams)
124 {
126  "muse.muse_bias");
127  muse_imagelist *images = muse_basicproc_load(aProcessing, aParams->nifu, bpars);
129  cpl_ensure(images, cpl_error_get_code(), -1);
130 
131  cpl_bivector *bron = muse_imagelist_compute_ron(images,
132  RON_HALFSIZE,
133  RON_NSAMPLES);
134 
135  muse_combinepar *cpars = muse_combinepar_new(aProcessing->parameters,
136  "muse.muse_bias");
137  muse_image *masterimage = muse_combine_images(cpars, images);
138  muse_combinepar_delete(cpars);
139  if (!masterimage) {
140  cpl_msg_error(__func__, "Combining input frames failed!");
141  muse_imagelist_delete(images);
142  cpl_bivector_delete(bron);
143  return -1;
144  }
145  cpl_propertylist_erase_regexp(masterimage->header, MUSE_WCS_KEYS, 0);
146 
147  muse_bias_qc_header(masterimage, bron, images);
148  muse_imagelist_delete(images);
149  cpl_bivector_delete(bron);
150 
151  int nbad = muse_quality_bad_columns(masterimage, aParams->losigmabadpix,
152  aParams->hisigmabadpix);
153  cpl_propertylist_append_int(masterimage->header, QC_BIAS_MASTER_NBADPIX,
154  nbad);
155 
156  muse_basicproc_qc_saturated(masterimage, QC_BIAS_MASTER_PREFIX);
157  int rc = muse_processing_save_image(aProcessing, aParams->nifu,
158  masterimage, MUSE_TAG_MASTER_BIAS);
159  muse_image_delete(masterimage);
160  return rc == CPL_ERROR_NONE ? 0 : -1;
161 } /* 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:84
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:47
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:742
Structure definition of MUSE three extension FITS file.
Definition: muse_image.h:41
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:73
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:716
double losigmabadpix
Low sigma to find dark columns in the combined bias.
Definition: muse_bias_z.h:85
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:673
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.
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.