Esempio n. 1
0
    def qso(self):  #, name=qso_file):
        if self.file is None:
            name = qso_file
        else:
            name = self.file
        try:
            data = fits.open(name)[1].data
            data = data[:-1]
            data = data[np.logical_and(data['wave'] * 0.1 > self.wmin.value,
                                       data['wave'] * 0.1 < self.wmax.value)]
            wavef = data['wave'] * 0.1 * au.nm
            fluxf = data['flux']
            spl = cspline(wavef, fluxf)(self.wave.value)
            sel = np.where(self.wave.value < 313)
            spl[sel] = 1.0
        except:
            data = ascii.read(name,
                              data_start=1,
                              names=['col1', 'col2', 'col3', 'col4'])
            wavef = data['col1'] * 0.1 * au.nm * (1 + qso_zem)
            fluxf = data['col2']
            spl = cspline(wavef, fluxf)(self.wave.value)

        flux = spl / np.mean(spl)  #* au.photon/au.nm
        #if qso_lya_abs:
        #    flux = self.lya_abs(flux)
        return flux * self.atmo_ex
Esempio n. 2
0
 def star(self):  #, name=star_file):
     if self.file is None:
         name = star_file
     else:
         name = self.file
     data = ascii.read(name, data_start=2, names=['col1', 'col2'])  #name)
     wavef = data['col1'] * 0.1 * au.nm
     fluxf = data['col2']
     spl = cspline(wavef, fluxf)(self.wave.value)
     flux = spl / np.mean(spl)  #* au.photon/au.nm
     return flux * self.atmo_ex
Esempio n. 3
0
 def qso(self):  #, name=qso_file):
     if self.file is None:
         name = qso_file
     else:
         name = self.file
     data = fits.open(name)[1].data
     data = data[np.logical_and(data['wave'] * 0.1 > self.wmin.value,
                                data['wave'] * 0.1 < self.wmax.value)]
     wavef = data['wave'] * 0.1 * au.nm
     fluxf = data['flux']
     spl = cspline(wavef, fluxf)(self.wave.value)
     sel = np.where(self.wave.value < 313)
     spl[sel] = 1.0
     flux = spl / np.mean(spl)  #* au.photon/au.nm
     return flux * self.atmo_ex
Esempio n. 4
0
    def __init__(self,
                 phot,
                 file=None,
                 wmin=305 * au.nm,
                 wmax=390 * au.nm,
                 dw=1e-3 * au.nm,
                 func=spec_func):
        self.phot = phot
        self.file = file
        self.wmin = wmin
        self.wmax = wmax
        self.wmean = 0.5 * (wmin + wmax)
        self.wave = np.arange(wmin.value, wmax.value, dw.value) * wmin.unit

        # Extrapolate extinction
        spl = cspline(self.phot.atmo_wave, self.phot.atmo_ex)(self.wave)
        self.atmo_ex = pow(10, -0.4 * spl * airmass)

        flux = getattr(self,
                       func)()  # Apply the chosen function for the spectrum
        self.normalize(flux)
