11 #include "ncdispatch.h"
12 #include "nc_logging.h"
17 static int searchgroup(
int ncid1,
int tid1,
int grp,
int* tid2);
18 static int searchgrouptree(
int ncid1,
int tid1,
int grp,
int* tid2);
35 NC_compare_nc_types(
int ncid1,
int typeid1,
int ncid2,
int typeid2,
int *equalp)
48 if (typeid1 <= NC_MAX_ATOMIC_TYPE)
50 if (typeid2 != typeid1)
61 size_t nelems1, nelems2;
65 size_t offset1, offset2;
73 &base1, &nelems1, &class1)))
76 &base2, &nelems2, &class2)))
80 if(size1 != size2 || class1 != class2 || strcmp(name1,name2))
87 if((ret = NC_compare_nc_types(ncid1, base1, ncid2,
97 if(base1 != base2 || nelems1 != nelems2)
return NC_NOERR;
99 if (!(value1 = malloc(size1)))
101 if (!(value2 = malloc(size2))) {
106 for(i = 0; i < nelems1; i++)
112 strcmp(name1, name2) || memcmp(value1, value2, size1))
123 if(nelems1 != nelems2)
127 for(i = 0; i < nelems1; i++)
131 &ftype1, &ndims1, dimsizes1)))
134 &ftype2, &ndims2, dimsizes2)))
138 for(j = 0; j < ndims1;j++)
139 if(dimsizes1[j] != dimsizes2[j])
143 if((ret = NC_compare_nc_types(ncid1, ftype1, ncid2, ftype2,
178 NC_rec_find_nc_type(
int ncid1,
nc_type tid1,
int ncid2,
nc_type* tid2)
183 if((ret = searchgroup(ncid1,tid1,ncid2,tid2)))
192 ret = NC_rec_find_nc_type(ncid1, tid1, parent, tid2);
197 ret = searchgrouptree(ncid1,tid1,ncid2,tid2);
219 NC_find_equal_type(
int ncid1,
nc_type xtype1,
int ncid2,
nc_type *xtype2)
228 if (xtype1 <= NC_MAX_ATOMIC_TYPE)
238 ret = NC_rec_find_nc_type(ncid1, xtype1 , ncid2, xtype2);
282 size_t *count = NULL, *start = NULL;
284 size_t *dimlen = NULL;
287 int src_format, dest_format;
293 if ((retval =
nc_inq_var(ncid_in, varid_in, name, &xtype,
294 &ndims, dimids_in, &natts)))
297 for(i = 0; i < ndims; i++) {
298 dimids_out[i] = dimids_in[i];
301 if ((retval =
nc_inq_dimid(ncid_out, dimname_in, &dimids_out[i])))
305 LOG((2,
"nc_copy_var: ncid_in 0x%x varid_in %d ncid_out 0x%x",
306 ncid_in, varid_in, ncid_out));
321 if ((retval =
nc_inq_type(ncid_in, xtype, type_name, &type_size)))
323 LOG((3,
"type %s has size %d", type_name, type_size));
329 if ((retval =
nc_def_var(ncid_out, name, xtype,
330 ndims, dimids_out, &varid_out)))
334 for (a=0; a<natts; a++)
338 if ((retval =
nc_copy_att(ncid_in, varid_in, att_name,
339 ncid_out, varid_out)))
350 real_ndims = ndims ? ndims : 1;
351 if (!(start = malloc((
size_t)real_ndims *
sizeof(
size_t))))
353 if (!(count = malloc((
size_t)real_ndims *
sizeof(
size_t))))
361 if (!(dimlen = malloc((
size_t)real_ndims *
sizeof(
size_t))))
369 for (d=0; d<ndims; d++)
371 if ((retval =
nc_inq_dimlen(ncid_in, dimids_in[d], &dimlen[d])))
373 LOG((4,
"nc_copy_var: there are %d data", dimlen[d]));
380 for (d=0; d<real_ndims; d++)
383 count[d] = d ? dimlen[d] : 1;
384 if (d) reclen *= dimlen[d];
392 if (!(data = malloc(reclen * type_size))) {
393 if(count) free(count);
394 if(dimlen) free(dimlen);
395 if(start) free(start);
400 for (start[0]=0; !retval && start[0]<(size_t)dimlen[0]; start[0]++)
406 (
signed char *)data);
409 (
const signed char *)data);
448 (
unsigned char *)data);
451 (
unsigned char *)data);
455 (
unsigned short *)data);
458 (
unsigned short *)data);
462 (
unsigned int *)data);
465 (
unsigned int *)data);
476 (
unsigned long long *)data);
479 (
unsigned long long *)data);
487 if (data) free(data);
488 if (dimlen) free(dimlen);
489 if (start) free(start);
490 if (count) free(count);
509 int ncid_out,
int varid_out)
516 LOG((2,
"nc_copy_att: ncid_in 0x%x varid_in %d name %s",
517 ncid_in, varid_in, name));
520 if ((res =
nc_inq_att(ncid_in, varid_in, name, &xtype, &len)))
528 size_t size = NC_atomictypelen(xtype);
531 if (!(data = malloc(len * size)))
535 res =
nc_get_att(ncid_in, varid_in, name, data);
537 res =
nc_put_att(ncid_out, varid_out, name, xtype,
547 if (!(str_data = malloc(
sizeof(
char *) * len)))
552 (
const char **)str_data);
567 if ((res = NC_find_equal_type(ncid_in, xtype, ncid_out, &xtype_out)))
573 NULL, NULL, &
class)))
579 if (!(vldata = malloc(
sizeof(
nc_vlen_t) * len)))
581 if ((res =
nc_get_att(ncid_in, varid_in, name, vldata)))
583 if ((res =
nc_put_att(ncid_out, varid_out, name, xtype_out,
586 for (i = 0; i < len; i++)
593 if (!(data = malloc(size * len)))
595 res =
nc_get_att(ncid_in, varid_in, name, data);
597 res =
nc_put_att(ncid_out, varid_out, name, xtype_out, len, data);
629 int ncid_out,
int varid_out)
631 int format, target_natts, target_attid;
640 if (ncid_in == ncid_out && varid_in == varid_out)
648 retval =
nc_inq_attid(ncid_out, varid_out, name, &target_attid);
652 return NC_copy_att(ncid_in, varid_in, name, ncid_out, varid_out);
662 if (target_attid == target_natts - 1)
663 return NC_copy_att(ncid_in, varid_in, name, ncid_out, varid_out);
667 for (a = 0; a < target_natts; a++)
669 if (a == target_attid)
671 if ((retval =
NC_copy_att(ncid_in, varid_in, name, ncid_out, varid_out)))
678 if ((retval =
NC_copy_att(ncid_out, varid_out, att_name,
679 ncid_out, varid_out)))
688 return NC_copy_att(ncid_in, varid_in, name, ncid_out, varid_out);
699 searchgroup(
int ncid1,
int tid1,
int grp,
int* tid2)
712 if (!(ids = (
int *)malloc((
size_t)nids *
sizeof(
int))))
716 for(i = 0; i < nids; i++)
719 if ((ret = NC_compare_nc_types(ncid1, tid1, grp, ids[i], &equal)))
740 searchgrouptree(
int ncid1,
int tid1,
int grp,
int* tid2)
745 NClist* queue = nclistnew();
750 nclistpush(queue,(
void*)
id);
751 while(nclistlength(queue) > 0) {
752 id = (uintptr_t)nclistremove(queue,0);
754 if((ret = searchgroup(ncid1,tid1,gid,tid2)))
761 if (!(ids = (
int *)malloc((
size_t)nids *
sizeof(
int))))
766 for(i=0;i<nids;i++) {
768 nclistpush(queue,(
void*)
id);
770 free(ids); ids = NULL;