/
lib_gs.py
179 lines (154 loc) · 7.02 KB
/
lib_gs.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
##########################################################################
# TP - Introduction à l'interpolation spatiale et aux géostatistiques #
##########################################################################
# P. Bosser / ENSTA Bretagne
# Version du 24/02/2021
# Numpy
import numpy as np
# Matplotlib / plot
import matplotlib.pyplot as plt
################## Modèle de fonction d'interpolation ##################
def interp_xxx(x_obs, y_obs, z_obs, x_int, y_int):
# Interpolation par ???
# x_obs, y_obs, z_obs : observations
# [np.array dimension 1*n]
# x_int, y_int, positions pour lesquelles on souhaite interpoler une valeur z_int
# [np array dimension m*p]
z_int = np.nan*np.zeros(x_int.shape)
#
# ...
#
return z_int
####################### Fonctions d'interpolation ######################
def interp_lin(x_obs, y_obs, z_obs, x_int, y_int):
# Interpolation par ???
# x_obs, y_obs, z_obs : observations
# [np.array dimension 1*n]
# x_int, y_int, positions pour lesquelles on souhaite interpoler une valeur z_int
# [np array dimension m*p]
z_int = np.nan*np.zeros(x_int.shape)
tri = Delaunay(np.array([x_obs, y_obs]))
#
# ... à vous de jouer !!!
#
return z_int
def interp_ppv(x_obs, y_obs, z_obs, x_int, y_int):
# Interpolation par plus proche voisin
# x_obs, y_obs, z_obs : observations
# [np.array dimension 1*n]
# x_int, y_int, positions pour lesquelles on souhaite interpoler une valeur z_int
# [np array dimension m*p]
z_int = np.nan*np.zeros(x_int.shape)
for i in np.arange(0,x_int.shape[0]):
for j in np.arange(0,x_int.shape[1]):
z_int[i,j] = z_obs[np.argmin(np.sqrt((x_int[i,j]-x_obs)**2+(y_int[i,j]-y_obs)**2))]
return z_int
############################# Visualisation ############################
def plot_contour_2d(x_grd ,y_grd ,z_grd, x_obs = np.array([]) ,y_obs = np.array([]), xlabel = "", ylabel = "", title = "", fileo = ""):
# Tracé du champ interpolé sous forme d'isolignes
# x_grd, y_grd, z_grd : grille de valeurs interpolées
# x_obs, y_obs : observations (facultatif)
# xlabel, ylabel : étiquettes des axes (facultatif)
# title : titre (facultatif)
# fileo : nom du fichier d'enregistrement de la figure (facultatif)
z_grd_m = np.ma.masked_invalid(z_grd)
fig = plt.figure()
plt.contour(x_grd, y_grd, z_grd_m, int(np.round((np.max(z_grd_m)-np.min(z_grd_m))/4)),colors ='k')
if x_obs.shape[0]>0:
plt.scatter(x_obs, y_obs, marker = 'o', c = 'k', s = 5)
plt.xlim(0.95*np.min(x_obs),np.max(x_obs)+0.05*np.min(x_obs))
plt.ylim(0.95*np.min(y_obs),np.max(y_obs)+0.05*np.min(y_obs))
else:
plt.xlim(0.95*np.min(x_grd),np.max(x_grd)+0.05*np.min(x_grd))
plt.ylim(0.95*np.min(y_grd),np.max(y_grd)+0.05*np.min(y_grd))
plt.gca().set_aspect('equal', adjustable='box')
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title(title)
plt.grid()
if not fileo == "": plt.savefig(fileo,bbox_inches='tight')
def plot_surface_2d(x_grd ,y_grd ,z_grd, x_obs = np.array([]) ,y_obs = np.array([]), minmax = [0,0], xlabel = "", ylabel = "", zlabel = "", title = "", fileo = ""):
# Tracé du champ interpolé sous forme d'une surface colorée
# x_grd, y_grd, z_grd : grille de valeurs interpolées
# x_obs, y_obs : observations (facultatif)
# minmax : valeurs min et max de la variable interpolée (facultatif)
# xlabel, ylabel, zlabel : étiquettes des axes (facultatif)
# title : titre (facultatif)
# fileo : nom du fichier d'enregistrement de la figure (facultatif)
from matplotlib import cm
z_grd_m = np.ma.masked_invalid(z_grd)
fig = plt.figure()
if minmax[0] < minmax[-1]:
p=plt.pcolormesh(x_grd, y_grd, z_grd_m, cmap=cm.terrain, vmin = minmax[0], vmax = minmax[-1], shading = 'auto')
else:
p=plt.pcolormesh(x_grd, y_grd, z_grd_m, cmap=cm.terrain, shading = 'auto')
if x_obs.shape[0]>0:
plt.scatter(x_obs, y_obs, marker = 'o', c = 'k', s = 5)
plt.xlim(0.95*np.min(x_obs),np.max(x_obs)+0.05*np.min(x_obs))
plt.ylim(0.95*np.min(y_obs),np.max(y_obs)+0.05*np.min(y_obs))
else:
plt.xlim(0.95*np.min(x_grd),np.max(x_grd)+0.05*np.min(x_grd))
plt.ylim(0.95*np.min(y_grd),np.max(y_grd)+0.05*np.min(y_grd))
plt.gca().set_aspect('equal', adjustable='box')
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title(title)
plt.grid()
fig.colorbar(p,ax=plt.gca(),label=zlabel,fraction=0.046, pad=0.04)
if not fileo == "": plt.savefig(fileo,bbox_inches='tight')
def plot_points(x_obs, y_obs, xlabel = "", ylabel = "", title = "", fileo = ""):
# Tracé des sites d'observations
# x_obs, y_obs : observations
# xlabel, ylabel : étiquettes des axes (facultatif)
# title : titre (facultatif)
# fileo : nom du fichier d'enregistrement de la figure (facultatif)
fig = plt.figure()
ax = plt.gca()
plt.plot(x_obs, y_obs, 'ok', ms = 4)
ax.set_xlim(0.95*min(x_obs),max(x_obs)+0.05*min(x_obs))
ax.set_ylim(0.95*min(y_obs),max(y_obs)+0.05*min(y_obs))
ax.set_aspect('equal', adjustable='box')
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title(title)
plt.grid()
if not fileo == "": plt.savefig(fileo,bbox_inches='tight')
def plot_patch(x_obs, y_obs, z_obs, xlabel = "", ylabel = "", zlabel = "", title = "", fileo = ""):
# Tracé des valeurs observées
# x_obs, y_obs, z_obs : observations
# xlabel, ylabel, zlabel : étiquettes des axes (facultatif)
# title : titre (facultatif)
# fileo : nom du fichier d'enregistrement de la figure (facultatif)
from matplotlib import cm
fig = plt.figure()
p=plt.scatter(x_obs, y_obs, marker = 'o', c = z_obs, s = 80, cmap=cm.terrain)
plt.xlim(0.95*min(x_obs),max(x_obs)+0.05*min(x_obs))
plt.ylim(0.95*min(y_obs),max(y_obs)+0.05*min(y_obs))
plt.gca().set_aspect('equal', adjustable='box')
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title(title)
plt.grid()
fig.colorbar(p,ax=plt.gca(),label=zlabel,fraction=0.046, pad=0.04)
if not fileo == "": plt.savefig(fileo,bbox_inches='tight')
def plot_triangulation(x_obs, y_obs, xlabel = "", ylabel = "", title = "", fileo = ""):
# Tracé de la triangulation sur des sites d'observations
# x_obs, y_obs : observations
# xlabel, ylabel : étiquettes des axes (facultatif)
# title : titre (facultatif)
# fileo : nom du fichier d'enregistrement de la figure (facultatif)
from scipy.spatial import Delaunay as delaunay
tri = delaunay(np.hstack((x_obs,y_obs)))
plt.figure()
plt.triplot(x_obs[:,0], y_obs[:,0], tri.simplices)
plt.plot(x_obs, y_obs, 'or', ms=4)
plt.xlim(0.95*min(x_obs),max(x_obs)+0.05*min(x_obs))
plt.ylim(0.95*min(y_obs),max(y_obs)+0.05*min(y_obs))
plt.gca().set_aspect('equal', adjustable='box')
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title(title)
plt.grid()
if not fileo == "": plt.savefig(fileo,bbox_inches='tight')