-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquery_api_PowerBI.R
executable file
·138 lines (122 loc) · 4.46 KB
/
query_api_PowerBI.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#Program for Meteomatics_Weather_API
#Packages
library(httr)
library(data.table)
library(lubridate)
library(ggplot2)
VERSION = "2.0.1"
query_user_features = function(username, password)
{
r=GET(sprintf('https://%s:%[email protected]/user_stats_json', username, password))
j=jsonlite::fromJSON(content(r, 'text'))
res <- logical(3)
names(res) <- c('area request option', 'historic request option', 'model select option')
res['area request option'] = j$`user statistics`$`area request option`
res['historic request option'] = j$`user statistics`$`historic request option`
res['model select option'] = j$`user statistics`$`model select option`
return(res)
}
api_timeseries = function(query)
{
resp1 = GET(query)
con = textConnection(content(resp1,"text"))
parsed1 = read.csv(con, sep = ";")
structure(
list(content = parsed1)
)
}
api_domain = function(query)
{
resp1 = fread(query, skip=2, fill = TRUE)
structure(
list(content = resp1)
)
}
query_api = function(username, password, startdate, enddate,
interval, parameters, coordinate,
request_type="timeseries",
model="mix", time_zone="UTC"
)
{
#def Variabeln
startdate_query = strftime(startdate,format='%Y-%m-%dT%H:%M:%OSZ', tz='UTC')
enddate_query = strftime(enddate,format='%Y-%m-%dT%H:%M:%OSZ', tz='UTC')
ensSelect = {}
#URL
if(request_type == "timeseries")
{
query = sprintf("https://%s:%[email protected]/%s--%s:%s/%s/%s/csv?model=%s&connector=PowerBI_connector_v%s",
username, password, startdate_query, enddate_query, interval, parameters, coordinate, model, VERSION
)
result = api_timeseries(query)
#Data in new Dataframe
df = as.data.frame(result$content)
#Numbers of col and row
r = nrow(df)
c = ncol(df)
#Number of Parameters
n_r = match("validdate", names(df))+1
n_p = ncol(df[n_r:c])
#Dates in right shape
df$validdate = as.POSIXct(df$validdate,format='%Y-%m-%dT%H:%M:%OSZ', tz = 'UTC')
df$validdate = with_tz(df$validdate, tzone = time_zone)
#Right shape parameters
parameter = data.frame(stack(df[n_r:c]))
parameter = parameter[c(2,1)]
#Length of Date (double so many time as parameters are there)
df_timeseries = as.data.frame(rep(df$validdate, n_p))
#Rename and put togheter
colnames(df_timeseries) = c('Date')
df_timeseries['Lat'] = as.data.frame(rep(df$lat, n_p))
df_timeseries["Lon"] = as.data.frame(rep(df$lon, n_p))
df_timeseries['Parameter']= parameter$ind
df_timeseries['Value']= parameter$values
return(df_timeseries)
} else {
query = sprintf("https://%s:%[email protected]/%s/%s/%s/csv?model=%s&connector=PowerBI_connector_v%s",
username, password, startdate_query, parameters, coordinate, model, VERSION
)
result = api_domain(query)
#New Dataframe
df = as.data.frame(result$content)
#Numbers of columns and rows
r = nrow(df)
c = ncol(df)
#Longitude
Lon = rep(c(t(df[1,2:c])), r-1)
df_domain = data.frame(Lon)
#Latitude
Lat = rep(df[1:r,1], each = c-1)
df_domain["Lat"] = data.frame(as.numeric(Lat))
#Values
df_domain["Values"] = data.frame(Values = c(t(df[1:r,2:c])))
mm = matrix(unlist(df_domain["Values"]), ncol = c -1 , byrow = TRUE)
image(z=t(mm[nrow(mm):1,]))
return(df_domain)
}
}
#Choose if timeseries or domain
request_type = "timeseries" #"domain","timeseries"
#Data
username = "powerbi-community"
password = "Alepafume675"
time_zone = "Europe/Berlin"
startdate = ISOdatetime(year = strtoi(strftime(today(),'%Y')),
month = strtoi(strftime(today(),'%m'), 10),
day = strtoi(strftime(today(),'%d'), 10),
hour = 00, min = 00, sec = 00, tz = "UTC")
enddate = ISOdatetime(year = strtoi(strftime(today(),'%Y')),
month = strtoi(strftime(today(),'%m'), 10),
day = strtoi(strftime(today(),'%d'), 10)+1,
hour = 00, min = 00, sec = 00, tz = "UTC")
interval = "PT1H"
if (request_type == "timeseries"){
parameters = "t_2m:C,relative_humidity_1000hPa:p" #different parameters
coordinate = "47.11,11.47" #Point and line
} else {
#startdate is used
parameters = "t_2m:C" #only one parameter
coordinate = "47.9,5.7_45.8,10.7:0.1,0.1" #Rectangle
}
#Data from the API
output = query_api(username, password, startdate, enddate, interval, parameters, coordinate, time_zone = time_zone)