14 #ifdef HAVE_SYS_RESOURCE_H 15 #include <sys/resource.h> 17 #ifdef HAVE_SYS_TYPES_H 18 #include <sys/types.h> 20 #ifdef HAVE_SYS_STAT_H 23 #include "ncdispatch.h" 25 #include "ncwinpath.h" 27 extern int NC_initialized;
28 extern int NC_finalized;
89 if(magic[1] ==
'H' && magic[2] ==
'D' && magic[3] ==
'F') {
93 }
else if(magic[0] ==
'\016' && magic[1] ==
'\003' 94 && magic[2] ==
'\023' && magic[3] ==
'\001') {
100 if(magic[0] ==
'C' && magic[1] ==
'D' && magic[2] ==
'F') {
101 if(magic[3] ==
'\001') {
104 }
else if(magic[3] ==
'\002') {
108 }
else if(magic[3] ==
'\005') {
128 int* model,
int* version)
130 char magic[MAGIC_NUMBER_LEN];
139 NC_MEM_INFO* meminfo = (NC_MEM_INFO*)parameters;
140 if(meminfo == NULL || meminfo->size < MAGIC_NUMBER_LEN)
142 memcpy(magic,meminfo->memory,MAGIC_NUMBER_LEN);
152 MPI_Comm comm = MPI_COMM_WORLD;
153 MPI_Info info = MPI_INFO_NULL;
155 if(parameters != NULL) {
156 comm = ((NC_MPI_INFO*)parameters)->comm;
157 info = ((NC_MPI_INFO*)parameters)->info;
159 if((retval = MPI_File_open(comm,(
char*)path,MPI_MODE_RDONLY,info,
160 &fh)) != MPI_SUCCESS)
162 if((retval = MPI_File_read(fh, magic, MAGIC_NUMBER_LEN, MPI_CHAR,
163 &mstatus)) != MPI_SUCCESS)
165 if((retval = MPI_File_close(&fh)) != MPI_SUCCESS)
172 #ifdef HAVE_FILE_LENGTH_I64 173 __int64 file_len = 0;
177 if(path == NULL || strlen(path)==0)
180 if (!(fp = fopen(path,
"r")))
181 {status = errno;
goto done;}
183 #ifdef HAVE_SYS_STAT_H 189 #ifdef HAVE_FILE_LENGTH_I64 190 if((file_len = _filelengthi64(fileno(fp))) < 0) {
196 if(file_len < MAGIC_NUMBER_LEN) {
202 {
int fno = fileno(fp);
203 if(!(fstat(fno,&st) == 0)) {
208 if(st.st_size < MAGIC_NUMBER_LEN) {
214 #endif //HAVE_FILE_LENGTH_I64 216 #endif //HAVE_SYS_STAT_H 218 i = fread(magic, MAGIC_NUMBER_LEN, 1, fp);
223 {status = errno;
goto done;}
513 size_t *chunksizehintp,
int *ncidp)
515 return NC_create(path, cmode, initialsz, 0,
516 chunksizehintp, 0, NULL, ncidp);
528 nc__create_mp(
const char *path,
int cmode,
size_t initialsz,
529 int basepe,
size_t *chunksizehintp,
int *ncidp)
531 return NC_create(path, cmode, initialsz, basepe,
532 chunksizehintp, 0, NULL, ncidp);
650 nc_open(
const char *path,
int mode,
int *ncidp)
652 return NC_open(path, mode, 0, NULL, 0, NULL, ncidp);
708 size_t *chunksizehintp,
int *ncidp)
714 return NC_open(path, mode, 0, chunksizehintp, 0,
764 nc_open_mem(
const char* path,
int mode,
size_t size,
void* memory,
int* ncidp)
770 if(memory == NULL || size < MAGIC_NUMBER_LEN || path == NULL)
776 meminfo.memory = memory;
777 return NC_open(path, mode, 0, NULL, 0, &meminfo, ncidp);
792 nc__open_mp(
const char *path,
int mode,
int basepe,
793 size_t *chunksizehintp,
int *ncidp)
795 return NC_open(path, mode, basepe, chunksizehintp,
821 if ((stat = NC_check_id(ncid, &ncp)))
823 if(ncp->path == NULL) {
824 if(pathlen) *pathlen = 0;
825 if(path) path[0] =
'\0';
827 if (pathlen) *pathlen = strlen(ncp->path);
828 if (path) strcpy(path, ncp->path);
885 int stat = NC_check_id(ncid, &ncp);
887 return ncp->dispatch->redef(ncid);
950 status = NC_check_id(ncid, &ncp);
951 if(status !=
NC_NOERR)
return status;
952 return ncp->dispatch->_enddef(ncid,0,1,0,1);
1037 nc__enddef(
int ncid,
size_t h_minfree,
size_t v_align,
size_t v_minfree,
1041 int stat = NC_check_id(ncid, &ncp);
1043 return ncp->dispatch->_enddef(ncid,h_minfree,v_align,v_minfree,r_align);
1117 int stat = NC_check_id(ncid, &ncp);
1119 return ncp->dispatch->sync(ncid);
1169 int stat = NC_check_id(ncid, &ncp);
1178 stat = ncp->dispatch->abort(ncid);
1179 del_from_NCList(ncp);
1228 int stat = NC_check_id(ncid, &ncp);
1233 if(ncp->refcount <= 0)
1237 stat = ncp->dispatch->close(ncid);
1239 del_from_NCList(ncp);
1347 int stat = NC_check_id(ncid, &ncp);
1349 return ncp->dispatch->set_fill(ncid,fillmode,old_modep);
1364 nc_inq_base_pe(
int ncid,
int *pe)
1367 int stat = NC_check_id(ncid, &ncp);
1369 return ncp->dispatch->inq_base_pe(ncid,pe);
1384 nc_set_base_pe(
int ncid,
int pe)
1387 int stat = NC_check_id(ncid, &ncp);
1389 return ncp->dispatch->set_base_pe(ncid,pe);
1414 int stat = NC_check_id(ncid, &ncp);
1416 return ncp->dispatch->inq_format(ncid,formatp);
1449 int stat = NC_check_id(ncid, &ncp);
1451 return ncp->dispatch->inq_format_extended(ncid,formatp,modep);
1499 nc_inq(
int ncid,
int *ndimsp,
int *nvarsp,
int *nattsp,
int *unlimdimidp)
1502 int stat = NC_check_id(ncid, &ncp);
1504 return ncp->dispatch->inq(ncid,ndimsp,nvarsp,nattsp,unlimdimidp);
1508 nc_inq_nvars(
int ncid,
int *nvarsp)
1511 int stat = NC_check_id(ncid, &ncp);
1513 return ncp->dispatch->inq(ncid, NULL, nvarsp, NULL, NULL);
1591 if(xtype <= ATOMICTYPEMAX4) {
1592 if(name) strncpy(name,NC_atomictypename(xtype),
NC_MAX_NAME);
1593 if(size) *size = NC_atomictypelen(xtype);
1598 stat = NC_check_id(ncid, &ncp);
1602 return ncp->dispatch->inq_type(ncid,xtype,name,size);
1639 NC_create(
const char *path0,
int cmode,
size_t initialsz,
1640 int basepe,
size_t *chunksizehintp,
int useparallel,
1641 void* parameters,
int *ncidp)
1645 NC_Dispatch* dispatcher = NULL;
1660 if ((stat = nc_initialize()))
1664 #ifndef USE_DISKLESS 1670 path = NCpathcvt(path0);
1671 fprintf(stderr,
"XXX: path0=%s path=%s\n",path0,path); fflush(stderr);
1673 path = nulldup(path0);
1678 ncp = find_in_NCList_by_name(path);
1686 char* newpath = NULL;
1687 model = NC_urlmodel(path,cmode,&newpath);
1688 isurl = (model != 0);
1712 int format = nc_get_default_format();
1753 if (dispatcher == NULL)
1759 dispatcher = NC4_dispatch_table;
1764 dispatcher = NCP_dispatch_table;
1768 dispatcher = NC3_dispatch_table;
1774 stat = new_NC(dispatcher,path,cmode,&ncp);
1775 nullfree(path); path = NULL;
1777 if(stat)
return stat;
1788 if ((stat = dispatcher->create(ncp->path, cmode, initialsz, basepe, chunksizehintp,
1789 useparallel, parameters, dispatcher, ncp))) {
1790 del_from_NCList(ncp);
1793 if(ncidp)*ncidp = ncp->ext_ncid;
1814 NC_open(
const char *path0,
int cmode,
1815 int basepe,
size_t *chunksizehintp,
1816 int useparallel,
void* parameters,
1821 NC_Dispatch* dispatcher = NULL;
1832 if(!NC_initialized) {
1833 stat = nc_initialize();
1834 if(stat)
return stat;
1842 #ifndef USE_DISKLESS 1852 path = NCpathcvt(path0);
1854 path = nulldup(path0);
1859 ncp = find_in_NCList_by_name(path);
1863 if(ncidp) *ncidp = ncp->ext_ncid;
1869 char* newpath = NULL;
1870 model = NC_urlmodel(path,cmode,&newpath);
1871 isurl = (model != 0);
1893 fprintf(stderr,
"Model == 0\n");
1920 else if(version == 5) {
1933 if(dispatcher != NULL)
goto havetable;
1936 #if defined(ENABLE_DAP) 1938 dispatcher = NCD2_dispatch_table;
1941 #if defined(ENABLE_DAP4) 1943 dispatcher = NCD4_dispatch_table;
1946 #if defined(USE_PNETCDF) 1948 dispatcher = NCP_dispatch_table;
1951 #if defined(USE_NETCDF4) 1953 dispatcher = NC4_dispatch_table;
1957 dispatcher = NC3_dispatch_table;
1963 if(dispatcher == NULL)
1967 stat = new_NC(dispatcher,path,cmode,&ncp);
1968 nullfree(path); path = NULL;
1969 if(stat)
return stat;
1980 stat = dispatcher->open(ncp->path, cmode, basepe, chunksizehintp,
1981 useparallel, parameters, dispatcher, ncp);
1983 if(ncidp) *ncidp = ncp->ext_ncid;
1985 del_from_NCList(ncp);
1996 static int pseudofd = 0;
2006 #ifdef HAVE_GETRLIMIT 2008 if(getrlimit(RLIMIT_NOFILE,&rl) == 0) {
2009 if(rl.rlim_max != RLIM_INFINITY)
2010 maxfd = (int)rl.rlim_max;
2011 if(rl.rlim_cur != RLIM_INFINITY)
2012 maxfd = (int)rl.rlim_cur;
#define NC_PNETCDF
Use parallel-netcdf library; alias for NC_MPIIO.
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.
static int NC_interpret_magic_number(char *magic, int *model, int *version, int use_parallel)
Interpret the magic number found in the header of a netCDF file.
#define NC_FORMATX_NC4
alias
#define NC_CLASSIC_MODEL
Enforce classic model on netCDF-4.
Main header file for in-memory (diskless) functionality.
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.
#define NC_FORMAT_CDF5
Format specifier for nc_set_default_format() and returned by nc_inq_format.
#define NC_64BIT_DATA
CDF-5 format: classic model but 64 bit dimensions and sizes.
int nc_close(int ncid)
Close an open netCDF dataset.
#define NC_EDISKLESS
Error in using diskless access.
int nc_abort(int ncid)
No longer necessary for user to invoke manually.
#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_FORMATX_DAP2
Extended format specifier returned by nc_inq_format_extended() Added in version 4.3.1.
#define NC_FORMATX_NC3
Extended format specifier returned by nc_inq_format_extended() Added in version 4.3.1.
#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.
#define NC_FORMATX_DAP4
Extended format specifier returned by nc_inq_format_extended() Added in version 4.3.1.
static int NC_check_file_type(const char *path, int flags, void *parameters, int *model, int *version)
Given an existing file, figure out its format and return that format value (NC_FORMATX_XXX) in model ...
int nc_enddef(int ncid)
Leave define mode.
#define NC_FORMATX_PNETCDF
Extended format specifier returned by nc_inq_format_extended() Added in version 4.3.1.
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_FORMAT_64BIT_OFFSET
Format specifier for nc_set_default_format() and returned by nc_inq_format.
#define NC_MMAP
Use diskless file with mmap.
#define NC_FORMATX_UNDEFINED
Extended format specifier returned by nc_inq_format_extended() Added in version 4.3.1.
#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.