Include file for climate tools library. More...
#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
#include <time.h>
#include <stdlib.h>
#include <misc.h>
#include <utils.h>
#include <filter.h>
Go to the source code of this file.
Functions | |
void | clim_daily_tserie_climyear (double *bufout, double *bufin, tstruct *buftime, double missing_val, int ni, int nj, int ntime) |
Compute daily climatology for climatological months of a daily time serie. | |
void | remove_seasonal_cycle (double *bufout, double *clim, double *bufin, tstruct *buftime, double missing_val, int filter_width, char *type, int clim_provided, int ni, int nj, int ntime) |
Remove seasonal cycle using a time filter. | |
int | dayofclimyear (int day, int month) |
Compute day of year of 366-day climatological year given a day and a month. |
Include file for climate tools library.
Definition in file clim.h.
void clim_daily_tserie_climyear | ( | double * | bufout, | |
double * | bufin, | |||
tstruct * | buftime, | |||
double | missing_val, | |||
int | ni, | |||
int | nj, | |||
int | nt | |||
) |
Compute daily climatology for climatological months of a daily time serie.
[out] | bufout | Output 3D matrix of daily climatology for a year. |
[in] | bufin | Input 3D matrix. |
[in] | buftime | Time vector for input vector data. |
[in] | missing_val | Missing value. |
[in] | ni | Horizontal dimension of buffer input vector. |
[in] | nj | Horizontal dimension of buffer input vector. |
[in] | nt | Temporal dimension of buffer input vector. |
Definition at line 58 of file clim_daily_tserie_climyear.c.
References alloc_error().
Referenced by remove_seasonal_cycle().
00058 { 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 }
int dayofclimyear | ( | int | day, | |
int | month | |||
) |
Compute day of year of 366-day climatological year given a day and a month.
[in] | day | Day of the month. |
[in] | month | Month of the year. |
Definition at line 59 of file dayofclimyear.c.
Referenced by find_the_days(), and remove_seasonal_cycle().
void remove_seasonal_cycle | ( | double * | bufout, | |
double * | clim, | |||
double * | bufin, | |||
tstruct * | buftime, | |||
double | missing_val, | |||
int | filter_width, | |||
char * | type, | |||
int | clim_provided, | |||
int | ni, | |||
int | nj, | |||
int | ntime | |||
) |
Remove seasonal cycle using a time filter.
[out] | bufout | Output data 3D matrix with seasonal cycle removed. |
[in,out] | clim | Climatology vector (on 366 days). Can be already provided as input or not (clim_provided parameter). |
[in] | bufin | Input 3D matrix. |
[in] | buftime | Time vector for input vector data. |
[in] | type | Type of filter. Possible values: hanning. |
[in] | missing_val | Missing value. |
[in] | filter_width | Width of filter. |
[in] | clim_provided | Set to 1 if clim is already calculated and provided as input. |
[in] | ni | Horizontal dimension of buffer input vector. |
[in] | nj | Horizontal dimension of buffer input vector. |
[in] | ntime | Dimension of buffer input vector. |
Output filtered climatology value
Definition at line 58 of file remove_seasonal_cycle.c.
References alloc_error(), clim_daily_tserie_climyear(), dayofclimyear(), filter(), tstruct::month, and TRUE.
Referenced by main(), and remove_clim().
00059 { 00074 double *tmpbuf = NULL; /* Temporary vector. */ 00075 int i; /* Loop counter for ni. */ 00076 int j; /* Loop counter for nj. */ 00077 int t; /* Loop counter. */ 00078 int ndays_m = 31; /* Maximum number of days in a month. */ 00079 int month; /* Climatological month. */ 00080 int day; /* Climatological day. */ 00081 int dayofclimy; /* Day of year in a 366-day climatological year */ 00082 00083 /* Allocate temporay buffer memory. */ 00084 tmpbuf = (double *) calloc(ni*nj*ntime, sizeof(double)); 00085 if (tmpbuf == NULL) alloc_error(__FILE__, __LINE__); 00086 00087 (void) fprintf(stdout, "%s: Removing seasonal cycle for a time serie.\n", __FILE__); 00088 00089 if (clim_provided != TRUE) { 00090 /* Climatology field was not provided */ 00091 00092 /* Compute daily climatologies for climatological year */ 00093 (void) clim_daily_tserie_climyear(bufout, bufin, buftime, missing_val, ni, nj, ntime); 00094 (void) fprintf(stdout, "%s: Using a %s filter for climatology (wrap edges).\n", __FILE__, type); 00095 /* Filter climatologies using a filter (wrap edges) */ 00096 (void) filter(tmpbuf, bufout, type, filter_width, ni, nj, ntime); 00097 00098 /* Remove climatology from time serie */ 00099 for (t=0; t<ntime; t++) 00100 for (j=0; j<nj; j++) 00101 for (i=0; i<ni; i++) 00102 bufout[i+j*ni+t*ni*nj] = bufin[i+j*ni+t*ni*nj] - tmpbuf[i+j*ni+t*ni*nj]; 00103 00105 for (month=0; month<12; month++) 00106 for (day=0; day<ndays_m; day++) 00107 /* Loop over all the times */ 00108 for (t=0; t<ntime; t++) { 00109 if (buftime[t].day == (day+1) && buftime[t].month == (month+1)) { 00110 dayofclimy = dayofclimyear(day+1, month+1); 00111 for (j=0; j<nj; j++) 00112 for (i=0; i<ni; i++) 00113 clim[i+j*ni+(dayofclimy-1)*ni*nj] = tmpbuf[i+j*ni+t*ni*nj]; 00114 /* Exit loop: matched month and day */ 00115 t = ntime; 00116 } 00117 } 00118 } 00119 else { 00120 /* Climatology field was provided */ 00121 /* Loop over all the times */ 00122 for (t=0; t<ntime; t++) { 00123 dayofclimy = dayofclimyear(buftime[t].day, buftime[t].month); 00124 for (j=0; j<nj; j++) 00125 for (i=0; i<ni; i++) 00126 bufout[i+j*ni+t*ni*nj] = bufin[i+j*ni+t*ni*nj] - clim[i+j*ni+(dayofclimy-1)*ni*nj]; 00127 } 00128 } 00129 00130 /* Free memory */ 00131 (void) free(tmpbuf); 00132 }