irplib_utils.c

00001 /* $Id: irplib_utils.c,v 1.40 2006/12/07 08:42:09 llundin 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: llundin $
00023  * $Date: 2006/12/07 08:42:09 $
00024  * $Revision: 1.40 $
00025  * $Name:  $
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 <assert.h>
00038 
00039 #include <cxutils.h>
00040 
00041 #include <irplib_error.h>
00042 
00043 #include "irplib_utils.h"
00044 
00045 /*----------------------------------------------------------------------------*/
00049 /*----------------------------------------------------------------------------*/
00052 /*----------------------------------------------------------------------------*/
00065 /*----------------------------------------------------------------------------*/
00066 void irplib_reset(void)
00067 {
00068     irplib_error_reset();
00069     return;
00070 }
00071 
00072 /*----------------------------------------------------------------------------*/
00079 /*----------------------------------------------------------------------------*/
00080 int irplib_compare_tags(
00081         cpl_frame   *   frame1,
00082         cpl_frame   *   frame2)
00083 {
00084     char            *   v1 ;
00085     char            *   v2 ;
00086 
00087     /* Test entries */
00088     if (frame1==NULL || frame2==NULL) return -1 ;
00089 
00090     /* Get the tags */
00091     if ((v1 = (char*)cpl_frame_get_tag(frame1)) == NULL) return -1 ;
00092     if ((v2 = (char*)cpl_frame_get_tag(frame2)) == NULL) return -1 ;
00093 
00094     /* Compare the tags */
00095     if (strcmp(v1, v2)) return 0 ;
00096     else return 1 ;
00097 }
00098 
00099 /*----------------------------------------------------------------------------*/
00115 /*----------------------------------------------------------------------------*/
00116 const char * irplib_frameset_find_file(const cpl_frameset * self,
00117                                       const char * tag)
00118 {
00119     const cpl_frame * frame = cpl_frameset_find(self, tag);
00120 
00121 
00122     cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
00123 
00124     if (frame == NULL) return NULL;
00125 
00126     if (cpl_frameset_find(self, NULL))
00127         cpl_msg_warning(cpl_func,
00128             "Frameset has more than one file with tag: %s",
00129                         tag);
00130 
00131     return cpl_frame_get_filename(frame);
00132 
00133 }
00134 
00135 /*----------------------------------------------------------------------------*/
00145 /*----------------------------------------------------------------------------*/
00146 cpl_frame * irplib_frameset_get_first_from_group(const cpl_frameset * self,
00147                                                  cpl_frame_group group)
00148 {
00149 
00150     cpl_frame * frame;
00151 
00152 
00153     cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
00154 
00155     for (frame = cpl_frameset_get_first(self); frame != NULL ;
00156          frame = cpl_frameset_get_next(self)) {
00157 
00158 
00159         if (cpl_frame_get_group(frame) == group) break;
00160 
00161 
00162     }
00163 
00164     return frame;
00165 
00166 }
00167 
00168 
00169 /*----------------------------------------------------------------------------*/
00188 /*----------------------------------------------------------------------------*/
00189 cpl_error_code irplib_apertures_find_max_flux(const cpl_apertures * self,
00190                                               int * ind, int nfind)
00191 {
00192     const int    nsize = cpl_apertures_get_size(self);
00193     int          ifind;
00194 
00195 
00196     cpl_ensure_code(nsize > 0,      cpl_error_get_code());
00197     cpl_ensure_code(ind,          CPL_ERROR_NULL_INPUT);
00198     cpl_ensure_code(nfind > 0,      CPL_ERROR_ILLEGAL_INPUT);
00199     cpl_ensure_code(nfind <= nsize, CPL_ERROR_ILLEGAL_INPUT);
00200 
00201     for (ifind=0; ifind < nfind; ifind++) {
00202         double maxflux = -1;
00203         int maxind = -1;
00204         int i;
00205         for (i=1; i <= nsize; i++) {
00206             int k;
00207 
00208             /* The flux has to be the highest among those not already found */
00209             for (k=0; k < ifind; k++) if (ind[k] == i) break;
00210 
00211             if (k == ifind) {
00212                 /* i has not been inserted into ind */
00213                 const double flux = cpl_apertures_get_flux(self, i);
00214 
00215                 if (maxind < 0 || flux > maxflux) {
00216                     maxind = i;
00217                     maxflux = flux;
00218                 }
00219             }
00220         }
00221         ind[ifind] = maxind;
00222     }
00223 
00224     return CPL_ERROR_NONE;
00225 
00226 }
00227 
00228 /*----------------------------------------------------------------------------*/
00244 /*----------------------------------------------------------------------------*/
00245 char * irplib_vsprintf_macro(const char * format, va_list arglist,
00246                              va_list argcopy)
00247 {
00248 
00249     char   * self;
00250     size_t   selfsize;
00251     int      nlen;
00252 
00253 
00254     cpl_ensure(format != NULL, CPL_ERROR_NULL_INPUT, NULL);
00255 
00256     /* A guess at the necessary length of the string */
00257     selfsize = 1 + strlen(format);
00258     self = (char*)cpl_malloc(selfsize);
00259 
00260     nlen = cx_vsnprintf(self, (cxsize)selfsize, format, arglist);
00261 
00262     /* The format string may be invalid */
00263     if (nlen < 0) {
00264         cpl_free(self);
00265         cpl_ensure(0, CPL_ERROR_ILLEGAL_INPUT, NULL);
00266     }
00267 
00268     if (selfsize < 1 + (size_t)nlen) {
00269         /* self was too short, extend it */
00270 
00271         cpl_free(self);
00272 
00273          /* The exact size of self */
00274         selfsize = 1 + (size_t)nlen;
00275 
00276         self = (char*)cpl_malloc(selfsize);
00277 
00278         nlen = cx_vsnprintf(self, (cxsize)selfsize, format, argcopy);
00279 
00280         assert( nlen > 0);
00281         assert( selfsize == 1 + (size_t)nlen);
00282 
00283     } else if (selfsize > 1 + (size_t)nlen) {
00284         /* self was too long, shorten it */
00285 
00286         /* The exact size of self */
00287         selfsize = 1 + (size_t)nlen;
00288 
00289         self = (char*)cpl_realloc((void*)self, selfsize);
00290 
00291     }
00292 
00293     assert(selfsize == 1 + strlen(self));
00294 
00295     return self;
00296 
00297 }
00298 
00299 /*----------------------------------------------------------------------------*/
00331 /*----------------------------------------------------------------------------*/
00332 char * irplib_sprintf(const char * format, ...)
00333 {
00334 
00335     char    * self;
00336     va_list   arglist;
00337     va_list   argcopy;
00338 
00339 
00340     cpl_ensure(format != NULL, CPL_ERROR_NULL_INPUT, NULL);
00341 
00342     va_start(arglist, format);
00343     va_start(argcopy, format);
00344     self = irplib_vsprintf_macro(format, arglist, argcopy);
00345     va_end(arglist);
00346     va_end(argcopy);
00347 
00348     cpl_ensure(self != NULL, cpl_error_get_code(), NULL);
00349 
00350     return self;
00351 
00352 }
00353 
00354 
00355 /*----------------------------------------------------------------------------*/
00366 /*----------------------------------------------------------------------------*/
00367 cpl_type_bpp irplib_bpp_find(int minval, int maxval)
00368 {
00369     cpl_type_bpp self = CPL_BPP_32_SIGNED; /* Default value */
00370 
00371 
00372     cpl_ensure(minval <= maxval, CPL_ERROR_ILLEGAL_INPUT, self);
00373 
00374     if (0 <= minval) {
00375         if (maxval <= 255) {
00376             self = CPL_BPP_8_UNSIGNED;
00377         } else if (maxval <= 32767) {
00378             self = CPL_BPP_16_SIGNED;
00379 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE > CPL_VERSION(3, 0, 90)
00380         } else if (maxval <= 65535) {
00381             self = CPL_BPP_16_UNSIGNED;
00382 #endif
00383         }
00384     } else if (-32768 <= minval && maxval <= 32767) {
00385         self = CPL_BPP_16_SIGNED;
00386     }
00387 
00388     return self;
00389 
00390 }
00391 
00392 

Generated on Wed Jan 17 08:33:41 2007 for SINFONI Pipeline Reference Manual by  doxygen 1.4.4