irplib_utils.c

00001 /* $Id: irplib_utils.c,v 1.42 2007/05/23 12:48:38 jmlarsen Exp $
00002  *
00003  * This file is part of the irplib package 
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: jmlarsen $
00023  * $Date: 2007/05/23 12:48:38 $
00024  * $Revision: 1.42 $
00025  * $Name: uves-3_3_1 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                    Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <math.h>
00037 
00038 #include <string.h>
00039 
00040 #include <cpl.h>
00041 
00042 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(3, 1, 90)
00043 #else
00044   /* FIXME: Remove once support for CPL 3.1 is dropped */
00045 
00046 #include <assert.h>
00047 
00048 #include <cxutils.h>
00049 #endif
00050 
00051 #include <irplib_error.h>
00052 
00053 #include "irplib_utils.h"
00054 
00055 /*----------------------------------------------------------------------------*/
00059 /*----------------------------------------------------------------------------*/
00062 /*----------------------------------------------------------------------------*/
00075 /*----------------------------------------------------------------------------*/
00076 void irplib_reset(void)
00077 {
00078     irplib_error_reset();
00079     return;
00080 }
00081 
00082 /*----------------------------------------------------------------------------*/
00089 /*----------------------------------------------------------------------------*/
00090 int irplib_compare_tags(
00091         cpl_frame   *   frame1,
00092         cpl_frame   *   frame2)
00093 {
00094     char            *   v1 ;
00095     char            *   v2 ;
00096 
00097     /* Test entries */
00098     if (frame1==NULL || frame2==NULL) return -1 ;
00099 
00100     /* Get the tags */
00101     if ((v1 = (char*)cpl_frame_get_tag(frame1)) == NULL) return -1 ;
00102     if ((v2 = (char*)cpl_frame_get_tag(frame2)) == NULL) return -1 ;
00103 
00104     /* Compare the tags */
00105     if (strcmp(v1, v2)) return 0 ;
00106     else return 1 ;
00107 }
00108 
00109 /*----------------------------------------------------------------------------*/
00125 /*----------------------------------------------------------------------------*/
00126 const char * irplib_frameset_find_file(const cpl_frameset * self,
00127                                       const char * tag)
00128 {
00129     const cpl_frame * frame = cpl_frameset_find(self, tag);
00130 
00131 
00132     cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
00133 
00134     if (frame == NULL) return NULL;
00135 
00136     if (cpl_frameset_find(self, NULL))
00137         cpl_msg_warning(cpl_func,
00138             "Frameset has more than one file with tag: %s",
00139                         tag);
00140 
00141     return cpl_frame_get_filename(frame);
00142 
00143 }
00144 
00145 /*----------------------------------------------------------------------------*/
00155 /*----------------------------------------------------------------------------*/
00156 cpl_frame * irplib_frameset_get_first_from_group(const cpl_frameset * self,
00157                                                  cpl_frame_group group)
00158 {
00159 
00160     cpl_frame * frame;
00161 
00162 
00163     cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
00164 
00165     for (frame = cpl_frameset_get_first(self); frame != NULL ;
00166          frame = cpl_frameset_get_next(self)) {
00167 
00168 
00169         if (cpl_frame_get_group(frame) == group) break;
00170 
00171 
00172     }
00173 
00174     return frame;
00175 
00176 }
00177 
00178 
00179 /*----------------------------------------------------------------------------*/
00198 /*----------------------------------------------------------------------------*/
00199 cpl_error_code irplib_apertures_find_max_flux(const cpl_apertures * self,
00200                                               int * ind, int nfind)
00201 {
00202     const int    nsize = cpl_apertures_get_size(self);
00203     int          ifind;
00204 
00205 
00206     cpl_ensure_code(nsize > 0,      cpl_error_get_code());
00207     cpl_ensure_code(ind,          CPL_ERROR_NULL_INPUT);
00208     cpl_ensure_code(nfind > 0,      CPL_ERROR_ILLEGAL_INPUT);
00209     cpl_ensure_code(nfind <= nsize, CPL_ERROR_ILLEGAL_INPUT);
00210 
00211     for (ifind=0; ifind < nfind; ifind++) {
00212         double maxflux = -1;
00213         int maxind = -1;
00214         int i;
00215         for (i=1; i <= nsize; i++) {
00216             int k;
00217 
00218             /* The flux has to be the highest among those not already found */
00219             for (k=0; k < ifind; k++) if (ind[k] == i) break;
00220 
00221             if (k == ifind) {
00222                 /* i has not been inserted into ind */
00223                 const double flux = cpl_apertures_get_flux(self, i);
00224 
00225                 if (maxind < 0 || flux > maxflux) {
00226                     maxind = i;
00227                     maxflux = flux;
00228                 }
00229             }
00230         }
00231         ind[ifind] = maxind;
00232     }
00233 
00234     return CPL_ERROR_NONE;
00235 
00236 }
00237 
00238 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(3, 1, 90)
00239 #else
00240   /* FIXME: Remove once support for CPL 3.1 is dropped */
00241 /*----------------------------------------------------------------------------*/
00257 /*----------------------------------------------------------------------------*/
00258 char * irplib_vsprintf_macro(const char * format, va_list arglist,
00259                              va_list argcopy)
00260 {
00261 
00262     char   * self;
00263     size_t   selfsize;
00264     int      nlen;
00265 
00266 
00267     cpl_ensure(format != NULL, CPL_ERROR_NULL_INPUT, NULL);
00268 
00269     /* A guess at the necessary length of the string */
00270     selfsize = 1 + strlen(format);
00271     self = (char*)cpl_malloc(selfsize);
00272 
00273     nlen = cx_vsnprintf(self, (cxsize)selfsize, format, arglist);
00274 
00275     /* The format string may be invalid */
00276     if (nlen < 0) {
00277         cpl_free(self);
00278         cpl_ensure(0, CPL_ERROR_ILLEGAL_INPUT, NULL);
00279     }
00280 
00281     if (selfsize < 1 + (size_t)nlen) {
00282         /* self was too short, extend it */
00283 
00284         cpl_free(self);
00285 
00286          /* The exact size of self */
00287         selfsize = 1 + (size_t)nlen;
00288 
00289         self = (char*)cpl_malloc(selfsize);
00290 
00291         nlen = cx_vsnprintf(self, (cxsize)selfsize, format, argcopy);
00292 
00293         assert( nlen > 0);
00294         assert( selfsize == 1 + (size_t)nlen);
00295 
00296     } else if (selfsize > 1 + (size_t)nlen) {
00297         /* self was too long, shorten it */
00298 
00299         /* The exact size of self */
00300         selfsize = 1 + (size_t)nlen;
00301 
00302         self = (char*)cpl_realloc((void*)self, selfsize);
00303 
00304     }
00305 
00306     assert(selfsize == 1 + strlen(self));
00307 
00308     return self;
00309 
00310 }
00311 
00312 /*----------------------------------------------------------------------------*/
00344 /*----------------------------------------------------------------------------*/
00345 char * irplib_sprintf(const char * format, ...)
00346 {
00347 
00348     char    * self;
00349     va_list   arglist;
00350     va_list   argcopy;
00351 
00352 
00353     cpl_ensure(format != NULL, CPL_ERROR_NULL_INPUT, NULL);
00354 
00355     va_start(arglist, format);
00356     va_start(argcopy, format);
00357     self = irplib_vsprintf_macro(format, arglist, argcopy);
00358     va_end(arglist);
00359     va_end(argcopy);
00360 
00361     cpl_ensure(self != NULL, cpl_error_get_code(), NULL);
00362 
00363     return self;
00364 
00365 }
00366 #endif
00367 
00368 /*----------------------------------------------------------------------------*/
00379 /*----------------------------------------------------------------------------*/
00380 cpl_type_bpp irplib_bpp_find(int minval, int maxval)
00381 {
00382     cpl_type_bpp self = CPL_BPP_32_SIGNED; /* Default value */
00383 
00384 
00385     cpl_ensure(minval <= maxval, CPL_ERROR_ILLEGAL_INPUT, self);
00386 
00387     if (0 <= minval) {
00388         if (maxval <= 255) {
00389             self = CPL_BPP_8_UNSIGNED;
00390         } else if (maxval <= 32767) {
00391             self = CPL_BPP_16_SIGNED;
00392         } else if (maxval <= 65535) {
00393             self = CPL_BPP_16_UNSIGNED;
00394         }
00395     } else if (-32768 <= minval && maxval <= 32767) {
00396         self = CPL_BPP_16_SIGNED;
00397     }
00398 
00399     return self;
00400 
00401 }
00402 
00403 /*----------------------------------------------------------------------------*/
00407 /*----------------------------------------------------------------------------*/
00408 int irplib_isinf(double value)
00409 {
00410 #if defined HAVE_ISINF && HAVE_ISINF
00411     return isinf(value);
00412 #else
00413     return value != 0 && value == 2 * value;
00414 #endif
00415 }
00416 
00417 /*----------------------------------------------------------------------------*/
00421 /*----------------------------------------------------------------------------*/
00422 int irplib_isnan(double value)
00423 {
00424 #if defined HAVE_ISNAN && HAVE_ISNAN
00425     return isnan(value);
00426 #else
00427     return value != value;
00428 #endif
00429 }
00430 

Generated on Tue Jun 19 14:39:14 2007 for UVES Pipeline Reference Manual by  doxygen 1.4.6