ml-solarwind / plot_measurement.py
plot_measurement.py
Raw
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 17 12:58:42 2020

@author: baum_c4
"""

import pandas as pd
from datetime import datetime
import matplotlib.colors as mcol
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib
import pickle
import numpy as np
def multicolor_ylabel(ax,list_of_strings,list_of_colors,axis='x',anchorpad=0,**kw):
    """this function creates axes labels with multiple colors
    ax specifies the axes object where the labels should be drawn
    list_of_strings is a list of all of the text items
    list_if_colors is a corresponding list of colors for the strings
    axis='x', 'y', or 'both' and specifies which label(s) should be drawn"""
    from matplotlib.offsetbox import AnchoredOffsetbox, TextArea, HPacker, VPacker

    # x-axis label
    if axis=='x' or axis=='both':
        boxes = [TextArea(text, textprops=dict(color=color, ha='left',va='bottom',**kw)) 
                    for text,color in zip(list_of_strings,list_of_colors) ]
        xbox = HPacker(children=boxes,align="center",pad=0, sep=5)
        anchored_xbox = AnchoredOffsetbox(loc=3, child=xbox, pad=anchorpad,frameon=False,bbox_to_anchor=(0.2, -0.09),
                                          bbox_transform=ax.transAxes, borderpad=0.)
        ax.add_artist(anchored_xbox)

    # y-axis label
    if axis=='y' or axis=='both':
        boxes = [TextArea(text, textprops=dict(color=color, ha='left',va='bottom',rotation=90,**kw)) 
                     for text,color in zip(list_of_strings[::-1],list_of_colors) ]
        ybox = VPacker(children=boxes,align="center", pad=0, sep=4)
        anchored_ybox = AnchoredOffsetbox(loc=3, child=ybox, pad=anchorpad, frameon=False, bbox_to_anchor=(-0.10, 0.0), 
                                          bbox_transform=ax.transAxes, borderpad=0.)
        ax.add_artist(anchored_ybox)

    if axis=='yy':
        boxes = [TextArea(text, textprops=dict(color=color, ha='right',va='bottom',rotation=90,**kw)) 
                     for text,color in zip(list_of_strings[::-1],list_of_colors) ]
        ybox = VPacker(children=boxes,align="right", pad=0, sep=4)
        anchored_ybox = AnchoredOffsetbox(loc=3, child=ybox, pad=anchorpad, frameon=False, bbox_to_anchor=(1.3, -0.1), 
                                          bbox_transform=ax.transAxes, borderpad=0.)
        ax.add_artist(anchored_ybox)
dstdata=pd.read_csv('20200717-10-27-supermag.csv') 
with open('plot_measure.pickle', 'rb') as f:
    swe_data,magnet_data=pickle.load(f)



dens=swe_data.quantity('Np').value
dens[dens<0]=np.nan
speed=swe_data.quantity('Vp').value
magneti=magnet_data.quantity('Magnitude').value
magneti[magneti<-200]=np.nan
magnet_time=magnet_data.index

speed[speed<0]=np.nan
swe_time=swe_data.index
speed_x=swe_data.quantity('V_GSE_0').value
speed_x[speed_x<-5000]=np.nan
speed_y=swe_data.quantity('V_GSE_1').value
speed_y[speed_y<-5000]=np.nan
speed_z=swe_data.quantity('V_GSE_2').value
speed_z[speed_z<-5000]=np.nan
pos_x=swe_data.quantity('SC_pos_GSE_0').value
pos_y=swe_data.quantity('SC_pos_GSE_1').value
pos_z=swe_data.quantity('SC_pos_GSE_2').value
loc='AND'
test=dstdata["IAGA"].tolist()

data=dstdata["dbn_nez"].tolist()
abk=data[0::8]
andd=data[1::8]
asc=data[2::8]
bng=data[3::8]
fur=data[4::8]
ler=data[5::8]
ngk=data[6::8]
tan=data[7::8]
time=dstdata["Date_UTC"].tolist()
time=time[0::8]
date_time = [datetime.strptime(date, '%Y-%m-%dT%H:%M:%S') for date in time]
ACEtime=datetime(year=2000,month=7,day=19,hour=14,minute=48,second=35)

impacttime=datetime(year=2000,month=7,day=19,hour=15,minute=28,second=0)


starttime=datetime(year=2000,month=7,day=19,hour=14,minute=0,second=0)

endtime=datetime(year=2000,month=7,day=19,hour=16,minute=0,second=0)

font = { 'size'   : 10,'weight' : 'normal',}

matplotlib.rc('font', **font)

f,ax = plt.subplots(3,1,figsize=(9*0.39,18*0.39))
#ax[0].axvline(ACEtime,color='k')
ax[0].plot(swe_time,dens,color='g')
ax[0].plot(magnet_time,magneti,color='b')
ax[0].set_xlim([starttime, endtime])
multicolor_ylabel(ax[0],('        n$_p$','[cm$^{-3}$],',' B','[nT]'),('k','b','k','g','k'),anchorpad=-2.5,axis='y',size=10)#weight='bold'
ax[0].set_title('Interplanetary Shock 2000/07/19')
ax[0].set_ylim([0,15])
ax2 = ax[0].twinx()
ax2.plot(swe_time,speed, 'r-')
ax2.set_ylim([450,650])
multicolor_ylabel(ax2,('  abs. SW speed','[km/s]'),('k','r'),anchorpad=0,axis='yy',size=10)#weight='bold'


ax[1].axvline(ACEtime,color='k')
ax[1].plot(swe_time,speed_y,color='g')
ax[1].plot(swe_time,speed_z,color='b')
ax[1].set_xlim([starttime, endtime])
#ax[0].set_ylabel('SW speed y [km/s]', color='g')
multicolor_ylabel(ax[1],('   SW speed','Y','/','Z','[km/s]'),('k','b','k','g','k'),anchorpad=-2.5,axis='y',size=10)#weight='bold'
ax[1].set_title('Solar wind speed at ACE')

ax2 = ax[1].twinx()

ax2.plot(swe_time,speed_x, 'r-')
#ax2.set_ylabel('SW speed x [km/s]', color='r')
ax2.set_ylim([-650,-450])
multicolor_ylabel(ax2,('   SW speed','X','[km/s]'),('k','r','k'),anchorpad=0,axis='yy',size=10)#weight='bold'

ax[2].set_title('Magnetic field at Earth')
ax[2].axvline(ACEtime,color='k')
ax[2].axvline(impacttime,color='r')
ax[2].plot(date_time,abk,label='ABK')
#ax[1].plot(date_time,andd,label='AND')
ax[2].plot(date_time[:-1],ler,label='LER')
ax[2].plot(date_time[:-1],fur,label='FUR')
ax[2].plot(date_time[:-1],asc,label='ASC')
#ax[1].plot(date_time[:-1],bng,label='BNG')



ax[2].set_xlim([starttime, endtime])
ax[2].set_ylim([-30, 150])
ax[2].legend(loc='center left',bbox_to_anchor=(1.0, 0.5))
f.autofmt_xdate()
ax[2].xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
#plt.xticks(np.arange(215, 256, step=10))
ax[2].set_ylabel('$\Delta$B north [nt]')
ax[2].set_xlabel('UTC ')
ax[2].set_yticks(np.arange(-30, 151, step=30))
plt.savefig('plot_measurement.pdf',bbox_inches='tight')
plt.show()