NetCDF  4.7.4
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 *storagep, 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  /* Did the user want the chunksizes? */
191  if (var->storage == NC_CHUNKED && chunksizesp)
192  {
193  for (d = 0; d < var->ndims; d++)
194  {
195  chunksizesp[d] = var->chunksizes[d];
196  LOG((4, "chunksizesp[%d]=%d", d, chunksizesp[d]));
197  }
198  }
199 
200  /* Did the user inquire about the storage? */
201  if (storagep)
202  *storagep = var->storage;
203 
204  /* Filter stuff. */
205  if (shufflep)
206  *shufflep = (int)var->shuffle;
207  if (fletcher32p)
208  *fletcher32p = (int)var->fletcher32;
209 
210  if (deflatep)
211  return NC_EFILTER;
212 
213  if (idp) {
214 #if 0
215  NC* nc = h5->controller;
216  NC_FILTER_ACTION action;
217  action.action = NCFILTER_INQ_FILTER;
218  action.format = NC_FORMATX_NC_HDF5;
219  action.id = (idp)?*idp:0;
220  action.nelems = (nparamsp)?*nparamsp:0;
221  action.elems = params;
222  if((retval = nc->dispatch->filter_actions(ncid,varid,&action)) == NC_NOERR) {
223  if(idp) *idp = action.id;
224  if(nparamsp) *nparamsp = action.nelems;
225  }
226  return retval;
227 #else
228  return NC_EFILTER;
229 #endif
230  }
231 
232  /* Fill value stuff. */
233  if (no_fill)
234  *no_fill = (int)var->no_fill;
235 
236  /* Don't do a thing with fill_valuep if no_fill mode is set for
237  * this var, or if fill_valuep is NULL. */
238  if (!var->no_fill && fill_valuep)
239  {
240  /* Do we have a fill value for this var? */
241  if (var->fill_value)
242  {
243  if (var->type_info->nc_type_class == NC_STRING)
244  {
245  assert(*(char **)var->fill_value);
246  /* This will allocate memory and copy the string. */
247  if (!(*(char **)fill_valuep = strdup(*(char **)var->fill_value)))
248  {
249  free(*(char **)fill_valuep);
250  return NC_ENOMEM;
251  }
252  }
253  else
254  {
255  assert(var->type_info->size);
256  memcpy(fill_valuep, var->fill_value, var->type_info->size);
257  }
258  }
259  else
260  {
261  if (var->type_info->nc_type_class == NC_STRING)
262  {
263  if (!(*(char **)fill_valuep = calloc(1, sizeof(char *))))
264  return NC_ENOMEM;
265 
266  if ((retval = nc4_get_default_fill_value(var->type_info, (char **)fill_valuep)))
267  {
268  free(*(char **)fill_valuep);
269  return retval;
270  }
271  }
272  else
273  {
274  if ((retval = nc4_get_default_fill_value(var->type_info, fill_valuep)))
275  return retval;
276  }
277  }
278  }
279 
280  /* Does the user want the endianness of this variable? */
281  if (endiannessp)
282  *endiannessp = var->type_info->endianness;
283 
284  return NC_NOERR;
285 }
286 
303 int
304 nc_inq_var_chunking_ints(int ncid, int varid, int *storagep, int *chunksizesp)
305 {
306  NC_VAR_INFO_T *var;
307  size_t *cs = NULL;
308  int i, retval;
309 
310  /* Get pointer to the var. */
311  if ((retval = nc4_find_grp_h5_var(ncid, varid, NULL, NULL, &var)))
312  return retval;
313  assert(var);
314 
315  /* Allocate space for the size_t copy of the chunksizes array. */
316  if (var->ndims)
317  if (!(cs = malloc(var->ndims * sizeof(size_t))))
318  return NC_ENOMEM;
319 
320  /* Call the netcdf-4 version directly. */
321  retval = NC4_inq_var_all(ncid, varid, NULL, NULL, NULL, NULL, NULL,
322  NULL, NULL, NULL, NULL, storagep, cs, NULL,
323  NULL, NULL, NULL, NULL, NULL);
324 
325  /* Copy from size_t array. */
326  if (!retval && chunksizesp && var->storage == NC_CHUNKED)
327  {
328  for (i = 0; i < var->ndims; i++)
329  {
330  chunksizesp[i] = (int)cs[i];
331  if (cs[i] > NC_MAX_INT)
332  retval = NC_ERANGE;
333  }
334  }
335 
336  if (var->ndims)
337  free(cs);
338  return retval;
339 }
340 
353 int
354 NC4_inq_varid(int ncid, const char *name, int *varidp)
355 {
356  NC *nc;
357  NC_GRP_INFO_T *grp;
358  NC_VAR_INFO_T *var;
359  char norm_name[NC_MAX_NAME + 1];
360  int retval;
361 
362  if (!name)
363  return NC_EINVAL;
364  if (!varidp)
365  return NC_NOERR;
366 
367  LOG((2, "%s: ncid 0x%x name %s", __func__, ncid, name));
368 
369  /* Find info for this file and group, and set pointer to each. */
370  if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, NULL)))
371  return retval;
372 
373  /* Normalize name. */
374  if ((retval = nc4_normalize_name(name, norm_name)))
375  return retval;
376 
377  /* Find var of this name. */
378  var = (NC_VAR_INFO_T*)ncindexlookup(grp->vars,norm_name);
379  if(var)
380  {
381  *varidp = var->hdr.id;
382  return NC_NOERR;
383  }
384  return NC_ENOTVAR;
385 }
386 
405 int
406 NC4_var_par_access(int ncid, int varid, int par_access)
407 {
408 #ifndef USE_PARALLEL4
409  NC_UNUSED(ncid);
410  NC_UNUSED(varid);
411  NC_UNUSED(par_access);
412  return NC_ENOPAR;
413 #else
414  NC *nc;
415  NC_GRP_INFO_T *grp;
416  NC_FILE_INFO_T *h5;
417  NC_VAR_INFO_T *var;
418  int retval;
419 
420  LOG((1, "%s: ncid 0x%x varid %d par_access %d", __func__, ncid,
421  varid, par_access));
422 
423  if (par_access != NC_INDEPENDENT && par_access != NC_COLLECTIVE)
424  return NC_EINVAL;
425 
426  /* Find info for this file and group, and set pointer to each. */
427  if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
428  return retval;
429 
430  /* This function only for files opened with nc_open_par or nc_create_par. */
431  if (!h5->parallel)
432  return NC_ENOPAR;
433 
434  /* Find the var, and set its preference. */
435  var = (NC_VAR_INFO_T*)ncindexith(grp->vars,varid);
436  if (!var) return NC_ENOTVAR;
437  assert(var->hdr.id == varid);
438 
439  /* If zlib, shuffle, or fletcher32 filters are in use, then access
440  * must be collective. Fail an attempt to set such a variable to
441  * independent access. */
442  if ((nclistlength(var->filters) > 0 || var->shuffle || var->fletcher32) &&
443  par_access == NC_INDEPENDENT)
444  return NC_EINVAL;
445 
446  if (par_access)
447  var->parallel_access = NC_COLLECTIVE;
448  else
449  var->parallel_access = NC_INDEPENDENT;
450  return NC_NOERR;
451 #endif /* USE_PARALLEL4 */
452 }
453 
476 int
477 nc4_convert_type(const void *src, void *dest, const nc_type src_type,
478  const nc_type dest_type, const size_t len, int *range_error,
479  const void *fill_value, int strict_nc3)
480 {
481  char *cp, *cp1;
482  float *fp, *fp1;
483  double *dp, *dp1;
484  int *ip, *ip1;
485  short *sp, *sp1;
486  signed char *bp, *bp1;
487  unsigned char *ubp, *ubp1;
488  unsigned short *usp, *usp1;
489  unsigned int *uip, *uip1;
490  long long *lip, *lip1;
491  unsigned long long *ulip, *ulip1;
492  size_t count = 0;
493 
494  *range_error = 0;
495  LOG((3, "%s: len %d src_type %d dest_type %d", __func__, len, src_type,
496  dest_type));
497 
498  /* OK, this is ugly. If you can think of anything better, I'm open
499  to suggestions!
500 
501  Note that we don't use a default fill value for type
502  NC_BYTE. This is because Lord Voldemort cast a nofilleramous spell
503  at Harry Potter, but it bounced off his scar and hit the netcdf-4
504  code.
505  */
506  switch (src_type)
507  {
508  case NC_CHAR:
509  switch (dest_type)
510  {
511  case NC_CHAR:
512  for (cp = (char *)src, cp1 = dest; count < len; count++)
513  *cp1++ = *cp++;
514  break;
515  default:
516  LOG((0, "%s: Unknown destination type.", __func__));
517  }
518  break;
519 
520  case NC_BYTE:
521  switch (dest_type)
522  {
523  case NC_BYTE:
524  for (bp = (signed char *)src, bp1 = dest; count < len; count++)
525  *bp1++ = *bp++;
526  break;
527  case NC_UBYTE:
528  for (bp = (signed char *)src, ubp = dest; count < len; count++)
529  {
530  if (*bp < 0)
531  (*range_error)++;
532  *ubp++ = *bp++;
533  }
534  break;
535  case NC_SHORT:
536  for (bp = (signed char *)src, sp = dest; count < len; count++)
537  *sp++ = *bp++;
538  break;
539  case NC_USHORT:
540  for (bp = (signed char *)src, usp = dest; count < len; count++)
541  {
542  if (*bp < 0)
543  (*range_error)++;
544  *usp++ = *bp++;
545  }
546  break;
547  case NC_INT:
548  for (bp = (signed char *)src, ip = dest; count < len; count++)
549  *ip++ = *bp++;
550  break;
551  case NC_UINT:
552  for (bp = (signed char *)src, uip = dest; count < len; count++)
553  {
554  if (*bp < 0)
555  (*range_error)++;
556  *uip++ = *bp++;
557  }
558  break;
559  case NC_INT64:
560  for (bp = (signed char *)src, lip = dest; count < len; count++)
561  *lip++ = *bp++;
562  break;
563  case NC_UINT64:
564  for (bp = (signed char *)src, ulip = dest; count < len; count++)
565  {
566  if (*bp < 0)
567  (*range_error)++;
568  *ulip++ = *bp++;
569  }
570  break;
571  case NC_FLOAT:
572  for (bp = (signed char *)src, fp = dest; count < len; count++)
573  *fp++ = *bp++;
574  break;
575  case NC_DOUBLE:
576  for (bp = (signed char *)src, dp = dest; count < len; count++)
577  *dp++ = *bp++;
578  break;
579  default:
580  LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
581  __func__, src_type, dest_type));
582  return NC_EBADTYPE;
583  }
584  break;
585 
586  case NC_UBYTE:
587  switch (dest_type)
588  {
589  case NC_BYTE:
590  for (ubp = (unsigned char *)src, bp = dest; count < len; count++)
591  {
592  if (!strict_nc3 && *ubp > X_SCHAR_MAX)
593  (*range_error)++;
594  *bp++ = *ubp++;
595  }
596  break;
597  case NC_SHORT:
598  for (ubp = (unsigned char *)src, sp = dest; count < len; count++)
599  *sp++ = *ubp++;
600  break;
601  case NC_UBYTE:
602  for (ubp = (unsigned char *)src, ubp1 = dest; count < len; count++)
603  *ubp1++ = *ubp++;
604  break;
605  case NC_USHORT:
606  for (ubp = (unsigned char *)src, usp = dest; count < len; count++)
607  *usp++ = *ubp++;
608  break;
609  case NC_INT:
610  for (ubp = (unsigned char *)src, ip = dest; count < len; count++)
611  *ip++ = *ubp++;
612  break;
613  case NC_UINT:
614  for (ubp = (unsigned char *)src, uip = dest; count < len; count++)
615  *uip++ = *ubp++;
616  break;
617  case NC_INT64:
618  for (ubp = (unsigned char *)src, lip = dest; count < len; count++)
619  *lip++ = *ubp++;
620  break;
621  case NC_UINT64:
622  for (ubp = (unsigned char *)src, ulip = dest; count < len; count++)
623  *ulip++ = *ubp++;
624  break;
625  case NC_FLOAT:
626  for (ubp = (unsigned char *)src, fp = dest; count < len; count++)
627  *fp++ = *ubp++;
628  break;
629  case NC_DOUBLE:
630  for (ubp = (unsigned char *)src, dp = dest; count < len; count++)
631  *dp++ = *ubp++;
632  break;
633  default:
634  LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
635  __func__, src_type, dest_type));
636  return NC_EBADTYPE;
637  }
638  break;
639 
640  case NC_SHORT:
641  switch (dest_type)
642  {
643  case NC_UBYTE:
644  for (sp = (short *)src, ubp = dest; count < len; count++)
645  {
646  if (*sp > X_UCHAR_MAX || *sp < 0)
647  (*range_error)++;
648  *ubp++ = *sp++;
649  }
650  break;
651  case NC_BYTE:
652  for (sp = (short *)src, bp = dest; count < len; count++)
653  {
654  if (*sp > X_SCHAR_MAX || *sp < X_SCHAR_MIN)
655  (*range_error)++;
656  *bp++ = *sp++;
657  }
658  break;
659  case NC_SHORT:
660  for (sp = (short *)src, sp1 = dest; count < len; count++)
661  *sp1++ = *sp++;
662  break;
663  case NC_USHORT:
664  for (sp = (short *)src, usp = dest; count < len; count++)
665  {
666  if (*sp < 0)
667  (*range_error)++;
668  *usp++ = *sp++;
669  }
670  break;
671  case NC_INT:
672  for (sp = (short *)src, ip = dest; count < len; count++)
673  *ip++ = *sp++;
674  break;
675  case NC_UINT:
676  for (sp = (short *)src, uip = dest; count < len; count++)
677  {
678  if (*sp < 0)
679  (*range_error)++;
680  *uip++ = *sp++;
681  }
682  break;
683  case NC_INT64:
684  for (sp = (short *)src, lip = dest; count < len; count++)
685  *lip++ = *sp++;
686  break;
687  case NC_UINT64:
688  for (sp = (short *)src, ulip = dest; count < len; count++)
689  {
690  if (*sp < 0)
691  (*range_error)++;
692  *ulip++ = *sp++;
693  }
694  break;
695  case NC_FLOAT:
696  for (sp = (short *)src, fp = dest; count < len; count++)
697  *fp++ = *sp++;
698  break;
699  case NC_DOUBLE:
700  for (sp = (short *)src, dp = dest; count < len; count++)
701  *dp++ = *sp++;
702  break;
703  default:
704  LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
705  __func__, src_type, dest_type));
706  return NC_EBADTYPE;
707  }
708  break;
709 
710  case NC_USHORT:
711  switch (dest_type)
712  {
713  case NC_UBYTE:
714  for (usp = (unsigned short *)src, ubp = dest; count < len; count++)
715  {
716  if (*usp > X_UCHAR_MAX)
717  (*range_error)++;
718  *ubp++ = *usp++;
719  }
720  break;
721  case NC_BYTE:
722  for (usp = (unsigned short *)src, bp = dest; count < len; count++)
723  {
724  if (*usp > X_SCHAR_MAX)
725  (*range_error)++;
726  *bp++ = *usp++;
727  }
728  break;
729  case NC_SHORT:
730  for (usp = (unsigned short *)src, sp = dest; count < len; count++)
731  {
732  if (*usp > X_SHORT_MAX)
733  (*range_error)++;
734  *sp++ = *usp++;
735  }
736  break;
737  case NC_USHORT:
738  for (usp = (unsigned short *)src, usp1 = dest; count < len; count++)
739  *usp1++ = *usp++;
740  break;
741  case NC_INT:
742  for (usp = (unsigned short *)src, ip = dest; count < len; count++)
743  *ip++ = *usp++;
744  break;
745  case NC_UINT:
746  for (usp = (unsigned short *)src, uip = dest; count < len; count++)
747  *uip++ = *usp++;
748  break;
749  case NC_INT64:
750  for (usp = (unsigned short *)src, lip = dest; count < len; count++)
751  *lip++ = *usp++;
752  break;
753  case NC_UINT64:
754  for (usp = (unsigned short *)src, ulip = dest; count < len; count++)
755  *ulip++ = *usp++;
756  break;
757  case NC_FLOAT:
758  for (usp = (unsigned short *)src, fp = dest; count < len; count++)
759  *fp++ = *usp++;
760  break;
761  case NC_DOUBLE:
762  for (usp = (unsigned short *)src, dp = dest; count < len; count++)
763  *dp++ = *usp++;
764  break;
765  default:
766  LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
767  __func__, src_type, dest_type));
768  return NC_EBADTYPE;
769  }
770  break;
771 
772  case NC_INT:
773  switch (dest_type)
774  {
775  case NC_UBYTE:
776  for (ip = (int *)src, ubp = dest; count < len; count++)
777  {
778  if (*ip > X_UCHAR_MAX || *ip < 0)
779  (*range_error)++;
780  *ubp++ = *ip++;
781  }
782  break;
783  case NC_BYTE:
784  for (ip = (int *)src, bp = dest; count < len; count++)
785  {
786  if (*ip > X_SCHAR_MAX || *ip < X_SCHAR_MIN)
787  (*range_error)++;
788  *bp++ = *ip++;
789  }
790  break;
791  case NC_SHORT:
792  for (ip = (int *)src, sp = dest; count < len; count++)
793  {
794  if (*ip > X_SHORT_MAX || *ip < X_SHORT_MIN)
795  (*range_error)++;
796  *sp++ = *ip++;
797  }
798  break;
799  case NC_USHORT:
800  for (ip = (int *)src, usp = dest; count < len; count++)
801  {
802  if (*ip > X_USHORT_MAX || *ip < 0)
803  (*range_error)++;
804  *usp++ = *ip++;
805  }
806  break;
807  case NC_INT: /* src is int */
808  for (ip = (int *)src, ip1 = dest; count < len; count++)
809  {
810  if (*ip > X_INT_MAX || *ip < X_INT_MIN)
811  (*range_error)++;
812  *ip1++ = *ip++;
813  }
814  break;
815  case NC_UINT:
816  for (ip = (int *)src, uip = dest; count < len; count++)
817  {
818  if (*ip > X_UINT_MAX || *ip < 0)
819  (*range_error)++;
820  *uip++ = *ip++;
821  }
822  break;
823  case NC_INT64:
824  for (ip = (int *)src, lip = dest; count < len; count++)
825  *lip++ = *ip++;
826  break;
827  case NC_UINT64:
828  for (ip = (int *)src, ulip = dest; count < len; count++)
829  {
830  if (*ip < 0)
831  (*range_error)++;
832  *ulip++ = *ip++;
833  }
834  break;
835  case NC_FLOAT:
836  for (ip = (int *)src, fp = dest; count < len; count++)
837  *fp++ = *ip++;
838  break;
839  case NC_DOUBLE:
840  for (ip = (int *)src, dp = dest; count < len; count++)
841  *dp++ = *ip++;
842  break;
843  default:
844  LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
845  __func__, src_type, dest_type));
846  return NC_EBADTYPE;
847  }
848  break;
849 
850  case NC_UINT:
851  switch (dest_type)
852  {
853  case NC_UBYTE:
854  for (uip = (unsigned int *)src, ubp = dest; count < len; count++)
855  {
856  if (*uip > X_UCHAR_MAX)
857  (*range_error)++;
858  *ubp++ = *uip++;
859  }
860  break;
861  case NC_BYTE:
862  for (uip = (unsigned int *)src, bp = dest; count < len; count++)
863  {
864  if (*uip > X_SCHAR_MAX)
865  (*range_error)++;
866  *bp++ = *uip++;
867  }
868  break;
869  case NC_SHORT:
870  for (uip = (unsigned int *)src, sp = dest; count < len; count++)
871  {
872  if (*uip > X_SHORT_MAX)
873  (*range_error)++;
874  *sp++ = *uip++;
875  }
876  break;
877  case NC_USHORT:
878  for (uip = (unsigned int *)src, usp = dest; count < len; count++)
879  {
880  if (*uip > X_USHORT_MAX)
881  (*range_error)++;
882  *usp++ = *uip++;
883  }
884  break;
885  case NC_INT:
886  for (uip = (unsigned int *)src, ip = dest; count < len; count++)
887  {
888  if (*uip > X_INT_MAX)
889  (*range_error)++;
890  *ip++ = *uip++;
891  }
892  break;
893  case NC_UINT:
894  for (uip = (unsigned int *)src, uip1 = dest; count < len; count++)
895  {
896  if (*uip > X_UINT_MAX)
897  (*range_error)++;
898  *uip1++ = *uip++;
899  }
900  break;
901  case NC_INT64:
902  for (uip = (unsigned int *)src, lip = dest; count < len; count++)
903  *lip++ = *uip++;
904  break;
905  case NC_UINT64:
906  for (uip = (unsigned int *)src, ulip = dest; count < len; count++)
907  *ulip++ = *uip++;
908  break;
909  case NC_FLOAT:
910  for (uip = (unsigned int *)src, fp = dest; count < len; count++)
911  *fp++ = *uip++;
912  break;
913  case NC_DOUBLE:
914  for (uip = (unsigned int *)src, dp = dest; count < len; count++)
915  *dp++ = *uip++;
916  break;
917  default:
918  LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
919  __func__, src_type, dest_type));
920  return NC_EBADTYPE;
921  }
922  break;
923 
924  case NC_INT64:
925  switch (dest_type)
926  {
927  case NC_UBYTE:
928  for (lip = (long long *)src, ubp = dest; count < len; count++)
929  {
930  if (*lip > X_UCHAR_MAX || *lip < 0)
931  (*range_error)++;
932  *ubp++ = *lip++;
933  }
934  break;
935  case NC_BYTE:
936  for (lip = (long long *)src, bp = dest; count < len; count++)
937  {
938  if (*lip > X_SCHAR_MAX || *lip < X_SCHAR_MIN)
939  (*range_error)++;
940  *bp++ = *lip++;
941  }
942  break;
943  case NC_SHORT:
944  for (lip = (long long *)src, sp = dest; count < len; count++)
945  {
946  if (*lip > X_SHORT_MAX || *lip < X_SHORT_MIN)
947  (*range_error)++;
948  *sp++ = *lip++;
949  }
950  break;
951  case NC_USHORT:
952  for (lip = (long long *)src, usp = dest; count < len; count++)
953  {
954  if (*lip > X_USHORT_MAX || *lip < 0)
955  (*range_error)++;
956  *usp++ = *lip++;
957  }
958  break;
959  case NC_UINT:
960  for (lip = (long long *)src, uip = dest; count < len; count++)
961  {
962  if (*lip > X_UINT_MAX || *lip < 0)
963  (*range_error)++;
964  *uip++ = *lip++;
965  }
966  break;
967  case NC_INT:
968  for (lip = (long long *)src, ip = dest; count < len; count++)
969  {
970  if (*lip > X_INT_MAX || *lip < X_INT_MIN)
971  (*range_error)++;
972  *ip++ = *lip++;
973  }
974  break;
975  case NC_INT64:
976  for (lip = (long long *)src, lip1 = dest; count < len; count++)
977  *lip1++ = *lip++;
978  break;
979  case NC_UINT64:
980  for (lip = (long long *)src, ulip = dest; count < len; count++)
981  {
982  if (*lip < 0)
983  (*range_error)++;
984  *ulip++ = *lip++;
985  }
986  break;
987  case NC_FLOAT:
988  for (lip = (long long *)src, fp = dest; count < len; count++)
989  *fp++ = *lip++;
990  break;
991  case NC_DOUBLE:
992  for (lip = (long long *)src, dp = dest; count < len; count++)
993  *dp++ = *lip++;
994  break;
995  default:
996  LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
997  __func__, src_type, dest_type));
998  return NC_EBADTYPE;
999  }
1000  break;
1001 
1002  case NC_UINT64:
1003  switch (dest_type)
1004  {
1005  case NC_UBYTE:
1006  for (ulip = (unsigned long long *)src, ubp = dest; count < len; count++)
1007  {
1008  if (*ulip > X_UCHAR_MAX)
1009  (*range_error)++;
1010  *ubp++ = *ulip++;
1011  }
1012  break;
1013  case NC_BYTE:
1014  for (ulip = (unsigned long long *)src, bp = dest; count < len; count++)
1015  {
1016  if (*ulip > X_SCHAR_MAX)
1017  (*range_error)++;
1018  *bp++ = *ulip++;
1019  }
1020  break;
1021  case NC_SHORT:
1022  for (ulip = (unsigned long long *)src, sp = dest; count < len; count++)
1023  {
1024  if (*ulip > X_SHORT_MAX)
1025  (*range_error)++;
1026  *sp++ = *ulip++;
1027  }
1028  break;
1029  case NC_USHORT:
1030  for (ulip = (unsigned long long *)src, usp = dest; count < len; count++)
1031  {
1032  if (*ulip > X_USHORT_MAX)
1033  (*range_error)++;
1034  *usp++ = *ulip++;
1035  }
1036  break;
1037  case NC_UINT:
1038  for (ulip = (unsigned long long *)src, uip = dest; count < len; count++)
1039  {
1040  if (*ulip > X_UINT_MAX)
1041  (*range_error)++;
1042  *uip++ = *ulip++;
1043  }
1044  break;
1045  case NC_INT:
1046  for (ulip = (unsigned long long *)src, ip = dest; count < len; count++)
1047  {
1048  if (*ulip > X_INT_MAX)
1049  (*range_error)++;
1050  *ip++ = *ulip++;
1051  }
1052  break;
1053  case NC_INT64:
1054  for (ulip = (unsigned long long *)src, lip = dest; count < len; count++)
1055  {
1056  if (*ulip > X_INT64_MAX)
1057  (*range_error)++;
1058  *lip++ = *ulip++;
1059  }
1060  break;
1061  case NC_UINT64:
1062  for (ulip = (unsigned long long *)src, ulip1 = dest; count < len; count++)
1063  *ulip1++ = *ulip++;
1064  break;
1065  case NC_FLOAT:
1066  for (ulip = (unsigned long long *)src, fp = dest; count < len; count++)
1067  *fp++ = *ulip++;
1068  break;
1069  case NC_DOUBLE:
1070  for (ulip = (unsigned long long *)src, dp = dest; count < len; count++)
1071  *dp++ = *ulip++;
1072  break;
1073  default:
1074  LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
1075  __func__, src_type, dest_type));
1076  return NC_EBADTYPE;
1077  }
1078  break;
1079 
1080  case NC_FLOAT:
1081  switch (dest_type)
1082  {
1083  case NC_UBYTE:
1084  for (fp = (float *)src, ubp = dest; count < len; count++)
1085  {
1086  if (*fp > X_UCHAR_MAX || *fp < 0)
1087  (*range_error)++;
1088  *ubp++ = *fp++;
1089  }
1090  break;
1091  case NC_BYTE:
1092  for (fp = (float *)src, bp = dest; count < len; count++)
1093  {
1094  if (*fp > (double)X_SCHAR_MAX || *fp < (double)X_SCHAR_MIN)
1095  (*range_error)++;
1096  *bp++ = *fp++;
1097  }
1098  break;
1099  case NC_SHORT:
1100  for (fp = (float *)src, sp = dest; count < len; count++)
1101  {
1102  if (*fp > (double)X_SHORT_MAX || *fp < (double)X_SHORT_MIN)
1103  (*range_error)++;
1104  *sp++ = *fp++;
1105  }
1106  break;
1107  case NC_USHORT:
1108  for (fp = (float *)src, usp = dest; count < len; count++)
1109  {
1110  if (*fp > X_USHORT_MAX || *fp < 0)
1111  (*range_error)++;
1112  *usp++ = *fp++;
1113  }
1114  break;
1115  case NC_UINT:
1116  for (fp = (float *)src, uip = dest; count < len; count++)
1117  {
1118  if (*fp > X_UINT_MAX || *fp < 0)
1119  (*range_error)++;
1120  *uip++ = *fp++;
1121  }
1122  break;
1123  case NC_INT:
1124  for (fp = (float *)src, ip = dest; count < len; count++)
1125  {
1126  if (*fp > (double)X_INT_MAX || *fp < (double)X_INT_MIN)
1127  (*range_error)++;
1128  *ip++ = *fp++;
1129  }
1130  break;
1131  case NC_INT64:
1132  for (fp = (float *)src, lip = dest; count < len; count++)
1133  {
1134  if (*fp > X_INT64_MAX || *fp <X_INT64_MIN)
1135  (*range_error)++;
1136  *lip++ = *fp++;
1137  }
1138  break;
1139  case NC_UINT64:
1140  for (fp = (float *)src, lip = dest; count < len; count++)
1141  {
1142  if (*fp > X_UINT64_MAX || *fp < 0)
1143  (*range_error)++;
1144  *lip++ = *fp++;
1145  }
1146  break;
1147  case NC_FLOAT:
1148  for (fp = (float *)src, fp1 = dest; count < len; count++)
1149  {
1150  /* if (*fp > X_FLOAT_MAX || *fp < X_FLOAT_MIN)
1151  (*range_error)++;*/
1152  *fp1++ = *fp++;
1153  }
1154  break;
1155  case NC_DOUBLE:
1156  for (fp = (float *)src, dp = dest; count < len; count++)
1157  *dp++ = *fp++;
1158  break;
1159  default:
1160  LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
1161  __func__, src_type, dest_type));
1162  return NC_EBADTYPE;
1163  }
1164  break;
1165 
1166  case NC_DOUBLE:
1167  switch (dest_type)
1168  {
1169  case NC_UBYTE:
1170  for (dp = (double *)src, ubp = dest; count < len; count++)
1171  {
1172  if (*dp > X_UCHAR_MAX || *dp < 0)
1173  (*range_error)++;
1174  *ubp++ = *dp++;
1175  }
1176  break;
1177  case NC_BYTE:
1178  for (dp = (double *)src, bp = dest; count < len; count++)
1179  {
1180  if (*dp > X_SCHAR_MAX || *dp < X_SCHAR_MIN)
1181  (*range_error)++;
1182  *bp++ = *dp++;
1183  }
1184  break;
1185  case NC_SHORT:
1186  for (dp = (double *)src, sp = dest; count < len; count++)
1187  {
1188  if (*dp > X_SHORT_MAX || *dp < X_SHORT_MIN)
1189  (*range_error)++;
1190  *sp++ = *dp++;
1191  }
1192  break;
1193  case NC_USHORT:
1194  for (dp = (double *)src, usp = dest; count < len; count++)
1195  {
1196  if (*dp > X_USHORT_MAX || *dp < 0)
1197  (*range_error)++;
1198  *usp++ = *dp++;
1199  }
1200  break;
1201  case NC_UINT:
1202  for (dp = (double *)src, uip = dest; count < len; count++)
1203  {
1204  if (*dp > X_UINT_MAX || *dp < 0)
1205  (*range_error)++;
1206  *uip++ = *dp++;
1207  }
1208  break;
1209  case NC_INT:
1210  for (dp = (double *)src, ip = dest; count < len; count++)
1211  {
1212  if (*dp > X_INT_MAX || *dp < X_INT_MIN)
1213  (*range_error)++;
1214  *ip++ = *dp++;
1215  }
1216  break;
1217  case NC_INT64:
1218  for (dp = (double *)src, lip = dest; count < len; count++)
1219  {
1220  if (*dp > X_INT64_MAX || *dp < X_INT64_MIN)
1221  (*range_error)++;
1222  *lip++ = *dp++;
1223  }
1224  break;
1225  case NC_UINT64:
1226  for (dp = (double *)src, lip = dest; count < len; count++)
1227  {
1228  if (*dp > X_UINT64_MAX || *dp < 0)
1229  (*range_error)++;
1230  *lip++ = *dp++;
1231  }
1232  break;
1233  case NC_FLOAT:
1234  for (dp = (double *)src, fp = dest; count < len; count++)
1235  {
1236  if (isgreater(*dp, X_FLOAT_MAX) || isless(*dp, X_FLOAT_MIN))
1237  (*range_error)++;
1238  *fp++ = *dp++;
1239  }
1240  break;
1241  case NC_DOUBLE:
1242  for (dp = (double *)src, dp1 = dest; count < len; count++)
1243  {
1244  /* if (*dp > X_DOUBLE_MAX || *dp < X_DOUBLE_MIN) */
1245  /* (*range_error)++; */
1246  *dp1++ = *dp++;
1247  }
1248  break;
1249  default:
1250  LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
1251  __func__, src_type, dest_type));
1252  return NC_EBADTYPE;
1253  }
1254  break;
1255 
1256  default:
1257  LOG((0, "%s: unexpected src type. src_type %d, dest_type %d",
1258  __func__, src_type, dest_type));
1259  return NC_EBADTYPE;
1260  }
1261  return NC_NOERR;
1262 }
1263 
1275 int
1276 nc4_get_default_fill_value(const NC_TYPE_INFO_T *type_info, void *fill_value)
1277 {
1278  switch (type_info->hdr.id)
1279  {
1280  case NC_CHAR:
1281  *(char *)fill_value = NC_FILL_CHAR;
1282  break;
1283 
1284  case NC_STRING:
1285  *(char **)fill_value = strdup(NC_FILL_STRING);
1286  break;
1287 
1288  case NC_BYTE:
1289  *(signed char *)fill_value = NC_FILL_BYTE;
1290  break;
1291 
1292  case NC_SHORT:
1293  *(short *)fill_value = NC_FILL_SHORT;
1294  break;
1295 
1296  case NC_INT:
1297  *(int *)fill_value = NC_FILL_INT;
1298  break;
1299 
1300  case NC_UBYTE:
1301  *(unsigned char *)fill_value = NC_FILL_UBYTE;
1302  break;
1303 
1304  case NC_USHORT:
1305  *(unsigned short *)fill_value = NC_FILL_USHORT;
1306  break;
1307 
1308  case NC_UINT:
1309  *(unsigned int *)fill_value = NC_FILL_UINT;
1310  break;
1311 
1312  case NC_INT64:
1313  *(long long *)fill_value = NC_FILL_INT64;
1314  break;
1315 
1316  case NC_UINT64:
1317  *(unsigned long long *)fill_value = NC_FILL_UINT64;
1318  break;
1319 
1320  case NC_FLOAT:
1321  *(float *)fill_value = NC_FILL_FLOAT;
1322  break;
1323 
1324  case NC_DOUBLE:
1325  *(double *)fill_value = NC_FILL_DOUBLE;
1326  break;
1327 
1328  default:
1329  return NC_EINVAL;
1330  }
1331 
1332  return NC_NOERR;
1333 }
1334 
1347 int
1348 nc4_get_typelen_mem(NC_FILE_INFO_T *h5, nc_type xtype, size_t *len)
1349 {
1350  NC_TYPE_INFO_T *type;
1351  int retval;
1352 
1353  LOG((4, "%s xtype: %d", __func__, xtype));
1354  assert(len);
1355 
1356  /* If this is an atomic type, the answer is easy. */
1357  switch (xtype)
1358  {
1359  case NC_BYTE:
1360  case NC_CHAR:
1361  case NC_UBYTE:
1362  *len = sizeof(char);
1363  return NC_NOERR;
1364  case NC_SHORT:
1365  case NC_USHORT:
1366  *len = sizeof(short);
1367  return NC_NOERR;
1368  case NC_INT:
1369  case NC_UINT:
1370  *len = sizeof(int);
1371  return NC_NOERR;
1372  case NC_FLOAT:
1373  *len = sizeof(float);
1374  return NC_NOERR;
1375  case NC_DOUBLE:
1376  *len = sizeof(double);
1377  return NC_NOERR;
1378  case NC_INT64:
1379  case NC_UINT64:
1380  *len = sizeof(long long);
1381  return NC_NOERR;
1382  case NC_STRING:
1383  *len = sizeof(char *);
1384  return NC_NOERR;
1385  }
1386 
1387  /* See if var is compound type. */
1388  if ((retval = nc4_find_type(h5, xtype, &type)))
1389  return retval;
1390 
1391  if (!type)
1392  return NC_EBADTYPE;
1393 
1394  *len = type->size;
1395 
1396  LOG((5, "type->size: %d", type->size));
1397 
1398  return NC_NOERR;
1399 }
NC_USHORT
#define NC_USHORT
unsigned 2-byte int
Definition: netcdf.h:43
NC_NOERR
#define NC_NOERR
No Error.
Definition: netcdf.h:329
NC_EINVAL
#define NC_EINVAL
Invalid Argument.
Definition: netcdf.h:339
NC_FILL_DOUBLE
#define NC_FILL_DOUBLE
Default fill value.
Definition: netcdf.h:72
NC_FILL_USHORT
#define NC_FILL_USHORT
Default fill value.
Definition: netcdf.h:74
NC_FLOAT
#define NC_FLOAT
single precision floating point number
Definition: netcdf.h:40
NC_DOUBLE
#define NC_DOUBLE
double precision floating point number
Definition: netcdf.h:41
NC_FILL_FLOAT
#define NC_FILL_FLOAT
Default fill value.
Definition: netcdf.h:71
NC_FILL_BYTE
#define NC_FILL_BYTE
Default fill value.
Definition: netcdf.h:67
NC_BYTE
#define NC_BYTE
signed 1 byte integer
Definition: netcdf.h:35
NC_FILL_CHAR
#define NC_FILL_CHAR
Default fill value.
Definition: netcdf.h:68
NC_EFILTER
#define NC_EFILTER
Filter operation failed.
Definition: netcdf.h:474
NC_CHUNKED
#define NC_CHUNKED
In HDF5 files you can set storage for each variable to be either contiguous or chunked,...
Definition: netcdf.h:297
NC_COLLECTIVE
#define NC_COLLECTIVE
Use with nc_var_par_access() to set parallel access to collective.
Definition: netcdf_par.h:28
NC_INT
#define NC_INT
signed 4 byte integer
Definition: netcdf.h:38
NC_MAX_NAME
#define NC_MAX_NAME
Maximum for classic library.
Definition: netcdf.h:275
NC_UBYTE
#define NC_UBYTE
unsigned 1 byte int
Definition: netcdf.h:42
NC_FORMATX_NC_HDF5
#define NC_FORMATX_NC_HDF5
netCDF-4 subset of HDF5
Definition: netcdf.h:211
NC_ENOPAR
#define NC_ENOPAR
Parallel operation on file opened for non-parallel access.
Definition: netcdf.h:455
NC_ERANGE
#define NC_ERANGE
Math result not representable.
Definition: netcdf.h:408
NC_GLOBAL
#define NC_GLOBAL
Attribute id to put/get a global attribute.
Definition: netcdf.h:248
NC_FILL_UINT
#define NC_FILL_UINT
Default fill value.
Definition: netcdf.h:75
NC_MAX_INT
#define NC_MAX_INT
Max or min values for a type.
Definition: netcdf.h:94
NC_FILL_INT64
#define NC_FILL_INT64
Default fill value.
Definition: netcdf.h:76
NC_FILL_SHORT
#define NC_FILL_SHORT
Default fill value.
Definition: netcdf.h:69
NC_FILL_INT
#define NC_FILL_INT
Default fill value.
Definition: netcdf.h:70
NC_SHORT
#define NC_SHORT
signed 2 byte integer
Definition: netcdf.h:37
nc_type
int nc_type
The nc_type type is just an int.
Definition: netcdf.h:25
NC_EBADTYPE
#define NC_EBADTYPE
Not a netcdf data type.
Definition: netcdf.h:371
NC_FILL_UBYTE
#define NC_FILL_UBYTE
Default fill value.
Definition: netcdf.h:73
NC_ENOMEM
#define NC_ENOMEM
Memory allocation (malloc) failure.
Definition: netcdf.h:409
NC_UINT64
#define NC_UINT64
unsigned 8-byte int
Definition: netcdf.h:46
NC_UINT
#define NC_UINT
unsigned 4-byte int
Definition: netcdf.h:44
NC_STRING
#define NC_STRING
string
Definition: netcdf.h:47
NC_FILL_STRING
#define NC_FILL_STRING
Default fill value.
Definition: netcdf.h:78
NC_INDEPENDENT
#define NC_INDEPENDENT
Use with nc_var_par_access() to set parallel access to independent.
Definition: netcdf_par.h:26
NC_CHAR
#define NC_CHAR
ISO/ASCII character.
Definition: netcdf.h:36
NC_FILL_UINT64
#define NC_FILL_UINT64
Default fill value.
Definition: netcdf.h:77
NC_ENOTVAR
#define NC_ENOTVAR
Variable not found.
Definition: netcdf.h:383
NC_INT64
#define NC_INT64
signed 8-byte int
Definition: netcdf.h:45

Return to the Main Unidata NetCDF page.
Generated on Tue Apr 21 2020 19:56:21 for NetCDF. NetCDF is a Unidata library.