-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwind_data_extract.py
54 lines (41 loc) · 2.21 KB
/
wind_data_extract.py
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
import csv
import os
import tables_queries as tq
# From the completed db, a set of data is retrieved and fetched to create the windroses
# The data is written into new CSV files and stored in the folder 'windrose_csv_data', which contains the final data used to create the windroses.
#Retrieving and fetching data from tables in function of the year
def yearly_windspeed_direction(database):
cursor = database.cursor()
cursor.execute(tq.YEARLY_WINDSPEED_DIRECTION)
return cursor.fetchall()
#Save fetched data into files
def yearly_data_to_csv(data, filename):
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, 'w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(['year', 'id_estacao', 'dicofre', 'concelho', 'id_direcc_vento', 'min_intensidade_vento', 'max_intensidade_vento', 'avg_intensidade_vento'])
writer.writerows(data)
#Retrieving and fetching data from tables in function of the month
def monthly_windspeed_direction(database):
cursor = database.cursor()
cursor.execute(tq.MONTHLY_WINDSPEED_DIRECTION)
return cursor.fetchall()
#Save fetched data into files
def monthly_data_to_csv(data, filename):
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, 'w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(['month', 'id_estacao', 'dicofre', 'concelho', 'id_direcc_vento', 'min_intensidade_vento', 'max_intensidade_vento', 'avg_intensidade_vento'])
writer.writerows(data)
#Retrieving and fetching data from tables in function of the month and year
def year_month_windspeed_direction(database):
cursor = database.cursor()
cursor.execute(tq.YEAR_MONTH_WINDSPEED_DIRECTION)
return cursor.fetchall()
#Save fetched data into files
def year_month_data_to_csv(data, filename):
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, 'w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(['month', 'year', 'id_estacao', 'dicofre', 'concelho', 'id_direcc_vento', 'min_intensidade_vento', 'max_intensidade_vento', 'avg_intensidade_vento'])
writer.writerows(data)