sinfo_new_nst.c

00001 /*
00002  * This file is part of the ESO SINFONI Pipeline
00003  * Copyright (C) 2004,2005 European Southern Observatory
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA
00018  */
00019 /*----------------------------------------------------------------------------
00020  
00021      File name    :       spiffi_north_south_test.c
00022    Author       :    A. Modigliani
00023    Created on   :    Sep 17, 2003
00024    Description  : 
00025 
00026  * nsh.h
00027  * Result of a north-south test exposure are 32 continuum spectra of a 
00028  * pinhole that means one spectrum in each slitlet at the same spatial 
00029  * position.
00030  * Each spectrum is fitted in sp[atial direction by a Gaussian to get the 
00031  * sub-pixel positions for each row.
00032  *
00033  * Then the distances are determined in each row and averaged
00034  *
00035  * Result: are distances of each slitlet from each other => 31 values stored 
00036  *  in an ASCII file this Python script needs a frame of a pinhole source with 
00037  *  a continuous spectrum, that is shifted exactly perpendicular to the 
00038  *  slitlets. It fits the spectra in spatial direction by a Gaussian fit 
00039  *  function and therefore determines the sub-pixel position of the source.
00040  *
00041  *  Then the distances of the slitlets from each other are determined and 
00042  *   saved in an ASCII list. 
00043  *
00044 
00045 
00046  ---------------------------------------------------------------------------*/
00047 
00048 #ifdef HAVE_CONFIG_H
00049 #  include <config.h>
00050 #endif
00051 /*----------------------------------------------------------------------------
00052                                 Includes
00053  ---------------------------------------------------------------------------*/
00054 #include "sinfo_new_nst.h"
00055 #include "sinfo_pro_save.h"
00056 #include "sinfo_pro_types.h"
00057 #include "sinfo_functions.h"
00058 #include "sinfo_ns_ini_by_cpl.h"
00059 #include "sinfo_cube_construct.h"
00060 #include "sinfo_utilities.h"
00061 #include "sinfo_utils_wrappers.h"
00062 #include "sinfo_error.h"
00063 #include "sinfo_globals.h"
00064 
00065 /*----------------------------------------------------------------------------
00066                                 Defines
00067  ---------------------------------------------------------------------------*/
00068 
00076 /*----------------------------------------------------------------------------
00077                              Function Definitions
00078  ---------------------------------------------------------------------------*/
00079 
00080 /*----------------------------------------------------------------------------
00081    Function     :       north_south_test()
00082    In           :       ini_file: file name of according .ini file
00083    Out          :       integer (0 if it worked, -1 if it doesn't) 
00084    Job          : Result of a north-south test exposure are 32 continuum 
00085                   spectra of a pinhole    that means one spectrum in each 
00086                   slitlet at the same spatial position.
00087 
00088                   Each spectrum is fitted in sp[atial direction by a Gaussian
00089                   to get the sub-pixel positions for each row.
00090 
00091                   Then the distances are determined in each row and averaged
00092 
00093                   Result: are distances of each slitlet from each other =>
00094                           31 values stored in an ASCII file
00095 
00096 
00097  ---------------------------------------------------------------------------*/
00098 int 
00099 sinfo_new_nst(const char* plugin_id, 
00100               cpl_parameterlist* config, 
00101               cpl_frameset* sof,
00102               cpl_frameset* ref_set)
00103 {
00104   ns_config * cfg=NULL ;
00105   cpl_imagelist * list_object=NULL ;
00106   cpl_imagelist * list_off=NULL;
00107   cpl_image * im_on=NULL;
00108   cpl_image * im_on_sub=NULL ;
00109   cpl_image * im_on_ind=NULL ;
00110   cpl_image * im_on_gauss=NULL ;
00111   cpl_image * im_mask=NULL ;
00112   cpl_image * im_off=NULL ;
00113 
00114   char* name=NULL;
00115   char tbl_name[MAX_NAME_SIZE];
00116   int typ=0;
00117   int i = 0;
00118 
00119   int nob =0;
00120   int nof =0;
00121 
00122   float* distances=NULL;
00123   cpl_table* tbl_dist=NULL;
00124 
00125   cpl_vector* qc_dist=NULL;
00126 
00127   double  qc_dist_mean=0;
00128   double  qc_dist_stdev=0;
00129   cpl_frameset* raw=NULL;
00130 
00131   cpl_table* qclog_tbl=NULL;
00132   char key_value[MAX_NAME_SIZE];
00133   char key_name[MAX_NAME_SIZE];
00134   int  no=0;
00135   double lo_cut=0.;
00136   double hi_cut=0.;
00137   int pdensity=0;
00138 
00139   /* 
00140        -----------------------------------------------------------------
00141        1) parse the file names and parameters to the ns_config data 
00142           structure cfg
00143        -----------------------------------------------------------------
00144   */
00145 
00146   /* 
00147       parse the file names and parameters to the ns_config data structure cfg 
00148   */
00149 
00150   sinfo_msg("Parse cpl input");
00151   check_nomsg(raw=cpl_frameset_new());
00152   cknull(cfg = sinfo_parse_cpl_input_ns(config,sof,&raw),
00153      "could not parse cpl input!") ;
00154 
00155   if (cfg->maskInd == 1) {
00156     if(sinfo_is_fits_file(cfg->mask) != 1) {
00157       sinfo_msg_error("Input file %s is not FITS",cfg->mask); 
00158       goto cleanup;
00159     }
00160   }
00161   /*
00162      --------------------------------------------------------------------
00163      stack the frames in data cubes and take the clean mean of all frames
00164      --------------------------------------------------------------------
00165   */
00166   /* allocate memory for lists of object and off-frames */
00167   sinfo_msg("stack the frames in data cubes");
00168   check(list_object = cpl_imagelist_new(),
00169           "could not allocate memory");
00170 
00171   if (cfg->noff > 0 ) {
00172     check(list_off = cpl_imagelist_new(),
00173       "could not allocate memory");
00174   }
00175  
00176   /*
00177       #build different image lists for the different cases----
00178   */
00179   sinfo_msg("build different image lists for the different cases");
00180   nob = 0;
00181   nof = 0;
00182 
00183   for (i=0; i< cfg->nframes; i++){
00184     name = cfg->framelist[i];
00185     if(sinfo_is_fits_file(name) != 1) {
00186       sinfo_msg_error("Input file %s is not FITS",name);
00187       goto cleanup;
00188     } else {
00189       typ = sinfo_new_intarray_get_value( cfg->frametype, i );
00190       if (typ == 1) {
00191     cpl_imagelist_set(list_object,
00192                           cpl_image_load(name,CPL_TYPE_FLOAT,0,0),nob);
00193     nob = nob + 1;
00194       } else {
00195     cpl_imagelist_set(list_off,
00196                           cpl_image_load(name,CPL_TYPE_FLOAT,0,0),nof);
00197     nof = nof + 1;
00198       }
00199     }
00200   }
00201 
00202   if (cfg->noff != nof || cfg->nobj != nob ){       
00203       sinfo_msg_error("something wrong with the number of the "
00204                       "different types of frames");
00205       goto cleanup;
00206   }
00207   
00208   /*
00209   #---take the average of the different cubes -------------
00210   */
00211   sinfo_msg("take the average of the different cubes");
00212 
00213   check_nomsg(no=cpl_imagelist_get_size(list_object));
00214   lo_cut=(floor)(cfg->loReject*no+0.5);
00215   hi_cut=(floor)(cfg->hiReject*no+0.5);
00216   check(im_on=cpl_imagelist_collapse_minmax_create(list_object,lo_cut,hi_cut),
00217               "sinfo_average_with_rejection failed" );
00218 
00219   if (cfg->noff != 0) {
00220     /*
00221       im_off = sinfo_average_with_rejection( cube_off, 
00222                                        cfg->loReject, cfg->hiReject );
00223     */
00224     check_nomsg(no=cpl_imagelist_get_size(list_off));
00225     lo_cut=(floor)(cfg->loReject*no+0.5);
00226     hi_cut=(floor)(cfg->hiReject*no+0.5);
00227     check(im_off=cpl_imagelist_collapse_minmax_create(list_off,lo_cut,hi_cut),
00228       "sinfo_average_with_rejection failed" );
00229       sinfo_free_imagelist(&list_off);
00230   }
00231   sinfo_free_imagelist(&list_object);
00232 
00233   /*
00234   #finally, subtract off from on frames and store the result in the object cube
00235   */
00236 
00237   if (cfg->noff != 0) {
00238     sinfo_msg("subtract off from on frames");
00239     check(im_on_sub = cpl_image_subtract_create(im_on, im_off),
00240       "sinfo_sub_image failed" );
00241 
00242     sinfo_free_image(&im_on);
00243     sinfo_free_image(&im_off);
00244   } else {
00245     check_nomsg(im_on_sub = cpl_image_duplicate(im_on));
00246     sinfo_free_image(&im_on);
00247   }
00248 
00249   /*
00250   #---------------------------------------------------------
00251   # convolution with Gaussian if recommended
00252   #---------------------------------------------------------
00253   */
00254   if (cfg->gaussInd == 1) {
00255     sinfo_msg("convolution with Gaussian");
00256     cknull(im_on_gauss = sinfo_new_convolve_ns_image_by_gauss(im_on_sub, 
00257                                                               cfg->hw),
00258                      "could not carry out sinfo_convolveNSImageByGauss" );
00259 
00260     sinfo_free_image(&im_on_sub);
00261     check_nomsg(im_on_sub = cpl_image_duplicate(im_on_gauss));
00262     sinfo_free_image(&im_on_gauss);
00263   }
00264 
00265   /*
00266   #---------------------------------------------------------
00267   # static bad pixel indication
00268   #---------------------------------------------------------
00269   */
00270 
00271    if (cfg->maskInd == 1) {
00272      sinfo_msg("static bad pixel indication");
00273      check(im_mask = cpl_image_load(cfg->mask,CPL_TYPE_FLOAT,0,0),
00274        "could not load static bad pixel mask" );
00275      cknull(im_on_ind = sinfo_new_mult_image_by_mask(im_on_sub, im_mask),
00276         "could not carry out sinfo_multImageByMask" );
00277      sinfo_free_image(&im_mask);
00278      sinfo_free_image(&im_on_sub);
00279   
00280    } else {
00281       check_nomsg(im_on_ind = cpl_image_duplicate(im_on_sub));
00282       sinfo_free_image(&im_on_sub);
00283    }
00284 
00285    if(pdensity > 1 && strcmp(plugin_id,"sinfo_rec_distortion")!=0) {
00286      ck0(sinfo_pro_save_ima(im_on_ind,ref_set,sof,cfg->fitsname,
00287                 PRO_MASTER_SLIT,NULL,plugin_id,config),
00288      "cannot save ima %s", cfg->fitsname);
00289    }
00290  
00291    /*
00292    #---------------------------------------------------------
00293    # do the north - south - test
00294    #---------------------------------------------------------
00295    */
00296    sinfo_msg("Do the north - south - test");
00297 
00298    cknull(distances = sinfo_north_south_test(im_on_ind, 
00299                        cfg->nslits,
00300                        cfg->halfWidth,
00301                        cfg->fwhm ,
00302                        cfg->minDiff,
00303                        cfg->estimated_dist,
00304                                            cfg->devtol, 
00305                                            IMA_PIX_START, 
00306                                            IMA_PIX_END ),
00307       "North South Test distance determination failed");
00308    sinfo_free_image(&im_on_ind);
00309     /*
00310    sinfo_new_parameter_to_ascii(distances, cfg->nslits - 1, cfg->outName);
00311    */
00312    check_nomsg(tbl_dist = cpl_table_new(cfg->nslits - 1));
00313    check_nomsg(cpl_table_new_column(tbl_dist,"slitlet_distance",
00314                     CPL_TYPE_FLOAT));
00315    check_nomsg(cpl_table_copy_data_float(tbl_dist,"slitlet_distance",
00316                      distances));
00317 
00318    strcpy(tbl_name,cfg->outName);
00319 
00320    check_nomsg(qclog_tbl = cpl_table_new(cfg->nslits + 1));
00321    check_nomsg(cpl_table_new_column(qclog_tbl,"key_name", CPL_TYPE_STRING));
00322    check_nomsg(cpl_table_new_column(qclog_tbl,"key_type", CPL_TYPE_STRING));
00323    check_nomsg(cpl_table_new_column(qclog_tbl,"key_value", CPL_TYPE_STRING));
00324    check_nomsg(cpl_table_new_column(qclog_tbl,"key_help", CPL_TYPE_STRING));
00325 
00326    check_nomsg(qc_dist=cpl_vector_new(cfg->nslits - 1));
00327  
00328    for(i=0;i<cfg->nslits - 1;i++) {
00329          snprintf(key_name,MAX_NAME_SIZE-1,"%s%i","QC SL DIST",i);
00330          cpl_table_set_string(qclog_tbl,"key_name",i,key_name);
00331          cpl_table_set_string(qclog_tbl,"key_type",i,"CPL_TYPE_DOUBLE");
00332          snprintf(key_value,MAX_NAME_SIZE-1,"%g",distances[i]);
00333          cpl_table_set_string(qclog_tbl,"key_value",i,key_value);
00334          cpl_table_set_string(qclog_tbl,"key_help",i,"Slitlet distance");
00335 
00336          cpl_vector_set(qc_dist,i,distances[i]);
00337    }
00338    check_nomsg(qc_dist_mean=cpl_vector_get_mean(qc_dist));
00339    check_nomsg(qc_dist_stdev=cpl_vector_get_stdev(qc_dist));
00340 
00341    cpl_table_set_string(qclog_tbl,"key_name",cfg->nslits-1,"QC SL DISTAVG");
00342    cpl_table_set_string(qclog_tbl,"key_type",cfg->nslits-1,"CPL_TYPE_DOUBLE");
00343    snprintf(key_value,MAX_NAME_SIZE-1,"%g",cpl_vector_get_mean(qc_dist));
00344    cpl_table_set_string(qclog_tbl,"key_value",cfg->nslits-1,key_value);
00345    cpl_table_set_string(qclog_tbl,"key_help",cfg->nslits-1,
00346                                   "Average Slitlet distance");
00347 
00348    cpl_table_set_string(qclog_tbl,"key_name",cfg->nslits,"QC SL DISTRMS");
00349    cpl_table_set_string(qclog_tbl,"key_type",cfg->nslits,"CPL_TYPE_DOUBLE");
00350    snprintf(key_value,MAX_NAME_SIZE-1,"%g",qc_dist_stdev);
00351    cpl_table_set_string(qclog_tbl,"key_value",cfg->nslits,key_value);
00352    cpl_table_set_string(qclog_tbl,"key_help",cfg->nslits,
00353                         "RMS Slitlet distance");
00354 
00355    ck0(sinfo_pro_save_tbl(tbl_dist,ref_set,sof,tbl_name,
00356            PRO_SLITLETS_DISTANCE,qclog_tbl,plugin_id,config),
00357        "cannot dump tbl %s", tbl_name);
00358 
00359    sinfo_free_my_vector(&qc_dist);
00360    sinfo_free_table(&tbl_dist);
00361    sinfo_free_table(&qclog_tbl);
00362    sinfo_free_float(&distances);
00363    sinfo_ns_free (&cfg);
00364    sinfo_free_frameset(&raw);
00365 
00366   return 0;
00367 
00368   cleanup:
00369   sinfo_free_my_vector(&qc_dist);
00370   sinfo_free_table(&tbl_dist);
00371   sinfo_free_table(&qclog_tbl);
00372   sinfo_free_float(&distances);
00373   sinfo_free_table(&tbl_dist);
00374   sinfo_free_table(&qclog_tbl);
00375   sinfo_free_imagelist(&list_object);
00376   sinfo_free_imagelist(&list_off);
00377   sinfo_free_image(&im_on);
00378   sinfo_free_image(&im_mask);
00379   sinfo_free_image(&im_on_gauss);
00380   sinfo_free_image(&im_on_sub);
00381   sinfo_free_image(&im_off);
00382   sinfo_free_image(&im_on_ind);
00383   sinfo_ns_free (&cfg);
00384   sinfo_free_frameset(&raw);
00385   return -1;
00386 
00387 
00388 }
00389 

Generated on 3 Mar 2013 for SINFONI Pipeline Reference Manual by  doxygen 1.6.1