forked from stegro/hdf5oct
-
Notifications
You must be signed in to change notification settings - Fork 1
/
h5read.cc
1698 lines (1528 loc) · 49.1 KB
/
h5read.cc
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
/*
*
* Copyright (C) 2012 Tom Mullins
* Copyright (C) 2015 Tom Mullins, Thorsten Liebig, Anton Starikov, Stefan Großhauser
* Copyright (C) 2008-2013 Andrew Collette
*
* This file is part of hdf5oct.
*
* hdf5oct is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* hdf5oct is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with hdf5oct. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
// integrated into the GNU Octave build
#include "oct.h"
#include "lo-ieee.h"
#else
// as a package
#include <octave/oct.h>
#include <octave/lo-ieee.h>
#endif
#include <cstdlib>
#include <cerrno>
#include <iostream>
#include <algorithm>
#include <string>
#include "gripes.h"
#include "file-stat.h"
using namespace std;
#if ((H5_VERS_MAJOR > 1) || (H5_VERS_MINOR >= 8))
// define this in case there is no configure script at work. This
// should not be necessary any more when integrated into core.
#define HAVE_HDF5_18 1
#endif
#if defined (HAVE_HDF5) && defined (HAVE_HDF5_18)
#include <hdf5.h>
#include "h5read.h"
#include "ls-hdf5.h"
bool
any_int_leq_zero (const Matrix& mat)
{
for (int i = 0; i < mat.numel (); i++)
{
if (mat(i) < 0.5)
return true;
}
return false;
}
// if ALLOW_ZEROS, then Infs will be converted to 0s (used for COUNT)
// else, 0s will produce an error message (used for others)
int
check_vec (const octave_value& val, Matrix& mat/*out*/,
const char *name, bool allow_zeros)
{
mat = val.matrix_value ();
if (error_state)
return 0;
if (! mat.is_vector ())
{
error ("%s must be a vector", name);
return 0;
}
double mind, maxd;
if (allow_zeros)
{
for (int i = 0; i < mat.numel (); i++)
{
if (mat(i) == octave_Inf)
mat(i) = 0;
}
if (! mat.all_integers (mind, maxd) || mat.any_element_is_negative ())
{
error ("%s can only contain non-negative integers", name);
return 0;
}
}
else if (! mat.all_integers (mind, maxd) || any_int_leq_zero (mat))
{
error ("%s can only contain positive integers", name);
return 0;
}
return 1;
}
#endif
DEFUN_DLD (h5read, args, nargout,
"-*- texinfo -*-\n\
@deftypefn {Loadable Function} {@var{data} =} h5read (@var{filename}, @var{dsetname})\n\
@deftypefnx {Loadable Function} {@var{data} =} h5read (@var{filename}, @var{dsetname}, @var{start}, @var{count})\n\
@deftypefnx {Loadable Function} {@var{data} =} h5read (@var{filename}, @var{dsetname}, @var{start}, @var{count}, @var{stride})\n\
@deftypefnx {Loadable Function} {@var{data} =} h5read (@var{filename}, @var{dsetname}, @var{start}, @var{count}, @var{stride}, @var{block})\n\
Read a hyperslab of data from an HDF5 file specified by its @var{filename}. \n\
The datatype will be coerced to double.\n\
For example:\n\
\n\
@example\n\
@group\n\
data = h5read (\"mydata.h5\", \"/grid/time\");\n\
@end group\n\
@end example\n\
\n\
The variable @var{dsetname} is the name of the dataset in the HDF5\n\
file to read. It has to be specified by its absolute path (or relative\n\
to the root group / ).\n\
\n\
The other four arguments are 1xn or nx1 matrices, where n is the number of dimensions\n\
in the dataset. If all are omitted, the entire dataset will be read.\n\
\n\
@var{start} is a 1-based starting offset\n\
@var{count} is the number of blocks to read. If 0 or Inf\n\
is specified in any dimension, as many blocks as possible \n\
are read in that dimension (this is not valid for @code{h5write}).\n\
@var{stride} is the offset between the start of each block. \n\
Defaults to a vector of ones.\n\
@var{block} is the size of each block to read. Defaults to a vector of ones.\n\
\n\
Datasets having a compound type consisting of two double values will\n\
be interpreted as complex valued.\n\
\n\
Generally this function tries to use the Octave datatype of\n\
the appropriate size for the given HDF5 type.\n\
\n\
@seealso{h5write}\n\
@end deftypefn")
{
#if ! (defined (HAVE_HDF5) && defined (HAVE_HDF5_18))
err_disabled_feature ("h5read", "HDF5 IO");
return octave_value ();
#else
int nargin = args.length ();
if (nargin < 2 || nargin == 3 || nargin > 6 || nargout > 1)
{
print_usage ();
return octave_value ();
}
if (! (args(0).is_string () && args (1).is_string ()))
{
print_usage ();
return octave_value ();
}
string filename = args(0).string_value ();
string dsetname = args(1).string_value ();
if (error_state)
return octave_value ();
//open the hdf5 file
H5File file (filename.c_str (), false, false);
if (error_state)
return octave_value ();
if (nargin < 4)
{
octave_value retval = file.read_dset_complete (dsetname.c_str ());
return retval;
}
else
{
Matrix start, count, stride, block;
int err = 0;
err = err || ! check_vec (args(2), start, "START", false);
start -= 1;
err = err || ! check_vec (args(3), count, "COUNT", true);
if (nargin < 5)
stride = Matrix ();
else
err = err || ! check_vec (args(4), stride, "STRIDE", false);
if (nargin < 6)
block = Matrix ();
else
err = err || ! check_vec (args(5), block, "BLOCK", false);
if (err)
return octave_value ();
return file.read_dset_hyperslab (dsetname.c_str (),
start, count, stride, block, nargin-2);
}
#endif
}
DEFUN_DLD (h5readatt, args, nargout,
"-*- texinfo -*-\n\
@deftypefn {Loadable Function} {@var{data} =} h5readatt (@var{filename}, @var{objectname}, @var{attname})\n\
\n\
Reads one attribute of an object from an HDF5 file, specified by the\n\
@var{filename} and the @var{objectname}.\n\
The third argument @var{attname} is the name of the attribute which \n\
is to read.\n\
\n\
@seealso{h5writeatt}\n\
@end deftypefn")
{
octave_value retval;
#if ! (defined (HAVE_HDF5) && defined (HAVE_HDF5_18))
err_disabled_feature ("h5readatt", "HDF5 IO");
return octave_value ();
#else
int nargin = args.length ();
if (nargin != 3)
{
print_usage ();
return retval;
}
if (! (args(0).is_string () && args(1).is_string () && args(2).is_string ()))
{
print_usage ();
return retval;
}
string filename = args(0).string_value ();
string objname = args(1).string_value ();
string attname = args(2).string_value ();
if (error_state)
return octave_value ();
//open the hdf5 file
H5File file (filename.c_str (), false, false);
if (error_state)
return octave_value ();
retval = file.read_att (objname.c_str (), attname.c_str ());
return retval;
#endif
}
DEFUN_DLD (h5write, args, nargout,
"-*- texinfo -*-\n\
@deftypefn {Loadable Function} h5write (@var{filename}, @var{dsetname}, @var{data})\n\
@deftypefnx {Loadable Function} h5write (@var{filename}, @var{dsetname}, @var{data}, @var{start}, @var{count})\n\
@deftypefnx {Loadable Function} h5write (@var{filename}, @var{dsetname}, @var{data}, @var{start}, @var{count}, @var{stride})\n\
@deftypefnx {Loadable Function} h5write (@var{filename}, @var{dsetname}, @var{data}, @var{start}, @var{count}, @var{stride}, @var{block})\n\
\n\
Write a matrix @var{data} to the specified location @var{dsetname} in \n\
a HDF5 file specified by @var{filename}.\n\
\n\
In the simplest form of this function, if the file @var{filename} does\n \
not exist, it will be created and if the dataset @var{dsetname} already\n\
exists, it will be overwritten.\n\
\n\
If more than three arguments are specified, an error will be raised if\n\
the file @var{filename} or the dataset @var{dsetname} do not exist\n\
(use @code{h5create} in order to create an file or an empty dataset).\n \
If the dataset exists, the data will be written into the specified \n\
hyperslab. This way it is possible to append data to existing datasets,\n\
provided their maximum size is sufficiently large.\n\
\n\
@var{start} is a 1-based starting offset\n\
@var{count} is the number of blocks to read. In view of @code{h5read}, note that 0 or Inf\n\
are not valid elements here.\n\
@var{stride} is the offset between the start of each block. \n\
Defaults to a vector of ones.\n\
@var{block} is the size of each block to read. Defaults to a vector of ones.\n\
\n\
Complex valued data will lead to datasets having a compound type consisting\n\
of two double values.\n\
\n\
Generally this function tries to use the HDF5 datatype of\n\
the appropriate size for the given Octave type.\n\
\n\
@seealso{h5read}\n\
@end deftypefn")
{
#if ! (defined (HAVE_HDF5) && defined (HAVE_HDF5_18))
err_disabled_feature ("h5write", "HDF5 IO");
return octave_value ();
#else
int nargin = args.length ();
if (! (nargin == 3 || nargin == 5 || nargin == 6 || nargin == 7) || nargout != 0)
{
print_usage ();
return octave_value ();
}
if (! (args(0).is_string () && args(1).is_string ()))
{
print_usage ();
return octave_value ();
}
string filename = args(0).string_value ();
string location = args(1).string_value ();
if (error_state)
return octave_value ();
if (nargin == 3)
{
//open the hdf5 file, create it if it does not exist
H5File file (filename.c_str (), true, true);
if (error_state)
return octave_value ();
file.write_dset (location.c_str (),
args(2));
}
else
{
//open the hdf5 file, complain if it does not exist
H5File file (filename.c_str (), false, true);
if (error_state)
return octave_value ();
Matrix start, count, stride, block;
int err = 0;
err = err || ! check_vec (args(3), start, "START", false);
start -= 1;
// A count value 0 is not allowed when writing data.
err = err || ! check_vec (args(4), count, "COUNT", false);
if (nargin <= 5)
stride = Matrix ();
else
err = err || ! check_vec (args(5), stride, "STRIDE", false);
if (nargin <= 6)
block = Matrix ();
else
err = err || ! check_vec (args(6), block, "BLOCK", false);
if (err)
return octave_value ();
file.write_dset_hyperslab (location.c_str (),
args(2),
start, count, stride, block, nargin-3);
}
return octave_value ();
#endif
}
DEFUN_DLD (h5writeatt, args, nargout,
"-*- texinfo -*-\n\
@deftypefn {Loadable Function} h5writeatt (@var{filename}, @var{objectname}, @var{attname}, @var{attvalue})\n\
\n\
Write an attribute with name @var{attname} and value @var{attvalue} to\n\
the object named @var{objectname} in the HDF5 file specified by @var{filename}.\n\
\n\
@seealso{h5readatt}\n\
@end deftypefn")
{
#if ! (defined (HAVE_HDF5) && defined (HAVE_HDF5_18))
err_disabled_feature ("h5writeatt", "HDF5 IO");
return octave_value ();
#else
int nargin = args.length ();
if (nargin != 4 || nargout != 0)
{
print_usage ();
return octave_value ();
}
if (! (args(0).is_string () && args(1).is_string () && args(2).is_string ()))
{
print_usage ();
return octave_value ();
}
string filename = args(0).string_value ();
string location = args(1).string_value ();
string attname = args(2).string_value ();
if (error_state)
return octave_value ();
//open the hdf5 file
H5File file (filename.c_str (), false, true);
if (error_state)
return octave_value ();
file.write_att (location.c_str (), attname.c_str (),
args(3));
return octave_value ();
#endif
}
DEFUN_DLD (h5create, args, nargout,
"-*- texinfo -*-\n\
@deftypefn {Loadable Function} h5create (@var{filename}, @var{dsetname}, @var{size}, @var{key}, @var{val},...)\n\
\n\
Create a dataset with name @var{dsetname} and size @var{size} \n\
in the HDF5 file specified by @var{filename}. Intermediate groups\n\
are created as necessary.\n\
\n\
The vector @var{size} may contain one or several Inf (or \n\
equivalently: zero) values.\n\
This will lead to unlimited maximum extent of the dataset in the\n\
respective dimensions and 0 initial extent.\n\
Note that any dataset with at least one unlimited dimension must be chunked and\n\
it is generally recommended for large datasets.\n\
\n\
The list of @var{key}, @var{val} arguments allows to specify\n\
certain properties of the dataset. Allowed settings are:\n\
\n\
@table @asis\n\
@item @option{Datatype}\n\
one of the strings @samp{double} @samp{single} @samp{uint64} @samp{uint32} @samp{uint16} @samp{uint8} @samp{int64} @samp{int32} @samp{int16} @samp{int8} \n\
\n\
@item @option{ChunkSize}\n\
The value may be either a vector specifying the chunk size,\n\
or an empty vector [], which means no chunking (this is the default),\n\
or the string @samp{auto} which makes the library choose automatically \n\
an appropriate chunk size, as best as it can. Note that the @samp{auto}\n\
setting is not @sc{matlab} compatible.\n\
@end table\n\
\n\
@seealso{h5write}\n\
@end deftypefn")
{
#if ! (defined (HAVE_HDF5) && defined (HAVE_HDF5_18))
err_disabled_feature("h5create", "HDF5 IO");
return octave_value ();
#else
int nargin = args.length ();
if (! (nargin == 3 || nargin == 5 || nargin == 7) || nargout != 0)
{
print_usage ();
return octave_value ();
}
if (! (args(0).is_string () && args(1).is_string ()))
{
print_usage ();
return octave_value ();
}
if ((nargin == 5 && ! args(3).is_string ())
|| (nargin == 7 && ! args(5).is_string ()))
{
print_usage ();
return octave_value ();
}
string filename = args(0).string_value ();
string location = args(1).string_value ();
if (error_state)
return octave_value ();
Matrix size;
if (! check_vec (args(2), size, "SIZE", true))
return octave_value ();
// loop over the key-value pairs and see what is given
string datatype = "double";
Matrix chunksize;
for (int i = 3; i+1 < nargin; i+=2)
{
if (args(i).string_value () == "Datatype")
{
datatype = args(i+1).string_value ();
if (error_state)
{
error ("Datatype argument must be a string");
return octave_value ();
}
}
else if (args(i).string_value () == "ChunkSize")
{
if (args(i+1).is_string ())
{
if(args(i+1).string_value () != "auto")
{
error ("ChunkSize argument must be either a vector, or the string 'auto'.");
return octave_value ();
}
chunksize = args(2).matrix_value ();
chunksize(0) = 0;
}
else if (! check_vec (args(i+1), chunksize, "ChunkSize", false))
return octave_value ();
}
else
{
error ("unknown parameter name %s", args(i).string_value ().c_str ());
return octave_value ();
}
}
//open the hdf5 file
H5File file (filename.c_str (), true, true);
if (error_state)
return octave_value ();
file.create_dset (location.c_str (), size, datatype.c_str (), chunksize);
return octave_value ();
#endif
}
DEFUN_DLD (h5delete, args, nargout,
"-*- texinfo -*-\n\
@deftypefn {Loadable Function} h5delete (@var{filename}, @var{objname})\n\
@deftypefnx {Loadable Function} h5delete (@var{filename}, @var{objname}, @var{attname})\n\
\n\
In the first form, delete a dataset or group with name @var{objname}\n\
in the HDF5 file specified by @var{filename}. Note that hdf5 is like a\n\
filesystem: the library does not free the used space when a dataset is\n\
deleted. One can use the tool h5repack afterwards to actually reduce the \n\
filesize.\n \
\n\
In the second form, delete an attribute with name @var{attname} associated\n\
to a dataset or group with name @var{objname}\n\
in the HDF5 file specified by @var{filename}.\n\
\n\
Note that this function is not @sc{matlab} compliant.\n\
\n\
@seealso{h5create}\n\
@end deftypefn")
{
#if ! (defined (HAVE_HDF5) && defined (HAVE_HDF5_18))
err_disabled_feature("h5delete", "HDF5 IO");
return octave_value ();
#else
int nargin = args.length ();
if (! (nargin == 2 || nargin == 3) || nargout != 0)
{
print_usage ();
return octave_value ();
}
if (! (args(0).is_string () && args(1).is_string ()))
{
print_usage ();
return octave_value ();
}
if (nargin == 3 && ! args(2).is_string ())
{
print_usage ();
return octave_value ();
}
string filename = args(0).string_value ();
string location = args(1).string_value ();
if (error_state)
return octave_value ();
//open the hdf5 file
H5File file (filename.c_str (), true, true);
if (error_state)
return octave_value ();
if (nargin == 2)
file.delete_link (location.c_str ());
else if (nargin == 3)
{
string attname = args(2).string_value ();
if(!error_state)
file.delete_att (location.c_str (), attname.c_str ());
}
return octave_value ();
#endif
}
#if defined (HAVE_HDF5) && defined (HAVE_HDF5_18)
H5File::H5File (const char *filename, const bool create_if_nonexisting,
const bool write_access)
{
H5E_auto_t oef;
void *olderr;
H5Eget_auto (H5E_DEFAULT,&oef,&olderr);
//suppress hdf5 error output
H5Eset_auto (H5E_DEFAULT,0,0);
file_stat fs (filename);
if (! fs.exists () && create_if_nonexisting)
file = H5Fcreate (filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
else if (! fs.exists () && ! create_if_nonexisting)
error ("The file %s does not exist: %s", filename, strerror (errno));
else
{
// test if the existing file is in HDF5 format
if (! H5Fis_hdf5 (filename))
error ("The file is not in the HDF5 format, %s: %s", filename, strerror (errno));
else
{
file = H5Fopen (filename,
write_access ? H5F_ACC_RDWR : H5F_ACC_RDONLY,
H5P_DEFAULT);
if (file < 0)
error ("Opening the file failed, %s: %s", filename, strerror (errno));
}
}
// restore old setting
H5Eset_auto (H5E_DEFAULT,oef,olderr);
}
H5File::~H5File ()
{
if (H5Iis_valid (memspace_id))
H5Sclose (memspace_id);
if (H5Iis_valid (dspace_id))
H5Sclose (dspace_id);
if (H5Iis_valid (dset_id))
H5Dclose (dset_id);
if (H5Iis_valid (att_id))
H5Aclose (att_id);
if (H5Iis_valid (obj_id))
H5Oclose (obj_id);
if (H5Iis_valid (type_id))
H5Tclose (type_id);
if (H5Iis_valid (mem_type_id))
H5Tclose (mem_type_id);
if (H5Iis_valid (file))
H5Fclose (file);
if (h5_dims != NULL)
{
free (h5_dims);
h5_dims = NULL;
}
if (h5_maxdims != NULL)
{
free (h5_maxdims);
h5_maxdims = NULL;
}
}
// T will be Matrix or dim_vector
template <typename T>
hsize_t*
H5File::alloc_hsize (const T& dim, const int mode, const bool reverse)
{
int rank = dim.numel ();
hsize_t *hsize = (hsize_t*)malloc (rank * sizeof (hsize_t));
for (int i = 0; i < rank; i++)
{
int j = reverse ? rank-i-1 : i;
if (mode == ALLOC_HSIZE_INFZERO_TO_UNLIMITED && (dim(i) == octave_Inf || dim(i) == 0))
hsize[j] = H5S_UNLIMITED;
else if (mode == ALLOC_HSIZE_INF_TO_ZERO && dim(i) == octave_Inf)
hsize[j] = 0;
else
hsize[j] = dim(i);
}
return hsize;
}
int
H5File::open_dset (const char *dsetname)
{
dset_id = H5Dopen (file, dsetname, H5P_DEFAULT);
if (dset_id < 0)
{
error ("Error opening dataset %s", dsetname);
return -1;
}
dspace_id = H5Dget_space (dset_id);
if (dspace_id < 0)
{
error ("Error opening dataspace of dataset %s", dsetname);
return -1;
}
rank = H5Sget_simple_extent_ndims (dspace_id);
if (rank < 0)
{
error ("Error reading extent of %s", dsetname);
return -1;
}
h5_dims = (hsize_t*)malloc (rank * sizeof(hsize_t));
h5_maxdims = (hsize_t*)malloc (rank * sizeof(hsize_t));
if (! h5_dims || ! h5_maxdims)
{
error ("Error allocating memory for %s", dsetname);
return -1;
}
if (H5Sget_simple_extent_dims (dspace_id, h5_dims, h5_maxdims) < 0)
{
error ("Error determining current dimensions and maximum size of dataset %s", dsetname);
return -1;
}
return 0;
}
octave_value
H5File::read_dset_complete (const char *dsetname)
{
if (open_dset (dsetname) < 0)
return octave_value ();
mat_dims.resize (max (rank, 2));
// .resize(1) still leaves mat_dims with a length of 2, so
// we need at least 2 filled
mat_dims(0) = mat_dims(1) = 1;
for (int i = 0; i < rank; i++)
//note that this is reversing the order
mat_dims(i) = h5_dims[rank-i-1];
if (H5Sselect_all (dspace_id) < 0)
{
error ("Error selecting complete dataset %s", dsetname);
return octave_value ();
}
octave_value retval = read_dset ();
return retval;
}
octave_value
H5File::read_dset_hyperslab (const char *dsetname,
const Matrix& start, const Matrix& count,
const Matrix& stride, const Matrix& block,
int nargin)
{
if (open_dset (dsetname) < 0)
return octave_value ();
if (rank == 0 && ! (start.is_empty () && count.is_empty ()
&& stride.is_empty () && block.is_empty ()))
{
error ("Cannot specify hyperslab for scalar datasets (rank 0)");
return octave_value ();
}
if (start.numel () != rank)
{
error ("start must be a vector of length %d, the dataset rank", rank);
return octave_value ();
}
if (count.numel () != rank)
{
error ("count must be a vector of length %d, the dataset rank", rank);
return octave_value ();
}
Matrix _stride = stride;
if (nargin < 3)
_stride = Matrix (dim_vector(1, rank), 1);
if (_stride.numel () != rank)
{
error ("stride must be a vector of length %d, the dataset rank", rank);
return octave_value ();
}
Matrix _block = block;
if (nargin < 4)
_block = Matrix (dim_vector(1, rank), 1);
if (_block.numel () != rank)
{
error ("block must be a vector of length %d, the dataset rank", rank);
return octave_value ();
}
// .resize(1) still leaves mat_dims with a length of 2, so
// we need at least 2 filled
mat_dims.resize (max (rank, 2));
mat_dims(0) = mat_dims(1) = 1;
Matrix _count = count;
for (int i = 0; i < rank; i++)
{
if (_stride(i) < _block(i))
{
error ("In dimension %d, requested stride %d smaller than block size %d",
i+1, (int)_stride(i), (int)_block(i));
return octave_value ();
}
if (_count(i) == 0)
{
// a value of 0 (or Inf) means that as many blocks as possible
// shall be read in this dimension
_count(i) = (h5_dims[rank-i-1] - start(i) - _block(i)) / _stride(i) + 1;
}
mat_dims(i) = _count(i)*_block(i);
int end = start(i) + _stride(i)*(_count(i)-1) + _block(i); // exclusive
if (h5_dims[rank-i-1] < end)
{
error ("In dimension %d, dataset only has %d elements, but at least %d"
" are required for requested hyperslab", i+1, (int)h5_dims[rank-i-1],
end);
return octave_value ();
}
}
hsize_t *hstart = alloc_hsize (start, ALLOC_HSIZE_DEFAULT, true);
if(hstart == NULL)
{
error ("error when allocating hstart array, for %s", dsetname);
return octave_value ();
}
hsize_t *hstride = alloc_hsize (_stride, ALLOC_HSIZE_DEFAULT, true);
if(hstride == NULL)
{
error ("error when allocating hstride array, for %s", dsetname);
return octave_value ();
}
hsize_t *hcount = alloc_hsize (_count, ALLOC_HSIZE_DEFAULT, true);
if(hcount == NULL)
{
error ("error when allocating hcount array, for %s", dsetname);
return octave_value ();
}
hsize_t *hblock = alloc_hsize (_block, ALLOC_HSIZE_DEFAULT, true);
if(hblock == NULL)
{
error ("error when allocating hblock array, for %s", dsetname);
return octave_value ();
}
herr_t sel_result = H5Sselect_hyperslab (dspace_id, H5S_SELECT_SET, hstart,
hstride, hcount, hblock);
free (hstart);
free (hstride);
free (hcount);
free (hblock);
if (sel_result < 0)
{
error ("error when selecting the hyperslab of dataset %s to read from", dsetname);
return octave_value ();
}
octave_value retval = read_dset ();
return retval;
}
octave_value
H5File::read_dset ()
{
bool is_cmplx = false;
type_id = H5Dget_type (dset_id);
hid_t complex_type_id = hdf5_make_complex_type (H5T_NATIVE_DOUBLE);
hsize_t *hmem = alloc_hsize (mat_dims, ALLOC_HSIZE_DEFAULT, false);
hid_t memspace_id = H5Screate_simple (rank, hmem, hmem);
free (hmem);
if (memspace_id < 0)
{
return octave_value ();
}
if (H5Sselect_valid (dspace_id) <= 0)
{
error ("selected dataspace is not valid");
return octave_value ();
}
octave_value retval;
herr_t read_result;
if (H5Tget_class (type_id) == H5T_COMPOUND &&
H5Tget_class (complex_type_id) == H5T_COMPOUND &&
hdf5_types_compatible (type_id, complex_type_id) > 0)
{
ComplexNDArray ret (mat_dims);
// macro begin
#define HDF5_READ_DATA(type) \
read_result = H5Dread (dset_id, \
type, \
memspace_id, dspace_id, \
H5P_DEFAULT, ret.fortran_vec ()); \
if (read_result < 0) \
{ \
error ("error when reading dataset"); \
return octave_value (); \
} \
retval = octave_value (ret)
// macro end
HDF5_READ_DATA (type_id);
}
else if (H5Tget_class (type_id) == H5T_INTEGER)
{
switch (H5Tget_size (type_id)*8)
{
case 64:
if (H5Tget_sign (type_id) == H5T_SGN_NONE)
{
uint64NDArray ret (mat_dims);
HDF5_READ_DATA (type_id);
break;
}
else
{
int64NDArray ret (mat_dims);
HDF5_READ_DATA (type_id);
break;
}
break;
case 32:
if (H5Tget_sign (type_id) == H5T_SGN_NONE)
{
uint32NDArray ret (mat_dims);
HDF5_READ_DATA (type_id);
break;
}
else
{
int32NDArray ret (mat_dims);
HDF5_READ_DATA (type_id);
break;
}
break;
case 16:
if (H5Tget_sign (type_id) == H5T_SGN_NONE)
{
uint16NDArray ret (mat_dims);
HDF5_READ_DATA (type_id);
break;
}
else
{
int16NDArray ret (mat_dims);
HDF5_READ_DATA (type_id);
break;
}
break;
case 8:
if (H5Tget_sign (type_id) == H5T_SGN_NONE)
{
uint8NDArray ret (mat_dims);
HDF5_READ_DATA (type_id);
break;
}
else
{
int8NDArray ret (mat_dims);
HDF5_READ_DATA (type_id);
break;
}
break;
default:
{
error ("unknown integer size %d", H5Tget_size (type_id));
NDArray ret (mat_dims);
HDF5_READ_DATA (type_id);
}
}
}
else
{
NDArray ret (mat_dims);
HDF5_READ_DATA (H5T_NATIVE_DOUBLE);
}
H5Tclose (complex_type_id);
return retval;
}
void
H5File::write_dset (const char *dsetname,
const octave_value ov_data)
{
int rank = ov_data.dims ().numel ();
hsize_t *dims = alloc_hsize (ov_data.dims(), ALLOC_HSIZE_DEFAULT, true);
dspace_id = H5Screate_simple (rank, dims, NULL);
free (dims);
// determine the endianness of this system
H5T_order_t o = H5Tget_order (H5T_NATIVE_INT);
if (o == H5T_ORDER_ERROR)