Repository navigation
Expand file tree
/
Copy pathDatatype.java
More file actions
613 lines (556 loc) · 18.5 KB
/
Copy pathDatatype.java
File metadata and controls
613 lines (556 loc) · 18.5 KB
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
/*****************************************************************************
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of the HDF Java Products distribution. *
* The full copyright notice, including terms governing use, modification, *
* and redistribution, is contained in the files COPYING and Copyright.html. *
* COPYING can be found at the root of the source code distribution tree. *
* Or, see http://hdfgroup.org/products/hdf-java/doc/Copyright.html. *
* If you do not have access to either file, you may request a copy from *
* help@hdfgroup.org. *
****************************************************************************/
package ncsa.hdf.object;
import java.util.List;
/**
* Datatype is an abstract class that defines datatype characteristics and APIs
* for a data type.
* <p>
* A datatype has four basic characteristics: class, size, byte order and sign.
* These charactertics are defeined in the <a
* href="http://hdfgroup.org/HDF5/doc/UG/index.html">HDF5 User's Guide</a>.
* <p>
* These charactertics apply to all the sub-classes. The sub-classes may have
* different ways to describe a datatype. We here define the <strong> native
* datatype</strong> to the datatype used by the sub-class. For example,
* H5Datatype uses a datatype identifier (hid_t) to specify a datatype.
* NC2Datatype uses ucar.nc2.DataType object to describe its datatype. "Native"
* here is different from the "native" definition in the HDF5 library.
* <p>
* Two functions, toNative() and fromNative(), are defined to convert the
* general charactertics to/form the native datatype. Sub-classes must implement
* these functions so that the conversion will be done correctly.
* The values of the CLASS member are not identical to HDF5 values for a datatype class
* <p>
*
* @version 1.1 9/4/2007
* @author Peter X. Cao
*/
public abstract class Datatype extends HObject {
/**
*
*/
private static final long serialVersionUID = -581324710549963177L;
private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(Datatype.class);
/**
* The default definition for datatype size, order, and sign.
*/
public static final int NATIVE = -1;
/**
* See {@link <a href="http://hdfgroup.org/HDF5/doc/UG/index.html">HDF5
* User's Guide</a>}
*/
public static final int CLASS_NO_CLASS = -1;
/**
* See {@link <a href="http://hdfgroup.org/HDF5/doc/UG/index.html">HDF5
* User's Guide</a>}
*/
public static final int CLASS_INTEGER = 0;
/**
* See {@link <a href="http://hdfgroup.org/HDF5/doc/UG/index.html">HDF5
* User's Guide</a>}
*/
public static final int CLASS_FLOAT = 1;
/**
* See {@link <a href="http://hdfgroup.org/HDF5/doc/UG/index.html">HDF5
* User's Guide</a>}
*/
public static final int CLASS_CHAR = 2;
/**
* See {@link <a href="http://hdfgroup.org/HDF5/doc/UG/index.html">HDF5
* User's Guide</a>}
*/
public static final int CLASS_STRING = 3;
/**
* See {@link <a href="http://hdfgroup.org/HDF5/doc/UG/index.html">HDF5
* User's Guide</a>}
*/
public static final int CLASS_BITFIELD = 4;
/**
* See {@link <a href="http://hdfgroup.org/HDF5/doc/UG/index.html">HDF5
* User's Guide</a>}
*/
public static final int CLASS_OPAQUE = 5;
/**
* See {@link <a href="http://hdfgroup.org/HDF5/doc/UG/index.html">HDF5
* User's Guide</a>}
*/
public static final int CLASS_COMPOUND = 6;
/**
* See {@link <a href="http://hdfgroup.org/HDF5/doc/UG/index.html">HDF5
* User's Guide</a>}
*/
public static final int CLASS_REFERENCE = 7;
/**
* See {@link <a href="http://hdfgroup.org/HDF5/doc/UG/index.html">HDF5
* User's Guide</a>}
*/
public static final int CLASS_ENUM = 8;
/**
* See {@link <a href="http://hdfgroup.org/HDF5/doc/UG/index.html">HDF5
* User's Guide</a>}
*/
public static final int CLASS_VLEN = 9;
/**
* See {@link <a href="http://hdfgroup.org/HDF5/doc/UG/index.html">HDF5
* User's Guide</a>}
*/
public static final int CLASS_ARRAY = 10;
/**
* See {@link <a href="http://hdfgroup.org/HDF5/doc/UG/index.html">HDF5
* User's Guide</a>}
*/
public static final int CLASS_TIME = 11;
/**
* See {@link <a href="http://hdfgroup.org/HDF5/doc/UG/index.html">HDF5
* User's Guide</a>}
*/
public static final int ORDER_LE = 0;
/**
* See {@link <a href="http://hdfgroup.org/HDF5/doc/UG/index.html">HDF5
* User's Guide</a>}
*/
public static final int ORDER_BE = 1;
/**
* See {@link <a href="http://hdfgroup.org/HDF5/doc/UG/index.html">HDF5
* User's Guide</a>}
*/
public static final int ORDER_VAX = 2;
/**
* See {@link <a href="http://hdfgroup.org/HDF5/doc/UG/index.html">HDF5
* User's Guide</a>}
*/
public static final int ORDER_NONE = 3;
// sign
/**
* See {@link <a href="http://hdfgroup.org/HDF5/doc/UG/index.html">HDF5
* User's Guide</a>}
*/
public static final int SIGN_NONE = 0;
/**
* See {@link <a href="http://hdfgroup.org/HDF5/doc/UG/index.html">HDF5
* User's Guide</a>}
*/
public static final int SIGN_2 = 1;
/**
* See {@link <a href="http://hdfgroup.org/HDF5/doc/UG/index.html">HDF5
* User's Guide</a>}
*/
public static final int NSGN = 2;
/**
* The class of the datatype.
*/
protected int datatypeClass;
/**
* The size (in bytes) of the datatype.
*/
protected int datatypeSize;
/**
* The byte order of the datatype. Valid values are ORDER_LE, ORDER_BE, and
* ORDER_VAX.
*/
protected int datatypeOrder;
/**
* The sign of the datatype.
*/
protected int datatypeSign;
/**
* The (name, value) pairs of enum members
*/
protected String enumMembers;
/**
* The base datatype of every element of the array (for CLASS_ARRAY
* datatype).
*/
protected Datatype baseType;
/*
* The dimension of the ARRAY element. For ARRAY datatype only
*/
protected long[] dims;
/**
* Constructs a named datatype with a given file, name and path.
* <p>
*
* @param theFile
* the HDF file.
* @param name
* the name of the datatype, e.g "12-bit Integer".
* @param path
* the full group path of the datatype, e.g. "/datatypes/".
*/
public Datatype(FileFormat theFile, String name, String path) {
this(theFile, name, path, null);
}
/**
* @deprecated Not for public use in the future.<br>
* Using {@link #Datatype(FileFormat, String, String)}
*/
@Deprecated
public Datatype(FileFormat theFile, String name, String path, long[] oid) {
super(theFile, name, path, oid);
}
/**
* Constructs a Datatype with specified class, size, byte order and sign.
* <p>
* The following is a list of a few example of H5Datatype.
* <OL>
* <LI>to create unsigned native integer<br>
* H5Datatype type = new H5Dataype(CLASS_INTEGER, NATIVE, NATIVE,
* SIGN_NONE);
* <LI>to create 16-bit signed integer with big endian<br>
* H5Datatype type = new H5Dataype(CLASS_INTEGER, 2, ORDER_BE, NATIVE);
* <LI>to create native float<br>
* H5Datatype type = new H5Dataype(CLASS_FLOAT, NATIVE, NATIVE, -1);
* <LI>to create 64-bit double<br>
* H5Datatype type = new H5Dataype(CLASS_FLOAT, 8, NATIVE, -1);
* </OL>
*
* @param tclass
* the class of the datatype, e.g. CLASS_INTEGER, CLASS_FLOAT and
* etc.
* @param tsize
* the size of the datatype in bytes, e.g. for a 32-bit integer,
* the size is 4.
* @param torder
* the byte order of the datatype. Valid values are ORDER_LE,
* ORDER_BE, ORDER_VAX and ORDER_NONE
* @param tsign
* the sign of the datatype. Valid values are SIGN_NONE, SIGN_2
* and MSGN
*/
public Datatype(int tclass, int tsize, int torder, int tsign) {
this(tclass, tsize, torder, tsign, null);
}
/**
* Constructs a Datatype with specified class, size, byte order and sign.
* <p>
* The following is a list of a few example of H5Datatype.
* <OL>
* <LI>to create unsigned native integer<br>
* H5Datatype type = new H5Dataype(CLASS_INTEGER, NATIVE, NATIVE,
* SIGN_NONE);
* <LI>to create 16-bit signed integer with big endian<br>
* H5Datatype type = new H5Dataype(CLASS_INTEGER, 2, ORDER_BE, NATIVE);
* <LI>to create native float<br>
* H5Datatype type = new H5Dataype(CLASS_FLOAT, NATIVE, NATIVE, -1);
* <LI>to create 64-bit double<br>
* H5Datatype type = new H5Dataype(CLASS_FLOAT, 8, NATIVE, -1);
* </OL>
*
* @param tclass
* the class of the datatype, e.g. CLASS_INTEGER, CLASS_FLOAT and
* etc.
* @param tsize
* the size of the datatype in bytes, e.g. for a 32-bit integer,
* the size is 4.
* @param torder
* the byte order of the datatype. Valid values are ORDER_LE,
* ORDER_BE, ORDER_VAX and ORDER_NONE
* @param tsign
* the sign of the datatype. Valid values are SIGN_NONE, SIGN_2
* and MSGN
* @param tbase
* the base datatype of the new datatype
*/
public Datatype(int tclass, int tsize, int torder, int tsign, Datatype tbase) {
datatypeClass = tclass;
datatypeSize = tsize;
datatypeOrder = torder;
datatypeSign = tsign;
enumMembers = null;
baseType = tbase;
dims = null;
log.trace("datatypeClass={} datatypeSize={} datatypeOrder={} datatypeSign={} baseType={}", datatypeClass, datatypeSize, datatypeOrder, datatypeSign, baseType);
}
/**
* Constructs a Datatype with a given native datatype identifier.
* <p>
* For example, if the datatype identifier is a 32-bit unsigned integer
* created from HDF5,
*
* <pre>
* int tid = H5.H5Tcopy(HDF5Constants.H5T_NATIVE_UNINT32);
* Datatype dtype = new Datatype(tid);
* </pre>
*
* will construct a datatype equivalent to new Datatype(CLASS_INTEGER, 4,
* NATIVE, SIGN_NONE);
* <p>
*
* @see #fromNative(int nativeID)
* @param type
* the native datatype identifier.
*/
public Datatype(int type) {
this(CLASS_NO_CLASS, NATIVE, NATIVE, NATIVE);
}
/**
* Returns the class of the datatype. Valid values are:
* <ul>
* <li>CLASS_NO_CLASS
* <li>CLASS_INTEGER
* <li>CLASS_FLOAT
* <li>CLASS_CHAR
* <li>CLASS_STRING
* <li>CLASS_BITFIELD
* <li>CLASS_OPAQUE
* <li>CLASS_COMPOUND
* <li>CLASS_REFERENCE
* <li>CLASS_ENUM
* <li>CLASS_VLEN
* <li>CLASS_ARRAY
* </ul>
*
* @return the class of the datatype.
*/
public int getDatatypeClass() {
return datatypeClass;
}
/**
* Returns the size of the datatype in bytes. For example, for a 32-bit
* integer, the size is 4 (bytes).
*
* @return the size of the datatype.
*/
public int getDatatypeSize() {
return datatypeSize;
}
/**
* Returns the byte order of the datatype. Valid values are
* <ul>
* <li>ORDER_LE
* <li>ORDER_BE
* <li>ORDER_VAX
* <li>ORDER_NONE
* </ul>
*
* @return the byte order of the datatype.
*/
public int getDatatypeOrder() {
return datatypeOrder;
}
/**
* Returns the sign (SIGN_NONE, SIGN_2 or NSGN) of an integer datatype.
*
* @return the sign of the datatype.
*/
public int getDatatypeSign() {
return datatypeSign;
}
/**
* Returns the datatype of array element for ARRAY datatype.
* <p>
* For example, a dataset set of ARRAY of integer, The datatype of the
* dataset is ARRAY. The datatype of the base type is integer.
*
* @return the the datatype of array element for ARRAY datatype.
*/
public Datatype getBasetype() {
return baseType;
}
/**
* Sets the (name, value) pairs of enum members for enum datatype.
* <p>
* For Example,
* <dl>
* <dt>setEnumMembers("lowTemp=-40, highTemp=90")</dt>
* <dd>sets the value of enum member lowTemp to -40 and highTemp to 90.</dd>
* <dt>setEnumMembers("lowTemp, highTemp")</dt>
* <dd>sets enum members to defaults, i.e. lowTemp=0 and highTemp=1</dd>
* <dt>setEnumMembers("lowTemp=10, highTemp")</dt>
* <dd>sets enum member lowTemp to 10 and highTemp to 11.</dd>
* </dl>
*
* @param enumStr
* the (name, value) pairs of enum members
*/
public final void setEnumMembers(String enumStr) {
enumMembers = enumStr;
}
/**
* Returns the "name=value" pairs of enum members for enum datatype.
* <p>
* For Example,
* <dl>
* <dt>setEnumMembers("lowTemp=-40, highTemp=90")</dt>
* <dd>sets the value of enum member lowTemp to -40 and highTemp to 90.</dd>
* <dt>setEnumMembers("lowTemp, highTemp")</dt>
* <dd>sets enum members to defaults, i.e. lowTemp=0 and highTemp=1</dd>
* <dt>setEnumMembers("lowTemp=10, highTemp")</dt>
* <dd>sets enum member lowTemp to 10 and highTemp to 11.</dd>
* </dl>
*
* @return enumStr the (name, value) pairs of enum members
*/
public final String getEnumMembers() {
return enumMembers;
}
/**
* Converts the datatype object to a native datatype.
*
* Subclasses must implement it so that this datatype will be converted
* accordingly. Use close() to close the native identifier; otherwise, the
* datatype will be left open.
* <p>
* For example, a HDF5 datatype created from<br>
*
* <pre>
* H5Dataype dtype = new H5Datatype(CLASS_INTEGER, 4, NATIVE, SIGN_NONE);
* int tid = dtype.toNative();
* </pre>
*
* There "tid" will be the HDF5 datatype id of a 32-bit unsigned integer,
* which is equivalent to
*
* <pre>
* int tid = H5.H5Tcopy(HDF5Constants.H5T_NATIVE_UNINT32);
* </pre>
*
* @return the identifier of the native datatype.
*/
public abstract int toNative();
/**
* Set datatype characteristics (class, size, byte order and sign) from a
* given datatye identifier.
* <p>
* Sub-classes must implement it so that this datatype will be converted
* accordingly.
* <p>
* For example, if the type identifier is a 32-bit unsigned integer created
* from HDF5,
*
* <pre>
* H5Datatype dtype = new H5Datatype();
* dtype.fromNative(HDF5Constants.H5T_NATIVE_UNINT32);
* </pre>
*
* Where dtype is equivalent to <br>
* new H5Datatype(CLASS_INTEGER, 4, NATIVE, SIGN_NONE);
* <p>
*
* @param nativeID
* the datatype identifier.
*/
public abstract void fromNative(int nativeID);
/**
* Returns a short text description of this datatype.
*
* @return a short text description of this datatype
*/
public String getDatatypeDescription() {
String description = "Unknown";
switch (datatypeClass) {
case CLASS_INTEGER:
if (datatypeSign == SIGN_NONE) {
description = String.valueOf(datatypeSize * 8)
+ "-bit unsigned integer";
}
else {
description = String.valueOf(datatypeSize * 8) + "-bit integer";
}
break;
case CLASS_FLOAT:
description = String.valueOf(datatypeSize * 8)
+ "-bit floating-point";
break;
case CLASS_STRING:
description = "String";
break;
case CLASS_REFERENCE:
description = "Object reference";
break;
case CLASS_BITFIELD:
description = "Bitfield";
break;
case CLASS_ENUM:
description = String.valueOf(datatypeSize * 8) + "-bit enum";
break;
case CLASS_ARRAY:
description = "Array";
break;
case CLASS_COMPOUND:
description = "Compound ";
break;
case CLASS_VLEN:
description = "Variable-length";
break;
default:
description = "Unknown";
break;
}
log.trace("description={}", description);
return description;
}
/**
* Checks if this datatype is an unsigned integer.
*
* @return true if the datatype is an unsigned integer; otherwise, returns
* false.
*/
public abstract boolean isUnsigned();
/**
* Opens access to this named datatype. Sub-clases must replace this default
* implementation. For example, in H5Datatype, open() function
* H5.H5Topen(loc_id, name) to get the datatype identifier.
*
* @return the datatype identifier if successful; otherwise returns negative
* value.
*/
@Override
public int open() {
return -1;
}
/**
* Closes a datatype identifier.
* <p>
* Sub-clases must replace this default implementation.
*
* @param id
* the datatype identifier to close.
*/
@Override
public abstract void close(int id);
/*
* (non-Javadoc)
*
* @see ncsa.hdf.object.DataFormat#getMetadata()
*/
public List getMetadata() throws Exception {
return null;
}
/*
* (non-Javadoc)
*
* @see ncsa.hdf.object.DataFormat#writeMetadata(java.lang.Object)
*/
public void writeMetadata(Object info) throws Exception {
;
}
/*
* (non-Javadoc)
*
* @see ncsa.hdf.object.DataFormat#removeMetadata(java.lang.Object)
*/
public void removeMetadata(Object info) throws Exception {
;
}
/*
* (non-Javadoc)
*
* @see ncsa.hdf.object.DataFormat#updateMetadata(java.lang.Object)
*/
public void updateMetadata(Object info) throws Exception {
;
}
}