def readSpectrum(self, fileName, colWave=0, colFlux=1, skipRows=0): """ Reading spectrum in text or FITS format and update regions and points """ if not ".fits" in fileName: self.spectrum = sp.readSpectrum(fileName,\ colWave=colWave,\ colFlux=colFlux,\ skipRows=skipRows) else: self.spectrum = sp.Spectrum() """ Check more at http://archive.eso.org/cms/eso-data/help/1dspectra.html https://www.hs.uni-hamburg.de/DE/Ins/Per/Czesla/PyA/PyA/pyaslDoc/aslDoc/readFitsSpec.html """ self.spectrum.wave, self.spectrum.flux = pyasl.read1dFitsSpec( fileName) # self.spectrum.wave = self.spectrum.wave.byteswap().newbyteorder() self.spectrum.flux = self.spectrum.flux.byteswap().newbyteorder( ) #TODO PyAstronomy bug self.spectrum.name = fileName self.radialVelocity = 0.0 self.oryginalWavelength = copy.deepcopy(self.spectrum.wave) self.spectrumNote.set_spectrum(fileName)
def mk_detection_plots(IFU, image_catalog, layoutmode=False, single_id=None): global wldetect, lineset, dlineset, flinelist, ax_pos foutmap = "map_{}.fits.gz".format(IFU) fincube = "data/sf2outcube_median_{}.fits.gz".format(IFU) frawincube = "data/outcube_median_{}.fits.gz".format(IFU) fcalincube = "data/coutcube_median_{}.fits.gz".format(IFU) fnoisecube = "data/sf2ncoutcube_median_{}.fits.gz".format(IFU) ftcal = "mean_cal.txt" # load flux calibration tcal = ascii.read(ftcal, format="fixed_width") cal_interp = interp1d(tcal["wl[A]"], tcal["cal[erg/s/cm^2/A/cnt]"], kind='cubic', bounds_error=False) fcatalog = "catalog_{}.txt".format(IFU) flinelist = "linelist.txt" #threshold = 0.15 threshold = 0.1 # line detection threshold wlwin = 50. # width of window for peak fit #win = 50. width = 25. / 3600. # size for slice plots and aux images # load data outmap = fits.getdata(foutmap) s = spectrum.readSpectrum(fincube) sraw = spectrum.readSpectrum(frawincube) ns = spectrum.readSpectrum(fnoisecube) t = ascii.read(fcatalog, format="ecsv") print("Found {} sources in IFU {} ...".format(len(t), IFU)) _t = t for i, r in enumerate(_t): #try: id, ra, dec = r["id"], r["ra_com"], r["dec_com"] if single_id != None and not id == single_id: continue print("Creating plot for source {}, number {} in {}".format( id, i + 1, len(_t))) mk_detection_plot(IFU, id, r, s, sraw, ns, outmap, image_catalog, threshold, wlwin, width, cal_interp, layoutmode)
def readTheoreticalSpectrum(self, fileName, colWave=0, colFlux=1, skipRows=0): self.theoreticalSpectrum = sp.readSpectrum(fileName,\ colWave=colWave,\ colFlux=colFlux,\ skipRows=skipRows)
def readSpectrum(self, fileName, colWave=0, colFlux=1, skipRows=0): """ Reading spectrum in text or FITS format and update regions and points """ if not ".fits" in fileName: self.spectrum = sp.readSpectrum(fileName,\ colWave=colWave,\ colFlux=colFlux,\ skipRows=skipRows) else: self.spectrum = sp.Spectrum() """ Check more at http://archive.eso.org/cms/eso-data/help/1dspectra.html https://www.hs.uni-hamburg.de/DE/Ins/Per/Czesla/PyA/PyA/pyaslDoc/aslDoc/readFitsSpec.html """ self.spectrum.wave = None self.spectrum.flux = None # Search FITS data for Molecfit keywords waveKey = "lambda" # use orignal wavelength not the the molecfit corrected fluxKey = "cflux" # use telluric absorption corrected flux dataKeys = [waveKey, fluxKey] fitsFile = fits.open(fileName) for hdu in fitsFile: if hdu.data is None or not hasattr(hdu.data, 'names'): continue if all(name in hdu.data.names for name in dataKeys): self.spectrum.wave = hdu.data[waveKey] self.spectrum.flux = hdu.data[fluxKey] self.spectrum.wave = self.spectrum.wave * 1000 # micrometre to nanometre break fitsFile.close() # If no Molecfit formated data is found, fall back to pyastronomy solution used in main version of HANDY if self.spectrum.wave is None or self.spectrum.flux is None: self.spectrum.wave, self.spectrum.flux = pyasl.read1dFitsSpec( fileName) self.spectrum.flux = self.spectrum.flux.byteswap( ).newbyteorder() # TODO PyAstronomy bug self.spectrum.name = fileName self.radialVelocity = 0.0 self.oryginalWavelength = copy.deepcopy(self.spectrum.wave)
default='', help='Output map.') parser.add_argument('-b', '--bad_regions', type=str, default='', help='Output map.') args = parser.parse_args(sys.argv[1:]) badwlregions = [] t = ascii.read(args.bad_regions) for r in t: badwlregions.append([r["start"], r["end"]]) fcube = args.infile fmapout = args.outfile detect_threshold = args.detect_threshold grow_threshold = args.grow_threshold minsize = args.minsize s = spectrum.readSpectrum(fcube) c = s.data for r in badwlregions: c[r[0]:r[1]] = 0. # take out bad region outmap = build_map(c, detect_threshold) outmap = filter_minsize(outmap, minsize) save_map(outmap, fmapout)
"dz": "px", "diam": "arcsec", "x_ext": "px", "y_ext": "px", "z_ext": "px", "wl_com": "A", "dwl": "A", "xmin": "px", "xmax": "px", "ymin": "px", "ymax": "px", "zmin": "px", "zmax": "px" } s = spectrum.readSpectrum(args.incube) c = s.data zz, yy, xx = [np.arange(s, dtype=int) for s in c.shape] YY, ZZ, XX = np.meshgrid(yy, zz, xx) w = wcs.WCS(s.hdu) platescale = s.hdu.header["CDELT2"] outmap = fits.getdata(args.map) for n in names: t[n].dtype = dtypes[n] t[n].format = formats[n] t[n].unit = units[n] rr = np.sort(np.unique(outmap.flatten()))
return ww, np.array(all_sout), mask #plt.imshow(mask) if False: IFU = '022' foutmap = "map_{}.fits.gz".format(IFU) fincube = "data/sf2outcube_median_{}.fits.gz".format(IFU) fnoisecube = "data/sf2ncoutcube_median_{}.fits.gz".format(IFU) fcatalog = "catalog_{}.txt".format(IFU) flinelist = "linelist.txt" outmap = fits.getdata(foutmap) s = spectrum.readSpectrum(fincube) ns = spectrum.readSpectrum(fnoisecube) t = ascii.read(fcatalog, format="ecsv") r = t[0] ww, sout, mask = extract(r, s, outmap) ww, nsout, mask = nextract(r, ns, outmap) mm = np.std(nsout, axis=0) plt.plot(ww, sout) plt.fill_between(ww, 2. * mm, -2. * mm, alpha=0.3, edgecolor='black', facecolor='grey')