Beispiel #1
0
def get_envelops(x, t=None):
    """ Find the upper and lower envelopes of the array `x`.
    """
    if t is None:
        t = np.arange(x.shape[0])
    maxima = argrelmax(x)[0]
    minima = argrelmin(x)[0]

    # consider the start and end to be extrema

    ext_maxima = np.zeros((maxima.shape[0] + 2,), dtype=int)
    ext_maxima[1:-1] = maxima
    ext_maxima[0] = 0
    ext_maxima[-1] = t.shape[0] - 1

    ext_minima = np.zeros((minima.shape[0] + 2,), dtype=int)
    ext_minima[1:-1] = minima
    ext_minima[0] = 0
    ext_minima[-1] = t.shape[0] - 1

    tck = interpolate.splrep(t[ext_maxima], x[ext_maxima])
    upper = interpolate.splev(t, tck)
    tck = interpolate.splrep(t[ext_minima], x[ext_minima])
    lower = interpolate.splev(t, tck)
    return upper, lower
Beispiel #2
0
def interpolate_1km_geolocation(lons_40km, lats_40km):
    """Interpolate AVHRR 40km navigation to 1km.

    This code was extracted from the python-geotiepoints package from the PyTroll group. To avoid adding another
    dependency to this package this simple case from the geotiepoints was copied.
    """
    cols40km = numpy.arange(24, 2048, 40)
    cols1km = numpy.arange(2048)
    lines = lons_40km.shape[0]
    # row_indices = rows40km = numpy.arange(lines)
    rows1km = numpy.arange(lines)

    lons_rad = numpy.radians(lons_40km)
    lats_rad = numpy.radians(lats_40km)
    x__ = EARTH_RADIUS * numpy.cos(lats_rad) * numpy.cos(lons_rad)
    y__ = EARTH_RADIUS * numpy.cos(lats_rad) * numpy.sin(lons_rad)
    z__ = EARTH_RADIUS * numpy.sin(lats_rad)
    along_track_order = 1
    cross_track_order = 3

    lines = len(rows1km)
    newx = numpy.empty((len(rows1km), len(cols1km)), x__.dtype)
    newy = numpy.empty((len(rows1km), len(cols1km)), y__.dtype)
    newz = numpy.empty((len(rows1km), len(cols1km)), z__.dtype)
    for cnt in range(lines):
        tck = splrep(cols40km, x__[cnt, :], k=cross_track_order, s=0)
        newx[cnt, :] = splev(cols1km, tck, der=0)
        tck = splrep(cols40km, y__[cnt, :], k=cross_track_order, s=0)
        newy[cnt, :] = splev(cols1km, tck, der=0)
        tck = splrep(cols40km, z__[cnt, :], k=cross_track_order, s=0)
        newz[cnt, :] = splev(cols1km, tck, der=0)

    lons_1km = get_lons_from_cartesian(newx, newy)
    lats_1km = get_lats_from_cartesian(newx, newy, newz)
    return lons_1km, lats_1km
Beispiel #3
0
    def _genJuddVos(self):
        '''
        '''
        try:
            from scipy import interpolate as interp
        except ImportError:
            raise ImportError('Sorry cannot import scipy')
            
        #lights = np.array([700, 546.1, 435.8])
        juddVos = np.genfromtxt('data/ciexyzjv.csv', delimiter=',')
        spec = juddVos[:, 0]
        juddVos = juddVos[:, 1:]

        juddVos[:, 0] *= 100. / sum(juddVos[:, 0]) 
        juddVos[:, 1] *= 100. / sum(juddVos[:, 1])
        juddVos[:, 2] *= 100. / sum(juddVos[:, 2])
        r, g, b = self.TrichromaticEquation(juddVos[:, 0], 
                                            juddVos[:, 1],
                                            juddVos[:, 2])
        juddVos[:, 0], juddVos[:, 1], juddVos[:, 2] = r, g, b

        L_spline = interp.splrep(spec, juddVos[:, 0], s=0)
        M_spline = interp.splrep(spec, juddVos[:, 1], s=0)
        S_spline = interp.splrep(spec, juddVos[:, 2], s=0)
        L_interp = interp.splev(self.spectrum, L_spline, der=0)
        M_interp = interp.splev(self.spectrum, M_spline, der=0)
        S_interp = interp.splev(self.spectrum, S_spline, der=0)

        JVinterp = np.array([L_interp, M_interp, S_interp]).T      
        
        return JVinterp
Beispiel #4
0
    def __startBarionicAccretionRate(self):

        np = 1000
        deltaz = self._zmax / float(np)

        z = [self._zmax - i * deltaz for i in range(np)]
        z.append(0)
        z = array(z)
        fbt2 = array([self.fbstruc(zi) for zi in z])
        ascale = array([1.0 / (1.0 + zi) for zi in z])
        self._ascale = ascale

        tck = spint.splrep(ascale, fbt2)
        ab3 = spint.splev(ascale, tck, der=1)

        def a5(z, i):
            a = 1.0 / (1.0 + z)
            a2 = a * a
            a3 = -1.0 * ab3[i] * a2
            a4 = a3
            a5 = self._cosmology.getRobr0() * abs(a4) \
                 / self._cosmology.dt_dz(z)
            return a5

        self._abt2 = array([a5(z[i], i) for i in range(z.size)])
        self._tck_ab = spint.splrep(self._ascale, self._abt2)
Beispiel #5
0
	def __init__(self, A, h, alpha, CL, CD, rho, smoothing=0, k_spline=3):
		self.A   = A
		self.h   = h
		self.Asp = 2*self.h**2/self.A

		self.rho = rho

		# Input lift and drag data
		self.n     = len(alpha)
		self.alpha = alpha
		self.CL    = CL
		self.CD    = CD

		self.k_spline = k_spline

		# -------- Spline interpolation -----------------------------
		if len(self.alpha.shape) == 1:
			self.interpolationMethod = 'spline'
			self.nrControlParameters = 1
			self.CL_spline = interpolate.splrep(self.alpha, self.CL, s=smoothing, k=self.k_spline)
			self.CD_spline = interpolate.splrep(self.alpha, self.CD, s=smoothing, k=self.k_spline)
		elif len(self.alpha.shape) == 2:
			self.interpolationMethod = 'griddata'
			self.nrControlParameters = 2
			self.CL_RbfModel = interpolate.Rbf(self.alpha[:, 0],self.alpha[:, 1], self.CL, smooth=smoothing)
			self.CD_RbfModel = interpolate.Rbf(self.alpha[:, 0],self.alpha[:, 1], self.CD, smooth=smoothing)
Beispiel #6
0
    def _interp1d(self):
        """Interpolate in one dimension."""
        lines = len(self.hrow_indices)

        self.newx = np.empty((len(self.hrow_indices),
                              len(self.hcol_indices)),
                             self.x__.dtype)

        self.newy = np.empty((len(self.hrow_indices),
                              len(self.hcol_indices)),
                             self.y__.dtype)

        self.newz = np.empty((len(self.hrow_indices),
                              len(self.hcol_indices)),
                             self.z__.dtype)

        for cnt in range(lines):
            tck = splrep(self.col_indices, self.x__[cnt, :], k=self.ky_, s=0)
            self.newx[cnt, :] = splev(self.hcol_indices, tck, der=0)

            tck = splrep(self.col_indices, self.y__[cnt, :], k=self.ky_, s=0)
            self.newy[cnt, :] = splev(self.hcol_indices, tck, der=0)

            tck = splrep(self.col_indices, self.z__[cnt, :], k=self.ky_, s=0)
            self.newz[cnt, :] = splev(self.hcol_indices, tck, der=0)
Beispiel #7
0
    def RealParts(self, om):
        """ calculate functions on real axis
        """
        Func=[]
        derivs=[]
        for i in range(len(self.pos)):
            En = self.pos[i]

            (x0, dh0) = swing_make_mesh(300, 1e-2*abs(En), 2*abs(En), 7*abs(En))

            wb = array([self.F0(x+En, En) for x in x0])
            
            F0j = array([self.F0(x, En) for x in om])

            Frc = kramskron(om, F0j, wb, x0, dh0, En)

            spl = interpolate.splrep(om, real(Frc), k=3, s=0.0)            
            dersr = [interpolate.splev(0.0, spl, der=0),
                     interpolate.splev(0.0, spl, der=1),
                     interpolate.splev(0.0, spl, der=2)]

            spl = interpolate.splrep(om, imag(Frc), k=3, s=0.0)
            dersi = [interpolate.splev(0.0, spl, der=0),
                     interpolate.splev(0.0, spl, der=1),
                     interpolate.splev(0.0, spl, der=2)]

            Func.append(Frc)
            ders = array(dersr + dersi)
            derivs.append(array(ders))
        return (Func, derivs)
Beispiel #8
0
def ladybug_interp_data( data ):
    '''Interpolates between each 'new' GPS coordinate. Repeated coordinates are
    assumed to be incorrect/inaccurate. This method replaces all repetitions
    with interpolated coordinates in place. This method uses cubic spline
    interpolation.
    '''
    for i in reversed(xrange(1,len(data['lon']))):
        if data['lon'][i] == data['lon'][i-1]:
            data['lon'][i] = 1000.0
            data['valid'][i] = False
    select = where(data['lon'] < 999)

    # SPLINE VERSION
    data['alt'] = interpolate.splev(data['seqid'],
                                    interpolate.splrep(data['seqid'][select],
                                                       data['alt'][select],
                                                       s=0, k=2  ),
                                    der=0)
    data['lon'] = interpolate.splev(data['seqid'],
                                    interpolate.splrep(data['seqid'][select],
                                                       data['lon'][select],
                                                       s=0, k=2  ),
                                    der=0)
    data['lat'] = interpolate.splev(data['seqid'],
                                    interpolate.splrep(data['seqid'][select],
                                                       data['lat'][select],
                                                       s=0, k=2  ),
                                    der=0)
    return data
Beispiel #9
0
 def setGrid(self):
     from scipy import interpolate
     x0 = numpy.logspace(-3,1,81)
     etas = numpy.linspace(0.,2.,21)
     qs = numpy.linspace(0.2,1.,17)
     grid1 = numpy.empty((x0.size,x0.size,etas.size,qs.size))
     grid2 = numpy.empty(grid1.shape)
     for i in range(qs.size):
         q = qs[i]
         q2 = q**2
         b = 1-q2
         for j in range(etas.size):
             eta = etas[j]
             g = 0.5*eta-1. # g = -1*gamma
             for k in range(x0.size):
                 x = x0[k]
                 for l in range(x0.size):
                     y = x0[l]
                     qb = ((2*x*y)/b)**g  # q_bar
                     qt = q*(x*y)**0.5/b  # q_tilde
                     sb = 0.5*(x/y - y/x) + s**2*b/(2*x*y)
                     nu1 = s**2*b/(2*x*y)
                     nu2 = nu1+ 0.5*b*(x/y + y/(x*q2))
                     nu = numpy.logspace(nu1,nu2,1001)
                     mu = nu-sb
                     t = (1+mu**2)**0.5
                     f1 = (t-mu)**0.5/t
                     f2 = (t+mu)**0.5/t
                     ng = nu**g
                     I1 = interpolate.splrep(nu,f1*ng)
                     I2 = interpolate.splrep(nu,f2*ng)
                     grid1[k,l,i,j] = qt*interpolate.splint(nu1,nu2,I1)
                     grid2[k,l,i,j] = qt*interpolate.splint(nu1,nu2,I2)
             pylab.imshow(grid1[:,:,i,j])
             pylab.show()
Beispiel #10
0
def interp_data_fixed_num(data_set, num=100):
    """
    interpolate data with fixed number of points
    """
    interp_data = dict()
    for key in data_set:
        interp_data[key] = []
        for l in data_set[key]:
            interp_letter = []
            for s in l:
                time_len = s.shape[0]
                if time_len > 3:
                    #interpolate each dim, cubic
                    t = np.linspace(0, 1, time_len)
                    spl_x = interpolate.splrep(t, s[:, 0])
                    spl_y = interpolate.splrep(t, s[:, 1])
                    #resample, 4 times more, vel is also scaled...
                    t_spl = np.linspace(0, 1, num)
                    x_interp = interpolate.splev(t_spl, spl_x, der=0)
                    y_interp = interpolate.splev(t_spl, spl_y, der=0)
                    # #construct new stroke
                    data = np.concatenate([x_interp, y_interp])
                    dt = float(time_len)/num
                    interp_letter.append(np.concatenate([data, [dt]]))
                else:
                    #direct copy if no sufficient number of points
                    interp_letter.append(s)
            interp_data[key].append(interp_letter)
    return interp_data
Beispiel #11
0
def expand_traj_dim_with_derivative(data, dt=0.01):
    augmented_trajs = []
    for traj in data:
        time_len = len(traj)
        t = np.linspace(0, time_len * dt, time_len)
        if time_len > 3:
            if len(traj.shape) == 1:
                """
                mono-dimension trajectory, row as the entire trajectory...
                """
                spl = interpolate.splrep(t, traj)
                traj_der = interpolate.splev(t, spl, der=1)
                tmp_augmented_traj = np.array([traj, traj_der]).T
            else:
                """
                multi-dimensional trajectory, row as the state variable...
                """
                tmp_traj_der = []
                for traj_dof in traj.T:
                    spl_dof = interpolate.splrep(t, traj_dof)
                    traj_dof_der = interpolate.splev(t, spl_dof, der=1)
                    tmp_traj_der.append(traj_dof_der)
                tmp_augmented_traj = np.vstack([traj.T, np.array(tmp_traj_der)]).T

            augmented_trajs.append(tmp_augmented_traj)

    return augmented_trajs
Beispiel #12
0
def Interpo(spectra) :
    wave_min = 1000
    wave_max = 20000
    pix = 2
    #wavelength = np.linspace(wave_min,wave_max,(wave_max-wave_min)/pix+1)  #creates N equally spaced wavelength values
    wavelength = np.arange(ceil(wave_min), floor(wave_max), dtype=int, step=pix)
    fitted_flux = []
    fitted_error = []
    new = []
    #new = Table()
    #new['col0'] = Column(wavelength,name = 'wavelength')
    new_spectrum=spectra	#declares new spectrum from list
    new_wave=new_spectrum[:,0]	#wavelengths
    new_flux=new_spectrum[:,1]	#fluxes
    new_error=new_spectrum[:,2]   #errors
    lower = new_wave[0] # Find the area where interpolation is valid
    upper = new_wave[len(new_wave)-1]
    lines = np.where((new_wave>lower) & (new_wave<upper))	#creates an array of wavelength values between minimum and maximum wavelengths from new spectrum
    indata=inter.splrep(new_wave[lines],new_flux[lines])	#creates b-spline from new spectrum
    inerror=inter.splrep(new_wave[lines],new_error[lines]) # doing the same with the errors
    fitted_flux=inter.splev(wavelength,indata)	#fits b-spline over wavelength range
    fitted_error=inter.splev(wavelength,inerror)   # doing the same with errors
    badlines = np.where((wavelength<lower) | (wavelength>upper))
    fitted_flux[badlines] = 0  # set the bad values to ZERO !!! 
    new = Table([wavelength,fitted_flux],names=('col1','col2')) # put the interpolated data into the new table    
    #newcol = Column(fitted_flux,name = 'Flux')  
    #new.add_column(newcol,index = None)
    return new
Beispiel #13
0
def interp_data(data_set):
    """
    interpolate data
    """
    interp_data = dict()
    for key in data_set:
        interp_data[key] = []
        for l in data_set[key]:
            interp_letter = []
            for s in l:
                time_len = s.shape[0]
                if time_len > 3:
                    #interpolate each dim, cubic
                    t = np.linspace(0, 1, time_len)
                    spl_x = interpolate.splrep(t, s[:, 0])
                    spl_y = interpolate.splrep(t, s[:, 1])
                    #resample, 4 times more, vel is also scaled...
                    t_spl = np.linspace(0, 1, 4 * len(t))
                    x_interp = interpolate.splev(t_spl, spl_x, der=0)
                    y_interp = interpolate.splev(t_spl, spl_y, der=0)
                    # #construct new stroke
                    interp_letter.append(np.concatenate([[x_interp], [y_interp]], axis=0).transpose())
                else:
                    #direct copy if no sufficient number of points
                    interp_letter.append(s)
            interp_data[key].append(interp_letter)
    return interp_data
Beispiel #14
0
    def calc_spline_sqrt_coefs(self):
        """
        Calculate cubic spline coefficients for a set of curves.

        INPUT:
          None

        RETURN:
          None

        For further usage compute spline coefficients for
        rmnc_b(s), zmns_b(s), pmns_b(s).

        Divide odd m terms by sqrt(s), 
        keep even m terms as they are.
        Use 3rd order qubic spline.

        ATTENTION: For interpolation keep in mind to 're-do' sqrt(s)!

        For evaluation/interpolation: 
         Use splev_sqrt(). This routine calls splev() which is
         to be found in from scipy.interpolate.
         Make sure that splev is imported from scipy.interpolate.
         Call like:      ynew = splev_sqrt(xnew,sqrt_tck,der=0)
         or for derivs   yder = splev_sqrt(xnew,sqrt_tck,der=1).
        """

        # as we shall not divide by zero...
        eps = 1.e-30
        self.x = numpy.where(self.x >= eps, self.x, eps)

        # do some checks
        if(self.y.shape[0] != self.x.shape[0]):
            raise Exception, "".join(["dimensions do not agree for: y[n,m]",
                                      "and x[n]"])
        if(self.y.shape[1] != self.ixm.shape[0]):
            raise Exception, "".join(["dimensions do not agree for: y[n,m]",
                                      "and ixm[m]"])

        self.sqrt_tck=[]
        if(len(self.y.shape) == 1):
            self.sqrt_tck.append(interpolate.splrep(self.x, self.y, s=0))
        else:
            sqx = numpy.sqrt(self.x)
            n = self.y.shape[1]
            for i in range(n):
                if(self.ixm[i]%2 == 0):
                    self.sqrt_tck.append(interpolate.splrep(self.x, 
                                                            self.y[:,i], 
                                                            s=0))
                else:
                    self.sqrt_tck.append(interpolate.splrep(self.x, 
                                                            self.y[:,i]/sqx, 
                                                            s=0))

        self.__kind_computed__ |= self.SQRT_SPLINE
        self.importQuantity('sqrt_tck', self.sqrt_tck)
        self.importQuantity('__kind_computed__', self.__kind_computed__)

        return
Beispiel #15
0
def VegaFilterMagnitude(filter,spectrum,redshift):
    """
    Determines the Vega magnitude (up to a constant) given an input filter,
        SED, and redshift.
    """
    from scipy.interpolate import splev,splint,splrep
    from scipy.integrate import simps
    from math import log10

    wave = spectrum[0].copy()
    data = spectrum[1].copy()

    # Redshift the spectrum and determine the valid range of wavelengths
    wave *= (1.+redshift)
    data /= (1.+redshift)
    wmin,wmax = filter[0][0],filter[0][-1]
    cond = (wave>=wmin)&(wave<=wmax)

    # Evaluate the filter at the wavelengths of the spectrum
    response = splev(wave[cond],filter)

    # Determine the total observed flux (without the bandpass correction)
    observed = splrep(wave[cond],(response*data[cond]),s=0,k=1)
    flux = splint(wmin,wmax,observed)

    # Determine the magnitude of Vega through the filter
    vwave,vdata = getSED('Vega')
    cond = (vwave>=wmin)&(vwave<=wmax)
    response = splev(vwave[cond],filter)
    vega = splrep(vwave[cond],response*vdata[cond],s=0,k=1)
    vegacorr = splint(wmin,wmax,vega)

    return -2.5*log10(flux/vegacorr)#+2.5*log10(1.+redshift)
Beispiel #16
0
def equilData(krange,back, Nbefore):
    zzzOutL=np.array([])
    zzOutL=np.array([])
    fnlOutL=np.array([])    
    times=np.array([])    
    Hin = np.array([])    
    
    for ii in range(0,np.size(krange)):
        k=krange[ ii]    
        k1 = k
        k2=k
        k3=k
        for jj in range(0,np.size(back[:,0])):
            Hin[jj]=MTeasyPy.H(back[jj,1:])
            
        Nexit = interpolate.splev(k, np.exp(back[:,0])*Hin,interpolate.splrep(back[:,1], back[:,0], s=1e-15),der=0)

        Nstart = Nexit - Nbefore
        backExitMinus = np.zeros(2*nF)
        for i in range (1,2*nF+1):
            backExitMinus[i-1] = interpolate.splev(Nstart,interpolate.splrep(back[:,0], back[:,i], s=1e-15),der=0)
            
        start_timeIn = time.time()

# run solver for this triangle
 
        t=np.linspace(Nstart,Nend, 10)
        threePt = MTeasyPy.alphaEvolve(t,k1,k2,k3, backExitMinus, pvalue, 1)
        zzz= threePt[:,:5]
        zzzOutL = np.append(zzzOutL, zzz[-1,4])
        zzOutL = np.append(zzOutL, zzz[-1,1])
        times = np.append(times, time.time()-start_timeIn) 
    return (zzOutL, zzzOutL, times)
