Skip to content

Commit 349df86

Browse files
committed
Merge branch 'visualization'
2 parents 926b947 + c2c8f1d commit 349df86

File tree

2 files changed

+332
-29
lines changed

2 files changed

+332
-29
lines changed

util/pixfunc.py

+104-29
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from astropy.coordinates import SkyCoord
1515
import scipy.sparse
1616

17+
UNSEEN = None
18+
1719
def coord2pix(lon, lat=None, coord='C', res='F'):
1820
'''
1921
Get the COBE pixel closest to the input longitude/latitude
@@ -66,18 +68,15 @@ def coord2pix(lon, lat=None, coord='C', res='F'):
6668

6769
output = _uv2pix(c, res=res)
6870

69-
if npts == 1:
70-
return output[0]
71-
72-
return output
71+
return np.squeeze(output)
7372

7473
def _uv2pix(c, res=6):
7574
'''Returns pixel number given unit vector pointing to center
7675
of pixel resolution of the cude
7776
7877
Parameters
7978
----------
80-
c : astropy.coordinates.SkyCoord or array-like (Npts, )
79+
c : astropy.coordinates.SkyCoord or array-like
8180
sky coordinate system in cartesian coordinates
8281
8382
res : int, optional
@@ -89,13 +88,20 @@ def _uv2pix(c, res=6):
8988
pixel numbers
9089
'''
9190

91+
ndim = np.ndim(c)
9292
npts = np.size(c)
9393

9494
res1 = res - 1
9595
num_pix_side = 2.0**res1
9696
#num_pix_face = num_pix_side**2
9797

9898
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)
99105

100106
i = x * num_pix_side
101107
i[i > 2**res1-1] = 2**res1 - 1
@@ -104,14 +110,17 @@ def _uv2pix(c, res=6):
104110
j = y * num_pix_side
105111
j[j > 2**res1-1] = 2**res1 - 1
106112
j = np.array(j, dtype=np.int)
107-
113+
108114
fij = np.empty([npts, 3])
109115
fij[:, 0] = face
110116
fij[:, 1] = i
111117
fij[:, 2] = j
112118

113119
pixel = _fij2pix(fij, res)
114120

121+
if ndim > 1:
122+
pixel = np.reshape(pixel, shape)
123+
115124
return pixel
116125

117126
def _axisxy(c):
@@ -127,21 +136,24 @@ def _axisxy(c):
127136

128137

129138
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
137149
else:
138150
c0 = np.array([c.x.value])
139151
c1 = np.array([c.y.value])
140152
c2 = np.array([c.z.value])
141153

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
145157

146158
g = c0 != 0
147159
abs_yx[g] = np.abs(c1[g]/c0[g])
@@ -160,6 +172,29 @@ def _axisxy(c):
160172
eta = np.zeros_like(c0)
161173
xi = np.zeros_like(c0)
162174

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+
'''
163198
for i in range(n):
164199
if nface[i] == 0:
165200
eta[i] = -c0[i]/c2[i]
@@ -181,6 +216,7 @@ def _axisxy(c):
181216
xi[i] = -c1[i]/c2[i]
182217
else:
183218
raise ValueError("Invalid face number")
219+
'''
184220

185221
x, y = _incube(xi, eta)
186222

@@ -559,7 +595,7 @@ def edgchk(nface, ix, iy, maxval):
559595
tempy = iy
560596
tempface = nface % 6
561597

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:
563599
if tempx < 0:
564600
if tempface == 0:
565601
mface = 4
@@ -727,8 +763,8 @@ def get_8_neighbors(pixel, res, four_neighbors=False):
727763
1. Divide the pixel number up into x and y bits. These are the cartesian coordinates
728764
of the pixel on the face.
729765
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.
732768
Resoution=15 is the 'intermediary' format for finding neighbors.
733769
3. Determine the cartesian coordinates (res=15) of the neighbors by adding the appropriate
734770
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):
786822
jyhi = jy // 128
787823
jylo = jy % 128
788824
neighbors[0] = (nface * two28) + ixtab[jxlo] + iytab[jylo] + two14 * (ixtab[jxhi] + iytab[jyhi])
789-
neighbors[0] /= divisor
790-
825+
neighbors[0] //= divisor
826+
791827
nface, jx, jy = edgchk(face, ix, iy+distance, two14) #Top
792828
jxhi = jx // 128
793829
jxlo = jx % 128
794830
jyhi = jy // 128
795831
jylo = jy % 128
796832
neighbors[1] = (nface * two28) + ixtab[jxlo] + iytab[jylo] + two14 * (ixtab[jxhi] + iytab[jyhi])
797-
neighbors[1] /= divisor
833+
neighbors[1] //= divisor
798834

799835
nface, jx, jy = edgchk(face, ix-distance, iy, two14) #Left
800836
jxhi = jx // 128
801837
jxlo = jx % 128
802838
jyhi = jy // 128
803839
jylo = jy % 128
804840
neighbors[2] = (nface * two28) + ixtab[jxlo] + iytab[jylo] + two14 * (ixtab[jxhi] + iytab[jyhi])
805-
neighbors[2] /= divisor
841+
neighbors[2] //= divisor
806842

807843
nface, jx, jy = edgchk(face, ix, iy-distance, two14) #Bottom
808844
jxhi = jx // 128
809845
jxlo = jx % 128
810846
jyhi = jy // 128
811847
jylo = jy % 128
812848
neighbors[3] = (nface * two28) + ixtab[jxlo] + iytab[jylo] + two14 * (ixtab[jxhi] + iytab[jyhi])
813-
neighbors[3] /= divisor
849+
neighbors[3] //= divisor
814850

815851
if not four_neighbors:
816852
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):
819855
jyhi = jy // 128
820856
jylo = jy % 128
821857
neighbors[4] = (nface * two28) + ixtab[jxlo] + iytab[jylo] + two14 * (ixtab[jxhi] + iytab[jyhi])
822-
neighbors[4] /= divisor
858+
neighbors[4] //= divisor
823859

824860
nface, jx, jy = edgchk(face, ix-distance, iy+distance, two14) #Top-Left
825861
jxhi = jx // 128
826862
jxlo = jx % 128
827863
jyhi = jy // 128
828864
jylo = jy % 128
829865
neighbors[5] = (nface * two28) + ixtab[jxlo] + iytab[jylo] + two14 * (ixtab[jxhi] + iytab[jyhi])
830-
neighbors[5] /= divisor
866+
neighbors[5] //= divisor
831867

832868
nface, jx, jy = edgchk(face, ix-distance, iy-distance, two14) #Bottom-Left
833869
jxhi = jx // 128
834870
jxlo = jx % 128
835871
jyhi = jy // 128
836872
jylo = jy % 128
837873
neighbors[6] = (nface * two28) + ixtab[jxlo] + iytab[jylo] + two14 * (ixtab[jxhi] + iytab[jyhi])
838-
neighbors[6] /= divisor
874+
neighbors[6] //= divisor
839875

840876
nface, jx, jy = edgchk(face, ix+distance, iy-distance, two14) #Bottom-Right
841877
jxhi = jx // 128
842878
jxlo = jx % 128
843879
jyhi = jy // 128
844880
jylo = jy % 128
845881
neighbors[7] = (nface * two28) + ixtab[jxlo] + iytab[jylo] + two14 * (ixtab[jxhi] + iytab[jyhi])
846-
neighbors[7] /= divisor
882+
neighbors[7] //= divisor
847883

848-
return np.unique(neighbors)
884+
#return np.unique(neighbors)
885+
return neighbors
849886

850887
def get_4_neighbors(pixel, res):
851888

@@ -1178,3 +1215,41 @@ def _uv2proj(uvec, proj, sz_proj):
11781215
else:
11791216
raise ValueError("Invalid projection string entered")
11801217

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

Comments
 (0)