test_mean_variance_temperature.c

Go to the documentation of this file.
00001 /* ********************************************************* */
00002 /* test_mean_variance_temperature                            */
00003 /* Test mean_variance_field_spatial function.                */
00004 /* test_mean_variance_temperature.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_UNSTD_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 <udunits.h>
00099 
00100 #include <utils.h>
00101 #include <filter.h>
00102 #include <clim.h>
00103 
00105 void show_usage(char *pgm);
00106 void handle_netcdf_error(int status, int lineno);
00107 
00109 int main(int argc, char **argv)
00110 {
00118   int nlat;
00119   int nlon;
00120   int nlat_sub;
00121   int nlon_sub;
00122   int ntime;
00123   int ntime_sub;
00124   int nseason_learn;
00125   int *ntime_learn = NULL;
00126 
00127   int *year = NULL;
00128   int *month = NULL;
00129   int *day = NULL;
00130   int *hour = NULL;
00131   int *min = NULL;
00132   double *sec = NULL;
00133 
00134   double mean;
00135   double var;
00136 
00137   char varname[500];
00138 
00139   size_t dimval;
00140 
00141   char *filein = NULL;
00142   char *filein_learn = NULL;
00143 
00144   int istat, ncinid, ncinid_learn;
00145   int varinid, timeinid, timediminid, loninid, londiminid, latinid, latdiminid;
00146   int seasondiminid_learn;
00147   int *time_learn_dimid = NULL;
00148   int *time_learn_varid = NULL;
00149   nc_type vartype_main;
00150   nc_type vartype_time;
00151   int varndims;
00152   int vardimids[NC_MAX_VAR_DIMS];    /* dimension ids */
00153 
00154   size_t start[3];
00155   size_t count[3];
00156 
00157   size_t t_len;
00158   size_t t_len_learn;
00159   char *time_units = NULL;
00160   char *time_units_learn = NULL;
00161   char *cal_type = NULL;
00162   char *cal_type_learn = NULL;
00163   ut_system *unitSystem = NULL; /* Unit System (udunits) */
00164   ut_unit *dataunits = NULL;
00165   ut_unit *dataunits_learn = NULL;
00166 
00167   double *tas = NULL;
00168   double *tas_sub = NULL;
00169   double *tas_subt = NULL;
00170   double *tas_smean = NULL;
00171   double *timein = NULL;
00172   double *lat = NULL;
00173   double *lon = NULL;
00174   double *lat_sub = NULL;
00175   double *lon_sub = NULL;
00176   double **time_learn = NULL;
00177   double *buftmp = NULL;
00178   int **year_learn = NULL;
00179   int **month_learn = NULL;
00180   int **day_learn = NULL;
00181   int **hour_learn = NULL;
00182   int **min_learn = NULL;
00183   double **sec_learn = NULL;
00184 
00185   double minlon = -15.0;
00186   double maxlon = 20.0;
00187   double minlat = 38.0;
00188   double maxlat = 60.0;
00189 
00190   int i;
00191   int t;
00192   int season;
00193 
00194   /* Print BEGIN banner */
00195   (void) banner(basename(argv[0]), "1.0", "BEGIN");
00196 
00197   /* Get command-line arguments and set appropriate variables */
00198   for (i=1; i<argc; i++) {
00199     if ( !strcmp(argv[i], "-h") ) {
00200       (void) show_usage(basename(argv[0]));
00201       (void) banner(basename(argv[0]), "OK", "END");
00202       return 0;
00203     }
00204     else if ( !strcmp(argv[i], "-i") ) {
00205       filein = (char *) malloc((strlen(argv[++i])+1) * sizeof(char));
00206       if (filein == NULL) alloc_error(__FILE__, __LINE__);
00207       (void) strcpy(filein, argv[i]);
00208     }
00209     else if ( !strcmp(argv[i], "-i_learn") ) {
00210       filein_learn = (char *) malloc((strlen(argv[++i])+1) * sizeof(char));
00211       if (filein_learn == NULL) alloc_error(__FILE__, __LINE__);
00212       (void) strcpy(filein_learn, argv[i]);
00213     }
00214     else {
00215       (void) fprintf(stderr, "%s:: Wrong arg %s.\n\n", basename(argv[0]), argv[i]);
00216       (void) show_usage(basename(argv[0]));
00217       (void) banner(basename(argv[0]), "ABORT", "END");
00218       (void) abort();
00219     }
00220   }
00221 
00222   /* Initialize udunits */
00223   ut_set_error_message_handler(ut_ignore);
00224   unitSystem = ut_read_xml(NULL);
00225   ut_set_error_message_handler(ut_write_to_stderr);
00226 
00227   /* Read data in NetCDF file */
00228   printf("%s: Reading info from input file %s.\n", __FILE__, filein);
00229   istat = nc_open(filein, NC_NOWRITE, &ncinid);  /* open for reading */
00230   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00231 
00232   istat = nc_inq_dimid(ncinid, "time", &timediminid);  /* get ID for time dimension */
00233   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00234   istat = nc_inq_dimlen(ncinid, timediminid, &dimval); /* get time length */
00235   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00236   ntime = (int) dimval;
00237 
00238   istat = nc_inq_dimid(ncinid, "lat1", &latdiminid);  /* get ID for lat dimension */
00239   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00240   istat = nc_inq_dimlen(ncinid, latdiminid, &dimval); /* get lat length */
00241   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00242   nlat = (int) dimval;
00243 
00244   istat = nc_inq_dimid(ncinid, "lon1", &londiminid);  /* get ID for lon dimension */
00245   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00246   istat = nc_inq_dimlen(ncinid, londiminid, &dimval); /* get lon length */
00247   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00248   nlon = (int) dimval;
00249   
00250   istat = nc_inq_varid(ncinid, "time", &timeinid);  /* get ID for time variable */
00251   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00252   istat = nc_inq_varid(ncinid, "er40.lat", &latinid);  /* get ID for lat variable */
00253   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00254   istat = nc_inq_varid(ncinid, "er40.lon", &loninid);  /* get ID for lon variable */
00255   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00256 
00257   istat = nc_inq_varid(ncinid, "tas", &varinid); /* get tas variable ID */
00258   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00259 
00261   buftmp = (double *) malloc(nlat * nlon * sizeof(double));
00262   if (buftmp == NULL) alloc_error(__FILE__, __LINE__);
00263   lat = (double *) malloc(nlat * sizeof(double));
00264   if (lat == NULL) alloc_error(__FILE__, __LINE__);
00265   lon = (double *) malloc(nlon * sizeof(double));
00266   if (lon == NULL) alloc_error(__FILE__, __LINE__);
00267   /* Allocate memory and set start and count */
00268   start[0] = 0;
00269   start[1] = 0;
00270   start[2] = 0;
00271   count[0] = (size_t) nlat;
00272   count[1] = (size_t) nlon;
00273   count[2] = 0;
00274 
00275   /* Read values from netCDF variable */
00276   istat = nc_get_vara_double(ncinid, latinid, start, count, buftmp);
00277   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00278 
00279   for (i=0; i<nlat; i++)
00280     lat[i] = buftmp[i*nlon];
00281 
00282   /* Allocate memory and set start and count */
00283   start[0] = 0;
00284   start[1] = 0;
00285   start[2] = 0;
00286   count[0] = (size_t) nlat;
00287   count[1] = (size_t) nlon;
00288   count[2] = 0;
00289 
00290   /* Read values from netCDF variable */
00291   istat = nc_get_vara_double(ncinid, loninid, start, count, buftmp);
00292   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00293 
00294   for (i=0; i<nlon; i++)
00295     lon[i] = buftmp[i];
00296   (void) free(buftmp);
00297   
00298   /* Get time dimensions and type */
00299   istat = nc_inq_var(ncinid, timeinid, (char *) NULL, &vartype_time, &varndims, vardimids, (int *) NULL); /* get variable information */
00300   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00301   
00302   if (varndims != 1) {
00303     (void) fprintf(stderr, "Error NetCDF type and/or dimensions.\n");
00304     (void) banner(basename(argv[0]), "ABORT", "END");
00305     (void) abort();
00306   }
00307 
00308   /* Allocate memory and set start and count */
00309   start[0] = 0;
00310   count[0] = (size_t) ntime;
00311   timein = malloc(ntime * sizeof(double));
00312   if (timein == NULL) alloc_error(__FILE__, __LINE__);
00313 
00314   /* Read values from netCDF variable */
00315   istat = nc_get_vara_double(ncinid, timeinid, start, count, timein);
00316   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00317 
00318   /* Check values of time variable because many times they are all zero. In that case assume a 1 increment and a start at zero. */
00319   for (t=0; t<ntime; t++)
00320     if (timein[t] != 0.0)
00321       break;
00322   if (t == ntime) {
00323     fprintf(stderr, "WARNING: Time variable values all zero!!! Fixing time variable to index value...\n");
00324     for (t=0; t<ntime; t++)
00325       timein[t] = (double) t;
00326   }
00327 
00330   /* Get variable information */
00331   istat = nc_inq_var(ncinid, varinid, (char *) NULL, &vartype_main, &varndims, vardimids, (int *) NULL);
00332   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00333 
00334   if (varndims != 3) {
00335     (void) fprintf(stderr, "Error NetCDF type and/or dimensions.\n");
00336     (void) banner(basename(argv[0]), "ABORT", "END");
00337     (void) abort();
00338   }
00339 
00340   /* Allocate memory and set start and count */
00341   start[0] = 0;
00342   start[1] = 0;
00343   start[2] = 0;
00344   count[0] = (size_t) ntime;
00345   count[1] = (size_t) nlat;
00346   count[2] = (size_t) nlon;
00347   /* Allocate memory */
00348   tas = (double *) calloc(nlat*nlon*ntime, sizeof(double));
00349   if (tas == NULL) alloc_error(__FILE__, __LINE__);
00350 
00351   /* Read values from netCDF variable */
00352   printf("%s: Reading datafrom input file %s.\n", __FILE__, filein);
00353   istat = nc_get_vara_double(ncinid, varinid, start, count, tas);
00354   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00355 
00356   /* Get time units attribute length */
00357   istat = nc_inq_attlen(ncinid, timeinid, "units", &t_len);
00358   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00359   /* Allocate required space before retrieving values */
00360   time_units = (char *) malloc(t_len + 1);
00361   if (time_units == NULL) alloc_error(__FILE__, __LINE__);
00362   /* Get time units attribute value */
00363   istat = nc_get_att_text(ncinid, timeinid, "units", time_units);
00364   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00365   time_units[t_len] = '\0'; /* null terminate */
00366 
00367   /* Get calendar type attribute length */
00368   istat = nc_inq_attlen(ncinid, timeinid, "calendar", &t_len);
00369   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00370   /* Allocate required space before retrieving values */
00371   cal_type = (char *) malloc(t_len + 1);
00372   if (cal_type == NULL) alloc_error(__FILE__, __LINE__);
00373   /* Get calendar type attribute value */
00374   istat = nc_get_att_text(ncinid, timeinid, "calendar", cal_type);
00375   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00376   cal_type[t_len] = '\0'; /* null terminate */
00377 
00378 
00379 
00380   /* Read data in NetCDF file */
00381   printf("%s: Reading info from input file %s.\n", __FILE__, filein_learn);
00382   istat = nc_open(filein_learn, NC_NOWRITE, &ncinid_learn);  /* open for reading */
00383   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00384 
00385   istat = nc_inq_dimid(ncinid_learn, "season", &seasondiminid_learn);  /* get ID for season dimension */
00386   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00387   istat = nc_inq_dimlen(ncinid_learn, seasondiminid_learn, &dimval); /* get season length */
00388   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00389   nseason_learn = (int) dimval;
00390 
00392   time_learn_dimid = malloc(nseason_learn * sizeof(int));
00393   if (time_learn_dimid == NULL) alloc_error(__FILE__, __LINE__);
00394   time_learn_varid = malloc(nseason_learn * sizeof(int));
00395   if (time_learn_varid == NULL) alloc_error(__FILE__, __LINE__);
00396 
00397   ntime_learn = malloc(nseason_learn * sizeof(int));
00398   if (ntime_learn == NULL) alloc_error(__FILE__, __LINE__);
00399 
00401   time_learn = (double **) malloc(nseason_learn * sizeof(double *));
00402   if (time_learn == NULL) alloc_error(__FILE__, __LINE__);
00403 
00404   year_learn = (int **) malloc(nseason_learn * sizeof(int *));
00405   if (year_learn == NULL) alloc_error(__FILE__, __LINE__);
00406   month_learn = (int **) malloc(nseason_learn * sizeof(int *));
00407   if (month_learn == NULL) alloc_error(__FILE__, __LINE__);
00408   day_learn = (int **) malloc(nseason_learn * sizeof(int *));
00409   if (day_learn == NULL) alloc_error(__FILE__, __LINE__);
00410   hour_learn = (int **) malloc(nseason_learn * sizeof(int *));
00411   if (hour_learn == NULL) alloc_error(__FILE__, __LINE__);
00412   min_learn = (int **) malloc(nseason_learn * sizeof(int *));
00413   if (min_learn == NULL) alloc_error(__FILE__, __LINE__);
00414   sec_learn = (double **) malloc(nseason_learn * sizeof(double *));
00415   if (sec_learn == NULL) alloc_error(__FILE__, __LINE__);
00416 
00417   for (i=0; i<nseason_learn; i++) {
00418 
00420     if (i == 0)
00421       (void) strcpy(varname, "time_aut");
00422     else if (i == 1)
00423       (void) strcpy(varname, "time_hiv");
00424     else if (i == 2)
00425       (void) strcpy(varname, "time_pri");
00426     else if (i == 3)
00427       (void) strcpy(varname, "time_ete");
00428     else
00429       exit(1);
00430     istat = nc_inq_dimid(ncinid_learn, varname, &(time_learn_dimid[i]));  /* get ID for time_learn dimension */
00431     if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00432     istat = nc_inq_dimlen(ncinid_learn, time_learn_dimid[i], &dimval); /* get dimension length */
00433     if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00434     ntime_learn[i] = (int) dimval;
00435 
00436     istat = nc_inq_varid(ncinid_learn, varname, &(time_learn_varid[i]));  /* get ID for variable */
00437     if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00438 
00439     /* Allocate memory and set start and count */
00440     start[0] = 0;
00441     start[1] = 0;
00442     start[2] = 0;
00443     count[0] = (size_t) ntime_learn[i];
00444     count[1] = (size_t) 0;
00445     count[2] = (size_t) 0;
00446     /* Allocate memory */
00447     time_learn[i] = (double *) calloc(ntime_learn[i], sizeof(double));
00448     if (time_learn[i] == NULL) alloc_error(__FILE__, __LINE__);
00449     
00450     /* Read values from netCDF variable */
00451     printf("%s: Reading data from input file %s.\n", __FILE__, filein_learn);
00452     istat = nc_get_vara_double(ncinid_learn, time_learn_varid[i], start, count, time_learn[i]);
00453     if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00454     //    printf("%d %lf\n",i,time_learn[i][0]);
00455 
00456     /* Get variable information */
00457     istat = nc_inq_var(ncinid_learn, time_learn_varid[i], (char *) NULL, &vartype_main, &varndims, vardimids, (int *) NULL);
00458     if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00459     
00460     if (varndims != 1) {
00461       (void) fprintf(stderr, "Error NetCDF type and/or dimensions Line %d.\n", __LINE__);
00462       (void) banner(basename(argv[0]), "ABORT", "END");
00463       (void) abort();
00464     }
00465 
00466     /* Get time units attribute length */
00467     istat = nc_inq_attlen(ncinid_learn, time_learn_varid[i], "units", &t_len_learn);
00468     if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00469     /* Allocate required space before retrieving values */
00470     time_units_learn = (char *) malloc(t_len_learn * sizeof(char));
00471     if (time_units_learn == NULL) alloc_error(__FILE__, __LINE__);
00472     /* Get time units attribute value */
00473     istat = nc_get_att_text(ncinid_learn, time_learn_varid[i], "units", time_units_learn);
00474     if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00475     time_units_learn[t_len_learn-2] = '\0'; /* null terminate */
00476     
00477     /* Get calendar type attribute length */
00478     istat = nc_inq_attlen(ncinid_learn, time_learn_varid[i], "calendar", &t_len_learn);
00479     if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00480     /* Allocate required space before retrieving values */
00481     cal_type_learn = (char *) malloc(t_len_learn + 1);
00482     if (cal_type_learn == NULL) alloc_error(__FILE__, __LINE__);
00483     /* Get calendar type attribute value */
00484     istat = nc_get_att_text(ncinid_learn, time_learn_varid[i], "calendar", cal_type_learn);
00485     if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00486     cal_type_learn[t_len_learn] = '\0'; /* null terminate */
00487     
00488     /* Compute time info */
00489     year_learn[i] = (int *) malloc(ntime_learn[i] * sizeof(int));
00490     if (year_learn[i] == NULL) alloc_error(__FILE__, __LINE__);
00491     month_learn[i] = (int *) malloc(ntime_learn[i] * sizeof(int));
00492     if (month_learn[i] == NULL) alloc_error(__FILE__, __LINE__);
00493     day_learn[i] = (int *) malloc(ntime_learn[i] * sizeof(int));
00494     if (day_learn[i] == NULL) alloc_error(__FILE__, __LINE__);
00495     hour_learn[i] = (int *) malloc(ntime_learn[i] * sizeof(int));
00496     if (hour_learn[i] == NULL) alloc_error(__FILE__, __LINE__);
00497     min_learn[i] = (int *) malloc(ntime_learn[i] * sizeof(int));
00498     if (min_learn[i] == NULL) alloc_error(__FILE__, __LINE__);
00499     sec_learn[i] = (double *) malloc(ntime_learn[i] * sizeof(double));
00500     if (sec_learn[i] == NULL) alloc_error(__FILE__, __LINE__);
00501 
00502     dataunits_learn = ut_parse(unitSystem, time_units_learn, UT_ASCII);
00503 
00504     for (t=0; t<ntime_learn[i]; t++)
00505       istat = utCalendar2_cal(time_learn[i][t], dataunits_learn,
00506                               &(year_learn[i][t]), &(month_learn[i][t]), &(day_learn[i][t]), &(hour_learn[i][t]),
00507                               &(min_learn[i][t]), &(sec_learn[i][t]), cal_type_learn);
00508 
00509     (void) free(time_units_learn);
00510     (void) ut_free(dataunits_learn);
00511     (void) free(cal_type_learn);
00512 
00513 
00514   } /* Season loop */
00515 
00516   /* Compute time info */
00517 
00518   year = (int *) malloc(ntime * sizeof(int));
00519   if (year == NULL) alloc_error(__FILE__, __LINE__);
00520   month = (int *) malloc(ntime * sizeof(int));
00521   if (month == NULL) alloc_error(__FILE__, __LINE__);
00522   day = (int *) malloc(ntime * sizeof(int));
00523   if (day == NULL) alloc_error(__FILE__, __LINE__);
00524   hour = (int *) malloc(ntime * sizeof(int));
00525   if (hour == NULL) alloc_error(__FILE__, __LINE__);
00526   min = (int *) malloc(ntime * sizeof(int));
00527   if (min == NULL) alloc_error(__FILE__, __LINE__);
00528   sec = (double *) malloc(ntime * sizeof(double));
00529   if (sec == NULL) alloc_error(__FILE__, __LINE__);
00530 
00531   dataunits = ut_parse(unitSystem, time_units, UT_ASCII);
00532   for (t=0; t<ntime; t++)
00533     istat = utCalendar2_cal(timein[t], dataunits, &(year[t]), &(month[t]), &(day[t]), &(hour[t]), &(min[t]), &(sec[t]), cal_type);
00534   (void) free(time_units);
00535   (void) ut_free(dataunits);
00536   (void) free(cal_type);
00537 
00538   /* Extract subdomain */
00539   (void) extract_subdomain(&tas_sub, &lon_sub, &lat_sub, &nlon_sub, &nlat_sub, tas, lon, lat, minlon, maxlon, minlat, maxlat,
00540                            nlon, nlat, ntime);
00541 
00542   for (season=0; season<nseason_learn; season++) {
00543 
00544     (void) printf("Season=%d\n", season);
00545 
00546     /* Select sub period of field analyzed */
00547     /* Assuming season=0 is autumn and the rest in order, that is winter, spring, summer */
00548     (void) sub_period_common(&tas_subt, &ntime_sub, tas_sub, year, month, day, year_learn[season], month_learn[season], day_learn[season],
00549                              3, nlon_sub, nlat_sub, ntime, ntime_learn[season]);
00550 
00551     tas_smean = (double *) malloc(ntime * sizeof(double));
00552     if (tas_smean == NULL) alloc_error(__FILE__, __LINE__);
00553     (void) mean_field_spatial(tas_smean, tas_sub, (short int *) NULL, nlon_sub, nlat_sub, ntime);
00554     
00555     /* Compute mean and variance of spatially-averaged temperature */
00556     (void) mean_variance_field_spatial(&mean, &var, tas_subt, (short int *) NULL, nlon_sub, nlat_sub, ntime_sub);
00557 
00558     (void) printf("Season: %d  TAS mean=%lf variance=%lf\n", season, mean, var);
00559 
00560     (void) free(tas_subt);
00561     (void) free(tas_smean);
00562   }
00563 
00565   /* Close the intput netCDF file. */
00566   istat = ncclose(ncinid);
00567   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00568   istat = ncclose(ncinid_learn);
00569   if (istat != NC_NOERR) handle_netcdf_error(istat, __LINE__);
00570 
00571   (void) ut_free_system(unitSystem);  
00572 
00573   (void) free(time_learn_dimid);
00574   (void) free(time_learn_varid);
00575   (void) free(ntime_learn);
00576   for (i=0; i<nseason_learn; i++) {
00577     (void) free(time_learn[i]);
00578     (void) free(year_learn[i]);
00579     (void) free(month_learn[i]);
00580     (void) free(day_learn[i]);
00581     (void) free(hour_learn[i]);
00582     (void) free(min_learn[i]);
00583     (void) free(sec_learn[i]);
00584   }
00585   (void) free(time_learn);
00586   (void) free(year_learn);
00587   (void) free(month_learn);
00588   (void) free(day_learn);
00589   (void) free(hour_learn);
00590   (void) free(min_learn);
00591   (void) free(sec_learn);
00592 
00593   (void) free(tas);
00594   (void) free(tas_sub);
00595   (void) free(lon);
00596   (void) free(lat);
00597   (void) free(lon_sub);
00598   (void) free(lat_sub);
00599   (void) free(timein);
00600   (void) free(filein);
00601   (void) free(filein_learn);
00602 
00603   /* Print END banner */
00604   (void) banner(basename(argv[0]), "OK", "END");
00605 
00606   return 0;
00607 }
00608 
00609 
00613 void show_usage(char *pgm) {
00618   (void) fprintf(stderr, "%s: usage:\n", pgm);
00619   (void) fprintf(stderr, "-i: input NetCDF file\n");
00620   (void) fprintf(stderr, "-o: output NetCDF file\n");
00621   (void) fprintf(stderr, "-h: help\n");
00622 
00623 }
00624 
00625 /* Handle error */
00626 void handle_netcdf_error(int status, int lineno)
00627 {
00628   if (status != NC_NOERR) {
00629     fprintf(stderr, "Line: %d Error %d: %s\n", lineno, status, nc_strerror(status));
00630     exit(-1);
00631   }
00632 }

Generated on 12 May 2016 for DSCLIM by  doxygen 1.6.1