naco_util_spc_argon.c

00001 /* $Id: naco_util_spc_argon.c,v 1.6 2011/12/22 11:21:03 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: 2011/12/22 11:21:03 $
00024  * $Revision: 1.6 $
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 <string.h>
00037 
00038 #include "naco_recipe.h"
00039 
00040 #include "naco_spc.h"
00041 
00042 /*-----------------------------------------------------------------------------
00043                             Recipe defines
00044  -----------------------------------------------------------------------------*/
00045 
00046 #define RECIPE_STRING "naco_util_spc_argon"
00047 
00048 #ifndef NACO_SPC_ARGON_SIZE
00049 #define NACO_SPC_ARGON_SIZE 80
00050 #endif
00051 
00052 /*-----------------------------------------------------------------------------
00053                             Private Functions prototypes
00054  -----------------------------------------------------------------------------*/
00055 
00056 NACO_RECIPE_DEFINE(naco_util_spc_argon, 0,
00057                    "Generate FITS table with argon lines",
00058                    "The input frame(s) must be tagged "
00059                    NACO_SPC_ARGON_ASCII ".\n"
00060                    "The file must comprise two columns:"
00061                    "the 1st with the line wavelength [Angstrom],\n"
00062                    "the 2nd with the line intensity.\n"
00063                    "\n"
00064                    "The default input ASCII-file is in the catalogs/ "
00065                    "directory of the NACO source-code distribution.");
00066 
00067 static IRPLIB_UTIL_SET_ROW(naco_util_spc_argon_set_row);
00068 static IRPLIB_UTIL_CHECK(naco_util_spc_argon_check);
00069 
00070 static unsigned nzero = 0; /* Quick hack to count zero-intensity lines */
00071 
00072 /*----------------------------------------------------------------------------*/
00076 /*----------------------------------------------------------------------------*/
00077 
00078 /*-----------------------------------------------------------------------------
00079 
00080                                 Functions code
00081  -----------------------------------------------------------------------------*/
00082 
00083 /*----------------------------------------------------------------------------*/
00090 /*----------------------------------------------------------------------------*/
00091 static int naco_util_spc_argon(cpl_frameset            * framelist,
00092                                const cpl_parameterlist * parlist)
00093 {
00094     irplib_framelist * allframes = NULL;
00095     irplib_framelist * rawframes = NULL;
00096     cpl_frameset     * useframes = NULL;
00097     cpl_table        * self      = NULL;
00098     
00099 
00100     if (cpl_error_get_code()) return cpl_error_get_code();
00101 
00102     /* Identify the RAW frames in the input frameset */
00103     skip_if (naco_dfs_set_groups(framelist));
00104 
00105     /* FIXME: Using framelists is the simplest way to extract the relevant
00106        frames :-( */
00107 
00108     allframes = irplib_framelist_cast(framelist);
00109     bug_if(allframes == NULL);
00110 
00111     rawframes = irplib_framelist_extract(allframes, NACO_SPC_ARGON_ASCII);
00112     skip_if (rawframes == NULL);
00113 
00114     irplib_framelist_empty(allframes);
00115 
00116     useframes = irplib_frameset_cast(rawframes);
00117     bug_if(allframes == NULL);
00118 
00119     /* At least one row per file */
00120     self = cpl_table_new(irplib_framelist_get_size(rawframes));
00121 
00122     irplib_framelist_empty(rawframes);
00123 
00124     /* Create the table columns - with units */
00125     bug_if (cpl_table_new_column(self, NACO_SPC_LAB_WAVE,   CPL_TYPE_DOUBLE));
00126     bug_if (cpl_table_new_column(self, NACO_SPC_LAB_INTENS, CPL_TYPE_DOUBLE));
00127 
00128     bug_if(cpl_table_set_column_unit(self, NACO_SPC_LAB_WAVE, "micron"));
00129 
00130     skip_if(irplib_dfs_table_convert(self, framelist, useframes, 
00131                                      NACO_SPC_ARGON_SIZE, '#', NULL,
00132                                      NACO_SPC_ARGON, parlist, RECIPE_STRING,
00133                                      NULL, NULL, NULL, "NACO", naco_pipe_id,
00134                                      naco_util_spc_argon_set_row,
00135                                      naco_util_spc_argon_check));
00136     end_skip;
00137 
00138     cpl_table_delete(self);
00139     cpl_frameset_delete(useframes);
00140     irplib_framelist_delete(allframes);
00141     irplib_framelist_delete(rawframes);
00142 
00143     return cpl_error_get_code();
00144 }
00145 
00146 /*----------------------------------------------------------------------------*/
00158 /*----------------------------------------------------------------------------*/
00159 static
00160 cpl_boolean naco_util_spc_argon_set_row(cpl_table * self,
00161                                         const char * line,
00162                                         int irow,
00163                                         const cpl_frame * rawframe,
00164                                         const cpl_parameterlist * parlist)
00165 {
00166 
00167 
00168     /* gcc can only check sscanf()s format when it is a string literal */
00169 #define FORMAT "%lg %lg"
00170 
00171     int nvals;
00172     double wlen, intens;
00173 
00174 
00175     bug_if(0);
00176     bug_if(self     == NULL);
00177     bug_if(line     == NULL);
00178     bug_if(irow   <  0);
00179     bug_if(rawframe == NULL);
00180     bug_if(parlist  == NULL);
00181 
00182     nvals = sscanf(line, FORMAT, &wlen, &intens);
00183 
00184     error_if (nvals != 2, CPL_ERROR_BAD_FILE_FORMAT,
00185               "Line with length=%u has %d not 2 items formatted: %s",
00186               (unsigned)strlen(line), nvals, FORMAT);
00187 
00188     error_if (wlen <= 0.0, CPL_ERROR_BAD_FILE_FORMAT,
00189               "Non-positive wavelength %g in line %s", wlen, line);
00190 
00191     error_if (intens < 0.0, CPL_ERROR_BAD_FILE_FORMAT,
00192               "Negative intensity %g in line %s", intens, line);
00193 
00194     wlen *= 1e-4; /* Convert from Angstrom to Micron */
00195 
00196     bug_if(cpl_table_set_double(self, NACO_SPC_LAB_WAVE,   irow, wlen));
00197     bug_if(cpl_table_set_double(self, NACO_SPC_LAB_INTENS, irow, intens));
00198 
00199     if (intens <= 0.0) nzero++; /* Don't use == to avoid warning ... */
00200 
00201     end_skip;
00202 
00203     return CPL_TRUE;
00204 
00205 }
00206 
00207 
00208 /*----------------------------------------------------------------------------*/
00217 /*----------------------------------------------------------------------------*/
00218 static
00219 cpl_error_code naco_util_spc_argon_check(cpl_table * self,
00220                                          const cpl_frameset * useframes,
00221                                          const cpl_parameterlist * parlist)
00222 {
00223 
00224     cpl_propertylist * reflist = cpl_propertylist_new();
00225 
00226 
00227     bug_if(0);
00228     bug_if(self     == NULL);
00229     bug_if(parlist  == NULL);
00230 
00231     /* Sort after wavelength with smallest first */
00232     bug_if(cpl_propertylist_append_bool(reflist, NACO_SPC_LAB_WAVE, 0));
00233 
00234     bug_if(cpl_table_sort(self, reflist));
00235 
00236     cpl_msg_info(cpl_func, "Created table of %d argon lines (%u with zero "
00237                  "intensity) from %d file(s)", (int)cpl_table_get_nrow(self),
00238                  nzero, (int)cpl_frameset_get_size(useframes));
00239 
00240     end_skip;
00241 
00242     cpl_propertylist_delete(reflist);
00243 
00244     return cpl_error_get_code();
00245 }
00246 

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