-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_ghcn_graph.py
76 lines (56 loc) · 1.76 KB
/
example_ghcn_graph.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
#!/usr/bin/env python3
# coding: utf-8
import os
import numpy as np
from matplotlib import pyplot as plt
import pygsp as pg
# plt.rc('font', family='Latin Modern Roman')
plt.rc('text', usetex=True)
plt.rc('text.latex', preamble=r'\usepackage{lmodern}')
stations = np.load('ghcn_stations_2010-2014.npz')
data = np.load('ghcn_data_2010-2014_TMAX.npz')
keep = data['valid_days'].flatten()
data = data['data'].reshape(len(stations['id_ghcn']), -1)
data = data[:, keep]
data = data / 10
# Show the same stations as for the temperature plot.
year, month, day = 2014, 1, 1
t = (year-2010)*365 + (month-1)*30 + (day-1)
keep = ~np.isnan(data[:, t])
data = data[keep]
lon = stations['lon'][keep]
lat = stations['lat'][keep]
print('n_stations: {}, n_days: {}'.format(*data.shape))
# Rotate the view.
lon -= 50
lat -= 20
lon *= np.pi / 180
lat *= np.pi / 180
x = np.cos(lat) * np.cos(lon)
y = np.cos(lat) * np.sin(lon)
z = np.sin(lat)
positions = np.stack([x, y, z], axis=1)
graph = pg.graphs.NNGraph(positions, k=20)
print(graph)
graph.set_signal(np.clip(data[:, t], -20, 40), 'temperature')
graph = graph.subgraph(x > 0)
print(graph)
graph.plotting.update({
'elevation': 0,
'azimuth': 0,
'distance': 8,
'vertex_size': 10,
'vertex_color': (0, 0, 0, 1),
'edge_color': (0.5, 0.5, 0.5, 0.2),
})
graph.coords = graph.coords[:, 1:]
fig = plt.figure(figsize=(4, 4))
ax = fig.add_subplot(1, 1, 1)
graph.plot(edges=True, title='graph of GHCN stations', ax=ax)
#fig, ax = graph.plot(graph.signals['temperature'], edges=True, title='')
#ax.scatter(graph.coords[:, 0], graph.coords[:, 1], 10, graph.signals['temperature'], cmap='RdYlBu_r')
ax.axis('equal')
ax.axis('off')
#fig.tight_layout()
filename = os.path.splitext(os.path.basename(__file__))[0] + '.pdf'
fig.savefig(filename)