I have hourly rainfall data which I have split by year into matrices. The matrices are in a list called tenyr.matrix.list
. I want to divide the rainfall data up into events, based on an inter-event period of 6 hours.
The code below is my attempt. I have used a mask to identify the next 0 value or next value which is not zero to define the end and start of events. Then I have used a repeat loop to repeat this process until there is a 6-hour gap between the end of the event and the start of the next event (in case there are events close together which contain a 0 in-between).
This works if there are no gaps within the events, however where there are gaps, the code only prints the end of the event beyond the zero value, and not the preceding values.
e.g. If the event is:
2006/12/12 07.00 5; 2006/12/12 08.00 10; 2006/12/12 09.00 7;
2006/12/12 10.00 3; 2006/12/12 11.00 1.
That is what the code prints.
However if the event is:
2006/12/12 07.00 5, 2006/12/12 08.00 0, 2006/12/12 09.00 7, 2006/12/12
10.00 3, 2006/12/12 11.00 1.
The code prints only the second part beyond the zero: 2006/12/12 09.00 7, 2006/12/12 10.00 3, 2006/12/12 11.00 1.
Can anyone explain what I am missing?
mask <- (tenyr_matrix.list[[j]]) #create a mask matrix
for (x in c(1:68)){
firstnonzero <- which(mask[,2]!=0 & mask[,2]!=9999)[1] #first nonzero
repeat {
nonzero <- which(mask[,2]!=0 & mask[,2]!=9999)[1] #nonzero
mask[1:nonzero,2]=9999 #set values up to nonzero to 9999
zero <- which(mask[,2]==0)[1] #first zero after event
mask[1:zero,2]=9999 #set values up to zero to 9999
nonzero2 <- which(mask[,2]!=0 & mask[,2]!=9999)[1] #find next nonzero
#mask[1:nonzero2,2]=9999 #set values up to zero to 9999
if (((tenyr_matrix.list[[j]][nonzero2,1]-tenyr_matrix.list[[j]] [zero-1,1])/(60*60))>=6 | is.na(nonzero2))
print(tenyr_matrix.list[[j]][firstnonzero:(zero-1),])
break
}
} #end of x