In [11]:
import h5py    # HDF5 support
import numpy

print "Write a NeXus HDF5 file"
fileName = "prj_test.nexus.hdf5"
timestamp = "2010-10-18T17:17:04-0500"

# load data from two column format
data = numpy.loadtxt('test.dat',delimiter=',').T
mr_arr = data[0]
#i00_arr = numpy.asarray(data[1],'int32')
i00_arr=data[1]
# create the HDF5 NeXus file
f = h5py.File(fileName, "w")
# point to the default data to be plotted
f.attrs['default']          = 'entry'
# give the HDF5 root some more attributes
f.attrs['file_name']        = fileName
f.attrs['file_time']        = timestamp
f.attrs['instrument']       = 'APS USAXS at 32ID-B'
f.attrs['creator']          = 'BasicWriter.py'
f.attrs['NeXus_version']    = '4.3.0'
f.attrs['HDF5_Version']     = h5py.version.hdf5_version
f.attrs['h5py_version']     = h5py.version.version

# create the NXentry group
nxentry = f.create_group('entry')
nxentry.attrs['NX_class'] = 'NXentry'
nxentry.attrs['default'] = 'mr_scan'
nxentry.create_dataset('title', data='1-D scan of I00 v. mr')

# create the NXentry group
nxdata = nxentry.create_group('mr_scan')
nxdata.attrs['NX_class'] = 'NXdata'
nxdata.attrs['signal'] = 'I00'      # Y axis of default plot
nxdata.attrs['axes'] = 'mr'         # X axis of default plot
nxdata.attrs['mr_indices'] = [0,]   # use "mr" as the first dimension of I00

# X axis data
ds = nxdata.create_dataset('mr', data=mr_arr)
ds.attrs['units'] = 'degrees'
ds.attrs['long_name'] = 'USAXS mr (degrees)'    # suggested X axis plot label

# Y axis data
ds = nxdata.create_dataset('I00', data=i00_arr)
ds.attrs['units'] = 'counts'
ds.attrs['long_name'] = 'USAXS I00 (counts)'    # suggested Y axis plot label

f.close()   # be CERTAIN to close the file

print "wrote file:", fileName


Write a NeXus HDF5 file
wrote file: prj_test.nexus.hdf5

In [13]:
fileName = "prj_test.nexus.hdf5"
f = h5py.File(fileName,  "r")
for item in f.attrs.keys():
    print item + ":", f.attrs[item]
mr = f['/entry/mr_scan/mr']
i00 = f['/entry/mr_scan/I00']
print "%s\t%s\t%s" % ("#", "mr", "I00")
for i in range(len(mr)):
    print "%d\t%g\t%g" % (i, mr[i], i00[i])
f.close()


