00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
00034
00035
00036 #include <cpl.h>
00037 #include "midi_utils.h"
00038 #include "midi_cplutils.h"
00039 #include "cpl_image_filter.h"
00040
00041 #include "midi_dfs.h"
00042 #include "string.h"
00043
00044 #define DIMENDATA 2
00045
00046
00047
00048
00049 static int midi_opdff_create(cpl_plugin *);
00050 static int midi_opdff_exec(cpl_plugin *);
00051 static int midi_opdff_destroy(cpl_plugin *);
00052 static int midi_opdff(cpl_frameset *, const cpl_parameterlist *);
00053
00054 static void midi_check_medianwindow(int * medianwindowX, int * medianwindowY);
00055
00056
00057 static int append_image_to_table(cpl_table * table, const char * columname,
00058 cpl_image * image, int row);
00059
00060
00061
00062
00063
00064
00065 static char midi_opdff_description[] =
00066 "TBD\n"
00067 "\n";
00068
00069
00070
00071
00072
00073
00078
00079
00082
00092
00093 int cpl_plugin_get_info(cpl_pluginlist * list)
00094 {
00095 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe );
00096 cpl_plugin * plugin = &recipe->interface;
00097
00098 if (cpl_plugin_init(plugin,
00099 CPL_PLUGIN_API,
00100 MIDI_BINARY_VERSION,
00101 CPL_PLUGIN_TYPE_RECIPE,
00102 "midi_opdff",
00103 "Derives the flatfield for the OPD measurements",
00104 midi_opdff_description,
00105 "Armin Gabasch",
00106 PACKAGE_BUGREPORT,
00107 midi_get_license(),
00108 midi_opdff_create,
00109 midi_opdff_exec,
00110 midi_opdff_destroy)) {
00111 cpl_msg_error(cpl_func, "Plugin initialization failed");
00112 (void)cpl_error_set_where(cpl_func);
00113 return 1;
00114 }
00115
00116 if (cpl_pluginlist_append(list, plugin)) {
00117 cpl_msg_error(cpl_func, "Error adding plugin to list");
00118 (void)cpl_error_set_where(cpl_func);
00119 return 1;
00120 }
00121
00122 return 0;
00123 }
00124
00125
00133
00134 static int midi_opdff_create(cpl_plugin * plugin)
00135 {
00136 cpl_recipe * recipe;
00137 cpl_parameter * p;
00138
00139
00140 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00141 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
00142 cpl_func, __LINE__, cpl_error_get_where());
00143 return (int)cpl_error_get_code();
00144 }
00145
00146 if (plugin == NULL) {
00147 cpl_msg_error(cpl_func, "Null plugin");
00148 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00149 }
00150
00151
00152 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00153 cpl_msg_error(cpl_func, "Plugin is not a recipe");
00154 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
00155 }
00156
00157
00158 recipe = (cpl_recipe *)plugin;
00159
00160
00161 recipe->parameters = cpl_parameterlist_new();
00162 if (recipe->parameters == NULL) {
00163 cpl_msg_error(cpl_func, "Parameter list allocation failed");
00164 cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);
00165 }
00166
00167
00168
00169 p = cpl_parameter_new_value("midi.midi_opdff.Xmedianwindow",
00170 CPL_TYPE_INT, "The window size in x-direction of the median filter", "midi.midi_opdff",5);
00171 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "Xmedianwindow");
00172 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00173 cpl_parameterlist_append(recipe->parameters, p);
00174
00175 p = cpl_parameter_new_value("midi.midi_opdff.Ymedianwindow",
00176 CPL_TYPE_INT, "The window size in y-direction of the median filter", "midi.midi_opdff",5);
00177 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "Ymedianwindow");
00178 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00179 cpl_parameterlist_append(recipe->parameters, p);
00180
00181
00182 return 0;
00183 }
00184
00185
00191
00192 static int midi_opdff_exec(cpl_plugin * plugin)
00193 {
00194
00195 cpl_recipe * recipe;
00196 int recipe_status;
00197 cpl_errorstate initial_errorstate = cpl_errorstate_get();
00198
00199
00200 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00201 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
00202 cpl_func, __LINE__, cpl_error_get_where());
00203 return (int)cpl_error_get_code();
00204 }
00205
00206 if (plugin == NULL) {
00207 cpl_msg_error(cpl_func, "Null plugin");
00208 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00209 }
00210
00211
00212 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00213 cpl_msg_error(cpl_func, "Plugin is not a recipe");
00214 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
00215 }
00216
00217
00218 recipe = (cpl_recipe *)plugin;
00219
00220
00221 if (recipe->parameters == NULL) {
00222 cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
00223 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00224 }
00225 if (recipe->frames == NULL) {
00226 cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
00227 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00228 }
00229
00230
00231 recipe_status = midi_opdff(recipe->frames, recipe->parameters);
00232
00233
00234 if (cpl_dfs_update_product_header(recipe->frames)) {
00235 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
00236 }
00237
00238 if (!cpl_errorstate_is_equal(initial_errorstate)) {
00239
00240
00241 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
00242 }
00243
00244 return recipe_status;
00245 }
00246
00247
00253
00254 static int midi_opdff_destroy(cpl_plugin * plugin)
00255 {
00256 cpl_recipe * recipe;
00257
00258 if (plugin == NULL) {
00259 cpl_msg_error(cpl_func, "Null plugin");
00260 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00261 }
00262
00263
00264 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00265 cpl_msg_error(cpl_func, "Plugin is not a recipe");
00266 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
00267 }
00268
00269
00270 recipe = (cpl_recipe *)plugin;
00271
00272 cpl_parameterlist_delete(recipe->parameters);
00273
00274 return 0;
00275 }
00276
00277
00284
00285 static int midi_opdff(cpl_frameset * frameset,
00286 const cpl_parameterlist * parlist)
00287 {
00288
00289 const cpl_parameter * paramX=NULL;
00290 const cpl_parameter * paramY=NULL;
00291 cpl_frame * cur_frame=NULL;
00292 int dimenDATA=DIMENDATA;
00293 cpl_imagelist * imglst_AOPEN_DATA_T[DIMENDATA]={NULL,NULL};
00294 cpl_imagelist * imglst_BOPEN_DATA_T[DIMENDATA]={NULL,NULL};
00295 cpl_image * dummy_image;
00296 cpl_image * image_AOPEN_DATA_T[DIMENDATA];
00297 cpl_image * image_BOPEN_DATA_T[DIMENDATA];
00298 cpl_image * image_AOPEN_DATA_T_filtered[DIMENDATA];
00299 cpl_image * image_BOPEN_DATA_T_filtered[DIMENDATA];
00300 cpl_image * flatfield_AOPEN[DIMENDATA];
00301 cpl_image * flatfield_BOPEN[DIMENDATA];
00302 cpl_errorstate prestate = cpl_errorstate_get();
00303 int medianwindowX=0;
00304 int medianwindowY=0;
00305 int i=0;
00306 char * tag=NULL;
00307 int ext_imaging_data=0;
00308 cpl_table * table=NULL;
00309 char * dataname=NULL;
00310 cpl_propertylist * qclist=NULL;
00311 cpl_propertylist * pro_list=NULL;
00312 cpl_mask * mask=NULL;
00313 cpl_table * opdff_table=NULL;
00314
00315
00316
00317
00318 if(midi_check_sof(frameset,MIDI_DOME_AOPEN)<1)
00319 {
00320 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
00321 "SOF has no appropriate AOPEN fitsfiles! Aborting!");
00322
00323 }
00324
00325 if(midi_check_sof(frameset,MIDI_DOME_BOPEN)<1)
00326 {
00327 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
00328 "SOF has no appropriate BOPEN fitsfiles! Aborting!");
00329
00330 }
00331
00332
00333
00334 paramX = cpl_parameterlist_find_const(parlist,
00335 "midi.midi_opdff.Xmedianwindow");
00336 paramY = cpl_parameterlist_find_const(parlist,
00337 "midi.midi_opdff.Ymedianwindow");
00338 medianwindowX = cpl_parameter_get_int(paramX);
00339 medianwindowY = cpl_parameter_get_int(paramY);
00340 midi_check_medianwindow(&medianwindowX, &medianwindowY);
00341
00342 if (!cpl_errorstate_is_equal(prestate)) {
00343 return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(), "Could not retrieve the input parameters");
00344 }
00345
00346
00347 cpl_ensure_code(midi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
00348 cpl_error_get_code());
00349
00350
00351 for (i=0; i<dimenDATA;i++){
00352 imglst_AOPEN_DATA_T[i]=cpl_imagelist_new();
00353 imglst_BOPEN_DATA_T[i]=cpl_imagelist_new();
00354
00355 }
00356
00357 cur_frame = cpl_frameset_get_first(frameset);
00358 if (cur_frame == NULL) {
00359 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
00360 "SOF does not have any file");
00361 }
00362
00363
00364
00365
00366 while(cur_frame)
00367 {
00368
00369 tag = (char*)cpl_frame_get_tag(cur_frame);
00370 if (strcmp(tag, MIDI_DOME_AOPEN) && strcmp(tag, MIDI_DOME_BOPEN)) {
00371 cur_frame = cpl_frameset_get_next( frameset );
00372 continue;
00373 }
00374 cpl_msg_info(cpl_func, "Processing file %s",cpl_frame_get_filename(cur_frame));
00375 ext_imaging_data=cpl_fits_find_extension(cpl_frame_get_filename(cur_frame),"IMAGING_DATA");
00376
00377 if (strcmp(tag, MIDI_DOME_AOPEN)==0)
00378 {
00379
00380 table = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_imaging_data, 1);
00381 if (table == NULL) {
00382 return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00383 "Could not load the table");
00384 }
00385 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00386
00387
00388 for (i=0; i<dimenDATA;i++){
00389
00390 dataname=cpl_sprintf("DATA%d",i+1);
00391
00392
00393 if (cpl_table_has_column(table,dataname)){
00394 table_to_imglst(dataname,imglst_AOPEN_DATA_T[i],table);
00395 }
00396
00397 cpl_msg_info(cpl_func, "Number of so far processed AOPEN %s Frames: %d",dataname,cpl_imagelist_get_size(imglst_AOPEN_DATA_T[i]));
00398 cpl_free(dataname);
00399 }
00400
00401 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00402
00403 cpl_table_delete(table);
00404 }
00405 if (strcmp(tag, MIDI_DOME_BOPEN)==0)
00406 {
00407
00408 table = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_imaging_data, 1);
00409 if (table == NULL) {
00410 return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00411 "Could not load the table");
00412 }
00413 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00414
00415
00416 for (i=0; i<dimenDATA;i++){
00417
00418 dataname=cpl_sprintf("DATA%d",i+1);
00419
00420
00421 if (cpl_table_has_column(table,dataname)){
00422 table_to_imglst(dataname,imglst_BOPEN_DATA_T[i],table);
00423 }
00424
00425 cpl_msg_info(cpl_func, "Number of so far processed BOPEN %s Frames: %d",dataname,cpl_imagelist_get_size(imglst_BOPEN_DATA_T[i]));
00426 cpl_free(dataname);
00427 }
00428
00429 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00430
00431 cpl_table_delete(table);
00432 }
00433
00434 cur_frame = cpl_frameset_get_next( frameset );
00435 }
00436
00437
00438
00439
00440
00441 for (i=0; i<dimenDATA;i++){
00442
00443 dummy_image=cpl_imagelist_collapse_create(imglst_AOPEN_DATA_T[i]);
00444 image_AOPEN_DATA_T[i]=cpl_image_cast(dummy_image,CPL_TYPE_DOUBLE);
00445 cpl_image_delete(dummy_image);
00446
00447 dummy_image=cpl_imagelist_collapse_create(imglst_BOPEN_DATA_T[i]);
00448 image_BOPEN_DATA_T[i]=cpl_image_cast(dummy_image,CPL_TYPE_DOUBLE);
00449 cpl_image_delete(dummy_image);
00450
00451 }
00452
00453
00454
00455 mask = cpl_mask_new(medianwindowX, medianwindowY);
00456 cpl_mask_not(mask);
00457
00458 cpl_msg_info(cpl_func, "Smoothing the images ...");
00459
00460
00461 for (i=0; i<dimenDATA;i++){
00462 image_AOPEN_DATA_T_filtered[i]=cpl_image_duplicate(image_AOPEN_DATA_T[i]);
00463 image_BOPEN_DATA_T_filtered[i]=cpl_image_duplicate(image_BOPEN_DATA_T[i]);
00464 }
00465
00466
00467 for (i=0; i<dimenDATA;i++){
00468 cpl_image_filter_mask(image_AOPEN_DATA_T_filtered[i],image_AOPEN_DATA_T[i], mask,CPL_FILTER_MEDIAN ,CPL_BORDER_FILTER);
00469 cpl_image_filter_mask(image_BOPEN_DATA_T_filtered[i],image_BOPEN_DATA_T[i], mask,CPL_FILTER_MEDIAN ,CPL_BORDER_FILTER);
00470
00471 }
00472
00473
00474
00475
00476 for (i=0; i<dimenDATA;i++){
00477 flatfield_AOPEN[i]=cpl_image_divide_create(image_AOPEN_DATA_T[i],image_AOPEN_DATA_T_filtered[i]);
00478 flatfield_BOPEN[i]=cpl_image_divide_create(image_BOPEN_DATA_T[i],image_BOPEN_DATA_T_filtered[i]);
00479 }
00480
00481
00482
00483
00484 for (i=0; i<dimenDATA;i++){
00485 cpl_image_add(flatfield_AOPEN[i],flatfield_BOPEN[i]);
00486 cpl_image_divide_scalar(flatfield_AOPEN[i],2.0);
00487 }
00488
00489
00490
00491 qclist = cpl_propertylist_new();
00492 cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG, "MIDI_OPDFF_DATA1");
00493 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, flatfield_AOPEN[0],
00494 CPL_BPP_IEEE_FLOAT, "flatfield_DATA1",
00495 qclist, NULL,
00496 PACKAGE "/" PACKAGE_VERSION,
00497 "flatfield_DATA1.fits")) {
00498
00499 (void)cpl_error_set_where(cpl_func);
00500 }
00501 cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG, "MIDI_OPDFF_DATA2");
00502 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, flatfield_AOPEN[1],
00503 CPL_BPP_IEEE_FLOAT, "flatfield_DATA2",
00504 qclist, NULL,
00505 PACKAGE "/" PACKAGE_VERSION,
00506 "flatfield_DATA2.fits")) {
00507
00508 (void)cpl_error_set_where(cpl_func);
00509 }
00510
00511
00512
00513
00514 opdff_table=cpl_table_new(1);
00515
00516
00517
00518 cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG, "MIDI_OPDFF");
00519 cpl_propertylist_append_string(qclist, "EXTNAME", "IMAGING_DATA");
00520
00521 append_image_to_table(opdff_table,"DATA1",flatfield_AOPEN[0],0);
00522 append_image_to_table(opdff_table,"DATA2",flatfield_AOPEN[1],0);
00523
00524 cpl_dfs_save_table(frameset, NULL, parlist, frameset, NULL, opdff_table,
00525 qclist, "midi_opdff",
00526 qclist, NULL,
00527 PACKAGE "/" PACKAGE_VERSION,
00528 "midi_opdff.fits");
00529 cpl_table_delete(opdff_table);
00530
00531
00532
00533
00534
00535
00536
00537 cpl_mask_delete(mask);
00538 cpl_propertylist_delete(qclist);
00539 cpl_propertylist_delete(pro_list);
00540
00541 for (i=0; i<dimenDATA;i++){
00542 cpl_image_delete(flatfield_AOPEN[i]);
00543 cpl_image_delete(flatfield_BOPEN[i]);
00544 cpl_image_delete(image_AOPEN_DATA_T[i]);
00545 cpl_image_delete(image_BOPEN_DATA_T[i]);
00546 cpl_image_delete(image_AOPEN_DATA_T_filtered[i]);
00547 cpl_image_delete(image_BOPEN_DATA_T_filtered[i]);
00548 }
00549
00550 for (i=0; i<dimenDATA;i++){
00551 while(cpl_imagelist_get_size(imglst_AOPEN_DATA_T[i])>0){
00552 cpl_image_delete(cpl_imagelist_unset(imglst_AOPEN_DATA_T[i],0));
00553 }
00554 while(cpl_imagelist_get_size(imglst_BOPEN_DATA_T[i])>0){
00555 cpl_image_delete(cpl_imagelist_unset(imglst_BOPEN_DATA_T[i],0));
00556 }
00557 cpl_imagelist_delete(imglst_AOPEN_DATA_T[i]);
00558 cpl_imagelist_delete(imglst_BOPEN_DATA_T[i]);
00559 }
00560
00561 return (int)cpl_error_get_code();
00562 }
00563
00564
00565
00566
00567
00568
00569
00570
00571
00580
00581
00582 static int append_image_to_table(cpl_table * table, const char * columname, cpl_image * image, int row)
00583 {
00584
00585 cpl_array * array_dimension=NULL;
00586 cpl_array * array_dummy=NULL;
00587
00588 array_dimension=cpl_array_new(2,CPL_TYPE_INT);
00589 cpl_array_set(array_dimension, 0,cpl_image_get_size_x(image));
00590 cpl_array_set(array_dimension, 1,cpl_image_get_size_y(image));
00591
00592 cpl_table_new_column_array(table, columname, CPL_TYPE_DOUBLE, cpl_image_get_size_x(image)*cpl_image_get_size_y(image));
00593 cpl_table_set_column_dimensions(table,columname,array_dimension);
00594 array_dummy = cpl_array_wrap_double(cpl_image_get_data_double(image), cpl_image_get_size_x(image)*cpl_image_get_size_y(image));
00595 cpl_table_set_array(table, columname, row, array_dummy);
00596 cpl_array_unwrap(array_dummy);
00597
00598
00599 cpl_array_delete(array_dimension);
00600
00601 return 0;
00602 }
00603
00604
00605
00612
00613
00614 static void midi_check_medianwindow(int * medianwindowX, int * medianwindowY)
00615 {
00616
00617 if((*medianwindowX)%2 == 0)
00618 {
00619 cpl_msg_warning(cpl_func, "The x window size of the median filter is not odd,");
00620 cpl_msg_warning(cpl_func, "therefore the size is increased by unity");
00621 *medianwindowX=*medianwindowX+1;
00622 }
00623 if((*medianwindowY)%2 == 0)
00624 {
00625 cpl_msg_warning(cpl_func, "The y window size of the median filter is not odd,");
00626 cpl_msg_warning(cpl_func, "therefore the size is increased by unity");
00627 *medianwindowY=*medianwindowY+1;
00628 }
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639 if(*medianwindowX < 0)
00640 {
00641 cpl_msg_warning(cpl_func, "The x window size of the median filter must be positive,");
00642 cpl_msg_warning(cpl_func, "therefore the size is reset to 1");
00643 *medianwindowX=1;
00644 }
00645
00646 if(*medianwindowY < 0)
00647 {
00648 cpl_msg_warning(cpl_func, "The y window size of the median filter must be positive,");
00649 cpl_msg_warning(cpl_func, "therefore the size is reset to 1");
00650 *medianwindowY=1;
00651 }
00652
00653
00654 }
00655
00656
00657
00658