NetCDF  4.6.2
nc4var.c
Go to the documentation of this file.
1 /* Copyright 2003-2006, University Corporation for Atmospheric
2  * Research. See COPYRIGHT file for copying and redistribution
3  * conditions.*/
13 #include "config.h"
14 #include <nc4internal.h>
15 #include "nc4dispatch.h"
16 #include "hdf5internal.h"
17 #include <math.h>
18 
37 int
38 NC4_set_var_chunk_cache(int ncid, int varid, size_t size, size_t nelems,
39  float preemption)
40 {
41  NC *nc;
42  NC_GRP_INFO_T *grp;
43  NC_FILE_INFO_T *h5;
44  NC_VAR_INFO_T *var;
45  int retval;
46 
47  /* Check input for validity. */
48  if (preemption < 0 || preemption > 1)
49  return NC_EINVAL;
50 
51  /* Find info for this file and group, and set pointer to each. */
52  if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
53  return retval;
54  assert(nc && grp && h5);
55 
56  /* Find the var. */
57  var = (NC_VAR_INFO_T*)ncindexith(grp->vars,varid);
58  if(!var)
59  return NC_ENOTVAR;
60  assert(var && var->hdr.id == varid);
61 
62  /* Set the values. */
63  var->chunk_cache_size = size;
64  var->chunk_cache_nelems = nelems;
65  var->chunk_cache_preemption = preemption;
66 
67  if ((retval = nc4_reopen_dataset(grp, var)))
68  return retval;
69 
70  return NC_NOERR;
71 }
72 
86 int
87 nc_set_var_chunk_cache_ints(int ncid, int varid, int size, int nelems,
88  int preemption)
89 {
90  size_t real_size = H5D_CHUNK_CACHE_NBYTES_DEFAULT;
91  size_t real_nelems = H5D_CHUNK_CACHE_NSLOTS_DEFAULT;
92  float real_preemption = CHUNK_CACHE_PREEMPTION;
93 
94  if (size >= 0)
95  real_size = ((size_t) size) * MEGABYTE;
96 
97  if (nelems >= 0)
98  real_nelems = nelems;
99 
100  if (preemption >= 0)
101  real_preemption = preemption / 100.;
102 
103  return NC4_set_var_chunk_cache(ncid, varid, real_size, real_nelems,
104  real_preemption);
105 }
106 
123 int
124 NC4_get_var_chunk_cache(int ncid, int varid, size_t *sizep,
125  size_t *nelemsp, float *preemptionp)
126 {
127  NC *nc;
128  NC_GRP_INFO_T *grp;
129  NC_FILE_INFO_T *h5;
130  NC_VAR_INFO_T *var;
131  int retval;
132 
133  /* Find info for this file and group, and set pointer to each. */
134  if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
135  return retval;
136  assert(nc && grp && h5);
137 
138  /* Find the var. */
139  var = (NC_VAR_INFO_T*)ncindexith(grp->vars,varid);
140  if(!var)
141  return NC_ENOTVAR;
142  assert(var && var->hdr.id == varid);
143 
144  /* Give the user what they want. */
145  if (sizep)
146  *sizep = var->chunk_cache_size;
147  if (nelemsp)
148  *nelemsp = var->chunk_cache_nelems;
149  if (preemptionp)
150  *preemptionp = var->chunk_cache_preemption;
151 
152  return NC_NOERR;
153 }
154 
171 int
172 nc_get_var_chunk_cache_ints(int ncid, int varid, int *sizep,
173  int *nelemsp, int *preemptionp)
174 {
175  size_t real_size, real_nelems;
176  float real_preemption;
177  int ret;
178 
179  if ((ret = NC4_get_var_chunk_cache(ncid, varid, &real_size,
180  &real_nelems, &real_preemption)))
181  return ret;
182 
183  if (sizep)
184  *sizep = real_size / MEGABYTE;
185  if (nelemsp)
186  *nelemsp = (int)real_nelems;
187  if(preemptionp)
188  *preemptionp = (int)(real_preemption * 100);
189 
190  return NC_NOERR;
191 }
192 
230 int
231 NC4_inq_var_all(int ncid, int varid, char *name, nc_type *xtypep,
232  int *ndimsp, int *dimidsp, int *nattsp,
233  int *shufflep, int *deflatep, int *deflate_levelp,
234  int *fletcher32p, int *contiguousp, size_t *chunksizesp,
235  int *no_fill, void *fill_valuep, int *endiannessp,
236  unsigned int* idp, size_t* nparamsp, unsigned int* params
237  )
238 {
239  NC *nc;
240  NC_GRP_INFO_T *grp;
241  NC_FILE_INFO_T *h5;
242  NC_VAR_INFO_T *var;
243  int d;
244  int retval;
245 
246  LOG((2, "%s: ncid 0x%x varid %d", __func__, ncid, varid));
247 
248  /* Find info for this file and group, and set pointer to each. */
249  if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
250  return retval;
251 
252  assert(nc);
253  assert(grp && h5);
254 
255  /* Walk through the list of vars, and return the info about the one
256  with a matching varid. If the varid is -1, find the global
257  atts and call it a day. */
258  if (varid == NC_GLOBAL)
259  {
260  if (nattsp)
261  {
262  /* Do we need to read the atts? */
263  if (grp->atts_not_read)
264  if ((retval = nc4_read_atts(grp, NULL)))
265  return retval;
266 
267  *nattsp = ncindexcount(grp->att);
268  }
269  return NC_NOERR;
270  }
271 
272  /* Find the var. */
273  var = (NC_VAR_INFO_T*)ncindexith(grp->vars,varid);
274  if(!var)
275  return NC_ENOTVAR;
276  assert(var && var->hdr.id == varid);
277 
278  /* Copy the data to the user's data buffers. */
279  if (name)
280  strcpy(name, var->hdr.name);
281  if (xtypep)
282  *xtypep = var->type_info->hdr.id;
283  if (ndimsp)
284  *ndimsp = var->ndims;
285  if (dimidsp)
286  for (d = 0; d < var->ndims; d++)
287  dimidsp[d] = var->dimids[d];
288  if (nattsp)
289  {
290  if (var->atts_not_read)
291  if ((retval = nc4_read_atts(grp, var)))
292  return retval;
293  *nattsp = ncindexcount(var->att);
294  }
295 
296  /* Chunking stuff. */
297  if (!var->contiguous && chunksizesp)
298  for (d = 0; d < var->ndims; d++)
299  {
300  chunksizesp[d] = var->chunksizes[d];
301  LOG((4, "chunksizesp[%d]=%d", d, chunksizesp[d]));
302  }
303 
304  if (contiguousp)
305  *contiguousp = var->contiguous ? NC_CONTIGUOUS : NC_CHUNKED;
306 
307  /* Filter stuff. */
308  if (deflatep)
309  *deflatep = (int)var->deflate;
310  if (deflate_levelp)
311  *deflate_levelp = var->deflate_level;
312  if (shufflep)
313  *shufflep = (int)var->shuffle;
314  if (fletcher32p)
315  *fletcher32p = (int)var->fletcher32;
316 
317  if (idp)
318  *idp = var->filterid;
319  if (nparamsp)
320  *nparamsp = (var->params == NULL ? 0 : var->nparams);
321  if (params && var->params != NULL)
322  memcpy(params,var->params,var->nparams*sizeof(unsigned int));
323 
324  /* Fill value stuff. */
325  if (no_fill)
326  *no_fill = (int)var->no_fill;
327 
328  /* Don't do a thing with fill_valuep if no_fill mode is set for
329  * this var, or if fill_valuep is NULL. */
330  if (!var->no_fill && fill_valuep)
331  {
332  /* Do we have a fill value for this var? */
333  if (var->fill_value)
334  {
335  if (var->type_info->nc_type_class == NC_STRING)
336  {
337  assert(*(char **)var->fill_value);
338  /* This will allocate memeory and copy the string. */
339  if (!(*(char **)fill_valuep = strdup(*(char **)var->fill_value)))
340  {
341  free(*(char **)fill_valuep);
342  return NC_ENOMEM;
343  }
344  }
345  else
346  {
347  assert(var->type_info->size);
348  memcpy(fill_valuep, var->fill_value, var->type_info->size);
349  }
350  }
351  else
352  {
353  if (var->type_info->nc_type_class == NC_STRING)
354  {
355  if (!(*(char **)fill_valuep = calloc(1, sizeof(char *))))
356  return NC_ENOMEM;
357 
358  if ((retval = nc4_get_default_fill_value(var->type_info, (char **)fill_valuep)))
359  {
360  free(*(char **)fill_valuep);
361  return retval;
362  }
363  }
364  else
365  {
366  if ((retval = nc4_get_default_fill_value(var->type_info, fill_valuep)))
367  return retval;
368  }
369  }
370  }
371 
372  /* Does the user want the endianness of this variable? */
373  if (endiannessp)
374  *endiannessp = var->type_info->endianness;
375 
376  return NC_NOERR;
377 }
378 
395 int
396 nc_inq_var_chunking_ints(int ncid, int varid, int *contiguousp, int *chunksizesp)
397 {
398  NC_VAR_INFO_T *var;
399  size_t *cs = NULL;
400  int i, retval;
401 
402  /* Get pointer to the var. */
403  if ((retval = nc4_find_grp_h5_var(ncid, varid, NULL, NULL, &var)))
404  return retval;
405  assert(var);
406 
407  /* Allocate space for the size_t copy of the chunksizes array. */
408  if (var->ndims)
409  if (!(cs = malloc(var->ndims * sizeof(size_t))))
410  return NC_ENOMEM;
411 
412  /* Call the netcdf-4 version directly. */
413  retval = NC4_inq_var_all(ncid, varid, NULL, NULL, NULL, NULL, NULL,
414  NULL, NULL, NULL, NULL, contiguousp, cs, NULL,
415  NULL, NULL, NULL, NULL, NULL);
416 
417  /* Copy from size_t array. */
418  if (!retval && chunksizesp && var->contiguous == NC_CHUNKED)
419  {
420  for (i = 0; i < var->ndims; i++)
421  {
422  chunksizesp[i] = (int)cs[i];
423  if (cs[i] > NC_MAX_INT)
424  retval = NC_ERANGE;
425  }
426  }
427 
428  if (var->ndims)
429  free(cs);
430  return retval;
431 }
432 
445 int
446 NC4_inq_varid(int ncid, const char *name, int *varidp)
447 {
448  NC *nc;
449  NC_GRP_INFO_T *grp;
450  NC_VAR_INFO_T *var;
451  char norm_name[NC_MAX_NAME + 1];
452  int retval;
453 
454  if (!name)
455  return NC_EINVAL;
456  if (!varidp)
457  return NC_NOERR;
458 
459  LOG((2, "%s: ncid 0x%x name %s", __func__, ncid, name));
460 
461  /* Find info for this file and group, and set pointer to each. */
462  if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, NULL)))
463  return retval;
464 
465  /* Normalize name. */
466  if ((retval = nc4_normalize_name(name, norm_name)))
467  return retval;
468 
469  /* Find var of this name. */
470  var = (NC_VAR_INFO_T*)ncindexlookup(grp->vars,norm_name);
471  if(var)
472  {
473  *varidp = var->hdr.id;
474  return NC_NOERR;
475  }
476  return NC_ENOTVAR;
477 }
478 
497 int
498 NC4_var_par_access(int ncid, int varid, int par_access)
499 {
500 #ifndef USE_PARALLEL4
501  return NC_ENOPAR;
502 #else
503  NC *nc;
504  NC_GRP_INFO_T *grp;
505  NC_FILE_INFO_T *h5;
506  NC_VAR_INFO_T *var;
507  int retval;
508 
509  LOG((1, "%s: ncid 0x%x varid %d par_access %d", __func__, ncid,
510  varid, par_access));
511 
512  if (par_access != NC_INDEPENDENT && par_access != NC_COLLECTIVE)
513  return NC_EINVAL;
514 
515  /* Find info for this file and group, and set pointer to each. */
516  if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
517  return retval;
518 
519  /* This function only for files opened with nc_open_par or nc_create_par. */
520  if (!h5->parallel)
521  return NC_ENOPAR;
522 
523  /* Find the var, and set its preference. */
524  var = (NC_VAR_INFO_T*)ncindexith(grp->vars,varid);
525  if (!var) return NC_ENOTVAR;
526  assert(var->hdr.id == varid);
527 
528  if (par_access)
529  var->parallel_access = NC_COLLECTIVE;
530  else
531  var->parallel_access = NC_INDEPENDENT;
532  return NC_NOERR;
533 #endif /* USE_PARALLEL4 */
534 }
#define NC_ENOMEM
Memory allocation (malloc) failure.
Definition: netcdf.h:402
#define NC_CHUNKED
In HDF5 files you can set storage for each variable to be either contiguous or chunked, with nc_def_var_chunking().
Definition: netcdf.h:291
#define NC_MAX_INT
Max or min values for a type.
Definition: netcdf.h:93
#define NC_CONTIGUOUS
In HDF5 files you can set storage for each variable to be either contiguous or chunked, with nc_def_var_chunking().
Definition: netcdf.h:292
#define NC_ERANGE
Math result not representable.
Definition: netcdf.h:401
#define NC_STRING
string
Definition: netcdf.h:46
#define NC_INDEPENDENT
Use with nc_var_par_access() to set parallel access to independent.
Definition: netcdf_par.h:23
int nc_type
The nc_type type is just an int.
Definition: netcdf.h:24
#define NC_COLLECTIVE
Use with nc_var_par_access() to set parallel access to collective.
Definition: netcdf_par.h:25
#define NC_EINVAL
Invalid Argument.
Definition: netcdf.h:332
#define NC_MAX_NAME
Maximum for classic library.
Definition: netcdf.h:272
#define NC_ENOPAR
Parallel operation on file opened for non-parallel access.
Definition: netcdf.h:448
#define NC_ENOTVAR
Variable not found.
Definition: netcdf.h:376
#define NC_NOERR
No Error.
Definition: netcdf.h:322
#define NC_GLOBAL
Attribute id to put/get a global attribute.
Definition: netcdf.h:245

Return to the Main Unidata NetCDF page.
Generated on Tue Nov 20 2018 06:04:35 for NetCDF. NetCDF is a Unidata library.