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;
66 size_t* NC_coord_zero;
70 nc_local_initialize(
void)
74 if(NC_coord_zero == NULL) abort();
76 if(NC_coord_one == NULL) abort();
84 NC_check_file_type(
const char *path,
int use_parallel,
void *mpi_info,
87 char magic[MAGIC_NUMBER_LEN];
93 #ifdef USE_PARALLEL_MPIO
103 if(mpi_info != NULL) {
104 comm = ((NC_MPI_INFO*)mpi_info)->comm;
105 info = ((NC_MPI_INFO*)mpi_info)->info;
107 if((retval = MPI_File_open(comm, (
char *)path, MPI_MODE_RDONLY,info,
108 &fh)) != MPI_SUCCESS)
110 if((retval = MPI_File_read(fh, magic, MAGIC_NUMBER_LEN, MPI_CHAR,
111 &status)) != MPI_SUCCESS)
113 if((retval = MPI_File_close(&fh)) != MPI_SUCCESS)
122 #ifdef HAVE_SYS_STAT_H
126 if(path == NULL || strlen(path)==0)
129 if (!(fp = fopen(path,
"r")))
132 #ifdef HAVE_SYS_STAT_H
136 if(!(fstat(fileno(fp),&st) == 0))
139 if(st.st_size < MAGIC_NUMBER_LEN)
144 i = fread(magic, MAGIC_NUMBER_LEN, 1, fp);
152 if(magic[1] ==
'H' && magic[2] ==
'D' && magic[3] ==
'F')
154 else if(magic[0] ==
'\016' && magic[1] ==
'\003'
155 && magic[2] ==
'\023' && magic[3] ==
'\001')
157 else if(magic[0] ==
'C' && magic[1] ==
'D' && magic[2] ==
'F')
159 if(magic[3] ==
'\001')
161 else if(magic[3] ==
'\002')
436 size_t *chunksizehintp,
int *ncidp)
438 return NC_create(path, cmode, initialsz, 0,
439 chunksizehintp, 0, NULL, ncidp);
451 nc__create_mp(
const char *path,
int cmode,
size_t initialsz,
452 int basepe,
size_t *chunksizehintp,
int *ncidp)
454 return NC_create(path, cmode, initialsz, basepe,
455 chunksizehintp, 0, NULL, ncidp);
570 nc_open(
const char *path,
int mode,
int *ncidp)
572 return NC_open(path, mode, 0, NULL, 0, NULL, ncidp);
628 size_t *chunksizehintp,
int *ncidp)
630 return NC_open(path, mode, 0, chunksizehintp, 0,
643 nc__open_mp(
const char *path,
int mode,
int basepe,
644 size_t *chunksizehintp,
int *ncidp)
646 return NC_open(path, mode, basepe, chunksizehintp,
672 if ((stat = NC_check_id(ncid, &ncp)))
674 if(ncp->path == NULL) {
675 if(pathlen) *pathlen = 0;
676 if(path) path[0] =
'\0';
678 if (pathlen) *pathlen = strlen(ncp->path);
679 if (path) strcpy(path, ncp->path);
736 int stat = NC_check_id(ncid, &ncp);
738 return ncp->dispatch->redef(ncid);
801 status = NC_check_id(ncid, &ncp);
802 if(status !=
NC_NOERR)
return status;
803 return ncp->dispatch->_enddef(ncid,0,1,0,1);
888 nc__enddef(
int ncid,
size_t h_minfree,
size_t v_align,
size_t v_minfree,
892 int stat = NC_check_id(ncid, &ncp);
894 return ncp->dispatch->_enddef(ncid,h_minfree,v_align,v_minfree,r_align);
968 int stat = NC_check_id(ncid, &ncp);
970 return ncp->dispatch->sync(ncid);
1019 int stat = NC_check_id(ncid, &ncp);
1021 stat = ncp->dispatch->abort(ncid);
1022 del_from_NCList(ncp);
1071 int stat = NC_check_id(ncid, &ncp);
1073 stat = ncp->dispatch->close(ncid);
1075 del_from_NCList(ncp);
1182 int stat = NC_check_id(ncid, &ncp);
1184 return ncp->dispatch->set_fill(ncid,fillmode,old_modep);
1199 nc_inq_base_pe(
int ncid,
int *pe)
1202 int stat = NC_check_id(ncid, &ncp);
1204 return ncp->dispatch->inq_base_pe(ncid,pe);
1219 nc_set_base_pe(
int ncid,
int pe)
1222 int stat = NC_check_id(ncid, &ncp);
1224 return ncp->dispatch->set_base_pe(ncid,pe);
1248 int stat = NC_check_id(ncid, &ncp);
1250 return ncp->dispatch->inq_format(ncid,formatp);
1298 nc_inq(
int ncid,
int *ndimsp,
int *nvarsp,
int *nattsp,
int *unlimdimidp)
1301 int stat = NC_check_id(ncid, &ncp);
1303 return ncp->dispatch->inq(ncid,ndimsp,nvarsp,nattsp,unlimdimidp);
1307 nc_inq_nvars(
int ncid,
int *nvarsp)
1310 int stat = NC_check_id(ncid, &ncp);
1312 return ncp->dispatch->inq(ncid, NULL, nvarsp, NULL, NULL);
1386 if(xtype <= ATOMICTYPEMAX) {
1388 if(name) strncpy(name,NC_atomictypename(xtype),
NC_MAX_NAME);
1389 if(size) *size = NC_atomictypelen(xtype);
1392 int stat = NC_check_id(ncid, &ncp);
1394 return ncp->dispatch->inq_type(ncid,xtype,name,size);
1434 NC_create(
const char *path,
int cmode,
size_t initialsz,
1435 int basepe,
size_t *chunksizehintp,
int useparallel,
1436 void* mpi_info,
int *ncidp)
1440 NC_Dispatch* dispatcher = NULL;
1451 if ((stat = NC_initialize()))
1454 nc_local_initialize();
1458 if((isurl = NC_testurl(path)))
1459 model = NC_urlmodel(path);
1464 model = NC_DISPATCH_NC4;
1466 model = NC_DISPATCH_NC3;
1471 int format = nc_get_default_format();
1476 model = NC_DISPATCH_NC4;
1480 model = NC_DISPATCH_NC4;
1488 model = NC_DISPATCH_NC3;
1501 if (!(dispatcher = NC_get_dispatch_override()))
1506 #ifdef USE_CDMREMOTE
1507 if(model == (NC_DISPATCH_NC4 | NC_DISPATCH_NCR))
1508 dispatcher = NCCR_dispatch_table;
1511 if(model == (NC_DISPATCH_NC4))
1512 dispatcher = NC4_dispatch_table;
1516 if(model == (NC_DISPATCH_NC3 | NC_DISPATCH_NCD))
1517 dispatcher = NCD3_dispatch_table;
1520 if(model == (NC_DISPATCH_NC3))
1521 dispatcher = NC3_dispatch_table;
1527 stat = new_NC(dispatcher,path,&ncp);
1528 if(stat)
return stat;
1534 if ((stat = dispatcher->create(path, cmode, initialsz, basepe, chunksizehintp,
1535 useparallel, mpi_info, dispatcher, ncp))) {
1536 del_from_NCList(ncp);
1539 if(ncidp)*ncidp = ncp->ext_ncid;
1560 NC_open(
const char *path,
int cmode,
1561 int basepe,
size_t *chunksizehintp,
1562 int useparallel,
void* mpi_info,
1567 NC_Dispatch* dispatcher = NULL;
1574 if(!nc_initialized) {
1575 stat = NC_initialize();
1576 if(stat)
return stat;
1578 nc_local_initialize();
1582 isurl = NC_testurl(path);
1584 model = NC_urlmodel(path);
1588 stat = NC_check_file_type(path,useparallel,mpi_info,&cdfversion,&hdfversion);
1590 if(hdfversion != 0) {
1591 model = NC_DISPATCH_NC4;
1592 }
else if(cdfversion != 0) {
1593 model = NC_DISPATCH_NC3;
1601 if(cmode &
NC_NETCDF4 || cmode & NC_PNETCDF) model |= NC_DISPATCH_NC4;
1604 if(model == 0) model = NC_DISPATCH_NC3;
1607 if(model & NC_DISPATCH_NC4)
1609 else if(model & NC_DISPATCH_NC3) {
1614 if((cmode &
NC_MPIIO && cmode & NC_MPIPOSIX))
1618 dispatcher = NC_get_dispatch_override();
1619 if(dispatcher != NULL)
goto havetable;
1622 #if defined(USE_CDMREMOTE)
1623 if(model == (NC_DISPATCH_NC4 | NC_DISPATCH_NCR))
1624 dispatcher = NCCR_dispatch_table;
1627 #if defined(USE_DAP)
1628 if(model == (NC_DISPATCH_NC3 | NC_DISPATCH_NCD))
1629 dispatcher = NCD3_dispatch_table;
1632 #if defined(USE_NETCDF4)
1633 if(model == (NC_DISPATCH_NC4))
1634 dispatcher = NC4_dispatch_table;
1637 if(model == (NC_DISPATCH_NC3))
1638 dispatcher = NC3_dispatch_table;
1645 stat = new_NC(dispatcher,path,&ncp);
1646 if(stat)
return stat;
1652 stat = dispatcher->open(path, cmode, basepe, chunksizehintp,
1653 useparallel, mpi_info, dispatcher, ncp);
1655 if(ncidp) *ncidp = ncp->ext_ncid;
1657 del_from_NCList(ncp);
1668 static int pseudofd = 0;
1678 #ifdef HAVE_GETRLIMIT
1680 if(getrlimit(RLIMIT_NOFILE,&rl) == 0) {
1681 if(rl.rlim_max != RLIM_INFINITY)
1682 maxfd = rl.rlim_max;
1683 if(rl.rlim_cur != RLIM_INFINITY)
1684 maxfd = rl.rlim_cur;