예제 #1
0
파일: splreg.py 프로젝트: viveikjha/PyCS
def splreg(jds, mags, magerrs,knotstep=20.0, n=None, stab=True,stabext=300.0, stabgap=20.0, stabstep=5.0,
		stabmagerr=-2.0, stabrampsize=0, stabrampfact=1.0, bokit=1, bokeps=2.0, boktests=5,
		bokwindow=None, k=3, verbose=True):
		
	"""
	Give me datapoints x y, with errorbars on y
	I return a function : you pass an array of new x, the func returns (newy, newyerr)
	
	I return a spline as well, so I can use it for magshifts later 
	performed with a BOK spline fitting on the datas and the errors.
	The errors on y take into account the seasons in y 
	
	"""
	
	l     = lc.factory(jds,mags,np.zeros(len(jds))+1e-5)
	l_err = lc.factory(jds,magerrs,np.zeros(len(jds))+1e-5)
	
	spl_mag = spl.fit([l],knotstep=knotstep, n=n, stab=stab, stabext=stabext, stabgap=stabgap, stabstep=stabstep,
				stabmagerr=stabmagerr, stabrampsize=stabrampsize, stabrampfact=stabrampfact, bokit=bokit,
				bokeps=bokeps,boktests=boktests,bokwindow=bokwindow, k=k, verbose=verbose) 
				
	spl_magerr = spl.fit([l_err],knotstep=knotstep, n=n, stab=stab, stabext=stabext, stabgap=stabgap, stabstep=stabstep,
				stabmagerr=stabmagerr, stabrampsize=stabrampsize, stabrampfact=stabrampfact, bokit=bokit,
				bokeps=bokeps,boktests=boktests,bokwindow=bokwindow, k=k, verbose=verbose)
	
	interseasons = sea.autofactory(l,seasongap=30, tpe='interseasons')
	
	
	def normaldistrib(x, mu, sig):
    		return np.exp(-np.power(x - mu, 2.) / (2 * np.power(sig, 2.)))
		
	def errorinterseason(minintsea,maxintsea,rsjd):
		
		# we modelise the error enveloppe as a gaussian curve, with sigma adapted to the length of interseason
	
		normrsjd = (rsjd-minintsea+1e-5)/(maxintsea-minintsea)		
		return normaldistrib(normrsjd,0.5,0.2)**1.5		      
	        
	def outfct(rsjds):
				
		newy = spl_mag.eval(rsjds) 
		newyerr = spl_magerr.eval(rsjds)
		
		#let's modify the error enveloppe for the rsjds between seasons:
		
		for interseason in interseasons:
			for ind,rsjd in enumerate(rsjds):
				
				#the height of the enveloppe will be related to the length of the interseason
				ndays = interseason[1]-interseason[0]
				multfact = ndays/8.0  #arbitrary factor, may be changed...
				
				if rsjd < interseason[1] and rsjd > interseason[0]:
					newyerr[ind] += newyerr[ind]*errorinterseason(interseason[0],interseason[1],rsjd)*multfact
		
		return (newy, newyerr)	
	
	return outfct, spl_mag
예제 #2
0
    def save(self):
        """
		Save the data in a dictionnary of lightcurve define by the shifting part of the program
		So we can directly take this pickle to test the main program

		@todo: maybe give different name for archive
		"""

        dict = {}
        for curve in self.curves:
            dict[curve.name] = lc.factory(
                curve.datatime, curve.datamag, magerrs=curve.dataerr, telescopename="simulation", object=curve.name
            )

        writepickle(dict, "lcs.pkl")
예제 #3
0
    def save(self):
        """
		Save the data in a dictionnary of lightcurve define by the shifting part of the program
		So we can directly take this pickle to test the main program

		@todo: maybe give different name for archive
		"""

        dict = {}
        for curve in self.curves:
            dict[curve.name] = lc.factory(curve.datatime,
                                          curve.datamag,
                                          magerrs=curve.dataerr,
                                          telescopename="simulation",
                                          object=curve.name)

        writepickle(dict, 'lcs.pkl')
