1+ import numpy as np
2+ from numpy import radians , cos , sin , arcsin , sqrt
3+ import matplotlib .pyplot as plt
4+ import pandas as pd
5+
6+ def haversine (lat0 , lon0 , lat , lon ):
7+
8+ R = 6378.137e3 # this is in meters. For Earth radius in kilometers use 6372.8 km
9+
10+ dLat = radians (lat - lat0 )
11+ dLon = radians (lon - lon0 )
12+ lat1 = radians (lat0 )
13+ lat2 = radians (lat )
14+
15+ a = sin (dLat / 2 )** 2 + cos (lat1 )* cos (lat2 )* sin (dLon / 2 )** 2
16+ c = 2 * arcsin (sqrt (a ))
17+
18+ return R * c
19+
20+
21+ def get_time_distance (lat , lon , time , lat0 , lon0 , time0 ):
22+ lat0 = lat0 * np .ones_like (lat )
23+ lon0 = lon0 * np .ones_like (lon )
24+ dt = time - time0
25+
26+ distance_from_origin = (haversine (lat0 ,lon0 ,lat ,lon ))
27+ if (type (dt ) == pd .core .series .Series ):
28+ dt = dt .to_numpy ()
29+
30+ time_from_origin = ((dt ).astype ('timedelta64[ns]' ).astype (float )/ 1e9 )
31+ return distance_from_origin , time_from_origin
32+
33+
34+ def time_distance_plot_interactive (interactive_lma , ax ):
35+ lat = interactive_lma .this_lma_lat
36+ lon = interactive_lma .this_lma_lon
37+ alt = interactive_lma .this_lma_alt
38+ time = interactive_lma .this_lma_time
39+ first = np .nanargmin (time )
40+
41+ distance_from_origin , time_from_origin = get_time_distance (lat , lon , time ,
42+ lat [first ], lon [first ], time [first ])
43+
44+ art_out = time_distance_plot (ax , time_from_origin , distance_from_origin )
45+
46+ return art_out
47+
48+
49+ def time_distance_plot (ax , time , distance , pad_sec = 10 , ** kwargs ):
50+ m_reference_lines = 10
51+ m = - m_reference_lines
52+ dt = time [- 1 ]- time [0 ]
53+ x = np .linspace (0 , dt + pad_sec , 100 )
54+ y = x * 2 * 10 ** 4
55+ yy = x * 10 ** 5
56+ yyy = x * 10 ** 6
57+
58+ art_out = []
59+
60+ while (m < m_reference_lines ):
61+ art = ax .plot (x + (m / m_reference_lines ), y , color = 'b' )
62+ art_out .extend (art )
63+ art = ax .plot (x + (m / m_reference_lines ), yy , color = 'r' )
64+ art_out .extend (art )
65+ art = ax .plot (x + (m / m_reference_lines ), yyy , color = 'g' )
66+ art_out .extend (art )
67+ m += 1
68+
69+ art = ax .plot (x , y , color = 'b' , label = 'Positive Leader' )
70+ art_out .extend (art )
71+ art = ax .plot (x , yy , color = 'r' , label = 'Negative Leader' )
72+ art_out .extend (art )
73+ art = ax .plot (x , yyy , color = 'g' , label = 'Dart Leader' )
74+ art_out .extend (art )
75+
76+ ax .set_ylim (0 , max (distance )+ 1000 )
77+ ax .set_xlim (- 0.05 , max (time )+ 0.1 )
78+
79+ sc = ax .scatter (time , distance , ** kwargs )
80+ art_out .append (sc )
81+ #art = ax.legend(title = "Leader Type Key", loc = 2, fontsize=12, title_fontsize=16, framealpha=1)
82+ #art_out.extend(art)
83+ ax .set_title ("Lightning Leader Speed" , size = 24 )
84+ ax .set_xlabel ("Time from Origin (s)" , size = 12 )
85+ ax .set_ylabel ("Distance from Origin (m)" , size = 12 )
86+
87+ return art_out
88+
89+
90+
91+
0 commit comments