0% found this document useful (0 votes)
36 views31 pages

IIO352 CP04b 2023 04 05 hydroTS Knitr

This document introduces the hydroTSM package for analyzing hydrologic and environmental time series data. It discusses functions for generating temporal sequences, extracting values by month, year or season, and aggregating data to different temporal scales. Examples show how to generate daily and hourly sequences, extract values for specific periods, and convert between sub-daily, daily, monthly, seasonal and annual time scales using hydroTSM functions. The package provides tools for common operations involved in working with time series data in hydrology.

Uploaded by

matish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views31 pages

IIO352 CP04b 2023 04 05 hydroTS Knitr

This document introduces the hydroTSM package for analyzing hydrologic and environmental time series data. It discusses functions for generating temporal sequences, extracting values by month, year or season, and aggregating data to different temporal scales. Examples show how to generate daily and hourly sequences, extract values for specific periods, and convert between sub-daily, daily, monthly, seasonal and annual time scales using hydroTSM functions. The package provides tools for common operations involved in working with time series data in hydrology.

Uploaded by

matish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Introducción al manejo de series temporales

Curso: IIO352 (”Hidrologı́a”)

Mauricio Zambrano-Bigiarini, PhD

Fac. de Ingenierı́a y Ciencias


Universidad de La Frontera, Temuco

mauricio.zambrano @ ufrontera.cl

Clase Práctica 3b, 05 de Abril 2023


Tabla de Contenidos

1 Context

2 Temporal sequences

3 Extraction

4 Conversions/aggregation

5 Monthly, Annual and seasonal values

6 Plots

7 References

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 2 / 31


Context

Tabla de Contenidos

1 Context

2 Temporal sequences

3 Extraction

4 Conversions/aggregation

5 Monthly, Annual and seasonal values

6 Plots

7 References

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 3 / 31


Context

In the previous lesson we saw:

How to build temporal series.

How to build, extract, agregate, read and write zoo/xts objects.

How to manipulate zoo/xts objects.

How to plot zoo/xts objects.

Today the following package will be explored: hydroTSM, which has several useful
functions for hydrologic and environmental data analysis.

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 4 / 31


Temporal sequences

Tabla de Contenidos

1 Context

2 Temporal sequences

3 Extraction

4 Conversions/aggregation

5 Monthly, Annual and seasonal values

6 Plots

7 References

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 5 / 31


Temporal sequences

Temporal sequences - I

The main functions used for temporal sequences generation or for calculating the number of
elements of a given sequence are:
hip: generates a sequence of hours between an initial and final hour.

dip: generates a sequence of days between an initial and final date.

mip: generates a sequence of months between an initial and final month.

yip: generates a sequence of years between an initial and final year.

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 6 / 31


Temporal sequences

Temporal sequences - II

All the functions aforementioned have the following structure:


funcion(from, to, date.fmt, out.type)
donde:

from: indicates the start of the temporal sequence.

to: indicates the end of the temporal sequence.

date.fmt: indicates the format in which the data is given from y to. By default,
date.fmt="%Y-%m-%d.

out.type: indicates if the result is a temporal sequence between from y to or the number
of elements between from and to.

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 7 / 31


Temporal sequences

Temporal sequences - III


Examples; generation of a daily sequence between two dates:

Traditional way:
require(zoo)
## Loading required package: zoo
##
## Attaching package: ’zoo’
## The following objects are masked from ’package:base’:
##
## as.Date, as.Date.numeric
dates1 <- seq(from=as.Date("2014-01-01"),
to=as.Date("2014-06-12"), by="days")

With hydroTSM:
require(hydroTSM)
## Loading required package: hydroTSM
## Loading required package: xts
dates2 <- dip(from="2014-01-01", to="2014-06-12")

Computation of the number of days between two dates:


dip(from="2014-01-01", to="2014-06-12",out.type="nmbr")
## [1] 163
dip(from="2014-01-01", to="2014-06-12",,"nmbr")
## [1] 163

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 8 / 31


Temporal sequences

Temporal sequences - IV
Examples; generation of a hourly sequence between two dates:

Traditional way:
require(zoo)
hrs1 <- seq(from=as.POSIXlt("2012-01-01 21:00"),
to=as.POSIXct("2012-01-01 23:00"), by="hour")

Con hydroTSM:
require(hydroTSM)
hrs2 <- hip(from="2012-01-01 21", to="2012-01-01 23")

Computation of the number of hours between two time steps:


hip(from="2012-01-01 21", to="2012-01-01 23", out.type="nmbr")
## [1] 3
hip("2012-01-01 21", "2012-01-01 23",,"nmbr")
## [1] 3

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 9 / 31


Extraction

Tabla de Contenidos

1 Context

2 Temporal sequences

3 Extraction

4 Conversions/aggregation

5 Monthly, Annual and seasonal values

6 Plots

7 References

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 10 / 31


Extraction

Extraction - I

The main function used to extract all the values belonging to a determined month, year or
season is:

extract(x, trgt, ...)

Where:

