-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_solweig_jobs.py
90 lines (77 loc) · 3.37 KB
/
run_solweig_jobs.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
from solweig_run import run_solweig
from server.routing import make_walking_network_graph
from datetime import datetime, timezone, timedelta
import sys
import pandas as pd
import rasterio
import os
def main():
# if the arg is daily the run a daily job
if len(sys.argv) > 1:
print("running now")
if sys.argv[1] == 'daily':
run_solweig_daily(datetime.now(timezone.utc))
if sys.argv[1] == 'build_buffer':
# run the solweig job for the next 72 hours
run_solweig_buildup()
elif sys.argv[1] == "hourly":
# add an hour to today
today = datetime.now(timezone.utc) + timedelta(hours=1)
print(today)
# gets the timestamp but zeros out the minutes, seconds, and microseconds
today_ts = pd.Timestamp(today).replace(minute=0, second=0, microsecond=0)
run_solweig_hourly(today_ts)
else:
# Get the Date
today = datetime.now(timezone.utc)
print(today)
# gets the timestamp but zeros out the minutes, seconds, and microseconds
today_ts = pd.Timestamp(today).replace(minute=0, second=0, microsecond=0)
run_solweig_hourly(today_ts)
file_cleanup()
def run_solweig_buildup():
file_cleanup()
today = datetime.now(timezone.utc)
for day in range(3):
when_to_run = today + timedelta(days=day)
run_solweig_daily(when_to_run)
def run_solweig_daily(today):
# run solweig for all of the hours in the day
today = today.replace(minute=0, second=0, microsecond=0)
for hour in range(24):
# Get the Date
# gets the timestamp but zeros out the minutes, seconds, and microseconds
today_ts = pd.Timestamp(today).replace(hour=hour)
run_solweig_hourly(today_ts)
file_cleanup()
def run_solweig_hourly(today_ts, force_make=False):
should_run = force_make or today_ts >= datetime.now(timezone.utc)
if not should_run:
return
print("running solweig for {0}".format(today_ts))
timekey = today_ts.strftime('%Y-%m-%d-%H00')
# run solweig
run_solweig(today_ts)
# get the newly generated mean radiant temperature raster
# the timestamp is usually in UTC so we need to convert it to remove the extra info
mean_radiant_temp = rasterio.open('output/{0}_mrt.tif'.format(timekey))
print("finished making mrt for {0}".format(timekey))
# generate the graph
make_walking_network_graph(mean_radiant_temp, timekey)
def file_cleanup():
default_mrt_file_path = '2024-04-26-2100_mrt.tif'
default_graph_path = '2024-04-26-2100_graph_networked.graphml'
# clean up any existing files in the output directory
for file in os.listdir('output'):
# delete a file if the time stamp is before the current time
# get time stamp from file name
if file.count('_') >= 1:
file_time = datetime.strptime(file.split('_')[0], '%Y-%m-%d-%H00')
if (file.endswith('.tif') or file.endswith('.graphml')) and file_time < datetime.now().replace(minute=0, second=0, microsecond=0) - timedelta(hours=1):
# TODO DUMP TO LONG TERM STORAGE
if file != default_graph_path and file != default_mrt_file_path:
print('deleting {0}'.format(file), flush=True)
os.remove(os.path.join('output', file))
if __name__ == '__main__':
main()