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
|
/*
* Copyright 2007 - 2018 ETH Zuerich, CISD and SIS.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.hdf5;
import static ch.systemsx.cisd.hdf5.HDF5Utils.HOUSEKEEPING_NAME_SUFFIX_STRINGLENGTH_ATTRIBUTE_NAME;
import static ch.systemsx.cisd.hdf5.HDF5Utils.createAttributeTypeVariantAttributeName;
import static ch.systemsx.cisd.hdf5.HDF5Utils.createObjectTypeVariantAttributeName;
import static ch.systemsx.cisd.hdf5.HDF5Utils.getBooleanDataTypePath;
import static ch.systemsx.cisd.hdf5.HDF5Utils.getDataTypeGroup;
import static ch.systemsx.cisd.hdf5.HDF5Utils.getOneDimensionalArraySize;
import static ch.systemsx.cisd.hdf5.HDF5Utils.getTypeVariantDataTypePath;
import static ch.systemsx.cisd.hdf5.HDF5Utils.getTypeVariantMembersAttributeName;
import static ch.systemsx.cisd.hdf5.HDF5Utils.getVariableLengthStringDataTypePath;
import static ch.systemsx.cisd.hdf5.HDF5Utils.removeInternalNames;
import static hdf.hdf5lib.HDF5Constants.H5S_ALL;
import static hdf.hdf5lib.HDF5Constants.H5T_ARRAY;
import static hdf.hdf5lib.HDF5Constants.H5T_ENUM;
import static hdf.hdf5lib.HDF5Constants.H5T_NATIVE_INT32;
import static hdf.hdf5lib.HDF5Constants.H5T_STRING;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import hdf.hdf5lib.exceptions.HDF5JavaException;
import ch.systemsx.cisd.base.mdarray.MDAbstractArray;
import ch.systemsx.cisd.base.mdarray.MDArray;
import ch.systemsx.cisd.hdf5.HDF5DataTypeInformation.DataTypeInfoOptions;
import ch.systemsx.cisd.hdf5.IHDF5WriterConfigurator.FileFormatVersionBounds;
import ch.systemsx.cisd.hdf5.cleanup.CleanUpCallable;
import ch.systemsx.cisd.hdf5.cleanup.CleanUpRegistry;
import ch.systemsx.cisd.hdf5.cleanup.ICallableWithCleanUp;
import ch.systemsx.cisd.hdf5.cleanup.ICleanUpRegistry;
import ch.systemsx.cisd.hdf5.exceptions.HDF5FileNotFoundException;
import ch.systemsx.cisd.hdf5.exceptions.HDF5SpaceRankMismatch;
import ch.systemsx.cisd.hdf5.hdf5lib.HDFHelper;
import hdf.hdf5lib.HDF5Constants;
/**
* Class that provides base methods for reading HDF5 files.
*
* @author Bernd Rinn
*/
class HDF5BaseReader
{
/** State that this reader / writer is currently in. */
protected enum State
{
CONFIG, OPEN, CLOSED
}
/** Control of metadate image generation. */
static enum MDCImageGeneration
{
/** Do not generate an MDC image. */
NO_GENERATE_MDC_IMAGE(false),
/** Generate an MDC image if the file already has one. */
KEEP_MDC_IMAGE(false),
/** Generate an MDC image. */
GENERATE_MDC_IMAGE(true);
final boolean generateImageForNewFile;
public boolean isGenerateImageForNewFile()
{
return generateImageForNewFile;
}
MDCImageGeneration(boolean generateImageForNewFile)
{
this.generateImageForNewFile = generateImageForNewFile;
}
};
/** The size of a reference in bytes. */
static final int REFERENCE_SIZE_IN_BYTES = 8;
protected final File hdf5File;
protected final CleanUpCallable runner;
protected final CleanUpRegistry fileRegistry;
protected final boolean performNumericConversions;
protected final boolean hasMDCImage;
protected boolean readOnly;
/** Map from named data types to ids. */
private final Map<String, Long> namedDataTypeMap;
private class DataTypeContainer
{
final long typeId;
final String typePath;
DataTypeContainer(long typeId, String typePath)
{
this.typeId = typeId;
this.typePath = typePath;
}
}
private final List<DataTypeContainer> namedDataTypeList;
protected final HDF5 h5;
protected final long fileId;
protected final long booleanDataTypeId;
protected final long variableLengthStringDataTypeId;
protected final HDF5EnumerationType typeVariantDataType;
protected State state;
final String houseKeepingNameSuffix;
final CharacterEncoding encodingForNewDataSets;
// We keep this reference in order to not have the reader garbage collected and thus
// closing the file when specialized readers are still open and need access to this base
// reader.
@SuppressWarnings("unused")
private HDF5Reader myReader;
HDF5BaseReader(File hdf5File, boolean performNumericConversions, boolean autoDereference,
FileFormatVersionBounds fileFormat, MDCImageGeneration mdcGenerateImage, boolean overwrite,
String preferredHouseKeepingNameSuffix)
{
this(hdf5File, performNumericConversions, false, autoDereference, fileFormat, mdcGenerateImage,
overwrite,
preferredHouseKeepingNameSuffix);
}
HDF5BaseReader(File hdf5File, boolean performNumericConversions, boolean useUTF8CharEncoding,
boolean autoDereference, FileFormatVersionBounds fileFormat, MDCImageGeneration mdcGenerateImage,
boolean overwrite, String preferredHouseKeepingNameSuffix)
{
assert hdf5File != null;
assert preferredHouseKeepingNameSuffix != null;
this.readOnly = true;
this.performNumericConversions = performNumericConversions;
this.hdf5File = hdf5File.getAbsoluteFile();
this.runner = new CleanUpCallable();
this.fileRegistry = CleanUpRegistry.createSynchonized();
this.namedDataTypeMap = new HashMap<String, Long>();
this.namedDataTypeList = new ArrayList<DataTypeContainer>();
this.encodingForNewDataSets =
useUTF8CharEncoding ? CharacterEncoding.UTF8 : CharacterEncoding.ASCII;
this.h5 =
new HDF5(fileRegistry, runner, performNumericConversions, useUTF8CharEncoding,
autoDereference);
this.fileId = openFile(fileFormat, mdcGenerateImage, overwrite);
this.hasMDCImage = HDFHelper.H5Fhas_mdc_image(fileId);
this.state = State.OPEN;
final String houseKeepingNameSuffixFromFileOrNull = tryGetHouseKeepingNameSuffix();
this.houseKeepingNameSuffix =
(houseKeepingNameSuffixFromFileOrNull == null) ? preferredHouseKeepingNameSuffix
: houseKeepingNameSuffixFromFileOrNull;
readNamedDataTypes();
variableLengthStringDataTypeId = openOrCreateVLStringType();
booleanDataTypeId = openOrCreateBooleanDataType();
typeVariantDataType = openOrCreateTypeVariantDataType();
}
void setMyReader(HDF5Reader myReader)
{
this.myReader = myReader;
}
void copyObject(String srcPath, long dstFileId, String dstPath)
{
final boolean dstIsDir = dstPath.endsWith("/");
if ("/".equals(srcPath))
{
final String dstDir = dstIsDir ? dstPath : dstPath + "/";
for (String object : getGroupMembers("/"))
{
h5.copyObject(fileId, object, dstFileId, dstDir + object);
}
} else if (dstIsDir)
{
final int idx = srcPath.lastIndexOf('/');
final String sourceObjectName = srcPath.substring(idx < 0 ? 0 : idx);
h5.copyObject(fileId, srcPath, dstFileId, dstPath + sourceObjectName);
} else
{
h5.copyObject(fileId, srcPath, dstFileId, dstPath);
}
}
long openFile(FileFormatVersionBounds fileFormat, MDCImageGeneration mdcGenerateImage, boolean overwrite)
{
if (hdf5File.exists() == false)
{
throw new HDF5FileNotFoundException(hdf5File, "Path does not exit.");
}
if (hdf5File.canRead() == false)
{
throw new HDF5FileNotFoundException(hdf5File, "Path is not readable.");
}
if (hdf5File.isFile() == false)
{
throw new HDF5FileNotFoundException(hdf5File, "Path is not a file.");
}
if (HDF5Factory.isHDF5File(hdf5File) == false)
{
throw new HDF5FileNotFoundException(hdf5File, "Path is not a valid HDF5 file.");
}
return h5.openFileReadOnly(hdf5File.getPath(), fileRegistry);
}
void checkOpen() throws HDF5JavaException
{
if (state != State.OPEN)
{
final String msg =
"HDF5 file '" + hdf5File.getPath() + "' is "
+ (state == State.CLOSED ? "closed." : "not opened yet.");
throw new HDF5JavaException(msg);
}
}
/**
* Closes this object and the file referenced by this object. This object must not be used after
* being closed.
*/
void close()
{
synchronized (fileRegistry)
{
if (state == State.OPEN)
{
fileRegistry.cleanUp(false);
}
state = State.CLOSED;
}
}
boolean isClosed()
{
return state == State.CLOSED;
}
String tryGetHouseKeepingNameSuffix()
{
final ICallableWithCleanUp<String> readRunnable = new ICallableWithCleanUp<String>()
{
@Override
public String call(ICleanUpRegistry registry)
{
final long objectId = h5.openObject(fileId, "/", registry);
if (h5.existsAttribute(objectId,
HDF5Utils.HOUSEKEEPING_NAME_SUFFIX_ATTRIBUTE_NAME))
{
final int suffixLen =
getHousekeepingAttributeExplicitStringLength(objectId, registry);
final boolean explicitLengthStored = (suffixLen >= 0);
final String rawSuffix =
getStringAttribute(objectId, "/",
HDF5Utils.HOUSEKEEPING_NAME_SUFFIX_ATTRIBUTE_NAME,
explicitLengthStored, registry);
return explicitLengthStored ? rawSuffix.substring(0, suffixLen) : rawSuffix;
} else
{
return null;
}
}
};
return runner.call(readRunnable);
}
boolean isMDCImageGenerationEnabled()
{
return readOnly ? hasMDCImage : h5.isMDCImageGenerationEnabled(fileId);
}
public HDF5DataSet openDataSet(final String objectPath)
{
final ICallableWithCleanUp<HDF5DataSet> openDataSetCallable =
new ICallableWithCleanUp<HDF5DataSet>()
{
@Override
public HDF5DataSet call(ICleanUpRegistry registry)
{
final long dataSetId = h5.openDataSet(fileId, objectPath, null);
final HDF5StorageLayout layout = h5.getLayout(dataSetId, registry);
final long dataSpaceId = h5.getDataSpaceForDataSet(dataSetId, null);
final long[] dimensions = h5.getDataSpaceDimensions(dataSpaceId);
final HDF5DataSet dataSet =
new HDF5DataSet(HDF5BaseReader.this, objectPath, dataSetId, dataSpaceId,
dimensions, null, layout, true);
fileRegistry.registerCleanUp(new Runnable()
{
@Override
public void run()
{
dataSet.close();
}
});
return dataSet;
}
};
return runner.call(openDataSetCallable);
}
byte[] getAttributeAsByteArray(final long objectId, final String attributeName,
ICleanUpRegistry registry)
{
final long attributeId = h5.openAttribute(objectId, attributeName, registry);
final long nativeDataTypeId = h5.getNativeDataTypeForAttribute(attributeId, registry);
final int dataClass = h5.getClassType(nativeDataTypeId);
final int size;
if (dataClass == H5T_ARRAY)
{
final int numberOfElements =
MDAbstractArray.getLength(h5.getArrayDimensions(nativeDataTypeId));
final long baseDataType = h5.getBaseDataType(nativeDataTypeId, registry);
final int elementSize = h5.getDataTypeSize(baseDataType);
size = numberOfElements * elementSize;
} else if (dataClass == H5T_STRING)
{
final long stringDataTypeId = h5.getDataTypeForAttribute(attributeId, registry);
size = h5.getDataTypeSize(stringDataTypeId);
if (h5.isVariableLengthString(stringDataTypeId))
{
String[] data = new String[1];
h5.readAttributeVL(attributeId, stringDataTypeId, data);
return data[0].getBytes();
}
} else
{
final int numberOfElements =
MDAbstractArray.getLength(h5.getDataDimensionsForAttribute(attributeId,
registry));
final int elementSize = h5.getDataTypeSize(nativeDataTypeId);
size = numberOfElements * elementSize;
}
return h5.readAttributeAsByteArray(attributeId, nativeDataTypeId, size);
}
long openOrCreateBooleanDataType()
{
final String booleanDataTypePath = getBooleanDataTypePath(houseKeepingNameSuffix);
long dataTypeId = getDataTypeId(booleanDataTypePath);
if (dataTypeId < 0)
{
dataTypeId = createBooleanDataType();
commitDataType(booleanDataTypePath, dataTypeId);
}
return dataTypeId;
}
String tryGetDataTypePath(long dataTypeId)
{
for (DataTypeContainer namedDataType : namedDataTypeList)
{
if (h5.dataTypesAreEqual(dataTypeId, namedDataType.typeId))
{
return namedDataType.typePath;
}
}
return h5.tryGetDataTypePath(dataTypeId);
}
void renameNamedDataType(String oldPath, String newPath)
{
final Long typeIdOrNull = namedDataTypeMap.remove(oldPath);
if (typeIdOrNull != null)
{
namedDataTypeMap.put(newPath, typeIdOrNull);
}
for (int i = 0; i < namedDataTypeList.size(); ++i)
{
final DataTypeContainer c = namedDataTypeList.get(i);
if (c.typePath.equals(oldPath))
{
namedDataTypeList.set(i, new DataTypeContainer(c.typeId, newPath));
}
}
}
String tryGetDataTypeName(long dataTypeId, HDF5DataClass dataClass)
{
final String dataTypePathOrNull = tryGetDataTypePath(dataTypeId);
return HDF5Utils.tryGetDataTypeNameFromPath(dataTypePathOrNull, houseKeepingNameSuffix,
dataClass);
}
long getDataTypeId(final String dataTypePath)
{
final Long dataTypeIdOrNull = namedDataTypeMap.get(dataTypePath);
if (dataTypeIdOrNull == null)
{
// Just in case of data types added to other groups than HDF5Utils.DATATYPE_GROUP
if (h5.exists(fileId, dataTypePath))
{
final long dataTypeId = h5.openDataType(fileId, dataTypePath, fileRegistry);
namedDataTypeMap.put(dataTypePath, dataTypeId);
return dataTypeId;
} else
{
return -1;
}
} else
{
return dataTypeIdOrNull;
}
}
long createBooleanDataType()
{
return h5.createDataTypeEnum(new String[]
{ "FALSE", "TRUE" }, fileRegistry);
}
HDF5EnumerationType openOrCreateTypeVariantDataType()
{
final String typeVariantTypePath = getTypeVariantDataTypePath(houseKeepingNameSuffix);
long dataTypeId = getDataTypeId(typeVariantTypePath);
if (dataTypeId < 0)
{
return createTypeVariantDataType();
}
final long nativeDataTypeId = h5.getNativeDataType(dataTypeId, fileRegistry);
final String[] typeVariantNames = h5.getNamesForEnumOrCompoundMembers(dataTypeId);
return new HDF5EnumerationType(fileId, dataTypeId, nativeDataTypeId, typeVariantTypePath,
typeVariantNames, this);
}
HDF5EnumerationType createTypeVariantDataType()
{
final HDF5DataTypeVariant[] typeVariants = HDF5DataTypeVariant.values();
final String[] typeVariantNames = new String[typeVariants.length];
for (int i = 0; i < typeVariants.length; ++i)
{
typeVariantNames[i] = typeVariants[i].name();
}
final long dataTypeId = h5.createDataTypeEnum(typeVariantNames, fileRegistry);
final long nativeDataTypeId = h5.getNativeDataType(dataTypeId, fileRegistry);
return new HDF5EnumerationType(fileId, dataTypeId, nativeDataTypeId,
getTypeVariantDataTypePath(houseKeepingNameSuffix), typeVariantNames, this);
}
void readNamedDataTypes()
{
final String typeGroup = getDataTypeGroup(houseKeepingNameSuffix);
if (h5.exists(fileId, typeGroup) == false)
{
return;
}
readNamedDataTypes(typeGroup);
}
private void readNamedDataTypes(String dataTypePath)
{
for (String dataTypeSubPath : getGroupMemberPaths(dataTypePath))
{
final HDF5ObjectType type = h5.getObjectTypeInfo(fileId, dataTypeSubPath, false);
if (HDF5ObjectType.isGroup(type))
{
readNamedDataTypes(dataTypeSubPath);
} else if (HDF5ObjectType.isDataType(type))
{
final long dataTypeId = h5.openDataType(fileId, dataTypeSubPath, fileRegistry);
namedDataTypeMap.put(dataTypeSubPath, dataTypeId);
namedDataTypeList.add(new DataTypeContainer(dataTypeId, dataTypeSubPath));
}
}
}
void commitDataType(final String dataTypePath, final long dataTypeId)
{
// Overwrite this method in writer.
}
/**
* Class to store the parameters of a 1d data space.
*/
static class DataSpaceParameters
{
final long memorySpaceId;
final long dataSpaceId;
final int blockSize;
final long[] dimensions;
DataSpaceParameters(long memorySpaceId, long dataSpaceId, int blockSize, long[] dimensions)
{
this.memorySpaceId = memorySpaceId;
this.dataSpaceId = dataSpaceId;
this.blockSize = blockSize;
this.dimensions = dimensions;
}
}
/**
* Returns the {@link DataSpaceParameters} for the given <var>dataSetId</var>.
*/
DataSpaceParameters getSpaceParameters(final long dataSetId, ICleanUpRegistry registry)
{
long[] dimensions = h5.getDataDimensions(dataSetId, registry);
return new DataSpaceParameters(H5S_ALL, H5S_ALL, MDAbstractArray.getLength(dimensions),
dimensions);
}
/**
* Returns the {@link DataSpaceParameters} for a 1d block of the given <var>dataSetId</var>, or
* <code>null</code>, if the offset is outside of the dataset and
* <code>nullWhenOutside == true</code>.
*/
DataSpaceParameters tryGetSpaceParameters(final long dataSetId, final long offset,
final int blockSize, boolean nullWhenOutside, ICleanUpRegistry registry)
{
return tryGetSpaceParameters(dataSetId, 0, offset, blockSize, nullWhenOutside, registry);
}
/**
* Returns the {@link DataSpaceParameters} for a 1d block of the given <var>dataSetId</var>.
*/
DataSpaceParameters getSpaceParameters(final long dataSetId, final long offset,
final int blockSize, ICleanUpRegistry registry)
{
return tryGetSpaceParameters(dataSetId, 0, offset, blockSize, false, registry);
}
/**
* Returns the {@link DataSpaceParameters} for a 1d block of the given <var>dataSet</var>.
*/
DataSpaceParameters getSpaceParameters(final HDF5DataSet dataSet, final long offset,
final int blockSize)
{
final long memorySpaceId;
final long dataSpaceId;
final int effectiveBlockSize;
final long[] dimensions;
if (blockSize > 0)
{
dataSpaceId = dataSet.getDataSpaceId();
dimensions = dataSet.getDimensions();
if (dimensions.length != 1)
{
throw new HDF5JavaException("Data Set is expected to be of rank 1 (rank="
+ dimensions.length + ")");
}
final long size = dimensions[0];
final long maxFileBlockSize = size - offset;
if (maxFileBlockSize <= 0)
{
throw new HDF5JavaException("Offset " + offset + " >= Size " + size);
}
effectiveBlockSize = (int) Math.min(blockSize, Math.min(size, maxFileBlockSize));
final long[] blockShape = new long[]
{ effectiveBlockSize };
h5.setHyperslabBlock(dataSpaceId, new long[]
{ offset }, blockShape);
memorySpaceId = dataSet.getMemorySpaceId(blockShape);
h5.setHyperslabBlock(memorySpaceId, new long[]
{ 0 }, blockShape);
} else
{
memorySpaceId = HDF5Constants.H5S_ALL;
dataSpaceId = HDF5Constants.H5S_ALL;
dimensions = dataSet.getDimensions();
effectiveBlockSize = getOneDimensionalArraySize(dimensions);
}
return new DataSpaceParameters(memorySpaceId, dataSpaceId, effectiveBlockSize, dimensions);
}
/**
* Returns the {@link DataSpaceParameters} for a 1d block of the given <var>dataSetId</var>.
*/
DataSpaceParameters getSpaceParameters(final long dataSetId, final long memoryOffset,
final long offset, final int blockSize, ICleanUpRegistry registry)
{
return tryGetSpaceParameters(dataSetId, memoryOffset, offset, blockSize, false, registry);
}
/**
* Returns the {@link DataSpaceParameters} for a 1d block of the given <var>dataSetId</var>.
*/
DataSpaceParameters tryGetSpaceParameters(final long dataSetId, final long memoryOffset,
final long offset, final int blockSize, boolean nullWhenOutside,
ICleanUpRegistry registry)
{
final long memorySpaceId;
final long dataSpaceId;
final int effectiveBlockSize;
final long[] dimensions;
if (blockSize > 0)
{
dataSpaceId = h5.getDataSpaceForDataSet(dataSetId, registry);
dimensions = h5.getDataSpaceDimensions(dataSpaceId);
if (dimensions.length != 1)
{
throw new HDF5JavaException("Data Set is expected to be of rank 1 (rank="
+ dimensions.length + ")");
}
final long size = dimensions[0];
final long maxFileBlockSize = size - offset;
if (maxFileBlockSize <= 0)
{
if (nullWhenOutside)
{
return null;
}
throw new HDF5JavaException("Offset " + offset + " >= Size " + size);
}
final long maxMemoryBlockSize = size - memoryOffset;
if (maxMemoryBlockSize <= 0)
{
if (nullWhenOutside)
{
return null;
}
throw new HDF5JavaException("Memory offset " + memoryOffset + " >= Size " + size);
}
effectiveBlockSize =
(int) Math.min(blockSize, Math.min(maxMemoryBlockSize, maxFileBlockSize));
final long[] blockShape = new long[]
{ effectiveBlockSize };
h5.setHyperslabBlock(dataSpaceId, new long[]
{ offset }, blockShape);
memorySpaceId = h5.createSimpleDataSpace(blockShape, registry);
h5.setHyperslabBlock(memorySpaceId, new long[]
{ memoryOffset }, blockShape);
} else
{
memorySpaceId = HDF5Constants.H5S_ALL;
dataSpaceId = HDF5Constants.H5S_ALL;
dimensions = h5.getDataDimensions(dataSetId, registry);
effectiveBlockSize = getOneDimensionalArraySize(dimensions);
}
return new DataSpaceParameters(memorySpaceId, dataSpaceId, effectiveBlockSize, dimensions);
}
/**
* Returns the {@link DataSpaceParameters} for a multi-dimensional block of the given
* <var>dataSetId</var>.
*/
DataSpaceParameters getSpaceParameters(final long dataSetId, final long[] offset,
final int[] blockDimensionsOrNull, ICleanUpRegistry registry)
{
return tryGetSpaceParameters(dataSetId, offset, blockDimensionsOrNull, false, registry);
}
/**
* Returns the {@link DataSpaceParameters} for a multi-dimensional block of the given
* <var>dataSetId</var>.
*/
DataSpaceParameters tryGetSpaceParameters(final long dataSetId, final long[] offset,
final int[] blockDimensionsOrNull, boolean nullWhenOutside, ICleanUpRegistry registry)
{
final long memorySpaceId;
final long dataSpaceId;
final long[] effectiveBlockDimensions;
if (blockDimensionsOrNull != null)
{
assert offset != null;
assert blockDimensionsOrNull.length == offset.length;
dataSpaceId = h5.getDataSpaceForDataSet(dataSetId, registry);
final long[] dimensions = h5.getDataSpaceDimensions(dataSpaceId);
if (blockDimensionsOrNull.length != offset.length)
{
throw new HDF5SpaceRankMismatch(blockDimensionsOrNull.length, offset.length);
}
if (blockDimensionsOrNull.length != dimensions.length)
{
throw new HDF5SpaceRankMismatch(blockDimensionsOrNull.length, dimensions.length);
}
effectiveBlockDimensions = new long[blockDimensionsOrNull.length];
for (int i = 0; i < offset.length; ++i)
{
final long maxBlockSize = dimensions[i] - offset[i];
if (maxBlockSize <= 0)
{
if (nullWhenOutside)
{
return null;
}
throw new HDF5JavaException("Offset " + offset[i] + " >= Size " + dimensions[i]);
}
effectiveBlockDimensions[i] =
(blockDimensionsOrNull[i] < 0) ? (int) maxBlockSize : Math.min(
blockDimensionsOrNull[i], maxBlockSize);
}
h5.setHyperslabBlock(dataSpaceId, offset, effectiveBlockDimensions);
memorySpaceId = h5.createSimpleDataSpace(effectiveBlockDimensions, registry);
} else
{
memorySpaceId = H5S_ALL;
dataSpaceId = H5S_ALL;
effectiveBlockDimensions = h5.getDataDimensions(dataSetId, registry);
}
return new DataSpaceParameters(memorySpaceId, dataSpaceId,
MDAbstractArray.getLength(effectiveBlockDimensions), effectiveBlockDimensions);
}
/**
* Returns the {@link DataSpaceParameters} for a multi-dimensional block of the given
* <var>dataSet</var>.
*/
DataSpaceParameters getSpaceParameters(final HDF5DataSet dataSet, final long[] offset,
final int[] blockDimensionsOrNull)
{
final long memorySpaceId;
final long dataSpaceId;
final long[] effectiveBlockDimensions;
if (blockDimensionsOrNull != null)
{
assert offset != null;
assert blockDimensionsOrNull.length == offset.length;
dataSpaceId = dataSet.getDataSpaceId();
final long[] dimensions = dataSet.getDimensions();
if (dimensions.length != blockDimensionsOrNull.length)
{
throw new HDF5SpaceRankMismatch(blockDimensionsOrNull.length, dimensions.length);
}
effectiveBlockDimensions = new long[blockDimensionsOrNull.length];
for (int i = 0; i < offset.length; ++i)
{
final long maxBlockSize = dimensions[i] - offset[i];
if (maxBlockSize <= 0)
{
throw new HDF5JavaException("Offset " + offset[i] + " >= Size " + dimensions[i]);
}
effectiveBlockDimensions[i] =
(blockDimensionsOrNull[i] < 0) ? (int) maxBlockSize : Math.min(
blockDimensionsOrNull[i], maxBlockSize);
}
h5.setHyperslabBlock(dataSpaceId, offset, effectiveBlockDimensions);
memorySpaceId = dataSet.getMemorySpaceId(effectiveBlockDimensions);
} else
{
memorySpaceId = H5S_ALL;
dataSpaceId = H5S_ALL;
effectiveBlockDimensions = dataSet.getDimensions();
}
return new DataSpaceParameters(memorySpaceId, dataSpaceId,
MDAbstractArray.getLength(effectiveBlockDimensions), effectiveBlockDimensions);
}
/**
* Returns the {@link DataSpaceParameters} for the given <var>dataSetId</var> when they are
* mapped to a block in memory.
*/
DataSpaceParameters getBlockSpaceParameters(final long dataSetId, final int[] memoryOffset,
final int[] memoryDimensions, ICleanUpRegistry registry)
{
return tryGetBlockSpaceParameters(dataSetId, memoryOffset, memoryDimensions, false,
registry);
}
/**
* Returns the {@link DataSpaceParameters} for the given <var>dataSet</var> when they are
* mapped to a block in memory.
*/
DataSpaceParameters getBlockSpaceParameters(final HDF5DataSet dataSet, final int[] memoryOffset,
final int[] memoryDimensions)
{
return tryGetBlockSpaceParameters(dataSet, memoryOffset, memoryDimensions, false);
}
/**
* Returns the {@link DataSpaceParameters} for the given <var>dataSetId</var> when they are
* mapped to a block in memory.
*/
DataSpaceParameters tryGetBlockSpaceParameters(final long dataSetId, final int[] memoryOffset,
final int[] memoryDimensions, final boolean nullWhenOutside, ICleanUpRegistry registry)
{
assert memoryOffset != null;
assert memoryDimensions != null;
assert memoryDimensions.length == memoryOffset.length;
final long[] dimensions = h5.getDataDimensions(dataSetId, registry);
final long memorySpaceId =
h5.createSimpleDataSpace(MDAbstractArray.toLong(memoryDimensions), registry);
for (int i = 0; i < dimensions.length; ++i)
{
if (dimensions[i] + memoryOffset[i] > memoryDimensions[i])
{
if (nullWhenOutside)
{
return null;
}
throw new HDF5JavaException("Dimensions " + dimensions[i] + " + memory offset "
+ memoryOffset[i] + " >= memory buffer " + memoryDimensions[i]);
}
}
h5.setHyperslabBlock(memorySpaceId, MDAbstractArray.toLong(memoryOffset), dimensions);
return new DataSpaceParameters(memorySpaceId, H5S_ALL,
MDAbstractArray.getLength(dimensions), dimensions);
}
/**
* Returns the {@link DataSpaceParameters} for the given <var>dataSetId</var> when they are
* mapped to a block in memory.
*/
DataSpaceParameters tryGetBlockSpaceParameters(final HDF5DataSet dataSet, final int[] memoryOffset,
final int[] memoryDimensions, final boolean nullWhenOutside)
{
assert memoryOffset != null;
assert memoryDimensions != null;
assert memoryDimensions.length == memoryOffset.length;
final long[] dimensions = dataSet.getDimensions();
final long memorySpaceId = dataSet.getMemorySpaceId(MDArray.toLong(memoryDimensions));
for (int i = 0; i < dimensions.length; ++i)
{
if (dimensions[i] + memoryOffset[i] > memoryDimensions[i])
{
if (nullWhenOutside)
{
return null;
}
throw new HDF5JavaException("Dimensions " + dimensions[i] + " + memory offset "
+ memoryOffset[i] + " >= memory buffer " + memoryDimensions[i]);
}
}
h5.setHyperslabBlock(memorySpaceId, MDAbstractArray.toLong(memoryOffset), dimensions);
return new DataSpaceParameters(memorySpaceId, H5S_ALL,
MDAbstractArray.getLength(dimensions), dimensions);
}
/**
* Returns the {@link DataSpaceParameters} for a block of the given <var>dataSetId</var> when
* they are mapped to a block in memory.
*/
DataSpaceParameters getBlockSpaceParameters(final long dataSetId, final int[] memoryOffset,
final int[] memoryDimensions, final long[] offset, final int[] blockDimensions,
ICleanUpRegistry registry)
{
assert memoryOffset != null;
assert memoryDimensions != null;
assert offset != null;
assert blockDimensions != null;
assert memoryOffset.length == offset.length;
assert memoryDimensions.length == memoryOffset.length;
assert blockDimensions.length == offset.length;
final long memorySpaceId;
final long dataSpaceId;
final long[] effectiveBlockDimensions;
dataSpaceId = h5.getDataSpaceForDataSet(dataSetId, registry);
final long[] dimensions = h5.getDataSpaceDimensions(dataSpaceId);
if (dimensions.length != blockDimensions.length)
{
throw new HDF5JavaException("Data Set is expected to be of rank "
+ blockDimensions.length + " (rank=" + dimensions.length + ")");
}
effectiveBlockDimensions = new long[blockDimensions.length];
for (int i = 0; i < offset.length; ++i)
{
final long maxFileBlockSize = dimensions[i] - offset[i];
if (maxFileBlockSize <= 0)
{
throw new HDF5JavaException("Offset " + offset[i] + " >= Size " + dimensions[i]);
}
final long maxMemoryBlockSize = memoryDimensions[i] - memoryOffset[i];
if (maxMemoryBlockSize <= 0)
{
throw new HDF5JavaException("Memory offset " + memoryOffset[i] + " >= Size "
+ memoryDimensions[i]);
}
effectiveBlockDimensions[i] =
Math.min(blockDimensions[i], Math.min(maxMemoryBlockSize, maxFileBlockSize));
}
h5.setHyperslabBlock(dataSpaceId, offset, effectiveBlockDimensions);
memorySpaceId =
h5.createSimpleDataSpace(MDAbstractArray.toLong(memoryDimensions), registry);
h5.setHyperslabBlock(memorySpaceId, MDAbstractArray.toLong(memoryOffset),
effectiveBlockDimensions);
return new DataSpaceParameters(memorySpaceId, dataSpaceId,
MDAbstractArray.getLength(effectiveBlockDimensions), effectiveBlockDimensions);
}
/**
* Returns the {@link DataSpaceParameters} for a block of the given <var>dataSet</var> when
* they are mapped to a block in memory.
*/
DataSpaceParameters getBlockSpaceParameters(final HDF5DataSet dataSet, final int[] memoryOffset,
final int[] memoryDimensions, final long[] offset, final int[] blockDimensions)
{
assert dataSet != null;
assert memoryOffset != null;
assert memoryDimensions != null;
assert offset != null;
assert blockDimensions != null;
assert memoryOffset.length == offset.length;
assert memoryDimensions.length == memoryOffset.length;
assert blockDimensions.length == offset.length;
final long memorySpaceId;
final long dataSpaceId;
final long[] effectiveBlockDimensions;
dataSpaceId = dataSet.getDataSpaceId();
final long[] dimensions = dataSet.getDimensions();
if (dimensions.length != blockDimensions.length)
{
throw new HDF5JavaException("Data Set is expected to be of rank "
+ blockDimensions.length + " (rank=" + dimensions.length + ")");
}
effectiveBlockDimensions = new long[blockDimensions.length];
for (int i = 0; i < offset.length; ++i)
{
final long maxFileBlockSize = dimensions[i] - offset[i];
if (maxFileBlockSize <= 0)
{
throw new HDF5JavaException("Offset " + offset[i] + " >= Size " + dimensions[i]);
}
final long maxMemoryBlockSize = memoryDimensions[i] - memoryOffset[i];
if (maxMemoryBlockSize <= 0)
{
throw new HDF5JavaException("Memory offset " + memoryOffset[i] + " >= Size "
+ memoryDimensions[i]);
}
effectiveBlockDimensions[i] =
Math.min(blockDimensions[i], Math.min(maxMemoryBlockSize, maxFileBlockSize));
}
h5.setHyperslabBlock(dataSpaceId, offset, effectiveBlockDimensions);
memorySpaceId = dataSet.getMemorySpaceId(MDArray.toLong(memoryDimensions));
h5.setHyperslabBlock(memorySpaceId, MDAbstractArray.toLong(memoryOffset),
effectiveBlockDimensions);
return new DataSpaceParameters(memorySpaceId, dataSpaceId,
MDAbstractArray.getLength(effectiveBlockDimensions), effectiveBlockDimensions);
}
/**
* Returns the native data type for the given <var>dataSetId</var>, or
* <var>overrideDataTypeId</var>, if it is not negative.
*/
long getNativeDataTypeId(final long dataSetId, final long overrideDataTypeId,
ICleanUpRegistry registry)
{
final long nativeDataTypeId;
if (overrideDataTypeId < 0)
{
nativeDataTypeId = h5.getNativeDataTypeForDataSet(dataSetId, registry);
} else
{
nativeDataTypeId = overrideDataTypeId;
}
return nativeDataTypeId;
}
/**
* Returns the members of <var>groupPath</var>. The order is <i>not</i> well defined.
*
* @param groupPath The path of the group to get the members for.
* @throws IllegalArgumentException If <var>groupPath</var> is not a group.
*/
List<String> getGroupMembers(final String groupPath)
{
assert groupPath != null;
return removeInternalNames(getAllGroupMembers(groupPath), houseKeepingNameSuffix, false);
}
/**
* Returns all members of <var>groupPath</var>, including internal groups that may be used by
* the library to do house-keeping. The order is <i>not</i> well defined.
*
* @param groupPath The path of the group to get the members for.
* @throws IllegalArgumentException If <var>groupPath</var> is not a group.
*/
List<String> getAllGroupMembers(final String groupPath)
{
final String[] groupMemberArray = h5.getGroupMembers(fileId, groupPath);
return new LinkedList<String>(Arrays.asList(groupMemberArray));
}
/**
* Returns the paths of the members of <var>groupPath</var> (including the parent). The order is
* <i>not</i> well defined.
*
* @param groupPath The path of the group to get the member paths for.
* @throws IllegalArgumentException If <var>groupPath</var> is not a group.
*/
List<String> getGroupMemberPaths(final String groupPath)
{
final String superGroupName = (groupPath.equals("/") ? "/" : groupPath + "/");
final List<String> memberNames = getGroupMembers(groupPath);
for (int i = 0; i < memberNames.size(); ++i)
{
memberNames.set(i, superGroupName + memberNames.get(i));
}
return memberNames;
}
/**
* Returns the information about a data set as a {@link HDF5DataTypeInformation} object. It is a
* failure condition if the <var>dataSetPath</var> does not exist or does not identify a data
* set. <br>
* <i>Does not read the data type path of a committed data type.</i>
*
* @param dataSetPath The name (including path information) of the data set to return
* information about.
*/
HDF5DataSetInformation getDataSetInformation(final String dataSetPath)
{
return getDataSetInformation(dataSetPath, DataTypeInfoOptions.DEFAULT, true);
}
/**
* Returns the information about a data set as a {@link HDF5DataTypeInformation} object. It is a
* failure condition if the <var>dataSetPath</var> does not exist or does not identify a data
* set.
*
* @param dataSetPath The name (including path information) of the data set to return
* information about.
* @param options What information to obtain about the data type.
* @param fillDimensions If <code>true</code>, fill in the dimensions of the dataset
*/
HDF5DataSetInformation getDataSetInformation(final String dataSetPath,
final DataTypeInfoOptions options, final boolean fillDimensions)
{
assert dataSetPath != null;
final ICallableWithCleanUp<HDF5DataSetInformation> informationDeterminationRunnable =
new ICallableWithCleanUp<HDF5DataSetInformation>()
{
@Override
public HDF5DataSetInformation call(ICleanUpRegistry registry)
{
final long dataSetId = h5.openDataSet(fileId, dataSetPath, registry);
final long dataTypeId = h5.getDataTypeForDataSet(dataSetId, registry);
final HDF5DataTypeInformation dataTypeInfo =
getDataTypeInformation(dataTypeId, options, registry);
final HDF5DataTypeVariant variantOrNull =
options.knowsDataTypeVariant() ? tryGetTypeVariant(dataSetId,
registry) : null;
final HDF5DataSetInformation dataSetInfo =
new HDF5DataSetInformation(dataTypeInfo, variantOrNull);
// Is it a variable-length string?
final boolean vlString =
(dataTypeInfo.getDataClass() == HDF5DataClass.STRING && h5
.isVariableLengthString(dataTypeId));
if (vlString)
{
dataTypeInfo.setElementSize(-1);
}
if (fillDimensions)
{
h5.fillDataDimensions(dataSetId, false, dataSetInfo, registry);
}
return dataSetInfo;
}
};
return runner.call(informationDeterminationRunnable);
}
/**
* Returns the dimensions of the data set. It is a failure condition if the
* <var>dataSetPath</var> does not exist or does not identify a data set.
*
* @param dataSetPath The name (including path information) of the data set to return
* information about.
*/
long[] getSpaceDimensions(final String dataSetPath)
{
assert dataSetPath != null;
final ICallableWithCleanUp<long[]> informationDeterminationRunnable =
new ICallableWithCleanUp<long[]>()
{
@Override
public long[] call(ICleanUpRegistry registry)
{
final long dataSetId = h5.openDataSet(fileId, dataSetPath, registry);
return h5.getDimensions(dataSetId, false, registry);
}
};
return runner.call(informationDeterminationRunnable);
}
/**
* Returns the rank of this data set of <var>objectPath</var>. This combines the space rank and
* the array rank into one rank. It is a failure condition if the <var>objectPath</var> does not
* exist or does not identify a data set. This method follows symbolic links.
*/
int getRank(String dataSetPath)
{
final HDF5DataSetInformation info =
getDataSetInformation(dataSetPath, DataTypeInfoOptions.MINIMAL, true);
return info.getRank() + info.getTypeInformation().getRank();
}
/**
* Returns the dimensions of the space of <var>objectPath</var> (empty if this is a scalar
* space). It is a failure condition if the <var>objectPath</var> does not exist or does not
* identify a data set. This method follows symbolic links.
*/
long[] getDimensions(String dataSetPath)
{
assert dataSetPath != null;
final HDF5DataSetInformation info =
getDataSetInformation(dataSetPath, DataTypeInfoOptions.MINIMAL, true);
return MatrixUtils.concat(info.getDimensions(), info.getTypeInformation().getDimensions());
}
/**
* Returns the rank of the data set. It is a failure condition if the <var>dataSetPath</var>
* does not exist or does not identify a data set.
*
* @param dataSetPath The name (including path information) of the data set to return
* information about.
*/
int getSpaceRank(final String dataSetPath)
{
assert dataSetPath != null;
final ICallableWithCleanUp<Integer> informationDeterminationRunnable =
new ICallableWithCleanUp<Integer>()
{
@Override
public Integer call(ICleanUpRegistry registry)
{
final long dataSetId = h5.openDataSet(fileId, dataSetPath, registry);
return h5.getRank(dataSetId, false, registry);
}
};
return runner.call(informationDeterminationRunnable);
}
HDF5DataTypeVariant tryGetTypeVariant(final String objectPath)
{
assert objectPath != null;
final ICallableWithCleanUp<HDF5DataTypeVariant> readRunnable =
new ICallableWithCleanUp<HDF5DataTypeVariant>()
{
@Override
public HDF5DataTypeVariant call(ICleanUpRegistry registry)
{
final long objectId = h5.openObject(fileId, objectPath, registry);
return tryGetTypeVariant(objectId, registry);
}
};
return runner.call(readRunnable);
}
HDF5DataTypeVariant tryGetTypeVariant(final String objectPath, final String attributeName)
{
assert objectPath != null;
final ICallableWithCleanUp<HDF5DataTypeVariant> readRunnable =
new ICallableWithCleanUp<HDF5DataTypeVariant>()
{
@Override
public HDF5DataTypeVariant call(ICleanUpRegistry registry)
{
final long objectId = h5.openObject(fileId, objectPath, registry);
return tryGetTypeVariant(objectId, attributeName, registry);
}
};
return runner.call(readRunnable);
}
HDF5EnumerationValueArray getEnumValueArray(final long attributeId, final String objectPath,
final String attributeName, ICleanUpRegistry registry)
{
final long storageDataTypeId = h5.getDataTypeForAttribute(attributeId, registry);
final long nativeDataTypeId = h5.getNativeDataType(storageDataTypeId, registry);
final int len;
final long enumTypeId;
if (h5.getClassType(storageDataTypeId) == H5T_ARRAY)
{
final int[] arrayDimensions = h5.getArrayDimensions(storageDataTypeId);
if (arrayDimensions.length != 1)
{
throw new HDF5JavaException("Attribute '" + attributeName + "' of object '"
+ objectPath + "' is not an array of rank 1, but is of rank "
+ arrayDimensions.length);
}
len = arrayDimensions[0];
enumTypeId = h5.getBaseDataType(storageDataTypeId, registry);
if (h5.getClassType(enumTypeId) != H5T_ENUM)
{
throw new HDF5JavaException("Attribute '" + attributeName + "' of object '"
+ objectPath + "' is not of type enumeration array.");
}
} else
{
if (h5.getClassType(storageDataTypeId) != H5T_ENUM)
{
throw new HDF5JavaException("Attribute '" + attributeName + "' of object '"
+ objectPath + "' is not of type enumeration array.");
}
enumTypeId = storageDataTypeId;
final long[] arrayDimensions = h5.getDataDimensionsForAttribute(attributeId, registry);
len = HDF5Utils.getOneDimensionalArraySize(arrayDimensions);
}
final HDF5EnumerationType enumType =
getEnumTypeForEnumDataType(null, enumTypeId, true, fileRegistry);
final byte[] data =
h5.readAttributeAsByteArray(attributeId, nativeDataTypeId, len
* enumType.getStorageForm().getStorageSize());
final HDF5EnumerationValueArray value =
new HDF5EnumerationValueArray(enumType, EnumerationType.fromStorageForm(data,
enumType.getStorageForm()));
return value;
}
HDF5EnumerationValueMDArray getEnumValueMDArray(final long attributeId, final String objectPath,
final String attributeName, ICleanUpRegistry registry)
{
final long storageDataTypeId = h5.getDataTypeForAttribute(attributeId, registry);
final long nativeDataTypeId = h5.getNativeDataType(storageDataTypeId, registry);
final int len;
final long enumTypeId;
final int[] arrayDimensions;
if (h5.getClassType(storageDataTypeId) == H5T_ARRAY)
{
arrayDimensions = h5.getArrayDimensions(storageDataTypeId);
len = MDAbstractArray.getLength(arrayDimensions);
enumTypeId = h5.getBaseDataType(storageDataTypeId, registry);
if (h5.getClassType(enumTypeId) != H5T_ENUM)
{
throw new HDF5JavaException("Attribute '" + attributeName + "' of object '"
+ objectPath + "' is not of type enumeration array.");
}
} else
{
if (h5.getClassType(storageDataTypeId) != H5T_ENUM)
{
throw new HDF5JavaException("Attribute '" + attributeName + "' of object '"
+ objectPath + "' is not of type enumeration array.");
}
enumTypeId = storageDataTypeId;
arrayDimensions =
MDAbstractArray.toInt(h5.getDataDimensionsForAttribute(attributeId, registry));
len = MDAbstractArray.getLength(arrayDimensions);
}
final HDF5EnumerationType enumType =
getEnumTypeForEnumDataType(null, enumTypeId, true, fileRegistry);
final byte[] data =
h5.readAttributeAsByteArray(attributeId, nativeDataTypeId, len
* enumType.getStorageForm().getStorageSize());
final HDF5EnumerationValueMDArray value =
new HDF5EnumerationValueMDArray(enumType, EnumerationType.fromStorageForm(data,
arrayDimensions, enumType.getStorageForm()));
return value;
}
long getEnumDataTypeId(final long storageDataTypeId, ICleanUpRegistry registry)
{
final long enumDataTypeId;
if (h5.getClassType(storageDataTypeId) == H5T_ARRAY)
{
enumDataTypeId = h5.getBaseDataType(storageDataTypeId, registry);
} else
{
enumDataTypeId = storageDataTypeId;
}
return enumDataTypeId;
}
HDF5DataTypeVariant[] tryGetTypeVariantForCompoundMembers(String dataTypePathOrNull,
ICleanUpRegistry registry)
{
if (dataTypePathOrNull == null)
{
return null;
}
checkOpen();
final long objectId = h5.openObject(fileId, dataTypePathOrNull, registry);
final String typeVariantMembersAttributeName =
getTypeVariantMembersAttributeName(houseKeepingNameSuffix);
if (h5.existsAttribute(objectId, typeVariantMembersAttributeName) == false)
{
return null;
}
final long attributeId =
h5.openAttribute(objectId, typeVariantMembersAttributeName, registry);
final HDF5EnumerationValueArray valueArray =
getEnumValueArray(attributeId, dataTypePathOrNull, typeVariantMembersAttributeName,
registry);
final HDF5DataTypeVariant[] variants = new HDF5DataTypeVariant[valueArray.getLength()];
boolean hasVariants = false;
for (int i = 0; i < variants.length; ++i)
{
variants[i] = HDF5DataTypeVariant.values()[valueArray.getOrdinal(i)];
hasVariants |= variants[i].isTypeVariant();
}
if (hasVariants)
{
return variants;
} else
{
return null;
}
}
HDF5DataTypeVariant tryGetTypeVariant(final long objectId, ICleanUpRegistry registry)
{
final int typeVariantOrdinal = getAttributeTypeVariant(objectId, registry);
return typeVariantOrdinal < 0 ? null : HDF5DataTypeVariant.values()[typeVariantOrdinal];
}
HDF5DataTypeVariant tryGetTypeVariant(final long objectId, String attributeName,
ICleanUpRegistry registry)
{
final int typeVariantOrdinal = getAttributeTypeVariant(objectId, attributeName, registry);
return typeVariantOrdinal < 0 ? null : HDF5DataTypeVariant.values()[typeVariantOrdinal];
}
/**
* Returns the ordinal for the type variant of <var>objectPath</var>, or <code>-1</code>, if no
* type variant is defined for this <var>objectPath</var>.
*
* @param objectId The id of the data set object in the file.
* @return The ordinal of the type variant or <code>null</code>.
*/
int getAttributeTypeVariant(final long objectId, ICleanUpRegistry registry)
{
checkOpen();
final String dataTypeVariantAttributeName =
createObjectTypeVariantAttributeName(houseKeepingNameSuffix);
if (h5.existsAttribute(objectId, dataTypeVariantAttributeName) == false)
{
return -1;
}
final long attributeId = h5.openAttribute(objectId, dataTypeVariantAttributeName, registry);
return getEnumOrdinal(attributeId, -1, typeVariantDataType);
}
/**
* Returns the ordinal for the type variant of <var>objectPath</var>, or <code>-1</code>, if no
* type variant is defined for this <var>objectPath</var>.
*
* @param objectId The id of the data set object in the file.
* @param attributeName The name of the attribute to get the type variant for.
* @return The ordinal of the type variant or <code>null</code>.
*/
int getAttributeTypeVariant(final long objectId, String attributeName, ICleanUpRegistry registry)
{
checkOpen();
final String typeVariantAttrName =
createAttributeTypeVariantAttributeName(attributeName, houseKeepingNameSuffix);
if (h5.existsAttribute(objectId, typeVariantAttrName) == false)
{
return -1;
}
final long attributeId = h5.openAttribute(objectId, typeVariantAttrName, registry);
return getEnumOrdinal(attributeId, -1, typeVariantDataType);
}
int getEnumOrdinal(final long attributeId, long nativeDataTypeId,
final HDF5EnumerationType enumType)
{
final byte[] data =
h5.readAttributeAsByteArray(attributeId,
(nativeDataTypeId < 0) ? enumType.getNativeTypeId() : nativeDataTypeId,
enumType.getStorageForm().getStorageSize());
return EnumerationType.fromStorageForm(data);
}
/**
* Returns the explicitly saved string length for attribute
* <var>HOUSEKEEPING_NAME_SUFFIX_ATTRIBUTE_NAME</var> of <var>objectId</var>, or <code>-1</code>
* , if no explicit string length is defined for this attribute.
*
* @param objectId The id of the data set object in the file.
* @return The length of the string attribute or -1.
*/
private int getHousekeepingAttributeExplicitStringLength(final long objectId,
ICleanUpRegistry registry)
{
if (h5.existsAttribute(objectId, HOUSEKEEPING_NAME_SUFFIX_STRINGLENGTH_ATTRIBUTE_NAME) == false)
{
return -1;
}
final long attributeId =
h5.openAttribute(objectId, HOUSEKEEPING_NAME_SUFFIX_STRINGLENGTH_ATTRIBUTE_NAME,
registry);
final int[] data = h5.readAttributeAsIntArray(attributeId, H5T_NATIVE_INT32, 1);
return data[0];
}
HDF5DataTypeInformation getDataTypeInformation(final long dataTypeId,
final DataTypeInfoOptions options)
{
final ICallableWithCleanUp<HDF5DataTypeInformation> informationDeterminationRunnable =
new ICallableWithCleanUp<HDF5DataTypeInformation>()
{
@Override
public HDF5DataTypeInformation call(ICleanUpRegistry registry)
{
final HDF5DataTypeInformation dataTypeInfo =
getDataTypeInformation(dataTypeId, options, registry);
// Is it a variable-length string?
final boolean vlString =
(dataTypeInfo.getDataClass() == HDF5DataClass.STRING && h5
.isVariableLengthString(dataTypeId));
if (vlString)
{
dataTypeInfo.setElementSize(-1);
}
return dataTypeInfo;
}
};
return runner.call(informationDeterminationRunnable);
}
HDF5DataTypeInformation getDataTypeInformation(final long dataTypeId,
final DataTypeInfoOptions options, final ICleanUpRegistry registry)
{
final int classTypeId = h5.getClassType(dataTypeId);
final HDF5DataClass dataClass;
final int totalSize = h5.getDataTypeSize(dataTypeId);
if (classTypeId == H5T_ARRAY)
{
dataClass = getElementClassForArrayDataType(dataTypeId);
final int[] arrayDimensions = h5.getArrayDimensions(dataTypeId);
final int numberOfElements = MDAbstractArray.getLength(arrayDimensions);
final int size = totalSize / numberOfElements;
final long baseTypeId = h5.getBaseDataType(dataTypeId, registry);
final boolean signed =
(dataClass == HDF5DataClass.INTEGER) ? h5.getSigned(baseTypeId)
: (dataClass == HDF5DataClass.FLOAT);
final String dataTypePathOrNull =
options.knowsDataTypePath() ? tryGetDataTypePath(baseTypeId) : null;
final CharacterEncoding dataSetEncoding =
(dataClass == HDF5DataClass.STRING) ? h5.getCharacterEncoding(baseTypeId)
: CharacterEncoding.ASCII;
final boolean variableLengthString =
(dataClass == HDF5DataClass.STRING) ? h5.isVariableLengthString(baseTypeId)
: false;
return new HDF5DataTypeInformation(dataTypePathOrNull, options, dataClass,
dataSetEncoding, houseKeepingNameSuffix, size, arrayDimensions, true, signed,
variableLengthString);
} else
{
dataClass = getDataClassForClassType(classTypeId, dataTypeId);
final String opaqueTagOrNull;
if (dataClass == HDF5DataClass.OPAQUE)
{
opaqueTagOrNull = h5.tryGetOpaqueTag(dataTypeId);
} else
{
opaqueTagOrNull = null;
}
final String dataTypePathOrNull =
options.knowsDataTypePath() ? tryGetDataTypePath(dataTypeId) : null;
final boolean signed =
(dataClass == HDF5DataClass.INTEGER) ? h5.getSigned(dataTypeId)
: (dataClass == HDF5DataClass.FLOAT);
final CharacterEncoding dataSetEncoding =
(dataClass == HDF5DataClass.STRING) ? h5.getCharacterEncoding(dataTypeId)
: CharacterEncoding.ASCII;
final boolean variableLengthString =
(dataClass == HDF5DataClass.STRING) ? h5.isVariableLengthString(dataTypeId)
: false;
return new HDF5DataTypeInformation(dataTypePathOrNull, options, dataClass,
dataSetEncoding, houseKeepingNameSuffix, totalSize, signed,
variableLengthString, opaqueTagOrNull);
}
}
private HDF5DataClass getDataClassForClassType(final int classTypeId, final long dataTypeId)
{
HDF5DataClass dataClass = HDF5DataClass.classIdToDataClass(classTypeId);
// Is it a boolean?
if (dataClass == HDF5DataClass.ENUM && h5.dataTypesAreEqual(dataTypeId, booleanDataTypeId))
{
dataClass = HDF5DataClass.BOOLEAN;
}
return dataClass;
}
private HDF5DataClass getElementClassForArrayDataType(final long arrayDataTypeId)
{
for (HDF5DataClass eClass : HDF5DataClass.values())
{
if (h5.hasClassType(arrayDataTypeId, eClass.getId()))
{
return eClass;
}
}
return HDF5DataClass.OTHER;
}
//
// Compound
//
String getCompoundDataTypeName(final String nameOrNull, final long dataTypeId)
{
return getDataTypeName(nameOrNull, HDF5DataClass.COMPOUND, dataTypeId);
}
<T> HDF5ValueObjectByteifyer<T> createCompoundByteifyers(final Class<T> compoundClazz,
final HDF5CompoundMemberMapping[] compoundMembers,
final CompoundTypeInformation compoundTypeInfoOrNull)
{
final HDF5ValueObjectByteifyer<T> objectByteifyer =
new HDF5ValueObjectByteifyer<T>(compoundClazz,
new HDF5ValueObjectByteifyer.IFileAccessProvider()
{
@Override
public long getBooleanDataTypeId()
{
return booleanDataTypeId;
}
@Override
public long getStringDataTypeId(int maxLength)
{
final long typeId =
h5.createDataTypeString(maxLength, fileRegistry);
return typeId;
}
@Override
public long getArrayTypeId(long baseTypeId, int length)
{
final long typeId =
h5.createArrayType(baseTypeId, length, fileRegistry);
return typeId;
}
@Override
public long getArrayTypeId(long baseTypeId, int[] dimensions)
{
final long typeId =
h5.createArrayType(baseTypeId, dimensions, fileRegistry);
return typeId;
}
@Override
public CharacterEncoding getCharacterEncoding(long dataTypeId)
{
return (dataTypeId < 0) ? encodingForNewDataSets : h5
.getCharacterEncoding(dataTypeId);
}
@Override
public HDF5EnumerationType getEnumType(String[] options)
{
final long storageDataTypeId =
h5.createDataTypeEnum(options, fileRegistry);
final long nativeDataTypeId =
h5.getNativeDataType(storageDataTypeId, fileRegistry);
return new HDF5EnumerationType(fileId, storageDataTypeId,
nativeDataTypeId, null, options, HDF5BaseReader.this);
}
@Override
public long getVariableLengthStringDataTypeId()
{
return variableLengthStringDataTypeId;
}
@Override
public byte[] createObjectReference(String referencedObjectPath)
{
return h5.createObjectReference(fileId, referencedObjectPath);
}
}, compoundTypeInfoOrNull, compoundMembers);
return objectByteifyer;
}
long createStorageCompoundDataType(HDF5ValueObjectByteifyer<?> objectArrayifyer)
{
final long storageDataTypeId =
h5.createDataTypeCompound(objectArrayifyer.getRecordSizeOnDisk(), fileRegistry);
objectArrayifyer.insertMemberTypes(storageDataTypeId);
return storageDataTypeId;
}
long createNativeCompoundDataType(HDF5ValueObjectByteifyer<?> objectArrayifyer)
{
final long nativeDataTypeId =
h5.createDataTypeCompound(objectArrayifyer.getRecordSizeInMemory(), fileRegistry);
objectArrayifyer.insertNativeMemberTypes(nativeDataTypeId, h5, fileRegistry);
return nativeDataTypeId;
}
//
// Enum
//
HDF5EnumerationType getEnumTypeForStorageDataType(final String nameOrNull,
final long storageDataTypeId, final boolean resolveName, final String objectPathOrNull,
final String attributeNameOrNull, final ICleanUpRegistry registry)
{
int classType = h5.getClassType(storageDataTypeId);
final boolean isArray = (classType == H5T_ARRAY);
final long enumStoreDataTypeId;
if (isArray)
{
enumStoreDataTypeId = h5.getBaseDataType(storageDataTypeId, registry);
classType = h5.getClassType(enumStoreDataTypeId);
} else
{
enumStoreDataTypeId = storageDataTypeId;
}
if (classType != H5T_ENUM)
{
if (attributeNameOrNull != null)
{
throw new HDF5JavaException("Attribute '" + attributeNameOrNull + "' of object '"
+ objectPathOrNull + "' is not of enum type.");
} else if (objectPathOrNull != null)
{
throw new HDF5JavaException("Object '" + objectPathOrNull
+ "' is not of enum type.");
} else
{
throw new HDF5JavaException("Type '" + (nameOrNull != null ? nameOrNull : "???")
+ "' is not of enum type.");
}
}
return getEnumTypeForEnumDataType(nameOrNull, enumStoreDataTypeId, resolveName, registry);
}
HDF5EnumerationType getEnumTypeForEnumDataType(final String nameOrNull,
final long enumStoreDataTypeId, final boolean resolveName,
final ICleanUpRegistry registry)
{
final long nativeDataTypeId = h5.getNativeDataType(enumStoreDataTypeId, registry);
final String[] values = h5.getNamesForEnumOrCompoundMembers(enumStoreDataTypeId);
return new HDF5EnumerationType(fileId, enumStoreDataTypeId, nativeDataTypeId,
resolveName ? getEnumDataTypeName(nameOrNull, enumStoreDataTypeId) : nameOrNull,
values, this);
}
void checkEnumValues(long dataTypeId, final String[] values, final String nameOrNull)throws HDF5JavaException
{
final String[] valuesStored = h5.getNamesForEnumOrCompoundMembers(dataTypeId);
if (valuesStored.length != values.length)
{
throw new HDF5JavaException("Enum " + getEnumDataTypeName(nameOrNull, dataTypeId)
+ " has " + valuesStored.length + " members, but should have " + values.length);
}
for (int i = 0; i < values.length; ++i)
{
if (values[i].equals(valuesStored[i]) == false)
{
throw new HDF5JavaException("Enum member index " + i + " of enum "
+ getEnumDataTypeName(nameOrNull, dataTypeId) + " is '" + valuesStored[i]
+ "', but should be '" + values[i] + "'");
}
}
}
String getEnumDataTypeName(final String nameOrNull, final long dataTypeId)
{
return getDataTypeName(nameOrNull, HDF5DataClass.ENUM, dataTypeId);
}
private String getDataTypeName(final String nameOrNull, final HDF5DataClass dataClass,
final long dataTypeId)
{
if (nameOrNull != null)
{
return nameOrNull;
} else
{
final String nameFromPathOrNull =
HDF5Utils.tryGetDataTypeNameFromPath(tryGetDataTypePath(dataTypeId),
houseKeepingNameSuffix, dataClass);
return (nameFromPathOrNull == null) ? "UNKNOWN" : nameFromPathOrNull;
}
}
boolean isScaledEnum(final long objectId, final ICleanUpRegistry registry)
{
final HDF5DataTypeVariant typeVariantOrNull = tryGetTypeVariant(objectId, registry);
return (HDF5DataTypeVariant.ENUM == typeVariantOrNull);
}
boolean isScaledBitField(final long objectId, final ICleanUpRegistry registry)
{
final HDF5DataTypeVariant typeVariantOrNull = tryGetTypeVariant(objectId, registry);
return (HDF5DataTypeVariant.BITFIELD == typeVariantOrNull);
}
//
// String
//
private long openOrCreateVLStringType()
{
final String variableLengthStringTypePath =
getVariableLengthStringDataTypePath(houseKeepingNameSuffix);
long dataTypeId = getDataTypeId(variableLengthStringTypePath);
if (dataTypeId < 0)
{
dataTypeId = h5.createDataTypeVariableString(fileRegistry);
commitDataType(variableLengthStringTypePath, dataTypeId);
}
return dataTypeId;
}
String getStringAttribute(final long objectId, final String objectPath,
final String attributeName, final boolean readRaw, final ICleanUpRegistry registry)
{
final long attributeId = h5.openAttribute(objectId, attributeName, registry);
final long stringDataTypeId = h5.getDataTypeForAttribute(attributeId, registry);
final boolean isString = (h5.getClassType(stringDataTypeId) == H5T_STRING);
if (isString == false)
{
throw new IllegalArgumentException("Attribute " + attributeName + " of object "
+ objectPath + " needs to be a String.");
}
final int size = h5.getDataTypeSize(stringDataTypeId);
if (h5.isVariableLengthString(stringDataTypeId))
{
String[] data = new String[1];
h5.readAttributeVL(attributeId, stringDataTypeId, data);
return data[0];
} else
{
final CharacterEncoding dataSetEncoding = h5.getCharacterEncoding(stringDataTypeId);
final byte[] data = h5.readAttributeAsByteArray(attributeId, stringDataTypeId, size);
return (readRaw ? StringUtils.fromBytes(data, dataSetEncoding) : StringUtils
.fromBytes0Term(data, dataSetEncoding));
}
}
String[] getStringArrayAttribute(final long objectId, final String objectPath,
final String attributeName, final boolean readRaw, final ICleanUpRegistry registry)
{
final long attributeId = h5.openAttribute(objectId, attributeName, registry);
final long stringArrayDataTypeId = h5.getDataTypeForAttribute(attributeId, registry);
final boolean isArray = (h5.getClassType(stringArrayDataTypeId) == H5T_ARRAY);
if (isArray == false)
{
throw new HDF5JavaException("Attribute " + attributeName + " of object " + objectPath
+ " needs to be a String array of rank 1.");
}
final long stringDataTypeId = h5.getBaseDataType(stringArrayDataTypeId, registry);
final boolean isStringArray = (h5.getClassType(stringDataTypeId) == H5T_STRING);
if (isStringArray == false)
{
throw new HDF5JavaException("Attribute " + attributeName + " of object " + objectPath
+ " needs to be a String array of rank 1.");
}
final int size = h5.getDataTypeSize(stringArrayDataTypeId);
if (h5.isVariableLengthString(stringDataTypeId))
{
String[] data = new String[1];
h5.readAttributeVL(attributeId, stringDataTypeId, data);
return data;
} else
{
final CharacterEncoding dataSetEncoding =
h5.getCharacterEncoding(stringArrayDataTypeId);
byte[] data = h5.readAttributeAsByteArray(attributeId, stringArrayDataTypeId, size);
final int[] arrayDimensions = h5.getArrayDimensions(stringArrayDataTypeId);
if (arrayDimensions.length != 1)
{
throw new HDF5JavaException("Attribute " + attributeName + " of object "
+ objectPath + " needs to be a String array of rank 1.");
}
final int lengthPerElement = h5.getDataTypeSize(stringDataTypeId);
final int numberOfElements = arrayDimensions[0];
final String[] result = new String[numberOfElements];
for (int i = 0, startIdx = 0, endIdx = lengthPerElement; i < numberOfElements; ++i, startIdx +=
lengthPerElement, endIdx += lengthPerElement)
{
result[i] =
readRaw ? StringUtils.fromBytes(data, startIdx, endIdx, dataSetEncoding)
: StringUtils.fromBytes0Term(data, startIdx, endIdx,
dataSetEncoding);
}
return result;
}
}
MDArray<String> getStringMDArrayAttribute(final long objectId, final String objectPath,
final String attributeName, final boolean readRaw, final ICleanUpRegistry registry)
{
final long attributeId = h5.openAttribute(objectId, attributeName, registry);
final long stringArrayDataTypeId = h5.getDataTypeForAttribute(attributeId, registry);
final boolean isArray = (h5.getClassType(stringArrayDataTypeId) == H5T_ARRAY);
if (isArray == false)
{
throw new HDF5JavaException("Attribute " + attributeName + " of object " + objectPath
+ " needs to be a String array.");
}
final long stringDataTypeId = h5.getBaseDataType(stringArrayDataTypeId, registry);
final boolean isStringArray = (h5.getClassType(stringDataTypeId) == H5T_STRING);
if (isStringArray == false)
{
throw new HDF5JavaException("Attribute " + attributeName + " of object " + objectPath
+ " needs to be a String array.");
}
final int size = h5.getDataTypeSize(stringArrayDataTypeId);
if (h5.isVariableLengthString(stringDataTypeId))
{
String[] data = new String[1];
h5.readAttributeVL(attributeId, stringDataTypeId, data);
return new MDArray<String>(data, new int[]
{ 1 });
} else
{
final byte[] data =
h5.readAttributeAsByteArray(attributeId, stringArrayDataTypeId, size);
final CharacterEncoding dataSetEncoding =
h5.getCharacterEncoding(stringArrayDataTypeId);
final int[] arrayDimensions = h5.getArrayDimensions(stringArrayDataTypeId);
final int lengthPerElement = h5.getDataTypeSize(stringDataTypeId);
final int numberOfElements = MDAbstractArray.getLength(arrayDimensions);
final String[] result = new String[numberOfElements];
for (int i = 0, startIdx = 0, endIdx = lengthPerElement; i < numberOfElements; ++i, startIdx +=
lengthPerElement, endIdx += lengthPerElement)
{
result[i] =
readRaw ? StringUtils.fromBytes(data, startIdx, endIdx, dataSetEncoding)
: StringUtils.fromBytes0Term(data, startIdx, endIdx,
dataSetEncoding);
}
return new MDArray<String>(result, arrayDimensions);
}
}
// Date & Time
void checkIsTimeStamp(final String objectPath, final long dataSetId, ICleanUpRegistry registry)
throws HDF5JavaException
{
final long typeVariantOrdinal = getAttributeTypeVariant(dataSetId, registry);
if (typeVariantOrdinal != HDF5DataTypeVariant.TIMESTAMP_MILLISECONDS_SINCE_START_OF_THE_EPOCH
.ordinal())
{
throw new HDF5JavaException("Data set '" + objectPath + "' is not a time stamp.");
}
}
void checkIsTimeStamp(final String objectPath, final String attributeName, final long dataSetId,
ICleanUpRegistry registry) throws HDF5JavaException
{
final long typeVariantOrdinal = getAttributeTypeVariant(dataSetId, attributeName, registry);
if (typeVariantOrdinal != HDF5DataTypeVariant.TIMESTAMP_MILLISECONDS_SINCE_START_OF_THE_EPOCH
.ordinal())
{
throw new HDF5JavaException("Attribute '" + attributeName + "' of data set '"
+ objectPath + "' is not a time stamp.");
}
}
HDF5TimeUnit checkIsTimeDuration(final String objectPath, final long dataSetId,
ICleanUpRegistry registry) throws HDF5JavaException
{
final int typeVariantOrdinal = getAttributeTypeVariant(dataSetId, registry);
if (HDF5DataTypeVariant.isTimeDuration(typeVariantOrdinal) == false)
{
throw new HDF5JavaException("Data set '" + objectPath + "' is not a time duration.");
}
return HDF5DataTypeVariant.getTimeUnit(typeVariantOrdinal);
}
HDF5TimeUnit checkIsTimeDuration(final String objectPath, final String attributeName,
final long dataSetId, ICleanUpRegistry registry) throws HDF5JavaException
{
final int typeVariantOrdinal = getAttributeTypeVariant(dataSetId, attributeName, registry);
if (HDF5DataTypeVariant.isTimeDuration(typeVariantOrdinal) == false)
{
throw new HDF5JavaException("Attribute '" + attributeName + "' of data set '"
+ objectPath + "' is not a time duration.");
}
return HDF5DataTypeVariant.getTimeUnit(typeVariantOrdinal);
}
}
|