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
def GxESingleFold(x,mu,sigma,A,tau,trainlength):
    #This model takes a single Guassian pulse with mean mu and sigma
    #Convolves it with a broadening function
    #It extracts one of the last convolved profiles subtracts the climbed baseline and then adds noise to it 
    observed_postfold = np.zeros(P)      
    bins, profile = psr.makeprofile(nbins = P, ncomps = 1, amps = A, means = mu, sigmas = sigma)
    binstau = np.linspace(1,trainlength*P,trainlength*P)
    scat = psr.psrscatterpostfold(psr.broadfunc(binstau,tau),psr.pulsetrain(1, bins, profile))
    climb, observed_nonoise, rec, flux = psr.extractpulse(scat, 0, trainlength*P)
    for i in range(trainlength*P):    
        observed_postfold[np.mod(i,P)] += observed_nonoise[i]         
        GxESingleFold = observed_postfold[x]-np.min(observed_postfold[0:P])
    return GxESingleFold     
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)
#    plt.plot(observednonoise[i]/np.max(observednonoise[i]),'g',linewidth = 4.0)
#    plt.xlabel("time(sec)",fontsize=16)
#    plt.ylabel("normalized intensity",fontsize=16)