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_dual_baseline_create(cpl_plugin *);
53 static int gravi_dual_baseline_exec(cpl_plugin *);
54 static int gravi_dual_baseline_destroy(cpl_plugin *);
55 static int gravi_dual_baseline(cpl_frameset *,
const cpl_parameterlist *);
61 static char gravi_dual_baseline_description[] =
62 "This recipe computes the narrow angle baseline from the calibrated visibilities. "
63 "It is used in dual field mode\n"
64 "Description DO category\n"
66 "Raw visibility file (one or more) " VIS_DUAL_CALIB
"\n"
68 "Narow Angle Baseline (one per input file) " NAB_CAL
"\n"
86 int cpl_plugin_get_info(cpl_pluginlist * list)
88 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
89 cpl_plugin * plugin = &recipe->interface;
91 if (cpl_plugin_init(plugin,
94 CPL_PLUGIN_TYPE_RECIPE,
95 "gravi_dual_baseline",
96 "This recipe computes the narrow angle baseline from the calibrated visibilitie.",
97 gravi_dual_baseline_description,
101 gravi_dual_baseline_create,
102 gravi_dual_baseline_exec,
103 gravi_dual_baseline_destroy)) {
104 cpl_msg_error(cpl_func,
"Plugin initialization failed");
105 (void)cpl_error_set_where(cpl_func);
109 if (cpl_pluginlist_append(list, plugin)) {
110 cpl_msg_error(cpl_func,
"Error adding plugin to list");
111 (void)cpl_error_set_where(cpl_func);
127 static int gravi_dual_baseline_create(cpl_plugin * plugin)
132 if (cpl_error_get_code() != CPL_ERROR_NONE) {
133 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
134 cpl_func, __LINE__, cpl_error_get_where());
135 return (
int)cpl_error_get_code();
138 if (plugin == NULL) {
139 cpl_msg_error(cpl_func,
"Null plugin");
140 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
144 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
145 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
146 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
150 recipe = (cpl_recipe *)plugin;
153 recipe->parameters = cpl_parameterlist_new();
154 if (recipe->parameters == NULL) {
155 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
156 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
171 static int gravi_dual_baseline_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_dual_baseline(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_dual_baseline_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_dual_baseline(cpl_frameset * frameset,
266 const cpl_parameterlist * parlist)
268 cpl_frameset * baseline_frameset = NULL, * usedframes;
270 cpl_propertylist * applist, * primary_hdr;
273 gravi_data * raw_data, * baseline_data;
278 cpl_ensure_code(gravi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
279 cpl_error_get_code()) ;
282 baseline_frameset = gravi_frameset_extract_vis_calib(frameset);
283 if (cpl_frameset_is_empty(baseline_frameset)) {
284 cpl_frameset_delete(baseline_frameset);
287 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_INVALID_TYPE,
288 "No dark frame in the frameset") ;
292 nb_frame = cpl_frameset_get_size(baseline_frameset);
295 for (comp = 0; comp < nb_frame; comp++){
296 frame = cpl_frameset_get_position(baseline_frameset, comp);
298 input = cpl_frame_get_filename(frame) ;
299 output = cpl_sprintf(
"gravi_dual_baseline_0%d.fits", comp + 1);
302 cpl_frameset_delete(baseline_frameset);
305 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_NULL_INPUT,
306 "Input file does not exist") ;
310 raw_data = gravi_data_load(input);
312 baseline_data = gravi_compute_baseline ( raw_data );
314 if (cpl_error_get_code()) {
315 gravi_data_delete(baseline_data);
316 gravi_data_delete(raw_data);
317 cpl_frameset_delete(baseline_frameset);
320 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_OUTPUT,
321 "Error while computing the master dark");
324 primary_hdr = gravi_data_get_propertylist(baseline_data,
325 GRAVI_PRIMARY_HDR_NAME_EXT);
331 usedframes = cpl_frameset_new();
332 cpl_frameset_insert(usedframes, cpl_frame_duplicate(frame));
333 applist = gravi_propertylist_get_qc (primary_hdr);
336 cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG, NAB_CAL);
339 if (gravi_data_save(baseline_data, frameset, output, parlist,
340 usedframes, frame,
"gravi_dual_baseline", applist)
342 gravi_data_delete(baseline_data);
343 gravi_data_delete(raw_data);
344 cpl_frameset_delete(baseline_frameset);
345 cpl_frameset_delete(usedframes);
346 cpl_propertylist_delete(applist);
348 return (
int) cpl_error_set_message(cpl_func,
349 CPL_ERROR_ILLEGAL_OUTPUT,
"Could not save the reduced baseline"
350 " on the output file");
353 gravi_data_delete(baseline_data);
354 gravi_data_delete(raw_data);
355 cpl_frameset_delete(usedframes);
356 cpl_propertylist_delete(applist);
362 cpl_frameset_delete(baseline_frameset);
363 return (
int)cpl_error_get_code();