Beispiel #17
0
    def calc_spline_conv_coefs(self):
        """
        Calculate cubic spline coefficients for a set of curves.

        INPUT:
          None

        RETURN:
          None

        For evaluation/interpolation: 
         Make sure that splev is imported from scipy.interpolate.
         Call like:      ynew = splev(xnew,tck,der=0)
         or for derivs   yder = splev(xnew,tck,der=1).
        """

        # do some checks
        if(self.y.shape[0] != self.x.shape[0]):
            raise Exception, "".join(["dimensions do not agree for: y[n,m]",
                                      "and x[n]"])

        self.tck=[]
        if(len(self.y.shape) == 1):
            self.tck.append(interpolate.splrep(self.x, self.y, s=0))
        else:
            n = self.y.shape[1]
            for i in range(n):
                self.tck.append(interpolate.splrep(self.x, self.y[:,i], s=0))

        self.__kind_computed__ |= self.CONV_SPLINE
        self.importQuantity('tck', self.tck)
        self.importQuantity('__kind_computed__', self.__kind_computed__)

        return
Beispiel #18
0
 def apply_map(self, tau, kind = 'linear'):
     """        
     Actually use the motor map to calculate the slip speed and the motor
     efficiency
     
     Parameters
     ----------
     tau : float
         Torque [N-m]
     kind : string, optional
         The kind of interpolation to do (see scipy.interpolate.interp1d) - deprecated
         
     Returns
     -------
     eta : float
         Efficiency [-]
     omega : float
         Rotational speed [rad/s]
     """
     if not kind == 'linear':
         warnings.warn('apply_map does not take parameter "kind" anymore')
         
     eta_interp = interp.splrep(self.tau_coeffs, self.eta_coeffs, k=2, s=0)
     eta = interp.splev(tau, eta_interp)
     omega_interp = interp.splrep(self.tau_coeffs, self.omega_coeffs, k=2, s=0)
     omega = interp.splev(tau, omega_interp)
     
     #Return the values
     return eta, omega
def getInitialCurve(im, nbr_points):
    """ Select a number of vertices for builing a cubic spline planar 
    curve and return a set of nbr_points of that curve, with equidistant 
    parametrization.    
    """
    im = np.array(im)
    plt.gray()
    plt.imshow(np.flipud(im), origin='lower')
    vertices = np.array(plt.ginput(0, timeout=10))
    # I add an extra point to close the curve
    # this is for spline interpolation
    # I will remove it from the interpolant
    vx = vertices[:,0]
    vx = np.append(vx, vx[0])
    vy = vertices[:,1]
    vy = np.append(vy, vy[0])

    nbr_vertices = len(vx)
    t = np.linspace(0,nbr_vertices,nbr_vertices) #returns an array of numbers from 0 to nbr of clicks+1. Num of samples are also nbr of clicks+1. E.g. 4 clicks: ([0., x.x, x.x, x.x, 5.])
    sx = interpolate.splrep(t, vx, s= 0) 
    sy = interpolate.splrep(t, vy, s= 0)
    tnew = np.linspace(0, nbr_vertices, nbr_points+1)
    
    vxnew = interpolate.splev(tnew, sx, der=0)
    vynew = interpolate.splev(tnew, sy, der=0)
    # I don't want the last point
    return vxnew[:-1], vynew[:-1], vertices
def bspline(pts, degree, smoothness):
    # cycle check
    periodic = False
    if pts[0] == pts[-1]:
        # pts.pop()
        periodic = True
        pts = pts[:-1]

    if periodic: pts = pts + pts[0 : degree+1]
    else: pts = [pts[0]] + pts + [pts[-1],pts[-1]]

    pts = array(pts)
    n_points = len(pts)
    x, y = pts[:,0], pts[:,1]

    t = range(len(x))
    ipl_t = linspace(1.0, len(pts) - degree, smoothness)

    x_tup = si.splrep(t, x, k=degree, per=periodic)
    y_tup = si.splrep(t, y, k=degree, per=periodic)
    x_list = list(x_tup)
    xl = x.tolist()

    y_list = list(y_tup)
    yl = y.tolist()

    if periodic:
        x_list[1] = [0.0] + xl + [0.0, 0.0, 0.0, 0.0]
        y_list[1] = [0.0] + yl + [0.0, 0.0, 0.0, 0.0]

    x_i = si.splev(ipl_t, x_list)
    y_i = si.splev(ipl_t, y_list)

    return zip(x_i, y_i)
Beispiel #21
0
def upsample_to_memmap(x,factor=10):
    """Performs upsampling using splines and returns a memmap. Now works for 1d, 2d and 3d arrays."""
    factor = int(round(factor))
    if factor<1 or factor>1000000:
        raise ValueError("Illegal value for the upsampling-factor")
    if len(x.shape) == 1: #1d 
        #y = np.zeros((factor*x.shape[0]),"d")
        spl = splrep(np.arange(x.shape[0]),x)
        #f = interp1d(np.arange(x.shape[0]),x)
        y,tmp_fn = tmp_memmap(dtype=np.double,shape=(x.shape[0]*factor),mode="w+")
        y[:] = splev(np.arange(0,x.shape[0],1.0/factor),spl)
        #y[:-factor] = f(np.arange(0,x.shape[0],1.0/factor)[:-factor])
        #y[:] = resample(x,x.shape[0]*factor)
    #TODO: make kind of interpolation consistent for different dimensionalities
    elif len(x.shape) == 2: #1d 
        y,tmp_fn = tmp_memmap(dtype=np.double,shape=(x.shape[0]*factor,x.shape[1]),mode="w+")
        for i in range(x.shape[1]):
            spl = splrep(np.arange(x.shape[0]),x[:,i])
            y[:,i] = splev(np.arange(0,x.shape[0],1.0/factor),spl)
    elif len(x.shape) == 3: #1d 
        y,tmp_fn = tmp_memmap(dtype=np.double,shape=(x.shape[0]*factor,x.shape[1],x.shape[2]),mode="w+")
        for i in range(x.shape[1]):
            for j in range(x.shape[2]):
                spl = splrep(np.arange(x.shape[0]),x[:,i,j])
                y[:,i,j] = splev(np.arange(0,x.shape[0],1.0/factor),spl)
    else:
        raise ValueError("Only 1d, 2d and 3d arrays are accepted for upsampling up to now.")
    return y, tmp_fn
Beispiel #22
0
	def pgrid(self,tname):

		self._hasTeff_ = True
		self._hasLogg_ = True
		self._hasMet_ = False
		
		grid = Table.read(tname,format='ascii.no_header')

		len_teff = len(np.unique(grid['col2']))
		len_logg = len(np.unique(grid['col3']))

		# Reads Teff

		self.gteff = np.zeros(len_teff)

		for i,ii in enumerate(np.arange(len_teff)*len_logg):
			self.gteff[i] = np.float(os.path.basename(grid['col1'][ii])[2:].split('_')[0])

		# Reads logg
		
		self.glogg = np.zeros(len_logg)

		for i,ii in enumerate(np.arange(len_logg)):
			self.glogg[i] = np.float(os.path.basename(grid['col1'][ii]).split('_')[1].split('.')[0])

		# evalute grid interpolation

		self._teffc = itrp.splrep(np.arange(len_teff),self.gteff)
		self._loggc = itrp.splrep(np.arange(len_logg),self.glogg)
Beispiel #23
0
 def GetSourceSize(self,kpc=False):
     self.z=source_redshifts[self.name]
     self.Da = astCalc.da(self.z)
     self.scale = self.Da*1e3*np.pi/180./3600.
     if len(self.srcs) == 1 or self.name == 'J0837':
         self.Re_v = self.Ddic['Source 1 re']*0.05
         self.Re_i = self.Re_v.copy()
         self.Re_lower = self.Ldic['Source 1 re']*0.05
         self.Re_upper = self.Udic['Source 1 re']*0.05
     elif len(self.srcs) == 2 and self.name != 'J0837':
         print 'test this out...!'
         Xgrid = np.logspace(-4,5,1501)
         Res = []
         for i in range(len(self.imgs)):
             #if self.name == 'J1605':
             #    source = 
             source = self.fits[i][-3]*self.srcs[0].eval(Xgrid) + self.fits[i][-2]*self.srcs[1].eval(Xgrid)
             R = Xgrid.copy()
             light = source*2.*np.pi*R
             mod = splrep(R,light,t=np.logspace(-3.8,4.8,1301))
             intlight = np.zeros(len(R))
             for i in range(len(R)):
                 intlight[i] = splint(0,R[i],mod)
             model = splrep(intlight[:-300],R[:-300])
             
             if len(model[1][np.where(np.isnan(model[1])==True)]>0):
                 print "arrays need to be increasing monotonically! But don't worry about it"
                 model = splrep(intlight[:-450],R[:-450])
             reff = splev(0.5*intlight[-1],model)
             Res.append(reff*0.05)
         self.Re_v,self.Re_i = Res
     if kpc:
         return [self.Re_v*self.scale, self.Re_i*self.scale]
     return [self.Re_v, self.Re_i]
def qvRealtion(voltage):

    global chargeVoltage, chargeQuantity
	
    vPoints = chargeVoltage
    qPoints = chargeQuantity
    SOCPoints = chargeSOC
	
    qvCurveTck = interpolate.splrep(vPoints, qPoints, s = 0.002)
    batteryQuantity = interpolate.splev(voltage, qvCurveTck, der = 0)
    SOCVCurveTck = interpolate.splrep(SOCPoints, vPoints,s = 0.002)
    batterySOC = interpolate.splev(voltage, SOCVCurveTck, der = 0)
	
    newX = np.arange(vPoints[0], vPoints[-1], 0.1)
    newY = interpolate.splev(newX, qvCurveTck, der = 0)
	
    plt.plot(vPoints, qPoints, 'o', newX, newY, '-')
    plt.title("V-Q curve")
	
    newSOC = np.arange(SOCPoints[0], SOCPoints[-1], 0.01)
    newVoltage = interpolate.splev(newSOC, SOCVCurveTck, der = 0)
	
    plt.plot(SOCPoints, vPoints, 'o', newSOC, newVoltage, '-')
    plt.title("V-SOC curve")
    plt.show()
	
    return batteryQuantity
Beispiel #25
0
def conv2(lam,flu,ac,bc,rot):
	
	lux = 299792.458
	
	WAV = np.array([3530,4860,6260,7670,8350])
		
	"""Determining Limb Darkening Coefficients"""
	tck = interpolate.splrep(WAV,ac,k=3,s=0)
	A = interpolate.splev(lam,tck,der=0)
	tck = interpolate.splrep(WAV,bc,k=3,s=0)
	B = interpolate.splev(lam,tck,der=0)
		
	F = flu.copy()
	
	MIN = lam * ( 1. - float(rot) / lux )
	MAX = lam * ( 1. + float(rot) / lux )
	
	lar = len(F)
	
	if rot > 0.0:
		H = Cfunctions.Conv(lam.astype("double"),flu.astype("double"),F.astype("double"), \
		    A.astype("double"),B.astype("double"),MIN.astype("double"),MAX.astype("double"),rot,lar)
	else:
		H = flu
	
	Fnu = np.array(H)

	return Fnu
Beispiel #26
0
def get_optimal_splines(events, optimise_bin_edges, k=1):

    cut_events = {}
    cut_energies, ga_cuts, xi_cuts = [], [], []
    for elow, ehigh, emid in zip(optimise_bin_edges[:-1],
                                 optimise_bin_edges[1:],
                                 np.sqrt(optimise_bin_edges[:-1] *
                                         optimise_bin_edges[1:])):

        for key in events:
            cut_events[key] = events[key][
                (events[key]["MC_Energy"] > elow) &
                (events[key]["MC_Energy"] < ehigh)]

        res = optimize.differential_evolution(
            cut_and_sensitivity,
            bounds=[(.5, 1), (0, 0.5)],
            maxiter=1000, popsize=10,
            args=(cut_events,
                  np.array([elow / energy_unit,
                            ehigh / energy_unit]) * energy_unit,
                  alpha)
        )

        if res.success:
            cut_energies.append(emid.value)
            ga_cuts.append(res.x[0])
            xi_cuts.append(res.x[1])

    spline_ga = interpolate.splrep(cut_energies, ga_cuts, k=k)
    spline_xi = interpolate.splrep(cut_energies, xi_cuts, k=k)

    return (spline_ga, ga_cuts), (spline_xi, xi_cuts)
Beispiel #27
0
def envelope(min_arr, max_arr, N, periodic=0):
    #Cubic Spline by default
    order_max = 3
    order_min = 3

    min_arr = np.asarray(min_arr)
    max_arr = np.asarray(max_arr)

    if min_arr.shape[1] < 4:
        order_min = 1 #Do linear interpolation if not enough points
    elif min_arr.shape[1] < 5:
        order_min = 2 #Do quad interpolation if not enough points
    else:
        order_min = 3

    if max_arr.shape[1] < 4:
        order_max = 1  #Do linear interpolation if not enough points
    elif max_arr.shape[1] < 5:
        order_max = 2 #Do quad interpolation if not enough points
    else:
        order_max = 3
    # Mirror Method requires per flag = 1
    # No extrapolation requires per flag = 0
    t = interpolate.splrep(*min_arr, k=order_min, per=periodic)
    top = interpolate.splev(np.arange(N), t)

    b = interpolate.splrep(*max_arr, k=order_max, per=periodic)
    bot = interpolate.splev(np.arange(N), b)
    mean = (top + bot)/2

    return mean
Beispiel #28
0
def Interpo (wave, flux, variance) :
    wave_min = 1500
    wave_max = 12000
    dw = 2

    #wavelength = np.linspace(wave_min,wave_max,(wave_max-wave_min)/pix+1)
    wavelength = np.arange(math.ceil(wave_min), math.floor(wave_max), dtype=int, step=dw) #creates N equally spaced wavelength values
    inter_flux = []
    inter_var  = []
    output     = []

    lower = wave[0] # Find the area where interpolation is valid
    upper = wave[-1]

    good_data = np.where((wave >= lower) & (wave <= upper))	#creates an array of wavelength values between minimum and maximum wavelengths from new spectrum

    influx = inter.splrep(wave[good_data], flux[good_data])	#creates b-spline from new spectrum

    invar  = inter.splrep(wave[good_data], variance[good_data]) # doing the same with the errors

    inter_flux = inter.splev(wavelength, influx)	#fits b-spline over wavelength range
    inter_var  = inter.splev(wavelength, invar)   # doing the same with errors

    missing_data = np.where((wavelength < lower) | (wavelength > upper))
    inter_flux[missing_data] = float('NaN')  # set the bad values to NaN !!!
    inter_var[missing_data] =  float('NaN')

    output = np.array([wavelength, inter_flux, inter_var]) # put the interpolated data into the new table

    return output # return new table
Beispiel #29
0
def fit_fwhm_enclosed_direct(peak, rad, flux):

    # We use splines to interpolate and derivate
    spl = splrep(rad, flux)
    # First derivative
    vald1 = splev(rad, spl, der=1)

    splinter = splrep(rad, vald1 - math.pi * peak * rad)

    roots = sproot(splinter)
    nroots = len(roots)
    if peak < 0:
        msg = "The method doesn't converge, peak is negative"
        fwhm = -99
    else:
        if nroots == 0:
            msg = "The method doesn't converge, no roots"
            fwhm = -99
        elif nroots == 1:
            r12 = roots[0]
            fwhm = 2 * r12
            msg = "The method converges, one root"
        else:
            msg = "The method doesn't converge, multiple roots"
            r12 = roots[0]
            fwhm = 2 * r12

    return peak, fwhm, msg
Beispiel #30
0
    def redshiftfunctions(self):   
        D=self.D
        zbins=self.zbins
        z2bins=self.z2bins
        Dabins=zbins*0.0
        Dmodbins=zbins*0.0
        Da2bins=numpy.zeros((z2bins.size,z2bins.size))
        volumebins=zbins*0.0
        for i in range(zbins.size):
            Dabins[i]=D.Da(zbins[i])
            Dmodbins[i]=D.distance_modulus(zbins[i])
            volumebins[i]=D.volume(zbins[i])
        for i in range(z2bins.size):
            for j in range(z2bins.size):
                if j>i:
                    Da2bins[i,j]=D.Da(z2bins[i],z2bins[j])

        self.Da_spline=interpolate.splrep(zbins,Dabins)
        self.Dmod_spline=interpolate.splrep(zbins,Dmodbins)

        self.volume_spline=interpolate.splrep(zbins,volumebins)

        z2d=iT.coords((z2bins.size,z2bins.size))*self.dz2
        self.Da_bispline=interpolate.RectBivariateSpline(z2bins,z2bins,Da2bins)

        #pickle the splines
        splinedump=open("redshiftsplines.pkl","wb")
        cPickle.dump([self.Da_spline,self.Dmod_spline,self.volume_spline,self.Da_bispline],splinedump,2)
Beispiel #31
0
def _interp_cubic_spline(rri, time, fs):
    time_rri_interp = _create_interp_time(time, fs)
    tck = interpolate.splrep(time, rri, s=0)
    rri_interp = interpolate.splev(time_rri_interp, tck, der=0)
    return rri_interp
