source: trunk/src/scripts_Laura/ARCTIC/cartesian_grid_shoreline.py @ 55

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

modifs

File size: 6.1 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# 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
24latitude = lat_cote
25longitude = lon_cote
26
27Rt = 6371.
28alpha = (pi*Rt)/180.
29beta = pi/180.
30x = np.zeros([len(latitude), len(longitude)], float)
31y = np.zeros([len(latitude), len(longitude)], float)
32
33for 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
52rootgrp = Dataset('/net/argos/data/parvati/lahlod/ARCTIC/z_cote_global_final_cartes_z0.nc', 'w', format = 'NETCDF4')
53rootgrp.createDimension('longitude', len(lon_cote))
54rootgrp.createDimension('latitude', len(lat_cote))
55nc_lon = rootgrp.createVariable('longitude', 'f', ('longitude',))
56nc_lat = rootgrp.createVariable('latitude', 'f', ('latitude',))
57nc_x = rootgrp.createVariable('x', 'f', ('latitude', 'longitude',))
58nc_y = rootgrp.createVariable('y', 'f', ('latitude', 'longitude',))
59nc_lon[:] = lon_cote
60nc_lat[:] = lat_cote
61nc_x[:] = x
62nc_y[:] = y
63rootgrp.close()
64
65
66
67X = reshape(x, size(x))
68Y = reshape(y, size(y))
69Z_LIM = reshape(lim_cote, size(lim_cote))
70
71indices = np.where(Z_LIM == 1.)[0]
72x_ind = X[indices]
73y_ind = Y[indices]
74z_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
90plt.figure()
91plt.scatter(x_ind, y_ind, c = z_ind/10.)
92
93# definition of the new cartesian grid (x, y) #
94Mx = max(x)
95mx = min(x)
96x0 = round(mx) - 50. # xmin
97x1 = round(Mx) + 50. # xmax
98#dx = 50. # delta(x)
99xvec = np.arange(x0, x1+dx, dx)
100nx = len(xvec)
101My = max(y)
102my = min(y)
103y0 = round(my) - 50. # ymin
104y1 = round(My) + 50. # ymax
105#dy = 50. # delta(y)
106yvec = np.arange(y0, y1+dy, dy)
107ny = len(yvec)
108xgrid_cart, ygrid_cart= np.meshgrid(xvec, yvec) # new cartesian grid (centered on North pole)
109
110
111# counting each grid cell #
112ix = np.zeros([L], int)
113i = 0
114for 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
120i = 0
121iy = np.zeros([L], int) 
122for 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 #
130close_point = np.zeros([L, 2], int)
131k = 0
132for 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 #
154zgrid = np.zeros([ny, nx], float) # z in new grid
155ngrid = np.zeros([ny, nx], int) # nb of obs per new grid cell
156z2grid = np.zeros([ny, nx], float) # z2 in new grid
157for 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 #
164ZGRID = zgrid / ngrid
165# variance of z in each grid cell #
166sigmagrid = np.zeros([ny, nx], float)
167for 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
Note: See TracBrowser for help on using the repository browser.