testbestclassif_realdata.c

Go to the documentation of this file.
00001 /* ***************************************************** */
00002 /* testbestclassif_realdata Test best classification     */
00003 /* algorithm using real NetCDF data.                     */
00004 /* testbestclassif_realdata.c                            */
00005 /* ***************************************************** */
00006 /* Author: Christian Page, CERFACS, Toulouse, France.    */
00007 /* ***************************************************** */
00012 /* LICENSE BEGIN
00013 
00014 Copyright Cerfacs (Christian Page) (2015)
00015 
00016 christian.page@cerfacs.fr
00017 
00018 This software is a computer program whose purpose is to downscale climate
00019 scenarios using a statistical methodology based on weather regimes.
00020 
00021 This software is governed by the CeCILL license under French law and
00022 abiding by the rules of distribution of free software. You can use, 
00023 modify and/ or redistribute the software under the terms of the CeCILL
00024 license as circulated by CEA, CNRS and INRIA at the following URL
00025 "http://www.cecill.info". 
00026 
00027 As a counterpart to the access to the source code and rights to copy,
00028 modify and redistribute granted by the license, users are provided only
00029 with a limited warranty and the software's author, the holder of the
00030 economic rights, and the successive licensors have only limited
00031 liability. 
00032 
00033 In this respect, the user's attention is drawn to the risks associated
00034 with loading, using, modifying and/or developing or reproducing the
00035 software by the user in light of its specific status of free software,
00036 that may mean that it is complicated to manipulate, and that also
00037 therefore means that it is reserved for developers and experienced
00038 professionals having in-depth computer knowledge. Users are therefore
00039 encouraged to load and test the software's suitability as regards their
00040 requirements in conditions enabling the security of their systems and/or 
00041 data to be ensured and, more generally, to use and operate it in the 
00042 same conditions as regards security. 
00043 
00044 The fact that you are presently reading this means that you have had
00045 knowledge of the CeCILL license and that you accept its terms.
00046 
00047 LICENSE END */
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 /* C standard includes */
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];    /* dimension ids */
00138 
00139   size_t start[2];
00140   size_t count[2];
00141 
00142   /* Print BEGIN banner */
00143   (void) banner(basename(argv[0]), "1.0", "BEGIN");
00144 
00145   /* Get command-line arguments and set appropriate variables */
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   /* Try 1000 classifications */
00188   nclassif = 1000;
00189   /* Use 8 clusters */
00190   nclusters = 10;
00191   /* Try 100 partitions for best classification */
00192   npart = 30;
00193 
00194   /* Read data in NetCDF file */
00195   istat = nc_open(filein, NC_NOWRITE, &ncid);  /* open for reading */
00196   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00197   
00198   istat = nc_inq_dimid(ncid, "eof", &eofdimid);  /* get ID for eof dimension */
00199   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00200   istat = nc_inq_dimlen(ncid, eofdimid, &dimval); /* get eof length */
00201   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00202   neof = (int) dimval;
00203 
00204   istat = nc_inq_dimid(ncid, "day", &daydimid);  /* get ID for day dimension */
00205   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00206   istat = nc_inq_dimlen(ncid, daydimid, &dimval); /* get day length */
00207   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00208   ndays = (int) dimval;
00209   
00210   istat = nc_inq_varid(ncid, "eof", &eofid);  /* get ID for eof variable */
00211   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00212   istat = nc_inq_varid(ncid, "day", &dayid);  /* get ID for day variable */
00213   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00214 
00215   istat = nc_inq_varid(ncid, "pc_proj", &varid); /* get pc_proj variable ID */
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); /* get variable information */
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   /* Allocate memory and set start and count */
00229   start[0] = 0;
00230   start[1] = 0;
00231   count[0] = (size_t) ndays;
00232   count[1] = (size_t) neof;
00233   /* Allocate memory */
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   /* Read values from netCDF variable */
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   /* Close the netCDF file. */
00244   istat = ncclose(ncid);
00245   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00246 
00247   /* Find clusters: test best classification algorithm */
00248   (void) best_clusters(clusters, pc_eof_days, "euclidian", npart, nclassif, neof, nclusters, ndays);
00249 
00250   /* Output data */
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   /* Free memory */
00263   (void) free(pc_eof_days);
00264   (void) free(clusters);
00265   (void) free(filein);
00266   (void) free(fileoutclust);
00267   (void) free(fileoutpc);
00268 
00269   /* Print END banner */
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 /* Handle error */
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 }

Generated on 12 May 2016 for DSCLIM by  doxygen 1.6.1