Beispiel #32
0
def interpolate(*args, **kwargs):
    r"""Interpolation convinience function.

    Convenience function to interpolate different kind of data.
    Currently supported interpolation schemes are:

    * Interpolate mesh based data from one mesh to another
     (syntactic sugar for the core based interpolate (see below))

      Parameters:
        args: :gimliapi:`GIMLI::Mesh`, :gimliapi:`GIMLI::Mesh`, iterable
            `outData = interpolate(outMesh, inMesh, vals)`
            Interpolate values based on inMesh to outMesh.
            Values can be of length inMesh.cellCount() interpolated to 
            outMesh.cellCenters() or inMesh.nodeCount() which are interpolated tp 
            outMesh.positions().            
            
      Returns:
        Interpolated values.

    * Mesh based values to arbitrary points, based on finite element
      interpolation (from gimli core).

      Parameters:
        args: :gimliapi:`GIMLI::Mesh`, ...
            Arguments forwarded to :gimliapi:`GIMLI::interpolate`
        kwargs:
            Arguments forwarded to :gimliapi:`GIMLI::interpolate`
            
        `interpolate(srcMesh, destMesh)`
            All data from inMesh are interpolated to outMesh

      Returns:
        Interpolated values

    * Interpolate along curve.
      Forwarded to :py:mod:`pygimli.meshtools.interpolateAlongCurve`

      Parameters:
        args: curve, t

        kwargs:
            Arguments forwarded to
            :py:mod:`pygimli.meshtools.interpolateAlongCurve`

    * 1D point set :math:`u(x)` for ascending :math:`x`.
      Find interpolation function :math:`I = u(x)` and
      returns :math:`u_{\text{i}} = I(x_{\text{i}})`
      (interpolation methods are [**linear** via matplotlib,
      cubic **spline** via scipy, fit with **harmonic** functions' via pygimli])
      Note, for 'linear' and 'spline' the interpolate contains all original
      coordinates while 'harmonic' returns an approximate best fit.
      The amount of harmonic coefficients can be specfied with the 'nc' keyword.

      Parameters:
        args: xi, x, u
            * :math:`x_{\text{i}}` - target sample points
            * :math:`x` - function sample points
            * :math:`u` - function values
        kwargs:
            * method : string
                Specify interpolation method 'linear, 'spline', 'harmonic'
            * nc : int
                Number of harmonic coefficients for the 'harmonic' method.

      Returns:
        ui: array of length xi
            :math:`u_{\text{i}} = I(x_{\text{i}})`, with :math:`I = u(x)`


    To use the core functions :gimliapi:`GIMLI::interpolate` start with a
    mesh instance as first argument or use the appropriate keyword arguments.

    TODO

    * 2D parametric to points (method=['linear, 'spline', 'harmonic'])
    * 2D/3D point cloud to points/grids ('Delauney', 'linear, 'spline', 'harmonic')
    * Mesh to points based on nearest neighbour values (pg.core)

    Examples
    --------
    >>> # no need to import matplotlib. pygimli's show does
    >>> import numpy as np
    >>> import pygimli as pg
    >>> fig, ax = pg.plt.subplots(1, 1, figsize=(10, 5))
    >>> u = np.array([1.0, 12.0, 3.0, -4.0, 5.0, 6.0, -1.0])
    >>> xu = np.array(range(len(u)))
    >>> xi = np.linspace(xu[0], xu[-1], 1000)
    >>> _= ax.plot(xu, u, 'o')
    >>> _= ax.plot(xi, pg.interpolate(xi, xu, u, method='linear'),
    ...         color='blue', label='linear')
    >>> _= ax.plot(xi, pg.interpolate(xi, xu, u, method='spline'),
    ...            color='red', label='spline')
    >>> _= ax.plot(xi, pg.interpolate(xi, xu, u, method='harmonic'),
    ...         color='green', label='harmonic')
    >>> _= ax.legend()
    """
    pgcore = False
    if 'srcMesh' in kwargs:
        pgcore = True

    elif len(args) > 0:
        if isinstance(args[0], pg.Mesh):
            if len(args) == 2 and isinstance(args[1], pg.Mesh):
                return pg.core._pygimli_.interpolate(args[0], args[1],
                                                     **kwargs)

            if len(args) == 3 and isinstance(args[1], pg.Mesh):
                pgcore = False  # (outMesh, inMesh, vals)
            else:
                pgcore = True

    if pgcore:
        if len(args) == 3:  # args: outData = (inMesh, inData, outPos)

            if args[1].ndim == 2:  # outData = (inMesh, mat, vR3)

                outMat = pg.Matrix()
                pg.core._pygimli_.interpolate(args[0],
                                              inMat=np.array(args[1]),
                                              destPos=args[2],
                                              outMat=outMat,
                                              **kwargs)
                return np.array(outMat)

        if len(args) == 4:  # args: (inMesh, inData, outPos, outData)

            if args[1].ndim == 1 and args[2].ndim == 1 and args[3].ndim == 1:
                return pg.core._pygimli_.interpolate(args[0],
                                                     inVec=args[1],
                                                     x=args[2],
                                                     y=args[3],
                                                     **kwargs)

            if isinstance(args[1], pg.RMatrix) and \
               isinstance(args[3], pg.RMatrix):
                return pg.core._pygimli_.interpolate(args[0],
                                                     inMat=args[1],
                                                     destPos=args[2],
                                                     outMat=args[3],
                                                     **kwargs)
            if isinstance(args[1], pg.RVector) and \
               isinstance(args[3], pg.RVector):
                return pg.core._pygimli_.interpolate(args[0],
                                                     inVec=args[1],
                                                     destPos=args[2],
                                                     outVec=args[3],
                                                     **kwargs)

        if len(args) == 5:
            if args[1].ndim == 1 and args[2].ndim == 1 and \
               args[3].ndim == 1 and args[4].ndim == 1:
                return pg.core._pygimli_.interpolate(args[0],
                                                     inVec=args[1],
                                                     x=args[2],
                                                     y=args[3],
                                                     z=args[4],
                                                     **kwargs)

        return pg.core._pygimli_.interpolate(*args, **kwargs)
        # end if pg.core:

    if len(args) == 3:

        if isinstance(args[0], pg.Mesh):  # args: (outMesh, inMesh, data)
            outMesh = args[0]
            inMesh = args[1]
            data = args[2]

            if isinstance(data, pg.R3Vector) or isinstance(
                    data, pg.stdVectorRVector3):
                x = pg.interpolate(outMesh, inMesh, pg.x(data))
                y = pg.interpolate(outMesh, inMesh, pg.y(data))
                z = pg.interpolate(outMesh, inMesh, pg.z(data))
                return np.vstack([x, y, z]).T

            if isinstance(data, np.ndarray):
                if data.ndim == 2 and data.shape[1] == 3:
                    x = pg.interpolate(outMesh, inMesh, data[:, 0])
                    y = pg.interpolate(outMesh, inMesh, data[:, 1])
                    z = pg.interpolate(outMesh, inMesh, data[:, 2])
                    return np.vstack([x, y, z]).T

            if len(data) == inMesh.cellCount():
                return pg.interpolate(srcMesh=inMesh,
                                      inVec=data,
                                      destPos=outMesh.cellCenters())
            elif len(data) == inMesh.nodeCount():
                return pg.interpolate(srcMesh=inMesh,
                                      inVec=data,
                                      destPos=outMesh.positions())
            else:
                print(inMesh)
                print(outMesh)
                raise Exception("Don't know how to interpolate data of size",
                                str(len(data)))

            print("data: ", data)
            raise Exception("Cannot interpret data: ", str(len(data)))

        else:  #args: xi, x, u

            xi = args[0]
            x = args[1]
            u = args[2]

            method = kwargs.pop('method', 'linear')

            if 'linear' in method:
                return np.interp(xi, x, u)

            if 'harmonic' in method:
                coeff = kwargs.pop('nc', int(np.ceil(np.sqrt(len(x)))))
                from pygimli.frameworks import harmfitNative
                return harmfitNative(u, x=x, nc=coeff, xc=xi, err=None)[0]

            if 'spline' in method:
                if pg.optImport("scipy",
                                requiredFor="use interpolate splines."):
                    from scipy import interpolate
                    tck = interpolate.splrep(x, u, s=0)
                    return interpolate.splev(xi, tck, der=0)
                else:
                    return xi * 0.

    if len(args) == 2:  # args curve, t
        curve = args[0]
        t = args[1]
        return interpolateAlongCurve(curve, t, **kwargs)
Beispiel #33
0
def f(x, x_points, y_points):
    tck = interpolate.splrep(x_points, y_points)
    return interpolate.splev(x, tck)
Beispiel #34
0
def main():

# Parameters
  doproj = True  # Model projected statistics (or angular statistics)
  if (doproj):
    opt = 1      # 1) Delta Sigma 2) w_p 3) xi
    rmin = 0.5   # Minimum projected separation of model [Mpc/h]
    rmax = 50.   # Maximum projected separation of model [Mpc/h]
    nrbin = 10   # Number of bins of model
    rlinlog = 2  # Space bins as 1) linear or 2) logarithmic
    pimax = 60.  # Limit of integral over Pi [Mpc/h]
  else:
    opt = 1      # 1) xi_+ 2) xi_- 3) gamma_t 4) w
    thmin = 0.03 # Minimum angular separation of model [degrees]
    thmax = 3.   # Maximum angular separation of model [degrees]
    nthbin = 20  # Number of bins of model
    thlinlog = 2 # Space bins as 1) linear or 2) logarithmic
    lmin = 1.    # Minimum multipole to pre-compute
    lmax = 1.e+5 # Maximum multipole to pre-compute
    nl = 500     # Number of multipole bins to pre-compute
# Cosmological model
  om = 0.286     # Omega_m
  h = 0.7        # Hubble parameter
  fb = 0.164335664336 # Baryon fraction
  sig8 = 0.82    # sigma_8
  b = 1.         # Galaxy bias
# Files containing the source and lens redshift distributions
  pzsourcefile = 'pz_sources_buzzard_zp0pt80_1pt00.dat'
  pzlensfile = 'pz_lenses_buzzard_lrg_zs0pt40_0pt60.dat'
# File containing the model power spectrum
  pkcambfile,npcamb,nkcamb = 'pkcambhalofit_zrange_buzzard.dat',21,922
  klinlog,kminmod,kmaxmod,nkmod = 2,1.e-4,0.999e+4,800
# Pre-compute model as a function of r
  rminmod,rmaxmod,nrmod = 1.e-4,1.e+3,70
# Limits of integral over k
  kminint,kmaxint = 1.e-4,0.999e+4
# astropy cosmology
  cosmo = FlatLambdaCDM(H0=100.,Om0=om)
# Read in redshift distributions
  zsourcearr,pzsourcearr,zminsource,zmaxsource = readnz(pzsourcefile)
  zlensarr,pzlensarr,zminlens,zmaxlens = readnz(pzlensfile)
  tcksource = splrep(zsourcearr,pzsourcearr)
  tcklens = splrep(zlensarr,pzlensarr)
# Read in power spectra
  kmod,zmod,pkzmod = readpkcamb(pkcambfile,npcamb,nkcamb,om,h,fb,sig8,klinlog,kminmod,kmaxmod,nkmod)
# Determine radial functions
  zsourcearr,fsourcearr = getfsource(0.,zmaxsource,zminsource,zmaxsource,tcksource,cosmo)
  zlensarr,flensarr = getflens(zminlens,zmaxlens,tcklens,cosmo)
# Compute projected lensing correlation functions
  if (doproj):
    z = 0.5*(zminlens+zmaxlens)
    pkmod = intpkzmod(z,zmod,pkzmod)
# Generate lensing function
    rbin,lensprojmod = getlensprojmod(opt,rmin,rmax,nrbin,rlinlog,pimax,rminmod,rmaxmod,nrmod,kminint,kmaxint,kmod,pkmod,om)
# Compute angular lensing correlation functions
  else:
# Determine angular power spectra
    larr,clshapearr = getclshapearr(opt,lmin,lmax,nl,zminlens,zmaxlens,zlensarr,flensarr,zminsource,zmaxsource,zsourcearr,fsourcearr,kmod,zmod,pkzmod,om,b,cosmo)
# Generate lensing function
    thdeg,lensshapemod = getlensangmod(opt,thmin,thmax,nthbin,thlinlog,lmin,lmax,larr,clshapearr)
  return
 def _interpolate(self, spline, spline_size):
     spline = np.append(spline, spline[0])
     x = np.linspace(0, 1, len(spline))
     tck = splrep(x, spline, per=True)
     x2 = np.linspace(0, 1, spline_size)
     return splev(x2, tck)[:-1]
Beispiel #36
0
    df.drop_duplicates(inplace=True)
    df['y_invert'] = df['y'].mean() - df.y
    #    df.plot('x','y_invert')
    base = peakutils.baseline(df.y_invert, 2)
    indexes = peakutils.indexes(df.y_invert - base,
                                thres=0.00001,
                                min_dist=200)
    #    print(indexes)
    #    pplot(df.x,df.y_invert, indexes)
    for i in indexes:
        if i[(i < 100) & (i > 5)]:
            peak_x = i
            x2, y2 = df.x, df.y_invert
            skewness = stats.skew(y2)
            tck = interpolate.splrep(
                x2, y2, s=.00000001
            )  # s =m-sqrt(2m) where m= #datapts and s is smoothness factor
            x_ = np.arange(np.min(x2), np.max(x2), 0.003)
            y_ = interpolate.splev(x_, tck, der=0)

            HM = (np.max(y_) - np.min(y_)) / 2
            w = splrep(x_, y_ - HM, k=3)
            #            print(sproot(w_j))
            try:
                if len(sproot(w)) % 2 == 0:
                    r1, r2 = sproot(w)
                    #                    print(r1 , r2)
                    FWHM = np.abs(r1 - r2)
                    #                    half_width.append(FWHM)
                    center_wavelength = r1 + FWHM / 2
#                    peak_center.append(center_wavelength)
Beispiel #37
0
    def interpol(self, emin, emax, rom, broaden, dest_dir, sp=False):
        """
		This performs the interpolation of points on the real axis.
		"""
        print("\nInterpolating points on real axis...")
        headerline = 2
        om, Sig = Fileio.Read_complex_multilines("./ac/Sig.out", headerline)
        s_oo = None
        Vdc = None
        # The exec() function doesn't work properly on Python3 so I had to use a workaround:
        fi = open("./ac/Sig.out", "r")
        line1 = fi.readline()
        s_oo = re.findall(r"\s*([0-9.+-]*)", line1)
        while "" in s_oo:
            s_oo.remove("")
        line2 = fi.readline()
        Vdc = re.findall(r"\s*([0-9.+-]*)", line2)
        while "" in Vdc:
            Vdc.remove("")

            # exec(ar[0])
            # m=re.search('#(.*)',line)
            # exec(m.group(1).strip())
        # s_oo_Vdc=np.array(s_oo)-array(Vdc)
        fi.close()
        s_oo_Vdc = np.array((np.array(s_oo)).astype(np.float)) - np.array(
            (np.array(Vdc)).astype(np.float)
        )

        ommesh = np.linspace(emin, emax, rom)

        # non spin polarized case
        if sp == False:

            Sig_tot = np.zeros((len(Sig), rom), dtype=complex)

            for i in range(len(Sig)):
                SigSpline = interpolate.splrep(om, Sig[i].real, k=1, s=0)
                Sig_tot[i, :] += interpolate.splev(ommesh, SigSpline)
                SigSpline = interpolate.splrep(om, Sig[i].imag, k=1, s=0)
                Sig_tot[i, :] += 1j * interpolate.splev(ommesh, SigSpline)

            header1 = "# nom,ncor_orb= " + str(len(ommesh)) + " " + str(len(Sig_tot))
            # header2='# T= %18.15f'%(1.0/pC['beta'][0])#+str(self.T)
            header2 = "# T= %18.15f" % (broaden)  # +str(self.T)
            header3 = "# s_oo-Vdc= "
            for i in range(len(s_oo_Vdc)):
                header3 += "%18.15f " % (s_oo_Vdc[i])
            header4 = "# s_oo= " + str(s_oo)
            header5 = "# Vdc= " + str(Vdc)
            if dest_dir == "dos":
                Fileio.Print_complex_multilines(
                    Sig_tot,
                    ommesh,
                    "./dos/sig.inp_real",
                    [header1, header2, header3, header4, header5],
                )

            # create sig.inp_real
            if dest_dir == "bands":
                Fileio.Print_complex_multilines(
                    Sig_tot,
                    ommesh,
                    "./bands/sig.inp_real",
                    [header1, header2, header3, header4, header5],
                )

        # Spin polarized calculation
        if sp:

            Sig_tot = np.zeros((int(len(Sig) / 2), rom), dtype=complex)
            Sig_tot_dn = np.zeros((int(len(Sig) / 2), rom), dtype=complex)

            for i in range(int(len(Sig) / 2)):
                # spin
                SigSpline = interpolate.splrep(om, Sig[i].real, k=1, s=0)
                Sig_tot[i, :] += interpolate.splev(ommesh, SigSpline)
                SigSpline = interpolate.splrep(om, Sig[i].imag, k=1, s=0)
                Sig_tot[i, :] += 1j * interpolate.splev(ommesh, SigSpline)

                # spin down
                SigSpline = interpolate.splrep(
                    om, Sig[i + int(len(Sig) / 2)].real, k=1, s=0
                )
                Sig_tot_dn[i, :] += interpolate.splev(ommesh, SigSpline)
                SigSpline = interpolate.splrep(
                    om, Sig[i + int(len(Sig) / 2)].imag, k=1, s=0
                )
                Sig_tot_dn[i, :] += 1j * interpolate.splev(ommesh, SigSpline)

            header1 = "# nom,ncor_orb= " + str(len(ommesh)) + " " + str(len(Sig_tot))
            header2 = "# T= %18.15f" % (broaden)
            header3 = "# s_oo-Vdc= "
            header3_dn = "# s_oo-Vdc= "
            for i in range(int(len(s_oo_Vdc) / 2)):
                header3 += "%18.15f " % (s_oo_Vdc[i])
                header3_dn += "%18.15f " % (s_oo_Vdc[i + int(len(s_oo_Vdc) / 2)])
            header4 = "# s_oo= " + str(s_oo[0 : int(len(s_oo) / 2)])
            header4_dn = "# s_oo= " + str(s_oo[int(len(s_oo) / 2) :])
            header5 = "# Vdc= " + str(Vdc[0 : int(len(Vdc) / 2)])
            header5_dn = "# Vdc= " + str(Vdc[int(len(Vdc) / 2) :])

            if dest_dir == "dos":
                Fileio.Print_complex_multilines(
                    Sig_tot,
                    ommesh,
                    "./dos/sig.inp_real",
                    [header1, header2, header3, header4, header5],
                )
                Fileio.Print_complex_multilines(
                    Sig_tot_dn,
                    ommesh,
                    "./dos/sig.inp_real_dn",
                    [header1, header2, header3_dn, header4_dn, header5_dn],
                )

            # create sig.inp_real
            if dest_dir == "bands":
                Fileio.Print_complex_multilines(
                    Sig_tot,
                    ommesh,
                    "./bands/sig.inp_real",
                    [header1, header2, header3, header4, header5],
                )
                Fileio.Print_complex_multilines(
                    Sig_tot_dn,
                    ommesh,
                    "./bands/sig.inp_real_dn",
                    [header1, header2, header3_dn, header4_dn, header5_dn],
                )

        print("Interpolation complete.\n")
ax.legend()
number_of_ticks = 20
ax.set_xlabel('$X$')
ax.set_ylabel('$Y$')
ax.set_zlabel('$frame no $', rotation=0)
ax.zaxis.set_ticks(
    np.arange(min(Z),
              max(Z) + (max(Z) - min(Z)) / number_of_ticks,
              ((max(Z) - min(Z)) / number_of_ticks)))
plt.show()
fig.savefig(figure_name3d)
plt.show()

# draw gradient graph
Z_new = np.linspace(Z.min(), Z.max(), 500)
bspl = splrep(Z, gradiet_vals, s=5)
bspl_y = splev(Z_new, bspl)

smooth_df = pd.DataFrame({'Z': Z_new, 'data': bspl_y})

smooth_df.to_csv("smoothen_y.csv", index=False)

# smooth_df['min'] = smooth_df.Y_Smooth[(smooth_df.Y_Smooth.shift(1) > smooth_df.Y_Smooth) & (smooth_df.Y_Smooth.shift(-1) > smooth_df.Y_Smooth)]
# plt.scatter(smooth_df.index, df['min'], c='r')
# smooth_df.Y_Smooth.plot()

# filt_grad=medfilt(gradiet_vals,)

# fig=plt.figure()
fig, ax = plt.subplots(2, 1)
ax[0].plot(X, Y)
Beispiel #39
0
    def normalized_hilbert(self, s):
        """
            Perform the "Normalized" Hilbert transform (Huang 2008 sec. 3.1) on the IMF s, decomposing the
            signal s into AM and FM components. Returns am,fm,phase,ifreq - am is the AM component, fm is the FM
            component, phase is the the arccos of fm, and ifreq is the instantaneous frequency.
        """

        x = copy.copy(s)
        iter = 0
        converged = False

        while not converged:
            #take the absolute value of the IMF and find the extrema
            absx = np.abs(x)
            mini, maxi = find_extrema(absx)
            if len(mini) == 0 or len(maxi) == 0:
                converged = True
                break
            spline_order = 3

            #reflect first and last maxima to remove edge effects for interpolation
            left_padding = maxi[0]
            right_padding = len(x) - maxi[-1]
            x_padded = np.zeros([len(x) + left_padding + right_padding])
            x_padded[left_padding:-right_padding] = absx
            x_padded[0] = absx[maxi[0]]
            x_padded[-1] = absx[maxi[-1]]

            #create new array of extremas
            new_maxi = [i + left_padding for i in maxi]
            new_maxi.insert(0, 0)
            new_maxi.append(len(x_padded) - 1)

            #fit a cubic spline to the extrema of the absolute value of the signal
            if len(maxi) <= 3:
                spline_order = 1
            max_spline = splrep(new_maxi, x_padded[new_maxi], k=spline_order)

            #use the spline to interpolate over the course of the signal
            t = np.arange(0, len(x_padded))
            fit_index = range(left_padding, len(x_padded) - right_padding)
            env = splev(t[fit_index], max_spline)
            """
            plt.figure()
            plt.plot(t, x_padded, 'k-')
            plt.plot(new_maxi, x_padded[new_maxi], 'ro-', markersize=8)
            plt.axis('tight')
            plt.suptitle('Iter %d' % iter)
            """

            #divide by envelope
            x /= env

            #check for convergence
            iter += 1
            if iter >= self.hilbert_max_iter:
                converged = True
            if (x.max() - 1.0) <= 1e-6:
                converged = True
            if len(mini) == 0 or len(maxi) == 0:
                converged = True

        #compute the FM and AM components
        fm = x
        am = s / fm

        #compute the phase
        phase = np.arccos(fm)

        #compute the instantaneous frequency
        ifreq = np.zeros([len(phase)])
        ifreq[1:] = np.diff(phase) * self.sample_rate

        return am, fm, phase, ifreq
def interpo_flux_conserving(wave,
                            flux,
                            ivar,
                            waveb,
                            waver,
                            dw=2,
                            testing=False):
    """This is a an interpolation algorithm that does trapezoidal integration
        to conserve flux. The variance is then propagated correctly. Since 
        interpolation always introduces correlation in the variance spectrum,
        we ignore  this correlation byscale the original variance spectrum 
        to the new variance spectrum.
    """
    var = 1. / ivar
    pixel_scale = np.median(np.diff(wave))
    # print pixel_scale

    wave_min = 999
    wave_max = 12001
    wavelength_mids = np.arange(math.ceil(wave_min),
                                math.floor(wave_max),
                                dtype=int,
                                step=dw)
    lower = wave[0]
    upper = wave[-1]

    good_data = np.where((wave >= waveb) & (wave <= waver))
    influx = inter.splrep(wave[good_data], flux[good_data])
    invar = inter.splrep(wave[good_data], var[good_data])

    low_int = math.ceil(lower)
    up_int = math.ceil(upper)
    wave_final = []
    flux_final = []
    var_final = []
    for mid_point in wavelength_mids:
        inter_flux_mid_left = float(inter.splev(mid_point, influx, ext=3))
        inter_var_mid_left = float(inter.splev(mid_point, invar, ext=3))
        inter_flux_mid_right = float(inter.splev(mid_point + dw, influx,
                                                 ext=3))
        inter_var_mid_right = float(inter.splev(mid_point + dw, invar, ext=3))
        waves_to_sum = np.where((wave > mid_point)
                                & (wave < mid_point + dw))[0]
        wave_final.append((mid_point + mid_point + dw) / 2.)
        if (mid_point >= lower and (mid_point + dw) <= upper):
            waves = [mid_point]
            fluxes = [inter_flux_mid_left]
            variances = [inter_var_mid_left]
            for i, w in enumerate(waves_to_sum):
                waves.append(wave[w])
                fluxes.append(flux[w])
                variances.append(var[w])
            waves.append(mid_point + dw)
            fluxes.append(inter_flux_mid_right)
            variances.append(inter_var_mid_right)
            new_point = np.trapz(fluxes, x=waves)
            flux_final.append(new_point)
            diffs = np.diff(waves)
            var_tot = 0.
            for i in range(len(variances) - 1):
                v1 = variances[i]
                v2 = variances[i + 1]
                var_tot = var_tot + (diffs[i]**2.) * (v1 + v2)
            var_tot = var_tot * .25
            var_final.append(var_tot)
        else:
            flux_final.append(0.)
            var_final.append(0.)

    inter_var = inter.splev(wave_final, invar, ext=3)
    s = scale_together(var_final, inter_var)[0]
    scale = 1. / s
    var_uncorrelated = scale * inter_var
    ivar_final = 1. / var_uncorrelated

    wave_final = np.asarray(wave_final)
    flux_final = np.asarray(flux_final)
    ivar_final = np.asarray(ivar_final)
    if testing:
        var_final = np.asarray(var_final)

    data = np.where(
        (wave_final > lower + dw) &
        (wave_final <
         upper - dw))  # padding to avoid interpolation errors near edges
    # flux_final[missing_data] = float('NaN')
    # ivar_final[missing_data] = float('NaN')
    # if testing:
    #     var_final[missing_data] = float('NaN')

    output = np.array([wave_final[data], flux_final[data], ivar_final[data]])

    if testing:
        interp_wave = output[0, :]
        interp_flux = output[1, :]
        interp_ivar = output[2, :]
        # print scale
        plt.plot(wave, var)
        plt.plot(interp_wave, var_final)
        plt.plot(interp_wave, 1. / interp_ivar)
        plt.xlim([7000, 7100])
        plt.ylim([-.05e-32, 2.e-32])
        plt.show()

    return output
