-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_ancil_data_postgres.R
72 lines (56 loc) · 2.24 KB
/
create_ancil_data_postgres.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
# Author: Saeesh Mangwani
# Date: 2021-06-02
# Description: A script that generates the ancilliary data files from the
# hourly observation well data (scraped monthly by the obswell_scraping.py
# script)
# ==== Loading libraries ====
library(DBI)
library(RPostgres)
library(lubridate)
library(rjson)
# ==== Reading data ====
# Credentials files
creds <- fromJSON(file = 'credentials.json')
# Setting default schema unless pre-specified
if (is.null(creds$schema)) creds$schema <- 'obswell'
# Opening database connection
conn <- dbConnect(RPostgres::Postgres(),
host = creds$host, dbname = creds$dbname,
user = creds$user, password = creds$password)
# Dropping any temp tables if they exist
dbExecute(conn, paste0('drop table if exists ', creds$schema, '.temp'))
# Creating a temp table as a copy of the hourly dataset with time converted to date
dbExecute(conn,
paste0('create table ', creds$schema, '.temp as (
select "Time","Time"::date as "Date", "Value", "Approval", "myLocation"
from ', creds$schema, '.hourly
)'))
# ==== Daily mean dataset ====
# Dropping the table if it exists
dbExecute(conn, paste0('drop table if exists ', creds$schema, '.daily_mean'))
# Create the daily mean table from the temp table
dbExecute(conn,
paste0('
create table ', creds$schema, '.daily_mean as (
select "myLocation",
"Date",
avg("Value") as "Value",
count("Date") as "numObservations"
from ', creds$schema, '.temp
group by "myLocation", "Date"
order by "myLocation", "Date")
'))
# ==== Past 1-year dataset ====
# Dropping table if it exists
dbExecute(conn, paste0('drop table if exists ', creds$schema, '.hourly_recent'))
# Specifying the timestamp for 1-year ago
date_filter <- ymd((Sys.Date() - 366))
# Creating a past 1 year dataset from the temptable
dbExecute(conn,
paste0('create table ', creds$schema, '.hourly_recent as(',
'select "Time", "Value", "Approval", "myLocation"
from ', creds$schema, '.temp where "Date" >= ',
"'", date_filter, "'",
')'))
# Dropping the temp table
dbExecute(conn, paste0('drop table ', creds$schema, '.temp'))