-
Notifications
You must be signed in to change notification settings - Fork 0
/
point_regrid.py
134 lines (117 loc) · 4.61 KB
/
point_regrid.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
import sys
import cartopy.crs as ccrs
from cf_units import Unit
import iris
import iris_grib
import iris.quickplot as qplt
import matplotlib.pyplot as plt
import numpy as np
import numpy.ma as ma
def create_1d_coord(lower, upper, no, *args, **kwargs):
bounds_pre = np.linspace(lower, upper, no)
bounds = np.stack([bounds_pre[:-1],
bounds_pre[1:]],
axis=-1)
points = bounds.mean(axis=-1)
#print(bounds)
#print(points)
coord = iris.coords.DimCoord(
points,
*args,
bounds=bounds,
**kwargs
)
return coord
def create_tgt_cube():
no_lat = 120
no_lon = 276
lat = create_1d_coord(20, 50, no_lat+1,
'latitude',
long_name='latitude',
var_name='lat',
units=Unit('degrees_north'))
lon = create_1d_coord(-131, -62, no_lon+1,
'longitude',
long_name='longitude',
var_name='lon',
units=Unit('degrees_east'),
circular=False)
#timef = (tpoints, standard_name='time', long_name='time', var_name='time')
data = np.empty((no_lat, no_lon))
cube = iris.cube.Cube(data,
dim_coords_and_dims=[(lat, 0), (lon, 1)])
return cube
"""
def create_tgt_cube():
no_lat = 180
no_lon = 360
lat = create_1d_coord(-90, 90, no_lat+1,
'latitude',
long_name='latitude',
var_name='lat',
units=Unit('degrees_north'))
lon = create_1d_coord(0, 360, no_lon+1,
'longitude',
long_name='longitude',
var_name='lon',
units=Unit('degrees_east'),
circular=True)
data = np.empty((no_lat, no_lon))
cube = iris.cube.Cube(data,
dim_coords_and_dims=[(lat, 0), (lon, 1)])
return cube
"""
def get_range(coord):
min = coord.points.min()
max = coord.points.max()
return (min, max)
"""
def plot_global_cube(cube):
cube.units = Unit('degF')
cube.convert_units('degC')
ax = plt.subplot(1, 2, 1, projection=ccrs.PlateCarree())
qplt.pcolormesh(cube)
ax.coastlines()
"""
def plot_cube(cube):
cube.units = Unit('degK')
#cube.convert_units('degF')
#ax = plt.subplot(1, 2, 2, projection=ccrs.Mollweide())
proj = ccrs.LambertConformal(central_longitude=-97.5, central_latitude=38.5,
false_easting=0, false_northing=0,
standard_parallels=(38.5, 3))
fig = plt.figure(figsize=(8, 8), frameon=True)
ax = fig.add_axes([0.08, 0.05, 0.8, 0.94], projection=proj)
qplt.pcolormesh(cube, cmap='rainbow')
ax.coastlines(resolution='10m',lw=0.3)
def main():
filename = sys.argv[1]
cube = iris.load_cube(filename)
#print(cube.coord('time').points)
print(cube)
lat_range = get_range(cube.coord('latitude'))
lon_range = get_range(cube.coord('longitude'))
tgt_cube_global = create_tgt_cube()
rgr_cube_global = cube.regrid(tgt_cube_global,
iris.analysis.UnstructuredNearest())
rgr_cube = rgr_cube_global.intersection(latitude=lat_range,
longitude=lon_range)
radius=iris.fileformats.pp.EARTH_RADIUS
rgr_cube.coord(dimensions=[0]).coord_system=iris.coord_systems.GeogCS(radius)
rgr_cube.coord(dimensions=[1]).coord_system=iris.coord_systems.GeogCS(radius)
#rgr_cube.coord(axis='X').coord_system=iris.coord_systems.GeogCS(654321)
#rgr_cube.coord(axis='Y').coord_system=iris.coord_systems.GeogCS(654321)
#rgr_cube.coord(dimensions=[0]).coord_system
#rgr_cube.coord(dimensions=[1]).coord_system
#plot_global_cube(rgr_cube_global)
t_unit = Unit('seconds since 1970-01-01 00:00:00', calendar='gregorian')
rgr_cube.add_aux_coord(iris.coords.DimCoord(1541865240, standard_name='time', units=t_unit))
rgr_cube.add_aux_coord(iris.coords.DimCoord(0, standard_name='forecast_period', units='hours'))
rgr_cube.add_aux_coord(iris.coords.DimCoord(0, standard_name='height', units='m'))
rgr_cube.data = ma.masked_invalid(rgr_cube.data)
print(rgr_cube)
#plot_cube(rgr_cube)
iris.save(rgr_cube, '/home/awips/python-awips/ups/latest.grib2')
plt.show()
if __name__ == '__main__':
main()