source: trunk/src/scripts_Laura/cartesian_grid_test.py @ 30

Last change on this file since 30 was 30, checked in by lahlod, 10 years ago

scrit nouvelle grille

File size: 6.0 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3import string
4import numpy as np
5import matplotlib.pyplot as plt
6import ffgrid2
7from pylab import *
8from mpl_toolkits.basemap import Basemap
9from mpl_toolkits.basemap import shiftgrid, cm
10import draw_map
11
12
13
14###############
15# test values #
16###############
17longi = 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
19lati = 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
21z = 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 ])
22z0 = min(z)
23z1 = 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)
31Rt = 6371.
32alpha = (pi*Rt)/180.
33beta = pi/180.
34# definition of theta angle and coord x,y #
35x = np.zeros([len(lati), len(longi)], float)
36y = np.zeros([len(lati), len(longi)], float)
37for 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) #
56Sx = size(x)
57Sy = size(y)
58xx = reshape(x, size(x))
59yy = reshape(y, size(y))
60mx = min(xx)
61Mx = max(xx)
62my = min(yy)
63My = max(yy)
64
65x0 = round(mx) - 50. # xmin
66x1 = round(Mx) + 50. # xmax
67dx = 25. # delta(x)
68xvec = np.arange(x0, x1+dx, dx)
69nx = size(xvec)
70y0 = round(my) - 50. # ymin
71y1 = round(My) + 50. # ymax
72dy = 25. # delta(y)
73yvec = np.arange(y0, y1+dy, dy)
74ny = size(yvec)
75xgrid_cart, ygrid_cart = np.meshgrid(xvec, yvec) # new cartesian grid (centered on North pole)
76
77
78# counting each grid cell #
79ix = np.zeros([Sx], int)
80for 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
86iy = np.zeros([Sy], int) 
87for 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 #
106dist1 = np.zeros([Sx], float)
107dist2 = np.zeros([Sx], float)
108dist3 = np.zeros([Sx], float)
109dist4 = np.zeros([Sx], float)
110min_dist = np.zeros([Sx], float)
111for 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
144for 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
156M = np.array([xx, yy])
157for k in range (0, len(xx)):
158    xcoord[k] = M[:,k][0]
159    ycoord[k] = M[:,k][1]
160
161
162
163
164ix = np.zeros([len(xx)], int)
165for 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
171iy = np.zeros([len(yy)], int)
172for 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
184figure()
185m = Basemap(projection = 'npaeqd',boundinglat = 60, lon_0 = 0, resolution = 'l')
186clevs=arange(0.5, 1., 0.01)
187m.drawcoastlines(linewidth=1)
188m.drawparallels(np.arange(60, 90, 20))
189m.drawmeridians(np.arange(-180, 180, 20))
190xii,yii = m(*np.meshgrid(xx ,yy))
191cs=m.contourf(xii,yii, zgrid, clevs, cmap=cm.s3pcpn_l_r)
192cbar =colorbar(cs)
193
Note: See TracBrowser for help on using the repository browser.