read_analog_data.c

Go to the documentation of this file.
00001 /* ***************************************************** */
00002 /* read_analog_data Read analog data.                    */
00003 /* read_analog_data.c                                    */
00004 /* ***************************************************** */
00005 /* Author: Christian Page, CERFACS, Toulouse, France.    */
00006 /* ***************************************************** */
00007 /* Date of creation: feb 2009                            */
00008 /* Last date of modification: feb 2009                   */
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
00066 read_analog_data(analog_day_struct *analog_days, double **delta, double **time_ls,
00067                  char *filename, char *timename) {
00076   int istat; /* Diagnostic status */
00077 
00078   int ncinid; /* NetCDF input file handle ID */
00079   int timediminid; /* NetCDF time dimension output ID */
00080   int timeinid; /* NetCDF time variable ID */
00081   int atimeinid; /* NetCDF analog dates variable ID */
00082   int ayearinid; /* NetCDF analog dates variable ID */
00083   int amonthinid; /* NetCDF analog dates variable ID */
00084   int adayinid; /* NetCDF analog dates variable ID */
00085   int dyearinid; /* NetCDF downscaled dates variable ID */
00086   int dmonthinid; /* NetCDF downscaled dates variable ID */
00087   int ddayinid; /* NetCDF downscaled dates variable ID */
00088   int deltatinid; /* NetCDF delta T variable ID */
00089     
00090   size_t start[1]; /* Start element when reading */
00091   size_t count[1]; /* Count of elements to read */
00092   size_t ntime; /* Time dimension length */
00093 
00094   istat = nc_open(filename, NC_NOWRITE, &ncinid);  /* open for reading */
00095   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00096 
00097   istat = nc_inq_dimid(ncinid, timename, &timediminid);  /* get ID for time dimension */
00098   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00099   istat = nc_inq_dimlen(ncinid, timediminid, &ntime); /* get time dimension length */
00100   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00101   analog_days->ntime = ntime;
00102 
00103   istat = nc_inq_varid(ncinid, timename, &timeinid); /* get time variable ID */
00104   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00105 
00106   istat = nc_inq_varid(ncinid, "downscaled_date_year", &dyearinid); /* get variable ID */
00107   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00108   istat = nc_inq_varid(ncinid, "downscaled_date_month", &dmonthinid); /* get variable ID */
00109   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00110   istat = nc_inq_varid(ncinid, "downscaled_date_day", &ddayinid); /* get variable ID */
00111   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00112 
00113   istat = nc_inq_varid(ncinid, "analog_date", &atimeinid); /* get variable ID */
00114   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00115   istat = nc_inq_varid(ncinid, "analog_date_year", &ayearinid); /* get variable ID */
00116   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00117   istat = nc_inq_varid(ncinid, "analog_date_month", &amonthinid); /* get variable ID */
00118   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00119   istat = nc_inq_varid(ncinid, "analog_date_day", &adayinid); /* get variable ID */
00120   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00121 
00122   istat = nc_inq_varid(ncinid, "analog_delta_t", &deltatinid); /* get variable ID */
00123   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00124 
00125   /* Allocate memory and set start and count */
00126   start[0] = 0;
00127   count[0] = (size_t) analog_days->ntime;
00128 
00129   /* Read values from netCDF variables */
00130 
00131   analog_days->year_s = (int *) malloc(analog_days->ntime * sizeof(int));
00132   if (analog_days->year_s == NULL) alloc_error(__FILE__, __LINE__);
00133   istat = nc_get_vara_int(ncinid, dyearinid, start, count, analog_days->year_s);
00134   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00135 
00136   analog_days->month_s = (int *) malloc(analog_days->ntime * sizeof(int));
00137   if (analog_days->month_s == NULL) alloc_error(__FILE__, __LINE__);
00138   istat = nc_get_vara_int(ncinid, dmonthinid, start, count, analog_days->month_s);
00139   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00140 
00141   analog_days->day_s = (int *) malloc(analog_days->ntime * sizeof(int));
00142   if (analog_days->day_s == NULL) alloc_error(__FILE__, __LINE__);
00143   istat = nc_get_vara_int(ncinid, ddayinid, start, count, analog_days->day_s);
00144   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00145 
00146   analog_days->year = (int *) malloc(analog_days->ntime * sizeof(int));
00147   if (analog_days->year == NULL) alloc_error(__FILE__, __LINE__);
00148   istat = nc_get_vara_int(ncinid, ayearinid, start, count, analog_days->year);
00149   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00150 
00151   analog_days->month = (int *) malloc(analog_days->ntime * sizeof(int));
00152   if (analog_days->month == NULL) alloc_error(__FILE__, __LINE__);
00153   istat = nc_get_vara_int(ncinid, amonthinid, start, count, analog_days->month);
00154   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00155 
00156   analog_days->day = (int *) malloc(analog_days->ntime * sizeof(int));
00157   if (analog_days->day == NULL) alloc_error(__FILE__, __LINE__);
00158   istat = nc_get_vara_int(ncinid, adayinid, start, count, analog_days->day);
00159   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00160 
00161   (*delta) = (double *) malloc(analog_days->ntime * sizeof(double));
00162   if ((*delta) == NULL) alloc_error(__FILE__, __LINE__);
00163   istat = nc_get_vara_double(ncinid, deltatinid, start, count, *delta);
00164   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00165 
00166   (*time_ls) = (double *) malloc(analog_days->ntime * sizeof(double));
00167   if ((*time_ls) == NULL) alloc_error(__FILE__, __LINE__);
00168   istat = nc_get_vara_double(ncinid, timeinid, start, count, *time_ls);
00169   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00170 
00171   /* Close the input netCDF file. */
00172   istat = ncclose(ncinid);
00173   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00174 }

Generated on 12 May 2016 for DSCLIM by  doxygen 1.6.1