default: entry
file_name: prj_test.nexus.hdf5
file_time: 2010-10-18T17:17:04-0500
instrument: APS USAXS at 32ID-B
creator: BasicWriter.py
NeXus_version: 4.3.0
HDF5_Version: 1.8.15
h5py_version: 2.6.0
#	mr	I00
0	138.56	0.0430102
1	138.592	0.0413923
2	138.688	0.0414661
3	138.752	0.0417725
4	138.816	0.0416456
5	138.88	0.0523672
6	138.944	0.0458781
7	139.04	0.0461439
8	139.072	0.0458299
9	139.168	0.0537148
10	139.232	0.0493163
11	139.296	0.0539845
12	139.36	0.042111
13	139.456	0.0413812
14	139.488	0.0397415
15	139.584	0.0413433
16	139.648	0.0732311
17	139.68	0.0435675
18	139.776	0.044536
19	139.84	0.0427058
20	139.936	0.0440455
21	139.968	0.0412422
22	140.064	0.0410037
23	140.128	0.0405981
24	140.192	0.0417035
25	140.256	0.0409213
26	140.352	0.0480981
27	140.384	0.045026
28	140.48	0.0421759
29	140.544	0.0451926
30	140.608	0.0559294
31	140.672	0.0976421
32	140.736	0.0414424
33	140.832	0.0549768
34	140.896	0.0557693
35	140.96	1
36	141.024	0.0422378
37	141.088	0.041093
38	141.152	0.22832
39	141.248	0.0508695
40	141.28	0.0403796
41	141.376	0.167391
42	141.44	0.107568
43	141.504	0.0507728
44	141.568	0.100053
45	141.632	0.119671
46	141.728	0.0972988
47	141.792	0.262689
48	141.856	0.0844824
49	141.92	0.0441579
50	141.984	0.0437865
51	142.048	0.85294
52	142.144	0.131096
53	142.176	0.0865055
54	142.272	0.0826924
55	142.336	0.100858
56	142.4	0.0792989
57	142.464	0.224286
58	142.56	0.335792
59	142.624	0.0470763
60	142.688	0.0415331
61	142.752	0.0419934
62	142.848	0.0429833
63	142.88	0.0415363
64	142.976	0.041296
65	143.04	0.0411091
66	143.104	0.0413223
67	143.168	0.0542471
68	143.232	0.0437874
69	143.296	0.043149
70	143.392	0.0421327
71	143.456	0.0417183
72	143.52	0.0420377
73	143.584	0.0414479
74	143.648	0.0428493
75	143.744	0.0429992
76	143.776	0.0416623
77	143.84	0.0390057
78	143.936	0.0366288
79	143.968	0.0362437
80	144.064	0.0366184
81	144.128	0.0381364
82	144.192	0.0370921
83	144.256	0.0356435
84	144.352	0.0371753
85	144.416	0.0367782
86	144.48	0.0356989
87	144.544	0.0357427
88	144.608	0.0367997
89	144.672	0.035686
90	144.736	0.0353423
91	144.832	0.0356449
92	144.864	0.0353982
93	144.96	0.0356265
94	145.024	0.0364229
95	145.088	0.0363611
96	145.152	0.0362725
97	145.216	0.0363641
98	145.28	0.0359861
99	145.344	0.0362689
100	145.44	0.0360004
101	145.504	0.0354861
102	145.568	0.0363733
103	145.632	0.0371236
104	145.728	0.0612662
105	145.76	0.321984
106	145.856	0.275395
107	145.92	0.0512513
108	145.984	0.190251
109	146.048	0.089003
110	146.112	0.0389278
111	146.176	0.0375519
112	146.24	0.0444683
113	146.336	0.0427238
114	146.4	0.0361027
115	146.464	0.0409782
116	146.528	0.131597
117	146.624	0.0895861
118	146.656	0.0373111
119	146.752	0.0410513
120	146.816	0.0370493
121	146.88	0.0690997
122	146.944	0.0716551
123	147.008	0.146294
124	147.072	0.0568554
125	147.136	0.0537599
126	147.232	0.0573101
127	147.296	0.0550196
128	147.36	0.064647
129	147.424	0.0528023
130	147.52	0.0476365
131	147.552	0.0575875
132	147.616	0.0455153
133	147.712	0.0568644
134	147.776	0.0480064
135	147.84	0.0501595
136	147.904	0.0447032
137	147.968	0.0480784
138	148.032	0.0460128
139	148.128	0.058346
140	148.192	0.0432713
141	148.256	0.055401
142	148.32	0.0425588
143	148.416	0.0415451
144	148.448	0.041456
145	148.544	0.044854
146	148.608	0.0729375
147	148.672	0.181457
148	148.736	0.0567712
149	148.8	0.041536
150	148.864	0.0421395
151	148.928	0.0426842
152	149.024	0.0485488
153	149.088	0.0445902
154	149.152	0.0434655
155	149.216	0.0433093
156	149.312	0.0461598
157	149.344	0.0423401
158	149.44	0.0435712
159	149.504	0.0450809
160	149.568	0.0446497
161	149.632	0.0646905
162	149.696	0.0450607
163	149.76	0.0473716
164	149.856	0.0593422
165	149.92	0.0444371
166	149.984	0.0468517
167	150.048	0.0445762
168	150.112	0.0440217
169	150.208	0.0429205
170	150.272	0.043048
171	150.336	0.0436382
172	150.4	0.0433632
173	150.464	0.0436947
174	150.528	0.04391
175	150.624	0.0713019
176	150.688	0.0467895
177	150.752	0.046668
178	150.816	0.0465358
179	150.88	0.0442793
180	150.944	0.047447
181	151.04	0.0468261
182	151.104	0.164775
183	151.168	0.0589846
184	151.232	0.0522721
185	151.296	0.0586505
186	151.36	0.0453663
187	151.456	0.0443018
188	151.52	0.0433247
189	151.584	0.0506314
190	151.648	0.0733174
191	151.744	0.0529331
192	151.776	0.109879
193	151.84	0.224709
194	151.936	0.0643376
195	152	0.0477998
196	152.064	0.0638889
197	152.128	0.0773728
198	152.224	0.0699774
199	152.256	0.0519889
200	152.352	0.07301
201	152.416	0.0505244
202	152.48	0.0973882
203	152.544	0.0696324
204	152.608	0.0652412
205	152.672	0.0462527
206	152.736	0.0529542
207	152.832	0.05248
208	152.896	0.0507106
209	152.928	0.0757545
210	153.024	0.0562762
211	153.088	0.0751052
212	153.152	0.151946
213	153.216	0.053826
214	153.312	0.050912
215	153.344	0.141521
216	153.44	0.120901
217	153.504	0.0496977
218	153.568	0.0630663
219	153.632	0.0768431
220	153.696	0.0589582
221	153.792	0.0514666
222	153.856	0.0610395
223	153.92	0.0637026
224	153.984	0.0521897
225	154.048	0.0556464
226	154.112	0.0651332
227	154.208	0.067875
228	154.24	0.0683504
229	154.336	0.0671352
230	154.4	0.0606351
231	154.464	0.0473815
232	154.528	0.0555238
233	154.624	0.0709072
234	154.688	0.046229
235	154.752	0.0466327
236	154.816	0.0551434
237	154.912	0.0571363
238	154.944	0.0534155
239	155.04	0.0515078
240	155.104	0.0431789
241	155.168	0.0460124
242	155.232	0.0443388
243	155.296	0.0430386
244	155.36	0.0497241
245	155.424	0.0434728
246	155.52	0.0447187
247	155.552	0.345648
248	155.648	0.0411497
249	155.712	0.0409666
250	155.808	0.0434746
251	155.84	0.0410852
252	155.936	0.0416809
253	156	0.0413195
254	156.064	0.0409788
255	156.128	0.0412901
256	156.224	0.0417272
257	156.256	0.0438945
258	156.352	0.0419515
259	156.416	0.040656
260	156.48	0.0408083
261	156.544	0.0404258
262	156.64	0.0405831
263	156.704	0.0417949
264	156.768	0.0419056
265	156.832	0.0406007
266	156.896	0.0419466
267	156.96	0.0425373
268	157.024	0.0426068
269	157.12	0.0422463
270	157.152	0.0403368
271	157.248	0.0406486
272	157.312	0.0407557
273	157.376	0.0407241
274	157.44	0.0411276
275	157.536	0.0399703
276	157.6	0.0415532
277	157.664	0.040666
278	157.728	0.0418195
279	157.824	0.190544
280	157.856	0.207
281	157.952	0.228217
282	158.016	0.039172
283	158.08	0.0386949
284	158.144	0.0452044
285	158.208	0.0571933
286	158.272	0.0383152
287	158.336	0.0430707
288	158.432	0.0709255
289	158.496	0.0665446
290	158.56	0.048623
291	158.624	0.0516272
292	158.688	0.0358569
293	158.752	0.035883
294	158.816	0.0363172
295	158.912	0.035657
296	158.944	0.041525
297	159.04	0.147504
298	159.104	0.0611245
299	159.168	0.0372038

In [ ]:


In [ ]: