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
102 if(mpi_info != NULL) {
103 comm = ((NC_MPI_INFO*)mpi_info)->comm;
104 info = ((NC_MPI_INFO*)mpi_info)->info;
106 if((retval = MPI_File_open(comm, (
char *)path, MPI_MODE_RDONLY,info,
107 &fh)) != MPI_SUCCESS)
109 if((retval = MPI_File_read(fh, magic, MAGIC_NUMBER_LEN, MPI_CHAR,
110 &status)) != MPI_SUCCESS)
112 if((retval = MPI_File_close(&fh)) != MPI_SUCCESS)
120 if(path == NULL || strlen(path)==0)
123 if (!(fp = fopen(path,
"r")))
125 i = fread(magic, MAGIC_NUMBER_LEN, 1, fp);
132 if(magic[1] ==
'H' && magic[2] ==
'D' && magic[3] ==
'F')
134 else if(magic[0] ==
'\016' && magic[1] ==
'\003'
135 && magic[2] ==
'\023' && magic[3] ==
'\001')
137 else if(magic[0] ==
'C' && magic[1] ==
'D' && magic[2] ==
'F')
139 if(magic[3] ==
'\001')
141 else if(magic[3] ==
'\002')
413 size_t *chunksizehintp,
int *ncidp)
415 return NC_create(path, cmode, initialsz, 0,
416 chunksizehintp, 0, NULL, ncidp);
428 nc__create_mp(
const char *path,
int cmode,
size_t initialsz,
429 int basepe,
size_t *chunksizehintp,
int *ncidp)
431 return NC_create(path, cmode, initialsz, basepe,
432 chunksizehintp, 0, NULL, ncidp);
547 nc_open(
const char *path,
int mode,
int *ncidp)
549 return NC_open(path, mode, 0, NULL, 0, NULL, ncidp);
605 size_t *chunksizehintp,
int *ncidp)
607 return NC_open(path, mode, 0, chunksizehintp, 0,
620 nc__open_mp(
const char *path,
int mode,
int basepe,
621 size_t *chunksizehintp,
int *ncidp)
623 return NC_open(path, mode, basepe, chunksizehintp,
649 if ((stat = NC_check_id(ncid, &ncp)))
651 if(ncp->path == NULL) {
652 if(pathlen) *pathlen = 0;
653 if(path) path[0] =
'\0';
655 if (pathlen) *pathlen = strlen(ncp->path);
656 if (path) strcpy(path, ncp->path);
713 int stat = NC_check_id(ncid, &ncp);
715 return ncp->dispatch->redef(ncid);
778 status = NC_check_id(ncid, &ncp);
779 if(status !=
NC_NOERR)
return status;
780 return ncp->dispatch->_enddef(ncid,0,1,0,1);
865 nc__enddef(
int ncid,
size_t h_minfree,
size_t v_align,
size_t v_minfree,
869 int stat = NC_check_id(ncid, &ncp);
871 return ncp->dispatch->_enddef(ncid,h_minfree,v_align,v_minfree,r_align);
945 int stat = NC_check_id(ncid, &ncp);
947 return ncp->dispatch->sync(ncid);
996 int stat = NC_check_id(ncid, &ncp);
998 if(ncp->path != NULL) free(ncp->path);
1000 return ncp->dispatch->abort(ncid);
1047 int stat = NC_check_id(ncid, &ncp);
1049 return ncp->dispatch->close(ncid);
1154 int stat = NC_check_id(ncid, &ncp);
1156 return ncp->dispatch->set_fill(ncid,fillmode,old_modep);
1171 nc_inq_base_pe(
int ncid,
int *pe)
1174 int stat = NC_check_id(ncid, &ncp);
1176 return ncp->dispatch->inq_base_pe(ncid,pe);
1191 nc_set_base_pe(
int ncid,
int pe)
1194 int stat = NC_check_id(ncid, &ncp);
1196 return ncp->dispatch->set_base_pe(ncid,pe);
1220 int stat = NC_check_id(ncid, &ncp);
1222 return ncp->dispatch->inq_format(ncid,formatp);
1270 nc_inq(
int ncid,
int *ndimsp,
int *nvarsp,
int *nattsp,
int *unlimdimidp)
1273 int stat = NC_check_id(ncid, &ncp);
1275 return ncp->dispatch->inq(ncid,ndimsp,nvarsp,nattsp,unlimdimidp);
1279 nc_inq_nvars(
int ncid,
int *nvarsp)
1282 int stat = NC_check_id(ncid, &ncp);
1284 return ncp->dispatch->inq(ncid, NULL, nvarsp, NULL, NULL);
1358 if(xtype <= ATOMICTYPEMAX) {
1360 if(name) strncpy(name,NC_atomictypename(xtype),
NC_MAX_NAME);
1361 if(size) *size = NC_atomictypelen(xtype);
1364 int stat = NC_check_id(ncid, &ncp);
1366 return ncp->dispatch->inq_type(ncid,xtype,name,size);
1406 NC_create(
const char *path,
int cmode,
size_t initialsz,
1407 int basepe,
size_t *chunksizehintp,
int useparallel,
1408 void* mpi_info,
int *ncidp)
1412 NC_Dispatch* dispatcher = NULL;
1417 extern int default_create_format;
1424 if ((stat = NC_initialize()))
1427 nc_local_initialize();
1431 if((isurl = NC_testurl(path)))
1432 model = NC_urlmodel(path);
1437 model = NC_DISPATCH_NC4;
1442 int format = default_create_format;
1447 model = NC_DISPATCH_NC4;
1451 model = NC_DISPATCH_NC4;
1459 model = NC_DISPATCH_NC3;
1472 if (!(dispatcher = NC_get_dispatch_override()))
1477 #ifdef USE_CDMREMOTE
1478 if(model == (NC_DISPATCH_NC4 | NC_DISPATCH_NCR))
1479 dispatcher = NCCR_dispatch_table;
1482 if(model == (NC_DISPATCH_NC4))
1483 dispatcher = NC4_dispatch_table;
1487 if(model == (NC_DISPATCH_NC3 | NC_DISPATCH_NCD))
1488 dispatcher = NCD3_dispatch_table;
1491 if(model == (NC_DISPATCH_NC3))
1492 dispatcher = NC3_dispatch_table;
1497 if ((stat = dispatcher->create(path, cmode, initialsz, basepe, chunksizehintp,
1498 useparallel, mpi_info, dispatcher, &ncp)))
1501 ncp->dispatch = dispatcher;
1503 *ncidp = ncp->ext_ncid;
1504 if (!(ncp->path = nulldup(path)))
1525 NC_open(
const char *path,
int cmode,
1526 int basepe,
size_t *chunksizehintp,
1527 int useparallel,
void* mpi_info,
1532 NC_Dispatch* dispatcher = NULL;
1538 extern int default_create_format;
1540 if(!nc_initialized) {
1541 stat = NC_initialize();
1542 if(stat)
return stat;
1544 nc_local_initialize();
1548 isurl = NC_testurl(path);
1550 model = NC_urlmodel(path);
1554 stat = NC_check_file_type(path,useparallel,mpi_info,&cdfversion,&hdfversion);
1556 if(hdfversion != 0) {
1557 model = NC_DISPATCH_NC4;
1558 }
else if(cdfversion != 0) {
1559 model = NC_DISPATCH_NC3;
1570 if(model == 0) model = NC_DISPATCH_NC3;
1573 if(model & NC_DISPATCH_NC4)
1575 else if(model & NC_DISPATCH_NC3) {
1580 if((cmode &
NC_MPIIO && cmode & NC_MPIPOSIX))
1584 dispatcher = NC_get_dispatch_override();
1585 if(dispatcher != NULL)
goto havetable;
1588 #if defined(USE_CDMREMOTE)
1589 if(model == (NC_DISPATCH_NC4 | NC_DISPATCH_NCR))
1590 dispatcher = NCCR_dispatch_table;
1593 #if defined(USE_DAP)
1594 if(model == (NC_DISPATCH_NC3 | NC_DISPATCH_NCD))
1595 dispatcher = NCD3_dispatch_table;
1598 #if defined(USE_NETCDF4)
1599 if(model == (NC_DISPATCH_NC4))
1600 dispatcher = NC4_dispatch_table;
1603 if(model == (NC_DISPATCH_NC3))
1604 dispatcher = NC3_dispatch_table;
1609 stat = dispatcher->open(path, cmode, basepe, chunksizehintp,
1610 useparallel, mpi_info, dispatcher, &ncp);
1612 ncp->dispatch = dispatcher;
1613 if(ncidp) *ncidp = ncp->ext_ncid;
1614 ncp->path = nulldup(path);
1625 static int pseudofd = 0;
1635 #ifdef HAVE_GETRLIMIT
1637 if(getrlimit(RLIMIT_NOFILE,&rl) == 0) {
1638 if(rl.rlim_max != RLIM_INFINITY)
1639 maxfd = rl.rlim_max;
1640 if(rl.rlim_cur != RLIM_INFINITY)
1641 maxfd = rl.rlim_cur;