00001
00002
00003
00004
00005
00006
00007
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #include <dsclim.h>
00056
00058 int
00059 find_the_days(analog_day_struct analog_days, double *precip_index, double *precip_index_learn,
00060 double *sup_field_index, double *sup_field_index_learn, double *sup_field, double *sup_field_learn, short int *mask,
00061 int *class_clusters, int *class_clusters_learn, int *year, int *month, int *day,
00062 int *year_learn, int *month_learn, int *day_learn, char *time_units,
00063 int ntime, int ntime_learn, int *months, int nmonths, int ndays, int ndayschoices, int npts, int shuffle, int sup,
00064 int sup_choice, int sup_cov, int use_downscaled_year, int only_wt, int nlon, int nlat, int sup_nlon, int sup_nlat) {
00102 int *buf_sub_i = NULL;
00103 int *buf_learn_sub_i = NULL;
00104 int ntime_sub;
00105 int ntime_learn_sub;
00106
00107 unsigned long int *random_num = NULL;
00108
00109 const gsl_rng_type *T = NULL;
00110 gsl_rng *rng = NULL;
00111
00112 int cur_dayofy;
00113 int learn_dayofy;
00114 int diff_day_learn;
00115
00116 double max_metric = 0.0;
00117
00118 double max_metric_sup = 0.0;
00119 double min_metric = 0.0;
00120
00121 double precip_diff;
00122 double diff_precip_pt;
00123 double sup_diff;
00124
00125 double varstd;
00126 double varmean;
00127 double varstd_sup;
00128 double varmean_sup;
00129
00130 double *metric = NULL;
00131 double *metric_norm = NULL;
00132 double *metric_sup = NULL;
00133 double *metric_sup_norm = NULL;
00134 size_t *metric_index = NULL;
00135 size_t *random_index = NULL;
00136 int min_metric_index;
00137 int max_metric_index;
00138 int *clust_diff = NULL;
00139
00140 int ntime_days;
00141 int *ntime_days_learn = NULL;
00142
00143 int ii;
00144 int t;
00145 int tt;
00146 int tl;
00147 int pts;
00148 int ndays_year;
00149
00150 ut_system *unitSystem = NULL;
00151 ut_unit *dataunits = NULL;
00152 int istat;
00153 double timei;
00154
00155
00156 ut_set_error_message_handler(ut_ignore);
00157 unitSystem = ut_read_xml(NULL);
00158 ut_set_error_message_handler(ut_write_to_stderr);
00159 dataunits = ut_parse(unitSystem, time_units, UT_ASCII);
00160
00161
00162 if (shuffle == TRUE) {
00163 T = gsl_rng_default;
00164 rng = gsl_rng_alloc(T);
00165 (void) gsl_rng_set(rng, time(NULL));
00166
00167 random_num = (unsigned long int *) malloc(ndayschoices * sizeof(unsigned long int));
00168 if (random_num == NULL) alloc_error(__FILE__, __LINE__);
00169 random_index = (size_t *) malloc(ndayschoices * sizeof(size_t));
00170 if (random_index == NULL) alloc_error(__FILE__, __LINE__);
00171 }
00172
00173
00174 metric_index = (size_t *) malloc(ndayschoices * sizeof(size_t));
00175 if (metric_index == NULL) alloc_error(__FILE__, __LINE__);
00176
00177
00178 ntime_sub = 0;
00179 for (t=0; t<ntime; t++)
00180 for (tt=0; tt<nmonths; tt++)
00181 if (month[t] == months[tt]) {
00182 buf_sub_i = (int *) realloc(buf_sub_i, (ntime_sub+1) * sizeof(int));
00183 if (buf_sub_i == NULL) alloc_error(__FILE__, __LINE__);
00184 buf_sub_i[ntime_sub++] = t;
00185 }
00186
00187 ntime_learn_sub = 0;
00188 for (t=0; t<ntime_learn; t++)
00189 for (tt=0; tt<nmonths; tt++)
00190 if (month_learn[t] == months[tt]) {
00191 buf_learn_sub_i = (int *) realloc(buf_learn_sub_i, (ntime_learn_sub+1) * sizeof(int));
00192 if (buf_learn_sub_i == NULL) alloc_error(__FILE__, __LINE__);
00193 buf_learn_sub_i[ntime_learn_sub++] = t;
00194 }
00195
00196
00197 for (t=0; t<ntime_sub; t++) {
00198
00199 #if DEBUG > 7
00200 printf("%d %d %d %d\n",t,year[buf_sub_i[t]],month[buf_sub_i[t]],day[buf_sub_i[t]]);
00201 #endif
00202
00203
00204 cur_dayofy = dayofclimyear(day[buf_sub_i[t]], month[buf_sub_i[t]]);
00205 if (is_leap_year(year[buf_sub_i[t]]))
00206 ndays_year = 366;
00207 else
00208 ndays_year = 365;
00209
00210
00211 ntime_days = 0;
00212 max_metric = -9999999.9;
00213 max_metric_sup = -9999999.9;
00214
00215
00216 for (tl=0; tl<ntime_learn_sub; tl++) {
00217
00218
00219 if (use_downscaled_year != 0 || (use_downscaled_year == 0 && year_learn[buf_learn_sub_i[tl]] != year[buf_sub_i[t]])) {
00220
00221
00222 learn_dayofy = dayofclimyear(day_learn[buf_learn_sub_i[tl]], month_learn[buf_learn_sub_i[tl]]);
00223 diff_day_learn = (int) fminl( (float) abs(cur_dayofy - learn_dayofy), (float) abs(cur_dayofy - learn_dayofy + ndays_year) );
00224
00225
00226 if (diff_day_learn <= ndays) {
00227
00228
00229 metric = (double *) realloc(metric, (ntime_days+1) * sizeof(double));
00230 if (metric == NULL) alloc_error(__FILE__, __LINE__);
00231 metric_norm = (double *) realloc(metric_norm, (ntime_days+1) * sizeof(double));
00232 if (metric_norm == NULL) alloc_error(__FILE__, __LINE__);
00233
00234
00235 precip_diff = 0.0;
00236 for (pts=0; pts<npts; pts++) {
00237 diff_precip_pt = precip_index[pts+t*npts] - precip_index_learn[pts+tl*npts];
00238 precip_diff += (diff_precip_pt*diff_precip_pt);
00239
00240
00241
00242
00243
00244
00245
00246
00247 }
00248 metric[ntime_days] = sqrt(precip_diff);
00249
00250
00251
00252
00253 if (metric[ntime_days] > max_metric) {
00254
00255
00256 max_metric = metric[ntime_days];
00257 max_metric_index = ntime_days;
00258 }
00259
00260
00261 if (sup_choice == TRUE || sup == TRUE) {
00262
00263 metric_sup = (double *) realloc(metric_sup, (ntime_days+1) * sizeof(double));
00264 if (metric_sup == NULL) alloc_error(__FILE__, __LINE__);
00265 metric_sup_norm = (double *) realloc(metric_sup_norm, (ntime_days+1) * sizeof(double));
00266 if (metric_sup_norm == NULL) alloc_error(__FILE__, __LINE__);
00267
00268 if (sup_cov != TRUE) {
00269
00270 sup_diff = sup_field_index[t] - sup_field_index_learn[buf_learn_sub_i[tl]];
00271 metric_sup[ntime_days] = sqrt(sup_diff * sup_diff);
00272 }
00273 else {
00274
00275 if (nlon != sup_nlon || nlat != sup_nlat) {
00276 (void) fprintf(stderr, "%s: Dimensions of downscaled large-scale secondary field (nlat=%d nlon=%d) are not the same as the learning field (nlat=%d nlon=%d. Cannot proceed...\n", __FILE__, nlat, nlon, sup_nlat, sup_nlon);
00277 return -1;
00278 }
00279 (void) covariance_fields_spatial(&sup_diff, sup_field, sup_field_learn, mask, t, tl, sup_nlon, sup_nlat);
00280 metric_sup[ntime_days] = sqrt(sup_diff * sup_diff);
00281 }
00282
00283 if (metric_sup[ntime_days] > max_metric_sup)
00284 max_metric_sup = metric_sup[ntime_days];
00285
00286
00287
00288
00289
00290
00291 }
00292
00293
00294 clust_diff = (int *) realloc(clust_diff, (ntime_days+1) * sizeof(int));
00295 if (clust_diff == NULL) alloc_error(__FILE__, __LINE__);
00296
00297 clust_diff[ntime_days] = class_clusters_learn[tl] - class_clusters[t];
00298
00299
00300 ntime_days_learn = (int *) realloc(ntime_days_learn, (ntime_days+1) * sizeof(int));
00301 if (ntime_days_learn == NULL) alloc_error(__FILE__, __LINE__);
00302
00303 ntime_days_learn[ntime_days] = buf_learn_sub_i[tl];
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315 ntime_days++;
00316 }
00317 }
00318 }
00319
00320
00321 if (ntime_days > 0) {
00322
00323 if (only_wt != 0)
00324
00325
00326 for (tl=0; tl<ntime_days; tl++) {
00327 if (clust_diff[tl] != 0) {
00328 metric[tl] = max_metric;
00329 if (sup_choice == TRUE || sup == TRUE)
00330 metric_sup[tl] = max_metric_sup;
00331 }
00332 }
00333
00334
00335
00336
00337
00338
00339
00340
00341
00343
00344 varmean = gsl_stats_mean(metric, 1, (size_t) ntime_days);
00345 varstd = gsl_stats_sd_m(metric, 1, (size_t) ntime_days, varmean);
00346 if (sup_choice == TRUE) {
00347
00348 varmean_sup = gsl_stats_mean(metric_sup, 1, (size_t) ntime_days);
00349 varstd_sup = gsl_stats_sd_m(metric_sup, 1, (size_t) ntime_days, varmean_sup);
00350
00351
00352 if (sup_choice == TRUE) {
00353
00354 for (tl=0; tl<ntime_days; tl++) {
00355
00356
00357
00358
00359
00360
00361
00362
00363 metric_sup_norm[tl] = (metric_sup[tl] - varmean_sup) / varstd_sup;
00364 metric_norm[tl] = ((metric[tl] - varmean) / varstd) + metric_sup_norm[tl];
00365
00366
00367
00368
00369
00370
00371
00372
00373 }
00374 }
00375 else
00376 for (tl=0; tl<ntime_days; tl++)
00377 metric_norm[tl] = (metric[tl] - varmean) / varstd;
00378 }
00379 else
00380 for (tl=0; tl<ntime_days; tl++)
00381 metric_norm[tl] = (metric[tl] - varmean) / varstd;
00382
00383
00384
00385 (void) gsl_sort_smallest_index(metric_index, (size_t) ndayschoices, metric_norm, 1, (size_t) ntime_days);
00386
00387 if (shuffle == TRUE) {
00388
00389 for (ii=0; ii<ndayschoices; ii++)
00390 random_num[ii] = gsl_rng_uniform_int(rng, 100);
00391 (void) gsl_sort_ulong_index(random_index, random_num, 1, (size_t) ndayschoices);
00392
00393 min_metric = metric_norm[metric_index[random_index[0]]];
00394 min_metric_index = metric_index[random_index[0]];
00395
00396
00397 analog_days.tindex[t] = ntime_days_learn[metric_index[random_index[0]]];
00398 analog_days.year[t] = year_learn[analog_days.tindex[t]];
00399 analog_days.month[t] = month_learn[analog_days.tindex[t]];
00400 analog_days.day[t] = day_learn[analog_days.tindex[t]];
00401 analog_days.tindex_all[t] = buf_learn_sub_i[analog_days.tindex[t]];
00402 istat = utInvCalendar2(analog_days.year[t], analog_days.month[t], analog_days.day[t], 0, 0, 0.0, dataunits, &timei);
00403 analog_days.time[t] = (int) timei;
00404
00405
00406 analog_days.year_s[t] = year[buf_sub_i[t]];
00407 analog_days.month_s[t] = month[buf_sub_i[t]];
00408 analog_days.day_s[t] = day[buf_sub_i[t]];
00409 analog_days.tindex_s_all[t] = buf_sub_i[t];
00410
00411
00412 analog_days.analog_dayschoice[t] = (tstruct *) malloc(ndayschoices * sizeof(tstruct));
00413 if (analog_days.analog_dayschoice[t] == NULL) alloc_error(__FILE__, __LINE__);
00414 analog_days.metric_norm[t] = (float *) malloc(ndayschoices * sizeof(float));
00415 if (analog_days.metric_norm[t] == NULL) alloc_error(__FILE__, __LINE__);
00416 analog_days.tindex_dayschoice[t] = (int *) malloc(ndayschoices * sizeof(int));
00417 if (analog_days.tindex_dayschoice[t] == NULL) alloc_error(__FILE__, __LINE__);
00418 for (ii=0; ii<ndayschoices; ii++) {
00419 analog_days.metric_norm[t][ii] = metric_norm[metric_index[ii]];
00420 analog_days.tindex_dayschoice[t][ii] = ntime_days_learn[metric_index[ii]];
00421 analog_days.analog_dayschoice[t][ii].year = year_learn[ntime_days_learn[metric_index[ii]]];
00422 analog_days.analog_dayschoice[t][ii].month = month_learn[ntime_days_learn[metric_index[ii]]];
00423 analog_days.analog_dayschoice[t][ii].day = day_learn[ntime_days_learn[metric_index[ii]]];
00424 analog_days.analog_dayschoice[t][ii].hour = 0;
00425 analog_days.analog_dayschoice[t][ii].min = 0;
00426 analog_days.analog_dayschoice[t][ii].sec = 0;
00427 }
00428 }
00429 else {
00430
00431
00432 min_metric = 99999999.9;
00433 min_metric_index = -1;
00434 if (sup == TRUE) {
00435
00436 for (ii=0; ii<ndayschoices; ii++) {
00437 if (metric_sup[metric_index[ii]] < min_metric) {
00438 min_metric_index = metric_index[ii];
00439 min_metric = metric_sup[metric_index[ii]];
00440 }
00441
00442
00443
00444
00445
00446
00447
00448
00449 }
00450 }
00451 else {
00452
00453 for (ii=0; ii<ndayschoices; ii++) {
00454
00455
00456
00457
00458 if (metric_norm[metric_index[ii]] < min_metric) {
00459 min_metric_index = metric_index[ii];
00460 min_metric = metric_norm[metric_index[ii]];
00461 }
00462 }
00463 }
00464
00465
00466 analog_days.tindex[t] = ntime_days_learn[min_metric_index];
00467 analog_days.year[t] = year_learn[analog_days.tindex[t]];
00468 analog_days.month[t] = month_learn[analog_days.tindex[t]];
00469 analog_days.day[t] = day_learn[analog_days.tindex[t]];
00470 analog_days.tindex_all[t] = buf_learn_sub_i[analog_days.tindex[t]];
00471 istat = utInvCalendar2(analog_days.year[t], analog_days.month[t], analog_days.day[t], 0, 0, 0.0, dataunits, &timei);
00472 analog_days.time[t] = (int) timei;
00473
00474
00475 analog_days.year_s[t] = year[buf_sub_i[t]];
00476 analog_days.month_s[t] = month[buf_sub_i[t]];
00477 analog_days.day_s[t] = day[buf_sub_i[t]];
00478 analog_days.tindex_s_all[t] = buf_sub_i[t];
00479
00480
00481 analog_days.analog_dayschoice[t] = (tstruct *) malloc(ndayschoices * sizeof(tstruct));
00482 if (analog_days.analog_dayschoice[t] == NULL) alloc_error(__FILE__, __LINE__);
00483 analog_days.metric_norm[t] = (float *) malloc(ndayschoices * sizeof(float));
00484 if (analog_days.metric_norm[t] == NULL) alloc_error(__FILE__, __LINE__);
00485 analog_days.tindex_dayschoice[t] = (int *) malloc(ndayschoices * sizeof(int));
00486 if (analog_days.tindex_dayschoice[t] == NULL) alloc_error(__FILE__, __LINE__);
00487 for (ii=0; ii<ndayschoices; ii++) {
00488 analog_days.metric_norm[t][ii] = metric_norm[metric_index[ii]];
00489 analog_days.tindex_dayschoice[t][ii] = ntime_days_learn[metric_index[ii]];
00490 analog_days.analog_dayschoice[t][ii].year = year_learn[ntime_days_learn[metric_index[ii]]];
00491 analog_days.analog_dayschoice[t][ii].month = month_learn[ntime_days_learn[metric_index[ii]]];
00492 analog_days.analog_dayschoice[t][ii].day = day_learn[ntime_days_learn[metric_index[ii]]];
00493 analog_days.analog_dayschoice[t][ii].hour = 0;
00494 analog_days.analog_dayschoice[t][ii].min = 0;
00495 analog_days.analog_dayschoice[t][ii].sec = 0;
00496 }
00497 }
00498
00499
00500 (void) free(metric);
00501 (void) free(metric_norm);
00502 metric = NULL;
00503 metric_norm = NULL;
00504 if (sup_choice == TRUE || sup == TRUE) {
00505 (void) free(metric_sup);
00506 metric_sup = NULL;
00507 (void) free(metric_sup_norm);
00508 metric_sup_norm = NULL;
00509 }
00510 (void) free(clust_diff);
00511 clust_diff = NULL;
00512 (void) free(ntime_days_learn);
00513 ntime_days_learn = NULL;
00514 }
00515 if (year[buf_sub_i[t]] == 1999 && month[buf_sub_i[t]] == 5)
00516 if (month[buf_sub_i[t]] == 3 || month[buf_sub_i[t]] == 4 || month[buf_sub_i[t]] == 5)
00517 printf("Time downscaled %d: %d %d %d. Analog day: %d %d %d %lf\n", t, year[buf_sub_i[t]], month[buf_sub_i[t]], day[buf_sub_i[t]], year_learn[analog_days.tindex[t]], month_learn[analog_days.tindex[t]], day_learn[analog_days.tindex[t]], min_metric);
00518
00519 }
00520
00521
00522 if (shuffle == TRUE) {
00523 (void) gsl_rng_free(rng);
00524 (void) free(random_num);
00525 (void) free(random_index);
00526 }
00527 (void) free(metric_index);
00528
00529 (void) free(buf_sub_i);
00530 (void) free(buf_learn_sub_i);
00531
00532 (void) ut_free(dataunits);
00533 (void) ut_free_system(unitSystem);
00534
00535 return 0;
00536 }