Beispiel #41
0
    def calcLossAnchor(self, Ep, anc_slip_extr1=0.0, anc_slip_extr2=0.0):
        '''Creates the attributes lossAnchor and stressAfterLossAnchor of type 
        array that contains  for each point in fineCoordMtr the cumulative 
        immediate loss of prestressing due to anchorage slip and the stress 
        after this loss, respectively.
        Loss due to friction must be previously calculated

        :param Ep:      elasic modulus of the prestressing steel

        :param anc_slip_extr1: anchorage slip (data provided by the manufacturer
                        of the anchorage system) at extremity 1 of 
                        the tendon (starting point)  (= deltaL)
        :param anc_slip_extr2: anchorage slip at extremity 2 of 
                        the tendon (ending point) (= deltaL)
        '''
        self.projXYcoordZeroAnchLoss = [0, self.fineProjXYcoord[-1]
                                        ]  # projected coordinates of the
        # points near extremity 1 and extremity 2,
        #respectively, that delimite the lengths of
        # tendon affected by the loss of prestress
        # due to the anchorages slip
        #Initialization values
        lossAnchExtr1 = np.zeros(len(self.fineScoord))
        lossAnchExtr2 = np.zeros(len(self.fineScoord))
        if anc_slip_extr1 > 0:
            self.slip1 = Ep * anc_slip_extr1
            self.tckLossFric = interpolate.splrep(
                self.fineScoord, self.stressAfterLossFrictionOnlyExtr1, k=3)
            if self.fAnc_ext1(self.fineScoord[-1]) < 0:  #the anchorage slip
                #affects all the tendon length
                lackArea = -2 * self.fAnc_ext1(self.fineScoord[-1])
                excess_delta_sigma = lackArea / self.getCumLength().item(-1)
                sCoordZeroLoss = self.fineScoord[-1]
            else:
                # we use newton_krylov solver to find the zero of function
                # fAnc_ext1 that gives us the point from which the tendon is
                # not affected by the anchorage slip. Tolerance and relative
                # step is given as parameters in order to enhance convergence
                tol = self.slip1 * 1e-6
                sCoordZeroLoss = optimize.newton_krylov(
                    F=self.fAnc_ext1,
                    xin=self.fineScoord[-1] / 2.0,
                    rdiff=0.1,
                    f_tol=tol)  #point from which the tendon is not
                #affected by the anchorage slip
                self.projXYcoordZeroAnchLoss[0] = self.ScoordToXYprojCoord(
                    sCoordZeroLoss.item(0))
                excess_delta_sigma = 0
            stressSCoordZeroLoss = interpolate.splev(
                sCoordZeroLoss, self.tckLossFric,
                der=0)  #stress in that point (after loss due to friction)
            condlist = [self.fineScoord <= sCoordZeroLoss]
            choicelist = [
                2 *
                (self.stressAfterLossFrictionOnlyExtr1 - stressSCoordZeroLoss)
                + excess_delta_sigma
            ]
            lossAnchExtr1 = np.select(condlist, choicelist)
        if anc_slip_extr2 > 0:
            self.slip2 = Ep * anc_slip_extr2
            self.tckLossFric = interpolate.splrep(
                self.fineScoord, self.stressAfterLossFrictionOnlyExtr2, k=3)
            if self.fAnc_ext2(self.fineScoord[0]) < 0:  #the anchorage slip
                #affects all the tendon length
                lackArea = -2 * self.fAnc_ext2(self.fineScoord[0])
                excess_delta_sigma = lackArea / self.getCumLength().item(-1)
                sCoordZeroLoss = self.fineScoord[0]
            else:
                # we use newton_krylov solver to find the zero of function
                # fAnc_ext1 that gives us the point from which the tendon is
                # not affected by the anchorage slip
                tol = self.slip2 * 1e-6
                sCoordZeroLoss = optimize.newton_krylov(
                    self.fAnc_ext2,
                    self.fineScoord[-1] / 2.0,
                    rdiff=0.1,
                    f_tol=tol)  #point from which the tendon is affected
                #by the anchorage slip
                self.projXYcoordZeroAnchLoss[1] = self.ScoordToXYprojCoord(
                    sCoordZeroLoss.item(0))
                excess_delta_sigma = 0
            stressSCoordZeroLoss = interpolate.splev(
                sCoordZeroLoss, self.tckLossFric,
                der=0)  #stress in that point (after loss due to friction)
            condlist = [self.fineScoord >= sCoordZeroLoss]
            choicelist = [
                2 * (self.stressAfterLossFriction - stressSCoordZeroLoss) +
                excess_delta_sigma
            ]
            lossAnchExtr2 = np.select(condlist, choicelist)
        self.lossAnch = lossAnchExtr1 + lossAnchExtr2
        self.stressAfterLossAnch = self.stressAfterLossFriction - self.lossAnch