x: object zoo/xts with the original data.

trgt: integer indicating the number of the month (trgt ≤ 12) or year to be extracted, or
also a character indicating the season to be extracted (e.g., DJF, MAM, JJA, SON, etc).

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 11 / 31


Extraction

Extraction - II
Some examples of values extraction:
Loading the daily rainfall data:
data(SanMartinoPPts)
x <- SanMartinoPPts

All February values:


feb <- extract(x, trgt=2)

All values of 1974:


y1974 <- extract(x, trgt=1974)

All the Fall values in the Northern Hemisphere(SON):


son <- extract(x, trgt="SON")
M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 12 / 31
Conversions/aggregation

Tabla de Contenidos

1 Context

2 Temporal sequences

3 Extraction

4 Conversions/aggregation

5 Monthly, Annual and seasonal values

6 Plots

7 References

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 13 / 31


Conversions/aggregation

Conversions - I

The main functions used to aggregate temporal series are:


1 subdaily2daily: converts series of lower frequency than daily (e.g., hourly, 30-, 15
minutes) to daily.

item daily2monthly: converts series of daily frequency (or lower) to monthly values.

2 daily2annual: converts series of daily frequency (or lower) to annual values.

3 monthly2annual: converts series of monthly frequency (or lower) to annual values.

4 dm2seasonal: converts series of daily/monthly frequency (or lower) to seasonal values.

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 14 / 31


Conversions/aggregation

Conversions - II

The 4 first aforementioned functions have the following structure:


funcion(x, FUN, na.rm = TRUE, ...)
Where:

x: object zoo/xts to be converted.

FUN: function that will be applied to the elements of x to obtain the new object with the
needed frequency.

na.rm: indicates if the missing values in x should be ignored during the conversion.

Moreover, dm2sesonal requires:


season: indicates the season of the year for which the values must be obtained.

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 15 / 31


Conversions/aggregation

Conversions - III
Conversion exaples:
Loading precipitation daily values:
data(SanMartinoPPts)
x <- SanMartinoPPts
Daily to monthly series:
m <- daily2monthly(x, FUN=sum, na.rm=TRUE)

Loading hourly discharge data:


data(KarameaAtGorgeQts)
x <- KarameaAtGorgeQts

Hourly to monthly series:


m <- subdaily2monthly(x, FUN=mean, na.rm=TRUE)
M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 16 / 31
Conversions/aggregation

Conversions - IV
Conversions examples:
Loading daily precipitation data:
data(SanMartinoPPts)
x <- SanMartinoPPts
Daily to monthly series:
a <- daily2annual(x, FUN=sum, na.rm=TRUE)

Loading hourly discharge data:


data(KarameaAtGorgeQts)
q <- KarameaAtGorgeQts

Hourly to seasonal summer series in the Southern Hemisphere (DJF):


DJF <- dm2seasonal(q, FUN=mean, season="DJF")
M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 17 / 31
Monthly, Annual and seasonal values

Tabla de Contenidos

1 Context

2 Temporal sequences

3 Extraction

4 Conversions/aggregation

5 Monthly, Annual and seasonal values

6 Plots

7 References

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 18 / 31


Monthly, Annual and seasonal values

Mensual, Annual and seasonal values

The next functions are used to obtain representative mensual, annual ir seasonal values:
1 monthlyfunction: function that allows to obtain 12 monthly values, applying an additive
function to ALL the values of an zoo/xts object that belongs to a given month
(Jan...Dec).

2 annualfunction: function that allows to obtain annual values, applying an additive


function to ALL the values of an zoo/xts object that belongs to a given year.

3 seasonalfunction: function that allows to obtain 4 seasonal values, applying an additive


function to ALL the values of an zoo/xts object that belongs to a given season (DJF,
MAM, JJA, SON, ...).

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 19 / 31


Monthly, Annual and seasonal values

Mensual, annual and seasonal values - II

All the aforementioned functions have the following structure:


funcion(x, FUN, na.rm = TRUE, ...)
Where:

x: original zoo/xts object.

FUN: function that will be applied to the elements of x that belong to a determined
month or season.

na.rm: indicates if the missing values in x should be ignored during the computation.

Moreover, dm2sesonal requires:


season: indicates the season of the year for which the values must be obtained.

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 20 / 31


Monthly, Annual and seasonal values

Mensual, annual and seasonal values - III


Some examples:

Loading the hourly discharge data:


data(KarameaAtGorgeQts)
q <- KarameaAtGorgeQts

Mean monthly discharge values for the whole season:


monthlyfunction(q, FUN=mean, season="DJF")
## Jan Feb Mar Apr May Jun
## x 116.1014 67.56221 65.45352 97.33134 143.9699 102.5171
## Jul Aug Sep Oct Nov Dec
## x 130.0628 129.0778 182.8112 137.7709 134.0901 126.7648

Mean monthly discharge values for the whole period:


annualfunction(q, FUN=mean)
## X
## [1,] 119.343

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 21 / 31


Monthly, Annual and seasonal values

Mensual, annual and seasonal values - IV


