-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrb_utils.py
123 lines (100 loc) · 2.36 KB
/
grb_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
"""Support functions
"""
import numpy
import pygrib
ww3_f = "data/nww3.t00z.grib.grib2.grb"
# Region lat/lon bounding boxes
region_box = {
"gold_coast": {
"lat": [-31, -25],
"lon": [152, 157]
}
}
# Era5 variable names
var_names = [
u'10 metre U wind component',
u'10 metre V wind component',
u'Mean direction of total swell',
u'Mean direction of wind waves',
u'Mean period of total swell',
u'Mean period of wind waves',
u'Mean wave direction',
u'Mean wave direction of first swell partition',
u'Mean wave direction of second swell partition',
u'Mean wave period',
u'Mean wave period based on first moment for swell',
u'Mean wave period of first swell partition',
u'Mean wave period of second swell partition',
u'Significant height of combined wind waves and swell',
u'Significant height of wind waves'
]
var_names_short = [
u'10u',
u'10v',
u'mdts',
u'mdww',
u'mpts',
u'mpww',
u'mwd',
u'mwd1',
u'mwd2',
u'mwp',
u'mwp1',
u'mwp2',
u'p1ps',
u'shww',
u'swh'
]
# WW3 variable names
ww3_var_names = [
u"Direction of wind waves",
u"Mean period of wind waves",
u"Primary wave direction",
u"Primary wave mean period",
u"Significant height of combined wind waves and swell",
u"Significant height of wind waves",
u"U component of wind",
u"V component of wind"
]
ww3_var_names_short = [
u"wvdir",
u"mpww",
u"dirpw",
u"perpw",
u"swh",
u"shww",
u"u",
u"v"
]
# Wavewatch III lat/lon grid
def ww3_grid(in_ww3_grb=None):
"""
Return the lat and lon grid locations
for WW3 model.
Uses `ww3_f` as input but this could be
any WW3 output file
"""
if in_ww3_grb is None:
# Read file
in_ww3_grb = pygrib.open(ww3_f)
# Get gribmessage
grb = in_ww3_grb.read(1)[0]
lat, lon = grb.latlons()
# Reset
in_ww3_grb.seek(0)
return lat, lon
def ww3_mask(lat_range, lon_range, in_ww3_grb=None):
"""
"""
# Read file
if in_ww3_grb is None:
in_ww3_grb = pygrib.open(ww3_f)
# Get gribmessage
grb = in_ww3_grb.read(1)[0]
# Get data
data = grb.data(lat1=lat_range[0], lat2=lat_range[1],
lon1=lon_range[0], lon2=lon_range[1])
# Reset
in_ww3_grb.seek(0)
# Return mask
return data[0].mask