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
2e3c5b83
Commit
2e3c5b83
authored
3 years ago
by
Clemens Berteld
Browse files
Options
Downloads
Patches
Plain Diff
Repairing stuff
parent
3896f2f1
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
api/api.py
+2
-2
2 additions, 2 deletions
api/api.py
dataacquisition/ExportToDatabase.py
+15
-11
15 additions, 11 deletions
dataacquisition/ExportToDatabase.py
dataacquisition/sandbox.py
+2
-9
2 additions, 9 deletions
dataacquisition/sandbox.py
with
19 additions
and
22 deletions
api/api.py
+
2
−
2
View file @
2e3c5b83
...
...
@@ -48,7 +48,7 @@ def index():
else
:
if
'
id
'
in
request
.
args
:
station_id
=
request
.
args
[
'
id
'
]
wheres
=
wheres
+
(
sql
.
SQL
(
"
AND id = {values}
"
).
format
(
column
=
sql
.
Identifier
(
'
stations
'
,
'
id
'
),
values
=
sql
.
Placeholder
()))
wheres
=
wheres
+
(
sql
.
SQL
(
"
AND
station_
id = {values}
"
).
format
(
column
=
sql
.
Identifier
(
'
stations
'
,
'
station_
id
'
),
values
=
sql
.
Placeholder
()))
for
n
in
[
int
(
station_id
)]:
values
=
(
*
values
,
n
)
# adding n to existing tuple
...
...
@@ -61,7 +61,7 @@ def index():
# Composing query
query
=
sql
.
SQL
(
"
SELECT array_to_json(array_agg(row_to_json(t))) from (
"
"
SELECT id, {} FROM stations
"
"
SELECT
station_
id, {} FROM stations
"
"
WHERE lon IS NOT NULL
"
# Unnecessary filter, just so the real filters can always be written with AND
"
{}
"
"
) t;
"
).
format
(
columns
,
wheres
)
...
...
This diff is collapsed.
Click to expand it.
dataacquisition/ExportToDatabase.py
+
15
−
11
View file @
2e3c5b83
...
...
@@ -13,10 +13,10 @@ import psycopg2.extras
from
psycopg2
import
sql
from
psycopg2.extensions
import
ISOLATION_LEVEL_AUTOCOMMIT
import
configparser
#
from api.GetAverageData import get_interpolation_data_for_point
from
api.GetAverageData
import
get_interpolation_data_for_point
cfg
=
configparser
.
ConfigParser
()
cfg
.
read
(
'
./config.ini
'
)
cfg
.
read
(
'
.
.
/config.ini
'
)
assert
"
POSTGRES
"
in
cfg
,
"
missing POSTGRES in config.ini
"
param_postgres
=
cfg
[
"
POSTGRES
"
]
param_interpol
=
cfg
[
"
INTERPOLATION
"
]
...
...
@@ -73,7 +73,7 @@ def check_for_db_existence(cursor):
def
create_table
(
station_list
,
cursor
):
print
(
'
Creating table stations
'
)
df_columns
=
list
(
station_list
)
columns
=
[
'
id INTEGER
'
,
'
lon NUMERIC
'
,
'
lat NUMERIC
'
,
'
country TEXT
'
,
'
file TEXT
'
]
columns
=
[
'
station_
id INTEGER
'
,
'
lon NUMERIC
'
,
'
lat NUMERIC
'
,
'
country TEXT
'
,
'
file TEXT
'
]
for
column
in
df_columns
:
if
str
(
column
).
startswith
(
'
19
'
)
or
str
(
column
).
startswith
(
'
20
'
):
columns
.
append
(
'"
{}
"
NUMERIC
'
.
format
(
column
))
...
...
@@ -104,7 +104,7 @@ def insert_empty_matrix_into_db(cursor):
for
n
in
[
id
,
lon
,
lat
]:
values
=
(
*
values
,
n
)
# adding n to existing tuple
query
=
sql
.
SQL
(
"
INSERT INTO STATIONS (id, lon, lat, country)
"
query
=
sql
.
SQL
(
"
INSERT INTO STATIONS (
station_
id, lon, lat, country)
"
"
VALUES ({id}, {lon}, {lat},
'
Germany
'
);
"
).
format
(
id
=
sql
.
Placeholder
(),
lon
=
sql
.
Placeholder
(),
lat
=
sql
.
Placeholder
())
# print(query.as_string(cursor))
# print(values)
...
...
@@ -117,7 +117,7 @@ def insert_empty_matrix_into_db(cursor):
def
create_matrix_data
(
cursor
,
amount_points
):
print
(
'
Calculating interpolation data for matrix
'
)
# start_time = time.time()
cursor
.
execute
(
"
SELECT id, lon, lat FROM stations WHERE file is NULL;
"
)
cursor
.
execute
(
"
SELECT
station_
id, lon, lat FROM stations WHERE file is NULL;
"
)
matrix_points
=
cursor
.
fetchall
()
update_data
=
[]
for
i
,
point
in
enumerate
(
matrix_points
):
...
...
@@ -133,7 +133,7 @@ def create_matrix_data(cursor, amount_points):
print
(
round
(
finished
),
end
=
"
% ...
"
)
print
(
''
,
end
=
'
100%, Done.
\n
'
)
query
=
sql
.
SQL
(
"""
UPDATE stations SET
"
%(year)s
"
= %(value)s WHERE id = %(id)s;
"""
)
query
=
sql
.
SQL
(
"""
UPDATE stations SET
"
%(year)s
"
= %(value)s WHERE
station_
id = %(id)s;
"""
)
print
(
'
Writing interpolation data to database
'
)
psycopg2
.
extras
.
execute_batch
(
cursor
,
query
,
update_data
)
# Multiple times faster than using execute() in a for loop
# print((time.time() - start_time), 'seconds')
...
...
@@ -143,16 +143,20 @@ def create_matrix_data(cursor, amount_points):
# Dumping all existing data from database. Inserting station data into database in bulk.
def
createInsertStatement
(
station_list
):
# create INSERT INTO table (columns) VALUES('%s',...)
station_list
.
columns
.
astype
(
str
)
#
station_list.columns.astype(str)
df_columns
=
list
(
station_list
)
station_list
=
station_list
.
round
(
decimals
=
3
)
columns
=
[
'"'
+
column
+
'"'
for
column
in
df_columns
]
columns
=
str
(
columns
).
replace
(
'
[
'
,
''
).
replace
(
'
]
'
,
''
).
replace
(
"'"
,
""
).
replace
(
'
\n
'
,
''
).
replace
(
'
'
,
''
)
values
=
"
VALUES({})
"
.
format
(
"
,
"
.
join
([
"
%s
"
for
_
in
df_columns
]))
df_columns
=
str
(
df_columns
).
strip
(
'
[]
'
)
station_list
=
station_list
.
round
(
decimals
=
3
)
# create INSERT INTO table (columns) VALUES('%s',...)
insert_stmt
=
"""
INSERT INTO {} ({}) {}
"""
.
format
(
'
stations
'
,
df_
columns
,
values
)
insert_stmt
=
"""
INSERT INTO {} ({}) {}
"""
.
format
(
'
stations
'
,
columns
,
values
)
return
insert_stmt
def
insert_data
(
station_list
,
cursor
):
print
(
'
Inserting data into database
'
)
if
len
(
station_list
)
>
0
:
...
...
@@ -177,7 +181,7 @@ def insert_data(station_list, cursor):
# create INSERT INTO table (columns) VALUES('%s',...)
#insert_stmt = """INSERT INTO {} ({}) {}""".format('stations', columns, values)
insert_stmt
=
createInsertStatement
(
station_list
)
print
(
insert_stmt
)
psycopg2
.
extras
.
execute_batch
(
cursor
,
insert_stmt
,
station_list
.
values
)
print
(
'
Done
'
)
...
...
This diff is collapsed.
Click to expand it.
dataacquisition/sandbox.py
+
2
−
9
View file @
2e3c5b83
import
time
top
=
101
for
i
in
range
(
0
,
top
):
time
.
sleep
(
0.1
)
if
i
%
10
==
0
:
finished
=
i
/
top
*
100
print
(
round
(
finished
),
end
=
"
% ...
"
)
query
=
"""'
lon
'
,
'
lat
'
,
'
file
'
,
'
country
'
,
'
2018
'
,
'
2017
'
,
'
2016
'
,
'
2015
'
,
'
2014
'
,
'
2013
'
,
'
2012
'
,
'
2011
'
,
'
2010
'
,
'
2009
'
,
'
2008
'
,
'
2007
'
,
'
2006
'
,
'
2005
'
,
'
2004
'
,
'
2003
'
,
'
2002
'
,
'
2001
'
,
'
2000
'
,
'
1999
'
,
'
1998
'
,
'
1997
'
,
'
1996
'
,
'
1995
'
,
'
1994
'
,
'
1993
'
,
'
1992
'
,
'
1991
'
,
'
1990
'
,
'
1989
'
,
'
1988
'
,
'
1987
'
,
'
1986
'
,
'
1985
'
,
'
1984
'
,
'
1983
'
,
'
1982
'
,
'
1981
'
,
'
1980
'
,
'
1979
'
,
'
1978
'
,
'
1977
'
,
'
1976
'
,
'
1975
'
,
'
1974
'
,
'
1973
'
,
'
1972
'
,
'
1971
'
,
'
1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'
)))
\ 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