Some examples:

Mean seasonal discharge values for the whole period:


q.d <- subdaily2daily(q, FUN=mean)
seasonalfunction(q.d, FUN=mean)
## DJF MAM JJA SON
## 103.8161 102.3105 120.7486 151.6703

Equivalently for MAM:


MAM <- extract(q.d, trgt="MAM")
mean(MAM, na.rm=TRUE)
## [1] 102.3105
M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 22 / 31
Plots

Tabla de Contenidos

1 Context

2 Temporal sequences

3 Extraction

4 Conversions/aggregation

5 Monthly, Annual and seasonal values

6 Plots

7 References

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 23 / 31


Plots

hydroplot - I

hydroplot
The function hydroplot creates the daily, monthly and seasonal temporal series plots.

The value of the argument pfreq controls the temporal frequency of the shown series
(daily, monthly, annual, seasonal).

The value of the argument ptype controls the plot type that is produced: time series,
boxplots and/or histogramas.

Loading the daily discharge data of Oca station in Ona (Ebro River Basin, Spain).

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 24 / 31


Plots

hydroplot - II
require(hydroTSM)
## Loading daily streamflows at the station ##
data(OcaEnOnaQts)

El siguiente comando crea un gráfico sólo con la serie temporal, durante todo el perı́odo

hydroplot(OcaEnOnaQts, pfreq="o", main="Caudal diario, [m3/s]")

## [Note: pfreq=’o’ => ptype has been changed to ’ts’]

Caudal diario, [m3/s]


50
40
OcaEnOnaQts
30
20
10
0

Jan 01 Jul 01 Jan 01 Jul 01 Jan 01 Jul 01 Dec 31


1961 1961 1962 1962 1963 1963 1963
Time

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 25 / 31


Plots

hydroplot - III
El siguiente comando crea un gráfico sólo para el año 1962:

hydroplot(OcaEnOnaQts, pfreq="o", from="1962-01-01", to="1962-12-31")

## [Note: pfreq=’o’ => ptype has been changed to ’ts’]


40
30
OcaEnOnaQts
20
10
0

Jan 01 Mar 01 May 01 Jul 01 Sep 01 Nov 01 Dec 31


1962 1962 1962 1962 1962 1962 1962
Time

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 26 / 31


Plots

hydroplot - IV
The following comand creates a plot of the daily discharge for each season (personalyzed for the Southern Hemisphere):

hydroplot(OcaEnOnaQts, pfreq="seasonal", FUN=mean, stype="default",


season.names=c("Summer","Autumn", "Winter","Spring"))

Summer (DJF) Summer (DJF)


OcaEnOnaQts

OcaEnOnaQts
11

11
6 8

6 8
1961 1962 1963
Time

Autumn (MAM) Autumn (MAM)


OcaEnOnaQts

OcaEnOnaQts
9

9
7

7
5

5
1961 1962 1963
Time

Winter (JJA) Winter (JJA)


OcaEnOnaQts

OcaEnOnaQts
2.8

2.8
2.2

2.2
1961 1962 1963
Time

Spring (SON) Spring (SON)


OcaEnOnaQts

OcaEnOnaQts
3.5

3.5
2.0

2.0

1961 1962 1963


Time

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 27 / 31


Plots

hydroplot - V
The following command creates a plot with 3 temporal series, 3 boxplots and 3 hystograms:

hydroplot(OcaEnOnaQts, FUN=mean, ylab= "Q", var.unit = "m3/s")

Daily time series Daily Boxplot Daily Histogram


Q [m3/s/day]

Q [m3/s/day]
Daily series
40

40

0.15
Pbb
20

20

0.00
0

Jan 01 Jan 01 Jan 01 Dec 31 0 1961 1962 1963 0 10 20 30 40 50


1961 1962 1963 1963
Time years Q [m3/s/day]

Monthly time series Monthly Boxplot Monthly Histogram


Q [m3/s/month]

Q [m3/s/month]

Monthly series

0.08
5 10

5 10

Pbb
0.00
Jan Jan Jan Dec Jan Mar May Jul Sep Nov 0 5 10 15
1961 1962 1963 1963
Time months Q [m3/s/month]

Annual time series Annual Boxplot Annual Histogram


Q [m3/s/year]

Q [m3/s/year]

0.0 0.3 0.6


6.0

6.0

Pbb
5.0

5.0

4.5 5.0 5.5 6.0 6.5


1961 1962 1963
Time Q [m3/s/year]

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 28 / 31


References

Tabla de Contenidos

1 Context

2 Temporal sequences

3 Extraction

4 Conversions/aggregation

5 Monthly, Annual and seasonal values

6 Plots

7 References

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 29 / 31


References

Referencias - I

hydroTSM vignette

hydroTSM manual

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 30 / 31


Thanks

Thank you !

mauricio.zambrano @ ufrontera.cl
Teléfono: +56 45 259 2812

M. Zambrano-Bigiarini (UFRO) IIO352 CP3b, 05-Abr-2023 31 / 31

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy