source: TOOLS/MOSAIX/ComputeNemoCoast.py

Last change on this file was 6190, checked in by omamce, 22 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 Id
File size: 5.2 KB
Line 
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##
35import netCDF4
36import nemo
37import numpy as np
38import getopt, sys
39
40def usage () :
41    texte = """Compute a mask for coastal point of NEMO domain
42
43    %(prog)s usage :
44python %(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
53example :
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
59nperio      = None
60GridFile    = None
61Debug       = False
62
63## Command line options
64try:
65    myopts, myargs = getopt.getopt ( sys.argv[1:], 'i:n:h', [ 'input=', 'nperio=', 'debug=', 'help' ] )
66except getopt.GetoptError as cmdle :
67    print ( "Command line error : "+str(cmdle)+"\n" )
68    usage ()
69    sys.exit(1)
70
71for 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##
77if GridFile == None :
78    print ("Input grid file not specified\n")
79    usage ( )
80    sys.exit(-1)
81
82print ("Input file : " + GridFile)   
83
84## Open grid file
85GridFile = netCDF4.Dataset ( GridFile, "r+" )
86
87if 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####   
101if nperio == None :
102    print ("Periodicity not specified and not found\n")
103    usage ( )
104    sys.exit(-1)
105
106###########################################################
107   
108print ("Periodicity : "+ str(nperio) )
109
110if nperio in  [1, 4, 6] : print ("Cyclic on i dimension (generaly longitudes)")
111if nperio in  [2,     ] : print ("Obsolete (was symmetric condition at southern boundary ?)")
112if nperio in  [3, 4   ] : print ("North fold T-point pivot (legacy ORCA2)")
113if nperio in  [5, 6   ] : print ("North fold F-point pivot (ORCA1, ORCA025, ORCA2 with new grid for paleo)")
114
115###   
116OceMask = GridFile.variables["maskutil_T"]
117OceMask = np.array ( OceMask)
118
119OceMask = nemo.lbc ( np.int_(OceMask[:,:]), nperio=nperio, cd_type='T' )
120
121Temp = OceMask*0
122Temp[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
128CoastCrit = Temp.max()
129print ("Maximum number of neighbors : "+str(CoastCrit) )
130
131Temp = nemo.lbc ( Temp,  nperio=nperio, cd_type='T' )
132
133OceCoastal = np.where (OceMask == 1, True, False) * np.where (Temp < CoastCrit, True, False)
134OceCoastal = np.where (OceCoastal, 1, 0)
135
136varOceCoastal = GridFile.createVariable  ("OceCoastal", "i4", ( "y_grid_T", "x_grid_T") )
137varOceCoastal.cell_measures = "area: area_grid_T"
138varOceCoastal.coordinates   = "nav_lat_grid_T nav_lon_grid_T"
139
140varOceCoastal[:,:] = OceCoastal[:,:]
141
142
143GridFile.sync()
144GridFile.close()
Note: See TracBrowser for help on using the repository browser.