-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
1508 lines (1180 loc) · 44 KB
/
util.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
# -*- coding: utf-8 -*-
"""
ovlib.util: Utilities
===============================================================================
"""
def vecarraycross(b, c):
''' cross products of arrays of vectors '''
import numpy as np
bx = vecarrayconvert(b[0])
by = vecarrayconvert(b[1])
bz = vecarrayconvert(b[2])
cx = vecarrayconvert(c[0])
cy = vecarrayconvert(c[1])
cz = vecarrayconvert(c[2])
if np.shape(bx) == np.shape(by) == np.shape(bz) == \
np.shape(cx) == np.shape(cy) == np.shape(cz):
ax = by * cz - bz * cy
ay = bz * cx - bx * cz
az = bx * cy - by * cx
return [ax, ay, az]
else:
print "vecarraycross error: check that the lengths of arguments are equal."
#-------------------------------------------------------------------------------
def vecarraynorm(a):
''' norm of an array of vectors '''
import numpy as np
ax = vecarrayconvert(a[0])
ay = vecarrayconvert(a[1])
az = vecarrayconvert(a[2])
if np.shape(ax) == np.shape(ay) == np.shape(az):
nrm=(ax*ax + ay*ay + az*az)**0.5
else:
print "vecarraynorm error: check that the lengths of arguments are equal."
return nrm
#-------------------------------------------------------------------------------
def vecarraydot(b,c):
''' dot products of an array of vectors '''
import numpy as np
bx = vecarrayconvert(b[0])
by = vecarrayconvert(b[1])
bz = vecarrayconvert(b[2])
cx = vecarrayconvert(c[0])
cy = vecarrayconvert(c[1])
cz = vecarrayconvert(c[2])
if np.shape(bx) == np.shape(by) == np.shape(bz) == \
np.shape(cx) == np.shape(cy) == np.shape(cz):
return bx*cx + by*cy + bz*cz
else:
print "vecarraydot error: check that the lengths of arguments are equal."
#-------------------------------------------------------------------------------
def vecarraygcd(a,b):
''' Compute greatest common denominator for values in two arrays'''
import numpy as np
a = vecarrayconvert(a)
b = vecarrayconvert(b)
if np.shape(a) == np.shape(b):
r = np.zeros( np.size(a) ) # preallocate remainder array
# if b is smaller than floating point error, we will make gcd=1
a[ b < np.spacing(1) ] = 1.0
j = b > np.spacing(1)
if j.any():
while j.any():
r[j] = a[j] % b[j]
a[j] = b[j]
b[j] = r[j]
j[b==0]=False
return a
else:
return a
#-------------------------------------------------------------------------------
def vecarrayconvert(a): # FIXME: Not necessary? Can just use asanyarray?
''' convert any reasonable datatype into an n x 3 vector array'''
import numpy as np
#a = np.asanyarray(a)
#a = np.squeeze( np.float64( list( (a,) )))
#a = np.atleast_2d(np.asarray(a))
# Make the arrays indexable if they are actually scalars
#if np.size(a) == 1:
# a = np.array([a])
a = np.atleast_1d(np.squeeze(np.asanyarray(a)))
return a
#-------------------------------------------------------------------------------
def stereotrans(xyz, center=[0,0,1], south=[0,1,0], east=[1,0,0]):
'''
Perform non-standard stereographic projections following
Kosel TH, J Mater Sci 19 (1984)
'''
import numpy as np
abc = vecarrayconvert(center)
nrmabc = 1.0/vecarraynorm(abc)
uvw = vecarrayconvert(east)
nrmuvw = 1.0/vecarraynorm(uvw)
fed = vecarrayconvert(south) # 'def' is reserved in python
nrmdef = 1.0/vecarraynorm(fed)
xyz = vecarrayconvert(xyz)
nrmxyz = 1.0/vecarraynorm(xyz)
xx = xyz[0]*nrmxyz
yy = xyz[1]*nrmxyz
zz = xyz[2]*nrmxyz
n = np.size(xx)
aa = np.tile(abc[0]*nrmabc,n)
bb = np.tile(abc[1]*nrmabc,n)
cc = np.tile(abc[2]*nrmabc,n)
dd = np.tile(fed[0]*nrmdef,n)
ee = np.tile(fed[1]*nrmdef,n)
ff = np.tile(fed[2]*nrmdef,n)
uu = np.tile(uvw[0]*nrmuvw,n)
vv = np.tile(uvw[1]*nrmuvw,n)
ww = np.tile(uvw[2]*nrmuvw,n)
cosdl = vecarraydot([xx,yy,zz],[dd,ee,ff])
cosmu = vecarraydot([xx,yy,zz],[uu,vv,ww])
cosal = vecarraydot([xx,yy,zz],[aa,bb,cc])
denom = 1.0/(1.0+np.absolute(cosal))
xproj = cosmu * denom
yproj = -cosdl * denom
hemis = np.tile('S',n)
if np.array(n)>1.0:
hemis[cosal<0.0] = 'N'
else:
if cosal<0:
hemis = np.tile('N',n)
return xproj, yproj, hemis
#-------------------------------------------------------------------------------
def stereotransstandard(xyz):
'''
Perform the standard stereographic transform on cartesian vectors
following De Graef & McHenry
Returns projected x, projected y, and the hemisphere of the projection
'''
import numpy as np
x = vecarrayconvert(xyz[0])
y = vecarrayconvert(xyz[1])
z = vecarrayconvert(xyz[2])
nrm = 1.0/vecarraynorm([x,y,z])
denom = nrm/(1.0+abs(z*nrm))
xproj = x * denom
yproj = -y * denom
n = np.shape(x)
hemis = np.tile('S',n)
if n[0]>1.0:
hemis[z<0.0] = 'N'
else:
if z<0.0:
hemis = 'N'
return xproj, yproj, hemis
#-------------------------------------------------------------------------------
def revstereotrans(xyh,center=[0,0,1],south=[0,1,0],east=[1,0,0]):
'''
Reverse non-standard stereographic projection
'''
import numpy as np
import numpy.linalg as npla
abc = vecarrayconvert(center)
nrmabc = 1.0/vecarraynorm(abc)
uvw = vecarrayconvert(east)
nrmuvw = 1.0/vecarraynorm(uvw)
fed = vecarrayconvert(south) # 'def' is reserved in python
nrmdef = 1.0/vecarraynorm(fed)
xproj = vecarrayconvert(xyh[0])
yproj = vecarrayconvert(xyh[1])
hemis = np.array(np.squeeze(xyh[2]))
n = np.shape(xproj)
x = np.zeros(n)
y = np.zeros(n)
z = np.zeros(n)
aa = abc[0]*nrmabc
bb = abc[1]*nrmabc
cc = abc[2]*nrmabc
dd = fed[0]*nrmdef
ee = fed[1]*nrmdef
ff = fed[2]*nrmdef
uu = uvw[0]*nrmuvw
vv = uvw[1]*nrmuvw
ww = uvw[2]*nrmuvw
R = xproj*xproj + yproj*yproj
m = np.ones(np.shape(hemis))
m[hemis=='N'] = -1.0
cosal = (1.0 - R) / (1.0 + R)
denom = abs(cosal) + 1.0
cosmu = xproj * denom
cosdl = yproj * denom
# For each case we determine xyz by solving a system of linear equations
for i in range(0,np.shape(cosal)[0]):
xyzcoef = np.squeeze(np.array([[dd,ee,ff],[uu,vv,ww],[aa,bb,cc]]))
xyzsoln = np.array([cosdl[i], cosmu[i], cosal[i]])
xyz = npla.solve(xyzcoef, xyzsoln)
x[i]= xyz[0]
y[i]=-xyz[1]
z[i]= xyz[2]
# Correct z for the hemisphere
z = z * m
return x,y,z
#-------------------------------------------------------------------------------
def revstereotransstandard(xyh):
'''
Performs the reverse stereographic transform on the projected x and y
coordinates measured from the stereographic projection.
Reverse of De Graef and McHenry procedure.
Returns the cartesian x, y, and z values of the normalized vector.
'''
import numpy as np
xproj = vecarrayconvert(xyh[0])
yproj = vecarrayconvert(xyh[1])
hemis = np.array(np.squeeze(xyh[2]))
R = xproj*xproj + yproj*yproj
m = np.ones(np.shape(hemis))
m[hemis=='N'] = -1.0
z = (m - m * R) / (1.0 + R)
s = m * z + 1.0
x = xproj * s
y = -yproj * s
return x,y,z
#-------------------------------------------------------------------------------
def eatrans(xyz,center=[0,0,1],south=[0,1,0],east=[1,0,0]):
'''
Perform non-standard equal area projections.
Reference:
[1] Kosel TH, J Mater Sci 19 (1984)
'''
import numpy as np
abc = vecarrayconvert(center)
nrmabc = 1.0/vecarraynorm(abc)
uvw = vecarrayconvert(east)
nrmuvw = 1.0/vecarraynorm(uvw)
fed = vecarrayconvert(south) # 'def' is reserved in python
nrmdef = 1.0/vecarraynorm(fed)
xyz = vecarrayconvert(xyz)
nrmxyz = 1.0/vecarraynorm(xyz)
xx = xyz[0]*nrmxyz
yy = xyz[1]*nrmxyz
zz = xyz[2]*nrmxyz
n = np.shape(xx)
aa = np.tile(abc[0]*nrmabc,n)
bb = np.tile(abc[1]*nrmabc,n)
cc = np.tile(abc[2]*nrmabc,n)
dd = np.tile(fed[0]*nrmdef,n)
ee = np.tile(fed[1]*nrmdef,n)
ff = np.tile(fed[2]*nrmdef,n)
uu = np.tile(uvw[0]*nrmuvw,n)
vv = np.tile(uvw[1]*nrmuvw,n)
ww = np.tile(uvw[2]*nrmuvw,n)
cosdl = vecarraydot([xx,yy,zz],[dd,ee,ff])
cosmu = vecarraydot([xx,yy,zz],[uu,vv,ww])
cosal = vecarraydot([xx,yy,zz],[aa,bb,cc])
denom = 1.0/np.sqrt(1.0+np.absolute(cosal))
xproj = cosmu * denom
yproj = -cosdl * denom
hemis = np.tile('S',n)
if np.array(n)>1.0:
hemis[cosal<0.0] = 'N'
else:
if cosal<0:
hemis = np.tile('N',n)
return xproj, yproj, hemis
#-------------------------------------------------------------------------------
def eatransstandard(xyz):
'''
Perform the standard stereographic transform on cartesian vectors
following De Graef & McHenry
Returns projected x, projected y, and the hemisphere of the projection
'''
import numpy as np
x=vecarrayconvert(xyz[0])
y=vecarrayconvert(xyz[1])
z=vecarrayconvert(xyz[2])
nrm=1.0/vecarraynorm([x,y,z])
denom=nrm/np.sqrt(1.0+abs(z*nrm))
xproj = x * denom
yproj = -y * denom
n=np.shape(x)
hemis = np.tile('S',n)
if n[0]>1.0:
hemis[z<0.0] = 'N'
else:
if z<0.0:
hemis = 'N'
return xproj, yproj, hemis
#-------------------------------------------------------------------------------
def reveatrans(xyh,center=[0,0,1],south=[0,1,0],east=[1,0,0]):
'''
Reverse non-standard stereographic projection
'''
import numpy as np
import numpy.linalg as npla
abc = vecarrayconvert(center)
nrmabc = 1.0/vecarraynorm(abc)
uvw = vecarrayconvert(east)
nrmuvw = 1.0/vecarraynorm(uvw)
fed = vecarrayconvert(south) # 'def' is reserved in python
nrmdef = 1.0/vecarraynorm(fed)
xproj = vecarrayconvert(xyh[0])
yproj = vecarrayconvert(xyh[1])
hemis = np.array(np.squeeze(xyh[2]))
n = np.shape(xproj)
x = np.zeros(n)
y = np.zeros(n)
z = np.zeros(n)
aa = abc[0]*nrmabc
bb = abc[1]*nrmabc
cc = abc[2]*nrmabc
dd = fed[0]*nrmdef
ee = fed[1]*nrmdef
ff = fed[2]*nrmdef
uu = uvw[0]*nrmuvw
vv = uvw[1]*nrmuvw
ww = uvw[2]*nrmuvw
R = xproj*xproj + yproj*yproj
m = np.ones(np.shape(hemis))
m[hemis=='N'] = -1.0
cosal = (1.0 - R) / (1.0 + R)
denom = (np.absolute(cosal) + 1.0)**2.0
cosmu = xproj * denom
cosdl = yproj * denom
# For each case we determine xyz by solving a system of linear equations
for i in range(0, np.shape(cosal)[0]):
xyzcoef = np.squeeze(np.array([[dd,ee,ff],[uu,vv,ww],[aa,bb,cc]]))
xyzsoln = np.array([cosdl[i], cosmu[i], cosal[i]])
xyz = npla.solve(xyzcoef, xyzsoln)
x[i]= xyz[0]
y[i]=-xyz[1]
z[i]= xyz[2]
# Correct z for the hemisphere
z = z * m
return x,y,z
#-------------------------------------------------------------------------------
def reveatransstandard(xyh):
'''
Performs the reverse stereographic transform on the projected x and y
coordinates measured from the stereographic projection.
Reverse of De Graef and McHenry procedure.
Returns the cartesian x, y, and z values of the normalized vector.
'''
import numpy as np
xproj = vecarrayconvert(xyh[0])
yproj = vecarrayconvert(xyh[1])
hemis = np.array(np.squeeze(xyh[2]))
R = xproj*xproj + yproj*yproj
m = np.ones(np.shape(hemis))
m[hemis=='N'] = -1.0
z = (m - m * R) / (1.0 + R)
s = (m * z + 1.0)**2.0
x = xproj * s
y = -yproj * s
return x,y,z
#-------------------------------------------------------------------------------
def gnomonictrans(xyz,center=[0,0,1],south=[0,1,0],east=[1,0,0]):
'''
Perform the gnomonic projection, allowing different projection views
'''
import numpy as np
abc = vecarrayconvert(center)
nrmabc = 1.0/vecarraynorm(abc)
uvw = vecarrayconvert(east)
nrmuvw = 1.0/vecarraynorm(uvw)
fed = vecarrayconvert(south) # 'def' is reserved in python
nrmdef = 1.0/vecarraynorm(fed)
xyz = vecarrayconvert(xyz)
nrmxyz = 1.0/vecarraynorm(xyz)
xx = xyz[0]*nrmxyz
yy = xyz[1]*nrmxyz
zz = xyz[2]*nrmxyz
n = np.shape(xx)
aa = np.tile(abc[0]*nrmabc,n)
bb = np.tile(abc[1]*nrmabc,n)
cc = np.tile(abc[2]*nrmabc,n)
dd = np.tile(fed[0]*nrmdef,n)
ee = np.tile(fed[1]*nrmdef,n)
ff = np.tile(fed[2]*nrmdef,n)
uu = np.tile(uvw[0]*nrmuvw,n)
vv = np.tile(uvw[1]*nrmuvw,n)
ww = np.tile(uvw[2]*nrmuvw,n)
cosdl = vecarraydot([xx,yy,zz],[dd,ee,ff])
cosmu = vecarraydot([xx,yy,zz],[uu,vv,ww])
cosal = vecarraydot([xx,yy,zz],[aa,bb,cc])
denom = 1.0/np.absolute(cosal)
xproj = cosmu * denom
yproj = -cosdl * denom
hemis = np.tile('S',n)
if np.array(n)>1.0:
hemis[cosal<0.0] = 'N'
else:
if cosal<0:
hemis = np.tile('N',n)
return xproj, yproj, hemis
#-------------------------------------------------------------------------------
def gnomonictransstandard(xyz):
'''
Perform the standard gnomonic projection
Returns projected x, projected y, and the hemisphere of the projection
'''
import numpy as np
x=vecarrayconvert(xyz[0])
y=vecarrayconvert(xyz[1])
z=vecarrayconvert(xyz[2])
denom=1.0/np.absolute(z)
xproj = x * denom
yproj = -y * denom
n=np.shape(x)
hemis = np.tile('S',n)
if n[0]>1.0:
hemis[z<0.0] = 'N'
else:
if z<0.0:
hemis = 'N'
return xproj, yproj, hemis
#-------------------------------------------------------------------------------
def revgnomonictrans(xyh,center=[0,0,1],south=[0,1,0],east=[1,0,0]):
'''
reverse gnomonic projection allowing different projection views
'''
import numpy as np
import numpy.linalg as npla
abc = vecarrayconvert(center)
nrmabc = 1.0/vecarraynorm(abc)
uvw = vecarrayconvert(east)
nrmuvw = 1.0/vecarraynorm(uvw)
fed = vecarrayconvert(south) # 'def' is reserved in python
nrmdef = 1.0/vecarraynorm(fed)
xproj = vecarrayconvert(xyh[0])
yproj = vecarrayconvert(xyh[1])
hemis = np.array(np.squeeze(xyh[2]))
n = np.shape(xproj)
x = np.zeros(n)
y = np.zeros(n)
z = np.zeros(n)
aa = abc[0]*nrmabc
bb = abc[1]*nrmabc
cc = abc[2]*nrmabc
dd = fed[0]*nrmdef
ee = fed[1]*nrmdef
ff = fed[2]*nrmdef
uu = uvw[0]*nrmuvw
vv = uvw[1]*nrmuvw
ww = uvw[2]*nrmuvw
R = xproj*xproj + yproj*yproj
m = np.ones(np.shape(hemis))
m[hemis=='N'] = -1.0
cosal = (1.0 - R) / (1.0 + R)
denom = np.absolute(cosal)
cosmu = xproj * denom
cosdl = yproj * denom
# For each case we determine xyz by solving a system of linear equations
for i in range(0,np.shape(cosal)[0]):
xyzcoef = np.squeeze(np.array([[dd,ee,ff],[uu,vv,ww],[aa,bb,cc]]))
xyzsoln = np.array([cosdl[i], cosmu[i], cosal[i]])
xyz = npla.solve(xyzcoef, xyzsoln)
x[i]= xyz[0]
y[i]=-xyz[1]
z[i]= xyz[2]
# Correct z for the hemisphere
z = z * m
return x,y,z
#-------------------------------------------------------------------------------
def revgnomonictransstandard(xyh):
'''
performs the reverse standard gnomonic projection
'''
import numpy as np
xproj = vecarrayconvert(xyh[0])
yproj = vecarrayconvert(xyh[1])
hemis = np.array(np.squeeze(xyh[2]))
R = xproj*xproj + yproj*yproj
m = np.ones(np.shape(hemis))
m[hemis=='N'] = -1.0
z = (m - m * R) / (1.0 + R)
s = m * z
x = xproj * s
y = -yproj * s
return x,y,z
#-------------------------------------------------------------------------------
def xtaldot(p1=1,p2=0,p3=0,
g11=1,g12=0,g13=0,g21=0,g22=1,g23=0,g31=0,g32=0,g33=1,
q1=1,q2=0,q3=0):
''' implements the dot product within the crystallographic reference frame:
p*G\q where p and q are vectors and G is a metric matrix '''
p1 = vecarrayconvert(p1)
p2 = vecarrayconvert(p2)
p3 = vecarrayconvert(p3)
q1 = vecarrayconvert(q1)
q2 = vecarrayconvert(q2)
q3 = vecarrayconvert(q3)
g11 = vecarrayconvert(g11)
g12 = vecarrayconvert(g12)
g13 = vecarrayconvert(g13)
g21 = vecarrayconvert(g21)
g22 = vecarrayconvert(g22)
g23 = vecarrayconvert(g23)
g31 = vecarrayconvert(g31)
g32 = vecarrayconvert(g32)
g33 = vecarrayconvert(g33)
return (g11*q1+g12*q2+g13*q3)*p1+ \
(g21*q1+g22*q2+g23*q3)*p2+ \
(g31*q1+g32*q2+g33*q3)*p3
#-------------------------------------------------------------------------------
def xtalangle(p1=1,p2=0,p3=0,q1=1,q2=0,q3=0,
g11=1,g12=0,g13=0,g21=0,g22=1,g23=0,g31=0,g32=0,g33=1):
''' compute the angle between two directions in a crystal'''
import numpy as np
p1 = vecarrayconvert(p1)
p2 = vecarrayconvert(p2)
p3 = vecarrayconvert(p3)
q1 = vecarrayconvert(q1)
q2 = vecarrayconvert(q2)
q3 = vecarrayconvert(q3)
g11 = vecarrayconvert(g11)
g12 = vecarrayconvert(g12)
g13 = vecarrayconvert(g13)
g21 = vecarrayconvert(g21)
g22 = vecarrayconvert(g22)
g23 = vecarrayconvert(g23)
g31 = vecarrayconvert(g31)
g32 = vecarrayconvert(g32)
g33 = vecarrayconvert(g33)
nrm=1.0/(vecarraynorm([p1,p2,p3])*vecarraynorm([q1,q2,q3]))
return np.arccos(nrm *
xtaldot(p1,p2,p3,g11,g12,g13,g21,g22,g23,g31,g32,g33,q1,q2,q3))
#-------------------------------------------------------------------------------
def rationalize(v,maxval=9):
''' produce rational indices from fractions '''
import numpy as np
import copy
v0 = vecarrayconvert(v[0])
v1 = vecarrayconvert(v[1])
v2 = vecarrayconvert(v[2])
nrm=1.0/vecarraynorm([v0,v1,v2])
v0 = v0 * nrm
v1 = v1 * nrm
v2 = v2 * nrm
if np.shape(v0)==np.shape(v1)==np.shape(v2):
if np.size(v0)==1:
v0=np.array([v0])
v1=np.array([v1])
v2=np.array([v2])
n=np.size(v[0])
vi=np.zeros([n,maxval])
vj=copy.copy(vi); vk=copy.copy(vi); vq=copy.copy(vi)
for i in range(1,maxval+1):
vx = np.around(np.float64(i) * v0)
vy = np.around(np.float64(i) * v1)
vz = np.around(np.float64(i) * v2)
tmpx = np.absolute(vx); tmpy = np.absolute(vy); tmpz = np.absolute(vz)
# calculate the greatest common divisor between tmpx, tmpy, and tmpz
# we have to do this manually because fractions.gcd doesn't work on
# arrays and numpy doesn't yet have a gcd function implemented
div = 1.0/vecarraygcd(vecarraygcd(tmpx,tmpy),tmpz)
# multipy the irrational indices by the greatest common divisor
vi[:,i-1] = vx * div
vj[:,i-1] = vy * div
vk[:,i-1] = vz * div
nrm=1.0/vecarraynorm([vi[:,i-1],vj[:,i-1],vk[:,i-1]])
vq[:,i-1] = np.arccos(np.amin([
np.tile(1.0,n),
vecarraydot([ vi[:,i-1]*nrm,vj[:,i-1]*nrm,vk[:,i-1]*nrm],
[v[0],v[1],v[2]])
],axis=0))
# extract the best match rational values
loc=np.argmin(vq.T, axis=0)
vi=vi[range(0, n),loc]
vj=vj[range(0, n),loc]
vk=vk[range(0, n),loc]
vq=vq[range(0, n),loc]
return vi, vj, vk,vq
else:
print "rationalize error:"+ \
"check that the lengths of arguments are equal."
#-------------------------------------------------------------------------------
def degsymbol():
'''returns the degree symbol for plotting'''
return unichr(176).encode("latin-1")
#-------------------------------------------------------------------------------
def degrees(a):
'''converts radians to degrees'''
import numpy as np
a=vecarrayconvert(a)
return a*180.0/np.pi
#-------------------------------------------------------------------------------
def radians(a):
'''converts degrees to radians'''
import numpy as np
a=vecarrayconvert(a)
return a*np.pi/180.0
#------------------------------------------------------------------------------
def uniquerows(aa):
''' returns the number of unique rows in an array.
Parameters
----------
aa : numpy array
Returns
-------
cc : numpy array
Rows in aa without repetitions
ia : numpy Bool array
Indexing array such that cc = aa[ia]
ic : numpy index array
Indexing array such that aa = cc[ic]
Notes
-----
Mimics behavior of matlab function 'unique' with optional parameter 'rows'.
Algorithm modified from a stack overflow posting [1]_.
References
---------
.. [1] http://stackoverflow.com/questions/8560440/, Accessed 2012-09-10
'''
import numpy as np
ind = np.lexsort(np.fliplr(aa).T) # indices for aa sorted by rows
rev = np.argsort(ind) # reverse of the sorting indices
ab = aa[ind] # ab is sorted version of aa
dd = np.diff(ab, axis=0) # get differences between the rows
ui = np.ones(np.shape(ab)[0], 'bool') # preallocate boolean array
ui[1:] = (dd != 0).any(axis=1) # if difference is zero, row is not unique
ia = ui[rev] # positions of unique rows in aa (original, unsorted array)
cc = aa[ia] # unique rows in aa in original order
loc = np.cumsum(np.uint64(ui))-1 # cumulative sum := locs of repeats in ab
# - rev[ia] gives us the indices of the unique rows in aa
# - argsort(rev[ia]) gives us the indices of the corresponding rows in cc
# - argsort(rev[ia])[loc] gives us the indexing relationship for ab from cc
# - np.argsort(rev[ia])[loc][rev] give indexing reln in original order
ic = np.argsort(rev[ia])[loc][rev]
return cc, ia, ic
#------------------------------------------------------------------------------
def sortrows(a):
'''
sorts an array by rows
Notes
-----
Mimics the behavior of matlab's function of the same name.
'''
import numpy as np
order = np.lexsort(np.fliplr(a).T)
return a[order]
#------------------------------------------------------------------------------
def sigdec(a, n=1):
'''
Rounds the elements of a to n decimals.
A slight modification of Peter J. Acklam's Matlab function FIXDIG
'''
import numpy as np
a = vecarrayconvert(a)
n = vecarrayconvert(n)
f = np.array(10.**n)
s = np.sign(a)
return s * np.around(np.absolute(a) * f) / f
#-------------------------------------------------------------------------------
def tic():
''' Timing function (like tic/toc in matlab). See also: toc'''
import time
return time.time()
def toc(t):
''' Timing function (like tic/toc in matlab). See also: tic'''
import time
t2 = time.time()
print str(t2 - t) + ' seconds elapsed.'
return t2 - t
#-------------------------------------------------------------------------------
class progbar:
''' console progress indicator
Parameters
----------
finalcount : int
the last number in the range
period : float
the amount of time that is ignored for updates to reduce slowdowns
caused by frequent calls to the function
message : string
the message to be displayed above the bar in the console
Returns
-------
None
Notes
-----
This is a modification of the console progress indicator class recipe by
Larry Bates [1]_ to include ideas from Ben Mitch's matlab progbar [2]_.
References
----------
.. [1] L. Bates, "Console progress indicator class." Accessed 2013-02-13.
URL: http://code.activestate.com/recipes/299207-console-text-progress-indicator-class/
.. [2] B. Mitch, "progbar.m: general purpose progress bar for matlab."
Accessed 2013-02-13.
URL: http://www.mathworks.com/matlabcentral/fileexchange/869
Examples
--------
>>> # Create the progbar object
>>> pb = progbar(finalcount=10, period=0.2, message='Progress indicator')
>>> for i in range(1,11,1):
... pb.update(i) # pass the progress values to the update method
>>> pb.update(-1) # update a negative value to signal process is complete
# and trigger the output of the elapsed time.
'''
def __init__(self, finalcount=1, period=0.2,
progresschar=None, message=''):
import time
import sys
import numpy as np
self.finalcount = finalcount
self.blockcount = 0
# See if caller passed me a character to use on the
# progress bar (like "*"). If not use the block
# character that makes it look like a real progress
# bar.
if not progresschar: self.block = chr(149)
else: self.block = progresschar
# Get pointer to sys.stdout so I can use the write/flush
# methods to display the progress bar.
self.f = sys.stdout
# Store the current time and minimum period between updates
self.ctim = time.time()
self.itim = time.time() # initial time
self.period = period
self.finished = False
# If the final count is zero, don't start the progress gauge
if not self.finalcount: return
if len(message) < 50:
val = max(np.floor(0.5 * (50 - len(message)))-1, 0)
else:
val = 0
self.f.write('\n'+' '*val+message+'\n')
self.f.write('|_____________ P R O G R E S S ________________|\n')
self.f.write('|----1----2----3----4----5----6----7----8----9---|' + \
' FINISH!\n')
return
def update(self, count):
import time
import numpy as np
# Check if the elapsed time is greater than the period. If not, return.
etim = time.time()
if (etim - self.ctim < self.period and count != -1): return
# If the elapsed time is greater than the period, update the bar
self.ctim = etim
# Make sure I don't try to go off the end (e.g. >100%)
if count == -1: count = self.finalcount
count=min(count, self.finalcount)
# If finalcount is zero, I don't start
if self.finalcount:
percentcomplete=int(np.around(100*count/self.finalcount))
if percentcomplete < 1: percentcomplete = 1
else:
percentcomplete = 100
blockcount = int(percentcomplete/2)
if blockcount > self.blockcount:
for i in range(self.blockcount,blockcount):
self.f.write(self.block)
self.f.flush()
if percentcomplete == 100 or count == self.finalcount:
if self.finished == False:
self.f.write(" ELAPSED TIME: " + \
time.strftime('%H:%M:%S', \
time.gmtime(etim-self.itim)) + '\n\n')
self.finished = True
self.blockcount = blockcount
return
#-------------------------------------------------------------------------------
def iseven(x):
''' is it an even number?
Returns True if x is even and False if x is odd
Parameters
----------
x : int
Returns
-------
b : boolean
'''
import numpy as np
# using np.absolute in case input is array
xa = vecarrayconvert(x)
b = np.mod(xa, 1) == 0 and np.absolute(np.mod(xa, 2)) <= np.spacing(1)
return b
#------------------------------------------------------------------------------
def fileconvert_eurodecimals_to_international(filepath, outputfilepath=None, \
inplace=False):
''' convert commas to points in numbers in text data files
Converts a text file where the decimals are given by commas into one
where the decimals are given by decimal points.