37 #include "midi_utils.h"
38 #include "midi_cplutils.h"
39 #include "cpl_image_filter.h"
49 static int midi_opdff_create(cpl_plugin *);
50 static int midi_opdff_exec(cpl_plugin *);
51 static int midi_opdff_destroy(cpl_plugin *);
52 static int midi_opdff(cpl_frameset *,
const cpl_parameterlist *);
54 static void midi_check_medianwindow(
int * medianwindowX,
int * medianwindowY);
57 static int append_image_to_table(cpl_table * table,
const char * columname,
58 cpl_image * image,
int row);
65 static char midi_opdff_description[] =
95 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
96 cpl_plugin * plugin = &recipe->interface;
98 if (cpl_plugin_init(plugin,
101 CPL_PLUGIN_TYPE_RECIPE,
103 "Derives the flatfield for the OPD measurements",
104 midi_opdff_description,
110 midi_opdff_destroy)) {
111 cpl_msg_error(cpl_func,
"Plugin initialization failed");
112 (void)cpl_error_set_where(cpl_func);
116 if (cpl_pluginlist_append(list, plugin)) {
117 cpl_msg_error(cpl_func,
"Error adding plugin to list");
118 (void)cpl_error_set_where(cpl_func);
134 static int midi_opdff_create(cpl_plugin * plugin)
140 if (cpl_error_get_code() != CPL_ERROR_NONE) {
141 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
142 cpl_func, __LINE__, cpl_error_get_where());
143 return (
int)cpl_error_get_code();
146 if (plugin == NULL) {
147 cpl_msg_error(cpl_func,
"Null plugin");
148 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
152 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
153 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
154 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
158 recipe = (cpl_recipe *)plugin;
161 recipe->parameters = cpl_parameterlist_new();
162 if (recipe->parameters == NULL) {
163 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
164 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
169 p = cpl_parameter_new_value(
"midi.midi_opdff.Xmedianwindow",
170 CPL_TYPE_INT,
"The window size in x-direction of the median filter",
"midi.midi_opdff",5);
171 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"Xmedianwindow");
172 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
173 cpl_parameterlist_append(recipe->parameters, p);
175 p = cpl_parameter_new_value(
"midi.midi_opdff.Ymedianwindow",
176 CPL_TYPE_INT,
"The window size in y-direction of the median filter",
"midi.midi_opdff",5);
177 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"Ymedianwindow");
178 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
179 cpl_parameterlist_append(recipe->parameters, p);
192 static int midi_opdff_exec(cpl_plugin * plugin)
197 cpl_errorstate initial_errorstate = cpl_errorstate_get();
200 if (cpl_error_get_code() != CPL_ERROR_NONE) {
201 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
202 cpl_func, __LINE__, cpl_error_get_where());
203 return (
int)cpl_error_get_code();
206 if (plugin == NULL) {
207 cpl_msg_error(cpl_func,
"Null plugin");
208 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
212 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
213 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
214 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
218 recipe = (cpl_recipe *)plugin;
221 if (recipe->parameters == NULL) {
222 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
223 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
225 if (recipe->frames == NULL) {
226 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
227 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
231 recipe_status = midi_opdff(recipe->frames, recipe->parameters);
234 if (cpl_dfs_update_product_header(recipe->frames)) {
235 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
238 if (!cpl_errorstate_is_equal(initial_errorstate)) {
241 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
244 return recipe_status;
254 static int midi_opdff_destroy(cpl_plugin * plugin)
258 if (plugin == NULL) {
259 cpl_msg_error(cpl_func,
"Null plugin");
260 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
264 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
265 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
266 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
270 recipe = (cpl_recipe *)plugin;
272 cpl_parameterlist_delete(recipe->parameters);
285 static int midi_opdff(cpl_frameset * frameset,
286 const cpl_parameterlist * parlist)
289 const cpl_parameter * paramX=NULL;
290 const cpl_parameter * paramY=NULL;
291 cpl_frame * cur_frame=NULL;
292 int dimenDATA=DIMENDATA;
293 cpl_imagelist * imglst_AOPEN_DATA_T[DIMENDATA]={NULL,NULL};
294 cpl_imagelist * imglst_BOPEN_DATA_T[DIMENDATA]={NULL,NULL};
295 cpl_image * dummy_image;
296 cpl_image * image_AOPEN_DATA_T[DIMENDATA];
297 cpl_image * image_BOPEN_DATA_T[DIMENDATA];
298 cpl_image * image_AOPEN_DATA_T_filtered[DIMENDATA];
299 cpl_image * image_BOPEN_DATA_T_filtered[DIMENDATA];
300 cpl_image * flatfield_AOPEN[DIMENDATA];
301 cpl_image * flatfield_BOPEN[DIMENDATA];
302 cpl_errorstate prestate = cpl_errorstate_get();
307 int ext_imaging_data=0;
308 cpl_table * table=NULL;
309 char * dataname=NULL;
310 cpl_propertylist * qclist=NULL;
311 cpl_propertylist * pro_list=NULL;
312 cpl_mask * mask=NULL;
313 cpl_table * opdff_table=NULL;
318 if(midi_check_sof(frameset,MIDI_DOME_AOPEN)<1)
320 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
321 "SOF has no appropriate AOPEN fitsfiles! Aborting!");
325 if(midi_check_sof(frameset,MIDI_DOME_BOPEN)<1)
327 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
328 "SOF has no appropriate BOPEN fitsfiles! Aborting!");
334 paramX = cpl_parameterlist_find_const(parlist,
335 "midi.midi_opdff.Xmedianwindow");
336 paramY = cpl_parameterlist_find_const(parlist,
337 "midi.midi_opdff.Ymedianwindow");
338 medianwindowX = cpl_parameter_get_int(paramX);
339 medianwindowY = cpl_parameter_get_int(paramY);
340 midi_check_medianwindow(&medianwindowX, &medianwindowY);
342 if (!cpl_errorstate_is_equal(prestate)) {
343 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
"Could not retrieve the input parameters");
348 cpl_error_get_code());
351 for (i=0; i<dimenDATA;i++){
352 imglst_AOPEN_DATA_T[i]=cpl_imagelist_new();
353 imglst_BOPEN_DATA_T[i]=cpl_imagelist_new();
357 cur_frame = cpl_frameset_get_first(frameset);
358 if (cur_frame == NULL) {
359 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
360 "SOF does not have any file");
369 tag = (
char*)cpl_frame_get_tag(cur_frame);
370 if (strcmp(tag, MIDI_DOME_AOPEN) && strcmp(tag, MIDI_DOME_BOPEN)) {
371 cur_frame = cpl_frameset_get_next( frameset );
374 cpl_msg_info(cpl_func,
"Processing file %s",cpl_frame_get_filename(cur_frame));
375 ext_imaging_data=cpl_fits_find_extension(cpl_frame_get_filename(cur_frame),
"IMAGING_DATA");
377 if (strcmp(tag, MIDI_DOME_AOPEN)==0)
380 table = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_imaging_data, 1);
382 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
383 "Could not load the table");
385 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
388 for (i=0; i<dimenDATA;i++){
390 dataname=cpl_sprintf(
"DATA%d",i+1);
393 if (cpl_table_has_column(table,dataname)){
394 table_to_imglst(dataname,imglst_AOPEN_DATA_T[i],table);
397 cpl_msg_info(cpl_func,
"Number of so far processed AOPEN %s Frames: %d",dataname,cpl_imagelist_get_size(imglst_AOPEN_DATA_T[i]));
401 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
403 cpl_table_delete(table);
405 if (strcmp(tag, MIDI_DOME_BOPEN)==0)
408 table = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_imaging_data, 1);
410 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
411 "Could not load the table");
413 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
416 for (i=0; i<dimenDATA;i++){
418 dataname=cpl_sprintf(
"DATA%d",i+1);
421 if (cpl_table_has_column(table,dataname)){
422 table_to_imglst(dataname,imglst_BOPEN_DATA_T[i],table);
425 cpl_msg_info(cpl_func,
"Number of so far processed BOPEN %s Frames: %d",dataname,cpl_imagelist_get_size(imglst_BOPEN_DATA_T[i]));
429 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
431 cpl_table_delete(table);
434 cur_frame = cpl_frameset_get_next( frameset );
441 for (i=0; i<dimenDATA;i++){
443 dummy_image=cpl_imagelist_collapse_create(imglst_AOPEN_DATA_T[i]);
444 image_AOPEN_DATA_T[i]=cpl_image_cast(dummy_image,CPL_TYPE_DOUBLE);
445 cpl_image_delete(dummy_image);
447 dummy_image=cpl_imagelist_collapse_create(imglst_BOPEN_DATA_T[i]);
448 image_BOPEN_DATA_T[i]=cpl_image_cast(dummy_image,CPL_TYPE_DOUBLE);
449 cpl_image_delete(dummy_image);
455 mask = cpl_mask_new(medianwindowX, medianwindowY);
458 cpl_msg_info(cpl_func,
"Smoothing the images ...");
461 for (i=0; i<dimenDATA;i++){
462 image_AOPEN_DATA_T_filtered[i]=cpl_image_duplicate(image_AOPEN_DATA_T[i]);
463 image_BOPEN_DATA_T_filtered[i]=cpl_image_duplicate(image_BOPEN_DATA_T[i]);
467 for (i=0; i<dimenDATA;i++){
468 cpl_image_filter_mask(image_AOPEN_DATA_T_filtered[i],image_AOPEN_DATA_T[i], mask,CPL_FILTER_MEDIAN ,CPL_BORDER_FILTER);
469 cpl_image_filter_mask(image_BOPEN_DATA_T_filtered[i],image_BOPEN_DATA_T[i], mask,CPL_FILTER_MEDIAN ,CPL_BORDER_FILTER);
476 for (i=0; i<dimenDATA;i++){
477 flatfield_AOPEN[i]=cpl_image_divide_create(image_AOPEN_DATA_T[i],image_AOPEN_DATA_T_filtered[i]);
478 flatfield_BOPEN[i]=cpl_image_divide_create(image_BOPEN_DATA_T[i],image_BOPEN_DATA_T_filtered[i]);
484 for (i=0; i<dimenDATA;i++){
485 cpl_image_add(flatfield_AOPEN[i],flatfield_BOPEN[i]);
486 cpl_image_divide_scalar(flatfield_AOPEN[i],2.0);
491 qclist = cpl_propertylist_new();
492 cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG,
"MIDI_OPDFF_DATA1");
493 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, flatfield_AOPEN[0],
494 CPL_BPP_IEEE_FLOAT,
"flatfield_DATA1",
496 PACKAGE
"/" PACKAGE_VERSION,
497 "flatfield_DATA1.fits")) {
499 (void)cpl_error_set_where(cpl_func);
501 cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG,
"MIDI_OPDFF_DATA2");
502 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, flatfield_AOPEN[1],
503 CPL_BPP_IEEE_FLOAT,
"flatfield_DATA2",
505 PACKAGE
"/" PACKAGE_VERSION,
506 "flatfield_DATA2.fits")) {
508 (void)cpl_error_set_where(cpl_func);
514 opdff_table=cpl_table_new(1);
518 cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG,
"MIDI_OPDFF");
519 cpl_propertylist_append_string(qclist,
"EXTNAME",
"IMAGING_DATA");
521 append_image_to_table(opdff_table,
"DATA1",flatfield_AOPEN[0],0);
522 append_image_to_table(opdff_table,
"DATA2",flatfield_AOPEN[1],0);
524 cpl_dfs_save_table(frameset, NULL, parlist, frameset, NULL, opdff_table,
525 qclist,
"midi_opdff",
527 PACKAGE
"/" PACKAGE_VERSION,
529 cpl_table_delete(opdff_table);
537 cpl_mask_delete(mask);
538 cpl_propertylist_delete(qclist);
539 cpl_propertylist_delete(pro_list);
541 for (i=0; i<dimenDATA;i++){
542 cpl_image_delete(flatfield_AOPEN[i]);
543 cpl_image_delete(flatfield_BOPEN[i]);
544 cpl_image_delete(image_AOPEN_DATA_T[i]);
545 cpl_image_delete(image_BOPEN_DATA_T[i]);
546 cpl_image_delete(image_AOPEN_DATA_T_filtered[i]);
547 cpl_image_delete(image_BOPEN_DATA_T_filtered[i]);
550 for (i=0; i<dimenDATA;i++){
551 while(cpl_imagelist_get_size(imglst_AOPEN_DATA_T[i])>0){
552 cpl_image_delete(cpl_imagelist_unset(imglst_AOPEN_DATA_T[i],0));
554 while(cpl_imagelist_get_size(imglst_BOPEN_DATA_T[i])>0){
555 cpl_image_delete(cpl_imagelist_unset(imglst_BOPEN_DATA_T[i],0));
557 cpl_imagelist_delete(imglst_AOPEN_DATA_T[i]);
558 cpl_imagelist_delete(imglst_BOPEN_DATA_T[i]);
561 return (
int)cpl_error_get_code();
582 static int append_image_to_table(cpl_table * table,
const char * columname, cpl_image * image,
int row)
585 cpl_array * array_dimension=NULL;
586 cpl_array * array_dummy=NULL;
588 array_dimension=cpl_array_new(2,CPL_TYPE_INT);
589 cpl_array_set(array_dimension, 0,cpl_image_get_size_x(image));
590 cpl_array_set(array_dimension, 1,cpl_image_get_size_y(image));
592 cpl_table_new_column_array(table, columname, CPL_TYPE_DOUBLE, cpl_image_get_size_x(image)*cpl_image_get_size_y(image));
593 cpl_table_set_column_dimensions(table,columname,array_dimension);
594 array_dummy = cpl_array_wrap_double(cpl_image_get_data_double(image), cpl_image_get_size_x(image)*cpl_image_get_size_y(image));
595 cpl_table_set_array(table, columname, row, array_dummy);
596 cpl_array_unwrap(array_dummy);
599 cpl_array_delete(array_dimension);
614 static void midi_check_medianwindow(
int * medianwindowX,
int * medianwindowY)
617 if((*medianwindowX)%2 == 0)
619 cpl_msg_warning(cpl_func,
"The x window size of the median filter is not odd,");
620 cpl_msg_warning(cpl_func,
"therefore the size is increased by unity");
621 *medianwindowX=*medianwindowX+1;
623 if((*medianwindowY)%2 == 0)
625 cpl_msg_warning(cpl_func,
"The y window size of the median filter is not odd,");
626 cpl_msg_warning(cpl_func,
"therefore the size is increased by unity");
627 *medianwindowY=*medianwindowY+1;
639 if(*medianwindowX < 0)
641 cpl_msg_warning(cpl_func,
"The x window size of the median filter must be positive,");
642 cpl_msg_warning(cpl_func,
"therefore the size is reset to 1");
646 if(*medianwindowY < 0)
648 cpl_msg_warning(cpl_func,
"The y window size of the median filter must be positive,");
649 cpl_msg_warning(cpl_func,
"therefore the size is reset to 1");