1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
|
/*-------------------------------------------------------------------------
*
* Copyright (C) 2000 National Center for Supercomputing Applications.
* All rights reserved.
*
*-------------------------------------------------------------------------
*/
/******************************************************************************
Description:
1. converter
See HDF4 to HDF5 mapping specification at
(http://hdf.ncsa.uiuc.edu/HDF5/papers/h4toh5) for the default mapping
from HDF4 object to HDF5 object.
The whole converter includes 10 files, h4toh5util.h, h4toh5main.h, h4toh5util.c, h4toh5main.c, h4toh5sds.c, h4toh5image.c,h4toh5vdata.c,h4toh5vgroup.c,h4toh5pal.c and h4toh5anno.c.
2. this file
Converting an hdf4 sds object into an hdf5 dataset.
Author: Kent Yang(ymuqun@ncsa.uiuc.edu)
*****************************************************************************/
#include "h4toh5main.h"
static int convert_zerosdsunlimit(int32 file_id,
int32 sds_id,
hid_t h5_group,
hid_t h5_dimgroup,
int h4_attr);
/*-------------------------------------------------------------------------
* Function: Sds_h4_to_h5
*
* Purpose: translate SDS object into hdf5 dataset
*
* Return: FAIL if failed, SUCCEED if successful.
*
* In :
sds_id: SDS identifier
h5_group: hdf5 group id
h5_dimgroup: hdf5 dimension group id
dim_pathname: dimensional path name
*-------------------------------------------------------------------------
*/
int Sds_h4_to_h5(int32 file_id,int32 sds_id,hid_t h5_group,hid_t h5_dimgroup,int h4_attr){
int32 sds_dtype;
int32 sds_rank;
int32 sds_dimsizes[MAX_VAR_DIMS];
int32* sds_start;
int32* sds_edge;
int32* sds_stride;
int32 count_sdsdata;
int32 sds_ref;
int sds_empty;
int32 istat;
int i,j;
int32 num_sdsattrs;
void* sds_data;
int check_sdsname;
int check_gloattr;
char sdsname[MAX_NC_NAME];
char sdslabel[MAX_NC_NAME];
size_t h4size;
size_t h4memsize;
HDF_CHUNK_DEF c_def_out;
hsize_t* chunk_dims;
int32 c_flags;
/* for checking compression */
sp_info_block_t info_block;
int16 special_code;
int32 access_id;
uint16 sd_ref;
int gzip_level;
/* define variables for hdf5. */
hid_t h5dset;
hid_t h5d_sid;
hid_t h5ty_id;
hid_t h5_memtype;
hid_t create_plist;
hid_t write_plist;
hsize_t h5dims[MAX_VAR_DIMS];
hsize_t max_h5dims[MAX_VAR_DIMS];
#ifdef H5_WANT_H5_V1_4_COMPAT
hsize_t bufsize;
#else /* H5_WANT_H5_V1_4_COMPAT */
size_t bufsize;
#endif /* H5_WANT_H5_V1_4_COMPAT */
char* h5csds_name;
herr_t ret;
/* define variables to handle transformation when the maximum memory
buffer is set by users. */
int NUM_HSLAB_PERD;
int32* h4slab_start;
int32* h4slab_stride;
int32* h4slab_stop;
int32* h4slab_dims;
int32* h4slab_edges;
hsize_t* h5slab_offset;
hsize_t* h5slab_count;
int h4slab_count,h4slab_index;
int32 slabsize;
int32 count_slabdata;
hid_t slabmemspace;
/* if(memsize <=0) slabsize = -1;*/
if(MEMOPT != 0)
slabsize = SLABSIZE*1000000;
else slabsize = 0;
special_code = -1;
/* zeroing out the memory for sdsname and sdslabel.*/
h4toh5_ZeroMemory(sdsname,MAX_NC_NAME);
h4toh5_ZeroMemory(sdslabel,MAX_NC_NAME);
/* check whether the sds is empty. */
if(SDcheckempty(sds_id,&sds_empty)== FAIL) {
printf("error in running SDcheckempty routine. \n");
return FAIL;
}
/*check whether the sds is created with unlimited dimension. */
/*obtain name,rank,dimsizes,datatype and num of attributes of sds */
if (SDgetinfo(sds_id,sdsname,&sds_rank,sds_dimsizes,&sds_dtype,
&num_sdsattrs)==FAIL) {
printf("unable to get information of sds h5dset.\n");
return FAIL;
}
if(sds_empty !=0) {
if(sds_dimsizes[0]==0) {
if(convert_zerosdsunlimit(file_id,sds_id,h5_group,h5_dimgroup,h4_attr)==FAIL){
printf("cannot convert unlimited dimension SDS with 0.\n");
return FAIL;
}
}
else if(convert_sdsfillvalue(file_id,sds_id,h5_group,h5_dimgroup,h4_attr)==FAIL) {
printf("cannot convert fill value successfully.\n");
return FAIL;
}
return SUCCEED;
}
if(SDgetchunkinfo(sds_id,&c_def_out, &c_flags)== FAIL) {
printf("error in getting chunking information. \n");
return FAIL;
}
/* obtain start,edge, stride and number of sds data. */
sds_start = malloc(sizeof(int32)*sds_rank);
if(sds_start == NULL) {
printf("error in allocating memory for sds start.\n");
return FAIL;
}
sds_edge = malloc(sizeof(int32)*sds_rank);
if(sds_edge == NULL) {
printf("error in allocating memory for sds edge.\n");
free(sds_start);
return FAIL;
}
sds_stride = malloc(sizeof(int32)*sds_rank);
if(sds_stride == NULL) {
printf("error in allocating memory for sds stride. \n");
free(sds_start);
free(sds_edge);
return FAIL;
}
count_sdsdata = 1;
for (i=0;i<sds_rank;i++){
sds_stride[i] = 1;
sds_start[i] = 0;
sds_edge[i] = sds_dimsizes[i];
count_sdsdata = count_sdsdata*sds_dimsizes[i];
}
for (i=0;i<sds_rank;i++) {
h5dims[i] = sds_edge[i]-sds_start[i];
max_h5dims[i] = h5dims[i];
}
if(SDisrecord(sds_id)) {
max_h5dims[0] = H5S_UNLIMITED;
}
/* convert hdf4 data type to hdf5 data type. */
if (h4type_to_h5type(sds_dtype,&h5_memtype,&h4memsize,&h4size,
&h5ty_id) == FAIL) {
printf("failed to translate datatype. \n");
free(sds_start);
free(sds_edge);
free(sds_stride);
return FAIL;
}
/* check whether the datatype is string, if we find string format,
we will change them back into integer format.*/
if (h5ty_id == H5T_STRING) {
/* rechange string datatype into numerical datatype.*/
if(h5string_to_int(sds_dtype,&h5_memtype,h4memsize,
&h5ty_id)== FAIL) {
printf("error in translating H5T_STRING to int.\n");
free(sds_start);
free(sds_edge);
free(sds_stride);
return FAIL;
}
}
/* sds_data = malloc(h4memsize*count_sdsdata);
if(sds_data == NULL) {
printf("error in allocating memory. \n");
free(sds_start);
free(sds_edge);
free(sds_stride);
return FAIL;
}
istat = SDreaddata(sds_id, sds_start, sds_stride, sds_edge,
(VOIDP)sds_data);
if (istat == FAIL) {
printf("unable to read data from h5dset. \n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
return FAIL;
}
*/
/* obtaining reference number and name of h5 dataset
corresponding to sds. */
sds_ref = SDidtoref(sds_id);
if(sds_ref == FAIL) {
printf("error in obtaining sds reference number. \n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
return FAIL;
}
h5csds_name = get_name(sds_ref,2*num_sds,sds_hashtab,&check_sdsname);
if (h5csds_name == NULL && check_sdsname == 0 ) {
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
printf("error,cannot find sds name \n");
return FAIL;
}
if (h5csds_name == NULL && check_sdsname == -1) {
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
printf("error,sds name is not defined.\n");
return FAIL;
}
if (h5csds_name == NULL && check_sdsname == -2) {
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
printf("error,not enough memory for allocating sds name.\n");
return FAIL;
}
h5d_sid = H5Screate_simple(sds_rank,h5dims,max_h5dims);
if (h5d_sid < 0) {
printf("failed to create hdf5 data space converted from SDS. \n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
return FAIL;
}
/* create property list. */
create_plist = H5Pcreate(H5P_DATASET_CREATE);
chunk_dims = malloc(sizeof(hsize_t)*sds_rank);
sd_ref = get_SDref(file_id,DFTAG_NDG,sds_ref);
if(sd_ref == 0)
sd_ref = get_SDref(file_id,DFTAG_SDG,sds_ref);
if(sd_ref >0 )
access_id = Hstartread(file_id,DFTAG_SD,sd_ref);
if(sd_ref == 0)
access_id = FAIL;
if(access_id != FAIL) {
istat = Hinquire(access_id,NULL,NULL,NULL,NULL,NULL,NULL,NULL,&special_code);
if(istat == FAIL) {
printf("failed to inquire information \n ");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Pclose(create_plist);
return FAIL;
}
if(special_code >0){
if(HDget_special_info(access_id,&info_block)==FAIL){
printf("fail to get special info.\n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Pclose(create_plist);
return FAIL;
}
/* free(info_block.cdims);*/
if(info_block.key == SPECIAL_COMP) {
if(c_flags == HDF_NONE){
/* 1. if the first dimension is unlimited dimension,
we have to provide a chunking size.
2. the current HDF5 will not handle compression case itself,
in order that the converted HDF5 is compressed, we have to
provide a chunking size. currently it is set to h5dim[i].*/
for(i=0;i<sds_rank;i++){
chunk_dims[i] = (hsize_t)(h5dims[i]);
}
if(H5Pset_chunk(create_plist, sds_rank, chunk_dims)<0) {
printf("failed to set up chunking information for ");
printf("property list.\n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Pclose(create_plist);
return FAIL;
}
if(H5Pset_deflate(create_plist,GZIP_COMLEVEL)<0){
/* if(H5Pset_deflate(create_plist,2)<0){*/
printf("fail to set compression method for HDF5 file.\n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Pclose(create_plist);
}
}
}
else if(c_flags == HDF_NONE && SDisrecord(sds_id))
{
for(i=0;i<sds_rank;i++){
chunk_dims[i] = (hsize_t)(sds_dimsizes[i]);
}
if(H5Pset_chunk(create_plist, sds_rank, chunk_dims)<0) {
printf("failed to set up chunking information for ");
printf("property list.\n");
free(chunk_dims);
H5Pclose(create_plist);
return FAIL;
}
}
}
}
/* HDF4 can support various compression methods including simple RLE, NBIT, Skip Huffman, gzip,Jpeg , HDF5 currently only supports gzip compression.
By default, we will compress HDF5 dataset by using gzip compression if HDF5 file is compressed. */
if(c_flags == HDF_CHUNK || c_flags == (HDF_CHUNK | HDF_COMP)
|| c_flags == (HDF_CHUNK | HDF_NBIT) ){
if(c_def_out.comp.comp_type == COMP_CODE_RLE || c_def_out.comp.comp_type == COMP_CODE_NBIT || c_def_out.comp.comp_type == COMP_CODE_SKPHUFF || c_def_out.comp.comp_type == COMP_CODE_DEFLATE || c_def_out.comp.comp_type == COMP_CODE_JPEG) {
for(i=0;i<sds_rank;i++)
chunk_dims[i] = (hsize_t)c_def_out.chunk_lengths[i];
if(H5Pset_chunk(create_plist, sds_rank, chunk_dims)<0) {
printf("failed to set up chunking information for ");
printf("property list.\n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Pclose(create_plist);
return FAIL;
}
if(c_def_out.comp.comp_type == COMP_CODE_DEFLATE)
gzip_level = c_def_out.comp.cinfo.deflate.level;
else gzip_level = GZIP_COMLEVEL;
if(H5Pset_deflate(create_plist,gzip_level)<0){
printf("fail to set compression method for HDF5 file.\n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Pclose(create_plist);
}
}
}
if(access_id == FAIL && c_flags != HDF_CHUNK && c_flags != (HDF_CHUNK | HDF_COMP)
&& c_flags != (HDF_CHUNK | HDF_NBIT)) {
for(i=0;i<sds_rank;i++)
chunk_dims[i] = h5dims[i];
if(H5Pset_chunk(create_plist, sds_rank, chunk_dims)<0) {
printf("failed to set up chunking information for ");
printf("property list.\n");
free(chunk_dims);
H5Sclose(h5d_sid);
H5Pclose(create_plist);
return FAIL;
}
}
h5dset = H5Dcreate(h5_group,h5csds_name,h5ty_id,h5d_sid,create_plist);
if (h5dset < 0) {
printf("failed to create hdf5 dataset converted from SDS. \n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Pclose(create_plist);
return FAIL;
}
if(count_sdsdata*h4memsize <= slabsize || MEMOPT==0) {
sds_data = malloc(h4memsize*count_sdsdata);
if(sds_data == NULL) {
printf("error in allocating memory. \n");
free(sds_start);
free(sds_edge);
free(sds_stride);
return FAIL;
}
istat = SDreaddata(sds_id, sds_start, sds_stride, sds_edge,
(VOIDP)sds_data);
if (istat == FAIL) {
printf("unable to read data from h5dset. \n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
return FAIL;
}
write_plist = H5Pcreate(H5P_DATASET_XFER);
bufsize = h4memsize;
for(i=0;i<sds_rank;i++)
bufsize *= h5dims[i];
if(H5Pset_buffer(write_plist,bufsize,NULL,NULL)<0) {
printf("fail to create data transfer property list.\n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Pclose(create_plist);
H5Pclose(write_plist);
return FAIL;
}
if (H5Dwrite(h5dset,h5_memtype,h5d_sid,h5d_sid,write_plist,
(void *)sds_data)<0) {
printf("failed to write data into hdf5 dataset");
printf(" converted from SDS.\n");
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
free(chunk_dims);
return FAIL;
}
free(sds_data);
}
else {
NUM_HSLAB_PERD= get_numslab_perD(h4memsize*count_sdsdata,slabsize,sds_rank);
h4slab_start = calloc(sds_rank,sizeof(int32));
h4slab_stride = calloc(sds_rank,sizeof(int32));
h4slab_stop = calloc(sds_rank,sizeof(int32));
h4slab_dims = calloc(sds_rank,sizeof(int32));
h4slab_edges = calloc(sds_rank,sizeof(int32));
h5slab_offset = calloc(sds_rank,sizeof(hsize_t));
h5slab_count = calloc(sds_rank,sizeof(hsize_t));
for ( i =0;i<sds_rank;i++) {
h4slab_start[i] =0;
h4slab_stop[i] = 0;
h4slab_stride[i] =1;
h4slab_dims[i] = h5ceil(sds_dimsizes[i],NUM_HSLAB_PERD);
printf("h4slab_dims[%d]%d\n",i,h4slab_dims[i]);
}
h4slab_count = -1;
while(h4slab_count != pow(NUM_HSLAB_PERD,sds_rank)-1){
h4slab_count++;
h4slab_index = -1;
for (i=0;i<sds_rank;i++){
if((h4slab_count%pow(NUM_HSLAB_PERD,(i+1)))==0){
h4slab_index = i;
}
}
if(h4slab_index != -1 && h4slab_index != (sds_rank-1)) {
for(j=0;j<=h4slab_index;j++){
h4slab_start[j] =0;
}
h4slab_start[j] = h4slab_start[j]+h4slab_dims[j];
}
for(i=0;i<sds_rank;i++){
h4slab_stop[i] = h4slab_start[i]+h4slab_dims[i];
if(h4slab_stop[i]>sds_dimsizes[i])
h4slab_stop[i] = sds_dimsizes[i];
printf("h4slab_start[%d] %d\n",i,h4slab_start[i]);
printf("h4slab_stop[%d] %d\n",i,h4slab_stop[i]);
}
count_slabdata = 1;
for(i=0;i<sds_rank;i++){
count_slabdata = count_slabdata*(h4slab_stop[i]-h4slab_start[i]);
h4slab_edges[i] = h4slab_stop[i]-h4slab_start[i];
}
count_slabdata = count_slabdata*h4memsize;
printf("count_slabdata %d\n",count_slabdata);
sds_data = malloc(count_slabdata);
if(sds_data == NULL) {
printf("error in allocating memory. \n");
free(sds_start);
free(sds_edge);
free(sds_stride);
return FAIL;
}
istat = SDreaddata(sds_id, h4slab_start, h4slab_stride, h4slab_edges,
(VOIDP)sds_data);
if (istat == FAIL) {
printf("unable to read SDS data. \n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
return FAIL;
}
printf("after SDread data\n");
write_plist = H5Pcreate(H5P_DATASET_XFER);
/* bufsize = h4memsize;
for(i=0;i<sds_rank;i++)
bufsize *= h5dims[i];*/
bufsize = slabsize;
if(H5Pset_buffer(write_plist,bufsize,NULL,NULL)<0) {
printf("fail to create data transfer property list.\n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Pclose(create_plist);
H5Pclose(write_plist);
return FAIL;
}
for (i=0;i<sds_rank;i++) {
h5slab_offset[i] = (hsize_t)(h4slab_start[i]);
h5slab_count[i] =(hsize_t)(h4slab_stop[i]-h4slab_start[i]);
}
slabmemspace = H5Screate_simple(sds_rank,h5slab_count,NULL);
H5Sselect_hyperslab(h5d_sid,H5S_SELECT_SET,h5slab_offset,NULL,h5slab_count,NULL);
if (H5Dwrite(h5dset,h5_memtype,slabmemspace,h5d_sid,write_plist,
(void *)sds_data)<0) {
/* printf("before writing \n");
if (H5Dwrite(h5dset,h5_memtype,h5d_sid,h5d_sid,H5P_DEFAULT,
(void *)sds_data)<0) { */
printf("failed to write data into hdf5 dataset");
printf(" converted from SDS.\n");
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
free(chunk_dims);
return FAIL;
}
printf("after writing hdf5 \n");
free(sds_data);
h4slab_start[0] = h4slab_start[0]+h4slab_dims[0];
}
free(h4slab_start);
free(h4slab_stop);
free(h4slab_dims);
free(h4slab_edges);
free(h5slab_offset);
free(h5slab_count);
}
/* convert sds annotation into attribute of sds dataset.
Since there is no routines to find the exact tag of sds object,
we will check three possible object tags of sds objects, that is:
DFTAG_SD,DFTAG_SDG,DFTAG_NDG. If the object tag of sds object is
falling out of this scope, we will not convert annotations into
hdf5 attributes; it is user's responsibility to make sure object tags
for sds objects are only one of the above three tags.*/
if(Annoobj_h4_to_h5(file_id,sds_ref,DFTAG_SD,h5dset)== FAIL){
printf("failed to convert sds annotation into hdf5 attribute.\n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
return FAIL;
}
if(Annoobj_h4_to_h5(file_id,sds_ref,DFTAG_SDG,h5dset)== FAIL){
printf("failed to convert sds annotation into hdf5 attribute.\n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
return FAIL;
}
if(Annoobj_h4_to_h5(file_id,sds_ref,DFTAG_NDG,h5dset)== FAIL){
printf("failed to convert sds annotation into hdf5 attribute.\n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
return FAIL;
}
/* convert sds dimensional scale dataset into hdf5 dataset. */
if(sdsdim_to_h5dataset(sds_id,sds_rank,h5dset,h5_dimgroup,sds_dimsizes[0]) == FAIL) {
printf("failed to convert dimensional scale to hdf5 dataset. \n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
return FAIL;
}
check_gloattr = 0;
if (sds_transattrs(sds_id,h5dset,num_sdsattrs,check_gloattr)==FAIL) {
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
printf(" Error in obtaining sds attributes. \n");
return FAIL;
}
/********************************************/
/* handle extra attributes of sds : sds label, object type
and reference num */
if(h4_attr !=0) {
strcpy(sdslabel,SDSLABEL);
if(h4_transpredattrs(h5dset,HDF4_OBJECT_TYPE,sdslabel)==FAIL) {
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
printf("unable to transfer sds label to HDF4 OBJECT TYPE.\n");
return FAIL;
}
if(sdsname[0] != '\0') {
if(h4_transpredattrs(h5dset,HDF4_OBJECT_NAME,sdsname)==FAIL){
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
printf("unable to transfer sds name to HDF5 dataset attribute.\n");
return FAIL;
}
}
if(h4_transnumattr(h5dset,HDF4_REF_NUM,sds_ref)==FAIL){
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
printf("unable to transfer sds ref. to HDF5 dataset attribute.\n");
return FAIL;
}
}
istat = SDendaccess(sds_id);
ret = H5Pclose(create_plist);
ret = H5Sclose(h5d_sid);
ret = H5Dclose(h5dset);
/* free(sds_data);*/
free(sds_start);
free(sds_edge);
free(sds_stride);
free(chunk_dims);
return SUCCEED;
}
/*-------------------------------------------------------------------------
* Function: sds_transattrs
*
* Purpose: translate attribute of HDF4 SDS object into
hdf5 dataset attribute
*
* Return: FAIL if failed, SUCCEED if successful.
*
* In :
ssds_id: SDS identifier
sh5_dset: hdf5 dataset
snum_sdsattrs: number of sds attribute
check_gloflag: a flag that check whether the attribute is
a file attribute or a sds id or a dimensional scale id.
*-------------------------------------------------------------------------
*/
int sds_transattrs(int32 ssds_id, hid_t sh5_dset,int snum_sdsattrs,
int check_gloflag) {
char ssdsatrr_name[2*MAX_NC_NAME];
char sdsglo[MAX_NC_NAME];
char* sdsrepattr_name;
int32 count_ssdsadata;
int32 ssds_atype;
size_t sh4_amemsize;
size_t sh4_asize;
hid_t sh5a_sid;
hid_t sh5a_id;
hid_t sh5_atype;
hid_t sh5_amemtype;
hid_t sh5str_type;
hid_t sh5str_memtype;
hsize_t sh5dims[MAX_VAR_DIMS];
void* ssds_adata;
herr_t sret;
int i;
for (i = 0;i < snum_sdsattrs; i++) {
if (SDattrinfo(ssds_id,i,ssdsatrr_name,&ssds_atype,
&count_ssdsadata)==FAIL){
printf("unable to obtain SDS attribute information. \n");
return FAIL;
}
/* make a table for the attribute type, to do the corresponding type. */
if(h4type_to_h5type(ssds_atype,&sh5_amemtype,&sh4_amemsize,
&sh4_asize,&sh5_atype)== FAIL) {
printf("fail to translate sds attribute data type from H4 to H5. \n");
return FAIL;
}
ssds_adata = malloc(sh4_amemsize * count_ssdsadata);
if(ssds_adata == NULL) {
printf("error, cannot allocate memory for sds attribute data. \n");
return FAIL;
}
if(SDreadattr(ssds_id,i,(VOIDP)ssds_adata)== FAIL) {
printf("error in reading attributes of sds object. \n");
free(ssds_adata);
return FAIL;
}
/* if attribute doesn't have name, a default name is set. */
if(ssdsatrr_name[0] == '\0') {
sdsrepattr_name = trans_obj_name(DFTAG_NDG,i);
strcpy(ssdsatrr_name,sdsrepattr_name);
free(sdsrepattr_name);
}
/* if the sds attribute is a file attribute. */
if(check_gloflag == 1){
strcpy(sdsglo,GLOSDS);
strcat(ssdsatrr_name,"_");
strcat(ssdsatrr_name,sdsglo);
}
/* now do attribute-transferring.
1. deal with string data type
2. set attribute space.
3. get attribute name, set property list. */
if (sh5_atype == H5T_STRING) {
sh5a_sid = H5Screate(H5S_SCALAR);
if (sh5a_sid < 0) {
printf("failed to create attribute space for");
printf(" HDF4_OBJECT_TYPE SDS. \n");
free(ssds_adata);
return FAIL;
}
if ((sh5str_type = mkstr(count_ssdsadata,
H5T_STR_SPACEPAD))<0) {
printf("error in making string. \n");
H5Sclose(sh5a_sid);
free(ssds_adata);
return FAIL;
}
/* check this line later. */
if ((sh5str_memtype = mkstr(count_ssdsadata*sh4_amemsize,
H5T_STR_SPACEPAD))<0) {
printf("error in making memory string. \n");
H5Sclose(sh5a_sid);
free(ssds_adata);
return FAIL;
}
sh5a_id = H5Acreate(sh5_dset,ssdsatrr_name,sh5str_type,
sh5a_sid,H5P_DEFAULT);
if (sh5a_id <0) {
printf("failed to obtain attribute id for");
printf(" HDF4_OBJECT_TYPE SDS. \n");
H5Sclose(sh5a_sid);
free(ssds_adata);
return FAIL;
}
sret = H5Awrite(sh5a_id,sh5str_memtype,(void *)ssds_adata);
if (sret <0) {
printf("failed to write attribute data for");
printf(" HDF4_OBJECT_TYPE SDS. \n");
H5Sclose(sh5a_sid);
H5Aclose(sh5a_id);
free(ssds_adata);
return FAIL;
}
sret = H5Sclose(sh5a_sid);
sret = H5Aclose(sh5a_id);
}
else {
if(count_ssdsadata == 1) {
sh5a_sid = H5Screate(H5S_SCALAR);
if (sh5a_sid < 0) {
printf("failed to create space id. \n");
free(ssds_adata);
return FAIL;
}
}
else {
sh5dims[0] = count_ssdsadata;
sh5a_sid = H5Screate_simple(1,sh5dims,NULL);
if (sh5a_sid < 0) {
printf("failed to create attribute space. \n");
free(ssds_adata);
return FAIL;
}
}
sh5a_id = H5Acreate(sh5_dset,ssdsatrr_name,sh5_atype,
sh5a_sid,H5P_DEFAULT);
if(sh5a_id <0) {
printf("failed to obtain attribute id. \n");
H5Sclose(sh5a_sid);
free(ssds_adata);
return FAIL;
}
sret = H5Awrite(sh5a_id,sh5_amemtype,(void *)ssds_adata);
if(sret <0) {
printf("failed to write attribute data.\n ");
H5Sclose(sh5a_sid);
H5Aclose(sh5a_id);
free(ssds_adata);
return FAIL;
}
sret = H5Sclose(sh5a_sid);
sret = H5Aclose(sh5a_id);
}
free(ssds_adata);
}
return SUCCEED;
}
/****************sdsdim_to_h5dataset*******************
* Purpose: translate dimensional scale dataset into
hdf5 dataset
*
* Return: FAIL if failed, SUCCEED if successful.
*
* In :
sds_id: SDS identifier
sds_rank: number of sds dimensions
Out:
Modification:
*-------------------------------------------------------------------------
*/
int sdsdim_to_h5dataset(int32 sds_id,int32 sds_rank,hid_t sh5dset,
hid_t sh5_dimgroup,int32 firstdimsize) {
int32 sdsdim_id;
int32 sdsdim_type = 0;
int32 sds_dimscasize[1];
int32 istat;
int i,k;
int count_h5objref;/* this counter updates the number of h5 object reference. */
int count_h5attrname;/*this counter updates the number of h5 dimensional name attribute.*/
int check_gloattr;
int32 num_sdsdimattrs;
int check_sdsdim;
void* dim_scadata;
char sdsdim_name[MAX_NC_NAME+1];
char* cor_sdsdimname;
size_t h4dim_memsize;
size_t h4dim_size;
HDF_CHUNK_DEF c_def_out;
int32 c_flags;
int32* sdsdimempty;
/* define varibles for hdf5. */
hid_t h5dim_dset;
hid_t h5dim_sid;
hid_t h5dim_tid;
hid_t h5dim_memtype;
hid_t h5dim_nameaid;
hid_t h5dim_namesid;
hid_t h5str_dimntype;
hid_t attr_refSpace;
hid_t attr_refType;
hid_t attribID;
hid_t create_plist;
int dim_index;
hsize_t h5dimscas[1];
hsize_t max_h5dimscas[1];
hsize_t h5dim_dims[1];
hsize_t h5dimname_dims[1];
hsize_t attr_refDims[1];
hsize_t h5dim_chunkdim[1];
hobj_ref_t dim_refdat;
hobj_ref_t* alldim_refdat;
char* h5sdsdim_name;
/* char *h5sdsdim_allname[MAX_VAR_DIMS];
char *h5newsdsdim_name; */
char h5sdsdim_allname[MAX_VAR_DIMS*MAX_DIM_NAME];
char h5newsdsdim_name[MAX_DIM_NAME];
char h5dimpath_name[MAX_DIM_NAME];
herr_t ret;
sdsdimempty = malloc(sds_rank *sizeof(int32));
for(i=0;i<sds_rank;i++)
sdsdimempty[i] = 0;
/*zero out memory for h5sdsdim_allname and h5dimpath_name */
/*** the following line should be erased for variable length HDF5 string.**/
h4toh5_ZeroMemory(h5sdsdim_allname,(MAX_VAR_DIMS*MAX_DIM_NAME)*sizeof(char));
h4toh5_ZeroMemory(h5dimpath_name,MAX_DIM_NAME*sizeof(char));
/*check whether the sds is created with unlimited dimension. */
if(firstdimsize !=0) {
if(SDgetchunkinfo(sds_id,&c_def_out, &c_flags)== FAIL) {
printf("error in getting chunking information. \n");
return FAIL;
}
}
/* initialize the dimensional number of sds dimensions, h5dim_dims
is used for grabbing hdf5 dimensional name list and object reference
list. */
h5dim_dims[0] = (hsize_t)sds_rank;
count_h5objref = 0;
count_h5attrname = 0;
for (i = 0; i<sds_rank;i++) {
sdsdim_id = SDgetdimid(sds_id,i);
if(sdsdim_id == FAIL) {
printf("error in obtaining sds dimension id. \n");
return FAIL;
}
istat = SDdiminfo(sdsdim_id,sdsdim_name,sds_dimscasize,
&sdsdim_type,&num_sdsdimattrs);
if (istat == FAIL) {
printf("sds get dim. information failed. \n");
SDendaccess(sdsdim_id);
return FAIL;
}
/* Here we have very messy cases for dimensional scale when
sdsdim_type is 0(or not set).
When sdsdim_type is 0, it means no SDS dimensional scale
data for this dimensions. However, users may define SDS dimensional
scale name. We want to keep this information.
If user doesn't specific the name we will skip this dimension */
if(sdsdim_type == 0) {
if(strncmp(sdsdim_name,fakeDim,strlen(fakeDim))==0){
SDendaccess(sdsdim_id);
continue;
}
}
/* for unlimited SDS dimension, grab the current dimensional size. */
if(sds_dimscasize[0] == 0) sds_dimscasize[0] = firstdimsize;
/* check whether this dimensional scale dataset is looked up. */
check_sdsdim = lookup_name(sdsdim_name,DIM_HASHSIZE,dim_hashtab);
strcpy(h5dimpath_name,HDF4_DIMG);
/* checking whether sds dimension scale name contains ORI_SLASH, changing into CHA_SLASH.*/
cor_sdsdimname = correct_name(sdsdim_name);
if(cor_sdsdimname == NULL) {
printf("error in generating corrected sds dimensional scale name.\n");
SDendaccess(sdsdim_id);
return FAIL;
}
/* generating hdf5 dimensional scale name. */
h5sdsdim_name = get_obj_aboname(cor_sdsdimname,NULL,h5dimpath_name,NULL);
if (h5sdsdim_name == NULL) {
printf("error in getting hdf5 sds dimension name.\n");
SDendaccess(sdsdim_id);
free(cor_sdsdimname);
return FAIL;
}
free(cor_sdsdimname);
strncpy(&h5sdsdim_allname[count_h5attrname*MAX_DIM_NAME],h5sdsdim_name,MAX_DIM_NAME);
/*h5sdsdim_allname[count_h5attrname]=HDstrdup(h5sdsdim_name);
should be added for variable length HDF5 string. 6/11/2001. */
/* here we should add some comments for fakedim0--name. It seems that
hdf4(netcdf) will use unique fake dimension name, fakedim + unique
number, so check_sdsdim will never be 1 if the dimension name is fake
name. Under this case, count_h5objref and count_h5attrname
will not increase if this dimension doesnot
have dimensional scale data. That assures the object reference of sds is
correct. */
/*if this dimension is not touched, get name of the dimensional scale data. */
if (check_sdsdim == 1){/* the dimension is touched, skip this one.*/
free(h5sdsdim_name);
SDendaccess(sdsdim_id);
/* here we have to check a special case when the dimension type is 0.
We should ignore counting object reference.*/
if(sdsdim_type == 0) {
count_h5attrname++;
continue;
}
count_h5objref = count_h5objref + 1;
count_h5attrname = count_h5attrname + 1;
continue;
}
if (check_sdsdim != 0) {
printf("error in checking sds dimensions.\n");
SDendaccess(sdsdim_id);
free(h5sdsdim_name);
return FAIL;
}
/* here we want to keep the dimensional scale name without
making the object reference. */
if(sdsdim_type == 0) {
count_h5attrname = count_h5attrname + 1;
sdsdimempty[i] = 1;
continue;
}
/* get h5 dimensional scale data type. */
if(h4type_to_h5type(sdsdim_type,&h5dim_memtype,&h4dim_memsize,
&h4dim_size,&h5dim_tid)== FAIL) {
printf("error in transferring sds dimension data type.\n");
SDendaccess(sdsdim_id);
free(h5sdsdim_name);
return FAIL;
}
/* dimensional scale dataset cannot be H5T_STRING data type.
So transferring back to int8 */
if (h5dim_tid == H5T_STRING) {
if(h5string_to_int(sdsdim_type,&h5dim_memtype,h4dim_memsize,
&h5dim_tid)==FAIL){
printf("error in translating from string to int. \n");
SDendaccess(sdsdim_id);
free(h5sdsdim_name);
return FAIL;
}
}
/* get the dimensional scale data. */
dim_scadata = malloc(h4dim_memsize*sds_dimscasize[0]);
istat = SDgetdimscale(sdsdim_id,(VOIDP)dim_scadata);
if (istat == FAIL) {
printf("sds get dim. scale failed. \n");
SDendaccess(sdsdim_id);
free(h5sdsdim_name);
free(dim_scadata);
return FAIL;
}
/* set dimensional scale size properly. */
h5dimscas[0] = sds_dimscasize[0];
/* only set for the first dimension if SDS is unlimited dimension. */
if(SDisrecord(sds_id) && i == 0)
max_h5dimscas[0] = H5S_UNLIMITED;
else
max_h5dimscas[0] = h5dimscas[0];
h5dim_sid = H5Screate_simple(1,h5dimscas,max_h5dimscas);
if(h5dim_sid <0) {
printf("error in creating space. \n");
SDendaccess(sdsdim_id);
free(h5sdsdim_name);
free(dim_scadata);
return FAIL;
}
/* create property list, for chunked sds or unlimited dimension cases */
create_plist = H5Pcreate(H5P_DATASET_CREATE);
if(create_plist == -1) {
printf("failed to create property list. \n");
SDendaccess(sdsdim_id);
free(h5sdsdim_name);
free(dim_scadata);
H5Sclose(h5dim_sid);
}
if(c_flags == HDF_NONE && SDisrecord(sds_id) && i == 0)
{
h5dim_chunkdim[0] = (hsize_t)(h5dimscas[0]/2);
if(H5Pset_chunk(create_plist,1, h5dim_chunkdim)<0) {
printf("failed to set up chunking information for ");
printf("dimensional scale property list.\n");
SDendaccess(sdsdim_id);
free(h5sdsdim_name);
free(dim_scadata);
H5Sclose(h5dim_sid);
H5Pclose(create_plist);
return FAIL;
}
}
if(c_flags == HDF_CHUNK || c_flags == (HDF_CHUNK | HDF_COMP)
|| c_flags == (HDF_CHUNK | HDF_NBIT) ){
h5dim_chunkdim[0] = (hsize_t)c_def_out.chunk_lengths[0];
if(H5Pset_chunk(create_plist,1, h5dim_chunkdim)<0) {
printf("failed to set up chunking information for ");
printf("property list.\n");
SDendaccess(sdsdim_id);
free(h5sdsdim_name);
free(dim_scadata);
H5Sclose(h5dim_sid);
H5Pclose(create_plist);
return FAIL;
}
}
/* create h5 dataset under group HDF4_DIMG*/
h5dim_dset = H5Dcreate(sh5_dimgroup,h5sdsdim_name,h5dim_tid,
h5dim_sid,create_plist);
if(h5dim_dset <0) {
printf("error in creating dataset. \n");
free(h5sdsdim_name);
free(dim_scadata);
SDendaccess(sdsdim_id);
H5Sclose(h5dim_sid);
H5Pclose(create_plist);
return FAIL;
}
if (H5Dwrite(h5dim_dset,h5dim_memtype,h5dim_sid,h5dim_sid,
H5P_DEFAULT,(void *)dim_scadata)<0) {
printf("error writing dimensional scale data\n");
free(h5sdsdim_name);
free(dim_scadata);
SDendaccess(sdsdim_id);
H5Sclose(h5dim_sid);
H5Pclose(create_plist);
H5Dclose(h5dim_dset);
return FAIL;
}
check_gloattr = 0;
if(sds_transattrs(sdsdim_id,h5dim_dset,num_sdsdimattrs,check_gloattr)
== FAIL){
printf("error in transferring attributes. \n");
free(h5sdsdim_name);
free(dim_scadata);
SDendaccess(sdsdim_id);
H5Sclose(h5dim_sid);
H5Dclose(h5dim_dset);
H5Pclose(create_plist);
return FAIL;
}
SDendaccess(sdsdim_id);
free(dim_scadata);
free(h5sdsdim_name);
ret = H5Sclose(h5dim_sid);
ret = H5Dclose(h5dim_dset);
ret = H5Pclose(create_plist);
count_h5objref = count_h5objref + 1;
count_h5attrname =count_h5attrname + 1;
}
/*1. create object reference number to dimensional scale dataset.
2. store absolute name of dimensional name into
dimensional list. */
if ( count_h5objref != 0) {
h5dim_dims[0] = count_h5objref;
attr_refDims[0] = count_h5objref;
attr_refSpace = H5Screate_simple(1,attr_refDims,NULL);
attr_refType = H5Tcopy(H5T_STD_REF_OBJ);
alldim_refdat = calloc((size_t)count_h5objref,sizeof(hobj_ref_t));
if(alldim_refdat == NULL) {
printf("error in allocating memory. \n");
H5Sclose(attr_refSpace);
H5Tclose(attr_refType);
return FAIL;
}
k =0;
for(i=0;i<count_h5objref;i++){
if(sdsdimempty[i])
k = k +1;
h4toh5_ZeroMemory(h5newsdsdim_name,MAX_DIM_NAME);
strcpy(h5newsdsdim_name,&h5sdsdim_allname[k*MAX_DIM_NAME]);
/*h5newsdsdim_name = HDstrdup(h5sdsdim_allname[k]);
for variable length HDF5 string. 6/11/2001; Kent*/
ret = H5Rcreate(&dim_refdat,sh5_dimgroup,h5newsdsdim_name,
H5R_OBJECT,-1);
if(ret <0) {
free(alldim_refdat);
H5Sclose(attr_refSpace);
H5Tclose(attr_refType);
printf("error in generating H5 reference. \n");
return FAIL;
}
alldim_refdat[i] = dim_refdat;
k = k +1;
/*free(h5newsdsdim_name); for variable length HDF5 string 6/11/2001.*/
}
attribID = H5Acreate(sh5dset,DIMSCALE,attr_refType,attr_refSpace,
H5P_DEFAULT);
if(attribID < 0) {
free(alldim_refdat);
H5Sclose(attr_refSpace);
H5Tclose(attr_refType);
H5Aclose(attribID);
printf("error in generating H5 attribute ID. \n");
return FAIL;
}
ret = H5Awrite(attribID,attr_refType,(void *)alldim_refdat);
H5Sclose(attr_refSpace);
H5Tclose(attr_refType);
H5Aclose(attribID);
free(alldim_refdat);
}
if(count_h5attrname!= 0) {
h5dimname_dims[0] = count_h5attrname;
h5dim_namesid = H5Screate_simple(1,h5dimname_dims,NULL);
if(h5dim_namesid <0) {
printf("error in creating sds dimensionlist space.\n");
return FAIL;
}
/*h5str_dimntype = mkstr(MAX_DIM_NAME,H5T_STR_SPACEPAD); */
h5str_dimntype = mkstr(MAX_DIM_NAME,H5T_STR_NULLTERM);
if(h5str_dimntype < 0) {
H5Sclose(h5dim_namesid);
printf("error in generating H5T_STRING type.\n");
return FAIL;
}
/*using variable length, h5dump and h5view do not
support this, this will be supported later.
h5str_dimntype = H5Tcopy(H5T_C_S1);
ret = H5Tset_size(h5str_dimntype,H5T_VARIABLE);
6/11/2001, Kent Yang
*/
h5dim_nameaid = H5Acreate(sh5dset,HDF4_DIMENSION_LIST,h5str_dimntype,
h5dim_namesid,H5P_DEFAULT);
if(h5dim_nameaid <0) {
H5Sclose(h5dim_namesid);
printf("error in creating sds dimensionlist id.\n");
return FAIL;
}
ret = H5Awrite(h5dim_nameaid,h5str_dimntype,h5sdsdim_allname);
if(ret < 0) {
H5Sclose(h5dim_namesid);
H5Aclose(h5dim_nameaid);
printf("error in writing sds dimensionlist. \n");
return FAIL;
}
ret = H5Sclose(h5dim_namesid);
ret = H5Aclose(h5dim_nameaid);
/*used for variable length HDF5 string.
for(dim_index = 0; dim_index <count_h5attrname;dim_index++)
free(h5sdsdim_allname[dim_index]);
6/11/2001, kent
since variable length has not been supported in h5dump yet and
that will cause testing failed so we use NULL TERM instead.
8/9/2001 kent */
}
free(sdsdimempty);
return SUCCEED;
}
int convert_sdsfillvalue(int32 file_id,int32 sds_id,hid_t h5_group,hid_t h5_dimgroup,int h4_attr){
int32 sds_dtype;
int32 sds_rank;
int32 sds_dimsizes[MAX_VAR_DIMS];
int32* sds_start;
int32* sds_edge;
int32* sds_stride;
int32 count_sdsdata;
int32 sds_ref;
int32 istat;
int i;
int32 num_sdsattrs;
void* fill_value;
int check_sdsname;
int check_gloattr;
char sdsname[MAX_NC_NAME];
char sdslabel[MAX_NC_NAME];
size_t h4size;
size_t h4memsize;
HDF_CHUNK_DEF c_def_out;
hsize_t* chunk_dims;
int32 c_flags;
/* define varibles for hdf5. */
hid_t h5dset;
hid_t h5d_sid;
hid_t h5ty_id;
hid_t h5_memtype;
hid_t create_plist;
hid_t write_plist;
hsize_t h5dims[MAX_VAR_DIMS];
hsize_t max_h5dims[MAX_VAR_DIMS];
#ifdef H5_WANT_H5_V1_4_COMPAT
hsize_t bufsize;
#else /* H5_WANT_H5_V1_4_COMPAT */
size_t bufsize;
#endif /* H5_WANT_H5_V1_4_COMPAT */
char* h5csds_name;
if (SDgetinfo(sds_id,sdsname,&sds_rank,sds_dimsizes,&sds_dtype,
&num_sdsattrs)==FAIL) {
printf("unable to get information of sds h5dset.\n");
return FAIL;
}
/* obtain start,edge, stride and number of sds data. */
sds_start = malloc(sizeof(int32)*sds_rank);
if(sds_start == NULL) {
printf("error in allocating memory for sds start.\n");
return FAIL;
}
sds_edge = malloc(sizeof(int32)*sds_rank);
if(sds_edge == NULL) {
printf("error in allocating memory for sds edge.\n");
free(sds_start);
return FAIL;
}
sds_stride = malloc(sizeof(int32)*sds_rank);
if(sds_stride == NULL) {
printf("error in allocating memory for sds stride. \n");
free(sds_start);
free(sds_edge);
return FAIL;
}
count_sdsdata = 1;
for (i=0;i<sds_rank;i++){
sds_stride[i] = 1;
sds_start[i] = 0;
sds_edge[i] = sds_dimsizes[i];
count_sdsdata = count_sdsdata*sds_dimsizes[i];
}
for (i=0;i<sds_rank;i++) {
h5dims[i] = sds_edge[i]-sds_start[i];
max_h5dims[i] = h5dims[i];
}
if(SDisrecord(sds_id)) {
max_h5dims[0] = H5S_UNLIMITED;
}
/* convert hdf4 data type to hdf5 data type. */
if (h4type_to_h5type(sds_dtype,&h5_memtype,&h4memsize,&h4size,
&h5ty_id) == FAIL) {
printf("failed to translate datatype. \n");
free(sds_start);
free(sds_edge);
free(sds_stride);
return FAIL;
}
/* check whether the datatype is string, if we find string format,
we will change them back into integer format.*/
if (h5ty_id == H5T_STRING) {
/* rechange string datatype into numerical datatype.*/
if(h5string_to_int(sds_dtype,&h5_memtype,h4memsize,
&h5ty_id)== FAIL) {
printf("error in translating H5T_STRING to int.\n");
free(sds_start);
free(sds_edge);
free(sds_stride);
return FAIL;
}
}
/* Since we know this SDS dataset fills with fill value, so currently
we will comment this out. */
/*
sds_data = malloc(h4memsize*count_sdsdata);
if(sds_data == NULL) {
printf("error in allocating memory. \n");
free(sds_start);
free(sds_edge);
free(sds_stride);
return FAIL;
}
istat = SDreaddata(sds_id, sds_start, sds_stride, sds_edge,
(VOIDP)sds_data);
if (istat == FAIL) {
printf("unable to read data from h5dset. \n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(sds_data);
return FAIL;
}
*/
fill_value = malloc(h4memsize);
h4toh5_ZeroMemory(fill_value,h4memsize*sizeof(char));
if(num_sdsattrs != 0)
if(SDgetfillvalue(sds_id,fill_value)==FAIL)
printf("unable to get fill value, fill value will be set to zero \n");
sds_ref = SDidtoref(sds_id);
if(sds_ref == FAIL) {
printf("error in obtaining sds reference number. \n");
free(sds_start);
free(sds_edge);
free(sds_stride);
return FAIL;
}
h5csds_name = get_name(sds_ref,2*num_sds,sds_hashtab,&check_sdsname);
if (h5csds_name == NULL && check_sdsname == 0 ) {
free(sds_start);
free(sds_edge);
free(sds_stride);
printf("error,cannot find sds name \n");
return FAIL;
}
if (h5csds_name == NULL && check_sdsname == -1) {
free(sds_start);
free(sds_edge);
free(sds_stride);
printf("error,sds name is not defined.\n");
return FAIL;
}
if (h5csds_name == NULL && check_sdsname == -2) {
free(sds_start);
free(sds_edge);
free(sds_stride);
printf("error,not enough memory for allocating sds name.\n");
return FAIL;
}
h5d_sid = H5Screate_simple(sds_rank,h5dims,max_h5dims);
if (h5d_sid < 0) {
printf("failed to create hdf5 data space converted from SDS. \n");
free(sds_start);
free(sds_edge);
free(sds_stride);
return FAIL;
}
/* set creation property list. */
create_plist = H5Pcreate(H5P_DATASET_CREATE);
if(create_plist <0) {
printf("failed to create hdf5 creation list.\n");
free(sds_start);
free(sds_edge);
free(sds_stride);
return FAIL;
}
chunk_dims = malloc(sizeof(hsize_t)*sds_rank);
if(chunk_dims == NULL) {
H5Pclose(create_plist);
free(sds_start);
free(sds_edge);
free(sds_stride);
printf("unable to allocate memory for chunk_dims\n");
return FAIL;
}
if(SDisrecord(sds_id)){
for(i=0;i<sds_rank;i++){
chunk_dims[i] = (hsize_t)(sds_dimsizes[i]/2);
}
if(H5Pset_chunk(create_plist, sds_rank, chunk_dims)<0) {
printf("cannot set chunking size properly.\n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(chunk_dims);
H5Pclose(create_plist);
return FAIL;
}
}
if(H5Pset_fill_value(create_plist,h5ty_id,fill_value)<0){
printf("failed to set property list fill value.\n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Pclose(create_plist);
return FAIL;
}
h5dset = H5Dcreate(h5_group,h5csds_name,h5ty_id,h5d_sid,create_plist);
if (h5dset < 0) {
printf("failed to create hdf5 dataset converted from SDS. \n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Pclose(create_plist);
return FAIL;
}
/* Before HDF5 library make the optimzation of dealing with fill value
data, leave this alone. */
/* write_plist = H5Pcreate(H5P_DATASET_XFER);
bufsize = h4memsize;
for(i=1;i<sds_rank;i++)
bufsize *= h5dims[i];
if(H5Pset_buffer(write_plist,bufsize,NULL,NULL)<0) {
printf("fail to create data transfer property list.\n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Pclose(create_plist);
H5Pclose(write_plist);
return FAIL;
}
if (H5Dwrite(h5dset,h5_memtype,h5d_sid,h5d_sid,write_plist,
(void *)sds_data)<0) {
printf("failed to write data into hdf5 dataset");
printf(" converted from SDS.\n");
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
H5Pclose(write_plist);
free(sds_start);
free(sds_edge);
free(sds_stride);
free(chunk_dims);
return FAIL;
}
*/
/* convert sds annotation into attribute of sds dataset.
Since there is no routines to find the exact tag of sds object,
we will check three possible object tags of sds objects, that is:
DFTAG_SD,DFTAG_SDG,DFTAG_NDG. If the object tag of sds object is
falling out of this scope, we will not convert annotations into
hdf5 attributes; it is user's responsibility to make sure object tags
for sds objects are only one of the above three tags.*/
if(Annoobj_h4_to_h5(file_id,sds_ref,DFTAG_SD,h5dset)== FAIL){
printf("failed to convert sds annotation into hdf5 attribute.\n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
return FAIL;
}
if(Annoobj_h4_to_h5(file_id,sds_ref,DFTAG_SDG,h5dset)== FAIL){
printf("failed to convert sds annotation into hdf5 attribute.\n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
return FAIL;
}
if(Annoobj_h4_to_h5(file_id,sds_ref,DFTAG_NDG,h5dset)== FAIL){
printf("failed to convert sds annotation into hdf5 attribute.\n");
free(sds_start);
free(sds_edge);
free(sds_stride);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
return FAIL;
}
check_gloattr = 0;
if (sds_transattrs(sds_id,h5dset,num_sdsattrs,check_gloattr)==FAIL) {
free(sds_start);
free(sds_edge);
free(sds_stride);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
printf(" Error in obtaining sds attributes. \n");
return FAIL;
}
/********************************************/
/* handle extra attributes of sds : sds label, object type
and reference num */
if(h4_attr !=0) {
strcpy(sdslabel,SDSLABEL);
if(h4_transpredattrs(h5dset,HDF4_OBJECT_TYPE,sdslabel)==FAIL) {
free(sds_start);
free(sds_edge);
free(sds_stride);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
printf("unable to transfer sds label to HDF4 OBJECT TYPE.\n");
return FAIL;
}
if(sdsname[0] != '\0') {
if(h4_transpredattrs(h5dset,HDF4_OBJECT_NAME,sdsname)==FAIL){
free(sds_start);
free(sds_edge);
free(sds_stride);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
printf("unable to transfer sds name to HDF5 dataset attribute.\n");
return FAIL;
}
}
if(h4_transnumattr(h5dset,HDF4_REF_NUM,sds_ref)==FAIL){
free(sds_start);
free(sds_edge);
free(sds_stride);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
printf("unable to transfer sds ref. to HDF5 dataset attribute.\n");
return FAIL;
}
}
free(sds_start);
free(sds_edge);
free(sds_stride);
free(chunk_dims);
free(fill_value);
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
/* H5Pclose(write_plist); */
return SUCCEED;
}
uint16 get_SDref(int32 file_id,uint16 tag,int32 sds_ref){
DFdi di;
int32 found,GroupID;
int sd_ref = 0;
if((GroupID = DFdiread(file_id,tag,(uint16)sds_ref))<0){
/* for some cases, although sd_ref cannot be found, the current
SDS object is still legal(unlimited dimension with the current
size set to 0. so comment this the following warning printfs out. */
/* printf("cannot find sd_ref\n");*/
return sd_ref;
}
found = 0;
di.tag = DFTAG_NULL;
di.ref = 0;
while((found == 0) &&(DFdiget(GroupID,&di.tag,&di.ref)==0)){
if(di.tag == DFTAG_SD)
found = 1;
}
sd_ref = di.ref;
if(!found) ;
/* printf("cannot find sd_ref\n");*/
DFdifree(GroupID);
return sd_ref;
}
static int convert_zerosdsunlimit(int32 file_id,
int32 sds_id,
hid_t h5_group,
hid_t h5_dimgroup,
int h4_attr) {
int32 sds_dtype;
int32 sds_rank;
int32 sds_dimsizes[MAX_VAR_DIMS];
int32* sds_start;
int32* sds_edge;
int32* sds_stride;
int32 count_sdsdata;
int32 sds_ref;
int32 istat;
int i;
int32 num_sdsattrs;
void* fill_value;
int check_sdsname;
int check_gloattr;
char sdsname[MAX_NC_NAME];
char sdslabel[MAX_NC_NAME];
size_t h4size;
size_t h4memsize;
HDF_CHUNK_DEF c_def_out;
hsize_t* chunk_dims;
int32 c_flags;
/* for checking compression */
sp_info_block_t info_block;
int16 special_code;
int32 access_id;
uint16 sd_ref;
int gzip_level;
/* define varibles for hdf5. */
hid_t h5dset;
hid_t h5d_sid;
hid_t h5ty_id;
hid_t h5_memtype;
hid_t create_plist;
hid_t write_plist;
hsize_t h5dims[MAX_VAR_DIMS];
hsize_t max_h5dims[MAX_VAR_DIMS];
hsize_t bufsize;
char* h5csds_name;
if (SDgetinfo(sds_id,sdsname,&sds_rank,sds_dimsizes,&sds_dtype,
&num_sdsattrs)==FAIL) {
printf("unable to get information of sds h5dset.\n");
return FAIL;
}
for (i=0;i<sds_rank;i++) {
h5dims[i] = sds_dimsizes[i];
max_h5dims[i] = h5dims[i];
}
if(SDisrecord(sds_id)){
max_h5dims[0] = H5S_UNLIMITED;
h5dims[0] = H5S_UNLIMITED;
}
/* convert hdf4 data type to hdf5 data type. */
if (h4type_to_h5type(sds_dtype,&h5_memtype,&h4memsize,&h4size,
&h5ty_id) == FAIL) {
printf("failed to translate datatype. \n");
return FAIL;
}
/* check whether the datatype is string, if we find string format,
we will change them back into integer format.*/
if (h5ty_id == H5T_STRING) {
/* rechange string datatype into numerical datatype.*/
if(h5string_to_int(sds_dtype,&h5_memtype,h4memsize,
&h5ty_id)== FAIL) {
printf("error in translating H5T_STRING to int.\n");
return FAIL;
}
}
sds_ref = SDidtoref(sds_id);
if(sds_ref == FAIL) {
printf("error in obtaining sds reference number. \n");
return FAIL;
}
h5csds_name = get_name(sds_ref,2*num_sds,sds_hashtab,&check_sdsname);
if (h5csds_name == NULL && check_sdsname == 0 ) {
printf("error,cannot find sds name \n");
return FAIL;
}
if (h5csds_name == NULL && check_sdsname == -1) {
printf("error,sds name is not defined.\n");
return FAIL;
}
if (h5csds_name == NULL && check_sdsname == -2) {
printf("error,not enough memory for allocating sds name.\n");
return FAIL;
}
h5d_sid = H5Screate_simple(sds_rank,h5dims,max_h5dims);
if (h5d_sid < 0) {
printf("failed to create hdf5 data space converted from SDS. \n");
return FAIL;
}
/* create property list. */
create_plist = H5Pcreate(H5P_DATASET_CREATE);
chunk_dims = malloc(sizeof(hsize_t)*sds_rank);
sd_ref = get_SDref(file_id,DFTAG_NDG,sds_ref);
if(sd_ref == 0)
sd_ref = get_SDref(file_id,DFTAG_SDG,sds_ref);
if(sd_ref >0 )
access_id = Hstartread(file_id,DFTAG_SD,sd_ref);
if(sd_ref == 0)
access_id = FAIL;
if(access_id != FAIL) {
istat = Hinquire(access_id,NULL,NULL,NULL,NULL,NULL,NULL,NULL,&special_code);
if(istat == FAIL) {
printf("failed to inquire information \n ");
free(chunk_dims);
H5Sclose(h5d_sid);
H5Pclose(create_plist);
free(h5csds_name);
return FAIL;
}
if(special_code >0){
if(HDget_special_info(access_id,&info_block)==FAIL){
printf("fail to get special info.\n");
free(chunk_dims);
H5Sclose(h5d_sid);
H5Pclose(create_plist);
free(h5csds_name);
return FAIL;
}
/* free(info_block.cdims);*/
if(info_block.key == SPECIAL_COMP) {
if(c_flags == HDF_NONE){
/* 1. if the first dimension is unlimited dimension,
we have to provide a chunking size.
2. the current HDF5 will not handle compression case itself,
in order that the converted HDF5 is compressed, we have to
provide a chunking size. currently it is set to h5dim[i].*/
for(i=0;i<sds_rank;i++){
chunk_dims[i] = (hsize_t)(h5dims[i]);
}
if(H5Pset_chunk(create_plist, sds_rank, chunk_dims)<0) {
printf("failed to set up chunking information for ");
printf("property list.\n");
free(chunk_dims);
free(h5csds_name);
H5Sclose(h5d_sid);
H5Pclose(create_plist);
return FAIL;
}
if(H5Pset_deflate(create_plist,GZIP_COMLEVEL)<0){
/* if(H5Pset_deflate(create_plist,2)<0){*/
printf("fail to set compression method for HDF5 file.\n");
free(chunk_dims);
free(h5csds_name);
H5Sclose(h5d_sid);
H5Pclose(create_plist);
}
}
}
else if(c_flags == HDF_NONE && SDisrecord(sds_id))
{
for(i=0;i<sds_rank;i++){
chunk_dims[i] = (hsize_t)(sds_dimsizes[i]);
if(sds_dimsizes[0] == 0)
chunk_dims[0] = (hsize_t)DEF_CHUNKSIZE;
}
if(H5Pset_chunk(create_plist, sds_rank, chunk_dims)<0) {
printf("failed to set up chunking information for ");
printf("property list.\n");
free(h5csds_name);
free(chunk_dims);
H5Pclose(create_plist);
return FAIL;
}
}
}
}
/* HDF4 can support various compression methods including simple RLE, NBIT, Skip Huffman, gzip,Jpeg , HDF5 currently only supports gzip compression.
By default, we will compress HDF5 dataset by using gzip compression if HDF5 file is compressed. */
if(c_flags == HDF_CHUNK || c_flags == (HDF_CHUNK | HDF_COMP)
|| c_flags == (HDF_CHUNK | HDF_NBIT) ){
if(c_def_out.comp.comp_type == COMP_CODE_RLE || c_def_out.comp.comp_type == COMP_CODE_NBIT || c_def_out.comp.comp_type == COMP_CODE_SKPHUFF || c_def_out.comp.comp_type == COMP_CODE_DEFLATE || c_def_out.comp.comp_type == COMP_CODE_JPEG) {
for(i=0;i<sds_rank;i++)
chunk_dims[i] = (hsize_t)c_def_out.chunk_lengths[i];
if(H5Pset_chunk(create_plist, sds_rank, chunk_dims)<0) {
printf("failed to set up chunking information for ");
printf("property list.\n");
free(chunk_dims);
free(h5csds_name);
H5Sclose(h5d_sid);
H5Pclose(create_plist);
return FAIL;
}
if(c_def_out.comp.comp_type == COMP_CODE_DEFLATE)
gzip_level = c_def_out.comp.cinfo.deflate.level;
else gzip_level = GZIP_COMLEVEL;
if(H5Pset_deflate(create_plist,gzip_level)<0){
printf("fail to set compression method for HDF5 file.\n");
free(chunk_dims);
free(h5csds_name);
H5Sclose(h5d_sid);
H5Pclose(create_plist);
}
}
}
if(access_id == FAIL) {
chunk_dims[0] = (hsize_t)DEF_CHUNKSIZE;
for(i=1;i<sds_rank;i++)
chunk_dims[i] = h5dims[i];
if(H5Pset_chunk(create_plist, sds_rank, chunk_dims)<0) {
printf("failed to set up chunking information for ");
printf("property list.\n");
free(chunk_dims);
free(h5csds_name);
H5Sclose(h5d_sid);
H5Pclose(create_plist);
return FAIL;
}
}
h5dset = H5Dcreate(h5_group,h5csds_name,h5ty_id,h5d_sid,create_plist);
if (h5dset < 0) {
printf("failed to create hdf5 dataset converted from SDS. \n");
free(chunk_dims);
free(h5csds_name);
H5Sclose(h5d_sid);
H5Pclose(create_plist);
return FAIL;
}
if(sdsdim_to_h5dataset(sds_id,sds_rank,h5dset,h5_dimgroup,sds_dimsizes[0])<0){
printf("failed to convert SDS dimensional scale dataset correctly \n"); free(h5csds_name);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Pclose(create_plist);
return FAIL;
}
/* convert sds annotation into attribute of sds dataset.
Since there is no routines to find the exact tag of sds object,
we will check three possible object tags of sds objects, that is:
DFTAG_SD,DFTAG_SDG,DFTAG_NDG. If the object tag of sds object is
falling out of this scope, we will not convert annotations into
hdf5 attributes; it is user's responsibility to make sure object tags
for sds objects are only one of the above three tags.*/
if(Annoobj_h4_to_h5(file_id,sds_ref,DFTAG_SD,h5dset)== FAIL){
printf("failed to convert sds annotation into hdf5 attribute.\n");
free(h5csds_name);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
return FAIL;
}
if(Annoobj_h4_to_h5(file_id,sds_ref,DFTAG_SDG,h5dset)== FAIL){
printf("failed to convert sds annotation into hdf5 attribute.\n");
free(h5csds_name);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
return FAIL;
}
if(Annoobj_h4_to_h5(file_id,sds_ref,DFTAG_NDG,h5dset)== FAIL){
printf("failed to convert sds annotation into hdf5 attribute.\n");
free(h5csds_name);
free(chunk_dims);
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
return FAIL;
}
check_gloattr = 0;
if (sds_transattrs(sds_id,h5dset,num_sdsattrs,check_gloattr)==FAIL) {
free(chunk_dims);
free(h5csds_name);
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
printf(" Error in obtaining sds attributes. \n");
return FAIL;
}
/********************************************/
/* handle extra attributes of sds : sds label, object type
and reference num */
if(h4_attr !=0) {
strcpy(sdslabel,SDSLABEL);
if(h4_transpredattrs(h5dset,HDF4_OBJECT_TYPE,sdslabel)==FAIL) {
free(chunk_dims);
free(h5csds_name);
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
printf("unable to transfer sds label to HDF4 OBJECT TYPE.\n");
return FAIL;
}
if(sdsname[0] != '\0') {
if(h4_transpredattrs(h5dset,HDF4_OBJECT_NAME,sdsname)==FAIL){
free(chunk_dims);
free(h5csds_name);
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
printf("unable to transfer sds name to HDF5 dataset attribute.\n");
return FAIL;
}
}
if(h4_transnumattr(h5dset,HDF4_REF_NUM,sds_ref)==FAIL){
free(chunk_dims);
free(h5csds_name);
H5Sclose(h5d_sid);
H5Dclose(h5dset);
H5Pclose(create_plist);
printf("unable to transfer sds ref. to HDF5 dataset attribute.\n");
return FAIL;
}
}
H5Sclose(h5d_sid);
free(h5csds_name);
free(chunk_dims);
H5Dclose(h5dset);
H5Pclose(create_plist);
return SUCCEED;
}
int get_numslab_perD(int32 totalsize,int slabsize,int rank) {
int i,flag;
int32 tempsize;
int numslab_perD;
numslab_perD =1;
flag = 0;
while(!flag) {
tempsize =1;
for(i=0;i<rank;i++)
tempsize = tempsize*numslab_perD;
if((totalsize/tempsize)<=slabsize){
flag = 1;
}
else
numslab_perD++;
}
printf("numslab_perD %d\n",numslab_perD);
return numslab_perD;
}
int h5ceil(int32 m,int32 n){
if(m/n * n < m) return m/n +1;
else return m/n;
}
int pow(int num,int h4index) {
int i;
int total;
total = 1;
for( i= 1; i<=h4index;i++)
total = total * num;
return total;
}
|