Beispiel #42
0
def CCF(spec,
        model_path='/dummy/path/',
        doplot=False,
        plot_dir='/home/rabrahm/',
        plot_name='MY_LUP',
        Li=4500.,
        Lf=6300.,
        npools=1,
        base='../utils/Correlation/'):
    """
	This function finds an aproximation to the stellar parameters (Teff, log(g), [Fe/H]) 
	of the input echelle spectrum using a CCF with model spectra. This code also is 
	constructed to find the radial velocity of the star and v*sin(i).
	"""
    global It, L_Gl, F_Gl, ons, velo2, wat, lux
    lux = 299792.458

    L1, F1 = spec[0, :, :], spec[5, :, :]
    for i in range(F1.shape[0]):
        I = np.where(np.isnan(F1[i]) == True)[0]
        F1[i][I] = 1.

    #width_path = '/home/rabrahm/Desktop/corr2/'
    #slines_path = '/home/rabrahm/Desktop/corr2/'

    SLi, SLf = numpy.loadtxt(base + 'lines2.dat', dtype=None, unpack=True)

    SLi = ToVacuum(SLi)
    SLf = ToVacuum(SLf)

    AT,AG,AZ,A000,A025,A050,A075,A100,A150,A200,A250,A300,A350,A400,A450,A500 =\
               numpy.loadtxt(base+'anchos50000.dat',dtype=None,unpack=True)

    vsini = [
        0.0, 2.5, 5.0, 7.5, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0,
        50.0
    ]

    or01 = 0
    for i in range(L1.shape[0]):
        I = numpy.where(L1[i] < Lf)[0]
        if len(I) > 0:
            or01 = i
            break

    or02 = L1.shape[0] - 1
    for i in range(L1.shape[0]):
        I = numpy.where(L1[i] < Li)[0]
        if len(I) > 0:
            or02 = i
            break

    or03 = 0
    for i in range(L1.shape[0]):
        I = numpy.where(L1[i] < 6250.0)[0]
        if len(I) > 0:
            or03 = i
            break

    or04 = L1.shape[0] - 1
    for i in range(L1.shape[0]):
        I = numpy.where(L1[i] < 5500.0)[0]
        if len(I) > 0:
            or04 = i
            break

    or05 = 0
    for i in range(L1.shape[0]):
        I = numpy.where(L1[i] < 5190.0)[0]
        W = numpy.where(L1[i] < 5170.0)[0]
        if len(I) > 0 and len(W) > 0:
            or05 = i
            break
    #print or01,or02,or03,or04,or05
    guess = [1.0, 1.0, 1.0]

    L = L1[or01:or02]
    F = F1[or01:or02]
    Lm = L1[or03:or04]
    Fm = F1[or03:or04]
    Lg = L1[or05]
    Fg = F1[or05]

    bad_lines = [[6860, 6900], [6550, 6580], [6270, 6320], [4850, 4880]]
    ons = bad_orders(L, bad_lines)

    modi = 'R_0.0_5000_30_p00p00.ms.fits'
    #print 'Radial velocity calculation via CCF with: '+modi

    sci = pyfits.getdata(model_path + 'vsini_0.0/' + modi)
    hdi = pyfits.getheader(model_path + 'vsini_0.0/' + modi)

    wam1 = ToVacuum(numpy.arange(len(sci)) * hdi['CD1_1'] + hdi['CRVAL1'])

    Im = numpy.where((wam1 > 5400.0) & (wam1 < 6350.0))[0]
    wam = wam1[Im]
    flm = sci[Im]

    Ig = numpy.where((wam1 > 5100.0) & (wam1 < 5250.0))[0]
    wag = wam1[Ig]

    It = numpy.where((wam1 > 4000.0) & (wam1 < 7500.0))[0]
    wat = wam1[It]

    for i in range(Lm.shape[0]):
        I = numpy.where((Fm[i] != 0.0) & (Fm[i] < 2.0))[0]
        Ls = Lm[i][I]
        Fs = Fm[i][I]

        I = numpy.where((wam > Ls[0] - 5.0) & (wam < Ls[-1] + 5.0))[0]
        MLs = wam[I]
        MFs = flm[I]

        vv, cc = vels.CCF(MLs, MFs, Ls, Fs, -200.0, 200.0)

        if i == 0:
            ccf = numpy.array(cc) * (Ls[-1] - Ls[0])
            nor = Ls[-1] - Ls[0]
        else:
            ccf = ccf + numpy.array(cc) * (Ls[-1] - Ls[0])
            nor = nor + (Ls[-1] - Ls[0])

    ccf = ccf / nor
    vv = numpy.array(vv)

    B = 0.5 * (ccf[0] + ccf[-1])
    A = numpy.max(ccf) - B
    med = vv[numpy.where(ccf == numpy.max(ccf))[0]]
    sig = 20.0
    guess1 = [B, A, med, sig]
    ajustep = optimize.leastsq(res_gauss1, guess1, args=(ccf, vv))
    velo = ajustep[0][2]

    #print 'The initial radial velocity is: '+str(velo)+' km/s'

    #print 'Determining parameters of the initial model'

    vecti = [3500, 4000, 4500, 5000, 5500, 6500]
    vecgi = [0.0, 1.5, 3.0, 4.5]
    #veczi = [-2.0,-1.0,0.0]
    veczi = [-1.0, 0.0]
    ccmax = 0
    names = []
    cc = 0
    nor = 0
    for g in vecgi:
        for z in veczi:
            for t in vecti:
                nam = get_name(t, g, z)
                names.append(nam)
    namemax = get_name(5500, 4.5, 0.0)
    TEI = 5500
    MEI = 0.0
    LGI = 4.5
    for nam in names:
        if ((os.access(model_path + 'vsini_0.0/R_0.0_' + nam,
                       os.F_OK) == True)):
            mod = pyfits.getdata(model_path + 'vsini_0.0/R_0.0_' + nam)
            flm = mod[Im]
            cc = 0.0
            nor = 0.0
            for o in range(Lm.shape[0]):
                I = numpy.where(Fm[i] != 0.0)[0]
                Ls = Lm[i][I]
                Fs = Fm[i][I]
                I = numpy.where((wam > Ls[0] - 5.0) & (wam < Ls[-1] + 5.0))[0]
                MLs = wam[I] * (1 + velo / lux)
                MFs = flm[I]

                tck = interpolate.splrep(MLs, MFs, k=3, s=0)
                NMFs = interpolate.splev(Ls, tck, der=0)

                cc = cc + integrate.simps(
                    NMFs[1:-1] * Fs[1:-1], Ls[1:-1]) / math.sqrt(
                        integrate.simps(Fs[1:-1] * Fs[1:-1], Ls[1:-1]) *
                        integrate.simps(NMFs[1:-1] * NMFs[1:-1], Ls[1:-1])) * (
                            Ls[-1] - Ls[0])
                nor = nor + (Ls[-1] - Ls[0])
            cc = cc / nor

            if cc >= ccmax:
                ccmax = cc
                namemax = nam

    mod = pyfits.getheader(model_path + 'vsini_0.0/R_0.0_' + namemax)
    TEI = mod['TEFF']

    #print 'Teff (initial) = '+str(TEI)+' K'

    if TEI <= 4000:
        rot = 5.0
        LGI = 3.0
        MTI = 0.0
        late = True
        velo2 = velo

    else:
        late = False
        t = TEI
        vecgi = [1.0, 2.0, 3.0, 4.0]
        #veczi  = [-2.0,-1.0,0.0]
        veczi = [-1.0, 0.0]
        dif = 1000.0

        for z in veczi:
            vals = []
            di = 1000
            meg = 3.0
            for g in vecgi:
                nam = get_name(t, g, z)
                if ((os.access(model_path + 'vsini_0.0/R_0.0_' + nam,
                               os.F_OK) == True)):
                    mod = pyfits.getdata(model_path + 'vsini_0.0/R_0.0_' + nam)
                    flm = mod[Im]
                    flm = el_stl(wam, flm, SLi, SLf)
                    intfl = 0.0
                    intflm = 0.0
                    di = 1000
                    for o in range(Lm.shape[0]):
                        I = numpy.where(Fm[i] != 0.0)[0]
                        Ls = Lm[i][I]
                        Fs = Fm[i][I]
                        Fs = el_stl(Ls, Fs, SLi * (1 + velo / lux),
                                    SLf * (1 + velo / lux))
                        I = numpy.where((wam > Ls[0] - 5.0)
                                        & (wam < Ls[-1] + 5.0))[0]
                        MLs = wam[I] * (1 + velo / lux)
                        MFs = flm[I]
                        tck = interpolate.splrep(MLs, MFs, k=3, s=0)
                        NMFs = interpolate.splev(Ls, tck, der=0)

                        intfl = intfl + integrate.simps(
                            Fs[1:-1] * Fs[1:-1], Ls[1:-1])
                        intflm = intflm + integrate.simps(
                            NMFs[1:-1] * NMFs[1:-1], Ls[1:-1])
                    intfl = math.sqrt(intfl)
                    intflm = math.sqrt(intflm)
                    dif1 = math.sqrt((intflm - intfl)**2)
                    vals.append(dif1)
                    if dif1 < di:
                        di = dif1
                        meg = g
            dif2 = numpy.mean(vals)
            #print z,dif2
            if dif2 < dif:
                dif = dif2
                LGI = meg
                MEI = z
                TEI = t

        #print '[Fe/H] (initial) = '+str(MEI)

        vecgi = [0.5, 1.5, 2.5, 3.5, 4.5]
        cfi2 = 0.0
        for g in vecgi:
            nam = get_name(TEI, g, MEI)
            if ((os.access(model_path + 'vsini_0.0/R_0.0_' + nam,
                           os.F_OK) == True)):
                mod = pyfits.getdata(model_path + 'vsini_0.0/R_0.0_' + nam)

                mflg = mod[Ig]

                I = numpy.where(Fg != 0.0)[0]
                Ls = Lg[I]
                Fs = Fg[I]

                I = numpy.where((wag > Ls[0] - 5.0) & (wag < Ls[-1] + 5.0))[0]
                MLs = wag[I] * (1 + velo / lux)
                MFs = mflg[I]
                tck = interpolate.splrep(MLs, MFs, k=3, s=0)
                NMFs = interpolate.splev(Ls, tck, der=0)

                cc2 = integrate.simps(Fs[1:-1] * NMFs[1:-1]) / math.sqrt(
                    integrate.simps(Fs[1:-1] * Fs[1:-1]) *
                    integrate.simps(NMFs[1:-1] * NMFs[1:-1]))
                #print g,cc2
                if cc2 > cfi2:
                    cfi2 = cc2
                    LGI = g

        #print 'Log(g) (initial) = '+str(LGI)

    itera = 0
    maximo = 0
    calculated = []
    rotss = []
    vecR = []
    vecT = []
    vecG = []

    vecZ = []
    vecCF = []
    while itera < 4:

        if late == False:

            MOG = get_name(TEI, LGI, MEI)
            sc = pyfits.getdata(model_path + 'vsini_0.0/R_0.0_' + MOG)

            #print '-Calculating radial shift and v*sin(i) with model: '+MOG

            flm = sc[Im]
            ies = []
            flm = el_stl(wam, flm, SLi, SLf)
            for i in range(Lm.shape[0]):
                I = numpy.where(Fm[i] != 0.0)[0]
                Ls = Lm[i][I]
                Fs = Fm[i][I]
                Fs = el_stl(Ls, Fs, SLi * (1 + velo / lux),
                            SLf * (1 + velo / lux))
                I = numpy.where((wam > Ls[0] - 5.0) & (wam < Ls[-1] + 5.0))[0]
                ies.append(I)
                MLs = wam[I]
                MFs = flm[I]
                vv, cc = vels.CCF(MLs, MFs, Ls, Fs, -200, 200)
                if i == 0:
                    ccf = numpy.array(cc) * (Ls[-1] - Ls[0])
                    nor = Ls[-1] - Ls[0]
                else:
                    ccf = ccf + numpy.array(cc) * (Ls[-1] - Ls[0])
                    nor = nor + (Ls[-1] - Ls[0])
            nor2 = 1.
            ccf = ccf / nor
            vv = numpy.array(vv)
            B = 0.5 * (ccf[0] + ccf[-1])
            A = numpy.max(ccf) - B
            med = vv[numpy.where(ccf == numpy.max(ccf))[0]]
            sig = 20.0
            guess1 = [B, A, med, sig]
            ajustep = optimize.leastsq(res_gauss1, guess1, args=(ccf, vv))
            velo2 = ajustep[0][2]
            sig2 = ajustep[0][3]
            sig2 = math.sqrt(sig2 * sig2)
            #plt.plot(vv,ccf,vv,gauss1(ajustep[0],vv))
            #plt.show()
            #print 'radial velocity = '+str(velo2)+' km/s'
            #print 'Sigma = '+str(sig2)+' km/s'
            """			
				
			vi = 0.0
			difsigmin = 1000
			while vi <= 20.0:
				ai = 0
				if ( (os.access(model_path+'vsini_'+str(vi)+'/R_'+str(vi)+'_'+MOG,os.F_OK) == True)):
					modt = pyfits.getdata(model_path+'vsini_'+str(vi)+'/R_'+str(vi)+'_'+MOG)
					flm2 = modt[Im]
					flm2 = el_stl(wam,flm2,SLi,SLf)
					for I in ies:
						Fs = flm[I]
						Ls = wam[I]
						Fs = el_stl(Ls,Fs,SLi,SLf)
						MFs = flm2[I]
						vv2,cc2 = vels.CCF(Ls,MFs,Ls,Fs,-200,200)
						if ai == 0:
							ccf2 = numpy.array(cc2)*(Ls[-1]-Ls[0])
							nor2 = Ls[-1]-Ls[0]
						else:
							ccf2 = ccf2 + numpy.array(cc2)*(Ls[-1]-Ls[0])
							nor2 = nor2 + (Ls[-1]-Ls[0])
						ai += 1
					cc2 = ccf2/nor2
					vv2 = numpy.array(vv2)
					B3 = 0.5*(cc2[0]+cc2[-1])
					A3 = numpy.max(cc2)-B3
					med3 = 0.0
					sig3 = 20.0
					guess1 = [B3,A3,med3,sig3]
					ajustep=optimize.leastsq(res_gauss1,guess1,args=(cc2,numpy.array(vv2)))
					cte3 = ajustep[0][0]
					no3 = ajustep[0][1]
					med3 = ajustep[0][2]	
					sig3 = ajustep[0][3]
					
					#plt.plot(vv2,cc2)
					
					print vi,sig3
					difsig = math.sqrt((sig2-sig3)**2)
					if difsig < difsigmin:
						difsigmin = difsig
						rot = vi
				ai +=1
				
				if vi <= 7.5:
					vi = vi+2.5
				elif vi < 50.0:
					vi = vi+5.0
				else:
					break
					
			#plt.show()
			"""
            if sig2 >= vsini[0]:
                I = numpy.where((AT == TEI) & (AG == LGI) & (AZ == MEI))[0][0]
                anchos = numpy.array([
                    A000[I], A025[I], A050[I], A075[I], A100[I], A150[I],
                    A200[I], A250[I], A300[I], A350[I], A400[I], A450[I],
                    A500[I]
                ])
                kis = 0
                while kis < len(anchos) - 1:
                    if anchos[kis] > anchos[kis + 1]:
                        portemp = anchos[kis]
                        anchos[kis] = anchos[kis + 1]
                        anchos[kis + 1] = portemp
                    kis += 1
                tck = interpolate.splrep(anchos, numpy.array(vsini), k=3, s=0)
                calrot = interpolate.splev(sig2, tck, der=0)
                difs = (numpy.array(vsini) - calrot)**2
                AI = int(numpy.where(difs == numpy.min(difs))[0])
                rot = vsini[AI]
            else:
                rot = vsini[0]
                calrot = 0.0

            #rot = 2.5
            #print 'v*sin(i) = '+str(calrot)+' km/s'

        RI = numpy.where(numpy.array(rotss) == rot)[0]

        if len(RI) > 0:
            break

        else:

            model_path1 = model_path + "vsini_" + str(rot) + "/"
            """ conociendo la velocidad radial realizo una busqueda gruesa de parametros estelares calculando el maximo de la CCF"""
            #print "-Searching the optimal stellar model"

            #maxT,maxG,maxZ,maxCor,maxvs = 5500,4.0,0.0,0.0,0.0
            maxCor = 0.0

            if TEI >= 3500 and TEI <= 4250:
                modT = [3500, 4000, 4500, 5000]
            else:
                modT = [4000, 4500, 5000, 5500, 6000, 6500, 7000]

            modG = [1.0, 2.5, 4.0]
            #modZ = [-2.0,-1.0,0.0]
            modZ = [-1.0, 0.0]
            MOD, MOD2 = [], []

            i = 0
            while i < len(modZ):
                ig = 0
                while ig < len(modG):
                    it = 0
                    while it < len(modT):
                        MK = 'R_' + str(rot) + '_' + get_name(
                            modT[it], modG[ig], modZ[i])
                        if os.access(model_path1 + MK, os.F_OK):
                            MOD.append(MK)
                            MOD2.append(model_path1 + MK)
                        it = it + 1
                    ig = ig + 1
                i = i + 1

            MOD = np.array(MOD)
            MOD2 = np.array(MOD2)

            all_pars = np.vstack((MOD, np.zeros(len(MOD)) + velo2))
            L_Gl, F_Gl = L.copy(), F.copy()
            p = Pool(npools)
            xc_values = np.array((p.map(corr_p, MOD2)))
            p.terminate()
            I_min = np.argmax(xc_values)
            hd = pyfits.getheader(MOD2[I_min])
            maxCor = xc_values[I_min]
            maxG = hd['LOG_G']
            maxT = hd['TEFF']
            maxZ = hd['FEH']
            maxvs = hd['VSINI']
            #print MOD2[I_min]
            """
			print xc_values
			print MOD2[I_min],I_min
			print dfgh

			for m in MOD:
				
				hd = pyfits.getheader(model_path1+m)
				T  = hd['TEFF']
				G  = hd['LOG_G']
				Z  = hd['FEH']
				vs = hd['VSINI']

				sc = pyfits.getdata(model_path1+m)
				FF = sc[It]
				mwa = wat*(1+velo2/lux)
				NCCF = corr(L,F,mwa,FF,ons)

				#print T,G,Z,vs,NCCF
				
				if NCCF > maxCor:
					maxCor = NCCF
					maxG   = G
					maxT   = T
					maxZ   = Z
					maxvs  = vs
				elif NCCF<0:
					print "Problem with spectrum!!! -> Negative value of CCF."
					maxG   = 4.5
					maxT   = 5500
					maxZ   = 0
					maxvs  = 0
			"""

            #print 'maxgrueso',maxT,maxG,maxZ,maxvs
            """ A partir de los parametros encontrados en la busqueda gruesa procedo a constrenir los limites de la busqueda fina"""

            #Tfin,Gfin,Zfin,rotfin,velfin = 5500,4.0,0.0,0.0,0.0
            """ Ahora se buscan los par'ametros estelares optimos mediante una exploracion fina"""

            if maxT == 3500 or maxT == 4000:
                modT = [3500, 3750, 4000, 4250, 4500]

            elif maxT == 7000 or maxT == 6500:
                modT = [6000, 6250, 6500, 6750, 7000]
            else:
                modT = [
                    maxT - 750, maxT - 500, maxT - 250, maxT, maxT + 250,
                    maxT + 500, maxT + 750
                ]

            if maxG == 1.0:
                modG = [0.0, 0.5, 1.0, 1.5, 2.0, 2.5]
            if maxG == 2.5:
                modG = [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0]
            if maxG == 4.0:
                modG = [2.5, 3.0, 3.5, 4.0, 4.5, 5.0]

            if maxZ == -2.0:
                modZ = [-2.5, -2.0, -1.5, -1.0]
            if maxZ == -1.0:
                modZ = [-1.5, -1.0, -0.5, 0.0]
            if maxZ == 0.0:
                modZ = [-0.5, 0.0, 0.2, 0.5]

            MOD, MOD2 = [], []
            for i in modZ:
                for ig in modG:
                    for it in modT:
                        MK = 'R_' + str(rot) + '_' + get_name(it, ig, i)
                        if os.access(model_path1 + MK, os.F_OK):
                            MOD.append(MK)
                            MOD2.append(model_path1 + MK)
            MOD2 = np.array(MOD2)

            L_Gl, F_Gl = L.copy(), F.copy()
            p = Pool(npools)
            xc_values = np.array((p.map(corr_p, MOD2)))
            p.terminate()
            I_min = np.argmax(xc_values)
            hd = pyfits.getheader(MOD2[I_min])
            maxCor = xc_values[I_min]
            maxG = hd['LOG_G']
            maxT = hd['TEFF']
            maxZ = hd['FEH']
            maxvs = hd['VSINI']
            #print MOD2[I_min]
            """
			#maxT,maxG,maxZ,maxCor = 5500,4.0,0,0
			maxCor = 0.0
			for m in MOD:
				calculated.append(m)
				hd = pyfits.getheader(model_path1+m)
				T  = hd['TEFF']
				G  = hd['LOG_G']
				Z  = hd['FEH']

				sc = pyfits.getdata(model_path1+m)
				FF = sc[It]
				mwa = wat*(1+velo2/lux)
		
				NCCF = corr(L,F,mwa,FF,ons)
				
				vecT.append(T)
				vecG.append(G)
				vecZ.append(Z)
				vecR.append(rot)
				vecCF.append(NCCF)
				
				#print T,G,Z,NCCF

				if NCCF > maxCor:
					maxCor = NCCF
					maxT   = T
					maxG   = G
					maxZ   = Z

				elif NCCF < 0:
					print "Problem with spectrum!!! -> Negative value of CCF."
					maxG   = 4.5
					maxT   = 5500
					maxZ   = 0
					maxvs  = 0
			"""
            #print "maxfino",maxT,maxG,maxZ,maxCor

            TEI = maxT
            LGI = maxG
            MEI = maxZ

            ultrot = rot

            if maxCor > maximo:
                maximo = maxCor
                Tfin, Gfin, Zfin, rotfin, velfin = maxT, maxG, maxZ, rot, velo2
                #Tf,Gf,Zf = intert,interg,maZ

            late = False
            rotss.append(rot)

        itera = itera + 1
    if maximo == 0:
        Tfin, Gfin, Zfin, rotfin, velfin = 5500, 4.5, 0, 0, velo2
    #print 'Pars fase 0:', Tfin, Gfin, Zfin,rotfin,maximo
    mejor = False
    if rotfin == 0.0:
        nrot = [0.0, 2.5]
    elif rotfin == 2.5:
        nrot = [0.0, 2.5, 5.0]
    elif rotfin == 5.0:
        nrot = [2.5, 5.0, 7.5]
    elif rotfin == 7.5:
        nrot = [5.0, 7.5, 10.0]
    else:
        nrot = [rotfin - 5.0, rotfin, rotfin + 5.0]

    if Tfin == 3500:
        nT = [3500, 3750, 4000]
    elif Tfin == 7000:
        nT = [6500, 6750, 7000]
    else:
        nT = [Tfin - 250, Tfin, Tfin + 250]

    if Gfin == 0.0:
        nG = [0.0, 0.5, 1.0]
    elif Gfin == 0.5:
        nG = [0.0, 0.5, 1.0]
    elif Gfin == 4.5:
        nG = [4.0, 4.5, 5.0]
    elif Gfin == 5.0:
        nG = [4.0, 4.5, 5.0]
    else:
        nG = [Gfin - 0.5, Gfin, Gfin + 0.5]

    if Zfin == -2.5:
        nZ = [-2.5, -2.0, -1.5]
    elif Zfin == 0.5:
        nZ = [0.0, 0.2, 0.5]
    elif Zfin == 0.2:
        nZ = [0.0, 0.2, 0.5]
    elif Zfin == 0.0:
        nZ = [-0.5, 0.0, 0.2, 0.5]
    else:
        nZ = [Zfin - 0.5, Zfin, Zfin + 0.5]

    for v in nrot:
        model_path2 = model_path + 'vsini_' + str(v) + '/'
        names = []
        calc = numpy.array(calculated)

        for t in nT:
            for g in nG:
                for z in nZ:
                    nam = 'R_' + str(v) + '_' + get_name(t, g, z)
                    I = numpy.where(calc == nam)[0]
                    if len(I) == 0 and os.access(model_path2 + nam, os.F_OK):
                        names.append(nam)

        for fits in names:
            calculated.append(fits)
            hd = pyfits.getheader(model_path2 + fits)
            T = hd['TEFF']
            G = hd['LOG_G']
            Z = hd['FEH']

            sc = pyfits.getdata(model_path2 + fits)
            FF = sc[It]
            mwa = wat * (1 + velfin / lux)

            NCCF = corr(L, F, mwa, FF, ons)

            vecT.append(T)
            vecG.append(G)
            vecZ.append(Z)
            vecCF.append(NCCF)
            vecR.append(v)
            #print T,G,Z,NCCF

            if NCCF > maximo:
                mejor = True
                maximo = NCCF
                Tfin = T
                Gfin = G
                Zfin = Z
                rotfin = v

        #print Tfin,Gfin,Zfin,rotfin,maximo
        maximCCF = maximo

    total_inter = 1
    if total_inter == 0:
        deltaV = 0.5
        deltaG = 0.05
        deltaT = 50.0
        deltaZ = 0.1

        vCF = numpy.array(vecCF)
        vvT = numpy.array(vecT)
        vvG = numpy.array(vecG)
        vvZ = numpy.array(vecZ)
        vvV = numpy.array(vecR)

        ejeT = numpy.arange(numpy.min(nT), numpy.max(nT) + deltaT, deltaT)
        ejeV = numpy.arange(numpy.min(nrot), numpy.max(nrot) + deltaV, deltaV)
        ejeG = numpy.arange(numpy.min(nG), numpy.max(nG) + deltaG, deltaG)
        ejeZ = numpy.arange(numpy.min(nZ), numpy.max(nZ) + deltaZ, deltaZ)

        lejeV = len(ejeV)
        lejeG = len(ejeG)
        lejeT = len(ejeT)
        lejeZ = len(ejeZ)

        matCCF = numpy.zeros([lejeT, lejeG, lejeZ, lejeV], float)

        for v in nrot:
            posV = int(round((v - ejeV[0]) / deltaV))
            for z in nZ:
                posZ = int(round((z - ejeZ[0]) / deltaZ))
                for g in nG:
                    posG = int(round((g - ejeG[0]) / deltaG))
                    I = numpy.where((vvV == v) & (vvG == g) & (vvZ == z))[0]
                    if len(I) > 0:
                        vTt, vCFt = orden(vvT[I], vCF[I])

                        if len(I) > 3:
                            tck = interpolate.splrep(vTt, vCFt, k=3, s=0)
                            ynew = interpolate.splev(ejeT, tck, der=0)
                        elif len(I) > 1:
                            tck = interpolate.splrep(vTt,
                                                     vCFt,
                                                     k=len(I) - 1,
                                                     s=0)
                            ynew = interpolate.splev(ejeT, tck, der=0)
                        else:
                            ynew = numpy.zeros(len(ejeT), float) + vCFt[0]

                        matCCF[:, posG, posZ, posV] = ynew

        for v in nrot:
            posV = int(round((v - ejeV[0]) / deltaV))
            for z in nZ:
                posZ = int(round((z - ejeZ[0]) / deltaZ))
                for t in ejeT:
                    posT = int(round((t - ejeT[0]) / deltaT))
                    y1 = matCCF[posT, :, posZ, posV]
                    I = numpy.where(y1 != 0.0)[0]
                    y1b = y1[I]
                    x1b = ejeG[I]
                    if len(I) > 0:
                        if len(I) > 3:
                            tck = interpolate.splrep(x1b, y1b, k=3, s=0)
                            ynew = interpolate.splev(ejeG, tck, der=0)
                        elif len(I) > 1:
                            tck = interpolate.splrep(x1b,
                                                     y1b,
                                                     k=len(I) - 1,
                                                     s=0)
                            ynew = interpolate.splev(ejeG, tck, der=0)
                        else:
                            ynew = numpy.zeros(len(ejeG), float) + y1b[0]
                        matCCF[posT, :, posZ, posV] = ynew
        for v in nrot:
            posV = int(round((v - ejeV[0]) / deltaV))
            for t in ejeT:
                posT = int(round((t - ejeT[0]) / deltaT))
                for g in ejeG:
                    posG = int(round((g - ejeG[0]) / deltaG))
                    y1 = matCCF[posT, posG, :, posV]
                    I = numpy.where(y1 != 0.0)[0]
                    y1b = y1[I]
                    x1b = ejeZ[I]
                    if len(I) > 0:
                        if len(I) > 3:
                            tck = interpolate.splrep(x1b, y1b, k=3, s=0)
                            ynew = interpolate.splev(ejeZ, tck, der=0)
                        elif len(I) > 1:
                            tck = interpolate.splrep(x1b,
                                                     y1b,
                                                     k=len(I) - 1,
                                                     s=0)
                            ynew = interpolate.splev(ejeZ, tck, der=0)
                        else:
                            ynew = numpy.zeros(len(ejeZ), float) + y1b[0]
                        matCCF[posT, posG, :, posV] = ynew

        for t in ejeT:
            posT = int(round((t - ejeT[0]) / deltaT))
            for g in ejeG:
                posG = int(round((g - ejeG[0]) / deltaG))
                for z in ejeZ:
                    posZ = int(round((z - ejeZ[0]) / deltaZ))
                    y1 = matCCF[posT, posG, posZ, :]
                    I = numpy.where(y1 != 0.0)[0]
                    y1b = y1[I]
                    x1b = ejeV[I]
                    if len(I) > 0:
                        if len(I) > 3:
                            tck = interpolate.splrep(x1b, y1b, k=3, s=0)
                            ynew = interpolate.splev(ejeV, tck, der=0)
                        elif len(I) > 1:
                            tck = interpolate.splrep(x1b,
                                                     y1b,
                                                     k=len(I) - 1,
                                                     s=0)
                            ynew = interpolate.splev(ejeV, tck, der=0)
                        else:
                            ynew = numpy.zeros(len(ejeV), float) + y1b[0]
                        matCCF[posT, posG, posZ, :] = ynew

        I = numpy.where(matCCF == numpy.max(matCCF))

        intert = ejeT[I[0]]
        interg = ejeG[I[1]]
        interz = ejeZ[I[2]]
        interv = ejeV[I[3]]
        maximCCF = numpy.max(CCF)
        #print 'interp',intert,interg,interz,interv

    elif total_inter == 1:
        if Tfin == 3500:
            nT = [3500, 3750, 4000, 4250]
        elif Tfin == 3750:
            nT = [3500, 3750, 4000, 4250]
        elif Tfin == 7000:
            nT = [6250, 6500, 6750, 7000]
        elif Tfin == 6750:
            nT = [6250, 6500, 6750, 7000]
        else:
            nT = [Tfin - 500, Tfin - 250, Tfin, Tfin + 250, Tfin + 500]

        if Gfin == 0.0:
            nG = [0.0, 0.5, 1.0, 1.5]
        elif Gfin == 0.5:
            nG = [0.0, 0.5, 1.0, 1.5]
        elif Gfin == 4.5:
            nG = [3.5, 4.0, 4.5, 5.0]
        elif Gfin == 5.0:
            nG = [3.5, 4.0, 4.5, 5.0]
        else:
            nG = [Gfin - 1.0, Gfin - 0.5, Gfin, Gfin + 0.5, Gfin + 1.0]
        calc = numpy.array(calculated)
        model_path2 = model_path + 'vsini_' + str(rotfin) + '/'
        names = []

        for t in nT:
            for g in nG:
                nam = 'R_' + str(rotfin) + '_' + get_name(t, g, Zfin)
                I = numpy.where(calc == nam)[0]

                if len(I) == 0 and os.access(model_path2 + nam, os.F_OK):
                    names.append(nam)

        for fits in names:
            calculated.append(fits)
            hd = pyfits.getheader(model_path2 + fits)
            T = hd['TEFF']
            G = hd['LOG_G']
            Z = hd['FEH']

            sc = pyfits.getdata(model_path2 + fits)
            FF = sc[It]
            mwa = wat * (1 + velfin / lux)

            NCCF = corr(L, F, mwa, FF, ons)

            vecZ.append(Z)
            vecT.append(T)
            vecG.append(G)
            vecR.append(rotfin)
            vecCF.append(NCCF)

        VZ = numpy.array(vecZ)
        VR = numpy.array(vecR)
        VT = numpy.array(vecT)
        VG = numpy.array(vecG)
        VF = numpy.array(vecCF)

        I = numpy.where((VZ == Zfin) & (VR == rotfin))[0]
        VT2 = VT[I]
        VG2 = VG[I]
        VF2 = VF[I]
        deltaT = 50.0
        deltaG = 0.05

        ejeT = numpy.arange(numpy.min(numpy.array(nT)),
                            numpy.max(numpy.array(nT)) + deltaT, deltaT)
        ejeG = numpy.arange(numpy.min(numpy.array(nG)),
                            numpy.max(numpy.array(nG)) + deltaG, deltaG)

        lejeT = len(ejeT)
        lejeG = len(ejeG)

        matCCF = numpy.zeros([lejeT, lejeG], float)

        for g in nG:
            pos = int(round((g - ejeG[0]) / deltaG))
            I = numpy.where(VG2 == g)[0]
            if len(I) > 0:

                vTt, vCFt = orden(VT2[I], VF2[I])
                #print vTt,vCFt
                if len(I) > 3:
                    tck = interpolate.splrep(vTt, vCFt, k=3, s=0)
                    ynew = interpolate.splev(ejeT, tck, der=0)
                elif len(I) > 1:
                    tck = interpolate.splrep(vTt, vCFt, k=len(I) - 1, s=0)
                    ynew = interpolate.splev(ejeT, tck, der=0)
                else:
                    ynew = numpy.zeros(len(ejeT), float) + vCFt[0]

                matCCF[:, pos] = ynew

        for t in ejeT:
            pos1 = int(round((t - ejeT[0]) / deltaT))
            y1 = matCCF[pos1, :]
            I = numpy.where(y1 != 0.0)[0]
            y1b = y1[I]
            x1b = ejeG[I]
            if len(I) > 0:
                if len(I) > 3:
                    tck = interpolate.splrep(x1b, y1b, k=3, s=0)
                    ynew = interpolate.splev(ejeG, tck, der=0)
                elif len(I) > 1:
                    tck = interpolate.splrep(x1b, y1b, k=len(I) - 1, s=0)
                    ynew = interpolate.splev(ejeG, tck, der=0)
                else:
                    ynew = numpy.zeros(len(ejeG), float) + y1b[0]
            matCCF[pos1, :] = ynew

        I = numpy.where(matCCF == numpy.max(matCCF))

        intert = ejeT[I[0]][0]
        interg = ejeG[I[1]][0]
        interz = Zfin
        interv = rotfin
        #print intert,interg
        maximCCF = numpy.max(matCCF)

    else:
        intert, interg, interz, interv = Tfin, Gfin, Zfin, rotfin
    #print 'HI'
    if doplot:
        nam = 'R_' + str(rotfin) + '_' + get_name(Tfin, Gfin, Zfin)
        hd = pyfits.getheader(model_path2 + nam)
        sc = pyfits.getdata(model_path2 + nam)
        PWAV = ToVacuum(numpy.arange(len(sc)) * hd['CD1_1'] + hd['CRVAL1'])
        PWAV = PWAV * (1 + velfin / lux)
        for i in range(L1.shape[0]):
            I = numpy.where((PWAV > L1[i, 0]) & (PWAV < L1[i, -1]))[0]
            #print L1[i]
            #print F1[i]
            #print i
            f = figure()
            ylim(0., 1.01)
            plot(L1[i], F1[i])
            plot(PWAV[I], sc[I])
            xlabel('wavelenth [A]')
            ylabel('Continuum Normalized Flux')
            savefig(plot_dir + plot_name + '_' + str(int(L1[i, 0])) + '_' +
                    str(int(L1[i, -1])) + '.pdf',
                    format='pdf')

    return [intert, interg, interz, interv, velfin, maximCCF]
