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