python - Creating half hour averages of a list -
i have calculate, 0 upcrossing period, averaged on half hour, dataset.
the time measurement utc-timestamp (# of seconds since 01.0.1970). every second there 2-3 measurements. measurements each day stored in own file.
the data acquisition unfortunately didn't start @ same time. started 6s after midnight, 10s , on. leads irregular numbers of measurements in during specific time periods. (there not same number of measurements in e.g. half hour)
so have load data file, somehow find measurements in interval of 30min (1800s). find 0 upcrossings (the values go <0 >0) , find corresponding time steps calculate period of upcrossings , calculate average of those. has iterate 48 times.
so far have made 2 attempts.
1
def get_t_z(filename): #zero uprcossing period heave=load_data_2(filename,2) timestamp=load_data_2(filename,0) zero_up_ind=[] # index of 0 upcrossings zero_up=[] #zero_upcrossing periods each half hour i=0 initial_time=0 day_len=int(timestamp[len(timestamp)-1]-timestamp[0]) count=0 while count<48: period=[] zero_up_ind=[] #stores sum of periods each half hour while timestamp[i]-timestamp[initial_time]<int(day_len/48): if heave[i]<0 , heave[i+1]>0: zero_up_ind.append(i) c in range(0,len(zero_up_ind)-1): periods.append(timestamp[zero_up_ind[c+1]]-timestamp[zero_up_ind[c]]) zero_up.append(mean(periods)) count+=1 return zero_up
2
def get_t_z_3(filename): heave=load_data_2(filename,2) #loads heave data file timestamp=load_data_2(filename,0) #loads timestamp file day_len=int(timestamp[-1]-timestamp[0]) #lenght of day (number of rows) t_init=0 #initial time i=0 half_hour_time=[] zero_up_p=[] print(heave) print(timestamp) #the half-hour averages of zero-ucrossing periods stored here count in range(1,49): zero_up_t=[] #the timestamps of upcrossings stored here period_sum=0 #stores sum periods if timestamp[i]<timestamp[t_init]+count*int(day_len/48): i+=1 elif timestamp[i]==timestamp[t_init]+count*int(day_len/48): half_hour_time=timestamp[t_init:i] half_hour_heave=heave[t_init:i] t_init=i print(half_hour_time) print(half_hour_heave) k in range(0,len(half_hour_time)): if half_hour_heave[k]<=0 , half_hour_heave[k+1]>0: zero_up_t.append(timestamp[k]) print(zero_up_t) c in range(0,len(zero_up_t)-1): period_sum+=zero_up_t[c+1]-zero_up_t[c] zero_up_p.append(period_sum/len(zero_up_t)) print(period_sum) return zero_up_p
when execute first code nothing happens. continues run nothing happens. tried modified version of code , time later laptop crashed. maybe there infinite loop or something? second code returns zero_up_p
empty list. have feeling doesn't start for
loop.
edit1: added incrementation of , changed way periods averaged. first code seems working now.
this code seems working now:
def get_t_z(filename): #zero uprcossing period heave=load_data_2(filename,2) timestamp=load_data_2(filename,0) # index of 0 upcrossings zero_up=[] #zero_upcrossing periods each half hour i=0 initial_time=0 day_len=int(timestamp[len(timestamp)-1]-timestamp[0]) count=0 while count<48: periods=[] zero_up_ind=[] #stores sum of periods each half hour while timestamp[i]-timestamp[initial_time]<int(day_len/48) , i<len(timestamp): if heave[i]<0 , heave[i+1]>0: zero_up_ind.append(i) i+=1 c in range(0,len(zero_up_ind)-1): periods.append(timestamp[zero_up_ind[c+1]]-timestamp[zero_up_ind[c]]) zero_up.append(mean(periods)) count+=1 initial_time=i return zero_up
Comments
Post a Comment