naco_img_slitpos.c

00001 /* $Id: naco_img_slitpos.c,v 1.60 2010/03/02 15:29:12 llundin Exp $
00002  *
00003  * This file is part of the NACO 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., 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: llundin $
00023  * $Date: 2010/03/02 15:29:12 $
00024  * $Revision: 1.60 $
00025  * $Name: naco-4_3_3 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include "naco_recipe.h"
00037 #include "irplib_slitpos.h"
00038 
00039 /*-----------------------------------------------------------------------------
00040                             Recipe defines
00041  -----------------------------------------------------------------------------*/
00042 
00043 #define RECIPE_STRING  "naco_img_slitpos"
00044 
00045 /*-----------------------------------------------------------------------------
00046                             Private Functions prototypes
00047  -----------------------------------------------------------------------------*/
00048 
00049 static cpl_error_code naco_img_slitpos_qc(cpl_propertylist *,
00050                                           cpl_propertylist *,
00051                                           const irplib_framelist *);
00052 
00053 static cpl_error_code naco_img_slitpos_save(cpl_frameset *,
00054                                             const cpl_parameterlist *,
00055                                             const cpl_propertylist *,
00056                                             const cpl_propertylist *,
00057                                             const cpl_table *, int,
00058                                             const irplib_framelist *);
00059 
00060 NACO_RECIPE_DEFINE(naco_img_slitpos, NACO_PARAM_SLIT_W,
00061                    "Slit Position recipe",
00062                    RECIPE_STRING " -- NACO imaging slit position recipe.\n"          
00063                    "The Set Of Frames (sof-file) must specify at least one file "
00064                    "which must be tagged\n"                                     
00065                    "raw-file.fits " NACO_IMG_SLITPOS_RAW "\n"
00066                    "\n"
00067                    "Each image is flipped around the x=y axis before the "
00068                    "analysis.");
00069 
00070 /*----------------------------------------------------------------------------*/
00074 /*----------------------------------------------------------------------------*/
00075 
00076 /*-----------------------------------------------------------------------------
00077                                 Functions code
00078  -----------------------------------------------------------------------------*/
00079 
00080 
00081 /*----------------------------------------------------------------------------*/
00088 /*----------------------------------------------------------------------------*/
00089 static int naco_img_slitpos(cpl_frameset            * framelist,
00090                             const cpl_parameterlist * parlist)
00091 {
00092     irplib_framelist* allframes = NULL;
00093     irplib_framelist* rawframes = NULL;
00094     irplib_framelist* oneframe  = NULL;
00095     cpl_frame       * frame     = NULL;
00096     cpl_image       * image     = NULL;
00097     cpl_table       * out_table = NULL;
00098     cpl_propertylist* qclist    = cpl_propertylist_new();
00099     cpl_propertylist* paflist   = cpl_propertylist_new();
00100 
00101     int               slit_width;
00102     int               nframes;
00103     int               i;
00104 
00105 
00106     slit_width = naco_parameterlist_get_int(parlist, RECIPE_STRING,
00107                                             NACO_PARAM_SLIT_W);
00108     bug_if(0);
00109 
00110     /* Identify the RAW and CALIB frames in the input frameset */
00111     skip_if (naco_dfs_set_groups(framelist));
00112 
00113     allframes = irplib_framelist_cast(framelist);
00114     bug_if(allframes == NULL);
00115 
00116     irplib_check(rawframes
00117                  = irplib_framelist_extract(allframes, NACO_IMG_SLITPOS_RAW),
00118                  "Could not load the raw frames");
00119     irplib_framelist_empty(allframes);
00120 
00121     nframes = irplib_framelist_get_size(rawframes);
00122 
00123     oneframe = irplib_framelist_new();
00124 
00125     /* Reduce each frame separately */
00126     for (i=0 ; i < nframes ; i++) {
00127         const char * name;
00128         int          slit_length;
00129         double       xpos, ypos, angle;
00130         double       slit_y0, slit_yn;
00131         double       slit_c0, slit_cn;
00132 
00133         /* Slit analysis */
00134         cpl_msg_info(cpl_func, "Analysing slit of frame %d out of %d", i+1,
00135                      nframes);
00136 
00137         frame = irplib_framelist_unset(rawframes, 0, NULL);
00138         bug_if(irplib_framelist_set(oneframe, frame, 0));
00139 
00140         name = cpl_frame_get_filename(frame);
00141         frame = NULL;
00142         bug_if(name == NULL);
00143 
00144         irplib_check(image = cpl_image_load(name, CPL_TYPE_FLOAT, 0, 0),
00145                      "Could not load FITS-image");
00146 
00147         bug_if(cpl_image_flip(image, 1));
00148 
00149         out_table = irplib_slitpos_analysis(image, slit_width, NULL);
00150         cpl_image_delete(image);
00151         image = NULL;
00152         skip_if (out_table == NULL);
00153 
00154         /* THE SLIT LENGTH */
00155         /* Find the slit angle in degrees with the horizontal axis   */
00156         slit_length = cpl_table_get_nrow(out_table);
00157 
00158         slit_y0 = cpl_table_get(out_table, "SLIT_Y", 0,             NULL);
00159         slit_yn = cpl_table_get(out_table, "SLIT_Y", slit_length-1, NULL);
00160         bug_if(0);
00161 
00162         slit_c0 = cpl_table_get(out_table, "SLIT_CENTER", 0,             NULL);
00163         slit_cn = cpl_table_get(out_table, "SLIT_CENTER", slit_length-1, NULL);
00164         bug_if(0);
00165 
00166         /* THE ANGLE: 180 degrees for a perfectly aligned slit */
00167         angle = 180.0 + CPL_MATH_DEG_RAD * atan2(slit_cn - slit_c0,
00168                                                     slit_yn - slit_y0);
00169 
00170         /* THE SLIT CENTER POSITION */
00171         ypos = (slit_c0 + slit_cn)/2.0;
00172         xpos = (slit_y0 + slit_yn)/2.0;
00173         
00174         /* Print the results */
00175         cpl_msg_info(cpl_func, "Slit angle  : %g", angle);
00176         cpl_msg_info(cpl_func, "Slit length : %d", slit_length);
00177         cpl_msg_info(cpl_func, "Slit center : %g %g", xpos, ypos);
00178 
00179         skip_if(irplib_framelist_load_propertylist_all(oneframe, 0, "^("
00180                                                        NACO_PFITS_REGEXP_SLITPOS "|"
00181                                                        NACO_PFITS_REGEXP_SLITPOS_PAF
00182                                                        ")$", CPL_FALSE));
00183 
00184         /* Add QC parameters */
00185         bug_if(cpl_propertylist_append_double(qclist, "ESO QC SLIT POSANG",
00186                                                angle));
00187         bug_if(cpl_propertylist_append_double(qclist, "ESO QC SLIT XPOS",
00188                                                xpos));
00189         bug_if(cpl_propertylist_append_double(qclist, "ESO QC SLIT YPOS",
00190                                                ypos));
00191 
00192         skip_if (naco_img_slitpos_qc(qclist, paflist, oneframe));
00193 
00194         /* PRO.CATG */
00195         bug_if (cpl_propertylist_append_string(paflist, CPL_DFS_PRO_CATG,
00196                                                NACO_IMG_SLITPOS_RES));
00197 
00198         skip_if (naco_img_slitpos_save(framelist, parlist, qclist, paflist,
00199                                        out_table, i+1, oneframe));
00200 
00201         cpl_table_delete(out_table);
00202         out_table = NULL;
00203 
00204         cpl_propertylist_empty(qclist);
00205         cpl_propertylist_empty(paflist);
00206 
00207         /* Empty the list - and deallocate the propertylists */
00208         irplib_framelist_empty(oneframe);
00209 
00210         bug_if(irplib_framelist_get_size(oneframe) != 0);
00211 
00212     }
00213 
00214     bug_if(irplib_framelist_get_size(rawframes) != 0);
00215 
00216     end_skip;
00217 
00218     cpl_image_delete(image);
00219     cpl_table_delete(out_table);
00220 
00221     irplib_framelist_delete(oneframe);
00222     irplib_framelist_delete(allframes);
00223     irplib_framelist_delete(rawframes);
00224     cpl_propertylist_delete(qclist);
00225     cpl_propertylist_delete(paflist);
00226 
00227     cpl_frame_delete(frame);
00228 
00229     return cpl_error_get_code();
00230 }
00231 
00232 /*----------------------------------------------------------------------------*/
00240 /*----------------------------------------------------------------------------*/
00241 static cpl_error_code naco_img_slitpos_qc(cpl_propertylist       * qclist,
00242                                           cpl_propertylist       * paflist,
00243                                           const irplib_framelist * rawframes)
00244 {
00245 
00246     cpl_errorstate cleanstate = cpl_errorstate_get();
00247     const cpl_propertylist * reflist
00248         = irplib_framelist_get_propertylist_const(rawframes, 0);
00249     const char               pafcopy[] = "^(" NACO_PFITS_REGEXP_SLITPOS_PAF ")$";
00250     const char             * filter;
00251 
00252 
00253     bug_if (0);
00254 
00255     filter = naco_pfits_get_filter(reflist);
00256     if (cpl_error_get_code())
00257         naco_error_reset("Could not get FITS key:");
00258     else
00259         bug_if(cpl_propertylist_append_string(qclist, "ESO QC FILTER OBS",
00260                                               filter));
00261 
00262     /* THE PAF FILE FOR QC PARAMETERS */
00263 
00264     /* Get the QC params in qclist and keys for paf in paflist */
00265     bug_if(cpl_propertylist_copy_property_regexp(paflist, reflist, pafcopy, 0));
00266 
00267     bug_if(cpl_propertylist_append(paflist, qclist));
00268 
00269 
00270     end_skip;
00271 
00272     return cpl_error_get_code();
00273 }
00274 
00275 /*----------------------------------------------------------------------------*/
00287 /*----------------------------------------------------------------------------*/
00288 static cpl_error_code naco_img_slitpos_save(cpl_frameset            * set,
00289                                             const cpl_parameterlist * parlist,
00290                                             const cpl_propertylist  * qclist,
00291                                             const cpl_propertylist  * paflist,
00292                                             const cpl_table         * out_table,
00293                                             int                       file_nb,
00294                                             const irplib_framelist  * oneframe)
00295 {
00296 
00297     cpl_frameset * rawframes = irplib_frameset_cast(oneframe);
00298     char         * filename  = NULL;
00299 
00300 
00301     bug_if (0);
00302 
00303     /* Write the FITS file */
00304     filename = cpl_sprintf(RECIPE_STRING "_%02d" CPL_DFS_FITS, file_nb);
00305     skip_if (irplib_dfs_save_table(set, parlist, rawframes, out_table, NULL,
00306                                RECIPE_STRING, NACO_IMG_SLITPOS_RES, qclist, NULL,
00307                                naco_pipe_id, filename));
00308 
00309     cpl_free(filename);
00310     filename = cpl_sprintf(RECIPE_STRING "_%02d" CPL_DFS_PAF, file_nb);
00311     skip_if (cpl_dfs_save_paf("NACO", RECIPE_STRING, paflist, filename));
00312 
00313     end_skip;
00314 
00315     cpl_free(filename);
00316     cpl_frameset_delete(rawframes);
00317 
00318     return cpl_error_get_code();
00319 }

Generated on Tue Mar 12 15:19:53 2013 for NACO Pipeline Reference Manual by  doxygen 1.4.7