0

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
989
  • 12,579
  • 5
  • 31
  • 53
Liz0888
  • 1
  • 1
  • 2
    give small representative data example PLEASE also please make your text short! eat time you can give an example so that a person who does not know your problem follows your question –  Jul 09 '16 at 11:00
  • 3
    Please read [how to provide minimal reproducible examples in R](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#answer-5963610). Then edit & improve it accordingly. A good post usually provides *minimal* input data, the desired output data & some code tries - all *copy-paste-run'able*. It also uses proper SO-formattings. The poster should throw off any ballast. Abstract from your problem if possible, minimize your ex. data set & code while retaining full reproducibility, focus on one problem at a time and make sure there's a question that stands out. – lukeA Jul 09 '16 at 11:21

1 Answers1

0

I have used a much simpler approach to break rainfall data into events based on a Minimum Inter-Event Time (MIT) of 30 minutes. Here is my code:

# rainfall data contained in dataframe called RainData, of two columns: column 1 is time stamp in POSIXct format, column 2 is rainfall depth. My data is collected at two minute intervals. 

Rain_Over_0<- RainData[RainData[,2]!=0,]  

# Create vector increasing by 1 as Diff=>30 (Time specific) # change value of Diff here to change the MIT.

Rainindex<-c(0,cumsum(diff(Rain_Over_0[,1])>30)) # input your value of MIT (in minutes) where the code says 30.

# Split into list of events

RainEvents<-split(Rain_Over_0, Rainindex) # this returns a list of events. You can then use sapply functions to determine the rain statistics you need. 

Hope that helps.