1 | ### =========================================================================== |
---|
2 | ### |
---|
3 | ### Checks integrals |
---|
4 | ### |
---|
5 | ### =========================================================================== |
---|
6 | ## |
---|
7 | ## Warning, to install, configure, run, use any of Olivier Marti's |
---|
8 | ## software or to read the associated documentation you'll need at least |
---|
9 | ## one (1) brain in a reasonably working order. Lack of this implement |
---|
10 | ## will void any warranties (either express or implied). |
---|
11 | ## O. Marti assumes no responsability for errors, omissions, |
---|
12 | ## data loss, or any other consequences caused directly or indirectly by |
---|
13 | ## the usage of his software by incorrectly or partially configured |
---|
14 | ## personal. |
---|
15 | ## |
---|
16 | ## SVN information |
---|
17 | __Author__ = "$Author$" |
---|
18 | __Date__ = "$Date$" |
---|
19 | __Revision__ = "$Revision$" |
---|
20 | __Id__ = "$Id: $" |
---|
21 | __HeadURL = "$HeadURL$" |
---|
22 | |
---|
23 | import cdms2 |
---|
24 | import numpy as np |
---|
25 | import glob |
---|
26 | import sys, argparse, textwrap |
---|
27 | import nemo |
---|
28 | |
---|
29 | # Creating a parser |
---|
30 | parser = argparse.ArgumentParser ( |
---|
31 | description = """Compute integral of matching coupling fields on atmosphere and ocean side.""", |
---|
32 | epilog='-------- This is the end of the help message --------') |
---|
33 | |
---|
34 | # Adding arguments |
---|
35 | parser.add_argument ( '--file' , help='Input file, e.g. dia_tICO40_to_teORCA1.2_1storder_false.nc', type=str, default='dia_tico_to_torc_1storder_false.nc' ) |
---|
36 | parser.add_argument ( '--grids' , help='grids file', type=str, default='grids.nc' ) |
---|
37 | parser.add_argument ( '--areas' , help='areas file', type=str, default='areas.nc' ) |
---|
38 | parser.add_argument ( '--masks' , help='masks file', type=str, default='masks.nc' ) |
---|
39 | parser.add_argument ( '--fracs' , help='fracs file', type=str, default=None ) |
---|
40 | parser.add_argument ( '--guess' , help='If set, build grids, areas and masks file name from the dia file name', action="store_true", default=False ) |
---|
41 | parser.add_argument ( '--src_int' , help='Source field is integrated' , action="store_true" ) |
---|
42 | parser.add_argument ( '--dst_int' , help='Destination field is integrated' , action="store_true" ) |
---|
43 | parser.add_argument ( '--src_nofrac', help='Do not apply frac on source' , action="store_true" ) |
---|
44 | parser.add_argument ( '--dst_nofrac', help='Do not apply frac on destination', action="store_true" ) |
---|
45 | |
---|
46 | # Parse command line |
---|
47 | myargs = parser.parse_args() |
---|
48 | |
---|
49 | # |
---|
50 | name_input = myargs.file |
---|
51 | |
---|
52 | # Determination des modeles |
---|
53 | Elements = myargs.file.split('_') |
---|
54 | |
---|
55 | # |
---|
56 | Name_src = Elements[1][1:] ; Name_dst = Elements[3][1:] |
---|
57 | grid_src = Elements[1][0] ; grid_dst = Elements[3][0] |
---|
58 | |
---|
59 | Grid_src = grid_src.upper() ; Grid_dst = grid_dst.upper() |
---|
60 | if Grid_src in ( 'A', 'C') : Grid_src = 'T' |
---|
61 | if Grid_dst in ( 'A', 'C') : Grid_dst = 'T' |
---|
62 | |
---|
63 | # src side |
---|
64 | if Name_src.count('ico') != 0 or Name_src.count('lmd') != 0 : name_src = Name_src ; ShortNames = True |
---|
65 | else : |
---|
66 | if Name_src.count('ICO') : name_src = 'ico' ; ShortNames = False |
---|
67 | if Name_src.count('LMD') : name_src = 'lmd' ; ShortNames = False |
---|
68 | |
---|
69 | if Name_dst.count('orc') != 0 : name_src = Name_src ; ShortNames = True |
---|
70 | else : |
---|
71 | if Name_src.count('ORC')!= 0 : name_src = 'orc' ; ShortNames = False |
---|
72 | |
---|
73 | # dst side |
---|
74 | if Name_dst.count('ico')!= 0 or Name_dst.count('lmd') != 0 : name_dst = Name_dst ; ShortNames = True |
---|
75 | else : |
---|
76 | if Name_dst.count('ICO') : name_dst = 'ico' ; ShortNames = False |
---|
77 | if Name_dst.count('LMD') : name_dst = 'lmd' ; ShortNames = False |
---|
78 | |
---|
79 | if Name_dst.count('orc') != 0 : name_dst = Name_dst ; ShortNames = True |
---|
80 | else : |
---|
81 | if Name_dst.count('ORC') != 0 : name_dst = 'orc' ; ShortNames = False |
---|
82 | |
---|
83 | if Name_src.count('ORC') : CplModel = Name_src + 'x' + Name_dst |
---|
84 | if Name_dst.count('ORC') : CplModel = Name_dst + 'x' + Name_src |
---|
85 | |
---|
86 | print ('CplModel : ' + CplModel ) |
---|
87 | print ('Input file: ' + name_input ) |
---|
88 | |
---|
89 | # Coordonnees et masques |
---|
90 | if myargs.fracs == None : |
---|
91 | if Name_src.count('ORC') : name_frc = Name_dst + '_grid_maskFrom_' + Name_src + '.nc' |
---|
92 | if Name_dst.count('ORC') : name_frc = Name_src + '_grid_maskFrom_' + Name_dst + '.nc' |
---|
93 | else: |
---|
94 | name_frc = myargs.fracs |
---|
95 | |
---|
96 | if myargs.guess : |
---|
97 | n_grids = 'grids_' + CplModel + '.nc' |
---|
98 | n_masks = 'masks_' + CplModel + '.nc' |
---|
99 | n_areas = 'areas_' + CplModel + '.nc' |
---|
100 | else : |
---|
101 | n_grids = myargs.grids |
---|
102 | n_masks = myargs.masks |
---|
103 | n_areas = myargs.areas |
---|
104 | |
---|
105 | print ('Opening grids file: ' + n_grids ) |
---|
106 | f_grids = cdms2.open ( n_grids ) |
---|
107 | print ('Opening masks file: ' + n_masks ) |
---|
108 | f_masks = cdms2.open ( n_masks ) |
---|
109 | print ('Opening areas file: ' + n_areas ) |
---|
110 | f_areas = cdms2.open ( n_areas ) |
---|
111 | print ('Opening fracs file: ' + name_frc ) |
---|
112 | f_frac = cdms2.open ( name_frc ) |
---|
113 | |
---|
114 | # |
---|
115 | msk_src = np.float64(1.0) - f_masks ( grid_src + name_src + '.msk', squeeze=True ) |
---|
116 | lon_src = f_grids ( grid_src + name_src + '.lon', squeeze=True ) |
---|
117 | lat_src = f_grids ( grid_src + name_src + '.lat', squeeze=True ) |
---|
118 | if myargs.src_int : srf_src = np.ones ( (msk_src.shape), dtype=np.float64 ) |
---|
119 | else : srf_src = f_areas ( grid_src + name_src + '.srf', squeeze=True ) |
---|
120 | |
---|
121 | # |
---|
122 | msk_dst = np.float64(1.0) - f_masks ( grid_dst + name_dst + '.msk', squeeze=True ) |
---|
123 | lon_dst = f_grids ( grid_dst + name_dst + '.lon', squeeze=True ) |
---|
124 | lat_dst = f_grids ( grid_dst + name_dst + '.lat', squeeze=True ) |
---|
125 | if myargs.dst_int : srf_dst = np.ones ( (msk_dst.shape), dtype=np.float64) |
---|
126 | else : srf_dst = f_areas ( grid_dst + name_dst + '.srf' ) |
---|
127 | |
---|
128 | # |
---|
129 | if name_src in ( 'ico', 'lmd' ) : |
---|
130 | if myargs.src_nofrac : frc_src = np.ones ( (msk_src.shape), dtype=np.float64) |
---|
131 | else : frc_src = f_frac ( 'OceFrac', squeeze=True ) |
---|
132 | if name_src == 'orc' : frc_src = np.ones ( (msk_src.shape), dtype=np.float64) |
---|
133 | if name_dst in ( 'ico', 'lmd' ) : |
---|
134 | if myargs.dst_nofrac : frc_dst = np.ones ( (msk_dst.shape), dtype=np.float64) |
---|
135 | else : frc_dst = f_frac ( 'OceFrac', squeeze=True ) |
---|
136 | if name_dst == 'orc' : frc_dst = np.ones ( (msk_dst.shape), dtype=np.float64) |
---|
137 | |
---|
138 | # Periodicity |
---|
139 | nperio_src = 0 ; nperio_dst = 0 |
---|
140 | |
---|
141 | if Name_src == 'ORCA2.3' : nperio_src = 4 |
---|
142 | if Name_src == 'eORCA1.2' : nperio_src = 6 |
---|
143 | if Name_src == 'eORCA025' : nperio_src = 6 |
---|
144 | |
---|
145 | if Name_dst == 'ORCA2.3' : nperio_dst = 4 |
---|
146 | if Name_dst == 'eORCA1.2' : nperio_dst = 6 |
---|
147 | if Name_dst == 'eORCA025' : nperio_dst = 6 |
---|
148 | |
---|
149 | # NEMO periodicity. |
---|
150 | if Name_src in ('ORCA2.3', 'eORCA1.2', 'eORCA025') : msk_src = nemo.lbc_mask ( msk_src, nperio=nperio_src, cd_type=Grid_src ) |
---|
151 | if Name_dst in ('ORCA2.3', 'eORCA1.2', 'eORCA025') : msk_dst = nemo.lbc_mask ( msk_dst, nperio=nperio_dst, cd_type=Grid_dst ) |
---|
152 | |
---|
153 | # Surfaces |
---|
154 | area_src = np.sum ( srf_src * msk_src * frc_src ) |
---|
155 | area_dst = np.sum ( srf_dst * msk_dst * frc_dst ) |
---|
156 | |
---|
157 | print ( Name_src ) |
---|
158 | print 'mask: {:12.3} {:12.3} {:12.3}'.format( np.min(msk_src), np.max(msk_src), np.sum(msk_src) ) |
---|
159 | print 'frac: {:12.3} {:12.3} {:12.3}'.format( np.min(frc_src), np.max(frc_src), np.sum(frc_src) ) |
---|
160 | print 'area: {:12.3} {:12.3} {:12.3}'.format( np.min(srf_src), np.max(srf_src), np.sum(srf_src) ) |
---|
161 | |
---|
162 | print ( Name_dst ) |
---|
163 | print 'mask: {:12.3} {:12.3} {:12.3}'.format( np.min(msk_dst), np.max(msk_dst), np.sum(msk_dst) ) |
---|
164 | print 'frac: {:12.3} {:12.3} {:12.3}'.format( np.min(frc_dst), np.max(frc_dst), np.sum(frc_dst) ) |
---|
165 | print 'area: {:12.3} {:12.3} {:12.3}'.format( np.min(srf_dst), np.max(srf_dst), np.sum(srf_dst) ) |
---|
166 | print (' ') |
---|
167 | print ( "Surfaces : %14.6e %14.6e %11.3e"%( area_src, area_dst, (area_src-area_dst)/(area_src+area_dst)*0.5 ) ) |
---|
168 | print (' ') |
---|
169 | |
---|
170 | ## |
---|
171 | f_input = cdms2.open ( name_input ) |
---|
172 | # Loop over fields |
---|
173 | for num in np.arange (1, 7) : |
---|
174 | name_src = 'field{:02}_src'.format(num) |
---|
175 | name_dst = 'field{:02}_dst'.format(num) |
---|
176 | print ( '{:02}'.format(num) + ':' + name_src + ':' + name_dst) |
---|
177 | |
---|
178 | # Reading field |
---|
179 | v_src = f_input ( name_src ) |
---|
180 | v_dst = f_input ( name_dst ) |
---|
181 | |
---|
182 | # Compute integrals |
---|
183 | sum_src = np.sum (v_src * srf_src * msk_src * frc_src) |
---|
184 | sum_dst = np.sum (v_dst * srf_dst * msk_dst * frc_dst) |
---|
185 | |
---|
186 | # Compute average |
---|
187 | mean_src = sum_src / area_src |
---|
188 | mean_dst = sum_dst / area_dst |
---|
189 | |
---|
190 | # |
---|
191 | print ( "Integrals : %14.6e %14.6e %11.3e"%( sum_src , sum_dst , (sum_src - sum_dst)/(sum_src + sum_dst)*0.5 ) ) |
---|
192 | print ( "Averages : %14.6e %14.6e %11.3e"%( mean_src, mean_dst, (mean_src-mean_dst)/(mean_src+mean_dst)*0.5 ) ) |
---|
193 | print ( " " ) |
---|
194 | #======================================= |
---|