-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform_forecast_data.py
39 lines (31 loc) · 1.3 KB
/
transform_forecast_data.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
import csv
from datetime import datetime, timedelta
from io import StringIO
def transform(data):
# Extract parameters from data
parameters = data["SiteRep"]["Wx"]["Param"]
parameter_names = [
f"{param['$']} - {param['units']}" for param in parameters
]
# Get the current datetime
current_datetime = datetime.now()
# Extract periods from data
location = data["SiteRep"]["DV"]["Location"]
periods = location["Period"]
# Define a generator function to yield CSV rows
def generate_csv_rows():
yield ["Datetime"] + parameter_names
for period in periods:
period_date = datetime.strptime(period["value"], "%Y-%m-%dZ")
for rep in period["Rep"]:
period_datetime = period_date + timedelta(minutes=int(rep["$"]))
if period_datetime <= current_datetime < period_datetime + timedelta(hours=3):
values = [rep.get(param["name"]) for param in parameters]
yield [period_datetime.strftime("%Y-%m-%d %H:%M:%S")] + values
# Use the generator to create an in-memory string buffer
csv_buffer = StringIO()
writer = csv.writer(csv_buffer)
writer.writerows(generate_csv_rows())
# Get the CSV data as a string
csv_data = csv_buffer.getvalue()
return csv_data