midi_acq.c

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

Generated on 5 Mar 2013 for MIDI Pipeline Reference Manual by  doxygen 1.6.1