00001 /* ***************************************************** */ 00002 /* Compute the spatial covariance of two fields. */ 00003 /* covariance_fields_spatial.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 <utils.h> 00055 00057 void 00058 covariance_fields_spatial(double *cov, double *buf1, double *buf2, short int *mask, int t1, int t2, int ni, int nj) { 00059 00071 int i; /* Loop counter */ 00072 int j; /* Loop counter */ 00073 int pts = 0; /* Points counter */ 00074 int num = 0; /* Elements counter */ 00075 00076 double sum1; /* Temporary sum for buf1 */ 00077 double sum2; /* Temporary sum for buf2 */ 00078 double mean1; /* Mean for buf1 */ 00079 double mean2; /* Mean for buf2 */ 00080 00081 /* Compute spatial covariance, optionally using a mask */ 00082 if (mask == NULL) { 00083 00084 /* Calculate mean of each field */ 00085 sum1 = 0.0; 00086 sum2 = 0.0; 00087 for (j=0; j<nj; j++) 00088 for (i=0; i<ni; i++) { 00089 sum1 += buf1[i+j*ni+t1*ni*nj]; 00090 sum2 += buf2[i+j*ni+t2*ni*nj]; 00091 } 00092 mean1 = sum1 / (double) (ni*nj); 00093 mean2 = sum2 / (double) (ni*nj); 00094 00095 /* Calculate spatial covariance */ 00096 *cov = 0.0; 00097 num = 0; 00098 for (j=0; j<nj; j++) 00099 for (i=0; i<ni; i++) { 00100 /* Sum of the squares (remove mean) */ 00101 (*cov) += ( ( (buf1[i+j*ni+t1*ni*nj] - mean1) * (buf2[i+j*ni+t2*ni*nj] - mean2) ) - (*cov)) / (double) (num + 1); 00102 num++; 00103 } 00104 } 00105 else { 00106 /* Calculate mean of each field */ 00107 sum1 = 0.0; 00108 sum2 = 0.0; 00109 pts = 0; 00110 for (j=0; j<nj; j++) 00111 for (i=0; i<ni; i++) 00112 if (mask[i+j*ni] == 1) { 00113 sum1 += buf1[i+j*ni+t1*ni*nj]; 00114 sum2 += buf2[i+j*ni+t2*ni*nj]; 00115 pts++; 00116 } 00117 mean1 = sum1 / (double) (pts); 00118 mean2 = sum2 / (double) (pts); 00119 00120 /* Calculate spatial covariance */ 00121 *cov = 0.0; 00122 num = 0; 00123 for (j=0; j<nj; j++) 00124 for (i=0; i<ni; i++) { 00125 if (mask[i+j*ni] == 1) { 00126 /* Sum of the squares (remove mean) */ 00127 (*cov) += ( ( (buf1[i+j*ni+t1*ni*nj] - mean1) * (buf2[i+j*ni+t2*ni*nj] - mean2) ) - (*cov)) / (double) (num + 1); 00128 num++; 00129 } 00130 } 00131 } 00132 }