-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadEISCAT.py
75 lines (51 loc) · 1.73 KB
/
loadEISCAT.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
#!/usr/bin/env python
# coding: utf-8
# In[2]:
import scipy.io
import numpy as np
from os import listdir
from os.path import isfile, join
def loadEISCATdata(directory, identifiers):
"""
loads a list of variables from a directorz containing EISCAT data files
Parameters:
directory: path to the EISCAT .mat files
string
identifiers: Variables as a list of strings
ndarray
"""
data = np.empty(len(identifiers), dtype = 'object')
for i, var in enumerate(identifiers):
data_files = [f for f in listdir(directory) if isfile(join(directory, f)) and f[-3:] == 'mat']
n = len(data_files)
mat_data = [scipy.io.loadmat(directory + file) for file in sorted(data_files)]
data[i] = mat_extract_variable(mat_data, var)
del mat_data
return data
def mat_extract_variable(loaded_mat, var):
"""
Extracts data for variable var from pre-loaded matfiles:
loaded_mat = scipy.io.loadmat(path_to_file)
data = loaded_mat('var')
CAN HANDLE JAGGED ARRAYS (at least in 1 dimension)
Parameters:
loaded_mat: preloaded mat files as a list
ndarry
var: Variable to be extracted
string
Returns:
data: all data saved in the loaded_mat['var']
"""
n = len(loaded_mat)
var_data = np.array([data[var] for data in loaded_mat])
max_shape = np.amax([i.shape for i in var_data], axis = 0)
array_size = [n, *max_shape]
data = np.empty(array_size, dtype = 'object')
for i in range(n):
data[i, :len(var_data[i])] = var_data[i]
if max_shape.shape == (2,):
if max_shape[1] == 1:
data = data.reshape(array_size[:-1])
return data.astype('float')
# In[5]:
# In[ ]: