remove_seasonal_cycle.c

Go to the documentation of this file.
00001 /* ***************************************************** */
00002 /* Remove seasonal cycle for a time serie                */
00003 /* remove_seasonal_cycle.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 <clim.h>
00055 
00057 void
00058 remove_seasonal_cycle(double *bufout, double *clim, double *bufin, tstruct *buftime, double missing_val,
00059                       int filter_width, char *type, int clim_provided, int ni, int nj, int ntime) {
00074   double *tmpbuf = NULL; /* Temporary vector. */
00075   int i; /* Loop counter for ni. */
00076   int j; /* Loop counter for nj. */
00077   int t; /* Loop counter. */
00078   int ndays_m = 31; /* Maximum number of days in a month. */
00079   int month; /* Climatological month. */
00080   int day; /* Climatological day. */
00081   int dayofclimy; /* Day of year in a 366-day climatological year */
00082 
00083   /* Allocate temporay buffer memory. */
00084   tmpbuf = (double *) calloc(ni*nj*ntime, sizeof(double));
00085   if (tmpbuf == NULL) alloc_error(__FILE__, __LINE__);
00086 
00087   (void) fprintf(stdout, "%s: Removing seasonal cycle for a time serie.\n", __FILE__);
00088 
00089   if (clim_provided != TRUE) {
00090     /* Climatology field was not provided */
00091 
00092     /* Compute daily climatologies for climatological year */
00093     (void) clim_daily_tserie_climyear(bufout, bufin, buftime, missing_val, ni, nj, ntime);
00094     (void) fprintf(stdout, "%s: Using a %s filter for climatology (wrap edges).\n", __FILE__, type);
00095     /* Filter climatologies using a filter (wrap edges) */
00096     (void) filter(tmpbuf, bufout, type, filter_width, ni, nj, ntime);
00097     
00098     /* Remove climatology from time serie */
00099     for (t=0; t<ntime; t++)
00100       for (j=0; j<nj; j++)
00101         for (i=0; i<ni; i++)
00102           bufout[i+j*ni+t*ni*nj] = bufin[i+j*ni+t*ni*nj] - tmpbuf[i+j*ni+t*ni*nj];
00103     
00105     for (month=0; month<12; month++)
00106       for (day=0; day<ndays_m; day++)
00107         /* Loop over all the times */
00108         for (t=0; t<ntime; t++) {
00109           if (buftime[t].day == (day+1) && buftime[t].month == (month+1)) {
00110             dayofclimy = dayofclimyear(day+1, month+1);
00111             for (j=0; j<nj; j++)
00112               for (i=0; i<ni; i++)
00113                 clim[i+j*ni+(dayofclimy-1)*ni*nj] = tmpbuf[i+j*ni+t*ni*nj];
00114             /* Exit loop: matched month and day */
00115             t = ntime;
00116           }
00117         }
00118   }
00119   else {
00120     /* Climatology field was provided */
00121     /* Loop over all the times */
00122     for (t=0; t<ntime; t++) {
00123       dayofclimy = dayofclimyear(buftime[t].day, buftime[t].month);
00124       for (j=0; j<nj; j++)
00125         for (i=0; i<ni; i++)
00126           bufout[i+j*ni+t*ni*nj] = bufin[i+j*ni+t*ni*nj] - clim[i+j*ni+(dayofclimy-1)*ni*nj];
00127     }
00128   }
00129   
00130   /* Free memory */
00131   (void) free(tmpbuf);
00132 }

Generated on 12 May 2016 for DSCLIM by  doxygen 1.6.1