Processing PEER NGA motion database using Python.
Hi all,
I am currently building a source code library (using python 3.6) particularly for earthquake engineering related stuff. This probably not the most efficient code for converting NGA flat file (.AT2) into a 1-dimensional data array but hopefully it can help. As we know, the raw file provided by Pacific Earthquake Engineering Research Center (PEER) website, might be tricky to be processed using plotter such as Excel, MATLAB, etc. The file has a dimension of (n-rows x 5 column) as shown below:
The code provided below probably can make our life easier in doing that task.
"""
@author: Daniel Hutabarat - UC Berkeley, 2017
"""
import numpy as np
import os
import matplotlib.pyplot as plt
def processNGAfile(filepath, scalefactor=None):
'''
This function process acceleration history for NGA data file (.AT2 format)
to a single column value and return the total number of data points and
time iterval of the recording.
Parameters:
------------
filepath : string (location and name of the file)
scalefactor : float (Optional) - multiplier factor that is applied to each
component in acceleration array.
Output:
------------
desc: Description of the earthquake (e.g., name, year, etc)
npts: total number of recorded points (acceleration data)
dt: time interval of recorded points
time: array (n x 1) - time array, same length with npts
inp_acc: array (n x 1) - acceleration array, same length with time
unit usually in (g) unless stated as other.
Example: (plot time vs acceleration)
filepath = os.path.join(os.getcwd(),'motion_1')
desc, npts, dt, time, inp_acc = processNGAfile (filepath)
plt.plot(time,inp_acc)
'''
try:
if not scalefactor:
scalefactor = 1.0
with open(filepath,'r') as f:
content = f.readlines()
counter = 0
desc, row4Val, acc_data = "","",[]
for x in content:
if counter == 1:
desc = x
elif counter == 3:
row4Val = x
if row4Val[0][0] == 'N':
val = row4Val.split()
npts = float(val[(val.index('NPTS='))+1].rstrip(','))
dt = float(val[(val.index('DT='))+1])
else:
val = row4Val.split()
npts = float(val[0])
dt = float(val[1])
elif counter > 3:
data = str(x).split()
for value in data:
a = float(value) * scalefactor
acc_data.append(a)
inp_acc = np.asarray(acc_data)
time = []
for i in range (0,len(acc_data)):
t = i * dt
time.append(t)
counter = counter + 1
return desc, npts, dt, time, inp_acc
except IOError:
print("processMotion FAILED!: File is not in the directory")
In order to run this code, you have to provide an acceleration file and you may type this:
Once all the variable is stored, you can plot it using this command:
And the results will look similarly like this:
You can extract the numerical data of time and acceleration array (time and inp_acc variable in this example) for further task such as computing velocity & displacement time history, arias intensity, response spectra etc. I hope it is useful for your research or engineering practice and please feel free to comment on my code in case you have a better, simpler and of course faster algorithm.
ENJOY and GRACIAS!
Daniel Hutabarat
Bravo!!!!
Thank you Daniel for sharing this!
Thanks, Daniel; It works great!
Thanks for the code, I have some question about how can I make it so I can see the values ordered in 1 column and save all those values in one file.
Thanks, works fine