def scintillation_parameters(self,
                                 plotbound=1.0,
                                 maxr=None,
                                 maxc=None,
                                 savefig=None,
                                 show=True,
                                 full_output=False,
                                 simple=False,
                                 eta=0.2,
                                 cmap=cm.binary,
                                 finitescintleerrors=True):
        if self.acf is None:
            self.acf2d()
        if self.dT is None:
            dT = 1
        else:
            dT = self.dT
        if self.dF is None:
            dF = 1
        else:
            dF = self.dF

        acfshape = np.shape(self.acf)
        centerrind = acfshape[0] // 2
        centercind = acfshape[1] // 2

        if simple:  # Do 1D slices
            NF = len(self.F)
            Faxis = (np.arange(-(NF - 1), NF, dtype=np.float) * np.abs(dF)
                     )  #why abs?
            NT = len(self.T)
            Taxis = (np.arange(-(NT - 1), NT, dtype=np.float) *
                     np.abs(dT))[1:-1]  #???

            #try:
            pout, errs = ffit.gaussianfit(Taxis[NT // 2:3 * NT // 2],
                                          self.acf[centerrind,
                                                   NT // 2:3 * NT // 2],
                                          baseline=True)
            f = interpolate.interp1d(
                Taxis,
                ffit.funcgaussian(pout, Taxis, baseline=True) -
                (pout[3] + pout[0] / np.e))
            delta_t_d = optimize.brentq(f, 0, Taxis[-1])
            #except:
            #    delta_t_d = 0.0
            #    print "dtd",pout
            #    plt.plot(Taxis,self.acf[centerrind,:])
            #    plt.plot(Taxis,f(Taxis))
            #    plt.show()

            #try:
            pout, errs = ffit.gaussianfit(Faxis[NF // 2:3 * NF // 2],
                                          self.acf[NF // 2:3 * NF // 2,
                                                   centercind],
                                          baseline=True)
            f = interpolate.interp1d(
                Faxis,
                ffit.funcgaussian(pout, Faxis, baseline=True) -
                (pout[3] + pout[0] / 2))
            delta_nu_d = optimize.brentq(f, 0, Faxis[-1])
            #except:
            #    delta_nu_d = 0
            #print "dnud",pout,Faxis[-1],dF
            #print Faxis
            #raise SystemExit
            #plt.plot(Faxis,self.acf[:,centercind])
            #plt.plot(Faxis,f(Faxis))
            #plt.show()

            #following Glenn's code and Cordes 1986 (Space Velocities...)
            # Errors from finite scintle effect:
            bw = self.getBandwidth()
            T = self.getTspan()
            if delta_t_d == 0.0:
                N_d = (1 + eta * bw / delta_nu_d)
            elif delta_nu_d == 0.0:
                N_d = (1 + eta * T / delta_t_d)
            else:
                N_d = (1 + eta * bw / delta_nu_d) * (1 + eta * T / delta_t_d)
            fse_nu_d = delta_nu_d / (2 * np.log(2) * np.sqrt(N_d)
                                     )  #log because of FWHM?
            fse_t_d = delta_t_d / (2 * np.sqrt(N_d))

            err_nu_d = fse_nu_d
            err_t_d = fse_t_d  #need to add in fitting errors

            if full_output:
                return delta_t_d, err_t_d, delta_nu_d, err_nu_d
            return delta_t_d, delta_nu_d

        # Look for the central peak in the ACF
        MIN = np.min(self.acf)
        if MIN < 0:  #The min value is approximately from a gaussian distribution
            MIN = np.abs(MIN)
        else:
            #center,hist = u.histogram(acf.flatten(),interval=0.001) #relies on 0.001
            #MIN = center[np.argmax(hist)]
            MIN = u.RMS(self.acf.flatten())
        if maxr is None:
            rslice = self.acf[centerrind:, centercind]
            maxr = np.where(rslice <= MIN)[0][0]
        if maxc is None:
            cslice = self.acf[centerrind, centercind:]
            maxc = np.where(cslice <= MIN)[0][0]

        plotacf = self.acf[int(centerrind - plotbound * maxr +
                               1):int(centerrind + plotbound * maxr),
                           int(centercind - plotbound * maxc +
                               1):int(centercind + plotbound * maxc + 1)]

        params, pcov = ffit.fitgaussian2d(
            plotacf)  #pcov already takes into account s_sq issue
        SHAPE = np.shape(plotacf)

        fit = ffit.gaussian2d(*params)
        amplitude, center_x, center_y, width_x, width_y, rotation, baseline = params

        paramnames = [
            "amplitude", "center_x", "center_y", "width_x", "width_y",
            "rotation", "baseline"
        ]
        if pcov is not None:
            paramerrors = np.sqrt(np.diagonal(pcov))
        else:
            paramerrors = np.zeros_like(params)
        if self.verbose:
            for i, param in enumerate(params):
                print("%s: %0.2e+/-%0.2e" %
                      (paramnames[i], param, paramerrors[i]))

        #Solve for scintillation parameters numerically

        try:
            delta_t_d = (optimize.brentq(
                lambda y: fit(SHAPE[0] // 2, y) - baseline - amplitude / np.e,
                (SHAPE[1] - 1) // 2, SHAPE[1] * 2) -
                         (SHAPE[1] - 1) // 2) * dT  #FWHM test
            if self.verbose:
                print("delta_t_d %0.3f %s" % (delta_t_d, self.Tunit))
        except ValueError:
            if self.verbose:
                print("ERROR in delta_t_d")
            delta_t_d = SHAPE[1] * dT
        if pcov is not None:
            err_t_d = paramerrors[3] * dT  #assume no rotaton for now
        else:
            err_t_d = None

        try:
            delta_nu_d = (optimize.brentq(
                lambda x: fit(x, SHAPE[1] // 2) - baseline - amplitude / 2.0,
                (SHAPE[0] - 1) // 2, SHAPE[0]) - (SHAPE[0] - 1) // 2) * dF
            if self.verbose:
                print("delta_nu_d %0.3f %s" % (delta_nu_d, self.Funit))
        except ValueError:
            if self.verbose:
                print("ERROR in delta_nu_d")
            delta_nu_d = SHAPE[0] * dF
        if pcov is not None:
            err_nu_d = paramerrors[4] * dF  #assume no rotaton for now
        else:
            err_nu_d = None

        err_rot = paramerrors[5]

        #finite-scintle errors
        if finitescintleerrors:
            bw = self.getBandwidth()
            T = self.getTspan()
            if delta_t_d == 0.0:
                N_d = (1 + eta * bw / delta_nu_d)
            elif delta_nu_d == 0.0:
                N_d = (1 + eta * T / delta_t_d)
            else:
                N_d = (1 + eta * bw / delta_nu_d) * (1 + eta * T / delta_t_d)
            fse_nu_d = delta_nu_d / (2 * np.log(2) * np.sqrt(N_d)
                                     )  #log because of FWHM?
            fse_t_d = delta_t_d / (2 * np.sqrt(N_d))

            fse_rot = rotation * np.sqrt((fse_nu_d / delta_nu_d)**2 +
                                         (fse_t_d / delta_t_d)**2)

            err_nu_d = np.sqrt(err_nu_d**2 + fse_nu_d**2)
            err_t_d = np.sqrt(err_t_d**2 + fse_t_d**2)
            err_rot = np.sqrt(err_rot**2 + fse_rot**2)

        if self.verbose:
            f = (dF / dT) * np.tan(rotation)
            df = (dF / dT) * np.cos(rotation)**2 * err_rot
            print("dnu/dt %0.3e+/-%0.3e %s/%s" %
                  (f, df, self.Funit, self.Tunit))  #((dF/dT)*np.tan(rotation))

        if show or savefig is not None:
            fig = plt.figure()
            ax = fig.add_subplot(211)
            u.imshow(self.data, cmap=cmap)
            ax = fig.add_subplot(212)

            u.imshow(plotacf, cmap=cmap)
            plt.colorbar()
            levels = (amplitude * np.array([1.0, 0.5, 1.0 / np.e])) + baseline
            levels = (amplitude * np.array([0.5])) + baseline
            #print(levels)

            ax.contour(fit(*np.indices(plotacf.shape)), levels, colors='k')
            #ax.set_xlim(len(xs)-20,len(xs)+20)
            #ax.set_ylim(len(ys)-10,len(ys)+10)
            if savefig is not None:
                plt.savefig(savefig)
            if show:
                plt.show()
        if full_output:
            return delta_t_d, err_t_d, delta_nu_d, err_nu_d, rotation, err_rot
        return delta_t_d, delta_nu_d, rotation
Beispiel #2
0
    def scintillation_parameters(self,plotbound=1.0):
        if self.acf is None:
            self.acf2d()
        if self.dT is None:
            dT = 1
        else:
            dT = self.dT
        if self.dF is None:
            dF = 1
        else:
            dF = self.dF


        acfshape = np.shape(self.acf)
        centerrind = acfshape[0]//2
        centercind = acfshape[1]//2
            
        # Look for the central peak in the ACF

        MIN = np.min(self.acf) 
        if MIN < 0: #The min value is approximately from a gaussian distribution
            MIN = np.abs(MIN)
        else:
            #center,hist = u.histogram(acf.flatten(),interval=0.001) #relies on 0.001
            #MIN = center[np.argmax(hist)]
            MIN = u.RMS(self.acf.flatten())
        rslice = self.acf[centerrind:,centercind]
        maxr = np.where(rslice<=MIN)[0][0]
        cslice = self.acf[centerrind,centercind:]
        maxc = np.where(cslice<=MIN)[0][0]

        plotacf = self.acf[centerrind-plotbound*maxr+1:centerrind+plotbound*maxr,centercind-plotbound*maxc+1:centercind+plotbound*maxc+1]

        
        params, pcov = ffit.fitgaussian2d(plotacf)

        fit = ffit.gaussian2d(*params)
        amplitude,center_x,center_y,width_x,width_y,rotation,baseline = params
        if self.verbose:
            paramnames = ["amplitude","center_x","center_y","width_x","width_y","rotation","baseline"]
            if pcov is not None:
                paramerrors = np.array(list(map(np.sqrt,np.diagonal(pcov)))) #multiply by s_sq!!!!!
            else:
                paramerrors = np.zeros_like(params)
            for i,param in enumerate(params):
                print("%s: %0.2e+/-%0.2e"%(paramnames[i],param,paramerrors[i]))
                

        #Solve for scintillation parameters numerically
        SHAPE = np.shape(plotacf)
        
        try:
            delta_t_d = (optimize.brentq(lambda y: fit(SHAPE[0]//2,y)-baseline-amplitude/np.e,(SHAPE[1]-1)//2,SHAPE[1]*2)-(SHAPE[1]-1)//2)*dT #FWHM test
            if self.verbose:
                print("delta_t_d %0.3f minutes"%delta_t_d)
        except ValueError:
            if self.verbose:
                print("ERROR in delta_t_d")
            delta_t_d = SHAPE[1]*dT

        try:
            delta_nu_d = (optimize.brentq(lambda x: fit(x,SHAPE[1]//2)-baseline-amplitude/2.0,(SHAPE[0]-1)//2,SHAPE[0])-(SHAPE[0]-1)//2)*dF
            if self.verbose:
                print("delta_nu_d %0.3f MHz"%delta_nu_d)
        except ValueError:
            if self.verbose:
                print("ERROR in delta_nu_d")
            delta_nu_d = SHAPE[0]*dF

        if self.verbose:
            print("dnu/dt %0.3f MHz/min" % ((dF/dT)*np.tan(rotation)))#((dF/dT)*np.tan(rotation))

            fig = plt.figure()
            ax = fig.add_subplot(211)
            u.imshow(self.data)
            ax = fig.add_subplot(212)

            u.imshow(plotacf)
            plt.colorbar()
            levels = (amplitude*np.array([1.0,0.5,1.0/np.e]))+baseline
            levels = (amplitude*np.array([0.5]))+baseline
            print(levels)

            ax.contour(fit(*np.indices(plotacf.shape)),levels, colors='k')
            #ax.set_xlim(len(xs)-20,len(xs)+20)
            #ax.set_ylim(len(ys)-10,len(ys)+10)
            plt.show()
        return delta_t_d,delta_nu_d,rotation #need to report errors
Beispiel #3
0
    def scintillation_parameters(self,
                                 plotbound=1.0,
                                 maxr=None,
                                 maxc=None,
                                 savefig=None,
                                 show=True,
                                 full_output=False):
        if self.acf is None:
            self.acf2d()
        if self.dT is None:
            dT = 1
        else:
            dT = self.dT
        if self.dF is None:
            dF = 1
        else:
            dF = self.dF

        acfshape = np.shape(self.acf)
        centerrind = acfshape[0] // 2
        centercind = acfshape[1] // 2

        # Look for the central peak in the ACF

        MIN = np.min(self.acf)
        if MIN < 0:  #The min value is approximately from a gaussian distribution
            MIN = np.abs(MIN)
        else:
            #center,hist = u.histogram(acf.flatten(),interval=0.001) #relies on 0.001
            #MIN = center[np.argmax(hist)]
            MIN = u.RMS(self.acf.flatten())
        if maxr is None:
            rslice = self.acf[centerrind:, centercind]
            maxr = np.where(rslice <= MIN)[0][0]
        if maxc is None:
            cslice = self.acf[centerrind, centercind:]
            maxc = np.where(cslice <= MIN)[0][0]

        plotacf = self.acf[centerrind - plotbound * maxr + 1:centerrind +
                           plotbound * maxr, centercind - plotbound * maxc +
                           1:centercind + plotbound * maxc + 1]

        params, pcov = ffit.fitgaussian2d(
            plotacf)  #pcov already takes into account s_sq issue
        SHAPE = np.shape(plotacf)

        fit = ffit.gaussian2d(*params)
        amplitude, center_x, center_y, width_x, width_y, rotation, baseline = params
        if self.verbose:
            paramnames = [
                "amplitude", "center_x", "center_y", "width_x", "width_y",
                "rotation", "baseline"
            ]
            if pcov is not None:
                paramerrors = np.sqrt(np.diagonal(pcov))
            else:
                paramerrors = np.zeros_like(params)
            for i, param in enumerate(params):
                print("%s: %0.2e+/-%0.2e" %
                      (paramnames[i], param, paramerrors[i]))

        #Solve for scintillation parameters numerically

        try:
            delta_t_d = (optimize.brentq(
                lambda y: fit(SHAPE[0] // 2, y) - baseline - amplitude / np.e,
                (SHAPE[1] - 1) // 2, SHAPE[1] * 2) -
                         (SHAPE[1] - 1) // 2) * dT  #FWHM test
            if self.verbose:
                print("delta_t_d %0.3f minutes" % delta_t_d)
        except ValueError:
            if self.verbose:
                print("ERROR in delta_t_d")
            delta_t_d = SHAPE[1] * dT
        if pcov is not None:
            err_t_d = paramerrors[3] * dT  #assume no rotaton for now
        else:
            err_t_d = None

        try:
            delta_nu_d = (optimize.brentq(
                lambda x: fit(x, SHAPE[1] // 2) - baseline - amplitude / 2.0,
                (SHAPE[0] - 1) // 2, SHAPE[0]) - (SHAPE[0] - 1) // 2) * dF
            if self.verbose:
                print("delta_nu_d %0.3f MHz" % delta_nu_d)
        except ValueError:
            if self.verbose:
                print("ERROR in delta_nu_d")
            delta_nu_d = SHAPE[0] * dF
        if pcov is not None:
            err_nu_d = paramerrors[4] * dF  #assume no rotaton for now
        else:
            err_nu_d = None

        err_rot = paramerrors[5]

        if self.verbose:
            print("dnu/dt %0.3f MHz/min" %
                  ((dF / dT) * np.tan(rotation)))  #((dF/dT)*np.tan(rotation))

        if show or savefig is not None:
            fig = plt.figure()
            ax = fig.add_subplot(211)
            u.imshow(self.data)
            ax = fig.add_subplot(212)

            u.imshow(plotacf)
            plt.colorbar()
            levels = (amplitude * np.array([1.0, 0.5, 1.0 / np.e])) + baseline
            levels = (amplitude * np.array([0.5])) + baseline
            print(levels)

            ax.contour(fit(*np.indices(plotacf.shape)), levels, colors='k')
            #ax.set_xlim(len(xs)-20,len(xs)+20)
            #ax.set_ylim(len(ys)-10,len(ys)+10)
            if savefig is not None:
                plt.savefig(savefig)
            if show:
                plt.show()
        if full_output:
            return delta_t_d, err_t_d, delta_nu_d, err_nu_d, rotation, err_rot
        return delta_t_d, delta_nu_d, rotation