(fileName)
| 41 | import re |
| 42 | |
| 43 | def read(fileName): |
| 44 | data = [] |
| 45 | coordinates = [float('nan')] * 3 |
| 46 | coordComment = re.compile('#\s*x(\d)\s+([0-9\.eE\+\-]+)') |
| 47 | variables = '' |
| 48 | p_0 = 0. |
| 49 | t_s = 0. |
| 50 | t_d = 0. |
| 51 | f = open(fileName) |
| 52 | for row in f: |
| 53 | row = row.strip() |
| 54 | if not row[0].isalpha() and not row[0] == '#': |
| 55 | values = row.split() |
| 56 | data.append([float(numeric_string) for numeric_string in values]) |
| 57 | elif row[0] == '#': |
| 58 | match = coordComment.match(row) |
| 59 | if match: |
| 60 | coordinates[int(match.group(1))-1] = float(match.group(2)) |
| 61 | elif row[2:5] == 'P_0': |
| 62 | p_0 = float(row[5:]) |
| 63 | elif row[2:5] == 'T_s': |
| 64 | t_s = float(row[5:]) |
| 65 | elif row[2:5] == 'T_d': |
| 66 | t_d = float(row[5:]) |
| 67 | elif row.startswith('VARIABLES'): |
| 68 | row = row.split('=') |
| 69 | variables = row[1].replace('"', '').split(',') |
| 70 | variables = [v.strip() for v in variables] |
| 71 | f.close() |
| 72 | if len(data) == 0: |
| 73 | return None |
| 74 | if 'P_n' in variables: |
| 75 | indexP_n = variables.index('P_n') |
| 76 | data = [d[0:indexP_n] + [d[indexP_n] + p_0] + d[indexP_n+1:] for d in data] |
| 77 | if 'T_s' in variables: |
| 78 | indexT_s = variables.index('T_s') |
| 79 | data = [d[0:indexT_s] + [d[indexT_s] + t_s] + d[indexT_s+1:] for d in data] |
| 80 | if 'T_d' in variables: |
| 81 | indexT_d = variables.index('T_d') |
| 82 | data = [d[0:indexT_d] + [d[indexT_d] + t_d] + d[indexT_d+1:] for d in data] |
| 83 | return Waveform.Waveform(variables, data, coordinates) |
no test coverage detected