00001 /* ***************************************************** */ 00002 /* Compute daily climatology for climatological year */ 00003 /* clim_daily_tserie_climyear.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 clim_daily_tserie_climyear(double *bufout, double *bufin, tstruct *buftime, double missing_val, int ni, int nj, int nt) { 00069 int *index = NULL; /* Index to flag matching a specific day and month in a time serie covering several years. */ 00070 double *sum; /* Sum over all matching days. */ 00071 int *ndays = NULL; /* Number of days matching days. */ 00072 int month; /* Climatological month. */ 00073 00074 int t; /* Loop counter for time. */ 00075 int i; /* Loop counter for ni. */ 00076 int j; /* Loop counter for nj. */ 00077 int day; /* Loop counter for days. */ 00078 00079 (void) fprintf(stdout, "%s: Computing climatological months of a daily time serie.\n", __FILE__); 00080 00081 /* Allocate memory */ 00082 index = (int *) calloc(nt, sizeof(int)); 00083 if (index == NULL) alloc_error(__FILE__, __LINE__); 00084 sum = (double *) malloc(ni*nj * sizeof(double)); 00085 if (sum == NULL) alloc_error(__FILE__, __LINE__); 00086 ndays = (int *) malloc(ni*nj * sizeof(int)); 00087 if (ndays == NULL) alloc_error(__FILE__, __LINE__); 00088 00089 /* Loop over the 12 months of the year to generate daily climatological months */ 00090 for (month=1; month<=12; month++) { 00091 00092 /* Loop over each day of the month */ 00093 for (day=1; day<=31; day++) { 00094 00095 for (j=0; j<nj; j++) 00096 for (i=0; i<ni; i++) { 00097 sum[i+j*ni] = 0.0; 00098 ndays[i+j*ni] = 0; /* Initialize the number of days */ 00099 } 00100 00101 /* Loop over all the times */ 00102 for (t=0; t<nt; t++) { 00103 /* Initialize */ 00104 index[t] = 0; 00105 if (buftime[t].day == day && buftime[t].month == month) { 00106 /* The climatological day and month match */ 00107 index[t] = 1; /* Flag it */ 00108 for (j=0; j<nj; j++) 00109 for (i=0; i<ni; i++) 00110 if (bufin[i+j*ni+t*ni*nj] != missing_val) { 00111 /* Ignore missing values */ 00112 sum[i+j*ni] += bufin[i+j*ni+t*ni*nj]; /* Sum all the values for this matching day/month */ 00113 ndays[i+j*ni]++; 00114 } 00115 } 00116 } 00117 for (t=0; t<nt; t++) 00118 /* Compute the mean over all the matching days and apply mean for all these flagged days */ 00119 /* Assign mean value for all flagged days used in computing this mean */ 00120 if (index[t] == 1) { 00121 for (j=0; j<nj; j++) 00122 for (i=0; i<ni; i++) 00123 if (ndays[i+j*ni] > 0) { 00124 bufout[i+j*ni+t*ni*nj] = sum[i+j*ni] / (double) ndays[i+j*ni]; 00125 } 00126 else { 00127 bufout[i+j*ni+t*ni*nj] = missing_val; 00128 } 00129 } 00130 } 00131 } 00132 00133 /* Free memory */ 00134 (void) free(index); 00135 (void) free(sum); 00136 (void) free(ndays); 00137 }