-
Notifications
You must be signed in to change notification settings - Fork 8
/
merge_geomag_omni_dataframes.py
229 lines (171 loc) · 8.1 KB
/
merge_geomag_omni_dataframes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# coding: utf-8
# colnames
# In[ ]:
# In[1]:
from obspy.core import UTCDateTime
import pickle
import sys
import matplotlib.pyplot as plt
import numpy as np
import csv
import pandas as pd
#
#
# READING THE GEOMAGNETIC FIELD DATA (pkl FILE) X, Y, Z and F components
#
#
#dir = '/data/st/geomag_2015_2016_xyzf/'
dir = './'
filenames = ['X_2016_minutes.pkl', 'Y_2016_minutes.pkl','Z_2016_minutes.pkl','F_2016_minutes.pkl']
ncomps = len(filenames)
# First get number of rows and number of observatories
with open(dir+'X_2016_minutes.pkl', 'rb') as f2:
# compseries = pickle.load(f2,encoding='latin1')
compseries = pickle.load(f2)
comp=compseries[0]
times = comp.times()
numrows = len(times)
n_observatories = len(compseries)
colnames = ["" for x in range(ncomps*n_observatories)]
# Make UTC timestamps
UTCtimes = np.zeros(numrows, dtype=float)
for c in range(numrows):
UTCtimes[c] = (UTCDateTime(comp.__dict__['stats']['starttime']) + times[c]).timestamp
# Now loop through pickle files and put all into a 2D np array
geo = np.zeros([numrows,ncomps*len(compseries)])
# Geomag array
for c in range(len(filenames)):
with open(dir+filenames[c], 'rb') as f2:
#compseries = pickle.load(f2,encoding='latin1')
compseries = pickle.load(f2)
for o in range(n_observatories):
geo[:,ncomps*o + c] = compseries[o].data
colnames[ncomps*o + c] = compseries[o].__dict__['stats']['station']+'_'+compseries[o].__dict__['stats']['channel']
# Put into pandas DataFrame
geomagdf = pd.DataFrame(geo, columns=colnames)
geomagdf.insert(0, 'Date', pd.Series(np.array(UTCtimes)))
# In[2]:
geomagdf
# In[3]:
##################################################################
### READING OMNI DATA
##################################################################
omnirows = ['Year', 'Day', 'Hour', 'Minute', 'Field magnitude average nT', 'BX nT (GSE, GSM)',
'BY, nT (GSE)', 'BZ, nT (GSE)', 'BY, nT (GSM)', 'BZ, nT (GSM)', 'RMS SD B scalar, nT',
'RMS SD field vector, nT', 'Speed, km/s', 'Vx Velocity,km/s', 'Vy Velocity, km/s',
'Vz Velocity, km/s', 'Proton Density, n/cc', 'Temperature, K', 'Flow pressure, nPa',
'Electric field, mV/m', 'Total Plasma beta', 'Alfven mach number', 'Magnetosonic Mach number',
'S/C Xgse Re', 'S/C Ygse Re', 'S/c Zgse Re', 'BSN location Xgse Re',
'BSN location Ygse Re','BSN location Zgse Re','AE-index, nT','AL-index, nT',
'AU-index, nT', 'PCN-index']
# In[4]:
#dff = pd.DataFrame(omniarr[0:len(omnirows),:], index=[omnirows])
#omnigeo= pd.concat([df, dff])
#omnigeo.shape
# In[5]:
colnames=["Year", "Day", "Hour", "Minute","ID IMF Spacecraft", "IF SW Plasma Spacecraft",
"#points in IMF avg", "#points in plasma avgs", "Percent interp", "Timeshift (sec)",
"RMS, timeshift", "RMS, phase front normal", "Time btwn obs (sec)",
"Field mag avg, nT", "Bx, nT (GSE, GSM)", "By, nT (GSE,GSM)", "Bz, nT (GSE)", "By, nT (GSM)", "Bz, nT (GSM)",
"RMS SD B scalar, nT", "RMS SD field vector, nT", "Flow speed, km/s", "Vx, km/s, GSE", "Vy, km/s, GSE", "Vz, km/s, GSE",
"Proton density, n/cc", "Temperture, K", "Flow pressure, nPa", "Electric Field, mV/m", "Plasma beta",
"Alfven mach number", "X(s/c), GSE, Re", "Y(s/c), GSE, Re", "Z(s/c), GSE, Re",
"BSN location, Xgse, Re", "BSN location, Ygse, Re", "BSN location, Zgse, Re",
"AE-index, nT", "AL-index, nT", "AU-index, nT", "SYM/D index, nT", "SYM/H index, nT",
"ASY/D index, nT", "ASY/H index, nT", "PC(N) index, nT",
"Magnetosonic mach number"]
print(len(colnames))
#omnidir = '/data/st/omni/high_res_omni/'
omnidir = './'
omnidf=pd.read_csv(omnidir+'omni_min2016.asc',delimiter='\s+',skiprows=0,names=colnames)
# I4 1 ... 365 or 366
#01#Year
#02#Day
#03#Hour I3 0 ... 23
#04#Minute I3 0 ... 59 at start of average
#05#ID for IMF spacecraft I3 See footnote D below
#06#ID for SW Plasma spacecraft I3 See footnote D below
#07## of points in IMF averages I4
#08## of points in Plasma averages I4
#09#Percent interp I4 See footnote A below
#10#Timeshift, sec I7
#11#RMS, Timeshift I7
#12#RMS, Phase front normal F6.2 See Footnotes E, F below
#13#Time btwn observations, sec I7 DBOT1, See Footnote C below
#14#Field magnitude average, nT F8.2
#15#Bx, nT (GSE, GSM) F8.2
#16#By, nT (GSE) F8.2
#17#Bz, nT (GSE) F8.2
#18#By, nT (GSM) F8.2 Determined from post-shift GSE components
#19#Bz, nT (GSM) F8.2 Determined from post-shift GSE components
#20#RMS SD B scalar, nT F8.2
#21#RMS SD field vector, nT F8.2 See footnote E below
#22#Flow speed, km/s F8.1
#23#Vx Velocity, km/s, GSE F8.1
#24#Vy Velocity, km/s, GSE F8.1
#25#Vz Velocity, km/s, GSE F8.1
#26#Proton Density, n/cc F7.2
#27#Temperature, K F9.0
#28#Flow pressure, nPa F6.2 See footnote G below
#29#Electric field, mV/m F7.2 See footnote G below
#30#Plasma beta F7.2 See footnote G below
#31#Alfven mach number F6.1 See footnote G below
#32#X(s/c), GSE, Re F8.2
#33#Y(s/c), GSE, Re F8.2
#34#Z(s/c), GSE, Re F8.2
#35#BSN location, Xgse, Re F8.2 BSN = bow shock nose
#36#BSN location, Ygse, Re F8.2
#37#BSN location, Zgse, Re F8.2
#38#AE-index, nT I6 See footnote H below
#39#AL-index, nT I6 See footnote H below
#40#AU-index, nT I6 See footnote H below
#41#SYM/D index, nT I6 See footnote H below
#42#SYM/H index, nT I6 See footnote H below
#43#ASY/D index, nT I6 See footnote H below
#44#ASY/H index, nT I6 See footnote H below
#45#PC(N) index, F7.2 See footnote I below
#46#Magnetosonic mach number F5.1 See Footnote K below
#(2I4,4I3,3I4,2I7,F6.2,I7, 8F8.2,4F8.1,F7.2,F9.0,F6.2,2F7.2,F6.1,6F8.2,7I6,F7.2, F5.1)
# In[6]:
# Create DataFrame for UTCDateTime entries
times = []
for c in range(len(omnidf['Year'])):
times.append((UTCDateTime(
"{0}-{1:03d}T{2:02d}:{3:02d}:00.0".format(omnidf['Year'][c],
omnidf['Day'][c],
omnidf['Hour'][c],
omnidf['Minute'][c]))).timestamp)
date_df = pd.DataFrame(np.array(times), columns=['Date'])
# Insert column into omnidf
omnidf.insert(0, 'Date', pd.Series(np.array(times)))
# In[7]:
omnidf
# In[8]:
print(omnidf.shape)
print(geomagdf.shape)
# In[9]:
df = omnidf.merge(geomagdf, left_on='Date', right_on='Date', how='inner')
# In[10]:
df
# In[11]:
df.columns
# In[12]:
times = df.loc[:,'Date']
raw_data = df.loc[:,['Field mag avg, nT', 'Bx, nT (GSE, GSM)', 'By, nT (GSE,GSM)',
'Bz, nT (GSE)', 'By, nT (GSM)', 'Bz, nT (GSM)', 'RMS SD B scalar, nT',
'RMS SD field vector, nT', 'Flow speed, km/s', 'Vx, km/s, GSE',
'Vy, km/s, GSE', 'Vz, km/s, GSE', 'Proton density, n/cc',
'Temperture, K', 'Flow pressure, nPa', 'Electric Field, mV/m',
'Plasma beta', 'Alfven mach number', 'BOU_X', 'BOU_Y', 'BOU_Z', 'BOU_F', 'BRW_X', 'BRW_Y',
'BRW_Z', 'BRW_F', 'BSL_X', 'BSL_Y', 'BSL_Z', 'BSL_F', 'CMO_X',
'CMO_Y', 'CMO_Z', 'CMO_F', 'DED_X', 'DED_Y', 'DED_Z', 'DED_F',
'FRD_X', 'FRD_Y', 'FRD_Z', 'FRD_F', 'FRN_X', 'FRN_Y', 'FRN_Z',
'FRN_F', 'GUA_X', 'GUA_Y', 'GUA_Z', 'GUA_F', 'HON_X', 'HON_Y',
'HON_Z', 'HON_F', 'NEW_X', 'NEW_Y', 'NEW_Z', 'NEW_F', 'SHU_X',
'SHU_Y', 'SHU_Z', 'SHU_F', 'SIT_X', 'SIT_Y', 'SIT_Z', 'SIT_F',
'SJG_X', 'SJG_Y', 'SJG_Z', 'SJG_F', 'TUC_X', 'TUC_Y', 'TUC_Z',
'TUC_F']]
# In[13]:
print(raw_data.values.shape)
print(times.values.shape)
# In[ ]: