def get_stellar_data(plt, ifu, hdu): uniq_bins, uniq_indx, uniq_cnt = map( lambda x: x[1:], numpy.unique(hdu['BINID'].data[1].ravel(), return_index=True, return_counts=True)) keep = numpy.logical_not(hdu['BIN_MFLUX_MASK'].data.ravel()[uniq_indx] > 0) \ | numpy.logical_not(hdu['STELLAR_VEL_MASK'].data.ravel()[uniq_indx] > 0) \ | numpy.logical_not(hdu['STELLAR_SIGMA_MASK'].data.ravel()[uniq_indx] > 0) uniq_bins = uniq_bins[keep] uniq_indx = uniq_indx[keep] uniq_cnt = uniq_cnt[keep] data = StellarDataTable(shape=uniq_bins.shape) data['PLATE'] = plt data['IFU'] = ifu data['Y'], data['X'] = numpy.unravel_index(uniq_indx, hdu['BINID'].shape[1:]) data['BINID'] = uniq_bins data['NBIN'] = uniq_cnt data['R'] = hdu['BIN_LWELLCOO'].data[0].ravel()[uniq_indx] data['R_RE'] = hdu['BIN_LWELLCOO'].data[1].ravel()[uniq_indx] data['R_KPC'] = hdu['BIN_LWELLCOO'].data[2].ravel()[uniq_indx] data['GCNT'] = hdu['BIN_MFLUX'].data.ravel()[uniq_indx] data['GSNR'] = hdu['BIN_SNR'].data.ravel()[uniq_indx] data['RCHI2'] = hdu['STELLAR_FOM'].data[2].ravel()[uniq_indx] data['VEL'] = hdu['STELLAR_VEL'].data.ravel()[uniq_indx] data['VEL_ERR'] = numpy.ma.power( hdu['STELLAR_VEL_IVAR'].data.ravel()[uniq_indx], -0.5).filled(-1) data['SIG'] = hdu['STELLAR_SIGMA'].data.ravel()[uniq_indx] data['SIG_ERR'] = numpy.ma.power( hdu['STELLAR_SIGMA_IVAR'].data.ravel()[uniq_indx], -0.5).filled(-1) data['SIG_CORR'] = hdu['STELLAR_SIGMACORR'].data.ravel()[uniq_indx] # Deal with the masking # maps_bm = DAPMapsBitMask() qual_bm = DAPQualityBitMask() data_bm = StellarBitMask() if qual_bm.flagged(hdu[0].header['DAPQUAL'], flag='CRITICAL'): data['MASK'] = data_bm.turn_on(data['MASK'], 'CRITCUBE') indx = hdu['BIN_MFLUX_MASK'].data.ravel()[uniq_indx] > 0 if numpy.any(indx): data['MASK'][indx] = data_bm.turn_on(data['MASK'][indx], 'BADFLUX') indx = (hdu['STELLAR_VEL_MASK'].data.ravel()[uniq_indx] > 0) \ | numpy.logical_not(hdu['STELLAR_VEL_IVAR'].data.ravel()[uniq_indx] > 0) if numpy.any(indx): data['MASK'][indx] = data_bm.turn_on(data['MASK'][indx], 'BADVEL') indx = (hdu['STELLAR_SIGMA_MASK'].data.ravel()[uniq_indx] > 0) \ | numpy.logical_not(hdu['STELLAR_SIGMA_IVAR'].data.ravel()[uniq_indx] > 0) if numpy.any(indx): data['MASK'][indx] = data_bm.turn_on(data['MASK'][indx], 'BADSIG') return data
def is_critical(dapver, analysis_path, daptype, plt): ifus = [ 1901, 1902, 3701, 3702, 3703, 3704, 6101, 6102, 6103, 6104, 9101, 9102, 12701, 12702, 12703, 12704, 12705 ] dapqualbm = DAPQualityBitMask() critical = numpy.zeros(len(ifus), dtype=bool) for i, ifu in enumerate(ifus): apath = os.path.join(analysis_path, daptype, str(plt), str(ifu)) maps_file = os.path.join( apath, 'manga-{0}-{1}-MAPS-{2}.fits.gz'.format(plt, ifu, daptype)) if not os.path.isfile(maps_file): continue hdu = fits.open(maps_file) critical[i] = dapqualbm.flagged(hdu[0].header['dapqual'], 'CRITICAL') return ifus, critical
def ppxf_flag_info(tbl, maps_file): qual_bm = DAPQualityBitMask() bm = DAPMapsBitMask() # # Targeting bits # sdssMaskbits = defaults.sdss_maskbits_file() # mngtarg1_bm = BitMask.from_par_file(sdssMaskbits, 'MANGA_TARGET1') # mngtarg3_bm = BitMask.from_par_file(sdssMaskbits, 'MANGA_TARGET3') tbl['PLT'], tbl['IFU'] = map(lambda x : int(x), os.path.split(maps_file)[1].split('-')[1:3]) with fits.open(maps_file) as hdu: # Check if the file was CRITICAL tbl['CRIT'] = qual_bm.flagged(hdu['PRIMARY'].header['DAPQUAL'], flag='DRPCRIT') tbl['MAIN'] = hdu[0].header['MNGTARG1'] > 0 el = channel_dictionary(hdu, 'EMLINE_GFLUX') haflx = numpy.ma.MaskedArray(hdu['EMLINE_GFLUX'].data[el['Ha-6564']], mask=hdu['EMLINE_GFLUX_MASK'].data[el['Ha-6564']] > 0) tbl['HAMEAN'] = numpy.ma.median(haflx) tbl['NSPX'] = haflx.size tbl['NGOOD'] = numpy.sum(numpy.logical_not(numpy.ma.getmaskarray(haflx))) indx = bm.flagged(hdu['EMLINE_GFLUX_MASK'].data[el['Ha-6564']], 'FITFAILED') tbl['NFLG'] = numpy.sum(indx) if not numpy.any(indx): return snr = numpy.ma.MaskedArray(hdu['SPX_SNR'].data, mask=numpy.logical_not(hdu['SPX_SNR'].data > 0)) tbl['FLGSNR'] = numpy.ma.median(snr[indx]) srt = numpy.argsort(snr[indx]) tbl['EXSNR'] = snr[indx][srt][-1] tbl['EXBIN'] = hdu['BINID'].data[3][indx][srt][-1]
def get_halpha_data(plt, ifu, hdu): el = channel_dictionary(hdu, 'EMLINE_GFLUX') uniq_bins, uniq_indx, uniq_cnt = map( lambda x: x[1:], numpy.unique(hdu['BINID'].data[3].ravel(), return_index=True, return_counts=True)) keep = numpy.logical_not(hdu['EMLINE_GFLUX_MASK'].data.ravel()[uniq_indx] > 0) \ | numpy.logical_not(hdu['EMLINE_GEW_MASK'].data.ravel()[uniq_indx] > 0) \ | numpy.logical_not(hdu['EMLINE_GVEL_MASK'].data.ravel()[uniq_indx] > 0) \ | numpy.logical_not(hdu['EMLINE_GSIGMA_MASK'].data.ravel()[uniq_indx] > 0) uniq_bins = uniq_bins[keep] uniq_indx = uniq_indx[keep] uniq_cnt = uniq_cnt[keep] data = HalphaDataTable(shape=uniq_bins.shape) data['PLATE'] = plt data['IFU'] = ifu data['Y'], data['X'] = numpy.unravel_index(uniq_indx, hdu['BINID'].shape[1:]) data['BINID'] = uniq_bins data['R'] = hdu['SPX_ELLCOO'].data[0].ravel()[uniq_indx] data['R_RE'] = hdu['SPX_ELLCOO'].data[1].ravel()[uniq_indx] data['R_KPC'] = hdu['SPX_ELLCOO'].data[2].ravel()[uniq_indx] data['GCNT'] = hdu['SPX_MFLUX'].data.ravel()[uniq_indx] data['GSNR'] = hdu['SPX_SNR'].data.ravel()[uniq_indx] data['FLUX'] = hdu['EMLINE_GFLUX'].data[el['Ha-6564']].ravel()[uniq_indx] data['FLUX_ERR'] = numpy.ma.power( hdu['EMLINE_GFLUX_IVAR'].data[el['Ha-6564']].ravel()[uniq_indx], -0.5).filled(-1) data['AMP'] = hdu['EMLINE_GA'].data[el['Ha-6564']].ravel()[uniq_indx] data['ANR'] = hdu['EMLINE_GANR'].data[el['Ha-6564']].ravel()[uniq_indx] data['RCHI2'] = hdu['EMLINE_LFOM'].data[el['Ha-6564']].ravel()[uniq_indx] data['CNT'] = hdu['EMLINE_GEW_CNT'].data[el['Ha-6564']].ravel()[uniq_indx] data['EW'] = hdu['EMLINE_GEW'].data[el['Ha-6564']].ravel()[uniq_indx] data['EW_ERR'] = numpy.ma.power( hdu['EMLINE_GEW_IVAR'].data[el['Ha-6564']].ravel()[uniq_indx], -0.5).filled(-1) data['VEL'] = hdu['EMLINE_GVEL'].data[el['Ha-6564']].ravel()[uniq_indx] data['VEL_ERR'] = numpy.ma.power( hdu['EMLINE_GVEL_IVAR'].data[el['Ha-6564']].ravel()[uniq_indx], -0.5).filled(-1) data['SIG'] = hdu['EMLINE_GSIGMA'].data[el['Ha-6564']].ravel()[uniq_indx] data['SIG_ERR'] = numpy.ma.power( hdu['EMLINE_GSIGMA_IVAR'].data[el['Ha-6564']].ravel()[uniq_indx], -0.5).filled(-1) data['SIG_INST'] = hdu['EMLINE_INSTSIGMA'].data[ el['Ha-6564']].ravel()[uniq_indx] # Deal with the masking qual_bm = DAPQualityBitMask() data_bm = HalphaBitMask() if qual_bm.flagged(hdu[0].header['DAPQUAL'], flag='CRITICAL'): data['MASK'] = data_bm.turn_on(data['MASK'], 'CRITCUBE') indx = hdu['EMLINE_GFLUX_MASK'].data[el['Ha-6564']].ravel()[uniq_indx] > 0 if numpy.any(indx): data['MASK'][indx] = data_bm.turn_on(data['MASK'][indx], 'BADFLUX') indx = hdu['EMLINE_GEW_MASK'].data[el['Ha-6564']].ravel()[uniq_indx] > 0 if numpy.any(indx): data['MASK'][indx] = data_bm.turn_on(data['MASK'][indx], 'BADEW') indx = (hdu['EMLINE_GVEL_MASK'].data.ravel()[uniq_indx] > 0) \ | numpy.logical_not(hdu['EMLINE_GVEL_IVAR'].data.ravel()[uniq_indx] > 0) if numpy.any(indx): data['MASK'][indx] = data_bm.turn_on(data['MASK'][indx], 'BADVEL') indx = (hdu['EMLINE_GSIGMA_MASK'].data.ravel()[uniq_indx] > 0) \ | numpy.logical_not(hdu['EMLINE_GSIGMA_IVAR'].data.ravel()[uniq_indx] > 0) if numpy.any(indx): data['MASK'][indx] = data_bm.turn_on(data['MASK'][indx], 'BADSIG') return data
def d4000_sigma_haew_snr_dist(fits_file, n, d4000_lim, sigma_lim, haew_lim, snr_lim): # Channels with the H-alpha data and the D4000 data hac = 18 d4c = 43 # Set the method and get the root directory method = 'SPX-GAU-MILESHC' odir = default_dap_method_path(method) print('Output directory: {0}'.format(odir)) # Find the intermediate files with the stellar-continuum models srchstr = os.path.join(odir, '*', '*', 'manga-*-MAPS-{0}.fits.gz'.format(method)) print('Search string: {0}'.format(srchstr)) maps_files = glob.glob(srchstr) nfiles = len(maps_files) print('Found {0} MAPS files.'.format(nfiles)) d4000_bins = numpy.linspace(*d4000_lim, n + 1) sigma_bins = numpy.logspace(*numpy.log10(sigma_lim), n + 1) haew_bins = numpy.logspace(*numpy.log10(haew_lim), n + 1) snr_bins = numpy.logspace(*numpy.log10(snr_lim), n + 1) dl = d4000_bins[0] dd = d4000_bins[1] - d4000_bins[0] sl = numpy.log10(sigma_bins[0]) ds = numpy.mean(numpy.diff(numpy.log10(sigma_bins))) hl = numpy.log10(haew_bins[0]) dh = numpy.mean(numpy.diff(numpy.log10(haew_bins))) nl = numpy.log10(snr_bins[0]) dn = numpy.mean(numpy.diff(numpy.log10(snr_bins))) d4000_vs, vs_sigma = numpy.meshgrid( d4000_bins[:-1] + dd / 2, sigma_bins[:-1] + ds * sigma_bins[:-1] * numpy.log(10) / 2) d4000_vs, vs_haew = numpy.meshgrid( d4000_bins[:-1] + dd / 2, haew_bins[:-1] + dh * haew_bins[:-1] * numpy.log(10) / 2) d4000_vs, vs_snr = numpy.meshgrid( d4000_bins[:-1] + dd / 2, snr_bins[:-1] + dn * snr_bins[:-1] * numpy.log(10) / 2) sigma_vs, vs_haew = numpy.meshgrid( sigma_bins[:-1] + ds * sigma_bins[:-1] * numpy.log(10) / 2, haew_bins[:-1] + ds * haew_bins[:-1] * numpy.log(10) / 2) sigma_vs, vs_snr = numpy.meshgrid( sigma_bins[:-1] + ds * sigma_bins[:-1] * numpy.log(10) / 2, snr_bins[:-1] + dn * snr_bins[:-1] * numpy.log(10) / 2) haew_vs, vs_snr = numpy.meshgrid( haew_bins[:-1] + dh * haew_bins[:-1] * numpy.log(10) / 2, snr_bins[:-1] + dn * snr_bins[:-1] * numpy.log(10) / 2) bin_d4000_vs_sigma = numpy.zeros((n, n), dtype=int) bin_d4000_vs_haew = numpy.zeros((n, n), dtype=int) bin_d4000_vs_snr = numpy.zeros((n, n), dtype=int) bin_sigma_vs_haew = numpy.zeros((n, n), dtype=int) bin_sigma_vs_snr = numpy.zeros((n, n), dtype=int) bin_haew_vs_snr = numpy.zeros((n, n), dtype=int) qual_bm = DAPQualityBitMask() maps_bm = DAPMapsBitMask() for fi in range(nfiles): # for fi in range(20): print('{0}/{1}'.format(fi + 1, nfiles), end='\r') maps_file = maps_files[fi] print(maps_file) # Get the bin IDs, mapped to the correct spaxels hdu = fits.open(maps_file) # Check if the file was CRITICAL if qual_bm.flagged(hdu['PRIMARY'].header['DAPQUAL'], flag='DRPCRIT'): print('DRPCRIT') print(maps_file) hdu.close() del hdu continue plt, ifu = tuple( [int(x) for x in hdu[0].header['PLATEIFU'].split('-')]) nsa_z = hdu['PRIMARY'].header['SCINPVEL'] / astropy.constants.c.to( 'km/s').value binid_map = hdu['BINID'].data[0, :, :].copy() spatial_shape = binid_map.shape binid, bin_indx = numpy.unique(binid_map.ravel(), return_index=True) binid = binid[1:] bin_indx = bin_indx[1:] # Get the D4000, H-alpha EW, and sigma values # snr = hdu['SPX_SNR'].data.ravel()[bin_indx] # snr_indx = snr > 1 # good_snr = snr[snr_indx] # binid_snr = binid[snr_indx] mask = (hdu['EMLINE_GEW'].data[hac,:,:].ravel()[bin_indx] > 0) \ & (hdu['SPECINDEX'].data[d4c,:,:].ravel()[bin_indx] > 0) \ & (hdu['STELLAR_SIGMA'].data.ravel()[bin_indx] > 0) \ & (hdu['SPX_SNR'].data.ravel()[bin_indx] > 0) haew = hdu['EMLINE_GEW'].data[hac, :, :].ravel()[bin_indx][mask] d4000 = hdu['SPECINDEX'].data[d4c, :, :].ravel()[bin_indx][mask] sigma = hdu['STELLAR_SIGMA'].data.ravel()[bin_indx][mask] snr = hdu['SPX_SNR'].data.ravel()[bin_indx][mask] hdu.close() del hdu bin_d4000_vs_sigma += twod_bin(d4000, numpy.log10(sigma), dl, dd, n, sl, ds, n) bin_d4000_vs_haew += twod_bin(d4000, numpy.log10(haew), dl, dd, n, hl, dh, n) bin_d4000_vs_snr += twod_bin(d4000, numpy.log10(snr), dl, dd, n, nl, dn, n) bin_sigma_vs_haew += twod_bin(numpy.log10(sigma), numpy.log10(haew), sl, ds, n, hl, dh, n) bin_sigma_vs_snr += twod_bin(numpy.log10(sigma), numpy.log10(snr), sl, ds, n, nl, dn, n) bin_haew_vs_snr += twod_bin(numpy.log10(haew), numpy.log10(snr), hl, dh, n, nl, dn, n) fits.HDUList([ fits.PrimaryHDU(), fits.ImageHDU(data=bin_d4000_vs_sigma, name='D4000SIGMA'), fits.ImageHDU(data=bin_d4000_vs_haew, name='D4000HAEW'), fits.ImageHDU(data=bin_d4000_vs_snr, name='D4000SNR'), fits.ImageHDU(data=bin_sigma_vs_haew, name='SIGMAHAEW'), fits.ImageHDU(data=bin_sigma_vs_snr, name='SIGMASNR'), fits.ImageHDU(data=bin_haew_vs_snr, name='HAEWSNR') ]).writeto(fits_file, overwrite=True)
def include_maps_data(tbl, maps_file, exclude_ancillary=False): qual_bm = DAPQualityBitMask() bm = DAPMapsBitMask() # # Targeting bits # sdssMaskbits = defaults.sdss_maskbits_file() # mngtarg1_bm = BitMask.from_par_file(sdssMaskbits, 'MANGA_TARGET1') # mngtarg3_bm = BitMask.from_par_file(sdssMaskbits, 'MANGA_TARGET3') with fits.open(maps_file) as hdu: # Check if the file was CRITICAL if qual_bm.flagged(hdu['PRIMARY'].header['DAPQUAL'], flag='DRPCRIT'): return 1 if exclude_ancillary and hdu[0].header['MNGTARG1'] == 0: return 1 d4000_bins = numpy.unique(tbl['D4000BIN']) sigma_bins = numpy.unique(tbl['SIGMABIN']) haew_bins = numpy.unique(tbl['HAEWBIN']) snr_bins = numpy.unique(tbl['SNRBIN']) nsigma = len(sigma_bins) - 1 nd4000 = len(d4000_bins) - 1 nhaew = len(haew_bins) - 1 nsnr = len(snr_bins) - 1 el = channel_dictionary(hdu, 'EMLINE_GFLUX') si = channel_dictionary(hdu, 'SPECINDEX') plt, ifu = map(lambda x: int(x), os.path.split(maps_file)[1].split('-')[1:3]) # Input redshift z = hdu['PRIMARY'].header['SCINPVEL'] / astropy.constants.c.to( 'km/s').value # Stellar-kinematics binning map binid, bin_indx = map( lambda x: x[1:], numpy.unique(hdu['BINID'].data[1, :, :].ravel(), return_index=True)) # Get the D4000, H-alpha EW, and sigma values with S/N greater # than one and unmasked good_snr = hdu['SPX_SNR'].data.ravel() > 1 good_haew = numpy.logical_not( bm.flagged(hdu['EMLINE_GEW_MASK'].data[el['Ha-6564']].ravel(), flag='DONOTUSE')) good_d4000 = numpy.logical_not( bm.flagged(hdu['SPECINDEX_MASK'].data[si['D4000']].ravel(), flag='DONOTUSE')) good_sigma = numpy.logical_not( bm.flagged(hdu['STELLAR_SIGMA_MASK'].data.ravel(), flag='DONOTUSE')) indx = (good_snr & good_haew & good_d4000 & good_sigma)[bin_indx] snr = hdu['SPX_SNR'].data.ravel()[bin_indx][indx] stvel = hdu['STELLAR_VEL'].data.ravel()[bin_indx][indx] sigma = hdu['STELLAR_SIGMA'].data.ravel()[bin_indx][indx] haanr = hdu['EMLINE_GANR'].data[el['Ha-6564']].ravel()[bin_indx][indx] havel = hdu['EMLINE_GVEL'].data[el['Ha-6564']].ravel()[bin_indx][indx] hasig = hdu['EMLINE_GSIGMA'].data[ el['Ha-6564']].ravel()[bin_indx][indx] haew = hdu['EMLINE_GEW'].data[el['Ha-6564']].ravel()[bin_indx][indx] d4000 = hdu['SPECINDEX'].data[si['D4000']].ravel()[bin_indx][indx] binid = binid[indx] d4000_i = numpy.digitize(d4000, d4000_bins[1:-1]) sigma_j = numpy.digitize(sigma, sigma_bins[1:-1]) haew_k = numpy.digitize(haew, haew_bins[1:-1]) snr_l = numpy.digitize(snr, snr_bins[1:-1]) for i in range(nd4000): dindx = d4000_i == i for j in range(nsigma): sindx = sigma_j == j for k in range(nhaew): hindx = haew_k == k for l in range(nsnr): nindx = snr_l == l indx = dindx & sindx & hindx & nindx nbin = numpy.sum(indx) if nbin == 0: continue ii = i * nsigma * nhaew * nsnr + j * nhaew * nsnr + k * nsnr + l tbl['NBIN'][ii] += nbin _snr = snr[indx] # Still find the highest S/N spectrum in each # bin m = numpy.argsort(_snr)[-1] if _snr[m] < tbl['SNR'][ii]: continue tbl['PLT'][ii] = plt tbl['IFU'][ii] = ifu tbl['BIN'][ii] = binid[indx][m] tbl['MNGTARG1'][ii] = int(hdu[0].header['MNGTARG1']) tbl['MNGTARG3'][ii] = int(hdu[0].header['MNGTARG3']) tbl['Z'][ii] = z tbl['SNR'][ii] = snr[indx][m] tbl['STZ'][ii] \ = stvel[indx][m]*(1+z)/astropy.constants.c.to('km/s').value + z tbl['SIGMA'][ii] = sigma[indx][m] tbl['HAANR'][ii] = haanr[indx][m] tbl['HAZ'][ii] \ = havel[indx][m]*(1+z)/astropy.constants.c.to('km/s').value + z tbl['HASIGMA'][ii] = hasig[indx][m] tbl['HAEW'][ii] = haew[indx][m] tbl['D4000'][ii] = d4000[indx][m] return 0