ERIS Pipeline Reference Manual 1.8.15
eris_ifu_flat.c
1/* $Id: eris_ifu_flat.c,v 0.1 2018-07-22 06:06:06 agudo Exp $
2 *
3 * This file is part of the ERIS Pipeline
4 * Copyright (C) 2002,2003 European Southern Observatory
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20/*
21 * $Author: agudo $
22 * $Date: 2018-07-22 06:06:06 $
23 * $Revision: 0.1 $
24 * $Name: not supported by cvs2svn $
25 */
26
27#ifdef HAVE_CONFIG_H
28#include <config.h> /* allows the program compilation */
29#endif
30
31/*-----------------------------------------------------------------------------
32 Includes
33 ----------------------------------------------------------------------------*/
34#include <string.h>
35
36#include <cpl.h>
37#include <hdrl.h>
38
39#include "eris_utils.h"
40#include "eris_pfits.h"
41#include "eris_ifu_dfs.h"
42#include "eris_ifu_utils.h"
43#include "eris_ifu_functions.h"
44#include "eris_ifu_error.h"
45#include "eris_ifu_debug.h"
46#include "eris_ifu_flat_static.h"
47
48/*-----------------------------------------------------------------------------
49 Static variables
50 ----------------------------------------------------------------------------*/
51static const char eris_ifu_flat_description[] = "\
52This recipe performs ERIS/SPIFFIER flat field frames data reduction.\n\
53\n\
54Optionally one may have in input also several bad pixel maps to be coadded.\n\
55\n\
56-----------------------------------------------------------------------------\n\
57 Input files:\n\
58 DO CATG Explanation Required #Frames\n\
59 ------- ----------- -------- -------\n\
60 FLAT_LAMP Flatlamp exposures Y 2-n \n\
61 (at least 3 frames recommended) \n\
62 BPM_DARK Bad pixel mask from eris_ifu_dark Y 1 \n\
63 BPM_DETLIN Bad pixel mask from eris_ifu_detlin N [0,1] \n\
64 BPM_DIST Bad pixel mask from eris_ifu_distortion N [0,1] \n\
65\n\
66 Output files:\n\
67 DO CATG Explanation\n\
68 ------- -----------\n\
69 MASTER_FLAT Normalised flat field\n\
70 BPM_FLAT Updated bad pixel mask\n\
71-----------------------------------------------------------------------------\n\
72 Parameters:\n\
73 --mode=fast Simple subtraction of on- and off-frames and collapsing\n\
74 --collapse* parameters apply as well\n\
75 --mode=hdrl The HDRL-way to create a masterflat (not well suited to\n\
76 spectroscopic raw data)\n\
77 --collapse* + --flat* parameters apply as well\n\
78 --mode=segment The raw data is segmented vertically and inspected individually\n\
79\n\
80Information on relevant parameters may be found with\n\
81 esorex --params "REC_NAME_FLAT"\n\
82 esorex --help "REC_NAME_FLAT"\n";
83
84/*-----------------------------------------------------------------------------
85 Private function prototypes
86 -----------------------------------------------------------------------------*/
87cpl_recipe_define(eris_ifu_flat, ERIS_BINARY_VERSION, "Alex Agudo Berbel",
88 PACKAGE_BUGREPORT, "2018",
89 "This recipe performs flat field data reduction",
90 eris_ifu_flat_description);
91
92/*-----------------------------------------------------------------------------
93 Functions code
94 ----------------------------------------------------------------------------*/
95
96/*----------------------------------------------------------------------------*/
104/*----------------------------------------------------------------------------*/
105static cpl_error_code eris_ifu_flat_fill_parameterlist(cpl_parameterlist *pl)
106{
107 cpl_error_code err = CPL_ERROR_NONE;
108// cpl_parameter *p = NULL;
109
110 cpl_ensure_code(pl, CPL_ERROR_NULL_INPUT);
111
112 TRY
113 {
114 /* --instrument & --product_depth*/
116 eris_parlist_config_add_all_recipes(pl, REC_NAME_FLAT));
117
118 /* add common bpm-parameters */
120 eris_parlist_config_add_bpm(pl, REC_NAME_FLAT));
121
122 /* add common flat-parameters */
124 eris_parlist_config_add_flat(pl, REC_NAME_FLAT));
125 }
126 CATCH
127 {
128 CATCH_MSGS();
129 err = cpl_error_get_code();
130 }
131
132 return err;
133}
134
135/*----------------------------------------------------------------------------*/
142/*----------------------------------------------------------------------------*/
143static int eris_ifu_flat(cpl_frameset *frameset,
144 const cpl_parameterlist *parlist)
145{
146 cpl_propertylist *qc_list = NULL;
147 hdrl_image *masterFlatHdrlImg_lo = NULL;
148 cpl_image *qualityImage = NULL,
149 *masterFlatImage = NULL,
150 *errorImage = NULL,
151 *bp_img_flat = NULL;
152 const cpl_mask *bp_mask_flat = NULL;
153 int productDepth = 0,
154 nrfr = 0;
155 const char *instrument = NULL;
156
157 cpl_ensure_code(frameset, CPL_ERROR_NULL_INPUT);
158 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
159
160
161 /* check for invalid input */
162 if(eris_files_dont_exist(frameset) != CPL_ERROR_NONE) {
163 return CPL_ERROR_BAD_FILE_FORMAT;
164 }
165 TRY
166 {
167 /* --instrument */
169 instrument = cpl_parameter_get_string(
170 cpl_parameterlist_find_const(parlist,
171 "eris.eris_ifu_flat.instrument")));
172 cpl_msg_info(cpl_func, "Processing %s frames", instrument);
173
174 /* Identify the RAW and CALIB frames in the input frameset */
176 eris_ifu_dfs_set_groups(frameset));
177
178 /* check number of frames*/
179 nrfr = cpl_frameset_count_tags(frameset, ERIS_IFU_PRO_DARK_BPM);
180 ASSURE(nrfr == 1,
181 CPL_ERROR_ILLEGAL_INPUT,
182 "Exactly 1 "ERIS_IFU_PRO_DARK_BPM" frame must be provided.");
183
184 nrfr = cpl_frameset_count_tags(frameset, ERIS_IFU_PRO_DIST_BPM);
185 ASSURE((nrfr == 0) || (nrfr == 1),
186 CPL_ERROR_ILLEGAL_INPUT,
187 "Either 0 or 1 "ERIS_IFU_PRO_DIST_BPM" frame must be provided.");
188
189 nrfr = cpl_frameset_count_tags(frameset, ERIS_IFU_PRO_DETLIN_BPM);
190 ASSURE((nrfr == 0) || (nrfr == 1),
191 CPL_ERROR_ILLEGAL_INPUT,
192 "Either 0 or 1 "ERIS_IFU_PRO_DETLIN_BPM" frame must be provided.");
193
194 /* save additional products if requested via productDepth argument */
195 productDepth = cpl_parameter_get_int(
196 cpl_parameterlist_find_const(parlist,
197 "eris.eris_ifu_flat.product_depth"));
198
199 // get flat-mode
200 const char * tmp = cpl_parameter_get_string(cpl_parameterlist_find_const(parlist,
201 "eris.eris_ifu_flat.mode"));
202 flatMode mode = FLAT_MODE_NONE;
203 if (!strcmp(tmp, flatModes[0])) {
204 mode = FLAT_MODE_SEGMENT;
205 } else if (!strcmp(tmp, flatModes[1])) {
206 mode = FLAT_MODE_HDRL;
207 } else if (!strcmp(tmp, flatModes[2])) {
208 mode = FLAT_MODE_FAST;
209 } else {
210 BRK_WITH_ERROR_MSG(CPL_ERROR_ILLEGAL_INPUT, "Wrong mode provided.");
211 }
213
214 /* Performing flat calculation */
216 eris_ifu_flat_static(frameset,
217 parlist,
218 mode,
219 productDepth,
220 ERIS_IFU_PRO_FLAT,
221 REC_NAME_FLAT,
222 "FLAT",
223 instrument,
224 &qc_list,
225 &masterFlatHdrlImg_lo,
226 &qualityImage));
227
228 /* fetch master flat image */
230 masterFlatImage = hdrl_image_get_image(masterFlatHdrlImg_lo));
231 /* fetch noise image */
233 errorImage = hdrl_image_get_error(masterFlatHdrlImg_lo));
234
235 /* save masterflat image */
237 cpl_propertylist_update_string(qc_list, CPL_DFS_PRO_CATG, ERIS_IFU_PRO_FLAT));
238 //eris_ifu_get_badpix_qc_from_ima(hdrl_image_get_image(masterFlatHdrlImg_lo), qc_list, "FLAT");
240 eris_ifu_save_deq_image(frameset, NULL, parlist, frameset, NULL,
241 REC_NAME_FLAT,
242 qc_list, NULL,
243 ERIS_IFU_PRO_FLAT_FN,
244 masterFlatImage, errorImage, rmse,
245 qualityImage, flag16bit, UNITLESS));
246
247 /* save bpm image */
249 bp_mask_flat = hdrl_image_get_mask_const(masterFlatHdrlImg_lo));
251 bp_img_flat = cpl_image_new_from_mask(bp_mask_flat));
253 eris_ifu_save_image(frameset, qc_list, parlist, REC_NAME_FLAT,
254 ERIS_IFU_PRO_FLAT_BPM,
255 ERIS_IFU_PRO_FLAT_BPM_FN,
256 CPL_TYPE_USHORT,
257 bp_img_flat));
258 }
259 CATCH
260 {
261 CATCH_MSGS();
262 }
263
265 eris_ifu_free_hdrl_image(&masterFlatHdrlImg_lo);
266 eris_ifu_free_image(&qualityImage);
267 eris_ifu_free_image(&bp_img_flat);
268
269 if (cpl_memory_is_empty() == 0) {
270 cpl_memory_dump();
271 }
272
273 return (int) cpl_error_get_code();
274}
cpl_error_code eris_ifu_dfs_set_groups(cpl_frameset *self)
Set the frame group (RAW, CALIB, or PRODUCT) for all frames in a frameset.
Definition: eris_ifu_dfs.c:89
cpl_error_code eris_ifu_save_deq_image(cpl_frameset *allframes, cpl_propertylist *header, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const cpl_frame *inherit, const char *recipe, const cpl_propertylist *applist, const char *remregexp, const char *filename, const cpl_image *image, const cpl_image *error, deqErrorType errorType, const cpl_image *dataQualityMask, deqQualityType dataQualityType, const char *unit)
Save a DFS-compliant image product with data, error, and quality extensions.
Definition: eris_ifu_dfs.c:367
#define ASSURE(condition, error,...)
error handling macro (from fors-pipeline)
#define BRK_IF_ERROR(function)
If function is or returns an error != CPL_ERROR_NONE, then the try-block is exited.
#define CHECK_ERROR_STATE(void)
Check the CPL error state, and exit the try-block if not CPL_ERROR_NONE.
#define BRK_WITH_ERROR_MSG(code,...)
Set a new CPL error, and exit the try-block.
#define TRY
Beginning of a TRY-block.
#define CATCH
End of a TRY-block, beginning of a CATCH-block.
#define BRK_IF_NULL(function)
If function is or returns a NULL pointer, then the try-block is exited.
#define CATCH_MSGS()
Displays an error message stack.
cpl_error_code eris_parlist_config_add_flat(cpl_parameterlist *pl, const char *recname)
Add flat field configuration parameters to parameter list.
cpl_error_code eris_parlist_config_add_all_recipes(cpl_parameterlist *pl, const char *recname)
Add common configuration parameters for all recipes.
cpl_error_code eris_parlist_config_add_bpm(cpl_parameterlist *pl, const char *recname)
Add bad pixel mask configuration parameters to parameter list.
void eris_ifu_free_propertylist(cpl_propertylist **item)
Free memory and set pointer to null.
void eris_ifu_free_hdrl_image(hdrl_image **item)
Free memory and set pointer to null.
void eris_ifu_free_image(cpl_image **item)
Free memory and set pointer to null.
cpl_error_code eris_ifu_save_image(cpl_frameset *fs, const cpl_propertylist *plist, const cpl_parameterlist *parlist, const char *recipe, const char *procatg, const char *filename, cpl_type type, const cpl_image *image)
Save image with DFS compliance.
cpl_error_code eris_files_dont_exist(cpl_frameset *frameset)
Check if all SOF files exist.
Definition: eris_utils.c:867
cpl_image * hdrl_image_get_error(hdrl_image *himg)
get error as cpl image
Definition: hdrl_image.c:131
const cpl_mask * hdrl_image_get_mask_const(const hdrl_image *himg)
get cpl bad pixel mask from image
Definition: hdrl_image.c:175
cpl_image * hdrl_image_get_image(hdrl_image *himg)
get data as cpl image
Definition: hdrl_image.c:105