sub_period_common.c File Reference

Select a sub period of a vector using a common period over two different time vectors. More...

#include <utils.h>
Include dependency graph for sub_period_common.c:

Go to the source code of this file.

Functions

int sub_period_common (double **buf_sub, int *ntime_sub, double *bufin, int *year, int *month, int *day, int *year_learn, int *month_learn, int *day_learn, int timedim, int ndima, int ndimb, int ntime, int ntime_learn)
 Select a sub period of a vector using a common period over two different time vectors.

Detailed Description

Select a sub period of a vector using a common period over two different time vectors.

Definition in file sub_period_common.c.


Function Documentation

int sub_period_common ( double **  buf_sub,
int *  ntime_sub,
double *  bufin,
int *  year,
int *  month,
int *  day,
int *  year_learn,
int *  month_learn,
int *  day_learn,
int  timedim,
int  ndima,
int  ndimb,
int  ntime,
int  ntime_learn 
)

Select a sub period of a vector using a common period over two different time vectors.

Parameters:
[out] buf_sub Output 3D buffer spanning common time period
[out] ntime_sub Number of times for the common time period (time dimension length)
[in] bufin Input 3D buffer (ndima * ndimb * ntime)
[in] year Year vector for the first time vector
[in] month Month vector for the first time vector
[in] day Day vector for the first time vector
[in] year_learn Year vector for the second time vector
[in] month_learn Month vector for the second time vector
[in] day_learn Day vector for the second time vector
[in] timedim Position of the time period dimension (1 or 3)
[in] ndima First dimension
[in] ndimb Second dimension
[in] ntime Time dimension of the first time vector
[in] ntime_learn Time dimension of the second time vector

Definition at line 59 of file sub_period_common.c.

References alloc_error().

Referenced by main(), wt_downscaling(), and wt_learning().

00060                                                                                                                                     {
00078   int *buf_sub_i = NULL; /* Time indexes for common period */
00079 
00080   int dima; /* First dimension */
00081   int dimb; /* Second dimension */
00082   int t; /* Time loop counter */
00083   int tt; /* Time loop counter for second time vector */
00084 
00085   /* Initialize number of common times */
00086   *ntime_sub = 0;
00087 
00088   /* Loop over first time vector and find common day/month/year and store time indexes for these common times */
00089   for (t=0; t<ntime; t++) {
00090     /* Search in all second time vector times for matching date */
00091     for (tt=0; tt<ntime_learn; tt++) {
00092       if (year[t]  == year_learn[tt] &&
00093           month[t] == month_learn[tt] &&
00094           day[t]   == day_learn[tt]) {
00095         /* Found common date, store time index */
00096         buf_sub_i = (int *) realloc(buf_sub_i, ((*ntime_sub)+1) * sizeof(int));
00097         if (buf_sub_i == NULL) alloc_error(__FILE__, __LINE__);
00098         buf_sub_i[(*ntime_sub)++] = t;
00099       }
00100     }
00101   }
00102 
00103   if ( (*ntime_sub) == 0 ) {
00104     (void) fprintf(stderr, "%s: FATAL ERROR: No common subperiod! Maybe a problem in the time representation in the control run file.\nAborting.\n", __FILE__);
00105     (void) printf("MODEL TIMES ntime=%d\n", ntime);
00106     //#if DEBUG > 7
00107     for (t=0; t<ntime; t++)
00108       (void) printf("%d %d %d\n", year[t], month[t], day[t]);
00109     (void) printf("LEARNING TIMES ntime=%d\n", ntime_learn);
00110     for (t=0; t<ntime_learn; t++)
00111       (void) printf("%d %d %d\n", year_learn[t], month_learn[t], day_learn[t]);
00112     //#endif
00113     return -1;
00114   }
00115   
00116   (void) printf("%s: Sub-period: %d %d %d %d %d %d. Indexes: %d %d\n",__FILE__, year[buf_sub_i[0]], month[buf_sub_i[0]],
00117                 day[buf_sub_i[0]], year[buf_sub_i[(*ntime_sub)-1]],month[buf_sub_i[(*ntime_sub)-1]],
00118                 day[buf_sub_i[(*ntime_sub)-1]], buf_sub_i[0], buf_sub_i[(*ntime_sub)-1]);
00119 
00120   /* Allocate memory for output buffer */
00121   (*buf_sub) = (double *) malloc((*ntime_sub)*ndima*ndimb * sizeof(double));
00122   if ((*buf_sub) == NULL) alloc_error(__FILE__, __LINE__);
00123   /* Construct new 3D matrix with common times */
00124   if (timedim == 3)
00125     /* Time dimension is last */
00126     for (t=0; t<(*ntime_sub); t++)
00127       for (dimb=0; dimb<ndimb; dimb++)
00128         for (dima=0; dima<ndima; dima++)
00129           (*buf_sub)[dima+(dimb*ndima)+t*ndima*ndimb] = bufin[dima+dimb*ndima+buf_sub_i[t]*ndima*ndimb];
00130   else if (timedim == 1)
00131     /* Time dimension is first */
00132     for (t=0; t<(*ntime_sub); t++)
00133       for (dimb=0; dimb<ndimb; dimb++)
00134         for (dima=0; dima<ndima; dima++)
00135           (*buf_sub)[t+dima*(*ntime_sub)+dimb*(ndima*(*ntime_sub))] = bufin[buf_sub_i[t]+dima*ntime+dimb*(ndima*ntime)];
00136   else
00137     (void) fprintf(stderr, "%s: Fatal error: timedim argument must be equal to 1 or 3.\n", __FILE__);
00138 
00139   /* Free memory */
00140   (void) free(buf_sub_i);
00141 
00142   /* Success */
00143   return 0;
00144 }


Generated on 12 May 2016 for DSCLIM by  doxygen 1.6.1