41 #include "gravi_utils.h"
42 #include "gravi_pfits.h"
43 #include "gravi_dfs.h"
44 #include "gravi_calib.h"
46 #include "gravi_data.h"
52 static int gravi_all_dark_create(cpl_plugin *);
53 static int gravi_all_dark_exec(cpl_plugin *);
54 static int gravi_all_dark_destroy(cpl_plugin *);
55 static int gravi_all_dark(cpl_frameset *,
const cpl_parameterlist *);
61 static char gravi_all_dark_description[] =
62 "This recipe is used to compute the median of a raw dark set of integration.\n"
63 "The median of each pixel is computed along the time axis for the FT and SC channel.\n"
64 "The master dark frame is created from these two median images.\n"
65 "Description DO category\n"
67 "Raw dark file (one or more) " GRAVI_DARK
"\n"
69 "Master dark (one per input file) " DARK
"\n"
70 "Bad pixel map (one per input file) " BAD
"\n"
88 int cpl_plugin_get_info(cpl_pluginlist * list)
90 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
91 cpl_plugin * plugin = &recipe->interface;
93 if (cpl_plugin_init(plugin,
96 CPL_PLUGIN_TYPE_RECIPE,
98 "This recipe is used to compute the median of a raw dark set of integration.",
99 gravi_all_dark_description,
100 "Firstname Lastname",
103 gravi_all_dark_create,
105 gravi_all_dark_destroy)) {
106 cpl_msg_error(cpl_func,
"Plugin initialization failed");
107 (void)cpl_error_set_where(cpl_func);
111 if (cpl_pluginlist_append(list, plugin)) {
112 cpl_msg_error(cpl_func,
"Error adding plugin to list");
113 (void)cpl_error_set_where(cpl_func);
129 static int gravi_all_dark_create(cpl_plugin * plugin)
135 if (cpl_error_get_code() != CPL_ERROR_NONE) {
136 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
137 cpl_func, __LINE__, cpl_error_get_where());
138 return (
int)cpl_error_get_code();
141 if (plugin == NULL) {
142 cpl_msg_error(cpl_func,
"Null plugin");
143 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
147 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
148 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
149 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
153 recipe = (cpl_recipe *)plugin;
156 recipe->parameters = cpl_parameterlist_new();
157 if (recipe->parameters == NULL) {
158 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
159 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
164 p = cpl_parameter_new_value(
"gravi."
165 "flat_param.Bad_dark_threshold", CPL_TYPE_INT,
"the rms factor for "
166 "dark bad pixel threshold",
"gravi.gravi_all_flat", 5);
167 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"Bad_dark_threshold");
168 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
169 cpl_parameterlist_append(recipe->parameters, p);
181 static int gravi_all_dark_exec(cpl_plugin * plugin)
186 cpl_errorstate initial_errorstate = cpl_errorstate_get();
189 if (cpl_error_get_code() != CPL_ERROR_NONE) {
190 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
191 cpl_func, __LINE__, cpl_error_get_where());
192 return (
int)cpl_error_get_code();
195 if (plugin == NULL) {
196 cpl_msg_error(cpl_func,
"Null plugin");
197 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
201 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
202 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
203 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
207 recipe = (cpl_recipe *)plugin;
210 if (recipe->parameters == NULL) {
211 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
212 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
214 if (recipe->frames == NULL) {
215 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
216 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
220 recipe_status = gravi_all_dark(recipe->frames, recipe->parameters);
223 if (cpl_dfs_update_product_header(recipe->frames)) {
224 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
227 if (!cpl_errorstate_is_equal(initial_errorstate)) {
230 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
233 return recipe_status;
243 static int gravi_all_dark_destroy(cpl_plugin * plugin)
247 if (plugin == NULL) {
248 cpl_msg_error(cpl_func,
"Null plugin");
249 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
253 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
254 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
255 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
259 recipe = (cpl_recipe *)plugin;
261 cpl_parameterlist_delete(recipe->parameters);
275 static int gravi_all_dark(cpl_frameset * frameset,
276 const cpl_parameterlist * parlist)
278 cpl_frameset * dark_frameset = NULL, * usedframes;
280 cpl_propertylist * applist, * primary_hdr;
283 gravi_data * raw_dark, * reduced_dark, * bad_map;
289 cpl_ensure_code(gravi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
290 cpl_error_get_code()) ;
293 dark_frameset = gravi_frameset_extract_dark(frameset);
294 if (cpl_frameset_is_empty(dark_frameset)) {
295 cpl_frameset_delete(dark_frameset);
298 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_INVALID_TYPE,
299 "No dark frame in the frameset") ;
303 nb_frame = cpl_frameset_get_size(dark_frameset);
305 for (comp = 0; comp < nb_frame; comp++){
308 frame = cpl_frameset_get_position(dark_frameset, comp);
310 input = cpl_frame_get_filename(frame) ;
311 cpl_msg_info (NULL,
"This file %s is a raw dark file", input);
312 output = cpl_sprintf(
"gravi_all_dark_0%d.fits", comp + 1);
315 cpl_frameset_delete(dark_frameset);
318 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_NULL_INPUT,
319 "Input file does not exist") ;
323 raw_dark = gravi_data_load(input);
325 primary_hdr = gravi_data_get_propertylist(raw_dark,
326 GRAVI_PRIMARY_HDR_NAME_EXT);
327 shutter = gravi_shutters_check(primary_hdr);
329 if ((shutter [0] != 0) || (shutter [1] != 0) ||
330 (shutter [2] != 0) || (shutter [3] != 0)){
331 cpl_msg_info (NULL,
"The shutters of this file are not all close");
339 reduced_dark = gravi_compute_dark( raw_dark );
341 if (cpl_error_get_code()) {
342 gravi_data_delete(reduced_dark);
343 gravi_data_delete(raw_dark);
344 cpl_frameset_delete(dark_frameset);
347 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_OUTPUT,
348 "Error while computing the master dark");
351 primary_hdr = gravi_data_get_propertylist(reduced_dark,
352 GRAVI_PRIMARY_HDR_NAME_EXT);
358 usedframes = cpl_frameset_new();
359 cpl_frameset_insert(usedframes, cpl_frame_duplicate(frame));
360 applist = gravi_propertylist_get_qc (primary_hdr);
363 cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG, DARK);
366 if (gravi_data_save(reduced_dark, frameset, output, parlist,
367 usedframes, frame,
"gravi_all_dark", applist)
369 gravi_data_delete(reduced_dark);
370 gravi_data_delete(raw_dark);
371 cpl_frameset_delete(dark_frameset);
372 cpl_frameset_delete(usedframes);
373 cpl_propertylist_delete(applist);
375 return (
int) cpl_error_set_message(cpl_func,
376 CPL_ERROR_ILLEGAL_OUTPUT,
"Could not save the reduced_dark"
377 " on the output file");
380 gravi_data_delete(reduced_dark);
381 gravi_data_delete(raw_dark);
382 cpl_frameset_delete(usedframes);
383 cpl_propertylist_delete(applist);
389 cpl_frameset_delete(dark_frameset);
390 return (
int)cpl_error_get_code();