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