-
Notifications
You must be signed in to change notification settings - Fork 6
/
pal.pyx.in
3753 lines (3018 loc) · 99.5 KB
/
pal.pyx.in
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
# Copyright (C) 2012 Tim Jenness and Science and Technology
# Facilities Council.
# Copyright (C) 2014 Tim Jenness
# 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/>.
#cython: language_level=3
#distutils: language=c
cimport cpal
from libc.stdlib cimport malloc, free
cimport numpy as np
import numpy as np
D2PI = cpal.PAL__D2PI
DAS2R = cpal.PAL__DAS2R
DD2R = cpal.PAL__DD2R
DH2R = cpal.PAL__DH2R
DPI = cpal.PAL__DPI
DPIBY2 = cpal.PAL__DPIBY2
DR2AS = cpal.PAL__DR2AS
DR2D = cpal.PAL__DR2D
DR2H = cpal.PAL__DR2H
DR2S = cpal.PAL__DR2S
DS2R = cpal.PAL__DS2R
__version__ = @PALPY_VERSION@
def palvers():
"""
versiondict = palvers()
Returns the version information from the underlying PAL C
library used to build palpy. Version information is returned in
a dict with keys:
verstring : Version in string form, e.g "1.6.2"
major : Major version number as integer, e.g. 1
minor : Minor version number as integer, e.g. 6
patchlevel : Patch level as integer, e.g. 2
"""
return dict( { "verstring": @VERSTRING@,
"major": @MAJOR_VERSION@,
"minor": @MINOR_VERSION@,
"patchlevel": @PATCH_VERSION@ } )
def addet( double rm, double dm, double eq ):
"""
(rc, dc) = addet( rm, dm, eq)
@palAddet@
"""
cdef double rc
cdef double dc
cpal.palAddet( rm, dm, eq, &rc, &dc )
return ( rc, dc )
def airmas( double zd ):
"""
airmass = airmas( zd )
@palAirmas@
"""
return cpal.palAirmas( zd )
def airmasVector(np.ndarray[double, ndim=1] zd not None):
"""
airmass = airmasVector( zd )
where zd and airmass are numpy arrays
@palAirmas@
"""
cdef int length = len(zd)
cdef np.ndarray airmass_out = np.empty(length, dtype=np.float64)
for ii in range(length):
airmass_out[ii] = cpal.palAirmas(zd[ii])
return airmass_out
def altaz(double ha, double dec, double phi):
"""
(az, azd, azdd, el, eld, eldd, pa, pad, padd) = altaz( ha, dec, phi )
@palAltaz@
"""
cdef double az
cdef double azd
cdef double azdd
cdef double el
cdef double eld
cdef double eldd
cdef double pa
cdef double pad
cdef double padd
cpal.palAltaz (ha, dec, phi, &az, &azd, &azdd, &el, &eld, &eldd, &pa, &pad, &padd )
return (az, azd, azdd, el, eld, eldd, pa, pad, padd )
def altazVector(np.ndarray[double, ndim=1] ha not None,
np.ndarray[double, ndim=1] dec not None,
double phi):
"""
(az, azd, azdd, el, eld, eldd, pa, pad, padd) = altazVector( ha, dec, phi )
where ha, dec and all outputs are numpy arrays
@palAltaz@
Raises
------
ValueError if input numpy arrays are not of the same length
"""
cdef int length = len(ha)
if len(dec)!=length:
raise ValueError("You did not pass as many Decs as " \
+ "Hour Angles to altazVector")
cdef np.ndarray azout = np.zeros(length, dtype=np.float64)
cdef double az
cdef np.ndarray azdout = np.zeros(length, dtype=np.float64)
cdef double azd
cdef np.ndarray azddout = np.zeros(length, dtype=np.float64)
cdef double azdd
cdef np.ndarray elout = np.zeros(length, dtype=np.float64)
cdef double el
cdef np.ndarray eldout = np.zeros(length, dtype=np.float64)
cdef double eld
cdef np.ndarray elddout = np.zeros(length, dtype=np.float64)
cdef double eldd
cdef np.ndarray paout = np.zeros(length, dtype=np.float64)
cdef double pa
cdef np.ndarray padout = np.zeros(length, dtype=np.float64)
cdef double pad
cdef np.ndarray paddout = np.zeros(length, dtype=np.float64)
cdef double padd
cdef int i
for i in range(length):
cpal.palAltaz(ha[i], dec[i], phi, &az, &azd, &azdd, &el, &eld, &eldd, &pa, &pad, &padd)
azout[i] = az
azdout[i] = azd
azddout[i] = azdd
elout[i] = el
eldout[i] = eld
elddout[i] = eldd
paout[i] = pa
padout[i] = pad
paddout[i] = padd
return (azout, azdout, azddout, elout, eldout, elddout, paout, padout, paddout)
def amp( double ra, double da, double date, double eq):
"""
(rm, dm) = amp( ra, da, date, eq )
@palAmp@
"""
cdef double rm
cdef double dm
cpal.palAmp( ra, da, date, eq, &rm, &dm )
return ( rm, dm )
def ampqk( double ra, double da, np.ndarray[double, ndim=1] amprms not None ):
"""
(rm, dm) = ampqk( ra, da, amprms )
@palAmpqk@
"""
cdef double rm
cdef double dm
cdef double camprms[21]
for i in range(21):
camprms[i] = amprms[i]
cpal.palAmpqk( ra, da, camprms, &rm, &dm )
return (rm, dm)
def ampqkVector(np.ndarray[double, ndim=1] ra not None,
np.ndarray[double, ndim=1] dec not None,
np.ndarray[double, ndim=1] amprms not None):
"""
(rm, dm) = ampqkVector( ra, dec, amprms )
where ra, dec, rm, and dm are all numpy arrays
@palAmpqk@
Raises
------
ValueError if input arrays of RA and Dec are not of the same
length
"""
cdef int length = len(ra)
if len(dec)!=length:
raise ValueError("You did not pass as many Decs as RAs to " \
+ "ampqkVector")
cdef np.ndarray rm_out = np.empty(length, dtype=np.float64)
cdef np.ndarray dm_out = np.empty(length, dtype=np.float64)
cdef double rm
cdef double dm
cdef double camprms[21]
for ii in range(21):
camprms[ii] = amprms[ii]
for ii in range(length):
cpal.palAmpqk(ra[ii], dec[ii], camprms, &rm, &dm)
rm_out[ii] = rm
dm_out[ii] = dm
return (rm_out, dm_out)
def aop( double rap, double dap, double date, double dut,
double elongm, double phim, double hm, double xp,
double yp, double tdk, double pmb, double rh,
double wl, double tlr ):
"""
(aob, zob, hob, dob, rob) = aop( rap, dap, date, dut,
elongm, phim, hm, xp,
yp, tdk, pmb, rh,
wl, tlr )
@palAop@
"""
cdef double aob
cdef double zob
cdef double hob
cdef double dob
cdef double rob
cpal.palAop( rap, dap, date, dut, elongm, phim, hm,
xp, yp, tdk, pmb, rh, wl, tlr,
&aob, &zob, &hob, &dob, &rob)
return (aob, zob, hob, dob, rob )
def aoppa(double date, double dut, double elongm, double phim,
double hm, double xp, double yp, double tdk, double pmb,
double rh, double wl, double tlr ):
"""
aoprms = aoppa( date, dut, elongm, phim, hm, xp, yp,
tdk, pmb, rh, wl, tlr )
@palAoppa@
"""
cdef double caoprms[14]
cdef np.ndarray aoprms = np.zeros( [14], dtype=np.float64 )
cpal.palAoppa( date, dut, elongm, phim, hm, xp, yp, tdk, pmb,
rh, wl, tlr, caoprms )
for i in range(14):
aoprms[i] = caoprms[i]
return aoprms
def aoppat( double date, np.ndarray[double, ndim=1] aoprms not None ):
"""
aoprms_updated = aoppat( date, aoprms )
@palAoppat@
"""
# We can either copy the array or modify in place.
# For now we return a new copy
cdef double caoprms[14]
for i in range(14):
caoprms[i] = aoprms[i]
cpal.palAoppat( date, caoprms )
cdef np.ndarray result = np.zeros( [14], dtype=np.float64 )
for i in range(14):
result[i] = caoprms[i]
return result
def aopqk(double rap, double dap, np.ndarray[double, ndim=1] aoprms not None):
"""
(aob, zob, hob, dob, rob) = aopqk( rap, dap, aoprms )
@palAopqk@
"""
cdef double aob
cdef double zob
cdef double hob
cdef double dob
cdef double rob
cdef double caoprms[14]
for i in range(14):
caoprms[i]=aoprms[i]
cpal.palAopqk(rap,dap,caoprms,&aob,&zob,&hob,&dob,&rob)
return (aob,zob,hob,dob,rob)
def aopqkVector(np.ndarray[double, ndim=1] rap not None,
np.ndarray[double, ndim=1] dap not None,
np.ndarray[double, ndim=1] aoprms not None):
"""
(aob, zob, hob, dob, rob) = aopqkVector( rap, dap, aoprms )
where rap, dap and all outputs are numpy arrays
@palAopqk@
Raises
------
ValueError if input arrays are not of the same length
"""
cdef int i
cdef int length=len(rap)
if len(dap)!=length:
raise ValueError("You did not pass the same number of Decs " \
+ "as RAs to aopqkVector")
cdef np.ndarray aobout = np.zeros(length,dtype=np.float64)
cdef np.ndarray zobout = np.zeros(length,dtype=np.float64)
cdef np.ndarray hobout = np.zeros(length,dtype=np.float64)
cdef np.ndarray dobout = np.zeros(length,dtype=np.float64)
cdef np.ndarray robout = np.zeros(length,dtype=np.float64)
cdef double aob
cdef double zob
cdef double hob
cdef double dob
cdef double rob
cdef double caoprms[14]
for i in range(14):
caoprms[i]=aoprms[i]
for i in range(length):
cpal.palAopqk(rap[i],dap[i],caoprms,&aob,&zob,&hob,&dob,&rob)
aobout[i]=aob
zobout[i]=zob
hobout[i]=hob
dobout[i]=dob
robout[i]=rob
return (aobout,zobout,hobout,dobout,robout)
def atmdsp(double tdk, double pmb, double rh, double wl1,
double a1, double b1, double wl2):
"""
(a2, b2) = atmdsp( tdk, pmb, rh, wl1, a1, b1, wl2 )
@palAtmdsp@
"""
cdef double a2
cdef double b2
cpal.palAtmdsp(tdk, pmb, rh, wl1, a1, b1, wl2, &a2, &b2)
return (a2, b2)
def caldj( int iy, int im, int id ):
"""
djm = caldj( iy, im, id )
@palCaldj@
Raises
------
ValueError if arguments are out of range.
"""
cdef double djm
cdef int j
cpal.palCaldj( iy, im, id, &djm, &j )
if j==0:
return djm
else:
bad = ""
if j==-1:
bad = "year"
elif j==-2:
bad = "month"
else:
bad = "day"
raise ValueError( "Julian date not computed. Bad {}".format(bad) )
def caldjVector(np.ndarray[long, ndim=1] iy not None,
np.ndarray[long, ndim=1] im not None,
np.ndarray[long, ndim=1] id not None):
"""
djm = caldjVector( iy, im, id )
where iy, im, id and djm are vectors
@palCaldj@
Raises
------
ValueError if input arrays have different lengths
Bad input dates result in numpy.NaN for djm.
Note: This algorithm does not work for dates before
-4799 January 1
Note: If you pass in a two-digit year, this algorithm will
interpret it as being between 1949 and 2049
"""
cdef int length = len(iy)
if len(im)!=length:
raise ValueError("You did not pass as many months as years " \
+ "to caldjVector")
if len(id)!=length:
raise ValueError("You did not pass as many days as years " \
+ "to caldjVector")
cdef np.ndarray mjd_out = np.empty(length, dtype=np.float64)
cdef double mjd
cdef int flag
for ii in range(length):
cpal.palCaldj(iy[ii], im[ii], id[ii], &mjd, &flag)
if flag == 0:
mjd_out[ii] = mjd
else:
mjd_out[ii] = np.nan
return mjd_out
def cldj( int iy, int im, int id ):
"""
djm = cldj( iy, im, id )
@palCldj@
Raises
------
ValueError if arguments are out of range.
"""
cdef int j
cdef double djm
cpal.palCldj( iy, im, id, &djm, &j )
if j==0:
return djm
else:
bad = ""
if j==-1:
bad = "year"
elif j==-2:
bad = "month"
else:
bad = "day"
raise ValueError( "Bad {} argument".format(bad) )
def dafin( string, int ipos ):
"""
Decode a sexagesimal string into an angle.
(angle, endpos) = pal.dafin( string, startpos )
where string is the string to be analyzed, startpos
is the position in the string to start looking
(starts at 1), endpos is the position in the string
after the search and angle is the decoded angle in
radians.
A ValueError exception is thrown if no angle can
be found.
A ValueError exception is thrown if an angle can
be located but is numerically out of range.
The interface for this routine is experimental. In
particular the startpos and endpos variables need
some thought. startpos could simply be ignored
as a python slice would work just as well. endpos
is useful in that you know where to slice the string
to continue searching for angles. Zero- versus one-
based indexing is an issue.
"""
byte_string = string.encode('ascii')
cdef char * cstring = byte_string
cdef int cipos = ipos
cdef double a
cdef int j
cpal.palDafin( cstring, &cipos, &a, &j )
if j==0:
return ( a, cipos )
elif j==1:
raise ValueError( "No angle could be located in string '{}'".format(string) )
else:
bad = "unknown"
if j==-1:
bad = "degrees"
elif j==-2:
bad = "arcminutes"
elif j==-3:
bad = "arcseconds"
raise ValueError( "Bad {} in input string".format(bad) )
def dat( double dju ):
"""
deltat = dat( dju )
@palDat@
"""
return cpal.palDat( dju )
def dav2m( np.ndarray[double, ndim=1] axvec not None ):
"""
rmat = dav2m( axvec )
@palDav2m@
"""
cdef double c_rmat[3][3]
cdef double c_axvec[3]
for i in range(3):
c_axvec[i] = axvec[i]
cpal.palDav2m( c_axvec, c_rmat )
cdef np.ndarray rmat = np.zeros([3,3], dtype=np.float64)
for i in range(3):
for j in range(3):
rmat[i][j] = c_rmat[i][j]
return rmat
def dbear( double a1, double b1, double a2, double b2 ):
"""
b = dbear( a1, b1, a2, b2 )
@palDbear@
"""
return cpal.palDbear( a1, b1, a2, b2 )
def dbearVector( np.ndarray[ double, ndim=1] a1 not None,
np.ndarray[ double, ndim=1] b1 not None,
np.ndarray[ double, ndim=1] a2 not None,
np.ndarray[ double, ndim=1] b2 not None):
"""
b_out = dbearVector( a1, b1, a2, b2)
where a1 and b1 are numpy arrays of longitude. a2 and b2 are
also numpy arrays. a2 and b2 can have either one element, or the
same number of elements as a1, b1. If a2 and b2 have only one element,
b_out will be a numpy array of the bearings from the points in a1, b1
to the single point in a2, b2. If a2 and b2 have the same number
of elements as a1 and b1, b_out will be a numpy array of the bearings
between each corresponding pair of points, i.e.
b_out[0] = bearing(a1[0], b1[0], a2[0], b2[0])
b_out[1] = bearing(a1[1], b1[1], a2[1], b2[1])
...
b_out[n] = bearing(a1[n], b1[n], a2[n], b2[n])
@palDbear@
Raises
------
Value error if input arrays have inconsistent lengths
"""
cdef int length1 = len(a1)
cdef int length2 = len(a2)
if len(b1) != length1:
raise ValueError("The first two arguments of dbearVector must " \
+ "be numpy arrays with the same number of elements")
if len(b2) != length2:
raise ValueError("The second two arguments of dbearVector must " \
+ "be numpy arrays with the same number of elements")
if length2!=1 and length2!=length1:
raise ValueError("The second two arguments of dbearVector must " \
+ "either have the same number of elements " \
+ "as the first two arguments, or they must have " \
+ "only one element")
cdef np.ndarray b_out = np.zeros(length1, dtype=np.float64)
if length1==length2:
for i in range(length1):
b_out[i] = cpal.palDbear(a1[i], b1[i], a2[i], b2[i])
else:
for i in range(length1):
b_out[i] = cpal.palDbear(a1[i], b1[i], a2[0], b2[0])
return b_out
def daf2r( int ideg, int iamin, double asec ):
"""
ang = daf2r( ideg, iamin, asec )
@palDaf2r@
Raises
------
ValueError if the input arguments are out of range.
"""
cdef double rad
cdef int j
cpal.palDaf2r( ideg, iamin, asec, &rad, &j )
if j==0:
return rad
else:
bad = ""
if j==1:
bad = "Degree argument outside range 0-359"
elif j==2:
bad = "Minute argument outside range 0-59"
else:
bad = "Arcsec argument outside range 0-59.9999..."
raise ValueError( bad )
def daf2rVector(np.ndarray[long, ndim=1] ideg_in not None,
np.ndarray[long, ndim=1] iamin_in not None,
np.ndarray[double, ndim=1] asec_in not None):
"""
ang = daf2rVector( ideg, iamin, asec)
where ideg, iamin, and asec are numpy arrays of degrees,
minutes, and seconds. ideg and iamin are arrays of ints. asec
is an array of doubles. ang is a numpy array of the same angles
converted to radians.
Bad values are mapped to numpy.NaNs in ang.
Bad values are defined as:
ideg > 359
iamin > 59
asec > 59.99999
@palDaf2r@
Raises
------
ValueError if input arrays have inconsistent lengths
"""
cdef int length = len(ideg_in)
if len(iamin_in)!=length or len(asec_in)!=length:
raise ValueError("You need to pass the same number of degrees as " \
+ "minutes and seconds to daf2rVector")
cdef np.ndarray ang_out = np.zeros(length, dtype=np.float64)
cdef double rad
cdef int flag
for i in range(length):
cpal.palDaf2r(ideg_in[i], iamin_in[i], asec_in[i], &rad, &flag)
if flag==0:
ang_out[i] = rad
else:
ang_out[i] = np.nan
return ang_out
def dcc2s( np.ndarray[double, ndim=1] v not None ):
"""
(a, b) = dcc2s( v )
@palDcc2s@
"""
cdef double cv[3]
for i in range(3):
cv[i] = v[i]
cdef double a
cdef double b
cpal.palDcc2s( cv, &a, &b )
return (a, b)
def dcc2sVector( np.ndarray[double, ndim=2] v_in not None):
"""
(a, b) = dcc2sVector( v )
where v is a 2-D numpy array in which each row is a different
point in Cartesian coordinates. a and b will be numpy arrays
containing the corresponding spherical coordinates
@palDcc2s@
Raises
------
ValueError if you do not pass in a 2-D numpy array with
three columns
"""
if v_in.shape[1] != 3:
raise ValueError("The input to dcc2sVector should be " \
+ "a 2-D numpy array in which each row " \
+ "is a point in 3-D Cartesian coordinates." \
+ " The rows of your input" \
+ " have %d dimensions" % v_in.shape[1])
cdef int length = v_in.shape[0]
cdef double cv[3]
cdef double a
cdef double b
cdef np.ndarray a_out = np.zeros(length, dtype=np.float64)
cdef np.ndarray b_out = np.zeros(length, dtype=np.float64)
for row in range(length):
for i in range(3):
cv[i] = v_in[row][i]
cpal.palDcc2s( cv, &a, &b)
a_out[row] = a
b_out[row]= b
return (a_out, b_out)
def dcs2c( double a, double b ):
"""
v = dcs2c( a, b )
@palDcs2c@
"""
cdef double cv[3]
cpal.palDcs2c( a, b, cv )
cdef np.ndarray v = np.zeros( [3], dtype=np.float64 )
for i in range(3):
v[i] = cv[i]
return v
def dcs2cVector( np.ndarray[double, ndim=1] a_in not None,
np.ndarray[double, ndim=1] b_in not None):
"""
v = dcs2cVector(a, b)
where a and b are numpy arrays of spherical coordinates.
v is a 2-D numpy array in which each row contains the
directional cosines of the corresponding Cartesian coordinates
@palDcs2c@
Raises
------
ValueError if input arrays have inconsisten lengths
"""
cdef int length = len(a_in)
if len(b_in) != length:
raise ValueError("You did not pass as many latitudes as longitudes " \
+ "into dcs2sVector")
cdef double cv[3]
cdef np.ndarray v_out = np.zeros((length, 3), dtype=np.float64)
for row in range(length):
cpal.palDcs2c(a_in[row], b_in[row], cv)
for i in range(3):
v_out[row][i] = cv[i]
return v_out
def dd2tf( int ndp, double days ):
"""
(sign, ih, im, is, frac) = dd2tf( ndp, days )
@palDd2tf@
"""
cdef char csign = b' '
cdef int ihmsf[4]
cpal.palDd2tf( ndp, days, &csign, ihmsf )
sign = chr(csign)
return ( sign, ihmsf[0], ihmsf[1], ihmsf[2], ihmsf[3] )
def dd2tfVector( int ndp, np.ndarray[double, ndim=1] days not None):
"""
(sign, ih, im, is ,frac) = ddtfVector( ndp, dayList)
where ndp is an integer denoting the resolution to which the
fraction of seconds should be taken and dayList is a numpy
array of values to be converted. sign, ih, im, is, and frac
will also be numpy arrays corresponding to the elements in
dayList.
@palDd2tf@
"""
cdef int length = len(days)
cdef np.ndarray sign_out = np.empty(length, dtype = (unicode, 1))
cdef np.ndarray ih_out = np.empty(length, dtype = np.int64)
cdef np.ndarray im_out = np.empty(length, dtype = np.int64)
cdef np.ndarray is_out = np.empty(length, dtype = np.int64)
cdef np.ndarray frac_out = np.empty(length, dtype = np.int64)
cdef char csign = b' '
cdef int ihmsf[4]
for row in range(length):
cpal.palDd2tf( ndp, days[row], &csign, ihmsf)
sign_out[row] = chr(csign)
ih_out[row] = ihmsf[0]
im_out[row] = ihmsf[1]
is_out[row] = ihmsf[2]
frac_out[row] = ihmsf[3]
return (sign_out, ih_out, im_out, is_out, frac_out)
def de2h( double ha, double dec, double phi ):
"""
(az, el) = de2h( ha, dec, phi )
@palDe2h@
"""
cdef double az
cdef double el
cpal.palDe2h( ha, dec, phi, &az, &el )
return (az, el)
def de2hVector( np.ndarray[double, ndim=1] ha not None,
np.ndarray[double, ndim=1] dec not None,
double phi ):
"""
(az, el) = de2hVector( ha, dec, phi )
where ha, dec, az, and el are numpy arrays
@palDe2h@
Raises
------
ValueError if input arrays are not of the same length
"""
cdef int i
cdef int length=len(ha)
if len(dec)!=length:
raise ValueError("You did not pass the same number of " \
+ "Decs as Hour Angles to de2hVector")
cdef np.ndarray azout = np.zeros(length,dtype=np.float64)
cdef np.ndarray elout = np.zeros(length,dtype=np.float64)
cdef double az
cdef double el
for i in range(length):
cpal.palDe2h( ha[i], dec[i], phi, &az, &el )
azout[i]=az
elout[i]=el
return (azout, elout)
def deuler( order, double phi, double theta, double psi ):
"""
rmat = deuler( order, phi, theta, psi )
@palDeuler@
"""
cdef double c_rmat[3][3]
byte_order = order.encode('ascii')
cdef char * c_order = byte_order
cpal.palDeuler( c_order, phi, theta, psi, c_rmat )
cdef np.ndarray rmat = np.zeros([3,3], dtype=np.float64)
for i in range(3):
for j in range(3):
rmat[i][j] = c_rmat[i][j]
return rmat
# dfltin() not implemented -- not necessary for python
def dh2e( double az, double el, double phi ):
"""
(ha, dec) = dh2e( az, el, phi )
@palDh2e@
"""
cdef double ha
cdef double dec
cpal.palDh2e( az, el, phi, &ha, &dec )
return (ha, dec)
def dh2eVector(np.ndarray[double, ndim=1] az not None,
np.ndarray[double, ndim=1] el not None,
double phi):
"""
(ha, dec) = dh2eVector( az, el, phi)
where az, el, ha, and dec are all numpy arrays
@palDh2e@
Raises
------
ValueError if the input arrays are of different lengths
"""
cdef int length = len(az)
if len(el)!=length:
raise ValueError("You did not pass as many elevations as " \
+ "azimuths to dh2eVector")
cdef np.ndarray ha_out = np.empty(length, dtype=np.float64)
cdef np.ndarray dec_out = np.empty(length, dtype=np.float64)
cdef double ha
cdef double dec
for ii in range(length):
cpal.palDh2e(az[ii], el[ii], phi, &ha, &dec)
ha_out[ii] = ha
dec_out[ii] = dec
return (ha_out, dec_out)
def dimxv( np.ndarray[double, ndim=2] dm not None, np.ndarray[double, ndim=1] va not None):
"""
vb = dimxv( dm, va )
@palDimxv@
"""
cdef double c_dm[3][3]
cdef double c_va[3]
cdef double c_vb[3]
for i in range(3):
for j in range(3):
c_dm[i][j] = dm[i][j]
c_va[i] = va[i]
cpal.palDimxv(c_dm, c_va, c_vb)
cdef np.ndarray vb = np.zeros([3], dtype=np.float64)
for i in range(3):
vb[i] = c_vb[i]
return vb
def djcal( int ndp, double djm ):
"""
(iy, im, id, frac) = djcal( ndp, djm )
@palDjcal@
Raises
------
ValueError for unacceptable dates.
"""
cdef int iymdf[4]
cdef int j
cpal.palDjcal( ndp, djm, iymdf, &j )
if j==0:
return ( iymdf[0], iymdf[1], iymdf[2], iymdf[3] )
else:
raise ValueError( "Unacceptable date" )
def djcalVector(int ndp, np.ndarray[double, ndim=1] djm not None):
"""
(iy, im, id, frac) = djcalVector( ndp, djm)
where djm and outputs are numpy arrays
@palDjcal@
Raises
------
Does not raise any exceptions; unacceptable dates (mjd<-2468570
or mjd>1e9) result in -1 in iy, im, id and frac.
Note: -1 is a valid result of iy (not for im, id, or frac), so
any filters on unacceptable dates should examine the other
outputs.
"""
cdef int length = len(djm)
cdef np.ndarray iy_out = np.empty(length, dtype=np.int64)
cdef np.ndarray im_out = np.empty(length, dtype=np.int64)
cdef np.ndarray id_out = np.empty(length, dtype=np.int64)
cdef np.ndarray frac_out = np.empty(length, dtype=np.int64)
cdef int output[4]
cdef int flag
for ii in range(length):
cpal.palDjcal(ndp, djm[ii], output, &flag)
if flag==0:
iy_out[ii] = output[0]
im_out[ii] = output[1]
id_out[ii] = output[2]
frac_out[ii] = output[3]
else:
iy_out[ii] = -1
im_out[ii] = -1
id_out[ii] = -1
frac_out[ii] = -1
return (iy_out, im_out, id_out, frac_out)
def djcl( double djm ):
"""
(iy, im, id, frac) = djcl( djm )
@palDjcl@
Raises
------
ValueError for unacceptable date.
"""
cdef int iy