-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturbineOutputAverage.py
176 lines (125 loc) · 7.14 KB
/
turbineOutputAverage.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
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/python3
import logging
LEVEL = logging.INFO
logger = logging.getLogger(__name__)
import argparse
import gzip
import numpy as np
import constants as const
import utils
################################################################################
def turbineOutputAverage(casename,times_to_report=None,starttime=300,
blade_sample_to_report=27,overwrite=False):
"""Reads powerRotor from sowfatools directory, calculates a running average
and reports at requested times.
Written for Python 3.12, SOWFA 2.4.x for sowfatools
Jeffrey Johnston [email protected] May 2024
"""
casedir = const.CASES_DIR / casename
readdir = casedir / const.TURBINEOUTPUT_DIR
if not readdir.is_dir():
logger.warning(f'{readdir.stem} directory does not exist. '
f'Skipping case {casename}.')
return
sowfatoolsdir = casedir / const.SOWFATOOLS_DIR
logfilename = 'log.turbineOutputAverage'
utils.configure_function_logger(sowfatoolsdir/logfilename, level=LEVEL)
############################################################################
logger.info(f'Calculating Average turbineOutput for case {casename}')
logger.info('')
writedir = casedir / const.SOWFATOOLS_DIR / 'turbineOutputAveraged'
utils.create_directory(writedir)
quantities,turbines,blades = utils.parse_turbineOutput_files(readdir)
for quantity in quantities:
logger.info(f'Processing {casename}, {quantity}')
for turbine in turbines:
logger.info(f'{casename}, {quantity}, turbine{turbine}')
####################################################################
if quantity in const.TURBINE_QUANTITIES:
writefile = writedir / (f'{casename}_{quantity}_'
f'turbine{turbine}_averaged.gz')
if (writefile.exists() and overwrite is False
and times_to_report is None):
logger.warning(f'{writefile.name} already exists. '
f'Skippping.')
logger.warning('')
continue
readfile = (readdir
/ f'{casename}_{quantity}_turbine{turbine}.gz')
logger.debug(f'Reading {readfile}')
data = np.genfromtxt(readfile)
with gzip.open(readfile,mode='rt') as f:
header = f.readline()
header = header.removeprefix('# ').removesuffix('\n')
if 'start_idx' not in locals():
start_idx = np.argmin(np.abs((data[:,0]-data[0,0])
-starttime))
data[:start_idx,2] = np.nan
data[start_idx:,2] = \
utils.calculate_moving_average(data[start_idx:,:],2,1)
if (not writefile.exists() or overwrite is True):
np.savetxt(writefile,data,fmt='%.11e',header=header)
else:
logger.warning(f'{writefile.name} already exists. '
f'Not overwriting.')
if times_to_report is not None:
if 'time_idx' not in locals():
time_idx = utils.get_time_idx(data, times_to_report)
data_to_report = data[time_idx,2]
####################################################################
elif quantity in const.BLADE_QUANTITIES:
for blade in blades:
writefile = writedir / (f'{casename}_{quantity}_'
f'turbine{turbine}_blade{blade}_'
f'averaged.gz')
if (writefile.exists() and overwrite is False
and times_to_report is None):
logger.warning(f'{writefile.name} already exists. '
f'Skippping.')
logger.warning('')
continue
readfile = readdir / (f'{casename}_{quantity}_'
f'turbine{turbine}_blade{blade}.gz')
logger.debug(f'Reading {readfile}')
data = np.genfromtxt(readfile)
with gzip.open(readfile,mode='rt') as f:
header = f.readline()
header = header.removeprefix('# ').removesuffix('\n')
if 'start_idx' not in locals():
start_idx = np.argmin(np.abs((data[:,0]-data[0,0])
-starttime))
for i in range(2,data.shape[1]):
data[:start_idx,i] = np.nan
data[start_idx:,i] = \
utils.calculate_moving_average(data[start_idx:,:],
i,1)
if (not writefile.exists() or overwrite is True):
np.savetxt(writefile,data,fmt='%.11e',header=header)
else:
logger.warning(f'{writefile.name} already exists. '
f'Not overwriting.')
if times_to_report is not None:
if 'time_idx' not in locals():
time_idx = utils.get_time_idx(data, times_to_report)
data_to_report = data[time_idx,blade_sample_to_report]
####################################################################
if times_to_report is not None:
for i,time in enumerate(times_to_report):
logger.info(f'Average after {time} s is '
f'{data_to_report[i]:.5e}')
logger.info('')
logger.info(f'Finished case {casename}')
logger.info('')
################################################################################
if __name__ == '__main__':
utils.configure_root_logger(level=LEVEL)
description = "Calculate Running Average for turbineOutput"""
parser = argparse.ArgumentParser(description=description)
parser.add_argument('cases', help='list of cases to perform analysis for',
nargs='+')
parser.add_argument("-t", "--times", help="What times to report",
nargs='*', type=int)
args = parser.parse_args()
logger.debug(f'Parsed the command line arguments: {args}')
for casename in args.cases:
turbineOutputAverage(casename, args.times)