Beispiel #43
0
                        (currentCycle, int(data.split(',')[0].strip(' (')),
                         int(data.split(',')[1].strip(' )')) / 1024 / 1024))
                if topic == 'router-network-usage':
                    routerNetworkUsage.append(
                        (currentCycle, int(data.split(',')[0].strip(' (')),
                         int(data.split(',')[1].strip(' )')) / 1024 / 1024))
                if topic == 'open-channel':
                    channelCount += 1
                if topic == 'close-channel':
                    channelCount -= 1

        algName = algorithmName(stdoutFile)

        x_val = [e[0] for e in avgChannelCounts]
        y_val = [e[1] for e in avgChannelCounts]
        t, c, k = interpolate.splrep(x_val, y_val)
        spline = interpolate.BSpline(t, c, k, extrapolate=False)
        xx = np.arange(0, 520, 20)
        xx[0] = 1
        axs[0, 0].plot(xx,
                       spline(xx),
                       lineTypes[algName],
                       label=algNames[algName] + ' ' + scenarioName)

ax = axs[0, 0]
ax.set_xlabel('cycles')
ax.set_ylabel('avg channel count')

for ax in axs.flat:
    ax.set_xlim(left=-10, right=510)
    ax.grid()
Beispiel #44
0
    def compute_imf(self, s, plot=False):
        """
            Compute an intrinsic mode function from a signal s using sifting.
        """

        stop = False
        #make a copy of the signal
        imf = copy.copy(s)
        #find extrema for first iteration
        mini, maxi = find_extrema(s)

        if len(mini) == 0 or len(maxi) == 0:
            return None

        #keep track of extrema difference
        num_extrema = np.zeros(
            [self.sift_stoppage_S,
             2])  # first column are maxima, second column are minima
        num_extrema[-1, :] = [len(maxi), len(mini)]
        iter = 0
        while not stop:

            #set some things up for the iteration
            s_used = s
            left_padding = 0
            right_padding = 0

            #add an extra oscillation at the beginning and end of the signal to reduce edge effects; from Rato et. al (2008) section 3.2.2
            if self.sift_remove_edge_effects:
                Tl = maxi[0]  # index of left-hand (first) maximum
                tl = mini[0]  # index of left-hand (first) minimum

                Tr = maxi[-1]  # index of right hand (last) maximum
                tr = mini[-1]  # index of right hand (last) minimum

                #to reduce end effects, we need to extend the signal on both sides and reflect the first and last extrema
                #so that interpolation works better at the edges
                left_padding = max(Tl, tl)
                right_padding = len(s) - min(Tr, tr)

                #pad the original signal with zeros and reflected extrema
                s_used = np.zeros([len(s) + left_padding + right_padding])
                s_used[left_padding:-right_padding] = s

                #reflect the maximum on the left side
                imax_left = left_padding - tl
                s_used[imax_left] = s[Tl]
                #reflect the minimum on the left side
                imin_left = left_padding - Tl
                s_used[imin_left] = s[tl]

                #correct the indices on the right hand side so they're useful
                trr = len(s) - tr
                Trr = len(s) - Tr

                #reflect the maximum on the right side
                roffset = left_padding + len(s)
                imax_right = roffset + trr - 1
                s_used[imax_right] = s[Tr]
                #reflect the minimum on the right side
                imin_right = roffset + Trr - 1
                s_used[imin_right] = s[tr]

                #extend the array of maxima
                new_maxi = [i + left_padding for i in maxi]
                new_maxi.insert(0, imax_left)
                new_maxi.append(imax_right)
                maxi = new_maxi

                #extend the array of minima
                new_mini = [i + left_padding for i in mini]
                new_mini.insert(0, imin_left)
                new_mini.append(imin_right)
                mini = new_mini

            t = np.arange(0, len(s_used))
            fit_index = range(left_padding, len(s_used) - right_padding)

            #fit minimums with cubic splines
            spline_order = 3
            if len(mini) <= 3:
                spline_order = 1
            min_spline = splrep(mini, s_used[mini], k=spline_order)
            min_fit = splev(t[fit_index], min_spline)

            #fit maximums with cubic splines
            spline_order = 3
            if len(maxi) <= 3:
                spline_order = 1
            max_spline = splrep(maxi, s_used[maxi], k=spline_order)
            max_fit = splev(t[fit_index], max_spline)

            if plot:
                plt.figure()
                plt.plot(t[fit_index], max_fit, 'r-')
                plt.plot(maxi, s_used[maxi], 'ro')
                plt.plot(left_padding, 0.0, 'kx', markersize=10.0)
                plt.plot(left_padding + len(s), 0.0, 'kx', markersize=10.0)
                plt.plot(t, s_used, 'k-')
                plt.plot(t[fit_index], min_fit, 'b-')
                plt.plot(mini, s_used[mini], 'bo')
                plt.suptitle('Iteration %d' % iter)

            #take average of max and min splines
            z = (max_fit + min_fit) / 2.0

            #compute a factor used to dampen the subtraction of the mean spline; Rato et. al 2008, sec 3.2.3
            alpha, palpha = pearsonr(imf, z)
            alpha = min(alpha, 1e-2)

            #subtract off average of the two splines
            d = imf - alpha * z

            #set the IMF to the residual for next iteration
            imf = d

            #check for IMF S-stoppage criteria
            mini, maxi = find_extrema(imf)
            num_extrema = np.roll(num_extrema, -1, axis=0)
            num_extrema[-1, :] = [len(mini), len(maxi)]
            if iter >= self.sift_stoppage_S:
                num_extrema_change = np.diff(num_extrema, axis=0)
                de = np.abs(num_extrema[-1, 0] - num_extrema[-1, 1])
                if np.abs(num_extrema_change).sum() == 0 and de < 2 and np.abs(
                        imf.mean()) < self.sift_mean_tol:
                    stop = True
            if iter > self.sift_max_iter:
                stop = True
            print 'Iter %d: len(mini)=%d, len(maxi=%d), imf.mean()=%0.6f, alpha=%0.2f' % (
                iter, len(mini), len(maxi), imf.mean(), alpha)
            #print 'num_extrema=',num_extrema
            iter += 1
        return imf
Beispiel #45
0
def c_correlateX(a,
                 v,
                 returnx=False,
                 returnav=False,
                 s=0,
                 xran=None,
                 coarse=False,
                 interp='spl'):
    '''

    :param a:
    :param v: a and v can be a dict in following format {'x':[],'y':[]}. The length of a and v can be different.
    :param returnx:
    :return:
    '''
    from scipy.interpolate import splev, splrep
    import numpy.ma as ma

    if isinstance(a, dict):
        max_a = np.nanmax(a['x'])
        min_a = np.nanmin(a['x'])
        max_v = np.nanmax(v['x'])
        min_v = np.nanmin(v['x'])
        max_ = min(max_a, max_v)
        min_ = max(min_a, min_v)
        if not max_ > min_:
            print('the x ranges of a and v have no overlap.')
            return None
        if xran is not None:
            max_ = min(max_, xran[1])
            min_ = max(min_, xran[0])
        a_x = ma.masked_outside(a['x'].copy(), min_, max_)
        if isinstance(a['y'], np.ma.core.MaskedArray):
            a_y = ma.masked_array(a['y'].copy(), a['y'].mask | a_x.mask)
            a_x = ma.masked_array(a_x, a_y.mask)
        else:
            a_y = ma.masked_array(a['y'].copy(), a_x.mask)
        v_x = ma.masked_outside(v['x'].copy(), min_, max_)
        if isinstance(v['y'], np.ma.core.MaskedArray):
            v_y = ma.masked_array(v['y'].copy(), v['y'].mask | v_x.mask)
            v_x = ma.masked_array(v_x, v_y.mask)
        else:
            v_y = ma.masked_array(v['y'].copy(), v_x.mask)

        dx_a = np.abs(np.nanmean(np.diff(a_x)))
        dx_v = np.abs(np.nanmean(np.diff(v_x)))

        if coarse:
            if dx_a < dx_v:
                icase = 0
            elif dx_a >= dx_v:
                icase = 1
        else:
            if dx_a >= dx_v:
                icase = 0
            elif dx_a < dx_v:
                icase = 1

        if icase == 0:
            v_ = v_y.compressed()
            x_ = v_x.compressed()
            if interp == 'spl':
                tck = splrep(a_x.compressed(), a_y.compressed(), s=s)
                ys = splev(x_, tck)
            else:
                ys = np.interp(x_, a_x.compressed(), a_y.compressed())
            a_ = ys
        else:
            a_ = a_y.compressed()
            x_ = a_x.compressed()
            if interp == 'spl':
                tck = splrep(v_x.compressed(), v_y.compressed(), s=s)
                ys = splev(x_, tck)
            else:
                ys = np.interp(x_, v_x.compressed(), v_y.compressed())
            v_ = ys
    else:
        a_ = a.copy()
        v_ = v.copy()
        x_ = None
    a_ = (a_ - np.nanmean(a_)) / (np.nanstd(a_) * len(a_))
    v_ = (v_ - np.nanmean(v_)) / np.nanstd(v_)
    print(a_.shape, v_.shape, x_.shape)
    if returnx:
        if x_ is None:
            return [
                np.arange(len(a_)) - np.floor(len(a_) / 2.0),
                np.correlate(a_, v_, mode='same')
            ]
        else:
            return [(np.arange(len(a_)) - np.floor(len(a_) / 2.0)) *
                    np.nanmean(np.diff(x_)),
                    np.correlate(a_, v_, mode='same'), x_, a_, v_]
    else:
        return np.correlate(a_, v_, mode='same')
Beispiel #46
0
def port_opt_theory():
    pdb.set_trace()
    rets = port_opt_data()
    noa = len(rets.columns)
    print(rets.mean() * 252)
    print(rets.cov() * 252)

    weights = np.random.random(noa)
    weights /= np.sum(weights)
    print(weights)
    print(np.sum(rets.mean() * weights) * 252)
    # expected portfolio variance
    print(np.dot(weights.T, np.dot(rets.cov() * 252, weights)))
    # expected portfolio standard deviation/volatility 
    print(np.sqrt(np.dot(weights.T, np.dot(rets.cov() * 252, weights))))
    prets = []
    pvols = []
    for p in range (250):
        weights = np.random.random(noa)
        weights /= np.sum(weights)
        prets.append(np.sum(rets.mean() * weights) * 252)
        pvols.append(np.sqrt(np.dot(weights.T, 
                            np.dot(rets.cov() * 252, weights))))
    prets = np.array(prets)
    pvols = np.array(pvols)
    
    plt.figure(figsize=(8, 4))
    plt.scatter(pvols, prets, c=prets / pvols, marker='o')
    plt.grid(True)
    plt.xlabel('expected volatility')
    plt.ylabel('expected return')
    # plt.colorbar(label='Sharpe ratio')
    plt.savefig(PATH + 'port_opt1.png', dpi=300)
    plt.close()
    
    cons = ({'type': 'eq', 'fun': lambda x:  np.sum(x) - 1})
    bnds = tuple((0, 1) for x in range(noa))
    print(noa * [1. / noa,])
    opts = timeme(sco.minimize)(min_func_sharpe, noa * [1. / noa,], method='SLSQP',
                           bounds=bnds, constraints=cons)
    print(opts)
    print(opts['x'].round(3))
    print(statistics(opts['x']).round(3))

    optv = timeme(sco.minimize)(min_func_variance, noa * [1. / noa,], method='SLSQP',
                           bounds=bnds, constraints=cons)
    print(optv)
    print(optv['x'].round(3))
    print(statistics(optv['x']).round(3))
    cons = ({'type': 'eq', 'fun': lambda x:  statistics(x)[0] - tret},
            {'type': 'eq', 'fun': lambda x:  np.sum(x) - 1})
    bnds = tuple((0, 1) for x in weights)
    
    # Efficient Frontier
    # trets = np.linspace(0.0, 0.25, 50)
    trets = np.linspace(0.0, 0.25, 4)
    tvols = []
    for tret in trets:
        cons = ({'type': 'eq', 'fun': lambda x:  statistics(x)[0] - tret},
                {'type': 'eq', 'fun': lambda x:  np.sum(x) - 1})
        res = timeme(sco.minimize)(min_func_port, noa * [1. / noa,], method='SLSQP',
                           bounds=bnds, constraints=cons)
        tvols.append(res['fun'])
    
    tvols = np.array(tvols)
    plt.figure(figsize=(8, 4))
    plt.scatter(pvols, prets,
                c=prets / pvols, marker='o')
                # random portfolio composition
    plt.scatter(tvols, trets,
                c=trets / tvols, marker='x')
                # efficient frontier
    plt.plot(statistics(opts['x'])[1], statistics(opts['x'])[0],
            'r*', markersize=15.0)
            # portfolio with highest Sharpe ratio
    plt.plot(statistics(optv['x'])[1], statistics(optv['x'])[0],
         'y*', markersize=15.0)
            # minimum variance portfolio
    plt.grid(True)
    plt.xlabel('expected volatility')
    plt.ylabel('expected return')
    # plt.colorbar(label='Sharpe ratio')
    plt.savefig(PATH + 'port_opt_efficient_frontier.png', dpi=300)
    plt.close()
    
    ind = np.argmin(tvols)
    evols = tvols[ind:]
    erets = trets[ind:]
    tck = sci.splrep(evols, erets)
    
    def f(x):
        ''' Efficient frontier function (splines approximation). '''
        return sci.splev(x, tck, der=0)
    def df(x):
        ''' First derivative of efficient frontier function. '''
        return sci.splev(x, tck, der=1)

    def equations(p, rf=0.01):
        eq1 = rf - p[0]
        eq2 = rf + p[1] * p[2] - f(p[2])
        eq3 = p[1] - df(p[2])
        return eq1, eq2, eq3

    opt = sco.fsolve(equations, [0.01, 0.5, 0.15])
    print(opt)
    print(np.round(equations(opt), 6))

    plt.figure(figsize=(8, 4))
    plt.scatter(pvols, prets,
                c=(prets - 0.01) / pvols, marker='o')
                # random portfolio composition
    plt.plot(evols, erets, 'g', lw=4.0)
                # efficient frontier
    cx = np.linspace(0.0, 0.3)
    plt.plot(cx, opt[0] + opt[1] * cx, lw=1.5)
                # capital market line
    plt.plot(opt[2], f(opt[2]), 'r*', markersize=15.0) 
    plt.grid(True)
    plt.axhline(0, color='k', ls='--', lw=2.0)
    plt.axvline(0, color='k', ls='--', lw=2.0)
    plt.xlabel('expected volatility')
    plt.ylabel('expected return')
    # plt.colorbar(label='Sharpe ratio')
    plt.savefig(PATH + 'port_opt_capital_market_line.png', dpi=300)
    plt.close()

    cons = ({'type': 'eq', 'fun': lambda x:  statistics(x)[0] - f(opt[2])},
            {'type': 'eq', 'fun': lambda x:  np.sum(x) - 1})
    res = timeme(sco.minimize)(min_func_port, noa * [1. / noa,], method='SLSQP',
                           bounds=bnds, constraints=cons)
    print(res['x'].round(3))
Beispiel #47
0
    })
    results = sco.minimize(get_EF,
                           init_guess,
                           method='SLSQP',
                           bounds=boundary_new,
                           constraints=cons)
    tgstdv.append(results['fun'])
tgstdv = np.array(tgstdv)
''' Find CML using CAPM with risk-free rate being 5%'''

#For the spline interpolation, only consider portfolios under the EF
i = np.argmin(tgstdv)  #return indices of the min values along an axis
pfbystdv = tgstdv[i:]
pfbyrtn = tgrtns[i:]

tck = sci.splrep(pfbyrtn, pfbystdv)  #sci.splrep(x,y,...) where y=f(x)


def func(x):
    '''Spline approximation for the EF'''
    return sci.splev(x, tck, der=0)


def firstder(x):
    return sci.splev(x, tck, der=1)


def get_CAPM(param, rf=0.05):
    capm_1 = rf - param[0]
    capm_2 = rf + param[1] * param[2] - func(param[2])
    capm_3 = param[1] - firstder(param[2])
Beispiel #48
0
        k_t = k_c + k_i
        line_attenuation = attenuation(f_d, device.data_file,
                                       'drive_attenuation') - 3
        if 'powers' in list(hdf5_file[date][time]['LIN'].keys()):
            log_powers = np.asarray(hdf5_file[date][time]['LIN'].get('powers'))
        elif 'powers_swept' in list(hdf5_file[date][time]['LIN'].keys()):
            log_powers = np.asarray(
                hdf5_file[date][time]['LIN'].get('powers_swept'))
        log_powers = log_powers + line_attenuation
        powers = dBm_to_Watts(log_powers)
        stark_shift = f_0 - f_0[0]
        nbar = 1 / (2 * np.pi * h) * powers * k_c[0] / f_0[0] * (
            (f_d / f_0[0])**2) / ((f_d - f_0[0])**2 + (k_t[0] / 2)**2 *
                                  ((2 * f_d / (f_0[0] + f_d))**2))

        tck = interpolate.splrep(nbar, stark_shift, s=0)

        f = Flux(current, device.a, device.b)
        fudge_factor = 2.3
        g3_ = device.g3_distributed(f) * fudge_factor
        g4_ = device.g4_distributed(f)
        freq_a = f_0[0]
        kappa = k_c[0]
        kappa_t = k_t[0]
        kappa_c = k_c[0]

        n_crit = max(nbar)
        n_crit = 20000
        nbar_pump_sweep = np.linspace(0, n_crit, 5000)
        pump_freqs = np.linspace(2 * freq_a - 1.2e9, 2 * freq_a + 1.2e9, 400)
        gains = np.zeros((len(pump_freqs), 5000))
def interpolare(y, x):
    fct = interpolate.splrep(x,y) #se creeaza curba de interpolare
    new_x = np.arange(x[0],x[0]+2000, 4) # pastram 2000 de secunde / 4 secunde - 500 mom timp
    return interpolate.splev(x, fct) #se iau noile valori in functie de curba
Beispiel #50
0
def odepsi_m(E, r, U, m):

    chi_u = np.zeros((len(r), len(E)))
    chi_d = np.zeros(np.shape(chi_u))

    chi_u[0, :], chi_d[0, :] = psireg(r[0], E - U[0], m)

    nchi = np.abs(chi_u[0, :]) + np.abs(chi_d[0, :])  # this does not involve
    # squares of small numbers
    np.sqrt(chi_u[0, :]**2 + chi_d[0, :]**2)  # unlike prev version

    chi_u[0, :] /= nchi
    chi_d[0, :] /= nchi

    if m >= 0:
        Ku = 0
        Kd = -1.0 - 2.0 * m
        mu = m
    else:
        mu = -m - 1
        Ku = 1.0 + 2.0 * m
        Kd = 0

    #
    #  Right-hand side of the ODE: chi'(r) = rhs(r)
    #

    def rhs(chi_u, chi_d, r, U):
        f_u = Ku / r * chi_u - (E - U) * chi_d
        f_d = (E - U) * chi_u + Kd / r * chi_d
        #f_u = (m-mu)/r * chi_u - (E - U)*chi_d
        #f_d = (E - U)*chi_u - (1 + mu + m)/r * chi_d
        return f_u, f_d

    Us = interpolate.splrep(r, U)

    #
    # RK4 fourth order method
    #
    def rk4step(chi_u, chi_d, r_p, r_n, h):
        r1 = r_p
        r2 = r_p + 0.5 * h
        r3 = r2
        r4 = r_n

        #rs = np.array([r1, r2, r3, r4])
        Ui = interpolate.splev(np.array([r1, r2, r3, r4]), Us)

        U1 = Ui[0]
        U2 = Ui[1]
        U3 = Ui[2]
        U4 = Ui[3]

        k1u, k1d = rhs(chi_u, chi_d, r1, U1)
        k2u, k2d = rhs(chi_u + k1u * 0.5 * h, chi_d + 0.5 * k1d * h, r2, U2)
        k3u, k3d = rhs(chi_u + k2u * 0.5 * h, chi_d + 0.5 * k2d * h, r3, U3)
        k4u, k4d = rhs(chi_u + k3u * h, chi_d + k3d * h, r4, U4)

        chi_un = chi_u + h * (k1u + 2 * k2u + 2 * k3u + k4u) / 6.0
        chi_dn = chi_d + h * (k1d + 2 * k2d + 2 * k3d + k4d) / 6.0
        return chi_un, chi_dn

    for i in range(1, len(r)):
        chi_un, chi_dn = chi_u[i - 1, :], chi_d[i - 1, :]
        h = r[i] - r[i - 1]
        dxi = max(abs(E)) * h
        # dxi is the phase change over this grid step
        # We split the step into smaller sub-steps if dxi
        # is too large
        dxi0 = 0.1
        n_steps = int(dxi / dxi0) + 1
        dr = h / n_steps
        # Go over sub-steps
        for i_step in range(n_steps):
            r_p = r[i - 1] + i_step * dr
            r_n = r[i - 1] + (i_step + 1) * dr
            chi_un, chi_dn = rk4step(chi_un, chi_dn, r_p, r_n, dr)

        chi_u[i, :], chi_d[i, :] = chi_un, chi_dn
    uu, ud = psireg(r[-1], E, m)
    vu, vd = psising(r[-1], E, m)
    D = uu * vd - vu * ud
    A = (chi_u[-1, :] * vd - chi_d[-1, :] * vu) / D
    B = (chi_d[-1, :] * uu - chi_u[-1, :] * ud) / D
    r_mu = (r / r[-1])**(mu)
    #
    # The goal of the crazy expression below is to multiply
    # chi by the corresponding values of r^mu. In numpy, multiplying
    # a matrix chi(r, E) by vector r would not work: we have to
    # transpose the matrix first.
    #
    psi_u = (r_mu * chi_u.transpose()).transpose()
    psi_d = (r_mu * chi_d.transpose()).transpose()
    psi_u /= np.sqrt(A**2 + B**2)
    psi_d /= np.sqrt(A**2 + B**2)

    return psi_u, psi_d
