Connecting to the database

To connect with the database, you need the PyMySQL library installed with your version of Python. In addition you will need a login credential in order to have access to the database. Once obtained, a connection can be made to the database by using the following code :

import pymysql
db = pymysql.connect(host=host, user=user, passwd=passwd, port=int(port), db='3MdBs')

Once a connection is established, we can send any SQL request to the database. As an example, let ask the database how many shock models are available at the moment. The SQL command looks like this :

SELECT COUNT(*) ModelID FROM shock_params;

This command can be used directly in Python using the Pandas library :

import os
import pymysql
import pandas as pd
                                
# Get credentials stored from the environment variables
host = os.environ['MdB_HOST']
user = os.environ['MdB_USER']
passwd = os.environ['MdB_PASSWD']
port = os.environ['MdB_PORT']
                                
# Connect to the database
db = pymysql.connect(host=host, user=user, passwd=passwd, port=int(port), db='3MdBs')
                                
# Use pandas to send and receive infos
result = pd.read_sql("SELECT COUNT(*) ModelID FROM shock_params;", con=db)
                                
# Print the result in the terminal
print (result)

If successful, you should see something like this in your terminal :

   ModelID
0    199750

Simple graph example

In this example, we will explore the variation of the ratio [O III]/Hβ versus the shock velocity between 100 and 1000 km s -1 with the following shock parameters and SQL lines:


Grid reference Allen2008 shock_params.ref='Allen2008'
Model type shock only emis_VI.model_type='shock'
Abundances set Allen2008_Solar abundances.name='Allen2008_Solar'
Pre-shock density 10 cm -3 shock_params.preshck_dens=10
Transverse magnetic field 1 μG shock_params.mag_fld=1

To plot the graph, the matplotlib library needs to be installed with your version of Python.


import os
import pymysql
import pandas as pd
import matplotlib.pyplot as plt
                                                                        
# Login credential stored in environment variables
host = os.environ['MdB_HOST']
user = os.environ['MdB_USER']
passwd = os.environ['MdB_PASSWD']
port = os.environ['MdB_PORT']
                                                                        
# Connect to the database
db = pymysql.connect(host=host, user=user, passwd=passwd, port=int(port), db='3MdBs')

# SQL request with Pandas
result = pd.read_sql("""SELECT shock_params.shck_vel AS shck_vel, 
                        emis_VI.OIII_5007/emis_VI.HI_4861 AS OIII_Hb
                        FROM shock_params 
                        INNER JOIN emis_VI ON emis_VI.ModelID=shock_params.ModelID
                        INNER JOIN abundances ON abundances.AbundID=shock_params.AbundID
                        WHERE emis_VI.model_type='shock' 
                        AND abundances.name='Allen2008_Solar'
                        AND shock_params.ref='Allen08'
                        AND shock_params.shck_vel BETWEEN 100 AND 1000
                        AND shock_params.mag_fld=1
                        AND shock_params.preshck_dens=10
                        ORDER BY shck_vel;""", con=db)
                                        
# Plot the figure
fig, ax = plt.subplots(facecolor='white')
                                        
ax.plot(result.shck_vel, result.OIII_Hb, linewidth=2)
                                        
ax.set(xlabel=r'shock velocity (km s$^{-1}$)', ylabel=r'[O III] / H$\beta$')
                                        
plt.legend()
                                        
plt.show()
        

If successful, you should have this figure :

Diagnostic diagram

import os
import pymysql
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

host = os.environ['MdB_HOST']
user = os.environ['MdB_USER']
passwd = os.environ['MdB_PASSWD']
port = os.environ['MdB_PORT']

db = pymysql.connect(host=host, user=user, passwd=passwd, port=int(port), db='3MdBs')
cursor = db.cursor()

result = pd.read_sql("""SELECT shock_params.shck_vel AS shck_vel, 
                         shock_params.mag_fld AS mag_fld,
                         log10(emis_VI.NII_6583/emis_VI.HI_6563) AS NII6583_Hb,
                         log10(emis_VI.OIII_5007/emis_VI.HI_4861) AS OIII5007_Hb
                         FROM shock_params 
                         INNER JOIN emis_VI ON emis_VI.ModelID=shock_params.ModelID
                         INNER JOIN abundances ON abundances.AbundID=shock_params.AbundID
                         WHERE emis_VI.model_type='shock' 
                         AND abundances.name='Allen2008_Solar'
                         AND shock_params.ref='Allen08'
                         AND shock_params.shck_vel>=200
                         AND shock_params.preshck_dens=1
                         ORDER BY shck_vel, mag_fld;""", con=db)

fig, ax = plt.subplots(facecolor='white')

for B in np.unique(result.mag_fld):
    idx = np.where(result.mag_fld==B)
    ax.plot(result.NII6583_Hb[idx[0]], result.OIII5007_Hb[idx[0]], color='black', linewidth=1)

for v in np.unique(result.shck_vel):
    idx = np.where(result.shck_vel==v)
    ax.plot(result.NII6583_Hb[idx[0]], result.OIII5007_Hb[idx[0]], color='black', linewidth=1)

ax.set(xlim=[-0.7, 0.5], 
       ylim=[-0.8, 1.75], 
       xlabel=r'log([N II] $\lambda$6563) / H$\alpha$',
       ylabel=r'log([O III] $\lambda$5007) / H$\beta$')

plt.show()