forked from youtian95/MDOFModel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReadRecord.py
151 lines (125 loc) · 4.87 KB
/
ReadRecord.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
import os
def ReadRecord(inFilename, outFilename):
# search inFilename. If its filename extension is .at2, call ReadRecord_PEER.
# If it's .txt (1st data col is time, 2nd col is accel), call ReadRecord_TXT.
#
# Arguments:
# inFilename: no filename extension
# outFilename: '.dat' filename extension
if os.path.exists(inFilename + '.at2'):
dt, npts = ReadRecord_PEER (inFilename + '.at2', outFilename)
elif os.path.exists(inFilename + '.txt'):
dt, npts = ReadRecord_TXT (inFilename + '.txt', outFilename)
else:
print('ERROR: Cant find record file!')
dt = None
npts = None
return dt, npts
def ReadRecord_TXT (inFilename, outFilename):
inFileID = open(inFilename, 'r')
outFileID = open(outFilename, 'w')
time_1 = 0.0
time_2 = 0.0
npts=0
for line in inFileID:
if line == '\n':
continue
else:
words = str.replace(line,',',' ').split()
lengthLine = len(words)
if lengthLine == 2:
npts+=1
if npts==1:
time_1=float(words[0])
elif npts==2:
time_2=float(words[0])
outFileID.write(words[1])
outFileID.write('\n')
dt = time_2 - time_1
inFileID.close()
outFileID.close()
return dt, npts
def ReadRecord_PEER (inFilename, outFilename):
# ReadRecord.py
# ------------------------------------------------------------------------------------------------------------
#
# Written: minjie
# Date: May 2016
# A procedure which parses a ground motion record from the PEER
# strong motion database by finding dt in the record header, then
# echoing data values to the output file.
#
# Formal arguments
# inFilename -- file which contains PEER strong motion record
# outFilename -- file to be written in format G3 can read
# Return values
# dt -- time step determined from file header
# nPts -- number of data points from file header
#
# Assumptions
# The header in the PEER record is, e.g., formatted as 1 of following:
# 1) new PGA database
# PACIFIC ENGINEERING AND ANALYSIS STRONG-MOTION DATA
# IMPERIAL VALLEY 10/15/79 2319, EL CENTRO ARRAY 6, 230
# ACCELERATION TIME HISTORY IN UNITS OF G
# 3930 0.00500 NPTS, DT
# 2) old SMD database
# PACIFIC ENGINEERING AND ANALYSIS STRONG-MOTION DATA
# IMPERIAL VALLEY 10/15/79 2319, EL CENTRO ARRAY 6, 230
# ACCELERATION TIME HISTORY IN UNITS OF G
# NPTS= 3930, DT= .00500 SEC
dt = 0.0
npts = 0
# Open the input file and catch the error if it can't be read
inFileID = open(inFilename, 'r')
# Open output file for writing
outFileID = open(outFilename, 'w')
# Flag indicating dt is found and that ground motion
# values should be read -- ASSUMES dt is on last line
# of header!!!
flag = 0
# Look at each line in the file
for line in inFileID:
if line == '\n':
# Blank line --> do nothing
continue
elif flag == 1:
# Echo ground motion values to output file
outFileID.write(line)
else:
# Search header lines for dt
words = line.split()
lengthLine = len(words)
if lengthLine >= 4:
if words[0] == 'NPTS=':
# old SMD format
for word in words:
if word != '':
# Read in the time step
if flag == 1:
dt = float(word)
break
if flag == 2:
npts = int(word.strip(','))
flag = 0
# Find the desired token and set the flag
if word == 'DT=' or word == 'dt':
flag = 1
if word == 'NPTS=':
flag = 2
elif words[-1] == 'DT':
# new NGA format
count = 0
for word in words:
if word != '':
if count == 0:
npts = int(word)
elif count == 1:
dt = float(word)
elif word == 'DT':
flag = 1
break
count += 1
inFileID.close()
outFileID.close()
return dt, npts