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