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)))
529 size_t size = NC_atomictypelen(xtype);
532 if (!(data = malloc(len * size)))
536 res =
nc_get_att(ncid_in, varid_in, name, data);
538 res =
nc_put_att(ncid_out, varid_out, name, xtype,
548 if (!(str_data = malloc(
sizeof(
char *) * len)))
553 (
const char **)str_data);
568 if ((res = NC_find_equal_type(ncid_in, xtype, ncid_out, &xtype_out)))
574 NULL, NULL, &
class)))
580 if (!(vldata = malloc(
sizeof(
nc_vlen_t) * len)))
582 if ((res =
nc_get_att(ncid_in, varid_in, name, vldata)))
584 if ((res =
nc_put_att(ncid_out, varid_out, name, xtype_out,
587 for (i = 0; i < len; i++)
594 if (!(data = malloc(size * len)))
596 res =
nc_get_att(ncid_in, varid_in, name, data);
598 res =
nc_put_att(ncid_out, varid_out, name, xtype_out, len, data);
611 if(xtype <= NC_MAX_ATOMIC_TYPE) {
613 if((res =
nc_inq_type(ncid_out,xtype_out,NULL,&size)))
return res;
618 if ((res = NC_find_equal_type(ncid_in, xtype, ncid_out, &xtype_out)))
622 if ((res =
nc_inq_user_type(ncid_in, xtype, NULL, &size, NULL, NULL, &
class)))
626 if((data = malloc(size * len))==NULL) {
return NC_ENOMEM;}
627 res =
nc_get_att(ncid_in, varid_in, name, data);
629 res =
nc_put_att(ncid_out, varid_out, name, xtype_out, len, data);
630 (void)nc_reclaim_data_all(ncid_out,xtype_out,data,len);
660 int ncid_out,
int varid_out)
662 int format, target_natts, target_attid;
671 if (ncid_in == ncid_out && varid_in == varid_out)
679 retval =
nc_inq_attid(ncid_out, varid_out, name, &target_attid);
683 return NC_copy_att(ncid_in, varid_in, name, ncid_out, varid_out);
693 if (target_attid == target_natts - 1)
694 return NC_copy_att(ncid_in, varid_in, name, ncid_out, varid_out);
698 for (a = 0; a < target_natts; a++)
700 if (a == target_attid)
702 if ((retval =
NC_copy_att(ncid_in, varid_in, name, ncid_out, varid_out)))
709 if ((retval =
NC_copy_att(ncid_out, varid_out, att_name,
710 ncid_out, varid_out)))
719 return NC_copy_att(ncid_in, varid_in, name, ncid_out, varid_out);
730 searchgroup(
int ncid1,
int tid1,
int grp,
int* tid2)
743 if (!(ids = (
int *)malloc((
size_t)nids *
sizeof(
int))))
747 for(i = 0; i < nids; i++)
750 if ((ret = NC_compare_nc_types(ncid1, tid1, grp, ids[i], &equal)))
771 searchgrouptree(
int ncid1,
int tid1,
int grp,
int* tid2)
776 NClist* queue = nclistnew();
781 nclistpush(queue,(
void*)
id);
782 while(nclistlength(queue) > 0) {
783 id = (uintptr_t)nclistremove(queue,0);
785 if((ret = searchgroup(ncid1,tid1,gid,tid2)))
792 if (!(ids = (
int *)malloc((
size_t)nids *
sizeof(
int))))
797 for(i=0;i<nids;i++) {
799 nclistpush(queue,(
void*)
id);
801 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_get_att_string(int ncid, int varid, const char *name, char **ip)
Get an attribute array of type string.
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_put_att_string(int ncid, int varid, const char *name, size_t len, const char **op)
Write a string attribute.
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_free_vlen(nc_vlen_t *vl)
Free memory in a VLEN object.
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.
int nc_put_vara_schar(int ncid, int varid, const size_t *startp, const size_t *countp, const signed char *op)
Write an array of values to a variable.
int nc_get_vara_float(int ncid, int varid, const size_t *startp, const size_t *countp, float *ip)
Read an array of values from a variable.
EXTERNL int nc_free_string(size_t len, char **data)
Free string space allocated by the library.
int nc_put_vara_ulonglong(int ncid, int varid, const size_t *startp, const size_t *countp, const unsigned long long *op)
Write an array of values to a variable.
int nc_put_vara_float(int ncid, int varid, const size_t *startp, const size_t *countp, const float *op)
Write an array of values to a variable.
int nc_get_vara_text(int ncid, int varid, const size_t *startp, const size_t *countp, char *ip)
Read an array of values from a variable.
int nc_get_vara_double(int ncid, int varid, const size_t *startp, const size_t *countp, double *ip)
Read an array of values from a variable.
int nc_put_vara_int(int ncid, int varid, const size_t *startp, const size_t *countp, const int *op)
Write an array of values to a variable.
EXTERNL int nc_inq_varnatts(int ncid, int varid, int *nattsp)
Learn how many attributes are associated with a variable.
int nc_get_vara_uchar(int ncid, int varid, const size_t *startp, const size_t *countp, unsigned char *ip)
Read an array of values from a variable.
int nc_get_vara_ushort(int ncid, int varid, const size_t *startp, const size_t *countp, unsigned short *ip)
Read an array of values from a variable.
int nc_get_vara_schar(int ncid, int varid, const size_t *startp, const size_t *countp, signed char *ip)
Read an array of values from 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.
int nc_get_vara_int(int ncid, int varid, const size_t *startp, const size_t *countp, int *ip)
Read an array of values from a variable.
int nc_put_vara_ushort(int ncid, int varid, const size_t *startp, const size_t *countp, const unsigned short *op)
Write an array of values to a variable.
int nc_put_vara_longlong(int ncid, int varid, const size_t *startp, const size_t *countp, const long long *op)
Write an array of values to a variable.
int nc_get_vara_short(int ncid, int varid, const size_t *startp, const size_t *countp, short *ip)
Read an array of values from a variable.
int nc_put_vara_double(int ncid, int varid, const size_t *startp, const size_t *countp, const double *op)
Write an array of values to 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.
int nc_put_vara_text(int ncid, int varid, const size_t *startp, const size_t *countp, const char *op)
Write an array of values to a variable.
int nc_get_vara_uint(int ncid, int varid, const size_t *startp, const size_t *countp, unsigned int *ip)
Read an array of values from a variable.
int nc_put_vara_uint(int ncid, int varid, const size_t *startp, const size_t *countp, const unsigned int *op)
Write an array of values to a variable.
int nc_get_vara_longlong(int ncid, int varid, const size_t *startp, const size_t *countp, long long *ip)
Read an array of values from a variable.
int nc_put_vara_short(int ncid, int varid, const size_t *startp, const size_t *countp, const short *op)
Write an array of values to a variable.
int nc_get_vara_ulonglong(int ncid, int varid, const size_t *startp, const size_t *countp, unsigned long long *ip)
Read an array of values from a variable.
int nc_put_vara_uchar(int ncid, int varid, const size_t *startp, const size_t *countp, const unsigned char *op)
Write an array of values to a 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.
This is the type of arrays of vlens.