-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
93 lines (71 loc) · 4.2 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
---
title: "simpleRCRU"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
[![Build Status](https://travis-ci.org/ahmad-alkadri/simpleRCRU.svg?branch=master)](https://travis-ci.org/ahmad-alkadri/simpleRCRU)
Just like its name implies, this package aims to help working with CRU datasets in a simpler, or more straightforward way.
To achieve this, **simpleRCRU** wraps around two primary packages: namely, *ncdf4* and *raster*.
However, unlike its superiors, **simpleRCRU**'s sole objective is to obtain the climate data from the CRU datasets as quick as possible.
## Background
In 2017, during my internship with [CIFOR (Center for International Forestry Research)](http://www.cifor.org), I was tasked to obtain the climate data from CRU datasets for several regions in Kalimantan island, Indonesia.
I wasn't very familiar with CRU, nor with packages such as *raster* or *ncdf4*, but I was curious.
It took me days to finally got the hang of it, and about hours to finally wrote the one function that I included here: *extractcru()*.
## Methods
The reason why I wrote this package (or, back then, these functions) is simple: working with *raster* for obtaining the climate data from CRU datasets is simply not simple at all.
Sure, *raster* is very powerful for working with CRU datasets, but I only wanted to obtain some simple climate data.
The kind of information that I needed was just, for example, the monthly **precipitation** data for Cilacap (a little town in Indonesia where I grew up, more on that later), during the year **1991**.
With *extractcru* function, all I needed to do is typing:
```{r, message=FALSE}
library(simpleRCRU)
library(R.utils)
# Download the climate dataset
download.file("https://crudata.uea.ac.uk/cru/data/hrg/cru_ts_4.02/cruts.1811131722.v4.02/pre/cru_ts4.02.1991.2000.pre.dat.nc.gz",
destfile = "cru_ts4.02.1991.2000.pre.dat.nc.gz")
# Unzipping the dataset
gunzip("cru_ts4.02.1991.2000.pre.dat.nc.gz",
remove = FALSE, overwrite = TRUE)
file <- ("cru_ts4.02.1991.2000.pre.dat.nc")
year <- 1991 #Requested year
var <- "pre" #Variable code for precipitation
lon <- 109.01 #Longitude of Cilacap town
lat <- -7.74 #Latitude of Cilacap town
datres <- extractcru(file, lon, lat, var, year)
file.remove("cru_ts4.02.1991.2000.pre.dat.nc")
print(datres)
```
Next, we can plot this very easily, with:
```{r}
datres$Monthnum <- c(1:12)
plot(x = datres$Monthnum, y = datres$pre, xlab = "Month", ylab = "Monthly precipitation (mm)")
lines(x = datres$Monthnum, y = datres$pre)
```
Now let's say that we'd like to visualize the yearly precipitations in the town of Cilacap from 1991 to 2000 For that, it'd be better to use boxplot. To do this, we simply write:
```{r}
gunzip("cru_ts4.02.1991.2000.pre.dat.nc.gz",
remove = FALSE, overwrite = TRUE)
file <- "cru_ts4.02.1991.2000.pre.dat.nc" #Precipitation dataset from CRU
var <- "pre" #Variable code for precipitation
lon <- 109.01 #Longitude of Cilacap town
lat <- -7.74 #Latitude of Cilacap town
years <- 1991:2000 #Requested years
datres2 <- extractcru(file, lon, lat, var, year = years)
file.remove("cru_ts4.02.1991.2000.pre.dat.nc")
library(ggplot2) #Loading ggplot2 package, easier to work with boxplot
ggplot(data = datres2, aes(x = Year, y = pre, group = Year))+geom_boxplot()+
xlab("Year")+ylab("Precipitations (mm)")+theme_bw()
```
And finally, to export the result, for example the precipitation data from 1991 to 2000 above, we simply write:
```{r}
write.csv(datres2, file = "pre_1991_2000_Cilacap.csv")
```
## Future Works
I've finished my internship in CIFOR since 2017, and to be honest, I haven't touched these functions again since then.
It's only yesterday that I finally stumbled upon them again on my laptop, and thus I decided to upload them here.
I have some ideas on developing this package in the future, including but not limited to:
+ adding capabilities for extracting the data from several coordinates at the same time in a single function
+ adding capabilities for extracting and interpolating data for a given area, not just a single point coordinate
+ plotting
If you have any advice, inputs, or even if you want to contribute, please don't hesitate to contact me at ahmad.alkadri@outlook.com. Many thanks!