source: TOOLS/MOSAIX/check_conserv.py @ 4089

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

O.M. : add cases to check_conserv.py

  • Property svn:keywords set to Date Revision HeadURL Author
File size: 7.9 KB
Line 
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
23import cdms2
24import numpy as np
25import glob
26import sys, argparse, textwrap
27import nemo
28
29# Creating a parser
30parser = 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
35parser.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' )
36parser.add_argument ( '--grids'     , help='grids file', type=str, default='grids.nc' )
37parser.add_argument ( '--areas'     , help='areas file', type=str, default='areas.nc' )
38parser.add_argument ( '--masks'     , help='masks file', type=str, default='masks.nc' )
39parser.add_argument ( '--fracs'     , help='fracs file', type=str, default=None )
40parser.add_argument ( '--guess'     , help='If set, build grids, areas and masks file name from the dia file name', action="store_true", default=False )
41parser.add_argument ( '--src_int'   , help='Source field is integrated'      , action="store_true" )
42parser.add_argument ( '--dst_int'   , help='Destination field is integrated' , action="store_true" )
43parser.add_argument ( '--src_nofrac', help='Do not apply frac on source'     , action="store_true" )
44parser.add_argument ( '--dst_nofrac', help='Do not apply frac on destination', action="store_true" )
45
46# Parse command line
47myargs = parser.parse_args()
48
49#
50name_input = myargs.file
51
52# Determination des modeles
53Elements = myargs.file.split('_')
54
55#
56Name_src = Elements[1][1:] ; Name_dst = Elements[3][1:]
57grid_src = Elements[1][0]  ; grid_dst = Elements[3][0]
58
59Grid_src = grid_src.upper() ; Grid_dst = grid_dst.upper()
60if Grid_src in ( 'A', 'C') : Grid_src = 'T'
61if Grid_dst in ( 'A', 'C') : Grid_dst = 'T'
62
63# src side
64if Name_src.count('ico') != 0 or Name_src.count('lmd') != 0 : name_src = Name_src ; ShortNames = True
65else :
66    if Name_src.count('ICO') : name_src = 'ico' ; ShortNames = False
67    if Name_src.count('LMD') : name_src = 'lmd' ; ShortNames = False
68
69if Name_dst.count('orc') != 0    : name_src = Name_src ; ShortNames = True
70else :
71    if Name_src.count('ORC')!= 0 : name_src = 'orc'    ; ShortNames = False
72       
73# dst side
74if Name_dst.count('ico')!= 0 or Name_dst.count('lmd') != 0 : name_dst = Name_dst ; ShortNames = True
75else :
76    if Name_dst.count('ICO') : name_dst = 'ico' ; ShortNames = False
77    if Name_dst.count('LMD') : name_dst = 'lmd' ; ShortNames = False
78
79if Name_dst.count('orc') != 0 : name_dst = Name_dst ; ShortNames = True
80else :
81    if Name_dst.count('ORC') != 0 : name_dst = 'orc' ; ShortNames = False
82   
83if Name_src.count('ORC') : CplModel = Name_src + 'x' + Name_dst
84if Name_dst.count('ORC') : CplModel = Name_dst + 'x' + Name_src
85
86print ('CplModel : ' + CplModel )
87print ('Input file: ' + name_input )
88     
89# Coordonnees et masques
90if 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'
93else:
94    name_frc = myargs.fracs
95
96if myargs.guess :
97    n_grids =  'grids_' + CplModel + '.nc'
98    n_masks =  'masks_' + CplModel + '.nc'
99    n_areas =  'areas_' + CplModel + '.nc'
100else :
101    n_grids = myargs.grids
102    n_masks = myargs.masks
103    n_areas = myargs.areas
104
105print ('Opening grids file: ' + n_grids )
106f_grids = cdms2.open ( n_grids )
107print ('Opening masks file: ' + n_masks )
108f_masks = cdms2.open ( n_masks )
109print ('Opening areas file: ' + n_areas )
110f_areas = cdms2.open ( n_areas )
111print ('Opening fracs file: ' + name_frc )   
112f_frac  = cdms2.open ( name_frc     )
113
114#
115msk_src = np.float64(1.0) - f_masks ( grid_src + name_src + '.msk', squeeze=True )
116lon_src = f_grids ( grid_src + name_src + '.lon', squeeze=True )
117lat_src = f_grids ( grid_src + name_src + '.lat', squeeze=True )
118if myargs.src_int : srf_src = np.ones ( (msk_src.shape), dtype=np.float64 )
119else              : srf_src = f_areas ( grid_src + name_src + '.srf', squeeze=True )
120
121#
122msk_dst = np.float64(1.0) - f_masks ( grid_dst + name_dst + '.msk', squeeze=True )
123lon_dst = f_grids ( grid_dst + name_dst + '.lon', squeeze=True )
124lat_dst = f_grids ( grid_dst + name_dst + '.lat', squeeze=True )
125if myargs.dst_int : srf_dst = np.ones ( (msk_dst.shape), dtype=np.float64)
126else              : srf_dst = f_areas ( grid_dst + name_dst + '.srf' )
127
128#
129if 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 )
132if name_src == 'orc'     : frc_src = np.ones ( (msk_src.shape), dtype=np.float64)
133if 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 )
136if name_dst == 'orc'     : frc_dst = np.ones ( (msk_dst.shape), dtype=np.float64)
137
138# Periodicity
139nperio_src = 0 ; nperio_dst = 0
140
141if Name_src == 'ORCA2.3'  : nperio_src = 4
142if Name_src == 'eORCA1.2' : nperio_src = 6
143if Name_src == 'eORCA025' : nperio_src = 6
144
145if Name_dst == 'ORCA2.3'  : nperio_dst = 4
146if Name_dst == 'eORCA1.2' : nperio_dst = 6
147if Name_dst == 'eORCA025' : nperio_dst = 6
148
149# NEMO periodicity.
150if Name_src in ('ORCA2.3', 'eORCA1.2', 'eORCA025') : msk_src = nemo.lbc_mask ( msk_src, nperio=nperio_src, cd_type=Grid_src )
151if 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
154area_src = np.sum ( srf_src * msk_src * frc_src )
155area_dst = np.sum ( srf_dst * msk_dst * frc_dst )
156
157print ( Name_src )
158print 'mask: {:12.3} {:12.3} {:12.3}'.format( np.min(msk_src), np.max(msk_src), np.sum(msk_src) )
159print 'frac: {:12.3} {:12.3} {:12.3}'.format( np.min(frc_src), np.max(frc_src), np.sum(frc_src) )
160print 'area: {:12.3} {:12.3} {:12.3}'.format( np.min(srf_src), np.max(srf_src), np.sum(srf_src) )
161
162print ( Name_dst )
163print 'mask: {:12.3} {:12.3} {:12.3}'.format( np.min(msk_dst), np.max(msk_dst), np.sum(msk_dst) )
164print 'frac: {:12.3} {:12.3} {:12.3}'.format( np.min(frc_dst), np.max(frc_dst), np.sum(frc_dst) )
165print 'area: {:12.3} {:12.3} {:12.3}'.format( np.min(srf_dst), np.max(srf_dst), np.sum(srf_dst) )
166print (' ')
167print ( "Surfaces   : %14.6e %14.6e %11.3e"%( area_src, area_dst, (area_src-area_dst)/(area_src+area_dst)*0.5 ) )
168print (' ')
169
170##
171f_input = cdms2.open ( name_input )
172# Loop over fields
173for 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#=======================================
Note: See TracBrowser for help on using the repository browser.