The Python module allows you to use the Python language and environment to extend the capabilities of SWIP5. In particular seismic libraries such as ObsPy. You can create your own Python scripts that behave like SWIP5 plugins, including extending the program menu,  and they can be called from the menu.

All Python messages and comments created during the script work are displayed in the report window after the plug-in is finished.

Requirements: Python 3.6, ObsPy

Plugins written in Python are appropriate scripts written in Python language, containing the information needed to recognize them as modules for SWIP5 and placed in the Python subdirectory. These scripts will be visible as items in the menu of the SWIP5 program, the selection of which sends the appropriate data to the Python program, executes the given program, and transfers the relevant data back to the SWIP5 program. These are typical Python scripts preceded by definitions for SWIP5, which are corresponding lines at the beginning of the script treated by Python as comments. These lines are preceded by "#!" On their basis, the SWIP5 program recognizes them as plugins for the SWIP5 program, places them in the appropriate menu items, transfers, and receives the appropriate data from the Python program. These scripts can be run in the Python program without the use of SWIP -a, which allows you to test them before adding them to the program.

Such a script must start with the line:

# !SWIP5

informing the program that this is a script intended for SWIP5. Then there must be one or more lines describing the menu:

# !MENU …

followed by a few lines describing how data is passed between SWIP5 and Python. Of course, if you're testing scripts in Python itself, you'll need to load the data before calling the script. You can use other libraries in scripts, but you must define access to them.

Below are two sample scripts:

1. Script drawing seismograms from the zoom window in ObsPy:

# !SWIP5
# !MENU Test plot
# !DATA FROM ZOOM WINDOW
streamSWIP5.spectrogram(log=True, title='PL network ' + str(st[0].stats.starttime))

2. The script generating the NORDIC catalog (before that, all phenomena should be loaded into SWIP):

# !SWIP5
# !MENU Create IS-EPOS catalog
# !QUAKEML
catalogSWIP5.write("example.dat", format="NORDIC")

Menu configuration in Python scripts


The menu is defined by one or more lines describing the menu in the script, in the form:

# !MENU …

where "..." means SWIP5 program menu name. If there is one line describing a menu, that item will be added directly to the Python submenu. If there are several lines describing the menu, an appropriate Python submenu tree will be created. It is possible to add a function call outside the Python submenu. For this, the first and only the first menu item should be:

# !TOP MENU …

Then the name "..." will be treated as an item on the main menu bar of the SWIP5 program, e.g.

# !TOP MENU OBSPY

The above definitions must appear at the beginning of the script on a line # !SWIP5, otherwise, SWIP5 ignores this script.

Below are examples of definitions in the script and the corresponding menu items:

# !SWIP5
# !MENU Create IS-EPOS catalog

Python→Create IS-EPOS catalog

# !SWIP5
# !MENU Create catalogs
# !MENU IS-EPOS catalog

Python→Create catalogs→IS-EPOS catalog

# !SWIP5
# !TOP MENU Filters
# !MENU Python filter

Filters→Python filter

Configuring data transfer to Python scripts

The lines that define how to pass data to Python must be below the lines that define the menus. Depending on the appropriate lines at the beginning of the script, the appropriate data is passed to the Python program. It is presented in the table below



Data exported to python

Python script line

1

Current event. If SWIP has loaded many events, only the current (selected) phenomenon is transferred (see working on multiple events). The parameters of the event are available in the script as a QuakeML global variable catalogSWIP5.

# !CURRENT EVENT

2

The entire QuakeML structure, including all loaded phenomena, is passed to the Python environmen. This is useful, for example, when in Python we have functions to create a catalog or we want to study the seismicity of a given area in Python, study statistics, etc.. The catalog of the event is available in the script as a QuakeML global variable catalogSWIP5.

# !QUAKEML

3

All channels that are visible in the SWIP5 main window time clipped  to the signal visible in the main window are sent to Python. Seismic channels are available in the script as global variable streamSWIP5 of Stream type.

# !DATA FROM MAIN WINDOW

4

All channels visible in the SWIP5 zoom window time clipped to the signal visible in the zoom window are sent to Python. Seismic channels are available in the script as global variable streamSWIP5 of Stream type.

# !DATA FROM ZOOM WINDOW

5

Channels selected (shown as blue in the main window) untimed are sent to Python. Seismic channels are available in the script as global variable streamSWIP5 of Stream type.

# !DATA FROM SELECTION


Data imported from Python


6The study of the phenomenon stored in the variable catalogSWIP5 is imported into SWIP5 replacing the existing study or catalog# !REPLACE QUAKEML
7The parameters of the phenomenon stored in the variable catalogSWIP5 are imported into SWIP5. New parameters are added to the current study of the phenomenon: origin, magnitude, station magnitude, amplitudes, mechanisms, phases, etc. Only new values are added. The already existing ones are not changed. In the study exported from Python, there must be an event with the ID the same as the current event in SWIP5. Otherwise, the data is ignored.# !INSERT TO QUAKEML
8The MiniSEED file saved by the Python script is loaded into SWIP5. The script must contain a command to write to the file whose name is in the configuration file.# !READ MINISEED
9The MiniSEED file saved by the Python script is added into SWIP5. The script must contain a command to write to the file whose name is in the configuration file.# !ADD MINISEED

Parameters of phenomena or entire bulletins from QuakeML are passed to Python in QuakeML format and seen as catalogSWIP5 no matter how many channels are passed, while seismic records in MiniSEED format and seen as streamSWIP5. There may be one line in the script describing how to transfer QuakeML and one line describing how to transfer seismic records.


  • No labels