00001
00002
00003
00004
00005
00006
00011
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 #ifdef HAVE_CONFIG_H
00055 #include <config.h>
00056 #endif
00057
00059 #define _GNU_SOURCE
00060
00061
00062 #ifdef HAVE_SYS_TYPES_H
00063 #include <sys/types.h>
00064 #endif
00065 #ifdef HAVE_SYS_STAT_H
00066 #include <sys/stat.h>
00067 #endif
00068 #ifdef HAVE_FCNTL_H
00069 #include <fcntl.h>
00070 #endif
00071 #ifdef HAVE_UNSTD_H
00072 #include <unistd.h>
00073 #endif
00074 #ifdef HAVE_STDIO_H
00075 #include <stdio.h>
00076 #endif
00077 #ifdef HAVE_STRING_H
00078 #include <string.h>
00079 #endif
00080 #ifdef HAVE_STDLIB_H
00081 #include <stdlib.h>
00082 #endif
00083 #ifdef HAVE_MATH_H
00084 #include <math.h>
00085 #endif
00086 #ifdef HAVE_TIME_H
00087 #include <time.h>
00088 #endif
00089 #ifdef HAVE_LIBGEN_H
00090 #include <libgen.h>
00091 #endif
00092
00093 #include <zlib.h>
00094 #include <hdf5.h>
00095 #include <netcdf.h>
00096
00097 #include <udunits.h>
00098
00099 #include <utils.h>
00100 #include <filter.h>
00101 #include <clim.h>
00102 #include <classif.h>
00103 #include <pceof.h>
00104
00105 #include <gsl/gsl_statistics.h>
00106
00108 void show_usage(char *pgm);
00109 void handle_netcdf_error(int status, int lineno);
00110
00112 int main(int argc, char **argv)
00113 {
00121
00122
00123
00124
00125 int ntime;
00126 int neof;
00127 int neof_poid;
00128 int neof_learn;
00129 int npts_learn;
00130 int nseason_learn;
00131 int *nclust_learn = NULL;
00132 int *ntime_learn = NULL;
00133 int *nclust = NULL;
00134
00135 size_t dimval;
00136
00137 char *filein = NULL;
00138 char *filein_poid = NULL;
00139 char *filein_learn = NULL;
00140
00141 int istat, ncinid, ncinid_poid, ncinid_learn;
00142 int varinid, timeinid, timediminid;
00143
00144 int eofdiminid;
00145 int eofdiminid_poid;
00146 int eofdiminid_learn, ptsdiminid_learn, seasondiminid_learn;
00147 int *clust_dimid = NULL;
00148 int *time_learn_dimid = NULL;
00149 int *clust_learn_dimid = NULL;
00150 int *time_learn_varid = NULL;
00151 int *cst_varid = NULL;
00152 int *reg_varid = NULL;
00153 int *rrd_varid = NULL;
00154 int *ta_varid = NULL;
00155 int eca_pc_learn_varid;
00156 int *clust_varid = NULL;
00157 int *poid_varid = NULL;
00158 int tancp_mean_varid, tancp_var_varid;
00159 nc_type vartype_main;
00160 nc_type vartype_time;
00161 int varndims;
00162 int vardimids[NC_MAX_VAR_DIMS];
00163
00164 size_t start[3];
00165 size_t count[3];
00166
00167 size_t t_len;
00168 char *time_units = NULL;
00169 char *cal_type = NULL;
00170 ut_system *unitSystem = NULL;
00171 ut_unit *dataunits = NULL;
00172
00173 char attname[1000];
00174 float valf;
00175
00176 double *psl_pc = NULL;
00177 double *buf_sub = NULL;
00178 double *buftmp = NULL;
00179 int ntime_sub;
00180 double *timein = NULL;
00181 int *year = NULL;
00182 int *month = NULL;
00183 int *day = NULL;
00184 int *hour = NULL;
00185 int *min = NULL;
00186 double *sec = NULL;
00187
00188
00189
00190 double **poid = NULL;
00191 double **time_learn = NULL;
00192 int **year_learn = NULL;
00193 int **month_learn = NULL;
00194 int **day_learn = NULL;
00195 int **hour_learn = NULL;
00196 int **min_learn = NULL;
00197 double **sec_learn = NULL;
00198 double **cst_learn = NULL;
00199 double **reg_learn = NULL;
00200 double **rrd_learn = NULL;
00201 double **ta_learn = NULL;
00202 double *eca_pc_learn = NULL;
00203 double *tancp_mean = NULL;
00204 double *tancp_var = NULL;
00205
00206 double **mean_dist = NULL;
00207 double **var_dist = NULL;
00208
00209 double *var_pc_norm_all = NULL;
00210
00211 double fillvalue_poid;
00212 double fillvalue_cst;
00213 double fillvalue_reg;
00214 double fillvalue_rrd;
00215 double fillvalue_ta;
00216
00217 double first_variance = -9999.9999;
00218
00219 char varname[500];
00220
00221 int nseason = 4;
00222
00223 int i;
00224 int t;
00225 int ii;
00226
00227
00228 (void) banner(basename(argv[0]), "1.0", "BEGIN");
00229
00230
00231 for (i=1; i<argc; i++) {
00232 if ( !strcmp(argv[i], "-h") ) {
00233 (void) show_usage(basename(argv[0]));
00234 (void) banner(basename(argv[0]), "OK", "END");
00235 return 0;
00236 }
00237 else if ( !strcmp(argv[i], "-i") ) {
00238 filein = (char *) malloc((strlen(argv[++i])+1) * sizeof(char));
00239 if (filein == NULL) alloc_error(__FILE__, __LINE__);
00240 (void) strcpy(filein, argv[i]);
00241 }
00242 else if ( !strcmp(argv[i], "-i_poid") ) {
00243 filein_poid = (char *) malloc((strlen(argv[++i])+1) * sizeof(char));
00244 if (filein_poid == NULL) alloc_error(__FILE__, __LINE__);
00245 (void) strcpy(filein_poid, argv[i]);
00246 }
00247 else if ( !strcmp(argv[i], "-i_learn") ) {
00248 filein_learn = (char *) malloc((strlen(argv[++i])+1) * sizeof(char));
00249 if (filein_learn == NULL) alloc_error(__FILE__, __LINE__);
00250 (void) strcpy(filein_learn, argv[i]);
00251 }
00252 else {
00253 (void) fprintf(stderr, "%s:: Wrong arg %s.\n\n", basename(argv[0]), argv[i]);
00254 (void) show_usage(basename(argv[0]));
00255 (void) banner(basename(argv[0]), "ABORT", "END");
00256 (void) abort();
00257 }
00258 }
00259
00260
00261 ut_set_error_message_handler(ut_ignore);
00262 unitSystem = ut_read_xml(NULL);
00263 ut_set_error_message_handler(ut_write_to_stderr);
00264
00265
00266 printf("%s: Reading info from input file %s.\n", __FILE__, filein);
00267 istat = nc_open(filein, NC_NOWRITE, &ncinid);
00268 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00269
00270 istat = nc_inq_dimid(ncinid, "time", &timediminid);
00271 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00272 istat = nc_inq_dimlen(ncinid, timediminid, &dimval);
00273 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00274 ntime = (int) dimval;
00275
00276 istat = nc_inq_dimid(ncinid, "eof", &eofdiminid);
00277 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00278 istat = nc_inq_dimlen(ncinid, eofdiminid, &dimval);
00279 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00280 neof = (int) dimval;
00281
00282 istat = nc_inq_varid(ncinid, "time", &timeinid);
00283 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00284
00285 istat = nc_inq_varid(ncinid, "pc_proj", &varinid);
00286 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00287
00289
00290
00291
00292 istat = nc_inq_var(ncinid, timeinid, (char *) NULL, &vartype_time, &varndims, vardimids, (int *) NULL);
00293 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00294
00295 if (varndims != 1) {
00296 (void) fprintf(stderr, "Error NetCDF type and/or dimensions Line %d.\n", __LINE__);
00297 (void) banner(basename(argv[0]), "ABORT", "END");
00298 (void) abort();
00299 }
00300
00301
00302 start[0] = 0;
00303 count[0] = (size_t) ntime;
00304 timein = malloc(ntime * sizeof(double));
00305 if (timein == NULL) alloc_error(__FILE__, __LINE__);
00306
00307
00308 istat = nc_get_vara_double(ncinid, timeinid, start, count, timein);
00309 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00310
00311
00312 for (t=0; t<ntime; t++)
00313 if (timein[t] != 0.0)
00314 break;
00315 if (t == ntime) {
00316 fprintf(stderr, "WARNING: Time variable values all zero!!! Fixing time variable to index value...\n");
00317 for (t=0; t<ntime; t++)
00318 timein[t] = (double) t;
00319 }
00320
00323
00324 istat = nc_inq_var(ncinid, varinid, (char *) NULL, &vartype_main, &varndims, vardimids, (int *) NULL);
00325 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00326
00327 if (varndims != 2) {
00328 (void) fprintf(stderr, "Error NetCDF type and/or dimensions Line %d.\n", __LINE__);
00329 (void) banner(basename(argv[0]), "ABORT", "END");
00330 (void) abort();
00331 }
00332
00333
00334 start[0] = 0;
00335 start[1] = 0;
00336 start[2] = 0;
00337 count[0] = (size_t) ntime;
00338 count[1] = (size_t) neof;
00339 count[2] = 0;
00340
00341 psl_pc = (double *) calloc(ntime*neof, sizeof(double));
00342 if (psl_pc == NULL) alloc_error(__FILE__, __LINE__);
00343
00344
00345 printf("%s: Reading data from input file %s.\n", __FILE__, filein);
00346 istat = nc_get_vara_double(ncinid, varinid, start, count, psl_pc);
00347 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00348
00349
00350
00351 istat = nc_inq_attlen(ncinid, timeinid, "units", &t_len);
00352 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00353
00354 time_units = (char *) malloc(t_len * sizeof(char));
00355 if (time_units == NULL) alloc_error(__FILE__, __LINE__);
00356
00357 istat = nc_get_att_text(ncinid, timeinid, "units", time_units);
00358 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00359 time_units[t_len-2] = '\0';
00360
00361
00362 istat = nc_inq_attlen(ncinid, timeinid, "calendar", &t_len);
00363 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00364
00365 cal_type = (char *) malloc(t_len + 1);
00366 if (cal_type == NULL) alloc_error(__FILE__, __LINE__);
00367
00368 istat = nc_get_att_text(ncinid, timeinid, "calendar", cal_type);
00369 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00370 cal_type[t_len] = '\0';
00371
00372
00373
00374 year = (int *) malloc(ntime * sizeof(int));
00375 if (year == NULL) alloc_error(__FILE__, __LINE__);
00376 month = (int *) malloc(ntime * sizeof(int));
00377 if (month == NULL) alloc_error(__FILE__, __LINE__);
00378 day = (int *) malloc(ntime * sizeof(int));
00379 if (day == NULL) alloc_error(__FILE__, __LINE__);
00380 hour = (int *) malloc(ntime * sizeof(int));
00381 if (hour == NULL) alloc_error(__FILE__, __LINE__);
00382 min = (int *) malloc(ntime * sizeof(int));
00383 if (min == NULL) alloc_error(__FILE__, __LINE__);
00384 sec = (double *) malloc(ntime * sizeof(double));
00385 if (sec == NULL) alloc_error(__FILE__, __LINE__);
00386
00387 dataunits = ut_parse(unitSystem, time_units, UT_ASCII);
00388 for (t=0; t<ntime; t++)
00389 istat = utCalendar2_cal(timein[t], dataunits, &(year[t]), &(month[t]), &(day[t]), &(hour[t]), &(min[t]), &(sec[t]), cal_type);
00390 (void) free(time_units);
00391 (void) ut_free(dataunits);
00392 (void) free(cal_type);
00393
00394
00395 printf("%s: Reading info from input file %s.\n", __FILE__, filein_poid);
00396 istat = nc_open(filein_poid, NC_NOWRITE, &ncinid_poid);
00397 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00398
00399 istat = nc_inq_dimid(ncinid_poid, "eof", &eofdiminid_poid);
00400 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00401 istat = nc_inq_dimlen(ncinid_poid, eofdiminid_poid, &dimval);
00402 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00403 neof_poid = (int) dimval;
00404
00405 clust_dimid = malloc(nseason * sizeof(int));
00406 if (clust_dimid == NULL) alloc_error(__FILE__, __LINE__);
00407 poid_varid = malloc(nseason * sizeof(int));
00408 if (poid_varid == NULL) alloc_error(__FILE__, __LINE__);
00409
00410 clust_varid = malloc(nseason * sizeof(int));
00411 if (clust_varid == NULL) alloc_error(__FILE__, __LINE__);
00412
00413 nclust = malloc(nseason * sizeof(int));
00414 if (nclust == NULL) alloc_error(__FILE__, __LINE__);
00415
00416 poid = (double **) malloc(nseason * sizeof(double *));
00417 if (poid == NULL) alloc_error(__FILE__, __LINE__);
00418
00419 for (i=0; i<nseason; i++) {
00420 if (i == 0)
00421 (void) strcpy(varname, "clust_aut");
00422 else if (i == 1)
00423 (void) strcpy(varname, "clust_hiv");
00424 else if (i == 2)
00425 (void) strcpy(varname, "clust_pri");
00426 else if (i == 3)
00427 (void) strcpy(varname, "clust_ete");
00428 else
00429 exit(1);
00430 istat = nc_inq_dimid(ncinid_poid, varname, &(clust_dimid[i]));
00431 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00432 istat = nc_inq_dimlen(ncinid_poid, clust_dimid[i], &dimval);
00433 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00434 nclust[i] = (int) dimval;
00435
00436 if (i == 0)
00437 (void) strcpy(varname, "poid_aut");
00438 else if (i == 1)
00439 (void) strcpy(varname, "poid_hiv");
00440 else if (i == 2)
00441 (void) strcpy(varname, "poid_pri");
00442 else if (i == 3)
00443 (void) strcpy(varname, "poid_ete");
00444 else
00445 exit(1);
00446 istat = nc_inq_varid(ncinid_poid, varname, &(poid_varid[i]));
00447 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00448
00449
00450 istat = nc_inq_var(ncinid_poid, poid_varid[i], (char *) NULL, &vartype_main, &varndims, vardimids, (int *) NULL);
00451 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00452
00453 if (varndims != 2) {
00454 (void) fprintf(stderr, "Error NetCDF type and/or dimensions Line %d.\n", __LINE__);
00455 (void) banner(basename(argv[0]), "ABORT", "END");
00456 (void) abort();
00457 }
00458
00459
00460 if (i == 0) {
00461 (void) strcpy(attname, "missing_value");
00462 if (vartype_main == NC_FLOAT) {
00463 istat = nc_get_att_float(ncinid_poid, poid_varid[0], attname, &valf);
00464 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00465 fillvalue_poid = (double) valf;
00466 }
00467 else if (vartype_main == NC_DOUBLE) {
00468 istat = nc_get_att_double(ncinid_poid, poid_varid[0], attname, &fillvalue_poid);
00469 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00470 }
00471 }
00472
00473
00474 start[0] = 0;
00475 start[1] = 0;
00476 start[2] = 0;
00477 count[0] = (size_t) nclust[i];
00478 count[1] = (size_t) neof_poid;
00479 count[2] = (size_t) 0;
00480
00481 poid[i] = (double *) calloc(nclust[i] * neof_poid, sizeof(double));
00482 if (poid[i] == NULL) alloc_error(__FILE__, __LINE__);
00483
00484
00485 printf("%s: Reading data from input file %s.\n", __FILE__, filein_poid);
00486 istat = nc_get_vara_double(ncinid_poid, poid_varid[i], start, count, poid[i]);
00487 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00488
00489 }
00490
00491
00493
00494 istat = ncclose(ncinid_poid);
00495 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00496
00497
00498
00499 printf("%s: Reading info from input file %s.\n", __FILE__, filein_learn);
00500 istat = nc_open(filein_learn, NC_NOWRITE, &ncinid_learn);
00501 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00502
00503 istat = nc_inq_dimid(ncinid_learn, "eof", &eofdiminid_learn);
00504 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00505 istat = nc_inq_dimlen(ncinid_learn, eofdiminid_learn, &dimval);
00506 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00507 neof_learn = (int) dimval;
00508
00509 istat = nc_inq_dimid(ncinid_learn, "pts", &ptsdiminid_learn);
00510 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00511 istat = nc_inq_dimlen(ncinid_learn, ptsdiminid_learn, &dimval);
00512 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00513 npts_learn = (int) dimval;
00514
00515 istat = nc_inq_dimid(ncinid_learn, "season", &seasondiminid_learn);
00516 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00517 istat = nc_inq_dimlen(ncinid_learn, seasondiminid_learn, &dimval);
00518 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00519 nseason_learn = (int) dimval;
00520
00522 time_learn_dimid = malloc(nseason_learn * sizeof(int));
00523 if (time_learn_dimid == NULL) alloc_error(__FILE__, __LINE__);
00524 time_learn_varid = malloc(nseason_learn * sizeof(int));
00525 if (time_learn_varid == NULL) alloc_error(__FILE__, __LINE__);
00526 clust_learn_dimid = malloc(nseason_learn * sizeof(int));
00527 if (clust_learn_dimid == NULL) alloc_error(__FILE__, __LINE__);
00528
00529 cst_varid = malloc(nseason_learn * sizeof(int));
00530 if (cst_varid == NULL) alloc_error(__FILE__, __LINE__);
00531 reg_varid = malloc(nseason_learn * sizeof(int));
00532 if (reg_varid == NULL) alloc_error(__FILE__, __LINE__);
00533 rrd_varid = malloc(nseason_learn * sizeof(int));
00534 if (rrd_varid == NULL) alloc_error(__FILE__, __LINE__);
00535 ta_varid = malloc(nseason_learn * sizeof(int));
00536 if (ta_varid == NULL) alloc_error(__FILE__, __LINE__);
00537
00538 nclust_learn = malloc(nseason_learn * sizeof(int));
00539 if (nclust_learn == NULL) alloc_error(__FILE__, __LINE__);
00540 ntime_learn = malloc(nseason_learn * sizeof(int));
00541 if (ntime_learn == NULL) alloc_error(__FILE__, __LINE__);
00542
00544 time_learn = (double **) malloc(nseason_learn * sizeof(double *));
00545 if (time_learn == NULL) alloc_error(__FILE__, __LINE__);
00546 cst_learn = (double **) malloc(nseason_learn * sizeof(double *));
00547 if (cst_learn == NULL) alloc_error(__FILE__, __LINE__);
00548 reg_learn = (double **) malloc(nseason_learn * sizeof(double *));
00549 if (reg_learn == NULL) alloc_error(__FILE__, __LINE__);
00550 rrd_learn = (double **) malloc(nseason_learn * sizeof(double *));
00551 if (rrd_learn == NULL) alloc_error(__FILE__, __LINE__);
00552 ta_learn = (double **) malloc(nseason_learn * sizeof(double *));
00553 if (ta_learn == NULL) alloc_error(__FILE__, __LINE__);
00554
00555 year_learn = (int **) malloc(nseason_learn * sizeof(int *));
00556 if (year_learn == NULL) alloc_error(__FILE__, __LINE__);
00557 month_learn = (int **) malloc(nseason_learn * sizeof(int *));
00558 if (month_learn == NULL) alloc_error(__FILE__, __LINE__);
00559 day_learn = (int **) malloc(nseason_learn * sizeof(int *));
00560 if (day_learn == NULL) alloc_error(__FILE__, __LINE__);
00561 hour_learn = (int **) malloc(nseason_learn * sizeof(int *));
00562 if (hour_learn == NULL) alloc_error(__FILE__, __LINE__);
00563 min_learn = (int **) malloc(nseason_learn * sizeof(int *));
00564 if (min_learn == NULL) alloc_error(__FILE__, __LINE__);
00565 sec_learn = (double **) malloc(nseason_learn * sizeof(double *));
00566 if (sec_learn == NULL) alloc_error(__FILE__, __LINE__);
00567
00568 for (i=0; i<nseason_learn; i++) {
00569
00571 if (i == 0)
00572 (void) strcpy(varname, "time_aut");
00573 else if (i == 1)
00574 (void) strcpy(varname, "time_hiv");
00575 else if (i == 2)
00576 (void) strcpy(varname, "time_pri");
00577 else if (i == 3)
00578 (void) strcpy(varname, "time_ete");
00579 else
00580 exit(1);
00581 istat = nc_inq_dimid(ncinid_learn, varname, &(time_learn_dimid[i]));
00582 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00583 istat = nc_inq_dimlen(ncinid_learn, time_learn_dimid[i], &dimval);
00584 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00585 ntime_learn[i] = (int) dimval;
00586
00587 istat = nc_inq_varid(ncinid_learn, varname, &(time_learn_varid[i]));
00588 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00589
00590
00591 start[0] = 0;
00592 start[1] = 0;
00593 start[2] = 0;
00594 count[0] = (size_t) ntime_learn[i];
00595 count[1] = (size_t) 0;
00596 count[2] = (size_t) 0;
00597
00598 time_learn[i] = (double *) calloc(ntime_learn[i], sizeof(double));
00599 if (time_learn[i] == NULL) alloc_error(__FILE__, __LINE__);
00600
00601
00602 printf("%s: Reading data from input file %s.\n", __FILE__, filein_learn);
00603 istat = nc_get_vara_double(ncinid_learn, time_learn_varid[i], start, count, time_learn[i]);
00604 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00605
00606
00607
00608 istat = nc_inq_var(ncinid_learn, time_learn_varid[i], (char *) NULL, &vartype_main, &varndims, vardimids, (int *) NULL);
00609 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00610
00611 if (varndims != 1) {
00612 (void) fprintf(stderr, "Error NetCDF type and/or dimensions Line %d.\n", __LINE__);
00613 (void) banner(basename(argv[0]), "ABORT", "END");
00614 (void) abort();
00615 }
00616
00617
00618 istat = nc_inq_attlen(ncinid_learn, time_learn_varid[i], "units", &t_len);
00619 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00620
00621 time_units = (char *) malloc(t_len * sizeof(char));
00622 if (time_units == NULL) alloc_error(__FILE__, __LINE__);
00623
00624 istat = nc_get_att_text(ncinid_learn, time_learn_varid[i], "units", time_units);
00625 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00626 time_units[t_len-2] = '\0';
00627
00628
00629 istat = nc_inq_attlen(ncinid_learn, time_learn_varid[i], "calendar", &t_len);
00630 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00631
00632 cal_type = (char *) malloc(t_len + 1);
00633 if (cal_type == NULL) alloc_error(__FILE__, __LINE__);
00634
00635 istat = nc_get_att_text(ncinid_learn, time_learn_varid[i], "calendar", cal_type);
00636 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00637 cal_type[t_len] = '\0';
00638
00639
00640 year_learn[i] = (int *) malloc(ntime_learn[i] * sizeof(int));
00641 if (year_learn[i] == NULL) alloc_error(__FILE__, __LINE__);
00642 month_learn[i] = (int *) malloc(ntime_learn[i] * sizeof(int));
00643 if (month_learn[i] == NULL) alloc_error(__FILE__, __LINE__);
00644 day_learn[i] = (int *) malloc(ntime_learn[i] * sizeof(int));
00645 if (day_learn[i] == NULL) alloc_error(__FILE__, __LINE__);
00646 hour_learn[i] = (int *) malloc(ntime_learn[i] * sizeof(int));
00647 if (hour_learn[i] == NULL) alloc_error(__FILE__, __LINE__);
00648 min_learn[i] = (int *) malloc(ntime_learn[i] * sizeof(int));
00649 if (min_learn[i] == NULL) alloc_error(__FILE__, __LINE__);
00650 sec_learn[i] = (double *) malloc(ntime_learn[i] * sizeof(double));
00651 if (sec_learn[i] == NULL) alloc_error(__FILE__, __LINE__);
00652
00653 dataunits = ut_parse(unitSystem, time_units, UT_ASCII);
00654 for (t=0; t<ntime_learn[i]; t++)
00655 istat = utCalendar2_cal(time_learn[i][t], dataunits,
00656 &(year_learn[i][t]), &(month_learn[i][t]), &(day_learn[i][t]), &(hour_learn[i][t]),
00657 &(min_learn[i][t]), &(sec_learn[i][t]), cal_type);
00658
00659 (void) free(time_units);
00660 (void) ut_free(dataunits);
00661 (void) free(cal_type);
00662
00663
00664 if (i == 0)
00665 (void) strcpy(varname, "clust_aut");
00666 else if (i == 1)
00667 (void) strcpy(varname, "clust_hiv");
00668 else if (i == 2)
00669 (void) strcpy(varname, "clust_pri");
00670 else if (i == 3)
00671 (void) strcpy(varname, "clust_ete");
00672 else
00673 exit(1);
00674 istat = nc_inq_dimid(ncinid_learn, varname, &(clust_learn_dimid[i]));
00675 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00676 istat = nc_inq_dimlen(ncinid_learn, clust_learn_dimid[i], &dimval);
00677 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00678 nclust_learn[i] = (int) dimval;
00679
00680
00682 if (i == 0)
00683 (void) strcpy(varname, "cst_aut");
00684 else if (i == 1)
00685 (void) strcpy(varname, "cst_hiv");
00686 else if (i == 2)
00687 (void) strcpy(varname, "cst_pri");
00688 else if (i == 3)
00689 (void) strcpy(varname, "cst_ete");
00690 else
00691 exit(1);
00692 istat = nc_inq_varid(ncinid_learn, varname, &(cst_varid[i]));
00693 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00694
00695
00696 istat = nc_inq_var(ncinid_learn, cst_varid[i], (char *) NULL, &vartype_main, &varndims, vardimids, (int *) NULL);
00697 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00698
00699 if (varndims != 1) {
00700 (void) fprintf(stderr, "Error NetCDF type and/or dimensions Line %d.\n", __LINE__);
00701 (void) banner(basename(argv[0]), "ABORT", "END");
00702 (void) abort();
00703 }
00704
00705
00706 if (i == 0) {
00707 (void) strcpy(attname, "missing_value");
00708 if (vartype_main == NC_FLOAT) {
00709 istat = nc_get_att_float(ncinid_learn, cst_varid[0], attname, &valf);
00710 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00711 fillvalue_cst = (double) valf;
00712 }
00713 else if (vartype_main == NC_DOUBLE) {
00714 istat = nc_get_att_double(ncinid_learn, cst_varid[0], attname, &fillvalue_cst);
00715 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00716 }
00717 }
00718
00719
00720 start[0] = 0;
00721 start[1] = 0;
00722 start[2] = 0;
00723 count[0] = (size_t) npts_learn;
00724 count[1] = (size_t) 0;
00725 count[2] = (size_t) 0;
00726
00727 cst_learn[i] = (double *) calloc(npts_learn, sizeof(double));
00728 if (cst_learn[i] == NULL) alloc_error(__FILE__, __LINE__);
00729
00730
00731 printf("%s: Reading data from input file %s.\n", __FILE__, filein_learn);
00732 istat = nc_get_vara_double(ncinid_learn, cst_varid[i], start, count, cst_learn[i]);
00733 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00734
00735
00736 if (i == 0)
00737 (void) strcpy(varname, "reg_aut");
00738 else if (i == 1)
00739 (void) strcpy(varname, "reg_hiv");
00740 else if (i == 2)
00741 (void) strcpy(varname, "reg_pri");
00742 else if (i == 3)
00743 (void) strcpy(varname, "reg_ete");
00744 else
00745 exit(1);
00746 istat = nc_inq_varid(ncinid_learn, varname, &(reg_varid[i]));
00747 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00748
00749
00750 istat = nc_inq_var(ncinid_learn, reg_varid[i], (char *) NULL, &vartype_main, &varndims, vardimids, (int *) NULL);
00751 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00752
00753 if (varndims != 2) {
00754 (void) fprintf(stderr, "Error NetCDF type and/or dimensions Line %d.\n", __LINE__);
00755 (void) banner(basename(argv[0]), "ABORT", "END");
00756 (void) abort();
00757 }
00758
00759
00760 if (i == 0) {
00761 (void) strcpy(attname, "missing_value");
00762 if (vartype_main == NC_FLOAT) {
00763 istat = nc_get_att_float(ncinid_learn, reg_varid[0], attname, &valf);
00764 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00765 fillvalue_reg = (double) valf;
00766 }
00767 else if (vartype_main == NC_DOUBLE) {
00768 istat = nc_get_att_double(ncinid_learn, reg_varid[0], attname, &fillvalue_reg);
00769 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00770 }
00771 }
00772
00773
00774 start[0] = 0;
00775 start[1] = 0;
00776 start[2] = 0;
00777 count[0] = (size_t) nclust_learn[i];
00778 count[1] = (size_t) npts_learn;
00779 count[2] = (size_t) 0;
00780
00781 reg_learn[i] = (double *) calloc(nclust_learn[i] * npts_learn, sizeof(double));
00782 if (reg_learn[i] == NULL) alloc_error(__FILE__, __LINE__);
00783
00784
00785 printf("%s: Reading data from input file %s.\n", __FILE__, filein_learn);
00786 istat = nc_get_vara_double(ncinid_learn, reg_varid[i], start, count, reg_learn[i]);
00787 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00788
00789
00790 if (i == 0)
00791 (void) strcpy(varname, "rrd_aut");
00792 else if (i == 1)
00793 (void) strcpy(varname, "rrd_hiv");
00794 else if (i == 2)
00795 (void) strcpy(varname, "rrd_pri");
00796 else if (i == 3)
00797 (void) strcpy(varname, "rrd_ete");
00798 else
00799 exit(1);
00800 istat = nc_inq_varid(ncinid_learn, varname, &(rrd_varid[i]));
00801 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00802
00803
00804 istat = nc_inq_var(ncinid_learn, rrd_varid[i], (char *) NULL, &vartype_main, &varndims, vardimids, (int *) NULL);
00805 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00806 if (varndims != 2) {
00807 (void) fprintf(stderr, "Error NetCDF type and/or dimensions Line %d.\n", __LINE__);
00808 (void) banner(basename(argv[0]), "ABORT", "END");
00809 (void) abort();
00810 }
00811
00812
00813 if (i == 0) {
00814 (void) strcpy(attname, "missing_value");
00815 if (vartype_main == NC_FLOAT) {
00816 istat = nc_get_att_float(ncinid_learn, rrd_varid[0], attname, &valf);
00817 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00818 fillvalue_rrd = (double) valf;
00819 }
00820 else if (vartype_main == NC_DOUBLE) {
00821 istat = nc_get_att_double(ncinid_learn, rrd_varid[0], attname, &fillvalue_rrd);
00822 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00823 }
00824 }
00825
00826
00827 start[0] = 0;
00828 start[1] = 0;
00829 start[2] = 0;
00830 count[0] = (size_t) ntime_learn[i];
00831 count[1] = (size_t) npts_learn;
00832 count[2] = (size_t) 0;
00833
00834 rrd_learn[i] = (double *) calloc(ntime_learn[i] * npts_learn, sizeof(double));
00835 if (rrd_learn[i] == NULL) alloc_error(__FILE__, __LINE__);
00836
00837
00838 printf("%s: Reading data from input file %s.\n", __FILE__, filein_learn);
00839 istat = nc_get_vara_double(ncinid_learn, rrd_varid[i], start, count, rrd_learn[i]);
00840 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00841
00842
00843 if (i == 0)
00844 (void) strcpy(varname, "ta_aut");
00845 else if (i == 1)
00846 (void) strcpy(varname, "ta_hiv");
00847 else if (i == 2)
00848 (void) strcpy(varname, "ta_pri");
00849 else if (i == 3)
00850 (void) strcpy(varname, "ta_ete");
00851 else
00852 exit(1);
00853 istat = nc_inq_varid(ncinid_learn, varname, &(ta_varid[i]));
00854 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00855
00856
00857 istat = nc_inq_var(ncinid_learn, ta_varid[i], (char *) NULL, &vartype_main, &varndims, vardimids, (int *) NULL);
00858 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00859
00860 if (varndims != 1) {
00861 (void) fprintf(stderr, "Error NetCDF type and/or dimensions Line %d.\n", __LINE__);
00862 (void) banner(basename(argv[0]), "ABORT", "END");
00863 (void) abort();
00864 }
00865
00866
00867 if (i == 0) {
00868 (void) strcpy(attname, "missing_value");
00869 if (vartype_main == NC_FLOAT) {
00870 istat = nc_get_att_float(ncinid_learn, ta_varid[0], attname, &valf);
00871 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00872 fillvalue_ta = (double) valf;
00873 }
00874 else if (vartype_main == NC_DOUBLE) {
00875 istat = nc_get_att_double(ncinid_learn, ta_varid[0], attname, &fillvalue_ta);
00876 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00877 }
00878 }
00879
00880
00881 start[0] = 0;
00882 start[1] = 0;
00883 start[2] = 0;
00884 count[0] = (size_t) ntime_learn[i];
00885 count[1] = (size_t) 0;
00886 count[2] = (size_t) 0;
00887
00888 ta_learn[i] = (double *) calloc(ntime_learn[i], sizeof(double));
00889 if (ta_learn[i] == NULL) alloc_error(__FILE__, __LINE__);
00890
00891
00892 printf("%s: Reading data from input file %s.\n", __FILE__, filein_learn);
00893 istat = nc_get_vara_double(ncinid_learn, ta_varid[i], start, count, ta_learn[i]);
00894 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00895
00896 }
00897
00898 istat = nc_inq_varid(ncinid_learn, "eca_pc_learn", &eca_pc_learn_varid);
00899 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00900
00901
00902 istat = nc_inq_var(ncinid_learn, eca_pc_learn_varid, (char *) NULL, &vartype_main, &varndims, vardimids, (int *) NULL);
00903 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00904
00905 if (varndims != 1) {
00906 (void) fprintf(stderr, "Error NetCDF type and/or dimensions Line %d.\n", __LINE__);
00907 (void) banner(basename(argv[0]), "ABORT", "END");
00908 (void) abort();
00909 }
00910
00911
00912 start[0] = 0;
00913 start[1] = 0;
00914 start[2] = 0;
00915 count[0] = (size_t) neof_learn;
00916 count[1] = (size_t) 0;
00917 count[2] = (size_t) 0;
00918
00919 eca_pc_learn = (double *) calloc(neof_learn, sizeof(double));
00920 if (eca_pc_learn == NULL) alloc_error(__FILE__, __LINE__);
00921
00922
00923 printf("%s: Reading data from input file %s.\n", __FILE__, filein_learn);
00924 istat = nc_get_vara_double(ncinid_learn, eca_pc_learn_varid, start, count, eca_pc_learn);
00925 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00926
00927
00928 istat = nc_inq_varid(ncinid_learn, "tancp_mean", &tancp_mean_varid);
00929 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00930
00931
00932 istat = nc_inq_var(ncinid_learn, tancp_mean_varid, (char *) NULL, &vartype_main, &varndims, vardimids, (int *) NULL);
00933 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00934
00935 if (varndims != 1) {
00936 (void) fprintf(stderr, "Error NetCDF type and/or dimensions Line %d.\n", __LINE__);
00937 (void) banner(basename(argv[0]), "ABORT", "END");
00938 (void) abort();
00939 }
00940
00941
00942 start[0] = 0;
00943 start[1] = 0;
00944 start[2] = 0;
00945 count[0] = (size_t) nseason_learn;
00946 count[1] = (size_t) 0;
00947 count[2] = (size_t) 0;
00948
00949 tancp_mean = (double *) calloc(nseason_learn, sizeof(double));
00950 if (tancp_mean == NULL) alloc_error(__FILE__, __LINE__);
00951
00952
00953 printf("%s: Reading data from input file %s.\n", __FILE__, filein_learn);
00954 istat = nc_get_vara_double(ncinid_learn, tancp_mean_varid, start, count, tancp_mean);
00955 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00956
00957
00958 istat = nc_inq_varid(ncinid_learn, "tancp_var", &tancp_var_varid);
00959 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00960
00961
00962 istat = nc_inq_var(ncinid_learn, tancp_var_varid, (char *) NULL, &vartype_main, &varndims, vardimids, (int *) NULL);
00963 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00964
00965 if (varndims != 1) {
00966 (void) fprintf(stderr, "Error NetCDF type and/or dimensions Line %d.\n", __LINE__);
00967 (void) banner(basename(argv[0]), "ABORT", "END");
00968 (void) abort();
00969 }
00970
00971
00972 start[0] = 0;
00973 start[1] = 0;
00974 start[2] = 0;
00975 count[0] = (size_t) nseason_learn;
00976 count[1] = (size_t) 0;
00977 count[2] = (size_t) 0;
00978
00979 tancp_var = (double *) calloc(nseason_learn, sizeof(double));
00980 if (tancp_var == NULL) alloc_error(__FILE__, __LINE__);
00981
00982
00983 printf("%s: Reading data from input file %s.\n", __FILE__, filein_learn);
00984 istat = nc_get_vara_double(ncinid_learn, tancp_var_varid, start, count, tancp_var);
00985 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00986
00987
00988
00989
00990 (void) printf("Computing mean and variance of distances to clusters.\n");
00991
00992 mean_dist = (double **) malloc(nseason * sizeof(double *));
00993 if (mean_dist == NULL) alloc_error(__FILE__, __LINE__);
00994 var_dist = (double **) malloc(nseason * sizeof(double *));
00995 if (var_dist == NULL) alloc_error(__FILE__, __LINE__);
00996
00997 for (i=0; i<nseason; i++) {
00998
00999 mean_dist[i] = (double *) malloc(nclust[i] * sizeof(double));
01000 if (mean_dist[i] == NULL) alloc_error(__FILE__, __LINE__);
01001 var_dist[i] = (double *) malloc(nclust[i] * sizeof(double));
01002 if (var_dist[i] == NULL) alloc_error(__FILE__, __LINE__);
01003
01004
01005
01006 var_pc_norm_all = (double *) malloc(neof * sizeof(double));
01007 if (var_pc_norm_all == NULL) alloc_error(__FILE__, __LINE__);
01008 buftmp = (double *) malloc(ntime*neof * sizeof(double));
01009 if (buftmp == NULL) alloc_error(__FILE__, __LINE__);
01010
01011 first_variance = -9999.9999;
01012 (void) normalize_pc(var_pc_norm_all, &first_variance, buftmp, psl_pc, neof, ntime);
01013
01014
01015
01016 (void) sub_period_common(&buf_sub, &ntime_sub, buftmp, year, month, day, year_learn[i], month_learn[i], day_learn[i], 1,
01017 neof, 1, ntime, ntime_learn[i]);
01018 (void) free(buftmp);
01019
01020 (void) mean_variance_dist_clusters(mean_dist[i], var_dist[i], buf_sub, poid[i], eca_pc_learn, var_pc_norm_all,
01021 neof, nclust[i], ntime_sub);
01022 (void) free(buf_sub);
01023 (void) free(var_pc_norm_all);
01024
01025 printf("Season: %d\n", i);
01026 for (ii=0; ii<nclust[i]; ii++)
01027 printf("Cluster #%d. Mean and variance of distances to clusters: %lf %lf\n", ii, mean_dist[i][ii], var_dist[i][ii]);
01028
01029 }
01030
01031
01032
01033
01035
01036 istat = ncclose(ncinid);
01037 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
01038
01039 (void) ut_free_system(unitSystem);
01040
01041 (void) free(psl_pc);
01042
01043 (void) free(timein);
01044 (void) free(filein);
01045 (void) free(filein_poid);
01046 (void) free(filein_learn);
01047
01048 for (i=0; i<nseason; i++) {
01049 (void) free(poid[i]);
01050 (void) free(mean_dist[i]);
01051 (void) free(var_dist[i]);
01052 }
01053 for (i=0; i<nseason_learn; i++) {
01054 (void) free(time_learn[i]);
01055 (void) free(cst_learn[i]);
01056 (void) free(reg_learn[i]);
01057 (void) free(rrd_learn[i]);
01058 (void) free(ta_learn[i]);
01059 (void) free(year_learn[i]);
01060 (void) free(month_learn[i]);
01061 (void) free(day_learn[i]);
01062 (void) free(hour_learn[i]);
01063 (void) free(min_learn[i]);
01064 (void) free(sec_learn[i]);
01065 }
01066 (void) free(poid);
01067 (void) free(mean_dist);
01068 (void) free(var_dist);
01069 (void) free(time_learn);
01070 (void) free(cst_learn);
01071 (void) free(reg_learn);
01072 (void) free(rrd_learn);
01073 (void) free(ta_learn);
01074 (void) free(year_learn);
01075 (void) free(month_learn);
01076 (void) free(day_learn);
01077 (void) free(hour_learn);
01078 (void) free(min_learn);
01079 (void) free(sec_learn);
01080
01081 (void) free(clust_dimid);
01082 (void) free(poid_varid);
01083 (void) free(clust_varid);
01084 (void) free(time_learn_dimid);
01085 (void) free(time_learn_varid);
01086 (void) free(clust_learn_dimid);
01087
01088 (void) free(cst_varid);
01089 (void) free(reg_varid);
01090 (void) free(rrd_varid);
01091 (void) free(ta_varid);
01092
01093 (void) free(nclust);
01094
01095 (void) free(nclust_learn);
01096 (void) free(ntime_learn);
01097 (void) free(eca_pc_learn);
01098 (void) free(tancp_mean);
01099 (void) free(tancp_var);
01100
01101
01102 (void) banner(basename(argv[0]), "OK", "END");
01103
01104 return 0;
01105 }
01106
01107
01111 void show_usage(char *pgm) {
01116 (void) fprintf(stderr, "%s: usage:\n", pgm);
01117 (void) fprintf(stderr, "-i: input NetCDF file\n");
01118 (void) fprintf(stderr, "-o: output NetCDF file\n");
01119 (void) fprintf(stderr, "-h: help\n");
01120
01121 }
01122
01123
01124 void handle_netcdf_error(int status, int lineno)
01125 {
01126 if (status != NC_NOERR) {
01127 fprintf(stderr, "Line: %d Error %d: %s\n", lineno, status, nc_strerror(status));
01128 exit(-1);
01129 }
01130 }