Beispiel #51
0
def fit_subsamplefof_mean():
    parser = argparse.ArgumentParser(description='This is the MCMC code to get the fitting parameters using Zvonimir model, made by Zhejie Ding.')
    parser.add_argument('-rec_id', "--rec_id", help='The id of reconstruction, either 0 or 1.', required=True)   #0: pre-reconstruct; 1: post-reconstruct
    args = parser.parse_args()
    rec_id = int(args.rec_id)
    print("rec_id: ", rec_id)
    
    N_walkers = 200
    N_walkersteps = 20000
    space_id = 1                       # in redshift space
    print("N_walkers: ", N_walkers, "N_walkersteps: ", N_walkersteps, "\n")
    
    # 0: parameter fixed, 1: parameter free.
    params_indices = [1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1]    # b0 needs to be fitted. For this case, make sure \Sigma_0,2,4 positive, which should be set in mcmc_routine.
    ##params_indices = [0, 1, 0, 0, 1, 1]    # For this case, make sure \alpha_1 = \alpha_2 in the function lnlike(..) and set sigma_xy, sigma_z equal to Sigma_z.
    print("params_indices: ", params_indices)
    
    # simulation run name
    N_dataset = 20
    N_mu_bin = 100
    #N_skip_header = 11
    #N_skip_footer = 31977
    Omega_m = 0.3075
    G_0 = growth_factor(0.0, Omega_m) # G_0 at z=0, normalization factor
    Volume = 1380.0**3.0   # the volume of simulation box


    sim_z=['0', '0.6', '1.0']
    sim_seed = [0, 9]
    sim_wig = ['NW', 'WG']
    sim_a = ['1.0000', '0.6250', '0.5000']
    sim_space = ['r', 's']     # r for real space; s for redshift space
    rec_dirs = ['DD', 'ALL']   # "ALL" folder stores P(k, \mu) after reconstruction process, while DD is before reconstruction.
    rec_fprefix = ['', 'R']

    mcut_Npar_list = [[37, 149, 516, 1524, 3830],
                      [35, 123, 374, 962, 2105],
                      [34, 103, 290, 681, 1390]]
    N_masscut = np.size(mcut_Npar_list, axis=1)

    # Sigma_sm = sqrt(2.* Sig_RR) in post-reconstruction case, for pre-reconstruction, we don't use sub_Sigma_RR.
    Sigma_RR_list = [[37, 48.5, 65.5, 84.2, 110],
                     [33, 38, 48.5, 63.5, 91.5],
                     [31, 38, 49, 65, 86]]
    sub_Sigma_RR = 50.0       # note from Hee-Jong's recording

    inputf = '../Zvonimir_data/planck_camb_56106182_matterpower_smooth_z0.dat'
    k_smooth, Pk_smooth = np.loadtxt(inputf, dtype='f8', comments='#', unpack=True)
    tck_Pk_sm = interpolate.splrep(k_smooth, Pk_smooth)

    inputf = '../Zvonimir_data/planck_camb_56106182_matterpower_z0.dat'
    k_wiggle, Pk_wiggle = np.loadtxt(inputf, dtype='f8', comments='#', unpack=True)
    tck_Pk_linw = interpolate.splrep(k_wiggle, Pk_wiggle)


    # firstly, read one file and get k bins we want for the fitting range
    dir0='/Users/ding/Documents/playground/WiggleNowiggle/subsample_FoF_data_HS/Pk_obs_2d_wnw_mean_DD_ksorted_mu_masscut/'
    inputf = dir0 +'fof_kaver.wnw_diff_a_0.6250_mcut35_fraction0.126.dat'
    k_p, mu_p = np.loadtxt(inputf, dtype='f8', comments='#', delimiter=' ', usecols=(0,1), unpack=True)
    #print(k_p, mu_p)
    N_fitbin = len(k_p)
    #print('# of (k, mu) bins: ', N_fitbin)

    # for output parameters fitted
    odir = './params_{}_wig-now_b_bscale_fitted_mean_dset/'.format(rec_dirs[rec_id])
    if not os.path.exists(odir):
        os.makedirs(odir)
    

    alpha_1, alpha_2, sigma_0, sigma_2, sigma_4, f, b_0, b_scale0, b_scale2, b_scale4  = 1.0, 1.0, 8.0, 8.0, 8.0, 0.2, 1.0, 0.0, 0.0, 0.0
    all_names = "alpha_1", "alpha_2", "sigma_0", "sigma_2", "sigma_4", "sigma_sm", "f", "b_0", "b_scale0", "b_scale2", "b_scale4"    # the same order for params_indices
    all_temperature = 0.01, 0.01, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 1.0, 1.0, 1.0
    pool = MPIPool(loadbalance=True)
    for z_id in xrange(1):
        norm_gf = growth_factor(float(sim_z[z_id]), Omega_m)/G_0
#        for mcut_id in xrange(N_masscut):
#            np.random.seed()
#            sigma_sm = (float(Sigma_RR_list[z_id][mcut_id])*2.0)**0.5
#            all_params = alpha_1, alpha_2, sigma_0, sigma_2, sigma_4, sigma_sm, f, b_0, b_scale0, b_scale2, b_scale4
#            N_params, theta, fix_params, params_T, params_name = set_params(all_params, params_indices, all_names, all_temperature)
#
#            ifile_Pk = './run2_3_Pk_obs_2d_wnw_mean_{}_ksorted_mu_masscut/{}kave{}.wig_minus_now_mean_fof_a_{}_mcut{}.dat'.format(rec_dirs[rec_id], rec_fprefix[rec_id], sim_space[space_id], sim_a[z_id], mcut_Npar_list[z_id][mcut_id])
#            print(ifile_Pk)
#            Pk_wnw_diff_obs = np.loadtxt(ifile_Pk, dtype='f4', comments='#', usecols=(2,)) # be careful that there are k, \mu, P(k, \mu) columns.
#
#            ifile_Cov_Pk = './run2_3_Cov_Pk_obs_2d_wnw_{}_ksorted_mu_masscut/{}kave{}.wig_minus_now_mean_fof_a_{}_mcut{}.dat'.format(rec_dirs[rec_id], rec_fprefix[rec_id], sim_space[space_id], sim_a[z_id], mcut_Npar_list[z_id][mcut_id])
#            Cov_Pk_wnw = np.loadtxt(ifile_Cov_Pk, dtype='f4', comments='#')
#            ivar_Pk_wnow = N_dataset/np.diag(Cov_Pk_wnw)                                   # the mean sigma error
#
#            params_mcmc = mcmc_routine(N_params, N_walkers, N_walkersteps, theta, params_T, params_indices, fix_params, k_p, mu_p, Pk_wnw_diff_obs, ivar_Pk_wnow, tck_Pk_linw, tck_Pk_sm, norm_gf, params_name, pool)
#
#            chi_square = chi2(params_mcmc[:, 0], params_indices, fix_params, k_p, mu_p, Pk_wnw_diff_obs, ivar_Pk_wnow, tck_Pk_linw, tck_Pk_sm, norm_gf)
#            reduced_chi2 = chi_square/(N_fitbin-N_params)
#            print("Reduced chi2: {}\n".format(reduced_chi2))
#            # output parameters into a file
#            ofile_params = odir + 'ZV_fof_{}kave{}.wnw_diff_a_{}_mcut{}_params{}_Sigma_sm{}.dat'.format(rec_fprefix[rec_id], sim_space[space_id], sim_a[z_id], mcut_Npar_list[z_id][mcut_id], ''.join(map(str, params_indices)), round(sigma_sm, 3))
#            write_params(ofile_params, params_mcmc, params_name, reduced_chi2)

        np.random.seed()
        sigma_sm = 10.0  # for all redshifts, it's the same value.
        all_params = alpha_1, alpha_2, sigma_0, sigma_2, sigma_4, sigma_sm, f, b_0, b_scale0, b_scale2, b_scale4
        N_params, theta, fix_params, params_T, params_name = set_params(all_params, params_indices, all_names, all_temperature)
        
        # Fit for DM power spectrum
        ifile_Pk = './run2_3_sub_Pk_2d_wnw_mean_{}_ksorted_mu/{}kave{}.wig_minus_now_mean_sub_a_{}.dat'.format(rec_dirs[rec_id], rec_fprefix[rec_id], sim_space[space_id], sim_a[z_id])
        Pk_wnw_diff_true = np.loadtxt(ifile_Pk, dtype='f4', comments='#', usecols=(2,)) # be careful that there are k, \mu, P(k, \mu) columns.
        print(ifile_Pk)
        ifile_Cov_Pk = './run2_3_sub_Cov_Pk_2d_wnw_{}_ksorted_mu/{}kave{}.wig_minus_now_mean_sub_a_{}.dat'.format(rec_dirs[rec_id], rec_fprefix[rec_id], sim_space[space_id], sim_a[z_id])
        Cov_Pk_wnw = np.loadtxt(ifile_Cov_Pk, dtype='f4', comments='#')
        ivar_Pk_wnow = N_dataset/np.diag(Cov_Pk_wnw)                              # the mean sigma error

        params_mcmc = mcmc_routine(N_params, N_walkers, N_walkersteps, theta, params_T, params_indices, fix_params, k_p, mu_p, Pk_wnw_diff_true, ivar_Pk_wnow, tck_Pk_linw, tck_Pk_sm, norm_gf, params_name, pool)
        chi_square = chi2(params_mcmc[:, 0], params_indices, fix_params, k_p, mu_p, Pk_wnw_diff_true, ivar_Pk_wnow, tck_Pk_linw, tck_Pk_sm, norm_gf)
        reduced_chi2 = chi_square/(N_fitbin-N_params)
        print('Reduced chi2: {}\n'.format(reduced_chi2))
        ofile_params = odir + 'ZV_sub_{}kave{}.wnw_diff_a_{}_params{}_Sigma_sm{}.dat'.format(rec_fprefix[rec_id], sim_space[space_id], sim_a[z_id], ''.join(map(str, params_indices)), round(sigma_sm, 3))
        write_params(ofile_params, params_mcmc, params_name, reduced_chi2)
    pool.close()
Beispiel #52
0
def thermal_hall_with_hopping():
    plt.ion()
    fig, axes = plt.subplots(nrows=1, ncols=3)
    fig.subplots_adjust(left=0.05,
                        right=0.98,
                        top=0.98,
                        bottom=0.16,
                        hspace=0.2,
                        wspace=0.25)

    # phase diagram
    ax = axes[0]
    ts = np.array([
        0.2445, 0.2600, 0.2700, 0.2732, 0.2770, 0.2790, 0.2800, 0.2850, 0.2900,
        0.2950, 0.3000, 0.3080, 0.3100, 0.3155, 0.3200, 0.3250, 0.3300, 0.3350,
        0.3400, 0.3450, 0.3495
    ])
    us = np.array([
        0.0000, 0.2825, 0.4285, 0.4705, 0.5195, 0.5415, 0.5515, 0.5975, 0.6375,
        0.6695, 0.6945, 0.7075, 0.7065, 0.6945, 0.6775, 0.6505, 0.6135, 0.5335,
        0.3545, 0.1665, 0.0000
    ])
    X = np.linspace(ts.min(), ts.max(), 201)
    Y = itp.splev(X, itp.splrep(ts, us, k=3), der=0)
    ax.plot(X, Y, lw=2, color='blue', zorder=1)

    ts = np.array([0.2732, 0.2800, 0.2850, 0.2900, 0.2950, 0.3000, 0.3080])
    us = np.array([0.4705, 0.3835, 0.3215, 0.2615, 0.2005, 0.1315, 0.0000])
    X = np.linspace(ts.min(), ts.max(), 201)
    Y = itp.splev(X, itp.splrep(ts, us, k=3), der=0)
    ax.plot(X, Y, lw=2, color='blue', zorder=1)

    ts = np.array([0.3080, 0.3100, 0.3155, 0.3200, 0.3250, 0.3300, 0.3350])
    us = np.array([0.0000, 0.0235, 0.1325, 0.2545, 0.3585, 0.4605, 0.5335])
    X = np.linspace(ts.min(), ts.max(), 201)
    Y = itp.splev(X, itp.splrep(ts, us, k=3), der=0)
    ax.plot(X, Y, lw=2, color='blue', zorder=1)

    ax.plot([0.29, 0.32], [0.0, 0.0],
            ls='solid',
            lw=3,
            color='green',
            zorder=0,
            alpha=0.6,
            clip_on=False)
    ax.plot([0.29, 0.32], [0.1, 0.1],
            ls='solid',
            lw=3,
            color='red',
            zorder=0,
            alpha=0.6)

    ax.minorticks_on()
    ax.set_xlim(0.24, 0.36)
    ax.set_ylim(0.00, 0.75)
    ax.set_xticks(np.linspace(0.24, 0.36, 7))
    ax.set_xticklabels(['0.24', '', '0.28', '', '0.32', '', '0.36'])
    ax.set_yticks(np.linspace(0.0, 0.7, 8))
    ax.set_yticklabels(['0', '', '0.2', '', '0.4', '', '0.6', ''])
    for tick in ax.get_xticklabels():
        tick.set_fontsize(9)
    for tick in ax.get_yticklabels():
        tick.set_fontsize(9)
    ax.set_xlabel("$t^\prime/t$", fontdict={'fontsize': 10})
    ax.set_ylabel("$\Delta U/t$", fontdict={'fontsize': 10})

    ax.text(0.26,
            0.53,
            "NFM",
            fontsize=9,
            color='black',
            ha='center',
            va='center')
    ax.text(0.305,
            0.43,
            "FM",
            fontsize=9,
            color='black',
            ha='center',
            va='center')
    ax.text(0.305,
            0.31,
            "$(C=0)$",
            fontsize=9,
            color='black',
            ha='center',
            va='center')
    ax.text(0.275,
            0.2,
            "TFM$^+$",
            fontsize=9,
            color='black',
            ha='center',
            va='center')
    ax.text(0.275,
            0.1,
            "$(C=+1)$",
            fontsize=9,
            color='black',
            ha='center',
            va='center')
    ax.text(0.3305,
            0.2,
            "TFM$^-$",
            fontsize=9,
            color='black',
            ha='center',
            va='center')
    ax.text(0.3305,
            0.1,
            "$(C=-1)$",
            fontsize=9,
            color='black',
            ha='center',
            va='center')

    # line 1
    ax = axes[1]
    name = '%s/HCI_H2(1P-1P)_up_1.0_0.656_1.2_1.2_FBFM_THP30(1.0,1.0)_0.08.dat' % directory
    data = np.loadtxt(name)
    ax.plot(data[:, 0], data[:, 1] / 0.08, lw=2, color='green')
    ax.text(0.300,
            -0.20,
            "TFM$^+$",
            fontsize=9,
            color='black',
            ha='center',
            va='center')
    ax.text(0.315,
            -0.20,
            "TFM$^-$",
            fontsize=9,
            color='black',
            ha='center',
            va='center')
    ax.minorticks_on()
    ax.set_xlim(0.29, 0.32)
    ax.set_xticks(np.linspace(0.29, 0.32, 4))
    # ax.set_ylim(-0.7, 0.4)
    # ax.set_yticks(np.linspace(-0.6, 0.4, 6))
    for tick in ax.get_xticklabels():
        tick.set_fontsize(9)
    for tick in ax.get_yticklabels():
        tick.set_fontsize(9)
    ax.set_xlabel("$t^\prime/t$", fontdict={'fontsize': 10})
    ax.set_ylabel("$\kappa_{xy}/T(-\\frac{k^2_Bt}{K\hbar})$",
                  fontdict={'fontsize': 10})

    # line 2
    ax = axes[2]
    name = '%s/HCI_H2(1P-1P)_up_1.0_0.656_1.2_1.1_FBFM_THP30(1.0,1.0)_0.08.dat' % directory
    data = np.loadtxt(name)
    ax.plot(data[:, 0], data[:, 1] / 0.08, lw=2, color='red')
    ax.text(0.296,
            +0.20,
            "TFM$^+$",
            fontsize=9,
            color='black',
            ha='center',
            va='center')
    ax.text(0.308,
            +0.00,
            "FM",
            fontsize=9,
            color='black',
            ha='center',
            va='center')
    ax.text(0.317,
            -0.45,
            "TFM$^-$",
            fontsize=9,
            color='black',
            ha='center',
            va='center')
    ax.minorticks_on()
    ax.set_xlim(0.29, 0.32)
    ax.set_xticks(np.linspace(0.29, 0.32, 4))
    # ax.set_ylim(-0.8, 0.6)
    # ax.set_yticks(np.linspace(-0.8, 0.6, 8))
    for tick in ax.get_xticklabels():
        tick.set_fontsize(9)
    for tick in ax.get_yticklabels():
        tick.set_fontsize(9)
    ax.set_xlabel("$t^\prime/t$", fontdict={'fontsize': 10})
    ax.set_ylabel("$\kappa_{xy}/T(-\\frac{k^2_Bt}{K\hbar})$",
                  fontdict={'fontsize': 10})

    pdb.set_trace()
    plt.savefig('%s/ThermalHallWithHopping.pdf' % destination)
    plt.close()
Beispiel #53
0
def compute_irs(irs, t):
    tck = interpolate.splrep(IRS_TENORS, irs, k=3, s=0)
    R = interpolate.splev(t, tck, der=0)
    dR = interpolate.splev(t, tck, der=1)
    r = dR * t + R
    return r, R
