midi_acq.c

00001 /* $Id: midi_acq.c,v 1.18 2010/05/28 09:16:01 agabasch Exp $
00002  *
00003  * This file is part of the MIDI Pipeline
00004  * Copyright (C) 2002,2003 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: agabasch $
00023  * $Date: 2010/05/28 09:16:01 $
00024  * $Revision: 1.18 $
00025  * $Name: midi-2_7_0 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <cpl.h>
00037 #include <stdio.h>
00038 #include "midi_utils.h"
00039 #include "midi_pfits.h"
00040 #include "midi_dfs.h"
00041 #include "midiControl.h"
00042 #include "midiGlobal.h"
00043 #include "midiAppendPropertylist.h"
00044 #include <string.h>
00045 
00046 /*-----------------------------------------------------------------------------
00047                             Functions prototypes
00048  -----------------------------------------------------------------------------*/
00049 
00050 static int midi_acq_create(cpl_plugin *) ;
00051 static int midi_acq_exec(cpl_plugin *) ;
00052 static int midi_acq_destroy(cpl_plugin *) ;
00053 static int midi_acq(cpl_parameterlist *, cpl_frameset *) ;
00054 static int table_to_imglst(const char * columname,
00055         const char * columntype,
00056         cpl_imagelist * imglst_target,
00057         const char * columnentry,
00058         cpl_table * table);
00059 static cpl_error_code midi_qc_acq(const cpl_image * image,
00060         cpl_propertylist * pro_list);
00061 static cpl_error_code midi_image_get_fwhm(
00062         const cpl_image *   in,
00063         int                 xpos,
00064         int                 ypos,
00065         double          *   fwhm_x,
00066         double          *   fwhm_y);
00067 static double midi_vector_get_fwhm(
00068         const cpl_vector    *   vec,
00069         int                     pos,
00070         double                  half_max);
00071 static int midi_isnan(double value);
00072 
00073 
00074 /*-----------------------------------------------------------------------------
00075                             Static variables
00076  -----------------------------------------------------------------------------*/
00077 
00078 static char midi_acq_description[] =
00079         "The purpose of this recipe is to assess the position, size and the flux\n"
00080         "intensity of the target.\n\n"
00081         "Input files:\n\n"
00082         "  DO category:               Type:       Explanation:         Required:\n"
00083         "  ACQ                        Raw         Raw data frame          Y\n\n"
00084         "Output files:\n\n"
00085         "  DO category:               Data type:  Explanation:\n"
00086         "  IMAGE_QUALITY              FITS image  Image of the beam\n\n";
00087 
00088 /*-----------------------------------------------------------------------------
00089                                 Functions code
00090  -----------------------------------------------------------------------------*/
00091 
00092 /*----------------------------------------------------------------------------*/
00101 /*----------------------------------------------------------------------------*/
00102 int cpl_plugin_get_info(cpl_pluginlist * list)
00103 {
00104     cpl_recipe  *   recipe = cpl_calloc(1, sizeof *recipe ) ;
00105     cpl_plugin  *   plugin = &recipe->interface ;
00106 
00107     cpl_plugin_init(plugin,
00108             CPL_PLUGIN_API,
00109             MIDI_BINARY_VERSION,
00110             CPL_PLUGIN_TYPE_RECIPE,
00111             "midi_acq",
00112             "Image quality assessment",
00113             midi_acq_description,
00114             "Coorosh Sabet",
00115             PACKAGE_BUGREPORT,
00116             midi_get_license(),
00117             midi_acq_create,
00118             midi_acq_exec,
00119             midi_acq_destroy) ;
00120 
00121     cpl_pluginlist_append(list, plugin) ;
00122 
00123     return 0;
00124 }
00125 
00126 /*----------------------------------------------------------------------------*/
00134 /*----------------------------------------------------------------------------*/
00135 static int midi_acq_create(cpl_plugin * plugin)
00136 {
00137     cpl_recipe      * recipe ;
00138     /*    cpl_parameter   * p ;*/
00139 
00140     /* Check that the plugin is part of a valid recipe */
00141     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00142         recipe = (cpl_recipe *)plugin ;
00143     else return -1 ;
00144 
00145     /* Create the parameters list in the cpl_recipe object */
00146     recipe->parameters = cpl_parameterlist_new() ;
00147 
00148 
00149     /* Return */
00150     return 0;
00151 }
00152 
00153 /*----------------------------------------------------------------------------*/
00159 /*----------------------------------------------------------------------------*/
00160 static int midi_acq_exec(cpl_plugin * plugin)
00161 {
00162     cpl_recipe  *   recipe ;
00163 
00164     /* Get the recipe out of the plugin */
00165     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00166         recipe = (cpl_recipe *)plugin ;
00167     else return -1 ;
00168 
00169     batchNumber=0;
00170     return midi_acq(recipe->parameters, recipe->frames) ;
00171 }
00172 
00173 /*----------------------------------------------------------------------------*/
00179 /*----------------------------------------------------------------------------*/
00180 static int midi_acq_destroy(cpl_plugin * plugin)
00181 {
00182     cpl_recipe  *   recipe ;
00183 
00184     /* Get the recipe out of the plugin */
00185     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00186         recipe = (cpl_recipe *)plugin ;
00187     else return -1 ;
00188 
00189     cpl_parameterlist_delete(recipe->parameters) ;
00190     return 0 ;
00191 }
00192 
00193 /*----------------------------------------------------------------------------*/
00200 /*----------------------------------------------------------------------------*/
00201 static int midi_acq(
00202         cpl_parameterlist   *parlist,
00203         cpl_frameset        *frameset)
00204 {
00205 
00206     cpl_frame        * current_frame;
00207     int                error=0;
00208     FILE             * sofPtr=NULL;
00209     int                plotDuration;
00210     cpl_propertylist * header=NULL;
00211 
00212     cpl_frame        * cur_frame=NULL;
00213     cpl_table        * table=NULL;
00214     cpl_imagelist    * imglst_data1;
00215     cpl_imagelist    * imglst_data2;
00216     cpl_image        * image_data1, * image_data2;
00217     cpl_image        * image_data1_mask, * image_data2_mask;
00218     cpl_mask         * mask_data1, * mask_data2;
00219     cpl_matrix       * filter;
00220     cpl_errorstate     prestate = cpl_errorstate_get();
00221     int                ext_imaging_data=0;
00222     double             median_data1, median_data2;
00223     cpl_propertylist * qclist_data1=NULL;
00224     cpl_propertylist * qclist_data2=NULL;
00225     char               station[]= "AT";
00226     plotDuration=0;
00227 
00228     current_frame = cpl_frameset_get_first(frameset);
00229     sofPtr = fopen ("MIDI_sof.log", "w");
00230     while ( current_frame && sofPtr )
00231     {   
00232         fprintf (sofPtr,"%s \n", (char *)cpl_frame_get_filename(current_frame));
00233         current_frame = cpl_frameset_get_next( frameset );
00234     } /* All frames from frameset */
00235     fclose (sofPtr);
00236 
00237     /* NOW PERFORMING THE DATA REDUCTION */
00238     executeDataReduction ("", "","./", plotDuration,"MIDI_sof.log",&error,
00239             parlist,frameset);
00240     if (error) return -1;
00241     remove ("MIDI_sof.log");
00242 
00243     if (CPL_ERROR_NONE !=  appendPropertylist("MIDI_b1_acq_DATA1.pro.fits",
00244             CPL_FRAME_TYPE_IMAGE, "IMAGE_QUALITY",frameset,parlist))
00245     {
00246         cpl_msg_error(cpl_func,"Error in appendPropertylist");
00247     }
00248 
00249     if (CPL_ERROR_NONE !=  appendPropertylist("MIDI_b1_acq_DATA2.pro.fits",
00250             CPL_FRAME_TYPE_IMAGE, "IMAGE_QUALITY",frameset,parlist))
00251     {
00252         cpl_msg_error(cpl_func,"Error in appendPropertylist");
00253     }
00254 
00255 
00256     /*------------------------------------------------------------------------*/
00257 #ifdef MIDI_NEWCODE_ACQ_TEST
00258 
00259 
00260     imglst_data1=cpl_imagelist_new();
00261     imglst_data2=cpl_imagelist_new();
00262 
00263 
00264     cur_frame=cpl_frameset_find(frameset,MIDI_ACQ);
00265     if (cur_frame == NULL) {
00266         return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
00267                 "SOF does not have any file");
00268     }
00269     /* Append the images from the tables to the imagelists */
00270 
00271     cpl_msg_info(cpl_func,"Processing file %s",
00272             cpl_frame_get_filename(cur_frame));
00273 
00274     header = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0);
00275 
00276 
00277     /*       Checking if UT or AT */
00278     strcpy(station,"AT");
00279 
00280     if (cpl_propertylist_has(header, "ESO ISS CONF STATION1") == 1)
00281     {
00282         if(strcmp(cpl_propertylist_get_string(header,
00283                 "ESO ISS CONF STATION1"),"U1")==0 ||
00284                 strcmp(cpl_propertylist_get_string(header,
00285                         "ESO ISS CONF STATION1"),"U2")==0 ||
00286                         strcmp(cpl_propertylist_get_string(header,
00287                                 "ESO ISS CONF STATION1"),"U3")==0 ||
00288                                 strcmp(cpl_propertylist_get_string(header,
00289                                         "ESO ISS CONF STATION1"),"U4")==0){
00290             strcpy(station,"UT");
00291         }
00292     }
00293 
00294 
00295     /* Load extension  Imaging Data  */
00296     ext_imaging_data=cpl_fits_find_extension(cpl_frame_get_filename(cur_frame),
00297             "IMAGING_DATA");
00298     table =cpl_table_load(cpl_frame_get_filename(cur_frame),ext_imaging_data,1);
00299     if (table == NULL) {
00300         cpl_imagelist_delete(imglst_data1);
00301         cpl_imagelist_delete(imglst_data2);
00302         cpl_propertylist_delete(header);
00303         return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00304                 "Could not load the table");
00305     }
00306     cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00307 
00308     cpl_msg_indent_more();
00309     cpl_msg_info(cpl_func,"Reading DATA1 and DATA2 frames into imagelists ...");
00310     /* Adding the data1/data2 sky and target images to the imagelist*/
00311     if (cpl_table_has_column(table,"DATA1")){
00312         table_to_imglst("DATA1","TARTYP1",imglst_data1,"S",table);
00313         table_to_imglst("DATA1","TARTYP1",imglst_data1,"T",table);
00314     }
00315     if (cpl_table_has_column(table,"DATA2")){
00316         table_to_imglst("DATA2","TARTYP2",imglst_data2,"S",table);
00317         table_to_imglst("DATA2","TARTYP2",imglst_data2,"T",table);
00318 
00319     }
00320 
00321     if (cpl_msg_get_level() <= CPL_MSG_DEBUG) {
00322         cpl_msg_info(cpl_func, "Processed DATA1 frames: %d",
00323                 cpl_imagelist_get_size(imglst_data1));
00324         cpl_msg_info(cpl_func, "Processed DATA2 frames: %d",
00325                 cpl_imagelist_get_size(imglst_data2));
00326         cpl_imagelist_save(imglst_data1,"imglst_data1.fits", CPL_BPP_IEEE_FLOAT,
00327                 NULL, CPL_IO_CREATE);
00328         cpl_imagelist_save(imglst_data2,"imglst_data2.fits", CPL_BPP_IEEE_FLOAT,
00329                 NULL, CPL_IO_CREATE);
00330     }
00331 
00332     cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00333     cpl_table_delete(table);
00334 
00335 
00336     /*Average the imagelist to get an cpl_image*/
00337     cpl_msg_info(cpl_func, "Averaging the imagelists ...");
00338     image_data1=cpl_imagelist_collapse_create(imglst_data1);
00339     image_data2=cpl_imagelist_collapse_create(imglst_data2);
00340 
00341     cpl_msg_info(cpl_func, "Calculating the median ...");
00342     median_data1=cpl_image_get_median(image_data1);
00343     median_data2=cpl_image_get_median(image_data2);
00344     if (cpl_msg_get_level() <= CPL_MSG_DEBUG) {
00345         cpl_msg_info(cpl_func,"First median DATA1: %f DATA12:%f", median_data1,
00346                 median_data2);
00347     }
00348 
00349 
00350     /* Create a bad pixel mask and connect it with the image*/
00351     cpl_msg_info(cpl_func, "Creating a mask by using the median ...");
00352     mask_data1=cpl_mask_threshold_image_create(image_data1,0., median_data1);
00353     cpl_image_reject_from_mask(image_data1, mask_data1) ;
00354     mask_data2=cpl_mask_threshold_image_create(image_data2,0., median_data2);
00355     cpl_image_reject_from_mask(image_data2, mask_data2) ;
00356 
00357     /*Threshold the images */
00358     cpl_msg_info(cpl_func, "Thresholding the images by using the median ...");
00359     cpl_image_threshold(image_data1,median_data1,FLT_MAX,0.,FLT_MAX);
00360     cpl_image_threshold(image_data2,median_data2,FLT_MAX,0.,FLT_MAX);
00361 
00362     if (cpl_msg_get_level() <= CPL_MSG_DEBUG) {
00363         cpl_image_save(image_data1, "image_data1.fits", CPL_BPP_IEEE_FLOAT,
00364                 NULL, CPL_IO_CREATE);
00365         cpl_image_save(image_data2, "image_data2.fits", CPL_BPP_IEEE_FLOAT,
00366                 NULL, CPL_IO_CREATE);
00367     }
00368 
00369 
00370     /* Re Calculate the median deviation using the mask file*/
00371 
00372     cpl_msg_info(cpl_func, "Re-calculating the median after thresholding ...");
00373     median_data1=cpl_image_get_median(image_data1);
00374     median_data2=cpl_image_get_median(image_data2);
00375 
00376     if (cpl_msg_get_level() <= CPL_MSG_DEBUG) {
00377         cpl_msg_info(cpl_func,"Second median DATA1: %f DATA2:%f", median_data1,
00378                 median_data2);
00379     }
00380 
00381 
00382     /* Apply dilatation to decrease the good pixel area */
00383     cpl_msg_info(cpl_func, "Dilate the mask ...");
00384     filter = cpl_matrix_new(7, 7);
00385     cpl_matrix_fill(filter, 1.0);
00386     cpl_mask_dilation(mask_data1, filter);
00387     cpl_mask_dilation(mask_data2, filter);
00388     cpl_matrix_delete(filter);
00389 
00390     cpl_mask_not(mask_data1);
00391     cpl_mask_not(mask_data2);
00392 
00393     /*convert the mask to a cpl_image*/
00394     image_data1_mask=cpl_image_new_from_mask(mask_data1);
00395     image_data2_mask=cpl_image_new_from_mask(mask_data2);
00396 
00397     if (cpl_msg_get_level() <= CPL_MSG_DEBUG) {
00398         cpl_image_save(image_data1_mask, "image_data1_mask.fits",
00399                 CPL_BPP_IEEE_FLOAT, NULL, CPL_IO_CREATE);
00400         cpl_image_save(image_data2_mask, "image_data2_mask.fits",
00401                 CPL_BPP_IEEE_FLOAT, NULL, CPL_IO_CREATE);
00402     }
00403 
00404 
00405 
00406 
00407     /* multiply mask and image */
00408     cpl_msg_info(cpl_func, "Multiply the dilated mask and the image ...");
00409     cpl_image_multiply(image_data1,image_data1_mask);
00410     cpl_image_multiply(image_data2,image_data2_mask);
00411 
00412     cpl_msg_indent_less();
00413     if (strcmp(station,"AT")==0)
00414     {
00415 
00416         cpl_image_subtract_scalar(image_data1,median_data1);
00417         cpl_image_subtract_scalar(image_data2,median_data2);
00418 
00419         /*Threshold the images */
00420         cpl_image_threshold(image_data1, (median_data1*(-1.))+1., FLT_MAX,
00421                 0., FLT_MAX);
00422         cpl_image_threshold(image_data2, (median_data2*(-1.))+1. ,FLT_MAX,
00423                 0., FLT_MAX);
00424 
00425         cpl_image_multiply_scalar(image_data1,-1.);
00426         cpl_image_multiply_scalar(image_data2,-1.);
00427     }
00428 
00429 
00430     if (cpl_msg_get_level() <= CPL_MSG_DEBUG) {
00431         cpl_image_save(image_data1,"image_data1_final.fits", CPL_BPP_IEEE_FLOAT,
00432                 NULL, CPL_IO_CREATE);
00433         cpl_image_save(image_data2,"image_data2_final.fits", CPL_BPP_IEEE_FLOAT,
00434                 NULL, CPL_IO_CREATE);
00435     }
00436 
00437     qclist_data1=cpl_propertylist_new();
00438     qclist_data2=cpl_propertylist_new();
00439 
00440 
00441     /*Derive QC parameters*/
00442 
00443     prestate = cpl_errorstate_get();
00444 
00445     midi_qc_acq(image_data1,qclist_data1);
00446     midi_qc_acq(image_data2,qclist_data2);
00447 
00448 
00449     if (!cpl_errorstate_is_equal(prestate)) {
00450         /*An error happened*/
00451         cpl_errorstate_dump(prestate, CPL_FALSE, NULL);
00452     }
00453 
00454     /*cpl_errorstate_set(prestate);*/
00455 
00456 
00457 
00458 
00459     cpl_propertylist_update_string(qclist_data1, CPL_DFS_PRO_CATG,
00460             "MIDI_ACQ_FOV_DATA1");
00461     if (cpl_dfs_save_image(frameset, header, parlist, frameset, NULL,
00462             image_data1, CPL_BPP_IEEE_FLOAT, "midi_acq", qclist_data1, NULL,
00463             PACKAGE "/" PACKAGE_VERSION, "midi_acq_fov_data1.fits")) {
00464         /* Propagate the error */
00465         (void)cpl_error_set_where(cpl_func);
00466     }
00467 
00468     cpl_propertylist_update_string(qclist_data2, CPL_DFS_PRO_CATG,
00469             "MIDI_ACQ_FOV_DATA2");
00470     if (cpl_dfs_save_image(frameset, header, parlist, frameset, NULL,
00471             image_data2, CPL_BPP_IEEE_FLOAT, "midi_acq", qclist_data2, NULL,
00472             PACKAGE "/" PACKAGE_VERSION, "midi_acq_fov_data2.fits")) {
00473         /* Propagate the error */
00474         (void)cpl_error_set_where(cpl_func);
00475     }
00476     /*
00477     cpl_image_save(image_data1, "armin1_delayline.fits", CPL_BPP_IEEE_FLOAT,
00478             qclist_data1, CPL_IO_CREATE);
00479     cpl_image_save(image_data2, "armin2_delayline.fits", CPL_BPP_IEEE_FLOAT,
00480             qclist_data2, CPL_IO_CREATE);
00481      */
00482 
00483     if (cpl_error_get_code() != CPL_ERROR_NONE) {
00484         cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
00485                 cpl_func, __LINE__, cpl_error_get_message());
00486 
00487         cpl_msg_error(cpl_func,"%s %s",cpl_error_get_message(),
00488                 cpl_error_get_where());
00489     }
00490     /* un-connect the mask and free the memory*/
00491     cpl_image_accept_all(image_data1) ;
00492     cpl_mask_delete(mask_data1);
00493     cpl_image_accept_all(image_data2) ;
00494     cpl_mask_delete(mask_data2);
00495 
00496     /* Free the memory */
00497 
00498     cpl_imagelist_delete(imglst_data1);
00499     cpl_imagelist_delete(imglst_data2);
00500     cpl_image_delete(image_data1);
00501     cpl_image_delete(image_data2);
00502     cpl_image_delete(image_data1_mask);
00503     cpl_image_delete(image_data2_mask);
00504     cpl_propertylist_delete(header);
00505     cpl_propertylist_delete(qclist_data1);
00506     cpl_propertylist_delete(qclist_data2);
00507 
00508 #endif
00509     /*---------------------------------------------------------------------*/
00510 
00511 
00512 
00513     /* Return */
00514     if (cpl_error_get_code())
00515         return -1 ;
00516     else
00517         return 0 ;
00518 }
00519 
00520 static int table_to_imglst(const char * columname,
00521         const char * columntype,
00522         cpl_imagelist * imglst_target,
00523         const char * columnentry,
00524         cpl_table * table)
00525 {
00526 
00527     int dimenDATA;
00528     int   i, ctarget=0;
00529     cpl_array * array_data=NULL;
00530     cpl_image * image_data=NULL;
00531     cpl_errorstate          prestate = cpl_errorstate_get();
00532     char ** target_type;
00533     cpl_type ctype;
00534 
00535     /* Load extension  Imaging Data  */
00536 
00537     dimenDATA=cpl_table_get_column_dimensions(table, columname);
00538     cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00539     if (dimenDATA != 2) {
00540         return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00541                 "DATA has a wrong dimension");
00542     }
00543     /* Read target type columnentry and store them in the image list */
00544     /* Loop over all "images" stored in DATA and add them to the imagelist */
00545 
00546     if (cpl_table_has_column(table, columntype))
00547     {
00548         target_type=cpl_table_get_data_string(table, columntype);
00549 
00550     }
00551     else
00552     {
00553         return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00554                 "TYPE of the Column not found");
00555     }
00556 
00557     ctype=cpl_table_get_column_type(table, columname);
00558     /*    cpl_msg_info(cpl_func, "Type of the table column: %d",ctype); */
00559 
00560     ctarget=cpl_imagelist_get_size(imglst_target);
00561 
00562     for (i=0; i<cpl_table_get_nrow(table);i++)
00563     {
00564 
00565         array_data=(cpl_array *)cpl_table_get_array(table,columname, i);
00566         if(ctype&CPL_TYPE_INT){
00567             image_data=cpl_image_wrap_int(
00568                     cpl_table_get_column_dimension(table,columname,0 ),
00569                     cpl_table_get_column_dimension(table,columname,1 ),
00570                     cpl_array_get_data_int(array_data) );
00571         }
00572         if(ctype&CPL_TYPE_FLOAT){
00573             image_data=cpl_image_wrap_float(
00574                     cpl_table_get_column_dimension(table,columname,0 ),
00575                     cpl_table_get_column_dimension(table,columname,1 ),
00576                     cpl_array_get_data_float(array_data) );
00577         }
00578         /* Cast the image to float */
00579         /*       image_data=cpl_image_cast(image_data, CPL_TYPE_FLOAT); */
00580 
00581         /*        Append the image to the imagelists */
00582 
00583 
00584         if(strcmp(target_type[i],columnentry)== 0){
00585             cpl_imagelist_set(imglst_target,
00586                     cpl_image_cast(image_data, CPL_TYPE_FLOAT),ctarget++);
00587             cpl_image_unwrap(image_data);
00588             continue;
00589         }
00590 
00591         /*Unwrap processed image manualy if nor Target nor Sky*/
00592         if(image_data!=NULL){
00593             cpl_image_unwrap(image_data);
00594         }
00595 
00596     }
00597 
00598     return (int)cpl_error_get_code();
00599 }
00600 
00601 static cpl_error_code midi_qc_acq(const cpl_image * image,
00602         cpl_propertylist * pro_list)
00603 {
00604 
00605     cpl_array  *parameters = NULL;
00606     cpl_array  *err_params = NULL;
00607     cpl_array  *fit_params = NULL;
00608     double      major=0.0,minor=0.0,angle=0.0,rms=0.0;
00609     double      centroid_x=0.0, centroid_y=0.0;
00610     double      centroid_fwhm_x=0.0, centroid_fwhm_y=0.0;
00611     double      gauss_x=0.0, gauss_y=0.0;
00612     double      gauss_sigma_x=0.0, gauss_sigma_y=0.0;
00613     double      gauss_fwhm_x=0.0, gauss_fwhm_y=0.0;
00614     double      dummy=0.;
00615     int         i=0;
00616 
00617 
00618 
00619     const char  *p[7] = { /* Parameter names */
00620             "Background       ",
00621             "Normalisation    ",
00622             "Correlation      ",
00623             "Center position x",
00624             "Center position y",
00625             "Sigma x          ",
00626             "Sigma y          "};
00627 
00628     parameters = cpl_array_new(7, CPL_TYPE_DOUBLE);
00629     err_params = cpl_array_new(7, CPL_TYPE_DOUBLE);
00630     fit_params = cpl_array_new(7, CPL_TYPE_INT);
00631 
00632     /*All parameter should be fitted*/
00633     for (i = 0; i < 7; i++)
00634         cpl_array_set(fit_params, i, 1);
00635 
00636 
00637     /*Freez the background to a value of zero*/
00638     cpl_array_set(fit_params, 0, 0);
00639     cpl_array_set(parameters, 0,0.);
00640 
00641     /*Fit a gaussian*/
00642     if(cpl_fit_image_gaussian(image, NULL, 30, 30, 20, 20, parameters, NULL,
00643             fit_params, &rms, NULL, NULL, &major, &minor, &angle, NULL)
00644             == CPL_ERROR_NONE &&
00645             !midi_isnan(cpl_array_get(parameters,0,NULL)) &&
00646             !midi_isnan(cpl_array_get(parameters,1,NULL)) &&
00647             !midi_isnan(cpl_array_get(parameters,2,NULL)) &&
00648             !midi_isnan(cpl_array_get(parameters,3,NULL)) &&
00649             !midi_isnan(cpl_array_get(parameters,4,NULL)) &&
00650             !midi_isnan(cpl_array_get(parameters,5,NULL)) &&
00651             !midi_isnan(cpl_array_get(parameters,6,NULL)))
00652     {
00653 
00654 
00655         for (i = 0; i < 7; i++){
00656             cpl_msg_info(cpl_func,"%s: %f",
00657                     p[i], cpl_array_get(parameters,i,NULL));
00658         }
00659 
00660         gauss_x=cpl_array_get(parameters,3,NULL);
00661         gauss_y=cpl_array_get(parameters,4,NULL);
00662         gauss_sigma_x=cpl_array_get(parameters,5,NULL);
00663         gauss_sigma_y=cpl_array_get(parameters,6,NULL);
00664 
00665         /*Get the FWHM at the center of the gaussian fit*/
00666         cpl_image_get_fwhm(image, gauss_x, gauss_y, &gauss_fwhm_x,
00667                 &gauss_fwhm_y);
00668 
00669         if(gauss_fwhm_x<0){
00670             midi_image_get_fwhm(image, gauss_x, gauss_y, &gauss_fwhm_x,
00671                     &dummy);
00672         }
00673         if(gauss_fwhm_y<0){
00674             midi_image_get_fwhm(image, gauss_x, gauss_y, &dummy, &gauss_fwhm_y);
00675 
00676         }
00677 
00678     }
00679     /*Get the centroid of the image*/
00680     centroid_x=cpl_image_get_centroid_x(image);
00681     centroid_y=cpl_image_get_centroid_y(image);
00682     /*Get the FWHM at the centroid position*/
00683     cpl_image_get_fwhm(image, centroid_x, centroid_y, &centroid_fwhm_x,
00684             &centroid_fwhm_y);
00685 
00686 
00687     if(centroid_fwhm_x<0){
00688         midi_image_get_fwhm(image, centroid_x, centroid_y, &centroid_fwhm_x,
00689                 &dummy);
00690     }
00691     if(centroid_fwhm_y<0){
00692         midi_image_get_fwhm(image, centroid_x, centroid_y, &dummy,
00693                 &centroid_fwhm_y);
00694     }
00695 
00696 
00697 
00698     cpl_propertylist_update_float(pro_list,
00699             "ESO QC GAUSS FIT X", (float)gauss_x);
00700     cpl_propertylist_update_float(pro_list,
00701             "ESO QC GAUSS FIT Y", (float)gauss_y);
00702     cpl_propertylist_update_float(pro_list,
00703             "ESO QC GAUSS FIT SIGMA X", (float)gauss_sigma_x);
00704     cpl_propertylist_update_float(pro_list,
00705             "ESO QC GAUSS FIT SIGMA Y", (float)gauss_sigma_y);
00706     cpl_propertylist_update_float(pro_list,
00707             "ESO QC GAUSS MEASURED FWHM AT X", (float)gauss_fwhm_x);
00708     cpl_propertylist_update_float(pro_list,
00709             "ESO QC GAUSS MEASURED FWHM AT Y", (float)gauss_fwhm_y);
00710 
00711     cpl_propertylist_update_float(pro_list,
00712             "ESO QC CENTROID X", (float)centroid_x);
00713     cpl_propertylist_update_float(pro_list,
00714             "ESO QC CENTROID Y", (float)centroid_y);
00715     cpl_propertylist_update_float(pro_list,
00716             "ESO QC CENTROID MEASURED FWHM AT X", (float)centroid_fwhm_x);
00717     cpl_propertylist_update_float(pro_list,
00718             "ESO QC CENTROID MEASURED FWHM AT Y", (float)centroid_fwhm_y);
00719 
00720     if (cpl_msg_get_level() <= CPL_MSG_DEBUG) {
00721         cpl_msg_info(cpl_func,"centroid_x: %f centroid_y: %f", centroid_x,
00722                 centroid_y);
00723         cpl_msg_info(cpl_func,"centroid_fwhm_x: %f centroid_fwhm_y: %f",
00724                 centroid_fwhm_x, centroid_fwhm_y);
00725     }
00726 
00727     /*Free the memory*/
00728     cpl_array_delete(parameters);
00729     cpl_array_delete(err_params);
00730     cpl_array_delete(fit_params);
00731 
00732     return cpl_error_get_code();
00733 }
00734 
00735 /*----------------------------------------------------------------------------*/
00763 /*----------------------------------------------------------------------------*/
00764 static cpl_error_code midi_image_get_fwhm(
00765         const cpl_image *   in,
00766         int                 xpos,
00767         int                 ypos,
00768         double          *   fwhm_x,
00769         double          *   fwhm_y)
00770 {
00771     double              half_max;
00772     const int           minimum_size = 5;
00773     int                 is_rejected;
00774 
00775 
00776     /* Check entries - and initialize *fwhm_{x,y} */
00777     if (fwhm_y != NULL) *fwhm_y = -1;
00778     cpl_ensure_code(fwhm_x,         CPL_ERROR_NULL_INPUT);
00779     *fwhm_x = -1;
00780     cpl_ensure_code(fwhm_y,         CPL_ERROR_NULL_INPUT);
00781 
00782     /* This call will check the validity of image, xpos and ypos */
00783     half_max = 0.5 * cpl_image_get(in, xpos, ypos, &is_rejected);
00784 
00785     cpl_ensure_code(is_rejected >= 0, cpl_error_get_code());
00786     cpl_ensure_code(!is_rejected, CPL_ERROR_DATA_NOT_FOUND);
00787 
00788     cpl_ensure_code(half_max > 0, CPL_ERROR_DATA_NOT_FOUND);
00789 
00790     /* FWHM in x */
00791     if (cpl_image_get_size_x(in) >= minimum_size) {
00792         cpl_errorstate pstate;
00793 
00794         /* Extract the vector centered on the maximum */
00795         cpl_vector * row = cpl_vector_new_from_image_row(in, ypos);
00796 
00797         /* If an error happened, update its location */
00798         cpl_ensure_code(row, cpl_error_get_code());
00799 
00800         pstate = cpl_errorstate_get();
00801 
00802 
00803         /* Compute the FWHM */
00804         if (cpl_errorstate_is_equal(pstate))
00805             *fwhm_x = midi_vector_get_fwhm(row, xpos, half_max);
00806 
00807         cpl_vector_delete(row);
00808 
00809         /* Propagate the error, if any */
00810         cpl_ensure_code(cpl_errorstate_is_equal(pstate), cpl_error_get_code());
00811 
00812     }
00813 
00814     /* FWHM in y */
00815     if (cpl_image_get_size_y(in)>= minimum_size) {
00816         cpl_errorstate pstate;
00817 
00818         /* Extract the vector centered on the maximum */
00819         cpl_vector * col = cpl_vector_new_from_image_column(in, xpos);
00820 
00821         /* If an error happened, update its location */
00822         cpl_ensure_code(col, cpl_error_get_code());
00823 
00824         pstate = cpl_errorstate_get();
00825 
00826 
00827 
00828         /* Compute the FWHM */
00829         if (cpl_errorstate_is_equal(pstate))
00830             *fwhm_y = midi_vector_get_fwhm(col, ypos, half_max);
00831 
00832         cpl_vector_delete(col);
00833 
00834         /* Propagate the error, if any */
00835         cpl_ensure_code(cpl_errorstate_is_equal(pstate), cpl_error_get_code());
00836     }
00837 
00838     return CPL_ERROR_NONE;
00839 }
00840 /*----------------------------------------------------------------------------*/
00853 /*----------------------------------------------------------------------------*/
00854 static double midi_vector_get_fwhm(
00855         const cpl_vector    *   vec,
00856         int                     pos,
00857         double                  half_max)
00858 {
00859     const double * vec_data;
00860     int            nelem;
00861     double         x_left, x_right;
00862     double         y_1, y_2;
00863     int            i;
00864 
00865     /* Check entries */
00866     cpl_ensure(vec,          CPL_ERROR_NULL_INPUT, -1.0);
00867     nelem = cpl_vector_get_size(vec);
00868     cpl_ensure(pos >= 1,     CPL_ERROR_ILLEGAL_INPUT, -1.0);
00869     cpl_ensure(pos <= nelem, CPL_ERROR_ILLEGAL_INPUT, -1.0);
00870 
00871     vec_data = cpl_vector_get_data_const(vec);
00872 
00873     /* Object may be too noisy - or strange in some other way */
00874     if (vec_data[pos - 1] <= half_max) return -1.0;
00875 
00876     /* Find first pair of values, y(i) <= half_max < y(i+1)
00877          on the left of the maximum */
00878     i = pos - 1;
00879 
00880     while ((vec_data[i] > half_max) && (i > 0)) i--;
00881     if (vec_data[i] > half_max) return -1.0;  /* y_1 could not be found */
00882 
00883     y_1 = vec_data[i];
00884     y_2 = vec_data[i+1];
00885 
00886     /* assert ( y_1 <= half_max && half_max < y_2 ); */
00887 
00888     /* Assume linearity between y_1 and y_2 */
00889     x_left = i + (half_max-y_1) / (y_2-y_1);
00890 
00891     /* assert( x_left >= i ); */
00892 
00893     /* Find first pair of values, y(i-1) > half_max >= y(i)
00894          on the right of the maximum */
00895     i = pos - 1;
00896 
00897     while ((vec_data[i] > half_max) && (i < nelem-1)) i++;
00898     if (vec_data[i] > half_max) return -1.0;   /* y_2 could not be found */
00899 
00900     y_1 = vec_data[i-1];
00901     y_2 = vec_data[i];
00902 
00903     /* assert( y_1 > half_max && half_max >= y_2 ); */
00904 
00905     /* Assume linearity between y_1 and y_2 */
00906     x_right = i + (half_max-y_2) / (y_2-y_1);
00907 
00908     /* assert( x_right < i ); */
00909 
00910     if (x_right < x_left || x_right - x_left > FLT_MAX) return -1;
00911 
00912     return x_right - x_left;
00913 }
00914 /*----------------------------------------------------------------------------*/
00918 /*----------------------------------------------------------------------------*/
00919 static int midi_isnan(double value)
00920 {
00921 #if defined HAVE_ISNAN && HAVE_ISNAN
00922     return isnan(value);
00923 #else
00924     return value != value;
00925 #endif
00926 }

Generated on 11 Feb 2011 for MIDI Pipeline Reference Manual by  doxygen 1.6.1