Esempio n. 1
0
    def simulate(self, plot=False, **kwargs):
        """
        TODO Different run parameters can be passed through kwargs
        """

        waves, pdf = self.source_spectrum[0], self.source_spectrum[1]
        pdf_tot = scipy.integrate.simps(pdf, waves)
        t0 = time.time()
        d0 = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())

        # START SIMULATION
        log.info("Beginning simulation, %s", d0)
        for m in self.orders:
            # pre sample wavelengths
            mwaves, mpdf = wf.feed_spectrum(self, m, waves, pdf)
            if mwaves.size == 0 or mpdf.size == 0:
                log.warning("Order %s failed. Source spectrum does not have wavelengths in this range.", m)
                continue
            pdf_ratio = scipy.integrate.simps(mpdf, mwaves) / pdf_tot
            mnrays = int(pdf_ratio * self.nrays)
            # just in case we are given 0 rays to simulate
            while mnrays <= 0:
                mnrays = np.random.poisson(mnrays)
            waves_in = wf.sample_cdf(mwaves, mpdf, mnrays)
            self.nrays_per_order.append(mnrays)

            # pre sample slit
            slit_x, slit_y = self.slitfunc(mnrays)
            # normalize to unity
            slit_x /= self.slit_height
            slit_y /= self.slit_height

            # input through model
            assert slit_x.size == slit_y.size == waves_in.size
            self.modelfunc(m, waves_in, slit_x, slit_y)
        self.sim_time = time.time() - t0
        inds = np.where(self.outarr != 0)
        self.mean_rays_per_pixel = np.mean(self.outarr[inds])
        self.med_rays_per_pixel = np.median(self.outarr[inds])
        self.min_rays_per_pixel = np.min(self.outarr[inds])
        self.max_rays_per_pixel = np.max(self.outarr[inds])
        self.nrays_tot = np.sum(self.outarr[inds])
Esempio n. 2
0
    def simulate(self, plot=False, **kwargs):
        """
        TODO Different run parameters can be passed through kwargs
        """

        waves, pdf = self.source_spectrum[0], self.source_spectrum[1]
        
        pdf_tot = scipy.integrate.simps(pdf, waves)
        t0 = time.time()
        d0 = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())

        self.mwaves_mpdfs = []
        
        # START SIMULATION
        log.info("Beginning simulation, %s", d0)
        for m in self.orders:
            # pre sample wavelengths
            mwaves, mpdf = wf.feed_spectrum(self, m, waves, pdf)
            
            # Blaze function
            if self.blaze:
                blaze_eff, lambda_blaze = blazefunc.blaze_func(mwaves, m, self.echang, self.sigma_ech_inv, self.gamma_ech, self.blaze_ech)
                # New probability distribution with blaze efficiency embedded
                mpdf = np.multiply(mpdf, blaze_eff)            

            # save mwaves and mwaves_mpdfs
            self.mwaves_mpdfs.append((mwaves,mpdf))

            if mwaves.size == 0 or mpdf.size == 0:
                log.warning("Order %s failed. Source spectrum does not have wavelengths in this range.", m)
                continue

            pdf_ratio = scipy.integrate.simps(mpdf, mwaves) / pdf_tot
            mnrays = int(pdf_ratio * self.nrays)
            waves_in = wf.sample_cdf(mwaves, mpdf, mnrays)
            self.nrays_per_order.append(mnrays)

            # pre sample slit
            slit_x, slit_y = self.slitfunc(mnrays)

            # polarimeter
            if self.polarimeter:
                log.info("Adding polarimeter shift to ray coordinates")
                slit_x, slit_y = self.polarimeter_shift(m, waves_in, slit_x, slit_y)

            # normalize to unity
            slit_x /= self.slit_height
            slit_y /= self.slit_height

            # input through model
            assert slit_x.size == slit_y.size == waves_in.size
            self.modelfunc(m, waves_in, slit_x, slit_y)
        self.sim_time = time.time() - t0
        inds = np.where(self.outarr != 0)

   #     self.mean_rays_per_pixel = np.mean(self.outarr[inds])
   #     self.med_rays_per_pixel = np.median(self.outarr[inds])
   #     self.min_rays_per_pixel = np.min(self.outarr[inds])
   #     self.max_rays_per_pixel = np.max(self.outarr[inds])
   #     self.nrays_tot = np.sum(self.outarr[inds])

        #self.outwaves[inds] /= self.outarr[inds]
        
        # Because we get errors from inds if no rays hit the detectors.
        # For wavemap, overwrite outarray with wavelengths
        if (self.wavemap==True):
            self.outwaves /= self.outarr				# Normalise wavelengths
            self.outwaves[np.isnan(self.outwaves)]=0	# Remove eventual NaNs
            self.outarr = self.outwaves