dcopy.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /* Copyright 2010 University Corporation for Atmospheric
  2. Research/Unidata. See COPYRIGHT file for more info.
  3. This file has the var and att copy functions.
  4. "$Id: copy.c,v 1.1 2010/06/01 15:46:49 ed Exp $"
  5. */
  6. #include "ncdispatch.h"
  7. #include <nc_logging.h>
  8. #ifdef USE_NETCDF4
  9. /* Compare two netcdf types for equality. Must have the ncids as well,
  10. to find user-defined types. */
  11. static int
  12. NC_compare_nc_types(int ncid1, int typeid1, int ncid2, int typeid2,
  13. int *equalp)
  14. {
  15. int ret = NC_NOERR;
  16. /* If you don't care about the answer, neither do I! */
  17. if(equalp == NULL)
  18. return NC_NOERR;
  19. /* Assume the types are not equal. If we find any inequality, then
  20. exit with NC_NOERR and we're done. */
  21. *equalp = 0;
  22. /* Atomic types are so easy! */
  23. if (typeid1 <= NC_MAX_ATOMIC_TYPE)
  24. {
  25. if (typeid2 != typeid1)
  26. return NC_NOERR;
  27. *equalp = 1;
  28. }
  29. else
  30. {
  31. int i, ret, equal1;
  32. char name1[NC_MAX_NAME];
  33. char name2[NC_MAX_NAME];
  34. size_t size1, size2;
  35. nc_type base1, base2;
  36. size_t nelems1, nelems2;
  37. int class1, class2;
  38. void* value1 = NULL;
  39. void* value2 = NULL;
  40. size_t offset1, offset2;
  41. nc_type ftype1, ftype2;
  42. int ndims1, ndims2;
  43. int dimsizes1[NC_MAX_VAR_DIMS];
  44. int dimsizes2[NC_MAX_VAR_DIMS];
  45. /* Find out about the two types. */
  46. if ((ret = nc_inq_user_type(ncid1, typeid1, name1, &size1,
  47. &base1, &nelems1, &class1)))
  48. return ret;
  49. if ((ret = nc_inq_user_type(ncid2, typeid2, name2, &size2,
  50. &base2, &nelems2, &class2)))
  51. return ret;
  52. /* Check the obvious. */
  53. if(size1 != size2 || class1 != class2 || strcmp(name1,name2))
  54. return NC_NOERR;
  55. /* Check user-defined types in detail. */
  56. switch(class1)
  57. {
  58. case NC_VLEN:
  59. if((ret = NC_compare_nc_types(ncid1, base1, ncid2,
  60. base1, &equal1)))
  61. return ret;
  62. if(!equal1)
  63. return NC_NOERR;
  64. break;
  65. case NC_OPAQUE:
  66. /* Already checked size above. */
  67. break;
  68. case NC_ENUM:
  69. if(base1 != base2 || nelems1 != nelems2) return NC_NOERR;
  70. if (!(value1 = malloc(size1)))
  71. return NC_ENOMEM;
  72. if (!(value2 = malloc(size2)))
  73. return NC_ENOMEM;
  74. for(i = 0; i < nelems1; i++)
  75. {
  76. if ((ret = nc_inq_enum_member(ncid1, typeid1, i, name1,
  77. value1)) ||
  78. (ret = nc_inq_enum_member(ncid2, typeid2, i, name2,
  79. value2)) ||
  80. strcmp(name1, name2) || memcmp(value1, value2, size1))
  81. {
  82. free(value1);
  83. free(value2);
  84. return ret;
  85. }
  86. }
  87. free(value1);
  88. free(value2);
  89. break;
  90. case NC_COMPOUND:
  91. if(nelems1 != nelems2)
  92. return NC_NOERR;
  93. /* Compare each field. Each must be equal! */
  94. for(i = 0; i < nelems1; i++)
  95. {
  96. int j;
  97. if ((ret = nc_inq_compound_field(ncid1, typeid1, i, name1, &offset1,
  98. &ftype1, &ndims1, dimsizes1)))
  99. return ret;
  100. if ((ret = nc_inq_compound_field(ncid2, typeid2, i, name2, &offset2,
  101. &ftype2, &ndims2, dimsizes2)))
  102. return ret;
  103. if(ndims1 != ndims2)
  104. return NC_NOERR;
  105. for(j = 0; j < ndims1;j++)
  106. if(dimsizes1[j] != dimsizes2[j])
  107. return NC_NOERR;
  108. /* Compare user-defined field types. */
  109. if((ret = NC_compare_nc_types(ncid1, ftype1, ncid2, ftype2,
  110. &equal1)))
  111. return ret;
  112. if(!equal1)
  113. return NC_NOERR;
  114. }
  115. break;
  116. default:
  117. return NC_EINVAL;
  118. }
  119. *equalp = 1;
  120. }
  121. return ret;
  122. }
  123. /* Recursively hunt for a netCDF type id. (Code from nc4internal.c);
  124. Return matching typeid or 0 if not found. */
  125. static int
  126. NC_rec_find_nc_type(int ncid1, nc_type tid1, int ncid2, nc_type* tid2)
  127. {
  128. int i,ret = NC_NOERR;
  129. int nids;
  130. int* ids = NULL;
  131. /* Get all types in grp ncid2 */
  132. if(tid2)
  133. *tid2 = 0;
  134. if ((ret = nc_inq_typeids(ncid2, &nids, NULL)))
  135. return ret;
  136. if (nids)
  137. {
  138. if (!(ids = (int *)malloc(nids * sizeof(int))))
  139. return NC_ENOMEM;
  140. if ((ret = nc_inq_typeids(ncid2, &nids, ids)))
  141. return ret;
  142. for(i = 0; i < nids; i++)
  143. {
  144. int equal = 0;
  145. if ((ret = NC_compare_nc_types(ncid1, tid1, ncid2, ids[i], &equal)))
  146. return ret;
  147. if(equal)
  148. {
  149. if(tid2)
  150. *tid2 = ids[i];
  151. free(ids);
  152. return NC_NOERR;
  153. }
  154. }
  155. free(ids);
  156. }
  157. /* recurse */
  158. if ((ret = nc_inq_grps(ncid1, &nids, NULL)))
  159. return ret;
  160. if (nids)
  161. {
  162. if (!(ids = (int *)malloc(nids * sizeof(int))))
  163. return NC_ENOMEM;
  164. if ((ret = nc_inq_grps(ncid1, &nids, ids)))
  165. {
  166. free(ids);
  167. return ret;
  168. }
  169. for (i = 0; i < nids; i++)
  170. {
  171. ret = NC_rec_find_nc_type(ncid1, tid1, ids[i], tid2);
  172. if (ret && ret != NC_EBADTYPE)
  173. break;
  174. if (tid2 && *tid2 != 0) /* found */
  175. {
  176. free(ids);
  177. return NC_NOERR;
  178. }
  179. }
  180. free(ids);
  181. }
  182. return NC_EBADTYPE; /* not found */
  183. }
  184. /* Given a type in one file, find its equal (if any) in another
  185. * file. It sounds so simple, but it's a real pain! */
  186. static int
  187. NC_find_equal_type(int ncid1, nc_type xtype1, int ncid2, nc_type *xtype2)
  188. {
  189. int ret = NC_NOERR;
  190. /* Check input */
  191. if(xtype1 <= NC_NAT)
  192. return NC_EINVAL;
  193. /* Handle atomic types. */
  194. if (xtype1 <= NC_MAX_ATOMIC_TYPE)
  195. {
  196. if(xtype2)
  197. *xtype2 = xtype1;
  198. return NC_NOERR;
  199. }
  200. /* Recursively search group ncid2 and its children
  201. to find a type that is equal (using compare_type)
  202. to xtype1. */
  203. ret = NC_rec_find_nc_type(ncid1, xtype1 , ncid2, xtype2);
  204. return ret;
  205. }
  206. #endif /* USE_NETCDF4 */
  207. /* This will copy a variable from one file to another, assuming
  208. dimensions in output file are already defined and have same
  209. dimension ids.
  210. This function must work even if the files are different formats,
  211. (i.e. one old netcdf, the other hdf5-netcdf.)
  212. But if you're copying into a netcdf-3 file, from a netcdf-4 file,
  213. you must be copying a var of one of the six netcdf-3
  214. types. Similarly for the attributes. */
  215. int
  216. nc_copy_var(int ncid_in, int varid_in, int ncid_out)
  217. {
  218. char name[NC_MAX_NAME + 1];
  219. char att_name[NC_MAX_NAME + 1];
  220. nc_type xtype;
  221. int ndims, dimids[NC_MAX_VAR_DIMS], natts, real_ndims;
  222. int varid_out;
  223. int a, d;
  224. void *data = NULL;
  225. size_t *count = NULL, *start = NULL;
  226. size_t reclen = 1;
  227. size_t *dimlen = NULL;
  228. int retval = NC_NOERR;
  229. size_t type_size;
  230. int src_format, dest_format;
  231. char type_name[NC_MAX_NAME+1];
  232. /* Learn about this var. */
  233. if ((retval = nc_inq_var(ncid_in, varid_in, name, &xtype,
  234. &ndims, dimids, &natts)))
  235. return retval;
  236. #ifdef USE_NETCDF4
  237. LOG((2, "nc_copy_var: ncid_in 0x%x varid_in %d ncid_out 0x%x",
  238. ncid_in, varid_in, ncid_out));
  239. #endif
  240. /* Make sure we are not trying to write into a netcdf-3 file
  241. * anything that won't fit in netcdf-3. */
  242. if ((retval = nc_inq_format(ncid_in, &src_format)))
  243. return retval;
  244. if ((retval = nc_inq_format(ncid_out, &dest_format)))
  245. return retval;
  246. if ((dest_format == NC_FORMAT_CLASSIC || dest_format == NC_FORMAT_64BIT) &&
  247. src_format == NC_FORMAT_NETCDF4 && xtype > NC_DOUBLE)
  248. return NC_ENOTNC4;
  249. /* Later on, we will need to know the size of this type. */
  250. if ((retval = nc_inq_type(ncid_in, xtype, type_name, &type_size)))
  251. return retval;
  252. #ifdef USE_NETCDF4
  253. LOG((3, "type %s has size %d", type_name, type_size));
  254. #endif
  255. /* Switch back to define mode, and create the output var. */
  256. retval = nc_redef(ncid_out);
  257. if (retval && retval != NC_EINDEFINE)
  258. BAIL(retval);
  259. if ((retval = nc_def_var(ncid_out, name, xtype,
  260. ndims, dimids, &varid_out)))
  261. BAIL(retval);
  262. /* Copy the attributes. */
  263. for (a=0; a<natts; a++)
  264. {
  265. if ((retval = nc_inq_attname(ncid_in, varid_in, a, att_name)))
  266. BAIL(retval);
  267. if ((retval = nc_copy_att(ncid_in, varid_in, att_name,
  268. ncid_out, varid_out)))
  269. BAIL(retval);
  270. }
  271. /* End define mode, to write metadata and create file. */
  272. nc_enddef(ncid_out);
  273. nc_sync(ncid_out);
  274. /* Allocate memory for our start and count arrays. If ndims = 0
  275. this is a scalar, which I will treat as a 1-D array with one
  276. element. */
  277. real_ndims = ndims ? ndims : 1;
  278. if (!(start = malloc(real_ndims * sizeof(size_t))))
  279. BAIL(NC_ENOMEM);
  280. if (!(count = malloc(real_ndims * sizeof(size_t))))
  281. BAIL(NC_ENOMEM);
  282. /* The start array will be all zeros, except the first element,
  283. which will be the record number. Count will be the dimension
  284. size, except for the first element, which will be one, because
  285. we will copy one record at a time. For this we need the var
  286. shape. */
  287. if (!(dimlen = malloc(real_ndims * sizeof(size_t))))
  288. BAIL(NC_ENOMEM);
  289. /* Find out how much data. */
  290. for (d=0; d<ndims; d++)
  291. {
  292. if ((retval = nc_inq_dimlen(ncid_in, dimids[d], &dimlen[d])))
  293. BAIL(retval);
  294. #ifdef USE_NETCDF4
  295. LOG((4, "nc_copy_var: there are %d data", dimlen[d]));
  296. #endif
  297. }
  298. /* If this is really a scalar, then set the dimlen to 1. */
  299. if (ndims == 0)
  300. dimlen[0] = 1;
  301. for (d=0; d<real_ndims; d++)
  302. {
  303. start[d] = 0;
  304. count[d] = d ? dimlen[d] : 1;
  305. if (d) reclen *= dimlen[d];
  306. }
  307. /* If there are no records, we're done. */
  308. if (!dimlen[0])
  309. goto exit;
  310. /* Allocate memory for one record. */
  311. if (!(data = malloc(reclen * type_size)))
  312. return NC_ENOMEM;
  313. /* Copy the var data one record at a time. */
  314. for (start[0]=0; !retval && start[0]<(size_t)dimlen[0]; start[0]++)
  315. {
  316. switch (xtype)
  317. {
  318. case NC_BYTE:
  319. retval = nc_get_vara_schar(ncid_in, varid_in, start, count,
  320. (signed char *)data);
  321. if (!retval)
  322. retval = nc_put_vara_schar(ncid_out, varid_out, start, count,
  323. (const signed char *)data);
  324. break;
  325. case NC_CHAR:
  326. retval = nc_get_vara_text(ncid_in, varid_in, start, count,
  327. (char *)data);
  328. if (!retval)
  329. retval = nc_put_vara_text(ncid_out, varid_out, start, count,
  330. (char *)data);
  331. break;
  332. case NC_SHORT:
  333. retval = nc_get_vara_short(ncid_in, varid_in, start, count,
  334. (short *)data);
  335. if (!retval)
  336. retval = nc_put_vara_short(ncid_out, varid_out, start, count,
  337. (short *)data);
  338. break;
  339. case NC_INT:
  340. retval = nc_get_vara_int(ncid_in, varid_in, start, count,
  341. (int *)data);
  342. if (!retval)
  343. retval = nc_put_vara_int(ncid_out, varid_out, start, count,
  344. (int *)data);
  345. break;
  346. case NC_FLOAT:
  347. retval = nc_get_vara_float(ncid_in, varid_in, start, count,
  348. (float *)data);
  349. if (!retval)
  350. retval = nc_put_vara_float(ncid_out, varid_out, start, count,
  351. (float *)data);
  352. break;
  353. case NC_DOUBLE:
  354. retval = nc_get_vara_double(ncid_in, varid_in, start, count,
  355. (double *)data);
  356. if (!retval)
  357. retval = nc_put_vara_double(ncid_out, varid_out, start, count,
  358. (double *)data);
  359. break;
  360. case NC_UBYTE:
  361. retval = nc_get_vara_uchar(ncid_in, varid_in, start, count,
  362. (unsigned char *)data);
  363. if (!retval)
  364. retval = nc_put_vara_uchar(ncid_out, varid_out, start, count,
  365. (unsigned char *)data);
  366. break;
  367. case NC_USHORT:
  368. retval = nc_get_vara_ushort(ncid_in, varid_in, start, count,
  369. (unsigned short *)data);
  370. if (!retval)
  371. retval = nc_put_vara_ushort(ncid_out, varid_out, start, count,
  372. (unsigned short *)data);
  373. break;
  374. case NC_UINT:
  375. retval = nc_get_vara_uint(ncid_in, varid_in, start, count,
  376. (unsigned int *)data);
  377. if (!retval)
  378. retval = nc_put_vara_uint(ncid_out, varid_out, start, count,
  379. (unsigned int *)data);
  380. break;
  381. case NC_INT64:
  382. retval = nc_get_vara_longlong(ncid_in, varid_in, start, count,
  383. (long long *)data);
  384. if (!retval)
  385. retval = nc_put_vara_longlong(ncid_out, varid_out, start, count,
  386. (long long *)data);
  387. break;
  388. case NC_UINT64:
  389. retval = nc_get_vara_ulonglong(ncid_in, varid_in, start, count,
  390. (unsigned long long *)data);
  391. if (!retval)
  392. retval = nc_put_vara_ulonglong(ncid_out, varid_out, start, count,
  393. (unsigned long long *)data);
  394. break;
  395. default:
  396. retval = NC_EBADTYPE;
  397. }
  398. }
  399. exit:
  400. if (data) free(data);
  401. if (dimlen) free(dimlen);
  402. if (start) free(start);
  403. if (count) free(count);
  404. return retval;
  405. }
  406. static int
  407. NC_copy_att(int ncid_in, int varid_in, const char *name,
  408. int ncid_out, int varid_out)
  409. {
  410. nc_type xtype;
  411. size_t len;
  412. void *data=NULL;
  413. int res;
  414. LOG((2, "nc_copy_att: ncid_in 0x%x varid_in %d name %s",
  415. ncid_in, varid_in, name));
  416. /* Find out about the attribute to be copied. */
  417. if ((res = nc_inq_att(ncid_in, varid_in, name, &xtype, &len)))
  418. return res;
  419. if (xtype < NC_STRING)
  420. {
  421. /* Handle non-string atomic types. */
  422. if (len)
  423. if (!(data = malloc(len * NC_atomictypelen(xtype))))
  424. return NC_ENOMEM;
  425. res = nc_get_att(ncid_in, varid_in, name, data);
  426. if (!res)
  427. res = nc_put_att(ncid_out, varid_out, name, xtype,
  428. len, data);
  429. if (len)
  430. free(data);
  431. }
  432. #ifdef USE_NETCDF4
  433. else if (xtype == NC_STRING)
  434. {
  435. /* Copy string attributes. */
  436. char **str_data;
  437. if (!(str_data = malloc(sizeof(char *) * len)))
  438. return NC_ENOMEM;
  439. res = nc_get_att_string(ncid_in, varid_in, name, str_data);
  440. if (!res)
  441. res = nc_put_att_string(ncid_out, varid_out, name, len,
  442. (const char **)str_data);
  443. nc_free_string(len, str_data);
  444. free(str_data);
  445. }
  446. else
  447. {
  448. /* Copy user-defined type attributes. */
  449. int class;
  450. size_t size;
  451. void *data;
  452. nc_type xtype_out = NC_NAT;
  453. /* Find out if there is an equal type in the output file. */
  454. /* Note: original code used a libsrc4 specific internal function
  455. which we had to "duplicate" here */
  456. if ((res = NC_find_equal_type(ncid_in, xtype, ncid_out, &xtype_out)))
  457. return res;
  458. if (xtype_out)
  459. {
  460. /* We found an equal type! */
  461. if ((res = nc_inq_user_type(ncid_in, xtype, NULL, &size,
  462. NULL, NULL, &class)))
  463. return res;
  464. if (class == NC_VLEN) /* VLENs are different... */
  465. {
  466. nc_vlen_t *vldata;
  467. int i;
  468. if (!(vldata = malloc(sizeof(nc_vlen_t) * len)))
  469. return NC_ENOMEM;
  470. if ((res = nc_get_att(ncid_in, varid_in, name, vldata)))
  471. return res;
  472. if ((res = nc_put_att(ncid_out, varid_out, name, xtype_out,
  473. len, vldata)))
  474. return res;
  475. for (i = 0; i < len; i++)
  476. if((res = nc_free_vlen(&vldata[i])))
  477. return res;
  478. free(vldata);
  479. }
  480. else /* not VLEN */
  481. {
  482. if (!(data = malloc(size * len)))
  483. return NC_ENOMEM;
  484. res = nc_get_att(ncid_in, varid_in, name, data);
  485. if (!res)
  486. res = nc_put_att(ncid_out, varid_out, name, xtype_out, len, data);
  487. free(data);
  488. }
  489. }
  490. }
  491. #endif /*!USE_NETCDF4*/
  492. return res;
  493. }
  494. /* Copy an attribute from one open file to another.
  495. Special programming challenge: this function must work even if one
  496. of the other of the files is a netcdf version 1.0 file (i.e. not
  497. HDF5). So only use top level netcdf api functions.
  498. From the netcdf-3 docs: The output netCDF dataset should be in
  499. define mode if the attribute to be copied does not already exist
  500. for the target variable, or if it would cause an existing target
  501. attribute to grow.
  502. */
  503. int
  504. nc_copy_att(int ncid_in, int varid_in, const char *name,
  505. int ncid_out, int varid_out)
  506. {
  507. int format, target_natts, target_attid;
  508. char att_name[NC_MAX_NAME + 1];
  509. int a, retval;
  510. /* What is the destination format? */
  511. if ((retval = nc_inq_format(ncid_out, &format)))
  512. return retval;
  513. /* Can't copy to same var in same file. */
  514. if (ncid_in == ncid_out && varid_in == varid_out)
  515. return NC_NOERR;
  516. /* For classic model netCDF-4 files, order of attributes must be
  517. * maintained during copies. We MUST MAINTAIN ORDER! */
  518. if (format == NC_FORMAT_NETCDF4_CLASSIC)
  519. {
  520. /* Does this attribute already exist in the target file? */
  521. retval = nc_inq_attid(ncid_out, varid_out, name, &target_attid);
  522. if (retval == NC_ENOTATT)
  523. {
  524. /* Attribute does not exist. No order to be preserved. */
  525. return NC_copy_att(ncid_in, varid_in, name, ncid_out, varid_out);
  526. }
  527. else if (retval == NC_NOERR)
  528. {
  529. /* How many atts for this var? */
  530. if ((retval = nc_inq_varnatts(ncid_out, varid_out, &target_natts)))
  531. return retval;
  532. /* If this is the last attribute in the target file, we are
  533. * off the hook. */
  534. if (target_attid == target_natts - 1)
  535. return NC_copy_att(ncid_in, varid_in, name, ncid_out, varid_out);
  536. /* Order MUST BE MAINTAINED! Copy all existing atts in the target
  537. * file, stopping at our target att. */
  538. for (a = 0; a < target_natts; a++)
  539. {
  540. if (a == target_attid)
  541. {
  542. if ((retval = NC_copy_att(ncid_in, varid_in, name, ncid_out, varid_out)))
  543. return retval;
  544. }
  545. else
  546. {
  547. if ((retval = nc_inq_attname(ncid_out, varid_out, a, att_name)))
  548. return retval;
  549. if ((retval = NC_copy_att(ncid_out, varid_out, att_name,
  550. ncid_out, varid_out)))
  551. return retval;
  552. }
  553. }
  554. }
  555. else
  556. return retval; /* Some other error occured. */
  557. }
  558. else
  559. return NC_copy_att(ncid_in, varid_in, name, ncid_out, varid_out);
  560. return NC_NOERR;
  561. }