uves_reduce_mflat_combine.c

00001 /*                                                                              *
00002  *   This file is part of the ESO UVES Pipeline                                 *
00003  *   Copyright (C) 2004,2005 European Southern Observatory                      *
00004  *                                                                              *
00005  *   This library 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  * $Author: amodigli $
00022  * $Date: 2010/12/16 16:57:40 $
00023  * $Revision: 1.9 $
00024  */
00025 
00026 #ifdef HAVE_CONFIG_H
00027 #  include <config.h>
00028 #endif
00029 
00030 /*----------------------------------------------------------------------------*/
00035 /*----------------------------------------------------------------------------*/
00040 /*-----------------------------------------------------------------------------
00041                                 Includes
00042  -----------------------------------------------------------------------------*/
00043 #include <uves_reduce_mflat_combine.h>
00044 
00045 #include <uves.h>
00046 #include <uves_backsub.h>
00047 #include <uves_chip.h>
00048 #include <uves_dfs.h>
00049 #include <uves_pfits.h>
00050 #include <uves_parameters.h>
00051 #include <uves_utils.h>
00052 #include <uves_utils_wrappers.h>
00053 #include <uves_qclog.h>
00054 #include <uves_error.h>
00055 #include <uves_msg.h>
00056 
00057 #include <cpl.h>
00058 #include <float.h>
00059 #include <string.h>
00060 /*-----------------------------------------------------------------------------
00061                             Functions prototypes
00062  -----------------------------------------------------------------------------*/
00063 
00064 static void 
00065 uves_reduce_mflat_combine(cpl_frameset *frames, 
00066                           const cpl_parameterlist *parameters,
00067                           bool flames,
00068                           const char *recipe_id, 
00069                           const char *starttime);
00070 
00071 /*-----------------------------------------------------------------------------
00072                             Implementation
00073  -----------------------------------------------------------------------------*/
00074 const char * const uves_mflat_combine_desc =
00075 "This recipe combined a MASTER_FLAT_xxxx with a MASTER_DFLAT_xxxx\n"
00076 "Input are:\n" 
00077 "a master flat (MASTER_FLAT_xxxx)\n"
00078 "a master dflat (MASTER_DFLAT_xxxx)\n"
00079 "an order table (ORDER_TABLE_xxxx)\n"
00080 "provided for each chip (xxxx = BLUE, REDL, REDU). \n"
00081 "Output is a MASTER_FLAT_xxxx\n";
00082 
00083 /*----------------------------------------------------------------------------*/
00090 /*----------------------------------------------------------------------------*/
00091 int
00092 uves_mflat_combine_define_parameters_body(cpl_parameterlist *parameters, 
00093                   const char *recipe_id)
00094 {
00095 
00096     /*****************
00097     *    General    *
00098     *****************/
00099    if (uves_define_global_parameters(parameters) != CPL_ERROR_NONE)
00100    {
00101       return -1;
00102    }
00103 
00104     return (cpl_error_get_code() != CPL_ERROR_NONE);
00105 }
00106 
00107 
00108 
00109 
00110 /*----------------------------------------------------------------------------*/
00122 /*----------------------------------------------------------------------------*/
00123 void
00124 uves_mflat_combine_exe_body(cpl_frameset *frames, 
00125                             const cpl_parameterlist *parameters,
00126                             const char *starttime,
00127                             const char *recipe_id)
00128 {
00129    bool flames = false;
00130    check_nomsg(uves_reduce_mflat_combine(frames, parameters,flames,recipe_id,
00131                                    starttime));
00132     
00133   cleanup:
00134    return;
00135 }
00136 
00137 /*----------------------------------------------------------------------------*/
00153 cpl_image*
00154 uves_combine_flats(const cpl_frame* frm_flat,
00155                    const cpl_frame* frm_dflat,
00156                    cpl_frameset* frames,
00157                    bool flames)
00158 {
00159 
00160 
00161    const char                *   name_flat=NULL ;
00162    const char                *   name_dflat=NULL ;
00163    const char *ordertable_filename = "";
00164 
00165    cpl_image*              ima_flat=NULL;
00166    cpl_image*              ima_dflat=NULL;
00167    cpl_image*              ima_cflat=NULL;
00168    cpl_image*              ima_mask=NULL;
00169 
00170    int sx=0;
00171    int sy=0;
00172    int j=0;
00173    int i=0;
00174  
00175    double* point_mask=NULL;
00176 
00177    double xpos=0;
00178    double ypos=0;
00179   
00180    double order_ref=0;
00181    int ypos_min=0;
00182    int ypos_max=0;
00183    int ypos_cen=0;
00184    int xpos_cen=0;
00185    int xrad=5;
00186    int yrad=5;
00187    int llx=0;
00188    int lly=0;
00189    int urx=0;
00190    int ury=0;
00191    double dflux=0;
00192    double fflux=0;
00193    double scale=0;
00194 
00195    /* Input */
00196    /* Order table */
00197    cpl_table        *ordertable            = NULL;
00198    uves_propertylist *ordertable_header     = NULL;
00199    polynomial       *order_locations       = NULL;
00200    cpl_table        *traces                = NULL;
00201   const char *chip_name = "";
00202   enum uves_chip chip=UVES_CHIP_BLUE;
00203 
00204  
00205  
00206    name_flat=cpl_frame_get_filename(frm_flat);
00207    name_dflat=cpl_frame_get_filename(frm_dflat);
00208    check_nomsg(ima_flat=cpl_image_load(name_flat,CPL_TYPE_DOUBLE,0,0));
00209    check_nomsg(ima_dflat=cpl_image_load(name_dflat,CPL_TYPE_DOUBLE,0,0));
00210 
00211    /* check size flat is same as size dflat */
00212    sx=cpl_image_get_size_x(ima_flat);
00213    sy=cpl_image_get_size_y(ima_flat);
00214    assure(sx==cpl_image_get_size_x(ima_dflat),CPL_ERROR_ILLEGAL_INPUT,
00215           "illagal x size");
00216    assure(sy==cpl_image_get_size_y(ima_dflat),CPL_ERROR_ILLEGAL_INPUT,
00217           "illagal y size");
00218 
00219 
00220  
00221    check_nomsg( chip_name = UVES_CHIP_ID(chip));
00222    uves_msg("Combining %s chip", uves_chip_tostring_upper(chip));
00223    
00224    check( uves_load_ordertable(frames,
00225                                flames,
00226                                chip_name,
00227                                &ordertable_filename, 
00228                                &ordertable,
00229                                &ordertable_header, 
00230                                NULL,
00231                                &order_locations,
00232                                &traces, NULL, NULL,
00233                                NULL, NULL, /* fibre_pos,fibre_mask */
00234                                chip,
00235                                false),
00236           "Could not load order table");
00237    uves_msg("Using order table in '%s'", ordertable_filename);
00238 
00239    /* Do real job */
00240    order_ref=7;
00241    ypos=uves_polynomial_evaluate_2d(order_locations,0,order_ref);
00242    ypos+=uves_polynomial_evaluate_2d(order_locations,0,order_ref+1);
00243    ypos/=2.;
00244    ypos_min=(int)ypos;
00245 
00246 
00247    ypos=uves_polynomial_evaluate_2d(order_locations,(double)sx,order_ref);
00248    ypos+=uves_polynomial_evaluate_2d(order_locations,(double)sx,order_ref+1);
00249    ypos/=2.;
00250    ypos_max=(int)ypos;
00251  
00252    uves_msg_debug("ypos min=%d max=%d",ypos_min,ypos_max);
00253    ima_mask = cpl_image_new(sx,sy,CPL_TYPE_DOUBLE);
00254    point_mask=cpl_image_get_data_double(ima_mask);
00255    /* set upper part of mask to 1 */
00256    for(j=(int)ypos_max;j<sy;j++) {
00257       for(i=0;i<sx;i++) {
00258          point_mask[j*sx+i]=1.;
00259       }
00260    }
00261 
00262    /* in transition region (only) make the check */
00263    for(j=ypos_min;j<ypos_max;j++) {
00264       for(i=0;i<sx;i++) {
00265          xpos=(double)i;
00266          /* Here the order trace pass through the order center 
00267             but we want to have the trace in the inter order region
00268             y=(yc1+yc2)/2; where yc1 and yc2 are the two position at 
00269                            order center and order+1 center
00270          */ 
00271          ypos= uves_polynomial_evaluate_2d(order_locations,xpos,order_ref);
00272          ypos+=uves_polynomial_evaluate_2d(order_locations,xpos,order_ref+1);
00273          ypos/=2.;
00274          if(j > ypos)  {
00275             point_mask[j*sx+i] = 1.;
00276          }
00277       }
00278    }
00279 
00280 /*
00281    cpl_image_save(ima_mask,"ima_mask_ord.fits", CPL_BPP_IEEE_FLOAT,NULL,
00282                   CPL_IO_DEFAULT);
00283 
00284 */
00285    /*determine ref flux on flat (order_ref+1) */
00286    xpos_cen=sx/2;
00287    llx=xpos_cen-xrad;
00288    urx=xpos_cen+xrad; 
00289    ypos= uves_polynomial_evaluate_2d(order_locations,(double)xpos_cen,
00290                                      order_ref+1);
00291    ypos_cen=(int)ypos;
00292    lly=ypos_cen-yrad;
00293    ury=ypos_cen+yrad;
00294    fflux=cpl_image_get_median_window(ima_flat,llx,lly,urx,ury);
00295 
00296 
00297 
00298    /*determine ref flux on dflat (order_ref) */
00299    ypos=uves_polynomial_evaluate_2d(order_locations,(double)xpos_cen,order_ref);
00300    ypos_cen=(int)ypos;
00301    lly=ypos_cen-yrad;
00302    ury=ypos_cen+yrad;
00303    dflux=cpl_image_get_median_window(ima_dflat,llx,lly,urx,ury);
00304  
00305 
00306    scale=fflux/dflux;
00307 
00308    uves_msg_debug("flux: n=%g d=%g s=%g",fflux,dflux,scale);
00309 
00310    /* combine images */
00311    ima_cflat=cpl_image_duplicate(ima_flat);
00312    cpl_image_multiply(ima_cflat,ima_mask);
00313    cpl_image_multiply_scalar(ima_mask,-1.);
00314    cpl_image_add_scalar(ima_mask,1.);
00315    cpl_image_multiply(ima_dflat,ima_mask);
00316    cpl_image_multiply_scalar(ima_dflat,scale);
00317    cpl_image_add(ima_cflat,ima_dflat);
00318 /*
00319    cpl_image_save(ima_cflat,"ima_cflat.fits", CPL_BPP_IEEE_FLOAT,NULL,
00320                   CPL_IO_DEFAULT);
00321 */
00322 
00323 
00324   cleanup:
00325 
00326    uves_free_table(&ordertable);
00327    uves_free_propertylist(&ordertable_header);
00328    uves_polynomial_delete(&order_locations);
00329    uves_free_table(&traces);
00330    uves_free_image(&ima_flat);
00331    uves_free_image(&ima_dflat);
00332    uves_free_image(&ima_mask);
00333 
00334    return ima_cflat;
00335 }
00336 /*----------------------------------------------------------------------------*/
00346 /*----------------------------------------------------------------------------*/
00347 static void
00348 uves_reduce_mflat_combine(cpl_frameset *frames, 
00349                           const cpl_parameterlist *parameters,
00350                           bool flames,
00351                           const char *recipe_id,
00352                           const char *starttime)
00353 {
00354    bool DEBUG;
00355    cpl_frame           *   frm_dflat=NULL ;
00356    cpl_frame           *   frm_flat=NULL ;
00357     const char                *   name_flat=NULL ;
00358    const char                *   name_dflat=NULL ;
00359    uves_propertylist* hflat=NULL;
00360 
00361    /* Output */
00362    cpl_table *qclog[]                  = {NULL, NULL};
00363    uves_propertylist *product_header=NULL;
00364    cpl_image* ima_cflat=NULL;
00365  
00366    /* Local variables */
00367    //char *product_filename = "master_cflat_blue.fits";
00368    char *product_filename = NULL;
00369    const char *product_tag[2] = {NULL, NULL};
00370    char pro_filename[255];
00371    //const char *product_tag = "MASTER_FLAT_BLUE";
00372    const char* PROCESS_CHIP=NULL;
00373    enum uves_chip chip;
00374    int raw_index=0;
00375    bool blue=true;
00376 
00377    /* Read recipe parameters */
00378    {
00379       /* General */
00380       check( uves_get_parameter(parameters, NULL, "uves", "debug", 
00381                                 CPL_TYPE_BOOL  , &DEBUG ),
00382              "Could not read parameter");
00383    }
00384    check( uves_get_parameter(parameters, NULL, "uves", "process_chip", 
00385                              CPL_TYPE_STRING, &PROCESS_CHIP), 
00386           "Could not read parameter");
00387 
00388    uves_string_toupper((char*)PROCESS_CHIP);
00389 
00390    for (chip = uves_chip_get_first(blue); 
00391         chip != UVES_CHIP_INVALID; 
00392         chip = uves_chip_get_next(chip))
00393    {
00394       if (
00395         cpl_frameset_find(frames,UVES_MASTER_FLAT(chip)) != NULL || 
00396         cpl_frameset_find(frames,UVES_MASTER_DFLAT(chip)) != NULL 
00397          )
00398       {
00399 
00400          if(chip == UVES_CHIP_BLUE) {
00401             blue = true;
00402          }
00403          else {
00404             blue = false;
00405          }
00406       } else {
00407             blue = false;
00408       }
00409    }
00410    
00411    /* Load and check input frames */
00412    /* Loop over one or two chips */
00413    for (chip = uves_chip_get_first(blue); 
00414         chip != UVES_CHIP_INVALID; 
00415         chip = uves_chip_get_next(chip))
00416    {
00417 
00418       if(strcmp(PROCESS_CHIP,"REDU") == 0) {
00419          chip = uves_chip_get_next(chip);
00420       }
00421         
00422       raw_index = uves_chip_get_index(chip);
00423         
00424       uves_msg("Processing %s chip", uves_chip_tostring_upper(chip));
00425 
00426  
00427       frm_flat=cpl_frameset_find(frames,UVES_MASTER_FLAT(chip));
00428       frm_dflat=cpl_frameset_find(frames,UVES_MASTER_DFLAT(chip));
00429  
00430       if(frm_flat == NULL && frm_dflat == NULL) {
00431          uves_msg_error("A %s or %s frame or both must be provided in input",
00432                         UVES_MASTER_FLAT(chip),UVES_MASTER_DFLAT(chip));
00433          goto cleanup;
00434       }
00435       else if(frm_flat == NULL && frm_dflat != NULL) {
00436          name_dflat=cpl_frame_get_filename(frm_dflat); 
00437          hflat=uves_propertylist_load(name_dflat,0);
00438          ima_cflat=cpl_image_load(name_dflat,CPL_TYPE_DOUBLE,0,0);
00439       } else if (frm_flat != NULL && frm_dflat == NULL) {
00440          name_flat=cpl_frame_get_filename(frm_flat); 
00441          hflat=uves_propertylist_load(name_flat,0);
00442          ima_cflat=cpl_image_load(name_flat,CPL_TYPE_DOUBLE,0,0);
00443       } else {
00444          check_nomsg(name_dflat=cpl_frame_get_filename(frm_dflat));
00445          check_nomsg(hflat=uves_propertylist_load(name_dflat,0));
00446 
00447          check_nomsg(ima_cflat=uves_combine_flats(frm_flat,frm_dflat,frames,
00448                                                   flames));
00449   
00450       }
00451 
00452       cpl_free(product_filename);
00453       check( product_filename = uves_masterflat_filename(chip),
00454             "Error getting filename");
00455 
00456 
00457       sprintf(pro_filename,product_filename);
00458       product_header=uves_propertylist_duplicate(hflat);
00459       product_tag[uves_chip_get_index(chip)] = UVES_MASTER_FLAT(chip);
00460    
00461       check( uves_frameset_insert(
00462                 frames,
00463                 ima_cflat,
00464                 CPL_FRAME_GROUP_PRODUCT,
00465                 CPL_FRAME_TYPE_IMAGE,
00466                 CPL_FRAME_LEVEL_INTERMEDIATE,
00467                 pro_filename,
00468                 product_tag[raw_index],
00469                 hflat,
00470                 product_header,
00471                 NULL,
00472                 parameters,
00473                 recipe_id,
00474                 PACKAGE "/" PACKAGE_VERSION, qclog,
00475                 starttime, true, UVES_ALL_STATS),
00476              "Could not add master flat %s %s to frameset", 
00477              pro_filename, product_tag[raw_index]);
00478 
00479       uves_msg("Master flat %s %s added to frameset", 
00480                pro_filename, product_tag[raw_index]);
00481 
00482 
00483       if(strcmp(PROCESS_CHIP,"REDL") == 0) {
00484          chip = uves_chip_get_next(chip);
00485       }
00486 
00487 
00488      uves_free_image(&ima_cflat);
00489      uves_qclog_delete(&qclog[0]);
00490      uves_free_propertylist(&product_header);
00491      uves_free_propertylist(&hflat);
00492     
00493    } /* For each chip */
00494     
00495 
00496   cleanup:
00497   
00498   uves_free_image(&ima_cflat);
00499   uves_qclog_delete(&qclog[0]);
00500   uves_free_propertylist(&product_header);
00501   uves_free_propertylist(&hflat);
00502   cpl_free(product_filename);
00503 
00504    return;
00505 }
00506 
00507 

Generated on 8 Mar 2011 for UVES Pipeline Reference Manual by  doxygen 1.6.1