Miscellaneous data import

From SourceWiki
Jump to navigation Jump to search


This example is primarily intended to be used with the topmodel() function. However, it can be useful for other analysis such as baseflow separation.

In the example, all input files are supposed to be (.txt) files. (Each decent spreadsheet converts easily to text files with "save as"). All inputs are given in meters per time unit

Rainfall

In this case we have the rainfall data from a tipping bucket gauge. This means that data are stored as the moments of the tipping of the bucket. The resolution of the gauges is 0.2 mm, so each entry represents 0.2 mm of rainfall. An input file, say rain.txt could look like this:

   24/05/2001 00:52:46
   24/05/2001 01:07:37
   24/05/2001 01:22:51
   24/05/2001 01:27:00
   25/05/2001 01:05:17
   ...
   

The code we use to import these data into R is:

   rain <- read.table("rain.txt")
   rain <- paste(rain[,1],rain[,2])
   rain <- strptime(as.character(rain), format = "%d/%m/%Y %H:%M:%S")
   rain <- as.POSIXct(rain, tz="GMT")

NOTE: be sure to set the time zone correctly. If you are working on data from another time zone it may be advisable to set the environment variable accordingly, e.g. to avoid problems with daylight saving time. You can set the time zone in R with:

   Sys.putenv(TZ="GMT")

Then we transform the data to rainfall per time unit, say, per 15 min:

   rain <- rain+15*60
   rain <- cut(c(trunc(rain[1],"hour"),rain), "15 min")
   rain <- tapply(rep(0.2,length(rain)),rain,"sum")
   rain[1] <- rain[1]-0.2

Note the first line of this code block: we shift all the entries 15 min. to the future. This makes sure that the rainfall associated with each time step is the rainfall that occurred over the past time interval (and not the coming interval).

Discharge

Discharge is easier to import and does not require much preprocessing. The same goes for rainfall which is already a time series. For instance, if the input file is:

   04/09/2001 15:30 0.0492
   04/09/2001 16:00 0.0495
   04/09/2001 16:30 0.0503
   04/09/2001 17:00 0.0504
   04/09/2001 17:30 0.0500
   ...

Import these data:

   input <- read.table("discharge.txt")
   time <- paste(input[,1],input[,2])
   time <- strptime(as.character(time), format = "%d/%m/%Y %H:%M")
   time <- as.POSIXct(time, tz="GMT")
   level <- input[,3]
   discharge <- data.frame(time,level)
   rm(input,time,level)

Strictly, we do not need the time information in the discharge and rain objects, but it is handy to have them around, for instance to check that both series are synchronous.