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(lon, lat), y(lon, lat)) # |
---|
14 | ######################################################################## |
---|
15 | |
---|
16 | #def new_cartesian_grid(longitude, latitude, z, z0, z1, dx, dy): |
---|
17 | # associates a (x, y) couple to each point of (longitude, latitude) coordinates |
---|
18 | # origin of the (x, y) grid : (x=0, y=0) <=> (lon=0, lat=0) |
---|
19 | #L = len(z) |
---|
20 | #z0 = min(z) |
---|
21 | #z1 = max(z) |
---|
22 | |
---|
23 | |
---|
24 | latitude = lat_cote |
---|
25 | longitude = lon_cote |
---|
26 | |
---|
27 | Rt = 6371. |
---|
28 | alpha = (pi*Rt)/180. |
---|
29 | beta = pi/180. |
---|
30 | x = np.zeros([len(latitude), len(longitude)], float) |
---|
31 | y = np.zeros([len(latitude), len(longitude)], float) |
---|
32 | |
---|
33 | for ilon in range (0, len(longitude)): |
---|
34 | for ilat in range (0, len(latitude)): |
---|
35 | if ((longitude[ilon] >= 0.) & (longitude[ilon] <= 90.)): # 4eme quadrant |
---|
36 | theta = (90. - longitude[ilon]) * beta |
---|
37 | x[ilat, ilon] = (90. - latitude[ilat]) * alpha * cos(theta) |
---|
38 | y[ilat, ilon] = (90. - latitude[ilat]) * alpha * sin(-theta) |
---|
39 | else: |
---|
40 | if ((longitude[ilon] > 90.) & (longitude[ilon] <= 180.)): # 1er quadrant |
---|
41 | theta = (longitude[ilon] - 90.) * beta |
---|
42 | x[ilat, ilon] = (90. - latitude[ilat]) * alpha * cos(theta) |
---|
43 | y[ilat, ilon] = (90. - latitude[ilat]) * alpha * sin(theta) |
---|
44 | else: |
---|
45 | if ((longitude[ilon] >= -180.) & (longitude[ilon] < 0.)): # 2eme et 3eme quadrants |
---|
46 | theta = (270. + longitude[ilon]) * beta |
---|
47 | x[ilat, ilon] = (90. - latitude[ilat]) * alpha * cos(theta) |
---|
48 | y[ilat, ilon] = (90. - latitude[ilat]) * alpha * sin(theta) |
---|
49 | |
---|
50 | |
---|
51 | |
---|
52 | rootgrp = Dataset('/net/argos/data/parvati/lahlod/ARCTIC/z_cote_global_final_cartes_z0.nc', 'w', format = 'NETCDF4') |
---|
53 | rootgrp.createDimension('longitude', len(lon_cote)) |
---|
54 | rootgrp.createDimension('latitude', len(lat_cote)) |
---|
55 | nc_lon = rootgrp.createVariable('longitude', 'f', ('longitude',)) |
---|
56 | nc_lat = rootgrp.createVariable('latitude', 'f', ('latitude',)) |
---|
57 | nc_x = rootgrp.createVariable('x', 'f', ('latitude', 'longitude',)) |
---|
58 | nc_y = rootgrp.createVariable('y', 'f', ('latitude', 'longitude',)) |
---|
59 | nc_lon[:] = lon_cote |
---|
60 | nc_lat[:] = lat_cote |
---|
61 | nc_x[:] = x |
---|
62 | nc_y[:] = y |
---|
63 | rootgrp.close() |
---|
64 | |
---|
65 | |
---|
66 | |
---|
67 | X = reshape(x, size(x)) |
---|
68 | Y = reshape(y, size(y)) |
---|
69 | Z_LIM = reshape(lim_cote, size(lim_cote)) |
---|
70 | |
---|
71 | indices = np.where(Z_LIM == 1.)[0] |
---|
72 | x_ind = X[indices] |
---|
73 | y_ind = Y[indices] |
---|
74 | z_ind = Z_LIM[indices] |
---|
75 | |
---|
76 | |
---|
77 | #x0 = x_ind.min() - 50. |
---|
78 | #x1 = x_ind.max() + 50. |
---|
79 | #dx = 10. |
---|
80 | #xvec = np.arange(x0, x1+dx, dx) |
---|
81 | |
---|
82 | #y0 = y_ind.min() - 50. |
---|
83 | #y1 = y_ind.max() + 50. |
---|
84 | #dy = 10. |
---|
85 | #yvec = np.arange(y0, y1+dy, dy) |
---|
86 | |
---|
87 | #xgrid, ygrid = np.meshgrid(x_vec, y_vec) |
---|
88 | |
---|
89 | |
---|
90 | plt.figure() |
---|
91 | plt.scatter(x_ind, y_ind, c = z_ind/10.) |
---|
92 | |
---|
93 | # definition of the new cartesian grid (x, y) # |
---|
94 | Mx = max(x) |
---|
95 | mx = min(x) |
---|
96 | x0 = round(mx) - 50. # xmin |
---|
97 | x1 = round(Mx) + 50. # xmax |
---|
98 | #dx = 50. # delta(x) |
---|
99 | xvec = np.arange(x0, x1+dx, dx) |
---|
100 | nx = len(xvec) |
---|
101 | My = max(y) |
---|
102 | my = min(y) |
---|
103 | y0 = round(my) - 50. # ymin |
---|
104 | y1 = round(My) + 50. # ymax |
---|
105 | #dy = 50. # delta(y) |
---|
106 | yvec = np.arange(y0, y1+dy, dy) |
---|
107 | ny = len(yvec) |
---|
108 | xgrid_cart, ygrid_cart= np.meshgrid(xvec, yvec) # new cartesian grid (centered on North pole) |
---|
109 | |
---|
110 | |
---|
111 | # counting each grid cell # |
---|
112 | ix = np.zeros([L], int) |
---|
113 | i = 0 |
---|
114 | for i in range (0, L): # boucle sur les points M (abscisses) |
---|
115 | if x[i] == x0: |
---|
116 | ix[i] = 0 |
---|
117 | else: |
---|
118 | ix[i] = math.ceil((x[i] - x0) / dx) - 1 # associates each x vakue to a cell number |
---|
119 | |
---|
120 | i = 0 |
---|
121 | iy = np.zeros([L], int) |
---|
122 | for i in range (0, L):# boucle sur les points M (ordonnees) |
---|
123 | if y[i] == y0: |
---|
124 | iy[i] = 0 |
---|
125 | else: |
---|
126 | iy[i] = math.ceil((y[i] - y0) / dy) - 1 # associates each y vakue to a cell number |
---|
127 | |
---|
128 | |
---|
129 | # calculation of distances between point M(x,y) and 4 grid points of its belonging cell # |
---|
130 | close_point = np.zeros([L, 2], int) |
---|
131 | k = 0 |
---|
132 | for k in range (0, L): # boucle sur les points M (x et y) |
---|
133 | #print 'x', x[k] |
---|
134 | #print 'y', y[k] |
---|
135 | #print 'xvec', xvec[ix[k]] |
---|
136 | #print 'yvec', yvec[iy[k]] |
---|
137 | d1 = sqrt(((x[k] - xvec[ix[k]]) ** 2) + ((y[k] - yvec[iy[k]]) ** 2)) |
---|
138 | # print 'd1', d1 |
---|
139 | d2 = sqrt(((x[k] - xvec[ix[k] + 1]) ** 2) + ((y[k] - yvec[iy[k]]) ** 2)) |
---|
140 | #print 'd2', d2 |
---|
141 | d3 = sqrt(((x[k] - xvec[ix[k]]) ** 2) + ((y[k] - yvec[iy[k] + 1]) ** 2)) |
---|
142 | # print 'd3', d3 |
---|
143 | d4 = sqrt(((x[k] - xvec[ix[k] + 1]) ** 2) + ((y[k] - yvec[iy[k] + 1]) ** 2)) |
---|
144 | #print 'd4', d4 |
---|
145 | d_vec = np.array([d1, d2, d3, d4]) |
---|
146 | ind = np.where(d_vec == min(d_vec)) # finds the smallest distance between the 4 points of the grid |
---|
147 | #print 'grid cell no : ' + str(ind[0][0]+1) |
---|
148 | point_vec = np.array([(ix[k], iy[k]), (ix[k] + 1, iy[k]), (ix[k], iy[k] + 1), (ix[k] + 1, iy[k] + 1)]) |
---|
149 | #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) |
---|
150 | 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) |
---|
151 | |
---|
152 | |
---|
153 | # association of z value to closest grid point # |
---|
154 | zgrid = np.zeros([ny, nx], float) # z in new grid |
---|
155 | ngrid = np.zeros([ny, nx], int) # nb of obs per new grid cell |
---|
156 | z2grid = np.zeros([ny, nx], float) # z2 in new grid |
---|
157 | for k in range (0, L): |
---|
158 | zgrid[close_point[k, 1], close_point[k, 0]] = zgrid[close_point[k, 1], close_point[k, 0]] + z[k] |
---|
159 | ngrid[close_point[k, 1], close_point[k, 0]] = ngrid[close_point[k, 1], close_point[k, 0]] + 1 |
---|
160 | z2grid[close_point[k, 1], close_point[k, 0]] = z2grid[close_point[k, 1], close_point[k, 0]] + (z[k] * z[k]) |
---|
161 | |
---|
162 | |
---|
163 | # z in each point of new grid # |
---|
164 | ZGRID = zgrid / ngrid |
---|
165 | # variance of z in each grid cell # |
---|
166 | sigmagrid = np.zeros([ny, nx], float) |
---|
167 | for j in range (0, nx): |
---|
168 | for i in range (0, ny): |
---|
169 | if (ngrid[i, j] > 1): # take away the cells where no obs |
---|
170 | sigmagrid[i, j] = sqrt((1/(ngrid[i, j]-1))*(z2grid[i, j] - ngrid[i, j]*ZGRID[i, j]*ZGRID[i, j])) |
---|
171 | else: |
---|
172 | sigmagrid[i, j] = NaN |
---|
173 | |
---|
174 | |
---|
175 | #return ZGRID, ngrid, sigmagrid, xvec, yvec, xgrid_cart, ygrid_cart |
---|
176 | |
---|
177 | |
---|
178 | |
---|
179 | |
---|
180 | |
---|