-
Notifications
You must be signed in to change notification settings - Fork 0
/
tool_scripts.py
62 lines (52 loc) · 1.77 KB
/
tool_scripts.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
import numpy as np
import pandas as pd
import yaml
def array_stations(data_stations):
array_lon = []
array_lat = []
for project in data_stations:
for station in data_stations[project]:
lon = data_stations[project][station]['lon']
lat = data_stations[project][station]['lat']
array_lon = array_lon + [lon]
array_lat = array_lat + [lat]
return [array_lon, array_lat]
def find_nearest_value_index(array, value):
array = np.asarray(array)
idx = (np.abs(array - value)).argmin()
return [array[idx], idx]
def get_yaml(arg):
yaml_file=arg
file = open(yaml_file).read()
return yaml.load(file)
def start_end_time_ww3(dtime):
start = pd.to_datetime(str(dtime[0].values)).strftime('%Y-%m-%d')
end = pd.to_datetime(str(dtime[-1].values)).strftime('%Y-%m-%d')
return [start, end]
def getuv(wdir):
wdir_radian = _check_radians(np.radians(wdir), max_radians=4 * np.pi)
# import pdb; pdb.set_trace()
u_wave_dir = -np.sin(wdir_radian)
v_wave_dir = -np.cos(wdir_radian)
return u_wave_dir, v_wave_dir
def _check_radians(value, max_radians=2 * np.pi):
"""Input validation of values that could be in degrees instead of radians.
Parameters
----------
value : `pint.Quantity`
The input value to check.
max_radians : float
Maximum absolute value of radians before warning.
Returns
-------
`pint.Quantity`
The input value
"""
try:
value = value.to('radians').m
except AttributeError:
pass
if np.greater(np.nanmax(np.abs(value)), max_radians):
warnings.warn('Input over {} radians. '
'Ensure proper units are given.'.format(max_radians))
return value