netCDF
4.2.1.1
|
00001 00008 #ifndef NO_NETCDF_2 00009 00010 #include <config.h> 00011 #include <stdlib.h> 00012 #include <stdio.h> 00013 #include <stdarg.h> 00014 #include "netcdf.h" 00015 00016 /* The subroutines in error.c emit no messages unless NC_VERBOSE bit 00017 * is on. They call exit() when NC_FATAL bit is on. */ 00018 int ncopts = (NC_FATAL | NC_VERBOSE) ; 00019 int ncerr = NC_NOERR ; 00020 00021 #if SIZEOF_LONG == SIZEOF_SIZE_T 00022 /* 00023 * We don't have to copy the arguments to switch from 'long' 00024 * to 'size_t' or 'ptrdiff_t'. Use dummy macros. 00025 */ 00026 00027 # define NDIMS_DECL 00028 # define A_DECL(name, type, ndims, rhs) \ 00029 const type *const name = ((const type *)(rhs)) 00030 00031 # define A_FREE(name) 00032 00033 # define A_INIT(lhs, type, ndims, rhs) 00034 00035 #else 00036 /* 00037 * We do have to copy the arguments to switch from 'long' 00038 * to 'size_t' or 'ptrdiff_t'. In my tests on an SGI, 00039 * any additional cost was lost in measurement variation. 00040 * 00041 * This stanza is true on Windows with MinGW-64 00042 */ 00043 00044 # include "onstack.h" 00045 00046 static size_t 00047 nvdims(int ncid, int varid) 00048 { 00049 int ndims=-1, status; 00050 00051 if ((status = nc_inq_varndims(ncid, varid, &ndims))) 00052 { 00053 nc_advise("ncvdims", status, "ncid %d", ncid); 00054 return -1; 00055 } 00056 return ndims; 00057 } 00058 00059 #define NDIMS_DECL const size_t ndims = nvdims(ncid, varid); 00060 00061 # define A_DECL(name, type, ndims, rhs) \ 00062 ALLOC_ONSTACK(name, type, ndims) 00063 00064 # define A_FREE(name) \ 00065 FREE_ONSTACK(name) 00066 00067 # define A_INIT(lhs, type, ndims, rhs) \ 00068 { \ 00069 const long *lp = rhs; \ 00070 type *tp = lhs; \ 00071 type *const end = lhs + ndims; \ 00072 while(tp < end) \ 00073 { \ 00074 *tp++ = (type) *lp++; \ 00075 } \ 00076 } 00077 00078 00079 #endif 00080 00081 typedef signed char schar; 00082 00083 /* 00084 * Computes number of record variables in an open netCDF file, and an array of 00085 * the record variable ids, if the array parameter is non-null. 00086 */ 00087 static int 00088 numrecvars(int ncid, int *nrecvarsp, int *recvarids) 00089 { 00090 int status = NC_NOERR; 00091 int nvars = 0; 00092 int ndims = 0; 00093 int nrecvars = 0; 00094 int varid; 00095 int recdimid; 00096 int dimids[MAX_NC_DIMS]; 00097 00098 status = nc_inq_nvars(ncid, &nvars); 00099 if(status != NC_NOERR) 00100 return status; 00101 00102 status = nc_inq_unlimdim(ncid, &recdimid); 00103 if(status != NC_NOERR) 00104 return status; 00105 00106 if (recdimid == -1) { 00107 *nrecvarsp = 0; 00108 return NC_NOERR; 00109 } 00110 nrecvars = 0; 00111 for (varid = 0; varid < nvars; varid++) { 00112 status = nc_inq_varndims(ncid, varid, &ndims); 00113 if(status != NC_NOERR) 00114 return status; 00115 status = nc_inq_vardimid(ncid, varid, dimids); 00116 if(status != NC_NOERR) 00117 return status; 00118 if (ndims > 0 && dimids[0] == recdimid) { 00119 if (recvarids != NULL) 00120 recvarids[nrecvars] = varid; 00121 nrecvars++; 00122 } 00123 } 00124 *nrecvarsp = nrecvars; 00125 return NC_NOERR; 00126 } 00127 00128 00129 /* 00130 * Computes record size (in bytes) of the record variable with a specified 00131 * variable id. Returns size as 0 if not a record variable. 00132 */ 00133 static int 00134 ncrecsize(int ncid, int varid, size_t *recsizep) 00135 { 00136 int status = NC_NOERR; 00137 int recdimid; 00138 nc_type type; 00139 int ndims; 00140 int dimids[MAX_NC_DIMS]; 00141 int id; 00142 size_t size; 00143 00144 *recsizep = 0; 00145 status = nc_inq_unlimdim(ncid, &recdimid); 00146 if(status != NC_NOERR) 00147 return status; 00148 status = nc_inq_vartype(ncid, varid, &type); 00149 if(status != NC_NOERR) 00150 return status; 00151 status = nc_inq_varndims(ncid, varid, &ndims); 00152 if(status != NC_NOERR) 00153 return status; 00154 status = nc_inq_vardimid(ncid, varid, dimids); 00155 if(status != NC_NOERR) 00156 return status; 00157 if (ndims == 0 || dimids[0] != recdimid) { 00158 return NC_NOERR; 00159 } 00160 size = nctypelen(type); 00161 for (id = 1; id < ndims; id++) { 00162 size_t len; 00163 status = nc_inq_dimlen(ncid, dimids[id], &len); 00164 if(status != NC_NOERR) 00165 return status; 00166 size *= len; 00167 } 00168 *recsizep = size; 00169 return NC_NOERR; 00170 } 00171 00172 00173 /* 00174 * Retrieves the dimension sizes of a variable with a specified variable id in 00175 * an open netCDF file. Returns -1 on error. 00176 */ 00177 static int 00178 dimsizes(int ncid, int varid, size_t *sizes) 00179 { 00180 int status = NC_NOERR; 00181 int ndims; 00182 int id; 00183 int dimids[MAX_NC_DIMS]; 00184 00185 status = nc_inq_varndims(ncid, varid, &ndims); 00186 if(status != NC_NOERR) 00187 return status; 00188 status = nc_inq_vardimid(ncid, varid, dimids); 00189 if(status != NC_NOERR) 00190 return status; 00191 if (ndims == 0 || sizes == NULL) 00192 return NC_NOERR; 00193 for (id = 0; id < ndims; id++) { 00194 size_t len; 00195 status = nc_inq_dimlen(ncid, dimids[id], &len); 00196 if(status != NC_NOERR) 00197 return status; 00198 sizes[id] = len; 00199 } 00200 return NC_NOERR; 00201 } 00202 00203 00204 /* 00205 * Retrieves the number of record variables, the record variable ids, and the 00206 * record size of each record variable. If any pointer to info to be returned 00207 * is null, the associated information is not returned. Returns -1 on error. 00208 */ 00209 int 00210 nc_inq_rec( 00211 int ncid, 00212 size_t *nrecvarsp, 00213 int *recvarids, 00214 size_t *recsizes) 00215 { 00216 int status = NC_NOERR; 00217 int nvars = 0; 00218 int recdimid; 00219 int varid; 00220 int rvarids[MAX_NC_VARS]; 00221 int nrvars = 0; 00222 00223 status = nc_inq_nvars(ncid, &nvars); 00224 if(status != NC_NOERR) 00225 return status; 00226 00227 status = nc_inq_unlimdim(ncid, &recdimid); 00228 if(status != NC_NOERR) 00229 return status; 00230 00231 *nrecvarsp = 0; 00232 if (recdimid == -1) 00233 return NC_NOERR; 00234 00235 status = numrecvars(ncid, &nrvars, rvarids); 00236 if(status != NC_NOERR) 00237 return status; 00238 00239 if (nrecvarsp != NULL) 00240 *nrecvarsp = nrvars; 00241 if (recvarids != NULL) 00242 for (varid = 0; varid < nrvars; varid++) 00243 recvarids[varid] = rvarids[varid]; 00244 00245 if (recsizes != NULL) 00246 for (varid = 0; varid < nrvars; varid++) { 00247 size_t rsize; 00248 status = ncrecsize(ncid, rvarids[varid], &rsize); 00249 if (status != NC_NOERR) 00250 return status; 00251 recsizes[varid] = rsize; 00252 } 00253 return NC_NOERR; 00254 } 00255 00256 00257 /* 00258 * Write one record's worth of data, except don't write to variables for which 00259 * the address of the data to be written is NULL. Return -1 on error. This is 00260 * the same as the ncrecput() in the library, except that can handle errors 00261 * better. 00262 */ 00263 int 00264 nc_put_rec( 00265 int ncid, 00266 size_t recnum, 00267 void* const* datap) 00268 { 00269 int status = NC_NOERR; 00270 int varid; 00271 int rvarids[MAX_NC_VARS]; 00272 int nrvars; 00273 size_t start[MAX_NC_DIMS]; 00274 size_t edges[MAX_NC_DIMS]; 00275 00276 status = numrecvars(ncid, &nrvars, rvarids); 00277 if(status != NC_NOERR) 00278 return status; 00279 00280 if (nrvars == 0) 00281 return NC_NOERR; 00282 00283 start[0] = recnum; 00284 for (varid = 1; varid < nrvars; varid++) 00285 start[varid] = 0; 00286 00287 for (varid = 0; varid < nrvars; varid++) { 00288 if (datap[varid] != NULL) { 00289 status = dimsizes(ncid, rvarids[varid], edges); 00290 if(status != NC_NOERR) 00291 return status; 00292 00293 edges[0] = 1; /* only 1 record's worth */ 00294 status = nc_put_vara(ncid, rvarids[varid], start, edges, datap[varid]); 00295 if(status != NC_NOERR) 00296 return status; 00297 } 00298 } 00299 return 0; 00300 } 00301 00302 00303 /* 00304 * Read one record's worth of data, except don't read from variables for which 00305 * the address of the data to be read is null. Return -1 on error. This is 00306 * the same as the ncrecget() in the library, except that can handle errors 00307 * better. 00308 */ 00309 int 00310 nc_get_rec( 00311 int ncid, 00312 size_t recnum, 00313 void **datap) 00314 { 00315 int status = NC_NOERR; 00316 int varid; 00317 int rvarids[MAX_NC_VARS]; 00318 int nrvars; 00319 size_t start[MAX_NC_DIMS]; 00320 size_t edges[MAX_NC_DIMS]; 00321 00322 status = numrecvars(ncid, &nrvars, rvarids); 00323 if(status != NC_NOERR) 00324 return status; 00325 00326 if (nrvars == 0) 00327 return NC_NOERR; 00328 00329 start[0] = recnum; 00330 for (varid = 1; varid < nrvars; varid++) 00331 start[varid] = 0; 00332 00333 for (varid = 0; varid < nrvars; varid++) { 00334 if (datap[varid] != NULL) { 00335 status = dimsizes(ncid, rvarids[varid], edges); 00336 if(status != NC_NOERR) 00337 return status; 00338 edges[0] = 1; /* only 1 record's worth */ 00339 status = nc_get_vara(ncid, rvarids[varid], start, edges, datap[varid]); 00340 if(status != NC_NOERR) 00341 return status; 00342 } 00343 } 00344 return 0; 00345 } 00346 00347 /* 00348 */ 00349 void 00350 nc_advise(const char *routine_name, int err, const char *fmt,...) 00351 { 00352 va_list args; 00353 00354 if(NC_ISSYSERR(err)) 00355 ncerr = NC_SYSERR; 00356 else 00357 ncerr = err; 00358 00359 if( ncopts & NC_VERBOSE ) 00360 { 00361 (void) fprintf(stderr,"%s: ", routine_name); 00362 va_start(args ,fmt); 00363 (void) vfprintf(stderr,fmt,args); 00364 va_end(args); 00365 if(err != NC_NOERR) 00366 { 00367 (void) fprintf(stderr,": %s", 00368 nc_strerror(err)); 00369 } 00370 (void) fputc('\n',stderr); 00371 (void) fflush(stderr); /* to ensure log files are current */ 00372 } 00373 00374 if( (ncopts & NC_FATAL) && err != NC_NOERR ) 00375 { 00376 exit(ncopts); 00377 } 00378 } 00379 00380 /* End error handling */ 00381 00382 int 00383 nccreate(const char* path, int cmode) 00384 { 00385 int ncid; 00386 const int status = nc_create(path, cmode, &ncid); 00387 if(status != NC_NOERR) 00388 { 00389 nc_advise("nccreate", status, "filename \"%s\"", path); 00390 return -1; 00391 } 00392 return ncid; 00393 } 00394 00395 00396 int 00397 ncopen(const char *path, int mode) 00398 { 00399 int ncid; 00400 const int status = nc_open(path, mode, &ncid); 00401 if(status != NC_NOERR) 00402 { 00403 nc_advise("ncopen", status, "filename \"%s\"", path); 00404 return -1; 00405 } 00406 return ncid; 00407 } 00408 00409 00410 int 00411 ncredef(int ncid) 00412 { 00413 const int status = nc_redef(ncid); 00414 if(status != NC_NOERR) 00415 { 00416 nc_advise("ncredef", status, "ncid %d", ncid); 00417 return -1; 00418 } 00419 return 0; 00420 } 00421 00422 00423 int 00424 ncendef(int ncid) 00425 { 00426 const int status = nc_enddef(ncid); 00427 if(status != NC_NOERR) 00428 { 00429 nc_advise("ncendef", status, "ncid %d", ncid); 00430 return -1; 00431 } 00432 return 0; 00433 } 00434 00435 00436 int 00437 ncclose(int ncid) 00438 { 00439 const int status = nc_close(ncid); 00440 if(status != NC_NOERR) 00441 { 00442 nc_advise("ncclose", status, "ncid %d", ncid); 00443 return -1; 00444 00445 } 00446 return 0; 00447 } 00448 00449 00450 int 00451 ncinquire( 00452 int ncid, 00453 int* ndims, 00454 int* nvars, 00455 int* natts, 00456 int* recdim 00457 ) 00458 { 00459 int nd, nv, na; 00460 const int status = nc_inq(ncid, &nd, &nv, &na, recdim); 00461 00462 if(status != NC_NOERR) 00463 { 00464 nc_advise("ncinquire", status, "ncid %d", ncid); 00465 return -1; 00466 } 00467 /* else */ 00468 00469 if(ndims != NULL) 00470 *ndims = (int) nd; 00471 00472 if(nvars != NULL) 00473 *nvars = (int) nv; 00474 00475 if(natts != NULL) 00476 *natts = (int) na; 00477 00478 return ncid; 00479 } 00480 00481 00482 int 00483 ncsync(int ncid) 00484 { 00485 const int status = nc_sync(ncid); 00486 if(status != NC_NOERR) 00487 { 00488 nc_advise("ncsync", status, "ncid %d", ncid); 00489 return -1; 00490 00491 } 00492 return 0; 00493 } 00494 00495 00496 int 00497 ncabort(int ncid) 00498 { 00499 const int status = nc_abort(ncid); 00500 if(status != NC_NOERR) 00501 { 00502 nc_advise("ncabort", status, "ncid %d", ncid); 00503 return -1; 00504 } 00505 return 0; 00506 } 00507 00508 00509 int 00510 ncdimdef( 00511 int ncid, 00512 const char* name, 00513 long length 00514 ) 00515 { 00516 int dimid; 00517 int status = NC_NOERR; 00518 if(length < 0) { 00519 status = NC_EDIMSIZE; 00520 nc_advise("ncdimdef", status, "ncid %d", ncid); 00521 return -1; 00522 } 00523 status = nc_def_dim(ncid, name, (size_t)length, &dimid); 00524 if(status != NC_NOERR) 00525 { 00526 nc_advise("ncdimdef", status, "ncid %d", ncid); 00527 return -1; 00528 } 00529 return dimid; 00530 } 00531 00532 00533 int 00534 ncdimid(int ncid, const char* name) 00535 { 00536 int dimid; 00537 const int status = nc_inq_dimid(ncid, name, &dimid); 00538 if(status != NC_NOERR) 00539 { 00540 nc_advise("ncdimid", status, "ncid %d", ncid); 00541 return -1; 00542 } 00543 return dimid; 00544 } 00545 00546 00547 int 00548 ncdiminq( 00549 int ncid, 00550 int dimid, 00551 char* name, 00552 long* length 00553 ) 00554 { 00555 size_t ll; 00556 const int status = nc_inq_dim(ncid, dimid, name, &ll); 00557 00558 if(status != NC_NOERR) 00559 { 00560 nc_advise("ncdiminq", status, "ncid %d", ncid); 00561 return -1; 00562 } 00563 /* else */ 00564 00565 if(length != NULL) 00566 *length = (int) ll; 00567 00568 return dimid; 00569 } 00570 00571 00572 int 00573 ncdimrename( 00574 int ncid, 00575 int dimid, 00576 const char* name 00577 ) 00578 { 00579 const int status = nc_rename_dim(ncid, dimid, name); 00580 if(status != NC_NOERR) 00581 { 00582 nc_advise("ncdimrename", status, "ncid %d", ncid); 00583 return -1; 00584 } 00585 return dimid; 00586 } 00587 00588 00589 int 00590 ncvardef( 00591 int ncid, 00592 const char* name, 00593 nc_type datatype, 00594 int ndims, 00595 const int* dim 00596 ) 00597 { 00598 int varid = -1; 00599 const int status = nc_def_var(ncid, name, datatype, ndims, dim, &varid); 00600 if(status != NC_NOERR) 00601 { 00602 nc_advise("ncvardef", status, "ncid %d", ncid); 00603 return -1; 00604 } 00605 return varid; 00606 } 00607 00608 00609 int 00610 ncvarid( 00611 int ncid, 00612 const char* name 00613 ) 00614 { 00615 int varid = -1; 00616 const int status = nc_inq_varid(ncid, name, &varid); 00617 if(status != NC_NOERR) 00618 { 00619 nc_advise("ncvarid", status, "ncid %d", ncid); 00620 return -1; 00621 } 00622 return varid; 00623 } 00624 00625 00626 int 00627 ncvarinq( 00628 int ncid, 00629 int varid, 00630 char* name, 00631 nc_type* datatype, 00632 int* ndims, 00633 int* dim, 00634 int* natts 00635 ) 00636 { 00637 int nd, na; 00638 const int status = nc_inq_var(ncid, varid, name, datatype, 00639 &nd, dim, &na); 00640 00641 if(status != NC_NOERR) 00642 { 00643 nc_advise("ncvarinq", status, "ncid %d", ncid); 00644 return -1; 00645 } 00646 /* else */ 00647 00648 if(ndims != NULL) 00649 *ndims = (int) nd; 00650 00651 if(natts != NULL) 00652 *natts = (int) na; 00653 00654 return varid; 00655 } 00656 00657 00658 int 00659 ncvarput1( 00660 int ncid, 00661 int varid, 00662 const long* index, 00663 const void* value 00664 ) 00665 { 00666 NDIMS_DECL 00667 A_DECL(coordp, size_t, ndims, index); 00668 A_INIT(coordp, size_t, ndims, index); 00669 { 00670 const int status = nc_put_var1(ncid, varid, coordp, value); 00671 A_FREE(coordp); 00672 if(status != NC_NOERR) 00673 { 00674 nc_advise("ncvarput1", status, "ncid %d", ncid); 00675 return -1; 00676 } 00677 } 00678 return 0; 00679 } 00680 00681 00682 int 00683 ncvarget1( 00684 int ncid, 00685 int varid, 00686 const long* index, 00687 void* value 00688 ) 00689 { 00690 NDIMS_DECL 00691 A_DECL(coordp, size_t, ndims, index); 00692 A_INIT(coordp, size_t, ndims, index); 00693 { 00694 const int status = nc_get_var1(ncid, varid, coordp, value); 00695 A_FREE(coordp); 00696 if(status != NC_NOERR) 00697 { 00698 nc_advise("ncdimid", status, "ncid %d", ncid); 00699 return -1; 00700 } 00701 } 00702 return 0; 00703 } 00704 00705 00706 int 00707 ncvarput( 00708 int ncid, 00709 int varid, 00710 const long* start, 00711 const long* count, 00712 const void* value 00713 ) 00714 { 00715 NDIMS_DECL 00716 A_DECL(stp, size_t, ndims, start); 00717 A_DECL(cntp, size_t, ndims, count); 00718 A_INIT(stp, size_t, ndims, start); 00719 A_INIT(cntp, size_t, ndims, count); 00720 { 00721 const int status = nc_put_vara(ncid, varid, stp, cntp, value); 00722 A_FREE(cntp); 00723 A_FREE(stp); 00724 if(status != NC_NOERR) 00725 { 00726 nc_advise("ncvarput", status, "ncid %d", ncid); 00727 return -1; 00728 } 00729 } 00730 return 0; 00731 } 00732 00733 00734 int 00735 ncvarget( 00736 int ncid, 00737 int varid, 00738 const long* start, 00739 const long* count, 00740 void* value 00741 ) 00742 { 00743 NDIMS_DECL 00744 A_DECL(stp, size_t, ndims, start); 00745 A_DECL(cntp, size_t, ndims, count); 00746 A_INIT(stp, size_t, ndims, start); 00747 A_INIT(cntp, size_t, ndims, count); 00748 { 00749 const int status = nc_get_vara(ncid, varid, stp, cntp, value); 00750 A_FREE(cntp); 00751 A_FREE(stp); 00752 if(status != NC_NOERR) 00753 { 00754 nc_advise("ncvarget", status, "ncid %d; varid %d", ncid, varid); 00755 return -1; 00756 } 00757 } 00758 return 0; 00759 } 00760 00761 00762 int 00763 ncvarputs( 00764 int ncid, 00765 int varid, 00766 const long* start, 00767 const long* count, 00768 const long* stride, 00769 const void* value 00770 ) 00771 { 00772 if(stride == NULL) 00773 return ncvarput(ncid, varid, start, count, value); 00774 /* else */ 00775 { 00776 NDIMS_DECL 00777 A_DECL(stp, size_t, ndims, start); 00778 A_DECL(cntp, size_t, ndims, count); 00779 A_DECL(strdp, ptrdiff_t, ndims, stride); 00780 A_INIT(stp, size_t, ndims, start); 00781 A_INIT(cntp, size_t, ndims, count); 00782 A_INIT(strdp, ptrdiff_t, ndims, stride); 00783 { 00784 const int status = nc_put_vars(ncid, varid, stp, cntp, strdp, value); 00785 A_FREE(strdp); 00786 A_FREE(cntp); 00787 A_FREE(stp); 00788 if(status != NC_NOERR) 00789 { 00790 nc_advise("ncvarputs", status, "ncid %d", ncid); 00791 return -1; 00792 } 00793 } 00794 return 0; 00795 } 00796 } 00797 00798 00799 int 00800 ncvargets( 00801 int ncid, 00802 int varid, 00803 const long* start, 00804 const long* count, 00805 const long* stride, 00806 void* value 00807 ) 00808 { 00809 if(stride == NULL) 00810 return ncvarget(ncid, varid, start, count, value); 00811 /* else */ 00812 { 00813 NDIMS_DECL 00814 A_DECL(stp, size_t, ndims, start); 00815 A_DECL(cntp, size_t, ndims, count); 00816 A_DECL(strdp, ptrdiff_t, ndims, stride); 00817 A_INIT(stp, size_t, ndims, start); 00818 A_INIT(cntp, size_t, ndims, count); 00819 A_INIT(strdp, ptrdiff_t, ndims, stride); 00820 { 00821 const int status = nc_get_vars(ncid, varid, stp, cntp, strdp, value); 00822 A_FREE(strdp); 00823 A_FREE(cntp); 00824 A_FREE(stp); 00825 if(status != NC_NOERR) 00826 { 00827 nc_advise("ncvargets", status, "ncid %d", ncid); 00828 return -1; 00829 } 00830 } 00831 return 0; 00832 } 00833 } 00834 00835 00836 int 00837 ncvarputg( 00838 int ncid, 00839 int varid, 00840 const long* start, 00841 const long* count, 00842 const long* stride, 00843 const long* map, 00844 const void* value 00845 ) 00846 { 00847 if(map == NULL) 00848 return ncvarputs(ncid, varid, start, count, stride, value); 00849 /* else */ 00850 { 00851 NDIMS_DECL 00852 A_DECL(stp, size_t, ndims, start); 00853 A_DECL(cntp, size_t, ndims, count); 00854 A_DECL(strdp, ptrdiff_t, ndims, stride); 00855 A_DECL(imp, ptrdiff_t, ndims, map); 00856 A_INIT(stp, size_t, ndims, start); 00857 A_INIT(cntp, size_t, ndims, count); 00858 A_INIT(strdp, ptrdiff_t, ndims, stride); 00859 A_INIT(imp, ptrdiff_t, ndims, map); 00860 { 00861 const int status = nc_put_varm(ncid, varid, 00862 stp, cntp, strdp, imp, value); 00863 A_FREE(imp); 00864 A_FREE(strdp); 00865 A_FREE(cntp); 00866 A_FREE(stp); 00867 if(status != NC_NOERR) 00868 { 00869 nc_advise("ncvarputg", status, "ncid %d", ncid); 00870 return -1; 00871 } 00872 } 00873 return 0; 00874 } 00875 } 00876 00877 00878 int 00879 ncvargetg( 00880 int ncid, 00881 int varid, 00882 const long* start, 00883 const long* count, 00884 const long* stride, 00885 const long* map, 00886 void* value 00887 ) 00888 { 00889 if(map == NULL) 00890 return ncvargets(ncid, varid, start, count, stride, value); 00891 /* else */ 00892 { 00893 NDIMS_DECL 00894 A_DECL(stp, size_t, ndims, start); 00895 A_DECL(cntp, size_t, ndims, count); 00896 A_DECL(strdp, ptrdiff_t, ndims, stride); 00897 A_DECL(imp, ptrdiff_t, ndims, map); 00898 A_INIT(stp, size_t, ndims, start); 00899 A_INIT(cntp, size_t, ndims, count); 00900 A_INIT(strdp, ptrdiff_t, ndims, stride); 00901 A_INIT(imp, ptrdiff_t, ndims, map); 00902 { 00903 const int status = nc_get_varm(ncid, varid, 00904 stp, cntp, strdp, imp, value); 00905 A_FREE(imp); 00906 A_FREE(strdp); 00907 A_FREE(cntp); 00908 A_FREE(stp); 00909 if(status != NC_NOERR) 00910 { 00911 nc_advise("ncvargetg", status, "ncid %d", ncid); 00912 return -1; 00913 } 00914 } 00915 return 0; 00916 } 00917 } 00918 00919 00920 int 00921 ncvarrename( 00922 int ncid, 00923 int varid, 00924 const char* name 00925 ) 00926 { 00927 const int status = nc_rename_var(ncid, varid, name); 00928 if(status != NC_NOERR) 00929 { 00930 nc_advise("ncvarrename", status, "ncid %d", ncid); 00931 return -1; 00932 } 00933 return varid; 00934 } 00935 00936 00937 int 00938 ncattput( 00939 int ncid, 00940 int varid, 00941 const char* name, 00942 nc_type datatype, 00943 int len, 00944 const void* value 00945 ) 00946 { 00947 const int status = nc_put_att(ncid, varid, name, datatype, len, value); 00948 if(status != NC_NOERR) 00949 { 00950 nc_advise("ncattput", status, "ncid %d", ncid); 00951 return -1; 00952 } 00953 return 0; 00954 } 00955 00956 00957 int 00958 ncattinq( 00959 int ncid, 00960 int varid, 00961 const char* name, 00962 nc_type* datatype, 00963 int* len 00964 ) 00965 { 00966 size_t ll; 00967 const int status = nc_inq_att(ncid, varid, name, datatype, &ll); 00968 if(status != NC_NOERR) 00969 { 00970 nc_advise("ncattinq", status, 00971 "ncid %d; varid %d; attname \"%s\"", 00972 ncid, varid, name); 00973 return -1; 00974 } 00975 00976 if(len != NULL) 00977 *len = (int) ll; 00978 00979 return 1; 00980 00981 } 00982 00983 00984 int 00985 ncattget( 00986 int ncid, 00987 int varid, 00988 const char* name, 00989 void* value 00990 ) 00991 { 00992 const int status = nc_get_att(ncid, varid, name, value); 00993 if(status != NC_NOERR) 00994 { 00995 nc_advise("ncattget", status, "ncid %d", ncid); 00996 return -1; 00997 } 00998 return 1; 00999 } 01000 01001 01002 int 01003 ncattcopy( 01004 int ncid_in, 01005 int varid_in, 01006 const char* name, 01007 int ncid_out, 01008 int varid_out 01009 ) 01010 { 01011 const int status = nc_copy_att(ncid_in, varid_in, name, ncid_out, varid_out); 01012 if(status != NC_NOERR) 01013 { 01014 nc_advise("ncattcopy", status, "%s", name); 01015 return -1; 01016 } 01017 return 0; 01018 } 01019 01020 01021 int 01022 ncattname( 01023 int ncid, 01024 int varid, 01025 int attnum, 01026 char* name 01027 ) 01028 { 01029 const int status = nc_inq_attname(ncid, varid, attnum, name); 01030 if(status != NC_NOERR) 01031 { 01032 nc_advise("ncattname", status, "ncid %d", ncid); 01033 return -1; 01034 } 01035 return attnum; 01036 } 01037 01038 01039 int 01040 ncattrename( 01041 int ncid, 01042 int varid, 01043 const char* name, 01044 const char* newname 01045 ) 01046 { 01047 const int status = nc_rename_att(ncid, varid, name, newname); 01048 if(status != NC_NOERR) 01049 { 01050 nc_advise("ncattrename", status, "ncid %d", ncid); 01051 return -1; 01052 } 01053 return 1; 01054 } 01055 01056 01057 int 01058 ncattdel( 01059 int ncid, 01060 int varid, 01061 const char* name 01062 ) 01063 { 01064 const int status = nc_del_att(ncid, varid, name); 01065 if(status != NC_NOERR) 01066 { 01067 nc_advise("ncattdel", status, "ncid %d", ncid); 01068 return -1; 01069 } 01070 return 1; 01071 } 01072 01073 #endif /* NO_NETCDF_2 */ 01074 01075 #ifndef NO_NETCDF_2 01076 01077 int 01078 ncsetfill( 01079 int ncid, 01080 int fillmode 01081 ) 01082 { 01083 int oldmode = -1; 01084 const int status = nc_set_fill(ncid, fillmode, &oldmode); 01085 if(status != NC_NOERR) 01086 { 01087 nc_advise("ncsetfill", status, "ncid %d", ncid); 01088 return -1; 01089 } 01090 return oldmode; 01091 } 01092 01093 01094 int 01095 ncrecinq( 01096 int ncid, 01097 int* nrecvars, 01098 int* recvarids, 01099 long* recsizes 01100 ) 01101 { 01102 size_t nrv = 0; 01103 size_t rs[NC_MAX_VARS]; /* TODO */ 01104 const int status = nc_inq_rec(ncid, &nrv, recvarids, rs); 01105 if(status != NC_NOERR) 01106 { 01107 nc_advise("ncrecinq", status, "ncid %d", ncid); 01108 return -1; 01109 } 01110 01111 if(nrecvars != NULL) 01112 *nrecvars = (int) nrv; 01113 01114 if(recsizes != NULL) 01115 { 01116 size_t ii; 01117 for(ii = 0; ii < nrv; ii++) 01118 { 01119 recsizes[ii] = (long) rs[ii]; 01120 } 01121 } 01122 01123 return (int) nrv; 01124 } 01125 01126 01127 int 01128 ncrecget( 01129 int ncid, 01130 long recnum, 01131 void** datap 01132 ) 01133 { 01134 const int status = nc_get_rec(ncid, (size_t)recnum, datap); 01135 if(status != NC_NOERR) 01136 { 01137 nc_advise("ncrecget", status, "ncid %d", ncid); 01138 return -1; 01139 } 01140 return 0; 01141 } 01142 01143 01144 int 01145 ncrecput( 01146 int ncid, 01147 long recnum, 01148 void* const* datap 01149 ) 01150 { 01151 const int status = nc_put_rec(ncid, (size_t)recnum, datap); 01152 if(status != NC_NOERR) 01153 { 01154 nc_advise("ncrecput", status, "ncid %d", ncid); 01155 return -1; 01156 } 01157 return 0; 01158 } 01159 01160 #endif /* NO_NETCDF_2 */