00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00056
00057
00058
00059
00060
00061
00062
00063
00064 #include <io.h>
00065
00067 int
00068 write_netcdf_var_3d_2d(double *buf, double *timein, double fillvalue, char *filename,
00069 char *varname, char *longname, char *units, char *height,
00070 char *gridname, char *lonname, char *latname, char *timename,
00071 int t, int newfile, int format, int compression_level,
00072 int nlon, int nlat, int ntime, int outinfo) {
00098 int istat;
00099
00100 size_t dimval;
00101
00102 int ncoutid;
00103 int varoutid;
00104 int timedimoutid;
00105 int timeid;
00106 int londimoutid;
00107 int latdimoutid;
00108 int vardimids[NC_MAX_VAR_DIMS];
00109
00110
00111
00112
00113
00114 int ntime_file;
00115 int nlat_file;
00116 int nlon_file;
00117
00118 size_t start[3];
00119 size_t count[3];
00120
00121 char *attname = NULL;
00122 char *tmpstr = NULL;
00123
00124
00125 attname = (char *) malloc(MAXPATH * sizeof(char));
00126 if (attname == NULL) alloc_error(__FILE__, __LINE__);
00127
00128
00129 tmpstr = strdup(filename);
00130 istat = chdir(dirname(tmpstr));
00131 (void) free(tmpstr);
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00147 istat = nc_open(filename, NC_WRITE, &ncoutid);
00148 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00149
00150
00151 istat = nc_inq_dimid(ncoutid, timename, &timedimoutid);
00152 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00153 istat = nc_inq_dimlen(ncoutid, timedimoutid, &dimval);
00154 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00155 ntime_file = (int) dimval;
00156
00157 istat = nc_inq_varid(ncoutid, timename, &timeid);
00158 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00159
00160 istat = nc_inq_dimid(ncoutid, latname, &latdimoutid);
00161 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00162 istat = nc_inq_dimlen(ncoutid, latdimoutid, &dimval);
00163 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00164 nlat_file = (int) dimval;
00165
00166 istat = nc_inq_dimid(ncoutid, lonname, &londimoutid);
00167 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00168 istat = nc_inq_dimlen(ncoutid, londimoutid, &dimval);
00169 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00170 nlon_file = (int) dimval;
00171
00172
00173 if ( !strcmp(gridname, "list") ) {
00174 if ( ((nlat_file != nlon) || (nlon_file != nlon) )) {
00175 (void) fprintf(stderr, "%s: Error NetCDF type and/or dimensions.\n", __FILE__);
00176 return -1;
00177 }
00178 }
00179 else {
00180 if ( ((nlat_file != nlat) || (nlon_file != nlon) )) {
00181 (void) fprintf(stderr, "%s: Error NetCDF type and/or dimensions.\n", __FILE__);
00182 return -1;
00183 }
00184 }
00185
00186
00187 if (newfile == TRUE) {
00188 istat = nc_redef(ncoutid);
00189 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00190
00191
00192 vardimids[0] = timedimoutid;
00193 if ( !strcmp(gridname, "list") ) {
00194 vardimids[1] = londimoutid;
00195 istat = nc_def_var(ncoutid, varname, NC_FLOAT, 2, vardimids, &varoutid);
00196 }
00197 else {
00198 vardimids[1] = latdimoutid;
00199 vardimids[2] = londimoutid;
00200 istat = nc_def_var(ncoutid, varname, NC_FLOAT, 3, vardimids, &varoutid);
00201 }
00202 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00203
00204 #ifdef NC_NETCDF4
00205 if (format == 4 && compression_level > 0) {
00206
00207 istat = nc_def_var_deflate(ncoutid, varoutid, 0, 1, compression_level);
00208 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 }
00223 #endif
00224
00225
00226 (void) strcpy(attname, "_FillValue");
00227 istat = nc_put_att_double(ncoutid, varoutid, attname, NC_FLOAT, 1, &fillvalue);
00228 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00229
00230 (void) strcpy(attname, "missing_value");
00231 istat = nc_put_att_double(ncoutid, varoutid, attname, NC_FLOAT, 1, &fillvalue);
00232 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00233
00234 tmpstr = (char *) malloc(100 * sizeof(char));
00235 if (tmpstr == NULL) alloc_error(__FILE__, __LINE__);
00236 istat = nc_put_att_text(ncoutid, varoutid, "long_name", strlen(longname), longname);
00237 istat = nc_put_att_text(ncoutid, varoutid, "grid_mapping", strlen(gridname), gridname);
00238 istat = nc_put_att_text(ncoutid, varoutid, "units", strlen(units), units);
00239 istat = nc_put_att_text(ncoutid, varoutid, "height", strlen(height), height);
00240 istat = sprintf(tmpstr, "lon lat");
00241 istat = nc_put_att_text(ncoutid, varoutid, "coordinates", strlen(tmpstr), tmpstr);
00242 (void) free(tmpstr);
00243
00244
00245 istat = nc_enddef(ncoutid);
00246 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00247 }
00248 else {
00249 istat = nc_inq_varid(ncoutid, varname, &varoutid);
00250 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00251 }
00252
00253
00254 start[0] = ntime_file;
00255 start[1] = 0;
00256 start[2] = 0;
00257 count[0] = (size_t) 1;
00258 count[1] = 0;
00259 count[2] = 0;
00260 istat = nc_put_vara_double(ncoutid, timeid, start, count, &(timein[t]));
00261 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00262
00263
00264 start[0] = ntime_file;
00265 start[1] = 0;
00266 start[2] = 0;
00267 count[0] = (size_t) 1;
00268 if ( !strcmp(gridname, "list") ) {
00269 count[1] = (size_t) nlon;
00270 count[2] = 0;
00271 }
00272 else {
00273 count[1] = (size_t) nlat;
00274 count[2] = (size_t) nlon;
00275 }
00276 if (outinfo == TRUE)
00277 printf("%s: WRITE %s %s\n", __FILE__, varname, filename);
00278 istat = nc_put_vara_double(ncoutid, varoutid, start, count, buf);
00279 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00280
00281
00282 istat = ncclose(ncoutid);
00283 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00284
00285
00286 (void) free(attname);
00287
00288
00289 return 0;
00290 }