Extract a sub period of a vector of selected months. More...
#include <utils.h>
Go to the source code of this file.
Functions | |
void | extract_subperiod_months (double **buf_sub, int *ntime_sub, double *bufin, int *year, int *month, int *day, int *smonths, int timedim, int ndima, int ndimb, int ntime, int nmonths) |
Extract a sub period of a vector of selected months. |
Extract a sub period of a vector of selected months.
Definition in file extract_subperiod_months.c.
void extract_subperiod_months | ( | double ** | buf_sub, | |
int * | ntime_sub, | |||
double * | bufin, | |||
int * | year, | |||
int * | month, | |||
int * | day, | |||
int * | smonths, | |||
int | timedim, | |||
int | ndima, | |||
int | ndimb, | |||
int | ntime, | |||
int | nmonths | |||
) |
Extract a sub period of a vector of selected months.
[out] | buf_sub | 3D buffer spanning only time subperiod |
[out] | ntime_sub | Number of times in subperiod |
[in] | bufin | 3D input buffer |
[in] | year | Year vector |
[in] | month | Month vector |
[in] | day | Day vector |
[in] | smonths | Selected months vector (values 1-12) |
[in] | timedim | Time dimension position (1 or 3) |
[in] | ndima | First dimension length |
[in] | ndimb | Second dimension length |
[in] | ntime | Time dimension length |
[in] | nmonths | Number of months in smonths vector |
Definition at line 58 of file extract_subperiod_months.c.
References alloc_error().
Referenced by wt_downscaling(), and wt_learning().
00059 { 00075 int *buf_sub_i = NULL; /* Temporary buffer */ 00076 00077 int i; /* Loop counter */ 00078 int j; /* Loop counter */ 00079 int t; /* Time loop counter */ 00080 int tt; /* Time subperiod loop counter */ 00081 00082 /* Initializing */ 00083 *ntime_sub = 0; 00084 00085 /* Retrieve time index spanning selected months */ 00086 for (t=0; t<ntime; t++) 00087 for (tt=0; tt<nmonths; tt++) 00088 if (month[t] == smonths[tt]) { 00089 buf_sub_i = (int *) realloc(buf_sub_i, ((*ntime_sub)+1) * sizeof(int)); 00090 if (buf_sub_i == NULL) alloc_error(__FILE__, __LINE__); 00091 buf_sub_i[(*ntime_sub)++] = t; 00092 } 00093 00094 /* Allocate memory */ 00095 (*buf_sub) = (double *) malloc((*ntime_sub)*ndima*ndimb * sizeof(double)); 00096 if ((*buf_sub) == NULL) alloc_error(__FILE__, __LINE__); 00097 00098 /* Construct new 3D buffer */ 00099 if (timedim == 3) 00100 /* Time dimension is the last one */ 00101 for (t=0; t<(*ntime_sub); t++) 00102 for (j=0; j<ndimb; j++) 00103 for (i=0; i<ndima; i++) 00104 (*buf_sub)[i+j*ndima+t*ndima*ndimb] = bufin[i+j*ndima+buf_sub_i[t]*ndima*ndimb]; 00105 else 00106 /* Time dimension is the first one */ 00107 for (t=0; t<(*ntime_sub); t++) 00108 for (j=0; j<ndimb; j++) 00109 for (i=0; i<ndima; i++) 00110 (*buf_sub)[t+i*(*ntime_sub)+j*(*ntime_sub)*ndima] = bufin[buf_sub_i[t]+i*ntime+j*ntime*ndima]; 00111 00112 /* Free memory */ 00113 (void) free(buf_sub_i); 00114 }