Anda di halaman 1dari 19

Scope

Procedure
Useful Tips

Introduction to R programming language


Tutorial: Hydrological Modelllig & R

Claudia Vitolo1
1 Department of Civil and Environmental Engineering
Imperial College London

Civil Lunches, 11.10.2012

C.Vitolo R intro
Scope
Procedure
Useful Tips

Outline

1 Scope
What do you want to do?
What do you need?

2 Procedure
Data
Model
Results

3 Useful Tips

C.Vitolo R intro
Scope
What do you want to do?
Procedure
What do you need?
Useful Tips

Outline

1 Scope
What do you want to do?
What do you need?

2 Procedure
Data
Model
Results

3 Useful Tips

C.Vitolo R intro
Scope
What do you want to do?
Procedure
What do you need?
Useful Tips

Scope

Import available data for a given catchment


Use TOPMODEL to simulate streamflow discharge
Visualize results

C.Vitolo R intro
Scope
What do you want to do?
Procedure
What do you need?
Useful Tips

Scope

Import available data for a given catchment


Use TOPMODEL to simulate streamflow discharge
Visualize results

C.Vitolo R intro
Scope
What do you want to do?
Procedure
What do you need?
Useful Tips

Scope

Import available data for a given catchment


Use TOPMODEL to simulate streamflow discharge
Visualize results

C.Vitolo R intro
Scope
What do you want to do?
Procedure
What do you need?
Useful Tips

Outline

1 Scope
What do you want to do?
What do you need?

2 Procedure
Data
Model
Results

3 Useful Tips

C.Vitolo R intro
Scope
What do you want to do?
Procedure
What do you need?
Useful Tips

What do you need?

1 topografic index classes


2 delay function
3 precipitation time series
4 evapotranspiration time series
5 parameter set

How to produce the above information is beyond the scope of this


tutorial.

Let’s assume 1-4 are tables saved in text files, while parameters (5)
will be entered manually.

C.Vitolo R intro
Scope Data
Procedure Model
Useful Tips Results

Outline

1 Scope
What do you want to do?
What do you need?

2 Procedure
Data
Model
Results

3 Useful Tips

C.Vitolo R intro
Scope Data
Procedure Model
Useful Tips Results

Your data

Let’s say you have a text file (timeseries.csv) containing your time
series. . .

C.Vitolo R intro
Scope Data
Procedure Model
Useful Tips Results

Load data in R

R console
DATA <- read.csv("~/timeseries.csv",
header=TRUE,
sep=",")

Use the same command to read the tables containing delay function
and topographic index classes.

R console
parameters <- c("qs0"=,"lnTe"=,"m"=,"Sr0"=,"SrMax"=,
"td"=,"vch"=1000,"vr"=,"k0"=,"CD"=,"dt"=0.25)

C.Vitolo R intro
Scope Data
Procedure Model
Useful Tips Results

Plot data

R console
plot(DATA$rain,type="l",col="grey")

C.Vitolo R intro
Scope Data
Procedure Model
Useful Tips Results

Outline

1 Scope
What do you want to do?
What do you need?

2 Procedure
Data
Model
Results

3 Useful Tips

C.Vitolo R intro
Scope Data
Procedure Model
Useful Tips Results

Models are functions.


Thousands of functions have been developed for R but only the
most commonly used are built-in. The others are collected in
’packages’.
There are many packages tailored for hydrological analysis, one
of them is TOPMODEL which is an implementation of the model
developed by Beven and Kirkby (1979).

R console
install.packages("topmodel")
library(topmodel)
Qsim <- topmodel(parameters, topidx, delay,
DATA$rain, DATA$ET0)

C.Vitolo R intro
Scope Data
Procedure Model
Useful Tips Results

Outline

1 Scope
What do you want to do?
What do you need?

2 Procedure
Data
Model
Results

3 Useful Tips

C.Vitolo R intro
Scope Data
Procedure Model
Useful Tips Results

Compare simulation and observation


R console
plot(DATA$Qobs,type="l",main="Comparison",
xlab="Time",ylab="Discharge")
lines(Qsim, col="red")
legend("topright", legend=c("Observed","Simulated"),
lty=c(1,1), col=c("black","red"))

C.Vitolo R intro
Scope Data
Procedure Model
Useful Tips Results

Export results to text file

R console
write.csv(Qsim,
file="~/Qsim.csv",
row.names = FALSE)

C.Vitolo R intro
Scope
Procedure
Useful Tips

Useful Tips

Vectorization:
If your object ’parameters’ is a matrix rather than a vector, R will
run topmodel function as many times as the rows in the matrix.
Classes & Methods:
If Qobs is passed to the function, then NashSutcliffe efficiency is
automatically calculated.

R console
Qsim_mat <- topmodel(parameters, topidx,
delay, rain ,ET0)
NS <- topmodel(parameters, topidx,
delay, rain ,ET0, Qobs = Qobs)

C.Vitolo R intro
Appendix For Further Reading

For Further Reading I

Crawley, Michael J.
The R Book.
Wiley Publishing, 2007.

C.Vitolo R intro

Anda mungkin juga menyukai