dsclim.c

Go to the documentation of this file.
00001 /* ***************************************************** */
00002 /* dsclim Downscaling climate scenarios                  */
00003 /* dsclim.c                                              */
00004 /* ***************************************************** */
00005 /* Author: Christian Page, CERFACS, Toulouse, France.    */
00006 /* ***************************************************** */
00007 /* Date of creation: sep 2008                            */
00008 /* Last date of modification: sep 2008                   */
00009 /* ***************************************************** */
00010 /* Original version: 1.0                                 */
00011 /* Current revision:                                     */
00012 /* ***************************************************** */
00013 /* Revisions                                             */
00014 /* ***************************************************** */
00019 /* LICENSE BEGIN
00020 
00021 Copyright Cerfacs (Christian Page) (2015)
00022 
00023 christian.page@cerfacs.fr
00024 
00025 This software is a computer program whose purpose is to downscale climate
00026 scenarios using a statistical methodology based on weather regimes.
00027 
00028 This software is governed by the CeCILL license under French law and
00029 abiding by the rules of distribution of free software. You can use, 
00030 modify and/ or redistribute the software under the terms of the CeCILL
00031 license as circulated by CEA, CNRS and INRIA at the following URL
00032 "http://www.cecill.info". 
00033 
00034 As a counterpart to the access to the source code and rights to copy,
00035 modify and redistribute granted by the license, users are provided only
00036 with a limited warranty and the software's author, the holder of the
00037 economic rights, and the successive licensors have only limited
00038 liability. 
00039 
00040 In this respect, the user's attention is drawn to the risks associated
00041 with loading, using, modifying and/or developing or reproducing the
00042 software by the user in light of its specific status of free software,
00043 that may mean that it is complicated to manipulate, and that also
00044 therefore means that it is reserved for developers and experienced
00045 professionals having in-depth computer knowledge. Users are therefore
00046 encouraged to load and test the software's suitability as regards their
00047 requirements in conditions enabling the security of their systems and/or 
00048 data to be ensured and, more generally, to use and operate it in the 
00049 same conditions as regards security. 
00050 
00051 The fact that you are presently reading this means that you have had
00052 knowledge of the CeCILL license and that you accept its terms.
00053 
00054 LICENSE END */
00055 
00056 
00057 
00058 
00059 
00060 
00061 
00062 #include <dsclim.h>
00063 
00065 void show_usage(char *pgm);
00066 short int show_license();
00067 
00069 int main(int argc, char **argv)
00070 {
00078   int i; /* Loop counter */
00079   int istat = 0; /* Return status */
00080   data_struct *data = NULL; /* Main data structure */
00081   short int license_accept = 0; /* If user has accepted license or not */
00082   short int downscale = TRUE; /* If we want to downscale at least one period, excluding control */
00083   
00084   /* Command-line arguments variables */
00085   char fileconf[500]; /* Configuration filename */
00086 
00087   /* Show license so user can accept or deny it. */
00088   license_accept = show_license();
00089   if (license_accept == 1)
00090     (void) printf("\n\nYou accepted the license and its terms: the program can continue!\nYou will not have to input again the acceptance of the license\nand its terms for further use of the program in the current account.\n\n");
00091   else if (license_accept != 2) {
00092     (void) printf("\n\nYou did not accept the license and its terms: the program cannot continue. It must exit now.\n\n");
00093     (void) exit(0);
00094   }
00095   /* license_accept == 2 when user had already accepted the license in a previous execution of the program. */
00096 
00097   /* Print BEGIN banner */
00098   (void) banner(PACKAGE_NAME, PACKAGE_VERSION, "BEGIN");
00099 
00100   /* Allocate memory */
00101   data = (data_struct *) malloc(sizeof(data_struct));
00102   if (data == NULL) alloc_error(__FILE__, __LINE__);
00103 
00104   /* Get command-line arguments and set appropriate variables */
00105   (void) printf("\n**** PROCESS COMMAND-LINE ARGUMENTS ****\n\n");
00106   if (argc <= 1) {
00107     (void) show_usage(PACKAGE_NAME);
00108     (void) banner(PACKAGE_NAME, "ABORT", "END");
00109     (void) exit(1);
00110   }
00111   else
00112     /* Decode command-line arguments */
00113     for (i=1; i<argc; i++) {
00114       if ( !strcmp(argv[i], "-conf") )
00115         (void) strcpy(fileconf, argv[++i]);
00116       else if ( !strcmp(argv[i], "--version") ) {
00117         (void) printf("%s version %s\n\n", PACKAGE_NAME, PACKAGE_VERSION);
00118         (void) banner(PACKAGE_NAME, "OK", "END");
00119         (void) exit(0);
00120       }
00121       else {
00122         (void) fprintf(stderr, "%s:: Wrong arg %s.\n\n", PACKAGE_NAME, argv[i]);
00123         (void) show_usage(PACKAGE_NAME);
00124         (void) banner(PACKAGE_NAME, "ABORT", "END");
00125         (void) exit(1);
00126       }
00127     }
00128 
00129   /* Read and store configuration file in memory */
00130   /* Allocate memory for main data structures */
00131   (void) printf("\n**** LOADING CONFIGURATION ****\n\n");
00132   istat = load_conf(data, fileconf);
00133   if (istat != 0) {
00134     (void) fprintf(stderr, "%s: Error in loading configuration file. Aborting.\n", __FILE__);
00135     (void) banner(PACKAGE_NAME, "ABORT", "END");
00136     (void) abort();
00137   }
00138 
00139 
00140   /* Generate analog data only if we are not reading it off disk */
00141   if (data->conf->output_only == FALSE) {
00142     
00143     /* Read regression points positions if needed */
00144     if (data->reg->filename != NULL) {
00145       (void) printf("\n**** READING REGRESSION POSITIONS ****\n\n");
00146       istat = read_regression_points(data->reg);
00147       if (istat != 0) {
00148         (void) fprintf(stderr, "%s: Error in reading regression points positions. Aborting.\n", __FILE__);
00149         (void) banner(PACKAGE_NAME, "ABORT", "END");
00150         (void) abort();
00151       }
00152     }
00153 
00154     /* Read mask for secondary large-scale fields if needed */
00155     if (data->secondary_mask->use_mask == TRUE) {
00156       (void) printf("\n**** SECONDARY LARGE-SCALE FIELDS MASK ****\n\n");
00157       istat = read_mask(data->secondary_mask);
00158       if (istat != 0) {
00159         (void) fprintf(stderr, "%s: Error in reading secondary large-scale mask file. Aborting.\n", __FILE__);
00160         (void) banner(PACKAGE_NAME, "ABORT", "END");
00161         (void) abort();
00162       }
00163     }
00164     
00165     /* Read mask for learning fields if needed */
00166     if (data->conf->learning_maskfile->use_mask == TRUE) {
00167       (void) printf("\n**** LEARNING FIELDS MASK ****\n\n");
00168       istat = read_mask(data->conf->learning_maskfile);
00169       if (istat != 0) {
00170         (void) fprintf(stderr, "%s: Error in reading learning mask file. Aborting.\n", __FILE__);
00171         (void) banner(PACKAGE_NAME, "ABORT", "END");
00172         (void) abort();
00173       }
00174     }
00175 
00176     /* If wanted, generate learning data */
00177     (void) printf("\n**** LEARNING ****\n\n");
00178     istat = wt_learning(data);
00179     if (istat != 0) {
00180       (void) fprintf(stderr, "%s: Error in computing or reading learning data needed for downscaling. Aborting.\n", __FILE__);
00181       (void) banner(PACKAGE_NAME, "ABORT", "END");
00182       (void) abort();
00183     }
00184   }
00185   
00186   /* Perform downscaling */
00187   downscale = FALSE;
00188   for (i=0; i<data->conf->nperiods; i++)
00189     if (data->conf->period[i].downscale == TRUE)
00190       downscale = TRUE;
00191   if (data->conf->period_ctrl->downscale == TRUE || downscale == TRUE) {
00192     (void) printf("\n**** DOWNSCALING ****\n\n");
00193     if (data->conf->output_only == TRUE)
00194       (void) printf("****WARNING: Configuration for reading analog dates and writing data ONLY!\n\n");
00195     istat = wt_downscaling(data);
00196     if (istat != 0) {
00197       (void) fprintf(stderr, "%s: Error in performing downscaling. Aborting.\n", __FILE__);
00198       (void) banner(PACKAGE_NAME, "ABORT", "END");
00199       (void) abort();
00200     }
00201   }
00202   
00203   /* Free main data structure */
00204   (void) printf("\n**** FREE MEMORY ****\n\n");
00205   (void) free_main_data(data);
00206   (void) free(data);
00207 
00208   /* Print END banner */
00209   (void) banner(PACKAGE_NAME, "OK", "END");
00210   
00211   return 0;
00212 }
00213 
00214 
00218 void
00219 show_usage(char *pgm) {
00224   (void) fprintf(stderr, "%s:: usage:\n", pgm);
00225   (void) fprintf(stderr, "-conf: configuration file\n");
00226 
00227 }
00228 
00230 short int
00231 show_license() {
00236   short int license_accept = 0;
00237   int istat = -1;
00238   const char license[5000] = "\nCopyright Cerfacs (Christian Page) (2010)\n\nchristian.page@cerfacs.fr\n\nThis software is a computer program whose purpose is to downscale climate\nscenarios using a statistical methodology based on weather regimes.\n\nThis software is governed by the CeCILL license under French law and\nabiding by the rules of distribution of free software. You can use, \nmodify and/ or redistribute the software under the terms of the CeCILL\nlicense as circulated by CEA, CNRS and INRIA at the following URL\n\"http://www.cecill.info\". \n\nAs a counterpart to the access to the source code and rights to copy,\nmodify and redistribute granted by the license, users are provided only\nwith a limited warranty and the software's author, the holder of the\neconomic rights, and the successive licensors have only limited\nliability. \n\nIn this respect, the user's attention is drawn to the risks associated\nwith loading, using, modifying and/or developing or reproducing the\nsoftware by the user in light of its specific status of free software,\nthat may mean that it is complicated to manipulate, and that also\ntherefore means that it is reserved for developers and experienced\nprofessionals having in-depth computer knowledge. Users are therefore\nencouraged to load and test the software's suitability as regards their\nrequirements in conditions enabling the security of their systems and/or \ndata to be ensured and, more generally, to use and operate it in the \nsame conditions as regards security. \n\nTo further continue, you must accept the CeCILL license and its terms.\n\n";
00239   char *dsclim_lic_file = NULL; /* dsclim license check file */
00240   char *dsclim_lic = NULL; /* dsclim license check */
00241   FILE *ifile = NULL; /* File for license acceptance */
00242 
00243   /* Let user accepts license */
00244   dsclim_lic_file = (char *) malloc(MAXPATH * sizeof(char));
00245   if (dsclim_lic_file == NULL) alloc_error(__FILE__, __LINE__);
00246   (void) sprintf(dsclim_lic_file, "%s/%s", getenv("HOME"), ".dsclim_lic");
00247 
00248   if ( access(dsclim_lic_file, F_OK) != 0 ) {
00249     dsclim_lic = (char *) malloc(10 * sizeof(char));
00250     if (dsclim_lic == NULL) alloc_error(__FILE__, __LINE__);
00251     /* User never had the license acceptance */
00252     (void) fprintf(stdout, license);
00253     (void) fprintf(stdout, "Do you accept or deny the license? [Type accept or deny]: ");
00254     /* Read input from user */
00255     istat = scanf("%7s", dsclim_lic);
00256     if ( !strcmp(dsclim_lic, "accept") ) {
00257       license_accept = 1;
00258       ifile = fopen(dsclim_lic_file, "w");
00259       if (ifile == NULL)
00260         (void) fprintf(stderr, "%s: Failed to write license acceptance file.\n", __FILE__);
00261       (void) fprintf(ifile, license);
00262       (void) fprintf(ifile, "The user accepted the dsclim license and its terms.\n\n");
00263       (void) fclose(ifile);
00264     }
00265     (void) free(dsclim_lic);
00266   }
00267   else
00268     license_accept = 2;
00269 
00270   (void) free(dsclim_lic_file);
00271   
00272   return license_accept;
00273 }

Generated on 12 May 2016 for DSCLIM by  doxygen 1.6.1