<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Yasmine ACHEMINE on Medium]]></title>
        <description><![CDATA[Stories by Yasmine ACHEMINE on Medium]]></description>
        <link>https://medium.com/@achemine.yasmine?source=rss-4f0f5bec9f0a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*bt8yudSlFAl5l-Twj3wdQw.jpeg</url>
            <title>Stories by Yasmine ACHEMINE on Medium</title>
            <link>https://medium.com/@achemine.yasmine?source=rss-4f0f5bec9f0a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 01:52:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@achemine.yasmine/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Free Tool for Easy Conversion of Kinemetrics EVT Files to MATLAB Format]]></title>
            <link>https://medium.com/@achemine.yasmine/free-tool-for-easy-conversion-of-kinemetrics-evt-files-to-matlab-format-547888d9b045?source=rss-4f0f5bec9f0a------2</link>
            <guid isPermaLink="false">https://medium.com/p/547888d9b045</guid>
            <category><![CDATA[seismology]]></category>
            <category><![CDATA[obspy]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[matlab]]></category>
            <category><![CDATA[scipy]]></category>
            <dc:creator><![CDATA[Yasmine ACHEMINE]]></dc:creator>
            <pubDate>Fri, 27 Oct 2023 18:41:27 GMT</pubDate>
            <atom:updated>2023-10-27T18:41:27.817Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/534/1*1zVOh-RyS_ZA8JVheN5wBA.png" /><figcaption>Created using draw.io</figcaption></figure><p>In the realm of seismology, the ability to process and analyze seismic data is of utmost importance. Often, this involves converting data from one format to another.</p><p>In this article, I will take you through a step-by-step tutorial on how to write a Python script that converts Kinemetrics EVT files to MATLAB format using the ObsPy and SciPy libraries.</p><p>Before we dive into the code, let’s understand the need for such a conversion. EVT files are used for recording seismic data. Still, for in-depth analysis or compatibility with specific tools, converting them to MATLAB .mformat is essential.</p><p>If you’re interested in learning how to convert an EVT file to Sac format, I have an article that explains this: <a href="https://medium.com/@achemine.yasmine/a-step-by-step-guide-to-converting-kinemetrics-evt-files-to-sac-format-using-obspy-5473a7360a6c">A Step-by-Step Guide to Converting Kinemetrics EVT Files to SAC Format using ObsPy</a>.</p><p>If you’re a total beginner in using EVT files or Obspy and want to learn the basics, check out this article: <a href="https://medium.com/@achemine.yasmine/reading-modifying-and-converting-seismic-event-data-using-obspy-cc94c00badc4">Reading, Modifying, and Converting Seismic Event Data Using ObsPy</a>.</p><h3>Step 1: Setting the Stage</h3><p>This script is written in the <a href="https://www.python.org/">Python</a> programming language, and in addition to that, we need to install two libraries: <a href="https://docs.obspy.org/index.html">Obspy</a> and <a href="https://scipy.org/install/">Scipy</a>.</p><p>If you don’t already have them on your system, you can install them using pip:</p><pre>pip install obspy<br>python -m pip install scipy</pre><p>Or if you prefer using conda:</p><pre>conda install -c conda-forge obspy<br>conda install scipy</pre><h4>1.1.Importing Necessary Libraries</h4><p>Now that we have what we need, we can begin coding.</p><p>We start by importing three essential libraries:</p><pre>from obspy import read<br>from scipy.io import savemat<br>import os</pre><p><a href="https://docs.obspy.org/"><strong>ObsPy</strong></a> (from obspy import read): ObsPy is a powerful Python toolbox designed for seismology and seismological observatories. It provides various functionalities for working with seismic data, including reading, processing, and analyzing it.</p><ul><li>Theread function from ObsPy is used to read seismic data from different file formats and convert it into ObsPy&#39;s internal data structure, making it easier to work with.</li></ul><p><a href="https://scipy.org/"><strong>scipy.io</strong></a> (from scipy.io import savemat): Thescipy.io module is part of the SciPy library, which is a fundamental library for scientific computing in Python.</p><ul><li>We specifically import the savemat function from scipy.io to save data in MATLAB file format.</li></ul><p><a href="https://docs.python.org/fr/3/library/os.html"><strong>os</strong></a> (import os): Theos module provides a way to interact with the operating system, allowing you to perform various file and directory operations.</p><p>Having these libraries properly imported sets the stage for the next steps.</p><h4>1.2. A Friendly Welcome</h4><pre>print(&quot;*&quot; * len(&quot;Welcome to the Evt_MatLab converter&quot;))<br>print(&quot;Welcome to the Evt_MatLab converter&quot;)<br>print(&quot;*&quot; * len(&quot;Welcome to the Evt_MatLab converter&quot;))<br><br>print(&quot;\n Before we begin, make sure your data is in the same folder (or one of its subfolders) as the program.&quot;)</pre><p>The script begins with a user-friendly interface, greeting the user with welcome messages.</p><p>It also instructs the user to ensure their seismic data is in the same directory as the program.</p><p>This step is completely optional; you’re under no obligation to add it.</p><h3>Step 2: The Conversion Process</h3><p>The heart of the script lies in the conversion process. Let’s break down the essential steps.</p><h4>2.1. File Location</h4><pre>event_name = input(&quot;\nEnter the name of the file to convert: &quot;).replace(&quot; &quot;, &quot;&quot;)</pre><p>Theinput method prompts the user to provide the name of the EVT file they want to convert and collects the input.</p><p>Thereplace(&quot; &quot;, &quot;&quot;) method is applied to the user&#39;s input to remove any spaces from the filename. This step helps ensure that the user&#39;s input doesn&#39;t contain spaces that might cause issues when searching for the file.</p><p>The user, human as they must be, can give either the short name of the file (just the name) or the full name, which includes the path to the file.</p><p>In addition, we will take into account the fact that the file can either be in the same directory as the script or in one of its subdirectories.</p><pre>if not os.path.exists(event_name):<br>    start_directory = os.getcwd()  # Get the current working directory&#39;s path<br><br>    # Search for the file in all subdirectories<br>    for root, _, files in os.walk(start_directory):<br>        if event_name in files:<br>            event_name = os.path.join(root, event_name)<br>            event_not_found = False<br>            break<br>        else:<br>            event_not_found = True<br><br>    if event_not_found:<br>        print(f&quot;WARNING: &#39;{event_name}&#39; does not exist in &#39;{start_directory}&#39; or its subdirectories.&quot;)</pre><p>Thisif statement checks if the specified file doesn&#39;t exist in the current working directory.</p><p>We retrieve the current working directory’s path using os.getcwd(). This path serves as the starting point for the search for the specified file.</p><p>We use afor loop to traverse through all the subdirectories of the current working directory and its subdirectories. This is done using the os.walk() function, which returns directory paths, subdirectories, and filenames.</p><p>Within the loop, we check if the specified event_name matches any of the filenames (files) in the current directory (root).</p><p>If a matching file is found, we update the event_name to the full path of the file, including its directory. This is done using os.path.join().</p><p>We set the event_not_found flag to False to indicate that the file has been found.</p><p>In else: if the loop completes without finding the file, we set event_not_found to True.</p><p>Finally, if event_not_found is True, we print a warning message to inform the user that the specified file doesn&#39;t exist in the current directory or its subdirectories.</p><h4>2.2. Reading and Preprocessing</h4><pre>if os.path.exists(event_name):<br>    # reading the recording<br>    st = read(event_name, format=&quot;kinemetrics_evt&quot;).detrend()<br><br>    for tr in st:<br>        # Converting the data fron Conts to acceleration (cm/s²)<br>        tr.data = tr.data * tr.stats.calib * 100<br><br>        tr.stats.network = &quot;...&quot; # Replace as you please<br>        tr.stats.station = tr.stats.kinemetrics_evt.stnid<br>        tr.stats.location = &quot;...&quot; # Replace as you please<br>        tr.stats.channel = tr.stats.kinemetrics_evt.chan_id</pre><p>Now, if the file does exist, we can start processing it.</p><p>We read the seismic recording from the specified file using ObsPy’s read function. It specifies the format as &quot;kinemetrics_evt&quot; and then applies the detrend method to remove any linear trends in the data.</p><p>To ensure data compatibility and consistency, the script performs data conversion from ‘Counts’ to the physical unit, in my case, it’s acceleration in cm/s². <em>Make sure the modify this based on your data’s physical unit.</em> This step is crucial for meaningful data analysis.</p><p>The next steps are optional. I will not be explaining them here since you can find a detailed explanation in my article <a href="https://medium.com/@achemine.yasmine/reading-modifying-and-converting-seismic-event-data-using-obspy-cc94c00badc4">Reading, Modifying, and Converting Seismic Event Data Using ObsPy</a>.</p><h4>2.3. Creating a directory to store the converted recording</h4><pre>    # creating a directory to store the converted recoding<br>    directory_name = &quot;Converted_event_to_Matlab&quot;<br>    if not os.path.exists(directory_name):<br>        os.mkdir(directory_name)<br>    path_to_directory = os.path.abspath(directory_name)</pre><p>We define Converted_event_to_Matlab as the name of the directory where the converted files will be stored.</p><p>We check if the directory exists and create it if not.</p><p>We retrieve the absolute path of the newly created directory.</p><h4>2.4. Saving and Converting Data</h4><pre>    event_name= os.path.basename(event_name)<br><br>    # converting and saving the recording<br>    for i, tr in enumerate(st):<br>        mdict = {k: str(v) for k, v in tr.stats.items()}<br>        mdict[&quot;data&quot;] = tr.data<br>        output_file_name = f&quot;{event_name[:-4]}_{tr.stats.channel}.mat&quot;<br>        output_file_path = os.path.join(path_to_directory, output_file_name)<br>        savemat(output_file_path, mdict)<br><br>    print(&quot;\t**** Operation completed successfully ****&quot;)</pre><p>We extract the base name of the event_name using os.path.basename(). This is often done to remove the directory path if it was included in the original event_name. The variable event_name now holds just the file name without the directory.</p><p>We initiate a loop to iterate through each trace (tr) in the st object, where st contains the seismic data:</p><p>a. mdict = {k: str(v) for k, v in tr.stats.items()}: We create a dictionary (mdict) containing trace metadata. We convert all metadata values to strings to ensure they are compatible with the savemat function.</p><p>b. mdict[&quot;data&quot;] = tr.data: We add the trace data itself to the dictionary with the key “data.” This combines metadata and data in a single dictionary for saving.</p><p>c. output_file_name = f&quot;{event_name[:-4]}_{tr.stats.channel}.mat&quot;: We generate the output file name for the current trace. We use event_name (without the last 4 characters, typically the file extension) and the trace&#39;s channel to create a unique filename for each trace.</p><p>d. output_file_path = os.path.join(path_to_directory, output_file_name): We construct the full path to the output file by combining the directory path (path_to_directory) and the generated output_file_name.</p><p>e. savemat(output_file_path, mdict): This line saves the trace data and metadata as a MATLAB file in the specified directory with the file name generated in the previous steps.</p><p>Finally, we print a success message to indicate that the operation has been completed.</p><h3>Conclusion</h3><p>Automating the conversion of Kinemetrics EVT files to MATLAB format with this script significantly improves the efficiency and accuracy of seismic data analysis. The script simplifies what would otherwise be a time-consuming and error-prone task, allowing seismologists and researchers to focus on their core work.</p><p>You can find the complete Python script for this conversion process in my <a href="https://github.com/YasmineAche/seismology/tree/main/Data_Converters/EVT_to_Matlab">GitHub repository</a>.</p><p><em>Happy converting, and may your discoveries be as profound as the Earth’s movements.</em></p><ul><li><a href="https://medium.com/@achemine.yasmine/reading-modifying-and-converting-seismic-event-data-using-obspy-cc94c00badc4">Reading, Modifying, and Converting Seismic Event Data Using ObsPy</a></li><li><a href="https://medium.com/@achemine.yasmine/a-step-by-step-guide-to-converting-kinemetrics-evt-files-to-sac-format-using-obspy-5473a7360a6c">A Step-by-Step Guide to Converting Kinemetrics EVT Files to SAC Format using ObsPy</a></li></ul><p>Beyreuther, M., Barsch, R., Krischer, L., Megies, T., Behr, Y., &amp; Wassermann, J. (2010). ObsPy: A Python toolbox for seismology. <em>Seismological Research Letters</em>, <em>81</em>(3), 530–533.</p><p>Pauli Virtanen, Ralf Gommers, Travis E. Oliphant, Matt Haberland, Tyler Reddy, David Cournapeau, Evgeni Burovski, Pearu Peterson, Warren Weckesser, Jonathan Bright, Stéfan J. van der Walt, Matthew Brett, Joshua Wilson, K. Jarrod Millman, Nikolay Mayorov, Andrew R. J. Nelson, Eric Jones, Robert Kern, Eric Larson, CJ Carey, İlhan Polat, Yu Feng, Eric W. Moore, Jake VanderPlas, Denis Laxalde, Josef Perktold, Robert Cimrman, Ian Henriksen, E.A. Quintero, Charles R Harris, Anne M. Archibald, Antônio H. Ribeiro, Fabian Pedregosa, Paul van Mulbregt, and SciPy 1.0 Contributors. (2020) <strong>SciPy 1.0: Fundamental Algorithms for Scientific Computing in Python</strong>. <em>Nature Methods</em>, 17(3), 261–272.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=547888d9b045" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Step-by-Step Guide to Converting Kinemetrics EVT Files to SAC Format using ObsPy]]></title>
            <link>https://medium.com/@achemine.yasmine/a-step-by-step-guide-to-converting-kinemetrics-evt-files-to-sac-format-using-obspy-5473a7360a6c?source=rss-4f0f5bec9f0a------2</link>
            <guid isPermaLink="false">https://medium.com/p/5473a7360a6c</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[seismology]]></category>
            <category><![CDATA[obspy]]></category>
            <category><![CDATA[seismic-waves]]></category>
            <category><![CDATA[earthquake]]></category>
            <dc:creator><![CDATA[Yasmine ACHEMINE]]></dc:creator>
            <pubDate>Fri, 13 Oct 2023 22:03:20 GMT</pubDate>
            <atom:updated>2023-10-13T22:03:20.157Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/374/1*Ni3kHkEDz1dUz_e5SC7egg.png" /><figcaption>Created using draw.io</figcaption></figure><p>Working with seismic data has probably exposed you to Kinemetrics EVT files. These files are commonly used for recording seismic data; however, to perform in-depth analysis or work with specific tools, you may need to convert them to the SAC (Seismic Analysis Code) format.</p><p>In this article, I’ll walk you through the process of converting Kinemetrics EVT files to SAC using Python and the ObsPy library.</p><p>Whether you’re a seismologist, a researcher, or simply interested in the fascinating world of earthquakes, this step-by-step guide will help you unlock valuable insights from your EVT data. Let’s dive in!</p><h3>Prerequisites :</h3><ul><li><a href="https://www.python.org/downloads/"><strong>Python</strong></a><strong>3 and </strong><a href="https://docs.obspy.org/index.html"><strong>ObsPy</strong></a><strong>:</strong> Make sure you have Python installed on your system and the <a href="https://docs.obspy.org/index.html">ObsPy</a> library. You can install <a href="https://docs.obspy.org/index.html">ObsPy</a> using pip:</li></ul><pre>pip install obspy</pre><p>Or if you use Conda:</p><pre>conda install -c conda-forge obspy</pre><ul><li><strong>Kinemetrics EVT Files:</strong> Ensure that your EVT files are in the same directory as this script or in its subdirectories.</li></ul><p>Now that we have all that we need, we can finally start writing code.</p><h3>Step 1: Introduction</h3><ul><li>Start by importing the required libraries, ObsPy, and the “<a href="https://docs.python.org/3/library/os.html">os</a>” module.</li></ul><p><em>The “</em><a href="https://docs.python.org/3/library/os.html"><em>os</em></a><em>” library in Python, short for “operating system,” provides a way to interact with the underlying operating system on which your Python program is running. It is a standard Python library, so you don’t need to install it separately.</em></p><pre>from obspy import read<br>import os</pre><ul><li>This is entirely optional, but if you’re like me and you want to create a visually appealing header when starting your program or providing usage guidelines, you can achieve this using the print method.&quot;</li></ul><pre>print(&quot;*&quot; * len(&quot;Welcome to the Evt_SAC converter&quot;))<br>print(&quot;Welcome to the Evt_SAC converter&quot;)<br>print(&quot;*&quot; * len(&quot;Welcome to the Evt_SAC converter&quot;))<br><br>print(<br>    &quot;\n Before we begin, make sure your data is in the same folder (or one of its subfolders) as the program.&quot;<br>)</pre><h3>Step 2: Input EVT File</h3><ul><li>Prompt the user to provide the name of the EVT file they want to convert using the input method. The script that we will be creating accepts as input either the file name or the full path to the file, including the file name.</li></ul><pre>event_name = input(&quot;\nEnter the name of the file to convert: &quot;).replace(&quot; &quot;, &quot;&quot;)</pre><ul><li>The script checks whether the specified file exists in the current directory. If it’s not found, the script conducts a search in all subdirectories. At the end of the search, if the file is not found, a warning message is displayed.</li></ul><pre>if not os.path.exists(event_name):<br>    start_directory = os.getcwd()  # Get the current working directory&#39;s path<br><br>    # Search for the file in all subdirectories<br>    for root, _, files in os.walk(start_directory):<br>        if event_name in files:<br>            event_name = os.path.join(root, event_name)<br>            event_not_found = False<br>            break<br>        else:<br>            event_not_found = True<br><br>    if event_not_found:<br>        print(f&quot;WARNING: &#39;{event_name}&#39; does not exist in &#39;{start_directory}&#39; or its subdirectories.&quot;)</pre><p>Since what we’re doing is basically an interaction between the program and the operating system (looking through folders), as you can see, this above section relies heavily on the “<a href="https://docs.python.org/3/library/os.html">os</a>” library, which is why we imported it in Step 1.</p><h3>Step 3: Read and Convert the EVT File to SAC</h3><p>Now that we’ve sorted the boring stuff we can finally start converting. Let the fun begin!</p><pre>if os.path.exists(event_name):<br>    st = read(event_name, format=&quot;kinemetrics_evt&quot;).detrend()<br><br>    for tr in st:<br>        tr.data = tr.data * tr.stats.calib * 100 # Data conversion<br><br>        tr.stats.network = &quot;...&quot; # Replace as you please<br>        tr.stats.station = tr.stats.kinemetrics_evt.stnid<br>        tr.stats.location = &quot;...&quot; # Replace as you please<br>        tr.stats.channel = tr.stats.kinemetrics_evt.chan_id<br>        </pre><ul><li>If the EVT file exists, the script reads the recording using ObsPy’s read function, specifying the file format as &quot;kinemetrics_evt.&quot; It also applies detrending to the data.</li><li>We continue by iterating through each trace (‘tr’ which represents different channels) in the stream (‘st’) and perform data conversion from ‘Counts’ to the physical unit by multiplying it by the trace’s calibration factor (‘tr.stats.calib’). The final data unit can vary depending on the type of the sensor. In my case, it’s acceleration, and I want it to be in cm/s² or cm.s−2 that’s why I&#39;ve scaled it by 100.</li><li>From this point on, the rest of the above script is optional. What it does is that it extracts information from the file name and assigns it to the corresponding attributes in the ObsPy Trace objects. This includes setting the network, station, location, and channel. You can customize these values according to your specific requirements. I’ve explained this part in more detail in my article “<a href="https://medium.com/@achemine.yasmine/reading-modifying-and-converting-seismic-event-data-using-obspy-cc94c00badc4">Reading, Modifying, and Converting Seismic Event Data Using ObsPy</a>”.</li></ul><p><a href="https://medium.com/@achemine.yasmine/reading-modifying-and-converting-seismic-event-data-using-obspy-cc94c00badc4">Reading, Modifying, and Converting Seismic Event Data Using ObsPy</a></p><h3>Step 4: Create a Directory for Converted Files</h3><pre>    directory_name = &quot;Converted_event_to_SAC&quot;<br><br>    if not os.path.exists(directory_name):<br>        os.mkdir(directory_name)<br><br>    path_to_directory = os.path.abspath(directory_name)</pre><ul><li>In this step, we go back to using the “os” library to create a directory named “Converted_event_to_SAC” if it doesn’t already exist. This directory will be beneficial for storing the converted SAC files.</li><li>Then, we get the absolute path to the newly created directory.</li></ul><h3>Step 5: Write SAC Files</h3><pre>    event_name= os.path.basename(event_name)<br><br>    for i, tr in enumerate(st):<br>        output_file_name = f&quot;{event_name[:-4]}_{tr.stats.channel}.sac&quot;<br>        output_file_path = os.path.join(path_to_directory, output_file_name)<br>        tr.write(output_file_path, format=&quot;SAC&quot;)<br><br>    print(&quot;**** Operation completed successfully ****&quot;)</pre><ul><li>If you want to keep the same name format for the resulting SAC files (I’m using the plural form because each channel component will be stored in a separate file) as the EVT file we use the os.path.basename() function to extract the filename from the full path of the event_name variable. It is a common practice to do this to obtain just the file&#39;s name, excluding the directory path.</li><li>This for loop iterates through the Trace objects ‘tr’ in the ‘st’ variable. Each Trace object represents a channel of seismic data, and the loop allows you to process each channel individually.</li><li>We create unique file names for each SAC file. Here, the output_file_name is generated as follows:</li></ul><p>{event_name[:-4]}: This extracts the first part of the event_name, excluding the last four characters. The [:-4] slice is used to remove the file extension (e.g., &quot;.evt&quot;). This part of the name typically represents the event or station name.</p><p>_{tr.stats.channel}.sac: This part adds the channel name to the output file name, separated by an underscore, and appends the &quot;.sac&quot; file extension. The tr.stats.channel represents the channel name associated with the current Trace object, if you chose to skip the optional part in step 3 you may need to change this to _{tr.stats.kinemetrics_evt.chan_id}.sac</p><ul><li>We use os.path.join() to combine the path_to_directory (the directory where the SAC files will be saved) and the output_file_name to create the full file path for the current channel.</li><li>We write the current channel’s data to the specified output file path (“Converted_event_to_SAC” directory) in the SAC format. It uses the tr.write() function from ObsPy with the format set to &quot;SAC.&quot;</li><li>Finally, a success message is printed to indicate that the operation is completed.</li></ul><h3>Conclusion:</h3><p>This article has provided a fundamental understanding of converting Kinemetrics EVT files to SAC format, empowering you to leverage seismic data for analysis and research. The beauty of structured data is in its potential, and this conversion is your key to unlocking insights from the Earth’s dynamic movements.</p><p>For the complete Python code used in this tutorial, you can find it in my GitHub repository <a href="https://github.com/YasmineAche/seismology/tree/main/Data_Converters/EVT_to_SAC"><strong>EVT_to_SAC</strong></a>. Feel free to explore and adapt it to your specific needs.</p><p>I would like to acknowledge the invaluable contribution of the ObsPy library, a powerful and versatile tool for working with seismic data.</p><p><em>Happy converting, and may your discoveries be as profound as the Earth’s movements.</em></p><p>Beyreuther, M., Barsch, R., Krischer, L., Megies, T., Behr, Y., &amp; Wassermann, J. (2010). ObsPy: A Python toolbox for seismology. <em>Seismological Research Letters</em>, <em>81</em>(3), 530–533.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5473a7360a6c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reading, Modifying, and Converting Seismic Event Data Using ObsPy]]></title>
            <link>https://medium.com/@achemine.yasmine/reading-modifying-and-converting-seismic-event-data-using-obspy-cc94c00badc4?source=rss-4f0f5bec9f0a------2</link>
            <guid isPermaLink="false">https://medium.com/p/cc94c00badc4</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[earthquake]]></category>
            <category><![CDATA[obspy]]></category>
            <category><![CDATA[seismic-waves]]></category>
            <category><![CDATA[seismology]]></category>
            <dc:creator><![CDATA[Yasmine ACHEMINE]]></dc:creator>
            <pubDate>Fri, 13 Oct 2023 21:45:05 GMT</pubDate>
            <atom:updated>2023-10-13T21:45:05.953Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LmyWiMYsEu0Nk72a0dGVQw.png" /><figcaption>![ObsPy Logo](obspy_logo_full_highres.png)<br>*ObsPy logo. Image source: [ObsPy Logo Source](<a href="https://raw.githubusercontent.com/obspy/website/master/logo/obspy_logo_full_highres.png)*">https://raw.githubusercontent.com/obspy/website/master/logo/obspy_logo_full_highres.png)*</a></figcaption></figure><p>Seismic data analysis is a fundamental aspect of seismology and earthquake research. To delve into the world of seismic data, you need the right tools, and ObsPy is one such powerful tool that simplifies the process.</p><p>In this comprehensive tutorial, we will explore how to read seismic event data using ObsPy, convert data from Counts to the physical unit, and change essential channel information.</p><p>Whether you’re a beginner or a seasoned seismologist, this tutorial will provide insights into the nuances of working with ObsPy.</p><h3>What is Obspy?</h3><p>From the <a href="https://docs.obspy.org/">Obspy</a> web page, we can read:</p><blockquote><a href="https://docs.obspy.org/">ObsPy </a>is an open-source project dedicated to provide a <strong>Python framework for processing seismological data</strong>. It provides parsers for common file formats, clients to access data centers and seismological signal processing routines which allow the manipulation of seismological time seriesPrerequisites</blockquote><p><strong>ObsPy</strong> is an open-source Python library designed for seismology and seismological signal processing. It provides a comprehensive toolbox for working with seismological data, making it a valuable resource for researchers and professionals in the field of seismology. Some of the core features and functionalities of the ObsPy library include:</p><ol><li><strong>Data Retrieval:</strong> ObsPy allows users to access and retrieve seismic data from various seismological data centers and sources, including web services, in a standardized format.</li><li><strong>Data Processing:</strong> The library provides a wide range of tools and functions for processing and analyzing seismic data, including filtering, instrument response correction, and spectral analysis.</li><li><strong>Data Visualization:</strong> ObsPy includes modules for creating various types of plots and visualizations to help users better understand and interpret seismological data.</li><li><strong>Instrument Response:</strong> It offers utilities for handling instrument response, which is crucial for converting recorded data into meaningful physical units.</li><li><strong>Event Detection and Analysis:</strong> ObsPy can be used to detect and analyze seismic events, such as earthquakes, and provides tools for locating their epicenters and determining their magnitudes.</li><li><strong>Support for Standard Data Formats:</strong> The library supports standard seismic data formats, such as MiniSEED and SEIS-AN, which are widely used in the seismological community.</li></ol><h3>Prerequisites</h3><p>Before we get started, make sure you have <a href="https://www.python.org/downloads/">Python</a> and ObsPy installed. If you haven’t already, you can install Obspy using pip:</p><pre>pip install obspy</pre><p>Or if you use conda:</p><pre>conda install -c conda-forge obspy</pre><h3>The Code</h3><h4>Reading Data</h4><p>As mentioned before, Obspy offers the possibility of manipulating data in various formats; you can find these listed at this <a href="https://docs.obspy.org/packages/autogen/obspy.core.stream.read.html#supported-formats">link</a>. In this article, we will be exploring the usage of the <a href="https://kinemetrics.com/">Kinemetrics</a> EVT file format.</p><ul><li>Our journey begins by importing the read method from the ObsPy library. This method is a powerful tool for reading and handling seismic data. It equips us with the ability to parse various data formats, making it a versatile choice for seismic data analysis. We import it with the following line of code:</li></ul><pre>from obspy import read, write</pre><ul><li>The next essential step is to specify the file name. In this code, we store the file path in a variable named event_name , this variable holds the path to the seismic event data file we want to work with. It&#39;s important to ensure that this path is accurate and points to the correct file location.</li></ul><pre>event_name = &quot;/Users/user/Desktop/Obspy/Data/Event_name.evt&quot;</pre><ul><li>With the read method and the event_name variable in place, we proceed to read the seismic event data. The read method takes many arguments only one of them is necessary which is the <strong><em>pathname_or_url. </em></strong>In our code, we are telling ObsPy to read the data from the file specified in event_name using the &quot;kinemetrics_evt&quot; format. The detrend() method applied to the returned object st is used to remove any trends from the data, enhancing its suitability for analysis. It’s important to note that while we explicitly specify the format in this example, ObsPy is adept at automatically recognizing many common formats, which makes it even more user-friendly.</li></ul><pre>st = read(pathname_or_url=event_name, format=&quot;kinemetrics_evt&quot;).detrend()</pre><ul><li>In the next section, we loop through the traces in the st object, which represents the seismic data. For each trace (tr), we print its details, including the channel&#39;s status. This helps us get a better understanding and a visual of the data we are working with.</li></ul><pre>for tr in st:<br>    print(&quot;\n&quot;, tr)<br>    print(f&quot;Status of the channel {tr.stats.channel}:\n&quot;, tr.stats)</pre><p>Below is a snippet of <strong>part</strong> of the output that we get:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0Vzg0Ucq7mGKElzsqKQruw.png" /></figure><h4>Modifying the data</h4><p>After checking the headers of the EVT file we may find that we want to add some pieces of information that are missing or modify them or both.</p><p>As observed with the EVT format some of the contents of thetr.stats variable will be missing, also if you are in the habit of giving a specific name to each channel while configuring the sensor you’ll find that these names are not taken into consideration in the tr.stats.channel variable.</p><p>Fear not because in this section, we will work on fixing these issues.</p><pre>for tr in st:<br>    tr.stats.network = &quot;...&quot; # Replace ...<br>    tr.stats.station = tr.stats.kinemetrics_evt.stnid<br>    tr.stats.location = &quot;...&quot;  # Replace ...<br>    tr.stats.channel = tr.stats.kinemetrics_evt.chan_id<br><br>    # Converting the data from Counts to physical unit<br>    tr.data = tr.data * tr.stats.calib<br><br>    print(&quot;\n&quot;, tr)<br>    print(f&quot;Status of the channel {tr.stats.channel}:\n&quot;, tr.stats)</pre><ul><li>As done before, we iterate through each tr and modify the tr.stats variables: network, location, station, and channel. We retrieve these last two from the tr.stats.kinemetrics_evt (see bellow picture) attribute if they are not already assigned whereas for the other ones, unfortunately, we must add them manually hence the “…” placeholder.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v0I-337bWg8hGfqOOXZnwg.png" /></figure><ul><li>Furthermore, we convert the data from Counts to the physical unit by multiplying it by the calibration factor (calib). Finally, we print the modified trace and its status as done previously.</li></ul><h4>Saving the Modifications</h4><p>We will finalize by saving the update into a new EVT file. This is fairly easy and can be done with only two or one line of code if you’re wiled at heat.</p><ul><li>First, we specify the output_file variable to define the path where you want to save the modified event file. You can choose any valid path and filename you prefer.</li><li>Then, we use the write() method that we imported before on our stream object (st) with the specified output_file and format &quot;kinemetrics_evt&quot; to write the modifications back to the EVT file. This will save the seismic data with our changes to the new file specified in output_file.</li></ul><pre># Specify the output file path and name<br>output_file = &quot;/Users/user/Desktop/Obspy/Data/modified_event.evt&quot;<br><br>st.write(output_file, format=&quot;kinemetrics_evt&quot;)</pre><h4>Running the Code</h4><p>To run this code, you’ll need to replace event_name with the path to your own event file. Once executed, you&#39;ll have converted seismic data and updated channel information.</p><h3>Conclusion</h3><p>Putting it all together, ObsPy is a fantastic tool for anyone working with seismic data. It simplifies the process of reading, processing, and analyzing data, making it an essential tool for seismologists and researchers alike.</p><p>With the knowledge gained from this tutorial, you’re well on your way to mastering the art of seismic data manipulation using ObsPy.</p><p><em>Happy seismology!</em></p><h3>References</h3><p>Beyreuther, M., Barsch, R., Krischer, L., Megies, T., Behr, Y., &amp; Wassermann, J. (2010). ObsPy: A Python toolbox for seismology. <em>Seismological Research Letters</em>, <em>81</em>(3), 530–533. Retrieved from [<a href="https://docs.obspy.org/">ObsPy Project</a>](<a href="https://docs.obspy.org/">https://docs.obspy.org</a>)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc94c00badc4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>