source: TOOLS/MOSAIX/Utils/creates_nemo_bounds.py @ 3740

Last change on this file since 3740 was 3665, checked in by omamce, 6 years ago

O.M. : mise a jour des properties

  • Property svn:keywords set to Date Revision HeadURL Author Id
File size: 7.8 KB
Line 
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## -
10print 'Demarrage'
11## - Needed Python modules
12import cdms2, MV2, numpy as np
13import nemo
14
15def 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:
25cdms2.setNetcdfShuffleFlag     (0)
26cdms2.setNetcdfDeflateFlag     (0)
27cdms2.setNetcdfDeflateLevelFlag(0)
28
29print 'Ouverture fichier'
30f_grid = cdms2.open ( 'tmp_ORCA025_coordinates_mask.nc', 'r+' )
31
32print 'Lecture tmask'
33tmask = f_grid ( 'mask_T' )
34
35print 'Lecture des axes'
36yy      = tmask.getAxis(0) ; ny = len(yy)
37xx      = tmask.getAxis(1) ; nx = len(xx)
38
39print 'Creation axe'
40nvertex = cdms2.createAxis ( np.arange (4) )
41
42print 'Lecture lon lat'
43glamt =  f_grid ( 'nav_lon_grid_T' ).filled(0)
44gphit =  f_grid ( 'nav_lat_grid_T' ).filled(0)
45glamu =  f_grid ( 'nav_lon_grid_U' ).filled(0)
46gphiu =  f_grid ( 'nav_lat_grid_U' ).filled(0)
47glamv =  f_grid ( 'nav_lon_grid_V' ).filled(0)
48gphiv =  f_grid ( 'nav_lat_grid_V' ).filled(0)
49glamf =  f_grid ( 'nav_lon_grid_F' ).filled(0)
50gphif =  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
60print 'Creation bounds_*_T'
61bounds_lon_grid_T = np.zeros ( (ny, nx, 4), dtype=np.float32 )
62bounds_lat_grid_T = np.zeros ( (ny, nx, 4), dtype=np.float32 )
63
64print 'Calcul bounds_*_T'
65bounds_lon_grid_T [ :, :, 0] = np.roll ( np.roll ( glamf [:,:], 1, axis=0), 1, axis=1) # EOSHIFT (CSHIFT (glamf(:,:), -1, dim = 1), -1, dim = 2 )
66bounds_lon_grid_T [ :, :, 1] = np.roll ( glamf [:,:], 1, axis=0) # CSHIFT  (glamf(:,:), -1, dim = 2 )
67bounds_lon_grid_T [ :, :, 3] = np.roll ( glamf [:,:], 1, axis=1) # CSHIFT  (glamf(:,:), -1, dim = 1 )
68bounds_lon_grid_T [ :, :, 2] =           glamf [:,:]
69
70bounds_lat_grid_T [ :, :, 0] = np.roll ( np.roll ( gphif [:,:], 1, axis=0), 1, axis=1)
71bounds_lat_grid_T [ :, :, 1] = np.roll ( gphif [:,:], 1, axis=0)
72bounds_lat_grid_T [ :, :, 3] = np.roll ( gphif [:,:], 1, axis=1)
73bounds_lat_grid_T [ :, :, 2] =           gphif [:,:]
74
75bounds_lon_grid_T = cdms2.createVariable ( bounds_lon_grid_T, axes=(yy, xx, nvertex), id='bounds_lon_grid_T', dtype=np.float32 )
76bounds_lat_grid_T = cdms2.createVariable ( bounds_lat_grid_T, axes=(yy, xx, nvertex), id='bounds_lat_grid_T', dtype=np.float32 )
77
78print 'Calcul bounds_*_U'
79bounds_lon_grid_U = np.zeros ( (ny, nx, 4), dtype=np.float32 )
80bounds_lat_grid_U = np.zeros ( (ny, nx, 4), dtype=np.float32 )
81
82bounds_lon_grid_U [ :, :, 0] = np.roll ( glamv [:,:], 1, axis=0) # CSHIFT  (glamv(:,:), -1, dim = 2 )
83bounds_lon_grid_U [ :, :, 1] = np.roll ( np.roll ( glamv [:,:], 1, axis=0), 1, axis=1) # EOSHIFT (CSHIFT (glamv(:,:),  1, dim = 1 ), -1, dim = 2)
84bounds_lon_grid_U [ :, :, 3] =           glamv [:,:]
85bounds_lon_grid_U [ :, :, 2] = np.roll ( glamv [:,:], 1, axis=1) # CSHIFT  (glamv(:,:),  1, dim = 1 )
86
87bounds_lat_grid_U [ :, :, 0] = np.roll ( gphiv [:,:], 1, axis=0)
88bounds_lat_grid_U [ :, :, 1] = np.roll ( np.roll ( gphiv [:,:], 1, axis=0), 1, axis=1)
89bounds_lat_grid_U [ :, :, 3] =           gphiv [:,:]
90bounds_lat_grid_U [ :, :, 2] = np.roll ( gphiv [:,:], 1, axis=1)
91
92bounds_lon_grid_U = cdms2.createVariable ( bounds_lon_grid_U, axes = ( yy, xx, nvertex ), id='bounds_lon_grid_U' )
93bounds_lat_grid_U = cdms2.createVariable ( bounds_lat_grid_U, axes = ( yy, xx, nvertex ), id='bounds_lon_grid_U' )
94
95print 'Calcul bounds_*_V'
96bounds_lon_grid_V = np.zeros ( (ny, nx, 4), dtype=np.float32 )
97bounds_lat_grid_V = np.zeros ( (ny, nx, 4), dtype=np.float32 )
98
99bounds_lon_grid_V [ :, :, 0] = np.roll (glamu [:,:], 1, axis=1) # CSHIFT  (glamu(:,:), -1, dim = 1 )
100bounds_lon_grid_V [ :, :, 1] =          glamu [:,:]
101bounds_lon_grid_V [ :, :, 3] = np.roll (np.roll (glamu [:,:], 1, axis=0), 1, axis=1) # EOSHIFT (CSHIFT (glamu(:,:), -1, dim = 1 ),  1, dim = 2)
102bounds_lon_grid_V [ :, :, 2] = np.roll (glamu [:,:], 1, axis=0) # EOSHIFT (glamu(:,:), 1, dim = 2 )
103
104bounds_lat_grid_V [ :, :, 0] = np.roll (gphiu [:,:], 1, axis=1)
105bounds_lat_grid_V [ :, :, 1] =          gphiu [:,:]
106bounds_lat_grid_V [ :, :, 3] = np.roll (np.roll (glamu [:,:], 1, axis=0), 1, axis=1)
107bounds_lat_grid_V [ :, :, 2] = np.roll (gphiu [:,:], 1, axis=0) 
108
109bounds_lon_grid_V = cdms2.createVariable ( bounds_lon_grid_V, axes = ( yy, xx, nvertex ), id='bounds_lon_grid_V' )
110bounds_lat_grid_V = cdms2.createVariable ( bounds_lat_grid_V, axes = ( yy, xx, nvertex ), id='bounds_lat_grid_V' )
111
112print 'Calcul bounds_*_F'
113bounds_lon_grid_F = np.zeros((ny, nx, 4), dtype=np.float32)
114bounds_lat_grid_F = np.zeros((ny, nx, 4), dtype=np.float32)
115
116bounds_lon_grid_F [ :, :, 0] =          glamt [:,:]
117bounds_lon_grid_F [ :, :, 1] = np.roll (glamt [:,:], 1, axis=1) # CSHIFT  (glamt(:,:), 1, dim = 1 )
118bounds_lon_grid_F [ :, :, 3] = np.roll (glamt [:,:], 1, axis=0) # EOSHIFT (glamt(:,:), 1, dim = 2 )
119bounds_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
121bounds_lat_grid_F [ :, :, 0] =          gphit [:,:]
122bounds_lat_grid_F [ :, :, 1] = np.roll (gphit [:,:], 1, axis=1) # CSHIFT  (glamt(:,:), 1, dim = 1 )
123bounds_lat_grid_F [ :, :, 3] = np.roll (gphit [:,:], 1, axis=0) # EOSHIFT (glamt(:,:), 1, dim = 2 )
124bounds_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
126bounds_lon_grid_F = cdms2.createVariable ( bounds_lon_grid_F, axes = ( yy, xx, nvertex ), id='bounds_lon_grid_F' )
127bounds_lat_grid_F = cdms2.createVariable ( bounds_lat_grid_F, axes = ( yy, xx, nvertex ), id='bounds_lat_grid_F' )
128
129print '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
141print 'lbc'
142for 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
159f_grid.write ( bounds_lon_grid_T )
160f_grid.write ( bounds_lat_grid_T )
161f_grid.write ( bounds_lon_grid_U )
162f_grid.write ( bounds_lat_grid_U )
163f_grid.write ( bounds_lon_grid_V )
164f_grid.write ( bounds_lat_grid_V )
165f_grid.write ( bounds_lon_grid_F )
166f_grid.write ( bounds_lat_grid_F )
Note: See TracBrowser for help on using the repository browser.