testbestclassif_realdata.c File Reference

Test best classification algorithm using real NetCDF data. More...

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <libgen.h>
#include <zlib.h>
#include <hdf5.h>
#include <netcdf.h>
#include <classif.h>
Include dependency graph for testbestclassif_realdata.c:

Go to the source code of this file.

Defines

#define _GNU_SOURCE
 GNU extensions.

Functions

void show_usage (char *pgm)
 C prototypes.
void handle_netcdf_error (int status, int lineno)
int main (int argc, char **argv)
 Main program.

Detailed Description

Test best classification algorithm using real NetCDF data.

Definition in file testbestclassif_realdata.c.


Define Documentation

#define _GNU_SOURCE

GNU extensions.

Definition at line 60 of file testbestclassif_realdata.c.


Function Documentation

int main ( int  argc,
char **  argv 
)

Main program.

Parameters:
[in] argc Number of command-line arguments.
[in] argv Vector of command-line argument strings.
Returns:
Status.

Read data variable

Definition at line 105 of file testbestclassif_realdata.c.

References alloc_error(), banner(), best_clusters(), handle_netcdf_error(), and show_usage().

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 }

void show_usage ( char *  pgm  ) 

C prototypes.

Local Subroutines.

Show usage for program command-line arguments.

Parameters:
[in] pgm Program name.

Definition at line 279 of file testbestclassif_realdata.c.

00279                            {
00284   (void) fprintf(stderr, "%s: usage:\n", pgm);
00285   (void) fprintf(stderr, "-h: help\n");
00286 
00287 }


Generated on 12 May 2016 for DSCLIM by  doxygen 1.6.1