-
Notifications
You must be signed in to change notification settings - Fork 1
/
read_trwind.py
executable file
·69 lines (55 loc) · 1.94 KB
/
read_trwind.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
import xarray as xr
import sys
import numpy as np
def read_trwind_file(file,latcoord,loncoord,vert_coord):
'''
Reads nc file containing tangential/radial winds as well as tc lat and lon centers created by calc trwind scripts
Inputs
------
file : str
path to input nc file
latcoord : str
name of latitude coordinate in nc file
loncoord : str
name of longitude coordinate in nc file
vert_coord : str
the vertical coordinate that is going to be read, should correspond to what was used to create the input file
Outputs
-------
lons : numpy.ndarray
2d array of lons (nlat,nlon)
lats : numpy.ndarray
2d array of lats (nlat,nlon)
verts : numpy.ndarray
1d array containing the values for the vertical coordinate
twind : numpy.ndarray
3d array of tangential winds in m/s (nvert,nlat,nlon)
rwind : numpy.ndarray
3d array of radial winds in m/s (nvert,nlat,nlon)
tc_lons : numpy.ndarray
1d array of tc center longitudes (nvert)
tc_lats : numpy.ndarray
1d array of tc center latitudes (nvert)
'''
#Check that file has .nc extension
if not file.endswith(".nc"):
sys.exit("Input must be a netcdf (.nc) file... exiting")
#Read nc file with xarray
data = xr.open_dataset(file)
#Exctract lats and lons, if not 2d, put them in a mesh
lats = data[latcoord].values
lons = data[loncoord].values
if lats.ndim == 1 and lons.ndim == 1:
lons,lats = np.meshgrid(lons,lats)
#Get vertical coordinate
try:
verts = data.coords[vert_coord].values
except:
print("Vertical coordinate {} could not be found in file... exiting".format(vert_coord))
#Read tangential and radial winds
twind = data.twind.values
rwind = data.rwind.values
#TC lat and lon centers
tc_lons = data.tc_center_lons.values
tc_lats = data.tc_center_lats.values
return lons,lats,verts,twind,rwind,tc_lons,tc_lats