-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
325 lines (244 loc) · 7.29 KB
/
tools.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
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
"""
NAME:
tools
PURPOSE:
Utility tools and functions
CONTAINS:
does_directory_exist
create_dir
convert_unix_times
calculate_day_of_year
calculate_hour_of_day
"""
#==========================
# Standard library imports
#==========================
import numpy as np
from os import mkdir
from os.path import isdir
import pandas as pd
import sys
def does_directory_exist(dir):
"""
Check if directory exist
Arguments:
----------
+ dir: directory to check
Returns:
--------
isdir(dir): logical
"""
return isdir(dir)
def create_dir(dir):
"""
Create a new directory
Arguments:
----------
+ dir: directory to check
Returns:
--------
"""
mkdir(dir)
def convert_unix_times(unix_time):
"""
Convert from unix time to time string and datetime
Arguments:
----------
+ unix_time: Unix time
Returns:
--------
+ timestring_time: Time string array
+ datetime_time: Datetime array
"""
# Get dimensions of unix_time
dims = unix_time.shape#[0]
# Convert to pandas data frame
df = pd.DataFrame(data=unix_time.flatten(),columns=['date'])
# Convert from unix time to datetime
df['date'] = pd.to_datetime(df['date'],unit='s')
# Convert to pandas datetime index
dates = pd.DatetimeIndex(df['date'])
# Get the datetime array
datetime_time = np.array(dates.tolist()).reshape(dims)#.T
# datetime_time = np.array([np.datetime64(date) for date in dates.tolist()]).reshape(dims)
# Get the dates and convert them to strings
df = pd.DataFrame(data=np.char.mod('%04d', dates.year.values),columns=['dates'])
df['dates'] = df['dates'].astype(str)
years = df.to_numpy()
df = pd.DataFrame(data=np.char.mod('%02d', dates.month.values),columns=['dates'])
df['dates'] = df['dates'].astype(str)
months = df.to_numpy()
df = pd.DataFrame(data=np.char.mod('%02d', dates.day.values),columns=['dates'])
df['dates'] = df['dates'].astype(str)
days = df.to_numpy()
df = pd.DataFrame(data=np.char.mod('%02d', dates.hour.values),columns=['dates'])
df['dates'] = df['dates'].astype(str)
hours = df.to_numpy()
# Get the time stamp
timestamps = years + months + days + hours
timestring_time = timestamps.reshape(dims).T
return timestring_time, datetime_time
def calculate_day_of_year(unix_time):
"""
Calculate day of year
Arguments:
----------
+ unix_time: Unix timestamp
Returns:
--------
doy: Day of year array
"""
# Dimensions
dims = unix_time.shape
# Convert to pandas data frame
df = pd.DataFrame(data=unix_time.flatten(),columns=['date'])
# Convert from unix time to datetime
df['date'] = pd.to_datetime(df['date'],unit='s')
# Convert to pandas datetime index
doy = np.array(pd.DatetimeIndex(df['date']).dayofyear.tolist()).reshape(dims)
return doy
def calculate_hour_of_day(unix_time):
"""
Calculate hour of day
Arguments:
----------
+ unix_time: Unix timestamp
Returns:
--------
hod: Hour of day array
"""
# Dimensions
dims = unix_time.shape
# Convert to pandas data frame
df = pd.DataFrame(data=unix_time.flatten(),columns=['date'])
# Convert from unix time to datetime
df['date'] = pd.to_datetime(df['date'],unit='s')
# Convert to pandas datetime index
hod = np.array(pd.DatetimeIndex(df['date']).hour.tolist()).reshape(dims)
return hod
def nan_helper(y):
"""
Helper to handle indices and logical indices of NaNs.
Arguments:
----------
+ y, 1d numpy array with possible NaNs
Returns:
--------
+ nans, logical indices of NaNs
+ index, a function, with signature indices= index(logical_indices),
to convert logical indices of NaNs to 'equivalent' indices
Example:
>>> # linear interpolation of NaNs
>>> nans, x= nan_helper(y)
>>> y[nans]= np.interp(x(nans), x(~nans), y[~nans])
"""
return np.isnan(y), lambda z: z.nonzero()[0]
def consecutive_number_ranges(nums):
"""
Calculate ranges with consecutive numbers
Arguments:
----------
+ nums: List of numbers
Returns:
--------
+ List with start and end index (open end) for ranges with consecutive numbers
"""
nums = sorted(set(nums))
gaps = [[s, e] for s, e in zip(nums, nums[1:]) if s+1 < e]
edges = iter(nums[:1] + sum(gaps, []) + nums[-1:])
# Return ranges - with open end; [start, end)
return [(s, e+1) for s, e in zip(edges, edges)]
# # Return ranges - with closed end; [start, end]
# return list(zip(edges, edges))
def remove_structured_field_name(a, name):
"""
Remove field from structred array
Arguments:
----------
+ a: Structured array
+ name: Name of field to remove
Returns:
+ b: Structured array, equal to a, but without field "name"
"""
names = list(a.dtype.names)
if name in names:
names.remove(name)
b = a[names]
return b
def shift(arr, num, fill_value=np.nan):
"""
Shift 1D array
Arguments:
+ arr: Input array, 1D
+ num: The number used for shifting the array
+ fill_value: Number to fill the shifted spot(s) without data with
Returns:
arr: Shifted array, 1D
"""
arr = np.roll(arr,num)
if num < 0:
arr[num:] = fill_value
elif num > 0:
arr[:num] = fill_value
return arr
def shift2d(arr, num, ax, fill_value=np.nan):
"""
Shift 2D array
Arguments:
+ arr: Input array, 1D
+ num: The number used for shifting the array
+ ax: Axis to shift on
+ fill_value: Number to fill the shifted spot(s) without data with
Returns:
arr: Shifted array, 2D
"""
arr = np.roll(arr,num,ax)
if ax == 0:
if num < 0:
arr[num:,:] = fill_value
elif num > 0:
arr[:num,:] = fill_value
if ax == 1:
if num < 0:
arr[:,num:] = fill_value
elif num > 0:
arr[:,:num] = fill_value
return arr
def roll_odd_data(dims,data_unrolled,fill_val):
"""
Shift only the odd elements of the first axis in a 2D array
Arguments:
----------
+ dims: Dimensions of the full array
+ data_unrolled: 2D array for which odd elements are to be shifted
+ fill_value: Fill value
Returns:
+ data_roll: 2D array for which odd elements have been shifted
"""
# Initialize data
data_roll = np.full((dims), fill_value=np.nan)
# Even array
data_even = data_unrolled[::2,:]
# Odd array
data_odd = data_unrolled[1::2,:]
# Roll the odd array
data_odd_roll = shift2d(data_odd,1,1,fill_value=fill_val)#np.expand_dims(data_odd[:,0],1))
# Add even and rolled odd array to new array
data_roll[::2,:] = data_even
data_roll[1::2,:] = data_odd_roll
return data_roll
def cyclic_encoding(data,data_max):
"""
Cyclic encoding of data
Arguments:
----------
+ data: data to be encoded as cyclic
+ data_max: Max value for data (theorethical!)
Returns:
--------
sin_data: Cyclic sine encoding of data
cos_data: Cyclis cosine ecoding of data
"""
sin_data = np.sin(2 * np.pi * data/data_max)
cos_data = np.cos(2 * np.pi * data/data_max)
return sin_data, cos_data