Compute the spatial covariance of two fields. More...
#include <utils.h>
Go to the source code of this file.
Functions | |
void | covariance_fields_spatial (double *cov, double *buf1, double *buf2, short int *mask, int t1, int t2, int ni, int nj) |
Compute the spatial covariance of two fields. |
Compute the spatial covariance of two fields.
Definition in file covariance_fields_spatial.c.
void covariance_fields_spatial | ( | double * | cov, | |
double * | buf1, | |||
double * | buf2, | |||
short int * | mask, | |||
int | t1, | |||
int | t2, | |||
int | ni, | |||
int | nj | |||
) |
Compute the spatial covariance of two fields.
[out] | cov | Spatial covariance |
[in] | buf1 | Input 3D buffer 1 |
[in] | buf2 | Input 3D buffer 2 |
[in] | mask | Input 2D mask |
[in] | t1 | Time index of buf1 to process |
[in] | t2 | Time index of buf2 to process |
[in] | ni | First dimension |
[in] | nj | Second dimension |
Definition at line 58 of file covariance_fields_spatial.c.
Referenced by find_the_days().
00058 { 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 }