14
14
from astropy .coordinates import SkyCoord
15
15
import scipy .sparse
16
16
17
+ UNSEEN = None
18
+
17
19
def coord2pix (lon , lat = None , coord = 'C' , res = 'F' ):
18
20
'''
19
21
Get the COBE pixel closest to the input longitude/latitude
@@ -66,18 +68,15 @@ def coord2pix(lon, lat=None, coord='C', res='F'):
66
68
67
69
output = _uv2pix (c , res = res )
68
70
69
- if npts == 1 :
70
- return output [0 ]
71
-
72
- return output
71
+ return np .squeeze (output )
73
72
74
73
def _uv2pix (c , res = 6 ):
75
74
'''Returns pixel number given unit vector pointing to center
76
75
of pixel resolution of the cude
77
76
78
77
Parameters
79
78
----------
80
- c : astropy.coordinates.SkyCoord or array-like (Npts, )
79
+ c : astropy.coordinates.SkyCoord or array-like
81
80
sky coordinate system in cartesian coordinates
82
81
83
82
res : int, optional
@@ -89,13 +88,20 @@ def _uv2pix(c, res=6):
89
88
pixel numbers
90
89
'''
91
90
91
+ ndim = np .ndim (c )
92
92
npts = np .size (c )
93
93
94
94
res1 = res - 1
95
95
num_pix_side = 2.0 ** res1
96
96
#num_pix_face = num_pix_side**2
97
97
98
98
face , x , y = _axisxy (c )
99
+
100
+ if ndim > 1 :
101
+ shape = np .shape (face )
102
+ face = np .ravel (face )
103
+ x = np .ravel (x )
104
+ y = np .ravel (y )
99
105
100
106
i = x * num_pix_side
101
107
i [i > 2 ** res1 - 1 ] = 2 ** res1 - 1
@@ -104,14 +110,17 @@ def _uv2pix(c, res=6):
104
110
j = y * num_pix_side
105
111
j [j > 2 ** res1 - 1 ] = 2 ** res1 - 1
106
112
j = np .array (j , dtype = np .int )
107
-
113
+
108
114
fij = np .empty ([npts , 3 ])
109
115
fij [:, 0 ] = face
110
116
fij [:, 1 ] = i
111
117
fij [:, 2 ] = j
112
118
113
119
pixel = _fij2pix (fij , res )
114
120
121
+ if ndim > 1 :
122
+ pixel = np .reshape (pixel , shape )
123
+
115
124
return pixel
116
125
117
126
def _axisxy (c ):
@@ -127,21 +136,24 @@ def _axisxy(c):
127
136
128
137
129
138
if n > 1 :
130
- c0 = np .zeros (n )
131
- c1 = np .zeros (n )
132
- c2 = np .zeros (n )
133
- for i in range (n ):
134
- c0 [i ] = c [i ].x .value
135
- c1 [i ] = c [i ].y .value
136
- c2 [i ] = c [i ].z .value
139
+ #c0 = np.zeros(n)
140
+ #c1 = np.zeros(n)
141
+ #c2 = np.zeros(n)
142
+ #for i in range(n):
143
+ # c0[i] = c[i].x.value
144
+ # c1[i] = c[i].y.value
145
+ # c2[i] = c[i].z.value
146
+ c0 = c .x .value
147
+ c1 = c .y .value
148
+ c2 = c .z .value
137
149
else :
138
150
c0 = np .array ([c .x .value ])
139
151
c1 = np .array ([c .y .value ])
140
152
c2 = np .array ([c .z .value ])
141
153
142
- abs_yx = np .ones ( n )* np .inf
143
- abs_zx = np .ones ( n )* np .inf
144
- abs_zy = np .ones ( n )* np .inf
154
+ abs_yx = np .ones_like ( c0 , dtype = np . float )* np .inf
155
+ abs_zx = np .ones_like ( c0 , dtype = np . float )* np .inf
156
+ abs_zy = np .ones_like ( c0 , dtype = np . float )* np .inf
145
157
146
158
g = c0 != 0
147
159
abs_yx [g ] = np .abs (c1 [g ]/ c0 [g ])
@@ -160,6 +172,29 @@ def _axisxy(c):
160
172
eta = np .zeros_like (c0 )
161
173
xi = np .zeros_like (c0 )
162
174
175
+ i = nface == 0
176
+ eta [i ] = - c0 [i ]/ c2 [i ]
177
+ xi [i ] = c1 [i ]/ c2 [i ]
178
+ i = nface == 1
179
+ eta [i ] = c2 [i ]/ c0 [i ]
180
+ xi [i ] = c1 [i ]/ c0 [i ]
181
+ i = nface == 2
182
+ eta [i ] = c2 [i ]/ c1 [i ]
183
+ xi [i ] = - c0 [i ]/ c1 [i ]
184
+ i = nface == 3
185
+ eta [i ] = - c2 [i ]/ c0 [i ]
186
+ xi [i ] = c1 [i ]/ c0 [i ]
187
+ i = nface == 4
188
+ eta [i ] = - c2 [i ]/ c1 [i ]
189
+ xi [i ] = - c0 [i ]/ c1 [i ]
190
+ i = nface == 5
191
+ eta [i ] = - c0 [i ]/ c2 [i ]
192
+ xi [i ] = - c1 [i ]/ c2 [i ]
193
+ i = nface > 5
194
+ if np .sum (i ) > 0 :
195
+ raise ValueError ("Invalid face number at idx:" , np .where (i ))
196
+
197
+ '''
163
198
for i in range(n):
164
199
if nface[i] == 0:
165
200
eta[i] = -c0[i]/c2[i]
@@ -181,6 +216,7 @@ def _axisxy(c):
181
216
xi[i] = -c1[i]/c2[i]
182
217
else:
183
218
raise ValueError("Invalid face number")
219
+ '''
184
220
185
221
x , y = _incube (xi , eta )
186
222
@@ -559,7 +595,7 @@ def edgchk(nface, ix, iy, maxval):
559
595
tempy = iy
560
596
tempface = nface % 6
561
597
562
- while tempx < 0 or tempx >= maxval or tempy < 0 or tempy >= maxval :
598
+ while tempx < 0 or tempx >= maxval or tempy < 0 or tempy >= maxval :
563
599
if tempx < 0 :
564
600
if tempface == 0 :
565
601
mface = 4
@@ -727,8 +763,8 @@ def get_8_neighbors(pixel, res, four_neighbors=False):
727
763
1. Divide the pixel number up into x and y bits. These are the cartesian coordinates
728
764
of the pixel on the face.
729
765
2. Covert the (x, y) found from the original pixel number in the input resolution to the
730
- equivalent positions on a face of resolution 15. This is accomplixehd by multiplying
731
- by the varaince DISTANCE, which is the width of a RES pixel in resolution=15 pixels.
766
+ equivalent positions on a face of resolution 15. This is accomplished by multiplying
767
+ by the variable DISTANCE, which is the width of a RES pixel in resolution=15 pixels.
732
768
Resoution=15 is the 'intermediary' format for finding neighbors.
733
769
3. Determine the cartesian coordinates (res=15) of the neighbors by adding the appropriate
734
770
orthogonal offset (DISTANCE) to the center pixel (x, y). EDGCHK is called to adjust the
@@ -786,31 +822,31 @@ def get_8_neighbors(pixel, res, four_neighbors=False):
786
822
jyhi = jy // 128
787
823
jylo = jy % 128
788
824
neighbors [0 ] = (nface * two28 ) + ixtab [jxlo ] + iytab [jylo ] + two14 * (ixtab [jxhi ] + iytab [jyhi ])
789
- neighbors [0 ] /= divisor
790
-
825
+ neighbors [0 ] // = divisor
826
+
791
827
nface , jx , jy = edgchk (face , ix , iy + distance , two14 ) #Top
792
828
jxhi = jx // 128
793
829
jxlo = jx % 128
794
830
jyhi = jy // 128
795
831
jylo = jy % 128
796
832
neighbors [1 ] = (nface * two28 ) + ixtab [jxlo ] + iytab [jylo ] + two14 * (ixtab [jxhi ] + iytab [jyhi ])
797
- neighbors [1 ] /= divisor
833
+ neighbors [1 ] // = divisor
798
834
799
835
nface , jx , jy = edgchk (face , ix - distance , iy , two14 ) #Left
800
836
jxhi = jx // 128
801
837
jxlo = jx % 128
802
838
jyhi = jy // 128
803
839
jylo = jy % 128
804
840
neighbors [2 ] = (nface * two28 ) + ixtab [jxlo ] + iytab [jylo ] + two14 * (ixtab [jxhi ] + iytab [jyhi ])
805
- neighbors [2 ] /= divisor
841
+ neighbors [2 ] // = divisor
806
842
807
843
nface , jx , jy = edgchk (face , ix , iy - distance , two14 ) #Bottom
808
844
jxhi = jx // 128
809
845
jxlo = jx % 128
810
846
jyhi = jy // 128
811
847
jylo = jy % 128
812
848
neighbors [3 ] = (nface * two28 ) + ixtab [jxlo ] + iytab [jylo ] + two14 * (ixtab [jxhi ] + iytab [jyhi ])
813
- neighbors [3 ] /= divisor
849
+ neighbors [3 ] // = divisor
814
850
815
851
if not four_neighbors :
816
852
nface , jx , jy = edgchk (face , ix + distance , iy + distance , two14 ) #Top-Right
@@ -819,33 +855,34 @@ def get_8_neighbors(pixel, res, four_neighbors=False):
819
855
jyhi = jy // 128
820
856
jylo = jy % 128
821
857
neighbors [4 ] = (nface * two28 ) + ixtab [jxlo ] + iytab [jylo ] + two14 * (ixtab [jxhi ] + iytab [jyhi ])
822
- neighbors [4 ] /= divisor
858
+ neighbors [4 ] // = divisor
823
859
824
860
nface , jx , jy = edgchk (face , ix - distance , iy + distance , two14 ) #Top-Left
825
861
jxhi = jx // 128
826
862
jxlo = jx % 128
827
863
jyhi = jy // 128
828
864
jylo = jy % 128
829
865
neighbors [5 ] = (nface * two28 ) + ixtab [jxlo ] + iytab [jylo ] + two14 * (ixtab [jxhi ] + iytab [jyhi ])
830
- neighbors [5 ] /= divisor
866
+ neighbors [5 ] // = divisor
831
867
832
868
nface , jx , jy = edgchk (face , ix - distance , iy - distance , two14 ) #Bottom-Left
833
869
jxhi = jx // 128
834
870
jxlo = jx % 128
835
871
jyhi = jy // 128
836
872
jylo = jy % 128
837
873
neighbors [6 ] = (nface * two28 ) + ixtab [jxlo ] + iytab [jylo ] + two14 * (ixtab [jxhi ] + iytab [jyhi ])
838
- neighbors [6 ] /= divisor
874
+ neighbors [6 ] // = divisor
839
875
840
876
nface , jx , jy = edgchk (face , ix + distance , iy - distance , two14 ) #Bottom-Right
841
877
jxhi = jx // 128
842
878
jxlo = jx % 128
843
879
jyhi = jy // 128
844
880
jylo = jy % 128
845
881
neighbors [7 ] = (nface * two28 ) + ixtab [jxlo ] + iytab [jylo ] + two14 * (ixtab [jxhi ] + iytab [jyhi ])
846
- neighbors [7 ] /= divisor
882
+ neighbors [7 ] // = divisor
847
883
848
- return np .unique (neighbors )
884
+ #return np.unique(neighbors)
885
+ return neighbors
849
886
850
887
def get_4_neighbors (pixel , res ):
851
888
@@ -1178,3 +1215,41 @@ def _uv2proj(uvec, proj, sz_proj):
1178
1215
else :
1179
1216
raise ValueError ("Invalid projection string entered" )
1180
1217
1218
+ def get_map_size (m ):
1219
+ return len (m )
1220
+
1221
+ def get_res (m ):
1222
+ npix = get_map_size (m )
1223
+ return npix2res (npix )
1224
+
1225
+ def fit_monopole (m , bad = UNSEEN , gal_cut = 0 ):
1226
+ npix = m .size
1227
+ res = npix2res (npix )
1228
+ pixel = np .arange (npix )
1229
+
1230
+ c = pix2coord (pixel , res = res , coord = 'G' )
1231
+ galb = c .b .deg
1232
+
1233
+ idx = np .abs (galb ) > gal_cut
1234
+
1235
+ mono = np .nanmean (m [idx ])
1236
+
1237
+ return mono
1238
+
1239
+
1240
+ def remove_monopole (m , bad = UNSEEN , gal_cut = 0 , fitval = False , copy = True , verbose = True ):
1241
+ m = np .array (m , copy = copy )
1242
+ npix = m .size
1243
+ res = npix2res (npix )
1244
+ mono = fit_monopole (m , bad = bad , gal_cut = gal_cut )
1245
+
1246
+ idx = np .logical_and (m != bad , np .isfinite (m ))
1247
+
1248
+ m [idx ] -= mono
1249
+
1250
+ if verbose :
1251
+ print ("monopole:" , mono )
1252
+ if fitval :
1253
+ return m , mono
1254
+ else :
1255
+ return m
0 commit comments