39 #include "gravi_utils.h" 40 #include "gravi_pfits.h" 41 #include "gravi_dfs.h" 42 #include "gravi_image.h" 48 static int gravity_image_create(cpl_plugin *);
49 static int gravity_image_exec(cpl_plugin *);
50 static int gravity_image_destroy(cpl_plugin *);
51 static int gravity_image(cpl_frameset *,
const cpl_parameterlist *);
56 static char gravity_image_short[] = GRAVI_UNOFFERED
"Reconstruct an image from visibilities.";
57 static char gravity_image_description[] = GRAVI_UNOFFERED
58 "This recipe launch a Yorick batch executing mira-script.i to process an\n" 59 "input OIFITS file to produce an image in fits file.\n" 60 "The input frame is an OIFITS file :\n" 61 "GRAVI-GRAVI_MIRA-input-file.oifits " GRAVI_MIRA_INPUT_PROCATG
"\n" 62 "and the recipe generates a fits image\n" 63 "GRAVI-GRAVI_MIRA-image-file.fits " GRAVI_MIRA_OUTPUT_PROCATG
"\n";
80 int cpl_plugin_get_info(cpl_pluginlist * list)
82 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
83 cpl_plugin * plugin = &recipe->interface;
85 if (cpl_plugin_init(plugin,
88 CPL_PLUGIN_TYPE_RECIPE,
91 gravity_image_description,
97 gravity_image_destroy)) {
98 cpl_msg_error(cpl_func,
"Plugin initialization failed");
99 (void)cpl_error_set_where(cpl_func);
103 if (cpl_pluginlist_append(list, plugin)) {
104 cpl_msg_error(cpl_func,
"Error adding plugin to list");
105 (void)cpl_error_set_where(cpl_func);
121 static int gravity_image_create(cpl_plugin * plugin)
127 if (cpl_error_get_code() != CPL_ERROR_NONE) {
128 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
129 cpl_func, __LINE__, cpl_error_get_where());
130 return (
int)cpl_error_get_code();
133 if (plugin == NULL) {
134 cpl_msg_error(cpl_func,
"Null plugin");
135 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
139 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
140 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
141 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
145 recipe = (cpl_recipe *)plugin;
148 recipe->parameters = cpl_parameterlist_new();
149 if (recipe->parameters == NULL) {
150 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
151 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
154 gravi_parameter_add_image (recipe->parameters);
166 static int gravity_image_exec(cpl_plugin * plugin)
171 cpl_errorstate initial_errorstate = cpl_errorstate_get();
174 if (cpl_error_get_code() != CPL_ERROR_NONE) {
175 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
176 cpl_func, __LINE__, cpl_error_get_where());
177 return (
int)cpl_error_get_code();
180 if (plugin == NULL) {
181 cpl_msg_error(cpl_func,
"Null plugin");
182 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
186 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
187 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
188 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
192 recipe = (cpl_recipe *)plugin;
195 if (recipe->parameters == NULL) {
196 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
197 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
199 if (recipe->frames == NULL) {
200 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
201 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
205 recipe_status = gravity_image(recipe->frames, recipe->parameters);
208 if (cpl_dfs_update_product_header(recipe->frames)) {
209 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
212 if (!cpl_errorstate_is_equal(initial_errorstate)) {
215 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
218 return recipe_status;
228 static int gravity_image_destroy(cpl_plugin * plugin)
232 if (plugin == NULL) {
233 cpl_msg_error(cpl_func,
"Null plugin");
234 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
238 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
239 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
240 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
244 recipe = (cpl_recipe *)plugin;
246 cpl_parameterlist_delete(recipe->parameters);
260 static int gravity_image(cpl_frameset * frameset,
261 const cpl_parameterlist * parlist)
264 const cpl_frame * rawframe;
265 double qc_param = 0.0;
266 cpl_propertylist * applist;
268 cpl_frameset * usedframes;
272 cpl_errorstate prestate = cpl_errorstate_get();
276 if (!cpl_errorstate_is_equal(prestate)) {
277 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
278 "Could not retrieve the input " 284 cpl_error_get_code());
288 rawframe = cpl_frameset_find_const(frameset, GRAVI_MIRA_INPUT_PROCATG);
289 if (rawframe == NULL) {
292 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
293 "SOF does not have any file tagged " 294 "with %s", GRAVI_MIRA_INPUT_PROCATG);
299 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
301 cpl_msg_set_component_on();
304 const char * filename = cpl_frame_get_filename(rawframe);
307 int n_target = cpl_table_get_nrow(oi_target_table);
310 for (
int i_target = 0; i_target < n_target; i_target++) {
314 const char *target_name = cpl_table_get_string(oi_target_table,
"TARGET", i_target);
315 image = gravi_image(rawframe, parlist, target_name);
320 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_NULL_INPUT,
321 "The gravi_image function return NULL pointer");
324 applist = cpl_propertylist_new();
328 sprintf(proCatg,
"%s", GRAVI_MIRA_OUTPUT_PROCATG);
329 cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG, proCatg);
333 for(i = 0; proCatg[i]; i++)
if (proCatg[i]!=
'_') { catg_ext[j] = tolower(proCatg[i]); j++; }
337 cpl_propertylist_append_double(applist,
"ESO QC QCPARAM", qc_param);
343 usedframes = cpl_frameset_new();
344 cpl_frameset_insert(usedframes, cpl_frame_duplicate(rawframe));
346 cpl_msg_info(cpl_func,
"Writing image_out.fits");
372 char * product_name = NULL;
374 if ( cpl_parameterlist_find_const (parlist,
"gravity.dfs.static-name") &&
375 gravi_param_get_bool (parlist,
"gravity.dfs.static-name")) {
378 product_name = cpl_sprintf (
"%s_%s_%s.fits",
"gravity_image", catg_ext, target_name);
383 char * filenoext = cpl_strdup (FILESHORT(filename));
384 char * lastdot = strrchr (filenoext,
'.');
385 if (lastdot != NULL) *lastdot =
'\0';
386 product_name = cpl_sprintf (
"%s_%s_%s.fits", filenoext, catg_ext, target_name);
387 FREE (cpl_free, filenoext);
390 cpl_dfs_save_image(frameset, NULL, parlist, usedframes,
391 rawframe, image, CPL_BPP_IEEE_DOUBLE,
392 "gravity_image", applist, NULL,
393 PACKAGE
"/" PACKAGE_VERSION , product_name);
394 cpl_msg_info(cpl_func,
"Reconstructed image saved in image_out.fits");
397 cpl_image_delete(image);
398 cpl_propertylist_delete(applist);
399 cpl_frameset_delete(usedframes);
400 FREE (cpl_free, product_name);
405 return (
int)cpl_error_get_code();
407 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_NULL_INPUT,
408 "This recipe is only available if " 409 "the pipeline was compiled with yorick " 410 "support. Check configure --help");
gravi_data * gravi_data_load_ext(const char *filename, const char *extensions_regexp)
Low-level function to load FITS file.
cpl_error_code gravi_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
cpl_table * gravi_data_get_table(gravi_data *self, const char *extname)
Return a pointer on a table extension by its EXTNAME.
const char * gravi_get_license(void)
Get the pipeline copyright and license.
void gravi_data_delete(gravi_data *self)
Delete a gravi data.