00001 /* ***************************************************** */ 00002 /* Compute mean and variance of a field averaged */ 00003 /* spatially. */ 00004 /* mean_variance_field_spatial.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 void 00059 mean_variance_field_spatial(double *buf_mean, double *buf_var, double *buf, short int *mask, int ni, int nj, int ntime) { 00060 00071 double sum; /* Sum to compute mean */ 00072 double *buf_smean = NULL; /* Vector (over time) of spatially averaged data */ 00073 00074 int t; /* Time loop counter */ 00075 int i; /* Loop counter */ 00076 int j; /* Loop counter */ 00077 int pts = 0; /* Points counter */ 00078 00079 /* Allocate memory */ 00080 buf_smean = (double *) malloc(ntime * sizeof(double)); 00081 if (buf_smean == NULL) alloc_error(__FILE__, __LINE__); 00082 00083 /* Loop over all times and calculate spatial average, optionally using a mask */ 00084 if (mask == NULL) 00085 for (t=0; t<ntime; t++) { 00086 sum = 0.0; 00087 for (j=0; j<nj; j++) 00088 for (i=0; i<ni; i++) 00089 sum += buf[i+j*ni+t*ni*nj]; 00090 buf_smean[t] = sum / (double) (ni*nj); 00091 } 00092 else { 00093 for (t=0; t<ntime; t++) { 00094 sum = 0.0; 00095 pts = 0; 00096 for (j=0; j<nj; j++) 00097 for (i=0; i<ni; i++) 00098 if (mask[i+j*ni] == 1) { 00099 sum += buf[i+j*ni+t*ni*nj]; 00100 pts++; 00101 } 00102 buf_smean[t] = sum / (double) pts; 00103 } 00104 } 00105 00106 /* Compute mean and variance over time */ 00107 *buf_mean = gsl_stats_mean(buf_smean, 1, ntime); 00108 *buf_var = gsl_stats_variance(buf_smean, 1, ntime); 00109 00110 /* Free memory */ 00111 (void) free(buf_smean); 00112 }