Esempio n. 5
0
    def extr_arms(self, n=3, slice_n=slice_n):

        for a in range(n):
            wave_extr = self.wave_grid(self.wmins[a], self.wmaxs[a])
            for s in range(slice_n):
                print("Extracting slice %i of arm %i..." % (s + 1, a + 1))
                i = a * slice_n + s
                x = range(self.sl_cen[i] - self.sl_hlength,
                          self.sl_cen[i] + self.sl_hlength)
                s_extr = np.empty(int(self.ysize.value))
                n_extr = np.empty(int(self.ysize.value))
                for p in range(int(self.ysize.value)):
                    y = self.image[p, self.sl_cen[i] -
                                   self.sl_hlength:self.sl_cen[i] +
                                   self.sl_hlength]
                    b1 = np.mean(
                        self.image[p, self.sl_cen[i] - self.sl_hlength +
                                   1:self.sl_cen[i] - self.sl_hlength + 6])
                    b2 = np.mean(
                        self.image[p, self.sl_cen[i] + self.sl_hlength -
                                   6:self.sl_cen[i] + self.sl_hlength - 1])
                    y = y - 0.5 * (b1 + b2)
                    dy = self.noise[p, self.sl_cen[i] -
                                    self.sl_hlength:self.sl_cen[i] +
                                    self.sl_hlength]
                    s_extr[p], n_extr[p] = getattr(self, 'extr_'+self.func)\
                        (y, dy=dy, mod=self.mod_init[i], x=x, p=p)
                if s == 0:
                    flux_extr = s_extr
                    err_extr = n_extr
                else:
                    flux_extr += s_extr
                    err_extr = np.sqrt(err_extr**2 + n_extr**2)

            dw = (wave_extr[2:] - wave_extr[:-2]) * 0.5
            dw = np.append(dw[:1], dw)
            dw = np.append(dw, dw[-1:])
            flux_extr = flux_extr / dw
            err_extr = err_extr / dw
            line = self.spec.ax.scatter(wave_extr, flux_extr, s=2, c='C0')

            if a == 0:
                axt = self.spec.ax.twinx()
                axt.set_ylabel('SNR per pixel')

            print("Median error: %2.3e" % np.median(err_extr))
            print("RMS: %2.3e" % np.sqrt(
                np.mean(
                    np.square(flux_extr[3000:3100] -
                              np.mean(flux_extr[3000:3100])))))

            wave_snr = wave_extr[::snr_sampl]
            snr_extr = flux_extr / err_extr
            snr_extr[np.where(np.isnan(snr_extr))] = 0
            snr_extr[np.where(np.isinf(snr_extr))] = 0
            snr = cspline(wave_extr, snr_extr)(wave_snr)
            linet = axt.scatter(wave_snr, snr, s=4, c='black')

            if a == 0:
                line.set_label('Extracted')
                linet.set_label('SNR')
                axt.text(self.wmaxs[2].value,
                         0,
                         "Median SNR: %2.1f" % np.median(snr),
                         ha='right',
                         va='bottom')
            axt.legend(loc=1)
    def get_radial_plot(self, local_dir=None):

        storage_dir = os.path.join(local_dir, 'storage')
        if not os.path.exists(storage_dir):
            os.mkdir(storage_dir)

        n = 40
        theta = np.linspace(0, 2 * np.pi, n)
        phi = np.linspace(0, 1 * np.pi, n)

        dr = 0.01
        rho_max = 4
        rho = np.arange(0, rho_max, dr)

        T, P = np.meshgrid(theta, phi)
        a, b, c = self.site_of_injection_in_model_mm

        X = np.cos(T) * np.sin(P)
        Y = np.sin(T) * np.sin(P)
        Z = np.cos(P)

        s = 0

        cumulative_vec = np.zeros((rho.size, 2))

        for k, r in enumerate(rho):

            x = r * X.ravel() + a
            y = r * Y.ravel() + b
            z = r * Z.ravel() + c

            z = self.map_model_z_mm_to_experimental_z_mm(z)
            u = self.interpolate_intensity(x, y, z)

            mode = get_mode(u).mode[0]
            mean = np.mean(u)
            mx = u.max()
            threshold = np.max((mode, mean))

            #print('Mean = {:0.1f}, Mode = {:0.1f}, Max = {:0.1f}'.\
            #format(mean, mode, mx))

            #u *= threshold < u

            u /= 255
            s += u.sum()
            cumulative_vec[k] = [r, s]

        x = cumulative_vec[:, 0]
        y = cumulative_vec[:, 1]

        stride = 4
        spline = cspline(\
                x[::stride],\
                y[::stride],\
                )
        spline_prime = spline.derivative()

        xp = np.linspace(0, rho_max, 100)
        yp = spline_prime(xp)

        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.plot(x, y, 'b-')
        fig.suptitle('Cumulative intensity')
        ax.set_xlabel('Distance from injection (mm)')
        txt = r'$C(r)='
        txt += r'\int_{0}^{r}'
        txt += r'\int_{0}^{\pi}'
        txt += r'\int_{0}^{2\pi}'
        txt += r'I(\rho,\phi,\theta)'
        txt += r'\, d\theta \, d\phi \, d\rho$'
        ax.set_ylabel(txt)
        ax.yaxis.set_ticks(np.linspace(0, y.max(), 4))
        ax.set_yscale('log')

        fname = 'integral_radial.pdf'
        fname = os.path.join(storage_dir, fname)
        fig.savefig(fname, dpi=300)

        fig.clf()
        ax = fig.add_subplot(111)

        ax.plot(xp, yp, 'b-')
        fig.suptitle('Radial intensity')
        ax.set_xlabel('Distance from injection (mm)')
        txt = r'$\log(dC/d\rho)$'
        ax.set_ylabel(txt)
        ax.set_yscale('log')

        fname = 'derivative_radial.pdf'
        fname = os.path.join(storage_dir, fname)
        fig.savefig(fname, dpi=300)
        plt.close('all')
