Skip to content

Commit 1bcb9eb

Browse files
committed
15.09
1 parent bc2d89b commit 1bcb9eb

File tree

6 files changed

+163
-19
lines changed

6 files changed

+163
-19
lines changed

converstion_and_excerises

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ TASK ONE:
2222
7. You can perform trend analysis or any type of time series analysis you would like.
2323

2424
TASK TWO:
25+
2D maps
2526

2627

2728

images/Variables.png

30 KB
Loading

images/matrix_raster_terra.png

34.6 KB
Loading

index.adoc

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ considered. Examples, atmospheric data, oceanic data, and soil profiles.
196196
NOTE: All these dimensions can additionally include the time axis.
197197
198198
== Variables and constants
199+
image:images/Variables.png[height=10]
199200
200201
* Variables are used to store data, which can be changed afterwards
201202
* The name given to a variable is known as _identifier_
@@ -1796,6 +1797,31 @@ image::iris.png[height=800]
17961797
17971798
* Will go in more detail with `ggplot2` -> allows more modifications
17981799
1800+
== Exercise I
1801+
1802+
. Pick a location (i.e. longitude and latitude), where you want to apply your analysis.
1803+
1804+
. List all netCDF files (except files in `final task` folder) using `list.files.` Check the options `full.names` & `recursive`
1805+
1806+
. Loop over the listed files and use the function `get_ts_as_csv` to get your csv files (i.e. time series)
1807+
1808+
. Load CSV files of your choice and:
1809+
.. Plot different types of plots
1810+
.. Run some statistical tests.
1811+
.. Explore the climate conditions of your area
1812+
1813+
. You may do some aggregation, e.g., monthly, seasonally, and annually
1814+
. You can perform trend analysis or any time series analysis you would like.
1815+
1816+
[IMPORTANT]
1817+
.Climate Variables:
1818+
====
1819+
1820+
. sfcWind -> Surface wind [m/s]
1821+
. pr -> Precipitation [kg m-2 s-1]
1822+
. tas -> Surface temperature [k]
1823+
====
1824+
17991825
== Tidyverse
18001826
18011827
[quote, tidiverse.org]
@@ -2374,9 +2400,7 @@ image::gg_facet_1.png[height=650]
23742400
* There is a great amount of packages to work with spatial data
23752401
* Might not be as user friendly as QGIS, but really pays off to learn
23762402
* Packages needed:
2377-
** `rgdal`
2378-
** `raster`
2379-
** `sp`
2403+
** `terra`
23802404
** `sf`
23812405
* Some of those packages need installation of other software outside of R
23822406
** This might be time consuming...
@@ -2393,33 +2417,35 @@ image::gg_facet_1.png[height=650]
23932417
--
23942418
[source,R]
23952419
----
2396-
library(raster)
2420+
library(terra)
23972421

23982422
# Creating a raster from a matrix
2399-
r1 <- raster(matrix(rnorm(19*13), nrow = 19),
2400-
xmn=5, xmx=15, ymn=-5, ymx=10,
2401-
crs = sp::CRS(SRS_string = "EPSG:4326"))
2423+
r1 <- rast(matrix(rnorm(19*13), nrow = 19), crs = "EPSG:4326")
2424+
# define extent
2425+
ext(r1)<-c(xmin=5, xmax=15, ymin=-5, ymax=10)
2426+
24022427
r1
2403-
class : RasterLayer
2404-
dimensions : 19, 13, 247 (nrow, ncol, ncell)
2405-
resolution : 0.7692308, 0.7894737 (x, y)
2406-
extent : 5, 15, -5, 10 (xmin, xmax, ymin, ymax)
2407-
crs : +proj=longlat +datum=WGS84 +no_defs
2408-
source : memory
2409-
names : layer
2410-
values : -2.990121, 2.558331 (min, max)
2428+
class : SpatRaster
2429+
dimensions : 19, 13, 1 (nrow, ncol, nlyr)
2430+
resolution : 0.7692308, 0.7894737 (x, y)
2431+
extent : 5, 15, -5, 10 (xmin, xmax, ymin, ymax)
2432+
coord. ref. : lon/lat WGS 84 (EPSG:4326)
2433+
source : memory
2434+
name : lyr.1
2435+
min value : -2.777259
2436+
max value : 2.850702
24112437

24122438
plot(r1, main = "Raster made from a matrix")
24132439
# Plot the center of the pixels
2414-
points(coordinates(r1), pch=3, cex=0.5)
2440+
points(crds(r1), pch=3, cex=0.5)
24152441
----
2416-
* For other sources check `?raster`
2442+
* For other sources check `?terra`
24172443
24182444
--
24192445
24202446
[.column]
24212447
--
2422-
image::matrix_raster.png[height=900]
2448+
image::matrix_raster_terra.png[height=900]
24232449
--
24242450
24252451
=== Read raster data

