def healpixellize(f_in,theta_in,phi_in,nside,fancy=False): """ A dumb method for converting data f sampled at points theta and phi (not on a healpix grid) into a healpix at resolution nside """ # Input arrays are likely to be rectangular, but this is inconvenient f = f_in.flatten() theta = theta_in.flatten() phi = phi_in.flatten() pix = hp.ang2pix(nside,theta,phi) map = np.zeros(hp.nside2npix(nside)) hits = np.zeros(hp.nside2npix(nside)) # Simplest gridding is map[pix] = val. This tries to do some #averaging Better would be to do some weighting by distance from #pixel center or something ... if (fancy): for i,v in enumerate(f): # Find the nearest pixels to the pixel in question neighbours,weights = hp.get_neighbours(nside,theta[i],phi[i]) # Add weighted values to map map[neighbours] += v*weights # Keep track of weights hits[neighbours] += weights map = map/hits wh_no_hits = np.where(hits == 0) map[wh_no_hits[0]] = hp.UNSEEN else: for i,v in enumerate(f): map[pix[i]] += v hits[pix[i]] +=1 map = map/hits return map
def contour_pix(map, vals, all_neighbours=True): """ given a healpix map (map) and a set of values, we find and return lists of pixels that constitute the boarder of those sets """ npix = len(map) nside = hp.npix2nside(npix) ### separate into pixel sets based in p_values pix = np.arange(npix) boarders = [] for val in vals: _pix = pix[map>=val] ### pull out included pixels truth = np.zeros((npix,), bool) truth[_pix] = True boarder = np.zeros((npix,),bool) ### defines which modes are in the boarder for ipix in _pix: if all_neighbours: boarder[ipix] = not truth[[n for n in hp.get_all_neighbours(nside, ipix) if n != -1]].all() else: boarder[ipix] = not truth[[n for n in hp.get_neighbours(nside, ipix)[0] if n != -1]].all() boarders.append( pix[boarder] ) return boarders
def healpixellize(f_in,theta_in,phi_in,nside,weighted=True): """ A brute force method for converting data f sampled at points theta and phi (not on a healpix grid) into a healpix at resolution nside """ # Input arrays are likely to be rectangular, but this is inconvenient f = f_in.flatten() theta = theta_in.flatten() phi = phi_in.flatten() pix = hp.ang2pix(nside,theta,phi) map = np.zeros(hp.nside2npix(nside)) hits = np.zeros(hp.nside2npix(nside)) # Default method is gridding based on weighted four-point interpolation to # the nearest pixel (from healpy). Simply finding the nearest pixel is # what will happen if weighted is set to False if (weighted): for i,v in enumerate(f): # Find the nearest pixels to the pixel in question neighbours,weights = hp.get_neighbours(nside,theta[i],phi[i]) # Add weighted values to map map[neighbours] += v*weights # Keep track of weights hits[neighbours] += weights map = map/hits wh_no_hits = np.where(hits == 0) map[wh_no_hits[0]] = hp.UNSEEN else: for i,v in enumerate(f): map[pix[i]] += v hits[pix[i]] +=1 map = map/hits return (map,hits)
def _weighting(self,interpPoints, values): """ interpPoints is a numpy array where interpolation is desired values are the model values. """ result = np.zeros( (interpPoints.size, np.size(values[0])) ,dtype=float) inRange = np.where( (interpPoints['airmass'] <= np.max(self.dimDict['airmass'])) & (interpPoints['airmass'] >= np.min(self.dimDict['airmass'])) ) usePoints = interpPoints[inRange] # Find the neighboring healpixels hpids, hweights = hp.get_neighbours(self.nside, np.pi/2.-usePoints['altEclip'], usePoints['azEclipRelSun'] ) badhp = np.in1d(hpids.ravel(), self.dimDict['hpid'], invert=True).reshape(hpids.shape) hweights[badhp] = 0. norm = np.sum(hweights,axis=0) good= np.where(norm != 0.)[0] hweights[:,good] = hweights[:,good]/norm[good] amRightIndex, amLeftIndex, amRightW, amLeftW = self.indxAndWeights(usePoints['airmass'], self.dimDict['airmass']) nhpid = self.dimDict['hpid'].size # loop though the hweights and the airmass weights for hpid,hweight in zip(hpids,hweights): for amIndex, amW in zip([amRightIndex,amLeftIndex], [amRightW,amLeftW] ): weight = hweight*amW result[inRange] += weight[:,np.newaxis]*values[amIndex*nhpid+hpid] return result
def _weighting(self, interpPoints, values): """ """ result = np.zeros((interpPoints.size, np.size(values[0])), dtype=float) # Check that moonAltitude is in range, otherwise return zero array if np.max(interpPoints['moonAltitude']) < np.min( self.dimDict['moonAltitude']): return result # Find the neighboring healpixels hpids, hweights = hp.get_neighbours(self.nside, np.pi / 2. - interpPoints['alt'], interpPoints['azRelMoon']) badhp = np.in1d(hpids.ravel(), self.dimDict['hpid'], invert=True).reshape(hpids.shape) hweights[badhp] = 0. norm = np.sum(hweights, axis=0) good = np.where(norm != 0.)[0] hweights[:, good] = hweights[:, good] / norm[good] # Find the neighboring moonAltitude points in the grid rightMAs, leftMAs, maRightW, maLeftW = self.indxAndWeights( interpPoints['moonAltitude'], self.dimDict['moonAltitude']) # Find the neighboring moonSunSep points in the grid rightMss, leftMss, mssRightW, mssLeftW = self.indxAndWeights( interpPoints['moonSunSep'], self.dimDict['moonSunSep']) nhpid = self.dimDict['hpid'].size nMA = self.dimDict['moonAltitude'].size # Convert the hpid to an index. tmp = intid2id(hpids.ravel(), self.dimDict['hpid'], np.arange(self.dimDict['hpid'].size)) hpindx = tmp.reshape(hpids.shape) # loop though the hweights and the moonAltitude weights for hpid, hweight in zip(hpindx, hweights): for maid, maW in zip([rightMAs, leftMAs], [maRightW, maLeftW]): for mssid, mssW in zip([rightMss, leftMss], [mssRightW, mssLeftW]): weight = hweight * maW * mssW result += weight[:, np.newaxis] * values[mssid * nhpid * nMA + maid * nhpid + hpid] return result
def _weighting(self, interpPoints, values): """ """ result = np.zeros( (interpPoints.size, np.size(values[0])) ,dtype=float) # Check that moonAltitude is in range, otherwise return zero array if np.max(interpPoints['moonAltitude']) < np.min(self.dimDict['moonAltitude']): return result # Find the neighboring healpixels hpids, hweights = hp.get_neighbours(self.nside, np.pi/2.-interpPoints['alt'], interpPoints['azRelMoon'] ) badhp = np.in1d(hpids.ravel(), self.dimDict['hpid'], invert=True).reshape(hpids.shape) hweights[badhp] = 0. norm = np.sum(hweights,axis=0) good= np.where(norm != 0.)[0] hweights[:,good] = hweights[:,good]/norm[good] # Find the neighboring moonAltitude points in the grid rightMAs, leftMAs, maRightW, maLeftW = self.indxAndWeights(interpPoints['moonAltitude'], self.dimDict['moonAltitude']) # Find the neighboring moonSunSep points in the grid rightMss, leftMss, mssRightW, mssLeftW = self.indxAndWeights(interpPoints['moonSunSep'], self.dimDict['moonSunSep']) nhpid = self.dimDict['hpid'].size nMA = self.dimDict['moonAltitude'].size # Convert the hpid to an index. tmp = intid2id(hpids.ravel(), self.dimDict['hpid'], np.arange( self.dimDict['hpid'].size)) hpindx = tmp.reshape(hpids.shape) # loop though the hweights and the moonAltitude weights for hpid,hweight in zip(hpindx,hweights): for maid,maW in zip([rightMAs,leftMAs],[maRightW,maLeftW] ): for mssid,mssW in zip([rightMss,leftMss], [mssRightW,mssLeftW]): weight = hweight*maW*mssW result += weight[:,np.newaxis]*values[mssid*nhpid*nMA+maid*nhpid+hpid] return result
def orth(base_UT22, start_UT22, unit, show=False): UT22_interp = np.zeros_like(base_UT22) c = 0 for i, val in enumerate(start_UT22): if np.isnan(val): c += 1 theta, phi = hp.pix2ang(nside, i) neybs = hp.get_neighbours(nside, theta, phi=phi) UT22_interp[i] = np.nanmean(start_UT22[neybs[0]]) else: UT22_interp[i] = val print(c, 'NaN vals') hp.orthview(UT22_interp, rot=[0, 90], min=0, max=2, unit=unit, title='SAST 00:00 2012-02-13', half_sky=True) if show: pylab.show() return UT22_interp
def _weighting(self, interpPoints, values): """ interpPoints is a numpy array where interpolation is desired values are the model values. """ result = np.zeros((interpPoints.size, np.size(values[0])), dtype=float) inRange = np.where( (interpPoints['airmass'] <= np.max(self.dimDict['airmass'])) & (interpPoints['airmass'] >= np.min(self.dimDict['airmass']))) usePoints = interpPoints[inRange] # Find the neighboring healpixels hpids, hweights = hp.get_neighbours(self.nside, np.pi / 2. - usePoints['altEclip'], usePoints['azEclipRelSun']) badhp = np.in1d(hpids.ravel(), self.dimDict['hpid'], invert=True).reshape(hpids.shape) hweights[badhp] = 0. norm = np.sum(hweights, axis=0) good = np.where(norm != 0.)[0] hweights[:, good] = hweights[:, good] / norm[good] amRightIndex, amLeftIndex, amRightW, amLeftW = self.indxAndWeights( usePoints['airmass'], self.dimDict['airmass']) nhpid = self.dimDict['hpid'].size # loop though the hweights and the airmass weights for hpid, hweight in zip(hpids, hweights): for amIndex, amW in zip([amRightIndex, amLeftIndex], [amRightW, amLeftW]): weight = hweight * amW result[inRange] += weight[:, np.newaxis] * values[amIndex * nhpid + hpid] return result
print 'issue with %s'%filename UT22[p] = np.nan c+=1 continue print c,'IOErrors' hp.orthview(UT22,rot=[0,90],max=2,unit=r'rad m$^{-1}$',title='SAST 00:00 2012-02-13',half_sky=True) #pylab.show() print UT22 print 'Interpolating NaNs' UT22_interp = np.zeros_like(UT22) c=0 for i,val in enumerate(UT22): if np.isnan(val): c+=1 theta,phi = hp.pix2ang(nside,i) neybs = hp.get_neighbours(nside,theta,phi=phi) v = np.nanmean(UT22[neybs[0]]) UT22_interp[i] = v else: UT22_interp[i]=val print c,'NaN vals' hp.orthview(UT22_interp,rot=[0,90],min=0,max=2,unit=r'rad m$^{-1}$',title='SAST 00:00 2012-02-13',half_sky=True) #pylab.show() UT22_interp_2 = np.zeros_like(UT22) c=0 for i,val in enumerate(UT22_interp): if np.isnan(val): c+=1 theta,phi = hp.pix2ang(nside,i)