forked from Starlink/palpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_pal.py
2596 lines (2182 loc) · 103 KB
/
test_pal.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
# Test script for PAL interface
# Copyright (C) 2012 Tim Jenness and Science and Technology
# Facilities Council.
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import with_statement
import unittest
import palpy as pal
import numpy as np
class TestPal(unittest.TestCase):
def test_addet(self):
rm = 2.0
dm = -1.0
eq = 1975.0
(r1, d1) = pal.addet(rm, dm, eq)
self.assertAlmostEqual(r1 - rm, 2.983864874295250e-6, 11)
self.assertAlmostEqual(d1 - dm, 2.379650804185118e-7, 11)
(r2, d2) = pal.subet(r1, d1, eq)
self.assertAlmostEqual(r2 - rm, 0.0, 11)
self.assertAlmostEqual(d2 - dm, 0.0, 11)
def test_afin(self):
(d, i) = pal.dafin("12 34 56.7 |", 1)
self.assertEqual(i, 12)
self.assertAlmostEqual(d, 0.2196045986911432, 12)
(d, i) = pal.dafin("45 00 00.000 ", 1)
self.assertEqual(i, 14)
self.assertAlmostEqual(d, pal.DPI / 4.0, 12)
(d, i) = pal.dafin("30.2 < just decimal degrees", 1)
self.assertEqual(i, 6)
self.assertAlmostEqual(d, 30.2 * pal.DD2R, 12)
(d, i) = pal.dafin(" 30 23.6 < decimal armin", 1)
self.assertEqual(i, 11)
self.assertAlmostEqual(d, (30 + 23.6/60)*pal.DD2R, 12)
(d, i) = pal.dafin(" offset into string: 45.0 <<<", 22)
self.assertEqual(i, 27)
self.assertAlmostEqual(d, pal.DPI / 4.0, 12)
self.assertRaises(ValueError, pal.dafin, " not a sexagesimal string ", 1)
self.assertRaises(ValueError, pal.dafin, " 750.4 21 0.0 bad degrees ", 1)
self.assertRaises(ValueError, pal.dafin, " 45 30.5 0.0 bad arcminutes ", 1)
self.assertRaises(ValueError, pal.dafin, " 45 -30 0.0 bad arcminutes ", 1)
self.assertRaises(ValueError, pal.dafin, " 45 72 0.0 too many arcminutes ", 1)
self.assertRaises(ValueError, pal.dafin, " 45 43 85.0 too many arcseconds ", 1)
def test_airmass(self):
self.assertAlmostEqual(pal.airmas(1.2354),
3.015698990074724, 11)
def test_airmass_vector(self):
"""
Test that airmassVector gives results consistent with airmass
"""
np.random.seed(145)
n_samples = 1000
zd = np.random.random_sample(n_samples)*0.5*np.pi
test_am = pal.airmasVector(zd)
for ii in range(n_samples):
control_am = pal.airmas(zd[ii])
self.assertEqual(control_am, test_am[ii])
self.assertFalse(np.isnan(test_am[ii]))
def test_altaz(self):
(az, azd, azdd, el, eld, eldd, pa, pad, padd) = pal.altaz(0.7, -0.7, -0.65)
self.assertAlmostEqual(az, 4.400560746660174, 12)
self.assertAlmostEqual(azd, -0.2015438937145421, 12)
self.assertAlmostEqual(azdd, -0.4381266949668748, 13)
self.assertAlmostEqual(el, 1.026646506651396, 12)
self.assertAlmostEqual(eld, -0.7576920683826450, 13)
self.assertAlmostEqual(eldd, 0.04922465406857453, 14)
self.assertAlmostEqual(pa, 1.707639969653937, 12)
self.assertAlmostEqual(pad, 0.4717832355365627, 13)
self.assertAlmostEqual(padd, -0.2957914128185515, 13)
def test_altaz_vector(self):
np.random.seed(32)
phi = 0.5
ha_in = np.random.sample(20)*2.0*np.pi
dec_in = (np.random.sample(20)-0.5)*np.pi
az_c = np.zeros(20, dtype=np.float64)
azd_c = np.zeros(20, dtype=np.float64)
azdd_c = np.zeros(20, dtype=np.float64)
el_c = np.zeros(20, dtype=np.float64)
eld_c = np.zeros(20, dtype=np.float64)
eldd_c = np.zeros(20, dtype=np.float64)
pa_c = np.zeros(20, dtype=np.float64)
pad_c = np.zeros(20, dtype=np.float64)
padd_c = np.zeros(20, dtype=np.float64)
for i in range(20):
az, azd, azdd, el, eld, eldd, pa, pad, padd = pal.altaz(ha_in[i], dec_in[i], phi)
az_c[i] = az
azd_c[i] = azd
azdd_c[i] = azdd
el_c[i] = el
eld_c[i] = eld
eldd_c[i] = eldd
pa_c[i] = pa
pad_c[i] = pad
padd_c[i] = padd
azT, azdT, azddT, elT, eldT, elddT, paT, padT, paddT = pal.altazVector(ha_in, dec_in, phi)
for i in range(20):
self.assertAlmostEqual(az_c[i], azT[i], 12)
self.assertAlmostEqual(azd_c[i], azdT[i], 12)
self.assertAlmostEqual(azdd_c[i], azddT[i], 12)
self.assertAlmostEqual(el_c[i], elT[i], 12)
self.assertAlmostEqual(eld_c[i], eldT[i], 12)
self.assertAlmostEqual(eldd_c[i], elddT[i], 12)
self.assertAlmostEqual(pa_c[i], paT[i], 12)
self.assertAlmostEqual(pad_c[i], padT[i], 12)
self.assertAlmostEqual(padd_c[i], paddT[i], 12)
# test that an exception is raised if input arrays have
# different lengths
self.assertRaises(ValueError, pal.altazVector, ha_in[:10], dec_in, phi)
def test_amp(self):
(rm, dm) = pal.amp(2.345, -1.234, 50100., 1990.)
self.assertAlmostEqual(rm, 2.344472180027961, 6)
self.assertAlmostEqual(dm, -1.233573099847705, 7)
(rm, dm) = pal.amp(1.234, -0.567, 55927., 2010.)
self.assertAlmostEqual(rm, 1.233512033578303857, 12)
self.assertAlmostEqual(dm, -0.56702909748530827549, 12)
def test_ampqk(self):
amprms = pal.mappa(2010.0, 55927.0)
(rm, dm) = pal.ampqk(1.234, -0.567, amprms)
self.assertAlmostEqual(rm, 1.233512033578303857, 11)
self.assertAlmostEqual(dm, -0.56702909748530827549, 11)
def test_ampqk_vector(self):
"""
Test that ampqkVector produces results consistent with ampqk
"""
np.random.seed(144)
n_samples = 200
amprms = pal.mappa(2010.0, 55927.0)
ra_in = np.random.random_sample(n_samples)*2.0*np.pi
dec_in = (np.random.random_sample(n_samples)-0.5)*np.pi
testRa, testDec = pal.ampqkVector(ra_in, dec_in, amprms)
for ii in range(n_samples):
controlRa, controlDec = pal.ampqk(ra_in[ii], dec_in[ii], amprms)
self.assertEqual(controlRa, testRa[ii])
self.assertEqual(controlDec, testDec[ii])
self.assertFalse(np.isnan(testRa[ii]))
self.assertFalse(np.isnan(testDec[ii]))
# test that ampqkVector and mapqkzVector invert each other
ra_roundtrip, dec_roundtrip = pal.mapqkzVector(testRa, testDec, amprms)
pal.DR2AS = 3600.0*np.degrees(1.0)
distance = pal.DR2AS*pal.dsepVector(ra_roundtrip, dec_roundtrip,
ra_in, dec_in)
np.testing.assert_array_almost_equal(distance, np.zeros(n_samples), 9)
# test that exceptions are raised when input arrays are not of the same
# length
self.assertRaises(ValueError, pal.ampqkVector, ra_in[:17], dec_in, amprms)
def test_aopqk_vector(self):
date = 51000.1
dut = 25.0
elongm = 2.1
phim = 0.5
hm = 3000.0
xp = -0.5e-6
yp = 1.0e-6
tdk = 280.0
pmb = 550.0
rh = 0.6
tlr = 0.006
wl = 0.45
obsrms = pal.aoppa(date, dut, elongm, phim, hm, xp, yp, tdk, pmb, rh, wl, tlr)
np.random.seed(32)
n_tests = 100
ra_in = np.random.sample(n_tests)*2.0*np.pi
dec_in = (np.random.sample(n_tests)-0.5)*np.pi
az_control = None
ze_control = None
ha_control = None
d_control = None
r_control = None
for (rr, dd) in zip(ra_in, dec_in):
az, ze, ha, d, r = pal.aopqk(rr, dd, obsrms)
if az_control is None:
az_control = np.array([az])
ze_control = np.array([ze])
ha_control = np.array([ha])
d_control = np.array([d])
r_control = np.array([r])
else:
az_control = np.append(az_control, az)
ze_control = np.append(ze_control, ze)
ha_control = np.append(ha_control, ha)
d_control = np.append(d_control, d)
r_control = np.append(r_control, r)
azTest, zeTest, haTest, dTest, rTest = pal.aopqkVector(ra_in, dec_in, obsrms)
for (a1, z1, h1, d1, r1, a2, z2, h2, d2, r2) in \
zip(az_control, ze_control, ha_control, d_control, r_control,
azTest, zeTest, haTest, dTest, rTest):
self.assertAlmostEqual(a1, a2, 12)
self.assertAlmostEqual(z1, z2, 12)
self.assertAlmostEqual(h1, h2, 12)
self.assertAlmostEqual(d1, d2, 12)
self.assertAlmostEqual(r1, r2, 12)
# test that an exception is raised if input arrays have
# different lengths
self.assertRaises(ValueError, pal.aopqkVector, ra_in[:6], dec_in, obsrms)
def test_aop(self):
dap = -0.1234
date = 51000.1
dut = 25.0
elongm = 2.1
phim = 0.5
hm = 3000.0
xp = -0.5e-6
yp = 1.0e-6
tdk = 280.0
pmb = 550.0
rh = 0.6
tlr = 0.006
aopres = [
[
1.812817787123283034,
1.393860816635714034,
-1.297808009092456683,
-0.122967060534561,
2.699270287872084
],
[
2.019928026670621442,
1.101316172427482466,
-0.9432923558497740862,
-0.1232144708194224,
2.344754634629428
],
[
2.019928026670621442,
1.101267532198003760,
-0.9432533138143315937,
-0.1231850665614878,
2.344715592593984
]
]
aoptol = [[10, 7, 7, 8, 7],
[10, 10, 10, 10, 10],
[10, 10, 10, 10, 10]]
for i in range(len(aopres)):
# Not very pythonic
if i == 0:
rap = 2.7
wl = 0.45
elif i == 1:
rap = 2.345
else:
wl = 1.0e6
result = pal.aop(rap, dap, date, dut, elongm, phim, hm, xp, yp,
tdk, pmb, rh, wl, tlr)
for j in range(len(result)):
self.assertAlmostEqual(result[j], aopres[i][j], aoptol[i][j])
date = 48000.3
wl = 0.45
aoprms = pal.aoppa(date, dut, elongm, phim, hm, xp, yp, tdk, pmb,
rh, wl, tlr)
aoppares = [0.4999993892136306, 0.4794250025886467, 0.8775828547167932,
1.363180872136126e-6, 3000., 280., 550., 0.6, 0.45, 0.006,
0.0001562803328459898, -1.792293660141e-7, 2.101874231495843,
7.601916802079765]
aoppatol = [13, 13, 13, 13, 10, 11, 11, 13, 13, 15, 13, 13, 12, 8]
self.assertEqual(len(aoprms), len(aoppares))
for i in range(len(aoprms)):
self.assertAlmostEqual(aoprms[i], aoppares[i], aoppatol[i])
(rap, dap) = pal.oap("r", 1.6, -1.01, date, dut, elongm, phim,
hm, xp, yp, tdk, pmb, rh, wl, tlr)
self.assertAlmostEqual(rap, 1.601197569844787, 10)
self.assertAlmostEqual(dap, -1.012528566544262, 10)
(rap, dap) = pal.oap("h", -1.234, 2.34, date, dut, elongm, phim,
hm, xp, yp, tdk, pmb, rh, wl, tlr)
self.assertAlmostEqual(rap, 5.693087688154886463, 10)
self.assertAlmostEqual(dap, 0.8010281167405444, 10)
(rap, dap) = pal.oap("a", 6.1, 1.1, date, dut, elongm, phim,
hm, xp, yp, tdk, pmb, rh, wl, tlr)
self.assertAlmostEqual(rap, 5.894305175192448940, 10)
self.assertAlmostEqual(dap, 1.406150707974922, 10)
(rap, dap) = pal.oapqk("r", 2.1, -0.345, aoprms)
self.assertAlmostEqual(rap, 2.10023962776202, 10)
self.assertAlmostEqual(dap, -0.3452428692888919, 10)
(rap, dap) = pal.oapqk("h", -0.01, 1.03, aoprms)
self.assertAlmostEqual(rap, 1.328731933634564995, 10)
self.assertAlmostEqual(dap, 1.030091538647746, 10)
(rap, dap) = pal.oapqk("a", 4.321, 0.987, aoprms)
self.assertAlmostEqual(rap, 0.4375507112075065923, 10)
self.assertAlmostEqual(dap, -0.01520898480744436, 10)
aoprms = pal.aoppat(date + pal.DS2R, aoprms)
self.assertAlmostEqual(aoprms[13], 7.602374979243502, 8)
def test_oapqk_vector(self):
"""
Test that oapqkVector gives results consistent with oapqk
"""
date = 51000.1
dut = 25.0
elongm = 2.1
phim = 0.5
hm = 3000.0
xp = -0.5e-6
yp = 1.0e-6
tdk = 280.0
pmb = 550.0
rh = 0.6
tlr = 0.006
wl = 0.45
aoprms = pal.aoppa(date, dut, elongm, phim, hm, xp, yp, tdk, pmb,
rh, wl, tlr)
np.random.seed(133)
n_samples = 200
ob1 = np.random.random_sample(n_samples) * 2.0 * np.pi
ob2 = np.random.random_sample(n_samples) * 0.5 * np.pi
# restrict ob2 to 0 < ob2 < pi/2 because we will also be testing az-zenith distance
# coordinate pairs
for typeFlag in ['r', 'a', 'h']:
testRa, testDec = pal.oapqkVector(typeFlag, ob1, ob2, aoprms)
for ii in range(n_samples):
controlRa, controlDec = pal.oapqk(typeFlag, ob1[ii], ob2[ii], aoprms)
self.assertEqual(testRa[ii], controlRa)
self.assertEqual(testDec[ii], controlDec)
# verify that pal.aopqkVector and pal.oapqkVector invert each other;
# we limit our test raApparent, decApparent values to be within 75 degrees
# of zenith, because of the limited accuracy of these rout_ines at
# large zenith distance (even though we approximate the sky as a flat
# surface and demand that all of the sample points be within 50 degrees
# of zenith in this gross approximation, heuristically, this causes
# the actual maximum zenith distance to be 74.1 degrees).
raApCenter, decApCenter = pal.oapqk('h', 0.0, 0.0, aoprms)
rr = np.random.random_sample(n_samples)*np.radians(50.0)
theta = np.random.random_sample(n_samples)*2.0*np.pi
ra_ap_list = raApCenter + rr*np.cos(theta)
dec_ap_list = decApCenter + rr*np.sin(theta)
azList, zdList, haList, \
decObList, raObList = pal.aopqkVector(ra_ap_list, dec_ap_list, aoprms)
testRa, testDec = pal.oapqkVector('r', raObList, decObList, aoprms)
np.testing.assert_array_almost_equal(testRa, ra_ap_list, 12)
np.testing.assert_array_almost_equal(testDec, dec_ap_list, 12)
testRa, testDec = pal.oapqkVector('h', haList, decObList, aoprms)
np.testing.assert_array_almost_equal(testRa, ra_ap_list, 12)
np.testing.assert_array_almost_equal(testDec, dec_ap_list, 12)
testRa, testDec = pal.oapqkVector('a', azList, zdList, aoprms)
np.testing.assert_array_almost_equal(testRa, ra_ap_list, 12)
np.testing.assert_array_almost_equal(testDec, dec_ap_list, 12)
# test that an exception is thrown if the input arrays do not
# have the same length
self.assertRaises(ValueError, pal.oapqkVector, 'a',
azList, zdList[:17], aoprms)
def test_bear(self):
a1 = 1.234
b1 = -0.123
a2 = 2.345
b2 = 0.789
self.assertAlmostEqual(pal.dbear(a1, b1, a2, b2),
0.7045970341781791, 12)
d1 = pal.dcs2c(a1, b1)
d2 = pal.dcs2c(a2, b2)
self.assertAlmostEqual(pal.dpav(d1, d2), 0.7045970341781791, 12)
def test_dbear_vector(self):
"""
Test that dbearVector gives the same
results as dbear
"""
np.random.seed(122)
n_samples = 100
a1_in = np.random.random_sample(n_samples)*2.0*np.pi
b1_in = (np.random.random_sample(n_samples)-0.5)*np.pi
a2_in = np.random.random_sample(n_samples)*2.0*np.pi
b2_in = (np.random.random_sample(n_samples)-0.5)*np.pi
# test case where a2, b2 have the same number of elements
# as a1, b1
b_test = pal.dbearVector(a1_in, b1_in, a2_in, b2_in)
for i in range(len(a1_in)):
b_control = pal.dbear(a1_in[i], b1_in[i], a2_in[i], b2_in[i])
self.assertEqual(b_test[i], b_control)
# test case where a2, b2 have only one element
b_test = pal.dbearVector(a1_in, b1_in, a2_in[7:8], b2_in[7:8])
for i in range(len(a1_in)):
b_control = pal.dbear(a1_in[i], b1_in[i], a2_in[7], b2_in[7])
self.assertEqual(b_test[i], b_control)
# test that exceptions are raied when the numpy arrays are of
# incorrect size
self.assertRaises(ValueError, pal.dbearVector, a1_in, b1_in[:9], a2_in, b2_in)
self.assertRaises(ValueError, pal.dbearVector, a1_in, b1_in, a2_in, b2_in[:8])
self.assertRaises(ValueError, pal.dbearVector, a1_in, b1_in, a2_in[:9], b2_in[:9])
def test_dpav_vector(self):
"""
Test that dpavVector is consistent with dpav
"""
np.random.seed(127)
n_samples = 200
phi = np.random.random_sample(n_samples)*2.0*np.pi
theta = (np.random.random_sample(n_samples)-0.5)*np.pi
v1 = np.array([[np.cos(th), np.sin(th) * np.cos(ph), np.sin(th) * np.sin(ph)]
for th, ph in zip(theta, phi)])
phi = np.random.random_sample(n_samples)*2.0*np.pi
theta = (np.random.random_sample(n_samples)-0.5)*np.pi
v2 = np.array([[np.cos(th), np.sin(th) * np.cos(ph), np.sin(th) * np.sin(ph)]
for th, ph in zip(theta, phi)])
test_pa = pal.dpavVector(v1, v2)
for ii in range(n_samples):
pa_control = pal.dpav(v1[ii], v2[ii])
self.assertEqual(pa_control, test_pa[ii])
self.assertFalse(np.isnan(pa_control))
# test the case where we only feed one point in as v2
test_pa = pal.dpavVector(v1, v2[4])
for ii in range(n_samples):
pa_control = pal.dpav(v1[ii], v2[4])
self.assertEqual(pa_control, test_pa[ii])
self.assertFalse(np.isnan(pa_control))
# test that exceptions are raised when they should be
self.assertRaises(ValueError, pal.dpavVector, v1, v2[:19])
v3 = np.random.random_sample((n_samples, 6))
self.assertRaises(ValueError, pal.dpavVector, v3, v2)
self.assertRaises(ValueError, pal.dpavVector, v1, v3)
self.assertRaises(ValueError, pal.dpavVector, v1,
np.random.random_sample(9))
def test_caldj(self):
djm = pal.caldj(1999, 12, 31)
self.assertEqual(djm, 51543)
self.assertRaises(ValueError, pal.caldj, -5000, 12, 31)
self.assertRaises(ValueError, pal.caldj, 1970, 13, 1)
self.assertRaises(ValueError, pal.caldj, 1970, 1, 32)
def test_caldj_vector(self):
"""
Test that caldjVector gives results consistent with cadj
"""
np.random.seed(143)
n_samples = 200
iy = np.random.randint(1000, 10001, n_samples)
im = np.random.randint(1, 12, n_samples)
iday = np.random.randint(1, 21, n_samples)
test_mjd = pal.caldjVector(iy, im, iday)
for ii in range(n_samples):
control_mjd = pal.caldj(iy[ii], im[ii], iday[ii])
self.assertEqual(control_mjd, test_mjd[ii])
# test that caldjVector and djcalVector invert each other
iyTest, imTest, idTest, fracTest = pal.djcalVector(5, test_mjd)
np.testing.assert_array_equal(iyTest, iy)
np.testing.assert_array_equal(imTest, im)
np.testing.assert_array_equal(idTest, iday)
# test that bad values are handled properly
iy[5] = -4800
im[9] = 13
im[11] = 0
iday[17] = 33
test_mjd = pal.caldjVector(iy, im, iday)
for ii in range(n_samples):
if ii in (5, 9, 11, 17):
self.assertTrue(np.isnan(test_mjd[ii]))
self.assertRaises(ValueError, pal.caldj, iy[ii], im[ii],
iday[ii])
else:
control_mjd = pal.caldj(iy[ii], im[ii], iday[ii])
self.assertEqual(control_mjd, test_mjd[ii])
# test that exceptions are raised when the input arrays are
# of different lengths
self.assertRaises(ValueError, pal.caldjVector, iy, im[:11], iday)
self.assertRaises(ValueError, pal.caldjVector, iy, im, iday[:8])
def test_daf2r(self):
dr = pal.daf2r(76, 54, 32.1)
self.assertAlmostEqual(dr, 1.342313819975276, 12)
def test_daf2r_vector(self):
"""
Test that daf2rVector returns the same results as daf2r
"""
np.random.seed(123)
n_samples = 100
deg = np.random.randint(0, 360, n_samples)
i_min = np.random.randint(0, 60, n_samples)
asec = np.random.random_sample(n_samples)*60.0
radian_test = pal.daf2rVector(deg, i_min, asec)
for ii in range(len(deg)):
radian_control = pal.daf2r(deg[ii], i_min[ii], asec[ii])
self.assertEqual(radian_control, radian_test[ii])
# test that bad values get set to np.NaN
deg[9] = 360
i_min[17] = 60
asec[21] = 60.0
radian_test = pal.daf2rVector(deg, i_min, asec)
for ii, rad in enumerate(radian_test):
if ii in (9, 17, 21):
self.assertTrue(np.isnan(rad))
else:
self.assertFalse(np.isnan(rad))
def test_cc2s(self):
(da, db) = pal.dcc2s(np.array([100., -50., 25.]))
self.assertAlmostEqual(da, -0.4636476090008061, 12)
self.assertAlmostEqual(db, 0.2199879773954594, 12)
def test_dcc2s_vector(self):
"""
Test that dcc2sVector returns the same results as dcc2s
"""
np.random.seed(124)
input_data = np.random.random_sample((20, 3)) * 10.0
aTest, b_test = pal.dcc2sVector(input_data)
for ix in range(input_data.shape[0]):
aControl, b_control = pal.dcc2s(input_data[ix])
self.assertEqual(aControl, aTest[ix])
self.assertEqual(b_control, b_test[ix])
# test that an exception is raised if you don't pass in
# 3-D cartesian points
dummy_data = np.random.random_sample((20, 5))*10.0
self.assertRaises(ValueError, pal.dcc2sVector, dummy_data)
def test_dcs2c_vector(self):
"""
Test that dcs2cVector returns the same results as dcs2c
"""
np.random.seed(125)
n_samples = 100
ra = np.random.random_sample(n_samples)*2.0*np.pi
dec = (np.random.random_sample(n_samples)-0.5)*np.pi
v_test = pal.dcs2cVector(ra, dec)
for ii in range(n_samples):
v_control = pal.dcs2c(ra[ii], dec[ii])
np.testing.assert_array_equal(v_control, v_test[ii])
# test that dcs2cVector and dcc2sVector do, in fact, invert one another
aTest, b_test = pal.dcc2sVector(v_test)
np.testing.assert_array_almost_equal(np.cos(ra), np.cos(aTest), 12)
np.testing.assert_array_almost_equal(np.sin(ra), np.sin(aTest), 12)
np.testing.assert_array_almost_equal(np.cos(dec), np.cos(b_test), 12)
np.testing.assert_array_almost_equal(np.sin(dec), np.sin(b_test), 12)
# test that an exception is raised if you pass in arrays of different lengths
self.assertRaises(ValueError, pal.dcs2cVector, ra, dec[:16])
def test_cd2tf(self):
(sign, hours, minutes, seconds, fraction) = pal.dd2tf(4, -0.987654321)
self.assertEqual(sign, "-")
self.assertEqual(hours, 23)
self.assertEqual(minutes, 42)
self.assertEqual(seconds, 13)
self.assertEqual(fraction, 3333)
def test_dd2tf_vector(self):
"""
Test that dd2tfVector gives the same results as dd2tf
"""
np.random.seed(126)
n_samples = 100
days_list = (np.random.sample(n_samples)-0.5)*1200.0
for ndp in [2, 3, 4, 5]:
testSign, testIh, testIm, testIs, testFrac = pal.dd2tfVector(ndp, days_list)
for ix, days in enumerate(days_list):
controlSign, controlIh,\
controlIm, controlIs,\
controlFrac = pal.dd2tf(ndp, days)
self.assertEqual(controlSign, testSign[ix])
self.assertEqual(controlIm, testIm[ix])
self.assertEqual(controlIs, testIs[ix])
self.assertEqual(controlFrac, testFrac[ix])
def test_cldj(self):
d = pal.cldj(1899, 12, 31)
self.assertEqual(d, 15019)
self.assertRaises(ValueError, pal.cldj, -5000, 12, 31)
self.assertRaises(ValueError, pal.cldj, 1970, 13, 1)
self.assertRaises(ValueError, pal.cldj, 1970, 1, 32)
def test_dr2af(self):
(sign, deg, min, sec, f) = pal.dr2af(4, 2.345)
self.assertEqual(sign, "+")
self.assertEqual(deg, 134)
self.assertEqual(min, 21)
self.assertEqual(sec, 30)
self.assertEqual(f, 9706)
def test_dr2af_vector(self):
"""
Test that dr2afVector produces the same results as
dr2af
"""
np.random.seed(128)
n_samples = 200
angle_list = (np.random.random_sample(n_samples)-0.5)*4.0*np.pi
for npd in [2, 3, 4, 5]:
testSign, testDeg, \
testMin, testSec, testFrac = pal.dr2afVector(npd, angle_list)
for ii in range(n_samples):
controlSign, controlDeg, \
controlMin, controlSec, controlFrac = pal.dr2af(npd, angle_list[ii])
self.assertEqual(controlSign, testSign[ii])
self.assertEqual(controlDeg, testDeg[ii])
self.assertEqual(controlMin, testMin[ii])
self.assertEqual(controlSec, testSec[ii])
self.assertEqual(controlFrac, testFrac[ii])
def test_dr2tf(self):
(sign, hr, min, sec, f) = pal.dr2tf(4, -3.01234)
self.assertEqual(sign, "-")
self.assertEqual(hr, 11)
self.assertEqual(min, 30)
self.assertEqual(sec, 22)
self.assertEqual(f, 6484)
def test_dr2tf_vector(self):
"""
Test that dr2tfVector produces the same results as
dr2tf
"""
np.random.seed(128)
n_samples = 200
angle_list = (np.random.random_sample(n_samples)-0.5)*4.0*np.pi
for npd in [2, 3, 4, 5]:
testSign, testHr, \
testMin, testSec, testFrac = pal.dr2tfVector(npd, angle_list)
for ii in range(n_samples):
controlSign, controlHr, \
controlMin, controlSec, controlFrac = pal.dr2tf(npd, angle_list[ii])
self.assertEqual(controlSign, testSign[ii])
self.assertEqual(controlHr, testHr[ii])
self.assertEqual(controlMin, testMin[ii])
self.assertEqual(controlSec, testSec[ii])
self.assertEqual(controlFrac, testFrac[ii])
def test_dtf2d(self):
dd = pal.dtf2d(23, 56, 59.1)
self.assertAlmostEqual(dd, 0.99790625, 12)
self.assertRaises(ValueError, pal.dtf2d, 24, 1, 32)
self.assertRaises(ValueError, pal.dtf2d, 23, -1, 32)
self.assertRaises(ValueError, pal.dtf2d, 23, 1, 60)
def test_dtf2d_vector(self):
"""
Test that dtf2dVector gives results consistent with
dtf2d
"""
np.random.seed(131)
n_samples = 100
i_hour = np.random.randint(0, 24, n_samples)
i_min = np.random.randint(0, 60, n_samples)
sec = np.random.random_sample(n_samples)*60.0
test_days = pal.dtf2dVector(i_hour, i_min, sec)
for ii in range(n_samples):
control_days = pal.dtf2d(i_hour[ii], i_min[ii], sec[ii])
self.assertEqual(test_days[ii], control_days)
self.assertFalse(np.isnan(test_days[ii]))
# test that NaN's are produced when bad inputs are given
i_hour[5] = 24
i_min[7] = 60
sec[19] = 60.001
test_days = pal.dtf2dVector(i_hour, i_min, sec)
for ii in range(n_samples):
if ii not in (5, 7, 19):
control_days = pal.dtf2d(i_hour[ii], i_min[ii], sec[ii])
self.assertEqual(test_days[ii], control_days)
self.assertFalse(np.isnan(test_days[ii]))
else:
self.assertTrue(np.isnan(test_days[ii]))
# test that exceptions are raised when you pass in
# mis-matched input arrays
self.assertRaises(ValueError, pal.dtf2dVector, i_hour, i_min[:19], sec)
self.assertRaises(ValueError, pal.dtf2dVector, i_hour, i_min, sec[:19])
def test_dtf2r(self):
dr = pal.dtf2r(23, 56, 59.1)
self.assertAlmostEqual(dr, 6.270029887942679, 12)
self.assertRaises(ValueError, pal.dtf2r, 24, 1, 32)
self.assertRaises(ValueError, pal.dtf2r, 23, -1, 32)
self.assertRaises(ValueError, pal.dtf2r, 23, 1, 60)
def test_dtf2r_vector(self):
"""
Test that dtf2rVector gives results consistent with
dtf2r
"""
np.random.seed(131)
n_samples = 100
i_hour = np.random.randint(0, 24, n_samples)
i_min = np.random.randint(0, 60, n_samples)
sec = np.random.random_sample(n_samples)*60.0
test_rad = pal.dtf2rVector(i_hour, i_min, sec)
for ii in range(n_samples):
control_rad = pal.dtf2r(i_hour[ii], i_min[ii], sec[ii])
self.assertEqual(test_rad[ii], control_rad)
self.assertFalse(np.isnan(test_rad[ii]))
# test that NaN's are produced when bad inputs are given
i_hour[5] = 24
i_min[7] = 60
sec[19] = 60.001
test_rad = pal.dtf2rVector(i_hour, i_min, sec)
for ii in range(n_samples):
if ii not in (5, 7, 19):
control_rad = pal.dtf2r(i_hour[ii], i_min[ii], sec[ii])
self.assertEqual(test_rad[ii], control_rad)
self.assertFalse(np.isnan(test_rad[ii]))
else:
self.assertTrue(np.isnan(test_rad[ii]))
# test that exceptions are raised when you pass in
# mis-matched input arrays
self.assertRaises(ValueError, pal.dtf2rVector, i_hour, i_min[:19], sec)
self.assertRaises(ValueError, pal.dtf2rVector, i_hour, i_min, sec[:19])
def test_dat(self):
self.assertEqual(pal.dat(43900), 18)
self.assertAlmostEqual(pal.dtt(40404), 39.709746, 12)
self.assertAlmostEqual(pal.dt(500), 4686.7, 10)
self.assertAlmostEqual(pal.dt(1400), 408, 11)
self.assertAlmostEqual(pal.dt(1950), 27.99145626)
def test_djcal(self):
djm = 50123.9999
(y, m, d, f) = pal.djcal(4, djm)
self.assertEqual(y, 1996)
self.assertEqual(m, 2)
self.assertEqual(d, 10)
self.assertEqual(f, 9999)
(y, m, d, f) = pal.djcl(djm)
self.assertEqual(y, 1996)
self.assertEqual(m, 2)
self.assertEqual(d, 10)
self.assertAlmostEqual(f, 0.9999, 7)
def test_djcal_vector(self):
"""
Test that djcalVector returns results consistent with djcal
"""
np.random.seed(142)
n_samples = 200
mjd = (np.random.random_sample(n_samples)-0.5)*100000.0
for ndp in [2, 3, 4, 5]:
testY, testM, \
testD, testFrac = pal.djcalVector(ndp, mjd)
for ii in range(n_samples):
controlY, controlM, \
controlD, controlFrac = pal.djcal(ndp, mjd[ii])
self.assertEqual(controlY, testY[ii])
self.assertEqual(controlM, testM[ii])
self.assertEqual(controlD, testD[ii])
self.assertEqual(controlFrac, testFrac[ii])
ndp = 4
# test that unacceptable dates result in -1 being placed in all
# of the output slots
mjd[4] = -2468570
mjd[5] = -2468571
mjd[9] = 1.0e8
mjd[10] = 1.0e9
testY, testM, testD, testFrac = pal.djcalVector(ndp, mjd)
for ii in range(n_samples):
if ii in (5, 10):
self.assertEqual(testY[ii], -1)
self.assertEqual(testM[ii], -1)
self.assertEqual(testD[ii], -1)
self.assertEqual(testFrac[ii], -1)
self.assertRaises(ValueError, pal.djcal, ndp, mjd[ii])
else:
controlY, controlM, \
controlD, controlFrac = pal.djcal(ndp, mjd[ii])
self.assertEqual(controlY, testY[ii])
self.assertEqual(controlM, testM[ii])
self.assertEqual(controlD, testD[ii])
self.assertEqual(controlFrac, testFrac[ii])
def test_dmat(self):
da = np.array(
[[2.22, 1.6578, 1.380522],
[1.6578, 1.380522, 1.22548578],
[1.380522, 1.22548578, 1.1356276122]])
dv = np.array([2.28625, 1.7128825, 1.429432225])
(da, dv, dd) = pal.dmat(da, dv)
self.assertAlmostEqual(dd, 0.003658344147359863, 12)
self.assertAlmostEqual(dv[0], 1.002346480763383, 12)
self.assertAlmostEqual(dv[1], 0.03285594016974583489, 12)
self.assertAlmostEqual(dv[2], 0.004760688414885247309, 12)
da = np.array([[0., 1.], [0., 1.]])
dv = np.array([1., 1.])
self.assertRaises(ArithmeticError, pal.dmat, da, dv)
def test_e2h(self):
dp = -0.7
hin = -0.3
din = -1.1
(da, de) = pal.de2h(hin, din, dp)
self.assertAlmostEqual(da, 2.820087515852369, 12)
self.assertAlmostEqual(de, 1.132711866443304, 12)
(dh, dd) = pal.dh2e(da, de, dp)
self.assertAlmostEqual(dh, hin, 12)
self.assertAlmostEqual(dd, din, 12)
def test_de2h_vector(self):
n_tests = 100
phi = 0.35
np.random.seed(32)
ha_in = np.random.random_sample(n_tests)*np.pi*2.0
dec_in = (np.random.random_sample(n_tests)-0.5)*np.pi
az_control = None
el_control = None
for (ha, dd) in zip(ha_in, dec_in):
az, el = pal.de2h(ha, dd, phi)
if az_control is None:
az_control = np.array([az])
el_control = np.array([el])
else:
az_control = np.append(az_control, az)
el_control = np.append(el_control, el)
azTest, elTest = pal.de2hVector(ha_in, dec_in, phi)
for (a1, e1, a2, e2) in zip(az_control, el_control, azTest, elTest):
self.assertAlmostEqual(a1, a2, 12)
self.assertAlmostEqual(e1, e2, 12)
# test that an exception is raised if inputs are not
# of the same length
self.assertRaises(ValueError, pal.de2hVector, ha_in[:10], dec_in, phi)
def test_dh2e_vector(self):
"""
Test that dh2eVector gives results consistent with dh2e
"""
np.random.seed(142)
n_samples = 200
phi = 1.432
az = np.random.random_sample(n_samples)*2.0*np.pi
el = (np.random.random_sample(n_samples)-0.5)*np.pi
testHa, testDec = pal.dh2eVector(az, el, phi)
for ii in range(n_samples):
controlHa, controlDec = pal.dh2e(az[ii], el[ii], phi)
self.assertEqual(controlHa, testHa[ii])
self.assertEqual(controlDec, testDec[ii])
self.assertFalse(np.isnan(testHa[ii]))
self.assertFalse(np.isnan(testDec[ii]))
# test that dh2eVector and de2hVector invert each other
testAz, testEl = pal.de2hVector(testHa, testDec, phi)
pal.DR2ASs = 3600.0*np.degrees(1.0)
distance = pal.DR2ASs*pal.dsepVector(testAz, testEl, az, el)
np.testing.assert_array_almost_equal(distance,
np.zeros(n_samples), 9)
# test that an exception is raised when the input arrays
# are of different lengths
self.assertRaises(ValueError, pal.dh2eVector, az[:40], el, phi)
def test_ecleq(self):
(dr, dd) = pal.ecleq(1.234, -0.123, 43210.0)
self.assertAlmostEqual(dr, 1.229910118208851, 5)
self.assertAlmostEqual(dd, 0.2638461400411088, 5)
def test_ecleq_vector(self):
"""
Test that ecleqVector produces results consistent with
ecleq
"""
mjd = 58734.2
np.random.seed(138)
n_samples = 200
dl = np.random.random_sample(n_samples)*2.0*np.pi
db = (np.random.random_sample(n_samples)-0.5)*np.pi
testRa, testDec = pal.ecleqVector(dl, db, mjd)
for ii in range(n_samples):
controlRa, controlDec = pal.ecleq(dl[ii], db[ii], mjd)
self.assertEqual(controlRa, testRa[ii])
self.assertEqual(controlDec, testDec[ii])
# test that ecleqVector and eqeclVector invert
# one another
testDl, testDb = pal.eqeclVector(testRa, testDec, mjd)
pal.DR2AS = 3600.0*np.degrees(1.0)
distance = pal.DR2AS*pal.dsepVector(testDl, testDb, dl, db)
np.testing.assert_array_almost_equal(distance,
np.zeros(len(distance)), 4)
# test that an exception is raised if input arrays are
# of different lenghts
self.assertRaises(ValueError, pal.ecleqVector, dl[:4], db, mjd)
def test_ecmat(self):
expected = np.array([
[1.0, 0.0, 0.0],
[0.0, 0.91749307789883549624, 0.3977517467060596168],
[0.0, -0.3977517467060596168, 0.91749307789883549624]])
rmat = pal.ecmat(55966.46)
np.testing.assert_array_almost_equal(rmat, expected, decimal=12)
def test_epb(self):
self.assertAlmostEqual(pal.epb(45123), 1982.419793168669, 8)
def test_epb2d(self):
self.assertAlmostEqual(pal.epb2d(1975.5), 42595.5995279655, 7)