-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathnapi5.c
2716 lines (2416 loc) · 67.8 KB
/
napi5.c
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
/*---------------------------------------------------------------------------
NeXus - Neutron & X-ray Common Data Format
Application Program Interface (HDF5) Routines
Copyright (C) 1997-2014 NIAC
This library 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 2 of the License, or (at your option) any later version.
This library 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 this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
For further information, see <http://www.nexusformat.org>
----------------------------------------------------------------------------*/
#define H5Aiterate_vers 2
#include <nxconfig.h>
#ifdef WITH_HDF5
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <time.h>
#include "napi.h"
#include "napi_internal.h"
#include "napi5.h"
#ifdef H5_VERSION_GE
#if !H5_VERSION_GE(1,8,0)
#error HDF5 Version must be 1.8.0 or higher
#endif
#endif
#ifdef _MSC_VER
#define snprintf _snprintf
#endif /* _MSC_VER */
#define NX_UNKNOWN_GROUP "" /* for when no NX_class attr */
extern void *NXpData;
typedef struct __NexusFile5 {
struct iStack5 {
char irefn[1024];
hid_t iVref;
hsize_t iCurrentIDX;
} iStack5[NXMAXSTACK];
struct iStack5 iAtt5;
hid_t iFID;
hid_t iCurrentG;
hid_t iCurrentD;
hid_t iCurrentS;
hid_t iCurrentT;
hid_t iCurrentA;
int iNX;
int iNXID;
int iStackPtr;
char *iCurrentLGG;
char *iCurrentLD;
char name_ref[1024];
char name_tmp[1024];
char iAccess[2];
} NexusFile5, *pNexusFile5;
/* forward declaration of NX5closegroup in order to get rid of a nasty warning */
NXstatus NX5closegroup(NXhandle fid);
/*-------------------------------------------------------------------*/
static pNexusFile5 NXI5assert(NXhandle fid)
{
pNexusFile5 pRes;
assert(fid != NULL);
pRes = (pNexusFile5) fid;
assert(pRes->iNXID == NX5SIGNATURE);
return pRes;
}
/*--------------------------------------------------------------------*/
static void NXI5KillDir(pNexusFile5 self)
{
self->iStack5[self->iStackPtr].iCurrentIDX = 0;
}
static herr_t readStringAttribute(hid_t attr, char **data)
{
herr_t iRet = 0;
hid_t atype = -1;
hid_t space;
int ndims;
hsize_t thedims[H5S_MAX_RANK], sdim;
atype = H5Aget_type(attr);
sdim = H5Tget_size(atype);
space = H5Aget_space(attr);
ndims = H5Sget_simple_extent_dims(space, thedims, NULL);
if (ndims == 0) {
if (H5Tis_variable_str(atype)) {
hid_t btype = H5Tget_native_type(atype, H5T_DIR_ASCEND);
iRet = H5Aread(attr, btype, data);
H5Tclose(btype);
} else {
*data = malloc(sdim+1);
iRet = H5Aread(attr, atype, *data);
(*data)[sdim] = '\0';
}
} else if (ndims == 1) {
int i;
char **strings;
strings = (char **) malloc(thedims[0] * sizeof(char*));
if (! H5Tis_variable_str(atype)) {
strings[0] = (char *) malloc(thedims[0] * sdim * sizeof(char));
for(i=1; i<thedims[0]; i++) {
strings[i] = strings[0] + i * sdim;
}
}
iRet = H5Aread(attr, atype, strings[0]);
*data = calloc((sdim + 2) * thedims[0], sizeof(char));
for(i=0; i<thedims[0]; i++) {
if (i==0) {
strncpy(*data, strings[i], sdim);
} else {
strcat(*data, ", ");
strncat(*data, strings[i], sdim);
}
}
if (H5Tis_variable_str(atype)) {
H5Dvlen_reclaim(atype, space, H5P_DEFAULT, strings);
} else {
free(strings[0]);
}
free(strings);
} else {
*data = strdup(" higher dimensional string array");
}
H5Tclose(atype);
H5Sclose(space);
if (iRet < 0)
return NX_ERROR;
return NX_OK;
}
static herr_t readStringAttributeN(hid_t attr, char *data, int maxlen)
{
herr_t iRet;
char *vdat = NULL;
iRet = readStringAttribute(attr, &vdat);
if (iRet >= 0) {
strncpy(data, vdat, maxlen);
free(vdat);
}
data[maxlen - 1] = '\0';
return iRet;
}
/*--------------------------------------------------------------------*/
static void NXI5KillAttDir(pNexusFile5 self)
{
self->iAtt5.iCurrentIDX = 0;
}
/*---------------------------------------------------------------------*/
static void buildCurrentPath(pNexusFile5 self, char *pathBuffer,
int pathBufferLen)
{
memset(pathBuffer, 0, pathBufferLen);
if (self->iCurrentG != 0) {
strcpy(pathBuffer, "/");
if ((int)strlen(self->name_ref) + 1 < pathBufferLen) {
strcat(pathBuffer, self->name_ref);
}
}
if (self->iCurrentD != 0) {
strcat(pathBuffer, "/");
if ((int)strlen(self->iCurrentLD) + (int)strlen(pathBuffer) <
pathBufferLen) {
strcat(pathBuffer, self->iCurrentLD);
}
}
}
/* ----------------------------------------------------------------------
Definition of NeXus API
--------------------------------------------------------------------- */
NXstatus NX5reopen(NXhandle pOrigHandle, NXhandle * pNewHandle)
{
pNexusFile5 pNew = NULL, pOrig = NULL;
*pNewHandle = NULL;
pOrig = (pNexusFile5) pOrigHandle;
pNew = (pNexusFile5) malloc(sizeof(NexusFile5));
if (!pNew) {
NXReportError("ERROR: no memory to create File datastructure");
return NX_ERROR;
}
memset(pNew, 0, sizeof(NexusFile5));
pNew->iFID = H5Freopen(pOrig->iFID);
if (pNew->iFID <= 0) {
NXReportError("cannot clone file");
free(pNew);
return NX_ERROR;
}
strcpy(pNew->iAccess, pOrig->iAccess);
pNew->iNXID = NX5SIGNATURE;
pNew->iStack5[0].iVref = 0; /* root! */
*pNewHandle = (NXhandle) pNew;
return NX_OK;
}
/*---------------------------------------------------------------------
* private functions used in NX5open
*/
pNexusFile5 create_file_struct()
{
pNexusFile5 pNew = (pNexusFile5) malloc(sizeof(NexusFile5));
if (!pNew) {
NXReportError
("ERROR: not enough memory to create file structure");
}
else{
memset(pNew, 0, sizeof(NexusFile5));
}
return pNew;
}
hid_t create_file_access_plist(CONSTCHAR *filename)
{
char pBuffer[512];
hid_t fapl = -1;
/* create file access property list - required in all cases*/
if((fapl = H5Pcreate(H5P_FILE_ACCESS))<0){
sprintf(pBuffer,"Error: failed to create file access property "
"list for file %s",filename);
NXReportError(pBuffer);
return fapl;
}
/* set file close policy - need this in all cases*/
if(H5Pset_fclose_degree(fapl, H5F_CLOSE_STRONG)<0){
sprintf(pBuffer,"Error: cannot set close policy for file "
"%s",filename);
NXReportError(pBuffer);
return fapl;
}
return fapl;
}
herr_t set_file_cache(hid_t fapl,CONSTCHAR *filename)
{
char pBuffer[512];
int mdc_nelmts;
size_t rdcc_nelmts;
size_t rdcc_nbytes;
double rdcc_w0;
herr_t error = -1;
error = H5Pget_cache(fapl, &mdc_nelmts, &rdcc_nelmts,
&rdcc_nbytes,&rdcc_w0);
if(error < 0){
sprintf(pBuffer,"Error: cannot obtain HDF5 cache size"
" for file %s",filename);
NXReportError(pBuffer);
return error;
}
rdcc_nbytes = (size_t) nx_cacheSize;
error = H5Pset_cache(fapl, mdc_nelmts, rdcc_nelmts, rdcc_nbytes,rdcc_w0);
if(error < 0){
sprintf(pBuffer,"Error: cannot set cache size "
"for file %s",filename);
NXReportError(pBuffer);
return error;
}
return error;
}
herr_t set_str_attribute(hid_t parent_id,CONSTCHAR *name,CONSTCHAR *buffer)
{
char pBuffer[512];
hid_t attr_id;
hid_t space_id = H5Screate(H5S_SCALAR);
hid_t type_id = H5Tcopy(H5T_C_S1);
H5Tset_size(type_id, strlen(buffer));
attr_id = H5Acreate(parent_id,name,type_id,space_id,H5P_DEFAULT,
H5P_DEFAULT);
if (attr_id < 0) {
sprintf(pBuffer,"ERROR: failed to create %s attribute",name);
NXReportError(pBuffer);
return -1;
}
if(H5Awrite(attr_id,type_id,buffer) < 0) {
sprintf(pBuffer,"ERROR: failed writting %s attribute");
NXReportError(pBuffer);
return -1;
}
H5Tclose(type_id);
H5Sclose(space_id);
H5Aclose(attr_id);
return 0;
}
NXstatus NX5open(CONSTCHAR * filename, NXaccess am, NXhandle * pHandle)
{
hid_t root_id;
pNexusFile5 pNew = NULL;
char pBuffer[512];
char *time_buffer = NULL;
char version_nr[10];
unsigned int vers_major, vers_minor, vers_release, am1;
hid_t fapl = -1;
*pHandle = NULL;
if (H5get_libversion(&vers_major, &vers_minor, &vers_release) < 0) {
NXReportError("ERROR: cannot determine HDF5 library version");
return NX_ERROR;
}
if (vers_major == 1 && vers_minor < 8) {
NXReportError("ERROR: HDF5 library 1.8.0 or higher required");
return NX_ERROR;
}
/* mask of any options for now */
am = (NXaccess) (am & NXACCMASK_REMOVEFLAGS);
/* turn off the automatic HDF error handling */
H5Eset_auto(H5E_DEFAULT, NULL, NULL);
#ifdef USE_FTIME
struct timeb timeb_struct;
#endif
/* create the new file structure */
if(!(pNew = create_file_struct())) return NX_ERROR;
/* create the file access property list*/
if((fapl = create_file_access_plist(filename))<0)
{
free(pNew);
return NX_ERROR;
}
/* start HDF5 interface */
if (am == NXACC_CREATE5) {
/* set the cache size for the file */
if(set_file_cache(fapl,filename)<0) {
free(pNew);
return NX_ERROR;
}
am1 = H5F_ACC_TRUNC;
pNew->iFID = H5Fcreate(filename, am1, H5P_DEFAULT, fapl);
} else {
if (am == NXACC_READ) am1 = H5F_ACC_RDONLY;
else am1 = H5F_ACC_RDWR;
pNew->iFID = H5Fopen(filename, am1, fapl);
}
if (fapl != -1) H5Pclose(fapl); /*close file access property list*/
if (pNew->iFID <= 0) {
snprintf(pBuffer,sizeof(pBuffer)-1, "ERROR: cannot open file: %s", filename);
NXReportError(pBuffer);
free(pNew);
return NX_ERROR;
}
/*
* need to create global attributes file_name file_time NeXus_version
* at some point for new files
*/
if(am == NXACC_CREATE5)
{
root_id = H5Gopen(pNew->iFID,"/",H5P_DEFAULT);
if(set_str_attribute(root_id,"NeXus_version",NEXUS_VERSION)<0) {
H5Gclose(root_id);
H5Fclose(pNew->iFID);
free(pNew);
return NX_ERROR;
}
if(set_str_attribute(root_id,"file_name",filename)<0) {
H5Gclose(root_id);
H5Fclose(pNew->iFID);
free(pNew);
return NX_ERROR;
}
sprintf(version_nr,"%d.%d.%d",vers_major,vers_minor,vers_release);
if(set_str_attribute(root_id,"HDF5_Version",version_nr)<0) {
H5Gclose(root_id);
H5Fclose(pNew->iFID);
free(pNew);
return NX_ERROR;
}
time_buffer = NXIformatNeXusTime();
if (time_buffer != NULL){
if(set_str_attribute(root_id,"file_time",time_buffer)<0) {
H5Gclose(root_id);
H5Fclose(pNew->iFID);
free(pNew);
return NX_ERROR;
}
}
/*finally we set the NXroot NX_class attribute*/
if(set_str_attribute(root_id,"NX_class","NXroot")<0) {
H5Gclose(root_id);
H5Fclose(pNew->iFID);
free(pNew);
return NX_ERROR;
}
H5Gclose(root_id);
}
/* Set HDFgroup access mode */
if (am1 == H5F_ACC_RDONLY) {
strcpy(pNew->iAccess, "r");
} else {
strcpy(pNew->iAccess, "w");
}
pNew->iNXID = NX5SIGNATURE;
pNew->iStack5[0].iVref = 0; /* root! */
*pHandle = (NXhandle) pNew;
return NX_OK;
}
/* ------------------------------------------------------------------------- */
NXstatus NX5close(NXhandle * fid)
{
pNexusFile5 pFile = NULL;
herr_t iRet;
pFile = NXI5assert(*fid);
iRet = 0;
/*
printf("HDF5 object count before close: %d\n",
H5Fget_obj_count(pFile->iFID,H5F_OBJ_ALL));
*/
iRet = H5Fclose(pFile->iFID);
/*
leave this here: it helps in debugging leakage problems
printf("HDF5 object count after close: %d\n",
H5Fget_obj_count(H5F_OBJ_ALL,H5F_OBJ_ALL));
printf("HDF5 dataset count after close: %d\n",
H5Fget_obj_count(H5F_OBJ_ALL,H5F_OBJ_DATASET));
printf("HDF5 group count after close: %d\n",
H5Fget_obj_count(H5F_OBJ_ALL,H5F_OBJ_GROUP));
printf("HDF5 datatype count after close: %d\n",
H5Fget_obj_count(H5F_OBJ_ALL,H5F_OBJ_DATATYPE));
printf("HDF5 attribute count after close: %d\n",
H5Fget_obj_count(H5F_OBJ_ALL,H5F_OBJ_ATTR));
*/
if (iRet < 0) {
NXReportError("ERROR: cannot close HDF file");
}
/* release memory */
NXI5KillDir(pFile);
if (pFile->iCurrentLGG != NULL) {
free(pFile->iCurrentLGG);
}
if (pFile->iCurrentLD != NULL) {
free(pFile->iCurrentLD);
}
free(pFile);
*fid = NULL;
H5garbage_collect();
return NX_OK;
}
/*-----------------------------------------------------------------------*/
NXstatus NX5makegroup(NXhandle fid, CONSTCHAR * name, CONSTCHAR * nxclass)
{
pNexusFile5 pFile;
hid_t iRet;
hid_t iVID;
hid_t attr1, aid1, aid2;
char pBuffer[1024] = "";
pFile = NXI5assert(fid);
/* create and configure the group */
if (pFile->iCurrentG == 0) {
snprintf(pBuffer, 1023, "/%s", name);
} else {
snprintf(pBuffer, 1023, "/%s/%s", pFile->name_ref, name);
}
iVID =
H5Gcreate(pFile->iFID, (const char *)pBuffer, H5P_DEFAULT,
H5P_DEFAULT, H5P_DEFAULT);
if (iVID < 0) {
NXReportError("ERROR: could not create Group");
return NX_ERROR;
}
aid2 = H5Screate(H5S_SCALAR);
aid1 = H5Tcopy(H5T_C_S1);
H5Tset_size(aid1, strlen(nxclass));
attr1 =
H5Acreate(iVID, "NX_class", aid1, aid2, H5P_DEFAULT, H5P_DEFAULT);
if (attr1 < 0) {
NXReportError("ERROR: failed to store class name");
return NX_ERROR;
}
if (H5Awrite(attr1, aid1, (char *)nxclass) < 0) {
NXReportError("ERROR: failed to store class name");
return NX_ERROR;
}
/* close group */
iRet = H5Sclose(aid2);
iRet = H5Tclose(aid1);
iRet = H5Aclose(attr1);
iRet = H5Gclose(iVID);
return NX_OK;
}
/*------------------------------------------------------------------------*/
herr_t attr_check(hid_t loc_id, const char *member_name,
const H5A_info_t * unused, void *opdata)
{
char attr_name[8 + 1]; /* need to leave space for \0 as well */
strcpy(attr_name, "NX_class");
return strstr(member_name, attr_name) ? 1 : 0;
}
/*------------------------------------------------------------------------*/
NXstatus NX5opengroup(NXhandle fid, CONSTCHAR * name, CONSTCHAR * nxclass)
{
pNexusFile5 pFile;
hid_t attr1, atype, iVID;
herr_t iRet;
char pBuffer[1024];
char data[128];
pFile = NXI5assert(fid);
if (pFile->iCurrentG == 0) {
strcpy(pBuffer, name);
} else {
sprintf(pBuffer, "%s/%s", pFile->name_tmp, name);
}
iVID = H5Gopen(pFile->iFID, (const char *)pBuffer, H5P_DEFAULT);
if (iVID < 0) {
sprintf(pBuffer, "ERROR: group %s does not exist",
pFile->name_tmp);
NXReportError(pBuffer);
return NX_ERROR;
}
pFile->iCurrentG = iVID;
strcpy(pFile->name_tmp, pBuffer);
strcpy(pFile->name_ref, pBuffer);
if ((nxclass != NULL) && (strcmp(nxclass, NX_UNKNOWN_GROUP) != 0)) {
/* check group attribute */
iRet =
H5Aiterate(pFile->iCurrentG, H5_INDEX_CRT_ORDER,
H5_ITER_INC, 0, attr_check, NULL);
if (iRet < 0) {
NXReportError
("ERROR: iterating through attribute list");
return NX_ERROR;
} else if (iRet == 1) {
/* group attribute was found */
} else {
/* no group attribute available */
NXReportError("ERROR: no group attribute available");
return NX_ERROR;
}
/* check contents of group attribute */
attr1 =
H5Aopen_by_name(pFile->iCurrentG, ".", "NX_class",
H5P_DEFAULT, H5P_DEFAULT);
if (attr1 < 0) {
NXReportError
("ERROR: opening NX_class group attribute");
return NX_ERROR;
}
atype = H5Tcopy(H5T_C_S1);
H5Tset_size(atype, sizeof(data));
iRet = readStringAttributeN(attr1, data, sizeof(data));
if (strcmp(data, nxclass) == 0) {
/* test OK */
} else {
snprintf(pBuffer, sizeof(pBuffer),
"ERROR: group class is not identical: \"%s\" != \"%s\"",
data, nxclass);
NXReportError(pBuffer);
iRet = H5Tclose(atype);
iRet = H5Aclose(attr1);
return NX_ERROR;
}
iRet = H5Tclose(atype);
iRet = H5Aclose(attr1);
}
/* maintain stack */
pFile->iStackPtr++;
pFile->iStack5[pFile->iStackPtr].iVref = pFile->iCurrentG;
strcpy(pFile->iStack5[pFile->iStackPtr].irefn, name);
pFile->iAtt5.iCurrentIDX = 0;
pFile->iCurrentD = 0;
if (pFile->iCurrentLGG != NULL) {
free(pFile->iCurrentLGG);
}
pFile->iCurrentLGG = strdup(name);
NXI5KillDir(pFile);
return NX_OK;
}
/* ------------------------------------------------------------------- */
NXstatus NX5closegroup(NXhandle fid)
{
pNexusFile5 pFile;
int i, ii;
char *uname = NULL;
char *u1name = NULL;
pFile = NXI5assert(fid);
/* first catch the trivial case: we are at root and cannot get
deeper into a negative directory hierarchy (anti-directory)
*/
if (pFile->iCurrentG == 0) {
NXI5KillDir(pFile);
return NX_OK;
} else {
/* close the current group and decrement name_ref */
H5Gclose(pFile->iCurrentG);
i = 0;
i = (int)strlen(pFile->iStack5[pFile->iStackPtr].irefn);
ii = (int)strlen(pFile->name_ref);
if (pFile->iStackPtr > 1) {
ii = ii - i - 1;
} else {
ii = ii - i;
}
if (ii > 0) {
uname = strdup(pFile->name_ref);
u1name = (char *)malloc((ii + 1) * sizeof(char));
memset(u1name, 0, ii);
for (i = 0; i < ii; i++) {
*(u1name + i) = *(uname + i);
}
*(u1name + i) = '\0';
/*
strncpy(u1name, uname, ii);
*/
strcpy(pFile->name_ref, u1name);
strcpy(pFile->name_tmp, u1name);
free(uname);
free(u1name);
} else {
strcpy(pFile->name_ref, "");
strcpy(pFile->name_tmp, "");
}
NXI5KillDir(pFile);
pFile->iStackPtr--;
if (pFile->iStackPtr > 0) {
pFile->iCurrentG =
pFile->iStack5[pFile->iStackPtr].iVref;
} else {
pFile->iCurrentG = 0;
}
}
return NX_OK;
}
/*-----------------------------------------------------------------------*/
static hid_t nxToHDF5Type(int datatype)
{
hid_t type;
if (datatype == NX_CHAR) {
type = H5T_C_S1;
} else if (datatype == NX_INT8) {
type = H5T_NATIVE_CHAR;
} else if (datatype == NX_UINT8) {
type = H5T_NATIVE_UCHAR;
} else if (datatype == NX_INT16) {
type = H5T_NATIVE_SHORT;
} else if (datatype == NX_UINT16) {
type = H5T_NATIVE_USHORT;
} else if (datatype == NX_INT32) {
type = H5T_NATIVE_INT;
} else if (datatype == NX_UINT32) {
type = H5T_NATIVE_UINT;
} else if (datatype == NX_INT64) {
type = H5T_NATIVE_INT64;
} else if (datatype == NX_UINT64) {
type = H5T_NATIVE_UINT64;
} else if (datatype == NX_FLOAT32) {
type = H5T_NATIVE_FLOAT;
} else if (datatype == NX_FLOAT64) {
type = H5T_NATIVE_DOUBLE;
} else {
NXReportError("ERROR: nxToHDF5Type: unknown type");
type = -1;
}
return type;
}
/* --------------------------------------------------------------------- */
NXstatus NX5compmakedata64(NXhandle fid, CONSTCHAR * name,
int datatype,
int rank, int64_t dimensions[],
int compress_type, int64_t chunk_size[])
{
hid_t datatype1, dataspace, iNew, dID;
herr_t iRet;
hid_t type, cparms = -1;
pNexusFile5 pFile;
char pBuffer[256];
int i;
size_t byte_zahl = 0;
hsize_t chunkdims[H5S_MAX_RANK];
hsize_t mydim[H5S_MAX_RANK], mydim1[H5S_MAX_RANK];
hsize_t size[H5S_MAX_RANK];
hsize_t maxdims[H5S_MAX_RANK];
int compress_level;
int unlimiteddim = 0;
pFile = NXI5assert(fid);
if (pFile->iCurrentG <= 0) {
sprintf(pBuffer, "ERROR: no group open for makedata on %s",
name);
NXReportError(pBuffer);
return NX_ERROR;
}
if (rank <= 0) {
sprintf(pBuffer, "ERROR: invalid rank specified %s", name);
NXReportError(pBuffer);
return NX_ERROR;
}
type = nxToHDF5Type(datatype);
/*
Check dimensions for consistency. Dimension may be -1
thus denoting an unlimited dimension.
*/
for (i = 0; i < rank; i++) {
chunkdims[i] = chunk_size[i];
mydim[i] = dimensions[i];
maxdims[i] = dimensions[i];
size[i] = dimensions[i];
if (dimensions[i] <= 0) {
mydim[i] = 1;
maxdims[i] = H5S_UNLIMITED;
size[i] = 1;
unlimiteddim = 1;
} else {
mydim[i] = dimensions[i];
maxdims[i] = dimensions[i];
size[i] = dimensions[i];
}
}
if (datatype == NX_CHAR) {
/*
* This assumes string lenght is in the last dimensions and
* the logic must be the same as used in NX5getslab and NX5getinfo
*
* search for tests on H5T_STRING
*/
byte_zahl = (size_t) mydim[rank - 1];
for (i = 0; i < rank; i++) {
mydim1[i] = mydim[i];
if (dimensions[i] <= 0) {
mydim1[0] = 1;
maxdims[0] = H5S_UNLIMITED;
}
}
mydim1[rank - 1] = 1;
if (mydim[rank - 1] > 1) {
mydim[rank - 1] = maxdims[rank - 1] = size[rank - 1] =
1;
}
if (chunkdims[rank - 1] > 1) {
chunkdims[rank - 1] = 1;
}
dataspace = H5Screate_simple(rank, mydim1, maxdims);
} else {
if (unlimiteddim) {
dataspace = H5Screate_simple(rank, mydim, maxdims);
} else {
/* dataset creation */
dataspace = H5Screate_simple(rank, mydim, NULL);
}
}
datatype1 = H5Tcopy(type);
if (datatype == NX_CHAR) {
H5Tset_size(datatype1, byte_zahl);
/* H5Tset_strpad(H5T_STR_SPACEPAD); */
}
compress_level = 6;
if ((compress_type / 100) == NX_COMP_LZW) {
compress_level = compress_type % 100;
compress_type = NX_COMP_LZW;
}
if (compress_type == NX_COMP_LZW) {
cparms = H5Pcreate(H5P_DATASET_CREATE);
iNew = H5Pset_chunk(cparms, rank, chunkdims);
if (iNew < 0) {
NXReportError("ERROR: size of chunks could not be set");
return NX_ERROR;
}
H5Pset_shuffle(cparms); // mrt: improves compression
H5Pset_deflate(cparms, compress_level);
dID = H5Dcreate(pFile->iCurrentG, (char *)name, datatype1,
dataspace, H5P_DEFAULT, cparms, H5P_DEFAULT);
} else if (compress_type == NX_COMP_NONE) {
if (unlimiteddim) {
cparms = H5Pcreate(H5P_DATASET_CREATE);
iNew = H5Pset_chunk(cparms, rank, chunkdims);
if (iNew < 0) {
NXReportError
("ERROR: size of chunks could not be set");
return NX_ERROR;
}
dID =
H5Dcreate(pFile->iCurrentG, (char *)name, datatype1,
dataspace, H5P_DEFAULT, cparms,
H5P_DEFAULT);
} else {
dID =
H5Dcreate(pFile->iCurrentG, (char *)name, datatype1,
dataspace, H5P_DEFAULT, H5P_DEFAULT,
H5P_DEFAULT);
}
} else if (compress_type == NX_CHUNK) {
cparms = H5Pcreate(H5P_DATASET_CREATE);
iNew = H5Pset_chunk(cparms, rank, chunkdims);
if (iNew < 0) {
NXReportError("ERROR: size of chunks could not be set");
return NX_ERROR;
}
dID = H5Dcreate(pFile->iCurrentG, (char *)name, datatype1,
dataspace, H5P_DEFAULT, cparms, H5P_DEFAULT);
} else {
NXReportError
("HDF5 doesn't support selected compression method! Dataset created without compression");
iRet =
H5Dcreate(pFile->iCurrentG, (char *)name, datatype1,
dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
}
if (dID < 0) {
NXReportError("ERROR: creating chunked dataset failed");
return NX_ERROR;
} else {
pFile->iCurrentD = dID;
}
if (unlimiteddim) {
iNew = H5Dset_extent(pFile->iCurrentD, size);
if (iNew < 0) {
sprintf(pBuffer, "ERROR: cannot create dataset %s",
name);
NXReportError(pBuffer);
return NX_ERROR;
}
}
if (cparms != -1) {
iRet = H5Pclose(cparms);
}
iRet = H5Sclose(dataspace);
iRet = H5Tclose(datatype1);
iRet = H5Dclose(pFile->iCurrentD);
pFile->iCurrentD = 0;
if (iRet < 0) {
NXReportError("ERROR: HDF cannot close dataset");
return NX_ERROR;
}
return NX_OK;
}
/* --------------------------------------------------------------------- */
NXstatus NX5makedata64(NXhandle fid, CONSTCHAR * name, int datatype,
int rank, int64_t dimensions[])
{
int64_t chunk_size[H5S_MAX_RANK];
int i;
NXI5assert(fid);
memset(chunk_size, 0, H5S_MAX_RANK * sizeof(int64_t));
memcpy(chunk_size, dimensions, rank * sizeof(int64_t));
for (i = 0; i < rank; i++) {
if (dimensions[i] == NX_UNLIMITED || dimensions[i] <= 0) {
chunk_size[i] = 1;
}
}
return NX5compmakedata64(fid, name, datatype, rank, dimensions,
NX_COMP_NONE, chunk_size);
}
/* --------------------------------------------------------------------- */
NXstatus NX5compress(NXhandle fid, int compress_type)
{
printf
(" NXcompress ERROR: NeXus API based on HDF5 doesn't support\n");
printf
(" NXcompress function! Using HDF5 library,\n");
printf
(" the NXcompmakedata function can be applied\n");
printf(" for compression of data!\n");
return NX_ERROR;
}
/* --------------------------------------------------------------------- */
NXstatus NX5opendata(NXhandle fid, CONSTCHAR * name)
{
pNexusFile5 pFile;
char pBuffer[256];
pFile = NXI5assert(fid);
/* clear pending attribute directories first */
NXI5KillAttDir(pFile);
/* find the ID number and open the dataset */
pFile->iCurrentD = H5Dopen(pFile->iCurrentG, name, H5P_DEFAULT);
if (pFile->iCurrentD < 0) {
sprintf(pBuffer,
"ERROR: dataset \"%s\" not found at this level", name);
NXReportError(pBuffer);
return NX_ERROR;
}
/* find the ID number of datatype */
pFile->iCurrentT = H5Dget_type(pFile->iCurrentD);
if (pFile->iCurrentT < 0) {
NXReportError("ERROR: error opening dataset");
pFile->iCurrentT = 0;
return NX_ERROR;
}
/* find the ID number of dataspace */
pFile->iCurrentS = H5Dget_space(pFile->iCurrentD);
if (pFile->iCurrentS < 0) {
NXReportError("ERROR:HDF error opening dataset");
pFile->iCurrentS = 0;
return NX_ERROR;
}
if (pFile->iCurrentLD != NULL) {
free(pFile->iCurrentLD);
}
pFile->iCurrentLD = strdup(name);
return NX_OK;
}
/* ----------------------------------------------------------------- */