-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDoc_Reader.py
More file actions
280 lines (215 loc) · 10.2 KB
/
Copy pathDoc_Reader.py
File metadata and controls
280 lines (215 loc) · 10.2 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 4 20:44:28 2021
"""
import os
import csv
import datetime
#import numpy as np
import pandas as pd
import Utils
# -----------------------------------------------------------------------------
# Reader class for Docs data files
# -----------------------------------------------------------------------------
class Docs_File():
"""Read Docs data files, making corrections and adjustments along the way"""
# Used to remap CSV data file labels into something more friendly for analysis
label_remap = \
{
'timeStamp': 'secTimeStamp',
'Pfwd': 'secPFwd',
'PfwdSmoothed': 'secPFwdSmoothed',
'P45': 'secP45',
'P45Smoothed': 'secP45Smoothed',
'PStatic': 'secPStatic',
'Palt': 'secPAlt',
'IAS': 'secIAS',
'AngleofAttack': 'secAngleOfAttack',
'flapsPos': 'secFlapsPos',
'DataMark': 'secDataMark',
'OAT': 'secOAT',
'TAS': 'secTAS',
'imuTemp': 'secIMUTemp',
'VerticalG': 'secVerticalG',
'LateralG': 'secLateralG',
'ForwardG': 'secForwardG',
'RollRate': 'secRollRate',
'PitchRate': 'secPitchRate',
'YawRate': 'secYawRate',
'Pitch': 'secPitch',
'Roll': 'secRoll',
'EarthVerticalG': 'secEarthVerticalG',
'FlightPath': 'secFlightPath',
'VSI': 'secVSI',
'Altitude': 'secAltitude',
'efisPalt': 'efisPAlt',
}
def __init__(self, filename):
self.linenum = 0
self.fh = open(filename, 'rt', newline='')
def __iter__(self):
for csv_line in self.fh:
self.linenum += 1
csv_line = csv_line.rstrip()
if self.linenum == 1:
csv_line = self.fix_labels(csv_line)
if self.linenum == 2:
csv_line = self.fh.__next__()
yield csv_line
def fix_labels(self, csv_line):
"""Pound the csv label line into something usable"""
# First split it into components
labels = csv_line.split(",")
# Original set of labels
for label_idx in range(0, len(labels)):
try:
# Remap labels
labels[label_idx] = labels[label_idx].strip()
labels[label_idx] = self.label_remap[labels[label_idx]]
# If a label doesn't start with "efis" then it gets a "docs" stuck on the front
# labels[label_idx] = labels[label_idx].strip()
# if labels[label_idx][:4] != "efis":
# labels[label_idx] = "docs" + labels[label_idx][0].upper() + labels[label_idx][1:]
# Catch any remapping errors
except KeyError as e:
# print("Docs label remap error - {} - {}".format(label_idx, labels[label_idx]))
pass
# Now put it back together into a CSV string
csv_line = ",".join(labels)
return csv_line
# ---------------------------------------------------------------------------
# Utility routines for reading and parsing Docs data files
# ---------------------------------------------------------------------------
def make_dataframe(doc_filenames):
# Note: time correction is in seconds
# A couple of lists to accumulate data
doc_data_array_master = []
index_time_master = []
# Make sure these passed parameters are lists
#if isinstance(doc_filenames, tuple):
doc_filenames_list = list(doc_filenames)
#if isinstance(doc_filenames, str):
# doc_filenames_list = [doc_filenames,]
#if isinstance(time_corrections, tuple):
# time_corrections_list = list(time_corrections)
#if isinstance(time_corrections, int) or isinstance(time_corrections, float):
# time_corrections_list = [time_corrections,]
doc_file_num = 1
for file_idx in range(len(doc_filenames_list)):
# Get the current file name and time correction
(doc_filename, time_correction) = doc_filenames_list[file_idx]
# Read the CSV file
# -----------------
doc_data_array = []
doc_file = Docs_File(doc_filename)
doc_reader = csv.DictReader(doc_file)
doc_reader.__next__()
try:
for doc_row in doc_reader:
# Convert strings to numbers
if convert_doc_row(doc_row) == False:
print("Format error in {}, line {}".format(os.path.basename(doc_filename), doc_reader.line_num))
continue
# Do some data fixes, OK if it throws an excepton
try:
# Try getting rid of the cycle timer part
(time_trimmed, cycle_counter) = doc_row["efisTime"].split(".")
doc_row["efisTime"] = time_trimmed
# Take out columns we don't want for now
del doc_row["efisTAS"]
del doc_row["efisOAT"]
del doc_row["efisFuelRemaining"]
del doc_row["efisFuelFlow"]
del doc_row["efisMAP"]
del doc_row["efisRPM"]
del doc_row["efisPercentPower"]
del doc_row["efisMagHeading"]
del doc_row["efisAge"]
except:
continue
# Add the file number
doc_row["secFileNum"] = doc_file_num
# We got to here so store the data
doc_data_array.append(doc_row)
# Catch any other read errors
except csv.Error as e:
sys.exit('file {}, line {}: {}'.format(os.path.basename(doc_filename), doc_reader.line_num, e))
# Make a time index value for each row
# ------------------------------------
# Check the goodness of the timeStamp
#num_rows = len(doc_data_array)
#timestamp_span = int(doc_data_array[len(doc_data_array)-1]["secTimeStamp"]) - \
# int(doc_data_array[0] ["secTimeStamp"])
#if num_rows != (timestamp_span / 20) + 1:
# print("Warning - non-continuous timestamps")
# Time in the middle of the file is probably good so make a reference from that
middle_index_ref = int(len(doc_data_array) / 2)
mid_time_string_ref = doc_data_array[middle_index_ref]["efisTime"]
(utc_hours_ref, utc_minutes_ref, utc_seconds_ref) = mid_time_string_ref.split(":")
# Look for a line where the integer seconds increments.
for middle_index in range(middle_index_ref+1, middle_index_ref+100):
mid_time_string = doc_data_array[middle_index]["efisTime"]
(utc_hours, utc_minutes, utc_seconds) = mid_time_string.split(":")
if int(utc_seconds_ref) != int(utc_seconds):
break
mid_timestamp = int(doc_data_array[middle_index]["secTimeStamp"])
mid_time_utc = Utils.make_utc_from_str(mid_time_string)
# Make a UTC Time value to use as an index
array_idx = 0
index_time = []
while array_idx < len(doc_data_array):
try:
# Check for bad data
if doc_data_array[array_idx]["efisTime"] is None:
raise ValueError("Bad efisTime")
# Make values for the data timestamp and UTC time
data_timestamp = int(doc_data_array[array_idx]["secTimeStamp"])
# Calculate and store a time index value which is milliseconds since midnight
data_time_utc = mid_time_utc + (data_timestamp - mid_timestamp)
data_time_utc += time_correction * 1000
data_time_utc = round(float(data_time_utc) / 20.0) * 20
index_time.append(data_time_utc)
array_idx += 1
except Exception as error:
print("Error '" + repr(error) + "' at line " + str(array_idx))
doc_data_array.pop(array_idx)
doc_file_num += 1
# Append the new data to the end of the master data arrays
doc_data_array_master += doc_data_array
index_time_master += index_time
# Add data to a pandas dataframe of flight test data
# ---------------------------------------------------
doc_dataframe = pd.DataFrame(doc_data_array_master, index_time_master)
# doc_dataframe.columns = doc_column_names
doc_dups = doc_dataframe.index.duplicated()
doc_dataframe = doc_dataframe.loc[~doc_dups,:]
return doc_dataframe
# ---------------------------------------------------------------------------
def convert_doc_row(doc_row):
success = True
try:
# Convert most (but not all) values from strings to numbers
for doc_key in doc_row.keys():
if doc_key == "efisTime":
pass
elif doc_key == "secTimeStamp" or \
doc_key == "efisAge":
doc_row[doc_key] = int(doc_row[doc_key])
else:
doc_row[doc_key] = float(doc_row[doc_key])
except:
print("Error converting '{}'".format(doc_key))
success = False
return success
# =============================================================================
if __name__=='__main__':
doc__data_array = []
print("Read Docs Data...")
test_data_dir = "G:/.shortcut-targets-by-id/1JEHdf2zPb_F1R0v-s94Ia2RZNGjPCk2n/Flight Test Data/RV-4 Data/2022-05-11 Data/"
docs_filename = (test_data_dir + "11 May 22 Docs Box Data/log_4.csv", \
test_data_dir + "11 May 22 Docs Box Data/log_6.csv")
docs_correction = (0.0, 0.0)
#docs_filename = test_data_dir + "11 May 22 Docs Box Data/log_4.csv"
#docs_correction = 0.0
doc_dataframe = make_dataframe(docs_filename, docs_correction)
print("Done!")