save_analog_data.c

Go to the documentation of this file.
00001 /* ***************************************************** */
00002 /* Save analog data information for further use.         */
00003 /* save_analog_data.c                                    */
00004 /* ***************************************************** */
00005 /* Author: Christian Page, CERFACS, Toulouse, France.    */
00006 /* ***************************************************** */
00011 /* LICENSE BEGIN
00012 
00013 Copyright Cerfacs (Christian Page) (2015)
00014 
00015 christian.page@cerfacs.fr
00016 
00017 This software is a computer program whose purpose is to downscale climate
00018 scenarios using a statistical methodology based on weather regimes.
00019 
00020 This software is governed by the CeCILL license under French law and
00021 abiding by the rules of distribution of free software. You can use, 
00022 modify and/ or redistribute the software under the terms of the CeCILL
00023 license as circulated by CEA, CNRS and INRIA at the following URL
00024 "http://www.cecill.info". 
00025 
00026 As a counterpart to the access to the source code and rights to copy,
00027 modify and redistribute granted by the license, users are provided only
00028 with a limited warranty and the software's author, the holder of the
00029 economic rights, and the successive licensors have only limited
00030 liability. 
00031 
00032 In this respect, the user's attention is drawn to the risks associated
00033 with loading, using, modifying and/or developing or reproducing the
00034 software by the user in light of its specific status of free software,
00035 that may mean that it is complicated to manipulate, and that also
00036 therefore means that it is reserved for developers and experienced
00037 professionals having in-depth computer knowledge. Users are therefore
00038 encouraged to load and test the software's suitability as regards their
00039 requirements in conditions enabling the security of their systems and/or 
00040 data to be ensured and, more generally, to use and operate it in the 
00041 same conditions as regards security. 
00042 
00043 The fact that you are presently reading this means that you have had
00044 knowledge of the CeCILL license and that you accept its terms.
00045 
00046 LICENSE END */
00047 
00048 
00049 
00050 
00051 
00052 
00053 
00054 #include <dsclim.h>
00055 
00057 void
00058 save_analog_data(analog_day_struct analog_days, double *delta, double **delta_dayschoice, double *dist, int *cluster, double *time_ls,
00059                  char *filename, data_struct *data) {
00071   int istat; /* Diagnostic status */
00072 
00073   int ncoutid; /* NetCDF output file handle ID */
00074   int timedimoutid; /* NetCDF time dimension output ID */
00075   int timeoutid; /* NetCDF time variable ID */
00076   int ndayschoicedimoutid; /* NetCDF ndayschoice dimension output ID */
00077   int ndayschoiceoutid; /* NetCDF ndayschoice variable ID */
00078   int analogoutid; /* NetCDF analog dates variable ID */
00079   int analogyearoutid; /* NetCDF analog dates variable ID */
00080   int analogmonthoutid; /* NetCDF analog dates variable ID */
00081   int analogdayoutid; /* NetCDF analog dates variable ID */
00082   int analogyearndaysoutid; /* NetCDF analog ndayschoice dates variable ID */
00083   int analogmonthndaysoutid; /* NetCDF analog ndayschoice dates variable ID */
00084   int analogdayndaysoutid; /* NetCDF analog ndayschoice dates variable ID */
00085   int metricoutid; /* NetCDF analog normalized metric variable ID */
00086   int downscaledyearoutid; /* NetCDF downscaled dates variable ID */
00087   int downscaledmonthoutid; /* NetCDF downscaled dates variable ID */
00088   int downscaleddayoutid; /* NetCDF downscaled dates variable ID */
00089   int distoutid; /* NetCDF cluster distance variable ID */
00090   int clusteroutid; /* NetCDF cluster number variable ID */
00091   int deltatoutid; /* NetCDF delta T variable ID */
00092   int deltatndaysoutid; /* NetCDF delta T ndayschoice variable ID */
00093   int vardimids[NC_MAX_VAR_DIMS]; /* NetCDF dimension IDs */
00094   
00095   int *buftmp = NULL; /* Temporary int buffer for writing data */
00096   float *buftmpf = NULL; /* Temporary float buffer for writing data */
00097   //  double *buftmpd = NULL; /* Temporary double buffer for writing data */
00098   int maxndays; /* Maximum number of days selected for any particular date */
00099     
00100   size_t start[2]; /* Start element when writing */
00101   size_t count[2]; /* Count of elements to write */  
00102 
00103   int fillvaluei; /* Missing value */
00104   float fillvaluef; /* Missing value */
00105 
00106   char *tmpstr = NULL; /* Temporary string */
00107 
00108   int t; /* Time loop counter */
00109   int i; /* Loop counter */
00110 
00111   tmpstr = (char *) malloc(200 * sizeof(char));
00112   if (tmpstr == NULL) alloc_error(__FILE__, __LINE__);
00113 
00114   /* Open NetCDF file for writing, overwrite and truncate existing file if any */
00115   istat = nc_create(filename, NC_CLOBBER, &ncoutid);
00116   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00117 
00118   /* Set global attributes */
00119   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "processor", strlen(data->info->processor), data->info->processor);
00120   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "software", strlen(data->info->software), data->info->software);
00121   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "institution", strlen(data->info->institution), data->info->institution);
00122   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "creator_email", strlen(data->info->creator_email), data->info->creator_email);
00123   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "creator_url", strlen(data->info->creator_url), data->info->creator_url);
00124   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "creator_name", strlen(data->info->creator_name), data->info->creator_name);
00125   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "contact_email", strlen(data->info->contact_email), data->info->contact_email);
00126   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "contact_name", strlen(data->info->contact_name), data->info->contact_name);
00127   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "other_contact_email", strlen(data->info->other_contact_email),
00128                           data->info->other_contact_email);
00129   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "other_contact_name", strlen(data->info->other_contact_name),
00130                           data->info->other_contact_name);
00131 
00132   /* Set dimensions */
00133   istat = nc_def_dim(ncoutid, "time", NC_UNLIMITED, &timedimoutid);
00134   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00135   
00136   vardimids[0] = timedimoutid;
00137   istat = nc_def_var(ncoutid, "time", NC_INT, 1, vardimids, &timeoutid);
00138   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00139   
00140   istat = sprintf(tmpstr, "gregorian");
00141   istat = nc_put_att_text(ncoutid, timeoutid, "calendar", strlen(tmpstr), tmpstr);
00142   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00143   istat = sprintf(tmpstr, "%s", data->conf->time_units);
00144   istat = nc_put_att_text(ncoutid, timeoutid, "units", strlen(tmpstr), tmpstr);
00145   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00146   istat = sprintf(tmpstr, "time in %s", data->conf->time_units);
00147   istat = nc_put_att_text(ncoutid, timeoutid, "long_name", strlen(tmpstr), tmpstr);
00148   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00149 
00150   /* Find maximum number of days in the first selection of analog days to have constant dimension size */
00151   maxndays = analog_days.ndayschoice[0];
00152   for (t=0; t<analog_days.ntime; t++)
00153     if (maxndays < analog_days.ndayschoice[t])
00154       maxndays = analog_days.ndayschoice[t];
00155   istat = nc_def_dim(ncoutid, "ndayschoice", maxndays, &ndayschoicedimoutid);
00156   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00157 
00158   vardimids[0] = timedimoutid;
00159   istat = nc_def_var(ncoutid, "ndayschoice", NC_INT, 1, vardimids, &ndayschoiceoutid);
00160   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00161 
00162   istat = sprintf(tmpstr, "Number of analog days selected");
00163   istat = nc_put_att_text(ncoutid, ndayschoiceoutid, "long_name", strlen(tmpstr), tmpstr);
00164   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00165 
00166   /* Set variables */
00167 
00168   /* Define downscaled day variable: year */
00169   vardimids[0] = timedimoutid;
00170   istat = nc_def_var(ncoutid, "downscaled_date_year", NC_INT, 1, vardimids, &downscaledyearoutid);
00171   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00172 
00173   fillvaluei = -1;
00174   istat = nc_put_att_int(ncoutid, downscaledyearoutid, "missing_value", NC_INT, 1, &fillvaluei);
00175   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00176   (void) sprintf(tmpstr, "time");
00177   istat = nc_put_att_text(ncoutid, downscaledyearoutid, "coordinates", strlen(tmpstr), tmpstr);
00178   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00179   (void) sprintf(tmpstr, "%s", "year");
00180   istat = nc_put_att_text(ncoutid, downscaledyearoutid, "units", strlen(tmpstr), tmpstr);
00181   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00182   (void) strcpy(tmpstr, "Downscaled date: year");
00183   istat = nc_put_att_text(ncoutid, downscaledyearoutid, "long_name", strlen(tmpstr), tmpstr);
00184   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00185 
00186   /* Define downscaled day variable: month */
00187   vardimids[0] = timedimoutid;
00188   istat = nc_def_var(ncoutid, "downscaled_date_month", NC_INT, 1, vardimids, &downscaledmonthoutid);
00189   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00190 
00191   fillvaluei = -1;
00192   istat = nc_put_att_int(ncoutid, downscaledmonthoutid, "missing_value", NC_INT, 1, &fillvaluei);
00193   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00194   (void) sprintf(tmpstr, "time");
00195   istat = nc_put_att_text(ncoutid, downscaledmonthoutid, "coordinates", strlen(tmpstr), tmpstr);
00196   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00197   (void) sprintf(tmpstr, "%s", "month");
00198   istat = nc_put_att_text(ncoutid, downscaledmonthoutid, "units", strlen(tmpstr), tmpstr);
00199   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00200   (void) strcpy(tmpstr, "Downscaled date: month");
00201   istat = nc_put_att_text(ncoutid, downscaledmonthoutid, "long_name", strlen(tmpstr), tmpstr);
00202   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00203 
00204   /* Define downscaled day variable: day */
00205   vardimids[0] = timedimoutid;
00206   istat = nc_def_var(ncoutid, "downscaled_date_day", NC_INT, 1, vardimids, &downscaleddayoutid);
00207   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00208 
00209   fillvaluei = -1;
00210   istat = nc_put_att_int(ncoutid, downscaleddayoutid, "missing_value", NC_INT, 1, &fillvaluei);
00211   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00212   (void) sprintf(tmpstr, "time");
00213   istat = nc_put_att_text(ncoutid, downscaleddayoutid, "coordinates", strlen(tmpstr), tmpstr);
00214   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00215   (void) sprintf(tmpstr, "%s", "day");
00216   istat = nc_put_att_text(ncoutid, downscaleddayoutid, "units", strlen(tmpstr), tmpstr);
00217   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00218   (void) strcpy(tmpstr, "Downscaled date: day");
00219   istat = nc_put_att_text(ncoutid, downscaleddayoutid, "long_name", strlen(tmpstr), tmpstr);
00220   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00221 
00222   /* Define analog day time variable */
00223   vardimids[0] = timedimoutid;
00224   istat = nc_def_var(ncoutid, "analog_date", NC_INT, 1, vardimids, &analogoutid);
00225   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00226 
00227   fillvaluei = -1;
00228   istat = nc_put_att_int(ncoutid, analogoutid, "missing_value", NC_INT, 1, &fillvaluei);
00229   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00230   (void) sprintf(tmpstr, "time");
00231   istat = nc_put_att_text(ncoutid, analogoutid, "coordinates", strlen(tmpstr), tmpstr);
00232   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00233   (void) sprintf(tmpstr, "%s", data->conf->time_units);
00234   istat = nc_put_att_text(ncoutid, analogoutid, "units", strlen(tmpstr), tmpstr);
00235   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00236   (void) strcpy(tmpstr, "Analog date");
00237   istat = nc_put_att_text(ncoutid, analogoutid, "long_name", strlen(tmpstr), tmpstr);
00238   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00239 
00240   /* Define analog day variable: year */
00241   vardimids[0] = timedimoutid;
00242   istat = nc_def_var(ncoutid, "analog_date_year", NC_INT, 1, vardimids, &analogyearoutid);
00243   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00244 
00245   fillvaluei = -1;
00246   istat = nc_put_att_int(ncoutid, analogyearoutid, "missing_value", NC_INT, 1, &fillvaluei);
00247   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00248   (void) sprintf(tmpstr, "time");
00249   istat = nc_put_att_text(ncoutid, analogyearoutid, "coordinates", strlen(tmpstr), tmpstr);
00250   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00251   (void) sprintf(tmpstr, "%s", "year");
00252   istat = nc_put_att_text(ncoutid, analogyearoutid, "units", strlen(tmpstr), tmpstr);
00253   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00254   (void) strcpy(tmpstr, "Analog date: year");
00255   istat = nc_put_att_text(ncoutid, analogyearoutid, "long_name", strlen(tmpstr), tmpstr);
00256   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00257 
00258   /* Define analog day variable: month */
00259   vardimids[0] = timedimoutid;
00260   istat = nc_def_var(ncoutid, "analog_date_month", NC_INT, 1, vardimids, &analogmonthoutid);
00261   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00262 
00263   fillvaluei = -1;
00264   istat = nc_put_att_int(ncoutid, analogmonthoutid, "missing_value", NC_INT, 1, &fillvaluei);
00265   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00266   (void) sprintf(tmpstr, "time");
00267   istat = nc_put_att_text(ncoutid, analogmonthoutid, "coordinates", strlen(tmpstr), tmpstr);
00268   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00269   (void) sprintf(tmpstr, "%s", "month");
00270   istat = nc_put_att_text(ncoutid, analogmonthoutid, "units", strlen(tmpstr), tmpstr);
00271   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00272   (void) strcpy(tmpstr, "Analog date: month");
00273   istat = nc_put_att_text(ncoutid, analogmonthoutid, "long_name", strlen(tmpstr), tmpstr);
00274   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00275 
00276   /* Define analog day variable: day */
00277   vardimids[0] = timedimoutid;
00278   istat = nc_def_var(ncoutid, "analog_date_day", NC_INT, 1, vardimids, &analogdayoutid);
00279   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00280 
00281   fillvaluei = -1;
00282   istat = nc_put_att_int(ncoutid, analogdayoutid, "missing_value", NC_INT, 1, &fillvaluei);
00283   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00284   (void) sprintf(tmpstr, "time");
00285   istat = nc_put_att_text(ncoutid, analogdayoutid, "coordinates", strlen(tmpstr), tmpstr);
00286   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00287   (void) sprintf(tmpstr, "%s", "day");
00288   istat = nc_put_att_text(ncoutid, analogdayoutid, "units", strlen(tmpstr), tmpstr);
00289   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00290   (void) strcpy(tmpstr, "Analog date: day");
00291   istat = nc_put_att_text(ncoutid, analogdayoutid, "long_name", strlen(tmpstr), tmpstr);
00292   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00293 
00294   /* Define analog delta Temperature variable */
00295   vardimids[0] = timedimoutid;
00296   istat = nc_def_var(ncoutid, "analog_delta_t", NC_FLOAT, 1, vardimids, &deltatoutid);
00297   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00298 
00299   fillvaluef = -9999.0;
00300   istat = nc_put_att_float(ncoutid, deltatoutid, "missing_value", NC_FLOAT, 1, &fillvaluef);
00301   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00302   (void) sprintf(tmpstr, "time");
00303   istat = nc_put_att_text(ncoutid, deltatoutid, "coordinates", strlen(tmpstr), tmpstr);
00304   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00305   (void) strcpy(tmpstr, "K");
00306   istat = nc_put_att_text(ncoutid, deltatoutid, "units", strlen(tmpstr), tmpstr);
00307   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00308   (void) strcpy(tmpstr, "Delta of Temperature");
00309   istat = nc_put_att_text(ncoutid, deltatoutid, "long_name", strlen(tmpstr), tmpstr);
00310   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00311   
00312   /* Define analog delta Temperature ndayschoice variable */
00313   vardimids[0] = timedimoutid;
00314   vardimids[1] = ndayschoicedimoutid;
00315   istat = nc_def_var(ncoutid, "analog_ndays_delta_t", NC_FLOAT, 2, vardimids, &deltatndaysoutid);
00316   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00317 
00318   fillvaluef = -9999.0;
00319   istat = nc_put_att_float(ncoutid, deltatndaysoutid, "missing_value", NC_FLOAT, 1, &fillvaluef);
00320   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00321   (void) sprintf(tmpstr, "time ndayschoice");
00322   istat = nc_put_att_text(ncoutid, deltatndaysoutid, "coordinates", strlen(tmpstr), tmpstr);
00323   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00324   (void) strcpy(tmpstr, "K");
00325   istat = nc_put_att_text(ncoutid, deltatndaysoutid, "units", strlen(tmpstr), tmpstr);
00326   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00327   (void) strcpy(tmpstr, "Delta of Temperature ndays");
00328   istat = nc_put_att_text(ncoutid, deltatndaysoutid, "long_name", strlen(tmpstr), tmpstr);
00329   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00330   
00331   /* Define cluster distance variable */
00332   vardimids[0] = timedimoutid;
00333   istat = nc_def_var(ncoutid, "cluster_distance", NC_FLOAT, 1, vardimids, &distoutid);
00334   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00335 
00336   fillvaluef = -9999.0;
00337   istat = nc_put_att_float(ncoutid, distoutid, "missing_value", NC_FLOAT, 1, &fillvaluef);
00338   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00339   (void) sprintf(tmpstr, "time");
00340   istat = nc_put_att_text(ncoutid, distoutid, "coordinates", strlen(tmpstr), tmpstr);
00341   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00342   (void) strcpy(tmpstr, "none");
00343   istat = nc_put_att_text(ncoutid, distoutid, "units", strlen(tmpstr), tmpstr);
00344   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00345   (void) strcpy(tmpstr, "Normalized distance to cluster");
00346   istat = nc_put_att_text(ncoutid, distoutid, "long_name", strlen(tmpstr), tmpstr);
00347   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00348 
00349   /* Define cluster number variable */
00350   vardimids[0] = timedimoutid;
00351   istat = nc_def_var(ncoutid, "cluster", NC_INT, 1, vardimids, &clusteroutid);
00352   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00353 
00354   fillvaluei = -1;
00355   istat = nc_put_att_int(ncoutid, clusteroutid, "missing_value", NC_INT, 1, &fillvaluei);
00356   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00357   (void) sprintf(tmpstr, "time");
00358   istat = nc_put_att_text(ncoutid, clusteroutid, "coordinates", strlen(tmpstr), tmpstr);
00359   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00360   (void) strcpy(tmpstr, "none");
00361   istat = nc_put_att_text(ncoutid, clusteroutid, "units", strlen(tmpstr), tmpstr);
00362   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00363   (void) strcpy(tmpstr, "Cluster number");
00364   istat = nc_put_att_text(ncoutid, clusteroutid, "long_name", strlen(tmpstr), tmpstr);
00365   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00366 
00367   /* Define ndayschoice analog day variable: year */
00368   vardimids[0] = timedimoutid;
00369   vardimids[1] = ndayschoicedimoutid;
00370   istat = nc_def_var(ncoutid, "analog_ndays_date_year", NC_INT, 2, vardimids, &analogyearndaysoutid);
00371   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00372 
00373   fillvaluei = 0;
00374   istat = nc_put_att_int(ncoutid, analogyearndaysoutid, "missing_value", NC_INT, 1, &fillvaluei);
00375   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00376   (void) sprintf(tmpstr, "time ndayschoice");
00377   istat = nc_put_att_text(ncoutid, analogyearndaysoutid, "coordinates", strlen(tmpstr), tmpstr);
00378   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00379   (void) sprintf(tmpstr, "%s", "year");
00380   istat = nc_put_att_text(ncoutid, analogyearndaysoutid, "units", strlen(tmpstr), tmpstr);
00381   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00382   (void) strcpy(tmpstr, "Analog ndays date: year");
00383   istat = nc_put_att_text(ncoutid, analogyearndaysoutid, "long_name", strlen(tmpstr), tmpstr);
00384   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00385 
00386   /* Define ndayschoice analog day variable: month */
00387   vardimids[0] = timedimoutid;
00388   vardimids[1] = ndayschoicedimoutid;
00389   istat = nc_def_var(ncoutid, "analog_ndays_date_month", NC_INT, 2, vardimids, &analogmonthndaysoutid);
00390   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00391 
00392   fillvaluei = 0;
00393   istat = nc_put_att_int(ncoutid, analogmonthndaysoutid, "missing_value", NC_INT, 1, &fillvaluei);
00394   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00395   (void) sprintf(tmpstr, "time ndayschoice");
00396   istat = nc_put_att_text(ncoutid, analogmonthndaysoutid, "coordinates", strlen(tmpstr), tmpstr);
00397   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00398   (void) sprintf(tmpstr, "%s", "month");
00399   istat = nc_put_att_text(ncoutid, analogmonthndaysoutid, "units", strlen(tmpstr), tmpstr);
00400   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00401   (void) strcpy(tmpstr, "Analog ndays date: month");
00402   istat = nc_put_att_text(ncoutid, analogmonthndaysoutid, "long_name", strlen(tmpstr), tmpstr);
00403   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00404 
00405   /* Define ndayschoice analog day variable: day */
00406   vardimids[0] = timedimoutid;
00407   vardimids[1] = ndayschoicedimoutid;
00408   istat = nc_def_var(ncoutid, "analog_ndays_date_day", NC_INT, 2, vardimids, &analogdayndaysoutid);
00409   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00410 
00411   fillvaluei = 0;
00412   istat = nc_put_att_int(ncoutid, analogdayndaysoutid, "missing_value", NC_INT, 1, &fillvaluei);
00413   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00414   (void) sprintf(tmpstr, "time ndayschoice");
00415   istat = nc_put_att_text(ncoutid, analogdayndaysoutid, "coordinates", strlen(tmpstr), tmpstr);
00416   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00417   (void) sprintf(tmpstr, "%s", "day");
00418   istat = nc_put_att_text(ncoutid, analogdayndaysoutid, "units", strlen(tmpstr), tmpstr);
00419   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00420   (void) strcpy(tmpstr, "Analog ndays date: day");
00421   istat = nc_put_att_text(ncoutid, analogdayndaysoutid, "long_name", strlen(tmpstr), tmpstr);
00422   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00423 
00424   /* Define ndayschoice normalized metric */
00425   vardimids[0] = timedimoutid;
00426   vardimids[1] = ndayschoicedimoutid;
00427   istat = nc_def_var(ncoutid, "analog_metric_norm", NC_FLOAT, 2, vardimids, &metricoutid);
00428   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00429 
00430   fillvaluef = 0.0;
00431   istat = nc_put_att_float(ncoutid, metricoutid, "missing_value", NC_FLOAT, 1, &fillvaluef);
00432   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00433   (void) sprintf(tmpstr, "time ndayschoice");
00434   istat = nc_put_att_text(ncoutid, metricoutid, "coordinates", strlen(tmpstr), tmpstr);
00435   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00436   (void) sprintf(tmpstr, "%s", "metric");
00437   istat = nc_put_att_text(ncoutid, metricoutid, "units", strlen(tmpstr), tmpstr);
00438   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00439   (void) strcpy(tmpstr, "Analog normalized metric");
00440   istat = nc_put_att_text(ncoutid, metricoutid, "long_name", strlen(tmpstr), tmpstr);
00441   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00442 
00443   /* End definition mode */
00444   istat = nc_enddef(ncoutid);
00445   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00446 
00447   /* Write variables */  /* Write time */
00448   start[0] = 0;
00449   start[1] = 0;
00450   count[0] = (size_t) analog_days.ntime;
00451   count[1] = 0;
00452   istat = nc_put_vara_double(ncoutid, timeoutid, start, count, time_ls);
00453   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00454 
00455   /* Write ndayschoice */
00456   start[0] = 0;
00457   start[1] = 0;
00458   count[0] = (size_t) analog_days.ntime;
00459   count[1] = 0;
00460   istat = nc_put_vara_int(ncoutid, ndayschoiceoutid, start, count, analog_days.ndayschoice);
00461   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00462 
00463   /* Write downscaled dates */
00464   start[0] = 0;
00465   start[1] = 0;
00466   count[0] = (size_t) analog_days.ntime;
00467   count[1] = 0;
00468   istat = nc_put_vara_int(ncoutid, downscaledyearoutid, start, count, analog_days.year_s);
00469   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00470   istat = nc_put_vara_int(ncoutid, downscaledmonthoutid, start, count, analog_days.month_s);
00471   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00472   istat = nc_put_vara_int(ncoutid, downscaleddayoutid, start, count, analog_days.day_s);
00473   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00474 
00475   /* Write analog dates */
00476   start[0] = 0;
00477   start[1] = 0;
00478   count[0] = (size_t) analog_days.ntime;
00479   count[1] = 0;
00480   istat = nc_put_vara_int(ncoutid, analogoutid, start, count, analog_days.time);
00481   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00482   istat = nc_put_vara_int(ncoutid, analogyearoutid, start, count, analog_days.year);
00483   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00484   istat = nc_put_vara_int(ncoutid, analogmonthoutid, start, count, analog_days.month);
00485   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00486   istat = nc_put_vara_int(ncoutid, analogdayoutid, start, count, analog_days.day);
00487   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00488   
00489   /* Write ndayschoice analog dates */
00490   start[0] = 0;
00491   start[1] = 0;
00492   count[0] = (size_t) analog_days.ntime;
00493   count[1] = (size_t) maxndays;
00494   /* Build 2D array */
00495   buftmp = (int *) calloc(analog_days.ntime * maxndays, sizeof(int));
00496   if (buftmp == NULL) alloc_error(__FILE__, __LINE__);
00497   for (t=0; t<analog_days.ntime; t++)
00498     for (i=0; i<analog_days.ndayschoice[t]; i++)
00499       buftmp[i+t*maxndays] = analog_days.analog_dayschoice[t][i].year;
00500   istat = nc_put_vara_int(ncoutid, analogyearndaysoutid, start, count, buftmp);
00501   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00502   for (t=0; t<analog_days.ntime; t++)
00503     for (i=0; i<analog_days.ndayschoice[t]; i++)
00504       buftmp[i+t*maxndays] = analog_days.analog_dayschoice[t][i].month;
00505   istat = nc_put_vara_int(ncoutid, analogmonthndaysoutid, start, count, buftmp);
00506   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00507   for (t=0; t<analog_days.ntime; t++)
00508     for (i=0; i<analog_days.ndayschoice[t]; i++)
00509       buftmp[i+t*maxndays] = analog_days.analog_dayschoice[t][i].day;
00510   istat = nc_put_vara_int(ncoutid, analogdayndaysoutid, start, count, buftmp);
00511   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00512   (void) free(buftmp);
00513 
00514   /* Write analog normalized metric */
00515   start[0] = 0;
00516   start[1] = 0;
00517   count[0] = (size_t) analog_days.ntime;
00518   count[1] = (size_t) maxndays;
00519   buftmpf = (float *) calloc(analog_days.ntime * maxndays, sizeof(float));
00520   if (buftmpf == NULL) alloc_error(__FILE__, __LINE__);
00521   for (t=0; t<analog_days.ntime; t++)
00522     for (i=0; i<analog_days.ndayschoice[t]; i++)
00523       buftmpf[i+t*maxndays] = analog_days.metric_norm[t][i];
00524   istat = nc_put_vara_float(ncoutid, metricoutid, start, count, buftmpf);
00525   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00526   (void) free(buftmpf);
00527  
00528   /* Write delta of temperature */
00529   start[0] = 0;
00530   start[1] = 0;
00531   count[0] = (size_t) analog_days.ntime;
00532   count[1] = 0;
00533   istat = nc_put_vara_double(ncoutid, deltatoutid, start, count, delta);
00534   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00535 
00536   /* Write ndayschoice delta of temperature */
00537   start[0] = 0;
00538   start[1] = 0;
00539   count[0] = (size_t) analog_days.ntime;
00540   count[1] = (size_t) maxndays;
00541   buftmpf = (float *) calloc(analog_days.ntime * maxndays, sizeof(float));
00542   if (buftmpf == NULL) alloc_error(__FILE__, __LINE__);
00543   for (t=0; t<analog_days.ntime; t++)
00544     for (i=0; i<analog_days.ndayschoice[t]; i++)
00545       buftmpf[i+t*maxndays] = (float) delta_dayschoice[t][i];
00546   istat = nc_put_vara_float(ncoutid, deltatndaysoutid, start, count, buftmpf);
00547   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00548   (void) free(buftmpf);
00549 
00550   /* Write cluster distance */
00551   start[0] = 0;
00552   start[1] = 0;
00553   count[0] = (size_t) analog_days.ntime;
00554   count[1] = 0;
00555   istat = nc_put_vara_double(ncoutid, distoutid, start, count, dist);
00556   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00557   
00558   /* Write cluster number */
00559   start[0] = 0;
00560   start[1] = 0;
00561   count[0] = (size_t) analog_days.ntime;
00562   count[1] = 0;
00563   istat = nc_put_vara_int(ncoutid, clusteroutid, start, count, cluster);
00564   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00565   
00566   /* Close the output netCDF file */
00567   istat = ncclose(ncoutid);
00568   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00569 
00570   (void) free(tmpstr);
00571 }

Generated on 12 May 2016 for DSCLIM by  doxygen 1.6.1