📄 putget.m4
字号:
idim == 0 && IS_RECVAR (varp) ? NC_get_numrecs(ncp) : varp->shape[idim]; if (mystart[idim] >= dimlen) { status = NC_EINVALCOORDS; goto done; } if (mystart[idim] + myedges[idim] > dimlen) { status = NC_EEDGE; goto done; } }NC_VARM_Lower_Body(nc_get_vara_$1)}')dnlNCGETVARM(text, char)NCGETVARM(uchar, uchar)NCGETVARM(schar, schar)NCGETVARM(short, short)NCGETVARM(int, int)NCGETVARM(long, long)NCGETVARM(float, float)NCGETVARM(double, double)#ifdef NO_NETCDF_2extern intnctypelen(nc_type datatype);#endif/* * Generalized hyperslab output. */dnldnl NCPUTVARM(Abbrv, Type)dnldefine(`NCPUTVARM',dnl`dnlintnc_put_varm_$1(int ncid, int varid, const size_t *start, const size_t *edges, const ptrdiff_t *stride, const ptrdiff_t *map, const $2 *value){NC_VARM_Upper_Body() if (NC_readonly (ncp)) return NC_EPERM;NC_VARM_Mid_Body(putNCv_$1, $1) /* * Check start, edges */ for (idim = IS_RECVAR (varp); idim < maxidim; ++idim) { if (mystart[idim] > varp->shape[idim]) { status = NC_EINVALCOORDS; goto done; } if (mystart[idim] + myedges[idim] > varp->shape[idim]) { status = NC_EEDGE; goto done; } }NC_VARM_Lower_Body(nc_put_vara_$1)}')dnlNCPUTVARM(text, char)NCPUTVARM(uchar, uchar)NCPUTVARM(schar, schar)NCPUTVARM(short, short)NCPUTVARM(int, int)NCPUTVARM(long, long)NCPUTVARM(float, float)NCPUTVARM(double, double)/* * Copy the values of a variable from an input netCDF to an output netCDF. * Input and output var assummed to have the same shape. * return -1 on error. */intnc_copy_var(int ncid_in, int varid, int ncid_out){ int status = NC_NOERR; NC *inncp, *outncp; NC_var *invp, *outvp; status = NC_check_id(ncid_in, &inncp); if(status != NC_NOERR) return status; if(NC_indef(inncp)) { return NC_EINDEFINE; } status = NC_check_id(ncid_out, &outncp); if(status != NC_NOERR) return status; if(NC_readonly(outncp)) { /* output file isn't writable */ return NC_EPERM; } if(NC_indef(outncp)) { return NC_EINDEFINE; } /* find the variable in the input cdf */ invp = NC_lookupvar(inncp, varid); if(invp == NULL) { return NC_ENOTVAR; } /* find the variable in the output cdf */ if(NC_findvar(&outncp->vars, invp->name->cp, &outvp) == -1) { return NC_ENOTVAR; } /* can we even attempt to copy without conversion? */ if(outvp->type != invp->type) { return NC_EINVAL; } if( (invp->ndims == 0 && outvp->ndims != 0) || (invp->ndims != 0 && outvp->ndims == 0) || (IS_RECVAR(invp) && !IS_RECVAR(outvp)) || (!IS_RECVAR(invp) && IS_RECVAR(outvp)) || (invp->len != outvp->len) ) { return NC_EINVAL; } /* * Check coordinates */ { ALLOC_ONSTACK(coord, size_t, invp->ndims); const size_t nrecs = NC_get_numrecs(inncp); (void) memcpy(coord, invp->shape, invp->ndims * sizeof(size_t)); if(IS_RECVAR(invp)) *coord = nrecs; { size_t ii = 0; for(; ii < invp->ndims; ii++) coord[ii] --; } /* at this point, coord is the largest valid coord of invp */ if(NCcoordck(outncp, outvp, coord) != NC_NOERR) { return NC_EINVAL; } /* else */ (void) memset(coord, 0, invp->ndims * sizeof(size_t)); if(!IS_RECVAR(invp)) { status = NCxvarcpy(inncp, invp, coord, outncp, outvp, coord, invp->len); goto done; } /* else */ status = NCvnrecs(outncp, nrecs); if(status != NC_NOERR) goto done; for( /*NADA*/; *coord < nrecs; (*coord)++) { status = NCxvarcpy(inncp, invp, coord, outncp, outvp, coord, invp->len); if(status != NC_NOERR) break; }done: FREE_ONSTACK(coord); } return status;}/* no longer deprecated, used to support the 2.x interface and also the netcdf-4 api. */intnc_get_att(int ncid, int varid, const char *name, void *value){ int status; nc_type atttype; status = nc_inq_atttype(ncid, varid, name, &atttype); if(status != NC_NOERR) return status; switch (atttype) { case NC_BYTE: return nc_get_att_schar(ncid, varid, name, (schar *)value); case NC_CHAR: return nc_get_att_text(ncid, varid, name, (char *)value); case NC_SHORT: return nc_get_att_short(ncid, varid, name, (short *)value); case NC_INT:#if (SIZEOF_INT >= X_SIZEOF_INT) return nc_get_att_int(ncid, varid, name, (int *)value);#elif SIZEOF_LONG == X_SIZEOF_INT return nc_get_att_long(ncid, varid, name, (long *)value);#endif case NC_FLOAT: return nc_get_att_float(ncid, varid, name, (float *)value); case NC_DOUBLE: return nc_get_att_double(ncid, varid, name, (double *)value); } return NC_EBADTYPE;}intnc_put_att( int ncid, int varid, const char *name, nc_type type, size_t nelems, const void *value){ switch (type) { case NC_BYTE: return nc_put_att_schar(ncid, varid, name, type, nelems, (schar *)value); case NC_CHAR: return nc_put_att_text(ncid, varid, name, nelems, (char *)value); case NC_SHORT: return nc_put_att_short(ncid, varid, name, type, nelems, (short *)value); case NC_INT:#if (SIZEOF_INT >= X_SIZEOF_INT) return nc_put_att_int(ncid, varid, name, type, nelems, (int *)value);#elif SIZEOF_LONG == X_SIZEOF_INT return nc_put_att_long(ncid, varid, name, type, nelems, (long *)value);#endif case NC_FLOAT: return nc_put_att_float(ncid, varid, name, type, nelems, (float *)value); case NC_DOUBLE: return nc_put_att_double(ncid, varid, name, type, nelems, (double *)value); } return NC_EBADTYPE;}intnc_get_var1(int ncid, int varid, const size_t *coord, void *value){ int status; nc_type vartype; status = nc_inq_vartype(ncid, varid, &vartype); if(status != NC_NOERR) return status; switch(vartype){ case NC_CHAR: return nc_get_var1_text(ncid, varid, coord, (char *) value); case NC_BYTE: return nc_get_var1_schar(ncid, varid, coord, (schar *) value); case NC_SHORT: return nc_get_var1_short(ncid, varid, coord, (short *) value); case NC_INT: return nc_get_var1_int(ncid, varid, coord, (int *) value); case NC_FLOAT: return nc_get_var1_float(ncid, varid, coord, (float *) value); case NC_DOUBLE: return nc_get_var1_double(ncid, varid, coord, (double *) value); } return NC_EBADTYPE;}intnc_put_var1(int ncid, int varid, const size_t *coord, const void *value){ int status; nc_type vartype; status = nc_inq_vartype(ncid, varid, &vartype); if(status != NC_NOERR) return status; switch(vartype){ case NC_CHAR: return nc_put_var1_text(ncid, varid, coord, (const char *) value); case NC_BYTE: return nc_put_var1_schar(ncid, varid, coord, (const schar *) value); case NC_SHORT: return nc_put_var1_short(ncid, varid, coord, (const short *) value); case NC_INT: return nc_put_var1_int(ncid, varid, coord, (const int *) value); case NC_FLOAT: return nc_put_var1_float(ncid, varid, coord, (const float *) value); case NC_DOUBLE: return nc_put_var1_double(ncid, varid, coord, (const double *) value); } return NC_EBADTYPE;}intnc_get_vara(int ncid, int varid, const size_t *start, const size_t *edges, void *value){ int status; nc_type vartype; status = nc_inq_vartype(ncid, varid, &vartype); if(status != NC_NOERR) return status; switch(vartype){ case NC_CHAR: return nc_get_vara_text(ncid, varid, start, edges, (char *) value); case NC_BYTE: return nc_get_vara_schar(ncid, varid, start, edges, (schar *) value); case NC_SHORT: return nc_get_vara_short(ncid, varid, start, edges, (short *) value); case NC_INT:#if (SIZEOF_INT >= X_SIZEOF_INT) return nc_get_vara_int(ncid, varid, start, edges, (int *) value);#elif SIZEOF_LONG == X_SIZEOF_INT return nc_get_vara_long(ncid, varid, start, edges, (long *) value);#else#error "nc_get_vara implementation"#endif case NC_FLOAT: return nc_get_vara_float(ncid, varid, start, edges, (float *) value); case NC_DOUBLE: return nc_get_vara_double(ncid, varid, start, edges, (double *) value); } return NC_EBADTYPE;}intnc_put_vara(int ncid, int varid, const size_t *start, const size_t *edges, const void *value){ int status; nc_type vartype; status = nc_inq_vartype(ncid, varid, &vartype); if(status != NC_NOERR) return status; switch(vartype){ case NC_CHAR: return nc_put_vara_text(ncid, varid, start, edges, (const char *) value); case NC_BYTE: return nc_put_vara_schar(ncid, varid, start, edges, (const schar *) value); case NC_SHORT: return nc_put_vara_short(ncid, varid, start, edges, (const short *) value); case NC_INT: return nc_put_vara_int(ncid, varid, start, edges, (const int *) value); case NC_FLOAT: return nc_put_vara_float(ncid, varid, start, edges, (const float *) value); case NC_DOUBLE: return nc_put_vara_double(ncid, varid, start, edges, (const double *) value); } return NC_EBADTYPE;}intnc_get_varm ( int ncid, int varid, const size_t * start, const size_t * edges, const ptrdiff_t * stride, const ptrdiff_t * imapp, void *value){ int status; nc_type vartype; int varndims; ptrdiff_t *cvtmap = NULL; status = nc_inq_vartype(ncid, varid, &vartype); if(status != NC_NOERR) return status; status = nc_inq_varndims(ncid, varid, &varndims); if(status != NC_NOERR) return status; if(imapp != NULL && varndims != 0) { /* * convert map units from bytes to units of sizeof(type) */ size_t ii; const ptrdiff_t szof = (ptrdiff_t) nctypelen(vartype); cvtmap = (ptrdiff_t *)calloc(varndims, sizeof(ptrdiff_t)); if(cvtmap == NULL) return NC_ENOMEM; for(ii = 0; ii < varndims; ii++) { if(imapp[ii] % szof != 0) { free(cvtmap); return NC_EINVAL; } cvtmap[ii] = imapp[ii] / szof; } imapp = cvtmap; } switch(vartype){ case NC_CHAR: status = nc_get_varm_text(ncid, varid, start, edges, stride, imapp, (char *) value); break; case NC_BYTE: status = nc_get_varm_schar(ncid, varid, start, edges, stride, imapp, (schar *) value); break; case NC_SHORT: status = nc_get_varm_short(ncid, varid, start, edges, stride, imapp, (short *) value); break; case NC_INT:#if (SIZEOF_INT >= X_SIZEOF_INT) status = nc_get_varm_int(ncid, varid, start, edges, stride, imapp, (int *) value);#elif SIZEOF_LONG == X_SIZEOF_INT status = nc_get_varm_long(ncid, varid, start, edges, stride, imapp, (long *) value);#else#error "nc_get_varm implementation"#endif break; case NC_FLOAT: status = nc_get_varm_float(ncid, varid, start, edges, stride, imapp, (float *) value); break; case NC_DOUBLE: status = nc_get_varm_double(ncid, varid, start, edges, stride, imapp, (double *) value); break; default: status = NC_EBADTYPE; break; } if(cvtmap != NULL) { free(cvtmap); } return status;}intnc_put_varm ( int ncid, int varid, const size_t * start, const size_t * edges, const ptrdiff_t * stride, const ptrdiff_t * imapp, const void *value){ int status; nc_type vartype; int varndims; ptrdiff_t *cvtmap = NULL; status = nc_inq_vartype(ncid, varid, &vartype); if(status != NC_NOERR) return status; status = nc_inq_varndims(ncid, varid, &varndims); if(status != NC_NOERR) return status; if(imapp != NULL && varndims != 0) { /* * convert map units from bytes to units of sizeof(type) */ size_t ii; const ptrdiff_t szof = (ptrdiff_t) nctypelen(vartype); cvtmap = (ptrdiff_t *)calloc(varndims, sizeof(ptrdiff_t)); if(cvtmap == NULL) return NC_ENOMEM; for(ii = 0; ii < varndims; ii++) { if(imapp[ii] % szof != 0) { free(cvtmap); return NC_EINVAL; } cvtmap[ii] = imapp[ii] / szof; } imapp = cvtmap; } switch(vartype){ case NC_CHAR: status = nc_put_varm_text(ncid, varid, start, edges, stride, imapp, (const char *) value); break; case NC_BYTE: status = nc_put_varm_schar(ncid, varid, start, edges, stride, imapp, (const schar *) value); break; case NC_SHORT: status = nc_put_varm_short(ncid, varid, start, edges, stride, imapp, (const short *) value); break; case NC_INT:#if (SIZEOF_INT >= X_SIZEOF_INT) status = nc_put_varm_int(ncid, varid, start, edges, stride, imapp, (const int *) value);#elif SIZEOF_LONG == X_SIZEOF_INT status = nc_put_varm_long(ncid, varid, start, edges, stride, imapp, (const long *) value);#else#error "nc_put_varm implementation"#endif break; case NC_FLOAT: status = nc_put_varm_float(ncid, varid, start, edges, stride, imapp, (const float *) value); break; case NC_DOUBLE: status = nc_put_varm_double(ncid, varid, start, edges, stride, imapp, (const double *) value); break; default: status = NC_EBADTYPE; break; } if(cvtmap != NULL) { free(cvtmap); } return status;}intnc_get_vars ( int ncid, int varid, const size_t * start, const size_t * edges, const ptrdiff_t * stride, void *value){ return nc_get_varm (ncid, varid, start, edges, stride, 0, value);}intnc_put_vars ( int ncid, int varid, const size_t * start, const size_t * edges, const ptrdiff_t * stride, const void *value){ return nc_put_varm (ncid, varid, start, edges, stride, 0, value);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -