/Users/huttone/Devel/sedflux-new/sedflux/trunk/ew/hydrotrend/hydrofunct.c

Go to the documentation of this file.
00001 /*
00002  *  HydroFunct.c
00003  *
00004  *  Various functions used by HydroTrend
00005  *  From Kerningham and Ritchie
00006  *  and Numerical Recipes in C
00007  *
00008  *  Author:     M.D. Morehead   (1998)
00009  *
00010  */
00011 
00012 #include <string.h>
00013 
00014 /*--------------------------------
00015  *  Convert n to characters in s
00016  *  From: Kerningham and Ritchie
00017  *--------------------------------*/
00018 int itoa(int n,char s[]){
00019    int c, i, j, sign;
00020 
00021    if((sign = n) < 0 )          /* record sign */
00022       n = -n;                   /* make n positive */
00023    i = 0;
00024    do {                         /* generate digits in reverse order */
00025       s[i++] = n % 10 + '0';    /* get next digit */
00026       } while ((n /= 10) > 0);  /* delete it */
00027    if ( sign < 0 )
00028       s[i++] = '-';
00029    s[i] = '\0';
00030                                 /* reverse the string */
00031    for( i=0, j=strlen(s)-1; i<j; i++, j--) {
00032       c    = s[i];
00033       s[i] = s[j];
00034       s[j] = c;
00035    }
00036    return 0;
00037 }
00038 
00039 /*----------------------------------------------------------------
00040  *  The following was take from nrutil.c, I only wanted a subset
00041  *  and I wanted to make a standalone version so it was easier
00042  *  to use on various platforms
00043  *----------------------------------------------------------------*/
00044 
00045 /* CAUTION: This is the ANSI C (only) version of the Numerical Recipes
00046    utility file nrutil.c.  Do not confuse this file with the same-named
00047    file nrutil.c that is supplied in the same subdirectory or archive
00048    as the header file nrutil.h.  *That* file contains both ANSI and
00049    traditional K&R versions, along with #ifdef macros to select the
00050    correct version.  *This* file contains only ANSI C.               */
00051 
00052 #include <stdio.h>
00053 #include <stddef.h>
00054 #include <stdlib.h>
00055 #define NR_END 1
00056 #define FREE_ARG char*
00057 
00058 /*--------------------------------------------
00059  *  Numerical Recipes standard error handler
00060  *--------------------------------------------*/
00061 void nrerror(char error_text[]){
00062         fprintf(stderr,"Numerical Recipes run-time error...\n");
00063         fprintf(stderr,"%s\n",error_text);
00064         fprintf(stderr,"...now exiting to system...\n");
00065         exit(1);
00066 }
00067 
00068 /*----------------------------------------------------------------------
00069  *  allocate a float matrix with subscript range m[nrl..nrh][ncl..nch]
00070  *----------------------------------------------------------------------*/
00071 float **matrix(long nrl, long nrh, long ncl, long nch){
00072         long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
00073         float **m;
00074 
00075         /* allocate pointers to rows */
00076         m=(float **) malloc((size_t)((nrow+NR_END)*sizeof(float*)));
00077         if (!m) nrerror("allocation failure 1 in matrix()");
00078         m += NR_END;
00079         m -= nrl;
00080 
00081         /* allocate rows and set pointers to them */
00082         m[nrl]=(float *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float)));
00083         if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
00084         m[nrl] += NR_END;
00085         m[nrl] -= ncl;
00086 
00087         for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
00088         /* return pointer to array of pointers to rows */
00089         return m;
00090 }
00091 
00092 /*-----------------------------------------------------------------------
00093  *  allocate a double matrix with subscript range m[nrl..nrh][ncl..nch]
00094  *-----------------------------------------------------------------------*/
00095 double **dmatrix(long nrl, long nrh, long ncl, long nch){
00096         long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
00097         double **m;
00098 
00099         /* allocate pointers to rows */
00100         m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
00101         if (!m) nrerror("allocation failure 1 in matrix()");
00102         m += NR_END;
00103         m -= nrl;
00104 
00105         /* allocate rows and set pointers to them */
00106         m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
00107         if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
00108         m[nrl] += NR_END;
00109         m[nrl] -= ncl;
00110 
00111         for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
00112 
00113         /* return pointer to array of pointers to rows */
00114         return m;
00115 }
00116 
00117 /*-----------------------------------------------------------------------
00118  *  allocate a float 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh]
00119  *-----------------------------------------------------------------------*/
00120 float ***f3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh){
00121         long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
00122         float ***t;
00123 
00124         /* allocate pointers to pointers to rows */
00125         t=(float ***) malloc((size_t)((nrow+NR_END)*sizeof(float**)));
00126         if (!t) nrerror("allocation failure 1 in f3tensor()");
00127         t += NR_END;
00128         t -= nrl;
00129 
00130         /* allocate pointers to rows and set pointers to them */
00131         t[nrl]=(float **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float*)));
00132         if (!t[nrl]) nrerror("allocation failure 2 in f3tensor()");
00133         t[nrl] += NR_END;
00134         t[nrl] -= ncl;
00135 
00136         /* allocate rows and set pointers to them */
00137         t[nrl][ncl]=(float *) malloc((size_t)((nrow*ncol*ndep+NR_END)*sizeof(float)));
00138         if (!t[nrl][ncl]) nrerror("allocation failure 3 in f3tensor()");
00139         t[nrl][ncl] += NR_END;
00140         t[nrl][ncl] -= ndl;
00141 
00142         for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
00143         for(i=nrl+1;i<=nrh;i++) {
00144                 t[i]=t[i-1]+ncol;
00145                 t[i][ncl]=t[i-1][ncl]+ncol*ndep;
00146                 for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
00147         }
00148 
00149         /* return pointer to array of pointers to rows */
00150         return t;
00151 }
00152 
00153 /*--------------------------------------------------------------------------
00154  *  allocate a double 3D tensor with range t[nrl..nrh][ncl..nch][ndl..ndh]
00155  *--------------------------------------------------------------------------*/
00156 double ***d3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh){
00157         long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
00158         double ***t;
00159 
00160         /* allocate pointers to pointers to rows */
00161         t=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double**)));
00162         if (!t) nrerror("allocation failure 1 in d3tensor()");
00163         t += NR_END;
00164         t -= nrl;
00165 
00166         /* allocate pointers to rows and set pointers to them */
00167         t[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double*)));
00168         if (!t[nrl]) nrerror("allocation failure 2 in d3tensor()");
00169         t[nrl] += NR_END;
00170         t[nrl] -= ncl;
00171 
00172         /* allocate rows and set pointers to them */
00173         t[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*ndep+NR_END)*sizeof(double)));
00174         if (!t[nrl][ncl]) nrerror("allocation failure 3 in d3tensor()");
00175         t[nrl][ncl] += NR_END;
00176         t[nrl][ncl] -= ndl;
00177 
00178         for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
00179         for(i=nrl+1;i<=nrh;i++) {
00180                 t[i]=t[i-1]+ncol;
00181                 t[i][ncl]=t[i-1][ncl]+ncol*ndep;
00182                 for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
00183         }
00184 
00185         /* return pointer to array of pointers to rows */
00186         return t;
00187 }
00188 
00189 /*---------------------------------------------
00190  *  free a float matrix allocated by matrix()
00191  *---------------------------------------------*/
00192 void free_matrix(float **m, long nrl, long nrh, long ncl, long nch){
00193    free((FREE_ARG) (m[nrl]+ncl-NR_END));
00194    free((FREE_ARG) (m+nrl-NR_END));
00195 }
00196 
00197 /*-----------------------------------------------
00198  *  free a double matrix allocated by dmatrix()
00199  *-----------------------------------------------*/
00200 void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch){
00201    free((FREE_ARG) (m[nrl]+ncl-NR_END));
00202    free((FREE_ARG) (m+nrl-NR_END));
00203 }
00204 
00205 /*-------------------------------------------------
00206  *  free a float f3tensor allocated by f3tensor()
00207  *-------------------------------------------------*/
00208 void free_f3tensor(float ***t, long nrl, long nrh, long ncl, long nch, long ndl, long ndh){
00209    free((FREE_ARG) (t[nrl][ncl]+ndl-NR_END));
00210    free((FREE_ARG) (t[nrl]+ncl-NR_END));
00211    free((FREE_ARG) (t+nrl-NR_END));
00212 }
00213 
00214 /*-------------------------------------------------
00215  *  free a float d3tensor allocated by d3tensor()
00216  *-------------------------------------------------*/
00217 void free_d3tensor(double ***t, long nrl, long nrh, long ncl, long nch, long ndl, long ndh){
00218    free((FREE_ARG) (t[nrl][ncl]+ndl-NR_END));
00219    free((FREE_ARG) (t[nrl]+ncl-NR_END));
00220    free((FREE_ARG) (t+nrl-NR_END));
00221 }
00222 #include "hydrodaysmonths.h"
00223 
00224 int days_in_month( Month m )
00225 {
00226    static int daysim[12]  = { 31 , 28 , 31 , 30 ,
00227                               31 , 30 , 31 , 31 ,
00228                               30 , 31 , 30 , 31 };
00229    return daysim[m];
00230 }
00231 
00232 int start_of( Month m )
00233 {
00234    static int daystrm[12] = {   1 ,  32 ,  60 ,  91 ,
00235                               121 , 152 , 182 , 213 ,
00236                               244 , 274 , 305 , 335 };
00237    return daystrm[m];
00238 }
00239 
00240 int end_of( Month m )
00241 {
00242    static int dayendm[12] = {  31 ,  59 ,  90 , 120 ,
00243                               151 , 181 , 212 , 243 ,
00244                               273 , 304 , 334 , 365 };
00245    return dayendm[m];
00246 }
00247 

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