def main():

    # constants
    BIGFONT = 22
    MIDFONT = 18
    SMFONT = 16
    FIG_WIDTH = 12.0
    FIG_HEIGHT = 8.0

    varname = 'HFX'
    nbins = 20
    #cases=["ERA5_C2008_add", "ERA5_TY2001_add", "ERA5_WRFROMS_add", "ERA5_WRF_add"]
    #    cases=["ERA5_C2008_dynlim",  "ERA5_TY2001_nolimit",  "ERA5_WRFROMS_add", "ERA5_WRF_add"]
    #cases=["ERA5_C2008", "ERA5_TY2001", "ERA5_WRFROMS", "ERA5_WRF"]
    #cases=[ "ERA5_WRF","ERA5_WRFROMS",   "ERA5_TY2001", "ERA5_C2008_dynlim"]
    cases = ["WRFROMS", "C2008", "TY2001"]
    #line_libs=['ko','ro','bo','go']
    #line_libs=['k.','r.','b.','g.']
    shade_color_lib = ['salmon', 'cyan', 'lime']
    line_lib = ['r', 'b', 'g']
    dot_lib = ['*', '^', 's']
    #line_libs=['b.','g*','r^','k+']
    wrf_root = '/disk/v092.yhuangci/lzhenn/1911-COAWST/'

    i_dom = 2
    strt_time_str = '201809151800'
    end_time_str = '201809160000'
    box_R = 80

    epsilon = 0.333
    rho_air = 1.29

    strt_time_obj = datetime.datetime.strptime(strt_time_str, '%Y%m%d%H%M')
    end_time_obj = datetime.datetime.strptime(end_time_str, '%Y%m%d%H%M')

    fig, ax = plt.subplots()
    fig.subplots_adjust(left=0.08,
                        bottom=0.18,
                        right=0.99,
                        top=0.92,
                        wspace=None,
                        hspace=None)

    for (dot_case, shade_color, line_color,
         case) in zip(dot_lib, shade_color_lib, line_lib, cases):

        # read track data
        tc_info_fn = wrf_root + '/' + case + '/trck.' + case + '.d0' + str(
            i_dom)
        dateparse = lambda x: datetime.datetime.strptime(x, '%Y%m%d%H0000')
        df_tc_info = pd.read_csv(tc_info_fn,
                                 sep='\s+',
                                 parse_dates=True,
                                 index_col='timestamp',
                                 header=0,
                                 date_parser=dateparse)
        df_tc_info = df_tc_info[((df_tc_info.index >= strt_time_obj) &
                                 (df_tc_info.index <= end_time_obj))]

        print(df_tc_info)
        tc_lat = df_tc_info['lat']
        tc_lon = df_tc_info['lon']

        # read raw input
        ds = salem.open_wrf_dataset('/disk/v092.yhuangci/lzhenn/1911-COAWST/' +
                                    case + '/wrfout_d02')
        ds = ds.sel(time=slice(strt_time_obj, end_time_obj))

        var1 = ds[varname]  # heat exch
        var2 = ds['U10']  # momentum exch
        var3 = ds['U10']
        var4 = ds['V10']
        varmask = ds['LANDMASK']
        var1.values = np.where(varmask.values == 1, np.nan, var1.values)
        var2.values = np.where(varmask.values == 1, np.nan, var2.values)
        ws = np.sqrt(var3 * var3 + var4 * var4)
        idx = get_closest_idx(var1.lat, var1.lon, tc_lat.values, tc_lon.values)
        var1_box_comp = box_collect(var1.values, box_R, idx)  # nparray inout
        var2_box_comp = box_collect(var2.values, box_R, idx)  # nparray inout
        ratio = var1_box_comp / var2_box_comp
        ws_box_comp = box_collect(ws.values, box_R, idx)  # nparray inout

        ws_box_comp = ws_box_comp[~np.isnan(ws_box_comp)]
        var1_box_comp = var1_box_comp[~np.isnan(var1_box_comp)]
        # get bins
        bin_means, bin_edges, binnumber = stats.binned_statistic(
            ws_box_comp.flatten(),
            var1_box_comp.flatten(),
            statistic='mean',
            bins=nbins)
        bin_counts, bin_edges, binnumber = stats.binned_statistic(
            ws_box_comp.flatten(),
            var1_box_comp.flatten(),
            statistic='count',
            bins=nbins)

        lh_heat = bin_means * bin_counts * 3600 * 27000 * 27000 / 10e15  # 1e15 J
        x_pos = (bin_edges[0:-1] + bin_edges[1:]) / 2

        tck = interpolate.splrep(x_pos, lh_heat, s=0)
        x_spline = np.linspace(x_pos.min(), x_pos.max(), 300)
        lh_heat_smooth = interpolate.splev(x_spline, tck, der=0)

        # scatter
        #ax.plot(x_pos, lh_heat, linewidth=0.0, color=line_color, marker=dot_case, markersize=8)
        ax.plot(x_spline,
                lh_heat_smooth,
                label=case,
                linewidth=2.,
                color=line_color)
        ax.fill_between(x_spline,
                        0,
                        lh_heat_smooth,
                        alpha=0.2,
                        color=shade_color)
    plt.legend(loc='best', fontsize=SMFONT)
    plt.xlabel('10m WindSpeed (m/s)', fontsize=SMFONT)
    plt.ylabel(varname + ' (10^15 J)', fontsize=SMFONT)
    plt.xticks(fontsize=SMFONT)
    plt.yticks(fontsize=SMFONT)

    plt.title('Accum. ' + varname + ' - 10m WindSpeed', fontsize=BIGFONT)
    fig.set_size_inches(FIG_WIDTH, FIG_HEIGHT)
    fig.savefig('../fig/acc_' + varname + '.png')
Beispiel #55
0
    def TDI_signal(self, tvec):

        ### Getting parameters: TODO: FIX THIS, HARDCODED
        self.z = 1.  #!!!! HARDCODED TODO
        distance = (Cosmology.DL(self.z, 0.0)[0]) * 1.e6  # in pc

        #?| Defone the constants that we are going to be using
        P_SI = LC.pc
        MTSUN_SI = LC.MTsun
        C_SI = LC.clight
        AU_SI = LC.ua
        year = LC.year
        arm = LP.lisaL

        #?| Additional parameters
        #phic = 2.12 ## FIXME FIXME FIXME
        #phic = 1.2
        #phic = 2.12
        phic = 0.0
        Rmin = 6.0  # minimum distance (in M) used to terminate the waveform
        M = self.m1 + self.m2
        Mt = M * MTSUN_SI
        M2 = M * M
        eta = self.m1 * self.m2 / M2

        DLt = distance * P_SI / C_SI  # distance in sec
        if (self.m1 < self.m2):
            tmp = self.m1
            self.m1 = self.m2
            self.m2 = tmp

        tc = self.Tc
        if (self.Tobs < self.Tc):
            tc = self.Tobs

        # The N-th sample where the model is non-zero

        N = (int)(tc / self.dt)
        tm = np.arange(N) * self.dt

        # Coefficients
        fac = eta / (5.0 * Mt)

        p0 = -2.0  # -2 fac^(5/8)
        p10 = -(3715. / 4032. + 55. / 48. * eta
                )  # - (3715/4032 + 55/48 eta) fac^(3/8)
        p15 = 0.375  # (3/8) fac^(1/4)
        p150 = p15 * (4. * np.pi)
        # 4 pi (3/8) fac^(1/4)
        p20 = -1.0
        # - fac^(1/8)
        p200 = p20 * (9275495. / 7225344. + 284875. / 129024. * eta +
                      1855. / 1024. * eta * eta)
        # - (9275495/7225344 + 284875/129024 eta + 1855/1024 eta^2) fac^(1/8)

        beta = (self.chi1 / 12.0) * (
            113.0 * self.m1 * self.m1 / M2 + 75.0 * eta
        ) + (self.chi2 / 12.0) * (113.0 * self.m2 * self.m2 / M2 + 75.0 * eta)
        sigma = (237.0 / 24.0) * eta * self.chi1 * self.chi2

        f10 = 743. / 2688. + 11. / 32. * eta
        f15 = -3. * (4. * np.pi - beta) / 40.
        f20 = 1855099. / 14450688. + 56975. / 258048. * eta + 371. / 2048. * eta * eta - 3. / 64. * sigma

        #Orbital motion
        AU = AU_SI / C_SI
        Phi_LISA = 2.0 * np.pi * tm / year
        R = np.zeros((len(tm), 3))
        R[:, 0] = AU * np.cos(Phi_LISA)
        R[:, 1] = AU * np.sin(Phi_LISA)
        thS = 0.5 * np.pi - self.bet
        phS = self.lam

        n = np.array([
            np.cos(phS) * np.sin(thS),
            np.sin(phS) * np.sin(thS),
            np.cos(thS)
        ])
        nR = np.dot(R, n)

        tmk = tm + nR
        L = arm / C_SI
        tmk2L = tmk - 2.0 * L

        N = len(tvec) - 1
        Mom = np.zeros(N)
        phase = np.zeros(N)
        ampl = np.zeros(N)

        ampl0 = 2.0 * Mt * eta / DLt
        #print "Amplitude:", ampl0, Mt, eta, 1.0/DLt
        tau = fac * (tc - tmk)
        tau = np.clip(tau, 0.0001, 1. * year)
        Mom = (np.power(tau, (-3. / 8.)) + f10 * np.power(tau, (-5. / 8.)) +
               f15 * np.power(tau,
                              (-3. / 4.)) + f20 * np.power(tau,
                                                           (-7. / 8.))) / 8.
        phase_tk = (p0 * np.power(tau, (0.625)) + p10 * np.power(tau,
                                                                 (0.375)) +
                    (p150 - p15 * beta) * np.power(tau, (0.25)) +
                    (p200 + 15.0 * sigma / 32.) * np.power(tau, (0.125))) / eta
        #tau0  = fac*(tc-tm)
        #phase = (p0*tau0**(0.625) + p10*tau0**(0.375) + (p150 - p15*beta)*tau0**(0.25) +
        #            (p200 + 15.0*sigma/32.)*tau0**(0.125))/eta

        tau2L = fac * (tc - tmk2L)
        tau2L = np.clip(tau2L, 0.0001, 1. * year)
        phase_tk2L = (p0 * np.power(tau2L,
                                    (0.625)) + p10 * np.power(tau2L, (0.375)) +
                      (p150 - p15 * beta) * np.power(tau2L, (0.25)) +
                      (p200 + 15.0 * sigma / 32.) * np.power(tau2L,
                                                             (0.125))) / eta
        ampl = ampl0 * np.power(Mom, (2. / 3.))

        # let's find where we need to terminate the waveform
        Mom_max = np.power(7.0, (-1.5))
        Mom_max6 = np.power(6.0, (-1.5))

        if np.max(Mom) < Mom_max:
            Mom_max = 0.98 * Mom
            #Mom_max = Mom_max6
        #i_max = np.argwhere(Mom > Mom_max)[0][0]
        #i_max6 = np.argwhere(Mom > Mom_max6)[0][0]

        taper = 0.5 * (1.0 + np.tanh(57.0 * (np.power(
            (Mom_max), (2. / 3.)) - np.power(Mom, (2. / 3.)))))
        test1 = (np.power((Mom_max), (2. / 3.)))
        test2 = np.power(Mom, (2. / 3.))

        ampl = ampl * taper

        om = Mom / Mt

        # Antenna pattern
        th_d = self.bet + 0.5 * np.pi
        lam_d = self.lam + np.pi

        ### experiment
        Nt = (int)(tc / self.dt)  # + 1
        tm_f = tvec[:Nt]

        tck = interpolate.splrep(tm, phase_tk, s=0)
        ph_tk = interpolate.splev(tm_f, tck, der=0)

        tck = interpolate.splrep(tm, phase_tk2L, s=0)
        ph_tk2L = interpolate.splev(tm_f, tck, der=0)

        tck = interpolate.splrep(tm, ampl, s=0)
        ampl_f = interpolate.splev(tm_f, tck, der=0)

        tck = interpolate.splrep(tm, om, s=0)
        om_f = interpolate.splev(tm_f, tck, der=0)

        ampl = ampl_f
        om = om_f
        del_phi = 0.5 * (ph_tk - ph_tk2L)
        phi_p = 0.5 * (ph_tk + ph_tk2L)

        Phi_LISA = 2.0 * np.pi * tm / year

        #del_phi = 0.5*(phase_tk - phase_tk2L)
        #phi_p = 0.5*(phase_tk + phase_tk2L)

        Om_A = 0.0  # for A and Om = -0.5*np.pi for E
        Om_E = -0.5 * np.pi  # for E

        Fp_A = (1.0/32.0)*( 6.0*np.sin(2.0*th_d) *(3.0*np.sin(Phi_LISA + lam_d + Om_A) - np.sin(3.0*Phi_LISA - lam_d + Om_A) ) \
                    -18.0*np.sqrt(3.0)*np.sin(th_d)*np.sin(th_d)*np.sin(2.0*Phi_LISA+Om_A) - \
                    np.sqrt(3.0)*(1.0+np.cos(th_d)*np.cos(th_d))*(np.sin(4.0*Phi_LISA-2.0*lam_d+Om_A) + 9.0*np.sin(2.0*lam_d+Om_A))  )

        Fc_A = (1.0/16.0)*( np.sqrt(3.0)*np.cos(th_d)*(np.cos(4.0*Phi_LISA - 2.0*lam_d + Om_A)) -\
                    9.0*np.cos(2.0*lam_d + Om_A) + 6.0*np.sin(th_d)*( np.cos(3.0*Phi_LISA-lam_d + Om_A) + \
                    3.0*np.cos(Phi_LISA +lam_d + Om_A) )  )

        Fp_E = (1.0/32.0)*( 6.0*np.sin(2.0*th_d) *(3.0*np.sin(Phi_LISA + lam_d + Om_E) - np.sin(3.0*Phi_LISA - lam_d + Om_E) ) \
                    -18.0*np.sqrt(3.0)*np.sin(th_d)*np.sin(th_d)*np.sin(2.0*Phi_LISA+Om_E) - \
                    np.sqrt(3.0)*(1.0+np.cos(th_d)*np.cos(th_d))*(np.sin(4.0*Phi_LISA-2.0*lam_d+Om_E) + 9.0*np.sin(2.0*lam_d+Om_E))  )

        Fc_E = (1.0/16.0)*( np.sqrt(3.0)*np.cos(th_d)*(np.cos(4.0*Phi_LISA - 2.0*lam_d + Om_E)) -\
                    9.0*np.cos(2.0*lam_d + Om_E) + 6.0*np.sin(th_d)*( np.cos(3.0*Phi_LISA-lam_d + Om_E) + \
                    3.0*np.cos(Phi_LISA +lam_d + Om_E) )  )

        cpsi = np.cos(2.0 * self.psi)
        spsi = np.sin(2.0 * self.psi)
        hp0 = -(1.0 + np.cos(self.incl) *
                np.cos(self.incl)) * ampl  # multiply by sin\Phi
        hc0 = 2.0 * np.cos(self.incl) * ampl

        print('[om] = ', om.shape)
        print('[hp0] = ', hp0.shape)
        print('[Fp_A] = ', Fp_A.shape)
        print('[cpsi] = ', cpsi.shape)
        print('[Fc_A] = ', Fc_A.shape)
        print('[spsi] = ', spsi.shape)
        print('[phi_p] = ', phi_p.shape)


        h_A = - 2.0*L*om*np.sin(del_phi)*( hp0*(Fp_A*cpsi - Fc_A*spsi)*np.cos(phi_p+phic) + \
                                hc0*(Fp_A*spsi + Fc_A*cpsi)*np.sin(phi_p+phic) )

        h_E = - 2.0*L*om*np.sin(del_phi)*( hp0*(Fp_E*cpsi - Fc_E*spsi)*np.cos(phi_p+phic) + \
                                hc0*(Fp_E*spsi + Fc_E*cpsi)*np.sin(phi_p+phic) )

        # Zero-pad
        A = np.pad(h_A, (0, len(tvec) - len(h_A)), 'constant')
        E = np.pad(h_E, (0, len(tvec) - len(h_E)), 'constant')

        return A, E
Beispiel #56
0
import numpy as np
from scipy.interpolate import splrep, splev
import pylab

filtdir = 'VST_response/'

# optical atmospheric transmission
f = open('paranal_atm_transmission.dat', 'r')
atm_wav, atm_t = np.loadtxt(f, unpack=True)
f.close()

atm_wav *= 10.

atm_spline = splrep(atm_wav, atm_t, k=1)

pylab.plot(atm_wav, atm_t, color='k', label='Atmosphere')

bands = ['u', 'g', 'r', 'i']

colors = ['b', 'c', 'g', 'orange', 'r']

for band, color in zip(bands, colors):

    f = open('%s_OmegaCAM.res' % band, 'r')
    old_wav, old_t = np.loadtxt(f, unpack=True)
    f.close()

    f = open(filtdir + '/%s_modified_transmission_data_with_ccd' % band, 'r')
    filt_tab = np.loadtxt(f)
    f.close()
def make_spline(t,
                m,
                e_m,
                knots=None,
                k=1,
                s=None,
                fitflux=0,
                zpt=0,
                tmin=None,
                tmax=None,
                task=-1,
                anchor_dist=[5.0, 5.0],
                slopes=[None, None]):
    '''A wrapper around splrep that makes sure the independent variable is
   monotonic and non-repeating.  Required arguments:  time (t), magnitudes
   (m) and errors (e_m).  If knots are specified, use them (if task==-1), otherwise,
   they are computed from -10 days to 100 days in 10-day increments.  k is the 
   spline order (default 1) and s is the smoothing factor, as per splrep.  If fitflux
   is nonzero, convert magnitudes to flux (using provided zpt).  tmin and tmax should
   be set to the limits of the spline.'''
    # first, we make sure that t is monotonically increasing with no repeated
    # elements
    sids = num.argsort(t)
    tt = t[sids]  #num.take(t, sids)
    mm = m[sids]  # num.take(m, sids)
    ee_m = e_m[sids]  #num.take(e_m, sids)

    if tmin is None:
        tmin = t.min()
    if tmax is None:
        tmax = t.max()

    # here's some Numeric magic.  first, find where we have repeating x-values
    Nmatrix = num.equal(tt[:, num.newaxis], tt[num.newaxis, :])
    #val_matrix = mm[:,num.newaxis]*num.ones((len(mm),len(mm)))
    #e_matrix = ee_m[:,num.newaxis]*num.ones((len(mm),len(mm)))
    val_matrix = mm[:, num.newaxis] * Nmatrix
    e_matrix = ee_m[:, num.newaxis] * Nmatrix

    average = sum(val_matrix) / sum(Nmatrix)
    e_average = sum(e_matrix) / sum(Nmatrix)

    # at this point, average is the original data, but with repeating data points
    # replaced with their average.  Now, we just pick out the unique x's and
    # the first of any repeating points:
    gids = num.concatenate([[1], num.greater(tt[1:] - tt[:-1], 0.)])
    tt = num.compress(gids, tt)
    mm = num.compress(gids, average)
    ee_m = num.compress(gids, e_average)

    # Now get rid of any data that's outside [tmin,tmax]
    gids = num.less_equal(tt, tmax) * num.greater_equal(tt, tmin)
    #tt = num.compress(gids,tt)
    tt = tt[gids]
    #mm = num.compress(gids,mm)
    mm = mm[gids]
    #ee_m = num.compress(gids,ee_m)
    ee_m = ee_m[gids]
    ee_m = num.where(num.less(ee_m, 0.001), 0.001, ee_m)

    # Next, add some anchors to the data to control the slopes
    if anchor_dist[0] > 0 and task != -1:
        if slopes[0] is not None:
            mm0 = mm[0] - slopes[0] * anchor_dist[0]
        else:
            mm0 = mm[0] - (mm[1] - mm[0]) / (tt[1] - tt[0]) * anchor_dist[0]
        tt = num.concatenate([[tt[0] - anchor_dist[0]], tt])
        mm = num.concatenate([[mm0], mm])
        ee_m = num.concatenate([[ee_m[0]], ee_m])
    if anchor_dist[1] > 0:
        if slopes[1] is not None:
            mm1 = mm[-1] + slopes[1] * anchor_dist[1]
        else:
            mm1 = mm[-1] + (mm[-1] - mm[-2]) / (tt[-1] -
                                                tt[-2]) * anchor_dist[1]
        tt = num.concatenate([tt, [tt[-1] + anchor_dist[1]]])
        mm = num.concatenate([mm, [mm1]])
        ee_m = num.concatenate([ee_m, [ee_m[-1]]])

    # Now convert to flux if requested:
    if fitflux:
        mm = num.power(10, -0.4 * (mm - zpt))
        ee_m = mm * ee_m / 1.087

    if knots is None and task == -1:
        # Use the minimal number
        knots = tmin + num.arange(2 * k + 3) * (tmax - tmin) / (2 * k + 2)

    # Okay, now make the spline representation
    tck, fp, ier, msg = splrep(tt,
                               mm,
                               1.0 / ee_m,
                               k=k,
                               s=s,
                               t=knots,
                               task=task,
                               full_output=1)
    return (tck, fp, ier, msg)
Beispiel #58
0
def inputData(x, y, x_new, derivate_order=0, k=3):
    tck = interpolate.splrep(x, y, s=derivate_order, k=k)
    y_new = interpolate.splev(x_new, tck, der=0)
    return y_new
# Losses of prestressing due to friction
lssFrict = tendon.getLossFriction(coefFric=mu,
                                  k=k,
                                  sigmaP0_extr1=sigmap0max,
                                  sigmaP0_extr2=0.0)
# Losses of prestressing due to anchorage slip (loss due to friction must be
# previously calculated
lssAnch = tendon.getLossAnchor(Ep=Ep,
                               anc_slip_extr1=deltaL,
                               anc_slip_extr2=0.0)
stressAfterLossAnch = tendon.stressAfterLossFriction - lssAnch
'''
#Plot
fig1,ax2d=tendon.plot2D(XaxisValues='X',resultsToPlot=[[stressAfterLossAnch,'r-','Stress after loss due to anchorage slip']])
fig1.show()
'''

areaSigmFric = interpolate.splint(0, tendon.fineScoord[-1], tendon.tckLossFric)
tckLA = interpolate.splrep(tendon.fineScoord, stressAfterLossAnch, k=3)
areaSigmAnc = interpolate.splint(0, tendon.fineScoord[-1], tckLA)

ratio = (areaSigmFric - areaSigmAnc) - Ep * deltaL

import os
from misc_utils import log_messages as lmsg
fname = os.path.basename(__file__)
if (abs(ratio) < 1e-6):
    print('test ' + fname + ': ok.')
else:
    lmsg.error(fname + ' ERROR.')
import ndinterp
from scipy.interpolate import splrep


Ngal = 1000 # sample size

nv200 = 5
nvorb = 5

v200sigma_rat_grid = np.linspace(0.2, 1., nv200)
vorbv200_rat_grid = np.linspace(0.5, 1.5, nvorb)

vdisp_b_grid = np.zeros((nv200, nvorb))
vdisp_a_grid = np.zeros((nv200, nvorb))

v200sigma_rat_spline = splrep(v200sigma_rat_grid, np.arange(nv200))
vorbv200_rat_spline = splrep(vorbv200_rat_grid, np.arange(nvorb))

axes = {0: v200sigma_rat_spline, 1: vorbv200_rat_spline}

z_0 = 2.
ximin = 0.03

modelname = 'mstar'
imf_coeff = (0.21, 0.26, 0.)

vdisp_coeff = (2.34 + 0.20*(np.log10(3.) - np.log10(1.2)), 0.18)

lmhalos = shmrs.generate_halos(100000, z=2., lmhmax=13.5)
lmstars = shmrs.generate_mstar(lmhalos, z=2., scat=0.18)
selection = lmstars > 10.3