source: TOOLS/MOSAIX/ComputeNemoCoast.py @ 3901

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

O.M. : add weight for coastal points only, to prepare computation

of weights for river run-off

  • Property svn:keywords set to Date Revision HeadURL Author Id
File size: 4.6 KB
Line 
1### ===========================================================================
2###
3### Compute a mask for coastal point of NEMO domain
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 netCDF4
24import nemo
25import numpy as np
26import getopt, sys
27
28def usage () :
29    texte = """Compute a mask for coastal point of NEMO domain
30
31    %(prog)s usage :
32python %(prog)s [-h|--help] [-d|--debug] [-i <orca grid file>] [-n <perio>]
33 -d         | --debug         : debug
34 -i <file>  | --input=<file>  : input file  (default: none)
35 -n <perio> | --perio=<perio> : periodicity type (default: try to guess)
36    #   1, 4, 6 : Cyclic on i dimension (generaly longitudes)
37    #   2       : Obsolete (was symmetric condition at southern boundary ?)
38    #   3, 4    : North fold T-point pivot (legacy ORCA2)
39    #   5, 6    : North fold F-point pivot (ORCA1, ORCA025, ORCA2 with new grid for paleo)
40    If <perio> is no specified,  %(prog)s will try to guess it from the grid dimension
41example :
42    python %(prog)s -n 4 -i ORCA2.3_coordinates_mask.nc
43    """
44    print ( texte % { 'prog':sys.argv[0] } )
45   
46## Default input parameters
47nperio      = None
48GridFile    = None
49Debug       = False
50
51## Command line options
52try:
53    myopts, myargs = getopt.getopt ( sys.argv[1:], 'i:n:h', [ 'input=', 'nperio=', 'debug=', 'help' ] )
54except getopt.GetoptError as cmdle :
55    print ( "Command line error : "+str(cmdle)+"\n" )
56    usage ()
57    sys.exit(1)
58
59for myopt, myval in myopts :
60    if myopt in [ '-h', '--help'   ] : usage () ; sys.exit (0) ;
61    if myopt in [ '-i', '--input'  ] : GridFile = myval ;
62    if myopt in [ '-n', '--nperio' ] : nperio   = int(myval) ;
63    if myopt in [ '-d', '--debug'  ] : Debug    = True ;
64##
65if GridFile == None :
66    print ("Input grid file not specified\n")
67    usage ( )
68    sys.exit(-1)
69
70print ("Input file : " + GridFile)   
71
72## Open grid file
73GridFile = netCDF4.Dataset ( GridFile, "r+" )
74
75if nperio == None : # Try to get periodicity for the grid
76    print ("Trying to guess nperio parameter")
77    jpoi = GridFile.dimensions["x_grid_T"].size
78    jpoj = GridFile.dimensions["y_grid_T"].size
79    print ("Grid dimensions: ("+str(jpoj)+", "+str(jpoi)+")")
80    if (jpoj, jpoi) == (149, 182):
81        print ("ORCA 2 grid found: nperio may vary for this configuration")
82    if (jpoj, jpoi) == (332, 292):
83        nperio = 6
84        print ("ORCA1 grid found" )
85    if (jpoj, jpoi) == (332, 362):
86        nperio = 6
87        print ("eORCA1 grid found" )
88####   
89if nperio == None :
90    print ("Periodicity not specified and not found\n")
91    usage ( )
92    sys.exit(-1)
93
94###########################################################
95   
96print ("Periodicity : "+ str(nperio) )
97
98if nperio in  [1, 4, 6] : print ("Cyclic on i dimension (generaly longitudes)")
99if nperio in  [2,     ] : print ("Obsolete (was symmetric condition at southern boundary ?)")
100if nperio in  [3, 4   ] : print ("North fold T-point pivot (legacy ORCA2)")
101if nperio in  [5, 6   ] : print ("North fold F-point pivot (ORCA1, ORCA025, ORCA2 with new grid for paleo)")
102
103###   
104OceMask = GridFile.variables["maskutil_T"]
105OceMask = np.array ( OceMask)
106
107OceMask = nemo.lbc ( np.int_(OceMask[:,:]), nperio=nperio, cd_type='T' )
108
109Temp = OceMask*0
110Temp[1:-1, 1:-1] =   \
111       + OceMask[0:-2, 1:-1] + OceMask[2:  , 1:-1] \
112       + OceMask[1:-1, 0:-2] + OceMask[1:-1, 2:  ] \
113       + OceMask[0:-2, 0:-2] + OceMask[1:-1, 0:-2] \
114       + OceMask[1:-1, 1:-1] + OceMask[0:-2, 1:-1] 
115
116CoastCrit = Temp.max()
117print ("Maximum number of neighbors : "+str(CoastCrit) )
118
119Temp = nemo.lbc ( Temp,  nperio=nperio, cd_type='T' )
120
121OceCoastal = np.where (OceMask == 1, True, False) * np.where (Temp < CoastCrit, True, False)
122OceCoastal = np.where (OceCoastal, 1, 0)
123
124varOceCoastal = GridFile.createVariable  ("OceCoastal", "i4", ( "y_grid_T", "x_grid_T") )
125varOceCoastal.cell_measures = "area: area_grid_T"
126varOceCoastal.coordinates   = "nav_lat_grid_T nav_lon_grid_T"
127
128varOceCoastal[:,:] = OceCoastal[:,:]
129
130
131GridFile.sync()
132GridFile.close()
Note: See TracBrowser for help on using the repository browser.