11#include "ncdispatch.h"
12#include "nc_logging.h"
15static int NC_find_equal_type(
int ncid1,
nc_type xtype1,
int ncid2,
nc_type *xtype2);
19static int searchgroup(
int ncid1,
int tid1,
int grp,
int* tid2);
20static int searchgrouptree(
int ncid1,
int tid1,
int grp,
int* tid2);
41NC_compare_nc_types(
int ncid1,
int typeid1,
int ncid2,
int typeid2,
int *equalp)
54 if (typeid1 <= NC_MAX_ATOMIC_TYPE)
56 if (typeid2 != typeid1)
68 size_t nelems1, nelems2;
72 size_t offset1, offset2;
80 &base1, &nelems1, &class1)))
83 &base2, &nelems2, &class2)))
87 if(size1 != size2 || class1 != class2 || strcmp(name1,name2) != 0)
94 if((ret = NC_compare_nc_types(ncid1, base1, ncid2,
104 if(base1 != base2 || nelems1 != nelems2)
return NC_NOERR;
106 if (!(value1 = malloc(size1)))
108 if (!(value2 = malloc(size2))) {
113 for(i = 0; i < nelems1; i++)
119 strcmp(name1, name2) != 0 || memcmp(value1, value2, size1) != 0)
130 if(nelems1 != nelems2)
134 for(i = 0; i < nelems1; i++)
138 &ftype1, &ndims1, dimsizes1)))
141 &ftype2, &ndims2, dimsizes2)))
145 for(j = 0; j < ndims1;j++)
146 if(dimsizes1[j] != dimsizes2[j])
150 if((ret = NC_compare_nc_types(ncid1, ftype1, ncid2, ftype2,
185NC_rec_find_nc_type(
int ncid1,
nc_type tid1,
int ncid2,
nc_type* tid2)
190 if((ret = searchgroup(ncid1,tid1,ncid2,tid2)))
199 ret = NC_rec_find_nc_type(ncid1, tid1, parent, tid2);
204 ret = searchgrouptree(ncid1,tid1,ncid2,tid2);
228NC_find_equal_type(
int ncid1,
nc_type xtype1,
int ncid2,
nc_type *xtype2)
237 if (xtype1 <= NC_MAX_ATOMIC_TYPE)
248 ret = NC_rec_find_nc_type(ncid1, xtype1 , ncid2, xtype2);
289 int natts, real_ndims;
293 size_t *count = NULL, *start = NULL;
295 size_t *dimlen = NULL;
298 int src_format, dest_format;
304 if ((retval =
nc_inq_var(ncid_in, varid_in, name, &xtype,
305 &ndims, dimids_in, &natts)))
308 for(i = 0; i < (size_t)ndims; i++) {
309 dimids_out[i] = dimids_in[i];
312 if ((retval =
nc_inq_dimid(ncid_out, dimname_in, &dimids_out[i])))
316 LOG((2,
"nc_copy_var: ncid_in 0x%x varid_in %d ncid_out 0x%x",
317 ncid_in, varid_in, ncid_out));
332 if ((retval =
nc_inq_type(ncid_in, xtype, type_name, &type_size)))
334 LOG((3,
"type %s has size %d", type_name, type_size));
340 if ((retval =
nc_def_var(ncid_out, name, xtype,
341 ndims, dimids_out, &varid_out)))
345 for (a=0; a<natts; a++)
349 if ((retval =
nc_copy_att(ncid_in, varid_in, att_name,
350 ncid_out, varid_out)))
361 real_ndims = ndims ? ndims : 1;
362 if (!(start = malloc((
size_t)real_ndims *
sizeof(
size_t))))
364 if (!(count = malloc((
size_t)real_ndims *
sizeof(
size_t))))
372 if (!(dimlen = malloc((
size_t)real_ndims *
sizeof(
size_t))))
380 for (d=0; d<ndims; d++)
382 if ((retval =
nc_inq_dimlen(ncid_in, dimids_in[d], &dimlen[d])))
384 LOG((4,
"nc_copy_var: there are %d data", dimlen[d]));
391 for (d=0; d<real_ndims; d++)
394 count[d] = d ? dimlen[d] : 1;
395 if (d) reclen *= dimlen[d];
403 if (!(data = malloc(reclen * type_size))) {
404 if(count) free(count);
405 if(dimlen) free(dimlen);
406 if(start) free(start);
411 for (start[0]=0; !retval && start[0]<(size_t)dimlen[0]; start[0]++)
416 retval = nc_get_vara_schar(ncid_in, varid_in, start, count,
417 (
signed char *)data);
419 retval = nc_put_vara_schar(ncid_out, varid_out, start, count,
420 (
const signed char *)data);
423 retval = nc_get_vara_text(ncid_in, varid_in, start, count,
426 retval = nc_put_vara_text(ncid_out, varid_out, start, count,
430 retval = nc_get_vara_short(ncid_in, varid_in, start, count,
433 retval = nc_put_vara_short(ncid_out, varid_out, start, count,
437 retval = nc_get_vara_int(ncid_in, varid_in, start, count,
440 retval = nc_put_vara_int(ncid_out, varid_out, start, count,
444 retval = nc_get_vara_float(ncid_in, varid_in, start, count,
447 retval = nc_put_vara_float(ncid_out, varid_out, start, count,
451 retval = nc_get_vara_double(ncid_in, varid_in, start, count,
454 retval = nc_put_vara_double(ncid_out, varid_out, start, count,
458 retval = nc_get_vara_uchar(ncid_in, varid_in, start, count,
459 (
unsigned char *)data);
461 retval = nc_put_vara_uchar(ncid_out, varid_out, start, count,
462 (
unsigned char *)data);
465 retval = nc_get_vara_ushort(ncid_in, varid_in, start, count,
466 (
unsigned short *)data);
468 retval = nc_put_vara_ushort(ncid_out, varid_out, start, count,
469 (
unsigned short *)data);
472 retval = nc_get_vara_uint(ncid_in, varid_in, start, count,
473 (
unsigned int *)data);
475 retval = nc_put_vara_uint(ncid_out, varid_out, start, count,
476 (
unsigned int *)data);
479 retval = nc_get_vara_longlong(ncid_in, varid_in, start, count,
482 retval = nc_put_vara_longlong(ncid_out, varid_out, start, count,
486 retval = nc_get_vara_ulonglong(ncid_in, varid_in, start, count,
487 (
unsigned long long *)data);
489 retval = nc_put_vara_ulonglong(ncid_out, varid_out, start, count,
490 (
unsigned long long *)data);
498 if (data) free(data);
499 if (dimlen) free(dimlen);
500 if (start) free(start);
501 if (count) free(count);
520 int ncid_out,
int varid_out)
527 LOG((2,
"nc_copy_att: ncid_in 0x%x varid_in %d name %s",
528 ncid_in, varid_in, name));
531 if ((res =
nc_inq_att(ncid_in, varid_in, name, &xtype, &len)))
540 if(xtype <= NC_MAX_ATOMIC_TYPE) {
542 if((res =
nc_inq_type(ncid_out,xtype_out,NULL,&size)))
return res;
547 if ((res = NC_find_equal_type(ncid_in, xtype, ncid_out, &xtype_out)))
551 if ((res =
nc_inq_user_type(ncid_in, xtype, NULL, &size, NULL, NULL, &
class)))
555 if((data = malloc(size * len))==NULL) {
return NC_ENOMEM;}
556 res =
nc_get_att(ncid_in, varid_in, name, data);
558 res =
nc_put_att(ncid_out, varid_out, name, xtype_out, len, data);
559 (void)nc_reclaim_data_all(ncid_out,xtype_out,data,len);
588 int ncid_out,
int varid_out)
590 int format, target_natts, target_attid;
599 if (ncid_in == ncid_out && varid_in == varid_out)
607 retval =
nc_inq_attid(ncid_out, varid_out, name, &target_attid);
611 return NC_copy_att(ncid_in, varid_in, name, ncid_out, varid_out);
621 if (target_attid == target_natts - 1)
622 return NC_copy_att(ncid_in, varid_in, name, ncid_out, varid_out);
626 for (a = 0; a < target_natts; a++)
628 if (a == target_attid)
630 if ((retval =
NC_copy_att(ncid_in, varid_in, name, ncid_out, varid_out)))
637 if ((retval =
NC_copy_att(ncid_out, varid_out, att_name,
638 ncid_out, varid_out)))
647 return NC_copy_att(ncid_in, varid_in, name, ncid_out, varid_out);
658searchgroup(
int ncid1,
int tid1,
int grp,
int* tid2)
671 if (!(ids = (
int *)malloc((
size_t)nids *
sizeof(
int))))
675 for(i = 0; i < nids; i++)
678 if ((ret = NC_compare_nc_types(ncid1, tid1, grp, ids[i], &equal)))
699searchgrouptree(
int ncid1,
int tid1,
int grp,
int* tid2)
704 NClist* queue = nclistnew();
709 nclistpush(queue,(
void*)
id);
710 while(nclistlength(queue) > 0) {
711 id = (uintptr_t)nclistremove(queue,0);
713 if((ret = searchgroup(ncid1,tid1,gid,tid2)))
720 if (!(ids = (
int *)malloc((
size_t)nids *
sizeof(
int))))
725 for(i=0;i<nids;i++) {
726 id = (uintptr_t)ids[i];
727 nclistpush(queue,(
void*)
id);
729 free(ids); ids = NULL;
int nc_copy_var(int ncid_in, int varid_in, int ncid_out)
This will copy a variable that is an array of primitive type and its attributes from one file to anot...
int nc_copy_att(int ncid_in, int varid_in, const char *name, int ncid_out, int varid_out)
Copy an attribute from one open file to another.
static int NC_copy_att(int ncid_in, int varid_in, const char *name, int ncid_out, int varid_out)
Copy an attribute from one open file to another.
EXTERNL int nc_put_att(int ncid, int varid, const char *name, nc_type xtype, size_t len, const void *op)
Write an attribute of any type.
EXTERNL int nc_get_att(int ncid, int varid, const char *name, void *ip)
Get an attribute of any type.
EXTERNL int nc_inq_attid(int ncid, int varid, const char *name, int *idp)
Find an attribute ID.
EXTERNL int nc_inq_att(int ncid, int varid, const char *name, nc_type *xtypep, size_t *lenp)
Return information about a netCDF attribute.
EXTERNL int nc_inq_attname(int ncid, int varid, int attnum, char *name)
Find the name of an attribute.
EXTERNL int nc_inq_type(int ncid, nc_type xtype, char *name, size_t *size)
Inquire about a type.
EXTERNL int nc_enddef(int ncid)
Leave define mode.
EXTERNL int nc_redef(int ncid)
Put open netcdf dataset into define mode.
EXTERNL int nc_inq_format(int ncid, int *formatp)
Inquire about the binary format of a netCDF file as presented by the API.
EXTERNL int nc_sync(int ncid)
Synchronize an open netcdf dataset to disk.
EXTERNL int nc_inq_dimlen(int ncid, int dimid, size_t *lenp)
Find the length of a dimension.
EXTERNL int nc_inq_dimname(int ncid, int dimid, char *name)
Find out the name of a dimension.
EXTERNL int nc_inq_dimid(int ncid, const char *name, int *idp)
Find the ID of a dimension from the name.
EXTERNL int nc_inq_typeids(int ncid, int *ntypes, int *typeids)
Retrieve a list of types associated with a group.
EXTERNL int nc_inq_grps(int ncid, int *numgrps, int *ncids)
Get a list of groups or subgroups from a file or groupID.
EXTERNL int nc_inq_grp_parent(int ncid, int *parent_ncid)
Get the ID of the parent based on a group ID.
EXTERNL int nc_inq_enum_member(int ncid, nc_type xtype, int idx, char *name, void *value)
Learn about a about a member of an enum type.
EXTERNL int nc_inq_compound_field(int ncid, nc_type xtype, int fieldid, char *name, size_t *offsetp, nc_type *field_typeidp, int *ndimsp, int *dim_sizesp)
Get information about one of the fields of a compound type.
EXTERNL int nc_inq_user_type(int ncid, nc_type xtype, char *name, size_t *size, nc_type *base_nc_typep, size_t *nfieldsp, int *classp)
Learn about a user defined type.
EXTERNL int nc_inq_varnatts(int ncid, int varid, int *nattsp)
Learn how many attributes are associated with a variable.
EXTERNL int nc_inq_var(int ncid, int varid, char *name, nc_type *xtypep, int *ndimsp, int *dimidsp, int *nattsp)
Learn about a variable.
EXTERNL int nc_def_var(int ncid, const char *name, nc_type xtype, int ndims, const int *dimidsp, int *varidp)
Define a new variable.
#define NC_FORMAT_64BIT_OFFSET
Format specifier for nc_set_default_format() and returned by nc_inq_format.
#define NC_FORMAT_NETCDF4_CLASSIC
Format specifier for nc_set_default_format() and returned by nc_inq_format.
#define NC_EBADTYPE
Not a netcdf data type.
#define NC_UINT
unsigned 4-byte int
#define NC_INT
signed 4 byte integer
#define NC_MAX_VAR_DIMS
max per variable dimensions
#define NC_BYTE
signed 1 byte integer
#define NC_VLEN
vlen (variable-length) types
#define NC_ENOGRP
No group found.
#define NC_NAT
Not A Type.
#define NC_DOUBLE
double precision floating point number
#define NC_UBYTE
unsigned 1 byte int
#define NC_FLOAT
single precision floating point number
#define NC_ENOTNC4
Attempting netcdf-4 operation on netcdf-3 file.
#define NC_ENOMEM
Memory allocation (malloc) failure.
#define NC_COMPOUND
compound types
#define NC_EINDEFINE
Operation not allowed in define mode.
#define NC_SHORT
signed 2 byte integer
#define NC_ENUM
enum types
#define NC_INT64
signed 8-byte int
#define NC_FORMAT_NETCDF4
Format specifier for nc_set_default_format() and returned by nc_inq_format.
#define NC_UINT64
unsigned 8-byte int
#define NC_ENOTATT
Attribute not found.
#define NC_FORMAT_CLASSIC
Format specifier for nc_set_default_format() and returned by nc_inq_format.
#define NC_EINVAL
Invalid Argument.
#define NC_MAX_NAME
Maximum for classic library.
#define NC_NOERR
No Error.
#define NC_USHORT
unsigned 2-byte int
#define NC_OPAQUE
opaque types
#define NC_CHAR
ISO/ASCII character.
#define NC_FORMAT_64BIT_DATA
Format specifier for nc_set_default_format() and returned by nc_inq_format.
int nc_type
The nc_type type is just an int.