-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhdf5.i
1581 lines (1332 loc) · 49.3 KB
/
hdf5.i
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
HDF5_VERSION = "0.8.0";
/* HDF5 Yorick plugin
*
* $Id: hdf5.i,v 1.6 2010-04-15 02:46:29 frigaut Exp $
*
* Francois Rigaut, February 2005
* Created: 24 February 2005
* last revision/addition:
*
* $Log: hdf5.i,v $
* Revision 1.6 2010-04-15 02:46:29 frigaut
*
* updated to v0.8.0
*
* 2009/09/16:
* - Version 0.8.0
* - I just realized today there was an issue with the new HDF5 APIs.
* added dynamic declaration of H5 static variable (from their value
* in the HDF5 include files).
* - Also, writting attributes with string value was to be broken (see
* note in h5awrite. Fixed by setting last param of H5Acreate to 0.
*
* Revision 1.5 2008/11/21 19:00:54 frigaut
* - added some text to man page
* - added warning mechanism through h5v062bug_warning() function.
* - version 0.7.1
*
* Revision 1.4 2008/11/21 17:29:47 frigaut
* - added some support for reading 64bits longs in a 32 bits OS
*
* Revision 1.3 2008/11/21 16:19:17 frigaut
* - added h5old2new to convert pre-v0.6.2 files to post v0.6.2 file formats
* - added h5convert and h5info shell wrappers
* - added h5convert_fromshell.i and h5scan_fromshell.i to go with it.
* - added h5convert.1 man page (but not yet h5info.1)
* - updated Makefile to include these new files for the install and
* make package
* - bumped version to 0.7.0
*
* Revision 1.2 2008/11/13 21:19:44 frigaut
* - Fixed swapped array dimension and messed up array shape
* as reported by David Strozzi.
*
* Revision 1.1.1.1 2007/12/27 15:10:25 frigaut
* Initial Import - yorick-hdf5
*
* 11 dec 2005, v0.6
* many hours of work on this plugin.
* - the whole h5open/h5close is now much more robust.
* - fixed many occurences where things were left open
* - added 2 new functions: h5version, h5list_open
*
* 8 nov 2005
* fixed issue when reading large array of strings in hdf5.c
*
* 25 may 2005
* fixed a bug in creating multiple levels groups
*
* 24 february 2005
* initial revision
*
*
* Copyright (c) 2005, Francois RIGAUT (frigaut@gemini.edu, Gemini
* Observatory, 670 N A'Ohoku Place, HILO HI-96720).
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program 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
* General Public License for more details (to receive a copy of the GNU
* General Public License, write to the Free Software Foundation, Inc., 675
* Mass Ave, Cambridge, MA 02139, USA).
*/
plug_in, "hdf5";
local hdf5;
/* DOCUMENT HDF5 plugin: Simple HIERARCHICAL DATA FORMAT 5 wrappers.
DATA I/O:
h5read(fname,target) return data
h5write,fname,fullpath,data,zip=,mode= write data
h5open(filename,mode) return file handle
h5close(filename) close file
INFO, GROUP LINKING:
h5info,fname,target,att= print out file info/structure
h5link(fname,group,group2link2,linktype) link datasets
h5delete,fname,object delete data
ATTRIBUTE I/O:
h5awrite(fname,object,aname,attdata) write an object attribute
h5aread(fname,object,aname) read an object attribute
h5adelete,fname,object,aname delete an object attribute
MISC:
h5list_open list open files
h5version return linhdf5 version
ERROR RECOVERY
h5off close all reference to the
h5 library (clean up).
+ Many of the atomic HDF5 functions are available (e.g. H5Fopen, H5Dopen)
with mostly the same APIs. Many simple tasks can be done with the
provided high level wrappers. Beware that programming an HDF5 custom
wrapper is not trivial. Make sure you close every objects opened!
This implementation supports reading of most of the HDF5 supported
datatype. Only yorick datatype can be used for writes. Compression, soft
and hard links, as well as support for attribute read/writes is provided.
There is no support for hyperslabs, compound, enum or opaque datatype.
Generally, there is very little support for datatype related
functionalities.
EXAMPLES:
1. simplest:
h5write,"sinus.h5","/sin2t","sin(2*indgen(100));
2. ex2: save 2 vectors
fd1=h5open("sinus.h5","w"); // "w" start from scratch
t = span(0.,2*pi,100);
h5write,fd1,"/data/t",t;
h5write,fd1,"/data/sin2t",sin(2*t);
h5close,fd1;
3. ex3: append another vector and attribute
h5write,"sinus.h5","/data/damped sin",sin(t)*exp(-0.7*t),mode="a";
h5awrite,"sinus.h5","/data/damped sin", \
"functional form","sin(t)*exp(-0.7t)";
4. examine the content of the file
h5info,"sinus.h5";
NOTE: HDF5_SAFE is a global flag that, if set, will prevent to open
an existing file in mode "w". In this mode, if the file has indeed
to be overwritten, it needs to be deleted prior to the call to
h5open.
SEE ALSO:
*/
//::::::::::::::::::::::::::::::::::::
HDF5_SAFE=0;
// if set to 1, will prevent opening file that
// already exist in mode "w". This can prevent
// accidental deletion of important data, however
// it can be a pain.
//::::::::::::::::::::::::::::::::::::
struct _hdf5info { string name, objectType; pointer aname; string target; };
struct _hdf5file_struct { string fname; long fd; long nref; string curmode; };
//::::::::::::::::::::::::::::::::::::
hdf5_varname_maxcar = 36; // length of variable name printout. can be changed.
// DO NOT change this:
plugin_version_string="Generated by yorick HDF5 plugin version";
local h5convert;
/* DOCUMENT
This is a shell command. The help is provided here for convenience.
NAME
h5convert - Conversion of files written with hdf5 yorick plugin pre-
v0.6.2 to "fixed" format of v0.6.2 and higher
SYNOPSIS
h5convert [-p -v# -f -h] files
DESCRIPTION
h5convert is a shell utility to convert files that were generated with
the HDF5 yorick plugin v<0.6.2 to the new, fixed format used in
v>=0.6.2.
-p perform in-place conversion (output file name = input file name)
-f force conversion of files generated with version>0.6.2
-v# verbose: -v0:silent -v1:files -v2:files and contents
-h Print out short help
AUTHOR
Francois Rigaut, Gemini Observatory
BUGS
This is a patch for a previous bug. Beware: the flag "-p" will rename
the converted file to the original name when done. Beware! This should
be safe, but I haven't tested all possible scenario (i.e. objects
type). This will erase your original data. I encourage you to check
this conversion function on a few files before using this option.
In particular, do not try to convert files generated by external HDF
resources, as they will be converted while they should not!
Also, these files may include data type and object type not yet
implemented in the plugin, in which case it will trigger an error
and the file may result corrupted. You have been warned.
SEE ALSO:
*/
func h5info(file,target,att=,silent=,_recur=)
/* DOCUMENT h5info,filename,target_group,att=
Prints out the content of a HDF5 file (groups, datasets and links)
filename: file name (string) or file descriptor (long)
target_group: the root group at which to start scanning (default "/")
att=1: print out objects attributes (to group and datasets only)
SEE ALSO: h5read, h5write, h5link, h5delete
*/
{
extern objnovec,objnoname,objectInfo;
// initialize some variable at top of recursion
if (!_recur) {
objnovec=[];
objnoname=[];
objectInfo = [];
}
if (!target) target="/"; //default start group
// Open file
if (structof(file)==string) {
has2bclose=1;
file = h5open(file,"r");
}
// Open group
gid = _H5Gopen(file,target);
if (gid<0) {
if (has2bclose) h5close,file;
error,"Unable to find group (h5info target has to be a group)";
}
if (!_recur) { // process the root target group
grow,objectInfo,_hdf5info();
objectInfo(0).name = target;
objectInfo(0).objectType =" GROUP";
if (!silent) \
write,format="%-"+swrite(format="%d",hdf5_varname_maxcar)+"s",target;
if (!silent) write,format=" %-8s\n","GROUP";
if (att) {
natt = _H5Aget_num_attrs(gid);
tmp=[];
for (na=0;na<natt;na++) {
attid = _H5Aopen_idx(gid,na);
str = array(" ",128)(sum);
status= _H5Aget_name(attid,128,str);
grow,tmp,str;
if (!silent) \
write,format=array(" ",hdf5_varname_maxcar+1)(sum)+ \
"ATTRIB(%d): %-30s\n",na,strpart(str,1:30);
status= _H5Aclose(attid);
}
objectInfo(0).aname=&tmp;
}
}
if (target=="/") target="";
nobj = _H5Gget_num_objs(gid);
if (!nobj) return; // empty group
for (i=0;i<nobj;i++) { // loop on group objects
grow,objectInfo,_hdf5info();
name = array(" ",128)(sum);
size = _H5Gget_objname_by_idx(gid,i,name, 128n );
fullname = target+"/"+name;
dispname = fullname;
if (strlen(fullname)>hdf5_varname_maxcar) dispname="..."+
strpart(fullname,-(hdf5_varname_maxcar-4):0);
if (!silent) \
write,format="%-"+swrite(format="%d",hdf5_varname_maxcar)+"s",dispname;
objno=[0,0];
otype = _H5Gget_objtype_by_name(gid,fullname,objno);
objectInfo(0).name=fullname;
if (otype==H5G_GROUP) {
// object is other group. we will enter recursion.
if ((objnovec!=[])&&anyof((objno==objnovec)(sum,)==2)) {
w = where((objno==objnovec)(sum,)==2)(1);
link2 = objnoname(w);
if (!silent) write,format=" %s -> %s\n","HARDLINK",link2;
objectInfo(0).objectType="HARDLINK";
objectInfo(0).target=link2;
} else {
if (!silent) write,format=" %-8s\n","GROUP";
objectInfo(0).objectType="GROUP";
if (att) {
gid2 = _H5Gopen(file,fullname);
natt = _H5Aget_num_attrs(gid2);
tmp=[];
for (na=0;na<natt;na++) {
attid = _H5Aopen_idx(gid2,na);
str = array(" ",128)(sum);
status= _H5Aget_name(attid,128,str);
grow,tmp,str;
if (!silent) \
write,format=array(" ",hdf5_varname_maxcar+1)(sum)+\
"ATTRIB(%d): %-30s\n",na,strpart(str,1:30);
status= _H5Aclose(attid);
}
objectInfo(0).aname=&tmp;
status = _H5Gclose(gid2);
}
grow,objnovec,[objno];
grow,objnoname,fullname;
// call oneself one group down
h5info,file,target+"/"+name,att=att,silent=silent,_recur=1;
}
} else if (otype==H5G_DATASET) {
// object is dataset
if (!silent) write,format=" %-8s ","DATASET";
objectInfo(0).objectType="DATASET";
dataset = _H5Dopen(file,target+"/"+name);
// get dataspace id:
dspid = _H5Dget_space(dataset);
//get rank and dimensions:
rank = _H5Sget_simple_extent_ndims(dspid);
if (rank) {
dims = maxdims = array(long,rank);
status = _H5Sget_simple_extent_dims(dspid,dims,maxdims);
dims = dims(::-1); // dim bug fix
}
// get type:
yt=yotype(_H5Dget_type(dataset));
if (yt != -1) {
if (!silent) write,format=" %-6s ",strcase(1,typeof(yt));
} else {
if (!silent) write,format=" %-6s ","UNKNOWN";
}
if (rank) {
if (!silent) write,format=" DIMSOF()=[%d",rank;
for (j=1;j<=rank;j++) if (!silent) write,format=",%d",dims(j);
if (!silent) write,format="]%s\n","";
} else {
if (!silent) write,format=" %sSCALAR\n","";
}
if (att) {
natt = _H5Aget_num_attrs(dataset);
tmp=[];
for (na=0;na<natt;na++) {
attid = _H5Aopen_idx(dataset,na);
str = array(" ",128)(sum);
status= _H5Aget_name(attid,128,str);
grow,tmp,str;
if (!silent) write,format=array(" ",hdf5_varname_maxcar+1)(sum)+\
"ATTRIB(%d): %-30s\n",na,
strpart(str,1:30);
status= _H5Aclose(attid);
}
objectInfo(0).aname=&tmp;
}
status=_H5Dclose(dataset);
} else if (otype==H5G_LINK) {
link2 = array(" ",128)(sum);
status=_H5Gget_linkval(gid, fullname, 128l, link2 );
if (!silent) write,format=" %s -> %s\n","SOFTLINK",link2;
objectInfo(0).objectType="SOFTLINK";
objectInfo(0).target=link2;
} else if (otype==H5G_TYPE) {
if (!silent) write,format=" %s\n","DADATYPE";
objectInfo(0).objectType="DATATYPE";
} else {
if (!silent) write,format=" %s\n","UNKNOWN OBJECT";
objectInfo(0).objectType="UNKNOWN OBJECT";
}
}
status=_H5Gclose(gid);
if (has2bclose) h5close,file;
// because of recursive aspect of function:
// if(!_recur) _H5close;
if (!_recur) return objectInfo;
}
//::::::::::::::::::::::::::::::::::::
func h5open(fname,mode)
/* DOCUMENT func h5open(fname,mode)
Open file for write, append or read access.
filename: file name
mode: access mode ("r"=read-only; "w"=write; "a"=append)
Returns a file handle that can be used in further calls to HDF5
functions (e.g. h5read, h5write).
If HDF5_SAFE=1, try to open in mode "w" an existing file will
produce an error ( to prevent accidentaly overwritting the data).
Delete the file prior to the h5open call if this is the case.
SEE ALSO: h5close, h5list_open
*/
{
extern _hdf5file;
if (!fname||(fname=="")) error,"h5open takes at least one argument";
if (!mode) mode="a";
if (noneof(mode==["r","a","w"])) {
error,"mode should be either \"r\",\"a\" or \"w\"";
}
// the following is to ensure we are not opening the file twice.
// if the call was as a function, then we return the fd for the
// already opened file.
// in all case, we increment the # of open reference so that
// when we open twice, we have to close twice (each open must
// have its corresponding close
// this will deal with case as:
// 1. we explicitely open the file
// 2. we call a function that open the file automatically
// 3. this function will close the file, but
// 4. the file must remain open as requested in (1), thus
// has to be close by an explicit, balanced call to h5close
// There is a catch to that: if the file is open in read-only,
// we can't open it again in write or append, or vice-versa.
if (_hdf5file!=[]) {
if (anyof(_hdf5file.fname==fname)) {
w = where(_hdf5file.fname==fname);
if (numberof(w)) w=w(1);
// error: file already open for reading
if (((_hdf5file(w).curmode)=="r")&&(mode=="w")) {
error,swrite(format="%s currently open for read-only",fname);
}
// error: file already open for writing
if (((_hdf5file(w).curmode)=="w")&&(mode=="r")) {
error,swrite(format="%s currently open for write-only",fname);
}
(_hdf5file(w).nref)++;
return (_hdf5file(w).fd);
}
}
// below we parse the input "fname" into a path and a file name.
tok = strtok(fname,"/",20);
tok = tok(where(tok));
if (numberof(tok)>1) {
path = sum(tok(1:-1)+"/");
if (strpart(fname,1:1)=="/") path = "/"+path;
path = strpart(path,1:-1);
} else path=".";
name=tok(0);
// in case mode="r" or "a", file should already exist. check it.
ctn = lsdir(path);
if (anyof(mode==["a","r"])) {
if (noneof(ctn==name))
error,swrite(format="File does not exist (mode=\"%s\")",mode);
} else { // mode "w"
if ((HDF5_SAFE)&&(anyof(ctn==name)))
error,swrite(format="File already exist (mode=\"%s\") and HDF5_SAFE=1",
mode);
}
// Open file
if (mode=="r") {
file=_H5Fopen(fname,H5F_ACC_RDONLY,0);
if (file<0) {
error,"Unable to open file (already open?)";
}
} else {
// does not exist, have to create:
if (noneof(ctn==name)) mode="w";
if (mode=="a") { // open existing
file = _H5Fopen(fname,H5F_ACC_RDWR,0);
if (file<0) {
error,"Unable to open file (already open?)";
}
} else { // create:
flags = H5F_ACC_TRUNC;
file=_H5Fcreate(fname,H5F_ACC_TRUNC,0,0);
if (file<0) {
error,"Unable to create file (already open?)";
}
}
}
// success. we can add the fname to the open file list:
grow,_hdf5file,_hdf5file_struct(fname=fname,fd=file);
w = where(_hdf5file.fname==fname)(1);
_hdf5file(w).nref=1;
_hdf5file(w).curmode=mode;
return file;
}
//::::::::::::::::::::::::::::::::::::
func h5close(files)
/* DOCUMENT func h5close(files)
Close h5 access to file(s) (after a h5open)
files: scalar or vector of file descriptor or file names
Called without argument, this function closes all opened h5 files.
*/
{
extern _hdf5file;
if (files==[]) {
// close all open files
if (_hdf5file==[]) {
write,format="%s\n","No file open";
return 0;
}
// select all files fd:
files = _hdf5file.fd;
// force nref to 1 so that close will happen
_hdf5file.nref = _hdf5file.nref*0+1;
}
for (i=1;i<=numberof(files);i++) { // loop on input files
if (!files(i)) continue; // could be 0
if (_hdf5file==[]) {
write,format="%s\n","No file open";
return 0;
}
if (structof(files(i))==string) {
// user passed file name, not file descriptor
w = where(_hdf5file.fname==files(i));
wn = where(_hdf5file.fname!=files(i));
if (numberof(w)==0)
error,swrite(format="%s does not exist. Can not close.",files(i));
fd = _hdf5file(w(1)).fd;
} else {
// user passed FD
w = where(_hdf5file.fd==files(i));
wn = where(_hdf5file.fd!=files(i));
if (numberof(w)==0)
error,swrite(format="File descriptor (%d) does not correspond"+
" to any open file. Can not close",files(i));
fd = files(i);
}
// see comment in h5open. If files has been opened several times,
// we don't close it until the # opened reference is == 1
w=where(_hdf5file.fd==files(i))(1);
if ((_hdf5file(w).nref)(1)>1) {
(_hdf5file(w).nref)--;
continue;
}
status = _H5Fclose(fd);
if (status) error,swrite(format="Error closing file %s\n",_hdf5file(w).fname);
// now suppress entry in _hdf5file:
if (numberof(wn)==0) _hdf5file=[];
else _hdf5file=_hdf5file(wn);
}
return 0;
}
//::::::::::::::::::::::::::::::::::::
func h5list_open(void)
/* DOCUMENT func h5list_open(void)
Print (or return) a lit of open h5 files.
SEE ALSO: h5open, h5close
*/
{
extern _hdf5file;
if (_hdf5file==[]) {
write,format="%s\n","No file open";
return;
}
if (am_subroutine()) {
write,format="%-20s mode \"%s\" (%d ref)\n",_hdf5file.fname,
_hdf5file.curmode,_hdf5file.nref;
} else {
return _hdf5file.fname;
}
}
//::::::::::::::::::::::::::::::::::::
func h5read(file,target,pre062=)
/* DOCUMENT data=h5read(file,target,pre062=)
Read content of one dataset in a HDF5 file and return the data
file: h5 file name (string) or h5 file id (output from h5open)
target: dataset name (string)
pre062: set this keyword to read files generated by this plugin
version pre-0.6.2 (hdf5 libraries array dimensions issue)
This will read all data type but datatype will be casted to one
of the yorick datatype (char,short,int,long,float,double,string).
SEE ALSO: H5write, h5info, h5link, h5delete
*/
{
if (structof(file)==string) {
has2bclose=1;
file = h5open(file,"r");
}
// Open dataset
dataset=_H5Dopen(file,target);
if (dataset<0) {
if (has2bclose) h5close,file;
return;
//error,"Unable to find Dataset";
}
// get dataspace id:
dspid=_H5Dget_space(dataset);
//get rank:
rank = _H5Sget_simple_extent_ndims(dspid);
if (rank) {
dims = maxdims = array(long,rank);
status = _H5Sget_simple_extent_dims(dspid,dims,maxdims);
if (!pre062) dims = dims(::-1); // dim bug fix
} else {dims=0;}
ytype=yotype(_H5Dget_type(dataset),h5type);
if (ytype==-1) error,"Unknown Datatype";
data = array(ytype,_(rank,dims));
if (structof(ytype)==string) {
nelem = numberof(data);
data=_H5Dreads(dataset,data,nelem);
} else {
status=_H5Dread(dataset,h5type,0,0,0,&data);
}
status=_H5Dclose(dataset);
if (has2bclose) {h5close,file;}
return data;
}
//::::::::::::::::::::::::::::::::::::
func h5write(file,fullpath,data,zip=,mode=,noheader=)
/* DOCUMENT h5write,file,fullpath,data,zip=,mode=,noheader=
Write data in a HDF5 file.
fname: file name or file handle (from h5open)
fullpath: full path to the dataset (string), including
hierarchy (ex: "/g1/data"). If parent group(s)
do not exist, they will be created. This function
will refuse to overwrite an existing dataset.
To do so, use delete the dataset with h5delete
prior to the h5write.
data: data. Any yorick valid data. Scalar or arrays.
Any yorick type is accepted (from char to double,
and strings). Strings are stored in H5T_VARIABLE,
which is a variable length type in the HDF5
specification.
zip=N will use compression (N=0-9). Larger N will
compress more (but take longer).
mode= "w"=write-only (erase previous content, default) or "a"=append
Warning: If file is a string, then mode="w" is used,
which means an existing file will be overwritten.
To update an existing file, use a file handle
with the h5open/h5write/h5close combination.
SEE ALSO: h5read, h5info, h5link, h5delete
*/
{
if (!mode) mode="w";
tmp = strpart(fullpath,strword(fullpath,"/",20));
tmp = tmp(where(tmp));
dataname = tmp(0);
if (numberof(tmp)>1) {
group = ("/"+tmp(1:-1))(sum);
} else group="/";
// Open/Create FILE
if (structof(file)==string) {
has2bclose=1;
// default to overwrite when using the simple
// filename option
file = h5open(file,mode);
}
if ((group!="/")&&(strpart(group,0:0)=="/")) group=strpart(group,1:-1);
// Open/Create GROUP
gid = _H5Gopen(file,group);
if (gid<0) { //group does not exist, create:
g = strpart(group,strword(group,"/",20));
g = g(where(g));
g = "/"+g;
for (i=1;i<=numberof(g);i++) {
gid = _H5Gopen(file,g(1:i)(sum));
if (gid<0) {
gid = _H5Gcreate(file,g(1:i)(sum),0);
status=_H5Gclose(gid);
}
if (gid<0) {
if (has2bclose) h5close,file;
error,"Unable to create group";
}
status=_H5Gclose(gid);
}
gid = _H5Gopen(file,group);
}
if (data==[]) { // just create a group
gid = _H5Gopen(file,fullpath);
if (gid<0) {
gid = _H5Gcreate(file,fullpath,0);
status=_H5Gclose(gid);
}
if (gid<0) {
if (has2bclose) h5close,file;
error,"Unable to create group";
}
swrite=0;
} else { // create dataset and write data
// Determine dimsof() data
rank = dimsof(data)(1);
if (rank>0) {
dims = dimsof(data)(2:);
dims = dims(::-1); // dim bug fix
}
if (rank==0) {
dataspace = _H5Screate(H5S_SCALAR);
} else {
dataspace=_H5Screate_simple(rank,dims);
if (dataspace<0) {
status=_H5Gclose(gid);
if (has2bclose) h5close,file;
error,"Unable to create dataspace";
}
}
// Handle compression
if ((zip!=[])&&(rank)) {
plist = _H5Pcreate(H5P_DATASET_CREATE);
rank = dimsof(data)(1);
cdims = min(dimsof(data)(2:),20);
status = _H5Pset_chunk(plist, 2, cdims);
status = _H5Pset_deflate( plist, zip);
} else plist=H5P_DEFAULT;
if (structof(data)==char) htype=H5T_NATIVE_CHAR;
if (structof(data)==short) htype=H5T_NATIVE_SHORT;
if (structof(data)==int) htype=H5T_NATIVE_INT;
if (structof(data)==long) htype=H5T_NATIVE_LONG;
if (structof(data)==float) htype=H5T_NATIVE_FLOAT;
if (structof(data)==double) htype=H5T_NATIVE_DOUBLE;
if (structof(data)==string) {
/* create a datatype for the text */
htype = _H5Tcopy(H5T_C_S1);
/* set the total size for the datatype */
status = _H5Tset_size (htype,H5T_VARIABLE);
plist = _H5Pcreate(H5P_DATASET_CREATE);
}
// Create dataset
dataset=_H5Dcreate(file,group+"/"+dataname,htype,dataspace,plist);
if (dataset<0) {
status=_H5Sclose(dataspace);
status=_H5Gclose(gid);
if (has2bclose) h5close,file;
error,"Unable to create dataset (already exist?)";
}
// and finally, write data...
swrite=_H5Dwrite(dataset,htype,0,0,0,&data);
status=_H5Sclose(dataspace);
status=_H5Dclose(dataset);
}
status=_H5Gclose(gid);
if (has2bclose) h5close,file;
if (swrite<0) error,"Unable to write data";
// write the yorick plugin version as an attribute of "/"
h5awrite,file,"/",plugin_version_string,HDF5_VERSION,try=1;
}
//::::::::::::::::::::::::::::::::::::
func h5adelete(file,object,aname)
/* DOCUMENT h5adelete,file,object,aname
Delete attribute "aname" in object "object" in HDF5 file "file"
SEE ALSO: h5aread, h5awrite
*/
{
// Open FILE
if (structof(file)==string) {
has2bclose=1;
file = h5open(file,"a");
}
oid=_H5Dopen(file,object);
if (oid<0) { // may be a group?
oid=_H5Gopen(file,object);
isgroup=1;
if (oid<0) {
if (has2bclose) h5close,file;
error,swrite(format="Unable to find object %s\n",object);
}
}
if (structof(aname)==string) {
// attribute name passed as a string
// check attribute exist:
attid = _H5Aopen_name(oid,aname);
if (attid<0) {
if (isgroup) status=_H5Gclose(oid);
else status=_H5Dclose(oid);
if (has2bclose) h5close,file;
error,swrite(format="No such attribute \"%s\" in %s\n",aname,object);
}
// we can (have to) close it
status = _H5Aclose(attid);
} else if ((structof(aname)==int)||(structof(aname)==long)) {
// attribute name passed as an index
attid = _H5Aopen_idx(oid,aname);
if (attid<0) {
if (isgroup) status=_H5Gclose(oid);
else status=_H5Dclose(oid);
if (has2bclose) h5close,file;
error,swrite(format="No such attribute #%d in %s\n",aname,object);
}
// if it exist, then get name:
aname = array(" ",129)(sum);
status= _H5Aget_name(attid,128,aname);
} else {
if (isgroup) status=_H5Gclose(oid);
else status=_H5Dclose(oid);
if (has2bclose) h5close,file;
error,"Unknow attribute name type";
}
status = _H5Adelete(oid,aname);
if (isgroup) status=_H5Gclose(oid);
else status=_H5Dclose(oid);
if (has2bclose) h5close,file;
}
//::::::::::::::::::::::::::::::::::::
func h5aread(file,object,aname,pre062=)
/* DOCUMENT attrib = h5aread(file,object,aname,pre062=)
Read the value of an object attribute in a HDF5 file
file: file name (string) or file handle (from h5open)
object: object name (type GROUP or DATASET) (string)
aname: attribute name (string) or id (int).
pre062: set this keyword to read files generated by this plugin
version pre-0.6.2 (hdf5 libraries array dimensions issue)
If aname=[], then all attributes of the given object are read out
and returned.
SEE ALSO: h5awrite, h5adelete
*/
{
// Open FILE
if (structof(file)==string) {
has2bclose=1;
file = h5open(file,"r");
}
// Open object
oid=_H5Dopen(file,object);
if (oid<0) { // may be a group?
oid=_H5Gopen(file,object);
isgroup=1;
if (oid<0) {
if (has2bclose) h5close,file;
error,swrite(format="Unable to find object %s\n",object);
}
}
if (structof(aname)==string) {
attid = _H5Aopen_name(oid,aname);
if (attid<0) {
if (isgroup) status=_H5Gclose(oid);
else status=_H5Dclose(oid);
if (has2bclose) h5close,file;
error,swrite(format="No such attribute \"%s\" in %s\n",aname,object);
}
} else if ((structof(aname)==int)||(structof(aname)==long)) {
attid = _H5Aopen_idx(oid,aname);
if (attid<0) {
if (isgroup) status=_H5Gclose(oid);
else status=_H5Dclose(oid);
if (has2bclose) h5close,file;
error,swrite(format="No such attribute #%d in %s\n",aname,object);
}
str = array(" ",128)(sum);
status= _H5Aget_name(attid,128,str);
//write,format="Retrieving Attribute %s\n",str;
} else {
if (isgroup) status=_H5Gclose(oid);
else status=_H5Dclose(oid);
if (has2bclose) h5close,file;
error,"Unknow attribute name type";
}
dspid = _H5Aget_space(attid);
rank = _H5Sget_simple_extent_ndims(dspid);
if (rank) {
dims = maxdims = array(long,rank);
status = _H5Sget_simple_extent_dims(dspid,dims,maxdims);
if (!pre062) dims = dims(::-1); // dim bug fix
} else {dims=0;}
ytype=yotype(_H5Aget_type(attid),h5type);
if (ytype==-1) error,"Unknow attribute data type";
data = array(ytype,_(rank,dims));
if (structof(ytype)==string) {
nelem = numberof(data);
data=_H5Areads(attid,data,nelem);
} else {
status=_H5Aread(attid,h5type,&data);
}
status=_H5Aclose(attid);
if (isgroup) status=_H5Gclose(oid);
else status=_H5Dclose(oid);
if (has2bclose) h5close,file;
if (numberof(data)==1) data=data(1);
return data;
}
//::::::::::::::::::::::::::::::::::::
func h5awrite(file,object,aname,attdata,try=)
/* DOCUMENT h5awrite,file,object,aname,attdata
Write an object attribute in a HDF5 file, attached to
a group or a dataset.
file: file name (string) or file handle (from h5open)
object: object name (type GROUP or DATASET) (string)
aname: attribute name (string)
attdata: attribute value (data). Any yorick type. Can be scalar
or an array. HDF5 limits length to about 1000 elements.
try: if set, no errors are triggered, but the return status
is set with the error number:
0: no error
1: Unable to find object
2: Unable to create attribute dataspace
3: Unable to create attribute (already exist?)
SEE ALSO: h5aread, h5adelete
*/
{
// Open FILE
if (structof(file)==string) {
has2bclose=1;
file = h5open(file,"a");
}
// open object
oid=_H5Dopen(file,object);