-
Notifications
You must be signed in to change notification settings - Fork 95
/
test_coords.py
1747 lines (1652 loc) · 103 KB
/
test_coords.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
from pkg_resources import parse_version
import numpy
_NUMPY_VERSION= parse_version(numpy.__version__)
_NUMPY_1_22= (_NUMPY_VERSION > parse_version('1.21'))\
*(_NUMPY_VERSION < parse_version('1.23'))
from galpy.util import coords
import pytest
import astropy
_APY3= astropy.__version__ > '3'
def test_radec_to_lb_ngp():
_turn_off_apy()
# Test that the NGP is at b=90
ra, dec= 192.25, 27.4
lb= coords.radec_to_lb(ra,dec,degree=True,epoch=1950.)
assert not coords._APY_LOADED, "_APY_LOADED should be False, but isn't"
assert numpy.fabs(lb[1]-90.) < _NUMPY_1_22 * 1e-5 + (1-_NUMPY_1_22) * 1e-6, 'Galactic latitude of the NGP given in ra,dec is not 90'
# Also test this for degree=False
lb= coords.radec_to_lb(ra/180.*numpy.pi,dec/180.*numpy.pi,
degree=False,epoch=1950.)
assert numpy.fabs(lb[1]-numpy.pi/2.) < 10.**-7., 'Galactic latitude of the NGP given in ra,dec is not pi/2'
_turn_on_apy()
assert coords._APY_LOADED, "_APY_LOADED should be True, but isn't"
return None
def test_radec_to_lb_ngp_apyangles():
# Test, but using transformation angles derived from astropy
_turn_off_apy(keep_loaded=True)
# Test that the NGP is at b=90
ra, dec= 192.25, 27.4
assert coords._APY_LOADED, "_APY_LOADED should be True, but isn't"
lb= coords.radec_to_lb(ra,dec,degree=True,epoch='B1950')
assert numpy.fabs(lb[1]-90.) < 10.**-4., 'Galactic latitude of the NGP given in ra,dec is not 90'
# Also test this for degree=False
lb= coords.radec_to_lb(ra/180.*numpy.pi,dec/180.*numpy.pi,
degree=False,epoch='B1950')
assert numpy.fabs(lb[1]-numpy.pi/2.) < 10.**-4., 'Galactic latitude of the NGP given in ra,dec is not pi/2'
_turn_on_apy()
return None
def test_radec_to_lb_ngp_apy():
# Test that the NGP is at b=90, using astropy's coordinate transformations
ra, dec= 192.25, 27.4
lb= coords.radec_to_lb(ra,dec,degree=True,epoch=1950.)
assert numpy.fabs(lb[1]-90.) < 10.**-4., 'Galactic latitude of the NGP given in ra,dec is not 90'
# Also test this for degree=False
lb= coords.radec_to_lb(ra/180.*numpy.pi,dec/180.*numpy.pi,
degree=False,epoch=1950.)
assert numpy.fabs(lb[1]-numpy.pi/2.) < 10.**-4., 'Galactic latitude of the NGP given in ra,dec is not pi/2'
return None
def test_radec_to_lb_ngp_j2000():
_turn_off_apy()
# Test that the NGP is at b=90
ra, dec= 192.8594812065348, 27.12825118085622
lb= coords.radec_to_lb(ra,dec,degree=True,epoch=2000.)
assert numpy.fabs(lb[1]-90.) < 10.**-8., 'Galactic latitude of the NGP given in ra,dec is not 90'
# Also test this for degree=False
lb= coords.radec_to_lb(ra/180.*numpy.pi,dec/180.*numpy.pi,
degree=False,epoch=2000.)
assert numpy.fabs(lb[1]-numpy.pi/2.) < 10.**-8., 'Galactic latitude of the NGP given in ra,dec is not pi/2'
_turn_on_apy()
return None
def test_radec_to_lb_ngp_j2000_apy():
# Test that the NGP is at b=90
ra, dec= 192.8594812065348, 27.12825118085622
lb= coords.radec_to_lb(ra,dec,degree=True,epoch=2000.)
assert numpy.fabs(lb[1]-90.) < 10.**-4., 'Galactic latitude of the NGP given in ra,dec is not 90'
# Also test this for degree=False
lb= coords.radec_to_lb(ra/180.*numpy.pi,dec/180.*numpy.pi,
degree=False,epoch=2000.)
assert numpy.fabs(lb[1]-numpy.pi/2.) < 10.**-4., 'Galactic latitude of the NGP given in ra,dec is not pi/2'
return None
def test_radec_to_lb_ngp_j2000_apyangles():
# Same test, but using transformation angles derived from astropy
_turn_off_apy(keep_loaded=True)
# Test that the NGP is at b=90
ra, dec= 192.8594812065348, 27.12825118085622
lb= coords.radec_to_lb(ra,dec,degree=True,epoch='J2000')
assert numpy.fabs(lb[1]-90.) < 10.**-4., 'Galactic latitude of the NGP given in ra,dec is not 90'
# Also test this for degree=False
lb= coords.radec_to_lb(ra/180.*numpy.pi,dec/180.*numpy.pi,
degree=False,epoch='J2000')
assert numpy.fabs(lb[1]-numpy.pi/2.) < 10.**-4., 'Galactic latitude of the NGP given in ra,dec is not pi/2'
_turn_on_apy()
return None
def test_radec_to_lb_ngp_j2000_apyangles_icrs():
# Test, but using transformation angles derived from astropy, for ICRS
_turn_off_apy(keep_loaded=True)
# Test that the NGP is at b=90
ra, dec= 192.8594812065348, 27.12825118085622
lb= coords.radec_to_lb(ra,dec,degree=True,epoch=None)
assert numpy.fabs(lb[1]-90.) < 10.**-4., 'Galactic latitude of the NGP given in ra,dec is not 90'
# Also test this for degree=False
lb= coords.radec_to_lb(ra/180.*numpy.pi,dec/180.*numpy.pi,
degree=False,epoch=None)
assert numpy.fabs(lb[1]-numpy.pi/2.) < 10.**-4., 'Galactic latitude of the NGP given in ra,dec is not pi/2'
_turn_on_apy()
return None
def test_radec_to_lb_sgp():
_turn_off_apy()
# Test that the SGP is at b=90
ra, dec= 12.25, -27.4
assert not coords._APY_LOADED, "_APY_LOADED should be False, but isn't"
lb= coords.radec_to_lb(ra,dec,degree=True,epoch=1950.)
assert numpy.fabs(lb[1]+90.) < _NUMPY_1_22 * 1e-5 + (1-_NUMPY_1_22) * 1e-6, 'Galactic latitude of the SGP given in ra,dec is not 90'
# Also test this for degree=False
lb= coords.radec_to_lb(ra/180.*numpy.pi,dec/180.*numpy.pi,
degree=False,epoch=1950.)
assert numpy.fabs(lb[1]+numpy.pi/2.) < 10.**-7., 'Galactic latitude of the SGP given in ra,dec is not pi/2'
_turn_on_apy()
return None
# Test the longitude of the north celestial pole
def test_radec_to_lb_ncp():
_turn_off_apy()
ra, dec= 180., 90.
lb= coords.radec_to_lb(ra,dec,degree=True,epoch=1950.)
assert numpy.fabs(lb[0]-123.) < 10.**-8., 'Galactic longitude of the NCP given in ra,dec is not 123'
# Also test this for degree=False
lb= coords.radec_to_lb(ra/180.*numpy.pi,dec/180.*numpy.pi,
degree=False,epoch=1950.)
assert numpy.fabs(lb[0]-123./180.*numpy.pi) < 10.**-8., 'Galactic longitude of the NCP given in ra,dec is not 123'
# Also test the latter for vector inputs
os= numpy.ones(2)
lb= coords.radec_to_lb(os*ra/180.*numpy.pi,os*dec/180.*numpy.pi,
degree=False,epoch=1950.)
assert numpy.all(numpy.fabs(lb[:,0]-123./180.*numpy.pi) < 10.**-8.), 'Galactic longitude of the NCP given in ra,dec is not 123'
_turn_on_apy()
return None
def test_radec_to_lb_ncp_apyangles():
_turn_off_apy(keep_loaded=True)
ra, dec= 180., 90.
lb= coords.radec_to_lb(ra,dec,degree=True,epoch='B1950')
assert numpy.fabs(lb[0]-123.) < 10.**-4., 'Galactic longitude of the NCP given in ra,dec is not 123'
_turn_on_apy()
return None
# Test the longitude of the north celestial pole
def test_radec_to_lb_ncp_j2000():
_turn_off_apy()
ra, dec= 180., 90.
lb= coords.radec_to_lb(ra,dec,degree=True,epoch=2000.)
assert numpy.fabs(lb[0]-122.9319185680026) < 10.**-8., 'Galactic longitude of the NCP given in ra,dec is not 122.9319185680026'
# Also test this for degree=False
lb= coords.radec_to_lb(ra/180.*numpy.pi,dec/180.*numpy.pi,
degree=False,epoch=2000.)
assert numpy.fabs(lb[0]-122.9319185680026/180.*numpy.pi) < 10.**-8., 'Galactic longitude of the NCP given in ra,dec is not 122.9319185680026'
# Also test the latter for vector inputs
os= numpy.ones(2)
lb= coords.radec_to_lb(os*ra/180.*numpy.pi,os*dec/180.*numpy.pi,
degree=False,epoch=2000.)
assert numpy.all(numpy.fabs(lb[:,0]-122.9319185680026/180.*numpy.pi) < 10.**-8.), 'Galactic longitude of the NCP given in ra,dec is not 122.9319185680026'
_turn_on_apy()
return None
def test_radec_to_lb_ncp_j2000_apyangles():
_turn_off_apy(keep_loaded=True)
ra, dec= 180., 90.
lb= coords.radec_to_lb(ra,dec,degree=True,epoch='J2000')
assert numpy.fabs(lb[0]-122.9319185680026) < 10.**-4., 'Galactic longitude of the NCP given in ra,dec is not 122.9319185680026'
_turn_on_apy()
return None
# Test that other epochs do not work when not using astropy
def test_radec_to_lb_otherepochs():
_turn_off_apy()
ra, dec= 180., 90.
try:
lb= coords.radec_to_lb(ra/180.*numpy.pi,dec/180.*numpy.pi,
degree=False,epoch=1975.)
except IOError:
pass
else:
raise AssertionError('radec functions with epoch not equal to 1950 or 2000 did not raise IOError')
_turn_on_apy()
return None
# Test that other epochs do work when using astropy
def test_radec_to_lb_otherepochs_apy():
_turn_off_apy(keep_loaded=True)
ra, dec= 180., 90.
try:
lb= coords.radec_to_lb(ra/180.*numpy.pi,dec/180.*numpy.pi,
degree=False,epoch='J2015')
except IOError:
raise AssertionError('radec functions with epoch not equal to 1950 or 2000 did not raise IOError')
else:
pass
_turn_on_apy()
return None
# Test that radec_to_lb and lb_to_radec are each other's inverse
def test_lb_to_radec():
_turn_off_apy()
ra, dec= 120, 60.
lb= coords.radec_to_lb(ra,dec,degree=True,epoch=2000.)
rat, dect= coords.lb_to_radec(lb[0],lb[1],degree=True,epoch=2000.)
assert numpy.fabs(ra-rat) < 10.**-10., 'lb_to_radec is not the inverse of radec_to_lb'
assert numpy.fabs(dec-dect) < 10.**-10., 'lb_to_radec is not the inverse of radec_to_lb'
# Also test this for degree=False
lb= coords.radec_to_lb(ra/180.*numpy.pi,dec/180.*numpy.pi,
degree=False,epoch=2000.)
rat, dect= coords.lb_to_radec(lb[0],lb[1],degree=False,epoch=2000.)
assert numpy.fabs(ra/180.*numpy.pi-rat) < 10.**-10., 'lb_to_radec is not the inverse of radec_to_lb'
assert numpy.fabs(dec/180.*numpy.pi-dect) < 10.**-10., 'lb_to_radec is not the inverse of radec_to_lb'
# And also test this for arrays
os= numpy.ones(2)
lb= coords.radec_to_lb(os*ra/180.*numpy.pi,os*dec/180.*numpy.pi,
degree=False,epoch=2000.)
ratdect= coords.lb_to_radec(lb[:,0],lb[:,1],degree=False,epoch=2000.)
rat= ratdect[:,0]
dect= ratdect[:,1]
assert numpy.all(numpy.fabs(ra/180.*numpy.pi-rat) < 10.**-10.), 'lb_to_radec is not the inverse of radec_to_lb'
assert numpy.all(numpy.fabs(dec/180.*numpy.pi-dect) < 10.**-10.), 'lb_to_radec is not the inverse of radec_to_lb'
#Also test for a negative l
l,b= 240., 60.
ra,dec= coords.lb_to_radec(l,b,degree=True)
lt,bt= coords.radec_to_lb(ra,dec,degree=True)
assert numpy.fabs(lt-l) < 10.**-10., 'lb_to_radec is not the inverse of radec_to_lb'
assert numpy.fabs(bt-b) < 10.**-10., 'lb_to_radec is not the inverse of radec_to_lb'
_turn_on_apy()
return None
# Test that radec_to_lb and lb_to_radec are each other's inverse, using astropy
def test_lb_to_radec_apy():
ra, dec= 120, 60.
lb= coords.radec_to_lb(ra,dec,degree=True,epoch=2000.)
rat, dect= coords.lb_to_radec(lb[0],lb[1],degree=True,epoch=2000.)
assert numpy.fabs(ra-rat) < 10.**-10., 'lb_to_radec is not the inverse of radec_to_lb'
assert numpy.fabs(dec-dect) < 10.**-10., 'lb_to_radec is not the inverse of radec_to_lb'
# Also test this for degree=False
lb= coords.radec_to_lb(ra/180.*numpy.pi,dec/180.*numpy.pi,
degree=False,epoch=2000.)
rat, dect= coords.lb_to_radec(lb[0],lb[1],degree=False,epoch=2000.)
assert numpy.fabs(ra/180.*numpy.pi-rat) < 10.**-10., 'lb_to_radec is not the inverse of radec_to_lb'
assert numpy.fabs(dec/180.*numpy.pi-dect) < 10.**-10., 'lb_to_radec is not the inverse of radec_to_lb'
# And also test this for arrays
os= numpy.ones(2)
lb= coords.radec_to_lb(os*ra/180.*numpy.pi,os*dec/180.*numpy.pi,
degree=False,epoch=2000.)
ratdect= coords.lb_to_radec(lb[:,0],lb[:,1],degree=False,epoch=2000.)
rat= ratdect[:,0]
dect= ratdect[:,1]
assert numpy.all(numpy.fabs(ra/180.*numpy.pi-rat) < 10.**-10.), 'lb_to_radec is not the inverse of radec_to_lb'
assert numpy.all(numpy.fabs(dec/180.*numpy.pi-dect) < 10.**-10.), 'lb_to_radec is not the inverse of radec_to_lb'
#Also test for a negative l
l,b= 240., 60.
ra,dec= coords.lb_to_radec(l,b,degree=True)
lt,bt= coords.radec_to_lb(ra,dec,degree=True)
assert numpy.fabs(lt-l) < 10.**-10., 'lb_to_radec is not the inverse of radec_to_lb'
assert numpy.fabs(bt-b) < 10.**-10., 'lb_to_radec is not the inverse of radec_to_lb'
return None
# Test that radec_to_lb and lb_to_radec are each other's inverse, using astropy
def test_lb_to_radec_apy_icrs():
ra, dec= 120, 60.
lb= coords.radec_to_lb(ra,dec,degree=True,epoch=None)
rat, dect= coords.lb_to_radec(lb[0],lb[1],degree=True,epoch=None)
assert numpy.fabs(ra-rat) < 10.**-10., 'lb_to_radec is not the inverse of radec_to_lb'
assert numpy.fabs(dec-dect) < 10.**-10., 'lb_to_radec is not the inverse of radec_to_lb'
# Also test this for degree=False
lb= coords.radec_to_lb(ra/180.*numpy.pi,dec/180.*numpy.pi,
degree=False,epoch=None)
rat, dect= coords.lb_to_radec(lb[0],lb[1],degree=False,epoch=None)
assert numpy.fabs(ra/180.*numpy.pi-rat) < 10.**-10., 'lb_to_radec is not the inverse of radec_to_lb'
assert numpy.fabs(dec/180.*numpy.pi-dect) < 10.**-10., 'lb_to_radec is not the inverse of radec_to_lb'
# And also test this for arrays
os= numpy.ones(2)
lb= coords.radec_to_lb(os*ra/180.*numpy.pi,os*dec/180.*numpy.pi,
degree=False,epoch=None)
ratdect= coords.lb_to_radec(lb[:,0],lb[:,1],degree=False,epoch=None)
rat= ratdect[:,0]
dect= ratdect[:,1]
assert numpy.all(numpy.fabs(ra/180.*numpy.pi-rat) < 10.**-10.), 'lb_to_radec is not the inverse of radec_to_lb'
assert numpy.all(numpy.fabs(dec/180.*numpy.pi-dect) < 10.**-10.), 'lb_to_radec is not the inverse of radec_to_lb'
#Also test for a negative l
l,b= 240., 60.
ra,dec= coords.lb_to_radec(l,b,degree=True)
lt,bt= coords.radec_to_lb(ra,dec,degree=True)
assert numpy.fabs(lt-l) < 10.**-10., 'lb_to_radec is not the inverse of radec_to_lb'
assert numpy.fabs(bt-b) < 10.**-10., 'lb_to_radec is not the inverse of radec_to_lb'
return None
def test_radec_to_lb_galpyvsastropy():
# Test that galpy's radec_to_lb agrees with astropy's
from astropy.coordinates import SkyCoord
import astropy.units as u
_turn_off_apy(keep_loaded=True)
ra, dec= 33., -20.
# using galpy
lg,bg= coords.radec_to_lb(ra,dec,degree=True,epoch=2000.0)
# using astropy
c= SkyCoord(ra=ra*u.deg,dec=dec*u.deg,frame='fk5',equinox='J2000')
c= c.transform_to('galactic')
la,ba= c.l.to(u.deg).value,c.b.to(u.deg).value
assert numpy.fabs(lg-la) < 1e-12, "radec_to_lb using galpy's own transformations does not agree with astropy's"
assert numpy.fabs(bg-ba) < 1e-12, "radec_to_lb using galpy's own transformations does not agree with astropy's"
_turn_on_apy()
return None
def test_radec_to_lb__1950_galpyvsastropy():
# Test that galpy's radec_to_lb agrees with astropy's
from astropy.coordinates import SkyCoord
import astropy.units as u
_turn_off_apy(keep_loaded=True)
ra, dec= 33., -20.
# using galpy
lg,bg= coords.radec_to_lb(ra,dec,degree=True,epoch=1950.0)
# using astropy
c= SkyCoord(ra=ra*u.deg,dec=dec*u.deg,frame='fk4noeterms',equinox='B1950')
c= c.transform_to('galactic')
la,ba= c.l.to(u.deg).value,c.b.to(u.deg).value
assert numpy.fabs(lg-la) < 1e-12, "radec_to_lb using galpy's own transformations does not agree with astropy's"
assert numpy.fabs(bg-ba) < 1e-12, "radec_to_lb using galpy's own transformations does not agree with astropy's"
_turn_on_apy()
return None
# Test lb_to_XYZ
def test_lbd_to_XYZ():
l,b,d= 90., 30.,1.
XYZ= coords.lbd_to_XYZ(l,b,d,degree=True)
assert numpy.fabs(XYZ[0]) <10.**-10., 'lbd_to_XYZ conversion does not work as expected'
assert numpy.fabs(XYZ[1]-numpy.sqrt(3.)/2.) < 10.**-10., 'lbd_to_XYZ conversion does not work as expected'
assert numpy.fabs(XYZ[2]-0.5) < 10.**-10., 'lbd_to_XYZ conversion does not work as expected'
# Also test for degree=False
XYZ= coords.lbd_to_XYZ(l/180.*numpy.pi,b/180.*numpy.pi,d,degree=False)
assert numpy.fabs(XYZ[0]) <10.**-10., 'lbd_to_XYZ conversion does not work as expected'
assert numpy.fabs(XYZ[1]-numpy.sqrt(3.)/2.) < 10.**-10., 'lbd_to_XYZ conversion does not work as expected'
assert numpy.fabs(XYZ[2]-0.5) < 10.**-10., 'lbd_to_XYZ conversion does not work as expected'
# Also test for arrays
os= numpy.ones(2)
XYZ= coords.lbd_to_XYZ(os*l/180.*numpy.pi,os*b/180.*numpy.pi,
os*d,degree=False)
assert numpy.all(numpy.fabs(XYZ[:,0]) <10.**-10.), 'lbd_to_XYZ conversion does not work as expected'
assert numpy.all(numpy.fabs(XYZ[:,1]-numpy.sqrt(3.)/2.) < 10.**-10.), 'lbd_to_XYZ conversion does not work as expected'
assert numpy.all(numpy.fabs(XYZ[:,2]-0.5) < 10.**-10.), 'lbd_to_XYZ conversion does not work as expected'
return None
# Test that XYZ_to_lbd is the inverse of lbd_to_XYZ
def test_XYZ_to_lbd():
l,b,d= 90., 30.,1.
XYZ= coords.lbd_to_XYZ(l,b,d,degree=True)
lt,bt,dt= coords.XYZ_to_lbd(XYZ[0],XYZ[1],XYZ[2],degree=True)
assert numpy.fabs(lt-l) <10.**-10., 'XYZ_to_lbd conversion does not work as expected'
assert numpy.fabs(bt-b) < 10.**-10., 'XYZ_to_lbd conversion does not work as expected'
assert numpy.fabs(dt-d) < 10.**-10., 'XYZ_to_lbd conversion does not work as expected'
# Also test for degree=False
XYZ= coords.lbd_to_XYZ(l/180.*numpy.pi,b/180.*numpy.pi,d,degree=False)
lt,bt,dt= coords.XYZ_to_lbd(XYZ[0],XYZ[1],XYZ[2],degree=False)
assert numpy.fabs(lt-l/180.*numpy.pi) <10.**-10., 'XYZ_to_lbd conversion does not work as expected'
assert numpy.fabs(bt-b/180.*numpy.pi) < 10.**-10., 'XYZ_to_lbd conversion does not work as expected'
assert numpy.fabs(dt-d) < 10.**-10., 'XYZ_to_lbd conversion does not work as expected'
# Also test for arrays
os= numpy.ones(2)
XYZ= coords.lbd_to_XYZ(os*l/180.*numpy.pi,os*b/180.*numpy.pi,
os*d,degree=False)
lbdt= coords.XYZ_to_lbd(XYZ[:,0],XYZ[:,1],XYZ[:,2],degree=False)
assert numpy.all(numpy.fabs(lbdt[:,0]-l/180.*numpy.pi) <10.**-10.), 'XYZ_to_lbd conversion does not work as expected'
assert numpy.all(numpy.fabs(lbdt[:,1]-b/180.*numpy.pi) < 10.**-10.), 'XYZ_to_lbd conversion does not work as expected'
assert numpy.all(numpy.fabs(lbdt[:,2]-d) < 10.**-10.), 'XYZ_to_lbd conversion does not work as expected'
return None
def test_vrpmllpmbb_to_vxvyvz():
l,b,d= 90., 0.,1.
vr,pmll,pmbb= 10.,20./4.740470463496208,-10./4.740470463496208
vxvyvz= coords.vrpmllpmbb_to_vxvyvz(vr,pmll,pmbb,l,b,d,
degree=True,XYZ=False)
assert numpy.fabs(vxvyvz[0]+20.) < 10.**-9., 'vrpmllpmbb_to_vxvyvz conversion did not work as expected'
assert numpy.fabs(vxvyvz[1]-10.) < 10.**-9., 'vrpmllpmbb_to_vxvyvz conversion did not work as expected'
assert numpy.fabs(vxvyvz[2]+10.) < 10.**-9., 'vrpmllpmbb_to_vxvyvz conversion did not work as expected'
vxvyvz= coords.vrpmllpmbb_to_vxvyvz(vr,pmll,pmbb,l/180.*numpy.pi,
b/180.*numpy.pi,d,
degree=False,XYZ=False)
assert numpy.fabs(vxvyvz[0]+20.) < 10.**-9., 'vrpmllpmbb_to_vxvyvz conversion did not work as expected'
assert numpy.fabs(vxvyvz[1]-10.) < 10.**-9., 'vrpmllpmbb_to_vxvyvz conversion did not work as expected'
assert numpy.fabs(vxvyvz[2]+10.) < 10.**-9., 'vrpmllpmbb_to_vxvyvz conversion did not work as expected'
vxvyvz= coords.vrpmllpmbb_to_vxvyvz(vr,pmll,pmbb,0.,1,0.,
XYZ=True)
assert numpy.fabs(vxvyvz[0]+20.) < 10.**-9., 'vrpmllpmbb_to_vxvyvz conversion did not work as expected'
assert numpy.fabs(vxvyvz[1]-10.) < 10.**-9., 'vrpmllpmbb_to_vxvyvz conversion did not work as expected'
assert numpy.fabs(vxvyvz[2]+10.) < 10.**-9., 'vrpmllpmbb_to_vxvyvz conversion did not work as expected'
vxvyvz= coords.vrpmllpmbb_to_vxvyvz(vr,pmll,pmbb,0.,1,0.,
XYZ=True,degree=True)
assert numpy.fabs(vxvyvz[0]+20.) < 10.**-9., 'vrpmllpmbb_to_vxvyvz conversion did not work as expected'
assert numpy.fabs(vxvyvz[1]-10.) < 10.**-9., 'vrpmllpmbb_to_vxvyvz conversion did not work as expected'
assert numpy.fabs(vxvyvz[2]+10.) < 10.**-9., 'vrpmllpmbb_to_vxvyvz conversion did not work as expected'
#Also test for arrays
os= numpy.ones(2)
vxvyvz= coords.vrpmllpmbb_to_vxvyvz(os*vr,os*pmll,os*pmbb,os*l,os*b,
os*d,degree=True,XYZ=False)
assert numpy.all(numpy.fabs(vxvyvz[:,0]+20.) < 10.**-9.), 'vrpmllpmbb_to_vxvyvz conversion did not work as expected'
assert numpy.all(numpy.fabs(vxvyvz[:,1]-10.) < 10.**-9.), 'vrpmllpmbb_to_vxvyvz conversion did not work as expected'
assert numpy.all(numpy.fabs(vxvyvz[:,2]+10.) < 10.**-9.), 'vrpmllpmbb_to_vxvyvz conversion did not work as expected'
return None
def test_vxvyvz_to_vrpmllpmbb():
vx,vy,vz= -20.*4.740470463496208,10.,-10.*4.740470463496208
X,Y,Z= 0.,1.,0.
vrpmllpmbb= coords.vxvyvz_to_vrpmllpmbb(vx,vy,vz,X,Y,Z,
XYZ=True)
assert numpy.fabs(vrpmllpmbb[0]-10.) < 10.**-9., 'vxvyvz_to_vrpmllpmbb conversion did not work as expected'
assert numpy.fabs(vrpmllpmbb[1]-20.) < 10.**-9., 'vxvyvz_to_vrpmllpmbb conversion did not work as expected'
assert numpy.fabs(vrpmllpmbb[2]+10.) < 10.**-9., 'vxvyvz_to_vrpmllpmbb conversion did not work as expected'
# also try with degree=True (that shouldn't fail!)
vrpmllpmbb= coords.vxvyvz_to_vrpmllpmbb(vx,vy,vz,X,Y,Z,
XYZ=True,
degree=True)
assert numpy.fabs(vrpmllpmbb[0]-10.) < 10.**-9., 'vxvyvz_to_vrpmllpmbb conversion did not work as expected'
assert numpy.fabs(vrpmllpmbb[1]-20.) < 10.**-9., 'vxvyvz_to_vrpmllpmbb conversion did not work as expected'
assert numpy.fabs(vrpmllpmbb[2]+10.) < 10.**-9., 'vxvyvz_to_vrpmllpmbb conversion did not work as expected'
# also for lbd
vrpmllpmbb= coords.vxvyvz_to_vrpmllpmbb(vx,vy,vz,90.,0.,1.,
XYZ=False,degree=True)
assert numpy.fabs(vrpmllpmbb[0]-10.) < 10.**-9., 'vxvyvz_to_vrpmllpmbb conversion did not work as expected'
assert numpy.fabs(vrpmllpmbb[1]-20.) < 10.**-9., 'vxvyvz_to_vrpmllpmbb conversion did not work as expected'
assert numpy.fabs(vrpmllpmbb[2]+10.) < 10.**-9., 'vxvyvz_to_vrpmllpmbb conversion did not work as expected'
# also for lbd, not in degree
vrpmllpmbb= coords.vxvyvz_to_vrpmllpmbb(vx,vy,vz,numpy.pi/2.,0.,1.,
XYZ=False,degree=False)
assert numpy.fabs(vrpmllpmbb[0]-10.) < 10.**-9., 'vxvyvz_to_vrpmllpmbb conversion did not work as expected'
assert numpy.fabs(vrpmllpmbb[1]-20.) < 10.**-9., 'vxvyvz_to_vrpmllpmbb conversion did not work as expected'
assert numpy.fabs(vrpmllpmbb[2]+10.) < 10.**-9., 'vxvyvz_to_vrpmllpmbb conversion did not work as expected'
# and for arrays
os= numpy.ones(2)
vrpmllpmbb= coords.vxvyvz_to_vrpmllpmbb(os*vx,os*vy,os*vz,
os*numpy.pi/2.,os*0.,os,
XYZ=False,degree=False)
assert numpy.all(numpy.fabs(vrpmllpmbb[:,0]-10.) < 10.**-9.), 'vxvyvz_to_vrpmllpmbb conversion did not work as expected'
assert numpy.all(numpy.fabs(vrpmllpmbb[:,1]-20.) < 10.**-9.), 'vxvyvz_to_vrpmllpmbb conversion did not work as expected'
assert numpy.all(numpy.fabs(vrpmllpmbb[:,2]+10.) < 10.**-10.), 'vxvyvz_to_vrpmllpmbb conversion did not work as expected'
return None
def test_XYZ_to_galcenrect():
X,Y,Z= 1.,3.,-2.
gcXYZ= coords.XYZ_to_galcenrect(X,Y,Z,Xsun=1.,Zsun=0.)
assert numpy.fabs(gcXYZ[0]) < 10.**-5., 'XYZ_to_galcenrect conversion did not work as expected'
assert numpy.fabs(gcXYZ[1]-3.) < 10.**-5., 'XYZ_to_galcenrect conversion did not work as expected'
assert numpy.fabs(gcXYZ[2]+2.) < 10.**-5., 'XYZ_to_galcenrect conversion did not work as expected'
#Another test
X,Y,Z= -1.,3.,-2.
gcXYZ= coords.XYZ_to_galcenrect(X,Y,Z,Xsun=1.,Zsun=0.)
assert numpy.fabs(gcXYZ[0]-2.) < 10.**-5., 'XYZ_to_galcenrect conversion did not work as expected'
assert numpy.fabs(gcXYZ[1]-3.) < 10.**-5., 'XYZ_to_galcenrect conversion did not work as expected'
assert numpy.fabs(gcXYZ[2]+2.) < 10.**-5., 'XYZ_to_galcenrect conversion did not work as expected'
return None
def test_XYZ_to_galcenrect_negXsun():
# Check that XYZ_to_galcenrect works for negative Xsun
X,Y,Z= 0.3,2.1,-1.2
gcXYZ= coords.XYZ_to_galcenrect(X,Y,Z,Xsun=1.2,Zsun=0.2)
gcXYZn= coords.XYZ_to_galcenrect(X,Y,Z,Xsun=-1.2,Zsun=0.2)
assert numpy.fabs(gcXYZ[0]+gcXYZn[0]) < 10.**-10., 'XYZ_to_galcenrect conversion did not work as expected for negative Xsun'
assert numpy.fabs(gcXYZ[1]-gcXYZn[1]) < 10.**-10., 'XYZ_to_galcenrect conversion did not work as expected for negative Xsun'
assert numpy.fabs(gcXYZ[2]-gcXYZn[2]) < 10.**-10., 'XYZ_to_galcenrect conversion did not work as expected for negative Xsun'
def test_lbd_to_galcenrect_galpyvsastropy():
# Test that galpy's transformations agree with astropy's
from astropy.coordinates import SkyCoord, Galactocentric
import astropy.units as u
_turn_off_apy()
l,b,d= 32., -12., 3.
Zsun= 0.025
# Using galpy
X,Y,Z= coords.lbd_to_XYZ(l,b,d,degree=True)
gcXYZ= coords.XYZ_to_galcenrect(X,Y,Z,Xsun=8.,Zsun=Zsun)
# Using astropy
c= SkyCoord(l=l*u.deg,b=b*u.deg,distance=d*u.kpc,frame='galactic')
gc_frame= Galactocentric(galcen_distance=numpy.sqrt(8.**2.+Zsun**2.)*u.kpc,
z_sun=Zsun*u.kpc)
c= c.transform_to(gc_frame)
# galpy is left-handed, astropy right-handed
assert numpy.fabs(gcXYZ[0]+c.x.to(u.kpc).value) < 10.**-10., "lbd to galcenrect conversion using galpy's methods does not agree with astropy"
assert numpy.fabs(gcXYZ[1]-c.y.to(u.kpc).value) < 10.**-10., "lbd to galcenrect conversion using galpy's methods does not agree with astropy"
assert numpy.fabs(gcXYZ[2]-c.z.to(u.kpc).value) < 10.**-9.5, "lbd to galcenrect conversion using galpy's methods does not agree with astropy"
# Also with negative Xsun
l,b,d= 32., -12., 3.
Zsun= 0.025
# Using galpy
X,Y,Z= coords.lbd_to_XYZ(l,b,d,degree=True)
gcXYZ= coords.XYZ_to_galcenrect(X,Y,Z,Xsun=-8.,Zsun=Zsun)
# Using astropy
c= SkyCoord(l=l*u.deg,b=b*u.deg,distance=d*u.kpc,frame='galactic')
gc_frame= Galactocentric(galcen_distance=numpy.sqrt(8.**2.+Zsun**2.)*u.kpc,
z_sun=Zsun*u.kpc)
c= c.transform_to(gc_frame)
# galpy is now right-handed, astropy right-handed
assert numpy.fabs(gcXYZ[0]-c.x.to(u.kpc).value) < 10.**-10., "lbd to galcenrect conversion using galpy's methods does not agree with astropy"
assert numpy.fabs(gcXYZ[1]-c.y.to(u.kpc).value) < 10.**-10., "lbd to galcenrect conversion using galpy's methods does not agree with astropy"
assert numpy.fabs(gcXYZ[2]-c.z.to(u.kpc).value) < 10.**-9.5, "lbd to galcenrect conversion using galpy's methods does not agree with astropy"
_turn_on_apy()
return None
def test_lbd_to_galcencyl_galpyvsastropy():
# Test that galpy's transformations agree with astropy's
from astropy.coordinates import SkyCoord, Galactocentric
import astropy.units as u
_turn_off_apy()
l,b,d= 32., -12., 3.
Zsun= 0.025
# Using galpy
X,Y,Z= coords.lbd_to_XYZ(l,b,d,degree=True)
gcRpZ= coords.XYZ_to_galcencyl(X,Y,Z,Xsun=8.,Zsun=Zsun)
# Using astropy
c= SkyCoord(l=l*u.deg,b=b*u.deg,distance=d*u.kpc,frame='galactic')
gc_frame= Galactocentric(galcen_distance=numpy.sqrt(8.**2.+Zsun**2.)*u.kpc,
z_sun=Zsun*u.kpc)
c= c.transform_to(gc_frame)
c.representation_type= 'cylindrical'
# galpy is left-handed, astropy right-handed
assert numpy.fabs(gcRpZ[0]-c.rho.to(u.kpc).value) < 10.**-10., "lbd to galcencyl conversion using galpy's methods does not agree with astropy"
assert numpy.fabs(gcRpZ[1]-numpy.pi+c.phi.to(u.rad).value) < 10.**-10., "lbd to galcencyl conversion using galpy's methods does not agree with astropy"
assert numpy.fabs(gcRpZ[2]-c.z.to(u.kpc).value) < 10.**-9.5, "lbd to galcencyl conversion using galpy's methods does not agree with astropy"
# Also with negative Xsun
l,b,d= 32., -12., 3.
Zsun= 0.025
# Using galpy
X,Y,Z= coords.lbd_to_XYZ(l,b,d,degree=True)
gcRpZ= coords.XYZ_to_galcencyl(X,Y,Z,Xsun=-8.,Zsun=Zsun)
# Using astropy
c= SkyCoord(l=l*u.deg,b=b*u.deg,distance=d*u.kpc,frame='galactic')
gc_frame= Galactocentric(galcen_distance=numpy.sqrt(8.**2.+Zsun**2.)*u.kpc,
z_sun=Zsun*u.kpc)
c= c.transform_to(gc_frame)
c.representation_type= 'cylindrical'
# galpy is now right-handed, astropy right-handed
assert numpy.fabs(gcRpZ[0]-c.rho.to(u.kpc).value) < 10.**-10., "lbd to galcencyl conversion using galpy's methods does not agree with astropy"
assert numpy.fabs(gcRpZ[1]-c.phi.to(u.rad).value) < 10.**-10., "lbd to galcencyl conversion using galpy's methods does not agree with astropy"
assert numpy.fabs(gcRpZ[2]-c.z.to(u.kpc).value) < 10.**-9.5, "lbd to galcencyl conversion using galpy's methods does not agree with astropy"
_turn_on_apy()
return None
def test_galcenrect_to_XYZ_negXsun():
gcX, gcY, gcZ= -1.,4.,2.
XYZ= numpy.array(coords.galcenrect_to_XYZ(gcX,gcY,gcZ,Xsun=1.,Zsun=0.2))
XYZn= numpy.array(coords.galcenrect_to_XYZ(-gcX,gcY,gcZ,Xsun=-1.,Zsun=0.2))
assert numpy.all(numpy.fabs(XYZ-XYZn) < 10.**-10.), 'galcenrect_to_XYZ conversion did not work as expected for negative Xsun'
return None
def test_galcenrect_to_XYZ():
gcX, gcY, gcZ= -1.,4.,2.
XYZ= coords.galcenrect_to_XYZ(gcX,gcY,gcZ,Xsun=1.,Zsun=0.)
assert numpy.fabs(XYZ[0]-2.) < 10.**-5., 'galcenrect_to_XYZ conversion did not work as expected'
assert numpy.fabs(XYZ[1]-4.) < 10.**-5., 'galcenrect_to_XYZ conversion did not work as expected'
assert numpy.fabs(XYZ[2]-2.) < 10.**-5., 'galcenrect_to_XYZ conversion did not work as expected'
# Also for arrays
s= numpy.arange(2)+1
XYZ= coords.galcenrect_to_XYZ(gcX*s,gcY*s,gcZ*s,Xsun=1.,Zsun=0.)
assert numpy.fabs(XYZ[0,0]-2.) < 10.**-5., 'galcenrect_to_XYZ conversion did not work as expected'
assert numpy.fabs(XYZ[0,1]-4.) < 10.**-5., 'galcenrect_to_XYZ conversion did not work as expected'
assert numpy.fabs(XYZ[0,2]-2.) < 10.**-5., 'galcenrect_to_XYZ conversion did not work as expected'
# Check 2nd one
assert numpy.fabs(XYZ[1,0]-3.) < 10.**-5., 'galcenrect_to_XYZ conversion did not work as expected'
assert numpy.fabs(XYZ[1,1]-8.) < 10.**-5., 'galcenrect_to_XYZ conversion did not work as expected'
assert numpy.fabs(XYZ[1,2]-4.) < 10.**-4.7, 'galcenrect_to_XYZ conversion did not work as expected'
# Also for arrays with Xsun/Zsun also arrays
s= numpy.arange(2)+1
XYZ= coords.galcenrect_to_XYZ(gcX*s,gcY*s,gcZ*s,Xsun=1.*s,Zsun=0.*s)
assert numpy.fabs(XYZ[0,0]-2.) < 10.**-5., 'galcenrect_to_XYZ conversion did not work as expected'
assert numpy.fabs(XYZ[0,1]-4.) < 10.**-5., 'galcenrect_to_XYZ conversion did not work as expected'
assert numpy.fabs(XYZ[0,2]-2.) < 10.**-5., 'galcenrect_to_XYZ conversion did not work as expected'
# Check 2nd one
assert numpy.fabs(XYZ[1,0]-4.) < 10.**-5., 'galcenrect_to_XYZ conversion did not work as expected'
assert numpy.fabs(XYZ[1,1]-8.) < 10.**-5., 'galcenrect_to_XYZ conversion did not work as expected'
assert numpy.fabs(XYZ[1,2]-4.) < 10.**-4.7, 'galcenrect_to_XYZ conversion did not work as expected'
return None
def test_galcenrect_to_XYZ_asInverse():
# Test that galcenrect_to_XYZ is the inverse of XYZ_to_galcenrect
X,Y,Z= 1.,3.,-2.
gcXYZ= coords.XYZ_to_galcenrect(X,Y,Z,Xsun=1.,Zsun=0.1)
Xt,Yt,Zt= coords.galcenrect_to_XYZ(gcXYZ[0],gcXYZ[1],gcXYZ[2],Xsun=1.,Zsun=0.1)
assert numpy.fabs(X-Xt) < 1e-14, 'galcenrect_to_XYZ is not the exact inverse of XYZ_to_galcenrect'
assert numpy.fabs(Y-Yt) < 1e-14, 'galcenrect_to_XYZ is not the exact inverse of XYZ_to_galcenrect'
assert numpy.fabs(Z-Zt) < 1e-14, 'galcenrect_to_XYZ is not the exact inverse of XYZ_to_galcenrect'
return None
def test_XYZ_to_galcencyl():
X,Y,Z= 5.,4.,-2.
gcRpZ= coords.XYZ_to_galcencyl(X,Y,Z,Xsun=8.,Zsun=0.)
assert numpy.fabs(gcRpZ[0]-5.) < 10.**-5., 'XYZ_to_galcencyl conversion did not work as expected'
assert numpy.fabs(gcRpZ[1]-numpy.arctan(4./3.)) < 10.**-5., 'XYZ_to_galcencyl conversion did not work as expected'
assert numpy.fabs(gcRpZ[2]+2.) < 10.**-4.8, 'XYZ_to_galcencyl conversion did not work as expected'
#Another X
X,Y,Z= 11.,4.,-2.
gcRpZ= coords.XYZ_to_galcencyl(X,Y,Z,Xsun=8.,Zsun=0.)
assert numpy.fabs(gcRpZ[0]-5.) < 10.**-5., 'XYZ_to_galcencyl conversion did not work as expected'
assert numpy.fabs(gcRpZ[1]-numpy.pi+numpy.arctan(4./3.)) < 10.**-5., 'XYZ_to_galcencyl conversion did not work as expected'
assert numpy.fabs(gcRpZ[2]+2.) < 10.**-4.6, 'XYZ_to_galcencyl conversion did not work as expected'
return None
def test_galcencyl_to_XYZ():
gcR, gcp, gcZ= 5.,numpy.arctan(4./3.),2.
XYZ= coords.galcencyl_to_XYZ(gcR,gcp,gcZ,Xsun=8.,Zsun=0.)
assert numpy.fabs(XYZ[0]-5.) < 10.**-5., 'galcencyl_to_XYZ conversion did not work as expected'
assert numpy.fabs(XYZ[1]-4.) < 10.**-5., 'galcencyl_to_XYZ conversion did not work as expected'
assert numpy.fabs(XYZ[2]-2.) < 10.**-4.7, 'galcencyl_to_XYZ conversion did not work as expected'
# Also for arrays
s= numpy.arange(2)+1
XYZ= coords.galcencyl_to_XYZ(gcR*s,gcp*s,gcZ*s,Xsun=8.,Zsun=0.)
assert numpy.fabs(XYZ[0,0]-5.) < 10.**-5., 'galcencyl_to_XYZ conversion did not work as expected'
assert numpy.fabs(XYZ[0,1]-4.) < 10.**-5., 'galcencyl_to_XYZ conversion did not work as expected'
assert numpy.fabs(XYZ[0,2]-2.) < 10.**-4.7, 'galcencyl_to_XYZ conversion did not work as expected'
# Also test the second one
assert numpy.fabs(XYZ[1,0]-10.8) < 10.**-5., 'galcencyl_to_XYZ conversion did not work as expected'
assert numpy.fabs(XYZ[1,1]-9.6) < 10.**-4.7, 'galcencyl_to_XYZ conversion did not work as expected'
assert numpy.fabs(XYZ[1,2]-4.0) < 10.**-4.5, 'galcencyl_to_XYZ conversion did not work as expected'
# Also for arrays where Xsun/Zsun are also arrays
s= numpy.arange(2)+1
XYZ= coords.galcencyl_to_XYZ(gcR*s,gcp*s,gcZ*s,Xsun=8.*s,Zsun=0.*s)
assert numpy.fabs(XYZ[0,0]-5.) < 10.**-5., 'galcencyl_to_XYZ conversion did not work as expected'
assert numpy.fabs(XYZ[0,1]-4.) < 10.**-5., 'galcencyl_to_XYZ conversion did not work as expected'
assert numpy.fabs(XYZ[0,2]-2.) < 10.**-4.7, 'galcencyl_to_XYZ conversion did not work as expected'
# Also test the second one
assert numpy.fabs(XYZ[1,0]-18.8) < 10.**-5., 'galcencyl_to_XYZ conversion did not work as expected'
assert numpy.fabs(XYZ[1,1]-9.6) < 10.**-4.5, 'galcencyl_to_XYZ conversion did not work as expected'
assert numpy.fabs(XYZ[1,2]-4.0) < 10.**-4., 'galcencyl_to_XYZ conversion did not work as expected'
return None
def test_galcencyl_to_XYZ_asInverse():
# Test that galcencyl_to_XYZ is the inverse of XYZ_to_galcencyl
X,Y,Z= 1.,3.,-2.
gcRpZ= coords.XYZ_to_galcencyl(X,Y,Z,Xsun=1.,Zsun=0.1)
Xt,Yt,Zt= coords.galcencyl_to_XYZ(gcRpZ[0],gcRpZ[1],gcRpZ[2],Xsun=1.,Zsun=0.1)
assert numpy.fabs(X-Xt) < 1e-14, 'galcencyl_to_XYZ is not the exact inverse of XYZ_to_galcencyl'
assert numpy.fabs(Y-Yt) < 1e-14, 'galcencyl_to_XYZ is not the exact inverse of XYZ_to_galcencyl'
assert numpy.fabs(Z-Zt) < 1e-14, 'galcencyl_to_XYZ is not the exact inverse of XYZ_to_galcencyl'
# Also for arrays where Xsun/Zsun are also arrays
s= numpy.arange(2)+1
gcRpZ1= coords.XYZ_to_galcencyl(X*s[0],Y*s[0],Z*s[0],Xsun=1.*s[0],
Zsun=0.1*s[0])
gcRpZ2= coords.XYZ_to_galcencyl(X*s[1],Y*s[1],Z*s[1],Xsun=1.*s[1],
Zsun=0.1*s[1])
XYZt= coords.galcencyl_to_XYZ(numpy.hstack((gcRpZ1[0],gcRpZ2[0])),
numpy.hstack((gcRpZ1[1],gcRpZ2[1])),
numpy.hstack((gcRpZ1[2],gcRpZ2[2])),
Xsun=1.*s,Zsun=0.1*s)
# first one
assert numpy.fabs(XYZt[0,0]-Xt) < 1e-14, 'galcencyl_to_XYZ is not the exact inverse of XYZ_to_galcencyl'
assert numpy.fabs(XYZt[0,1]-Yt) < 1e-14, 'galcencyl_to_XYZ is not the exact inverse of XYZ_to_galcencyl'
assert numpy.fabs(XYZt[0,2]-Zt) < 1e-14, 'galcencyl_to_XYZ is not the exact inverse of XYZ_to_galcencyl'
# second one
assert numpy.fabs(XYZt[1,0]-Xt*s[1]) < 1e-14, 'galcencyl_to_XYZ is not the exact inverse of XYZ_to_galcencyl'
assert numpy.fabs(XYZt[1,1]-Yt*s[1]) < 1e-14, 'galcencyl_to_XYZ is not the exact inverse of XYZ_to_galcencyl'
assert numpy.fabs(XYZt[1,2]-Zt*s[1]) < 1e-14, 'galcencyl_to_XYZ is not the exact inverse of XYZ_to_galcencyl'
return None
def test_vxvyvz_to_galcenrect():
vx,vy,vz= 10.,-20.,30
vgc= coords.vxvyvz_to_galcenrect(vx,vy,vz,vsun=[-5.,10.,5.])
assert numpy.fabs(vgc[0]+15.) < 10.**-4., 'vxvyvz_to_galcenrect conversion did not work as expected'
assert numpy.fabs(vgc[1]+10.) < 10.**-4., 'vxvyvz_to_galcenrect conversion did not work as expected'
assert numpy.fabs(vgc[2]-35.) < 10.**-4., 'vxvyvz_to_galcenrect conversion did not work as expected'
return None
def test_vxvyvz_to_galcenrect_negXsun():
vx,vy,vz= 10.,-20.,30
vgc= coords.vxvyvz_to_galcenrect(vx,vy,vz,vsun=[-5.,10.,5.],
Xsun=1.1,Zsun=0.2)
vgcn= coords.vxvyvz_to_galcenrect(vx,vy,vz,vsun=[5.,10.,5.],
Xsun=-1.1,Zsun=0.2)
assert numpy.fabs(vgc[0]+vgcn[0]) < 10.**-4., 'vxvyvz_to_galcenrect conversion did not work as expected for negative Xsun'
assert numpy.fabs(vgc[1]-vgcn[1]) < 10.**-4., 'vxvyvz_to_galcenrect conversion did not work as expected for negative Xsun'
assert numpy.fabs(vgc[2]-vgcn[2]) < 10.**-4., 'vxvyvz_to_galcenrect conversion did not work as expected for negative Xsun'
return None
def test_vrpmllpmbb_to_galcenrect_galpyvsastropy():
# Only run this for astropy>3
if not _APY3: return None
# Test that galpy's transformations agree with astropy's
from astropy.coordinates import SkyCoord, Galactocentric, \
CartesianDifferential
import astropy.units as u
_turn_off_apy()
l,b,d= 32., -12., 3.
vr,pmll,pmbb= -112., -13.,5.
Zsun= 0.025
Rsun= 8.
vsun= [-10.,230.,7.]
# Using galpy
vx,vy,vz= coords.vrpmllpmbb_to_vxvyvz(vr,pmll,pmbb,l,b,d,degree=True)
vXYZg= coords.vxvyvz_to_galcenrect(vx,vy,vz,vsun=vsun,Xsun=Rsun,
Zsun=Zsun)
# Using astropy
c= SkyCoord(l=l*u.deg,b=b*u.deg,distance=d*u.kpc,
radial_velocity=vr*u.km/u.s,pm_l_cosb=pmll*u.mas/u.yr,
pm_b=pmbb*u.mas/u.yr,frame='galactic')
gc_frame= Galactocentric(\
galcen_distance=numpy.sqrt(Rsun**2.+Zsun**2.)*u.kpc,z_sun=Zsun*u.kpc,
galcen_v_sun=CartesianDifferential(numpy.array([-vsun[0],vsun[1],vsun[2]])*u.km/u.s))
c= c.transform_to(gc_frame)
c.representation_type= 'cartesian'
# galpy is left-handed, astropy right-handed
assert numpy.fabs(vXYZg[0]+c.v_x.to(u.km/u.s).value) < 10.**-8., "vrpmllpmbblbd to galcenrect conversion using galpy's methods does not agree with astropy"
assert numpy.fabs(vXYZg[1]-c.v_y.to(u.km/u.s).value) < 10.**-8., "vrpmllpmbb to galcenrect conversion using galpy's methods does not agree with astropy"
assert numpy.fabs(vXYZg[2]-c.v_z.to(u.km/u.s).value) < 10.**-8., "vrpmllpmbb to galcenrect conversion using galpy's methods does not agree with astropy"
# Also with negative Xsun
l,b,d= 32., -12., 3.
Zsun= 0.025
Rsun= -8.
vsun= numpy.array([-10.,230.,7.])
# Using galpy
vx,vy,vz= coords.vrpmllpmbb_to_vxvyvz(vr,pmll,pmbb,l,b,d,degree=True)
vXYZg= coords.vxvyvz_to_galcenrect(vx,vy,vz,vsun=vsun,Xsun=Rsun,
Zsun=Zsun)
# Using astropy
c= SkyCoord(l=l*u.deg,b=b*u.deg,distance=d*u.kpc,
radial_velocity=vr*u.km/u.s,pm_l_cosb=pmll*u.mas/u.yr,
pm_b=pmbb*u.mas/u.yr,frame='galactic')
gc_frame= Galactocentric(\
galcen_distance=numpy.sqrt(Rsun**2.+Zsun**2.)*u.kpc,z_sun=Zsun*u.kpc,
galcen_v_sun=CartesianDifferential(numpy.array([vsun[0],vsun[1],vsun[2]])*u.km/u.s))
c= c.transform_to(gc_frame)
c.representation_type= 'cartesian'
# galpy is now right-handed, astropy right-handed
assert numpy.fabs(vXYZg[0]-c.v_x.to(u.km/u.s).value) < 10.**-8., "vrpmllpmbblbd to galcenrect conversion using galpy's methods does not agree with astropy"
assert numpy.fabs(vXYZg[1]-c.v_y.to(u.km/u.s).value) < 10.**-8., "vrpmllpmbb to galcenrect conversion using galpy's methods does not agree with astropy"
assert numpy.fabs(vXYZg[2]-c.v_z.to(u.km/u.s).value) < 10.**-8., "vrpmllpmbb to galcenrect conversion using galpy's methods does not agree with astropy"
_turn_on_apy()
return None
def test_vxvyvz_to_galcencyl():
X,Y,Z= 3.,4.,2.
vx,vy,vz= 10.,-20.,30
vgc= coords.vxvyvz_to_galcencyl(vx,vy,vz,X,Y,Z,vsun=[-5.,10.,5.])
assert numpy.fabs(vgc[0]+17.) < 10.**-4., 'vxvyvz_to_galcenrect conversion did not work as expected'
assert numpy.fabs(vgc[1]-6.) < 10.**-4., 'vxvyvz_to_galcenrect conversion did not work as expected'
assert numpy.fabs(vgc[2]-35.) < 10.**-4., 'vxvyvz_to_galcenrect conversion did not work as expected'
#with galcen=True
vgc= coords.vxvyvz_to_galcencyl(vx,vy,vz,5.,numpy.arctan(4./3.),Z,
vsun=[-5.,10.,5.],galcen=True)
assert numpy.fabs(vgc[0]+17.) < 10.**-4., 'vxvyvz_to_galcenrect conversion did not work as expected'
assert numpy.fabs(vgc[1]-6.) < 10.**-4., 'vxvyvz_to_galcenrect conversion did not work as expected'
assert numpy.fabs(vgc[2]-35.) < 10.**-4., 'vxvyvz_to_galcenrect conversion did not work as expected'
return None
def test_vrpmllpmbb_to_galcencyl_galpyvsastropy():
# Only run this for astropy>3
if not _APY3: return None
# Test that galpy's transformations agree with astropy's
from astropy.coordinates import SkyCoord, Galactocentric, \
CartesianDifferential
import astropy.units as u
_turn_off_apy()
l,b,d= 32., -12., 3.
vr,pmll,pmbb= -112., -13.,5.
Zsun= 0.025
Rsun= 8.
vsun= [-10.,230.,7.]
# Using galpy
X,Y,Z= coords.lbd_to_XYZ(l,b,d,degree=True)
gcXYZ= coords.XYZ_to_galcenrect(X,Y,Z,Xsun=Rsun,Zsun=Zsun)
vx,vy,vz= coords.vrpmllpmbb_to_vxvyvz(vr,pmll,pmbb,l,b,d,degree=True)
vRTZg= coords.vxvyvz_to_galcencyl(vx,vy,vz,gcXYZ[0],gcXYZ[1],gcXYZ[2],
vsun=vsun,Xsun=Rsun,Zsun=Zsun)
# Using astropy
c= SkyCoord(l=l*u.deg,b=b*u.deg,distance=d*u.kpc,
radial_velocity=vr*u.km/u.s,pm_l_cosb=pmll*u.mas/u.yr,
pm_b=pmbb*u.mas/u.yr,frame='galactic')
gc_frame= Galactocentric(\
galcen_distance=numpy.sqrt(Rsun**2.+Zsun**2.)*u.kpc,z_sun=Zsun*u.kpc,
galcen_v_sun=CartesianDifferential(numpy.array([-vsun[0],vsun[1],vsun[2]])*u.km/u.s))
c= c.transform_to(gc_frame)
c.representation_type= 'cylindrical'
# galpy is left-handed, astropy right-handed
assert numpy.fabs(vRTZg[0]-c.d_rho.to(u.km/u.s).value) < 10.**-8., "vrpmllpmbblbd to galcencyl conversion using galpy's methods does not agree with astropy"
assert numpy.fabs(vRTZg[1]+(c.d_phi*c.rho).to(u.km/u.s,
equivalencies=u.dimensionless_angles()).value) < 10.**-8., "vrpmllpmbb to galcencyl conversion using galpy's methods does not agree with astropy"
assert numpy.fabs(vRTZg[2]-c.d_z.to(u.km/u.s).value) < 10.**-8., "vrpmllpmbb to galcencyl conversion using galpy's methods does not agree with astropy"
# Also with negative Xsun
l,b,d= 32., -12., 3.
Zsun= 0.025
Rsun= -8.
vsun= numpy.array([-10.,230.,7.])
# Using galpy
X,Y,Z= coords.lbd_to_XYZ(l,b,d,degree=True)
gcXYZ= coords.XYZ_to_galcenrect(X,Y,Z,Xsun=Rsun,Zsun=Zsun)
vx,vy,vz= coords.vrpmllpmbb_to_vxvyvz(vr,pmll,pmbb,l,b,d,degree=True)
vRTZg= coords.vxvyvz_to_galcencyl(vx,vy,vz,gcXYZ[0],gcXYZ[1],gcXYZ[2],
vsun=vsun,Xsun=Rsun,Zsun=Zsun)
# Using astropy
c= SkyCoord(l=l*u.deg,b=b*u.deg,distance=d*u.kpc,
radial_velocity=vr*u.km/u.s,pm_l_cosb=pmll*u.mas/u.yr,
pm_b=pmbb*u.mas/u.yr,frame='galactic')
gc_frame= Galactocentric(\
galcen_distance=numpy.sqrt(Rsun**2.+Zsun**2.)*u.kpc,z_sun=Zsun*u.kpc,
galcen_v_sun=CartesianDifferential(numpy.array([vsun[0],vsun[1],vsun[2]])*u.km/u.s))
c= c.transform_to(gc_frame)
c.representation_type= 'cylindrical'
# galpy is left-handed, astropy right-handed
assert numpy.fabs(vRTZg[0]-c.d_rho.to(u.km/u.s).value) < 10.**-8., "vrpmllpmbblbd to galcencyl conversion using galpy's methods does not agree with astropy"
assert numpy.fabs(vRTZg[1]-(c.d_phi*c.rho).to(u.km/u.s,
equivalencies=u.dimensionless_angles()).value) < 10.**-8., "vrpmllpmbb to galcencyl conversion using galpy's methods does not agree with astropy"
assert numpy.fabs(vRTZg[2]-c.d_z.to(u.km/u.s).value) < 10.**-8., "vrpmllpmbb to galcencyl conversion using galpy's methods does not agree with astropy"
_turn_on_apy()
return None
def test_galcenrect_to_vxvyvz():
vxg,vyg,vzg= -15.,-10.,35.
vxyz= coords.galcenrect_to_vxvyvz(vxg,vyg,vzg,vsun=[-5.,10.,5.])
assert numpy.fabs(vxyz[0]-10.) < 10.**-4., 'galcenrect_to_vxvyvz conversion did not work as expected'
assert numpy.fabs(vxyz[1]+20.) < 10.**-4., 'galcenrect_to_vxvyvz conversion did not work as expected'
assert numpy.fabs(vxyz[2]-30.) < 10.**-4., 'galcenrect_to_vxvyvz conversion did not work as expected'
#Also for arrays
os= numpy.ones(2)
vxyz= coords.galcenrect_to_vxvyvz(os*vxg,os*vyg,os*vzg,
vsun=[-5.,10.,5.])
assert numpy.all(numpy.fabs(vxyz[:,0]-10.) < 10.**-4.), 'galcenrect_to_vxvyvz conversion did not work as expected'
assert numpy.all(numpy.fabs(vxyz[:,1]+20.) < 10.**-4.), 'galcenrect_to_vxvyvz conversion did not work as expected'
assert numpy.all(numpy.fabs(vxyz[:,2]-30.) < 10.**-4.), 'galcenrect_to_vxvyvz conversion did not work as expected'
return None
def test_galcenrect_to_vxvyvz_negXsun():
vxg,vyg,vzg= -15.,-10.,35.
vxyz= coords.galcenrect_to_vxvyvz(vxg,vyg,vzg,vsun=[-5.,10.,5.],
Xsun=1.1,Zsun=0.2)
vxyzn= coords.galcenrect_to_vxvyvz(-vxg,vyg,vzg,vsun=[5.,10.,5.],
Xsun=-1.1,Zsun=0.2)
assert numpy.all(numpy.fabs(numpy.array(vxyz)-numpy.array(vxyzn)) < 10.**-4.), 'galcenrect_to_vxvyvz conversion did not work as expected'
return None
def test_galcenrect_to_vxvyvz_asInverse():
# Test that galcenrect_to_vxvyvz is the inverse of vxvyvz_to_galcenrect
vx,vy,vz= -15.,-10.,35.
vxg,vyg,vzg= coords.vxvyvz_to_galcenrect(vx,vy,vz,vsun=[-5.,10.,5.])
vxt,vyt,vzt= coords.galcenrect_to_vxvyvz(vxg,vyg,vzg,vsun=[-5.,10.,5.])
assert numpy.fabs(vx-vxt) < 10.**-14., 'galcenrect_to_vxvyvz is not the inverse of vxvyvz_to_galcenrect'
assert numpy.fabs(vy-vyt) < 10.**-14., 'galcenrect_to_vxvyvz is not the inverse of vxvyvz_to_galcenrect'
assert numpy.fabs(vz-vzt) < 10.**-14., 'galcenrect_to_vxvyvz is not the inverse of vxvyvz_to_galcenrect'
#Also for arrays
os= numpy.ones(2)
vxyzg= coords.vxvyvz_to_galcenrect(vx*os,vy*os,vz*os,
vsun=[-5.,10.,5.])
vxyzt= coords.galcenrect_to_vxvyvz(vxyzg[:,0],vxyzg[:,1],vxyzg[:,2],
vsun=[-5.,10.,5.])
assert numpy.all(numpy.fabs(vxyzt[:,0]-vx*os) < 10.**-10.), 'galcenrect_to_vxvyvz is not the inverse of vxvyvz_to_galcenrect'
assert numpy.all(numpy.fabs(vxyzt[:,1]-vy*os) < 10.**-10.), 'galcenrect_to_vxvyvz is not the inverse of vxvyvz_to_galcenrect'
assert numpy.all(numpy.fabs(vxyzt[:,2]-vz*os) < 10.**-10.), 'galcenrect_to_vxvyvz is not the inverse of vxvyvz_to_galcenrect'
return None
def test_galcencyl_to_vxvyvz():
vr,vp,vz= -17.,6.,35.
phi= numpy.arctan(4./3.)
vxyz= coords.galcencyl_to_vxvyvz(vr,vp,vz,phi,vsun=[-5.,10.,5.])
assert numpy.fabs(vxyz[0]-10.) < 10.**-4., 'galcenrect_to_vxvyvz conversion did not work as expected'
assert numpy.fabs(vxyz[1]+20.) < 10.**-4., 'galcenrect_to_vxvyvz conversion did not work as expected'
assert numpy.fabs(vxyz[2]-30.) < 10.**-4., 'galcenrect_to_vxvyvz conversion did not work as expected'
return None
def test_galcencyl_to_vxvyvz_asInverse():
# Test that galcencyl_to_vxvyvz is the inverse of vxvyvz_to_galcencyl
vx,vy,vz= -15.,-10.,35.
phi= numpy.arctan(4./3.)
vrg,vtg,vzg= coords.vxvyvz_to_galcencyl(vx,vy,vz,0.,phi,0.,
vsun=[-5.,10.,5.],galcen=True)
vxt,vyt,vzt= coords.galcencyl_to_vxvyvz(vrg,vtg,vzg,phi,vsun=[-5.,10.,5.])
assert numpy.fabs(vx-vxt) < 10.**-14., 'galcencyl_to_vxvyvz is not the inverse of vxvyvz_to_galcencyl'
assert numpy.fabs(vy-vyt) < 10.**-14., 'galcencyl_to_vxvyvz is not the inverse of vxvyvz_to_galcencyl'
assert numpy.fabs(vz-vzt) < 10.**-14., 'galcencyl_to_vxvyvz is not the inverse of vxvyvz_to_galcencyl'
#Also for arrays
os= numpy.ones(2)
vx,vy,vz= -15.,-10.,35.
phi= numpy.arctan(4./3.)
vrtzg= coords.vxvyvz_to_galcencyl(vx*os,vy*os,vz*os,0.,phi*os,0.,
vsun=[-5.,10.,5.],galcen=True)
vxyzt= coords.galcencyl_to_vxvyvz(vrtzg[:,0],vrtzg[:,1],vrtzg[:,2],
phi*os,vsun=[-5.,10.,5.])
assert numpy.all(numpy.fabs(vxyzt[:,0]-vx*os) < 10.**-10.), 'galcencyl_to_vxvyvz is not the inverse of vxvyvz_to_galcencyl'
assert numpy.all(numpy.fabs(vxyzt[:,1]-vy*os) < 10.**-10.), 'galcencyl_to_vxvyvz is not the inverse of vxvyvz_to_galcencyl'
assert numpy.all(numpy.fabs(vxyzt[:,2]-vz*os) < 10.**-10.), 'galcencyl_to_vxvyvz is not the inverse of vxvyvz_to_galcencyl'
return None
def test_sphergal_to_rectgal():
l,b,d= 90.,0.,1.
vr,pmll,pmbb= 10.,-20./4.740470463496208,30./4.740470463496208
X,Y,Z,vx,vy,vz= coords.sphergal_to_rectgal(l,b,d,vr,pmll,pmbb,
degree=True)
assert numpy.fabs(X-0.) < 10.**-9., 'sphergal_to_rectgal conversion did not work as expected'
assert numpy.fabs(Y-1.) < 10.**-9., 'sphergal_to_rectgal conversion did not work as expected'
assert numpy.fabs(Z-0.) < 10.**-9., 'sphergal_to_rectgal conversion did not work as expected'
assert numpy.fabs(vx-20.) < 10.**-9., 'sphergal_to_rectgal conversion did not work as expected'
assert numpy.fabs(vy-10.) < 10.**-9., 'sphergal_to_rectgal conversion did not work as expected'
assert numpy.fabs(vz-30.) < 10.**-9., 'sphergal_to_rectgal conversion did not work as expected'
#Also test for degree=False
X,Y,Z,vx,vy,vz= coords.sphergal_to_rectgal(l/180.*numpy.pi,
b/180.*numpy.pi,
d,vr,pmll,pmbb,
degree=False)
assert numpy.fabs(X-0.) < 10.**-9., 'sphergal_to_rectgal conversion did not work as expected'
assert numpy.fabs(Y-1.) < 10.**-9., 'sphergal_to_rectgal conversion did not work as expected'
assert numpy.fabs(Z-0.) < 10.**-9., 'sphergal_to_rectgal conversion did not work as expected'
assert numpy.fabs(vx-20.) < 10.**-9., 'sphergal_to_rectgal conversion did not work as expected'
assert numpy.fabs(vy-10.) < 10.**-9., 'sphergal_to_rectgal conversion did not work as expected'
assert numpy.fabs(vz-30.) < 10.**-9., 'sphergal_to_rectgal conversion did not work as expected'
#Also test for arrays
os= numpy.ones(2)
XYZvxvyvz= coords.sphergal_to_rectgal(os*l,os*b,os*d,
os*vr,os*pmll,os*pmbb,
degree=True)
X= XYZvxvyvz[:,0]
Y= XYZvxvyvz[:,1]
Z= XYZvxvyvz[:,2]
vx= XYZvxvyvz[:,3]
vy= XYZvxvyvz[:,4]
vz= XYZvxvyvz[:,5]
assert numpy.all(numpy.fabs(X-0.) < 10.**-9.), 'sphergal_to_rectgal conversion did not work as expected'
assert numpy.all(numpy.fabs(Y-1.) < 10.**-9.), 'sphergal_to_rectgal conversion did not work as expected'
assert numpy.all(numpy.fabs(Z-0.) < 10.**-9.), 'sphergal_to_rectgal conversion did not work as expected'
assert numpy.all(numpy.fabs(vx-20.) < 10.**-9.), 'sphergal_to_rectgal conversion did not work as expected'
assert numpy.all(numpy.fabs(vy-10.) < 10.**-9.), 'sphergal_to_rectgal conversion did not work as expected'
assert numpy.all(numpy.fabs(vz-30.) < 10.**-9.), 'sphergal_to_rectgal conversion did not work as expected'
return None
def test_rectgal_to_sphergal():
#Test that this is the inverse of sphergal_to_rectgal
l,b,d= 90.,30.,1.
vr,pmll,pmbb= 10.,-20.,30.
X,Y,Z,vx,vy,vz= coords.sphergal_to_rectgal(l,b,d,vr,pmll,pmbb,
degree=True)
lt,bt,dt,vrt,pmllt,pmbbt= coords.rectgal_to_sphergal(X,Y,Z,
vx,vy,vz,
degree=True)
assert numpy.fabs(lt-l) < 10.**-10., 'rectgal_to_sphergal conversion did not work as expected'
assert numpy.fabs(bt-b) < 10.**-10., 'rectgal_to_sphergal conversion did not work as expected'
assert numpy.fabs(dt-d) < 10.**-10., 'rectgal_to_sphergal conversion did not work as expected'
assert numpy.fabs(vrt-vr) < 10.**-10., 'rectgal_to_sphergal conversion did not work as expected'
assert numpy.fabs(pmllt-pmll) < 10.**-10., 'rectgal_to_sphergal conversion did not work as expected'
assert numpy.fabs(pmbbt-pmbb) < 10.**-10., 'rectgal_to_sphergal conversion did not work as expected'
#Also test for degree=False
lt,bt,dt,vrt,pmllt,pmbbt= coords.rectgal_to_sphergal(X,Y,Z,
vx,vy,vz,
degree=False)
assert numpy.fabs(lt-l/180.*numpy.pi) < 10.**-10., 'rectgal_to_sphergal conversion did not work as expected'
assert numpy.fabs(bt-b/180.*numpy.pi) < 10.**-10., 'rectgal_to_sphergal conversion did not work as expected'
assert numpy.fabs(dt-d) < 10.**-10., 'rectgal_to_sphergal conversion did not work as expected'
assert numpy.fabs(vrt-vr) < 10.**-10., 'rectgal_to_sphergal conversion did not work as expected'
assert numpy.fabs(pmllt-pmll) < 10.**-10., 'rectgal_to_sphergal conversion did not work as expected'
assert numpy.fabs(pmbbt-pmbb) < 10.**-10., 'rectgal_to_sphergal conversion did not work as expected'
#Also test for arrays
os= numpy.ones(2)
lbdvrpmllpmbbt= coords.rectgal_to_sphergal(os*X,os*Y,os*Z,
os*vx,os*vy,
os*vz,
degree=True)
lt= lbdvrpmllpmbbt[:,0]
bt= lbdvrpmllpmbbt[:,1]
dt= lbdvrpmllpmbbt[:,2]
vrt= lbdvrpmllpmbbt[:,3]
pmllt= lbdvrpmllpmbbt[:,4]
pmbbt= lbdvrpmllpmbbt[:,5]
assert numpy.all(numpy.fabs(lt-l) < 10.**-10.), 'rectgal_to_sphergal conversion did not work as expected'
assert numpy.all(numpy.fabs(bt-b) < 10.**-10.), 'rectgal_to_sphergal conversion did not work as expected'
assert numpy.all(numpy.fabs(dt-d) < 10.**-10.), 'rectgal_to_sphergal conversion did not work as expected'
assert numpy.all(numpy.fabs(vrt-vr) < 10.**-10.), 'rectgal_to_sphergal conversion did not work as expected'
assert numpy.all(numpy.fabs(pmllt-pmll) < 10.**-10.), 'rectgal_to_sphergal conversion did not work as expected'
assert numpy.all(numpy.fabs(pmbbt-pmbb) < 10.**-10.), 'rectgal_to_sphergal conversion did not work as expected'
return None
def test_pmrapmdec_to_pmllpmbb():
#This is a random ra,dec
ra, dec= 132., -20.4
pmra, pmdec= 10., 20.
pmll, pmbb= coords.pmrapmdec_to_pmllpmbb(pmra,pmdec,
ra,dec,degree=True,epoch=1950.)
assert numpy.fabs(numpy.sqrt(pmll**2.+pmbb**2.)-numpy.sqrt(pmra**2.+pmdec**2.)) < 10.**-10., 'pmrapmdec_to_pmllpmbb conversion did not work as expected'
# This is close to the NGP at 1950.
ra, dec= 192.24, 27.39
pmra, pmdec= 10., 20.
os= numpy.ones(2)
pmllpmbb= coords.pmrapmdec_to_pmllpmbb(os*pmra,os*pmdec,
os*ra,os*dec,
degree=True,epoch=1950.)
pmll= pmllpmbb[:,0]
pmbb= pmllpmbb[:,1]
assert numpy.all(numpy.fabs(numpy.sqrt(pmll**2.+pmbb**2.)-numpy.sqrt(pmra**2.+pmdec**2.)) < 10.**-10.), 'pmrapmdec_to_pmllpmbb conversion did not work as expected close to the NGP'
# This is the NGP at 1950.
ra, dec= 192.25, 27.4
pmra, pmdec= 10., 20.
os= numpy.ones(2)
pmllpmbb= coords.pmrapmdec_to_pmllpmbb(os*pmra,os*pmdec,
os*ra,os*dec,
degree=True,epoch=1950.)
pmll= pmllpmbb[:,0]
pmbb= pmllpmbb[:,1]
assert numpy.all(numpy.fabs(numpy.sqrt(pmll**2.+pmbb**2.)-numpy.sqrt(pmra**2.+pmdec**2.)) < 10.**-10.), 'pmrapmdec_to_pmllpmbb conversion did not work as expected for the NGP'
# This is the NCP
ra, dec= numpy.pi, numpy.pi/2.
pmra, pmdec= 10., 20.
pmll, pmbb= coords.pmrapmdec_to_pmllpmbb(pmra,pmdec,
ra,dec,degree=False,
epoch=1950.)
assert numpy.fabs(numpy.sqrt(pmll**2.+pmbb**2.)-numpy.sqrt(pmra**2.+pmdec**2.)) < 10.**-10., 'pmrapmdec_to_pmllpmbb conversion did not work as expected for the NCP'
return None