00001 /* ***************************************************** */ 00002 /* Classification subroutine: find the closest cluster */ 00003 /* of the principal components for each given day */ 00004 /* in EOF space. */ 00005 /* class_days_pc_clusters.c */ 00006 /* ***************************************************** */ 00007 /* Author: Christian Page, CERFACS, Toulouse, France. */ 00008 /* ***************************************************** */ 00013 /* LICENSE BEGIN 00014 00015 Copyright Cerfacs (Christian Page) (2015) 00016 00017 christian.page@cerfacs.fr 00018 00019 This software is a computer program whose purpose is to downscale climate 00020 scenarios using a statistical methodology based on weather regimes. 00021 00022 This software is governed by the CeCILL license under French law and 00023 abiding by the rules of distribution of free software. You can use, 00024 modify and/ or redistribute the software under the terms of the CeCILL 00025 license as circulated by CEA, CNRS and INRIA at the following URL 00026 "http://www.cecill.info". 00027 00028 As a counterpart to the access to the source code and rights to copy, 00029 modify and redistribute granted by the license, users are provided only 00030 with a limited warranty and the software's author, the holder of the 00031 economic rights, and the successive licensors have only limited 00032 liability. 00033 00034 In this respect, the user's attention is drawn to the risks associated 00035 with loading, using, modifying and/or developing or reproducing the 00036 software by the user in light of its specific status of free software, 00037 that may mean that it is complicated to manipulate, and that also 00038 therefore means that it is reserved for developers and experienced 00039 professionals having in-depth computer knowledge. Users are therefore 00040 encouraged to load and test the software's suitability as regards their 00041 requirements in conditions enabling the security of their systems and/or 00042 data to be ensured and, more generally, to use and operate it in the 00043 same conditions as regards security. 00044 00045 The fact that you are presently reading this means that you have had 00046 knowledge of the CeCILL license and that you accept its terms. 00047 00048 LICENSE END */ 00049 00050 00051 00052 00053 00054 00055 00056 #include <classif.h> 00057 00059 void 00060 class_days_pc_clusters(int *days_class_cluster, double *pc_eof_days, double *eof_days_cluster, char *type, 00061 int neof, int ncluster, int ndays) { 00072 double dist_min; /* Minimum distance found between a given day PC (summed over all EOF) and each cluster centroid. */ 00073 int clust_dist_min; /* Cluster number which has the minimum distance dist_min */ 00074 double dist_sum; /* Sum of distances (partial computation) over all EOFs */ 00075 double val; /* Distance between a given day PC (for a particular EOF) and one cluster centroid. */ 00076 double dist_clust; /* Distance (full computation of dist_sum). */ 00077 00078 int day; /* Loop counter for days */ 00079 int clust; /* Loop counter for cluster */ 00080 int eof; /* Loop counter for eofs */ 00081 00082 if ( !strcmp(type, "euclidian") ) { 00083 /* Euclidian distance type */ 00084 00085 /* Parse each day */ 00086 for (day=0; day<ndays; day++) { 00087 00088 /* Initialize */ 00089 dist_min = 9999999999.0; 00090 clust_dist_min = 999; 00091 00092 #if DEBUG >= 7 00093 (void) fprintf(stderr, "day=%d\n", day); 00094 #endif 00095 00096 /* Parse each cluster */ 00097 for (clust=0; clust<ncluster; clust++) { 00098 00099 #if DEBUG >= 7 00100 (void) fprintf(stderr, "clust=%d\n", clust); 00101 #endif 00102 00103 dist_sum = 0.0; 00104 /* Sum all distances (over EOF) between the PC of the day and the PC of the cluster centroid for each EOF respectively */ 00105 for (eof=0; eof<neof; eof++) { 00106 val = pc_eof_days[day+eof*ndays] - eof_days_cluster[eof+clust*neof]; 00107 #if DEBUG >= 9 00108 printf("%d %d %lf %lf\n",clust,eof,pc_eof_days[day+eof*ndays],eof_days_cluster[eof+clust*neof]); 00109 #endif 00110 /* Euclidian distance: square */ 00111 dist_sum += (val * val); 00112 } 00113 /* Euclidian distance: square root of squares */ 00114 dist_clust = sqrt(dist_sum); 00115 00116 #if DEBUG >= 7 00117 (void) fprintf(stderr, "dist_clust=%lf\n", dist_clust); 00118 #endif 00119 00120 /* Is it a cluster which has less distance as the minimum found yet ? */ 00121 if (dist_clust < dist_min) { 00122 /* Save cluster number */ 00123 clust_dist_min = clust; 00124 dist_min = dist_clust; 00125 } 00126 } 00127 if (clust_dist_min == 999) { 00128 /* Failing algorithm */ 00129 (void) fprintf(stderr, "%s: ABORT: Impossible: no cluster was selected!! Problem in algorithm...\n", __FILE__); 00130 (void) abort(); 00131 } 00132 /* Assign cluster with minimum distance to all EOFs for this day */ 00133 days_class_cluster[day] = clust_dist_min; 00134 #if DEBUG >= 9 00135 (void) fprintf(stderr, "%s: day %d cluster %d\n", __FILE__, day, clust_dist_min); 00136 #endif 00137 } 00138 } 00139 else { 00140 /* Unknown distance type */ 00141 (void) fprintf(stderr, "%s: ABORT: Unknown distance type=%s!!\n", __FILE__, type); 00142 (void) abort(); 00143 } 00144 }