Changeset 31


Ignore:
Timestamp:
06/20/14 18:55:02 (10 years ago)
Author:
lahlod
Message:

new_cartesian_grid and read_bathy

Location:
trunk/src/scripts_Laura
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/scripts_Laura/cartesian_grid_test.py

    r30 r31  
    1010import draw_map 
    1111 
    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  
    2612#################################################### 
    2713# from polar (lon, lat) to cartesian coords (x, y) # 
    2814#################################################### 
    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) 
     15 
     16def 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) 
    4333        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) 
     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) 
    4838            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) 
     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) 
    5343 
    5444 
    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) 
     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) 
    7661 
    7762 
    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 
     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 
    8571 
    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  
     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 
    9279 
    9380 
    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] 
     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) 
    101103 
    102104 
    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) 
     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]) 
    119113     
    120114 
    121 ##### probleme: z pas la même dimension (plus petite)!!####### 
     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  
    122128 
    123129 
     
    127133 
    128134 
    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  
     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) 
  • trunk/src/scripts_Laura/read_emis_glace.py

    r30 r31  
    113113 
    114114 
     115import 
Note: See TracChangeset for help on using the changeset viewer.