Esempio n. 7
0
    def extr_arms(self, n=3, slice_n=slice_n):

        wave_snr = np.arange(self.wmins[0].value, self.wmaxs[2].value,
                             snr_sampl.value)
        for a in range(n):
            wave_extr = self.wave_grid(self.wmins[a], self.wmaxs[a])
            for s in range(slice_n):
                print("Extracting slice %i of arm %i..." % (s + 1, a + 1))
                i = a * slice_n + s
                x = range(self.sl_cen[i] - self.sl_hlength,
                          self.sl_cen[i] + self.sl_hlength)
                s_extr = np.empty(int(self.ysize.value))
                n_extr = np.empty(int(self.ysize.value))
                for p in range(int(self.ysize.value)):
                    y = self.image[p, self.sl_cen[i] -
                                   self.sl_hlength:self.sl_cen[i] +
                                   self.sl_hlength]
                    b1 = np.mean(
                        self.image[p, self.sl_cen[i] - self.sl_hlength +
                                   1:self.sl_cen[i] - self.sl_hlength + 6])
                    b2 = np.mean(
                        self.image[p, self.sl_cen[i] + self.sl_hlength -
                                   6:self.sl_cen[i] + self.sl_hlength - 1])
                    y = y - 0.5 * (b1 + b2)
                    dy = self.noise[p, self.sl_cen[i] -
                                    self.sl_hlength:self.sl_cen[i] +
                                    self.sl_hlength]
                    s_extr[p], n_extr[p] = getattr(self, 'extr_'+self.func)\
                        (y, dy=dy, mod=self.mod_init[i], x=x, p=p)
                if s == 0:
                    flux_extr = s_extr
                    err_extr = n_extr
                else:
                    flux_extr += s_extr
                    err_extr = np.sqrt(err_extr**2 + n_extr**2)
            dw = (wave_extr[2:] - wave_extr[:-2]) * 0.5
            dw = np.append(dw[:1], dw)
            dw = np.append(dw, dw[-1:])
            flux_extr = flux_extr / dw
            err_extr = err_extr / dw
            line = self.spec.ax.scatter(wave_extr, flux_extr, s=2, c='C0')
            #line, = self.spec.ax.step(wave_extr, flux_extr, linewidth=3, c='C0')

            print("Median error of extraction: %2.3e" % np.nanmedian(err_extr))
            flux_window = flux_extr[3000 // ccd_xbin:3100 // ccd_ybin]
            print("RMS of extraction: %2.3e" % np.sqrt(
                np.nanmean(np.square(flux_window - np.nanmean(flux_window)))))

            #wave_snr = wave_extr[::snr_sampl]
            snr_extr = flux_extr / err_extr
            snr_extr[np.where(np.isnan(snr_extr))] = 0
            snr_extr[np.where(np.isinf(snr_extr))] = 0
            snr_spl = cspline(wave_extr, snr_extr)(wave_snr)
            snr_spl[wave_snr < self.wmins[a].value] = 0.0
            snr_spl[wave_snr > self.wmaxs[a].value] = 0.0
            if a == 0:
                snr = snr_spl
                line.set_label('Extracted')
            else:
                snr = np.sqrt(snr**2 + snr_spl**2)
            #snr = uspline(wave_extr, snr_extr)(wave_snr)
            """
            linet, = self.spec.ax_snr.plot(wave_snr, snr, c='black')

            if a == 0:
                line.set_label('Extracted')
                linet.set_label('SNR')
                self.spec.ax_snr.text(self.wmaxs[2].value, 0,
                                      "Median SNR: %2.1f" % np.median(snr),
                                      ha='right', va='bottom')
            self.spec.ax_snr.legend(loc=2)
            """
        linet, = self.spec.ax_snr.plot(wave_snr,
                                       snr,
                                       linestyle='--',
                                       c='black')
        linet.set_label('SNR')
        self.spec.ax_snr.text(0.99,
                              0.92,
                              "Median SNR: %2.1f" % np.median(snr),
                              ha='right',
                              va='top',
                              transform=self.spec.ax_snr.transAxes)
        self.spec.ax_snr.legend(loc=2, fontsize=8)