1 | # -*- Mode: python -*- |
---|
2 | ### - |
---|
3 | ### - Computes polynomial fit |
---|
4 | ### - |
---|
5 | ## - |
---|
6 | ## - Olivier Marti (olivier.marti@lsce.ipsl.fr) |
---|
7 | ## - Institut Pierre Simon Laplace |
---|
8 | ## - Laboratoire des Sciences du Climate et de L'environnment |
---|
9 | ## - |
---|
10 | print 'Demarrage' |
---|
11 | ## - Needed Python modules |
---|
12 | import cdms2, MV2, numpy as np |
---|
13 | import nemo |
---|
14 | |
---|
15 | def clo_lon (lon, lon_ref) : # Find closest longitude |
---|
16 | z0 = lon |
---|
17 | for nn in range (2) : |
---|
18 | z0 = np.where ( z0 > lon_ref+180.0, z0-360.0, z0) |
---|
19 | z0 = np.where ( z0 < lon_ref-180.0, z0+360.0, z0) |
---|
20 | |
---|
21 | return z0 |
---|
22 | |
---|
23 | ## - NetCDF parameters |
---|
24 | # - If you want to turn that off or set different values of compression use the functions: |
---|
25 | cdms2.setNetcdfShuffleFlag (0) |
---|
26 | cdms2.setNetcdfDeflateFlag (0) |
---|
27 | cdms2.setNetcdfDeflateLevelFlag(0) |
---|
28 | |
---|
29 | print 'Ouverture fichier' |
---|
30 | f_grid = cdms2.open ( 'tmp_ORCA025_coordinates_mask.nc', 'r+' ) |
---|
31 | |
---|
32 | print 'Lecture tmask' |
---|
33 | tmask = f_grid ( 'mask_T' ) |
---|
34 | |
---|
35 | print 'Lecture des axes' |
---|
36 | yy = tmask.getAxis(0) ; ny = len(yy) |
---|
37 | xx = tmask.getAxis(1) ; nx = len(xx) |
---|
38 | |
---|
39 | print 'Creation axe' |
---|
40 | nvertex = cdms2.createAxis ( np.arange (4) ) |
---|
41 | |
---|
42 | print 'Lecture lon lat' |
---|
43 | glamt = f_grid ( 'nav_lon_grid_T' ).filled(0) |
---|
44 | gphit = f_grid ( 'nav_lat_grid_T' ).filled(0) |
---|
45 | glamu = f_grid ( 'nav_lon_grid_U' ).filled(0) |
---|
46 | gphiu = f_grid ( 'nav_lat_grid_U' ).filled(0) |
---|
47 | glamv = f_grid ( 'nav_lon_grid_V' ).filled(0) |
---|
48 | gphiv = f_grid ( 'nav_lat_grid_V' ).filled(0) |
---|
49 | glamf = f_grid ( 'nav_lon_grid_F' ).filled(0) |
---|
50 | gphif = f_grid ( 'nav_lon_grid_F' ).filled(0) |
---|
51 | |
---|
52 | |
---|
53 | # Construction des limites |
---|
54 | # 1,2->3 2,2->2 |
---|
55 | # 1,1->0 2,1->1 |
---|
56 | |
---|
57 | # :, :, \([1,2]\), \([1,2]\) |
---|
58 | # \1, \2, :, : |
---|
59 | |
---|
60 | print 'Creation bounds_*_T' |
---|
61 | bounds_lon_grid_T = np.zeros ( (ny, nx, 4), dtype=np.float32 ) |
---|
62 | bounds_lat_grid_T = np.zeros ( (ny, nx, 4), dtype=np.float32 ) |
---|
63 | |
---|
64 | print 'Calcul bounds_*_T' |
---|
65 | bounds_lon_grid_T [ :, :, 0] = np.roll ( np.roll ( glamf [:,:], 1, axis=0), 1, axis=1) # EOSHIFT (CSHIFT (glamf(:,:), -1, dim = 1), -1, dim = 2 ) |
---|
66 | bounds_lon_grid_T [ :, :, 1] = np.roll ( glamf [:,:], 1, axis=0) # CSHIFT (glamf(:,:), -1, dim = 2 ) |
---|
67 | bounds_lon_grid_T [ :, :, 3] = np.roll ( glamf [:,:], 1, axis=1) # CSHIFT (glamf(:,:), -1, dim = 1 ) |
---|
68 | bounds_lon_grid_T [ :, :, 2] = glamf [:,:] |
---|
69 | |
---|
70 | bounds_lat_grid_T [ :, :, 0] = np.roll ( np.roll ( gphif [:,:], 1, axis=0), 1, axis=1) |
---|
71 | bounds_lat_grid_T [ :, :, 1] = np.roll ( gphif [:,:], 1, axis=0) |
---|
72 | bounds_lat_grid_T [ :, :, 3] = np.roll ( gphif [:,:], 1, axis=1) |
---|
73 | bounds_lat_grid_T [ :, :, 2] = gphif [:,:] |
---|
74 | |
---|
75 | bounds_lon_grid_T = cdms2.createVariable ( bounds_lon_grid_T, axes=(yy, xx, nvertex), id='bounds_lon_grid_T', dtype=np.float32 ) |
---|
76 | bounds_lat_grid_T = cdms2.createVariable ( bounds_lat_grid_T, axes=(yy, xx, nvertex), id='bounds_lat_grid_T', dtype=np.float32 ) |
---|
77 | |
---|
78 | print 'Calcul bounds_*_U' |
---|
79 | bounds_lon_grid_U = np.zeros ( (ny, nx, 4), dtype=np.float32 ) |
---|
80 | bounds_lat_grid_U = np.zeros ( (ny, nx, 4), dtype=np.float32 ) |
---|
81 | |
---|
82 | bounds_lon_grid_U [ :, :, 0] = np.roll ( glamv [:,:], 1, axis=0) # CSHIFT (glamv(:,:), -1, dim = 2 ) |
---|
83 | bounds_lon_grid_U [ :, :, 1] = np.roll ( np.roll ( glamv [:,:], 1, axis=0), 1, axis=1) # EOSHIFT (CSHIFT (glamv(:,:), 1, dim = 1 ), -1, dim = 2) |
---|
84 | bounds_lon_grid_U [ :, :, 3] = glamv [:,:] |
---|
85 | bounds_lon_grid_U [ :, :, 2] = np.roll ( glamv [:,:], 1, axis=1) # CSHIFT (glamv(:,:), 1, dim = 1 ) |
---|
86 | |
---|
87 | bounds_lat_grid_U [ :, :, 0] = np.roll ( gphiv [:,:], 1, axis=0) |
---|
88 | bounds_lat_grid_U [ :, :, 1] = np.roll ( np.roll ( gphiv [:,:], 1, axis=0), 1, axis=1) |
---|
89 | bounds_lat_grid_U [ :, :, 3] = gphiv [:,:] |
---|
90 | bounds_lat_grid_U [ :, :, 2] = np.roll ( gphiv [:,:], 1, axis=1) |
---|
91 | |
---|
92 | bounds_lon_grid_U = cdms2.createVariable ( bounds_lon_grid_U, axes = ( yy, xx, nvertex ), id='bounds_lon_grid_U' ) |
---|
93 | bounds_lat_grid_U = cdms2.createVariable ( bounds_lat_grid_U, axes = ( yy, xx, nvertex ), id='bounds_lon_grid_U' ) |
---|
94 | |
---|
95 | print 'Calcul bounds_*_V' |
---|
96 | bounds_lon_grid_V = np.zeros ( (ny, nx, 4), dtype=np.float32 ) |
---|
97 | bounds_lat_grid_V = np.zeros ( (ny, nx, 4), dtype=np.float32 ) |
---|
98 | |
---|
99 | bounds_lon_grid_V [ :, :, 0] = np.roll (glamu [:,:], 1, axis=1) # CSHIFT (glamu(:,:), -1, dim = 1 ) |
---|
100 | bounds_lon_grid_V [ :, :, 1] = glamu [:,:] |
---|
101 | bounds_lon_grid_V [ :, :, 3] = np.roll (np.roll (glamu [:,:], 1, axis=0), 1, axis=1) # EOSHIFT (CSHIFT (glamu(:,:), -1, dim = 1 ), 1, dim = 2) |
---|
102 | bounds_lon_grid_V [ :, :, 2] = np.roll (glamu [:,:], 1, axis=0) # EOSHIFT (glamu(:,:), 1, dim = 2 ) |
---|
103 | |
---|
104 | bounds_lat_grid_V [ :, :, 0] = np.roll (gphiu [:,:], 1, axis=1) |
---|
105 | bounds_lat_grid_V [ :, :, 1] = gphiu [:,:] |
---|
106 | bounds_lat_grid_V [ :, :, 3] = np.roll (np.roll (glamu [:,:], 1, axis=0), 1, axis=1) |
---|
107 | bounds_lat_grid_V [ :, :, 2] = np.roll (gphiu [:,:], 1, axis=0) |
---|
108 | |
---|
109 | bounds_lon_grid_V = cdms2.createVariable ( bounds_lon_grid_V, axes = ( yy, xx, nvertex ), id='bounds_lon_grid_V' ) |
---|
110 | bounds_lat_grid_V = cdms2.createVariable ( bounds_lat_grid_V, axes = ( yy, xx, nvertex ), id='bounds_lat_grid_V' ) |
---|
111 | |
---|
112 | print 'Calcul bounds_*_F' |
---|
113 | bounds_lon_grid_F = np.zeros((ny, nx, 4), dtype=np.float32) |
---|
114 | bounds_lat_grid_F = np.zeros((ny, nx, 4), dtype=np.float32) |
---|
115 | |
---|
116 | bounds_lon_grid_F [ :, :, 0] = glamt [:,:] |
---|
117 | bounds_lon_grid_F [ :, :, 1] = np.roll (glamt [:,:], 1, axis=1) # CSHIFT (glamt(:,:), 1, dim = 1 ) |
---|
118 | bounds_lon_grid_F [ :, :, 3] = np.roll (glamt [:,:], 1, axis=0) # EOSHIFT (glamt(:,:), 1, dim = 2 ) |
---|
119 | bounds_lon_grid_F [ :, :, 2] = np.roll (np.roll (glamt [:,:], 1, axis=0), 1, axis=1) # EOSHIFT (CSHIFT (glamt(:,:), 1, dim = 1), 1, dim = 2) |
---|
120 | |
---|
121 | bounds_lat_grid_F [ :, :, 0] = gphit [:,:] |
---|
122 | bounds_lat_grid_F [ :, :, 1] = np.roll (gphit [:,:], 1, axis=1) # CSHIFT (glamt(:,:), 1, dim = 1 ) |
---|
123 | bounds_lat_grid_F [ :, :, 3] = np.roll (gphit [:,:], 1, axis=0) # EOSHIFT (glamt(:,:), 1, dim = 2 ) |
---|
124 | bounds_lat_grid_F [ :, :, 2] = np.roll (np.roll (gphit [:,:], 1, axis=0), 1, axis=1) # EOSHIFT (CSHIFT (glamt(:,:), 1, dim = 1), 1, dim = 2) |
---|
125 | |
---|
126 | bounds_lon_grid_F = cdms2.createVariable ( bounds_lon_grid_F, axes = ( yy, xx, nvertex ), id='bounds_lon_grid_F' ) |
---|
127 | bounds_lat_grid_F = cdms2.createVariable ( bounds_lat_grid_F, axes = ( yy, xx, nvertex ), id='bounds_lat_grid_F' ) |
---|
128 | |
---|
129 | print 'Trucage au sud' |
---|
130 | |
---|
131 | #bounds_lat_grid_T [ 0, :, 0:1] = -90.0 |
---|
132 | #bounds_lat_grid_U [ 0, :, 0:1] = -90.0 |
---|
133 | #bounds_lat_grid_V [ 0, :, 0:1] = -90.0 |
---|
134 | #bounds_lat_grid_F [ 0, :, 0:1] = -90.0 |
---|
135 | |
---|
136 | #bounds_lon_grid_T [ 0, :, 0:1] = bounds_lon_grid_T [ 0, :, 2:3 ] |
---|
137 | #bounds_lon_grid_U [ 0, :, 0:1] = bounds_lon_grid_U [ 0, :, 2:3 ] |
---|
138 | #bounds_lon_grid_V [ 0, :, 0:1] = bounds_lon_grid_V [ 0, :, 2:3 ] |
---|
139 | #bounds_lon_grid_F [ 0, :, 0:1] = bounds_lon_grid_F [ 0, :, 2:3 ] |
---|
140 | |
---|
141 | print 'lbc' |
---|
142 | for nn in range (4) : |
---|
143 | print 'nn : ', nn |
---|
144 | bounds_lon_grid_T [:,:,nn] = nemo.lbc (bounds_lon_grid_T [:,:,nn], npolj=6, cd_type='T', psgn=1.0) |
---|
145 | bounds_lat_grid_T [:,:,nn] = nemo.lbc (bounds_lat_grid_T [:,:,nn], npolj=6, cd_type='T', psgn=1.0) |
---|
146 | bounds_lon_grid_U [:,:,nn] = nemo.lbc (bounds_lon_grid_U [:,:,nn], npolj=6, cd_type='U', psgn=1.0) |
---|
147 | bounds_lat_grid_U [:,:,nn] = nemo.lbc (bounds_lat_grid_U [:,:,nn], npolj=6, cd_type='U', psgn=1.0) |
---|
148 | bounds_lon_grid_V [:,:,nn] = nemo.lbc (bounds_lon_grid_V [:,:,nn], npolj=6, cd_type='V', psgn=1.0) |
---|
149 | bounds_lat_grid_V [:,:,nn] = nemo.lbc (bounds_lat_grid_V [:,:,nn], npolj=6, cd_type='V', psgn=1.0) |
---|
150 | bounds_lon_grid_F [:,:,nn] = nemo.lbc (bounds_lon_grid_F [:,:,nn], npolj=6, cd_type='F', psgn=1.0) |
---|
151 | bounds_lat_grid_F [:,:,nn] = nemo.lbc (bounds_lat_grid_F [:,:,nn], npolj=6, cd_type='F', psgn=1.0) |
---|
152 | |
---|
153 | bounds_lon_grid_T[:,:,nn] = clo_lon ( bounds_lon_grid_T[:,:,nn], lon_ref=glamt ) |
---|
154 | bounds_lon_grid_U[:,:,nn] = clo_lon ( bounds_lon_grid_U[:,:,nn], lon_ref=glamu ) |
---|
155 | bounds_lon_grid_V[:,:,nn] = clo_lon ( bounds_lon_grid_V[:,:,nn], lon_ref=glamv ) |
---|
156 | bounds_lon_grid_F[:,:,nn] = clo_lon ( bounds_lon_grid_F[:,:,nn], lon_ref=glamf ) |
---|
157 | |
---|
158 | |
---|
159 | f_grid.write ( bounds_lon_grid_T ) |
---|
160 | f_grid.write ( bounds_lat_grid_T ) |
---|
161 | f_grid.write ( bounds_lon_grid_U ) |
---|
162 | f_grid.write ( bounds_lat_grid_U ) |
---|
163 | f_grid.write ( bounds_lon_grid_V ) |
---|
164 | f_grid.write ( bounds_lat_grid_V ) |
---|
165 | f_grid.write ( bounds_lon_grid_F ) |
---|
166 | f_grid.write ( bounds_lat_grid_F ) |
---|