13 #ifdef HAVE_SYS_RESOURCE_H
14 #include <sys/resource.h>
16 #ifdef HAVE_SYS_TYPES_H
17 #include <sys/types.h>
19 #ifdef HAVE_SYS_STAT_H
25 #include "ncdispatch.h"
27 static int nc_initialized = 0;
70 nc_local_initialize(
void)
81 NC_check_file_type(
const char *path,
int use_parallel,
void *mpi_info,
82 int* model,
int* version)
84 char magic[MAGIC_NUMBER_LEN];
97 MPI_Comm comm = MPI_COMM_WORLD;
98 MPI_Info info = MPI_INFO_NULL;
100 if(mpi_info != NULL) {
101 comm = ((NC_MPI_INFO*)mpi_info)->comm;
102 info = ((NC_MPI_INFO*)mpi_info)->info;
104 if((retval = MPI_File_open(comm, (
char *)path, MPI_MODE_RDONLY,info,
105 &fh)) != MPI_SUCCESS)
107 if((retval = MPI_File_read(fh, magic, MAGIC_NUMBER_LEN, MPI_CHAR,
108 &mstatus)) != MPI_SUCCESS)
110 if((retval = MPI_File_close(&fh)) != MPI_SUCCESS)
117 #ifdef HAVE_SYS_STAT_H
121 if(path == NULL || strlen(path)==0)
124 if (!(fp = fopen(path,
"r")))
125 {status = errno;
goto done;}
127 #ifdef HAVE_SYS_STAT_H
131 if(!(fstat(fileno(fp),&st) == 0)) {
137 if(st.st_size < MAGIC_NUMBER_LEN) {
144 i = fread(magic, MAGIC_NUMBER_LEN, 1, fp);
149 {status = errno;
goto done;}
155 if(magic[1] ==
'H' && magic[2] ==
'D' && magic[3] ==
'F') {
156 *model = NC_DISPATCH_NC4;
159 }
else if(magic[0] ==
'\016' && magic[1] ==
'\003'
160 && magic[2] ==
'\023' && magic[3] ==
'\001') {
161 *model = NC_DISPATCH_NC4;
166 if(magic[0] ==
'C' && magic[1] ==
'D' && magic[2] ==
'F') {
167 if(magic[3] ==
'\001')
169 else if(magic[3] ==
'\002')
172 else if(magic[3] ==
'\005')
177 *model = (use_parallel || *version == 5)?NC_DISPATCH_NC5:NC_DISPATCH_NC3;
450 size_t *chunksizehintp,
int *ncidp)
452 return NC_create(path, cmode, initialsz, 0,
453 chunksizehintp, 0, NULL, ncidp);
465 nc__create_mp(
const char *path,
int cmode,
size_t initialsz,
466 int basepe,
size_t *chunksizehintp,
int *ncidp)
468 return NC_create(path, cmode, initialsz, basepe,
469 chunksizehintp, 0, NULL, ncidp);
587 nc_open(
const char *path,
int mode,
int *ncidp)
589 return NC_open(path, mode, 0, NULL, 0, NULL, ncidp);
645 size_t *chunksizehintp,
int *ncidp)
647 return NC_open(path, mode, 0, chunksizehintp, 0,
660 nc__open_mp(
const char *path,
int mode,
int basepe,
661 size_t *chunksizehintp,
int *ncidp)
663 return NC_open(path, mode, basepe, chunksizehintp,
689 if ((stat = NC_check_id(ncid, &ncp)))
691 if(ncp->path == NULL) {
692 if(pathlen) *pathlen = 0;
693 if(path) path[0] =
'\0';
695 if (pathlen) *pathlen = strlen(ncp->path);
696 if (path) strcpy(path, ncp->path);
753 int stat = NC_check_id(ncid, &ncp);
755 return ncp->dispatch->redef(ncid);
818 status = NC_check_id(ncid, &ncp);
819 if(status !=
NC_NOERR)
return status;
820 return ncp->dispatch->_enddef(ncid,0,1,0,1);
905 nc__enddef(
int ncid,
size_t h_minfree,
size_t v_align,
size_t v_minfree,
909 int stat = NC_check_id(ncid, &ncp);
911 return ncp->dispatch->_enddef(ncid,h_minfree,v_align,v_minfree,r_align);
985 int stat = NC_check_id(ncid, &ncp);
987 return ncp->dispatch->sync(ncid);
1036 int stat = NC_check_id(ncid, &ncp);
1045 stat = ncp->dispatch->abort(ncid);
1046 del_from_NCList(ncp);
1095 int stat = NC_check_id(ncid, &ncp);
1100 if(ncp->refcount <= 0)
1103 stat = ncp->dispatch->close(ncid);
1105 del_from_NCList(ncp);
1213 int stat = NC_check_id(ncid, &ncp);
1215 return ncp->dispatch->set_fill(ncid,fillmode,old_modep);
1230 nc_inq_base_pe(
int ncid,
int *pe)
1233 int stat = NC_check_id(ncid, &ncp);
1235 return ncp->dispatch->inq_base_pe(ncid,pe);
1250 nc_set_base_pe(
int ncid,
int pe)
1253 int stat = NC_check_id(ncid, &ncp);
1255 return ncp->dispatch->set_base_pe(ncid,pe);
1280 int stat = NC_check_id(ncid, &ncp);
1282 return ncp->dispatch->inq_format(ncid,formatp);
1314 int stat = NC_check_id(ncid, &ncp);
1316 return ncp->dispatch->inq_format_extended(ncid,formatp,modep);
1364 nc_inq(
int ncid,
int *ndimsp,
int *nvarsp,
int *nattsp,
int *unlimdimidp)
1367 int stat = NC_check_id(ncid, &ncp);
1369 return ncp->dispatch->inq(ncid,ndimsp,nvarsp,nattsp,unlimdimidp);
1373 nc_inq_nvars(
int ncid,
int *nvarsp)
1376 int stat = NC_check_id(ncid, &ncp);
1378 return ncp->dispatch->inq(ncid, NULL, nvarsp, NULL, NULL);
1452 if(xtype <= ATOMICTYPEMAX) {
1454 if(name) strncpy(name,NC_atomictypename(xtype),
NC_MAX_NAME);
1455 if(size) *size = NC_atomictypelen(xtype);
1458 int stat = NC_check_id(ncid, &ncp);
1460 return ncp->dispatch->inq_type(ncid,xtype,name,size);
1500 NC_create(
const char *path,
int cmode,
size_t initialsz,
1501 int basepe,
size_t *chunksizehintp,
int useparallel,
1502 void* mpi_info,
int *ncidp)
1506 NC_Dispatch* dispatcher = NULL;
1517 if ((stat = NC_initialize()))
1520 nc_local_initialize();
1526 ncp = find_in_NCList_by_name(path);
1531 if((isurl = NC_testurl(path)))
1532 model = NC_urlmodel(path);
1538 model = NC_DISPATCH_NC4;
1543 model = NC_DISPATCH_NC5;
1547 model = NC_DISPATCH_NC3;
1552 int format = nc_get_default_format();
1557 model = NC_DISPATCH_NC4;
1561 model = NC_DISPATCH_NC4;
1569 model = NC_DISPATCH_NC3;
1582 if (!(dispatcher = NC_get_dispatch_override()))
1587 #ifdef USE_CDMREMOTE
1588 if(model == (NC_DISPATCH_NC4 | NC_DISPATCH_NCR))
1589 dispatcher = NCCR_dispatch_table;
1592 if(model == (NC_DISPATCH_NC4))
1593 dispatcher = NC4_dispatch_table;
1597 if(model == (NC_DISPATCH_NC3 | NC_DISPATCH_NCD))
1598 dispatcher = NCD2_dispatch_table;
1602 if(model == (NC_DISPATCH_NC5))
1603 dispatcher = NC5_dispatch_table;
1606 if(model == (NC_DISPATCH_NC3))
1607 dispatcher = NC3_dispatch_table;
1613 stat = new_NC(dispatcher,path,cmode,&ncp);
1614 if(stat)
return stat;
1625 if ((stat = dispatcher->create(path, cmode, initialsz, basepe, chunksizehintp,
1626 useparallel, mpi_info, dispatcher, ncp))) {
1627 del_from_NCList(ncp);
1630 if(ncidp)*ncidp = ncp->ext_ncid;
1651 NC_open(
const char *path,
int cmode,
1652 int basepe,
size_t *chunksizehintp,
1653 int useparallel,
void* mpi_info,
1658 NC_Dispatch* dispatcher = NULL;
1664 if(!nc_initialized) {
1665 stat = NC_initialize();
1666 if(stat)
return stat;
1668 nc_local_initialize();
1674 ncp = find_in_NCList_by_name(path);
1677 if(ncidp) *ncidp = ncp->ext_ncid;
1682 isurl = NC_testurl(path);
1684 model = NC_urlmodel(path);
1689 stat = NC_check_file_type(path,useparallel,mpi_info,
1700 fprintf(stderr,
"Model != 0\n");
1707 if(cmode & NC_PNETCDF) model = NC_DISPATCH_NC5;
1708 else if(cmode & NC_NETCDF4) model = NC_DISPATCH_NC4;
1710 if(model == 0) model = NC_DISPATCH_NC3;
1714 if(model & NC_DISPATCH_NC4)
1716 else if(model & NC_DISPATCH_NC3) {
1717 cmode &= ~NC_NETCDF4;
1719 }
else if(model & NC_DISPATCH_NC5) {
1721 It appears that pnetcdf can read NC_64_BIT_OFFSET
1729 if((cmode &
NC_MPIIO && cmode & NC_MPIPOSIX))
1733 dispatcher = NC_get_dispatch_override();
1734 if(dispatcher != NULL)
goto havetable;
1737 #if defined(USE_CDMREMOTE)
1738 if(model == (NC_DISPATCH_NC4 | NC_DISPATCH_NCR))
1739 dispatcher = NCCR_dispatch_table;
1742 #if defined(USE_DAP)
1743 if(model == (NC_DISPATCH_NC3 | NC_DISPATCH_NCD))
1744 dispatcher = NCD2_dispatch_table;
1747 #if defined(USE_PNETCDF)
1748 if(model == (NC_DISPATCH_NC5))
1749 dispatcher = NC5_dispatch_table;
1752 #if defined(USE_NETCDF4)
1753 if(model == (NC_DISPATCH_NC4))
1754 dispatcher = NC4_dispatch_table;
1757 if(model == (NC_DISPATCH_NC3))
1758 dispatcher = NC3_dispatch_table;
1765 stat = new_NC(dispatcher,path,cmode,&ncp);
1766 if(stat)
return stat;
1777 stat = dispatcher->open(path, cmode, basepe, chunksizehintp,
1778 useparallel, mpi_info, dispatcher, ncp);
1780 if(ncidp) *ncidp = ncp->ext_ncid;
1782 del_from_NCList(ncp);
1793 static int pseudofd = 0;
1803 #ifdef HAVE_GETRLIMIT
1805 if(getrlimit(RLIMIT_NOFILE,&rl) == 0) {
1806 if(rl.rlim_max != RLIM_INFINITY)
1807 maxfd = (int)rl.rlim_max;
1808 if(rl.rlim_cur != RLIM_INFINITY)
1809 maxfd = (int)rl.rlim_cur;
#define NC_PNETCDF
Use parallel-netcdf library.
int nc__open(const char *path, int mode, size_t *chunksizehintp, int *ncidp)
Open a netCDF file with extra performance parameters for the classic library.
#define NC_ENFILE
Too many netcdfs open.
#define NC_CLASSIC_MODEL
Enforce classic model.
#define NC_MAX_VAR_DIMS
max per variable dimensions
int nc_redef(int ncid)
Put open netcdf dataset into define mode.
int nc__enddef(int ncid, size_t h_minfree, size_t v_align, size_t v_minfree, size_t r_align)
Leave define mode with performance tuning.
#define NC_MPIIO
Turn on MPI I/O.
int nc_inq_format(int ncid, int *formatp)
Inquire about the binary format of a netCDF file as presented by the API.
int nc_inq(int ncid, int *ndimsp, int *nvarsp, int *nattsp, int *unlimdimidp)
Inquire about a file or group.
int nc_type
The nc_type type is just an int.
#define NC_64BIT_OFFSET
Use large (64-bit) file offsets.
int nc_inq_format_extended(int ncid, int *formatp, int *modep)
Obtain more detailed (vis-a-vis nc_inq_format) format information about an open dataset.
#define NC_ENOTNC
Not a netcdf file.
int nc_close(int ncid)
Close an open netCDF dataset.
#define NC_FORMAT_64BIT
Format specifier for nc_set_default_format() and returned by nc_inq_format.
#define NC_EBADTYPE
Not a netcdf data type.
#define NC_SIZEHINT_DEFAULT
Let nc__create() or nc__open() figure out a suitable buffer size.
#define NC_EINVAL
Invalid Argument.
int nc_set_fill(int ncid, int fillmode, int *old_modep)
Change the fill-value mode to improve write performance.
#define NC_MAX_NAME
Maximum for classic library.
#define NC_NAT
Not A Type.
int nc_inq_type(int ncid, nc_type xtype, char *name, size_t *size)
Inquire about a type.
#define NC_EPARINIT
Error initializing for parallel access.
#define NC_NETCDF4
Use netCDF-4/HDF5 format.
int nc__create(const char *path, int cmode, size_t initialsz, size_t *chunksizehintp, int *ncidp)
Create a netCDF file with some extra parameters controlling classic file cacheing.
#define NC_FORMAT_NETCDF4_CLASSIC
Format specifier for nc_set_default_format() and returned by nc_inq_format.
#define NC_FORMAT_NETCDF4
Format specifier for nc_set_default_format() and returned by nc_inq_format.
int nc_inq_path(int ncid, size_t *pathlen, char *path)
Get the file pathname (or the opendap URL) which was used to open/create the ncid's file...
#define NC_NOERR
No Error.
int nc_open(const char *path, int mode, int *ncidp)
Open an existing netCDF file.
int nc_enddef(int ncid)
Leave define mode.
#define NC_FORMAT_CLASSIC
Format specifier for nc_set_default_format() and returned by nc_inq_format.
int nc_sync(int ncid)
Synchronize an open netcdf dataset to disk.
int nc_create(const char *path, int cmode, int *ncidp)
Create a new netCDF file.
#define NC_MPIPOSIX
Turn on MPI POSIX I/O.