I have this wind data set that consists of wind speed in m/s and I would like to to count periods of nonzero data in the time series.
Each period of nonzero data would count as one "weather event".
I would also like to know where those events are located within the data set (i.e., the indices).
One way to do this is to count the first 0 before each group of non-zero data in the series to determine the number of events and then adding each index value by one to get the location of the events.
# create mock data.
d=np.zeros([209])
d1=np.random.randn(189)
d2=np.zeros(9)
d3=np.random.randn(281)
d4=np.zeros(27)
d5=np.random.randn(21)
d6=np.zeros(155)
d7=np.random.randn(58)
mock_data=np.concatenate((d,d1,d2,d3,d4,d5,d6,d7),axis=0)
indices=np.squeeze(np.array(np.where(mock_data!=0))) # Returns the position vector of every non-zero in the record.
# create a vector to store the positions of the previous zero before each SAW event.
dummy=np.zeros(0)
for i in range(len(indices)):
dummy=np.append(dummy,indices[i]-1)
dummy=dummy.astype(int)
dummy=np.int64(dummy)
zerovalue=np.squeeze(np.array(np.where(mock_data[dummy]==0))) # Should return the position of the previous zero before each SAW event.
# Add 1 to each value in zerovalue
new_index=np.zeros(0)
for i in range(len(zerovalue)):
new_index=np.append(date_index,zerovalue[i]+1)
However, I run into the problem of np.where() not returning the indices I'm expecting. Instead of it returning the indices that indicate where the first value of the group of non-zero data is, it's returning seemingly random indices.
For example, the first index should be 209, but I'm getting 0. Any help is greatly appreciated.