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)
67 size_t nelems1, nelems2;
71 size_t offset1, offset2;
79 &base1, &nelems1, &class1)))
82 &base2, &nelems2, &class2)))
86 if(size1 != size2 || class1 != class2 || strcmp(name1,name2) != 0)
93 if((ret = NC_compare_nc_types(ncid1, base1, ncid2,
103 if(base1 != base2 || nelems1 != nelems2)
return NC_NOERR;
105 if (!(value1 = malloc(size1)))
107 if (!(value2 = malloc(size2))) {
112 for(i = 0; i < nelems1; i++)
118 strcmp(name1, name2) != 0 || memcmp(value1, value2, size1) != 0)
129 if(nelems1 != nelems2)
133 for(i = 0; i < nelems1; i++)
137 &ftype1, &ndims1, dimsizes1)))
140 &ftype2, &ndims2, dimsizes2)))
144 for(j = 0; j < ndims1;j++)
145 if(dimsizes1[j] != dimsizes2[j])
149 if((ret = NC_compare_nc_types(ncid1, ftype1, ncid2, ftype2,
184NC_rec_find_nc_type(
int ncid1,
nc_type tid1,
int ncid2,
nc_type* tid2)
189 if((ret = searchgroup(ncid1,tid1,ncid2,tid2)))
198 ret = NC_rec_find_nc_type(ncid1, tid1, parent, tid2);
203 ret = searchgrouptree(ncid1,tid1,ncid2,tid2);
227NC_find_equal_type(
int ncid1,
nc_type xtype1,
int ncid2,
nc_type *xtype2)
236 if (xtype1 <= NC_MAX_ATOMIC_TYPE)
247 ret = NC_rec_find_nc_type(ncid1, xtype1 , ncid2, xtype2);
290 size_t *count = NULL, *start = NULL;
292 size_t *dimlen = NULL;
295 int src_format, dest_format;
301 if ((retval =
nc_inq_var(ncid_in, varid_in, name, &xtype,
302 &ndims, dimids_in, &natts)))
305 for(i = 0; i < ndims; i++) {
306 dimids_out[i] = dimids_in[i];
309 if ((retval =
nc_inq_dimid(ncid_out, dimname_in, &dimids_out[i])))
313 LOG((2,
"nc_copy_var: ncid_in 0x%x varid_in %d ncid_out 0x%x",
314 ncid_in, varid_in, ncid_out));
329 if ((retval =
nc_inq_type(ncid_in, xtype, type_name, &type_size)))
331 LOG((3,
"type %s has size %d", type_name, type_size));
337 if ((retval =
nc_def_var(ncid_out, name, xtype,
338 ndims, dimids_out, &varid_out)))
342 for (a=0; a<natts; a++)
346 if ((retval =
nc_copy_att(ncid_in, varid_in, att_name,
347 ncid_out, varid_out)))
358 real_ndims = ndims ? ndims : 1;
359 if (!(start = malloc((
size_t)real_ndims *
sizeof(
size_t))))
361 if (!(count = malloc((
size_t)real_ndims *
sizeof(
size_t))))
369 if (!(dimlen = malloc((
size_t)real_ndims *
sizeof(
size_t))))
377 for (d=0; d<ndims; d++)
379 if ((retval =
nc_inq_dimlen(ncid_in, dimids_in[d], &dimlen[d])))
381 LOG((4,
"nc_copy_var: there are %d data", dimlen[d]));
388 for (d=0; d<real_ndims; d++)
391 count[d] = d ? dimlen[d] : 1;
392 if (d) reclen *= dimlen[d];
400 if (!(data = malloc(reclen * type_size))) {
401 if(count) free(count);
402 if(dimlen) free(dimlen);
403 if(start) free(start);
408 for (start[0]=0; !retval && start[0]<(size_t)dimlen[0]; start[0]++)
413 retval = nc_get_vara_schar(ncid_in, varid_in, start, count,
414 (
signed char *)data);
416 retval = nc_put_vara_schar(ncid_out, varid_out, start, count,
417 (
const signed char *)data);
420 retval = nc_get_vara_text(ncid_in, varid_in, start, count,
423 retval = nc_put_vara_text(ncid_out, varid_out, start, count,
427 retval = nc_get_vara_short(ncid_in, varid_in, start, count,
430 retval = nc_put_vara_short(ncid_out, varid_out, start, count,
434 retval = nc_get_vara_int(ncid_in, varid_in, start, count,
437 retval = nc_put_vara_int(ncid_out, varid_out, start, count,
441 retval = nc_get_vara_float(ncid_in, varid_in, start, count,
444 retval = nc_put_vara_float(ncid_out, varid_out, start, count,
448 retval = nc_get_vara_double(ncid_in, varid_in, start, count,
451 retval = nc_put_vara_double(ncid_out, varid_out, start, count,
455 retval = nc_get_vara_uchar(ncid_in, varid_in, start, count,
456 (
unsigned char *)data);
458 retval = nc_put_vara_uchar(ncid_out, varid_out, start, count,
459 (
unsigned char *)data);
462 retval = nc_get_vara_ushort(ncid_in, varid_in, start, count,
463 (
unsigned short *)data);
465 retval = nc_put_vara_ushort(ncid_out, varid_out, start, count,
466 (
unsigned short *)data);
469 retval = nc_get_vara_uint(ncid_in, varid_in, start, count,
470 (
unsigned int *)data);
472 retval = nc_put_vara_uint(ncid_out, varid_out, start, count,
473 (
unsigned int *)data);
476 retval = nc_get_vara_longlong(ncid_in, varid_in, start, count,
479 retval = nc_put_vara_longlong(ncid_out, varid_out, start, count,
483 retval = nc_get_vara_ulonglong(ncid_in, varid_in, start, count,
484 (
unsigned long long *)data);
486 retval = nc_put_vara_ulonglong(ncid_out, varid_out, start, count,
487 (
unsigned long long *)data);
495 if (data) free(data);
496 if (dimlen) free(dimlen);
497 if (start) free(start);
498 if (count) free(count);
517 int ncid_out,
int varid_out)
524 LOG((2,
"nc_copy_att: ncid_in 0x%x varid_in %d name %s",
525 ncid_in, varid_in, name));
528 if ((res =
nc_inq_att(ncid_in, varid_in, name, &xtype, &len)))
537 if(xtype <= NC_MAX_ATOMIC_TYPE) {
539 if((res =
nc_inq_type(ncid_out,xtype_out,NULL,&size)))
return res;
544 if ((res = NC_find_equal_type(ncid_in, xtype, ncid_out, &xtype_out)))
548 if ((res =
nc_inq_user_type(ncid_in, xtype, NULL, &size, NULL, NULL, &
class)))
552 if((data = malloc(size * len))==NULL) {
return NC_ENOMEM;}
553 res =
nc_get_att(ncid_in, varid_in, name, data);
555 res =
nc_put_att(ncid_out, varid_out, name, xtype_out, len, data);
556 (void)nc_reclaim_data_all(ncid_out,xtype_out,data,len);
585 int ncid_out,
int varid_out)
587 int format, target_natts, target_attid;
596 if (ncid_in == ncid_out && varid_in == varid_out)
604 retval =
nc_inq_attid(ncid_out, varid_out, name, &target_attid);
608 return NC_copy_att(ncid_in, varid_in, name, ncid_out, varid_out);
618 if (target_attid == target_natts - 1)
619 return NC_copy_att(ncid_in, varid_in, name, ncid_out, varid_out);
623 for (a = 0; a < target_natts; a++)
625 if (a == target_attid)
627 if ((retval =
NC_copy_att(ncid_in, varid_in, name, ncid_out, varid_out)))
634 if ((retval =
NC_copy_att(ncid_out, varid_out, att_name,
635 ncid_out, varid_out)))
644 return NC_copy_att(ncid_in, varid_in, name, ncid_out, varid_out);
655searchgroup(
int ncid1,
int tid1,
int grp,
int* tid2)
668 if (!(ids = (
int *)malloc((
size_t)nids *
sizeof(
int))))
672 for(i = 0; i < nids; i++)
675 if ((ret = NC_compare_nc_types(ncid1, tid1, grp, ids[i], &equal)))
696searchgrouptree(
int ncid1,
int tid1,
int grp,
int* tid2)
701 NClist* queue = nclistnew();
706 nclistpush(queue,(
void*)
id);
707 while(nclistlength(queue) > 0) {
708 id = (uintptr_t)nclistremove(queue,0);
710 if((ret = searchgroup(ncid1,tid1,gid,tid2)))
717 if (!(ids = (
int *)malloc((
size_t)nids *
sizeof(
int))))
722 for(i=0;i<nids;i++) {
723 id = (uintptr_t)ids[i];
724 nclistpush(queue,(
void*)
id);
726 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.