/Users/huttone/Devel/sedflux-new/sedflux/trunk/ew/plume/plumefunct.c

Go to the documentation of this file.
00001 //---
00002 //
00003 // This file is part of sedflux.
00004 //
00005 // sedflux is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 2 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // sedflux is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with sedflux; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //
00019 //---
00020 
00021 /*
00022  *      PLUMEFUNCT.C            Various functions used by Plume2
00023  */
00024 
00025 #include <string.h>
00026 #include <glib.h>
00027 #include <utils/utils.h>
00028 
00029 /*
00030  *      convert n to characters in s
00031  *      From: Kerningham and Ritchie
00032  */
00033 void itoa(int n,char s[])
00034 {
00035    int c, i, j, sign;
00036    
00037    if((sign = n) < 0 )          /* record sign */
00038       n = -n;                   /* make n positive */
00039    i = 0;
00040    do {                         /* generate digits in reverse order */
00041       s[i++] = n % 10 + '0';    /* get next digit */
00042       } while ((n /= 10) > 0);  /* delete it */
00043    if ( sign < 0 )
00044       s[i++] = '-';
00045    s[i] = '\0';
00046                                 /* reverse the string */
00047    for( i=0, j=strlen(s)-1; i<j; i++, j--) {
00048       c    = s[i];
00049       s[i] = s[j];
00050       s[j] = c;
00051    }
00052 }
00053 
00054 /*
00055  *      The following was take from nrutil.c, I only wanted a subset
00056  *      and I wanted to make a standalone version so it was easier
00057  *      to use on various platforms
00058  */
00059  
00060 /* CAUTION: This is the ANSI C (only) version of the Numerical Recipes
00061    utility file nrutil.c.  Do not confuse this file with the same-named
00062    file nrutil.c that is supplied in the same subdirectory or archive
00063    as the header file nrutil.h.  *That* file contains both ANSI and
00064    traditional K&R versions, along with #ifdef macros to select the
00065    correct version.  *This* file contains only ANSI C.               */
00066 
00067 #include <stdio.h>
00068 #include <stddef.h>
00069 #include <stdlib.h>
00070 #define NR_END 1
00071 #define FREE_ARG char*
00072 
00073 #ifdef NOTDEFINED
00074 
00075 void nrerror(char error_text[])
00076 /* Numerical Recipes standard error handler */
00077 {
00078         fprintf(stderr,"Numerical Recipes run-time error...\n");
00079         fprintf(stderr,"%s\n",error_text);
00080         fprintf(stderr,"...now exiting to system...\n");
00081         eh_exit(1);
00082 }
00083 
00084 #endif
00085 
00086 float **matrix(long nrl, long nrh, long ncl, long nch)
00087 /* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */
00088 {
00089         long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
00090         float **m;
00091 
00092          eh_require( nch-ncl>0 );
00093          eh_require( nrh-nrl>0 );
00094 
00095         /* allocate pointers to rows */
00096 //      m=(float **) malloc((size_t)((nrow+NR_END)*sizeof(float*)));
00097 //        m = (float**)eh_malloc( ((size_t)(nrow+NR_END)*sizeof(float*)) ,
00098 //                                NULL , __FILE__ , __LINE__ );
00099         m = eh_new( float* , nrow+NR_END );
00100         if (!m) eh_error("allocation failure 1 in matrix()");
00101         m += NR_END;
00102         m -= nrl;
00103 
00104         /* allocate rows and set pointers to them */
00105 //      m[nrl] = (float *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float)));
00106 //        m[nrl] = (float*)eh_malloc( ((size_t)(nrow*ncol+NR_END)*sizeof(float)) ,
00107 //                                    NULL , __FILE__ , __LINE__ );
00108         m[nrl] = eh_new( float , nrow*ncol+NR_END );
00109 
00110         if (!m[nrl]) eh_error("allocation failure 2 in matrix()");
00111         m[nrl] += NR_END;
00112         m[nrl] -= ncl;
00113 
00114         for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
00115 
00116         /* return pointer to array of pointers to rows */
00117         return m;
00118 }
00119 
00120 double **new_dmatrix( long n_rows , long n_cols )
00121 {
00122    int i;
00123    double **m;
00124 
00125    eh_require( n_rows>0 );
00126    eh_require( n_cols>0 );
00127 
00128    m = eh_new( double* , n_rows );
00129 
00130    m[0] = eh_new( double , n_rows*n_cols );
00131 
00132    for ( i=1 ; i<n_rows ; i++ )
00133       m[i] = m[i-1]+n_cols;
00134 
00135    return m;
00136 }
00137 
00138 float ***f3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh)
00139 /* allocate a float 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
00140 {
00141         long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
00142         float ***t;
00143 
00144          eh_require( ndh-ndl>0 );
00145          eh_require( nch-ncl>0 );
00146          eh_require( nrh-nrl>0 );
00147 
00148         /* allocate pointers to pointers to rows */
00149 //      t=(float ***) malloc((size_t)((nrow+NR_END)*sizeof(float**)));
00150 //        t = (float***)eh_malloc( ((size_t)(nrow+NR_END)*sizeof(float***)) ,
00151 //                                 NULL , __FILE__ , __LINE__ );
00152         t = eh_new( float** , nrow+NR_END );
00153         if (!t) eh_error("allocation failure 1 in f3tensor()");
00154         t += NR_END;
00155         t -= nrl;
00156 
00157         /* allocate pointers to rows and set pointers to them */
00158 //      t[nrl]=(float **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float*)));
00159 //        t[nrl]=(float**)eh_malloc(((size_t)(nrow*ncol+NR_END)*sizeof(float*)) ,
00160 //                                  NULL , __FILE__ , __LINE__ );
00161         t[nrl]=eh_new( float* , nrow*ncol+NR_END );
00162 
00163         if (!t[nrl]) eh_error("allocation failure 2 in f3tensor()");
00164         t[nrl] += NR_END;
00165         t[nrl] -= ncl;
00166 
00167         /* allocate rows and set pointers to them */
00168 //      t[nrl][ncl]=(float *) malloc((size_t)((nrow*ncol*ndep+NR_END)*sizeof(float)));
00169 //        t[nrl][ncl]=(float*)eh_malloc(((size_t)(nrow*ncol*ndep+NR_END)*sizeof(float)) ,
00170 //                                  NULL , __FILE__ , __LINE__ );
00171         t[nrl][ncl]=eh_new( float , nrow*ncol*ndep+NR_END );
00172 
00173         if (!t[nrl][ncl]) eh_error("allocation failure 3 in f3tensor()");
00174         t[nrl][ncl] += NR_END;
00175         t[nrl][ncl] -= ndl;
00176 
00177         for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
00178         for(i=nrl+1;i<=nrh;i++) {
00179                 t[i]=t[i-1]+ncol;
00180                 t[i][ncl]=t[i-1][ncl]+ncol*ndep;
00181                 for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
00182         }
00183 
00184         /* return pointer to array of pointers to rows */
00185         return t;
00186 }
00187 
00188 double ***new_d3tensor( long n_rows , long n_cols , long n_dep )
00189 {
00190    int i, j;
00191    double ***t;
00192 
00193    t = eh_new( double** , n_rows );
00194 
00195    t[0] = eh_new( double* , n_rows*n_cols );
00196 
00197    t[0][0] = eh_new( double , n_rows*n_cols*n_dep );
00198 
00199    for ( j=1 ; j<n_cols ; j++ )
00200       t[0][j] = t[0][j-1]+n_dep;
00201 
00202    for ( i=1 ; i<n_rows ; i++ )
00203    {
00204       t[i]    = t[i-1]+n_cols;
00205       t[i][0] = t[i-1][0]+n_cols*n_dep;
00206       for ( j=1 ; j<n_cols ; j++ )
00207          t[i][j]=t[i][j-1]+n_dep;
00208    }
00209 
00210    return t;
00211 }
00212 
00213 void free_matrix(float **m, long nrl, long nrh, long ncl, long nch)
00214 /* free a float matrix allocated by matrix() */
00215 {
00216    eh_free_mem((FREE_ARG) (m[nrl]+ncl-NR_END));
00217    eh_free_mem((FREE_ARG) (m+nrl-NR_END));
00218 }
00219 
00220 void free_dmatrix( double **m )
00221 {
00222    if ( m )
00223    {
00224       eh_free( m[0] );
00225       eh_free( m    );
00226    }
00227 }
00228 
00229 void free_f3tensor(float ***t, long nrl, long nrh, long ncl, long nch, long ndl, long ndh)
00230 /* free a float f3tensor allocated by f3tensor() */
00231 {
00232    eh_free_mem((FREE_ARG) (t[nrl][ncl]+ndl-NR_END));
00233    eh_free_mem((FREE_ARG) (t[nrl]+ncl-NR_END));
00234    eh_free_mem((FREE_ARG) (t+nrl-NR_END));
00235 }
00236 
00237 void free_d3tensor( double ***t )
00238 {
00239    if ( t )
00240    {
00241       if ( t[0] )
00242       {
00243          eh_free( t[0][0] );
00244          eh_free( t[0] );
00245       }
00246       eh_free( t );
00247    }
00248 }
00249 

Generated on Fri Jan 4 18:04:15 2008 for sedflux by  doxygen 1.5.2