[3723] | 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 |
---|
[3725] | 17 | __Author__ = "$Author$" |
---|
| 18 | __Date__ = "$Date$" |
---|
| 19 | __Revision__ = "$Revision$" |
---|
| 20 | __Id__ = "$Id$" |
---|
| 21 | __HeadURL = "$HeadURL$" |
---|
[3723] | 22 | ## |
---|
| 23 | import netCDF4 |
---|
| 24 | import nemo |
---|
| 25 | import numpy as np |
---|
| 26 | import getopt, sys |
---|
| 27 | |
---|
| 28 | def usage () : |
---|
[3901] | 29 | texte = """Compute a mask for coastal point of NEMO domain |
---|
| 30 | |
---|
| 31 | %(prog)s usage : |
---|
| 32 | python %(prog)s [-h|--help] [-d|--debug] [-i <orca grid file>] [-n <perio>] |
---|
[3723] | 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 |
---|
| 41 | example : |
---|
| 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 |
---|
| 47 | nperio = None |
---|
| 48 | GridFile = None |
---|
| 49 | Debug = False |
---|
| 50 | |
---|
| 51 | ## Command line options |
---|
| 52 | try: |
---|
[3901] | 53 | myopts, myargs = getopt.getopt ( sys.argv[1:], 'i:n:h', [ 'input=', 'nperio=', 'debug=', 'help' ] ) |
---|
[3723] | 54 | except getopt.GetoptError as cmdle : |
---|
| 55 | print ( "Command line error : "+str(cmdle)+"\n" ) |
---|
| 56 | usage () |
---|
| 57 | sys.exit(1) |
---|
| 58 | |
---|
| 59 | for 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 | ## |
---|
| 65 | if GridFile == None : |
---|
| 66 | print ("Input grid file not specified\n") |
---|
| 67 | usage ( ) |
---|
| 68 | sys.exit(-1) |
---|
| 69 | |
---|
[3901] | 70 | print ("Input file : " + GridFile) |
---|
[3723] | 71 | |
---|
| 72 | ## Open grid file |
---|
| 73 | GridFile = netCDF4.Dataset ( GridFile, "r+" ) |
---|
| 74 | |
---|
| 75 | if 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 | #### |
---|
| 89 | if nperio == None : |
---|
| 90 | print ("Periodicity not specified and not found\n") |
---|
| 91 | usage ( ) |
---|
| 92 | sys.exit(-1) |
---|
| 93 | |
---|
| 94 | ########################################################### |
---|
| 95 | |
---|
| 96 | print ("Periodicity : "+ str(nperio) ) |
---|
| 97 | |
---|
| 98 | if nperio in [1, 4, 6] : print ("Cyclic on i dimension (generaly longitudes)") |
---|
| 99 | if nperio in [2, ] : print ("Obsolete (was symmetric condition at southern boundary ?)") |
---|
| 100 | if nperio in [3, 4 ] : print ("North fold T-point pivot (legacy ORCA2)") |
---|
| 101 | if nperio in [5, 6 ] : print ("North fold F-point pivot (ORCA1, ORCA025, ORCA2 with new grid for paleo)") |
---|
| 102 | |
---|
| 103 | ### |
---|
| 104 | OceMask = GridFile.variables["maskutil_T"] |
---|
| 105 | OceMask = np.array ( OceMask) |
---|
| 106 | |
---|
| 107 | OceMask = nemo.lbc ( np.int_(OceMask[:,:]), nperio=nperio, cd_type='T' ) |
---|
| 108 | |
---|
| 109 | Temp = OceMask*0 |
---|
| 110 | Temp[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 | |
---|
| 116 | CoastCrit = Temp.max() |
---|
| 117 | print ("Maximum number of neighbors : "+str(CoastCrit) ) |
---|
| 118 | |
---|
[3901] | 119 | Temp = nemo.lbc ( Temp, nperio=nperio, cd_type='T' ) |
---|
[3723] | 120 | |
---|
| 121 | OceCoastal = np.where (OceMask == 1, True, False) * np.where (Temp < CoastCrit, True, False) |
---|
| 122 | OceCoastal = np.where (OceCoastal, 1, 0) |
---|
| 123 | |
---|
| 124 | varOceCoastal = GridFile.createVariable ("OceCoastal", "i4", ( "y_grid_T", "x_grid_T") ) |
---|
| 125 | varOceCoastal.cell_measures = "area: area_grid_T" |
---|
| 126 | varOceCoastal.coordinates = "nav_lat_grid_T nav_lon_grid_T" |
---|
| 127 | |
---|
| 128 | varOceCoastal[:,:] = OceCoastal[:,:] |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | GridFile.sync() |
---|
| 132 | GridFile.close() |
---|