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 extern int NC_initialized;
28 extern int NC_finalized;
68 NC_interpret_magic_number(
char* magic,
int* model,
int* version,
int use_parallel)
74 if(magic[1] ==
'H' && magic[2] ==
'D' && magic[3] ==
'F') {
78 }
else if(magic[0] ==
'\016' && magic[1] ==
'\003' 79 && magic[2] ==
'\023' && magic[3] ==
'\001') {
85 if(magic[0] ==
'C' && magic[1] ==
'D' && magic[2] ==
'F') {
86 if(magic[3] ==
'\001') {
89 }
else if(magic[3] ==
'\002') {
92 }
else if(magic[3] ==
'\005') {
110 int* model,
int* version)
112 char magic[MAGIC_NUMBER_LEN];
121 NC_MEM_INFO* meminfo = (NC_MEM_INFO*)parameters;
122 if(meminfo == NULL || meminfo->size < MAGIC_NUMBER_LEN)
124 memcpy(magic,meminfo->memory,MAGIC_NUMBER_LEN);
133 MPI_Comm comm = MPI_COMM_WORLD;
134 MPI_Info info = MPI_INFO_NULL;
136 if(parameters != NULL) {
137 comm = ((NC_MPI_INFO*)parameters)->comm;
138 info = ((NC_MPI_INFO*)parameters)->info;
140 if((retval = MPI_File_open(comm,(
char*)path,MPI_MODE_RDONLY,info,
141 &fh)) != MPI_SUCCESS)
143 if((retval = MPI_File_read(fh, magic, MAGIC_NUMBER_LEN, MPI_CHAR,
144 &mstatus)) != MPI_SUCCESS)
146 if((retval = MPI_File_close(&fh)) != MPI_SUCCESS)
153 #ifdef HAVE_SYS_STAT_H 156 if(path == NULL || strlen(path)==0)
159 if (!(fp = fopen(path,
"r")))
160 {status = errno;
goto done;}
162 #ifdef HAVE_SYS_STAT_H 166 if(!(fstat(fileno(fp),&st) == 0)) {
172 if(st.st_size < MAGIC_NUMBER_LEN) {
179 i = fread(magic, MAGIC_NUMBER_LEN, 1, fp);
184 {status = errno;
goto done;}
189 status = NC_interpret_magic_number(magic,model,version,use_parallel);
471 size_t *chunksizehintp,
int *ncidp)
473 return NC_create(path, cmode, initialsz, 0,
474 chunksizehintp, 0, NULL, ncidp);
486 nc__create_mp(
const char *path,
int cmode,
size_t initialsz,
487 int basepe,
size_t *chunksizehintp,
int *ncidp)
489 return NC_create(path, cmode, initialsz, basepe,
490 chunksizehintp, 0, NULL, ncidp);
608 nc_open(
const char *path,
int mode,
int *ncidp)
610 return NC_open(path, mode, 0, NULL, 0, NULL, ncidp);
666 size_t *chunksizehintp,
int *ncidp)
672 return NC_open(path, mode, 0, chunksizehintp, 0,
722 nc_open_mem(
const char* path,
int mode,
size_t size,
void* memory,
int* ncidp)
728 if(memory == NULL || size < MAGIC_NUMBER_LEN || path == NULL)
734 meminfo.memory = memory;
735 return NC_open(path, mode, 0, NULL, 0, &meminfo, ncidp);
750 nc__open_mp(
const char *path,
int mode,
int basepe,
751 size_t *chunksizehintp,
int *ncidp)
753 return NC_open(path, mode, basepe, chunksizehintp,
779 if ((stat = NC_check_id(ncid, &ncp)))
781 if(ncp->path == NULL) {
782 if(pathlen) *pathlen = 0;
783 if(path) path[0] =
'\0';
785 if (pathlen) *pathlen = strlen(ncp->path);
786 if (path) strcpy(path, ncp->path);
843 int stat = NC_check_id(ncid, &ncp);
845 return ncp->dispatch->redef(ncid);
908 status = NC_check_id(ncid, &ncp);
909 if(status !=
NC_NOERR)
return status;
910 return ncp->dispatch->_enddef(ncid,0,1,0,1);
995 nc__enddef(
int ncid,
size_t h_minfree,
size_t v_align,
size_t v_minfree,
999 int stat = NC_check_id(ncid, &ncp);
1001 return ncp->dispatch->_enddef(ncid,h_minfree,v_align,v_minfree,r_align);
1075 int stat = NC_check_id(ncid, &ncp);
1077 return ncp->dispatch->sync(ncid);
1126 int stat = NC_check_id(ncid, &ncp);
1135 stat = ncp->dispatch->abort(ncid);
1136 del_from_NCList(ncp);
1185 int stat = NC_check_id(ncid, &ncp);
1190 if(ncp->refcount <= 0)
1193 stat = ncp->dispatch->close(ncid);
1195 del_from_NCList(ncp);
1303 int stat = NC_check_id(ncid, &ncp);
1305 return ncp->dispatch->set_fill(ncid,fillmode,old_modep);
1320 nc_inq_base_pe(
int ncid,
int *pe)
1323 int stat = NC_check_id(ncid, &ncp);
1325 return ncp->dispatch->inq_base_pe(ncid,pe);
1340 nc_set_base_pe(
int ncid,
int pe)
1343 int stat = NC_check_id(ncid, &ncp);
1345 return ncp->dispatch->set_base_pe(ncid,pe);
1370 int stat = NC_check_id(ncid, &ncp);
1372 return ncp->dispatch->inq_format(ncid,formatp);
1404 int stat = NC_check_id(ncid, &ncp);
1406 return ncp->dispatch->inq_format_extended(ncid,formatp,modep);
1454 nc_inq(
int ncid,
int *ndimsp,
int *nvarsp,
int *nattsp,
int *unlimdimidp)
1457 int stat = NC_check_id(ncid, &ncp);
1459 return ncp->dispatch->inq(ncid,ndimsp,nvarsp,nattsp,unlimdimidp);
1463 nc_inq_nvars(
int ncid,
int *nvarsp)
1466 int stat = NC_check_id(ncid, &ncp);
1468 return ncp->dispatch->inq(ncid, NULL, nvarsp, NULL, NULL);
1545 stat = NC_check_id(ncid, &ncp);
1549 if(xtype <= ATOMICTYPEMAX3) {
1550 if(name) strncpy(name,NC_atomictypename(xtype),
NC_MAX_NAME);
1551 if(size) *size = NC_atomictypelen(xtype);
1556 return ncp->dispatch->inq_type(ncid,xtype,name,size);
1609 NC_create(
const char *path,
int cmode,
size_t initialsz,
1610 int basepe,
size_t *chunksizehintp,
int useparallel,
1611 void* parameters,
int *ncidp)
1615 NC_Dispatch* dispatcher = NULL;
1626 if ((stat = nc_initialize()))
1632 ncp = find_in_NCList_by_name(path);
1637 if((isurl = NC_testurl(path)))
1638 model = NC_urlmodel(path);
1657 int format = nc_get_default_format();
1696 if (!(dispatcher = NC_get_dispatch_override()))
1702 dispatcher = NC4_dispatch_table;
1707 dispatcher = NCP_dispatch_table;
1711 dispatcher = NC3_dispatch_table;
1717 stat = new_NC(dispatcher,path,cmode,&ncp);
1718 if(stat)
return stat;
1729 if ((stat = dispatcher->create(path, cmode, initialsz, basepe, chunksizehintp,
1730 useparallel, parameters, dispatcher, ncp))) {
1731 del_from_NCList(ncp);
1734 if(ncidp)*ncidp = ncp->ext_ncid;
1755 NC_open(
const char *path,
int cmode,
1756 int basepe,
size_t *chunksizehintp,
1757 int useparallel,
void* parameters,
1762 NC_Dispatch* dispatcher = NULL;
1770 if(!NC_initialized) {
1771 stat = nc_initialize();
1772 if(stat)
return stat;
1777 ncp = find_in_NCList_by_name(path);
1780 if(ncidp) *ncidp = ncp->ext_ncid;
1786 isurl = NC_testurl(path);
1788 model = NC_urlmodel(path);
1804 fprintf(stderr,
"Model == 0\n");
1827 else if(version == 5) {
1840 dispatcher = NC_get_dispatch_override();
1841 if(dispatcher != NULL)
goto havetable;
1844 #if defined(USE_CDMREMOTE) 1845 if(model == (NC_DISPATCH_NC4 | NC_DISPATCH_NCR))
1846 dispatcher = NCCR_dispatch_table;
1849 #if defined(USE_DAP) 1851 dispatcher = NCD2_dispatch_table;
1854 #if defined(USE_PNETCDF) 1856 dispatcher = NCP_dispatch_table;
1859 #if defined(USE_NETCDF4) 1861 dispatcher = NC4_dispatch_table;
1865 dispatcher = NC3_dispatch_table;
1872 stat = new_NC(dispatcher,path,cmode,&ncp);
1873 if(stat)
return stat;
1884 stat = dispatcher->open(path, cmode, basepe, chunksizehintp,
1885 useparallel, parameters, dispatcher, ncp);
1887 if(ncidp) *ncidp = ncp->ext_ncid;
1889 del_from_NCList(ncp);
1900 static int pseudofd = 0;
1910 #ifdef HAVE_GETRLIMIT 1912 if(getrlimit(RLIMIT_NOFILE,&rl) == 0) {
1913 if(rl.rlim_max != RLIM_INFINITY)
1914 maxfd = (int)rl.rlim_max;
1915 if(rl.rlim_cur != RLIM_INFINITY)
1916 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.
#define NC_FORMATX_NC4
alias
#define NC_CLASSIC_MODEL
Enforce classic model on netCDF-4.
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.
#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.
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 ...
#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.
#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.