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 | # from polar (lon, lat) to cartesian coords (x, y) # |
---|
14 | #################################################### |
---|
15 | |
---|
16 | def new_cartesian_grid(longitude, latitude, z, z0, z1, dx, dy): |
---|
17 | |
---|
18 | # associates a (x, y) couple to each point of (longitude, latitude) coordinates |
---|
19 | # origin of the (x, y) grid : (x=0, y=0) <=> (lon=0, lat=0) |
---|
20 | L = len(z) |
---|
21 | #z0 = min(z) |
---|
22 | #z1 = max(z) |
---|
23 | Rt = 6371. |
---|
24 | alpha = (pi*Rt)/180. |
---|
25 | beta = pi/180. |
---|
26 | x = np.zeros([L], float) |
---|
27 | y = np.zeros([L], float) |
---|
28 | for k in range (0, L): |
---|
29 | if ((longitude[k] >= 0.) & (longitude[k] <= 90.)): # 4eme quadrant |
---|
30 | theta = (90. - longitude[k]) * beta |
---|
31 | x[k] = (90. - latitude[k]) * alpha * cos(theta) |
---|
32 | y[k] = (90. - latitude[k]) * alpha * sin(-theta) |
---|
33 | else: |
---|
34 | if ((longitude[k] > 90.) & (longitude[k] <= 180.)): # 1er quadrant |
---|
35 | theta = (longitude[k] - 90.) * beta |
---|
36 | x[k] = (90. - latitude[k]) * alpha * cos(theta) |
---|
37 | y[k] = (90. - latitude[k]) * alpha * sin(theta) |
---|
38 | else: |
---|
39 | if ((longitude[k] >= -180.) & (longitude[k] < 0.)): # 2eme et 3eme quadrants |
---|
40 | theta = (270. + longitude[k]) * beta |
---|
41 | x[k] = (90. - latitude[k]) * alpha * cos(theta) |
---|
42 | y[k] = (90. - latitude[k]) * alpha * sin(theta) |
---|
43 | |
---|
44 | |
---|
45 | # definition of the new cartesian grid (x, y) # |
---|
46 | Mx = max(x) |
---|
47 | mx = min(x) |
---|
48 | x0 = round(mx) - 50. # xmin |
---|
49 | x1 = round(Mx) + 50. # xmax |
---|
50 | #dx = 50. # delta(x) |
---|
51 | xvec = np.arange(x0, x1+dx, dx) |
---|
52 | nx = len(xvec) |
---|
53 | My = max(y) |
---|
54 | my = min(y) |
---|
55 | y0 = round(my) - 50. # ymin |
---|
56 | y1 = round(My) + 50. # ymax |
---|
57 | #dy = 50. # delta(y) |
---|
58 | yvec = np.arange(y0, y1+dy, dy) |
---|
59 | ny = len(yvec) |
---|
60 | xgrid_cart, ygrid_cart= np.meshgrid(xvec, yvec) # new cartesian grid (centered on North pole) |
---|
61 | |
---|
62 | |
---|
63 | # counting each grid cell # |
---|
64 | ix = np.zeros([L], int) |
---|
65 | i = 0 |
---|
66 | for i in range (0, L): # boucle sur les points M (abscisses) |
---|
67 | if x[i] == x0: |
---|
68 | ix[i] = 0 |
---|
69 | else: |
---|
70 | ix[i] = math.ceil((x[i] - x0) / dx) - 1 # associates each x vakue to a cell number |
---|
71 | |
---|
72 | i = 0 |
---|
73 | iy = np.zeros([L], int) |
---|
74 | for i in range (0, L):# boucle sur les points M (ordonnees) |
---|
75 | if y[i] == y0: |
---|
76 | iy[i] = 0 |
---|
77 | else: |
---|
78 | iy[i] = math.ceil((y[i] - y0) / dy) - 1 # associates each y vakue to a cell number |
---|
79 | |
---|
80 | |
---|
81 | # calculation of distances between point M(x,y) and 4 grid points of its belonging cell # |
---|
82 | close_point = np.zeros([L, 2], int) |
---|
83 | k = 0 |
---|
84 | for k in range (0, L): # boucle sur les points M (x et y) |
---|
85 | # print 'x', x[k] |
---|
86 | # print 'y', y[k] |
---|
87 | # print 'xvec', xvec[ix[k]] |
---|
88 | # print 'yvec', yvec[iy[k]] |
---|
89 | d1 = sqrt(((x[k] - xvec[ix[k]]) ** 2) + ((y[k] - yvec[iy[k]]) ** 2)) |
---|
90 | # print 'd1', d1 |
---|
91 | d2 = sqrt(((x[k] - xvec[ix[k] + 1]) ** 2) + ((y[k] - yvec[iy[k]]) ** 2)) |
---|
92 | # print 'd2', d2 |
---|
93 | d3 = sqrt(((x[k] - xvec[ix[k]]) ** 2) + ((y[k] - yvec[iy[k] + 1]) ** 2)) |
---|
94 | # print 'd3', d3 |
---|
95 | d4 = sqrt(((x[k] - xvec[ix[k] + 1]) ** 2) + ((y[k] - yvec[iy[k] + 1]) ** 2)) |
---|
96 | # print 'd4', d4 |
---|
97 | d_vec = np.array([d1, d2, d3, d4]) |
---|
98 | ind = np.where(d_vec == min(d_vec)) # finds the smallest distance between the 4 points of the grid |
---|
99 | # print 'grid cell no : ' + str(ind[0][0]+1) |
---|
100 | point_vec = np.array([(ix[k], iy[k]), (ix[k] + 1, iy[k]), (ix[k], iy[k] + 1), (ix[k] + 1, iy[k] + 1)]) |
---|
101 | # print 'chosen point in grid', point_vec[ind[0][0]] # we have chosen which point of the grid is closer to M // point_vec[ind[0][0]] = (cell no of closest x, celle no of closest y) |
---|
102 | close_point[k, :] = point_vec[ind[0][0]]# we have chosen which point of the grid is closer to M // point_vec[ind[0][0]] = (cell no of closest x, celle no of closest y) |
---|
103 | |
---|
104 | |
---|
105 | # association of z value to closest grid point # |
---|
106 | zgrid = np.zeros([ny, nx], float) # z in new grid |
---|
107 | ngrid = np.zeros([ny, nx], int) # nb of obs per new grid cell |
---|
108 | z2grid = np.zeros([ny, nx], float) # z2 in new grid |
---|
109 | for k in range (0, L): |
---|
110 | zgrid[close_point[k, 1], close_point[k, 0]] = zgrid[close_point[k, 1], close_point[k, 0]] + z[k] |
---|
111 | ngrid[close_point[k, 1], close_point[k, 0]] = ngrid[close_point[k, 1], close_point[k, 0]] + 1 |
---|
112 | z2grid[close_point[k, 1], close_point[k, 0]] = z2grid[close_point[k, 1], close_point[k, 0]] + (z[k] * z[k]) |
---|
113 | |
---|
114 | |
---|
115 | # z in each point of new grid # |
---|
116 | ZGRID = zgrid / ngrid |
---|
117 | # variance of z in each grid cell # |
---|
118 | sigmagrid = np.zeros([ny, nx], float) |
---|
119 | for j in range (0, nx): |
---|
120 | for i in range (0, ny): |
---|
121 | if (ngrid[i, j] > 1): # take away the cells where no obs |
---|
122 | sigmagrid[i, j] = (1 / (ngrid[i, j] - 1) * sqrt(z2grid[i, j] - ngrid[i, j] * ZGRID[i, j] * ZGRID[i, j])) |
---|
123 | else: |
---|
124 | sigmagrid[i, j] = NaN |
---|
125 | |
---|
126 | |
---|
127 | return ZGRID, ngrid, sigmagrid, xvec, yvec, xgrid_cart, ygrid_cart |
---|
128 | |
---|
129 | |
---|
130 | |
---|
131 | |
---|
132 | |
---|
133 | |
---|
134 | |
---|
135 | #emisGRID, nemis, sigmaemis, xvec, yvec, xgrid_cart, ygrid_cart = cartesian_grid_test.new_cartesian_grid(lon_zon, lat_zon, emis_zon, emis_zon.min(), emis_zon.max(), 40, 40) |
---|