sub_period_common.c

Go to the documentation of this file.
00001 /* ***************************************************** */
00002 /* Select a sub period of a vector using a common period */
00003 /* over two different time vectors.                      */
00004 /* sub_period_common.c                                   */
00005 /* ***************************************************** */
00006 /* Author: Christian Page, CERFACS, Toulouse, France.    */
00007 /* ***************************************************** */
00012 /* LICENSE BEGIN
00013 
00014 Copyright Cerfacs (Christian Page) (2015)
00015 
00016 christian.page@cerfacs.fr
00017 
00018 This software is a computer program whose purpose is to downscale climate
00019 scenarios using a statistical methodology based on weather regimes.
00020 
00021 This software is governed by the CeCILL license under French law and
00022 abiding by the rules of distribution of free software. You can use, 
00023 modify and/ or redistribute the software under the terms of the CeCILL
00024 license as circulated by CEA, CNRS and INRIA at the following URL
00025 "http://www.cecill.info". 
00026 
00027 As a counterpart to the access to the source code and rights to copy,
00028 modify and redistribute granted by the license, users are provided only
00029 with a limited warranty and the software's author, the holder of the
00030 economic rights, and the successive licensors have only limited
00031 liability. 
00032 
00033 In this respect, the user's attention is drawn to the risks associated
00034 with loading, using, modifying and/or developing or reproducing the
00035 software by the user in light of its specific status of free software,
00036 that may mean that it is complicated to manipulate, and that also
00037 therefore means that it is reserved for developers and experienced
00038 professionals having in-depth computer knowledge. Users are therefore
00039 encouraged to load and test the software's suitability as regards their
00040 requirements in conditions enabling the security of their systems and/or 
00041 data to be ensured and, more generally, to use and operate it in the 
00042 same conditions as regards security. 
00043 
00044 The fact that you are presently reading this means that you have had
00045 knowledge of the CeCILL license and that you accept its terms.
00046 
00047 LICENSE END */
00048 
00049 
00050 
00051 
00052 
00053 
00054 
00055 #include <utils.h>
00056 
00058 int
00059 sub_period_common(double **buf_sub, int *ntime_sub, double *bufin, int *year, int *month, int *day,
00060                   int *year_learn, int *month_learn, int *day_learn, int timedim, int ndima, int ndimb, int ntime, int ntime_learn) {
00078   int *buf_sub_i = NULL; /* Time indexes for common period */
00079 
00080   int dima; /* First dimension */
00081   int dimb; /* Second dimension */
00082   int t; /* Time loop counter */
00083   int tt; /* Time loop counter for second time vector */
00084 
00085   /* Initialize number of common times */
00086   *ntime_sub = 0;
00087 
00088   /* Loop over first time vector and find common day/month/year and store time indexes for these common times */
00089   for (t=0; t<ntime; t++) {
00090     /* Search in all second time vector times for matching date */
00091     for (tt=0; tt<ntime_learn; tt++) {
00092       if (year[t]  == year_learn[tt] &&
00093           month[t] == month_learn[tt] &&
00094           day[t]   == day_learn[tt]) {
00095         /* Found common date, store time index */
00096         buf_sub_i = (int *) realloc(buf_sub_i, ((*ntime_sub)+1) * sizeof(int));
00097         if (buf_sub_i == NULL) alloc_error(__FILE__, __LINE__);
00098         buf_sub_i[(*ntime_sub)++] = t;
00099       }
00100     }
00101   }
00102 
00103   if ( (*ntime_sub) == 0 ) {
00104     (void) fprintf(stderr, "%s: FATAL ERROR: No common subperiod! Maybe a problem in the time representation in the control run file.\nAborting.\n", __FILE__);
00105     (void) printf("MODEL TIMES ntime=%d\n", ntime);
00106     //#if DEBUG > 7
00107     for (t=0; t<ntime; t++)
00108       (void) printf("%d %d %d\n", year[t], month[t], day[t]);
00109     (void) printf("LEARNING TIMES ntime=%d\n", ntime_learn);
00110     for (t=0; t<ntime_learn; t++)
00111       (void) printf("%d %d %d\n", year_learn[t], month_learn[t], day_learn[t]);
00112     //#endif
00113     return -1;
00114   }
00115   
00116   (void) printf("%s: Sub-period: %d %d %d %d %d %d. Indexes: %d %d\n",__FILE__, year[buf_sub_i[0]], month[buf_sub_i[0]],
00117                 day[buf_sub_i[0]], year[buf_sub_i[(*ntime_sub)-1]],month[buf_sub_i[(*ntime_sub)-1]],
00118                 day[buf_sub_i[(*ntime_sub)-1]], buf_sub_i[0], buf_sub_i[(*ntime_sub)-1]);
00119 
00120   /* Allocate memory for output buffer */
00121   (*buf_sub) = (double *) malloc((*ntime_sub)*ndima*ndimb * sizeof(double));
00122   if ((*buf_sub) == NULL) alloc_error(__FILE__, __LINE__);
00123   /* Construct new 3D matrix with common times */
00124   if (timedim == 3)
00125     /* Time dimension is last */
00126     for (t=0; t<(*ntime_sub); t++)
00127       for (dimb=0; dimb<ndimb; dimb++)
00128         for (dima=0; dima<ndima; dima++)
00129           (*buf_sub)[dima+(dimb*ndima)+t*ndima*ndimb] = bufin[dima+dimb*ndima+buf_sub_i[t]*ndima*ndimb];
00130   else if (timedim == 1)
00131     /* Time dimension is first */
00132     for (t=0; t<(*ntime_sub); t++)
00133       for (dimb=0; dimb<ndimb; dimb++)
00134         for (dima=0; dima<ndima; dima++)
00135           (*buf_sub)[t+dima*(*ntime_sub)+dimb*(ndima*(*ntime_sub))] = bufin[buf_sub_i[t]+dima*ntime+dimb*(ndima*ntime)];
00136   else
00137     (void) fprintf(stderr, "%s: Fatal error: timedim argument must be equal to 1 or 3.\n", __FILE__);
00138 
00139   /* Free memory */
00140   (void) free(buf_sub_i);
00141 
00142   /* Success */
00143   return 0;
00144 }

Generated on 12 May 2016 for DSCLIM by  doxygen 1.6.1