isaac_img_slitpos.c

00001 /* $Id: isaac_img_slitpos.c,v 1.35 2012/01/12 12:00:42 llundin Exp $
00002  *
00003  * This file is part of the ISAAC 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: llundin $
00023  * $Date: 2012/01/12 12:00:42 $
00024  * $Revision: 1.35 $
00025  * $Name: isaac-6_1_0 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <string.h>
00037 #include <math.h>
00038 #include <cpl.h>
00039 
00040 #include "irplib_utils.h"
00041 #include "irplib_slitpos.h"
00042 
00043 #include "isaac_utils.h"
00044 #include "isaac_pfits.h"
00045 #include "isaac_dfs.h"
00046 
00047 /*-----------------------------------------------------------------------------
00048                             Functions prototypes
00049  -----------------------------------------------------------------------------*/
00050 
00051 static int isaac_img_slitpos_create(cpl_plugin *);
00052 static int isaac_img_slitpos_exec(cpl_plugin *);
00053 static int isaac_img_slitpos_destroy(cpl_plugin *);
00054 static int isaac_img_slitpos(cpl_parameterlist *, cpl_frameset *);
00055 static int isaac_img_slitpos_save(cpl_table *, int, cpl_parameterlist *, 
00056         cpl_frameset *);
00057 
00058 /*-----------------------------------------------------------------------------
00059                             Static variables
00060  -----------------------------------------------------------------------------*/
00061 
00062 static struct {
00063     /* Inputs */
00064     double      slit_width;
00065     int         products_flag;
00066     /* Outputs */
00067     double      angle;
00068     int         slit_length;
00069     double      xpos;
00070     double      ypos;
00071     double      slit_flux;
00072 } isaac_img_slitpos_config;
00073 
00074 static char isaac_img_slitpos_description[] = 
00075 "isaac_img_slitpos -- ISAAC imaging slit position recipe.\n"
00076 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00077 "raw-file.fits " ISAAC_IMG_SLITPOS_RAW " or"
00078 "raw-file.fits " ISAAC_IMG_SLITPOS_CAL_RAW "\n";
00079 
00080 /*-----------------------------------------------------------------------------
00081                                 Functions code
00082  -----------------------------------------------------------------------------*/
00083 
00084 /*----------------------------------------------------------------------------*/
00092 /*----------------------------------------------------------------------------*/
00093 int cpl_plugin_get_info(cpl_pluginlist * list)
00094 {
00095     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe));
00096     cpl_plugin  *   plugin = &recipe->interface;
00097 
00098     cpl_plugin_init(plugin,
00099                     CPL_PLUGIN_API,
00100                     ISAAC_BINARY_VERSION,
00101                     CPL_PLUGIN_TYPE_RECIPE,
00102                     "isaac_img_slitpos",
00103                     "Slit position recipe",
00104                     isaac_img_slitpos_description,
00105                     "Lars Lundin",
00106                     PACKAGE_BUGREPORT,
00107                     isaac_get_license(),
00108                     isaac_img_slitpos_create,
00109                     isaac_img_slitpos_exec,
00110                     isaac_img_slitpos_destroy);
00111 
00112     cpl_pluginlist_append(list, plugin);
00113     
00114     return 0;
00115 }
00116 
00117 /*----------------------------------------------------------------------------*/
00126 /*----------------------------------------------------------------------------*/
00127 static int isaac_img_slitpos_create(cpl_plugin * plugin)
00128 {
00129     cpl_recipe      * recipe;
00130     cpl_parameter   * p;
00131 
00132     /* Get the recipe out of the plugin */
00133     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00134         recipe = (cpl_recipe *)plugin;
00135     else return -1;
00136 
00137     /* Create the parameters list in the cpl_recipe object */
00138     recipe->parameters = cpl_parameterlist_new();
00139 
00140     /* Fill the parameters list */
00141     /* --slit_w */
00142     p = cpl_parameter_new_value("isaac.isaac_img_slitpos.slit_width",
00143             CPL_TYPE_DOUBLE, "Slit width", "isaac.isaac_img_slitpos",
00144             20.0);
00145     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "slit_w");
00146     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00147     cpl_parameterlist_append(recipe->parameters, p);
00148     /* --prod */
00149     p = cpl_parameter_new_value("isaac.isaac_img_slitpos.products",
00150             CPL_TYPE_BOOL, "flag to create the products", 
00151             "isaac.isaac_img_slitpos", TRUE);
00152     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "prod");
00153     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00154     cpl_parameterlist_append(recipe->parameters, p);
00155 
00156     /* Return */
00157     return 0;
00158 }
00159 
00160 /*----------------------------------------------------------------------------*/
00166 /*----------------------------------------------------------------------------*/
00167 static int isaac_img_slitpos_exec(cpl_plugin * plugin)
00168 {
00169     cpl_recipe  *   recipe;
00170 
00171     /* Get the recipe out of the plugin */
00172     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00173         recipe = (cpl_recipe *)plugin;
00174     else return -1;
00175 
00176     return isaac_img_slitpos(recipe->parameters, recipe->frames);
00177 }
00178 
00179 /*----------------------------------------------------------------------------*/
00185 /*----------------------------------------------------------------------------*/
00186 static int isaac_img_slitpos_destroy(cpl_plugin * plugin)
00187 {
00188     cpl_recipe  *   recipe;
00189 
00190     /* Get the recipe out of the plugin */
00191     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00192         recipe = (cpl_recipe *)plugin;
00193     else return -1;
00194 
00195     cpl_parameterlist_delete(recipe->parameters);
00196     return 0;
00197 }
00198 
00199 /*----------------------------------------------------------------------------*/
00206 /*----------------------------------------------------------------------------*/
00207 static int isaac_img_slitpos(
00208         cpl_parameterlist   *   parlist, 
00209         cpl_frameset        *   framelist)
00210 {
00211     cpl_parameter       *   par;
00212     cpl_imagelist       *   imlist;
00213     cpl_propertylist    *   plist;
00214     cpl_frame           *   cur_frame;
00215     cpl_frameset        *   slitframes;
00216     const char          *   sval;
00217     cpl_table           *   out_table;
00218     int                     slit_length;
00219     int                     with_dark;
00220     int                     slit_ind;
00221     cpl_image           *   dark;
00222     double                  loc_pi;
00223     cpl_boolean             did_reduce = CPL_FALSE;
00224     int                     i;
00225 
00226     /* Initialise */
00227     isaac_img_slitpos_config.angle = -1.0;
00228     isaac_img_slitpos_config.slit_flux = 0.0;
00229     isaac_img_slitpos_config.xpos = -1.0;
00230     isaac_img_slitpos_config.ypos = -1.0;
00231     loc_pi = 3.1415926535897932384626433;
00232     
00233     /* Retrieve input parameters */
00234     par = cpl_parameterlist_find(parlist, "isaac.isaac_img_slitpos.slit_width");
00235     isaac_img_slitpos_config.slit_width = cpl_parameter_get_double(par);
00236     par = cpl_parameterlist_find(parlist, "isaac.isaac_img_slitpos.products"); 
00237     isaac_img_slitpos_config.products_flag = cpl_parameter_get_bool(par);
00238 
00239     /* Identify the RAW and CALIB frames in the input frameset */
00240     if (isaac_dfs_set_groups(framelist)) {
00241         cpl_msg_error(cpl_func, "Cannot identify RAW and CALIB frames");
00242         return -1;
00243     }
00244 
00245     slitframes = isaac_extract_frameset(framelist, ISAAC_IMG_SLITPOS_RAW);
00246     if (slitframes == NULL) 
00247         slitframes = isaac_extract_frameset(framelist,
00248                                             ISAAC_IMG_SLITPOS_CAL_RAW);
00249     cpl_ensure_code(slitframes != NULL, CPL_ERROR_DATA_NOT_FOUND);
00250 
00251     /* Load the data */
00252     imlist = cpl_imagelist_load_frameset(slitframes, CPL_TYPE_FLOAT, 1, 0);
00253     cpl_frameset_delete(slitframes);
00254     
00255     /* Check if the dark frame is there */
00256     /* Get FITS header from reference file */
00257     cur_frame = cpl_frameset_get_frame(framelist, 0);
00258     if ((plist=cpl_propertylist_load(cpl_frame_get_filename(cur_frame),
00259                     0)) == NULL) {
00260         cpl_msg_error(cpl_func, "getting header from first frame");
00261         cpl_imagelist_delete(imlist);
00262         return -1;
00263     }
00264     if ((sval = isaac_pfits_get_mode(plist)) == NULL) {
00265         cpl_msg_error(cpl_func, "getting the MODE");
00266         cpl_propertylist_delete(plist);
00267         cpl_imagelist_delete(imlist);
00268         return -1;
00269     }
00270     with_dark = 0;
00271     if (!strcmp(sval, "SW_DARK") || !strcmp(sval, "LW_DARK")) {
00272         dark = cpl_image_duplicate(cpl_imagelist_get(imlist, 0));
00273         cpl_imagelist_subtract_image(imlist, dark);
00274         cpl_image_delete(dark);
00275         with_dark = 1;
00276     }
00277     cpl_propertylist_delete(plist);
00278     
00279     /* Reduce each frame separately */
00280     for (i=0; i<cpl_imagelist_get_size(imlist); i++) {
00281         cpl_errorstate prestate = cpl_errorstate_get();
00282         if(i==0 && with_dark) continue;
00283         if (with_dark) slit_ind = i-1;
00284         else slit_ind = i;
00285         /* Slit analysis */
00286         cpl_msg_info(cpl_func, "Analyse slit nb %d", slit_ind+1);
00287         cpl_msg_indent_more();
00288         if ((out_table = irplib_slitpos_analysis(
00289                         cpl_imagelist_get(imlist, i),
00290                         isaac_img_slitpos_config.slit_width, 
00291                         &(isaac_img_slitpos_config.slit_flux))) == NULL) {
00292             cpl_msg_error(cpl_func, "Could not analyse the slit in image "
00293                           "%d/%d", i+1, (int)cpl_imagelist_get_size(imlist));
00294             cpl_errorstate_dump(prestate, CPL_FALSE, NULL);
00295             cpl_errorstate_set(prestate);
00296             cpl_msg_indent_less();
00297             continue;
00298         }
00299         
00300         /* Find the slit angle in degrees with the horizontal axis   */
00301         slit_length = cpl_table_get_nrow(out_table);
00302         /* THE ANGLE */
00303         isaac_img_slitpos_config.angle = (double)atan(
00304             (double)(cpl_table_get_int(out_table,"SLIT_Y",slit_length-1, NULL) -
00305                      cpl_table_get_int(out_table, "SLIT_Y", 0, NULL)) /
00306             (cpl_table_get_double(out_table,"SLIT_CENTER",slit_length-1, NULL) -
00307              cpl_table_get_double(out_table, "SLIT_CENTER", 0, NULL)));
00308         isaac_img_slitpos_config.angle *= 180.0/loc_pi;
00309         if (isaac_img_slitpos_config.angle >= 0.00 && 
00310                 isaac_img_slitpos_config.angle < 90.0)
00311             isaac_img_slitpos_config.angle += 180.0;
00312         /* THE SLIT LENGTH */
00313         isaac_img_slitpos_config.slit_length = slit_length;
00314         /* THE SLIT CENTER POSITION */
00315         isaac_img_slitpos_config.xpos = 
00316             (cpl_table_get_double(out_table,"SLIT_CENTER",0,NULL) +
00317              cpl_table_get_double(out_table,"SLIT_CENTER",slit_length-1,NULL))
00318             / 2.0;
00319         isaac_img_slitpos_config.ypos = 
00320             (double)(cpl_table_get_int(out_table,"SLIT_Y",0,NULL) +
00321              cpl_table_get_int(out_table,"SLIT_Y",slit_length-1,NULL))
00322             / 2.0;
00323         
00324         /* Print the results */
00325         cpl_msg_info(cpl_func, "Slit flux   : %g", 
00326                 isaac_img_slitpos_config.slit_flux);
00327         cpl_msg_info(cpl_func, "Slit angle  : %g", 
00328                 isaac_img_slitpos_config.angle);
00329         cpl_msg_info(cpl_func, "Slit length : %d", 
00330                 isaac_img_slitpos_config.slit_length);
00331         cpl_msg_info(cpl_func, "Slit center : %g %g", 
00332                 isaac_img_slitpos_config.xpos,
00333                 isaac_img_slitpos_config.ypos);
00334         cpl_msg_indent_less();
00335 
00336         /* Save the product */
00337         if (isaac_img_slitpos_config.products_flag) {
00338             cpl_msg_info(cpl_func, "Save the product");
00339             cpl_msg_indent_more();
00340             if (isaac_img_slitpos_save(out_table, slit_ind+1, parlist, 
00341                         framelist) == -1) {
00342                 cpl_msg_error(cpl_func, "Cannot save the products");
00343                 cpl_imagelist_delete(imlist);
00344                 cpl_table_delete(out_table);
00345                 cpl_msg_indent_less();
00346                 return -1;
00347             }
00348             cpl_msg_indent_less();
00349         }
00350         cpl_table_delete(out_table);
00351         if (!cpl_error_get_code()) did_reduce = CPL_TRUE;
00352     }
00353             
00354     /* Free and return */
00355     cpl_imagelist_delete(imlist);
00356 
00357     cpl_ensure_code(did_reduce, CPL_ERROR_ILLEGAL_INPUT);
00358 
00359     return cpl_error_set_where(cpl_func); /* Propagate error, if any */
00360 }
00361 
00362 /*----------------------------------------------------------------------------*/
00371 /*----------------------------------------------------------------------------*/
00372 static int isaac_img_slitpos_save(
00373         cpl_table           *   out_table,   
00374         int                     file_nb,
00375         cpl_parameterlist   *   parlist,
00376         cpl_frameset        *   set)
00377 {
00378     cpl_propertylist * plist;
00379     cpl_propertylist * paflist;
00380     cpl_propertylist * qclist;
00381     /* Get the reference frame */
00382     const cpl_frame  * ref_frame = cpl_frameset_get_frame_const(set, file_nb-1);
00383     char             * filename;
00384 
00385     /* Get the QC params in qclist */
00386     qclist = cpl_propertylist_new();
00387     cpl_propertylist_append_double(qclist, "ESO QC SLIT FLUX",
00388             isaac_img_slitpos_config.slit_flux);
00389     cpl_propertylist_append_double(qclist, "ESO QC SLIT POSANG",
00390             isaac_img_slitpos_config.angle);
00391     cpl_propertylist_append_double(qclist, "ESO QC SLIT XPOS",
00392             isaac_img_slitpos_config.xpos);
00393     cpl_propertylist_append_double(qclist, "ESO QC SLIT YPOS",
00394             isaac_img_slitpos_config.ypos);
00395 
00396     cpl_propertylist_append_string(qclist, CPL_DFS_PRO_CATG,
00397                                    ISAAC_IMG_SLITPOS_RES);
00398 
00399     /* Write the table */
00400     filename = cpl_sprintf("isaac_img_slitpos_%02d.fits", file_nb);
00401 
00402     cpl_dfs_save_table(set, NULL, parlist, set, ref_frame, out_table,
00403                        NULL, "isaac_img_slitpos", qclist, NULL,
00404                        PACKAGE "/" PACKAGE_VERSION, filename);
00405     cpl_free(filename);
00406 
00407     /* Get FITS header from reference file */
00408     if ((plist=cpl_propertylist_load(cpl_frame_get_filename(ref_frame),
00409                 0)) == NULL) {
00410         cpl_msg_error(cpl_func, "getting header from reference frame");
00411         cpl_propertylist_delete(qclist);
00412         return -1;
00413     }
00414 
00415     /* Get the keywords for the paf file */
00416     paflist = cpl_propertylist_new();
00417     cpl_propertylist_copy_property_regexp(paflist, plist,
00418         "^(ARCFILE|ORIGFILE|MJD-OBS|INSTRUME|ESO TPL ID|ESO TPL NEXP|"
00419         "ESO TPL EXPNO|ESO DPR CATG|ESO DPR TECH|ESO DPR TYPE|DATE-OBS|"
00420         "ESO INS OPTI1 ID)$",0);
00421     cpl_propertylist_delete(plist);
00422 
00423     /* Copy the QC in paflist */
00424     cpl_propertylist_copy_property_regexp(paflist, qclist, "", 0);
00425     cpl_propertylist_delete(qclist);
00426 
00427     /* PRO.CATG */
00428     cpl_propertylist_update_string(paflist, CPL_DFS_PRO_CATG,
00429                                    ISAAC_IMG_SLITPOS_RES);
00430 
00431     /* Save the PAF file */
00432     filename = cpl_sprintf("isaac_img_slitpos_%02d.paf", file_nb);
00433     cpl_dfs_save_paf("ISAAC",
00434             "isaac_img_slitpos",
00435             paflist,
00436             filename);
00437     cpl_free(filename);
00438     cpl_propertylist_delete(paflist);
00439     return  0;
00440 }
00441 

Generated on Mon Feb 6 14:44:02 2012 for ISAAC Pipeline Reference Manual by  doxygen 1.5.8