-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
executable file
·230 lines (192 loc) · 8.22 KB
/
utils.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
###########################################
# helper functions
###########################################
import numpy as np
import pandas as pd
from calendar import monthrange, month_name
import datetime as dt
###########################################
def Round_To_n(x, n):
return round(x, -int(np.floor(np.sign(x) * np.log10(abs(x)))) + n)
###########################################
###########################################
def get_vertical_locations(category, location=None, reverse=False):
"""
Extract vertical locations from a group of variables (in a category, usually)
If no specific location is provided, return a sorted list of vertical locations,
otherwise, provided the variable name, vertical location, and index nearest to location
"""
vertlocs = [int(var.replace(' ', '').split('(')[-1].split('_')[-1].split('m')[0]) for var in category]
ind = sorted(range(len(vertlocs)), key=lambda k: vertlocs[k])
# sort vertical locations
vertlocs = [vertlocs[x] for x in ind]
category = [category[x] for x in ind]
if location is not None:
temp = np.array(vertlocs)
ind = int(np.argmin(np.abs(temp - location)))
category = category[ind]
vertlocs = vertlocs[ind]
if reverse is True:
category = [x for x in category[-1::-1]]
vertlocs = [x for x in vertlocs[-1::-1]]
return category, vertlocs, ind
###########################################
###########################################
def get_nearest_direction(metdat, directions, category):
"""Extract vertical locations from a group of variables (in a category, usually)"""
vertlocs = [int(var.replace(' ', '').split('(')[-1].split('_')[-1].split('m')[0]) for var in metdat[category]]
dirlocs = [int(var.replace(' ', '').split('(')[-1].split('_')[-1].split('m')[0]) for var in metdat[directions]]
# for loc in vertlocs:
dirind = [np.argmin(np.abs(np.array(dirlocs) - loc)) for loc in vertlocs]
catind = [vertlocs.index(loc) for loc in vertlocs]
return dirind, catind, vertlocs
###########################################
###########################################
def get_nearest_stability(metdat, stability, category):
"""Extract vertical locations from a group of variables (in a category, usually)"""
if isinstance(category, list):
vertlocs = [int(var.replace(' ', '').split('(')[-1].split('_')[-1].split('m')[0]) for var in metdat[category]]
stablocs = [int(var.replace(' ', '').split('(')[-1].split('_')[-1].split('m')[0]) for var in metdat[stability]]
elif isinstance(category, str):
vertlocs = [int(category.replace(' ', '').split('(')[-1].split('_')[-1].split('m')[0])]
stablocs = [int(var.replace(' ', '').split('(')[-1].split('_')[-1].split('m')[0]) for var in metdat[stability]]
# for loc in vertlocs:
stabind = [np.argmin(np.abs(np.array(stablocs) - loc)) for loc in vertlocs]
catind = [vertlocs.index(loc) for loc in vertlocs]
return stabind, catind, vertlocs
###########################################
###########################################
def monthnames():
months = ['January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December']
return months
###########################################
###########################################
def get_stabconds():
stabconds = ['Very Stable', 'Stable', 'Neutral', 'Unstable', 'Very Unstable']
return stabconds
###########################################
###########################################
# color info functions
###########################################
###########################################
def get_colors(ncolors, basecolor='cycle', reverse=False):
"""make a gradient of colors for use in plotting"""
# nrel official colors
nrelcolors = get_nrelcolors()
if isinstance(basecolor, list):
colors = basecolor#[ nrelcolors[basecolor[x]][1] for x in range(len(basecolor))]
cdict = polylinear_gradient(colors,ncolors+2)
colors = cdict['hex']
elif basecolor in nrelcolors:
nc = ncolors+2
colors = []
while len(colors) < ncolors:
nc += 1
colors = ['#D1D5D8',nrelcolors[basecolor][1],nrelcolors[basecolor][0]]
cdict = polylinear_gradient(colors,nc)
colors = cdict['hex']
del colors[2]
elif basecolor is 'cycle':
nc = ncolors+2
colors = []
while len(colors) < ncolors:
nc += 1
colors = ['#0079C2','#D1D5D8','#D9531E','#00A4E4']
cdict = polylinear_gradient(colors,nc)
colors = cdict['hex']
del colors[2]
elif basecolor is 'span':
colors = [nrelcolors['blue'][0], '#a1a5a7', nrelcolors['red'][0]]
cdict = polylinear_gradient(colors,ncolors+2)
colors = cdict['hex']
if reverse is True:
colors = colors[-1::-1]
return colors
def get_nrelcolors():
nrelcolors = {'blue': ['#0079C2','#00A4E4'],
'red': ['#933C06','#D9531E'],
'green': ['#3D6321','#5D9732'],
'gray': ['#3A4246','#5E6A71']}
return nrelcolors
def hex_to_RGB(hex):
''' "#FFFFFF" -> [255,255,255] '''
# Pass 16 to the integer function for change of base
return [int(hex[i:i+2], 16) for i in range(1,6,2)]
def RGB_to_hex(RGB):
''' [255,255,255] -> "#FFFFFF" '''
# Components need to be integers for hex to make sense
RGB = [int(x) for x in RGB]
return "#"+"".join(["0{0:x}".format(v) if v < 16 else
"{0:x}".format(v) for v in RGB])
def linear_gradient(start_hex, finish_hex="#FFFFFF", n=10):
''' returns a gradient list of (n) colors between
two hex colors. start_hex and finish_hex
should be the full six-digit color string,
inlcuding the number sign ("#FFFFFF") '''
# Starting and ending colors in RGB form
s = hex_to_RGB(start_hex)
f = hex_to_RGB(finish_hex)
# Initilize a list of the output colors with the starting color
RGB_list = [s]
# Calcuate a color at each evenly spaced value of t from 1 to n
for t in range(1, n):
# Interpolate RGB vector for color at the current value of t
curr_vector = [
int(s[j] + (float(t)/(n-1))*(f[j]-s[j]))
for j in range(3)
]
# Add it to our list of output colors
RGB_list.append(curr_vector)
return color_dict(RGB_list)
def color_dict(gradient):
''' Takes in a list of RGB sub-lists and returns dictionary of
colors in RGB and hex form for use in a graphing function
defined later on '''
return {"hex":[RGB_to_hex(RGB) for RGB in gradient],
"r":[RGB[0] for RGB in gradient],
"g":[RGB[1] for RGB in gradient],
"b":[RGB[2] for RGB in gradient]}
def polylinear_gradient(colors, n):
"""
returns a list of colors forming linear gradients between
all sequential pairs of colors. n specifies the total
number of desired output colors
"""
# The number of colors per individual linear gradient
n_out = int(float(n) / (len(colors) - 1))
# returns dictionary defined by color_dict()
gradient_dict = linear_gradient(colors[0], colors[1], n_out)
if len(colors) > 1:
for col in range(1, len(colors) - 1):
next = linear_gradient(colors[col], colors[col+1], n_out)
for k in ("hex", "r", "g", "b"):
# Exclude first point to avoid duplicates
gradient_dict[k] += next[k][1:]
return gradient_dict
###########################################
###########################################
def matlab_datenum_to_python_datetime(datenum):
"""
Parameters
----------
datenum : int
"""
if isinstance(datenum,int):
dateout = dt.datetime.fromordinal(int(datenum)) +\
dt.timedelta(days=datenum%1) - dt.timedelta(days = 366)
else:
dateout = [dt.datetime.fromordinal(int(date)) +\
dt.timedelta(days=date%1) - dt.timedelta(days = 366) for date in datenum]
return dateout
###########################################