-
Notifications
You must be signed in to change notification settings - Fork 0
/
isis_raw_reader.py
1559 lines (1288 loc) · 57.9 KB
/
isis_raw_reader.py
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
import sys
from argparse import ArgumentParser, RawTextHelpFormatter
from ctypes import Structure, Union, c_char, c_float, c_int, c_long, c_ulong
from os import path
import h5py
import numpy as np
class IsisRawReader:
"""
Class to read from ISIS .raw files and covert to other formats (e.g. NeXus).
(see https://www.isis.stfc.ac.uk/Pages/ISIS-Raw-File-Format.aspx)
.raw files can also be read using
- OpenGenie (http://www.opengenie.org/Main_Page)
- Mantid (https://www.mantidproject.org/)
The purpose of this class is to give a simple standalone Python implementation.
The Main source of truth when writing this was the Mantid source code
(https://github.com/mantidproject/mantid//Framework/DataHandling/src/LoadRaw)
Raw files broadly contain the sections:
- Summary
- Run
- Instrument
- Sample
- DAE
- Time Channel
- User
- Data
- Log
These are stored as nested classes of the IsisRawReader, and have specific locations
within the file, stored in self.info_positions.
Usage:
# Read .raw file into memory
raw_file_path = "path/to/raw/file.raw"
isis_raw_reader = IsisRawReader()
isis_raw_reader.read_raw_file(raw_file_path)
# Output .raw file as .nxs file
output_filename = "SXD.nxs"
isis_raw_reader.output_tofraw_file(output_filename)
"""
def __init__(self):
self.raw_file = None
self.summary = self.SummaryInfo()
self.run = self.RunInfo()
self.instrument = self.InstrumentInfo()
self.sample = self.SampleInfo()
self.dae = self.DAEInfo()
self.time_channel = self.TimeChannelInfo()
self.user = self.UserInfo()
self.data = self.DataInfo()
self.log = self.RawLog()
self.info_positions = self.RawSectionPositions()
class SummaryInfo:
class RawHeader(Structure):
_fields_ = [
("instrument_name", c_char * 3),
("run_number", c_char * 5),
("user_name", c_char * 20),
("title", c_char * 24),
("start_date", c_char * 12),
("start_time", c_char * 8),
("run_duration", c_char * 8),
("format_number", c_int),
]
def __init__(self):
self.header = self.RawHeader()
self.format_version = c_int(2)
self.format = (c_int * 3)() # 0 = by TC, 1 = by spectrum
class RunInfo:
class RawUser(Structure):
_fields_ = [
("user", c_char * 20),
("daytime_phone", c_char * 20),
("daytime_phone_2", c_char * 20),
("nighttime_phone", c_char * 20),
("institute", c_char * 20),
("unused", (c_char * 20) * 3),
]
class RawRun(Structure):
_fields_ = [
("run_duration", c_int),
("duration_units", c_int),
("duration_freq", c_int),
("dump_interval", c_int),
("dump_units", c_int),
("dump_freq", c_int),
("frequency", c_int),
("good_proton_charge", c_float),
("total_proton_charge", c_float),
("good_frames", c_int),
("raw_frames", c_int),
("requested_duration", c_int),
("duration_seconds", c_int),
("monitor_sum_1", c_int),
("monitor_sum_2", c_int),
("monitor_sum_3", c_int),
("end_date", c_char * 12),
("end_time", c_char * 8),
("proposal_num", c_int),
("unused", c_int * 10),
]
def __init__(self):
self.title = (c_char * 80)()
self.user = self.RawUser()
self.params = self.RawRun()
self.version = c_int(1)
self.number = c_int()
class InstrumentInfo:
class RawInstrument(Structure):
_fields_ = [
("chopper_freq_1", c_float), # (Hz)
("chopper_freq_2", c_float),
("chopper_freq_3", c_float),
("chopper_delay_1", c_int), # (us)
("chopper_delay_2", c_int),
("chopper_delay_3", c_int),
("chopper_delay_err_1", c_int), # (us)
("chopper_delay_err_2", c_int),
("chopper_delay_err_3", c_int),
("i_chopsiz", c_float), # unknown param
("aperture_c2", c_int), # unknown param
("aperture_c3", c_int), # unknown param
("chopper_status_1", c_int),
("chopper_status_2", c_int),
("chopper_status_3", c_int),
("main_shutter", c_int), # open = 1
("thermal_shutter", c_int), # open = 1
("beam_aperture_h", c_float), # (mm)
("beam_aperture_v", c_float), # (mm)
("scattering_pos", c_int),
("moderator_type", c_int),
("detector_tank_vacuum", c_int),
("L1_scattering_length", c_float),
("rotor_freq", c_int),
("rotor_energy", c_float),
("rotor_phase", c_float),
("rotor_slit_package", c_int), # 0="unknown",1="L",2="Med",3="Hi"
("slow_chop", c_int), # on = 1
("LOQ_x_centre", c_float),
("LOQ_y_centre", c_float),
("LOQ_beam_stop", c_float),
("LOQ_beam_stop_radius", c_float),
("LOQ_source_detector_distance", c_float),
("LOQ_foe_angle", c_float),
("angle_of_incidence", c_float),
("unused", c_int * 29),
]
def __init__(self):
self.params = self.RawInstrument()
self.version = c_int()
self.name = (c_char * 8)()
self.num_detectors = (c_int * 3)()
self.num_monitors = c_int()
self.monitor_detector_number = (c_int * 4)()
self.monitor_prescale_val = (c_int * 4)()
self.spectrum_number_table = c_int()
self.hold_off_table = c_int()
self.L2_table = c_float()
self.UTn_tables_code = c_int()
self.two_theta = c_float()
self.num_user_tables = c_int()
class SampleInfo:
class RawSample(Structure):
_fields_ = [
("sample_changer_pos", c_int),
("sample_type", c_int), # 1 = sample + can, 2 = empty can
("sample_geom", c_int),
("sample_thickness", c_float), # (mm)
("sample_height", c_float), # (mm)
("sample_width", c_float), # (mm)
("sample_omega_angle", c_float), # (degrees)
("sample_chi_angle", c_float), # (degrees)
("sample_phi_angle", c_float), # (degrees)
("scattering_geom", c_float), # 1 = trans, 2 = reflect
("sample_coh_scattering_xsec", c_float), # (barn)
("sample_incoh_xsec", c_float),
("sample_absorb_xsec", c_float),
("sample_number_density", c_float), # (atoms A^-3)
("can_wall_thickness", c_float), # (mm)
("can_coh_scattering_xsec", c_float), # (barn)
("can_cs_inc", c_float), # unknown param
("can_cs_abs", c_float), # unknown param
("can_number_density", c_float), # (atoms A^-3)
("sample_name_chem_form", c_char * 40),
("e_equip", c_int), # unknown param
("e_eqname", c_int), # unknown param
("unused", c_int * 33),
]
class RawSampleEnvironment(Structure):
_fields_ = [
("sample_env_name", c_char * 8),
("sep_value", c_int), # unknown param
("sep_exponent", c_int), # unknown param
("sample_env_units", c_char * 8),
("sep_low_trip", c_int), # unknown param
("sep_high_trip", c_int), # unknown param
("sep_cur_val", c_int), # unknown param
("sample_env_status", c_int), # (bool)
("sample_env_ctrl", c_int), # (bool)
("sample_env_run_ctrl", c_int), # (bool)
("sample_env_log", c_int), # (bool)
("sample_env_stable", c_float), # (Hz)
("monitor_repeat_period", c_float),
("camac_loc_N", c_int),
("camac_loc_A", c_int),
("camac_offset", c_int),
("camac_register_group", c_int), # 1 or 2
("pre_proc_routine_num", c_int),
("camac_values", c_int * 12),
]
def __init__(self):
self.params = self.RawSample()
self.environment = self.RawSampleEnvironment()
self.env_version = c_int()
self.num_sample_env_params = c_int()
class DAEInfo:
class RawDAE(Structure):
_fields_ = [
("word_length", c_int),
("mem_size", c_int), # (bytes) **A
("ppp_min_val", c_int),
("ppp_good_high", c_int),
("ppp_good_low", c_int),
("ppp_raw_high", c_int),
("ppp_raw_low", c_int),
("neutron_good_high", c_int),
("neutron_good_low", c_int),
("neutron_raw_high", c_int),
("neutron_raw_low", c_int),
("neutron_gate_t1", c_int),
("neutron_gate_t2", c_int),
("mon1_detector", c_int),
("mon1_module", c_int),
("mon1_crate", c_int),
("mon1_mask", c_int),
("mon2_detector", c_int),
("mon2_module", c_int),
("mon2_crate", c_int),
("mon2_mask", c_int),
("total_good_events_high", c_int),
("total_good_events_low", c_int),
("frame_sync_delay", c_int),
("frame_sync_origin", c_int), # 0:none/1:ext/2:int
("secondary_master_pulse", c_int), # 0:en, 1:dis
("external_vetoes", c_int * 3), # 0,1,2
("num_shifted_time_regimes", c_int), # for alternative PC DAE
("time_regime_shift_value", c_int * 3), # for alternative PC DAE
("unused", c_int * 31),
]
def __init__(self):
self.params = self.RawDAE()
self.version = c_int()
self.detector_crate_nums = c_int()
self.detector_module_nums = c_int()
self.detector_module_positions = c_int()
self.detector_time_regimes = c_int()
self.detector_user_nums = c_int()
class TimeChannelInfo:
def __init__(self):
self.version = (c_int * 267)()
self.num_time_regimes = c_int()
self.num_frames_per_period = c_int()
self.num_periods = c_int(1)
self.period_num_per_period = (c_int * 256)()
self.num_spectra = c_int()
self.num_time_channels = c_int()
self.mode = (c_int * 5)()
self.params = ((c_float * 5) * 4)()
self.prescale = c_int()
self.raw_boundaries = c_int()
self.boundaries = c_float()
class UserInfo:
def __init__(self):
self.version = c_int()
self.length = c_int()
self.data = c_float()
class DataInfo:
def __init__(self):
self.version = c_int()
self.data_header = self.RawDataHeader()
self.ddes = self.RawDDES()
self.compressed_data = c_long()
class RawDataHeader(Structure):
_fields_ = [
("compression_type", c_int), # 0=none, 1=byte relative
("unused1", c_int),
("spectrum_array_offset", c_int),
("compression_ratio", c_float),
("file_compression_ratio", c_float),
("equiv_v1_filesize", c_int),
("unused2", c_int * 26),
]
class RawDDES(Structure):
_fields_ = [
("num_spec_compressed_words", c_int),
("compressed_spec_offset", c_int),
]
class RawSectionPositions(Structure):
_fields_ = [
("run", c_int),
("instrument", c_int),
("sample_environment", c_int),
("dae", c_int),
("time_channels", c_int),
("user", c_int),
("data", c_int),
("log", c_int),
("file_end", c_int),
]
class RawLog(Structure):
_fields_ = [
("version", c_int * 2),
("num_lines", c_int),
]
def decompress_data(self, data_in, data_out):
# Byte code used to indicate number cannot be
# written +/-127 of previous value
nan_val = -128
def number_in_byte(byte_val, nan_val):
return byte_val != nan_val
def byte_to_int(byte):
result = 0
for b in byte:
result = result * 256 + int(b)
if result > 127:
return (256 - result) * (-1)
return result
class BytePacket(Union):
_fields_ = [("pos", c_int), ("val", c_char * 4)]
counter = 0
byte_packet = BytePacket()
byte_packet.pos = 0
for i in range(len(data_out)):
assert counter < len(data_in), "channel_counter out of range"
new_byte = byte_to_int(data_in[counter])
if number_in_byte(new_byte, nan_val):
byte_packet.pos += new_byte
counter += 1
else:
assert counter + 4 < len(data_in), "byte out of range"
byte_packet.val = data_in[counter + 1 : counter + 5]
counter += 5
data_out[i] = byte_packet.pos
return data_out
def read_summary_info(self):
self.read_into_buffer(self.summary.header)
self.read_into_buffer(self.info_positions)
self.read_into_buffer(self.summary.format)
def read_run_info(self):
self.read_into_buffer(self.run.title)
self.run.title = self.run.title[:].rstrip()
self.read_into_buffer(self.run.user)
self.read_into_buffer(self.run.params)
def read_instrument_info(self):
def update_param_sizes(new_size):
self.instrument.spectrum_number_table = (c_int * new_size)()
self.instrument.hold_off_table = (c_float * new_size)()
self.instrument.L2_table = (c_float * new_size)()
self.instrument.UTn_tables_code = (c_float * new_size)()
self.instrument.two_theta = (c_float * new_size)()
self.read_into_buffer(self.instrument.version)
self.read_into_buffer(self.instrument.name)
self.read_into_buffer(self.instrument.params)
self.read_into_buffer(self.instrument.num_detectors)
self.instrument.num_detectors = self.instrument.num_detectors[0]
self.read_into_buffer(self.instrument.monitor_detector_number)
self.read_into_buffer(self.instrument.monitor_prescale_val)
update_param_sizes(self.instrument.num_detectors)
self.read_into_buffer(self.instrument.spectrum_number_table)
self.read_into_buffer(self.instrument.hold_off_table)
self.read_into_buffer(self.instrument.L2_table)
self.read_into_buffer(self.instrument.UTn_tables_code)
self.read_into_buffer(self.instrument.two_theta)
def read_sample_info(self):
self.read_into_buffer(self.sample.env_version)
self.read_into_buffer(self.sample.params)
self.read_into_buffer(self.sample.num_sample_env_params)
num_envs = self.sample.num_sample_env_params.value
self.sample.environment = (self.sample.RawSampleEnvironment * num_envs)()
self.read_into_buffer(self.sample.environment)
def read_dae_info(self):
def update_param_sizes(new_size):
self.dae.detector_crate_nums = (c_int * new_size)()
self.dae.detector_module_nums = (c_int * new_size)()
self.dae.detector_module_positions = (c_int * new_size)()
self.dae.detector_time_regimes = (c_int * new_size)()
self.dae.detector_user_nums = (c_int * new_size)()
update_param_sizes(self.instrument.num_detectors)
self.read_into_buffer(self.dae.version)
self.read_into_buffer(self.dae.params)
self.read_into_buffer(self.dae.detector_crate_nums)
self.read_into_buffer(self.dae.detector_module_nums)
self.read_into_buffer(self.dae.detector_module_positions)
self.read_into_buffer(self.dae.detector_time_regimes)
self.read_into_buffer(self.dae.detector_user_nums)
def read_time_channel_info(self):
def get_time_channel_boundaries(raw_boundaries, prescale, s_delay, fmt_ver):
added_delay = 4.0 * s_delay
if fmt_ver.value <= 1:
added_delay = 0
boundaries = []
for i in raw_boundaries:
boundaries.append(i * (prescale.value / 32.0) + added_delay)
return boundaries
# The version buffer is not currectly defined
# But in lieu of original documentation of the file structure
# the hack below is necessary
self.read_into_buffer(self.time_channel.version)
self.time_channel.num_time_channels = self.time_channel.version[-6]
self.time_channel.version = self.time_channel.version[0]
self.read_into_buffer(self.time_channel.params)
self.time_channel.num_spectra = self.instrument.num_detectors
self.read_into_buffer(self.time_channel.prescale)
num_boundaries = self.time_channel.num_time_channels + 1
self.time_channel.raw_boundaries = (c_int * (num_boundaries))()
self.time_channel.boundaries = (c_float * (num_boundaries))()
self.read_into_buffer(self.time_channel.raw_boundaries)
boundaries_lst = get_time_channel_boundaries(
self.time_channel.raw_boundaries,
self.time_channel.prescale,
self.dae.params.frame_sync_delay,
self.summary.format_version,
)
self.time_channel.boundaries = (c_float * len(boundaries_lst))(*boundaries_lst)
def get_counts_per_pixel(self, num_detectors, num_channels):
raw_shape = (1, num_detectors, num_channels)
raw_data = self.data.compressed_data[:]
return np.array(raw_data).reshape(raw_shape)
def read_user_info(self):
self.read_into_buffer(self.user.version)
# Stray byte here
dummy_int = c_int()
self.read_into_buffer(dummy_int)
self.user.length = self.info_positions.data - self.info_positions.user - 2
self.user.data = (c_float * self.user.length)()
self.read_into_buffer(self.user.data)
def read_data_info(self):
self.read_into_buffer(self.data.version)
self.read_into_buffer(self.data.data_header)
compression_type = self.data.data_header.compression_type
assert compression_type == 1, "Unable to decompress data"
num_periods = self.time_channel.num_periods.value
num_ddes = num_periods * (self.time_channel.num_spectra + 1)
self.data.ddes = (self.data.RawDDES * num_ddes)()
self.read_into_buffer(self.data.ddes)
ddes_size = self.time_channel.num_time_channels + 1
self.data.compressed_data = (c_ulong * (num_ddes * (ddes_size)))()
for i in range(num_ddes):
nwords = self.data.ddes[i].num_spec_compressed_words
read_buffer = (c_char * (4 * nwords))()
self.read_into_buffer(read_buffer)
dmin = i * ddes_size
dmax = (i + 1) * ddes_size
self.data.compressed_data[dmin:dmax] = self.decompress_data(
read_buffer, self.data.compressed_data[dmin:dmax]
)
def read_log_info(self):
self.read_into_buffer(self.log.version)
@staticmethod
def understand(raw_file_path):
try:
return IsisRawReader.is_raw_file(raw_file_path)
except IOError:
return False
@staticmethod
def is_raw_file(raw_file_path):
file_ext = raw_file_path.split(".")[-1].lower()
if file_ext != "raw":
return False
return True
def open_raw_file(self, raw_file_path):
self.raw_file = open(raw_file_path, "rb")
def read_raw_file(self, raw_file_path):
self.open_raw_file(raw_file_path)
if self.raw_file.tell() != 0:
pos = self.raw_file.tell()
print(f"Reseting raw_file cursor from {pos} to 0 before reading")
self.reset_file_cursor()
self.read_summary_info()
self.read_run_info()
self.read_instrument_info()
self.read_sample_info()
self.read_dae_info()
self.read_time_channel_info()
self.read_user_info()
self.read_data_info()
self.read_log_info()
self.reset_file_cursor()
def read_into_buffer(self, _buffer):
self.raw_file.readinto(_buffer)
def move_file_cursor(self, pos):
self.raw_file.seek(pos)
def reset_file_cursor(self):
self.move_file_cursor(0)
def print_struct(self, data_struct):
try:
for field_name, field_type in data_struct._fields_:
print(field_name, getattr(data_struct, field_name))
except AttributeError:
try:
for elem in data_struct:
for field_name, field_type in elem._fields_:
print(field_name, getattr(elem, field_name))
except AttributeError:
print("Cannot read data_struct (can only print ctypes Structures)")
def output_tofraw_file(self, output_filename):
"""
Converts info in memory to TOFRaw NeXus format
and outputs to output_filename.
The NeXus format used here follows the guide here:
https://www.nexusformat.org/TOFRaw.html
"""
def load_beamline(nxs_file):
name = "raw_data_1/beamline"
beamline = nxs_file.create_dataset(name, (1,), dtype="|S3")
beamline[0] = self.summary.header.instrument_name
def load_collection_time(nxs_file):
name = "raw_data_1/collection_time"
_type = np.dtype("f4")
collection_time = nxs_file.create_dataset(name, (1,), dtype=_type)
collection_time[0] = self.run.params.run_duration
def load_definition(nxs_file):
name = "raw_data_1/definition"
definition = nxs_file.create_dataset(name, (1,), dtype="|S6")
definition[0] = b"TOFRAW"
def load_definition_local(nxs_file):
name = "raw_data_1/definition_local"
definition_local = nxs_file.create_dataset(name, (1,), dtype="|S10")
definition_local[0] = b"ISISTOFRAW"
def load_detector_1(nxs_file, num_detectors, num_channels):
grp = nxs_file.create_group("raw_data_1/detector_1")
name = "counts"
# 1D raw counts need reshaping per pixel with
# monitor channels removed to be consistent with .nxs
raw_data = self.get_counts_per_pixel(num_detectors, num_channels)
raw_shape = raw_data.shape
nxs_shape = (raw_shape[0], raw_shape[1] - 5, raw_shape[2] - 1)
_type = np.dtype("i4")
counts = grp.create_dataset(name, nxs_shape, dtype=_type)
counts[0] = raw_data[:, 1:-4, 1:]
name = "period_index"
_type = np.dtype("i4")
period_idx = grp.create_dataset(name, (1,), dtype=_type)
period_idx[0] = self.time_channel.num_periods
"""
NOTE:
The .raw spectrum_number_table is inconsistent with the .nxs
detector_1/spectrum_index.
The former gives a 1D array from the top right of each detector
moving down each column (i.e for 64x64 detector [64,63,..]).
The .nxs index just gives [i for i in range(num pixels)].
When visualising both give the same laue plots, consistent with
SXD2001 only when using the indexing e.g. for a 64x64 detector:
np.arange(min_pixel_idx_range, max_pixel_idx_range).reshape(64,64).T
"""
name = "spectrum_index"
shape = (num_detectors - 1,)
_type = np.dtype("i4")
spectrum_idx = grp.create_dataset(name, shape, dtype=_type)
spectrum_idx[:] = self.instrument.spectrum_number_table[:]
name = "time_of_flight"
shape = (num_channels,)
_type = np.dtype("f4")
tof = grp.create_dataset(name, shape, dtype=_type)
tof[:] = np.array(self.time_channel.boundaries[:])
def load_duration(nxs_file):
name = "raw_data_1/duration"
_type = np.dtype("f4")
duration = nxs_file.create_dataset(name, (1,), dtype=_type)
duration[0] = self.run.params.run_duration
def convert_to_nxs_date(raw_date):
date_dict = {
"JAN": "1",
"FEB": "2",
"MAR": "3",
"APR": "4",
"MAY": "5",
"JUN": "6",
"JUL": "7",
"AUG": "8",
"SEP": "9",
"OCT": "10",
"NOV": "11",
"DEC": "12",
}
date = raw_date.decode().rstrip()
day, month, year = date.split("-")
return "-".join([year, date_dict[month], day])
def load_end_time(nxs_file):
name = "raw_data_1/end_time"
end_time = nxs_file.create_dataset(name, (1,), dtype="|S19")
raw_date = convert_to_nxs_date(self.run.params.end_date)
raw_time = self.run.params.end_time.decode()
end_time[0] = (raw_date + "T" + raw_time).encode()
def load_experiment_identifier(nxs_file):
name = "raw_data_1/experiment_identifier"
exp_identifier = nxs_file.create_dataset(name, (1,), dtype="|S7")
exp_identifier[0] = str(self.run.params.proposal_num).encode()
def load_frame_log(nxs_file):
nxs_file.create_group("raw_data_1/framelog")
def load_good_frames(nxs_file):
name = "raw_data_1/good_frames"
_type = np.dtype("i4")
good_frames = nxs_file.create_dataset(name, (1,), dtype=_type)
good_frames[0] = self.run.params.good_frames
def load_instrument(nxs_file):
inst_grp = nxs_file.create_group("raw_data_1/instrument")
dae_grp = nxs_file.create_group("raw_data_1/instrument/dae")
# Unable to find equivalent in .raw
name = "detector_table_file"
dae_grp.create_dataset(name, (1,), dtype="|S28")
name = "period_index"
_type = np.dtype("i4")
period_idx = dae_grp.create_dataset(name, (1,), dtype=_type)
period_idx[0] = self.time_channel.num_periods
# Unable to find equivalent in .raw
name = "spectra_table_file"
_type = np.dtype("S35")
dae_grp.create_dataset(name, (1,), dtype=_type)
tc1_grp = dae_grp.create_group("time_channels_1")
name = "time_of_flight"
tc1_grp[name] = nxs_file["raw_data_1/detector_1/time_of_flight"]
name = "time_of_flight_raw"
shape = (len(self.time_channel.boundaries[:]),)
_type = np.dtype("f4")
tof_raw = tc1_grp.create_dataset(name, shape, dtype=_type)
tof_raw[:] = np.array(self.time_channel.raw_boundaries[:]) * 31.25
# Unable to find equivalent in .raw
dae_grp.create_dataset("type", (1,), dtype=np.dtype("f4"))
vetos_grp = dae_grp.create_group("vetos")
# Unable to find equivalent in .raw
name = "ISIS_50Hz"
_type = np.dtype("i4")
vetos_grp.create_dataset(name, (1,), dtype=_type)
# Unable to find equivalent in .raw
name = "TS2_pulse"
_type = np.dtype("i4")
vetos_grp.create_dataset(name, (1,), dtype=_type)
name = "ext0"
_type = np.dtype("i4")
ext0 = vetos_grp.create_dataset(name, (1,), dtype=_type)
ext0[0] = self.dae.params.external_vetoes[0]
name = "ext1"
_type = np.dtype("i4")
ext1 = vetos_grp.create_dataset(name, (1,), dtype=_type)
ext1[0] = self.dae.params.external_vetoes[1]
name = "ext2"
_type = np.dtype("i4")
ext2 = vetos_grp.create_dataset(name, (1,), dtype=_type)
ext2[0] = self.dae.params.external_vetoes[2]
# Unable to find equivalent in .raw
name = "ext3"
_type = np.dtype("i4")
vetos_grp.create_dataset(name, (1,), dtype=_type)
name = "fermi_chopper0"
_type = np.dtype("i4")
f_chopper_0 = vetos_grp.create_dataset(name, (1,), dtype=_type)
f_chopper_0[0] = self.instrument.params.chopper_freq_1
name = "fermi_chopper1"
_type = np.dtype("i4")
f_chopper_1 = vetos_grp.create_dataset(name, (1,), dtype=_type)
f_chopper_1[0] = self.instrument.params.chopper_freq_2
name = "fermi_chopper2"
_type = np.dtype("i4")
f_chopper_2 = vetos_grp.create_dataset(name, (1,), dtype=_type)
f_chopper_2[0] = self.instrument.params.chopper_freq_3
# Unable to find equivalent in .raw
name = "fermi_chopper3"
_type = np.dtype("i4")
vetos_grp.create_dataset(name, (1,), dtype=_type)
# Unable to find equivalent in .raw
name = "fifo"
_type = np.dtype("i4")
vetos_grp.create_dataset(name, (1,), dtype=_type)
# Unable to find equivalent in .raw
name = "msmode"
_type = np.dtype("i4")
vetos_grp.create_dataset(name, (1,), dtype=_type)
name = "smp"
_type = np.dtype("i4")
smp = vetos_grp.create_dataset(name, (1,), dtype=_type)
smp[0] = self.dae.params.secondary_master_pulse
# Unable to find equivalent in .raw
name = "writing_table_file"
_type = np.dtype("S30")
dae_grp.create_dataset(name, (1,), dtype=_type)
d1_grp = inst_grp.create_group("detector_1")
d1_grp["counts"] = nxs_file["raw_data_1"]["detector_1"]["counts"]
name = "delt"
shape = (len(self.instrument.hold_off_table[:-4]),)
_type = np.dtype("f4")
d1_delt = d1_grp.create_dataset(name, shape, dtype=_type)
d1_delt[:] = self.instrument.hold_off_table[:-4]
name = "distance"
shape = (len(self.instrument.L2_table[:-4]),)
_type = np.dtype("f4")
d1_L2 = d1_grp.create_dataset(name, shape, dtype=_type)
d1_L2[:] = self.instrument.L2_table[:-4]
# From .nxs examples, looks to be a sum of the longitude angles
# See https://www.nexusformat.org/TOFRaw.html
name = "polar_angle"
shape = (len(self.instrument.two_theta) - 4,)
_type = np.dtype("f4")
d1_polar_angle = d1_grp.create_dataset(name, shape, dtype=_type)
d1_polar_angle[:] = self.instrument.two_theta[:-4]
name = "source_detector_distance"
_type = np.dtype("f4")
d1_detector_dist = d1_grp.create_dataset(name, (1,), dtype=_type)
d1_detector_dist[0] = self.instrument.params.LOQ_source_detector_distance
name = "spectrum_index"
d1_grp[name] = nxs_file["raw_data_1"]["detector_1"]["spectrum_index"][:]
name = "time_of_flight"
nxs_path = "raw_data_1/detector_1/time_of_flight"
d1_grp[name] = nxs_file[nxs_path]
name = "time_of_flight_raw"
nxs_path = "raw_data_1/instrument/dae/time_channels_1/time_of_flight_raw"
d1_grp[name] = nxs_file[nxs_path]
moderator_grp = inst_grp.create_group("moderator")
# Unable to find equivalent in .raw
name = "distance"
_type = np.dtype("f4")
moderator_grp.create_dataset(name, (1,), dtype=_type)
name = "name"
_type = np.dtype("S3")
m_name = inst_grp.create_dataset(name, (1,), dtype=_type)
m_name[0] = self.summary.header.instrument_name
# Unable to find equivalent in .raw
source_grp = inst_grp.create_group("source")
source_grp.create_dataset("name", (1,), dtype=np.dtype("S4"))
source_grp.create_dataset("probe", (1,), dtype=np.dtype("S8"))
source_grp.create_dataset("type", (1,), dtype=np.dtype("S21"))
def load_isis_vms_compat(nxs_file):
def extract_struct_to_arr(data_struct, data_arr, data_type=None):
count = 0
for i in data_struct._fields_:
try:
if data_type is None:
data_arr[count] = getattr(data_struct, i[0])
else:
data_arr[count] = data_type(getattr(data_struct, i[0]))
count += 1
except TypeError:
val = getattr(data_struct, i[0])
for j in val:
if data_type is None:
data_arr[count] = j
else:
data_arr[count] = data_type(j)
count += 1
grp = nxs_file.create_group("raw_data_1/isis_vms_compat")
name = "ADD"
_type = np.dtype("i4")
add = grp.create_dataset(name, (9,), dtype=_type)
for c, i in enumerate(self.info_positions._fields_):
add[c] = getattr(self.info_positions, i[0])
name = "CODE"
shape = (len(self.instrument.UTn_tables_code),)
_type = np.dtype("i4")
code = grp.create_dataset(name, shape, dtype=_type)
code[:] = self.instrument.UTn_tables_code[:]
name = "CRAT"
shape = (len(self.dae.detector_crate_nums),)
_type = np.dtype("i4")
crat = grp.create_dataset(name, shape, dtype=_type)
crat[:] = self.dae.detector_crate_nums[:]
# Unable to find equivalent in .raw
name = "CRPB"
shape = (32, 1)
_type = np.dtype("S4")
grp.create_dataset(name, shape, dtype=_type)
# Unable to find equivalent in .raw
name = "CSPB"
shape = (64, 1)
_type = np.dtype("S4")
grp.create_dataset(name, shape, dtype=_type)
name = "DAEP"
_type = np.dtype("i4")
daep = grp.create_dataset(name, (64,), dtype=_type)
extract_struct_to_arr(self.dae.params, daep)
name = "DELT"
shape = (len(self.instrument.hold_off_table),)
_type = np.dtype("f4")
delt = grp.create_dataset(name, shape, dtype=_type)
delt[:] = self.instrument.hold_off_table[:]
name = "FORM"
_type = np.dtype("i4")
form = grp.create_dataset(name, (1,), dtype=_type)
form[0] = self.summary.format[0]
_type = np.dtype("S80")
# hdr = grp.create_dataset("HDR", (1,), dtype=_type)
# extract_struct_to_arr(self.summary.header, hdr)
name = "IRPB"
_type = np.dtype("i4")
irpb = grp.create_dataset(name, (32,), dtype=_type)
extract_struct_to_arr(self.run.params, irpb)
name = "ISPB"
_type = np.dtype("i4")
ispb = grp.create_dataset(name, (64,), dtype=_type)
extract_struct_to_arr(self.sample.params, ispb)
name = "IVPB"
_type = np.dtype("i4")
ivpb = grp.create_dataset(name, (64,), dtype=_type)
extract_struct_to_arr(self.instrument.params, ivpb)
name = "LEN2"
shape = (len(self.instrument.L2_table),)
_type = np.dtype("f4")
len2 = grp.create_dataset(name, shape, dtype=_type)
len2[:] = self.instrument.L2_table[:]
name = "MDET"
shape = (len(self.instrument.monitor_detector_number),)
_type = np.dtype("i4")
mdet = grp.create_dataset(name, shape, dtype=_type)
mdet[:] = self.instrument.monitor_detector_number[:]
name = "MODN"
shape = (len(self.dae.detector_module_nums),)
_type = np.dtype("i4")
modn = grp.create_dataset(name, shape, dtype=_type)
modn[:] = self.dae.detector_module_nums[:]
name = "MONP"
shape = len(
self.instrument.monitor_prescale_val,
)
_type = np.dtype("i4")
monp = grp.create_dataset(name, shape, dtype=_type)
monp[:] = self.instrument.monitor_prescale_val[:]
name = "MPOS"
shape = (len(self.dae.detector_module_positions),)
_type = np.dtype("i4")
mpos = grp.create_dataset(name, shape, dtype=_type)
mpos[:] = self.dae.detector_module_positions[:]
name = "NAME"
_type = np.dtype("S8")
name = grp.create_dataset(name, (1,), dtype=_type)
name[:] = self.instrument.name[:]
name = "NDET"
_type = np.dtype("i4")
ndet = grp.create_dataset(name, (1,), dtype=_type)