예제 #4
0
def flexibleimport(filepath, jdcol=1, magcol=2, errcol=3, startline=8,
                   flagcol=None, propertycols=None,
                   telescopename="Unknown", object="Unknown",
                   plotcolour="red", verbose = True, units='ABmags'):
    """
    The general form of file reading. We read only one lightcurve
    object. To comment a line in the input file, start the line with
    '#'' like in python.

    :param jdcol: The column number containing the MHJDs. First column is number 1, not 0 !
    :param magcol: The column number containing the magnitudes.
    :param errcol: The column number containing the magnitude errorbars.
    :param flagcol: (default : None) The colum containing a mask (if available). This one should contain False (= will be masked) or True (not masked).

    :param propertycols: (default : None) is a dict : ``propertycols = {"fwhm":7, "skylevel":8}`` means that col 7 should be read in as property "fwhm" and 8 as "skylevel".
    :type propertycols: dictionary
    """

    if verbose : print "Reading \"%s\"..." % (os.path.basename(filepath))
    datafile = open(filepath, "r")
    filelines = datafile.readlines()[startline-1:] # we directly "skip" the first lines of eventual headers ...
    datafile.close()
    jds = []
    mags = []
    magerrs = []
    flags = []
    properties = []
    # Check all the lines for headers, empty lines, and consistent numbers of columns:

    elsperline = None
    for i, line in enumerate(filelines) :

        # Check for header lines:
        if line[0] == "#":
            print "Skipping header line %i : %s" % (i+startline, repr(line))
            continue

        # Check for "empty" lines - this does not seem to work correctly...
        if len(line.split()) < 5:
            print "Skipping empty line %i : %s" % (i+startline, repr(line))
            continue

        # Check the consistency of the number of columns:
        elements = line.split() # line is a string, elements is a list of strings

        if elsperline is not None:
            if len(elements) is not elsperline:
                raise RuntimeError, "Parsing error in line %i, check columns : \n%s" % (i+startline, repr(line))

        elsperline = len(elements)

        # Now, extend data arrays using elements of this row:
        jds.append(float(elements[jdcol-1]))
        x,xerr = float(elements[magcol-1]), float(elements[errcol-1])
        if units=='ABmags':
            mags.append(x)
            magerrs.append(xerr)
        elif units=='nmgy':
            m,merr = flux2magnitude(x,xerr)
            mags.append(m)
            magerrs.append(merr)
        else:
            raise RuntimeError, "Unknown units '%s'\n" % (units)

        # Also append flag array, as required:
        if flagcol is not None :
            strflag = str(elements[flagcol-1])
            if strflag == "True":
                flags.append(True)
            elif strflag == "False":
                flags.append(False)
            else:
                print "Flag error in line %i : %s" % (i+startline, repr(line))
        else:
            flags.append(True)

        propdict = {} # an empty dict for now
        if propertycols is not None :
            for (propname, propcol) in propertycols.items():
                propdict[propname] = str(elements[propcol-1])
        properties.append(propdict)

    #Call the factory function from pycs
    pycs.gen.lc.factory(jds, mags)

    # Make a brand new lightcurve object:
    newlc = factory(np.array(jds), np.array(mags), magerrs=np.array(magerrs), telescopename=telescopename, object=object)
    newlc.properties = properties[:]
    newlc.mask = np.array(flags[:])
    newlc.plotcolour = plotcolour
    nbmask = np.sum(newlc.mask==False)
    commentcols = "(%i, %i, %i)" % (jdcol, magcol, errcol)
    newlc.commentlist.insert(0, "Imported from %s, columns %s" % (os.path.basename(filepath), commentcols))
    if verbose: print "%s with %i points imported (%i of them masked)." % (str(newlc), len(newlc.jds), nbmask)
    return newlc