forked from ee-davies/sc-data-functions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions_wind.py
More file actions
executable file
·852 lines (752 loc) · 36.6 KB
/
functions_wind.py
File metadata and controls
executable file
·852 lines (752 loc) · 36.6 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
import numpy as np
import pandas as pd
from datetime import datetime, timedelta, timezone
from spacepy import pycdf
# import os
import glob
import urllib.request
from urllib.request import urlopen
import os.path
import pickle
from bs4 import BeautifulSoup
import data_frame_transforms as data_transform
import position_frame_transforms as pos_transform
import functions_general as fgen
from functions_general import load_path
"""
WIND SERVER DATA PATH
"""
wind_path=load_path(path_name='wind_path')
"""
WIND BAD DATA FILTER and PATH FORMATTING
"""
def format_path(fp):
"""Formatting required for CDF package."""
return fp.replace('/', '\\')
def filter_bad_data(df, col, bad_val): #filter across whole df
if bad_val < 0:
mask = df[col] < bad_val # boolean mask for all bad values
else:
mask = df[col] > bad_val # boolean mask for all bad values
cols = [x for x in df.columns if x != 'timestamp']
df.loc[mask, cols] = np.nan
return df
def filter_bad_col(df, col, bad_val):
if bad_val < 0:
mask = df[col] < bad_val # boolean mask for all bad values
else:
mask = df[col] > bad_val # boolean mask for all bad values
df[col][mask] = np.nan
return df
"""
WIND DATA INFO
"""
# Wind: earliest time available:
wind_earliest_mag = datetime(1994,11,13)
wind_earliest_swe = datetime(1994,11,17)
wind_earliest_pos = datetime(1994,11,1)
"""
WIND DOWNLOAD DATA
"""
def download_wind_orb(start_timestamp, end_timestamp, path=wind_path):
path = path+'orbit/'
if not os.path.exists(path):
os.makedirs(path)
start = start_timestamp.date()
end = end_timestamp.date() + timedelta(days=1)
while start < end:
year = start.year
date_str = f'{year}{start.month:02}{start.day:02}'
try:
data_url = f'https://cdaweb.gsfc.nasa.gov/pub/data/wind/orbit/pre_or/{year}/'
soup = BeautifulSoup(urlopen(data_url), 'html.parser')
for link in soup.find_all('a'):
href = link.get('href')
if href is not None and href.startswith('wi_or_pre_'+date_str):
filename = href
if os.path.isfile(f"{path}{filename}") == True:
print(f'{filename} has already been downloaded.')
else:
urllib.request.urlretrieve(data_url+filename, f"{path}{filename}")
print(f'Successfully downloaded {filename}')
except Exception as e:
print('ERROR', e, f'.File for {year} does not exist.')
start += timedelta(days=1)
def download_wind_mag_rtn(start_timestamp, end_timestamp, path=wind_path):
path = path+'mfi/rtn/'
if not os.path.exists(path):
os.makedirs(path)
start = start_timestamp.date()
end = end_timestamp.date() + timedelta(days=1)
while start < end:
year = start.year
date_str = f'{year}{start.month:02}{start.day:02}'
try:
data_url = f'https://cdaweb.gsfc.nasa.gov/pub/data/wind/mfi/mfi_h3-rtn/{year}/'
soup = BeautifulSoup(urlopen(data_url), 'html.parser')
for link in soup.find_all('a'):
href = link.get('href')
if href is not None and href.startswith('wi_h3-rtn_mfi_'+date_str):
filename = href
if os.path.isfile(f"{path}{filename}") == True:
print(f'{filename} has already been downloaded.')
else:
urllib.request.urlretrieve(data_url+filename, f"{path}{filename}")
print(f'Successfully downloaded {filename}')
except Exception as e:
print('ERROR', e, f'.File for {year} does not exist.')
start += timedelta(days=1)
def download_wind_mag_h0(start_timestamp, end_timestamp, path=wind_path):
path = path+'mfi/h0/'
if not os.path.exists(path):
os.makedirs(path)
start = start_timestamp.date()
end = end_timestamp.date() + timedelta(days=1)
while start < end:
year = start.year
date_str = f'{year}{start.month:02}{start.day:02}'
try:
data_url = f'https://cdaweb.gsfc.nasa.gov/pub/data/wind/mfi/mfi_h0/{year}/'
soup = BeautifulSoup(urlopen(data_url), 'html.parser')
for link in soup.find_all('a'):
href = link.get('href')
if href is not None and href.startswith('wi_h0_mfi_'+date_str):
filename = href
if os.path.isfile(f"{path}{filename}") == True:
print(f'{filename} has already been downloaded.')
else:
urllib.request.urlretrieve(data_url+filename, f"{path}{filename}")
print(f'Successfully downloaded {filename}')
except Exception as e:
print('ERROR', e, f'.File for {year} does not exist.')
start += timedelta(days=1)
def download_wind_swe(start_timestamp, end_timestamp, path=wind_path):
path = path+'swe/h1/'
if not os.path.exists(path):
os.makedirs(path)
start = start_timestamp.date()
end = end_timestamp.date() + timedelta(days=1)
while start < end:
year = start.year
date_str = f'{year}{start.month:02}{start.day:02}'
try:
data_url = f'https://cdaweb.gsfc.nasa.gov/pub/data/wind/swe/swe_h1/{year}/'
soup = BeautifulSoup(urlopen(data_url), 'html.parser')
for link in soup.find_all('a'):
href = link.get('href')
if href is not None and href.startswith('wi_h1_swe_'+date_str):
filename = href
if os.path.isfile(f"{path}{filename}") == True:
print(f'{filename} has already been downloaded.')
else:
urllib.request.urlretrieve(data_url+filename, f"{path}{filename}")
print(f'Successfully downloaded {filename}')
except Exception as e:
print('ERROR', e, f'.File for {year} does not exist.')
start += timedelta(days=1)
def download_wind_swe_rtn(start_timestamp, end_timestamp, path=wind_path):
path = path+'swe/rtn/'
if not os.path.exists(path):
os.makedirs(path)
start = start_timestamp.date()
end = end_timestamp.date() + timedelta(days=1)
while start < end:
year = start.year
date_str = f'{year}{start.month:02}{start.day:02}'
try:
data_url = f'https://cdaweb.gsfc.nasa.gov/pub/data/wind/swe/swe_h1_rtn/{year}/'
soup = BeautifulSoup(urlopen(data_url), 'html.parser')
for link in soup.find_all('a'):
href = link.get('href')
if href is not None and href.startswith('wi_h1_swe_rtn_'+date_str):
filename = href
if os.path.isfile(f"{path}{filename}") == True:
print(f'{filename} has already been downloaded.')
else:
urllib.request.urlretrieve(data_url+filename, f"{path}{filename}")
print(f'Successfully downloaded {filename}')
except Exception as e:
print('ERROR', e, f'.File for {year} does not exist.')
start += timedelta(days=1)
"""
WIND MAG DATA
"""
def get_windmag_gse(fp):
"""raw = gse"""
try:
cdf = pycdf.CDF(fp)
data = {df_col: cdf[cdf_col][:,0] for cdf_col, df_col in zip(['Epoch', 'BF1'], ['time', 'bt'])}
df = pd.DataFrame.from_dict(data)
bx, by, bz = cdf['BGSE'][:].T
df['bx'] = bx
df['by'] = by
df['bz'] = bz
df = filter_bad_col(df, 'bt', -9.99E30)
df = filter_bad_col(df, 'bx', -9.99E30)
df = filter_bad_col(df, 'by', -9.99E30)
df = filter_bad_col(df, 'bz', -9.99E30)
except Exception as e:
print('ERROR:', e, fp)
df = None
return df
def get_windmag_gse_range(start_timestamp, end_timestamp, path=wind_path):
"""Pass two datetime objects and grab .cdf files between dates, from
directory given."""
df = None
start = start_timestamp.date()
end = end_timestamp.date() + timedelta(days=1)
while start < end:
year = start.year
date_str = f'{year}{start.month:02}{start.day:02}'
try:
fn = glob.glob(f'{path}mfi/h0/wi_h0_mfi_{date_str}*.cdf')[0]
_df = get_windmag_gse(fn)
if _df is not None:
if df is None:
df = _df.copy(deep=True)
else:
df = pd.concat([df, _df])
except Exception as e:
print('ERROR', e, f'{date_str} does not exist')
start += timedelta(days=1)
return df
def get_windmag_gsm(fp):
"""raw = gse"""
try:
cdf = pycdf.CDF(fp)
data = {df_col: cdf[cdf_col][:,0] for cdf_col, df_col in zip(['Epoch', 'BF1'], ['time', 'bt'])}
df = pd.DataFrame.from_dict(data)
bx, by, bz = cdf['BGSM'][:].T
df['bx'] = bx
df['by'] = by
df['bz'] = bz
df = filter_bad_col(df, 'bt', -9.99E30)
df = filter_bad_col(df, 'bx', -9.99E30)
df = filter_bad_col(df, 'by', -9.99E30)
df = filter_bad_col(df, 'bz', -9.99E30)
except Exception as e:
print('ERROR:', e, fp)
df = None
return df
def get_windmag_gsm_range(start_timestamp, end_timestamp, path=wind_path):
"""Pass two datetime objects and grab .cdf files between dates, from
directory given."""
df = None
start = start_timestamp.date()
end = end_timestamp.date() + timedelta(days=1)
while start < end:
year = start.year
date_str = f'{year}{start.month:02}{start.day:02}'
try:
fn = glob.glob(f'{path}mfi/h0/wi_h0_mfi_{date_str}*.cdf')[0]
_df = get_windmag_gsm(fn)
if _df is not None:
if df is None:
df = _df.copy(deep=True)
else:
df = pd.concat([df, _df])
except Exception as e:
print('ERROR', e, f'{date_str} does not exist')
start += timedelta(days=1)
return df
def get_windmag_rtn(fp):
"""raw = rtn"""
try:
cdf = pycdf.CDF(fp)
data = {df_col: cdf[cdf_col][:] for cdf_col, df_col in zip(['Epoch', 'BF1'], ['time', 'bt'])}
df = pd.DataFrame.from_dict(data)
bx, by, bz = cdf['BRTN'][:].T
df['bx'] = bx
df['by'] = by
df['bz'] = bz
df = filter_bad_col(df, 'bt', -9.99E30)
df = filter_bad_col(df, 'bx', -9.99E30)
df = filter_bad_col(df, 'by', -9.99E30)
df = filter_bad_col(df, 'bz', -9.99E30)
except Exception as e:
print('ERROR:', e, fp)
df = None
return df
def get_windmag_rtn_range(start_timestamp, end_timestamp, path=wind_path):
"""Pass two datetime objects and grab .cdf files between dates, from
directory given."""
df = None
start = start_timestamp.date()
end = end_timestamp.date() + timedelta(days=1)
while start < end:
date_str = f'{start.year}{start.month:02}{start.day:02}'
try:
fn = glob.glob(f'{path}mfi/rtn/wi_h3-rtn_mfi_{date_str}*')[0]
_df = get_windmag_rtn(fn)
if _df is not None:
if df is None:
df = _df.copy(deep=True)
else:
df = pd.concat([df, _df])
except Exception as e:
print('ERROR:', e, f'{date_str} does not exist')
start += timedelta(days=1)
return df
def get_windmag_range(start_timestamp, end_timestamp, coord_sys:str):
if coord_sys == 'GSE':
df = get_windmag_gse_range(start_timestamp, end_timestamp)
elif coord_sys == 'GSM':
df = get_windmag_gsm_range(start_timestamp, end_timestamp)
elif coord_sys == 'RTN':
df = get_windmag_rtn_range(start_timestamp, end_timestamp)
return df
"""
WIND PLASMA DATA (3DP hidden for now, and SWE)
"""
# def get_wind3dp_pm(fp):
# cdf = pycdf.CDF(fp)
# data = {df_col: cdf[cdf_col][:] for cdf_col, df_col in zip(['Epoch', 'P_DENS', 'P_TEMP'], ['timestamp', 'density', 'v_therm'])}
# df = pd.DataFrame.from_dict(data)
# df['timestamp'] = pd.to_datetime(df['timestamp'])
# vx, vy, vz = cdf['P_VELS'][:].T #proton velocity vector is in GSE coordinates
# df['v_x'] = vx
# df['v_y'] = vy
# df['v_z'] = vz
# df['v_bulk'] = np.linalg.norm(df[['v_x', 'v_y', 'v_z']], axis=1)
# # for col in cols_new[1:]:
# # df[col] = df[col].astype('float32')
# return df
# def get_wind3dp_pm_range(start_timestamp, end_timestamp, path):
# """Pass two datetime objects and grab .cdf files between dates, from
# directory given."""
# df = None
# start = start_timestamp.date()
# end = end_timestamp.date()
# while start <= end:
# year = start.year
# month = start.month
# day = start.day
# fn = f'wi_pm_3dp_{year}{month:02}{day:02}_v05.cdf'
# #print(f'{path}/{fn}')
# _df = get_wind3dp_pm(f'{path}/{fn}')
# if _df is not None:
# if df is None:
# df = _df.copy(deep=True)
# else:
# df = df.append(_df.copy(deep=True))
# start += timedelta(days=1)
# return df
# def get_wind3dp_pm_range(start_timestamp, end_timestamp, path=r'/Volumes/External/Data/WIND/3dp/pm'):
# """Pass two datetime objects and grab .cdf files between dates, from
# directory given."""
# df = None
# start = start_timestamp.date()
# end = end_timestamp.date()
# while start <= end:
# year = start.year
# month = start.month
# day = start.day
# if start >= (datetime(2011, 12, 30).date()):
# fn = f'wi_pm_3dp_{year}{month:02}{day:02}_v05.cdf'
# elif (start <= datetime(2011, 12, 29).date()) & (start > datetime(2011, 1, 13).date()):
# fn = f'wi_pm_3dp_{year}{month:02}{day:02}_v04.cdf'
# elif (start <= datetime(2011, 1, 13).date()) & (start > datetime(2010, 5, 11).date()):
# fn = f'wi_pm_3dp_{year}{month:02}{day:02}_v03.cdf'
# elif (start <= datetime(2010, 5, 11).date()) & (start > datetime(2009, 5, 19).date()):
# fn = f'wi_pm_3dp_{year}{month:02}{day:02}_v04.cdf'
# elif (start <= datetime(2009, 5, 19).date()) & (start > datetime(2008, 1, 1).date()):
# fn = f'wi_pm_3dp_{year}{month:02}{day:02}_v03.cdf'
# elif start == datetime(2008, 1, 1).date():
# fn = f'wi_pm_3dp_{year}{month:02}{day:02}_v05.cdf'
# elif year <= 2007:
# fn = f'wi_pm_3dp_{year}{month:02}{day:02}_v03.cdf'
# #fn = f'wi_pm_3dp_{year}{month:02}{day:02}_v05.cdf'
# #print(f'{path}/{fn}')
# _df = get_wind3dp_pm(f'{path}/{fn}')
# if _df is not None:
# if df is None:
# df = _df.copy(deep=True)
# else:
# df = df.append(_df.copy(deep=True))
# start += timedelta(days=1)
# return df
def get_windswe_gse(fp): #choice between nonlin or moments
try:
cdf = pycdf.CDF(fp)
cols_raw = ['Epoch', 'Proton_V_nonlin', 'Proton_VX_nonlin', 'Proton_VY_nonlin',
'Proton_VZ_nonlin', 'Proton_W_nonlin', 'Proton_Np_nonlin']
cols_new = ['time', 'vt', 'vx', 'vy', 'vz', 'tp', 'np']
data = {df_col: cdf[cdf_col][:] for cdf_col, df_col in zip(cols_raw, cols_new)}
df = pd.DataFrame.from_dict(data)
df['time'] = pd.to_datetime(df['time'])
for col in cols_new[1:]:
df[col] = df[col].astype('float32')
df = filter_bad_col(df, 'np', 9.99E04)
df = filter_bad_col(df, 'tp', 9.99E04) #called tp but actually is v_therm
df = filter_bad_col(df, 'vt', 9.99E04)
df = filter_bad_col(df, 'vx', 9.99E04)
df = filter_bad_col(df, 'vy', 9.99E04)
df = filter_bad_col(df, 'vz', 9.99E04)
except Exception as e:
print('ERROR:', e, fp)
df = None
return df
def get_windswe_gse_range(start_timestamp, end_timestamp, path=wind_path):
"""Pass two datetime objects and grab .cdf files between dates, from
directory given."""
df = None
start = start_timestamp.date()
end = end_timestamp.date()
while start <= end:
date_str = f'{start.year}{start.month:02}{start.day:02}'
try:
fn = glob.glob(f'{path}swe/h1/wi_h1_swe_{date_str}*')[0]
_df = get_windswe_gse(fn)
if _df is not None:
if df is None:
df = _df.copy(deep=True)
else:
df = pd.concat([df, _df])
except Exception as e:
print('ERROR:', e, f'{date_str} does not exist')
start += timedelta(days=1)
return df
def get_windswe_rtn(fp): #choice between nonlin or moments
try:
cdf = pycdf.CDF(fp)
cols_raw = ['Epoch', 'Proton_V_nonlin', 'Proton_VR_nonlin', 'Proton_VT_nonlin',
'Proton_VN_nonlin', 'Proton_W_nonlin', 'Proton_Np_nonlin']
cols_new = ['time', 'vt', 'vx', 'vy', 'vz', 'tp', 'np']
data = {df_col: cdf[cdf_col][:] for cdf_col, df_col in zip(cols_raw, cols_new)}
df = pd.DataFrame.from_dict(data)
df = filter_bad_col(df, 'np', 9.99E04)
df = filter_bad_col(df, 'tp', 9.99E04) #called tp but actually is v_therm, km/s
df = filter_bad_col(df, 'vt', 9.99E04)
df = filter_bad_col(df, 'vx', 9.99E04)
df = filter_bad_col(df, 'vy', 9.99E04)
df = filter_bad_col(df, 'vz', 9.99E04)
except Exception as e:
print('ERROR:', e, fp)
df = None
return df
def get_windswe_rtn_range(start_timestamp, end_timestamp, path=wind_path):
"""Pass two datetime objects and grab .cdf files between dates, from
directory given."""
df = None
start = start_timestamp.date()
end = end_timestamp.date()
while start <= end:
date_str = f'{start.year}{start.month:02}{start.day:02}'
try:
fn = glob.glob(f'{path}swe/rtn/wi_h1_swe_rtn_{date_str}*')[0]
_df = get_windswe_rtn(fn)
if _df is not None:
if df is None:
df = _df.copy(deep=True)
else:
df = pd.concat([df, _df])
except Exception as e:
print('ERROR:', e, f'. wi_h1_swe_rtn_{date_str} does not exist, converting from wi_h1_swe_{date_str} (GSE) instead...')
try:
fn_swe = glob.glob(f'{path}swe/h1/wi_h1_swe_{date_str}*')[0]
_df_swe = get_windswe_gse(fn_swe)
fn_pos = glob.glob(f'{path}orbit/wi_or_pre_{date_str}*')[0]
_df_pos = get_wind_pos(fn_pos, 'GSE')
_df = windswe_gse_to_rtn(_df_swe, _df_pos)
if _df is not None:
if df is None:
df = _df.copy(deep=True)
else:
df = pd.concat([df, _df])
except Exception as e:
print('ERROR:', e, f'. wi_h1_swe_{date_str} does not exist either.')
start += timedelta(days=1)
return df
def windswe_gse_to_rtn(df_swe_gse, df_pos_gse):
df_swe_heeq = data_transform.perform_plas_transform(df_swe_gse, 'GSE', 'HEEQ')
df_pos_hee = pos_transform.GSE_to_HEE(df_pos_gse)
df_pos_heeq = pos_transform.perform_transform(df_pos_hee, 'HEE', 'HEEQ')
df_new_pos = data_transform.interp_to_newtimes(df_pos_heeq, df_swe_heeq)
combined_df = data_transform.combine_dataframes(df_swe_heeq,df_new_pos)
df_swe_rtn = data_transform.HEEQ_to_RTN_plas(combined_df)
return df_swe_rtn
def get_windswe_range(start_timestamp, end_timestamp, coord_sys:str):
if coord_sys == 'GSE':
df = get_windswe_gse_range(start_timestamp, end_timestamp)
elif coord_sys == 'GSM':
df_gse = get_windswe_gse_range(start_timestamp, end_timestamp)
df = data_transform.GSE_to_GSM_plas(df_gse)
elif coord_sys == 'RTN':
df = get_windswe_rtn_range(start_timestamp, end_timestamp)
return df
"""
WIND PAD FUNCTIONS:
EHPD data from 3DP instrument
"""
def get_wind3dp_ehpd(start, end, input_dir='/Volumes/External/Data/WIND/3dp/ehpd/'):
epoch_arr = np.empty((0,), dtype='datetime64[ns]')
energy_arr = np.empty((0,15))
pangle_arr = np.empty((0,8))
flux_arr = np.empty((0,8,15))
while start <= end:
fp = input_dir+'wi_ehpd_3dp_'+start.strftime("%Y")+start.strftime("%m")+start.strftime("%d")+'_v02.cdf'
cdf = pycdf.CDF(fp)
data = {df_col: cdf[cdf_col][:] for cdf_col, df_col in zip(['Epoch', 'ENERGY', 'PANGLE', 'FLUX'], ['time', 'energy', 'pangle', 'flux'])}
epoch = pd.to_datetime(data['time']) # array of dim X (time entries)
energy = data['energy'] # array of dim [X,Y] (time entries and 15 energy channels)
pangle = data['pangle'] # array of dim [X,Z] (time entries and 8 angles)
flux = data['flux'] # array of dim [X,Z,Y] (time entries, 8 angles, 15 energy channels)
epoch_arr = np.concatenate((epoch_arr, epoch))
energy_arr = np.concatenate((energy_arr, energy))
pangle_arr = np.concatenate((pangle_arr, pangle))
flux_arr = np.concatenate((flux_arr, flux))
start+=timedelta(days=1)
epoch_arr = pd.to_datetime(epoch_arr)
return epoch_arr, energy_arr, pangle_arr, flux_arr
def wind_pad_array(epoch_arr, pangle_arr, flux_arr):
x_arr = epoch_arr.to_numpy()
y_arr = np.nanmean(pangle_arr, axis=0)
z = np.sum(flux_arr, axis=2) #z = np.sum(flux_arr[:, :, i:j], axis=2) if want to select specific channel range
z_arr = z.T
return x_arr, y_arr, z_arr
"""
WIND POSITION FUNCTIONS:
Wind has no spice kernels, but does have predicted orbit files including J2000 GCI, GSE, GSM, HEC
"""
def cart2sphere(x,y,z):
r = np.sqrt(x**2+ y**2 + z**2) /1.495978707E8
theta = np.arctan2(z,np.sqrt(x**2+ y**2)) * 360 / 2 / np.pi
phi = np.arctan2(y,x) * 360 / 2 / np.pi
return (r, theta, phi)
def get_wind_pos(fp, coord_sys='GSE'):
try:
cdf = pycdf.CDF(fp)
data = {df_col: cdf[cdf_col][:] for cdf_col, df_col in zip(['Epoch'], ['time'])}
df = pd.DataFrame.from_dict(data)
x, y, z = cdf[f'{coord_sys}_POS'][:].T
r, lat, lon = cart2sphere(x,y,z)
df['x'] = x
df['y'] = y
df['z'] = z
df['r'] = r
df['lat'] = lat
df['lon'] = lon
except Exception as e:
print('ERROR:', e, fp)
df = None
return df
def get_wind_positions(start_timestamp, end_timestamp, coord_sys='GSE', path=wind_path+'orbit/'):
"""Pass two datetime objects and grab .cdf files between dates, from
directory given."""
coord_sys2 = coord_sys
if coord_sys2 == 'HEE':
coord_sys = 'GSE'
elif coord_sys2 == 'HEEQ':
coord_sys = 'GSE'
elif coord_sys2 == 'HAE':
coord_sys = 'GSE'
df=None
start = start_timestamp.date()
end = end_timestamp.date()
while start <= end:
year = start.year
date_str = f'{year}{start.month:02}{start.day:02}'
try:
fn = glob.glob(str(path)+f'wi_or_pre_{date_str}_*')[0]
_df = get_wind_pos(fn, coord_sys)
if _df is not None:
if df is None:
df = _df.copy(deep=True)
else:
df = pd.concat([df, _df])
except Exception as e:
print('ERROR:', e, f'{date_str} does not exist')
start += timedelta(days=1)
if coord_sys2 == 'HEE':
df_hee = pos_transform.GSE_to_HEE(df)
return df_hee
elif coord_sys2 == 'HEEQ':
df_heeq = pos_transform.GSE_to_HEEQ(df)
return df_heeq
elif coord_sys2 == 'HAE':
df_hae = pos_transform.GSE_to_HAE(df)
return df_hae
else:
return df
"""
COMBINED WIND MAG AND PLAS
"""
def get_windmagplas(start_timestamp, end_timestamp, coord_sys='RTN'):
df_mag = get_windmag_range(start_timestamp, end_timestamp, coord_sys)
if df_mag is None:
print(f'Wind MAG data is empty for this timerange')
df_mag = pd.DataFrame({'time':[], 'bt':[], 'bx':[], 'by':[], 'bz':[]})
mag_rdf = df_mag.drop(columns=['time'])
else:
mag_rdf = df_mag.set_index('time').resample('1min').mean().reset_index(drop=False)
mag_rdf.set_index(pd.to_datetime(mag_rdf['time']), inplace=True)
#PLASMA DATA
df_plas = get_windswe_range(start_timestamp, end_timestamp, coord_sys)
if df_plas is None:
print(f'WIND SWE data is empty for this timerange')
df_plas = pd.DataFrame({'time':[], 'vt':[], 'vx':[], 'vy':[], 'vz':[], 'np':[], 'tp':[]})
plas_rdf = df_plas
else:
plas_rdf = df_plas.set_index('time').resample('1min').mean().reset_index(drop=False)
plas_rdf.set_index(pd.to_datetime(plas_rdf['time']), inplace=True)
if mag_rdf.shape[0] != 0:
plas_rdf = plas_rdf.drop(columns=['time'])
magplas_rdf = pd.concat([mag_rdf, plas_rdf], axis=1)
magplas_rdf = magplas_rdf.drop(columns=['time'])
magplas_rdf['time'] = magplas_rdf.index
magplas_rdf = magplas_rdf.reset_index(drop=True)
return magplas_rdf
"""
WIND DATA SAVING FUNCTIONS:
"""
def create_wind_mag_pkl(start_timestamp, end_timestamp, coord_sys:str, output_path=wind_path):
df_mag = get_windmag_range(start_timestamp, end_timestamp, coord_sys)
if df_mag is None:
print(f'Wind MAG data is empty for this timerange')
df_mag = pd.DataFrame({'time':[], 'bt':[], 'bx':[], 'by':[], 'bz':[]})
rarr = fgen.make_mag_recarray(df_mag)
start = start_timestamp.date()
end = end_timestamp.date()
datestr_start = f'{start.year}{start.month:02}{start.day:02}'
datestr_end = f'{end.year}{end.month:02}{end.day:02}'
#create header
header='Science level magnetometer (MFI) data from Wind, sourced from https://cdaweb.gsfc.nasa.gov/pub/data/wind/mfi/.'+\
' Timerange: '+rarr.time[0].strftime("%Y-%b-%d %H:%M")+' to '+rarr.time[-1].strftime("%Y-%b-%d %H:%M")+'.'+\
' Magnetometer data available in original cadence of 1 min, units in nT.'+\
' Available coordinate systems include GSE, GSM, and RTN. GSE and GSM data are taken directly from wi_h0_mfi files, RTN data from wi_h3-rtn_mfi.'+\
' The data are available in a numpy recarray, fields can be accessed by wind.time, wind.bt, wind.bx, wind.by, wind.bz.'+\
' Made with script by E. E. Davies (github @ee-davies, sc-data-functions). File creation date: '+\
datetime.now(timezone.utc).strftime("%Y-%b-%d %H:%M")+' UTC'
#dump to pickle file
pickle.dump([rarr,header], open(output_path+f'wind_mag_{coord_sys}_{datestr_start}_{datestr_end}.p', "wb"))
def create_wind_plas_pkl(start_timestamp, end_timestamp, coord_sys:str, output_path=wind_path):
df_plas = get_windswe_range(start_timestamp, end_timestamp, coord_sys)
if df_plas is None:
print(f'Wind SWE data is empty for this timerange')
df_plas = pd.DataFrame({'time':[], 'vt':[], 'vx':[], 'vy':[], 'vz':[], 'np':[], 'tp':[]})
rarr = fgen.make_plas_recarray(df_plas)
start = start_timestamp.date()
end = end_timestamp.date()
datestr_start = f'{start.year}{start.month:02}{start.day:02}'
datestr_end = f'{end.year}{end.month:02}{end.day:02}'
#create header
header='Science level plasma (SWE) data from Wind, sourced from https://cdaweb.gsfc.nasa.gov/pub/data/wind/swe/.'+\
' Timerange: '+rarr.time[0].strftime("%Y-%b-%d %H:%M")+' to '+rarr.time[-1].strftime("%Y-%b-%d %H:%M")+'.'+\
' Plasma data available in original cadence of ~ 92 seconds. Parameters obtained from non-linear fitting to the ion CDF, rather than moment analysis (available by request).'+\
' Units: proton velocity [km/s], proton temperature => proton thermal speed [km/s], proton number density [n/cc].'+\
' Available coordinate systems include GSE, GSM, and RTN. GSE are taken directly from wi_h1_swe files, GSM data has been converted using data_frame_transforms based on Hapgood 1992.'+\
' RTN data is taken directly from wi_h1_swe_rtn, except for the years 2010--2014 (inclusive). Where RTN files are unavailable, original GSE files are converted to RTN using data_frame_transforms (Hapgood 1992 and spice kernels).'+\
' The data are available in a numpy recarray, fields can be accessed by wind.time, wind.vt, wind.vx, wind.vy, wind.vz, wind.np, and wind.tp.'+\
' Made with script by E. E. Davies (github @ee-davies, sc-data-functions). File creation date: '+\
datetime.now(timezone.utc).strftime("%Y-%b-%d %H:%M")+' UTC'
#dump to pickle file
pickle.dump([rarr,header], open(output_path+f'wind_plas_{coord_sys}_{datestr_start}_{datestr_end}.p', "wb"))
def create_wind_pos_pkl(start_timestamp, end_timestamp, coord_sys:str, output_path=wind_path):
df_pos = get_wind_positions(start_timestamp, end_timestamp, coord_sys)
if df_pos is None:
print(f'Wind predicted orbit data is empty for this timerange')
df_pos = pd.DataFrame({'time':[], 'x':[], 'y':[], 'z':[], 'r':[], 'lat':[], 'lon':[]})
rarr = fgen.make_pos_recarray(df_pos)
start = start_timestamp.date()
end = end_timestamp.date()
datestr_start = f'{start.year}{start.month:02}{start.day:02}'
datestr_end = f'{end.year}{end.month:02}{end.day:02}'
#create header
header='Predicted orbit data from Wind, sourced from https://cdaweb.gsfc.nasa.gov/pub/data/wind/orbit/pre_or/.'+\
' Timerange: '+rarr.time[0].strftime("%Y-%b-%d %H:%M")+' to '+rarr.time[-1].strftime("%Y-%b-%d %H:%M")+'.'+\
' Orbit available in original cadence of 10 minutes.'+\
' Units: xyz [km], r [AU], lat/lon [deg].'+\
' Available coordinate systems include GSE, GSM, J2000 GCI, HEC, HEE, HAE, and HEEQ. GSE, GSM, J2000 GCI and HEC are taken directly from wi_or_pre files, others using data_frame_transforms based on Hapgood 1992 and spice kernels.'+\
' The data are available in a numpy recarray, fields can be accessed by wind.x, wind.y, wind.z, wind.r, wind.lat, and wind.lon.'+\
' Made with script by E. E. Davies (github @ee-davies, sc-data-functions). File creation date: '+\
datetime.now(timezone.utc).strftime("%Y-%b-%d %H:%M")+' UTC'
#dump to pickle file
pickle.dump([rarr,header], open(output_path+f'wind_pos_{coord_sys}_{datestr_start}_{datestr_end}.p', "wb"))
def make_yearly_pkl_files(start_timestamp, end_timestamp, data_type:str, coord_sys:str, output_path=wind_path):
start = start_timestamp.year
end = end_timestamp.year
while start <= end:
if data_type == 'MAG':
create_wind_mag_pkl(datetime(start, 1, 1), datetime(start, 12, 31), coord_sys, output_path)
elif data_type == 'PLAS':
create_wind_plas_pkl(datetime(start, 1, 1), datetime(start, 12, 31), coord_sys, output_path)
elif data_type == 'POS':
create_wind_pos_pkl(datetime(start, 1, 1), datetime(start, 12, 31), coord_sys, output_path)
print(f'Finished creating pkl file for Wind {data_type} {start}')
start += 1
def create_wind_all_pkl(start_timestamp, end_timestamp, data_coord_sys='RTN', pos_coord_sys='HEEQ', output_path=wind_path):
#MAG DATA
df_mag = get_windmag_range(start_timestamp, end_timestamp, data_coord_sys)
if df_mag is None:
print(f'Wind MAG data is empty for this timerange')
df_mag = pd.DataFrame({'time':[], 'bt':[], 'bx':[], 'by':[], 'bz':[]})
mag_rdf = df_mag.drop(columns=['time'])
else:
mag_rdf = df_mag.set_index('time').resample('1min').mean().reset_index(drop=False)
mag_rdf.set_index(pd.to_datetime(mag_rdf['time']), inplace=True)
#PLASMA DATA
df_plas = get_windswe_range(start_timestamp, end_timestamp, data_coord_sys)
if df_plas is None:
print(f'WIND SWE data is empty for this timerange')
df_plas = pd.DataFrame({'time':[], 'vt':[], 'vx':[], 'vy':[], 'vz':[], 'np':[], 'tp':[]})
plas_rdf = df_plas
else:
plas_rdf = df_plas.set_index('time').resample('1min').mean().reset_index(drop=False)
plas_rdf.set_index(pd.to_datetime(plas_rdf['time']), inplace=True)
if mag_rdf.shape[0] != 0:
plas_rdf = plas_rdf.drop(columns=['time'])
#Combine MAG and PLASMA dfs
magplas_rdf = pd.concat([mag_rdf, plas_rdf], axis=1)
#some timestamps may be NaT so after joining, drop time column and reinstate from combined index col
magplas_rdf = magplas_rdf.drop(columns=['time'])
magplas_rdf['time'] = magplas_rdf.index
#POSITION DATA
df_pos = get_wind_positions(start_timestamp, end_timestamp, pos_coord_sys)
if df_pos is None:
print(f'Wind POS data is empty for this timerange')
df_pos = pd.DataFrame({'time':[], 'x':[], 'y':[], 'z':[], 'r':[], 'lat':[], 'lon':[]})
pos_rdf = df_pos
else:
pos_rdf = df_pos.set_index('time').resample('1min').interpolate(method='linear').reset_index(drop=False)
pos_rdf.set_index(pd.to_datetime(pos_rdf['time']), inplace=True)
if pos_rdf.shape[0] != 0:
pos_rdf = pos_rdf.drop(columns=['time'])
#Combine again:
comb_df_nans = pd.concat([magplas_rdf, pos_rdf], axis=1)
comb_df = comb_df_nans[comb_df_nans['bt'].notna()]
#Create rec array
rarr = fgen.make_combined_recarray(comb_df)
#Make header for pickle file
start = start_timestamp.date()
end = end_timestamp.date()
datestr_start = f'{start.year}{start.month:02}{start.day:02}'
datestr_end = f'{end.year}{end.month:02}{end.day:02}'
header='Science level magnetometer (MFI) data from Wind, sourced from https://cdaweb.gsfc.nasa.gov/pub/data/wind/mfi/.'+\
' Available coordinate systems include GSE, GSM, and RTN. GSE and GSM data are taken directly from wi_h0_mfi files, RTN data from wi_h3-rtn_mfi.'+\
' The data are available in a numpy recarray, fields can be accessed by wind.time, wind.bt, wind.bx, wind.by, wind.bz.'+\
' Science level plasma (SWE) data from Wind, sourced from https://cdaweb.gsfc.nasa.gov/pub/data/wind/swe/.'+\
' Parameters obtained from non-linear fitting to the ion CDF, rather than moment analysis (available by request).'+\
' Units: proton velocity [km/s], proton temperature => proton thermal speed [km/s], proton number density [n/cc].'+\
' Available coordinate systems include GSE, GSM, and RTN. GSE are taken directly from wi_h1_swe files, GSM data has been converted using data_frame_transforms based on Hapgood 1992.'+\
' RTN data is taken directly from wi_h1_swe_rtn, except for the years 2010--2014 (inclusive). Where RTN files are unavailable, original GSE files are converted to RTN using data_frame_transforms (Hapgood 1992 and spice kernels).'+\
' The data are available in a numpy recarray, fields can be accessed by wind.time, wind.vt, wind.vx, wind.vy, wind.vz, wind.np, and wind.tp.'+\
' Predicted orbit data from Wind, sourced from https://cdaweb.gsfc.nasa.gov/pub/data/wind/orbit/pre_or/.'+\
' Units: xyz [km], r [AU], lat/lon [deg].'+\
' Available coordinate systems include GSE, GSM, J2000 GCI, HEC, HEE, HAE, and HEEQ. GSE, GSM, J2000 GCI and HEC are taken directly from wi_or_pre files, others using data_frame_transforms based on Hapgood 1992 and spice kernels.'+\
' The data are available in a numpy recarray, fields can be accessed by wind.x, wind.y, wind.z, wind.r, wind.lat, and wind.lon.'+\
' All data resampled to cadence of 1 min. Position data has been linearly interpolated.'+\
' Made with script by E. E. Davies (github @ee-davies, sc-data-functions). File creation date: '+\
datetime.now(timezone.utc).strftime("%Y-%b-%d %H:%M")+' UTC'
pickle.dump([rarr,header], open(output_path+f'wind_data_{data_coord_sys}_pos_{pos_coord_sys}_{datestr_start}_{datestr_end}.p', "wb"))
# def transform_data(df, instrument, coord_system):
# #TODO: apply logic to handle b and v data transformations here
# if instrument == 'mag':
# prefix = 'b'
# elif instrument == 'swe' or instrument == '3dp_pm':
# prefix = 'v'
# if coord_system == 'rtn_approx':
# df[f'{prefix}_x'] = -1 * df[f'{prefix}_x']
# df[f'{prefix}_y'] = -1 * df[f'{prefix}_y']
# else:
# raise ValueError('transform does not exist yet, rtn_approx does exist')
# return df