00001
00002
00003
00004
00005
00006 #include "nc.h"
00007
00008 #ifndef NO_NETCDF_2
00009
00010 #if SIZEOF_LONG == SIZEOF_SIZE_T
00011
00012
00013
00014
00015
00016 # define NDIMS_DECL
00017 # define A_DECL(name, type, ndims, rhs) \
00018 const type *const name = ((const type *)(rhs))
00019
00020 # define A_FREE(name)
00021
00022 # define A_INIT(lhs, type, ndims, rhs)
00023
00024 #else
00025
00026
00027
00028
00029
00030
00031 # include "onstack.h"
00032
00033 static size_t
00034 nvdims(int ncid, int varid)
00035 {
00036 NC *ncp;
00037 if(NC_check_id(ncid, &ncp) != NC_NOERR)
00038 return 0;
00039 {
00040 const NC_var *const varp = NC_lookupvar(ncp, varid);
00041 if(varp == NULL)
00042 return 0;
00043 return varp->ndims;
00044 }
00045 }
00046
00047 #define NDIMS_DECL const size_t ndims = nvdims(ncid, varid);
00048
00049 # define A_DECL(name, type, ndims, rhs) \
00050 ALLOC_ONSTACK(name, type, ndims)
00051
00052 # define A_FREE(name) \
00053 FREE_ONSTACK(name)
00054
00055 # define A_INIT(lhs, type, ndims, rhs) \
00056 { \
00057 const long *lp = rhs; \
00058 type *tp = lhs; \
00059 type *const end = lhs + ndims; \
00060 while(tp < end) \
00061 { \
00062 *tp++ = (type) *lp++; \
00063 } \
00064 }
00065
00066
00067 #endif
00068
00069
00070
00071
00072
00073
00074
00075 int ncerr = NC_NOERR ;
00076
00077
00078
00079
00080
00081
00082 int ncopts = (NC_FATAL | NC_VERBOSE) ;
00083
00084
00085
00086
00087
00088 const char *cdf_routine_name;
00089
00090
00091
00092
00093
00094
00095 #include <stdio.h>
00096 #include <stdlib.h>
00097 #include <stdarg.h>
00098
00099
00100
00101
00102 void
00103 nc_advise(const char *routine_name, int err, const char *fmt,...)
00104 {
00105 va_list args;
00106
00107 if(NC_ISSYSERR(err))
00108 ncerr = NC_SYSERR;
00109 else
00110 ncerr = err;
00111
00112 if( ncopts & NC_VERBOSE )
00113 {
00114 (void) fprintf(stderr,"%s: ", routine_name);
00115 va_start(args ,fmt);
00116 (void) vfprintf(stderr,fmt,args);
00117 va_end(args);
00118 if(err != NC_NOERR)
00119 {
00120 (void) fprintf(stderr,": %s",
00121 nc_strerror(err));
00122 }
00123 (void) fputc('\n',stderr);
00124 (void) fflush(stderr);
00125 }
00126
00127 if( (ncopts & NC_FATAL) && err != NC_NOERR )
00128 {
00129 exit(ncopts);
00130 }
00131 }
00132
00133
00134
00135
00136
00137 void
00138 NCadvise(int err, char *fmt,...)
00139 {
00140 va_list args;
00141
00142 va_start(args ,fmt);
00143 nc_advise(cdf_routine_name, err, fmt, args);
00144 va_end(args);
00145 }
00146
00147
00148
00149
00150 int
00151 nccreate(const char* path, int cmode)
00152 {
00153 int ncid;
00154 const int status = nc_create(path, cmode, &ncid);
00155 if(status != NC_NOERR)
00156 {
00157 nc_advise("nccreate", status, "filename \"%s\"", path);
00158 return -1;
00159 }
00160 return ncid;
00161 }
00162
00163
00164 int
00165 ncopen(const char *path, int mode)
00166 {
00167 int ncid;
00168 const int status = nc_open(path, mode, &ncid);
00169 if(status != NC_NOERR)
00170 {
00171 nc_advise("ncopen", status, "filename \"%s\"", path);
00172 return -1;
00173 }
00174 return ncid;
00175 }
00176
00177
00178 int
00179 ncredef(int ncid)
00180 {
00181 const int status = nc_redef(ncid);
00182 if(status != NC_NOERR)
00183 {
00184 nc_advise("ncredef", status, "ncid %d", ncid);
00185 return -1;
00186 }
00187 return 0;
00188 }
00189
00190
00191 int
00192 ncendef(int ncid)
00193 {
00194 const int status = nc_enddef(ncid);
00195 if(status != NC_NOERR)
00196 {
00197 nc_advise("ncendef", status, "ncid %d", ncid);
00198 return -1;
00199 }
00200 return 0;
00201 }
00202
00203
00204 int
00205 ncclose(int ncid)
00206 {
00207 const int status = nc_close(ncid);
00208 if(status != NC_NOERR)
00209 {
00210 nc_advise("ncclose", status, "ncid %d", ncid);
00211 return -1;
00212
00213 }
00214 return 0;
00215 }
00216
00217
00218 int
00219 ncinquire(
00220 int ncid,
00221 int* ndims,
00222 int* nvars,
00223 int* natts,
00224 int* recdim
00225 )
00226 {
00227 int nd, nv, na;
00228 const int status = nc_inq(ncid, &nd, &nv, &na, recdim);
00229
00230 if(status != NC_NOERR)
00231 {
00232 nc_advise("ncinquire", status, "ncid %d", ncid);
00233 return -1;
00234 }
00235
00236
00237 if(ndims != NULL)
00238 *ndims = (int) nd;
00239
00240 if(nvars != NULL)
00241 *nvars = (int) nv;
00242
00243 if(natts != NULL)
00244 *natts = (int) na;
00245
00246 return ncid;
00247 }
00248
00249
00250 int
00251 ncsync(int ncid)
00252 {
00253 const int status = nc_sync(ncid);
00254 if(status != NC_NOERR)
00255 {
00256 nc_advise("ncsync", status, "ncid %d", ncid);
00257 return -1;
00258
00259 }
00260 return 0;
00261 }
00262
00263
00264 int
00265 ncabort(int ncid)
00266 {
00267 const int status = nc_abort(ncid);
00268 if(status != NC_NOERR)
00269 {
00270 nc_advise("ncabort", status, "ncid %d", ncid);
00271 return -1;
00272 }
00273 return 0;
00274 }
00275
00276
00277 int
00278 ncdimdef(
00279 int ncid,
00280 const char* name,
00281 long length
00282 )
00283 {
00284 int dimid;
00285 const int status = nc_def_dim(ncid, name, (size_t)length, &dimid);
00286 if(status != NC_NOERR)
00287 {
00288 nc_advise("ncdimdef", status, "ncid %d", ncid);
00289 return -1;
00290 }
00291 return dimid;
00292 }
00293
00294
00295 int
00296 ncdimid(int ncid, const char* name)
00297 {
00298 int dimid;
00299 const int status = nc_inq_dimid(ncid, name, &dimid);
00300 if(status != NC_NOERR)
00301 {
00302 nc_advise("ncdimid", status, "ncid %d", ncid);
00303 return -1;
00304 }
00305 return dimid;
00306 }
00307
00308
00309 int
00310 ncdiminq(
00311 int ncid,
00312 int dimid,
00313 char* name,
00314 long* length
00315 )
00316 {
00317 size_t ll;
00318 const int status = nc_inq_dim(ncid, dimid, name, &ll);
00319
00320 if(status != NC_NOERR)
00321 {
00322 nc_advise("ncdiminq", status, "ncid %d", ncid);
00323 return -1;
00324 }
00325
00326
00327 if(length != NULL)
00328 *length = (int) ll;
00329
00330 return dimid;
00331 }
00332
00333
00334 int
00335 ncdimrename(
00336 int ncid,
00337 int dimid,
00338 const char* name
00339 )
00340 {
00341 const int status = nc_rename_dim(ncid, dimid, name);
00342 if(status != NC_NOERR)
00343 {
00344 nc_advise("ncdimrename", status, "ncid %d", ncid);
00345 return -1;
00346 }
00347 return dimid;
00348 }
00349
00350
00351 int
00352 ncvardef(
00353 int ncid,
00354 const char* name,
00355 nc_type datatype,
00356 int ndims,
00357 const int* dim
00358 )
00359 {
00360 int varid = -1;
00361 const int status = nc_def_var(ncid, name, datatype, ndims, dim, &varid);
00362 if(status != NC_NOERR)
00363 {
00364 nc_advise("ncvardef", status, "ncid %d", ncid);
00365 return -1;
00366 }
00367 return varid;
00368 }
00369
00370
00371 int
00372 ncvarid(
00373 int ncid,
00374 const char* name
00375 )
00376 {
00377 int varid = -1;
00378 const int status = nc_inq_varid(ncid, name, &varid);
00379 if(status != NC_NOERR)
00380 {
00381 nc_advise("ncvarid", status, "ncid %d", ncid);
00382 return -1;
00383 }
00384 return varid;
00385 }
00386
00387
00388 int
00389 ncvarinq(
00390 int ncid,
00391 int varid,
00392 char* name,
00393 nc_type* datatype,
00394 int* ndims,
00395 int* dim,
00396 int* natts
00397 )
00398 {
00399 int nd, na;
00400 const int status = nc_inq_var(ncid, varid, name, datatype,
00401 &nd, dim, &na);
00402
00403 if(status != NC_NOERR)
00404 {
00405 nc_advise("ncvarinq", status, "ncid %d", ncid);
00406 return -1;
00407 }
00408
00409
00410 if(ndims != NULL)
00411 *ndims = (int) nd;
00412
00413 if(natts != NULL)
00414 *natts = (int) na;
00415
00416 return varid;
00417 }
00418
00419
00420 int
00421 ncvarput1(
00422 int ncid,
00423 int varid,
00424 const long* index,
00425 const void* value
00426 )
00427 {
00428 NDIMS_DECL
00429 A_DECL(coordp, size_t, ndims, index);
00430 A_INIT(coordp, size_t, ndims, index);
00431 {
00432 const int status = nc_put_var1(ncid, varid, coordp, value);
00433 A_FREE(coordp);
00434 if(status != NC_NOERR)
00435 {
00436 nc_advise("ncvarput1", status, "ncid %d", ncid);
00437 return -1;
00438 }
00439 }
00440 return 0;
00441 }
00442
00443
00444 int
00445 ncvarget1(
00446 int ncid,
00447 int varid,
00448 const long* index,
00449 void* value
00450 )
00451 {
00452 NDIMS_DECL
00453 A_DECL(coordp, size_t, ndims, index);
00454 A_INIT(coordp, size_t, ndims, index);
00455 {
00456 const int status = nc_get_var1(ncid, varid, coordp, value);
00457 A_FREE(coordp);
00458 if(status != NC_NOERR)
00459 {
00460 nc_advise("ncdimid", status, "ncid %d", ncid);
00461 return -1;
00462 }
00463 }
00464 return 0;
00465 }
00466
00467
00468 int
00469 ncvarput(
00470 int ncid,
00471 int varid,
00472 const long* start,
00473 const long* count,
00474 const void* value
00475 )
00476 {
00477 NDIMS_DECL
00478 A_DECL(stp, size_t, ndims, start);
00479 A_DECL(cntp, size_t, ndims, count);
00480 A_INIT(stp, size_t, ndims, start);
00481 A_INIT(cntp, size_t, ndims, count);
00482 {
00483 const int status = nc_put_vara(ncid, varid, stp, cntp, value);
00484 A_FREE(cntp);
00485 A_FREE(stp);
00486 if(status != NC_NOERR)
00487 {
00488 nc_advise("ncvarput", status, "ncid %d", ncid);
00489 return -1;
00490 }
00491 }
00492 return 0;
00493 }
00494
00495
00496 int
00497 ncvarget(
00498 int ncid,
00499 int varid,
00500 const long* start,
00501 const long* count,
00502 void* value
00503 )
00504 {
00505 NDIMS_DECL
00506 A_DECL(stp, size_t, ndims, start);
00507 A_DECL(cntp, size_t, ndims, count);
00508 A_INIT(stp, size_t, ndims, start);
00509 A_INIT(cntp, size_t, ndims, count);
00510 {
00511 const int status = nc_get_vara(ncid, varid, stp, cntp, value);
00512 A_FREE(cntp);
00513 A_FREE(stp);
00514 if(status != NC_NOERR)
00515 {
00516 nc_advise("ncvarget", status, "ncid %d", ncid);
00517 return -1;
00518 }
00519 }
00520 return 0;
00521 }
00522
00523
00524 int
00525 ncvarputs(
00526 int ncid,
00527 int varid,
00528 const long* start,
00529 const long* count,
00530 const long* stride,
00531 const void* value
00532 )
00533 {
00534 if(stride == NULL)
00535 return ncvarput(ncid, varid, start, count, value);
00536
00537 {
00538 NDIMS_DECL
00539 A_DECL(stp, size_t, ndims, start);
00540 A_DECL(cntp, size_t, ndims, count);
00541 A_DECL(strdp, ptrdiff_t, ndims, stride);
00542 A_INIT(stp, size_t, ndims, start);
00543 A_INIT(cntp, size_t, ndims, count);
00544 A_INIT(strdp, ptrdiff_t, ndims, stride);
00545 {
00546 const int status = nc_put_vars(ncid, varid, stp, cntp, strdp, value);
00547 A_FREE(strdp);
00548 A_FREE(cntp);
00549 A_FREE(stp);
00550 if(status != NC_NOERR)
00551 {
00552 nc_advise("ncvarputs", status, "ncid %d", ncid);
00553 return -1;
00554 }
00555 }
00556 return 0;
00557 }
00558 }
00559
00560
00561 int
00562 ncvargets(
00563 int ncid,
00564 int varid,
00565 const long* start,
00566 const long* count,
00567 const long* stride,
00568 void* value
00569 )
00570 {
00571 if(stride == NULL)
00572 return ncvarget(ncid, varid, start, count, value);
00573
00574 {
00575 NDIMS_DECL
00576 A_DECL(stp, size_t, ndims, start);
00577 A_DECL(cntp, size_t, ndims, count);
00578 A_DECL(strdp, ptrdiff_t, ndims, stride);
00579 A_INIT(stp, size_t, ndims, start);
00580 A_INIT(cntp, size_t, ndims, count);
00581 A_INIT(strdp, ptrdiff_t, ndims, stride);
00582 {
00583 const int status = nc_get_vars(ncid, varid, stp, cntp, strdp, value);
00584 A_FREE(strdp);
00585 A_FREE(cntp);
00586 A_FREE(stp);
00587 if(status != NC_NOERR)
00588 {
00589 nc_advise("ncvargets", status, "ncid %d", ncid);
00590 return -1;
00591 }
00592 }
00593 return 0;
00594 }
00595 }
00596
00597
00598 int
00599 ncvarputg(
00600 int ncid,
00601 int varid,
00602 const long* start,
00603 const long* count,
00604 const long* stride,
00605 const long* map,
00606 const void* value
00607 )
00608 {
00609 if(map == NULL)
00610 return ncvarputs(ncid, varid, start, count, stride, value);
00611
00612 {
00613 NDIMS_DECL
00614 A_DECL(stp, size_t, ndims, start);
00615 A_DECL(cntp, size_t, ndims, count);
00616 A_DECL(strdp, ptrdiff_t, ndims, stride);
00617 A_DECL(imp, ptrdiff_t, ndims, map);
00618 A_INIT(stp, size_t, ndims, start);
00619 A_INIT(cntp, size_t, ndims, count);
00620 A_INIT(strdp, ptrdiff_t, ndims, stride);
00621 A_INIT(imp, ptrdiff_t, ndims, map);
00622 {
00623 const int status = nc_put_varm(ncid, varid,
00624 stp, cntp, strdp, imp, value);
00625 A_FREE(imp);
00626 A_FREE(strdp);
00627 A_FREE(cntp);
00628 A_FREE(stp);
00629 if(status != NC_NOERR)
00630 {
00631 nc_advise("ncvarputg", status, "ncid %d", ncid);
00632 return -1;
00633 }
00634 }
00635 return 0;
00636 }
00637 }
00638
00639
00640 int
00641 ncvargetg(
00642 int ncid,
00643 int varid,
00644 const long* start,
00645 const long* count,
00646 const long* stride,
00647 const long* map,
00648 void* value
00649 )
00650 {
00651 if(map == NULL)
00652 return ncvargets(ncid, varid, start, count, stride, value);
00653
00654 {
00655 NDIMS_DECL
00656 A_DECL(stp, size_t, ndims, start);
00657 A_DECL(cntp, size_t, ndims, count);
00658 A_DECL(strdp, ptrdiff_t, ndims, stride);
00659 A_DECL(imp, ptrdiff_t, ndims, map);
00660 A_INIT(stp, size_t, ndims, start);
00661 A_INIT(cntp, size_t, ndims, count);
00662 A_INIT(strdp, ptrdiff_t, ndims, stride);
00663 A_INIT(imp, ptrdiff_t, ndims, map);
00664 {
00665 const int status = nc_get_varm(ncid, varid,
00666 stp, cntp, strdp, imp, value);
00667 A_FREE(imp);
00668 A_FREE(strdp);
00669 A_FREE(cntp);
00670 A_FREE(stp);
00671 if(status != NC_NOERR)
00672 {
00673 nc_advise("ncvargetg", status, "ncid %d", ncid);
00674 return -1;
00675 }
00676 }
00677 return 0;
00678 }
00679 }
00680
00681
00682 int
00683 ncvarrename(
00684 int ncid,
00685 int varid,
00686 const char* name
00687 )
00688 {
00689 const int status = nc_rename_var(ncid, varid, name);
00690 if(status != NC_NOERR)
00691 {
00692 nc_advise("ncvarrename", status, "ncid %d", ncid);
00693 return -1;
00694 }
00695 return varid;
00696 }
00697
00698
00699 int
00700 ncattput(
00701 int ncid,
00702 int varid,
00703 const char* name,
00704 nc_type datatype,
00705 int len,
00706 const void* value
00707 )
00708 {
00709 const int status = nc_put_att(ncid, varid, name, datatype, len, value);
00710 if(status != NC_NOERR)
00711 {
00712 nc_advise("ncattput", status, "ncid %d", ncid);
00713 return -1;
00714 }
00715 return 0;
00716 }
00717
00718
00719 int
00720 ncattinq(
00721 int ncid,
00722 int varid,
00723 const char* name,
00724 nc_type* datatype,
00725 int* len
00726 )
00727 {
00728 size_t ll;
00729 const int status = nc_inq_att(ncid, varid, name, datatype, &ll);
00730 if(status != NC_NOERR)
00731 {
00732 nc_advise("ncattinq", status, "ncid %d", ncid);
00733 return -1;
00734 }
00735
00736 if(len != NULL)
00737 *len = (int) ll;
00738
00739 return 1;
00740
00741 }
00742
00743
00744 int
00745 ncattget(
00746 int ncid,
00747 int varid,
00748 const char* name,
00749 void* value
00750 )
00751 {
00752 const int status = nc_get_att(ncid, varid, name, value);
00753 if(status != NC_NOERR)
00754 {
00755 nc_advise("ncattget", status, "ncid %d", ncid);
00756 return -1;
00757 }
00758 return 1;
00759 }
00760
00761
00762 int
00763 ncattcopy(
00764 int ncid_in,
00765 int varid_in,
00766 const char* name,
00767 int ncid_out,
00768 int varid_out
00769 )
00770 {
00771 const int status = nc_copy_att(ncid_in, varid_in, name, ncid_out, varid_out);
00772 if(status != NC_NOERR)
00773 {
00774 nc_advise("ncattcopy", status, "%s", name);
00775 return -1;
00776 }
00777 return 0;
00778 }
00779
00780
00781 int
00782 ncattname(
00783 int ncid,
00784 int varid,
00785 int attnum,
00786 char* name
00787 )
00788 {
00789 const int status = nc_inq_attname(ncid, varid, attnum, name);
00790 if(status != NC_NOERR)
00791 {
00792 nc_advise("ncattname", status, "ncid %d", ncid);
00793 return -1;
00794 }
00795 return attnum;
00796 }
00797
00798
00799 int
00800 ncattrename(
00801 int ncid,
00802 int varid,
00803 const char* name,
00804 const char* newname
00805 )
00806 {
00807 const int status = nc_rename_att(ncid, varid, name, newname);
00808 if(status != NC_NOERR)
00809 {
00810 nc_advise("ncattrename", status, "ncid %d", ncid);
00811 return -1;
00812 }
00813 return 1;
00814 }
00815
00816
00817 int
00818 ncattdel(
00819 int ncid,
00820 int varid,
00821 const char* name
00822 )
00823 {
00824 const int status = nc_del_att(ncid, varid, name);
00825 if(status != NC_NOERR)
00826 {
00827 nc_advise("ncattdel", status, "ncid %d", ncid);
00828 return -1;
00829 }
00830 return 1;
00831 }
00832
00833 #endif
00834
00835
00836
00837
00838
00839
00840
00841 int
00842 nctypelen(nc_type type)
00843 {
00844 switch(type){
00845 case NC_BYTE :
00846 case NC_CHAR :
00847 return((int)sizeof(char));
00848 case NC_SHORT :
00849 return(int)(sizeof(short));
00850 case NC_INT :
00851 return((int)sizeof(int));
00852 case NC_FLOAT :
00853 return((int)sizeof(float));
00854 case NC_DOUBLE :
00855 return((int)sizeof(double));
00856 }
00857
00858 #ifndef NO_NETCDF_2
00859
00860 nc_advise("nctypelen", NC_EBADTYPE, "Unknown type %d",
00861 (int)type);
00862 #endif
00863 return -1;
00864 }
00865
00866 #ifndef NO_NETCDF_2
00867
00868 int
00869 ncsetfill(
00870 int ncid,
00871 int fillmode
00872 )
00873 {
00874 int oldmode = -1;
00875 const int status = nc_set_fill(ncid, fillmode, &oldmode);
00876 if(status != NC_NOERR)
00877 {
00878 nc_advise("ncsetfill", status, "ncid %d", ncid);
00879 return -1;
00880 }
00881 return oldmode;
00882 }
00883
00884
00885 int
00886 ncrecinq(
00887 int ncid,
00888 int* nrecvars,
00889 int* recvarids,
00890 long* recsizes
00891 )
00892 {
00893 size_t nrv = 0;
00894 size_t rs[NC_MAX_VARS];
00895 const int status = nc_inq_rec(ncid, &nrv, recvarids, rs);
00896 if(status != NC_NOERR)
00897 {
00898 nc_advise("ncrecinq", status, "ncid %d", ncid);
00899 return -1;
00900 }
00901
00902 if(nrecvars != NULL)
00903 *nrecvars = (int) nrv;
00904
00905 if(recsizes != NULL)
00906 {
00907 size_t ii;
00908 for(ii = 0; ii < nrv; ii++)
00909 {
00910 recsizes[ii] = (long) rs[ii];
00911 }
00912 }
00913
00914 return (int) nrv;
00915 }
00916
00917
00918 int
00919 ncrecget(
00920 int ncid,
00921 long recnum,
00922 void** datap
00923 )
00924 {
00925 const int status = nc_get_rec(ncid, (size_t)recnum, datap);
00926 if(status != NC_NOERR)
00927 {
00928 nc_advise("ncrecget", status, "ncid %d", ncid);
00929 return -1;
00930 }
00931 return 0;
00932 }
00933
00934
00935 int
00936 ncrecput(
00937 int ncid,
00938 long recnum,
00939 void* const* datap
00940 )
00941 {
00942 const int status = nc_put_rec(ncid, (size_t)recnum, datap);
00943 if(status != NC_NOERR)
00944 {
00945 nc_advise("ncrecput", status, "ncid %d", ncid);
00946 return -1;
00947 }
00948 return 0;
00949 }
00950
00951 #endif