I need to calculate the mean for TRODRY and TROWET for this text file in python , since I have many files of this text file my file format is as follows:
%=TRO 0.01 EMR 23:208:83100 EMR 21:005:00030 21:006:00000 P SA20
+TROP/DESCRIPTION
*_________KEYWORD_____________ __VALUE(S)______________________
ELEVATION CUTOFF ANGLE 7.5
SAMPLING INTERVAL 30
TROP MAPPING FUNCTION VMF1_HT
SOLUTION_FIELDS_1 TRODRY TROWET STDDEV TGNTOT STDDEV TGETOT STDDEV
-TROP/DESCRIPTION
+TROP/STA_COORDINATES
*SITE PT SOLN T __STA_X_____ __STA_Y_____ __STA_Z_____ SYSTEM REMRK
SA20 A 1 P 4613949.845 2914211.009 3290440.104 IGb14 CSRS-PPP
-TROP/STA_COORDINATES
+TROP/SOLUTION
*SITE ____EPOCH___ TRODRY TROWET STDDEV TGNTOT STDDEV TGETOT STDDEV
SA20 21:005:00030 2310.1 102.9 13.1 -0.2 1.1 -0.1 1.0
SA20 21:005:00060 2310.1 102.9 12.9 -0.2 1.1 -0.1 1.0
SA20 21:005:00090 2309.9 102.8 12.8 -0.2 1.1 -0.1 1.0
SA20 21:005:00120 2309.8 102.8 12.6 -0.2 1.1 -0.1 1.0
SA20 21:005:00150 2309.8 102.8 12.5 -0.2 1.1 -0.1 1.0
SA20 21:005:00180 2309.8 102.8 12.4 -0.2 1.1 -0.1 1.0
SA20 21:005:00210 2309.8 102.8 12.2 -0.2 1.1 -0.1 1.0
SA20 21:005:00240 2309.9 102.8 12.1 -0.2 1.1 -0.1 1.0
SA20 21:005:00270 2309.9 102.8 12.0 -0.2 1.1 -0.1 1.0
SA20 21:005:00300 2309.9 102.8 11.9 -0.2 1.1 -0.1 1.0
https://onlinegdb.com/F75_kNQUJ
from multiprocessing.dummy import Pool
from pathlib import Path
from statistics import mean
import csv
def worker(filename):
data = False
dry, wet = [], []
with open(filename) as file:
for line in file.read().splitlines():
if '____EPOCH___' in line: data = True ; continue
if not data: continue
d, w = list(map(float, line.split()[2:4]))
dry.append(d) ; wet.append(w)
return filename, mean(dry), mean(wet)
files = Path('.').glob('*.txt')
with open('results.csv', 'w', newline='') as f, Pool() as pool:
writer = csv.writer(f)
writer.writerow(['FILE', 'TRODRY', 'TRYWET'])
for result in pool.imap_unordered(worker, files):
writer.writerow(result)