utilities.h

00001 /* $Id: utilities.h,v 1.14 2005/08/19 08:03:57 amodigli Exp $
00002  *
00003  * This file is part of the SINFONI 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: amodigli $
00023  * $Date: 2005/08/19 08:03:57 $
00024  * $Revision: 1.14 $
00025  * $Name:  $
00026  */
00027 #ifndef UTILITIES_H
00028 #define UTILITIES_H
00029 
00030 
00031 
00032 /*
00033 
00034   This recipe implements error handling cleanly using a pair of macros called
00035   skip_if() and end_skip.
00036 
00037   skip_if() takes one argument, which is a logical expression. 
00038   If the logical expression is false skip_if() takes no action and
00039   program execution continues.
00040   If the logical expression is true this indicates an error. In this case
00041   skip_if() will set the location of the error to the point where it
00042   was invoked in the recipe code (unless the error location is already in the
00043   recipe code). If no error code had been set, then skip_if() will set one.
00044   Finally, skip_if() causes program execution to skip to the macro 'end_skip'.
00045   The macro end_skip is located towards the end of the function, after
00046   which all resource deallocation and the function return is located. 
00047 
00048   The use of skip_if() assumes the following coding practice:
00049   1) Pointers used for dynamically allocated memory that they "own" shall always
00050      point to either NULL or to allocated memory (including CPL-objects).
00051   2) Such pointers may not be reused to point to memory whose deallocation
00052      requires calls to different functions.
00053   3) Pointers of type FILE should be set NULL when not pointing to an open
00054      stream and their closing calls (fclose(), freopen(), etc.) following the
00055      'end_skip' should be guarded against such NULL pointers.
00056 
00057   Error checking with skip_if() is encouraged due to the following advantages:
00058   1) It ensures that a CPL-error code is set.
00059   2) It ensures that the location of the error in the _recipe_ code is noted.
00060   3) The error checking may be confined to a single concise line.
00061   4) It is not necessary to replicate memory deallocation for every error
00062      condition.
00063   5) If more extensive error reporting/handling is required it is not precluded
00064      by the use of skip_if().
00065   6) It allows for a single point of function return.
00066   7) It allows for optional, uniformly formatted debugging/tracing information
00067      at each macro invocation.
00068 
00069   The implementation of skip_if() uses a goto/label construction.
00070   According to Kerningham & Ritchie, The C Programming Language, 2nd edition,
00071   Section 3.8:
00072   "This organization is handy if the error-handling code is non-trivial,
00073   and if errors can occur in several places."
00074 
00075   The use of goto for any other purpose should be avoided.
00076 
00077 */
00078 
00079 #define skip_if(CONDITION) \
00080   do if (CONDITION) { \
00081     if (cpl_error_get_code()) { \
00082         cpl_msg_debug("", "Skip in %s line %d due to '%s' with error '%s' " \
00083                       "at %s", __FILE__, __LINE__, #CONDITION, \
00084                       cpl_error_get_message(), cpl_error_get_where());  \
00085         if (strstr(cpl_error_get_where(), "visir") == NULL) \
00086             cpl_error_set_where(""); \
00087     } else { \
00088         cpl_msg_debug("", "Skip in %s line %d due to '%s'", \
00089                         __FILE__, __LINE__, #CONDITION);  \
00090         cpl_error_set("", CPL_ERROR_UNSPECIFIED); \
00091     } \
00092     goto cleanup; \
00093   } else { \
00094     if (cpl_error_get_code()) \
00095         cpl_msg_debug("", "No skip in %s line %d due to '%s' with error '%s' " \
00096                       "at %s", __FILE__, __LINE__, #CONDITION, \
00097                       cpl_error_get_message(), cpl_error_get_where()); \
00098     else \
00099         cpl_msg_debug("", "No skip in %s line %d due to '%s'", \
00100                       __FILE__, __LINE__, #CONDITION);  \
00101   } while (0)
00102 
00103 
00104 #define end_skip \
00105     do { \
00106         cleanup: \
00107         if (cpl_error_get_code()) \
00108             cpl_msg_debug("", "Cleanup in %s line %d with error '%s' at %s", \
00109                            __FILE__, __LINE__, \
00110                           cpl_error_get_message(), cpl_error_get_where()); \
00111         else \
00112             cpl_msg_debug("", "Cleanup in %s line %d", \
00113                           __FILE__, __LINE__);  \
00114     } while (0)
00115 
00116 
00117 #include <cxstrutils.h>
00118 #include <cxstring.h>
00119 #include <cpl.h>
00120 #include <image_ops.h> 
00121 #include <cxstring.h>
00122 #include "sinfoni_globals.h"
00123 CPL_BEGIN_DECLS
00124 
00125 void insert_cube( OneCube ** list, OneCube * cube, int i );
00126 void destroy_cube_list ( OneCube ** list );
00127 OneCube ** new_cube_list( int n_cubes );
00128 void array_set_value( float * array, float value, int i ); 
00129 float array_get_value( float * array, int i );
00130 void insert_image( OneImage ** list, OneImage * im, int i );
00131 void destroy_list ( OneImage ** list );
00132 void destroy_array(float * array);
00133 void destroy_stringarray(char ** array, int size_x);
00134 void intarray_set_value( int * array, int value, int i );
00135 void array2D_set_value( float ** array, float value, int x, int y );
00136 void destroy_2Darray(float ** array, int size_x);
00137 void destroy_2Dintarray(int ** array, int size_x);
00138 void destroy_intarray(int * array);
00139 void destroy_doublearray(double * array);
00140 void doublearray_set_value( double * array, double value, int i );
00141 int intarray_get_value( int * array, int i );
00142 int * new_int_array( int size);
00143 int ** new_2Dint_array( int size_x, int size_y);
00144 char * get_rootname(char * filename);
00145 char * get_basename(const char *filename);
00146 float Stats_get_cleanstdev(Stats * stats);
00147 float Stats_get_cleanmean(Stats * stats);
00148 float array2D_get_value( float ** array, int x, int y );
00149 float * new_float_array( int size);
00150 float ** new_2Dfloat_array( int size_x, int size_y);
00151 double   doublearray_get_value( double * array, int i );
00152 double * new_double_array( int size);
00153 OneImage * promoteImageToMask ( OneImage * im, int * n_badpixels );
00154 OneImage ** new_image_list( int n_images );
00155 FitParams ** newFitParams( int n_params ) ;
00156 
00157 cpl_imagelist * sinfoni_frameset_to_iset(cpl_frameset *) ;
00158 cpl_imagelist * sinfoni_imagelist_load_frameset(const cpl_frameset * frameset,cpl_type type, 
00159                                             int pnum,int extnum);
00160 
00161 char ** my_frameset_to_filenames(cpl_frameset *set, int *nfiles);
00162 char ** my_frameset_to_tags(cpl_frameset *set, int *ntags);
00163 char ** my_frameset_to_filenames(cpl_frameset *set, int *nfiles);
00164 char ** my_frameset_to_tags(cpl_frameset *set, int *ntags);
00165 
00166 CPL_END_DECLS
00167 
00168 #endif

Generated on Wed Oct 26 13:08:56 2005 for SINFONI Pipeline Reference Manual by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001