Compute mean and variance of a field averaged spatially. More...
#include <utils.h>
Go to the source code of this file.
Functions | |
void | mean_variance_field_spatial (double *buf_mean, double *buf_var, double *buf, short int *mask, int ni, int nj, int ntime) |
Compute mean and variance of a field averaged spatially. |
Compute mean and variance of a field averaged spatially.
Definition in file mean_variance_field_spatial.c.
void mean_variance_field_spatial | ( | double * | buf_mean, | |
double * | buf_var, | |||
double * | buf, | |||
short int * | mask, | |||
int | ni, | |||
int | nj, | |||
int | ntime | |||
) |
Compute mean and variance of a field averaged spatially.
[out] | buf_mean | Mean of spatially averaged field |
[out] | buf_var | Variance of spatially averaged field |
[in] | buf | Input 3D buffer |
[in] | mask | Input 2D mask |
[in] | ni | First dimension |
[in] | nj | Second dimension |
[in] | ntime | Time dimension |
Definition at line 59 of file mean_variance_field_spatial.c.
References alloc_error().
Referenced by main(), and wt_downscaling().
00059 { 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 }