removed_slides.adoc

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
== Spatial data in image:r-seeklogo.svg[height=45]
2+
3+
[%step]
4+
* There is a great amount of packages to work with spatial data
5+
* Might not be as user friendly as QGIS, but really pays off to learn
6+
* Packages needed:
7+
** `rgdal`
8+
** `raster`
9+
** `sp`
10+
** `sf`
11+
* Some of those packages need installation of other software outside of R
12+
** This might be time consuming...
13+
* Both _vector_ and _raster_ data can be:
14+
** Read to R
15+
** Modified
16+
** Created from scratch
17+
** Saved into desired format
18+
19+
[.columns.is-vcentered]
20+
== _Rasters_
21+
22+
[.column]
23+
--
24+
[source,R]
25+
----
26+
library(raster)
27+
28+
# Creating a raster from a matrix
29+
r1 <- raster(matrix(rnorm(19*13), nrow = 19),
30+
xmn=5, xmx=15, ymn=-5, ymx=10,
31+
crs = sp::CRS(SRS_string = "EPSG:4326"))
32+
r1
33+
class : RasterLayer
34+
dimensions : 19, 13, 247 (nrow, ncol, ncell)
35+
resolution : 0.7692308, 0.7894737 (x, y)
36+
extent : 5, 15, -5, 10 (xmin, xmax, ymin, ymax)
37+
crs : +proj=longlat +datum=WGS84 +no_defs
38+
source : memory
39+
names : layer
40+
values : -2.990121, 2.558331 (min, max)
41+
42+
plot(r1, main = "Raster made from a matrix")
43+
# Plot the center of the pixels
44+
points(coordinates(r1), pch=3, cex=0.5)
45+
----
46+
* For other sources check `?raster`
47+
48+
--
49+
50+
[.column]
51+
--
52+
image::matrix_raster.png[height=900]
53+
--
54+
=== Read raster data
55+
56+
[source,R]
57+
----
58+
# Run these 4 lines in this order to install the "hires" version of "rnaturalearth"
59+
install.packages("Rtools")
60+
install.packages("devtools")
61+
devtools::install_github("ropenscilabs/rnaturalearth")
62+
devtools::install_github("ropenscilabs/rnaturalearthhires")
63+
64+
library(sf)
65+
library(raster)
66+
library(rgdal)
67+
library(rnaturalearth)
68+
69+
setwd("/home/dqc/Documents/PhD/Students/R_course/FRM/spatial/")
70+
71+
de_dem <- raster("deutschland_dgm.asc")
72+
crs(de_dem) <- sp::CRS(SRS_string = "ESRI:31494")
73+
74+
print(de_dem)
75+
76+
class : RasterLayer
77+
dimensions : 910, 720, 655200 (nrow, ncol, ncell)
78+
resolution : 1000, 1000 (x, y)
79+
extent : 4030000, 4750000, 5230000, 6140000 (xmin, xmax, ymin, ymax)
80+
crs : +proj=tmerc +lat_0=0 +lon_0=12 +k=1 +x_0=4500000 +y_0=0 +ellps=bessel +units=m +no_defs
81+
source : /mnt/Linux/Documents/PhD/Data/Sachsen/deutschland_dgm.asc
82+
names : deutschland_dgm
83+
----

replace_rast.R

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,38 @@ coord. ref. : Germany_Zone_4 (ESRI:31494)
7070
source : deutschland_dgm.asc
7171
name : deutschland_dgm
7272
min value : -178.46
73-
max value : 2770.35
73+
max value : 2770.35
74+
75+
# PDF slide 87 ------------------------------------------------------------
76+
77+
sqrt(de_dem)
78+
class : SpatRaster
79+
dimensions : 910, 720, 1 (nrow, ncol, nlyr)
80+
resolution : 1000, 1000 (x, y)
81+
extent : 4030000, 4750000, 5230000, 6140000 (xmin, xmax, ymin, ymax)
82+
coord. ref. : Germany_Zone_4 (ESRI:31494)
83+
source : memory
84+
name : deutschland_dgm
85+
min value : 0.00000
86+
max value : 52.63412
87+
de_dem + de_dem*4 # Need to have same dimensions
88+
class : SpatRaster
89+
dimensions : 910, 720, 1 (nrow, ncol, nlyr)
90+
resolution : 1000, 1000 (x, y)
91+
extent : 4030000, 4750000, 5230000, 6140000 (xmin, xmax, ymin, ymax)
92+
coord. ref. : Germany_Zone_4 (ESRI:31494)
93+
source : memory
94+
name : deutschland_dgm
95+
min value : -892.30
96+
max value : 13851.75
97+
98+
# PDF slide 88------------------------------------------------------------
99+
par(mfrow=c(1,3))
100+
terra::hist(de_dem, main="Distribution of elevation \n values",
101+
breaks=40,maxcell=1000000)
102+
terra::boxplot(de_dem, ylab= "Elevation", main = "Boxplot")
103+
terra::plot(de_dem, main = "Basic plot",
104+
col = RColorBrewer::brewer.pal(7, "BrBG"),
105+
range = c(0, 2500),
106+
ylim = ext(de_dem)[c(3,4)])
107+
list.files()

0 commit comments

Comments
 (0)