def return_fiber_ratio(det): fiber_row = fiber_table.read_where('detectid == det') weights = np.sort(fiber_row['weight']) fiber_ratio = weights[-1] / weights[-2] return fiber_ratio version = str(sys.argv[1]) config = HDRconfig() # Note because refine is constantly updated, it isn't possible to # truly replicate older catalogs. TODO for HDR3 detects = Detections(survey='hdr2.1', catalog_type='lines').refine() sel_field = (detects.field == 'cosmos') | (detects.field == 'dex-fall') | ( detects.field == 'dex-spring') | (detects.field == 'egs') | (detects.field == 'goods-n') if version == '2.1.1': sel_chi2 = detects.chi2 < 1.2 sel_wave = (detects.wave >= 3510) * (detects.wave <= 5490) sel_lw = (detects.linewidth <= 6) sel_cont = detects.continuum > -3 sel_sn = detects.sn >= 4.8 sel_chi2fib = (detects.chi2fib < 4.5) sel_cat = sel_field * sel_chi2 * sel_wave * sel_lw * sel_cont * sel_sn * sel_chi2fib
def create_source_catalog(version="2.1.3", make_continuum=True, save=True, dsky=4.0): global config detects_line_table = Table.read('detect_hdr{}.fits'.format(version)) # detects = Detections(curated_version=version) # detects_line_table = detects.return_astropy_table() # detects_line_table.write('test.tab', format='ascii') detects_line_table.add_column( Column(str("line"), name="det_type", dtype=str)) detects_cont = Detections(catalog_type="continuum") sel1 = detects_cont.remove_bad_amps() sel2 = detects_cont.remove_meteors() sel3 = detects_cont.remove_shots() sel4 = detects_cont.remove_bad_detects() sel5 = detects_cont.remove_large_gal() sel6 = detects_cont.throughput > 0.08 sel_field = ((detects_cont.field == "cosmos") | (detects_cont.field == "dex-fall") | (detects_cont.field == "dex-spring") | (detects_cont.field == "egs") | (detects_cont.field == "goods-n")) detects_cont_table = detects_cont[sel1 * sel2 * sel3 * sel4 * sel5 * sel6 * sel_field].return_astropy_table() detects_cont_table.add_column( Column(str("cont"), name="det_type", dtype=str)) if make_continuum: detects_cont_table.write("continuum_" + version + ".fits", overwrite=True) detects_cont_table.write("continuum_" + version + ".tab", overwrite=True, format='ascii') dets_all = Detections().refine() sel_tp = dets_all.throughput > 0.08 sel_field = ((dets_all.field == "cosmos") | (dets_all.field == "dex-fall") | (dets_all.field == "dex-spring") | (dets_all.field == "egs") | (dets_all.field == "goods-n")) dets_all_table = dets_all[sel_tp * sel_field].return_astropy_table() dets_all_table.add_column(Column(str("line"), name="det_type", dtype=str)) agn_tab = Table.read(config.agncat, format="ascii", include_names=['detectid', 'flux_LyA']) # add in continuum sources to match to Chenxu's combined catalog #detects_cont_table_orig = detects_cont[sel1 * sel2 * sel3].return_astropy_table() dets_all_table = vstack([dets_all_table, detects_cont_table]) detects_broad_table = join(agn_tab, dets_all_table, join_type="inner", keys=["detectid"]) dets_all.close() del dets_all_table global detect_table detect_table = unique(vstack( [detects_broad_table, detects_cont_table, detects_line_table]), keys='detectid') # add fiber_ratio fiber_ratio = [] for row in detect_table: det = row['detectid'] det_type = row['det_type'] try: fiber_ratio.append(return_fiber_ratio(det, det_type)) except: fiber_ratio.append(np.nan) print('fiber_ratio failed for {}'.format(det)) detect_table['fiber_ratio'] = fiber_ratio detect_table.write('test.fits', overwrite=True) del detects_cont_table, detects_broad_table # calculate ebv and av for every detections all_coords = SkyCoord(ra=detect_table['ra'], dec=detect_table['dec'], unit='deg') sfd = SFDQuery() ebv = sfd(all_coords) Rv = 3.1 corr_SF2011 = 2.742 # Landolt V ext = [] Av = corr_SF2011 * ebv detect_table['Av'] = Av detect_table['ebv'] = ebv detect_table = add_elixer_cat_info(detect_table, version) #print('Adding 1sigma noise from flux limits') #p = Pool(24) #res = p.map(get_flux_noise_1sigma, detect_table['detectid']) #p.close() #flim = [] #flim_update = [] #for r in res: # flim.append(r[0]) # flim_update.append(r[1]) #detect_table['flux_noise_1sigma'] = flim_update #detect_table['flux_noise_1sigma_orig'] = flim detect_table.write('test2.fits', overwrite=True) print("Performing FOF in 3D space with linking length=6 arcsec") # get fluxes to derive flux-weighted distribution of group detect_table["gmag"][np.isnan(detect_table["gmag"])] = 27 gmag = detect_table["gmag"] * u.AB flux = gmag.to(u.Jy).value sel_line = detect_table['det_type'] == 'line' # remove continuum sources # first cluster in positional/wavelegnth space in a larger # linking length kdtree, r = fof.mktree(detect_table["ra"][sel_line], detect_table["dec"][sel_line], detect_table["wave"][sel_line], dsky=6.0, dwave=8.0) t0 = time.time() print("starting fof ...") wfriend_lst = fof.frinds_of_friends(kdtree, r, Nmin=2) t1 = time.time() wfriend_table = fof.process_group_list( wfriend_lst, detect_table["detectid"][sel_line], detect_table["ra"][sel_line], detect_table["dec"][sel_line], detect_table["wave"][sel_line], detect_table['flux'][sel_line], ) print("Generating combined table \n") memberlist = [] friendlist = [] for row in wfriend_table: friendid = row["id"] members = np.array(row["members"]) friendlist.extend(friendid * np.ones_like(members)) memberlist.extend(members) wfriend_table.remove_column("members") wdetfriend_tab = Table() wdetfriend_tab.add_column(Column(np.array(friendlist), name="id")) wdetfriend_tab.add_column(Column(memberlist, name="detectid")) wdetfriend_all = join(wdetfriend_tab, wfriend_table, keys="id") wdetfriend_all['wave_group_id'] = wdetfriend_all['id'] + 213000000 wdetfriend_all.rename_column('size', 'wave_group_size') wdetfriend_all.rename_column('a', 'wave_group_a') wdetfriend_all.rename_column('b', 'wave_group_b') wdetfriend_all.rename_column('pa', 'wave_group_pa') wdetfriend_all.rename_column('icx', 'wave_group_ra') wdetfriend_all.rename_column('icy', 'wave_group_dec') wdetfriend_all.rename_column('icz', 'wave_group_wave') w_keep = wdetfriend_all['detectid', 'wave_group_id', 'wave_group_a', 'wave_group_b', 'wave_group_pa', 'wave_group_ra', 'wave_group_dec', 'wave_group_wave'] print("3D FOF analysis complete in {:3.2f} minutes \n".format( (t1 - t0) / 60)) print("Performing FOF in 2D space with dlink=3.5 arcsec") kdtree, r = fof.mktree( detect_table["ra"], detect_table["dec"], np.zeros_like(detect_table["ra"]), dsky=3.5, ) t0 = time.time() print("starting fof ...") friend_lst = fof.frinds_of_friends(kdtree, r, Nmin=1) t1 = time.time() print("FOF analysis complete in {:3.2f} minutes \n".format((t1 - t0) / 60)) friend_table = fof.process_group_list( friend_lst, detect_table["detectid"], detect_table["ra"], detect_table["dec"], 0.0 * detect_table["wave"], flux, ) print("Generating combined table \n") memberlist = [] friendlist = [] for row in friend_table: friendid = row["id"] members = np.array(row["members"]) friendlist.extend(friendid * np.ones_like(members)) memberlist.extend(members) friend_table.remove_column("members") detfriend_tab = Table() detfriend_tab.add_column(Column(np.array(friendlist), name="id")) detfriend_tab.add_column(Column(memberlist, name="detectid")) detfriend_all = join(detfriend_tab, friend_table, keys="id") del detfriend_tab # match detectids at large linking length if a wave group exists joinfriend = join(detfriend_all, w_keep, keys='detectid', join_type='left') grp_by_id = joinfriend.group_by('id') sum_grp = grp_by_id.groups.aggregate(np.sum) spatial_id = grp_by_id.groups.keys spatial_id_to_keep1 = spatial_id[np.isfinite(sum_grp['wave_group_id'])] # also match if a group member is brighter than gmag=21 (testing this) sel_bright = detect_table['gmag'] < 21 gmag_join = join(joinfriend, detect_table['detectid', 'gmag'][sel_bright]) spatial_id_to_keep2 = gmag_join['id'] spatial_id_to_keep = vstack([spatial_id_to_keep1, spatial_id_to_keep2]) detfriend_1 = join(unique(spatial_id_to_keep), joinfriend) # link the rest of the detectids with smaller linking length keep_row = np.ones(np.size(detect_table), dtype=bool) for i, det in enumerate(detect_table['detectid']): if det in detfriend_1['detectid']: keep_row[i] = 0 print("Performing FOF in 2D space with dlink=1.0 arcsec") kdtree, r = fof.mktree( detect_table["ra"][keep_row], detect_table["dec"][keep_row], np.zeros_like(detect_table["ra"][keep_row]), dsky=1.0, ) t0 = time.time() print("starting fof ...") friend_lst = fof.frinds_of_friends(kdtree, r, Nmin=1) t1 = time.time() print("Final FOF analysis complete in {:3.2f} minutes \n".format( (t1 - t0) / 60)) friend_table = fof.process_group_list( friend_lst, detect_table["detectid"][keep_row], detect_table["ra"][keep_row], detect_table["dec"][keep_row], 0.0 * detect_table["wave"][keep_row], flux[keep_row], ) print("Generating combined table \n") memberlist = [] friendlist = [] for row in friend_table: friendid = row["id"] members = np.array(row["members"]) friendlist.extend(friendid * np.ones_like(members)) memberlist.extend(members) friend_table.remove_column("members") detfriend_tab = Table() detfriend_tab.add_column(Column(np.array(friendlist), name="id")) detfriend_tab.add_column(Column(memberlist, name="detectid")) detfriend_2 = join(detfriend_tab, friend_table, keys="id") starting_id_1 = int(version.replace('.', '', 2)) * 10**10 starting_id_2 = starting_id_1 + 10**8 detfriend_1.add_column(Column(detfriend_1["id"] + starting_id_1, name="source_id"), index=0) detfriend_2.add_column(Column(detfriend_2["id"] + starting_id_2, name="source_id"), index=0) detfriend_1.remove_column("id") detfriend_2.remove_column("id") detfriend_all = vstack([detfriend_1, detfriend_2]) expand_table = join(detfriend_all, detect_table, keys="detectid") expand_table['wave_group_id'] = expand_table['wave_group_id'].filled(0) expand_table.remove_column('detectname') expand_table.write('test3.fits', overwrite=True) # combine common wavegroups to the same source_id sel = expand_table['wave_group_id'] > 0 s_by_id = expand_table[sel].group_by('wave_group_id') for grp in s_by_id.groups: sid, ns = np.unique(grp['source_id'], return_counts=True) wid = grp['wave_group_id'][0] if np.size(sid) > 1: sel_wid = expand_table['wave_group_id'] == wid expand_table['source_id'][sel_wid] = sid[0] del detfriend_all, detect_table, friend_table gaia_stars = Table.read(config.gaiacat) gaia_coords = SkyCoord(ra=gaia_stars["ra"] * u.deg, dec=gaia_stars["dec"] * u.deg) src_coords = SkyCoord(ra=expand_table["ra"] * u.deg, dec=expand_table["dec"] * u.deg) idx, d2d, d3d = src_coords.match_to_catalog_sky(gaia_coords) sel = d2d < 5.0 * u.arcsec gaia_match_name = np.zeros_like(expand_table["source_id"], dtype=int) gaia_match_name[sel] = gaia_stars["source_id"][idx][sel] gaia_match_dist = np.zeros_like(expand_table["source_id"], dtype=float) gaia_match_dist[sel] = d2d[sel].to_value(u.arcsec) expand_table["gaia_match_id"] = gaia_match_name expand_table["gaia_match_dist"] = gaia_match_dist expand_table.rename_column("size", "n_members") expand_table.rename_column("icx", "ra_mean") expand_table.rename_column("icy", "dec_mean") expand_table.sort("source_id") # fill mask values with nans for col in expand_table.columns: try: expand_table[col] = expand_table[col].filled(np.nan) print('yes', col) except: pass #print('no', col) return expand_table
def main(argv=None): """ Main Function """ parser = get_parser() args = parser.parse_args(argv) args.log = setup_logging() args.log.info(args) class FiberImage2D(tb.IsDescription): detectid = tb.Int64Col(pos=0) im_wave = tb.Float32Col(args.width, pos=1) im_sum = tb.Float32Col((args.height, args.width), pos=2) im_array = tb.Float32Col((4, args.height, args.width), pos=3) if args.merge: fileh = tb.open_file("merged_im2D.h5", "w") fibim2D_table = fileh.create_table(fileh.root, "FiberImages", FiberImage2D, "Fiber Cutout Images", expectedrows=1000000) phot_table = fileh.create_table(fileh.root, "PhotImages", PhotImage, "Photometric Images", expectedrows=1000000) spec_table = fileh.create_table(fileh.root, "Spec1D", Spec1D, "Aperture Summed Spectrum", expectedrows=1000000) files = sorted(glob.glob("im2D*.h5")) for file in files: args.log.info('Ingesting %s' % file) fileh_i = tb.open_file(file, "r") fibim2D_table_i = fileh_i.root.FiberImages.read() phot_table_i = fileh_i.root.PhotImages.read() spec_table_i = fileh_i.root.Spec1D.read() fibim2D_table.append(fibim2D_table_i) phot_table.append(phot_table_i) spec_table.append(spec_table_i) fileh_i.close() fibim2D_table.flush() phot_table.flush() spec_table.flush() fibim2D_table.cols.detectid.create_csindex() phot_table.cols.detectid.create_csindex() spec_table.cols.detectid.create_csindex() fibim2D_table.flush() phot_table.flush() spec_table.flush() fileh.close() sys.exit("Merged h5 files in current directory. Exiting") shotid_i = args.shotid detects = Detections(args.survey, loadtable=False) if args.infile: try: catalog = Table.read(args.infile, format="ascii") except: catalog = Table.read(args.infile) selcat = catalog["shotid"] == args.shotid detectlist = np.array(catalog["detectid"][selcat]) elif args.dets: if op.exists(args.dets): try: catalog = Table.read(args.dets, format="ascii") selcat = catalog["shotid"] == int(shotid_i) detectlist = np.array(catalog["detectid"][selcat]) except: detectlist = np.loadtxt(args.dets, dtype=int) else: args.log.warning('No dets for ' + str(shotid_i)) sys.exit() if len(detectlist) == 0: sys.exit() # open up catalog library from elixer catlib = catalogs.CatalogLibrary() args.log.info("Opening shot: " + str(shotid_i)) fibers = Fibers(args.shotid, survey=args.survey) if args.h5file: fileh = tb.open_file("im2D_" + str(args.shotid) + ".h5", "w") fibim2D_table = fileh.create_table(fileh.root, "FiberImages", FiberImage2D, "Fiber Cutout Images") phot_table = fileh.create_table(fileh.root, "PhotImages", PhotImage, "Photometric Images") spec_table = fileh.create_table(fileh.root, "Spec1D", Spec1D, "Aperture Summed Spectrum") for detectid_i in detectlist: # add data to HDF5 file row = fibim2D_table.row row["detectid"] = detectid_i sel = detects.detectid == detectid_i try: row["im_wave"] = get_2Dimage_wave(detectid_i, detects, fibers, width=args.width, height=args.height) except: args.log.error("Could not get wave array for %s" % detectid_i) try: row["im_sum"] = get_2Dimage(detectid_i, detects, fibers, width=args.width, height=args.height) except: args.log.error("Could not get Fiber sum for %s" % detectid_i) try: im_arr, fiber_table = get_2Dimage_array(detectid_i, detects, fibers, width=args.width, height=args.height) row["im_array"] = im_arr except: args.log.error("Could not get 4 Fiber info for %s" % detectid_i) row.append() row_spec = spec_table.row spec_tab = detects.get_spectrum(detectid_i) row_spec["detectid"] = detectid_i row_spec["spec1D"] = spec_tab["spec1d"] row_spec["spec1D_err"] = spec_tab["spec1d_err"] row_spec.append() row_phot = phot_table.row # add in phot image, need RA/DEC from catalog # sel_det = detects.detectid == detectid_i # coord = detects.coords[sel_det] det_row = detects.hdfile.root.Detections.read_where( 'detectid == detectid_i') coord = SkyCoord(ra=det_row["ra"] * u.deg, dec=det_row["dec"] * u.deg) row_phot["detectid"] = detectid_i # ignore the Fall data for now if coord.dec.value > 10: try: cutout = catlib.get_cutouts( position=coord, radius=5, aperture=None, dynamic=False, filter="r", first=True, )[0] if cutout["instrument"] == "HSC": # get shape to ensure slicing on cropped images phot = np.shape(cutout["cutout"].data) row_phot["im_phot"] = cutout["cutout"].data header = cutout["cutout"].wcs.to_header() row_phot["im_phot_hdr"] = header.tostring() except: pass #args.log.warning("No imaging available for source") else: pass row_phot.append() spec_table.flush() phot_table.flush() fibim2D_table.flush() fileh.close() else: for detectid_i in detectlist: save_2Dimage( detectid_i, detects, fibers, width=args.width, height=args.height, path=args.path, ) if args.ra and args.dec: # NOTE this has not been updated yet.. will build functionality soon obj_coords = SkyCoord(args.ra * u.deg, args.dec * u.deg, frame="icrs") idx = fibers.query_region_idx(obj_coords, radius=(args.rad / 3600.0)) output = Table() output["ra"] = fibers.coords.ra[idx] * u.deg output["dec"] = fibers.coords.dec[idx] * u.deg filenames = [] fileidx = 101 for i in idx: filename = "tmp" + str(fileidx) + ".dat" filenames.append(filename) save_rsp_spectrum( fibers, i, file=filename, ) fileidx += 1 output["filename"] = np.array(filenames) ascii.write(output, "fib_coords.dat", overwrite=True) fibers.close() detects.close() tb.file._open_files.close_all()
flim = scube.get_f50( det_table_here["ra"], det_table_here["dec"], det_table_here["wave"], sncut ) sscube.close() return flim[0] version = str(sys.argv[1]) config = HDRconfig() # Note because refine is constantly updated, it isn't possible to # truly replicate older catalogs. TODO for HDR3 detects = Detections(survey="hdr2.1", catalog_type="lines").refine() sel_field = ( (detects.field == "cosmos") | (detects.field == "dex-fall") | (detects.field == "dex-spring") | (detects.field == "egs") | (detects.field == "goods-n") ) if version == "2.1.1": sel_chi2 = detects.chi2 < 1.2 sel_wave = (detects.wave >= 3510) * (detects.wave <= 5490) sel_lw = detects.linewidth <= 6 sel_cont = detects.continuum > -3 sel_sn = detects.sn >= 4.8
def create_source_catalog(version="2.1.3", make_continuum=True, save=True, dsky=4.0): global config detects_line_table = Table.read('detect_hdr{}.fits'.format(version)) # detects = Detections(curated_version=version) # detects_line_table = detects.return_astropy_table() # detects_line_table.write('test.tab', format='ascii') detects_line_table.add_column( Column(str("line"), name="det_type", dtype=str)) detects_cont = Detections(catalog_type="continuum") sel1 = detects_cont.remove_bad_amps() sel2 = detects_cont.remove_meteors() sel3 = detects_cont.remove_shots() sel4 = detects_cont.remove_bad_detects() sel5 = detects_cont.remove_large_gal() detects_cont_table = detects_cont[sel1 * sel2 * sel3 * sel4 * sel5].return_astropy_table() detects_cont_table.add_column( Column(str("cont"), name="det_type", dtype=str)) if make_continuum: detects_cont_table.write("continuum_" + version + ".fits", overwrite=True) dets_all = Detections().refine() dets_all_table = dets_all.return_astropy_table() agn_tab = Table.read(config.agncat, format="ascii", include_names=["detectid"]) # add in continuum sources to match to Chenxu's combined catalog detects_cont_table_orig = detects_cont[sel1 * sel2 * sel3].return_astropy_table() dets_all_table = vstack([dets_all_table, detects_cont_table_orig]) detects_broad_table = join(agn_tab, dets_all_table, join_type="inner", keys=["detectid"]) detects_broad_table.add_column( Column(str("agn"), name="det_type", dtype=str)) dets_all.close() del dets_all_table detect_table = vstack( [detects_line_table, detects_broad_table, detects_cont_table]) del detects_cont_table_orig, detects_cont_table, detects_broad_table kdtree, r = fof.mktree( detect_table["ra"], detect_table["dec"], np.zeros_like(detect_table["ra"]), dsky=dsky, ) t0 = time.time() print("starting fof ...") friend_lst = fof.frinds_of_friends(kdtree, r, Nmin=1) t1 = time.time() print("FOF analysis complete in {:3.2f} minutes \n".format((t1 - t0) / 60)) # get fluxes to derive flux-weighted distribution of group detect_table["gmag"][np.isnan(detect_table["gmag"])] = 27 gmag = detect_table["gmag"] * u.AB flux = gmag.to(u.Jy).value friend_table = fof.process_group_list( friend_lst, detect_table["detectid"], detect_table["ra"], detect_table["dec"], 0.0 * detect_table["wave"], flux, ) print("Generating combined table \n") memberlist = [] friendlist = [] for row in friend_table: friendid = row["id"] members = np.array(row["members"]) friendlist.extend(friendid * np.ones_like(members)) memberlist.extend(members) friend_table.remove_column("members") detfriend_tab = Table() detfriend_tab.add_column(Column(np.array(friendlist), name="id")) detfriend_tab.add_column(Column(memberlist, name="detectid")) detfriend_all = join(detfriend_tab, friend_table, keys="id") del detfriend_tab expand_table = join(detfriend_all, detect_table, keys="detectid") del detfriend_all, detect_table, friend_table starting_id = int(version.replace('.', '', 2)) * 10**10 expand_table.add_column(Column(expand_table["id"] + starting_id, name="source_id"), index=0) expand_table.remove_column("id") gaia_stars = Table.read(config.gaiacat) gaia_coords = SkyCoord(ra=gaia_stars["ra"] * u.deg, dec=gaia_stars["dec"] * u.deg) src_coords = SkyCoord(ra=expand_table["ra"] * u.deg, dec=expand_table["dec"] * u.deg) idx, d2d, d3d = src_coords.match_to_catalog_sky(gaia_coords) sel = d2d < 5.0 * u.arcsec gaia_match_name = np.zeros_like(expand_table["source_id"], dtype=int) gaia_match_name[sel] = gaia_stars["source_id"][idx][sel] gaia_match_dist = np.zeros_like(expand_table["source_id"], dtype=float) gaia_match_dist[sel] = d2d[sel] expand_table["gaia_match_id"] = gaia_match_name expand_table["gaia_match_dist"] = gaia_match_dist expand_table.rename_column("size", "n_members") expand_table.rename_column("icx", "ra_mean") expand_table.rename_column("icy", "dec_mean") expand_table.sort("source_id") return expand_table
import subprocess import numpy as np import tables as tb import matplotlib.pyplot as plt from astropy.io import ascii from astropy.table import Table, Column from astropy.coordinates import SkyCoord import astropy.units as u from hetdex_api.config import HDRconfig from hetdex_api.detections import Detections from hetdex_api.elixer_widget_cls import ElixerWidget # open detection catalogue detects = Detections(survey='hdr2', catalog_type='lines') # open LAE list laes = ascii.read("/work/05865/maja_n/wrangler/im2d/all_good_LAEs_060919.tab") laes = laes[laes["cat2"] == "good"] for i in range(len(laes)): ra, dec, wave = laes[i]["ra"], laes[i]["dec"], laes[i]["wl"] # set object coordinates and wavelength obj_coords = SkyCoord(ra * u.deg, dec * u.deg, frame='icrs') wave_obj = wave # find match in the catalogue idx = detects.find_match(obj_coords, wave=wave_obj,
def read_detect(version='2.1.2'): detects = Detections(curated_version=version) detect_table = detects.return_astropy_table() return detect_table