-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinputer.py
163 lines (152 loc) · 6.17 KB
/
inputer.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import os
# here = os.path.dirname(os.path.realpath(__file__))
is_cron = False
fmt = '%d/%m/%Y-%H:%M'
################################# LOGGING ####################################
import logging
import log_help
log_file = '.'.join( __file__.split('/')[-1].split('.')[:-1] ) + '.log'
# log_file = here+'/'+'.'.join( __file__.split('/')[-1].split('.')[:-1] ) + '.log'
lv = logging.DEBUG
logging.basicConfig(level=lv,
format='%(asctime)s %(name)s:%(levelname)s - %(message)s',
datefmt='%Y/%m/%d-%H:%M',
filename = log_file, filemode='w')
LG = logging.getLogger('main')
if not is_cron: log_help.screen_handler(LG, lv=lv)
LG.info(f'Starting: {__file__}')
##############################################################################
import common
import datetime as dt
fini = 'config.ini'
R = common.load(fini)
################
# namelist.wps #
################
fname = f'{R.domain_folder}/namelist.wps'
with open(fname,'r') as f:
all_text = []
for line in f.read().strip().splitlines():
if line.strip().startswith('max_dom'):
# line = f' max_dom = {len(R.domains)},' #XXX do not edit in namelists
Ndomains = int(line.strip().split('=')[-1].strip().replace(',',''))
elif line.strip().startswith('start_date'):
line = f"start_date = "
for _ in range(Ndomains): #R.domains:
line += f"\'{R.start_date.strftime('%Y-%m-%d_%H:%M:%S')}\', "
line = ' ' + line.strip()
elif line.strip().startswith('end_date'):
line = f"end_date = "
for _ in range(Ndomains): #R.domains:
line += f"\'{R.end_date.strftime('%Y-%m-%d_%H:%M:%S')}\', "
line = ' ' + line.strip()
all_text.append(line)
with open(f'namelist.wps','w') as f:
f.write('\n'.join(all_text)+'\n\n')
################## XXX nooooo no no no no no no no
# namelist.input # XXX for fuck's sake!!! no!!!!
################## XXX rewrite this
duration = R.end_date - R.start_date
duration = duration.total_seconds()
# time_control
run_days = int(duration/3600/24)
duration -= run_days * 3600 * 24
run_hours = int(duration/3600)
duration -= run_hours * 3600
run_minutes = int(duration/60)
duration -= run_minutes * 60
run_seconds = int(duration)
start_year = R.start_date.year
start_month = R.start_date.month
start_day = R.start_date.day
start_hour = R.start_date.hour
start_minute = R.start_date.minute
start_second = R.start_date.second
end_year = R.end_date.year
end_month = R.end_date.month
end_day = R.end_date.day
end_hour = R.end_date.hour
end_minute = R.end_date.minute
end_second = R.end_date.second
fname = f'{R.domain_folder}/namelist.input'
with open(fname,'r') as f:
all_text = []
for line in f.read().strip().splitlines():
# Ignored for real cases?
# https://www2.mmm.ucar.edu/wrf/users/namelist_best_prac_wrf.html#run_days
if line.strip().startswith('history_outname'):
line = "history_outname = "
line += f"'{R.output_folder}/wrfout_d<domain>_<date>',"
elif line.strip().startswith('run_days'):
line = f'run_days = {run_days},'
elif line.strip().startswith('run_hours'):
line = f'run_hours = {run_hours},'
elif line.strip().startswith('run_minutes'):
line = f'run_minutes = {run_minutes},'
elif line.strip().startswith('run_seconds'):
line = f'run_seconds = {run_seconds},'
if line.strip().startswith('start_year'):
line = f'start_year = '
for _ in range(Ndomains): #R.domains:
line += f'{start_year}, '
line = line.strip()
elif line.strip().startswith('start_month'):
line = f'start_month = '
for _ in range(Ndomains): #R.domains:
line += f'{start_month:02d}, '
line = line.strip()
elif line.strip().startswith('start_day'):
line = f'start_day = '
for _ in range(Ndomains): #R.domains:
line += f'{start_day:02d}, '
line = line.strip()
elif line.strip().startswith('start_hour'):
line = f'start_hour = '
for _ in range(Ndomains): #R.domains:
line += f'{start_hour:02d}, '
line = line.strip()
elif line.strip().startswith('start_minute'):
line = f'start_minute = '
for _ in range(Ndomains): #R.domains:
line += f'{start_minute:02d}, '
line = line.strip()
elif line.strip().startswith('start_second'):
line = f'start_second = '
for _ in range(Ndomains): #R.domains:
line += f'{start_second:02d}, '
line = line.strip()
elif line.strip().startswith('end_year'):
line = f'end_year = '
for _ in range(Ndomains): #R.domains:
line += f'{end_year}, '
line = line.strip()
elif line.strip().startswith('end_month'):
line = f'end_month = '
for _ in range(Ndomains): #R.domains:
line += f'{end_month:02d}, '
line = line.strip()
elif line.strip().startswith('end_day'):
line = f'end_day = '
for _ in range(Ndomains): #R.domains:
line += f'{end_day:02d}, '
line = line.strip()
elif line.strip().startswith('end_hour'):
line = f'end_hour = '
for _ in range(Ndomains): #R.domains:
line += f'{end_hour:02d}, '
line = line.strip()
elif line.strip().startswith('end_minute'):
line = f'end_minute = '
for _ in range(Ndomains): #R.domains:
line += f'{end_minute:02d}, '
line = line.strip()
elif line.strip().startswith('end_second'):
line = f'end_second = '
for _ in range(Ndomains): #R.domains:
line += f'{end_second:02d}, '
line = line.strip()
all_text.append(line)
with open(f'namelist.input','w') as f:
f.write('\n'.join(all_text)+'\n\n')