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 #ifdef HAVE_CONFIG_H
00056 #include <config.h>
00057 #endif
00058
00060 #define _GNU_SOURCE
00061
00062
00063 #ifdef HAVE_SYS_TYPES_H
00064 #include <sys/types.h>
00065 #endif
00066 #ifdef HAVE_SYS_STAT_H
00067 #include <sys/stat.h>
00068 #endif
00069 #ifdef HAVE_FCNTL_H
00070 #include <fcntl.h>
00071 #endif
00072 #ifdef HAVE_UNISTD_H
00073 #include <unistd.h>
00074 #endif
00075 #ifdef HAVE_STDIO_H
00076 #include <stdio.h>
00077 #endif
00078 #ifdef HAVE_STRING_H
00079 #include <string.h>
00080 #endif
00081 #ifdef HAVE_STDLIB_H
00082 #include <stdlib.h>
00083 #endif
00084 #ifdef HAVE_MATH_H
00085 #include <math.h>
00086 #endif
00087 #ifdef HAVE_TIME_H
00088 #include <time.h>
00089 #endif
00090 #ifdef HAVE_LIBGEN_H
00091 #include <libgen.h>
00092 #endif
00093
00094 #include <zlib.h>
00095 #include <hdf5.h>
00096 #include <netcdf.h>
00097
00098 #include <classif.h>
00099
00101 void show_usage(char *pgm);
00102 void handle_netcdf_error(int status, int lineno);
00103
00105 int main(int argc, char **argv)
00106 {
00114 int i;
00115 int j;
00116 int neof;
00117 int ndays;
00118 int nclusters;
00119 int nclassif;
00120 int npart;
00121
00122 size_t dimval;
00123
00124 double *pc_eof_days = NULL;
00125 double *clusters = NULL;
00126
00127 char *filein = NULL;
00128 char *fileoutclust = NULL;
00129 FILE *fileoutclust_ptr = NULL;
00130 char *fileoutpc = NULL;
00131 FILE *fileoutpc_ptr = NULL;
00132
00133 int istat, ncid;
00134 int eofid, dayid, eofdimid, daydimid, varid;
00135 nc_type vartype;
00136 int varndims;
00137 int vardimids[NC_MAX_VAR_DIMS];
00138
00139 size_t start[2];
00140 size_t count[2];
00141
00142
00143 (void) banner(basename(argv[0]), "1.0", "BEGIN");
00144
00145
00146 for (i=1; i<argc; i++) {
00147 if ( !strcmp(argv[i], "-h") ) {
00148 (void) show_usage(basename(argv[0]));
00149 (void) banner(basename(argv[0]), "OK", "END");
00150 return 0;
00151 }
00152 else if ( !strcmp(argv[i], "-i") ) {
00153 filein = (char *) malloc((strlen(argv[++i])+1) * sizeof(char));
00154 if (filein == NULL) alloc_error(__FILE__, __LINE__);
00155 (void) strcpy(filein, argv[i]);
00156 }
00157 else if ( !strcmp(argv[i], "-o_clust") ) {
00158 fileoutclust = (char *) malloc((strlen(argv[++i])+1) * sizeof(char));
00159 if (fileoutclust == NULL) alloc_error(__FILE__, __LINE__);
00160 (void) strcpy(fileoutclust, argv[i]);
00161 fileoutclust_ptr = fopen(fileoutclust, "w");
00162 if (fileoutclust_ptr == NULL) {
00163 (void) fprintf(stderr, "%s: Cannot open file %s for output!\n", __FILE__, fileoutclust);
00164 (void) banner(basename(argv[0]), "ABORT", "END");
00165 (void) abort();
00166 }
00167 }
00168 else if ( !strcmp(argv[i], "-o_pc") ) {
00169 fileoutpc = (char *) malloc((strlen(argv[++i])+1) * sizeof(char));
00170 if (fileoutpc == NULL) alloc_error(__FILE__, __LINE__);
00171 (void) strcpy(fileoutpc, argv[i]);
00172 fileoutpc_ptr = fopen(fileoutpc, "w");
00173 if (fileoutpc_ptr == NULL) {
00174 (void) fprintf(stderr, "%s: Cannot open file %s for output!\n", __FILE__, fileoutpc);
00175 (void) banner(basename(argv[0]), "ABORT", "END");
00176 (void) abort();
00177 }
00178 }
00179 else {
00180 (void) fprintf(stderr, "%s:: Wrong arg %s.\n\n", basename(argv[0]), argv[i]);
00181 (void) show_usage(basename(argv[0]));
00182 (void) banner(basename(argv[0]), "ABORT", "END");
00183 (void) abort();
00184 }
00185 }
00186
00187
00188 nclassif = 1000;
00189
00190 nclusters = 10;
00191
00192 npart = 30;
00193
00194
00195 istat = nc_open(filein, NC_NOWRITE, &ncid);
00196 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00197
00198 istat = nc_inq_dimid(ncid, "eof", &eofdimid);
00199 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00200 istat = nc_inq_dimlen(ncid, eofdimid, &dimval);
00201 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00202 neof = (int) dimval;
00203
00204 istat = nc_inq_dimid(ncid, "day", &daydimid);
00205 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00206 istat = nc_inq_dimlen(ncid, daydimid, &dimval);
00207 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00208 ndays = (int) dimval;
00209
00210 istat = nc_inq_varid(ncid, "eof", &eofid);
00211 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00212 istat = nc_inq_varid(ncid, "day", &dayid);
00213 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00214
00215 istat = nc_inq_varid(ncid, "pc_proj", &varid);
00216 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00217
00218 istat = nc_inq_var(ncid, varid, (char *) NULL, &vartype, &varndims, vardimids, (int *) NULL);
00219 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00220
00221 if (vartype != NC_DOUBLE || varndims != 2) {
00222 (void) fprintf(stderr, "Error NetCDF type and/or dimensions.\n");
00223 (void) banner(basename(argv[0]), "ABORT", "END");
00224 (void) abort();
00225 }
00226
00228
00229 start[0] = 0;
00230 start[1] = 0;
00231 count[0] = (size_t) ndays;
00232 count[1] = (size_t) neof;
00233
00234 pc_eof_days = (double *) calloc(neof*ndays, sizeof(double));
00235 if (pc_eof_days == NULL) alloc_error(__FILE__, __LINE__);
00236 clusters = (double *) calloc(neof*nclusters, sizeof(double));
00237 if (clusters == NULL) alloc_error(__FILE__, __LINE__);
00238
00239
00240 istat = nc_get_vara_double(ncid, varid, start, count, pc_eof_days);
00241 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00242
00243
00244 istat = ncclose(ncid);
00245 if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00246
00247
00248 (void) best_clusters(clusters, pc_eof_days, "euclidian", npart, nclassif, neof, nclusters, ndays);
00249
00250
00251 for (i=0; i<neof; i++)
00252 for (j=0; j<nclusters; j++)
00253 (void) fprintf(fileoutclust_ptr, "%d %d %lf\n", i, j, clusters[i+j*neof]);
00254
00255 for (j=0; j<ndays; j++)
00256 for (i=0; i<neof; i++)
00257 (void) fprintf(fileoutpc_ptr, "%d %d %lf\n", i, j, pc_eof_days[i+j*neof]);
00258
00259 (void) fclose(fileoutclust_ptr);
00260 (void) fclose(fileoutpc_ptr);
00261
00262
00263 (void) free(pc_eof_days);
00264 (void) free(clusters);
00265 (void) free(filein);
00266 (void) free(fileoutclust);
00267 (void) free(fileoutpc);
00268
00269
00270 (void) banner(basename(argv[0]), "OK", "END");
00271
00272 return 0;
00273 }
00274
00275
00279 void show_usage(char *pgm) {
00284 (void) fprintf(stderr, "%s: usage:\n", pgm);
00285 (void) fprintf(stderr, "-h: help\n");
00286
00287 }
00288
00289
00290 void handle_netcdf_error(int status, int lineno)
00291 {
00292 if (status != NC_NOERR) {
00293 fprintf(stderr, "Line: %d Error: %s\n", lineno, nc_strerror(status));
00294 exit(-1);
00295 }
00296 }