-
Notifications
You must be signed in to change notification settings - Fork 3
/
get_weather_data.R
115 lines (90 loc) · 2.45 KB
/
get_weather_data.R
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
library(tidyverse)
library(magrittr)
library(ncdf4)
library(raster)
library(MASS)
library(anytime)
#setwd('/home/ma/majw/Notebooks/data_service_catalog/')
# open the netcdf file
setwd('D:/projects/ecmwf')
load_ncdf <- function(year) {
# Open file
ncdffile <- nc_open(paste0(year,'.nc'), write=FALSE)
nvals <- length(ncdffile$dim[[3]]$vals)
# Number of variables to extract from ncdf
nvars <- length(ncdffile$var)
# Output data frame shape
envidata <- as_data_frame(matrix(rep(0, times = nvals * nvars), ncol = nvars))
varnames <- NULL
for(i in 1:nvars){
# Get variable
variable <- ncdffile$var[[i]]
# Get variable name
varnames[i] <- variable$name
# Get values for the entire grid
valuegrid <- ncvar_get(ncdffile, varid = variable)
# #retrieve latitude and longitude information
# lon <- which(valuegrid$dim$lon$vals == 358.50)
# lat <- which(valuegrid$dim$lat$vals == 51.00)
#
# # Get value of the specific cell we are interested in
# valuegrid[lon, lat, 1]
envidata[,i] <- valuegrid
}
# Set column names
envidata <- set_colnames(envidata, varnames)
# Get timestamps
# # "hours since 1900-01-01 00:00:0.0"
envidata[,'timestamps'] <- variable$dim[[3]]$vals %>%
as.numeric %>%
as_data_frame %>%
mutate(value = as.POSIXct(value * 60 * 60, origin="1900-01-01"))
oldnames <- c("fg10","sd","sf","tcc","u10","v10","t2m","d2m","e","mn2t","tco3","tp","timestamps")
if(all(colnames(envidata) == oldnames)){
newnames <- c(
'windgusts',
'snowdepth',
'snowfall',
'cloudcover',
'wind1',
'wind2',
'temperature',
'dewpoint',
'evaporation',
'mintemp',
'ozone',
'precipitation',
'timestamp'
)
envidata <- set_colnames(envidata, newnames)
}else{
error('columns not matching')
}
# fg10 = windgusts
# sd = snowdepth
# sf = snowfall
# tcc = cloudcover
# u10 = wind1
# v10 = wind2
# t2m = temperature
# d2m = dewpoint
# e = evaporation
# mn2t = mintemp
# tco3 = ozone
# tp = precipitation
return(envidata)
}
# Get data for all years
years <- seq(2001, 2016, 1)
for(year in years){
thisyear <- load_ncdf(year)
if(exists('total')){
total <- bind_rows(total, thisyear)
}else{
total <- thisyear
}
}
# Check the result
head(total)
write_csv(total, path = 'D:/projects/ecmwf/evidata.csv')
write_rds(total, path = 'D:/projects/ecmwf/evidata.rds')