NetCDF  4.7.3
nc4var.c
Go to the documentation of this file.
1 /* Copyright 2003-2018, 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 #ifdef USE_HDF5
17 #include "hdf5internal.h"
18 #endif
19 #include <math.h>
20 
37 int
38 NC4_get_var_chunk_cache(int ncid, int varid, size_t *sizep,
39  size_t *nelemsp, float *preemptionp)
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  /* Find info for this file and group, and set pointer to each. */
48  if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
49  return retval;
50  assert(nc && grp && h5);
51 
52  /* Find the var. */
53  var = (NC_VAR_INFO_T*)ncindexith(grp->vars,varid);
54  if(!var)
55  return NC_ENOTVAR;
56  assert(var && var->hdr.id == varid);
57 
58  /* Give the user what they want. */
59  if (sizep)
60  *sizep = var->chunk_cache_size;
61  if (nelemsp)
62  *nelemsp = var->chunk_cache_nelems;
63  if (preemptionp)
64  *preemptionp = var->chunk_cache_preemption;
65 
66  return NC_NOERR;
67 }
68 
85 int
86 nc_get_var_chunk_cache_ints(int ncid, int varid, int *sizep,
87  int *nelemsp, int *preemptionp)
88 {
89  size_t real_size, real_nelems;
90  float real_preemption;
91  int ret;
92 
93  if ((ret = NC4_get_var_chunk_cache(ncid, varid, &real_size,
94  &real_nelems, &real_preemption)))
95  return ret;
96 
97  if (sizep)
98  *sizep = real_size / MEGABYTE;
99  if (nelemsp)
100  *nelemsp = (int)real_nelems;
101  if(preemptionp)
102  *preemptionp = (int)(real_preemption * 100);
103 
104  return NC_NOERR;
105 }
106 
144 int
145 NC4_inq_var_all(int ncid, int varid, char *name, nc_type *xtypep,
146  int *ndimsp, int *dimidsp, int *nattsp,
147  int *shufflep, int *deflatep, int *deflate_levelp,
148  int *fletcher32p, int *contiguousp, size_t *chunksizesp,
149  int *no_fill, void *fill_valuep, int *endiannessp,
150  unsigned int *idp, size_t *nparamsp, unsigned int *params)
151 {
152  NC_GRP_INFO_T *grp;
153  NC_FILE_INFO_T *h5;
154  NC_VAR_INFO_T *var;
155  int d;
156  int retval;
157 
158  LOG((2, "%s: ncid 0x%x varid %d", __func__, ncid, varid));
159 
160  /* Find info for this file and group, and set pointer to each. */
161  if ((retval = nc4_find_nc_grp_h5(ncid, NULL, &grp, &h5)))
162  return retval;
163  assert(grp && h5);
164 
165  /* If the varid is -1, find the global atts and call it a day. */
166  if (varid == NC_GLOBAL && nattsp)
167  {
168  *nattsp = ncindexcount(grp->att);
169  return NC_NOERR;
170  }
171 
172  /* Find the var. */
173  if (!(var = (NC_VAR_INFO_T *)ncindexith(grp->vars, varid)))
174  return NC_ENOTVAR;
175  assert(var && var->hdr.id == varid);
176 
177  /* Copy the data to the user's data buffers. */
178  if (name)
179  strcpy(name, var->hdr.name);
180  if (xtypep)
181  *xtypep = var->type_info->hdr.id;
182  if (ndimsp)
183  *ndimsp = var->ndims;
184  if (dimidsp)
185  for (d = 0; d < var->ndims; d++)
186  dimidsp[d] = var->dimids[d];
187  if (nattsp)
188  *nattsp = ncindexcount(var->att);
189 
190  /* Chunking stuff. */
191  if (!var->contiguous && chunksizesp)
192  for (d = 0; d < var->ndims; d++)
193  {
194  chunksizesp[d] = var->chunksizes[d];
195  LOG((4, "chunksizesp[%d]=%d", d, chunksizesp[d]));
196  }
197 
198  if (contiguousp)
199  *contiguousp = var->contiguous ? NC_CONTIGUOUS : NC_CHUNKED;
200 
201  /* Filter stuff. */
202  if (deflatep)
203  *deflatep = (int)var->deflate;
204  if (deflate_levelp)
205  *deflate_levelp = var->deflate_level;
206  if (shufflep)
207  *shufflep = (int)var->shuffle;
208  if (fletcher32p)
209  *fletcher32p = (int)var->fletcher32;
210 
211  if (idp)
212  *idp = var->filterid;
213  if (nparamsp)
214  *nparamsp = (var->params == NULL ? 0 : var->nparams);
215  if (params && var->params != NULL)
216  memcpy(params,var->params,var->nparams*sizeof(unsigned int));
217 
218  /* Fill value stuff. */
219  if (no_fill)
220  *no_fill = (int)var->no_fill;
221 
222  /* Don't do a thing with fill_valuep if no_fill mode is set for
223  * this var, or if fill_valuep is NULL. */
224  if (!var->no_fill && fill_valuep)
225  {
226  /* Do we have a fill value for this var? */
227  if (var->fill_value)
228  {
229  if (var->type_info->nc_type_class == NC_STRING)
230  {
231  assert(*(char **)var->fill_value);
232  /* This will allocate memory and copy the string. */
233  if (!(*(char **)fill_valuep = strdup(*(char **)var->fill_value)))
234  {
235  free(*(char **)fill_valuep);
236  return NC_ENOMEM;
237  }
238  }
239  else
240  {
241  assert(var->type_info->size);
242  memcpy(fill_valuep, var->fill_value, var->type_info->size);
243  }
244  }
245  else
246  {
247  if (var->type_info->nc_type_class == NC_STRING)
248  {
249  if (!(*(char **)fill_valuep = calloc(1, sizeof(char *))))
250  return NC_ENOMEM;
251 
252  if ((retval = nc4_get_default_fill_value(var->type_info, (char **)fill_valuep)))
253  {
254  free(*(char **)fill_valuep);
255  return retval;
256  }
257  }
258  else
259  {
260  if ((retval = nc4_get_default_fill_value(var->type_info, fill_valuep)))
261  return retval;
262  }
263  }
264  }
265 
266  /* Does the user want the endianness of this variable? */
267  if (endiannessp)
268  *endiannessp = var->type_info->endianness;
269 
270  return NC_NOERR;
271 }
272 
289 int
290 nc_inq_var_chunking_ints(int ncid, int varid, int *contiguousp, int *chunksizesp)
291 {
292  NC_VAR_INFO_T *var;
293  size_t *cs = NULL;
294  int i, retval;
295 
296  /* Get pointer to the var. */
297  if ((retval = nc4_find_grp_h5_var(ncid, varid, NULL, NULL, &var)))
298  return retval;
299  assert(var);
300 
301  /* Allocate space for the size_t copy of the chunksizes array. */
302  if (var->ndims)
303  if (!(cs = malloc(var->ndims * sizeof(size_t))))
304  return NC_ENOMEM;
305 
306  /* Call the netcdf-4 version directly. */
307  retval = NC4_inq_var_all(ncid, varid, NULL, NULL, NULL, NULL, NULL,
308  NULL, NULL, NULL, NULL, contiguousp, cs, NULL,
309  NULL, NULL, NULL, NULL, NULL);
310 
311  /* Copy from size_t array. */
312  if (!retval && chunksizesp && var->contiguous == NC_CHUNKED)
313  {
314  for (i = 0; i < var->ndims; i++)
315  {
316  chunksizesp[i] = (int)cs[i];
317  if (cs[i] > NC_MAX_INT)
318  retval = NC_ERANGE;
319  }
320  }
321 
322  if (var->ndims)
323  free(cs);
324  return retval;
325 }
326 
339 int
340 NC4_inq_varid(int ncid, const char *name, int *varidp)
341 {
342  NC *nc;
343  NC_GRP_INFO_T *grp;
344  NC_VAR_INFO_T *var;
345  char norm_name[NC_MAX_NAME + 1];
346  int retval;
347 
348  if (!name)
349  return NC_EINVAL;
350  if (!varidp)
351  return NC_NOERR;
352 
353  LOG((2, "%s: ncid 0x%x name %s", __func__, ncid, name));
354 
355  /* Find info for this file and group, and set pointer to each. */
356  if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, NULL)))
357  return retval;
358 
359  /* Normalize name. */
360  if ((retval = nc4_normalize_name(name, norm_name)))
361  return retval;
362 
363  /* Find var of this name. */
364  var = (NC_VAR_INFO_T*)ncindexlookup(grp->vars,norm_name);
365  if(var)
366  {
367  *varidp = var->hdr.id;
368  return NC_NOERR;
369  }
370  return NC_ENOTVAR;
371 }
372 
391 int
392 NC4_var_par_access(int ncid, int varid, int par_access)
393 {
394 #ifndef USE_PARALLEL4
395  NC_UNUSED(ncid);
396  NC_UNUSED(varid);
397  NC_UNUSED(par_access);
398  return NC_ENOPAR;
399 #else
400  NC *nc;
401  NC_GRP_INFO_T *grp;
402  NC_FILE_INFO_T *h5;
403  NC_VAR_INFO_T *var;
404  int retval;
405 
406  LOG((1, "%s: ncid 0x%x varid %d par_access %d", __func__, ncid,
407  varid, par_access));
408 
409  if (par_access != NC_INDEPENDENT && par_access != NC_COLLECTIVE)
410  return NC_EINVAL;
411 
412  /* Find info for this file and group, and set pointer to each. */
413  if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
414  return retval;
415 
416  /* This function only for files opened with nc_open_par or nc_create_par. */
417  if (!h5->parallel)
418  return NC_ENOPAR;
419 
420  /* Find the var, and set its preference. */
421  var = (NC_VAR_INFO_T*)ncindexith(grp->vars,varid);
422  if (!var) return NC_ENOTVAR;
423  assert(var->hdr.id == varid);
424 
425  if (par_access)
426  var->parallel_access = NC_COLLECTIVE;
427  else
428  var->parallel_access = NC_INDEPENDENT;
429  return NC_NOERR;
430 #endif /* USE_PARALLEL4 */
431 }
432 
455 int
456 nc4_convert_type(const void *src, void *dest, const nc_type src_type,
457  const nc_type dest_type, const size_t len, int *range_error,
458  const void *fill_value, int strict_nc3)
459 {
460  char *cp, *cp1;
461  float *fp, *fp1;
462  double *dp, *dp1;
463  int *ip, *ip1;
464  short *sp, *sp1;
465  signed char *bp, *bp1;
466  unsigned char *ubp, *ubp1;
467  unsigned short *usp, *usp1;
468  unsigned int *uip, *uip1;
469  long long *lip, *lip1;
470  unsigned long long *ulip, *ulip1;
471  size_t count = 0;
472 
473  *range_error = 0;
474  LOG((3, "%s: len %d src_type %d dest_type %d", __func__, len, src_type,
475  dest_type));
476 
477  /* OK, this is ugly. If you can think of anything better, I'm open
478  to suggestions!
479 
480  Note that we don't use a default fill value for type
481  NC_BYTE. This is because Lord Voldemort cast a nofilleramous spell
482  at Harry Potter, but it bounced off his scar and hit the netcdf-4
483  code.
484  */
485  switch (src_type)
486  {
487  case NC_CHAR:
488  switch (dest_type)
489  {
490  case NC_CHAR:
491  for (cp = (char *)src, cp1 = dest; count < len; count++)
492  *cp1++ = *cp++;
493  break;
494  default:
495  LOG((0, "%s: Unknown destination type.", __func__));
496  }
497  break;
498 
499  case NC_BYTE:
500  switch (dest_type)
501  {
502  case NC_BYTE:
503  for (bp = (signed char *)src, bp1 = dest; count < len; count++)
504  *bp1++ = *bp++;
505  break;
506  case NC_UBYTE:
507  for (bp = (signed char *)src, ubp = dest; count < len; count++)
508  {
509  if (*bp < 0)
510  (*range_error)++;
511  *ubp++ = *bp++;
512  }
513  break;
514  case NC_SHORT:
515  for (bp = (signed char *)src, sp = dest; count < len; count++)
516  *sp++ = *bp++;
517  break;
518  case NC_USHORT:
519  for (bp = (signed char *)src, usp = dest; count < len; count++)
520  {
521  if (*bp < 0)
522  (*range_error)++;
523  *usp++ = *bp++;
524  }
525  break;
526  case NC_INT:
527  for (bp = (signed char *)src, ip = dest; count < len; count++)
528  *ip++ = *bp++;
529  break;
530  case NC_UINT:
531  for (bp = (signed char *)src, uip = dest; count < len; count++)
532  {
533  if (*bp < 0)
534  (*range_error)++;
535  *uip++ = *bp++;
536  }
537  break;
538  case NC_INT64:
539  for (bp = (signed char *)src, lip = dest; count < len; count++)
540  *lip++ = *bp++;
541  break;
542  case NC_UINT64:
543  for (bp = (signed char *)src, ulip = dest; count < len; count++)
544  {
545  if (*bp < 0)
546  (*range_error)++;
547  *ulip++ = *bp++;
548  }
549  break;
550  case NC_FLOAT:
551  for (bp = (signed char *)src, fp = dest; count < len; count++)
552  *fp++ = *bp++;
553  break;
554  case NC_DOUBLE:
555  for (bp = (signed char *)src, dp = dest; count < len; count++)
556  *dp++ = *bp++;
557  break;
558  default:
559  LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
560  __func__, src_type, dest_type));
561  return NC_EBADTYPE;
562  }
563  break;
564 
565  case NC_UBYTE:
566  switch (dest_type)
567  {
568  case NC_BYTE:
569  for (ubp = (unsigned char *)src, bp = dest; count < len; count++)
570  {
571  if (!strict_nc3 && *ubp > X_SCHAR_MAX)
572  (*range_error)++;
573  *bp++ = *ubp++;
574  }
575  break;
576  case NC_SHORT:
577  for (ubp = (unsigned char *)src, sp = dest; count < len; count++)
578  *sp++ = *ubp++;
579  break;
580  case NC_UBYTE:
581  for (ubp = (unsigned char *)src, ubp1 = dest; count < len; count++)
582  *ubp1++ = *ubp++;
583  break;
584  case NC_USHORT:
585  for (ubp = (unsigned char *)src, usp = dest; count < len; count++)
586  *usp++ = *ubp++;
587  break;
588  case NC_INT:
589  for (ubp = (unsigned char *)src, ip = dest; count < len; count++)
590  *ip++ = *ubp++;
591  break;
592  case NC_UINT:
593  for (ubp = (unsigned char *)src, uip = dest; count < len; count++)
594  *uip++ = *ubp++;
595  break;
596  case NC_INT64:
597  for (ubp = (unsigned char *)src, lip = dest; count < len; count++)
598  *lip++ = *ubp++;
599  break;
600  case NC_UINT64:
601  for (ubp = (unsigned char *)src, ulip = dest; count < len; count++)
602  *ulip++ = *ubp++;
603  break;
604  case NC_FLOAT:
605  for (ubp = (unsigned char *)src, fp = dest; count < len; count++)
606  *fp++ = *ubp++;
607  break;
608  case NC_DOUBLE:
609  for (ubp = (unsigned char *)src, dp = dest; count < len; count++)
610  *dp++ = *ubp++;
611  break;
612  default:
613  LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
614  __func__, src_type, dest_type));
615  return NC_EBADTYPE;
616  }
617  break;
618 
619  case NC_SHORT:
620  switch (dest_type)
621  {
622  case NC_UBYTE:
623  for (sp = (short *)src, ubp = dest; count < len; count++)
624  {
625  if (*sp > X_UCHAR_MAX || *sp < 0)
626  (*range_error)++;
627  *ubp++ = *sp++;
628  }
629  break;
630  case NC_BYTE:
631  for (sp = (short *)src, bp = dest; count < len; count++)
632  {
633  if (*sp > X_SCHAR_MAX || *sp < X_SCHAR_MIN)
634  (*range_error)++;
635  *bp++ = *sp++;
636  }
637  break;
638  case NC_SHORT:
639  for (sp = (short *)src, sp1 = dest; count < len; count++)
640  *sp1++ = *sp++;
641  break;
642  case NC_USHORT:
643  for (sp = (short *)src, usp = dest; count < len; count++)
644  {
645  if (*sp < 0)
646  (*range_error)++;
647  *usp++ = *sp++;
648  }
649  break;
650  case NC_INT:
651  for (sp = (short *)src, ip = dest; count < len; count++)
652  *ip++ = *sp++;
653  break;
654  case NC_UINT:
655  for (sp = (short *)src, uip = dest; count < len; count++)
656  {
657  if (*sp < 0)
658  (*range_error)++;
659  *uip++ = *sp++;
660  }
661  break;
662  case NC_INT64:
663  for (sp = (short *)src, lip = dest; count < len; count++)
664  *lip++ = *sp++;
665  break;
666  case NC_UINT64:
667  for (sp = (short *)src, ulip = dest; count < len; count++)
668  {
669  if (*sp < 0)
670  (*range_error)++;
671  *ulip++ = *sp++;
672  }
673  break;
674  case NC_FLOAT:
675  for (sp = (short *)src, fp = dest; count < len; count++)
676  *fp++ = *sp++;
677  break;
678  case NC_DOUBLE:
679  for (sp = (short *)src, dp = dest; count < len; count++)
680  *dp++ = *sp++;
681  break;
682  default:
683  LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
684  __func__, src_type, dest_type));
685  return NC_EBADTYPE;
686  }
687  break;
688 
689  case NC_USHORT:
690  switch (dest_type)
691  {
692  case NC_UBYTE:
693  for (usp = (unsigned short *)src, ubp = dest; count < len; count++)
694  {
695  if (*usp > X_UCHAR_MAX)
696  (*range_error)++;
697  *ubp++ = *usp++;
698  }
699  break;
700  case NC_BYTE:
701  for (usp = (unsigned short *)src, bp = dest; count < len; count++)
702  {
703  if (*usp > X_SCHAR_MAX)
704  (*range_error)++;
705  *bp++ = *usp++;
706  }
707  break;
708  case NC_SHORT:
709  for (usp = (unsigned short *)src, sp = dest; count < len; count++)
710  {
711  if (*usp > X_SHORT_MAX)
712  (*range_error)++;
713  *sp++ = *usp++;
714  }
715  break;
716  case NC_USHORT:
717  for (usp = (unsigned short *)src, usp1 = dest; count < len; count++)
718  *usp1++ = *usp++;
719  break;
720  case NC_INT:
721  for (usp = (unsigned short *)src, ip = dest; count < len; count++)
722  *ip++ = *usp++;
723  break;
724  case NC_UINT:
725  for (usp = (unsigned short *)src, uip = dest; count < len; count++)
726  *uip++ = *usp++;
727  break;
728  case NC_INT64:
729  for (usp = (unsigned short *)src, lip = dest; count < len; count++)
730  *lip++ = *usp++;
731  break;
732  case NC_UINT64:
733  for (usp = (unsigned short *)src, ulip = dest; count < len; count++)
734  *ulip++ = *usp++;
735  break;
736  case NC_FLOAT:
737  for (usp = (unsigned short *)src, fp = dest; count < len; count++)
738  *fp++ = *usp++;
739  break;
740  case NC_DOUBLE:
741  for (usp = (unsigned short *)src, dp = dest; count < len; count++)
742  *dp++ = *usp++;
743  break;
744  default:
745  LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
746  __func__, src_type, dest_type));
747  return NC_EBADTYPE;
748  }
749  break;
750 
751  case NC_INT:
752  switch (dest_type)
753  {
754  case NC_UBYTE:
755  for (ip = (int *)src, ubp = dest; count < len; count++)
756  {
757  if (*ip > X_UCHAR_MAX || *ip < 0)
758  (*range_error)++;
759  *ubp++ = *ip++;
760  }
761  break;
762  case NC_BYTE:
763  for (ip = (int *)src, bp = dest; count < len; count++)
764  {
765  if (*ip > X_SCHAR_MAX || *ip < X_SCHAR_MIN)
766  (*range_error)++;
767  *bp++ = *ip++;
768  }
769  break;
770  case NC_SHORT:
771  for (ip = (int *)src, sp = dest; count < len; count++)
772  {
773  if (*ip > X_SHORT_MAX || *ip < X_SHORT_MIN)
774  (*range_error)++;
775  *sp++ = *ip++;
776  }
777  break;
778  case NC_USHORT:
779  for (ip = (int *)src, usp = dest; count < len; count++)
780  {
781  if (*ip > X_USHORT_MAX || *ip < 0)
782  (*range_error)++;
783  *usp++ = *ip++;
784  }
785  break;
786  case NC_INT: /* src is int */
787  for (ip = (int *)src, ip1 = dest; count < len; count++)
788  {
789  if (*ip > X_INT_MAX || *ip < X_INT_MIN)
790  (*range_error)++;
791  *ip1++ = *ip++;
792  }
793  break;
794  case NC_UINT:
795  for (ip = (int *)src, uip = dest; count < len; count++)
796  {
797  if (*ip > X_UINT_MAX || *ip < 0)
798  (*range_error)++;
799  *uip++ = *ip++;
800  }
801  break;
802  case NC_INT64:
803  for (ip = (int *)src, lip = dest; count < len; count++)
804  *lip++ = *ip++;
805  break;
806  case NC_UINT64:
807  for (ip = (int *)src, ulip = dest; count < len; count++)
808  {
809  if (*ip < 0)
810  (*range_error)++;
811  *ulip++ = *ip++;
812  }
813  break;
814  case NC_FLOAT:
815  for (ip = (int *)src, fp = dest; count < len; count++)
816  *fp++ = *ip++;
817  break;
818  case NC_DOUBLE:
819  for (ip = (int *)src, dp = dest; count < len; count++)
820  *dp++ = *ip++;
821  break;
822  default:
823  LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
824  __func__, src_type, dest_type));
825  return NC_EBADTYPE;
826  }
827  break;
828 
829  case NC_UINT:
830  switch (dest_type)
831  {
832  case NC_UBYTE:
833  for (uip = (unsigned int *)src, ubp = dest; count < len; count++)
834  {
835  if (*uip > X_UCHAR_MAX)
836  (*range_error)++;
837  *ubp++ = *uip++;
838  }
839  break;
840  case NC_BYTE:
841  for (uip = (unsigned int *)src, bp = dest; count < len; count++)
842  {
843  if (*uip > X_SCHAR_MAX)
844  (*range_error)++;
845  *bp++ = *uip++;
846  }
847  break;
848  case NC_SHORT:
849  for (uip = (unsigned int *)src, sp = dest; count < len; count++)
850  {
851  if (*uip > X_SHORT_MAX)
852  (*range_error)++;
853  *sp++ = *uip++;
854  }
855  break;
856  case NC_USHORT:
857  for (uip = (unsigned int *)src, usp = dest; count < len; count++)
858  {
859  if (*uip > X_USHORT_MAX)
860  (*range_error)++;
861  *usp++ = *uip++;
862  }
863  break;
864  case NC_INT:
865  for (uip = (unsigned int *)src, ip = dest; count < len; count++)
866  {
867  if (*uip > X_INT_MAX)
868  (*range_error)++;
869  *ip++ = *uip++;
870  }
871  break;
872  case NC_UINT:
873  for (uip = (unsigned int *)src, uip1 = dest; count < len; count++)
874  {
875  if (*uip > X_UINT_MAX)
876  (*range_error)++;
877  *uip1++ = *uip++;
878  }
879  break;
880  case NC_INT64:
881  for (uip = (unsigned int *)src, lip = dest; count < len; count++)
882  *lip++ = *uip++;
883  break;
884  case NC_UINT64:
885  for (uip = (unsigned int *)src, ulip = dest; count < len; count++)
886  *ulip++ = *uip++;
887  break;
888  case NC_FLOAT:
889  for (uip = (unsigned int *)src, fp = dest; count < len; count++)
890  *fp++ = *uip++;
891  break;
892  case NC_DOUBLE:
893  for (uip = (unsigned int *)src, dp = dest; count < len; count++)
894  *dp++ = *uip++;
895  break;
896  default:
897  LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
898  __func__, src_type, dest_type));
899  return NC_EBADTYPE;
900  }
901  break;
902 
903  case NC_INT64:
904  switch (dest_type)
905  {
906  case NC_UBYTE:
907  for (lip = (long long *)src, ubp = dest; count < len; count++)
908  {
909  if (*lip > X_UCHAR_MAX || *lip < 0)
910  (*range_error)++;
911  *ubp++ = *lip++;
912  }
913  break;
914  case NC_BYTE:
915  for (lip = (long long *)src, bp = dest; count < len; count++)
916  {
917  if (*lip > X_SCHAR_MAX || *lip < X_SCHAR_MIN)
918  (*range_error)++;
919  *bp++ = *lip++;
920  }
921  break;
922  case NC_SHORT:
923  for (lip = (long long *)src, sp = dest; count < len; count++)
924  {
925  if (*lip > X_SHORT_MAX || *lip < X_SHORT_MIN)
926  (*range_error)++;
927  *sp++ = *lip++;
928  }
929  break;
930  case NC_USHORT:
931  for (lip = (long long *)src, usp = dest; count < len; count++)
932  {
933  if (*lip > X_USHORT_MAX || *lip < 0)
934  (*range_error)++;
935  *usp++ = *lip++;
936  }
937  break;
938  case NC_UINT:
939  for (lip = (long long *)src, uip = dest; count < len; count++)
940  {
941  if (*lip > X_UINT_MAX || *lip < 0)
942  (*range_error)++;
943  *uip++ = *lip++;
944  }
945  break;
946  case NC_INT:
947  for (lip = (long long *)src, ip = dest; count < len; count++)
948  {
949  if (*lip > X_INT_MAX || *lip < X_INT_MIN)
950  (*range_error)++;
951  *ip++ = *lip++;
952  }
953  break;
954  case NC_INT64:
955  for (lip = (long long *)src, lip1 = dest; count < len; count++)
956  *lip1++ = *lip++;
957  break;
958  case NC_UINT64:
959  for (lip = (long long *)src, ulip = dest; count < len; count++)
960  {
961  if (*lip < 0)
962  (*range_error)++;
963  *ulip++ = *lip++;
964  }
965  break;
966  case NC_FLOAT:
967  for (lip = (long long *)src, fp = dest; count < len; count++)
968  *fp++ = *lip++;
969  break;
970  case NC_DOUBLE:
971  for (lip = (long long *)src, dp = dest; count < len; count++)
972  *dp++ = *lip++;
973  break;
974  default:
975  LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
976  __func__, src_type, dest_type));
977  return NC_EBADTYPE;
978  }
979  break;
980 
981  case NC_UINT64:
982  switch (dest_type)
983  {
984  case NC_UBYTE:
985  for (ulip = (unsigned long long *)src, ubp = dest; count < len; count++)
986  {
987  if (*ulip > X_UCHAR_MAX)
988  (*range_error)++;
989  *ubp++ = *ulip++;
990  }
991  break;
992  case NC_BYTE:
993  for (ulip = (unsigned long long *)src, bp = dest; count < len; count++)
994  {
995  if (*ulip > X_SCHAR_MAX)
996  (*range_error)++;
997  *bp++ = *ulip++;
998  }
999  break;
1000  case NC_SHORT:
1001  for (ulip = (unsigned long long *)src, sp = dest; count < len; count++)
1002  {
1003  if (*ulip > X_SHORT_MAX)
1004  (*range_error)++;
1005  *sp++ = *ulip++;
1006  }
1007  break;
1008  case NC_USHORT:
1009  for (ulip = (unsigned long long *)src, usp = dest; count < len; count++)
1010  {
1011  if (*ulip > X_USHORT_MAX)
1012  (*range_error)++;
1013  *usp++ = *ulip++;
1014  }
1015  break;
1016  case NC_UINT:
1017  for (ulip = (unsigned long long *)src, uip = dest; count < len; count++)
1018  {
1019  if (*ulip > X_UINT_MAX)
1020  (*range_error)++;
1021  *uip++ = *ulip++;
1022  }
1023  break;
1024  case NC_INT:
1025  for (ulip = (unsigned long long *)src, ip = dest; count < len; count++)
1026  {
1027  if (*ulip > X_INT_MAX)
1028  (*range_error)++;
1029  *ip++ = *ulip++;
1030  }
1031  break;
1032  case NC_INT64:
1033  for (ulip = (unsigned long long *)src, lip = dest; count < len; count++)
1034  {
1035  if (*ulip > X_INT64_MAX)
1036  (*range_error)++;
1037  *lip++ = *ulip++;
1038  }
1039  break;
1040  case NC_UINT64:
1041  for (ulip = (unsigned long long *)src, ulip1 = dest; count < len; count++)
1042  *ulip1++ = *ulip++;
1043  break;
1044  case NC_FLOAT:
1045  for (ulip = (unsigned long long *)src, fp = dest; count < len; count++)
1046  *fp++ = *ulip++;
1047  break;
1048  case NC_DOUBLE:
1049  for (ulip = (unsigned long long *)src, dp = dest; count < len; count++)
1050  *dp++ = *ulip++;
1051  break;
1052  default:
1053  LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
1054  __func__, src_type, dest_type));
1055  return NC_EBADTYPE;
1056  }
1057  break;
1058 
1059  case NC_FLOAT:
1060  switch (dest_type)
1061  {
1062  case NC_UBYTE:
1063  for (fp = (float *)src, ubp = dest; count < len; count++)
1064  {
1065  if (*fp > X_UCHAR_MAX || *fp < 0)
1066  (*range_error)++;
1067  *ubp++ = *fp++;
1068  }
1069  break;
1070  case NC_BYTE:
1071  for (fp = (float *)src, bp = dest; count < len; count++)
1072  {
1073  if (*fp > (double)X_SCHAR_MAX || *fp < (double)X_SCHAR_MIN)
1074  (*range_error)++;
1075  *bp++ = *fp++;
1076  }
1077  break;
1078  case NC_SHORT:
1079  for (fp = (float *)src, sp = dest; count < len; count++)
1080  {
1081  if (*fp > (double)X_SHORT_MAX || *fp < (double)X_SHORT_MIN)
1082  (*range_error)++;
1083  *sp++ = *fp++;
1084  }
1085  break;
1086  case NC_USHORT:
1087  for (fp = (float *)src, usp = dest; count < len; count++)
1088  {
1089  if (*fp > X_USHORT_MAX || *fp < 0)
1090  (*range_error)++;
1091  *usp++ = *fp++;
1092  }
1093  break;
1094  case NC_UINT:
1095  for (fp = (float *)src, uip = dest; count < len; count++)
1096  {
1097  if (*fp > X_UINT_MAX || *fp < 0)
1098  (*range_error)++;
1099  *uip++ = *fp++;
1100  }
1101  break;
1102  case NC_INT:
1103  for (fp = (float *)src, ip = dest; count < len; count++)
1104  {
1105  if (*fp > (double)X_INT_MAX || *fp < (double)X_INT_MIN)
1106  (*range_error)++;
1107  *ip++ = *fp++;
1108  }
1109  break;
1110  case NC_INT64:
1111  for (fp = (float *)src, lip = dest; count < len; count++)
1112  {
1113  if (*fp > X_INT64_MAX || *fp <X_INT64_MIN)
1114  (*range_error)++;
1115  *lip++ = *fp++;
1116  }
1117  break;
1118  case NC_UINT64:
1119  for (fp = (float *)src, lip = dest; count < len; count++)
1120  {
1121  if (*fp > X_UINT64_MAX || *fp < 0)
1122  (*range_error)++;
1123  *lip++ = *fp++;
1124  }
1125  break;
1126  case NC_FLOAT:
1127  for (fp = (float *)src, fp1 = dest; count < len; count++)
1128  {
1129  /* if (*fp > X_FLOAT_MAX || *fp < X_FLOAT_MIN)
1130  (*range_error)++;*/
1131  *fp1++ = *fp++;
1132  }
1133  break;
1134  case NC_DOUBLE:
1135  for (fp = (float *)src, dp = dest; count < len; count++)
1136  *dp++ = *fp++;
1137  break;
1138  default:
1139  LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
1140  __func__, src_type, dest_type));
1141  return NC_EBADTYPE;
1142  }
1143  break;
1144 
1145  case NC_DOUBLE:
1146  switch (dest_type)
1147  {
1148  case NC_UBYTE:
1149  for (dp = (double *)src, ubp = dest; count < len; count++)
1150  {
1151  if (*dp > X_UCHAR_MAX || *dp < 0)
1152  (*range_error)++;
1153  *ubp++ = *dp++;
1154  }
1155  break;
1156  case NC_BYTE:
1157  for (dp = (double *)src, bp = dest; count < len; count++)
1158  {
1159  if (*dp > X_SCHAR_MAX || *dp < X_SCHAR_MIN)
1160  (*range_error)++;
1161  *bp++ = *dp++;
1162  }
1163  break;
1164  case NC_SHORT:
1165  for (dp = (double *)src, sp = dest; count < len; count++)
1166  {
1167  if (*dp > X_SHORT_MAX || *dp < X_SHORT_MIN)
1168  (*range_error)++;
1169  *sp++ = *dp++;
1170  }
1171  break;
1172  case NC_USHORT:
1173  for (dp = (double *)src, usp = dest; count < len; count++)
1174  {
1175  if (*dp > X_USHORT_MAX || *dp < 0)
1176  (*range_error)++;
1177  *usp++ = *dp++;
1178  }
1179  break;
1180  case NC_UINT:
1181  for (dp = (double *)src, uip = dest; count < len; count++)
1182  {
1183  if (*dp > X_UINT_MAX || *dp < 0)
1184  (*range_error)++;
1185  *uip++ = *dp++;
1186  }
1187  break;
1188  case NC_INT:
1189  for (dp = (double *)src, ip = dest; count < len; count++)
1190  {
1191  if (*dp > X_INT_MAX || *dp < X_INT_MIN)
1192  (*range_error)++;
1193  *ip++ = *dp++;
1194  }
1195  break;
1196  case NC_INT64:
1197  for (dp = (double *)src, lip = dest; count < len; count++)
1198  {
1199  if (*dp > X_INT64_MAX || *dp < X_INT64_MIN)
1200  (*range_error)++;
1201  *lip++ = *dp++;
1202  }
1203  break;
1204  case NC_UINT64:
1205  for (dp = (double *)src, lip = dest; count < len; count++)
1206  {
1207  if (*dp > X_UINT64_MAX || *dp < 0)
1208  (*range_error)++;
1209  *lip++ = *dp++;
1210  }
1211  break;
1212  case NC_FLOAT:
1213  for (dp = (double *)src, fp = dest; count < len; count++)
1214  {
1215  if (isgreater(*dp, X_FLOAT_MAX) || isless(*dp, X_FLOAT_MIN))
1216  (*range_error)++;
1217  *fp++ = *dp++;
1218  }
1219  break;
1220  case NC_DOUBLE:
1221  for (dp = (double *)src, dp1 = dest; count < len; count++)
1222  {
1223  /* if (*dp > X_DOUBLE_MAX || *dp < X_DOUBLE_MIN) */
1224  /* (*range_error)++; */
1225  *dp1++ = *dp++;
1226  }
1227  break;
1228  default:
1229  LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
1230  __func__, src_type, dest_type));
1231  return NC_EBADTYPE;
1232  }
1233  break;
1234 
1235  default:
1236  LOG((0, "%s: unexpected src type. src_type %d, dest_type %d",
1237  __func__, src_type, dest_type));
1238  return NC_EBADTYPE;
1239  }
1240  return NC_NOERR;
1241 }
1242 
1254 int
1255 nc4_get_default_fill_value(const NC_TYPE_INFO_T *type_info, void *fill_value)
1256 {
1257  switch (type_info->hdr.id)
1258  {
1259  case NC_CHAR:
1260  *(char *)fill_value = NC_FILL_CHAR;
1261  break;
1262 
1263  case NC_STRING:
1264  *(char **)fill_value = strdup(NC_FILL_STRING);
1265  break;
1266 
1267  case NC_BYTE:
1268  *(signed char *)fill_value = NC_FILL_BYTE;
1269  break;
1270 
1271  case NC_SHORT:
1272  *(short *)fill_value = NC_FILL_SHORT;
1273  break;
1274 
1275  case NC_INT:
1276  *(int *)fill_value = NC_FILL_INT;
1277  break;
1278 
1279  case NC_UBYTE:
1280  *(unsigned char *)fill_value = NC_FILL_UBYTE;
1281  break;
1282 
1283  case NC_USHORT:
1284  *(unsigned short *)fill_value = NC_FILL_USHORT;
1285  break;
1286 
1287  case NC_UINT:
1288  *(unsigned int *)fill_value = NC_FILL_UINT;
1289  break;
1290 
1291  case NC_INT64:
1292  *(long long *)fill_value = NC_FILL_INT64;
1293  break;
1294 
1295  case NC_UINT64:
1296  *(unsigned long long *)fill_value = NC_FILL_UINT64;
1297  break;
1298 
1299  case NC_FLOAT:
1300  *(float *)fill_value = NC_FILL_FLOAT;
1301  break;
1302 
1303  case NC_DOUBLE:
1304  *(double *)fill_value = NC_FILL_DOUBLE;
1305  break;
1306 
1307  default:
1308  return NC_EINVAL;
1309  }
1310 
1311  return NC_NOERR;
1312 }
1313 
1326 int
1327 nc4_get_typelen_mem(NC_FILE_INFO_T *h5, nc_type xtype, size_t *len)
1328 {
1329  NC_TYPE_INFO_T *type;
1330  int retval;
1331 
1332  LOG((4, "%s xtype: %d", __func__, xtype));
1333  assert(len);
1334 
1335  /* If this is an atomic type, the answer is easy. */
1336  switch (xtype)
1337  {
1338  case NC_BYTE:
1339  case NC_CHAR:
1340  case NC_UBYTE:
1341  *len = sizeof(char);
1342  return NC_NOERR;
1343  case NC_SHORT:
1344  case NC_USHORT:
1345  *len = sizeof(short);
1346  return NC_NOERR;
1347  case NC_INT:
1348  case NC_UINT:
1349  *len = sizeof(int);
1350  return NC_NOERR;
1351  case NC_FLOAT:
1352  *len = sizeof(float);
1353  return NC_NOERR;
1354  case NC_DOUBLE:
1355  *len = sizeof(double);
1356  return NC_NOERR;
1357  case NC_INT64:
1358  case NC_UINT64:
1359  *len = sizeof(long long);
1360  return NC_NOERR;
1361  case NC_STRING:
1362  *len = sizeof(char *);
1363  return NC_NOERR;
1364  }
1365 
1366  /* See if var is compound type. */
1367  if ((retval = nc4_find_type(h5, xtype, &type)))
1368  return retval;
1369 
1370  if (!type)
1371  return NC_EBADTYPE;
1372 
1373  *len = type->size;
1374 
1375  LOG((5, "type->size: %d", type->size));
1376 
1377  return NC_NOERR;
1378 }
#define NC_FILL_UBYTE
Default fill value.
Definition: netcdf.h:73
#define NC_ENOMEM
Memory allocation (malloc) failure.
Definition: netcdf.h:405
#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:294
#define NC_CHAR
ISO/ASCII character.
Definition: netcdf.h:36
#define NC_MAX_INT
Max or min values for a type.
Definition: netcdf.h:94
#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:295
#define NC_FILL_CHAR
Default fill value.
Definition: netcdf.h:68
#define NC_UBYTE
unsigned 1 byte int
Definition: netcdf.h:42
#define NC_ERANGE
Math result not representable.
Definition: netcdf.h:404
#define NC_FILL_UINT
Default fill value.
Definition: netcdf.h:75
#define NC_UINT
unsigned 4-byte int
Definition: netcdf.h:44
#define NC_INT64
signed 8-byte int
Definition: netcdf.h:45
#define NC_STRING
string
Definition: netcdf.h:47
#define NC_DOUBLE
double precision floating point number
Definition: netcdf.h:41
#define NC_FILL_UINT64
Default fill value.
Definition: netcdf.h:77
#define NC_INDEPENDENT
Use with nc_var_par_access() to set parallel access to independent.
Definition: netcdf_par.h:30
int nc_type
The nc_type type is just an int.
Definition: netcdf.h:25
#define NC_FILL_STRING
Default fill value.
Definition: netcdf.h:78
#define NC_COLLECTIVE
Use with nc_var_par_access() to set parallel access to collective.
Definition: netcdf_par.h:32
#define NC_BYTE
signed 1 byte integer
Definition: netcdf.h:35
#define NC_FILL_INT
Default fill value.
Definition: netcdf.h:70
#define NC_EBADTYPE
Not a netcdf data type.
Definition: netcdf.h:367
#define NC_EINVAL
Invalid Argument.
Definition: netcdf.h:335
#define NC_INT
signed 4 byte integer
Definition: netcdf.h:38
#define NC_MAX_NAME
Maximum for classic library.
Definition: netcdf.h:275
#define NC_USHORT
unsigned 2-byte int
Definition: netcdf.h:43
#define NC_FILL_FLOAT
Default fill value.
Definition: netcdf.h:71
#define NC_ENOPAR
Parallel operation on file opened for non-parallel access.
Definition: netcdf.h:451
#define NC_SHORT
signed 2 byte integer
Definition: netcdf.h:37
#define NC_ENOTVAR
Variable not found.
Definition: netcdf.h:379
#define NC_NOERR
No Error.
Definition: netcdf.h:325
#define NC_FILL_SHORT
Default fill value.
Definition: netcdf.h:69
#define NC_FILL_DOUBLE
Default fill value.
Definition: netcdf.h:72
#define NC_FILL_USHORT
Default fill value.
Definition: netcdf.h:74
#define NC_FILL_BYTE
Default fill value.
Definition: netcdf.h:67
#define NC_GLOBAL
Attribute id to put/get a global attribute.
Definition: netcdf.h:248
#define NC_FLOAT
single precision floating point number
Definition: netcdf.h:40
#define NC_FILL_INT64
Default fill value.
Definition: netcdf.h:76
#define NC_UINT64
unsigned 8-byte int
Definition: netcdf.h:46

Return to the Main Unidata NetCDF page.
Generated on Thu Nov 21 2019 04:31:58 for NetCDF. NetCDF is a Unidata library.