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 26 #include "ncdispatch.h" 28 extern int NC_initialized;
29 extern int NC_finalized;
91 if(magic[1] ==
'H' && magic[2] ==
'D' && magic[3] ==
'F') {
95 }
else if(magic[0] ==
'\016' && magic[1] ==
'\003' 96 && magic[2] ==
'\023' && magic[3] ==
'\001') {
102 if(magic[0] ==
'C' && magic[1] ==
'D' && magic[2] ==
'F') {
103 if(magic[3] ==
'\001') {
106 }
else if(magic[3] ==
'\002') {
109 }
else if(magic[3] ==
'\005') {
126 int* model,
int* version)
128 char magic[MAGIC_NUMBER_LEN];
137 NC_MEM_INFO* meminfo = (NC_MEM_INFO*)parameters;
138 if(meminfo == NULL || meminfo->size < MAGIC_NUMBER_LEN)
140 memcpy(magic,meminfo->memory,MAGIC_NUMBER_LEN);
149 MPI_Comm comm = MPI_COMM_WORLD;
150 MPI_Info info = MPI_INFO_NULL;
152 if(parameters != NULL) {
153 comm = ((NC_MPI_INFO*)parameters)->comm;
154 info = ((NC_MPI_INFO*)parameters)->info;
156 if((retval = MPI_File_open(comm,(
char*)path,MPI_MODE_RDONLY,info,
157 &fh)) != MPI_SUCCESS)
159 if((retval = MPI_File_read(fh, magic, MAGIC_NUMBER_LEN, MPI_CHAR,
160 &mstatus)) != MPI_SUCCESS)
162 if((retval = MPI_File_close(&fh)) != MPI_SUCCESS)
169 #ifdef HAVE_SYS_STAT_H 172 if(path == NULL || strlen(path)==0)
175 if (!(fp = fopen(path,
"r")))
176 {status = errno;
goto done;}
178 #ifdef HAVE_SYS_STAT_H 184 #ifdef HAVE_FILE_LENGTH_I64 185 __int64 file_len = 0;
186 if((file_len = _filelengthi64(fileno(fp))) < 0) {
192 if(file_len < MAGIC_NUMBER_LEN) {
199 if(!(fstat(fileno(fp),&st) == 0)) {
205 if(st.st_size < MAGIC_NUMBER_LEN) {
210 #endif //HAVE_FILE_LENGTH_I64 214 i = fread(magic, MAGIC_NUMBER_LEN, 1, fp);
219 {status = errno;
goto done;}
509 size_t *chunksizehintp,
int *ncidp)
511 return NC_create(path, cmode, initialsz, 0,
512 chunksizehintp, 0, NULL, ncidp);
524 nc__create_mp(
const char *path,
int cmode,
size_t initialsz,
525 int basepe,
size_t *chunksizehintp,
int *ncidp)
527 return NC_create(path, cmode, initialsz, basepe,
528 chunksizehintp, 0, NULL, ncidp);
646 nc_open(
const char *path,
int mode,
int *ncidp)
648 return NC_open(path, mode, 0, NULL, 0, NULL, ncidp);
704 size_t *chunksizehintp,
int *ncidp)
710 return NC_open(path, mode, 0, chunksizehintp, 0,
760 nc_open_mem(
const char* path,
int mode,
size_t size,
void* memory,
int* ncidp)
766 if(memory == NULL || size < MAGIC_NUMBER_LEN || path == NULL)
772 meminfo.memory = memory;
773 return NC_open(path, mode, 0, NULL, 0, &meminfo, ncidp);
788 nc__open_mp(
const char *path,
int mode,
int basepe,
789 size_t *chunksizehintp,
int *ncidp)
791 return NC_open(path, mode, basepe, chunksizehintp,
817 if ((stat = NC_check_id(ncid, &ncp)))
819 if(ncp->path == NULL) {
820 if(pathlen) *pathlen = 0;
821 if(path) path[0] =
'\0';
823 if (pathlen) *pathlen = strlen(ncp->path);
824 if (path) strcpy(path, ncp->path);
881 int stat = NC_check_id(ncid, &ncp);
883 return ncp->dispatch->redef(ncid);
946 status = NC_check_id(ncid, &ncp);
947 if(status !=
NC_NOERR)
return status;
948 return ncp->dispatch->_enddef(ncid,0,1,0,1);
1033 nc__enddef(
int ncid,
size_t h_minfree,
size_t v_align,
size_t v_minfree,
1037 int stat = NC_check_id(ncid, &ncp);
1039 return ncp->dispatch->_enddef(ncid,h_minfree,v_align,v_minfree,r_align);
1113 int stat = NC_check_id(ncid, &ncp);
1115 return ncp->dispatch->sync(ncid);
1165 int stat = NC_check_id(ncid, &ncp);
1174 stat = ncp->dispatch->abort(ncid);
1175 del_from_NCList(ncp);
1224 int stat = NC_check_id(ncid, &ncp);
1229 if(ncp->refcount <= 0)
1233 stat = ncp->dispatch->close(ncid);
1235 del_from_NCList(ncp);
1343 int stat = NC_check_id(ncid, &ncp);
1345 return ncp->dispatch->set_fill(ncid,fillmode,old_modep);
1360 nc_inq_base_pe(
int ncid,
int *pe)
1363 int stat = NC_check_id(ncid, &ncp);
1365 return ncp->dispatch->inq_base_pe(ncid,pe);
1380 nc_set_base_pe(
int ncid,
int pe)
1383 int stat = NC_check_id(ncid, &ncp);
1385 return ncp->dispatch->set_base_pe(ncid,pe);
1410 int stat = NC_check_id(ncid, &ncp);
1412 return ncp->dispatch->inq_format(ncid,formatp);
1445 int stat = NC_check_id(ncid, &ncp);
1447 return ncp->dispatch->inq_format_extended(ncid,formatp,modep);
1495 nc_inq(
int ncid,
int *ndimsp,
int *nvarsp,
int *nattsp,
int *unlimdimidp)
1498 int stat = NC_check_id(ncid, &ncp);
1500 return ncp->dispatch->inq(ncid,ndimsp,nvarsp,nattsp,unlimdimidp);
1504 nc_inq_nvars(
int ncid,
int *nvarsp)
1507 int stat = NC_check_id(ncid, &ncp);
1509 return ncp->dispatch->inq(ncid, NULL, nvarsp, NULL, NULL);
1586 stat = NC_check_id(ncid, &ncp);
1590 if(xtype <= ATOMICTYPEMAX4) {
1591 if(name) strncpy(name,NC_atomictypename(xtype),
NC_MAX_NAME);
1592 if(size) *size = NC_atomictypelen(xtype);
1597 return ncp->dispatch->inq_type(ncid,xtype,name,size);
1649 NC_create(
const char *path,
int cmode,
size_t initialsz,
1650 int basepe,
size_t *chunksizehintp,
int useparallel,
1651 void* parameters,
int *ncidp)
1655 NC_Dispatch* dispatcher = NULL;
1667 if ((stat = nc_initialize()))
1673 ncp = find_in_NCList_by_name(path);
1678 if((isurl = NC_testurl(path)))
1679 model = NC_urlmodel(path);
1698 int format = nc_get_default_format();
1738 dispatcher = NC_get_dispatch_override();
1740 if (dispatcher == NULL)
1746 dispatcher = NC4_dispatch_table;
1751 dispatcher = NCP_dispatch_table;
1755 dispatcher = NC3_dispatch_table;
1761 stat = new_NC(dispatcher,path,cmode,&ncp);
1762 if(stat)
return stat;
1773 if ((stat = dispatcher->create(path, cmode, initialsz, basepe, chunksizehintp,
1774 useparallel, parameters, dispatcher, ncp))) {
1775 del_from_NCList(ncp);
1778 if(ncidp)*ncidp = ncp->ext_ncid;
1799 NC_open(
const char *path,
int cmode,
1800 int basepe,
size_t *chunksizehintp,
1801 int useparallel,
void* parameters,
1806 NC_Dispatch* dispatcher = NULL;
1815 if(!NC_initialized) {
1816 stat = nc_initialize();
1817 if(stat)
return stat;
1822 ncp = find_in_NCList_by_name(path);
1825 if(ncidp) *ncidp = ncp->ext_ncid;
1831 isurl = NC_testurl(path);
1833 model = NC_urlmodel(path);
1849 fprintf(stderr,
"Model == 0\n");
1872 else if(version == 5) {
1886 dispatcher = NC_get_dispatch_override();
1888 if(dispatcher != NULL)
goto havetable;
1891 #if defined(USE_CDMREMOTE) 1892 if(model == (NC_DISPATCH_NC4 | NC_DISPATCH_NCR))
1893 dispatcher = NCCR_dispatch_table;
1896 #if defined(USE_DAP) 1898 dispatcher = NCD2_dispatch_table;
1901 #if defined(USE_PNETCDF) 1903 dispatcher = NCP_dispatch_table;
1906 #if defined(USE_NETCDF4) 1908 dispatcher = NC4_dispatch_table;
1912 dispatcher = NC3_dispatch_table;
1919 stat = new_NC(dispatcher,path,cmode,&ncp);
1920 if(stat)
return stat;
1931 stat = dispatcher->open(path, cmode, basepe, chunksizehintp,
1932 useparallel, parameters, dispatcher, ncp);
1934 if(ncidp) *ncidp = ncp->ext_ncid;
1936 del_from_NCList(ncp);
1947 static int pseudofd = 0;
1957 #ifdef HAVE_GETRLIMIT 1959 if(getrlimit(RLIMIT_NOFILE,&rl) == 0) {
1960 if(rl.rlim_max != RLIM_INFINITY)
1961 maxfd = (int)rl.rlim_max;
1962 if(rl.rlim_cur != RLIM_INFINITY)
1963 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.
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.
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.