write_regression_fields.c

Go to the documentation of this file.
00001 /* ***************************************************** */
00002 /* Write regression-related downscaling fields for       */
00003 /* diagnostics use.                                      */
00004 /* write_regression_fields.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 #include <dsclim.h>
00056 
00058 int
00059 write_regression_fields(data_struct *data, char *filename, double **timeval, int *ntime, double **precip_index, double **distclust,
00060                         double **sup_index) {
00071   int istat; /* Diagnostic status */
00072 
00073   int ncoutid; /* NetCDF output file handle ID */
00074   int *timedimoutid; /* NetCDF time dimension output ID */
00075   int sdimoutid; /* NetCDF season dimension output ID */
00076   int ptsdimoutid; /* NetCDF points dimension output ID */
00077   int *clustdimoutid; /* NetCDF clusters dimension output ID */
00078   int *timeoutid; /* NetCDF time variable ID */
00079   int *cstoutid; /* NetCDF regression constant variable ID */
00080   int *regoutid; /* NetCDF regression coefficients variable ID */
00081   int *rrdoutid; /* NetCDF precipitation index variable ID */
00082   int *distclustoutid; /* NetCDF distances to clusters variable ID */
00083   int *taoutid; /* NetCDF secondary large-scale field index variable ID */
00084   int vardimids[NC_MAX_VAR_DIMS]; /* NetCDF dimension IDs */
00085   
00086   size_t start[3]; /* Start element when writing */
00087   size_t count[3]; /* Count of elements to write */
00088 
00089   char *tmpstr = NULL; /* Temporary string */
00090 
00091   double fillvalue;
00092   char *nomvar = NULL;
00093 
00094   int s;
00095 
00096   timedimoutid = (int *) malloc(data->conf->nseasons * sizeof(int));
00097   if (timedimoutid == NULL) alloc_error(__FILE__, __LINE__);
00098   clustdimoutid = (int *) malloc(data->conf->nseasons * sizeof(int));
00099   if (clustdimoutid == NULL) alloc_error(__FILE__, __LINE__);
00100 
00101   timeoutid = (int *) malloc(data->conf->nseasons * sizeof(int));
00102   if (timeoutid == NULL) alloc_error(__FILE__, __LINE__);
00103   cstoutid = (int *) malloc(data->conf->nseasons * sizeof(int));
00104   if (cstoutid == NULL) alloc_error(__FILE__, __LINE__);
00105   regoutid = (int *) malloc(data->conf->nseasons * sizeof(int));
00106   if (regoutid == NULL) alloc_error(__FILE__, __LINE__);
00107   rrdoutid = (int *) malloc(data->conf->nseasons * sizeof(int));
00108   if (rrdoutid == NULL) alloc_error(__FILE__, __LINE__);
00109   distclustoutid = (int *) malloc(data->conf->nseasons * sizeof(int));
00110   if (distclustoutid == NULL) alloc_error(__FILE__, __LINE__);
00111   taoutid = (int *) malloc(data->conf->nseasons * sizeof(int));
00112   if (taoutid == NULL) alloc_error(__FILE__, __LINE__);
00113 
00114   nomvar = (char *) malloc(200 * sizeof(char));
00115   if (nomvar == NULL) alloc_error(__FILE__, __LINE__);
00116   tmpstr = (char *) malloc(200 * sizeof(char));
00117   if (tmpstr == NULL) alloc_error(__FILE__, __LINE__);
00118 
00119   /* Open NetCDF file for writing, overwrite and truncate existing file if any */
00120   istat = nc_create(filename, NC_CLOBBER, &ncoutid);
00121   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00122 
00123   /* Set global attributes */
00124   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "processor", strlen(data->info->processor), data->info->processor);
00125   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "software", strlen(data->info->software), data->info->software);
00126   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "institution", strlen(data->info->institution), data->info->institution);
00127   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "creator_email", strlen(data->info->creator_email), data->info->creator_email);
00128   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "creator_url", strlen(data->info->creator_url), data->info->creator_url);
00129   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "creator_name", strlen(data->info->creator_name), data->info->creator_name);
00130   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "contact_email", strlen(data->info->contact_email), data->info->contact_email);
00131   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "contact_name", strlen(data->info->contact_name), data->info->contact_name);
00132   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "other_contact_email", strlen(data->info->other_contact_email),
00133                           data->info->other_contact_email);
00134   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "other_contact_name", strlen(data->info->other_contact_name),
00135                           data->info->other_contact_name);
00136 
00137   fillvalue = -9999.9;
00138 
00139   /* Set dimensions */
00140   istat = nc_def_dim(ncoutid, "season", data->conf->nseasons, &sdimoutid);
00141   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00142   istat = nc_def_dim(ncoutid, data->conf->ptsname, data->reg->npts, &ptsdimoutid);
00143   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00144   for (s=0; s<data->conf->nseasons; s++) {
00145 
00146     /* Define time dimensions and variables */
00147     (void) sprintf(nomvar, "%s_%d", data->reg->timename, s+1);
00148     istat = nc_def_dim(ncoutid, nomvar, ntime[s], &(timedimoutid[s]));
00149     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00150 
00151     vardimids[0] = timedimoutid[s];
00152     istat = nc_def_var(ncoutid, nomvar, NC_INT, 1, vardimids, &(timeoutid[s]));
00153     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00154       
00155     istat = sprintf(tmpstr, "gregorian");
00156     istat = nc_put_att_text(ncoutid, timeoutid[s], "calendar", strlen(tmpstr), tmpstr);
00157     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00158     istat = sprintf(tmpstr, "%s", data->conf->time_units);
00159     istat = nc_put_att_text(ncoutid, timeoutid[s], "units", strlen(tmpstr), tmpstr);
00160     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00161     istat = sprintf(tmpstr, "time in %s", data->conf->time_units);
00162     istat = nc_put_att_text(ncoutid, timeoutid[s], "long_name", strlen(tmpstr), tmpstr);
00163     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00164 
00165     /* Define cluster dimensions */
00166     (void) sprintf(nomvar, "%s_%d", data->conf->clustname, s+1);
00167     istat = nc_def_dim(ncoutid, nomvar, data->conf->season[s].nclusters, &(clustdimoutid[s]));
00168     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00169 
00170     /* Define regression constant variables */
00171     (void) sprintf(nomvar, "%s_%d", data->learning->nomvar_precip_reg_cst, s+1);
00172     vardimids[0] = ptsdimoutid;
00173     istat = nc_def_var(ncoutid, nomvar, NC_DOUBLE, 1, vardimids, &(cstoutid[s]));
00174     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00175 
00176     istat = nc_put_att_double(ncoutid, cstoutid[s], "missing_value", NC_DOUBLE, 1, &fillvalue);
00177     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00178     istat = nc_put_att_text(ncoutid, cstoutid[s], "coordinates", strlen(data->conf->ptsname), data->conf->ptsname);
00179     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00180     istat = sprintf(tmpstr, "none");
00181     istat = nc_put_att_text(ncoutid, cstoutid[s], "units", strlen(tmpstr), tmpstr);
00182     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00183 
00184     /* Define regression coefficients variables */
00185     (void) sprintf(nomvar, "%s_%d", data->learning->nomvar_precip_reg, s+1);
00186     vardimids[0] = clustdimoutid[s];
00187     vardimids[1] = ptsdimoutid;
00188     istat = nc_def_var(ncoutid, nomvar, NC_DOUBLE, 2, vardimids, &(regoutid[s]));
00189     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00190 
00191     istat = nc_put_att_double(ncoutid, regoutid[s], "missing_value", NC_DOUBLE, 1, &fillvalue);
00192     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00193     istat = sprintf(tmpstr, "%s %s_%d", data->conf->ptsname, data->learning->nomvar_class_clusters, s+1);
00194     istat = nc_put_att_text(ncoutid, regoutid[s], "coordinates", strlen(tmpstr), tmpstr);
00195     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00196     istat = sprintf(tmpstr, "none");
00197     istat = nc_put_att_text(ncoutid, regoutid[s], "units", strlen(tmpstr), tmpstr);
00198     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00199 
00200     /* Define precipitation index variables */
00201     (void) sprintf(nomvar, "%s_%d", data->learning->nomvar_precip_index, s+1);
00202     vardimids[0] = timedimoutid[s];
00203     vardimids[1] = ptsdimoutid;
00204     istat = nc_def_var(ncoutid, nomvar, NC_DOUBLE, 2, vardimids, &(rrdoutid[s]));
00205     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00206 
00207     istat = nc_put_att_double(ncoutid, rrdoutid[s], "missing_value", NC_DOUBLE, 1, &fillvalue);
00208     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00209     istat = sprintf(tmpstr, "%s %s_%d", data->conf->ptsname, data->reg->timename, s+1);
00210     istat = nc_put_att_text(ncoutid, rrdoutid[s], "coordinates", strlen(tmpstr), tmpstr);
00211     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00212     istat = sprintf(tmpstr, "none");
00213     istat = nc_put_att_text(ncoutid, rrdoutid[s], "units", strlen(tmpstr), tmpstr);
00214     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00215 
00216     /* Define sup_index (secondary large-scale field index for learning period) */
00217     (void) sprintf(nomvar, "%s_%d", data->learning->nomvar_sup_index, s+1);
00218     vardimids[0] = timedimoutid[s];
00219     istat = nc_def_var(ncoutid, nomvar, NC_DOUBLE, 1, vardimids, &(taoutid[s]));
00220     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00221 
00222     istat = nc_put_att_double(ncoutid, taoutid[s], "missing_value", NC_DOUBLE, 1, &fillvalue);
00223     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00224     istat = sprintf(tmpstr, "%s_%d", data->reg->timename, s+1);
00225     istat = nc_put_att_text(ncoutid, taoutid[s], "coordinates", strlen(tmpstr), tmpstr);
00226     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00227     istat = sprintf(tmpstr, "none");
00228     istat = nc_put_att_text(ncoutid, taoutid[s], "units", strlen(tmpstr), tmpstr);
00229     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00230 
00231     /* Define distances to clusters variable */
00232     (void) sprintf(nomvar, "cluster_distance_%d", s+1);
00233     vardimids[0] = clustdimoutid[s];
00234     vardimids[1] = timedimoutid[s];
00235     istat = nc_def_var(ncoutid, nomvar, NC_DOUBLE, 2, vardimids, &(distclustoutid[s]));
00236     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00237 
00238     istat = nc_put_att_double(ncoutid, distclustoutid[s], "missing_value", NC_DOUBLE, 1, &fillvalue);
00239     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00240     istat = sprintf(tmpstr, "%s_%d %s_%d", data->reg->timename, s+1, data->conf->clustname, s+1);
00241     istat = nc_put_att_text(ncoutid, distclustoutid[s], "coordinates", strlen(tmpstr), tmpstr);
00242     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00243     istat = sprintf(tmpstr, "none");
00244     istat = nc_put_att_text(ncoutid, distclustoutid[s], "units", strlen(tmpstr), tmpstr);
00245     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00246 
00247   }
00248 
00249   /* End definition mode */
00250   istat = nc_enddef(ncoutid);
00251   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00252 
00253   /* Write variables */
00254   for (s=0; s<data->conf->nseasons; s++) {
00255 
00256     /* Write time */
00257     start[0] = 0;
00258     start[1] = 0;
00259     start[2] = 0;
00260     count[0] = (size_t) ntime[s];
00261     count[1] = 0;
00262     count[2] = 0;
00263     istat = nc_put_vara_double(ncoutid, timeoutid[s], start, count, timeval[s]);
00264     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00265 
00266     /* Write regression constants */
00267     start[0] = 0;
00268     start[1] = 0;
00269     start[2] = 0;
00270     count[0] = (size_t) data->reg->npts;
00271     count[1] = 0;
00272     count[2] = 0;
00273     istat = nc_put_vara_double(ncoutid, cstoutid[s], start, count, data->learning->data[s].precip_reg_cst);
00274     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00275 
00276     /* Write regressions coefficients */
00277     start[0] = 0;
00278     start[1] = 0;
00279     start[2] = 0;
00280     count[0] = (size_t) data->conf->season[s].nclusters;
00281     count[1] = (size_t) data->reg->npts;
00282     count[2] = 0;
00283     istat = nc_put_vara_double(ncoutid, regoutid[s], start, count, data->learning->data[s].precip_reg);
00284     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00285 
00286     /* Write reconstructed precipitation index */
00287     start[0] = 0;
00288     start[1] = 0;
00289     start[2] = 0;
00290     count[0] = (size_t) ntime[s];
00291     count[1] = (size_t) data->reg->npts;
00292     count[2] = 0;
00293     istat = nc_put_vara_double(ncoutid, rrdoutid[s], start, count, precip_index[s]);
00294     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00295 
00296     /* Write secondary field index */
00297     start[0] = 0;
00298     start[1] = 0;
00299     start[2] = 0;
00300     count[0] = (size_t) ntime[s];
00301     count[1] = 0;
00302     count[2] = 0;
00303     istat = nc_put_vara_double(ncoutid, taoutid[s], start, count, sup_index[s]);
00304     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00305 
00306     /* Write distances to clusters */
00307     start[0] = 0;
00308     start[1] = 0;
00309     start[2] = 0;
00310     count[0] = (size_t) data->conf->season[s].nclusters;
00311     count[1] = (size_t) ntime[s];
00312     count[2] = 0;
00313     istat = nc_put_vara_double(ncoutid, distclustoutid[s], start, count, distclust[s]);
00314     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00315 
00316   }
00317 
00318   /* Close the output netCDF file */
00319   istat = ncclose(ncoutid);
00320   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00321     
00322   (void) free(nomvar);
00323 
00324   (void) free(timedimoutid);
00325   (void) free(clustdimoutid);
00326 
00327   (void) free(timeoutid);
00328   (void) free(cstoutid);
00329   (void) free(regoutid);
00330   (void) free(rrdoutid);
00331   (void) free(distclustoutid);
00332   (void) free(taoutid);
00333 
00334   (void) free(tmpstr);
00335 
00336   return 0;
00337 }

Generated on 12 May 2016 for DSCLIM by  doxygen 1.6.1