1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | import string |
---|
4 | import numpy as np |
---|
5 | import matplotlib.pyplot as plt |
---|
6 | import ffgrid2 |
---|
7 | from pylab import * |
---|
8 | from mpl_toolkits.basemap import Basemap |
---|
9 | from mpl_toolkits.basemap import shiftgrid, cm |
---|
10 | import draw_map |
---|
11 | |
---|
12 | |
---|
13 | |
---|
14 | ############### |
---|
15 | # test values # |
---|
16 | ############### |
---|
17 | longi = array([-109.208, -117.39 , -117.928, -123.927, -120.325, -128.82 , -125.947, -122.596, -133.173, -124.778, -137.312, -134.984, -132.538, -129.883, -126.898, -136.826, -134.414, -131.833, -128.978, -141.05 , -138.707, -136.313, -133.787, -131.036, -127.931, -140.634, -138.243, -135.757, -133.087, -130.122, -126.704, -110.352, -142.613, -140.213, -137.751, -135.144, -132.291, -129.058, -125.246, -114.38 , -147.118, -144.65 , -142.229, -139.777, -137.216, -134.453, -131.372, -127.803, -118.005, -146.753]) |
---|
18 | |
---|
19 | lati = array([ 68.164, 69.205, 70.121, 70.087, 70.422, 69.994, 70.359, 70.709, 69.846, 70.983, 69.619, 70.061, 70.474, 70.867, 71.246, 70.259, 70.693, 71.103, 71.496, 69.936, 70.437, 70.896, 71.325, 71.735, 72.129, 70.593, 71.08 , 71.532, 71.959, 72.369, 72.764, 73.708, 70.723, 71.244, 71.721, 72.168, 72.595, 73.007, 73.403, 74.075, 70.188, 70.824, 71.384, 71.891, 72.361, 72.807, 73.235, 73.648, 74.394, 70.892]) |
---|
20 | |
---|
21 | z = array([ 0.938 , 0.899 , 0.958 , 0.944 , 0.991 , 0.867 , 0.948 , 0.963 , 0.911 , 0.978 , 0.935 , 0.932 , 0.957 , 0.972 , 0.983 , 0.945 , 0.941 , 0.965 , 0.986 , 0.88 , 0.942 , 0.945 , 0.966 , 0.989 , 0.9999, 0.914 , 0.908 , 0.93 , 0.96 , 0.996 , 0.9999, 0.952 , 0.898 , 0.88 , 0.852 , 0.86 , 0.906 , 0.984 , 0.976 , 0.888 , 0.794 , 0.89 , 0.846 , 0.849 , 0.869 , 0.812 , 0.764 , 0.904 , 0.853 , 0.934 ]) |
---|
22 | z0 = min(z) |
---|
23 | z1 = max(z) |
---|
24 | |
---|
25 | |
---|
26 | #################################################### |
---|
27 | # from polar (lon, lat) to cartesian coords (x, y) # |
---|
28 | #################################################### |
---|
29 | # associates a (x, y) couple to each point of (longitude, latitude) coordinates |
---|
30 | # origin of the (x, y) grid : (x=0, y=0) <=> (lon=0, lat=0) |
---|
31 | Rt = 6371. |
---|
32 | alpha = (pi*Rt)/180. |
---|
33 | beta = pi/180. |
---|
34 | # definition of theta angle and coord x,y # |
---|
35 | x = np.zeros([len(lati), len(longi)], float) |
---|
36 | y = np.zeros([len(lati), len(longi)], float) |
---|
37 | for ilat in range (0, len(lati)): |
---|
38 | for ilon in range (0, len(longi)): |
---|
39 | if ((longi[ilon] >= 0.) & (longi[ilon] <= 90.)): # 4eme quadrant |
---|
40 | theta = (90. - longi[ilon]) * beta |
---|
41 | x[ilat, ilon] = (90. - lati[ilat]) * alpha * cos(theta) |
---|
42 | y[ilat, ilon] = (90. - lati[ilat]) * alpha * sin(-theta) |
---|
43 | else: |
---|
44 | if ((longi[ilon] > 90.) & (longi[ilon] <= 180.)): # 1er quadrant |
---|
45 | theta = (longi[ilon] - 90.) * beta |
---|
46 | x[ilat, ilon] = (90. - lati[ilat]) * alpha * cos(theta) |
---|
47 | y[ilat, ilon] = (90. - lati[ilat]) * alpha * sin(theta) |
---|
48 | else: |
---|
49 | if ((longi[ilon] >= -180.) & (longi[ilon] < 0.)): # 2eme et 3eme quadrants |
---|
50 | theta = (270. + longi[ilon]) * beta |
---|
51 | x[ilat, ilon] = (90. - lati[ilat]) * alpha * cos(theta) |
---|
52 | y[ilat, ilon] = (90. - lati[ilat]) * alpha * sin(theta) |
---|
53 | |
---|
54 | |
---|
55 | # definition of the new cartesian grid (x, y) # |
---|
56 | Sx = size(x) |
---|
57 | Sy = size(y) |
---|
58 | xx = reshape(x, size(x)) |
---|
59 | yy = reshape(y, size(y)) |
---|
60 | mx = min(xx) |
---|
61 | Mx = max(xx) |
---|
62 | my = min(yy) |
---|
63 | My = max(yy) |
---|
64 | |
---|
65 | x0 = round(mx) - 50. # xmin |
---|
66 | x1 = round(Mx) + 50. # xmax |
---|
67 | dx = 25. # delta(x) |
---|
68 | xvec = np.arange(x0, x1+dx, dx) |
---|
69 | nx = size(xvec) |
---|
70 | y0 = round(my) - 50. # ymin |
---|
71 | y1 = round(My) + 50. # ymax |
---|
72 | dy = 25. # delta(y) |
---|
73 | yvec = np.arange(y0, y1+dy, dy) |
---|
74 | ny = size(yvec) |
---|
75 | xgrid_cart, ygrid_cart = np.meshgrid(xvec, yvec) # new cartesian grid (centered on North pole) |
---|
76 | |
---|
77 | |
---|
78 | # counting each grid cell # |
---|
79 | ix = np.zeros([Sx], int) |
---|
80 | for i in range (0, Sx): # boucle sur les points M (abscisses) |
---|
81 | if xx[i] == x0: |
---|
82 | ix[i] = 0 |
---|
83 | else: |
---|
84 | ix[i] = math.ceil((xx[i] - x0) / dx) - 1 |
---|
85 | |
---|
86 | iy = np.zeros([Sy], int) |
---|
87 | for j in range (0, Sy): # boucle sur les points M (ordonnees) |
---|
88 | if yy[j] == y0: |
---|
89 | iy[j] = 0 |
---|
90 | else: |
---|
91 | iy[j] = math.ceil((yy[j] - y0) / dy) - 1 |
---|
92 | |
---|
93 | |
---|
94 | #inx = (ix >= 0) & (ix < nx) |
---|
95 | #iny = (iy >= 0) & (iy < ny) |
---|
96 | #inz = (z > z0) & (z < z1) |
---|
97 | #inn = inx & iny & inz |
---|
98 | #iix = ix[inn] |
---|
99 | #iiy = iy[inn] |
---|
100 | #zz = z[inn] |
---|
101 | |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | # calculation of distances between point M(x,y) and 4 grid points of its belonging cell # |
---|
106 | dist1 = np.zeros([Sx], float) |
---|
107 | dist2 = np.zeros([Sx], float) |
---|
108 | dist3 = np.zeros([Sx], float) |
---|
109 | dist4 = np.zeros([Sx], float) |
---|
110 | min_dist = np.zeros([Sx], float) |
---|
111 | for k in range (0, Sx): # boucle sur les points M (x et y) |
---|
112 | dist1[k] = sqrt(((xx[k] - xvec[ix[k]]) ** 2) + ((yy[k] - yvec[iy[k]]) ** 2)) |
---|
113 | dist2[k] = sqrt(((xx[k] - xvec[ix[k] + 1]) ** 2) + ((yy[k] - yvec[iy[k]]) ** 2)) |
---|
114 | dist3[k] = sqrt(((xx[k] - xvec[ix[k]])**2) + ((yy[k] - yvec[iy[k] + 1]) ** 2)) |
---|
115 | dist4[k] = sqrt(((xx[k] - xvec[ix[k] + 1]) ** 2) + ((yy[k] - yvec[iy[k] + 1]) ** 2)) |
---|
116 | dist_vec = np.array([dist1[k], dist2[k], dist3[k], dist4[k]]) |
---|
117 | ind = np.where(dist_vec == min(dist_vec)) |
---|
118 | print 'grid cell no : ' + str(ind[0][0]+1) |
---|
119 | |
---|
120 | |
---|
121 | ##### probleme: z pas la même dimension (plus petite)!!####### |
---|
122 | |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | |
---|
127 | |
---|
128 | |
---|
129 | |
---|
130 | |
---|
131 | |
---|
132 | |
---|
133 | |
---|
134 | |
---|
135 | |
---|
136 | |
---|
137 | |
---|
138 | |
---|
139 | |
---|
140 | |
---|
141 | |
---|
142 | |
---|
143 | |
---|
144 | for i in range (0, 5): |
---|
145 | np.where((xx - xvec[i]) <= dx) |
---|
146 | |
---|
147 | for j in range (0, len(yy)): |
---|
148 | np.where ((xx[i] - xvec) <= dx): |
---|
149 | print 'ok' |
---|
150 | else: |
---|
151 | print 'not ok' |
---|
152 | np.where((yy[i] - yvec) <= dy) |
---|
153 | |
---|
154 | |
---|
155 | |
---|
156 | M = np.array([xx, yy]) |
---|
157 | for k in range (0, len(xx)): |
---|
158 | xcoord[k] = M[:,k][0] |
---|
159 | ycoord[k] = M[:,k][1] |
---|
160 | |
---|
161 | |
---|
162 | |
---|
163 | |
---|
164 | ix = np.zeros([len(xx)], int) |
---|
165 | for i in range (0, len(xx)): |
---|
166 | if xx[i] == x0: |
---|
167 | ix[i] = 0 |
---|
168 | else: |
---|
169 | ix[i] = math.ceil((xx[i] - x0) / dx) - 1 |
---|
170 | |
---|
171 | iy = np.zeros([len(yy)], int) |
---|
172 | for j in range (0, len(yy)): |
---|
173 | if yy[j] == y0: |
---|
174 | iy[j] = 0 |
---|
175 | else: |
---|
176 | iy[j] = math.ceil((yy[j] - y0) / dy) - 1 |
---|
177 | |
---|
178 | |
---|
179 | |
---|
180 | |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | figure() |
---|
185 | m = Basemap(projection = 'npaeqd',boundinglat = 60, lon_0 = 0, resolution = 'l') |
---|
186 | clevs=arange(0.5, 1., 0.01) |
---|
187 | m.drawcoastlines(linewidth=1) |
---|
188 | m.drawparallels(np.arange(60, 90, 20)) |
---|
189 | m.drawmeridians(np.arange(-180, 180, 20)) |
---|
190 | xii,yii = m(*np.meshgrid(xx ,yy)) |
---|
191 | cs=m.contourf(xii,yii, zgrid, clevs, cmap=cm.s3pcpn_l_r) |
---|
192 | cbar =colorbar(cs) |
---|
193 | |
---|