def from_skycoords(cls, skycoords, max_norder): """ Creates a MOC from an `astropy.coordinates.SkyCoord`. Parameters ---------- skycoords : `astropy.coordinates.SkyCoord` The sky coordinates that will belong to the MOC. max_norder : int The depth of the smallest HEALPix cells contained in the MOC. Returns ------- result : `~mocpy.moc.MOC` The resulting MOC """ hp = HEALPix(nside=(1 << max_norder), order='nested') ipix = hp.lonlat_to_healpix(skycoords.icrs.ra, skycoords.icrs.dec) ipix = ipix.astype(np.uint64) shift = np.uint8(2) * (AbstractMOC.HPY_MAX_NORDER - np.uint8(max_norder)) intervals = np.vstack((ipix << shift, (ipix + np.uint64(1)) << shift)).T interval_set = IntervalSet(intervals) return cls(interval_set)
def from_lonlat(cls, lon, lat, max_norder): """ Creates a MOC from astropy lon, lat `astropy.units.Quantity`. Parameters ---------- lon : `astropy.units.Quantity` The longitudes of the sky coordinates belonging to the MOC. lat : `astropy.units.Quantity` The latitudes of the sky coordinates belonging to the MOC. max_norder : int The depth of the smallest HEALPix cells contained in the MOC. Returns ------- result : `~mocpy.moc.MOC` The resulting MOC """ hp = HEALPix(nside=(1 << max_norder), order='nested') ipix = hp.lonlat_to_healpix(lon, lat) ipix = ipix.astype(np.uint64) shift = np.uint8(2) * (AbstractMOC.HPY_MAX_NORDER - np.uint8(max_norder)) intervals = np.vstack((ipix << shift, (ipix + np.uint64(1)) << shift)).T interval_set = IntervalSet(intervals) return cls(interval_set)
def contains(self, ra, dec, keep_inside=True): """ Returns a boolean mask array of the positions lying inside (or outside) the MOC instance. Parameters ---------- ra : `astropy.units.Quantity` Right ascension array dec : `astropy.units.Quantity` Declination array keep_inside : bool, optional True by default. If so the mask describes coordinates lying inside the MOC. If ``keep_inside`` is false, contains will return the mask of the coordinates lying outside the MOC. Returns ------- array : `~np.ndarray` A mask boolean array """ depth = self.max_order m = np.zeros(nside2npix(1 << depth), dtype=bool) pix_id = self._best_res_pixels() m[pix_id] = True if not keep_inside: m = np.logical_not(m) hp = HEALPix(nside=(1 << depth), order='nested') pix = hp.lonlat_to_healpix(ra, dec) return m[pix]
def from_moc(depth_ipix_d, wcs): # Create a new MOC that do not contain the HEALPix # cells that are backfacing the projection depths = [int(depth_str) for depth_str in depth_ipix_d.keys()] min_depth = min(depths) max_depth = max(depths) ipixels = np.asarray(depth_ipix_d[str(min_depth)]) # Split the cells located at the border of the projection # until at least the depth 7 max_split_depth = max(7, max_depth) ipix_d = {} for depth in range(min_depth, max_split_depth + 1): hp = HEALPix(nside=(1 << depth), order='nested', frame=ICRS()) ipix_boundaries = hp.boundaries_skycoord(ipixels, step=1) # Projection on the given WCS xp, yp = skycoord_to_pixel(coords=ipix_boundaries, wcs=wcs) _, _, frontface_id = backface_culling(xp, yp) # Get the pixels which are backfacing the projection backfacing_ipix = ipixels[~frontface_id] frontface_ipix = ipixels[frontface_id] depth_str = str(depth) ipix_d.update({depth_str: frontface_ipix}) next_depth = str(depth + 1) ipixels = [] if next_depth in depth_ipix_d: ipixels = depth_ipix_d[next_depth] for bf_ipix in backfacing_ipix: child_bf_ipix = bf_ipix << 2 ipixels.extend([child_bf_ipix, child_bf_ipix + 1, child_bf_ipix + 2, child_bf_ipix + 3]) ipixels = np.asarray(ipixels) return ipix_d
def compute_healpix_vertices(depth, ipix, wcs): path_vertices = np.array([]) codes = np.array([]) depth = int(depth) step = 1 if depth < 3: step = 2 hp = HEALPix(order="nested", nside=(1 << depth), frame=ICRS()) ipix_boundaries = hp.boundaries_skycoord(ipix, step=step) # Projection on the given WCS xp, yp = skycoord_to_pixel(ipix_boundaries, wcs=wcs) c1 = np.vstack((xp[:, 0], yp[:, 0])).T c2 = np.vstack((xp[:, 1], yp[:, 1])).T c3 = np.vstack((xp[:, 2], yp[:, 2])).T c4 = np.vstack((xp[:, 3], yp[:, 3])).T if depth < 3: c5 = np.vstack((xp[:, 4], yp[:, 4])).T c6 = np.vstack((xp[:, 5], yp[:, 5])).T c7 = np.vstack((xp[:, 6], yp[:, 6])).T c8 = np.vstack((xp[:, 7], yp[:, 7])).T cells = np.hstack((c1, c2, c3, c4, c5, c6, c7, c8, np.zeros((c1.shape[0], 2)))) path_vertices = cells.reshape((9*c1.shape[0], 2)) single_code = np.array([Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY]) else: cells = np.hstack((c1, c2, c3, c4, np.zeros((c1.shape[0], 2)))) path_vertices = cells.reshape((5*c1.shape[0], 2)) single_code = np.array([Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY]) codes = np.tile(single_code, c1.shape[0]) return path_vertices, codes
def test_moc_contains(): order = 4 size = 20 healpix_arr = np.random.randint(0, 12*4**order, size) all_healpix_arr = np.arange(12*4**order) healpix_outside_arr = np.setdiff1d(all_healpix_arr, healpix_arr) moc = MOC.from_json(json_moc={str(order): list(healpix_arr)}) hp = HEALPix(nside=(1 << order), order='nested', frame=ICRS()) lon, lat = hp.healpix_to_lonlat(healpix_arr) lon_out, lat_out = hp.healpix_to_lonlat(healpix_outside_arr) should_be_inside_arr = moc.contains(ra=lon, dec=lat) assert should_be_inside_arr.all() should_be_outside_arr = moc.contains(ra=lon_out, dec=lat_out) assert not should_be_outside_arr.any() # test keep_inside field should_be_outside_arr = moc.contains(ra=lon, dec=lat, keep_inside=False) assert not should_be_outside_arr.any() should_be_inside_arr = moc.contains(ra=lon_out, dec=lat_out, keep_inside=False) assert should_be_inside_arr.all()
def makeMoc(outdir, ras, decs, maxorder=7): """Write Moc coverage files.""" print('Constructing MOC') ra_deg = ras * u.deg dec_deg = decs * u.deg # work out coverage of pixels hp = HEALPix(2**maxorder, order='nested') pix = hp.lonlat_to_healpix(ra_deg, dec_deg) pix = list(N.unique(pix)) # combine sets of four pixels to lower orders order = maxorder out = [] while order>0: nextpix = [] i = 0 while i < len(pix)-3: vi = pix[i] if vi%4==0 and pix[i+1]==vi+1 and pix[i+2]==vi+2 and pix[i+3]==vi+3: nextpix.append(vi//4) pix = pix[:i]+pix[i+4:] else: i += 1 out.insert(0, (order, N.array(pix))) pix = nextpix if len(pix)==0: break order -= 1 if len(pix) > 0: out.insert(0, (order, N.array(pix))) # write json out_json = {} for order, pixels in out: out_json[str(order)] = [int(i) for i in pixels] with open(os.path.join(outdir, 'Moc.json'), 'w') as fout: fout.write('#MOCORDER %i\n' % maxorder) json.dump(out_json, fout) # now write in FITS format fits_pixels = [] for order, pixels in out: fits_pixels.append(4 * 4**order + pixels) fits_pixels = N.concatenate(fits_pixels) fits_pixels.sort() print(' Included', len(fits_pixels), 'healpix pixels, up to level', order) cols = fits.ColDefs([ fits.Column(name='NPIX', format='J', array=fits_pixels)]) hdu = fits.BinTableHDU.from_columns(cols) hdr = hdu.header hdr['PIXTYPE'] = 'HEALPIX' hdr['ORDERING'] = 'NUNIQ' hdr['COORDSYS'] = 'C' hdr['MOCORDER'] = maxorder hdr['MOCTOOL'] = builder hdr['DATE'] = getDate() hdulist = fits.HDUList([fits.PrimaryHDU(), hdu]) hdulist.writeto(os.path.join(outdir, 'Moc.fits'), overwrite=True)
def my_plot(moc, frame=None, labels=False, title='', grid=False, save='', color='black', degrade=True): frame = Galactic() if frame is None else frame from matplotlib.colors import LinearSegmentedColormap import matplotlib.pyplot as plt plot_order = 8 if moc.max_order > plot_order and degrade: plotted_moc = moc.degrade_to_order(plot_order) else: plotted_moc = moc num_pixels_map = 1024 delta = 2. * np.pi / num_pixels_map x = np.arange(-np.pi, np.pi, delta) y = np.arange(-np.pi / 2, np.pi / 2, delta) lon_rad, lat_rad = np.meshgrid(x, y) hp = HEALPix(nside=(1 << plotted_moc.max_order), order='nested') if frame and not isinstance(frame, BaseCoordinateFrame): raise ValueError( "Only Galactic/ICRS coordinate systems are supported.") pix_map = hp.lonlat_to_healpix(lon_rad * u.rad, lat_rad * u.rad) m = np.zeros(12 * 4**(plotted_moc.max_order)) pix_id = core.flatten_pixels(plotted_moc._interval_set._intervals, plotted_moc.max_order) # change the HEALPix cells if the frame of the MOC is not the same as the one associated with the plot method. if isinstance(frame, Galactic): lon, lat = hp.boundaries_lonlat(pix_id, step=2) sky_crd = SkyCoord(lon, lat, unit='deg') pix_id = hp.lonlat_to_healpix(sky_crd.galactic.l, sky_crd.galactic.b) m[pix_id] = 1 z = np.flip(m[pix_map], axis=1) figsize = (12, 10) fig = plt.figure(figsize=figsize) ax = fig.add_subplot(111, projection="aitoff") color_map = LinearSegmentedColormap.from_list('w2r', ['white', color]) color_map.set_under('w') color_map.set_bad('w') # Note I flip x ax.pcolormesh(x, y, z, cmap=color_map, vmin=0, vmax=1) if labels: ax.tick_params(labelsize=14, labelcolor='#000000') if title: plt.title(title) if grid: plt.grid(True, linestyle='--', linewidth=1, color='#555555') ax.set_xticklabels([ '210', '240', '270', '300', '330', '0', '30', '60', '90', '120', '150' ]) ax.grid(grid) plt.tight_layout() if not labels: # disable tick labels ax.set_xticklabels([]) ax.set_yticklabels([]) if save: plt.savefig(save) else: plt.show()
def templateChoseByStarMchNum(): gwacData = '/data/work/wj/dat' dirs = os.listdir(gwacData) dirs.sort() tNames = [] tDatas = [] for iii, tdir in enumerate(dirs): try: fullPath = "%s/%s" % (gwacData, tdir) tdata00 = np.loadtxt(fullPath, dtype='str') if tdir.find('Field_') == 0 and tdata00.shape[0] > 10000: tNames.append(tdir) tDatas.append(tdata00.shape[0]) print("fName is %s, num is %d" % (tdir, tdata00.shape[0])) except Exception as e: print(str(e)) tstr = traceback.format_exc() print(tstr) skyMapName = {} skyMapData = {} for i in range(len(tNames)): tkey = tNames[i][8:18] if tkey not in skyMapName: skyMapName[tkey] = [] skyMapData[tkey] = [] skyMapName[tkey].append(tNames[i]) skyMapData[tkey].append(tDatas[i]) allSkyDist = [] for tkey in skyMapName: print("\n\n*****************") print("match sky %s" % (tkey)) tNames = skyMapName[tkey] tDatas = skyMapData[tkey] maxNumIdx = -1 maxNum = -1 for i in range(len(tNames)): if tDatas[i].shape[0] > maxNum: maxNum = tDatas[i].shape[0] maxNumIdx = i print("maxNum is %d, fName is %s" % (maxNum, tNames[maxNumIdx])) maxDist = 30.0 / 60 / 60 templateData = tDatas[maxNumIdx] hp = HEALPix(nside=256) tidxs = buildIdx(hp, templateData) allDists1 = [] for i in range(len(tNames)): tdata = tDatas[i] allDists2 = [] if i != maxNumIdx and tdata.shape[0] > 10000: for td in tdata: #print(td) ra1 = float(td[1]) dec1 = float(td[2]) hpixs = hp.cone_search_lonlat(ra1 * u.deg, dec1 * u.deg, radius=maxDist * u.deg) #print(hpixs) minDist = 100000 for ti in hpixs: tposs = tidxs[ti] #tidx[tpix].append((ra[j],dec[j],j)) for tpos in tposs: ra2 = tpos[0] dec2 = tpos[1] try: tdist = getGreatCircleDistance( ra1, dec1, ra2, dec2) if tdist <= minDist: minDist = tdist except Exception as e: print("domatch error") #print(str(e)) #tstr = traceback.format_exc() #print(tstr) if minDist <= maxDist: allDists2.append(minDist) print("fName is %s, has %d stars, match num is %d" % (tNames[i], tdata.shape[0], len(allDists2))) allDists1.append(allDists2) allSkyDist.append(allDists1) for j, tdists in enumerate(allSkyDist): maxNum = 0 maxIdx = -1 for i, tds in enumerate(tdists): if len(tds) > maxNum: maxNum = len(tds) maxIdx = i if maxIdx > -1: print((maxIdx, maxNum)) x = np.array(tdists[maxIdx]) * 3600 bins = np.arange(0, 30, 1) plt.hist(x, bins, color='fuchsia', alpha=0.5) plt.xlabel('distance') plt.ylabel('count') plt.grid() plt.xlim(0, 30) plt.show()
def _compute_graph_HEALPix_boundaries(depth, ipixels): def insert_edge(G, l1, l2, p1_lon, p1_lat, p2_lon, p2_lat): # Nodes are indexed by str(skycoord). When getting ordered nodes, one can retrieve back the skycoord instance # by accessing the python dict `pts_d`. try: # Avoid the special case where holes are touching to each other # 'x' belongs to the MOC # ' ' is part of the holes in the MOC # |xxx # |xxx # ---A--- # xxx| # xxx| # # If this case occurs we split the node A into 2. One is attached to the bottom left graph and the other to the # top right one. When computing the MST (minimal spanning tree) from a graph, we need our graphs to have # only nodes of degrees 1 or 2 (i.e. to be lines). if G.degree[l1] >= 2: l1 += '_' except: pass try: if G.degree[l2] >= 2: l2 += '_' except: pass # Set the skycoord instance as an attribute of the nodes G.add_node(l1, ra=p1_lon, dec=p1_lat) G.add_node(l2, ra=p2_lon, dec=p2_lat) G.add_edge(l1, l2) # Phase 1: Retrieve the ipixels located at the border of # this connexe MOC component ipixels = ipixels.astype(np.int) hp = HEALPix(nside=(1 << depth), order='nested', frame=ICRS()) neighbours = hp.neighbours(ipixels)[[0, 2, 4, 6], :] ipixels = ipixels.astype(np.uint64) neighbours = neighbours.astype(np.uint64) isin = np.isin(neighbours, ipixels) border = isin.sum(axis=0) < 4 ipixels_border = ipixels[border] isin_border = isin[:, border] # Phase 2: Build the graph from the positions of the ipixels boundaries #import cdshealpix as healpix #ipix_lon, ipix_lat = healpix.vertices(ipixels_border, depth) #ipix_lon[:, [1, 3]] = ipix_lon[:, [3, 1]] #ipix_lat[:, [1, 3]] = ipix_lat[:, [3, 1]] ipix_lon, ipix_lat = hp.boundaries_lonlat(ipixels_border, step=1) ipix_lon_deg = ipix_lon.to_value(u.deg) ipix_lat_deg = ipix_lat.to_value(u.deg) ipix_lon_repr = \ np.around(np.asarray(ipix_lon.reshape((1, -1))[0]), decimals=3).tolist() ipix_lat_repr = \ np.around(np.asarray(ipix_lat.reshape((1, -1))[0]), decimals=3).tolist() west_border = ~isin_border[0, :] south_border = ~isin_border[1, :] east_border = ~isin_border[2, :] north_border = ~isin_border[3, :] E = nx.Graph() for i in range(ipixels_border.shape[0]): lon_deg = ipix_lon_deg[i] lat_deg = ipix_lat_deg[i] p0_lon = lon_deg[0] p1_lon = lon_deg[1] p2_lon = lon_deg[2] p3_lon = lon_deg[3] p0_lat = lat_deg[0] p1_lat = lat_deg[1] p2_lat = lat_deg[2] p3_lat = lat_deg[3] off = 4*i off2 = 4*(i + 1) repr_lon = ipix_lon_repr[off:off2] repr_lat = ipix_lat_repr[off:off2] s0 = str(repr_lon[0]) + ' ' + str(repr_lat[0]) s1 = str(repr_lon[1]) + ' ' + str(repr_lat[1]) s2 = str(repr_lon[2]) + ' ' + str(repr_lat[2]) s3 = str(repr_lon[3]) + ' ' + str(repr_lat[3]) # WEST border if west_border[i]: insert_edge(E, s1, s2, p1_lon, p1_lat, p2_lon, p2_lat) # NORTH border if north_border[i]: insert_edge(E, s2, s3, p2_lon, p2_lat, p3_lon, p3_lat) # EAST border if east_border[i]: insert_edge(E, s3, s0, p3_lon, p3_lat, p0_lon, p0_lat) # SOUTH border if south_border[i]: insert_edge(E, s0, s1, p0_lon, p0_lat, p1_lon, p1_lat) return E
import os from astropy_healpix import HEALPix from astropy import units as u import sys from astropy.table import Table, vstack t1 = Table.read('Winter/final-v0.5_photoz_v0.1_joined-physical.fits') t2 = Table.read('Spring-40-45/final-v0.4_photoz_v0.1_north-physical.fits') t3 = Table.read('Spring-60-65/final-v0.8_photoz_v0.1-physical.fits') t4 = Table.read('Fall/final-v0.6_photoz_v0.1-physical.fits') t1['field'] = '8h' t2['field'] = '13h40' t3['field'] = '13h60' t4['field'] = '0h' hp = HEALPix(nside=256) t1_pixel = np.array( hp.lonlat_to_healpix(t1['RA'].quantity.value * u.deg, t1['DEC'].quantity.value * u.deg)) t2_pixel = np.array( hp.lonlat_to_healpix(t2['RA'].quantity.value * u.deg, t2['DEC'].quantity.value * u.deg)) t1_pixels = np.unique(t1_pixel) t2_pixels = np.unique(t2_pixel) overlap = np.intersect1d(t1_pixels, t2_pixels) from_t1 = np.setdiff1d(t1_pixels, overlap) # the ones not in overlap from_t2 = np.setdiff1d(t2_pixels, overlap)
def stack_npixels( npixels, level_neighbours=5, params=None, max_data=1000, calc_flux=True, use_flagged_pixels=False, skip_detections=False, custom_bkg=False, moc_masked_sources=None, order=16, with_plots=False, plotfile=None, scale=None, ): ecf_pn = { "6": ECF.ecf_det_eband("PN", "6"), "7": ECF.ecf_det_eband("PN", "7"), "8": ECF.ecf_det_eband("PN", "8"), } num_neighbours = sum([8 * k for k in range(level_neighbours + 1)]) + 1 ebands = ["6", "7", "8"] src_stack = np.zeros((max_data, num_neighbours, len(ebands))) bkg_stack = np.zeros((max_data, num_neighbours, len(ebands))) exp_stack = np.zeros((max_data, num_neighbours, len(ebands))) eef_stack = np.ones((max_data, num_neighbours, len(ebands))) ac_stack = np.zeros((max_data, num_neighbours, len(ebands))) npixels_bkg_stack = np.ones((max_data, num_neighbours, len(ebands))) ecf_stack = np.zeros((max_data, len(ebands))) if calc_flux: src_flux_center = np.full((max_data, len(ebands)), np.nan) bkg_flux_center = np.full((max_data, len(ebands)), np.nan) src_flux_err_center = np.full((max_data, len(ebands)), np.nan) bkg_flux_err_center = np.full((max_data, len(ebands)), np.nan) if params: params_stack = np.zeros((max_data, len(params.colnames))) hp = HEALPix(nside=2**order, order="nested", frame=FK5()) n, nsrc = 0, 0 for j, npixel in enumerate(tqdm(npixels)): sorted_neighbours = get_neighbours(npixel, hp, level=level_neighbours) data = rapidxmm.query_npixels(sorted_neighbours["npixel"], obstype="pointed", instrum="PN") if len(data) == 0: continue nsrc += 1 data = data.group_by(["obsid", "instrum"]) for group in data.groups: data_obs_order = join(sorted_neighbours, group, keys=["npixel"], join_type="left") data_obs_order.sort("order") if skip_detections: if np.any(data_obs_order["band8_flags"] >= 8): continue if custom_bkg: bkg_data = get_bkg_data(npixel, group["obsid"][0], hp) if bkg_data is None: # We couldn't find a good background region for this npixel, # so it's rejected from the stack continue for i, eband in enumerate(ebands): if use_flagged_pixels: mask = [True] * len(sorted_neighbours) else: mask = data_obs_order[f"band{eband}_flags"] == 0 src_stack[n, mask, i] = data_obs_order[f"band{eband}_src_counts"][mask] exp_stack[n, mask, i] = data_obs_order[f"band{eband}_exposure"][mask] eef_stack[n, mask, i] = data_obs_order["eef"][mask] ac_stack[n, mask, i] = data_obs_order["area_ratio"][mask] if custom_bkg: mask_bkg = bkg_data[f"band{eband}_flags"] == 0 # The same average bkg value is assigned to all npixels in the detection bkg_counts = bkg_data[f"band{eband}_bck_counts"][mask_bkg] bkg_stack[n, mask, i] = np.mean(bkg_counts) npixels_bkg_stack[n, mask, i] = len(bkg_counts) else: bkg_stack[ n, mask, i] = data_obs_order[f"band{eband}_bck_counts"][mask] if calc_flux and np.any(mask): ecf_stack[n, i] = ecf_pn[eband][group["filt"][0]].get_ecf( params["NHGAL"][j], 1.9) exp = np.mean(exp_stack[n, mask, i]) ngood = len(exp_stack[n, mask, i]) src_flux_center[n, i] = (np.sum(src_stack[n, mask, i]) / exp / ecf_stack[n, i] / 1e11 / ngood) src_flux_err_center[n, i] = ( np.sqrt(np.sum(src_stack[n, mask, i])) / exp / ecf_stack[n, i] / 1e11 / ngood) if custom_bkg: exp_bkg = np.mean( bkg_data[f"band{eband}_exposure"][mask_bkg]) ngood_bkg = len( bkg_data[f"band{eband}_exposure"][mask_bkg]) bkg_flux_center[n, i] = (np.sum(bkg_counts) / exp_bkg / ecf_stack[n, i] / 1e11 / ngood_bkg) bkg_flux_err_center[n, i] = (np.sqrt(np.sum(bkg_counts)) / exp_bkg / ecf_stack[n, i] / 1e11 / ngood_bkg) else: bkg_flux_center[n, i] = (np.sum(bkg_stack[n, mask, i]) / exp / ecf_stack[n, i] / 1e11 / ngood) bkg_flux_err_center[n, i] = ( np.sqrt(np.sum(bkg_stack[n, mask, i])) / exp / ecf_stack[n, i] / 1e11 / ngood) if params: for i, col in enumerate(params.colnames): params_stack[n, i] = params[col][j] n += 1 src_stack = src_stack[:n, :, :] bkg_stack = bkg_stack[:n, :, :] exp_stack = exp_stack[:n, :, :] ecf_stack = ecf_stack[:n, :] if custom_bkg: # No need to take into account the area correction when using custom # backgrounds, since counts are extracted in regions with the same size ac_stack = None npixels_bkg_stack = npixels_bkg_stack[:n, :] else: ac_stack = ac_stack[:n, :] npixels_bkg_stack = None if n < 2: return None, None, None, None, None, None, None cr, cr_mad, snr, snr_mad, ecf, texp = stats_bootstrap(src_stack, bkg_stack, exp_stack, eef_stack, ecf_stack, ac_stack, npixels_bkg_stack, nsim=1000) flux, flux_mad = None, None flux2, flux2_mad = None, None if calc_flux: src_flux_center = src_flux_center[:n, :] src_flux_err_center = src_flux_err_center[:n, :] bkg_flux_center = bkg_flux_center[:n, :] bkg_flux_err_center = bkg_flux_err_center[:n, :] flux, flux_mad = flux_bootstrap(src_flux_center, src_flux_err_center, bkg_flux_center, bkg_flux_err_center, nsim=1000) flux2 = np.mean(cr, axis=0) / ecf / 1e11 flux2_mad = np.sqrt(np.mean(cr_mad**2, axis=0)) / ecf / 1e11 if with_plots: scale = plot_stack(sorted_neighbours["npixel"], hp, cr, snr, plotfile, scale) plot_radial(sorted_neighbours["npixel"], level_neighbours, hp, cr, cr_mad, snr, snr_mad, plotfile) print_stats(cr, cr_mad, snr, snr_mad, texp, flux, flux_mad) if params: average_params = print_params(params.colnames, params_stack[:n, :]) else: average_params = None return flux, flux_mad, flux2, flux2_mad, average_params, scale, n, nsrc
def make_plot(args): """ Take the steps to make the plot. Parameters ---------- args: array-like Command line arguments Returns ------- Nothing """ basename = 'PMmap-qso-galactic-aberration' gx = 5.04 gy = -0.10 gz = -0.29 if args['quiver']: hplevel = 3 else: hplevel = 5 nside = hp.order2nside(hplevel) npix = hp.nside2npix(nside) ahp = HEALPix(nside=nside, order='nested', frame=Galactic()) hpindices = np.arange(npix) skycoords = ahp.healpix_to_skycoord(hpindices) pm_l_cosb = -gx * np.sin(skycoords.l.to(u.rad)) + gy * np.cos( skycoords.l.to(u.rad)) pm_b = -gx * np.sin(skycoords.b.to(u.rad)) * np.cos(skycoords.l.to(u.rad)) \ - gy * np.sin(skycoords.b.to(u.rad)) * np.sin(skycoords.l.to(u.rad)) \ + gz * np.cos(skycoords.b.to(u.rad)) pmtot = np.sqrt(pm_l_cosb**2 + pm_b**2) backgr = plt.imread( '../star-trail-animation/sky-images/GaiaSky-colour-2k.png') default_proj = ccrs.PlateCarree() sky_proj = ccrs.Mollweide() fig = plt.figure(figsize=(16, 9), dpi=120, frameon=False, tight_layout={'pad': 0.01}) gs = GridSpec(1, 1, figure=fig) ax = fig.add_subplot(gs[0, 0], projection=sky_proj) ax.imshow(np.fliplr(backgr), transform=default_proj, zorder=-1, origin='upper') veccolor = plt.cm.get_cmap('tab10').colors[9] linecolor = plt.cm.get_cmap('tab10').colors[9] if args['quiver']: vscale = np.median(pmtot) / 50 ax.quiver(skycoords.l.value, skycoords.b.value, pm_l_cosb, pm_b, transform=default_proj, angles='xy', scale=vscale, scale_units='dots', color=veccolor, headwidth=4, headlength=4, headaxislength=3.5) else: if args['colourstreams']: ax.streamplot(skycoords.l.value, skycoords.b.value, pm_l_cosb, pm_b, transform=default_proj, linewidth=2.0, density=2, color=pmtot, cmap='viridis', maxlength=0.5, arrowsize=1, arrowstyle=ArrowStyle.Fancy(head_length=1.0, head_width=.4, tail_width=.4)) elif args['lwcode'] > 0: ax.streamplot(skycoords.l.value, skycoords.b.value, pm_l_cosb, pm_b, transform=default_proj, linewidth=args['lwcode'] * pmtot / np.median(pmtot), density=2, color=linecolor, maxlength=0.5, arrowsize=1, arrowstyle=ArrowStyle.Fancy(head_length=1.0, head_width=.4, tail_width=.4)) else: ax.streamplot(skycoords.l.value, skycoords.b.value, pm_l_cosb, pm_b, transform=default_proj, linewidth=1.5, density=2.5, color=linecolor, maxlength=0.5, arrowsize=1, arrowstyle=ArrowStyle.Fancy(head_length=1.0, head_width=.4, tail_width=.4)) # ax.gridlines() ax.invert_xaxis() if args['pdfOutput']: plt.savefig(basename + '.pdf') elif args['pngOutput']: plt.savefig(basename + '.png') else: plt.show()
def from_image(cls, header, max_norder, mask=None): """ Creates a `~mocpy.moc.MOC` from an image stored as a FITS file. Parameters ---------- header : `astropy.io.fits.Header` FITS header containing all the info of where the image is located (position, size, etc...) max_norder : int The moc resolution. mask : `numpy.ndarray`, optional A boolean array of the same size of the image where pixels having the value 1 are part of the final MOC and pixels having the value 0 are not. Returns ------- moc : `~mocpy.moc.MOC` The resulting MOC. """ # load the image data height = header['NAXIS2'] width = header['NAXIS1'] # use wcs from astropy to locate the image in the world coordinates w = wcs.WCS(header) if mask is not None: # We have an array of pixels that are part of of survey y, x = np.where(mask) pix_crd = np.dstack((x, y))[0] else: # If we do not have a mask array we create the moc of all the image # step_pix = 1 """ Coords returned by wcs_pix2world method correspond to pixel centers. We want to retrieve the moc pix crossing the borders of the image so we have to add 1/2 to the pixels coords before computing the lonlat. The step between two pix_crd is set to `step_pix` but can be diminished to have a better precision at the borders so that all the image is covered (a too big step does not retrieve all the moc pix crossing the borders of the image). """ x, y = np.mgrid[0.5:(width + 0.5 + step_pix):step_pix, 0.5:(height + 0.5 + step_pix):step_pix] pix_crd = np.dstack((x.ravel(), y.ravel()))[0] frame = wcs.utils.wcs_to_celestial_frame(w) world_pix_crd = SkyCoord(w.wcs_pix2world(pix_crd, 1), unit='deg', frame=frame) max_norder = np.uint8(max_norder) hp = HEALPix(nside=(1 << max_norder), order='nested', frame=ICRS()) ipix = hp.skycoord_to_healpix(world_pix_crd) ipix = ipix.astype(np.uint64) # remove doubles ipix = np.unique(ipix) shift = np.uint8(2) * (AbstractMOC.HPY_MAX_NORDER - max_norder) intervals_arr = np.vstack((ipix << shift, (ipix + np.uint64(1)) << shift)).T # This MOC will be consistent when one will do operations on the moc (union, inter, ...) or # simply write it to a fits or json file interval_set = IntervalSet(intervals_arr) return cls(interval_set=interval_set)
def plot(self, title='MOC', frame=None): """ Plot the MOC object using a mollweide projection. **Deprecated**: New `fill` and `border` methods produce more reliable results and allow you to specify additional matplotlib style parameters. Parameters ---------- title : str The title of the plot frame : `astropy.coordinates.BaseCoordinateFrame`, optional Describes the coordinate system the plot will be (ICRS, Galactic are the only coordinate systems supported). """ import warnings warnings.simplefilter('default') warnings.warn( 'This method is deprecated and is no longer tested.' 'Please refer to this documentation page for plotting MOCs using' 'matplotlib: https://mocpy.readthedocs.io/en/latest/examples/examples.html#loading-and-plotting-the-moc-of-sdss', DeprecationWarning) frame = ICRS() if frame is None else frame from matplotlib.colors import LinearSegmentedColormap import matplotlib.pyplot as plt plot_order = 8 if self.max_order > plot_order: plotted_moc = self.degrade_to_order(plot_order) else: plotted_moc = self num_pixels_map = 1024 delta = 2. * np.pi / num_pixels_map x = np.arange(-np.pi, np.pi, delta) y = np.arange(-np.pi / 2, np.pi / 2, delta) lon_rad, lat_rad = np.meshgrid(x, y) hp = HEALPix(nside=(1 << plotted_moc.max_order), order='nested') if frame and not isinstance(frame, BaseCoordinateFrame): raise ValueError( "Only Galactic/ICRS coordinate systems are supported." "Please set `coord` to either 'C' or 'G'.") pix_map = hp.lonlat_to_healpix(lon_rad * u.rad, lat_rad * u.rad) m = np.zeros(12 * 4**(plotted_moc.max_order)) pix_id = mocpy.flatten_pixels(plotted_moc._interval_set._intervals, plotted_moc.max_order) # change the HEALPix cells if the frame of the MOC is not the same as the one associated with the plot method. if isinstance(frame, Galactic): lon, lat = hp.boundaries_lonlat(pix_id, step=2) sky_crd = SkyCoord(lon, lat, unit='deg') pix_id = hp.lonlat_to_healpix(sky_crd.galactic.l, sky_crd.galactic.b) m[pix_id] = 1 z = np.flip(m[pix_map], axis=1) plt.figure(figsize=(10, 10)) ax = plt.subplot(111, projection="mollweide") ax.set_xticklabels([ '150°', '120°', '90°', '60°', '30°', '0°', '330°', '300°', '270°', '240°', '210°', '180°' ]) color_map = LinearSegmentedColormap.from_list('w2r', ['#eeeeee', '#aa0000']) color_map.set_under('w') color_map.set_bad('gray') ax.pcolormesh(x, y, z, cmap=color_map, vmin=0, vmax=1) ax.tick_params(labelsize=14, labelcolor='#000000') plt.title(title) plt.grid(True, linestyle='--', linewidth=1, color='#555555') plt.show()
def tmatch(): maxDist = 3.0 / 60 / 60 hp = HEALPix(nside=512) tpath = '/data/work/hjtian/QSOs_NINA.txt' tdata00 = np.loadtxt(tpath, dtype='str') #, delimiter=' ' print(tdata00.shape) raDec00 = tdata00[:, 0:2].astype(np.float) tdata00Match = [] for i in range(raDec00.shape[0]): tdata00Match.append([]) root = '/data/work/hjtian/crossmatch20200118' destDir = '/data/work/hjtian/crossmatch20200120Rematch' if not os.path.exists(destDir): os.system("mkdir -p %s" % (destDir)) dirs = os.listdir(root) tdata = np.array([]) for iii, tdir in enumerate(dirs): tdataPath = "%s/%s" % (root, tdir) print(tdataPath) tds = pd.read_csv(tdataPath).to_numpy() if tdata.shape[0] == 0: tdata = tds elif tdata.shape[0] > 0: tdata = np.concatenate([tdata, tds]) print(tdata.shape) tidxHealpix = getIdx(tdata, hp) try: for i in range(raDec00.shape[0]): ra00 = raDec00[i, 0] dec00 = raDec00[i, 1] if i % 100000 == 0: print(i) hpixs = hp.cone_search_lonlat(ra00 * u.deg, dec00 * u.deg, radius=maxDist * u.deg) minDist = maxDist tmchIdx = -1 for ti in hpixs: tposs = tidxHealpix[ti] for tpos in tposs: ra2 = tpos[0] dec2 = tpos[1] tdist = getGreatCircleDistance(ra00, dec00, ra2, dec2) if tdist <= minDist: #print("ra1=%f,dec1=%f, ra2=%f,dec2=%f, tdist=%f, maxDist=%f"%(ra00,dec00, ra2,dec2, tdist, maxDist)) minDist = tdist tmchIdx = tpos[2] if tmchIdx > -1: if len(tdata00Match[i]) > 0: if tdata00Match[i][0] > minDist: tm = tdata00Match[i][0] tm[0] = minDist tm[1] = tdata[tmchIdx] #tdata00Match[i].append([minDist,tdata[tmchIdx]]) else: tdata00Match[i].append([minDist, tdata[tmchIdx]]) #break except Exception as e: print(str(e)) tstr = traceback.format_exc() print(tstr) #break rst1 = [] rst2 = [] for i in range(len(tdata00Match)): if len(tdata00Match[i]) > 0: #print(tdata00Match[i]) rst1.append(tdata00[i]) rst2.append(tdata00Match[i][0][1]) if len(rst1) > 0: destDir = '/data/work/hjtian/crossmatch20200120all' if not os.path.exists(destDir): os.system("mkdir -p %s" % (destDir)) savePath = '%s/data1_%d.csv' % (destDir, len(rst1)) print(savePath) GPS1_df = pd.DataFrame(rst1) GPS1_df.to_csv(savePath, index=False) savePath = '%s/data2_%d.csv' % (destDir, len(rst2)) print(savePath) GPS1_df = pd.DataFrame(rst2) GPS1_df.to_csv(savePath, index=False)
break # Simulating a map that is all black everywhere except for # one bright spot bright_spot = 75 for i in range(0, one_point_map.size): if i == bright_spot: one_point_map[i] = 1.00 else: one_point_map[i] = 0.00 # Viewing the map # hp.mollview(one_point_map, title='One bright point', cmap='gist_gray') # Obtaining the latitude and longitude of this bright point map = HEALPix(nside=nside, order='nested') pt_lng, pt_lat = map.healpix_to_lonlat([bright_spot]) # Assigning these points to the input parameters lat = pt_lat lng = pt_lng # =============NUMERICAL LIGHTCURVE============================= # Setting the orbital properties # the unit here will be hours p_rotation = 23.934 p_orbit = 365.256363 * 24.0 # the unit here will be rotations
import pandas as pd from astropy_healpix import HEALPix from astropy.coordinates import ICRS, SkyCoord from astropy import units as u #importing the master GLADE file df_master = pd.read_csv( "Data Files/GLADE_Master_comma_100Mpc.csv", delimiter=",", low_memory=False) ##GLADE_Master.csv previously defined #grabbing just the coordinates of each galaxy ra, dec = df_master["RA"], df_master["dec"] #using HEALPix to split the celestial sky into equal area sectors hp = HEALPix(nside=16, order='ring', frame=ICRS()) #making a holding array to hold the sectors for each galaxy hold = np.array([]) for i in range(len(ra)): ''' This loops over all the galaxies, at each one it takes the galaxies coordinates and uses skycoord_to_healpix to find what HEALPix sector that galaxies belongs to, the result is then appended to the hold array ''' coords = SkyCoord(ra[i], dec[i], unit="deg") sector = hp.skycoord_to_healpix(coords) hold = np.append(hold, sector) #adding the sector identifyier data to the master file
def crossMatchStatistics(): gwacData = '/data/work/wj/dat' dirs = os.listdir(gwacData) dirs.sort() tNames = [] tDatas = [] obsNum = 0 for iii, tdir in enumerate(dirs): try: if tdir.find('21450595') > 0: obsNum = obsNum + 1 fullPath = "%s/%s" % (gwacData, tdir) tdata00 = np.loadtxt(fullPath, dtype='str') if tdata00.shape[0] > 10000: tNames.append(tdir) tDatas.append(tdata00) print("fName is %s, num is %d" % (tdir, tdata00.shape[0])) except Exception as e: print(str(e)) tstr = traceback.format_exc() print(tstr) print("total file is %d, large 10000 is %s" % (obsNum, len(tNames))) maxDist = 30 / 60 / 60 hp = HEALPix(nside=256) for j in range(len(tNames)): print("\n\n*****************") print("start match %d file: %s, has %d stars" % (j + 1, tNames[j], tDatas[j].shape[0])) templateData = tDatas[j] tidxs = buildIdx(hp, templateData) mchList = [] for i in range(len(tNames)): tdata = tDatas[i] if i != j: tnum00 = 0 for td in tdata: ra1 = float(td[1]) dec1 = float(td[2]) hpixs = hp.cone_search_lonlat(ra1 * u.deg, dec1 * u.deg, radius=maxDist * u.deg) isMatch = False for ti in hpixs: tposs = tidxs[ti] for tpos in tposs: ra2 = tpos[0] dec2 = tpos[1] try: tdist = getGreatCircleDistance( ra1, dec1, ra2, dec2) if tdist <= maxDist: isMatch = True tnum00 = tnum00 + 1 if tnum00 < 5: print( "ra1=%f,dec1=%f,ra2=%f,dec2=%f,dist=%f" % (ra1, dec1, ra2, dec2, tdist)) break except Exception as e: print("domatch error") if isMatch: break print("%d, fName is %s, has %d stars, match num is %d" % (i + 1, tNames[i], tdata.shape[0], tnum00)) if tnum00 > 1000: mchList.append([i, tnum00]) print(mchList)
def __init__(self, ra, dec, inside=None, max_depth=10): ra = ra.to(u.rad).value dec = dec.to(u.rad).value # Check if the vertices form a closed polygon if ra[0] != ra[-1] or dec[0] != dec[-1]: # If not, append the first vertex to ``vertices`` ra = np.append(ra, ra[0]) dec = np.append(dec, dec[0]) vertices = SkyCoord(ra=ra, dec=dec, unit="rad", frame="icrs") if inside: # Convert it to (x, y, z) cartesian coordinates on the sphere inside = (inside.icrs.ra.rad, inside.icrs.dec.rad) self.polygon = SphericalPolygon.from_lonlat(lon=ra, lat=dec, center=inside, degrees=False) start_depth, ipixels = self._get_starting_depth() end_depth = max_depth # When the start depth returned is > to the depth requested # For that specific case, we only do one iteration at start_depth # Thus the MOC will contain the partially intersecting cells with the # contained ones at start_depth # And we degrade the MOC to the max_depth self.degrade_to_max_depth = False if start_depth > end_depth: end_depth = start_depth self.degrade_to_max_depth = True self.ipix_d = {str(order): [] for order in range(start_depth, end_depth + 1)} ## Iterative version of the algorithm: seems a bit faster than the recursive one for depth in range(start_depth, end_depth + 1): # Define a HEALPix at the current depth hp = HEALPix(nside=(1 << depth), order='nested', frame=ICRS()) # Get the lon and lat of the corners of the pixels # intersecting the polygon lon, lat = hp.boundaries_lonlat(ipixels, step=1) lon = lon.to(u.rad).value lat = lat.to(u.rad).value # closes the lon and lat array so that their first and last value matches lon = self._closes_numpy_2d_array(lon) lat = self._closes_numpy_2d_array(lat) num_ipix_inter_poly = ipixels.shape[0] # Define a 3d numpy array containing the corners coordinates of the intersecting pixels # The first dim is the num of ipixels # The second is the number of coordinates (5 as it defines the closed polygon of a HEALPix cell) # The last is of size 2 (lon and lat) shapes = np.vstack((lon.ravel(), lat.ravel())).T.reshape(num_ipix_inter_poly, 5, -1) ipix_in_polygon_l = [] ipix_inter_polygon_l = [] for i in range(num_ipix_inter_poly): shape = shapes[i] # Definition of a SphericalPolygon from the border coordinates of a HEALPix cell ipix_shape = SphericalPolygon.from_radec(lon=shape[:, 0], lat=shape[:, 1], degrees=False) ipix = ipixels[i] if self.polygon.intersects_poly(ipix_shape): # If we are at the max depth then we direcly add to the MOC the intersecting ipixels if depth == end_depth: ipix_in_polygon_l.append(ipix) else: # Check whether polygon contains ipix or not if self.polygon_contains_ipix(ipix_shape): ipix_in_polygon_l.append(ipix) else: # The ipix is just intersecting without being contained in the polygon # We split it in its 4 children child_ipix = ipix << 2 ipix_inter_polygon_l.extend([child_ipix, child_ipix + 1, child_ipix + 2, child_ipix + 3]) self.ipix_d.update({str(depth): ipix_in_polygon_l}) ipixels = np.asarray(ipix_inter_polygon_l)
def get_cat(method,retries=100): cwd=os.getcwd() try: os.mkdir(method) except OSError: pass if method=='pslocal': hplist=[] if method=='wise': from astroquery.irsa import Irsa Irsa.ROW_LIMIT=1000000 ra_factor,pos=tile(cwd+'/image_ampphase1.app.restored.fits') print 'Downloading catalogues for',len(pos),'sky positions' for i,p in enumerate(pos): outfile=method+'/'+method+'-'+str(i)+'.vo' if os.path.isfile(outfile): print 'Catalogue at position',p,'already present' continue print 'Downloading at position',p if method=='panstarrs': count=0 while True: try: r = requests.post('http://archive.stsci.edu/panstarrs/search.php', data = {'ra':p[0],'dec':p[1],'SR':CSIZE,'max_records':100000,'nDetections':">+5",'action':'Search','selectedColumnsCsv':'objid,ramean,decmean'},timeout=300) except requests.exceptions.Timeout: print 'Timeout, retrying!' else: if 'Warning' not in r.text and 'Please' not in r.text: break else: # will go round the loop again print 'Bad response, retry download (%i)' % count sleep(5+count*15) count+=1 if count>=retries: raise RuntimeError('Number of retries exceeded for download') f=open(outfile,'w') f.writelines(r.text) f.close() elif method=='wise': t=Irsa.query_region(coord.SkyCoord(p[0],p[1],unit=(u.deg,u.deg)), catalog='allwise_p3as_psd', radius='0d30m0s') t.write(outfile,format='votable') elif method=='pslocal': from astropy_healpix import HEALPix hp = HEALPix(nside=64) cs = hp.cone_search_lonlat(p[0]*u.deg, p[1]*u.deg, radius=CSIZE*u.deg) hplist += list(cs) if not os.path.isdir(PSBASE): # we don't have a local PS database, so download for pix in cs: outfile=method+'/'+str(pix) if not os.path.isfile(outfile): print 'Downloading healpix pixel',pix download_file('http://uhhpc.herts.ac.uk/panstarrs-healpix/'+str(pix),outfile) else: raise NotImplementedError('Method '+method) if method=='pslocal': hplist=list(set(hplist)) print 'Found',len(hplist),'unique healpix pixels' outname=method+'/'+method+'.txt' with open(outname,'w') as outfile: outfile.write('# RA DEC ObjID\n') for pixel in hplist: print 'Appending pixel',pixel if os.path.isdir(PSBASE): pixelfile=PSBASE+'/'+str(pixel) else: pixelfile=method+'/'+str(pixel) if not os.path.isfile(pixelfile): raise RuntimeError('Pixel file '+pixelfile+'does not exist') os.system('cat '+pixelfile+' >> '+outname)
def _get_starting_depth(self): def compute_angular_distance(n1, n2): return np.arctan(np.linalg.norm(np.cross(n1, n2))/np.dot(n1, n2)) def to_xyz(lon, lat): x = np.cos(lon) * np.cos(lat) y = np.cos(lat) * np.sin(lon) z = np.sin(lat) return np.array([x, y, z], dtype=np.float64) def max_distance_center_to_vertex(depth): nside = 1 << depth lat1 = np.arcsin(2 / 3.0) lat2 = np.arcsin(1 - ((1 - 1.0/nside)**2 / 3.0)) lon1 = np.pi/(4 * nside) lon2 = 0 # Convert lon, lat to xyz, vector n1 = to_xyz(lon=lon1, lat=lat1) n2 = to_xyz(lon=lon2, lat=lat2) dist = compute_angular_distance(n1, n2) return dist # in rad # Get the polygon vertices as a Nx3 numpy array # Remove the last vertex as it counts double (closed polygon) p_vertices = np.asarray(list(self.polygon.points))[0][:-1, :] # Get the center formed by the vertices center = p_vertices.mean(axis=0) # Normalize it so that it lies on the unit sphere vector.normalize_vector(center, output=center) center = np.asarray(center) # Compute the maximum angular distance between the polygon vertices # and its center. This refers to the Vector version of the # Great-circle distance computation. # # See https://en.wikipedia.org/wiki/Great-circle_distance max_d = -1 # Check if the polygon covers more than an hemisphere covers_more_than_one_hemisphere = (self.polygon.area() > 2 * np.pi) for vertex in p_vertices: d = compute_angular_distance(center, vertex) if covers_more_than_one_hemisphere: d = np.pi - d if d > max_d: max_d = d # Return the min depth so that max_d > max_center_to_vertex_ipix(depth) depth = 0 while max_distance_center_to_vertex(depth) >= max_d: depth = depth + 1 # Get the ipixels from astropy_healpix covering the cone of (center, radius) = (center, max_d) lon_center, lat_center = vector.vector_to_lonlat(x=center[0], y=center[1], z=center[2], degrees=False) hp = HEALPix(nside=(1 << depth), order='nested', frame=ICRS()) starting_iter_ipix = hp.cone_search_lonlat(lon=lon_center * u.rad, lat=lat_center * u.rad, radius=max_d * u.rad) return depth, starting_iter_ipix
# Delta Maps cos_phi_phi_s = np.cos(phi) * cos_phi_s + np.sin(phi) * sin_phi_s cos_phi_phi_knot = np.cos(phi) * cos_phi_knot + np.sin(phi) * sin_phi_knot # (4) Inz = np.sin(theta) * sin_theta_s * cos_phi_phi_s + np.cos( theta) * cos_theta_s return np.maximum(0, Inz) # Setting the orbital properties for the numeric solution nside = 4 # From the numeric solution: exocartographer uses order = ring. order = 'ring' map = HEALPix(nside=nside, order=order) # The unit here will be hours p_rotation = 23.934 p_orbit = 365.256363 * 24.0 # Setting the inputs up for the analytic and numeric solutions # Creating an evenly-spaced time array containing num # of points times = np.linspace(start=0.0, stop=24.0, num=1400) measurement_std = 0.001 # Setting the input parameters w_rot = 2 * np.pi / p_rotation w_orb = 2 * np.pi / p_orbit inclination = np.pi / 3
def read_in_fits(filename, center, ref_head, ref_pixsize=8, ref_mapsize=260): ''' Purpose : This function reads in the fits files for the components and parses them so that we are left with data only for our field. Inputs: filename (str) - the singular filename for a component used in the MBB fit center (float array) - center of the field of interest ref_head (Astropy.header) - header for the reference field ref_pixsize - pixel size of the reference map ref_mapsize - mapsize of the reference map Outputs: map (float array) - an array of flux values at the given field pixsize (float) - pixel size of the uninterpolated component maps x_side (int) - the length of the x-axis of the map y_side (int) - the length of the y-axis of the map RA_grid (float array) - grid of the Right Ascension values used to pull out components DEC_grid (float array) - grid of the Declination values used to pull out components ''' hdul = fits.open(filename) head = hdul[1].header if 'Temperature' in filename: data = hdul[1].data.field('TEMP') error = hdul[1].data.field('ERR_TEMP') elif 'Spectral-Index' in filename: data = hdul[1].data.field('BETA') error = hdul[1].data.field('ERR_BETA') elif 'Opacity' in filename: data = hdul[1].data.field('TAU353') error = hdul[1].data.field('ERR_TAU') else: data = hdul[1].data.field(0) nside = head['NSIDE'] order = head['ORDERING'] hdul.close() #Galactic Coordinate System hp = HEALPix(nside=nside, order=order, frame='galactic') #create a pixel grid in terms of the nu=353 grid for GNILC to create our intensity maps pixsize = hp.pixel_resolution.to(u.arcsecond).value #* 10 is for boosting the size of the map so to fix edge effects from interpolation map_arc_x = ref_mapsize[0] * 2 * ref_pixsize #map size in arcseconds map_arc_y = ref_mapsize[1] * 2 * ref_pixsize npixxside = ceil(map_arc_x / pixsize) #convert to map size in pixels for nu = 353 map. npixyside = ceil(map_arc_y / pixsize) #* 10 is for boosting the size of the map so to fix edge effects from interpolation x = np.linspace(0, ref_mapsize[0] * 2, npixxside) y = np.linspace(0, ref_mapsize[1] * 2, npixyside) X, Y = np.meshgrid(x, y) w = world(ref_head) skycoords = pixel_to_skycoord(X.ravel(), Y.ravel(), wcs=w, origin=0) RA_grid = np.asarray(skycoords.ra.to_string(decimal=True), dtype='float') * u.deg DEC_grid = np.asarray(skycoords.dec.to_string(decimal=True), dtype='float') * u.deg # coords = SkyCoord(RA_grid.ravel(), DEC_grid.ravel(), frame='icrs') coords = SkyCoord(ra=RA_grid.ravel(), dec=DEC_grid.ravel(), frame='icrs') gal_coords = coords.galactic map = hp.interpolate_bilinear_skycoord(gal_coords, data) x_side = len(x) y_side = len(y) return map, pixsize, y_side, x_side, RA_grid, DEC_grid
def image_to_healpix(data, wcs_in, coord_system_out, nside, order='bilinear', nested=False): """ Convert image in a normal WCS projection to HEALPIX format. Parameters ---------- data : `numpy.ndarray` Input data array to reproject wcs_in : `~astropy.wcs.WCS` The WCS of the input array coord_system_out : str or `~astropy.coordinates.BaseCoordinateFrame` The target coordinate system for the HEALPIX projection, as an Astropy coordinate frame or corresponding string alias (e.g. ``'icrs'`` or ``'galactic'``) order : int or str, optional The order of the interpolation (if ``mode`` is set to ``'interpolation'``). This can be either one of the following strings: * 'nearest-neighbor' * 'bilinear' * 'biquadratic' * 'bicubic' or an integer. A value of ``0`` indicates nearest neighbor interpolation. nested : bool The order of the healpix_data, either nested or ring. Stored in FITS headers in the ORDERING keyword. Returns ------- reprojected_data : `numpy.ndarray` A HEALPIX array of values footprint : `~numpy.ndarray` Footprint of the input array in the output array. Values of 0 indicate no coverage or valid values in the input image, while values of 1 indicate valid values. """ from scipy.ndimage import map_coordinates hp = HEALPix(nside=nside, order='nested' if nested else 'ring') npix = hp.npix # Look up lon, lat of pixels in output system and convert colatitude theta # and longitude phi to longitude and latitude. lon_out, lat_out = hp.healpix_to_lonlat(np.arange(npix)) lon_out = lon_out.to(u.deg).value lat_out = lat_out.to(u.deg).value # Convert between celestial coordinates coord_system_out = parse_coord_system(coord_system_out) with np.errstate(invalid='ignore'): lon_in, lat_in = convert_world_coordinates(lon_out, lat_out, (coord_system_out, u.deg, u.deg), wcs_in) # Look up pixels in input system yinds, xinds = wcs_in.wcs_world2pix(lon_in, lat_in, 0) # Interpolate if isinstance(order, six.string_types): order = ORDER[order] healpix_data = map_coordinates(data, [xinds, yinds], order=order, mode='constant', cval=np.nan) return healpix_data, (~np.isnan(healpix_data)).astype(float)
def plot(self, title='MOC', frame=None): """ Plot the MOC object using a mollweide projection. **Deprecated**: New `fill` and `border` methods produce more reliable results and allow you to specify additional matplotlib style parameters. Parameters ---------- title : str The title of the plot frame : `astropy.coordinates.BaseCoordinateFrame`, optional Describes the coordinate system the plot will be (ICRS, Galactic are the only coordinate systems supported). """ frame = ICRS() if frame is None else frame from matplotlib.colors import LinearSegmentedColormap import matplotlib.pyplot as plt plot_order = 8 if self.max_order > plot_order: plotted_moc = self.degrade_to_order(plot_order) else: plotted_moc = self num_pixels_map = 1024 delta = 2. * np.pi / num_pixels_map x = np.arange(-np.pi, np.pi, delta) y = np.arange(-np.pi/2, np.pi/2, delta) lon_rad, lat_rad = np.meshgrid(x, y) hp = HEALPix(nside=(1 << plotted_moc.max_order), order='nested') if frame and not isinstance(frame, BaseCoordinateFrame): raise ValueError("Only Galactic/ICRS coordinate systems are supported." "Please set `coord` to either 'C' or 'G'.") pix_map = hp.lonlat_to_healpix(lon_rad * u.rad, lat_rad * u.rad) m = np.zeros(nside2npix(1 << plotted_moc.max_order)) pix_id = plotted_moc._best_res_pixels() # change the HEALPix cells if the frame of the MOC is not the same as the one associated with the plot method. if isinstance(frame, Galactic): lon, lat = hp.boundaries_lonlat(pix_id, step=2) sky_crd = SkyCoord(lon, lat, unit='deg') pix_id = hp.lonlat_to_healpix(sky_crd.galactic.l, sky_crd.galactic.b) m[pix_id] = 1 z = np.flip(m[pix_map], axis=1) plt.figure(figsize=(10, 10)) ax = plt.subplot(111, projection="mollweide") ax.set_xticklabels(['150°', '120°', '90°', '60°', '30°', '0°', '330°', '300°', '270°', '240°', '210°', '180°']) color_map = LinearSegmentedColormap.from_list('w2r', ['#eeeeee', '#aa0000']) color_map.set_under('w') color_map.set_bad('gray') ax.pcolormesh(x, y, z, cmap=color_map, vmin=0, vmax=1) ax.tick_params(labelsize=14, labelcolor='#000000') plt.title(title) plt.grid(True, linestyle='--', linewidth=1, color='#555555') plt.show()
from astropy import units as u import sys pos=np.loadtxt(os.environ['DDF_DIR']+'/ddf-pipeline/misc/DR2-pointings.txt',usecols=(1,2)) if len(sys.argv)==1: pos=pos[(pos[:,0]>137) & (pos[:,0]<250)] print 'Number of pointings is',len(pos) ra=pos[:,0] dec=pos[:,1] print 'RA range is',np.min(ra),np.max(ra) print 'Dec range is',np.min(dec),np.max(dec) hp = HEALPix(nside=1024) print hp.npix,'total healpix pixels on sky' area=hp.pixel_area.value*3283 print 'area of one healpix is',area,'sq. deg' plist=[] for i in range(len(ra)): pixels=hp.cone_search_lonlat(ra[i]*u.deg,dec[i]*u.deg,1.85*u.deg) plist=plist+list(pixels) print len(plist),'total pixels' print len(set(plist)),'total distinct pixels' print 'Area is',len(set(plist))*area,'sq. deg'
def test_rges_survey_region(): survey = galBulgeRubinRomanMetrics.RGESSurvey() ahp = HEALPix(nside=64, order='ring', frame=TETE()) survey.calcHealpix(ahp) assert len(survey.pixels > 0)
def get_pix(self, gl, gb): hp = HEALPix(nside=self.meta["NSIDE"], order=self.meta["ORDERING"]) return (hp.lonlat_to_healpix(gl * u.deg, gb * u.deg))
def healpix_to_image(healpix_data, coord_system_in, wcs_out, shape_out, order='bilinear', nested=False): """ Convert image in HEALPIX format to a normal FITS projection image (e.g. CAR or AIT). Parameters ---------- healpix_data : `numpy.ndarray` HEALPIX data array coord_system_in : str or `~astropy.coordinates.BaseCoordinateFrame` The coordinate system for the input HEALPIX data, as an Astropy coordinate frame or corresponding string alias (e.g. ``'icrs'`` or ``'galactic'``) wcs_out : `~astropy.wcs.WCS` The WCS of the output array shape_out : tuple The shape of the output array order : int or str, optional The order of the interpolation (if ``mode`` is set to ``'interpolation'``). This can be either one of the following strings: * 'nearest-neighbor' * 'bilinear' or an integer. A value of ``0`` indicates nearest neighbor interpolation. nested : bool The order of the healpix_data, either nested or ring. Stored in FITS headers in the ORDERING keyword. Returns ------- reprojected_data : `numpy.ndarray` HEALPIX image resampled onto the reference image footprint : `~numpy.ndarray` Footprint of the input array in the output array. Values of 0 indicate no coverage or valid values in the input image, while values of 1 indicate valid values. """ healpix_data = np.asarray(healpix_data, dtype=float) # Look up lon, lat of pixels in reference system yinds, xinds = np.indices(shape_out) lon_out, lat_out = wcs_out.wcs_pix2world(xinds, yinds, 0) # Convert between celestial coordinates coord_system_in = parse_coord_system(coord_system_in) with np.errstate(invalid='ignore'): lon_in, lat_in = convert_world_coordinates(lon_out, lat_out, wcs_out, (coord_system_in, u.deg, u.deg)) lon_in = u.Quantity(lon_in, unit=u.deg, copy=False) lat_in = u.Quantity(lat_in, unit=u.deg, copy=False) if isinstance(order, six.string_types): order = ORDER[order] nside = npix_to_nside(len(healpix_data)) hp = HEALPix(nside=nside, order='nested' if nested else 'ring') if order == 1: data = hp.interpolate_bilinear_lonlat(lon_in, lat_in, healpix_data) elif order == 0: ipix = hp.lonlat_to_healpix(lon_in, lat_in) data = healpix_data[ipix] else: raise ValueError("Only nearest-neighbor and bilinear interpolation are supported") footprint = np.ones(data.shape, bool) return data, footprint
return list(thishp[0][filt]) else: return [] def slice_multi(hpv): filt = (hps == hpv) return (names[filt], optras[filt], optdecs[filt]) print('*** process_overlap starting ***') with open('optical.pickle') as pf: (names, optras, optdecs) = pickle.load(pf) hd = {} hp = HEALPix(nside=512) hps = hp.lonlat_to_healpix(optras * u.deg, optdecs * u.deg) p = Pool(cpu_count()) hpl = list(set(hps)) for i, result in enumerate( tqdm(p.imap(slice_multi, hpl), total=len(hpl), desc='Make dictionary')): hd[hpl[i]] = result p.close() del (p) p = Pool(48) badlist = [] for result in tqdm(p.imap(select_multi, range(len(optras))),