📄 nc.c
字号:
{ /* * NC_SHARE implies sync up the number of records as well. * (File format version one.) * Note that other header changes are not shared * automatically. Some sort of IPC (external to this package) * would be used to trigger a call to nc_sync(). */ fSet(ncp->flags, NC_NSYNC); } status = nc_get_NC(ncp); if(status != NC_NOERR) goto unwind_ioc; add_to_NCList(ncp); if(chunksizehintp != NULL) *chunksizehintp = ncp->chunk; *ncid_ptr = ncp->nciop->fd; return NC_NOERR;unwind_ioc: (void) ncio_close(ncp->nciop, 0); ncp->nciop = NULL; /*FALLTHRU*/unwind_alloc: free_NC(ncp); return status;}intnc_open(const char * path, int ioflags, int *ncid_ptr){ return nc__open(path, ioflags, NULL, ncid_ptr);}intnc__enddef(int ncid, size_t h_minfree, size_t v_align, size_t v_minfree, size_t r_align){ int status; NC *ncp; status = NC_check_id(ncid, &ncp); if(status != NC_NOERR) return status; if(!NC_indef(ncp)) return(NC_ENOTINDEFINE); return (NC_endef(ncp, h_minfree, v_align, v_minfree, r_align));}intnc_enddef(int ncid){ int status; NC *ncp; status = NC_check_id(ncid, &ncp); if(status != NC_NOERR) return status; if(!NC_indef(ncp)) return(NC_ENOTINDEFINE); /* return(NC_endef(ncp, 0, 4096, 0, 4096)); */ return (NC_endef(ncp, 0, 1, 0, 1));}intnc_close(int ncid){ int status = NC_NOERR; NC *ncp; status = NC_check_id(ncid, &ncp); if(status != NC_NOERR) return status; if(NC_indef(ncp)) { status = NC_endef(ncp, 0, 1, 0, 1); /* TODO: defaults */ if(status != NC_NOERR ) { (void) nc_abort(ncid); return status; } } else if(!NC_readonly(ncp)) { status = NC_sync(ncp); /* flush buffers before any filesize comparisons */ (void) ncp->nciop->sync(ncp->nciop); } /* * If file opened for writing and filesize is less than * what it should be (due to previous use of NOFILL mode), * pad it to correct size, as reported by NC_calcsize(). */ if (status == ENOERR) { off_t filesize; /* current size of open file */ off_t calcsize; /* calculated file size, from header */ status = ncio_filesize(ncp->nciop, &filesize); if(status != ENOERR) return status; status = NC_calcsize(ncp, &calcsize); if(status != NC_NOERR) return status; if(filesize < calcsize && !NC_readonly(ncp)) { status = ncio_pad_length(ncp->nciop, calcsize); if(status != ENOERR) return status; } } (void) ncio_close(ncp->nciop, 0); ncp->nciop = NULL; del_from_NCList(ncp); free_NC(ncp); return status;}intnc_delete(const char * path){ return nc_delete_mp(path, 0);}intnc_delete_mp(const char * path, int basepe){ NC *ncp; int status; size_t chunk = 512; ncp = new_NC(&chunk); if(ncp == NULL) return NC_ENOMEM; #if defined(LOCKNUMREC) /* && _CRAYMPP */ if (status = NC_init_pe(ncp, basepe)) { return status; }#else /* * !_CRAYMPP, only pe 0 is valid */ if(basepe != 0) return NC_EINVAL;#endif status = ncio_open(path, NC_NOWRITE, 0, 0, &ncp->chunk, &ncp->nciop, 0); if(status) goto unwind_alloc; assert(ncp->flags == 0); status = nc_get_NC(ncp); if(status != NC_NOERR) { /* Not a netcdf file, don't delete */ /* ??? is this the right semantic? what if it was just too big? */ (void) ncio_close(ncp->nciop, 0); } else { /* ncio_close does the unlink */ status = ncio_close(ncp->nciop, 1); /* ncio_close does the unlink */ } ncp->nciop = NULL;unwind_alloc: free_NC(ncp); return status;}/* * In data mode, same as ncclose. * In define mode, restore previous definition. * In create, remove the file. */intnc_abort(int ncid){ int status; NC *ncp; int doUnlink = 0; status = NC_check_id(ncid, &ncp); if(status != NC_NOERR) return status; doUnlink = NC_IsNew(ncp); if(ncp->old != NULL) { /* a plain redef, not a create */ assert(!NC_IsNew(ncp)); assert(fIsSet(ncp->flags, NC_INDEF)); free_NC(ncp->old); ncp->old = NULL; fClr(ncp->flags, NC_INDEF); } else if(!NC_readonly(ncp)) { status = NC_sync(ncp); if(status != NC_NOERR) return status; } (void) ncio_close(ncp->nciop, doUnlink); ncp->nciop = NULL; del_from_NCList(ncp); free_NC(ncp); return NC_NOERR;}intnc_redef(int ncid){ int status; NC *ncp; status = NC_check_id(ncid, &ncp); if(status != NC_NOERR) return status; if(NC_readonly(ncp)) return NC_EPERM; if(NC_indef(ncp)) return NC_EINDEFINE; if(fIsSet(ncp->nciop->ioflags, NC_SHARE)) { /* read in from disk */ status = read_NC(ncp); if(status != NC_NOERR) return status; } ncp->old = dup_NC(ncp); if(ncp->old == NULL) return NC_ENOMEM; fSet(ncp->flags, NC_INDEF); return NC_NOERR;}intnc_inq(int ncid, int *ndimsp, int *nvarsp, int *nattsp, int *xtendimp){ int status; NC *ncp; status = NC_check_id(ncid, &ncp); if(status != NC_NOERR) return status; if(ndimsp != NULL) *ndimsp = (int) ncp->dims.nelems; if(nvarsp != NULL) *nvarsp = (int) ncp->vars.nelems; if(nattsp != NULL) *nattsp = (int) ncp->attrs.nelems; if(xtendimp != NULL) *xtendimp = find_NC_Udim(&ncp->dims, NULL); return NC_NOERR;}int nc_inq_ndims(int ncid, int *ndimsp){ int status; NC *ncp; status = NC_check_id(ncid, &ncp); if(status != NC_NOERR) return status; if(ndimsp != NULL) *ndimsp = (int) ncp->dims.nelems; return NC_NOERR;}int nc_inq_nvars(int ncid, int *nvarsp){ int status; NC *ncp; status = NC_check_id(ncid, &ncp); if(status != NC_NOERR) return status; if(nvarsp != NULL) *nvarsp = (int) ncp->vars.nelems; return NC_NOERR;}int nc_inq_natts(int ncid, int *nattsp){ int status; NC *ncp; status = NC_check_id(ncid, &ncp); if(status != NC_NOERR) return status; if(nattsp != NULL) *nattsp = (int) ncp->attrs.nelems; return NC_NOERR;}int nc_inq_unlimdim(int ncid, int *xtendimp){ int status; NC *ncp; status = NC_check_id(ncid, &ncp); if(status != NC_NOERR) return status; if(xtendimp != NULL) *xtendimp = find_NC_Udim(&ncp->dims, NULL); return NC_NOERR;}intnc_sync(int ncid){ int status; NC *ncp; status = NC_check_id(ncid, &ncp); if(status != NC_NOERR) return status; if(NC_indef(ncp)) return NC_EINDEFINE; if(NC_readonly(ncp)) { return read_NC(ncp); } /* else, read/write */ status = NC_sync(ncp); if(status != NC_NOERR) return status; return ncp->nciop->sync(ncp->nciop);}intnc_set_fill(int ncid, int fillmode, int *old_mode_ptr){ int status; NC *ncp; int oldmode; status = NC_check_id(ncid, &ncp); if(status != NC_NOERR) return status; if(NC_readonly(ncp)) return NC_EPERM; oldmode = fIsSet(ncp->flags, NC_NOFILL) ? NC_NOFILL : NC_FILL; if(fillmode == NC_NOFILL) { fSet(ncp->flags, NC_NOFILL); } else if(fillmode == NC_FILL) { if(fIsSet(ncp->flags, NC_NOFILL)) { /* * We are changing back to fill mode * so do a sync */ status = NC_sync(ncp); if(status != NC_NOERR) return status; } fClr(ncp->flags, NC_NOFILL); } else { return NC_EINVAL; /* Invalid fillmode */ } if(old_mode_ptr != NULL) *old_mode_ptr = oldmode; return NC_NOERR;}#ifdef LOCKNUMREC/* create function versions of the NC_*_numrecs macros */size_t NC_get_numrecs(const NC *ncp) { shmem_t numrec; shmem_short_get(&numrec, (shmem_t *) ncp->lock + LOCKNUMREC_VALUE, 1, ncp->lock[LOCKNUMREC_BASEPE]); return (size_t) numrec;}void NC_set_numrecs(NC *ncp, size_t nrecs) { shmem_t numrec = (shmem_t) nrecs; /* update local value too */ ncp->lock[LOCKNUMREC_VALUE] = (ushmem_t) numrec; shmem_short_put((shmem_t *) ncp->lock + LOCKNUMREC_VALUE, &numrec, 1, ncp->lock[LOCKNUMREC_BASEPE]);}void NC_increase_numrecs(NC *ncp, size_t nrecs) { /* this is only called in one place that's already protected * by a lock ... so don't worry about it */ if (nrecs > NC_get_numrecs(ncp)) NC_set_numrecs(ncp, nrecs);}#endif /* LOCKNUMREC *//* everyone in communicator group will be executing this *//*ARGSUSED*/intnc_set_base_pe(int ncid, int pe){#if _CRAYMPP && defined(LOCKNUMREC) int status; NC *ncp; shmem_t numrecs; if ((status = NC_check_id(ncid, &ncp)) != NC_NOERR) { return status; } if (pe < 0 || pe >= _num_pes()) { return NC_EINVAL; /* invalid base pe */ } numrecs = (shmem_t) NC_get_numrecs(ncp); ncp->lock[LOCKNUMREC_VALUE] = (ushmem_t) numrecs; /* update serving & lock values for a "smooth" transition */ /* note that the "real" server will being doing this as well */ /* as all the rest in the group */ /* must have syncronization before & after this step */ shmem_short_get( (shmem_t *) ncp->lock + LOCKNUMREC_SERVING, (shmem_t *) ncp->lock + LOCKNUMREC_SERVING, 1, ncp->lock[LOCKNUMREC_BASEPE]); shmem_short_get( (shmem_t *) ncp->lock + LOCKNUMREC_LOCK, (shmem_t *) ncp->lock + LOCKNUMREC_LOCK, 1, ncp->lock[LOCKNUMREC_BASEPE]); /* complete transition */ ncp->lock[LOCKNUMREC_BASEPE] = (ushmem_t) pe;#endif /* _CRAYMPP && LOCKNUMREC */ return NC_NOERR;}/*ARGSUSED*/intnc_inq_base_pe(int ncid, int *pe){#if _CRAYMPP && defined(LOCKNUMREC) int status; NC *ncp; if ((status = NC_check_id(ncid, &ncp)) != NC_NOERR) { return status; } *pe = (int) ncp->lock[LOCKNUMREC_BASEPE];#else /* * !_CRAYMPP, only pe 0 is valid */ *pe = 0;#endif /* _CRAYMPP && LOCKNUMREC */ return NC_NOERR;}intnc_inq_format(int ncid, int *formatp){ int status; NC *ncp; status = NC_check_id(ncid, &ncp); if(status != NC_NOERR) return status; /* only need to check for netCDF-3 variants, since this is never called for netCDF-4 files */ *formatp = fIsSet(ncp->flags, NC_64BIT_OFFSET) ? NC_FORMAT_64BIT : NC_FORMAT_CLASSIC; return NC_NOERR;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -