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_NC5: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];
130 NC_MEM_INFO* meminfo = (NC_MEM_INFO*)parameters;
131 if(meminfo == NULL || meminfo->size < MAGIC_NUMBER_LEN)
133 memcpy(magic,meminfo->memory,MAGIC_NUMBER_LEN);
142 MPI_Comm comm = MPI_COMM_WORLD;
143 MPI_Info info = MPI_INFO_NULL;
145 if(parameters != NULL) {
146 comm = ((NC_MPI_INFO*)parameters)->comm;
147 info = ((NC_MPI_INFO*)parameters)->info;
149 if((retval = MPI_File_open(comm,(
char*)path,MPI_MODE_RDONLY,info,
150 &fh)) != MPI_SUCCESS)
152 if((retval = MPI_File_read(fh, magic, MAGIC_NUMBER_LEN, MPI_CHAR,
153 &mstatus)) != MPI_SUCCESS)
155 if((retval = MPI_File_close(&fh)) != MPI_SUCCESS)
162 #ifdef HAVE_SYS_STAT_H
165 if(path == NULL || strlen(path)==0)
168 if (!(fp = fopen(path,
"r")))
169 {status = errno;
goto done;}
171 #ifdef HAVE_SYS_STAT_H
175 if(!(fstat(fileno(fp),&st) == 0)) {
181 if(st.st_size < MAGIC_NUMBER_LEN) {
188 i = fread(magic, MAGIC_NUMBER_LEN, 1, fp);
193 {status = errno;
goto done;}
198 status = NC_interpret_magic_number(magic,model,version,use_parallel);
469 size_t *chunksizehintp,
int *ncidp)
471 return NC_create(path, cmode, initialsz, 0,
472 chunksizehintp, 0, NULL, ncidp);
484 nc__create_mp(
const char *path,
int cmode,
size_t initialsz,
485 int basepe,
size_t *chunksizehintp,
int *ncidp)
487 return NC_create(path, cmode, initialsz, basepe,
488 chunksizehintp, 0, NULL, ncidp);
606 nc_open(
const char *path,
int mode,
int *ncidp)
608 return NC_open(path, mode, 0, NULL, 0, NULL, ncidp);
664 size_t *chunksizehintp,
int *ncidp)
666 return NC_open(path, mode, 0, chunksizehintp, 0,
716 nc_open_mem(
const char* path,
int mode,
size_t size,
void* memory,
int* ncidp)
722 if(memory == NULL || size < MAGIC_NUMBER_LEN || path == NULL)
728 meminfo.memory = memory;
729 return NC_open(path, mode, 0, NULL, 0, &meminfo, ncidp);
744 nc__open_mp(
const char *path,
int mode,
int basepe,
745 size_t *chunksizehintp,
int *ncidp)
747 return NC_open(path, mode, basepe, chunksizehintp,
773 if ((stat = NC_check_id(ncid, &ncp)))
775 if(ncp->path == NULL) {
776 if(pathlen) *pathlen = 0;
777 if(path) path[0] =
'\0';
779 if (pathlen) *pathlen = strlen(ncp->path);
780 if (path) strcpy(path, ncp->path);
837 int stat = NC_check_id(ncid, &ncp);
839 return ncp->dispatch->redef(ncid);
902 status = NC_check_id(ncid, &ncp);
903 if(status !=
NC_NOERR)
return status;
904 return ncp->dispatch->_enddef(ncid,0,1,0,1);
989 nc__enddef(
int ncid,
size_t h_minfree,
size_t v_align,
size_t v_minfree,
993 int stat = NC_check_id(ncid, &ncp);
995 return ncp->dispatch->_enddef(ncid,h_minfree,v_align,v_minfree,r_align);
1069 int stat = NC_check_id(ncid, &ncp);
1071 return ncp->dispatch->sync(ncid);
1120 int stat = NC_check_id(ncid, &ncp);
1129 stat = ncp->dispatch->abort(ncid);
1130 del_from_NCList(ncp);
1179 int stat = NC_check_id(ncid, &ncp);
1184 if(ncp->refcount <= 0)
1187 stat = ncp->dispatch->close(ncid);
1189 del_from_NCList(ncp);
1297 int stat = NC_check_id(ncid, &ncp);
1299 return ncp->dispatch->set_fill(ncid,fillmode,old_modep);
1314 nc_inq_base_pe(
int ncid,
int *pe)
1317 int stat = NC_check_id(ncid, &ncp);
1319 return ncp->dispatch->inq_base_pe(ncid,pe);
1334 nc_set_base_pe(
int ncid,
int pe)
1337 int stat = NC_check_id(ncid, &ncp);
1339 return ncp->dispatch->set_base_pe(ncid,pe);
1364 int stat = NC_check_id(ncid, &ncp);
1366 return ncp->dispatch->inq_format(ncid,formatp);
1398 int stat = NC_check_id(ncid, &ncp);
1400 return ncp->dispatch->inq_format_extended(ncid,formatp,modep);
1448 nc_inq(
int ncid,
int *ndimsp,
int *nvarsp,
int *nattsp,
int *unlimdimidp)
1451 int stat = NC_check_id(ncid, &ncp);
1453 return ncp->dispatch->inq(ncid,ndimsp,nvarsp,nattsp,unlimdimidp);
1457 nc_inq_nvars(
int ncid,
int *nvarsp)
1460 int stat = NC_check_id(ncid, &ncp);
1462 return ncp->dispatch->inq(ncid, NULL, nvarsp, NULL, NULL);
1536 if(xtype <= ATOMICTYPEMAX) {
1538 if(name) strncpy(name,NC_atomictypename(xtype),
NC_MAX_NAME);
1539 if(size) *size = NC_atomictypelen(xtype);
1542 int stat = NC_check_id(ncid, &ncp);
1544 return ncp->dispatch->inq_type(ncid,xtype,name,size);
1584 NC_create(
const char *path,
int cmode,
size_t initialsz,
1585 int basepe,
size_t *chunksizehintp,
int useparallel,
1586 void* parameters,
int *ncidp)
1590 NC_Dispatch* dispatcher = NULL;
1601 if ((stat = NC_initialize()))
1604 nc_local_initialize();
1610 ncp = find_in_NCList_by_name(path);
1615 if((isurl = NC_testurl(path)))
1616 model = NC_urlmodel(path);
1622 model = NC_DISPATCH_NC4;
1627 model = NC_DISPATCH_NC5;
1631 model = NC_DISPATCH_NC3;
1636 int format = nc_get_default_format();
1641 model = NC_DISPATCH_NC4;
1645 model = NC_DISPATCH_NC4;
1653 model = NC_DISPATCH_NC3;
1666 if (!(dispatcher = NC_get_dispatch_override()))
1671 #ifdef USE_CDMREMOTE
1672 if(model == (NC_DISPATCH_NC4 | NC_DISPATCH_NCR))
1673 dispatcher = NCCR_dispatch_table;
1676 if(model == (NC_DISPATCH_NC4))
1677 dispatcher = NC4_dispatch_table;
1681 if(model == (NC_DISPATCH_NC3 | NC_DISPATCH_NCD))
1682 dispatcher = NCD2_dispatch_table;
1686 if(model == (NC_DISPATCH_NC5))
1687 dispatcher = NC5_dispatch_table;
1690 if(model == (NC_DISPATCH_NC3))
1691 dispatcher = NC3_dispatch_table;
1697 stat = new_NC(dispatcher,path,cmode,&ncp);
1698 if(stat)
return stat;
1709 if ((stat = dispatcher->create(path, cmode, initialsz, basepe, chunksizehintp,
1710 useparallel, parameters, dispatcher, ncp))) {
1711 del_from_NCList(ncp);
1714 if(ncidp)*ncidp = ncp->ext_ncid;
1735 NC_open(
const char *path,
int cmode,
1736 int basepe,
size_t *chunksizehintp,
1737 int useparallel,
void* parameters,
1742 NC_Dispatch* dispatcher = NULL;
1750 if(!nc_initialized) {
1751 stat = NC_initialize();
1752 if(stat)
return stat;
1754 nc_local_initialize();
1760 ncp = find_in_NCList_by_name(path);
1763 if(ncidp) *ncidp = ncp->ext_ncid;
1769 isurl = NC_testurl(path);
1771 model = NC_urlmodel(path);
1778 stat = NC_check_file_type(path,flags,parameters,&model,&version);
1787 fprintf(stderr,
"Model == 0\n");
1792 if(model & NC_DISPATCH_NC4)
1794 else if(model & NC_DISPATCH_NC3) {
1795 cmode &= ~NC_NETCDF4;
1797 }
else if(model & NC_DISPATCH_NC5) {
1799 It appears that pnetcdf can read NC_64_BIT_OFFSET
1807 if((cmode &
NC_MPIIO && cmode & NC_MPIPOSIX))
1811 dispatcher = NC_get_dispatch_override();
1812 if(dispatcher != NULL)
goto havetable;
1815 #if defined(USE_CDMREMOTE)
1816 if(model == (NC_DISPATCH_NC4 | NC_DISPATCH_NCR))
1817 dispatcher = NCCR_dispatch_table;
1820 #if defined(USE_DAP)
1821 if(model == (NC_DISPATCH_NC3 | NC_DISPATCH_NCD))
1822 dispatcher = NCD2_dispatch_table;
1825 #if defined(USE_PNETCDF)
1826 if(model == (NC_DISPATCH_NC5))
1827 dispatcher = NC5_dispatch_table;
1830 #if defined(USE_NETCDF4)
1831 if(model == (NC_DISPATCH_NC4))
1832 dispatcher = NC4_dispatch_table;
1835 if(model == (NC_DISPATCH_NC3))
1836 dispatcher = NC3_dispatch_table;
1843 stat = new_NC(dispatcher,path,cmode,&ncp);
1844 if(stat)
return stat;
1855 stat = dispatcher->open(path, cmode, basepe, chunksizehintp,
1856 useparallel, parameters, dispatcher, ncp);
1858 if(ncidp) *ncidp = ncp->ext_ncid;
1860 del_from_NCList(ncp);
1871 static int pseudofd = 0;
1881 #ifdef HAVE_GETRLIMIT
1883 if(getrlimit(RLIMIT_NOFILE,&rl) == 0) {
1884 if(rl.rlim_max != RLIM_INFINITY)
1885 maxfd = (int)rl.rlim_max;
1886 if(rl.rlim_cur != RLIM_INFINITY)
1887 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.