source: TOOLS/MOSAIX/check_conserv.py @ 6764

Last change on this file since 6764 was 6190, checked in by omamce, 23 months ago

O.M. : Evolution on MOSAIX

  • Add licensing information
  • Update and beautify README.md
  • Add few model version to known list
  • Property svn:keywords set to Date Revision HeadURL Author
File size: 9.1 KB
Line 
1### ===========================================================================
2###
3### Checks integrals
4###
5### ===========================================================================
6##
7##  MOSAIX is under CeCILL_V2 licence. See "Licence_CeCILL_V2-en.txt"
8##  file for an english version of the licence and
9##  "Licence_CeCILL_V2-fr.txt" for a french version.
10##
11##  Permission is hereby granted, free of charge, to any person or
12##  organization obtaining a copy of the software and accompanying
13##  documentation covered by this license (the "Software") to use,
14##  reproduce, display, distribute, execute, and transmit the
15##  Software, and to prepare derivative works of the Software, and to
16##  permit third-parties to whom the Software is furnished to do so,
17##  all subject to the following:
18##
19##  Warning, to install, configure, run, use any of MOSAIX software or
20##  to read the associated documentation you'll need at least one (1)
21##  brain in a reasonably working order. Lack of this implement will
22##  void any warranties (either express or implied).  Authors assumes
23##  no responsability for errors, omissions, data loss, or any other
24##  consequences caused directly or indirectly by the usage of his
25##  software by incorrectly or partially configured
26##
27#
28# python3 -i check_conserv.py --file dia_tORCA2.3_to_tLMD9695_1stOrder_Normalized_Surfacic_NoArea.nc --grids grids_ORCA2.3xLMD9695.nc --areas areas_ORCA2.3xLMD9695.nc --masks masks_ORCA2.3xLMD9695.nc --frac LMD9695_grid_maskFrom_ORCA2.3.nc
29#
30## SVN information
31__Author__   = "$Author$"
32__Date__     = "$Date$"
33__Revision__ = "$Revision$"
34__Id__       = "$Id:  $"
35__HeadURL    = "$HeadURL$"
36
37import netCDF4
38import numpy as np
39import glob
40import sys, argparse, textwrap
41import nemo
42import os
43
44# Creating a parser
45parser = argparse.ArgumentParser (
46    description = """Compute integral of matching coupling fields on atmosphere and ocean side.""",
47    epilog='-------- This is the end of the help message --------')
48
49# Adding arguments
50parser.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' )
51parser.add_argument ( '--grids'     , help='grids file', type=str, default='grids.nc' )
52parser.add_argument ( '--areas'     , help='areas file', type=str, default='areas.nc' )
53parser.add_argument ( '--masks'     , help='masks file', type=str, default='masks.nc' )
54parser.add_argument ( '--fracs'     , help='fracs file', type=str, default=None )
55parser.add_argument ( '--guess'     , help='If set, build grids, areas and masks file name from the dia file name', action="store_true", default=False )
56parser.add_argument ( '--src_int'   , help='Source field is integrated'      , action="store_true" )
57parser.add_argument ( '--dst_int'   , help='Destination field is integrated' , action="store_true" )
58parser.add_argument ( '--src_nofrac', help='Do not apply frac on source'     , action="store_true" )
59parser.add_argument ( '--dst_nofrac', help='Do not apply frac on destination', action="store_true" )
60
61# Parse command line
62myargs = parser.parse_args()
63
64#
65name_input = myargs.file
66
67# Get dir and file
68InDir  =  os.path.dirname  (os.path.abspath(myargs.file))
69InFile =  os.path.basename (os.path.abspath(myargs.file))
70
71print ('InDir  : ' + InDir )
72print ('InFile : ' + InFile )
73
74Elements = InFile.split('_')
75
76# Get models
77Name_src = Elements[1][1:] ; Name_dst = Elements[3][1:]
78grid_src = Elements[1][0]  ; grid_dst = Elements[3][0]
79
80Grid_src = grid_src.upper() ; Grid_dst = grid_dst.upper()
81if Grid_src in ( 'A', 'C') : Grid_src = 'T'
82if Grid_dst in ( 'A', 'C') : Grid_dst = 'T'
83
84# src side
85if Name_src.count('ico') != 0 or Name_src.count('lmd') != 0 : name_src = Name_src ; ShortNames = True
86else :
87    if Name_src.count('ICO') : name_src = 'ico' ; ShortNames = False
88    if Name_src.count('LMD') : name_src = 'lmd' ; ShortNames = False
89
90if Name_dst.count('orc') != 0    : name_src = Name_src ; ShortNames = True
91else :
92    if Name_src.count('ORC')!= 0 : name_src = 'orc'    ; ShortNames = False
93       
94# dst side
95if Name_dst.count('ico')!= 0 or Name_dst.count('lmd') != 0 : name_dst = Name_dst ; ShortNames = True
96else :
97    if Name_dst.count('ICO') : name_dst = 'ico' ; ShortNames = False
98    if Name_dst.count('LMD') : name_dst = 'lmd' ; ShortNames = False
99
100if Name_dst.count('orc') != 0 : name_dst = Name_dst ; ShortNames = True
101else :
102    if Name_dst.count('ORC') != 0 : name_dst = 'orc' ; ShortNames = False
103   
104if Name_src.count('ORC') : CplModel = Name_src + 'x' + Name_dst
105if Name_dst.count('ORC') : CplModel = Name_dst + 'x' + Name_src
106
107print ('CplModel   : ' + CplModel )
108print ('Input file : ' + name_input )
109     
110# Coordonnees et masques
111if myargs.fracs == None :
112    if Name_src.count('ORC') : name_frc = InDir + '/' + Name_dst + '_grid_maskFrom_' + Name_src + '.nc'
113    if Name_dst.count('ORC') : name_frc = InDir + '/' + Name_src + '_grid_maskFrom_' + Name_dst + '.nc'
114else:
115    name_frc = myargs.fracs
116
117if myargs.guess :
118    n_grids =  InDir + '/grids_' + CplModel + '.nc'
119    n_masks =  InDir + '/masks_' + CplModel + '.nc'
120    n_areas =  InDir + '/areas_' + CplModel + '.nc'
121else :
122    n_grids = myargs.grids
123    n_masks = myargs.masks
124    n_areas = myargs.areas
125
126print ('Opening grids file: ' + n_grids )
127f_grids = netCDF4.Dataset ( n_grids )
128print ('Opening masks file: ' + n_masks )
129f_masks = netCDF4.Dataset  ( n_masks )
130print ('Opening areas file: ' + n_areas )
131f_areas = netCDF4.Dataset  ( n_areas )
132print ('Opening fracs file: ' + name_frc )   
133f_frac  = netCDF4.Dataset  ( name_frc     )
134
135#
136msk_src = np.float64(1.0) - f_masks.variables[ grid_src + name_src + '.msk'][:].squeeze()
137lon_src = f_grids.variables [grid_src + name_src + '.lon'][:].squeeze()
138lat_src = f_grids.variables [grid_src + name_src + '.lat'][:].squeeze()
139if myargs.src_int : srf_src = np.ones ( (msk_src.shape), dtype=np.float64 )[:]
140else              : srf_src = f_areas.variables [grid_src + name_src + '.srf'][:].squeeze()
141
142#
143msk_dst = np.float64(1.0) - f_masks.variables[grid_dst + name_dst + '.msk'][:]
144lon_dst = f_grids.variables [grid_dst + name_dst + '.lon'][:].squeeze()
145lat_dst = f_grids.variables [grid_dst + name_dst + '.lat'][:].squeeze()
146if myargs.dst_int : srf_dst = np.ones ( (msk_dst.shape), dtype=np.float64)[:]
147else              : srf_dst = f_areas.variables [grid_dst + name_dst + '.srf'][:].squeeze()
148
149#
150if name_src in ( 'ico', 'lmd' ) :
151    if myargs.src_nofrac : frc_src = np.ones ( (msk_src.shape), dtype=np.float64)
152    else                 : frc_src = f_frac.variables ['OceFrac'][:].squeeze()
153       
154if name_src == 'orc'     : frc_src = np.ones ( (msk_src.shape), dtype=np.float64)
155   
156if name_dst in ( 'ico', 'lmd' ) :
157    if myargs.dst_nofrac : frc_dst = np.ones ( (msk_dst.shape), dtype=np.float64)
158    else                 : frc_dst = f_frac.variables['OceFrac'][:].squeeze()
159       
160if name_dst == 'orc'     : frc_dst = np.ones ( (msk_dst.shape), dtype=np.float64)
161
162# Periodicity
163nperio_src = 0 ; nperio_dst = 0
164
165if Name_src == 'ORCA2.3'  : nperio_src = 4
166if Name_src == 'eORCA1.2' : nperio_src = 6
167if Name_src == 'eORCA025' : nperio_src = 6
168
169if Name_dst == 'ORCA2.3'  : nperio_dst = 4
170if Name_dst == 'eORCA1.2' : nperio_dst = 6
171if Name_dst == 'eORCA025' : nperio_dst = 6
172
173# NEMO periodicity.
174if Name_src in ('ORCA2.3', 'eORCA1.2', 'eORCA025') : msk_src = nemo.lbc_mask ( msk_src, nperio=nperio_src, cd_type=Grid_src )
175if Name_dst in ('ORCA2.3', 'eORCA1.2', 'eORCA025') : msk_dst = nemo.lbc_mask ( msk_dst, nperio=nperio_dst, cd_type=Grid_dst )
176   
177# Surfaces
178area_src = np.sum ( srf_src * msk_src * frc_src )
179area_dst = np.sum ( srf_dst * msk_dst * frc_dst )
180
181print ( Name_src )
182print ( 'mask: {:12.3} {:12.3} {:12.3}'.format( np.min(msk_src), np.max(msk_src), np.sum(msk_src) ) )
183print ( 'frac: {:12.3} {:12.3} {:12.3}'.format( np.min(frc_src), np.max(frc_src), np.sum(frc_src) ) )
184print ( 'area: {:12.3} {:12.3} {:12.3}'.format( np.min(srf_src), np.max(srf_src), np.sum(srf_src) ) )
185
186print ( Name_dst )
187print ( 'mask: {:12.3} {:12.3} {:12.3}'.format( np.min(msk_dst), np.max(msk_dst), np.sum(msk_dst) ) )
188print ( 'frac: {:12.3} {:12.3} {:12.3}'.format( np.min(frc_dst), np.max(frc_dst), np.sum(frc_dst) ) )
189print ( 'area: {:12.3} {:12.3} {:12.3}'.format( np.min(srf_dst), np.max(srf_dst), np.sum(srf_dst) ) )
190print (' ')
191print ( "Surfaces   : %14.6e %14.6e %11.3e"%( area_src, area_dst, (area_src-area_dst)/(area_src+area_dst)*0.5 ) )
192print (' ')
193
194##
195f_input = netCDF4.Dataset( name_input )
196# Loop over fields
197for num in np.arange (1, 7) :
198    name_src = 'field{:02}_src'.format(num)
199    name_dst = 'field{:02}_dst'.format(num)
200    print ( '{:02}'.format(num) + ':' + name_src + ':' + name_dst)
201
202    # Reading field
203    v_src = f_input [ name_src ][:]
204    v_dst = f_input [ name_dst ][:]
205
206    # Compute integrals
207    sum_src = np.sum (v_src * srf_src * msk_src * frc_src)
208    sum_dst = np.sum (v_dst * srf_dst * msk_dst * frc_dst)
209   
210    # Compute average
211    mean_src = sum_src / area_src
212    mean_dst = sum_dst / area_dst
213
214    #
215    print ( "Integrals : %14.6e %14.6e %11.3e"%( sum_src , sum_dst , (sum_src - sum_dst)/(sum_src + sum_dst)*0.5 ) )
216    print ( "Averages  : %14.6e %14.6e %11.3e"%( mean_src, mean_dst, (mean_src-mean_dst)/(mean_src+mean_dst)*0.5 ) )
217    print ( " " )
218#=======================================
Note: See TracBrowser for help on using the repository browser.