crires_util_convert.c

00001 /* $Id: crires_util_convert.c,v 1.15 2011/02/09 10:16:18 yjung Exp $
00002  *
00003  * This file is part of the crires 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: yjung $
00023  * $Date: 2011/02/09 10:16:18 $
00024  * $Revision: 1.15 $
00025  * $Name: crire-2_1_1 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include "crires_recipe.h"
00037 
00038 /*-----------------------------------------------------------------------------
00039                                 Define
00040  -----------------------------------------------------------------------------*/
00041 
00042 #define RECIPE_STRING "crires_util_convert"
00043 
00044 /*-----------------------------------------------------------------------------
00045                             Functions prototypes
00046  -----------------------------------------------------------------------------*/
00047 
00048 static int crires_util_convert_save(cpl_imagelist **, 
00049         const cpl_parameterlist *, cpl_frameset *) ;
00050 
00051 static char crires_util_convert_description[] = 
00052 "This recipe accepts 3 files (produced by crires_spec_detlin):\n"
00053 "First parameter:   the image with the A coefficients.\n"
00054 "Second parameter:  the image with the B coefficients.\n"
00055 "Third parameter:   the image with the C coefficients.\n" 
00056 "\n"
00057 "This recipe produces 1 file:\n"
00058 "First parameter:   the file with the detlin coefficients.\n"
00059 "                   (PRO TYPE = "CRIRES_PROTYPE_COEFFS_CUBE")\n" ;
00060 
00061 CRIRES_RECIPE_DEFINE(crires_util_convert,
00062         0,
00063         "Convert the detlin coefficients",
00064         crires_util_convert_description) ;
00065 
00066 /*-----------------------------------------------------------------------------
00067                                 Functions code
00068  -----------------------------------------------------------------------------*/
00069 
00070 /*----------------------------------------------------------------------------*/
00077 /*----------------------------------------------------------------------------*/
00078 static int crires_util_convert(
00079         cpl_frameset            *   frameset,
00080         const cpl_parameterlist *   parlist)
00081 {
00082     cpl_imagelist       **  coeffs ;
00083     cpl_frame           *   fr[3] ;
00084     cpl_image           *   cur_ima ;
00085     int                     i, j ;
00086     
00087     /* Retrieve input parameters */
00088 
00089     /* Identify the RAW and CALIB frames in the input frameset */
00090     if (crires_dfs_set_groups(frameset, NULL)) {
00091         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00092         return -1 ;
00093     }
00094     
00095     /* Check that we have 3 files in input */
00096     if (cpl_frameset_get_size(frameset) != 3) {
00097         cpl_msg_error(__func__, "Expects 3 file in input") ;
00098         return -1 ;
00099     }
00100     fr[0] = cpl_frameset_get_frame(frameset, 0);
00101     fr[1] = cpl_frameset_get_frame(frameset, 1);
00102     fr[2] = cpl_frameset_get_frame(frameset, 2);
00103 
00104     /* Create the image lists */
00105     coeffs = cpl_malloc(CRIRES_NB_DETECTORS * sizeof(cpl_imagelist*)) ;
00106     for (i=0 ; i<CRIRES_NB_DETECTORS; i++) 
00107         coeffs[i] = cpl_imagelist_new() ;
00108 
00109     /* Loop on the detectors */
00110     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00111         cpl_msg_info(__func__, "Get the coefficients for chip number %d", i+1) ;
00112         /* Coefficient A <- fr[0] */
00113         if ((cur_ima = cpl_image_load(cpl_frame_get_filename(fr[0]), 
00114                         CPL_TYPE_FLOAT, 0, i+1)) == NULL) {
00115             cpl_msg_error(__func__, "Cannot load ext %d", i+1) ;
00116             for (j=0 ; j<CRIRES_NB_DETECTORS; j++) 
00117                 cpl_imagelist_delete(coeffs[j]) ;
00118             cpl_free(coeffs) ;
00119             return -1 ;
00120         }
00121         cpl_imagelist_set(coeffs[i], cur_ima, 0) ;
00122 
00123         /* Coefficient B  <- fr[1] */
00124         if ((cur_ima = cpl_image_load(cpl_frame_get_filename(fr[1]), 
00125                         CPL_TYPE_FLOAT, 0, i+1)) == NULL) {
00126             cpl_msg_error(__func__, "Cannot load ext %d", i+1) ;
00127             for (j=0 ; j<CRIRES_NB_DETECTORS; j++) 
00128                 cpl_imagelist_delete(coeffs[j]) ;
00129             cpl_free(coeffs) ;
00130             return -1 ;
00131         }
00132         cpl_imagelist_set(coeffs[i], cur_ima, 1) ;
00133 
00134         /* Coefficient C <- fr[2] */
00135         if ((cur_ima = cpl_image_load(cpl_frame_get_filename(fr[2]), 
00136                         CPL_TYPE_FLOAT, 0, i+1)) == NULL) {
00137             cpl_msg_error(__func__, "Cannot load ext %d", i+1) ;
00138             for (j=0 ; j<CRIRES_NB_DETECTORS ; j++) 
00139                 cpl_imagelist_delete(coeffs[j]) ;
00140             cpl_free(coeffs) ;
00141             return -1 ;
00142         }
00143         cpl_imagelist_set(coeffs[i], cur_ima, 2) ;
00144     }
00145     
00146     /* Save the products */
00147     cpl_msg_info(__func__, "Save the products") ;
00148     cpl_msg_indent_more() ;
00149     if (crires_util_convert_save(coeffs, parlist, frameset)==-1) {
00150         cpl_msg_error(__func__, "Cannot save the products") ;
00151         for (j=0 ; j<CRIRES_NB_DETECTORS; j++) cpl_imagelist_delete(coeffs[j]) ;
00152         cpl_free(coeffs) ;
00153         cpl_msg_indent_less() ;
00154         return -1 ;
00155     }
00156     cpl_msg_indent_less() ;
00157 
00158     /* Return */
00159     for (i=0 ; i<CRIRES_NB_DETECTORS; i++) cpl_imagelist_delete(coeffs[i]) ;
00160     cpl_free(coeffs) ;
00161     if (cpl_error_get_code()) return -1 ;
00162     else return 0 ;
00163 }
00164 
00165 /*----------------------------------------------------------------------------*/
00173 /*----------------------------------------------------------------------------*/
00174 static int crires_util_convert_save(
00175         cpl_imagelist           **  coeffs,
00176         const cpl_parameterlist *   parlist,
00177         cpl_frameset            *   set)
00178 {
00179     const char          *   recipe_name = "crires_util_convert" ;
00180     cpl_propertylist    *   pro_list ;
00181     cpl_propertylist    *   qc_ext_list ;
00182     char                    sval[16] ;
00183     char                *   filename ;
00184     int                     i ;
00185 
00186     /* Output name */
00187     filename = cpl_sprintf("crires_util_convert.fits") ;
00188 
00189     /* PRO keys */
00190     pro_list = cpl_propertylist_new() ;
00191     cpl_propertylist_append_string(pro_list, CPL_DFS_PRO_CATG, 
00192             CRIRES_CALPRO_COEFFS_CUBE) ;
00193     cpl_propertylist_append_string(pro_list, CPL_DFS_PRO_TYPE,
00194              CRIRES_PROTYPE_COEFFS_CUBE) ;
00195 
00196     /* File with extensions */
00197     if (cpl_dfs_save_image(set, NULL, parlist, set, NULL, NULL,
00198                 CPL_BPP_IEEE_FLOAT, recipe_name, pro_list, NULL, 
00199                 PACKAGE "/" PACKAGE_VERSION, filename) != CPL_ERROR_NONE) {
00200         cpl_msg_error(__func__, "Cannot save the empty primary HDU") ;
00201         cpl_propertylist_delete(pro_list) ;
00202         cpl_free(filename) ;
00203         return -1 ;
00204     }
00205     /* Delete PRO LIST */
00206     cpl_propertylist_delete(pro_list) ;
00207 
00208     /* Save the extensions */
00209     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00210         /* Extension header */
00211         qc_ext_list = cpl_propertylist_new() ;
00212         sprintf(sval, "CHIP%d.INT1", i+1) ;
00213         cpl_propertylist_prepend_string(qc_ext_list, "EXTNAME", sval) ;
00214         cpl_imagelist_save(coeffs[i],
00215                 filename, CPL_BPP_IEEE_FLOAT, qc_ext_list, CPL_IO_EXTEND) ;
00216         cpl_propertylist_delete(qc_ext_list) ;
00217     }
00218     cpl_free(filename) ;
00219     return  0;
00220 }
00221 

Generated on 22 Mar 2011 for CRIRES Pipeline Reference Manual by  doxygen 1.6.1