16 #include "nc4internal.h"
17 #include "nc4dispatch.h"
19 #define NUM_ATOMIC_TYPES 13
22 char atomic_name[NUM_ATOMIC_TYPES][NC_MAX_NAME + 1] = {"none", "byte", "char",
23 "short",
"int",
"float",
26 "int64",
"uint64",
"string"};
30 #define NC_CHAR_LEN sizeof(char)
31 #define NC_STRING_LEN sizeof(char *)
33 #define NC_SHORT_LEN 2
35 #define NC_FLOAT_LEN 4
36 #define NC_DOUBLE_LEN 8
37 #define NC_INT64_LEN 8
55 NC4_inq_type_equal(
int ncid1,
nc_type typeid1,
int ncid2,
58 NC_GRP_INFO_T *grpone, *grptwo;
59 NC_TYPE_INFO_T *type1, *type2;
62 LOG((2,
"nc_inq_type_equal: ncid1 0x%x typeid1 %d ncid2 0x%x typeid2 %d",
63 ncid1, typeid1, ncid2, typeid2));
73 if ((typeid1 <= NC_STRING && typeid2 >
NC_STRING) ||
74 (typeid2 <= NC_STRING && typeid1 > NC_STRING))
76 if (equalp) *equalp = 0;
85 if (typeid1 == typeid2)
94 if ((retval = nc4_find_nc4_grp(ncid1, &grpone)))
96 if (!(type1 = nc4_rec_find_nc_type(grpone->nc4_info->root_grp,
99 if ((retval = nc4_find_nc4_grp(ncid2, &grptwo)))
101 if (!(type2 = nc4_rec_find_nc_type(grptwo->nc4_info->root_grp,
108 if ((retval = H5Tequal(type1->native_hdf_typeid, type2->native_hdf_typeid)) < 0)
110 *equalp = 1 ? retval : 0;
131 NC4_inq_typeid(
int ncid,
const char *name,
nc_type *typeidp)
134 NC_GRP_INFO_T *grptwo;
135 NC_HDF5_FILE_INFO_T *h5;
136 NC_TYPE_INFO_T *type = NULL;
142 if (!strcmp(name, atomic_name[i]))
150 if ((retval = nc4_find_grp_h5(ncid, &grp, &h5)))
157 if (name[0] !=
'/' && strstr(name,
"/"))
161 if (!(norm_name = (
char*)malloc(strlen(name) + 1)))
163 if ((retval = nc4_normalize_name(name, norm_name))) {
168 for (grptwo = grp; grptwo; grptwo = grptwo->parent)
169 for (type = grptwo->type; type; type = type->l.next)
170 if (!strcmp(norm_name, type->name))
173 *typeidp = type->nc_typeid;
180 if ((type = nc4_rec_find_named_type(grp->nc4_info->root_grp, norm_name)))
182 *typeidp = type->nc_typeid;
207 NC4_inq_typeids(
int ncid,
int *ntypes,
int *typeids)
210 NC_HDF5_FILE_INFO_T *h5;
211 NC_TYPE_INFO_T *type;
215 LOG((2,
"nc_inq_typeids: ncid 0x%x", ncid));
218 if ((retval = nc4_find_grp_h5(ncid, &grp, &h5)))
224 for (type = grp->type; type; type = type->l.next)
227 typeids[num] = type->nc_typeid;
258 add_user_type(
int ncid,
size_t size,
const char *name,
nc_type base_typeid,
261 NC_HDF5_FILE_INFO_T *h5;
263 NC_TYPE_INFO_T *type;
268 if ((retval = nc4_check_name(name, norm_name)))
271 LOG((2,
"%s: ncid 0x%x size %d name %s base_typeid %d ",
272 __FUNCTION__, ncid, size, norm_name, base_typeid));
275 if ((retval = nc4_find_grp_h5(ncid, &grp, &h5)))
280 if (!(h5->cmode & NC_INDEF))
281 if ((retval = NC4_redef(ncid)))
287 if ((retval = nc4_get_typelen_mem(grp->nc4_info, base_typeid, 0,
295 if ((retval = nc4_check_dup_name(grp, norm_name)))
299 if ((retval = nc4_type_list_add(grp, size, norm_name, &type)))
303 type->nc_type_class = type_class;
305 type->u.v.base_nc_typeid = base_typeid;
306 else if (type_class ==
NC_ENUM)
307 type->u.e.base_nc_typeid = base_typeid;
311 *typeidp = type->nc_typeid;
331 NC4_inq_type(
int ncid,
nc_type typeid1,
char *name,
size_t *size)
334 NC_TYPE_INFO_T *type;
335 int atomic_size[
NUM_ATOMIC_TYPES] = {0, NC_BYTE_LEN, NC_CHAR_LEN, NC_SHORT_LEN,
336 NC_INT_LEN, NC_FLOAT_LEN, NC_DOUBLE_LEN,
337 NC_BYTE_LEN, NC_SHORT_LEN, NC_INT_LEN, NC_INT64_LEN,
338 NC_INT64_LEN, NC_STRING_LEN};
342 LOG((2,
"nc_inq_type: ncid 0x%x typeid %d", ncid, typeid1));
345 if (typeid1 < NUM_ATOMIC_TYPES)
348 strcpy(name, atomic_name[typeid1]);
350 *size = atomic_size[typeid1];
355 if ((retval = nc4_find_nc4_grp(ncid, &grp)))
359 if (!(type = nc4_rec_find_nc_type(grp->nc4_info->root_grp, typeid1)))
363 strcpy(name, type->name);
367 if (type->nc_type_class ==
NC_VLEN)
369 else if (type->nc_type_class == NC_STRING)
393 NC4_def_compound(
int ncid,
size_t size,
const char *name,
nc_type *typeidp)
395 return add_user_type(ncid, size, name, 0,
NC_COMPOUND, typeidp);
414 NC4_insert_compound(
int ncid,
nc_type typeid1,
const char *name,
size_t offset,
418 field_typeid, 0, NULL);
439 NC4_insert_array_compound(
int ncid,
int typeid1,
const char *name,
440 size_t offset,
nc_type field_typeid,
441 int ndims,
const int *dim_sizesp)
444 NC_TYPE_INFO_T *type;
448 LOG((2,
"nc_insert_array_compound: ncid 0x%x, typeid %d name %s "
449 "offset %d field_typeid %d ndims %d", ncid, typeid1,
450 name, offset, field_typeid, ndims));
453 if ((retval = nc4_check_name(name, norm_name)))
457 if ((retval = nc4_find_nc4_grp(ncid, &grp)))
461 if ((retval = nc4_find_type(grp->nc4_info, typeid1, &type)))
474 if ((retval = nc4_field_list_add(&type->u.c.field, type->u.c.num_fields,
475 norm_name, offset, 0, 0, field_typeid,
478 type->u.c.num_fields++;
500 NC4_inq_user_type(
int ncid,
nc_type typeid1,
char *name,
size_t *size,
501 nc_type *base_nc_typep,
size_t *nfieldsp,
int *classp)
504 NC_TYPE_INFO_T *type;
507 LOG((2,
"nc_inq_user_type: ncid 0x%x typeid %d", ncid, typeid1));
510 if ((retval = nc4_find_nc4_grp(ncid, &grp)))
514 if (!(type = nc4_rec_find_nc_type(grp->nc4_info->root_grp, typeid1)))
521 *nfieldsp = type->u.c.num_fields;
522 else if (type->nc_type_class ==
NC_ENUM)
523 *nfieldsp = type->u.e.num_members;
531 if (type->nc_type_class ==
NC_VLEN)
533 else if (type->nc_type_class == NC_STRING)
539 strcpy(name, type->name);
545 if (type->nc_type_class ==
NC_ENUM)
546 *base_nc_typep = type->u.e.base_nc_typeid;
547 else if (type->nc_type_class ==
NC_VLEN)
548 *base_nc_typep = type->u.v.base_nc_typeid;
556 *classp = type->nc_type_class;
579 NC4_inq_compound_field(
int ncid,
nc_type typeid1,
int fieldid,
char *name,
580 size_t *offsetp,
nc_type *field_typeidp,
int *ndimsp,
584 NC_TYPE_INFO_T *type;
585 NC_FIELD_INFO_T *field;
589 if ((retval = nc4_find_nc4_grp(ncid, &grp)))
593 if (!(type = nc4_rec_find_nc_type(grp->nc4_info->root_grp, typeid1)))
597 for (field = type->u.c.field; field; field = field->l.next)
598 if (field->fieldid == fieldid)
601 strcpy(name, field->name);
603 *offsetp = field->offset;
605 *field_typeidp = field->nc_typeid;
607 *ndimsp = field->ndims;
609 for (d = 0; d < field->ndims; d++)
610 dim_sizesp[d] = field->dim_size[d];
630 find_nc4_file(
int ncid, NC **nc)
632 NC_HDF5_FILE_INFO_T* h5;
635 if (!((*nc) = nc4_find_nc_file(ncid, &h5)))
660 NC4_inq_compound_fieldindex(
int ncid,
nc_type typeid1,
const char *name,
int *fieldidp)
663 NC_TYPE_INFO_T *type;
664 NC_FIELD_INFO_T *field;
668 LOG((2,
"nc_inq_compound_fieldindex: ncid 0x%x typeid %d name %s",
669 ncid, typeid1, name));
672 if ((retval = find_nc4_file(ncid, &nc)))
676 if ((retval = nc4_find_type(NC4_DATA(nc), typeid1, &type)))
684 if ((retval = nc4_normalize_name(name, norm_name)))
688 for (field = type->u.c.field; field; field = field->l.next)
689 if (!strcmp(field->name, norm_name))
696 *fieldidp = field->fieldid;
718 NC4_def_opaque(
int ncid,
size_t datum_size,
const char *name,
721 return add_user_type(ncid, datum_size, name, 0,
NC_OPAQUE, typeidp);
740 NC4_def_vlen(
int ncid,
const char *name,
nc_type base_typeid,
743 return add_user_type(ncid, 0, name, base_typeid,
NC_VLEN, typeidp);
761 NC4_def_enum(
int ncid,
nc_type base_typeid,
const char *name,
764 return add_user_type(ncid, 0, name, base_typeid,
NC_ENUM, typeidp);
784 NC4_inq_enum_ident(
int ncid,
nc_type xtype,
long long value,
char *identifier)
787 NC_TYPE_INFO_T *type;
788 NC_ENUM_MEMBER_INFO_T *enum_member;
793 LOG((3,
"nc_inq_enum_ident: xtype %d value %d\n", xtype, value));
796 if ((retval = nc4_find_nc4_grp(ncid, &grp)))
800 if (!(type = nc4_rec_find_nc_type(grp->nc4_info->root_grp, xtype)))
804 if (type->nc_type_class !=
NC_ENUM)
808 enum_member = type->u.e.enum_member;
809 for (i = 0; i < type->u.e.num_members; i++)
811 switch (type->u.e.base_nc_typeid)
814 ll_val = *(
char *)enum_member->value;
817 ll_val = *(
unsigned char *)enum_member->value;
820 ll_val = *(
short *)enum_member->value;
823 ll_val = *(
unsigned short *)enum_member->value;
826 ll_val = *(
int *)enum_member->value;
829 ll_val = *(
unsigned int *)enum_member->value;
833 ll_val = *(
long long *)enum_member->value;
838 LOG((4,
"ll_val=%d", ll_val));
842 strcpy(identifier, enum_member->name);
846 enum_member = enum_member->l.next;
850 if (i == type->u.e.num_members)
873 NC4_inq_enum_member(
int ncid,
nc_type typeid1,
int idx,
char *identifier,
877 NC_TYPE_INFO_T *type;
878 NC_ENUM_MEMBER_INFO_T *enum_member;
882 LOG((2,
"nc_inq_enum_member: ncid 0x%x typeid %d", ncid, typeid1));
885 if ((retval = nc4_find_nc4_grp(ncid, &grp)))
889 if (!(type = nc4_rec_find_nc_type(grp->nc4_info->root_grp, typeid1)))
893 if (type->nc_type_class !=
NC_ENUM)
897 if (idx >= type->u.e.num_members)
901 enum_member = type->u.e.enum_member;
902 for (i = 0; i < idx; i++)
903 enum_member = enum_member->l.next;
907 strcpy(identifier, enum_member->name);
909 memcpy(value, enum_member->value, type->size);
931 NC4_insert_enum(
int ncid,
nc_type typeid1,
const char *identifier,
935 NC_TYPE_INFO_T *type;
939 LOG((2,
"nc_insert_enum: ncid 0x%x, typeid %d identifier %s value %d", ncid,
940 typeid1, identifier, value));
943 if ((retval = nc4_check_name(identifier, norm_name)))
947 if ((retval = nc4_find_nc4_grp(ncid, &grp)))
951 if ((retval = nc4_find_type(grp->nc4_info, typeid1, &type)))
955 if (!type || type->nc_type_class !=
NC_ENUM)
964 if ((retval = nc4_enum_member_add(&type->u.e.enum_member, type->size,
967 type->u.e.num_members++;
986 NC4_put_vlen_element(
int ncid,
int typeid1,
void *vlen_element,
987 size_t len,
const void *data)
991 tmp->
p = (
void *)data;
1009 NC4_get_vlen_element(
int ncid,
int typeid1,
const void *vlen_element,
1010 size_t *len,
void *data)
1016 memcpy(data, tmp->
p, tmp->
len * type_size);
#define NC_ENOMEM
Memory allocation (malloc) failure.
#define NC_UBYTE
unsigned 1 byte int
#define NC_CLASSIC_MODEL
Enforce classic model on netCDF-4.
#define NC_UINT
unsigned 4-byte int
#define NC_EHDFERR
Error at HDF5 layer.
#define NC_OPAQUE
opaque types
#define NC_INT64
signed 8-byte int
EXTERNL int nc_insert_array_compound(int ncid, nc_type xtype, const char *name, size_t offset, nc_type field_typeid, int ndims, const int *dim_sizes)
Insert a named array field into a compound type.
int nc_type
The nc_type type is just an int.
#define NC_BYTE
signed 1 byte integer
#define NC_EBADFIELD
Bad field ID.
size_t len
Length of VL data (in base type units)
#define NC_VLEN
vlen (variable-length) types
#define NC_ETYPDEFINED
Type has already been defined and may not be edited.
#define NUM_ATOMIC_TYPES
Number of netCDF atomic types.
#define NC_EBADTYPE
Not a netcdf data type.
#define NC_EINVAL
Invalid Argument.
#define NC_INT
signed 4 byte integer
#define NC_ESTRICTNC3
Attempting netcdf-4 operation on strict nc3 netcdf-4 file.
#define NC_MAX_NAME
Maximum for classic library.
void * p
Pointer to VL data.
#define NC_NAT
Not A Type.
#define NC_USHORT
unsigned 2-byte int
#define NC_EBADID
Not a netcdf id.
This is the type of arrays of vlens.
#define NC_SHORT
signed 2 byte integer
#define NC_NOERR
No Error.
#define NC_ENUM
enum types
#define NC_COMPOUND
compound types
#define NC_UINT64
unsigned 8-byte int