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"
87 int cpl_plugin_get_info(cpl_pluginlist * list)
89 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
90 cpl_plugin * plugin = &recipe->interface;
92 if (cpl_plugin_init(plugin,
95 CPL_PLUGIN_TYPE_RECIPE,
97 "This recipe is used to compute the median of a raw dark.",
98 gravi_all_dark_description,
102 gravi_all_dark_create,
104 gravi_all_dark_destroy)) {
105 cpl_msg_error(cpl_func,
"Plugin initialization failed");
106 (void)cpl_error_set_where(cpl_func);
110 if (cpl_pluginlist_append(list, plugin)) {
111 cpl_msg_error(cpl_func,
"Error adding plugin to list");
112 (void)cpl_error_set_where(cpl_func);
128 static int gravi_all_dark_create(cpl_plugin * plugin)
134 if (cpl_error_get_code() != CPL_ERROR_NONE) {
135 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
136 cpl_func, __LINE__, cpl_error_get_where());
137 return (
int)cpl_error_get_code();
140 if (plugin == NULL) {
141 cpl_msg_error(cpl_func,
"Null plugin");
142 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
146 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
147 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
148 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
152 recipe = (cpl_recipe *)plugin;
155 recipe->parameters = cpl_parameterlist_new();
156 if (recipe->parameters == NULL) {
157 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
158 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
171 static int gravi_all_dark_exec(cpl_plugin * plugin)
176 cpl_errorstate initial_errorstate = cpl_errorstate_get();
179 if (cpl_error_get_code() != CPL_ERROR_NONE) {
180 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
181 cpl_func, __LINE__, cpl_error_get_where());
182 return (
int)cpl_error_get_code();
185 if (plugin == NULL) {
186 cpl_msg_error(cpl_func,
"Null plugin");
187 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
191 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
192 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
193 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
197 recipe = (cpl_recipe *)plugin;
200 if (recipe->parameters == NULL) {
201 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
202 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
204 if (recipe->frames == NULL) {
205 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
206 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
210 recipe_status = gravi_all_dark(recipe->frames, recipe->parameters);
213 if (cpl_dfs_update_product_header(recipe->frames)) {
214 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
217 if (!cpl_errorstate_is_equal(initial_errorstate)) {
220 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
223 return recipe_status;
233 static int gravi_all_dark_destroy(cpl_plugin * plugin)
237 if (plugin == NULL) {
238 cpl_msg_error(cpl_func,
"Null plugin");
239 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
243 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
244 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
245 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
249 recipe = (cpl_recipe *)plugin;
251 cpl_parameterlist_delete(recipe->parameters);
265 static int gravi_all_dark(cpl_frameset * frameset,
266 const cpl_parameterlist * parlist)
268 cpl_frameset * dark_frameset = NULL, * usedframes;
270 cpl_propertylist * applist, * primary_hdr;
271 const char * input, * filename, * temp;
273 gravi_data * raw_dark, * reduced_dark, * bad_map;
279 cpl_msg_set_time_on();
280 cpl_msg_set_component_on();
281 cpl_msg_info(cpl_func,
"Start function");
284 cpl_ensure_code(gravi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
285 cpl_error_get_code()) ;
288 dark_frameset = gravi_frameset_extract_dark(frameset);
290 if (cpl_frameset_is_empty(dark_frameset)) {
291 cpl_frameset_delete(dark_frameset);
294 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
295 "No dark frame in the frameset") ;
299 nb_frame = cpl_frameset_get_size(dark_frameset);
301 for (comp = 0; comp < nb_frame; comp++){
304 frame = cpl_frameset_get_position(dark_frameset, comp);
306 input = cpl_frame_get_filename(frame) ;
308 cpl_frameset_delete(dark_frameset);
311 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_NULL_INPUT,
312 "Input file does not exist") ;
314 temp = strrchr(input,
'/');
315 filename = temp ? temp + 1 : input;
316 cpl_msg_info (cpl_func,
"This file %s is a raw dark file", filename);
319 output = gravi_data_product_name (filename,
"dark");
322 raw_dark = gravi_data_load(input);
324 primary_hdr = gravi_data_get_propertylist(raw_dark,
325 GRAVI_PRIMARY_HDR_NAME_EXT);
326 shutter = gravi_shutters_check(primary_hdr);
328 if (cpl_error_get_code()) {
329 gravi_data_delete(raw_dark);
330 cpl_frameset_delete(dark_frameset);
333 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
334 "Error while getting the shutter values");
337 if ((shutter [0] != 0) || (shutter [1] != 0) ||
338 (shutter [2] != 0) || (shutter [3] != 0)){
339 cpl_msg_info (cpl_func,
"The shutters of this file are not all close");
347 reduced_dark = gravi_compute_dark( raw_dark );
349 if (cpl_error_get_code()) {
350 gravi_data_delete(reduced_dark);
351 gravi_data_delete(raw_dark);
352 cpl_frameset_delete(dark_frameset);
355 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_OUTPUT,
356 "Error while computing the master dark");
359 primary_hdr = gravi_data_get_propertylist(reduced_dark,
360 GRAVI_PRIMARY_HDR_NAME_EXT);
366 usedframes = cpl_frameset_new();
367 cpl_frameset_insert(usedframes, cpl_frame_duplicate(frame));
368 applist = gravi_propertylist_get_qc (primary_hdr);
371 cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG, DARK);
374 if (gravi_data_save(reduced_dark, frameset, output, parlist,
375 usedframes, frame,
"gravi_all_dark", applist)
377 gravi_data_delete(reduced_dark);
378 gravi_data_delete(raw_dark);
379 cpl_frameset_delete(dark_frameset);
380 cpl_frameset_delete(usedframes);
381 cpl_propertylist_delete(applist);
383 return (
int) cpl_error_set_message(cpl_func,
384 CPL_ERROR_ILLEGAL_OUTPUT,
"Could not save the reduced_dark"
385 " on the output file");
388 gravi_data_delete(reduced_dark);
389 gravi_data_delete(raw_dark);
390 cpl_frameset_delete(usedframes);
391 cpl_propertylist_delete(applist);
397 cpl_frameset_delete(dark_frameset);
398 return (
int)cpl_error_get_code();