filter.c

Go to the documentation of this file.
00001 /* ***************************************************** */
00002 /* Filter subroutine.                                    */
00003 /* filter.c                                              */
00004 /* ***************************************************** */
00005 /* Author: Christian Page, CERFACS, Toulouse, France.    */
00006 /* ***************************************************** */
00011 /* LICENSE BEGIN
00012 
00013 Copyright Cerfacs (Christian Page) (2015)
00014 
00015 christian.page@cerfacs.fr
00016 
00017 This software is a computer program whose purpose is to downscale climate
00018 scenarios using a statistical methodology based on weather regimes.
00019 
00020 This software is governed by the CeCILL license under French law and
00021 abiding by the rules of distribution of free software. You can use, 
00022 modify and/ or redistribute the software under the terms of the CeCILL
00023 license as circulated by CEA, CNRS and INRIA at the following URL
00024 "http://www.cecill.info". 
00025 
00026 As a counterpart to the access to the source code and rights to copy,
00027 modify and redistribute granted by the license, users are provided only
00028 with a limited warranty and the software's author, the holder of the
00029 economic rights, and the successive licensors have only limited
00030 liability. 
00031 
00032 In this respect, the user's attention is drawn to the risks associated
00033 with loading, using, modifying and/or developing or reproducing the
00034 software by the user in light of its specific status of free software,
00035 that may mean that it is complicated to manipulate, and that also
00036 therefore means that it is reserved for developers and experienced
00037 professionals having in-depth computer knowledge. Users are therefore
00038 encouraged to load and test the software's suitability as regards their
00039 requirements in conditions enabling the security of their systems and/or 
00040 data to be ensured and, more generally, to use and operate it in the 
00041 same conditions as regards security. 
00042 
00043 The fact that you are presently reading this means that you have had
00044 knowledge of the CeCILL license and that you accept its terms.
00045 
00046 LICENSE END */
00047 
00048 
00049 
00050 
00051 
00052 
00053 
00054 #include <filter.h>
00055 
00057 void
00058 filter(double *bufferf, double *buffer, char *type, int width, int ni, int nj, int nt) {
00069   double *filter = NULL; /* Filter window vector */
00070   double *tmpvec = NULL; /* Temporary vector */
00071 
00072   int half_width; /* Half-width of filter window. */
00073   int i; /* Loop counter for ni. */
00074   int j; /* Loop counter for nj. */
00075   int t; /* Loop counter. */
00076   int tt; /* Loop counter. */
00077 
00078   double sum; /* To sum values over filter window width. */
00079 
00080   /*  (void) fprintf(stdout, "%s: Filtering data with a %s filter.\n", __FILE__, type);*/
00081 
00082   if ( !strcmp(type, "hanning") ) {
00083     /* Hanning filter implementation */
00084 
00085     /* Compute filter window vector */
00086     (void) filter_window(&filter, type, width);
00087     
00088     /* Half-width */
00089     half_width = ( width - 1 ) / 2;
00090     
00091     /* Expanded version of vector: wrapping edges. */
00092     tmpvec = (double *) calloc(nt*2, sizeof(double));
00093     if (tmpvec == NULL) alloc_error(__FILE__, __LINE__);
00094 
00095     for (j=0; j<nj; j++)
00096       for (i=0; i<ni; i++) {
00097         
00098         for (t=0; t<half_width; t++) {
00099           tmpvec[t] = buffer[i+j*ni+(nt-half_width+t)*ni*nj];
00100         }
00101         for (t=half_width; t<(half_width+nt); t++) {
00102           tmpvec[t] = buffer[i+j*ni+(t-half_width)*ni*nj];
00103         }
00104         for (t=(half_width+nt); t<(nt*2); t++) {
00105           tmpvec[t] = buffer[i+j*ni+(t-(half_width+nt))*ni*nj];
00106         }
00107         
00108         /* Apply filter. */
00109         for (t=0; t<nt; t++) {
00110           sum = 0.0;
00111           for (tt=t; tt<(t+width-1); tt++)
00112             sum += (filter[tt-t] * tmpvec[tt]);
00113           bufferf[i+j*ni+t*ni*nj] = sum;
00114         }
00115       }
00116     
00117     /* Free memory */
00118     (void) free(filter);
00119     (void) free(tmpvec);
00120   }
00121   else {
00122     /* Unknown filter type */
00123     (void) fprintf(stderr, "%s: ABORT: Unknown filtering type: %s\n", __FILE__, type);
00124     (void) abort();
00125   }
00126 }

Generated on 12 May 2016 for DSCLIM by  doxygen 1.6.1