00001
00002
00003
00004
00005
00006
00007
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
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;
00072
00073 int ncoutid;
00074 int *timedimoutid;
00075 int sdimoutid;
00076 int ptsdimoutid;
00077 int *clustdimoutid;
00078 int *timeoutid;
00079 int *cstoutid;
00080 int *regoutid;
00081 int *rrdoutid;
00082 int *distclustoutid;
00083 int *taoutid;
00084 int vardimids[NC_MAX_VAR_DIMS];
00085
00086 size_t start[3];
00087 size_t count[3];
00088
00089 char *tmpstr = NULL;
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
00120 istat = nc_create(filename, NC_CLOBBER, &ncoutid);
00121 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00122
00123
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
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
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
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
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
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
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
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
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
00250 istat = nc_enddef(ncoutid);
00251 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00252
00253
00254 for (s=0; s<data->conf->nseasons; s++) {
00255
00256
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
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
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
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
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
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
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 }