def GxETrain(x,mu,sigma, A, tau):
    #This model convolves a pulsetrain of length 20 with a broadening function defined over several P's (lf*P)
    #It extracts one of the last convolved profiles, subtracts the climbed baseline and then adds noise to it
    bins, profile = psr.makeprofile(nbins = P, ncomps = 1, amps = A, means = mu, sigmas = sigma)
    binstau = np.linspace(1,P,P)  #Tested: having a longer exp here makes no difference
    scat = psr.psrscatter(psr.broadfunc(binstau,tau),psr.pulsetrain(3, bins, profile))   
    climb, observed_nonoise, rec, flux = psr.extractpulse(scat, 2, P)
    return observed_nonoise
def GxETrain(x,mu,sigma, A, tau):
    #This model convolves a pulsetrain with a broadening function
    #It extracts one of the last convolved profiles, subtracts the climbed baseline and then adds noise to it
    bins, profile = psr.makeprofile(nbins = P, ncomps = 1, amps = A, means = mu, sigmas = sigma)
    binstau = np.linspace(1,P,P)
    scat = psr.psrscatter(psr.broadfunc(binstau,tau),psr.pulsetrain(3, bins, profile))   
#    plt.figure()
#    plt.plot(scat,'r')    
    climb, observed_nonoise, rec, flux = psr.extractpulse(scat, 2, P)
    return observed_nonoise
observednonoise = []
observedflux = []
observedfluxfixed = []
scatt = []
#scintt = []

results = []

xsec = np.linspace(0,pulseperiod,P)



#np.savetxt('Pythondata/photonsfreq.txt',photonsfreq)

for i in range(len(nurange)):
    scat = psr.psrscatter(probfreq[i],psr.pulsetrain(3, bins, profile_intr_norm[i]))
    scatt.append(scat)      
#    climb, observed_nonoise, rec, flux = psr.extractpulse(scat, 2, P)
    climb, observed_nonoise, rec, flux = psr.extractpulse(scat, 0, P)
    observedflux.append(flux)
    observednonoise.append(observed_nonoise)

#sys.exit()
# the flux as produced by psr.extractpulse is normalised such that the lowest frequency has flux 1
# Profiles:
plt.figure()
for i in range(len(nurange)):    
    observedfluxfix = observednonoise[i]*fluxtr[i]
#    plt.figure()    
##    plt.plot(xsec,observedfluxfix/np.max(observedfluxfix),'b',linewidth = 4.0)
##    plt.plot(scatt[i]/np.max(scatt[i]),'m',linewidth = 4.0)
observedflux = []
observedfluxfixed = []
scatt = []
#scintt = []


xsec = np.linspace(0,pulseperiod,P)



#np.savetxt('Pythondata/photonsfreq.txt',photonsfreq)

for i in range(len(nurange)):
#    scat = psr.psrscatter(probfreq[i],psr.pulsetrain(3, bins, profile_intr_norm[i]))
#    scat = psr.psrscatter(photonsinterpp[i],epn_norm[i])
    scat = psr.psrscatter(probfreq[i],epn_norm_interpp[i])
    scatt.append(scat)      
#    climb, observed_nonoise, rec, flux = psr.extractpulse(scat, 2, P)
    climb, observed_nonoise, rec, flux = psr.extractpulse(scat, 0, P)
#    observedflux.append(flux)
    observednonoise.append(observed_nonoise)
    
np.savetxt("Pythondata/EPN123725/Profiles_120_220.txt",observednonoise)    

#sys.exit()
# the flux as produced by psr.extractpulse is normalised such that the lowest frequency has flux 1
# Profiles:
#plt.figure()
for i in range(len(nurange)):    
#    observedfluxfix = observednonoise[i]*fluxtr[i]
    plt.figure()    
#    obtainedtau2=[]
#    obtainedtaustd2=[]    
    obtainedchecktau = []
    obtainedchecktaustd = []
    
    #        plt.figure(1)
        
    for k in range(0,len(taubins)):
        trainlength = int(10*taubins[k]/P)
        if trainlength < 5:
            trainlength = 5
        print ''
        print '%.3f GHz' % nurange[k]
        print 'trainlength: '+str(trainlength)   
#        scat = psr.psrscatter(psr.broadfunc(xaxlong,taubins[k]),psr.pulsetrain(trainlength, bins, profile_intr_norm[k]))
        scat = psr.psrscatter(psr.broadfunc2(xax,taubins[k],taubins2[k]),psr.pulsetrain(3, bins, profile_intr_norm[k]))        
        scatt.append(scat)        
        climb, observed_nonoise, rec, flux = psr.extractpulse(scat, 2, P)
        
        peak = np.max(observed_nonoise)
        noise = np.random.normal(0,peak/snr,P)
        observedadd = climb + noise
        checkfloor = observed_nonoise + noise
        observed.append(observedadd)
        
        observedflux.append(flux)
        observednonoise.append(observed_nonoise)
        checkfloors.append(checkfloor)
        
        ## Method 1: Create a median filter around the absolute minimum, to find minimum that represents the underlying noiseless minimum well.    
#        idmin = np.argmin(observedadd)