-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathread_hdf_file.py
806 lines (558 loc) · 25.7 KB
/
read_hdf_file.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
import h5py
import re
import numpy
class ParticlesFunctor():
"""
Collect values(weighting, position, momentum) from paticle dataset in hdf file.
positions -- group of position coords
momentum -- group of momentum coords
weightins -- values of weights for particles
"""
def __init__(self):
self.momentum = []
self.weighting = []
self.positions = []
self.bound_electrons = ""
self.position_offset = []
def __call__(self, name, node):
if isinstance(node, h5py.Dataset):
if node.name.endswith('weighting'):
self.weighting = node.name
if node.name.endswith('boundElectrons'):
self.bound_electrons = node.name
if isinstance(node, h5py.Group):
if node.name.endswith('position'):
self.positions.append(node)
if node.name.endswith('momentum'):
self.momentum.append(node)
if node.name.endswith('positionOffset'):
self.position_offset.append(node)
return None
class MassFunctor():
"""
"""
def __init__(self):
self.mass = []
def __call__(self, name, node):
if isinstance(node, h5py.Dataset):
if node.name.endswith('mass'):
self.weighting = node.value
if isinstance(node, h5py.Group):
if node.name.endswith('mass'):
self.mass = node.attrs['value']
return None
class ParticlesGroups():
"""
Collect particles groups from hdf file
particles_name -- name of main partcles group
"""
def __init__(self, particles_name):
self.name_particles = particles_name
self.particles_groups = []
def __call__(self, name, node):
if isinstance(node, h5py.Group):
name_idx = node.name.find(self.name_particles)
if name_idx != -1:
group_particles_name = node.name[name_idx + len(self.name_particles) + 1:]
if group_particles_name.find('/') == -1 and group_particles_name != '':
self.particles_groups.append(node)
return None
class ReadPatchGroup():
def __init__(self):
self.patch_group = []
def __call__(self, name, node):
if isinstance(node, h5py.Group):
if node.name.endswith('particlePatches'):
self.patch_group.append(node)
class ReadPatchValues():
def __init__(self):
self.numParticles = []
self.numParticlesOffset = []
def __call__(self, name, node):
if isinstance(node, h5py.Dataset):# numParticles
if node.name.endswith('numParticles'):
self.numParticles = node.value
if node.name.endswith('numParticlesOffset'):
self.numParticlesOffset = node.value
class points_writer():
"""
Write dataset into result hdf file
name_dataset -- name recorded dataset
hdf_file -- result hdf file
result_points -- points to write to hdf file
"""
def __init__(self, hdf_file, library_datasets, name_dataset):
self.dataset_x = name_dataset + '/x'
self.dataset_y = name_dataset + '/y'
self.dataset_z = name_dataset + '/z'
self.vector_x = library_datasets[self.dataset_x]
self.vector_y = library_datasets[self.dataset_y]
self.vector_z = library_datasets[self.dataset_z]
self.hdf_file = hdf_file
def __call__(self, name, node):
if isinstance(node, h5py.Dataset):
if node.name.endswith(self.dataset_x):
attributes = {}
for attr in node.attrs.keys():
attributes[attr] = node.attrs[attr]
node_name = node.name
current_dtype = self.hdf_file[node.name].dtype
del self.hdf_file[node.name]
dset = self.hdf_file.create_dataset(node_name, data=self.vector_x, dtype=current_dtype)
for attr in attributes:
dset.attrs[attr] = attributes[attr]
elif node.name.endswith(self.dataset_y):
attributes = {}
for attr in node.attrs.keys():
attributes[attr] = node.attrs[attr]
node_name = node.name
current_dtype = self.hdf_file[node.name].dtype
del self.hdf_file[node.name]
dset = self.hdf_file.create_dataset(node_name, data=self.vector_y, dtype=current_dtype)
for attr in attributes:
dset.attrs[attr] = attributes[attr]
elif node.name.endswith(self.dataset_z):
attributes = {}
for attr in node.attrs.keys():
attributes[attr] = node.attrs[attr]
node_name = node.name
current_dtype = self.hdf_file[node.name].dtype
del self.hdf_file[node.name]
dset = self.hdf_file.create_dataset(node_name, data=self.vector_z, dtype=current_dtype)
for attr in attributes:
dset.attrs[attr] = attributes[attr]
return None
class vector_writer():
"""
Write dataset into result hdf file
name_dataset -- name recorded dataset
hdf_file -- result hdf file
result_points -- points to write to hdf file
"""
def __init__(self, hdf_file, vector_values, name_dataset, start_dimension):
self.dataset_x = name_dataset + '/x'
self.dataset_y = name_dataset + '/y'
self.dataset_z = name_dataset + '/z'
self.vector_values = vector_values
self.hdf_file = hdf_file
self.is_first_part = True
self.start_dimension = start_dimension
def __call__(self, name, node):
if isinstance(node, h5py.Dataset):
if node.name.endswith(self.dataset_x):
self.choose_writing_type(node, self.start_dimension)
elif node.name.endswith(self.dataset_y):
self.choose_writing_type(node, self.start_dimension + 1)
elif node.name.endswith(self.dataset_z):
self.choose_writing_type(node, self.start_dimension + 2)
def choose_writing_type(self, node, type_idx):
if self.is_first_part:
self.first_part_writing(node, self.vector_values[:, type_idx])
else:
self.resize_writing(node, self.vector_values[:, type_idx])
def node_exists(self, name):
e = True
try:
self.hdf_file[name]
except KeyError:
e = False
return e
def first_part_writing(self, node, values):
attributes = {}
for attr in node.attrs.keys():
attributes[attr] = node.attrs[attr]
node_name = node.name
current_dtype = self.hdf_file[node.name].dtype
del self.hdf_file[node.name]
dset = self.hdf_file.create_dataset(node_name, maxshape=(None,), data=values, dtype=current_dtype, chunks=True)
for attr in attributes:
dset.attrs[attr] = attributes[attr]
return None
def resize_writing(self, node, values):
node_name = node.name
self.hdf_file[node_name].resize((self.hdf_file[node_name].shape[0] + values.shape[0]), axis=0)
self.hdf_file[node_name][-values.shape[0]:] = values
class dataset_writer():
"""
Write dataset into result hdf file
name_dataset -- name recorded dataset
hdf_file -- result hdf file
result_points -- points to write to hdf file
"""
def __init__(self, hdf_file, values, name_dataset):
self.values = values
self.hdf_file = hdf_file
self.name_dataset = name_dataset
self.is_first_part = True
def __call__(self, name, node):
if isinstance(node, h5py.Dataset):
if node.name.endswith(self.name_dataset):
if self.is_first_part:
self.first_part_writing(node, self.values)
else:
self.resize_writing(node, self.values)
def first_part_writing(self, node, values):
attributes = {}
for attr in node.attrs.keys():
attributes[attr] = node.attrs[attr]
node_name = node.name
current_dtype = self.hdf_file[node.name].dtype
del self.hdf_file[node.name]
dset = self.hdf_file.create_dataset(node_name, maxshape=(None,), data=values, dtype=current_dtype,
chunks=True)
for attr in attributes:
dset.attrs[attr] = attributes[attr]
return None
def resize_writing(self, node, values):
node_name = node.name
self.hdf_file[node_name].resize((self.hdf_file[node_name].shape[0] + values.shape[0]), axis=0)
self.hdf_file[node_name][-values.shape[0]:] = values
return None
class patch_values_writer():
"""
Write dataset into result hdf file
name_dataset -- name recorded dataset
hdf_file -- result hdf file
result_points -- points to write to hdf file
"""
def __init__(self, hdf_file, numParticlesOffset, numParticles):
self.numParticles = numParticles
self.numParticlesOffset = numParticlesOffset
self.hdf_file = hdf_file
def __call__(self, name, node):
if isinstance(node, h5py.Dataset):
if node.name.endswith('numParticles'):
node_name = node.name
del self.hdf_file[node.name]
dset = self.hdf_file.create_dataset(node_name, data=self.numParticles)
if node.name.endswith('numParticlesOffset'):
node_name = node.name
del self.hdf_file[node.name]
dset = self.hdf_file.create_dataset(node_name, data=self.numParticlesOffset)
return None
class Dataset_Reader():
"""
Read datasets values from hdf file
name_dataset -- name of base group
"""
def __init__(self, name_dataset):
self.vector = ['', '', '']
self.unit = [1., 1., 1.]
self.name_dataset = name_dataset
def __call__(self, name, node):
dataset_x = self.name_dataset + '/x'
dataset_y = self.name_dataset + '/y'
dataset_z = self.name_dataset + '/z'
if isinstance(node, h5py.Dataset):
if node.name.endswith(dataset_x) and node.shape != None:
self.vector[0] = node.name
self.unit[0] = node.attrs["unitSI"]
if node.name.endswith(dataset_y) and node.shape != None:
self.vector[1] = node.name
self.unit[1] = node.attrs["unitSI"]
if node.name.endswith(dataset_z) and node.shape != None:
self.vector[2] = node.name
self.unit[2] = node.attrs["unitSI"]
return None
def get_unit_si_array(self):
array_unit_SI = []
if self.get_dimension() == 3:
array_unit_SI = [self.unit[0], self.unit[1], self.unit[2]]
elif self.get_dimension() == 2:
array_unit_SI = [self.unit[0], self.unit[1]]
return array_unit_SI
def get_dimension(self):
"""
get dimension of particles datasets
"""
size = len(list(filter(lambda x: (x != ""), self.vector)))
return size
def get_particles_name(hdf_file):
""" Get name of particles group """
particles_name = ''
if hdf_file.attrs.get('particlesPath') != None:
particles_name = hdf_file.attrs.get('particlesPath')
particles_name = decode_name(particles_name)
else:
particles_name = 'particles'
return particles_name
def decode_name(attribute_name):
""" Decode name from binary """
decoding_name = attribute_name.decode('ascii', errors='ignore')
decoding_name = re.sub(r'\W+', '', decoding_name)
return decoding_name
def create_points_array(hdf_file, idx_start, idx_end, coord_collection, momentum_collection, bound_electrons):
"""
create array of 2-d, 3-d points from datasets
coord_collection -- datasets from hdf file
"""
vector_coords = []
for i in range(0, len(coord_collection.vector)):
if coord_collection.vector[i] != "":
current_vector = hdf_file[coord_collection.vector[i]][()][idx_start:idx_end]
vector_coords.append(current_vector)
for j in range(0, len(momentum_collection.vector)):
if momentum_collection.vector[j] != "":
current_vector = hdf_file[momentum_collection.vector[j]][()][idx_start:idx_end]
vector_coords.append(current_vector)
if bound_electrons != '':
bound_vector_values = hdf_file[bound_electrons][()][idx_start:idx_end]
vector_coords.append(bound_vector_values)
vector_coords = numpy.array(vector_coords)
vector_coords = vector_coords.transpose()
return vector_coords
def get_weights(hdf_file, idx_start, idx_end, path_weights):
weights = hdf_file[path_weights][()][idx_start:idx_end]
return weights
def get_position_offset(hdf_file, idx_start, idx_end, position_offset):
vector_offset = []
for i in range(0, len(position_offset.vector)):
if position_offset.vector[i] != "":
current_vector = hdf_file[position_offset.vector[i]][()][idx_start:idx_end]
vector_offset.append(current_vector)
vector_offset = numpy.array(vector_offset)
vector_offset = vector_offset.transpose()
return vector_offset
def create_points_array_bound_electrons(coord_collection, momentum_collection, bound_electrons):
"""
create array of 2-d, 3-d points from datasets
coord_collection -- datasets from hdf file
"""
vector_coords = []
dimension_coord = coord_collection.get_dimension()
dimension_momentum = momentum_collection.get_dimension()
if dimension_coord == 3 and dimension_momentum == 3:
vector_coords = [list(x) for x in
zip(coord_collection.vector_x, coord_collection.vector_y, coord_collection.vector_z,
momentum_collection.vector_x, momentum_collection.vector_y, momentum_collection.vector_z,
bound_electrons)]
elif dimension_coord == 3 and dimension_momentum == 2:
vector_coords = [list(x) for x in
zip(coord_collection.vector_x, coord_collection.vector_y, coord_collection.vector_z,
momentum_collection.vector_x, momentum_collection.vector_y, bound_electrons)]
elif dimension_coord == 2 and dimension_momentum == 3:
vector_coords = [list(x) for x in
zip(coord_collection.vector_x, coord_collection.vector_y,
momentum_collection.vector_x, momentum_collection.vector_y, momentum_collection.vector_z,
bound_electrons)]
elif dimension_coord == 2 and dimension_momentum == 2:
vector_coords = [list(x) for x in
zip(coord_collection.vector_x, coord_collection.vector_y,
momentum_collection.vector_x, momentum_collection.vector_y, bound_electrons)]
return vector_coords
def create_dataset_from_point_array(points, name_dataset):
vector_x = []
vector_y = []
vector_z = []
weighting = []
dimension = len(points[name_dataset][0].coords)
coordinates_dataset = points[name_dataset]
for i in range(0, len(coordinates_dataset)):
if dimension == 3:
vector_x.append(coordinates_dataset[i].coords[0])
vector_y.append(coordinates_dataset[i].coords[1])
vector_z.append(coordinates_dataset[i].coords[2])
weighting.append(coordinates_dataset[i].weight)
if dimension == 2:
vector_x.append(coordinates_dataset[i].coords[0])
vector_y.append(coordinates_dataset[i].coords[1])
weighting.append(coordinates_dataset[i].weight)
return vector_x, vector_y, vector_z, weighting
def create_library_of_datasets(points):
datasets = {}
position_x, position_y, position_z, weighting = create_dataset_from_point_array(points, 'position')
momentum_x, momentum_y, momentum_z, weighting = create_dataset_from_point_array(points, 'momentum')
datasets['position/x'] = position_x
datasets['position/y'] = position_y
datasets['position/z'] = position_z
datasets['momentum/x'] = momentum_x
datasets['momentum/y'] = momentum_y
datasets['momentum/z'] = momentum_z
datasets['weighting'] = weighting
return datasets
def read_mass(group):
hdf_mass = MassFunctor()
group.visititems(hdf_mass)
return hdf_mass.mass
def read_points_group(group):
"""
convert values from position and momentum datasets into points
group -- base group of points from hdf file
"""
hdf_datasets = ParticlesFunctor()
group.visititems(hdf_datasets)
weighting = hdf_datasets.weighting
bound_electrons = hdf_datasets.bound_electrons
position_values = Dataset_Reader('position')
momentum_values = Dataset_Reader('momentum')
if len(hdf_datasets.positions) == 0 or len(hdf_datasets.momentum) == 0:
return [], [], [], [], []
position_group = hdf_datasets.positions[0]
momentum_group = hdf_datasets.momentum[0]
position_group.visititems(position_values)
momentum_group.visititems(momentum_values)
points = []
if len(bound_electrons) == 0:
points = create_points_array(position_values, momentum_values)
else:
points = create_points_array_bound_electrons(position_values, momentum_values, bound_electrons)
dimention_position = position_values.get_dimension()
unit_SI_position = position_values.get_unit_si_array()
unit_SI_momentum = momentum_values.get_unit_si_array()
dimention_momentum = momentum_values.get_dimension()
dimensions = Dimensions(dimention_position, dimention_momentum)
return points, weighting, dimensions, unit_SI_position, unit_SI_momentum
def read_points_group_v2(hdf_datasets):
"""
convert values from position and momentum datasets into points
group -- base group of points from hdf file
"""
position_values = Dataset_Reader('position')
momentum_values = Dataset_Reader('momentum')
if len(hdf_datasets.positions) == 0 or len(hdf_datasets.momentum) == 0:
return position_values, momentum_values, [], []
position_group = hdf_datasets.positions[0]
momentum_group = hdf_datasets.momentum[0]
position_group.visititems(position_values)
momentum_group.visititems(momentum_values)
return position_values, momentum_values, hdf_datasets.weighting, hdf_datasets.bound_electrons
def read_position_offset(hdf_datasets):
position_offset_values = Dataset_Reader('positionOffset')
position_offset_group = hdf_datasets.position_offset[0]
position_offset_group.visititems(position_offset_values)
offset_unit_si = position_offset_values.get_unit_si_array()
return position_offset_values, offset_unit_si
def write_group_values(hdf_file_reduction, group, library_datasets, offset):
"""
write values from point library to hdf file
hdf_file_reduction -- result file
group -- base group of partilces from original file
result -- library points
"""
hdf_datasets = ParticlesFunctor()
group.visititems(hdf_datasets)
position_values = Dataset_Reader('position')
momentum_values = Dataset_Reader('momentum')
position_offset_values = Dataset_Reader('positionOffset')
position_offset_group = hdf_datasets.position_offset[0]
position_group = hdf_datasets.positions[0]
momentum_group = hdf_datasets.momentum[0]
position_group.visititems(position_values)
momentum_group.visititems(momentum_values)
position_offset_group.visititems(position_offset_values)
writen_position = points_writer(hdf_file_reduction, library_datasets, 'position')
writen_momentum = points_writer(hdf_file_reduction, library_datasets, 'momentum')
writen_weighting = dataset_writer(hdf_file_reduction, library_datasets)
position_group.visititems(writen_position)
momentum_group.visititems(writen_momentum)
group.visititems(writen_weighting)
def create_datasets_from_vector(reduced_data, dimensions, position_offset):
library_datasets = {}
size_values = len(reduced_data[0]) - 1
position_x = reduced_data[:, 0]
position_y = reduced_data[:, 1]
position_z = []
if dimensions.dimension_position == 3:
position_z = reduced_data[:, 2]
momentum_x = reduced_data[:, dimensions.dimension_position]
momentum_y = reduced_data[:, dimensions.dimension_position + 1]
momentum_z = []
if dimensions.dimension_momentum == 3:
momentum_z = reduced_data[:, dimensions.dimension_position + 2]
bound_electrons = reduced_data[:, size_values]
position_offset_x = position_offset[:, 0]
position_offset_y = position_offset[:, 1]
position_offset_z = []
if dimensions.dimension_position == 3:
position_offset_z = position_offset[:, 2]
library_datasets['position/x'] = position_x
library_datasets['position/y'] = position_y
library_datasets['position/z'] = position_z
library_datasets['positionOffset/x'] = position_offset_x
library_datasets['positionOffset/y'] = position_offset_y
library_datasets['positionOffset/z'] = position_offset_z
library_datasets['momentum/x'] = momentum_x
library_datasets['momentum/y'] = momentum_y
library_datasets['momentum/z'] = momentum_z
library_datasets['boundElectrons'] = bound_electrons
return library_datasets
def write_group_values(hdf_file_reduction, group, reduced_data, weights):
"""
write values from point library to hdf fileParticlesGroups
hdf_file_reduction -- result file
group -- base group of partilces from original file
result -- library points
"""
hdf_datasets = ParticlesFunctor()
group.visititems(hdf_datasets)
position_values = Dataset_Reader('position')
momentum_values = Dataset_Reader('momentum')
position_offset_values = Dataset_Reader('positionOffset')
position_group = hdf_datasets.positions[0]
momentum_group = hdf_datasets.momentum[0]
position_offset_group = hdf_datasets.position_offset[0]
position_group.visititems(position_values)
momentum_group.visititems(momentum_values)
position_offset_group.visititems(position_offset_values)
write_position = points_writer(hdf_file_reduction, reduced_data, 'position')
write_momentum = points_writer(hdf_file_reduction, reduced_data, 'momentum')
write_position_offset = points_writer(hdf_file_reduction, reduced_data, 'positionOffset')
write_weighting = dataset_writer(hdf_file_reduction, weights, 'weighting')
write_bound_electrons = dataset_writer(hdf_file_reduction, reduced_data['boundElectrons'], 'boundElectrons')
position_group.visititems(write_position)
momentum_group.visititems(write_momentum)
position_offset_group.visititems(write_position_offset)
group.visititems(write_weighting)
group.visititems(write_bound_electrons)
def write_patch_group(group, hdf_file_reduction, num_particles_offset, num_particles):
patch_group = ReadPatchGroup()
group.visititems(patch_group)
patch_writter = PatchValuesWriter(hdf_file_reduction, num_particles_offset, num_particles)
patch_group.patch_group[0].visititems(patch_writter)
def read_patches_values(group):
patch_group = ReadPatchGroup()
group.visititems(patch_group)
patch_values = ReadPatchValues()
patch_group.patch_group[0].visititems(patch_values)
return patch_values.numParticles, patch_values.numParticlesOffset
def get_absolute_coordinates(data, position_offset, unit_si_offset, unit_si_position, dimensions, unit_si_momentum):
absolute_result = []
unit_si_position = numpy.array(unit_si_position)
unit_si_offset = numpy.array(unit_si_offset)
unit_si_momentum = numpy.array(unit_si_momentum)
i = 0
for point in data:
offset = position_offset[i]
coordinates = numpy.array(point[0:dimensions.dimension_position])
absolute_coordinates = coordinates * unit_si_position + offset * unit_si_offset
momentum = point[dimensions.dimension_position:dimensions.dimension_momentum + dimensions.dimension_position]
absolute_momentum = momentum * unit_si_momentum
other_values = point[dimensions.dimension_momentum + dimensions.dimension_position: len(point)]
absolute_point = numpy.append(absolute_coordinates, absolute_momentum)
absolute_point = numpy.append(absolute_point, other_values)
absolute_result.append(absolute_point.tolist())
i+=1
return absolute_result
def get_relative_coordinates(absolute_coordinates, unit_si_offset,
unit_si_position, dimensions, unit_si_momentum):
relative_result = []
offset = []
unit_si_position = numpy.array(unit_si_position)
unit_si_offset = numpy.array(unit_si_offset)
unit_si_momentum = numpy.array(unit_si_momentum)
for point in absolute_coordinates:
coordinates = numpy.array(point[0:dimensions.dimension_position])
position_offset = numpy.divide(coordinates, unit_si_position)
position_offset = position_offset.astype(int)
offset.append(position_offset.tolist())
relative_coordinates = numpy.divide((coordinates - position_offset * unit_si_offset), unit_si_position)
momentum = point[dimensions.dimension_position:dimensions.dimension_momentum + dimensions.dimension_position]
relative_momentum = numpy.divide(momentum, unit_si_momentum)
other_values = point[dimensions.dimension_momentum + dimensions.dimension_position:
len(point)]
relative_point = numpy.append(relative_coordinates, relative_momentum)
relative_point = numpy.append(relative_point, other_values)
relative_result.append(relative_point.tolist())
relative_result = numpy.array(relative_result)
offset = numpy.array(offset)
return relative_result, offset