import numpy as np

def running_meaner(t,x,m):
  # makes running_mean values of dataset _x_ and timearray _t_ with a running_mean of _m_ values
  # does only work for EQUALLY SPACED dataset WITHOUT DATAGAPS
  # known bugs: If a "NaN" is in the designated interval, running mean will be "NaN" as well...
  nt=t[m/2:-(m/2)]
  nx=np.zeros(len(nt)) 
  l=len(nt) 
  for q in range(m):
    nx+=x[q:][:l] 
  nx/=m
  return nt,nx


### example
import matplotlib.pyplot as plt
t=np.linspace(0,20,300)
x=np.linspace(0,20,300)
x=np.sin(t)
m=10
#print t,x
plt.plot(t,x,'+')
nt,nx=running_meaner(t,x,m)
#print nt,nx
plt.plot(nt,nx,'o')
plt.show()
### example

"""### example 2
from datetime import datetime
from datetime import timedelta
import matplotlib.pyplot as plt
data_path="/home/pacifix/kuehl/work/analysis/solar_event/80_onset_times/10_hesperia_pene/merged_15min/data/"
event=2
data=np.loadtxt("%sfermi_klk_#%03d.dat"%(data_path,event))
pene_y,pene_mon,pene_day,pene_hour,pene_min=data[:,0],data[:,1],data[:,2],data[:,3],data[:,4]
t=[(datetime(int(pene_y[date]),int(pene_mon[date]),1,0)+timedelta(int(pene_day[date])-1,hours=int(pene_hour[date]),minutes=int(pene_min[date]))) for date in range(len(pene_y))]
x=data[:,5:]
x=x.reshape(len(x),)
plt.plot_date(t,x,'k-',lw=2)
nt,nx=running_meaner(t,x,4)
plt.plot_date(nt,nx,'r-',lw=2)
plt.show()
### example"""


