Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
Temperaturverteilung
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Package Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
s39174
Temperaturverteilung
Commits
cc8a6173
Commit
cc8a6173
authored
3 years ago
by
Clemens Berteld
Browse files
Options
Downloads
Patches
Plain Diff
Minor changes
parent
1ad33abf
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
dataacquisition/ExportToDatabase.py
+12
-9
12 additions, 9 deletions
dataacquisition/ExportToDatabase.py
dataacquisition/GetAverageData.py
+1
-1
1 addition, 1 deletion
dataacquisition/GetAverageData.py
dataacquisition/config.ini
+4
-1
4 additions, 1 deletion
dataacquisition/config.ini
with
17 additions
and
11 deletions
dataacquisition/ExportToDatabase.py
+
12
−
9
View file @
cc8a6173
...
...
@@ -20,6 +20,7 @@ cfg = configparser.ConfigParser()
cfg
.
read
(
'
config.ini
'
)
assert
"
POSTGRES
"
in
cfg
,
"
missing POSTGRES in config.ini
"
param_postgres
=
cfg
[
"
POSTGRES
"
]
param_interpol
=
cfg
[
"
INTERPOLATION
"
]
stationGPD
=
None
...
...
@@ -65,7 +66,7 @@ def dbexists(db_name):
def
check_for_db_existence
(
station_list
,
db_name
):
print
(
"
Checking for database existence
"
)
if
dbexists
(
db_name
):
print
(
'
DB existing
exists
'
)
print
(
'
DB existing
'
)
else
:
create_db
(
db_name
)
create_table
(
station_list
,
db_name
)
...
...
@@ -89,9 +90,11 @@ def create_table(station_list, db_name):
def
insert_empty_matrix_into_db
():
print
(
'
Inserting empty matrix into database
'
)
matrix_density
=
param_interpol
[
'
matrix_density
'
]
with
psycopg2
.
connect
(
database
=
param_postgres
[
'
dbName
'
],
user
=
param_postgres
[
"
user
"
],
password
=
param_postgres
[
"
password
"
],
host
=
param_postgres
[
"
host
"
],
port
=
param_postgres
[
"
port
"
])
as
connection
:
with
connection
.
cursor
()
as
cursor
:
with
open
(
'
clipped_matrix_
25x25.csv
'
,
'
r
'
)
as
matrix
:
with
open
(
'
clipped_matrix_
{}x{}.csv
'
.
format
(
matrix_density
,
matrix_density
)
,
'
r
'
)
as
matrix
:
matrix_data
=
matrix
.
readlines
()
for
line
in
matrix_data
[
1
:]:
values
=
''
# Used in second parameter of cursor.execute() (Avoids SQL injection)
...
...
@@ -107,10 +110,11 @@ def insert_empty_matrix_into_db():
# print(query.as_string(cursor))
# print(values)
cursor
.
execute
(
query
,
values
)
print
(
'
Inserted empty matrix into database
'
)
def
create_matrix_data
():
print
(
'
Calculating interpolation data for matrix
'
)
# start_time = time.time()
with
psycopg2
.
connect
(
database
=
param_postgres
[
'
dbName
'
],
user
=
param_postgres
[
"
user
"
],
password
=
param_postgres
[
"
password
"
],
host
=
param_postgres
[
"
host
"
],
port
=
param_postgres
[
"
port
"
],
keepalives
=
1
,
keepalives_idle
=
30
,
keepalives_interval
=
10
,
keepalives_count
=
5
)
as
connection
:
with
connection
.
cursor
()
as
cursor
:
...
...
@@ -127,10 +131,12 @@ def create_matrix_data():
update_data
.
append
({
'
year
'
:
year
,
'
value
'
:
value
,
'
id
'
:
id
})
query
=
sql
.
SQL
(
"""
UPDATE stations SET
"
%(year)s
"
= %(value)s WHERE id = %(id)s;
"""
)
psycopg2
.
extras
.
execute_batch
(
cursor
,
query
,
update_data
)
# 2 times faster than using execute() in a for loop, ~20 mins instead of 40
psycopg2
.
extras
.
execute_batch
(
cursor
,
query
,
update_data
)
# Multiple times faster than using execute() in a for loop
# print((time.time() - start_time), 'seconds')
def
insert_data
(
station_list
,
db_name
):
print
(
'
Inserting data into database
'
)
with
psycopg2
.
connect
(
database
=
db_name
,
user
=
param_postgres
[
"
user
"
],
password
=
param_postgres
[
"
password
"
],
host
=
param_postgres
[
"
host
"
],
port
=
param_postgres
[
"
port
"
])
as
connection
:
with
connection
.
cursor
()
as
cursor
:
...
...
@@ -160,8 +166,5 @@ def export(station_list):
check_for_db_existence
(
station_list
,
param_postgres
[
'
dbName
'
])
insert_data
(
station_list
,
param_postgres
[
'
dbName
'
])
insert_empty_matrix_into_db
()
start_time
=
time
.
time
()
create_matrix_data
()
print
((
time
.
time
()
-
start_time
),
'
seconds
'
)
create_matrix_data
()
print
(
'
Installation successful. You can run the api.py now.
'
)
This diff is collapsed.
Click to expand it.
dataacquisition/GetAverageData.py
+
1
−
1
View file @
cc8a6173
...
...
@@ -85,7 +85,7 @@ def get_interpolation_data_for_point(lat, lon, columns):
year_columns
=
(
str
(
columns
).
replace
(
"""
SQL(
'"""
,
""
).
replace
(
'"'
,
''
).
replace
(
"'
)
"
,
""
)).
split
(
'
,
'
)
neighbours
=
get_neighbours
(
cursor
,
lat
,
lon
,
columns
)
avg_data
=
calc_idw
(
neighbours
,
year_columns
)
print
(
avg_data
)
#
print(avg_data)
return
avg_data
...
...
This diff is collapsed.
Click to expand it.
dataacquisition/config.ini
+
4
−
1
View file @
cc8a6173
...
...
@@ -6,4 +6,7 @@ password = postgres
dbName
=
temperatures_berteld_morstein
[INTERPOLATION]
amount_neighbours
=
5
\ No newline at end of file
; neighbouring measurements used for interpolation
amount_neighbours
=
5
; in km. 25km or 10km possible
matrix_density
=
25
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment