Write regression-related downscaling fields for diagnostics use. More...
#include <dsclim.h>
Go to the source code of this file.
Functions | |
int | write_regression_fields (data_struct *data, char *filename, double **timeval, int *ntime, double **precip_index, double **distclust, double **sup_index) |
Write regression-related downscaling fields for diagnostics use. |
Write regression-related downscaling fields for diagnostics use.
Definition in file write_regression_fields.c.
int write_regression_fields | ( | data_struct * | data, | |
char * | filename, | |||
double ** | timeval, | |||
int * | ntime, | |||
double ** | precip_index, | |||
double ** | distclust, | |||
double ** | sup_index | |||
) |
Write regression-related downscaling fields for diagnostics use.
[in] | data | MASTER data structure. |
[in] | filename | Output filename. |
[in] | timeval | Time vector. |
[in] | ntime | Number of times in timeval. |
[in] | precip_index | Precipitation index. |
[in] | distclust | Distance of days to cluster centres. |
[in] | sup_index | Supplemental large-scale field index. |
Definition at line 59 of file write_regression_fields.c.
References alloc_error(), conf_struct::clustname, data_struct::conf, info_struct::contact_email, info_struct::contact_name, info_struct::creator_email, info_struct::creator_name, info_struct::creator_url, learning_struct::data, handle_netcdf_error(), data_struct::info, info_struct::institution, data_struct::learning, season_struct::nclusters, learning_struct::nomvar_class_clusters, learning_struct::nomvar_precip_index, learning_struct::nomvar_precip_reg, learning_struct::nomvar_precip_reg_cst, learning_struct::nomvar_sup_index, reg_struct::npts, conf_struct::nseasons, info_struct::other_contact_email, info_struct::other_contact_name, learning_data_struct::precip_reg, learning_data_struct::precip_reg_cst, info_struct::processor, conf_struct::ptsname, data_struct::reg, conf_struct::season, info_struct::software, conf_struct::time_units, and reg_struct::timename.
Referenced by wt_downscaling().
00060 { 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 }