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_interpret_magic_number(
char* magic,
int* model,
int* version,
int use_parallel)
87 if(magic[1] ==
'H' && magic[2] ==
'D' && magic[3] ==
'F') {
88 *model = NC_DISPATCH_NC4;
91 }
else if(magic[0] ==
'\016' && magic[1] ==
'\003'
92 && magic[2] ==
'\023' && magic[3] ==
'\001') {
93 *model = NC_DISPATCH_NC4;
98 if(magic[0] ==
'C' && magic[1] ==
'D' && magic[2] ==
'F') {
99 if(magic[3] ==
'\001')
101 else if(magic[3] ==
'\002')
104 else if(magic[3] ==
'\005')
109 *model = (use_parallel || *version == 5)?NC_DISPATCH_NCP:NC_DISPATCH_NC3;
117 NC_check_file_type(
const char *path,
int flags,
void *parameters,
118 int* model,
int* version)
120 char magic[MAGIC_NUMBER_LEN];
133 NC_MEM_INFO* meminfo = (NC_MEM_INFO*)parameters;
134 if(meminfo == NULL || meminfo->size < MAGIC_NUMBER_LEN)
136 memcpy(magic,meminfo->memory,MAGIC_NUMBER_LEN);
145 MPI_Comm comm = MPI_COMM_WORLD;
146 MPI_Info info = MPI_INFO_NULL;
148 if(parameters != NULL) {
149 comm = ((NC_MPI_INFO*)parameters)->comm;
150 info = ((NC_MPI_INFO*)parameters)->info;
152 if((retval = MPI_File_open(comm,(
char*)path,MPI_MODE_RDONLY,info,
153 &fh)) != MPI_SUCCESS)
155 if((retval = MPI_File_read(fh, magic, MAGIC_NUMBER_LEN, MPI_CHAR,
156 &mstatus)) != MPI_SUCCESS)
158 if((retval = MPI_File_close(&fh)) != MPI_SUCCESS)
165 #ifdef HAVE_SYS_STAT_H
168 if(path == NULL || strlen(path)==0)
171 if (!(fp = fopen(path,
"r")))
172 {status = errno;
goto done;}
174 #ifdef HAVE_SYS_STAT_H
178 if(!(fstat(fileno(fp),&st) == 0)) {
184 if(st.st_size < MAGIC_NUMBER_LEN) {
191 i = fread(magic, MAGIC_NUMBER_LEN, 1, fp);
196 {status = errno;
goto done;}
201 status = NC_interpret_magic_number(magic,model,version,use_parallel);
472 size_t *chunksizehintp,
int *ncidp)
474 return NC_create(path, cmode, initialsz, 0,
475 chunksizehintp, 0, NULL, ncidp);
487 nc__create_mp(
const char *path,
int cmode,
size_t initialsz,
488 int basepe,
size_t *chunksizehintp,
int *ncidp)
490 return NC_create(path, cmode, initialsz, basepe,
491 chunksizehintp, 0, NULL, ncidp);
609 nc_open(
const char *path,
int mode,
int *ncidp)
611 return NC_open(path, mode, 0, NULL, 0, NULL, ncidp);
667 size_t *chunksizehintp,
int *ncidp)
669 return NC_open(path, mode, 0, chunksizehintp, 0,
719 nc_open_mem(
const char* path,
int mode,
size_t size,
void* memory,
int* ncidp)
725 if(memory == NULL || size < MAGIC_NUMBER_LEN || path == NULL)
731 meminfo.memory = memory;
732 return NC_open(path, mode, 0, NULL, 0, &meminfo, ncidp);
747 nc__open_mp(
const char *path,
int mode,
int basepe,
748 size_t *chunksizehintp,
int *ncidp)
750 return NC_open(path, mode, basepe, chunksizehintp,
776 if ((stat = NC_check_id(ncid, &ncp)))
778 if(ncp->path == NULL) {
779 if(pathlen) *pathlen = 0;
780 if(path) path[0] =
'\0';
782 if (pathlen) *pathlen = strlen(ncp->path);
783 if (path) strcpy(path, ncp->path);
840 int stat = NC_check_id(ncid, &ncp);
842 return ncp->dispatch->redef(ncid);
905 status = NC_check_id(ncid, &ncp);
906 if(status !=
NC_NOERR)
return status;
907 return ncp->dispatch->_enddef(ncid,0,1,0,1);
992 nc__enddef(
int ncid,
size_t h_minfree,
size_t v_align,
size_t v_minfree,
996 int stat = NC_check_id(ncid, &ncp);
998 return ncp->dispatch->_enddef(ncid,h_minfree,v_align,v_minfree,r_align);
1072 int stat = NC_check_id(ncid, &ncp);
1074 return ncp->dispatch->sync(ncid);
1123 int stat = NC_check_id(ncid, &ncp);
1132 stat = ncp->dispatch->abort(ncid);
1133 del_from_NCList(ncp);
1182 int stat = NC_check_id(ncid, &ncp);
1187 if(ncp->refcount <= 0)
1190 stat = ncp->dispatch->close(ncid);
1192 del_from_NCList(ncp);
1300 int stat = NC_check_id(ncid, &ncp);
1302 return ncp->dispatch->set_fill(ncid,fillmode,old_modep);
1317 nc_inq_base_pe(
int ncid,
int *pe)
1320 int stat = NC_check_id(ncid, &ncp);
1322 return ncp->dispatch->inq_base_pe(ncid,pe);
1337 nc_set_base_pe(
int ncid,
int pe)
1340 int stat = NC_check_id(ncid, &ncp);
1342 return ncp->dispatch->set_base_pe(ncid,pe);
1367 int stat = NC_check_id(ncid, &ncp);
1369 return ncp->dispatch->inq_format(ncid,formatp);
1401 int stat = NC_check_id(ncid, &ncp);
1403 return ncp->dispatch->inq_format_extended(ncid,formatp,modep);
1451 nc_inq(
int ncid,
int *ndimsp,
int *nvarsp,
int *nattsp,
int *unlimdimidp)
1454 int stat = NC_check_id(ncid, &ncp);
1456 return ncp->dispatch->inq(ncid,ndimsp,nvarsp,nattsp,unlimdimidp);
1460 nc_inq_nvars(
int ncid,
int *nvarsp)
1463 int stat = NC_check_id(ncid, &ncp);
1465 return ncp->dispatch->inq(ncid, NULL, nvarsp, NULL, NULL);
1539 if(xtype <= ATOMICTYPEMAX) {
1541 if(name) strncpy(name,NC_atomictypename(xtype),
NC_MAX_NAME);
1542 if(size) *size = NC_atomictypelen(xtype);
1545 int stat = NC_check_id(ncid, &ncp);
1547 return ncp->dispatch->inq_type(ncid,xtype,name,size);
1587 NC_create(
const char *path,
int cmode,
size_t initialsz,
1588 int basepe,
size_t *chunksizehintp,
int useparallel,
1589 void* parameters,
int *ncidp)
1593 NC_Dispatch* dispatcher = NULL;
1604 if ((stat = NC_initialize()))
1607 nc_local_initialize();
1613 ncp = find_in_NCList_by_name(path);
1618 if((isurl = NC_testurl(path)))
1619 model = NC_urlmodel(path);
1625 model = NC_DISPATCH_NC4;
1630 model = NC_DISPATCH_NCP;
1634 model = NC_DISPATCH_NC3;
1639 int format = nc_get_default_format();
1644 model = NC_DISPATCH_NC4;
1648 model = NC_DISPATCH_NC4;
1656 model = NC_DISPATCH_NC3;
1669 if (!(dispatcher = NC_get_dispatch_override()))
1674 #ifdef USE_CDMREMOTE
1675 if(model == (NC_DISPATCH_NC4 | NC_DISPATCH_NCR))
1676 dispatcher = NCCR_dispatch_table;
1679 if(model == (NC_DISPATCH_NC4))
1680 dispatcher = NC4_dispatch_table;
1684 if(model == (NC_DISPATCH_NC3 | NC_DISPATCH_NCD))
1685 dispatcher = NCD2_dispatch_table;
1689 if(model == (NC_DISPATCH_NCP))
1690 dispatcher = NCP_dispatch_table;
1693 if(model == (NC_DISPATCH_NC3))
1694 dispatcher = NC3_dispatch_table;
1700 stat = new_NC(dispatcher,path,cmode,&ncp);
1701 if(stat)
return stat;
1712 if ((stat = dispatcher->create(path, cmode, initialsz, basepe, chunksizehintp,
1713 useparallel, parameters, dispatcher, ncp))) {
1714 del_from_NCList(ncp);
1717 if(ncidp)*ncidp = ncp->ext_ncid;
1738 NC_open(
const char *path,
int cmode,
1739 int basepe,
size_t *chunksizehintp,
1740 int useparallel,
void* parameters,
1745 NC_Dispatch* dispatcher = NULL;
1753 if(!nc_initialized) {
1754 stat = NC_initialize();
1755 if(stat)
return stat;
1757 nc_local_initialize();
1763 ncp = find_in_NCList_by_name(path);
1766 if(ncidp) *ncidp = ncp->ext_ncid;
1772 isurl = NC_testurl(path);
1774 model = NC_urlmodel(path);
1781 stat = NC_check_file_type(path,flags,parameters,&model,&version);
1790 fprintf(stderr,
"Model == 0\n");
1795 if(model & NC_DISPATCH_NC4)
1797 else if(model & NC_DISPATCH_NC3) {
1798 cmode &= ~NC_NETCDF4;
1800 }
else if(model & NC_DISPATCH_NCP) {
1802 It appears that pnetcdf can read NC_64_BIT_OFFSET
1810 if((cmode &
NC_MPIIO && cmode & NC_MPIPOSIX))
1814 dispatcher = NC_get_dispatch_override();
1815 if(dispatcher != NULL)
goto havetable;
1818 #if defined(USE_CDMREMOTE)
1819 if(model == (NC_DISPATCH_NC4 | NC_DISPATCH_NCR))
1820 dispatcher = NCCR_dispatch_table;
1823 #if defined(USE_DAP)
1824 if(model == (NC_DISPATCH_NC3 | NC_DISPATCH_NCD))
1825 dispatcher = NCD2_dispatch_table;
1828 #if defined(USE_PNETCDF)
1829 if(model == (NC_DISPATCH_NCP))
1830 dispatcher = NCP_dispatch_table;
1833 #if defined(USE_NETCDF4)
1834 if(model == (NC_DISPATCH_NC4))
1835 dispatcher = NC4_dispatch_table;
1838 if(model == (NC_DISPATCH_NC3))
1839 dispatcher = NC3_dispatch_table;
1846 stat = new_NC(dispatcher,path,cmode,&ncp);
1847 if(stat)
return stat;
1858 stat = dispatcher->open(path, cmode, basepe, chunksizehintp,
1859 useparallel, parameters, dispatcher, ncp);
1861 if(ncidp) *ncidp = ncp->ext_ncid;
1863 del_from_NCList(ncp);
1874 static int pseudofd = 0;
1884 #ifdef HAVE_GETRLIMIT
1886 if(getrlimit(RLIMIT_NOFILE,&rl) == 0) {
1887 if(rl.rlim_max != RLIM_INFINITY)
1888 maxfd = (int)rl.rlim_max;
1889 if(rl.rlim_cur != RLIM_INFINITY)
1890 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_INMEMORY
Read from memory.
#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_EDISKLESS
Error in using diskless access.
#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.
#define NC_WRITE
Set read-write access for nc_open().
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.
#define NC_DISKLESS
Use diskless file.
int nc_open(const char *path, int mode, int *ncidp)
Open an existing netCDF file.
int nc_enddef(int ncid)
Leave define mode.
int nc_open_mem(const char *path, int mode, size_t size, void *memory, int *ncidp)
Open a netCDF file with the contents taken from a block of memory.
#define NC_MMAP
Use diskless file with mmap.
#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.