[3738] | 1 | ### =========================================================================== |
---|
| 2 | ### |
---|
| 3 | ### Fill a coupler restart, ocean side |
---|
| 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 |
---|
[6669] | 17 | ## Author = "$Author$" |
---|
| 18 | ## Date = "$Date$" |
---|
| 19 | ## Revision = "$Revision$" |
---|
| 20 | ## Id = "$Id$" |
---|
| 21 | ## HeadURL = "$HeadURL$" |
---|
| 22 | ''' |
---|
| 23 | All documentation available at https://forge.ipsl.jussieu.fr/igcmg/wiki/IPSLCM6/CPLRESTART |
---|
| 24 | |
---|
| 25 | Extraction : |
---|
| 26 | > svn co http://forge.ipsl.jussieu.fr/igcmg/svn/TOOLS/CPLRESTART CPLRESTART |
---|
| 27 | ''' |
---|
| 28 | |
---|
| 29 | ## SVN information |
---|
| 30 | __SVN__ = ({ |
---|
| 31 | 'Author' : "$Author$", |
---|
| 32 | 'Date' : "$Date$", |
---|
| 33 | 'Revision' : "$Revision$", |
---|
| 34 | 'Id' : "$Id$", |
---|
| 35 | 'HeadURL' : "$HeadURL$", |
---|
| 36 | }) |
---|
[3738] | 37 | ## |
---|
[6669] | 38 | import shutil |
---|
| 39 | import getopt |
---|
| 40 | import sys |
---|
| 41 | import numpy.ma as np |
---|
[3738] | 42 | import netCDF4 |
---|
| 43 | from scipy import ndimage |
---|
| 44 | import nemo |
---|
| 45 | |
---|
| 46 | def MyFill (InputData=None) : |
---|
| 47 | """ |
---|
| 48 | Replace the value of masked 'InputData' cells by the value of the nearest valid data cell |
---|
| 49 | From https://stackoverflow.com/questions/5551286/filling-gaps-in-a-numpy-array |
---|
[6669] | 50 | |
---|
[3741] | 51 | Input: InputData : numpy.ma array of any dimension. |
---|
[3738] | 52 | |
---|
[6669] | 53 | Output: Return a filled array. |
---|
| 54 | """ |
---|
[3738] | 55 | |
---|
| 56 | Invalid = np.where ( InputData[:,:].mask, True, False) |
---|
| 57 | Indices = ndimage.distance_transform_bf(Invalid, metric='euclidean', sampling=None, return_distances=False, |
---|
| 58 | return_indices=True, distances=None ) |
---|
| 59 | FilledData = InputData[tuple(Indices)] |
---|
| 60 | return FilledData |
---|
| 61 | |
---|
| 62 | def usage () : |
---|
[3741] | 63 | __help__ = """%(prog)s usage : |
---|
| 64 | python %(prog)s [-d] -i <sstoce file> [-n <perio>] [-v variables] [-o <output file>] |
---|
[3738] | 65 | -d | --debug : debug |
---|
| 66 | -i <file> | --input=<file> : input file (default: none) |
---|
[3741] | 67 | -o <file> | --output=<file> : output file (default : build a name form input file) |
---|
| 68 | -r | --replace : replace input file by new file with filled variables |
---|
[4289] | 69 | -v <varlist> | --variable=<variables> : list of variable to fill (default: all variable in file) |
---|
[3741] | 70 | -x | --exclude : fills all variable in files, except those given in -v|--variable |
---|
[3738] | 71 | -n <perio> | --perio=<perio> : periodicity type (default: try to guess) |
---|
[3741] | 72 | 1, 4, 6 : Cyclic on i dimension (generaly longitudes) |
---|
| 73 | 2 : Obsolete (was symmetric condition at southern boundary ?) |
---|
| 74 | 3, 4 : North fold T-point pivot (legacy ORCA2) |
---|
| 75 | 5, 6 : North fold F-point pivot (ORCA1, ORCA025, ORCA2 with new grid for paleo) |
---|
[3738] | 76 | If <perio> is not specified, %(prog)s will try to guess it from the grid dimensions |
---|
| 77 | example : |
---|
| 78 | python %(prog)s -n 4 -i sstoce_ORCA2.3.nc |
---|
| 79 | python %(prog)s -n 4 -i sstoce_ORCA2.3.nc -v O_SSTSST,OIceFrc |
---|
| 80 | """ |
---|
[3741] | 81 | print ( __help__ % { 'prog':sys.argv[0] } ) |
---|
[3738] | 82 | |
---|
| 83 | ## Default input parameters |
---|
| 84 | InFile = None |
---|
| 85 | OuFile = None |
---|
| 86 | replace = False |
---|
| 87 | ListVarName = None |
---|
| 88 | nperio = None |
---|
| 89 | Debug = False |
---|
[6669] | 90 | ListExclude = None |
---|
| 91 | Exclude = False |
---|
[3738] | 92 | |
---|
| 93 | ## Command line options |
---|
| 94 | try: |
---|
[3741] | 95 | myopts, myargs = getopt.getopt ( sys.argv[1:], 'i:o:rv:xp:hd', |
---|
[6669] | 96 | [ 'input=', 'output=', 'replace', 'exclude', 'variable=', 'variables=', 'perio=', 'debug', 'help' ] ) |
---|
[3738] | 97 | except getopt.GetoptError as cmdle : |
---|
| 98 | print ( "Command line error : "+str(cmdle)+"\n" ) |
---|
| 99 | usage () |
---|
| 100 | sys.exit(1) |
---|
| 101 | |
---|
| 102 | for myopt, myval in myopts : |
---|
[6669] | 103 | if myopt in [ '-h', '--help' ] : usage () ; sys.exit (0) |
---|
| 104 | elif myopt in [ '-d', '--debug' ] : Debug = True |
---|
| 105 | elif myopt in [ '-i', '--input' ] : InFile = myval |
---|
[3738] | 106 | elif myopt in [ '-o', '--output' ] : |
---|
| 107 | if replace : |
---|
| 108 | print ("Error : you can not specify both -r|--replace and -o|--output" ) |
---|
| 109 | usage () |
---|
| 110 | sys.exit(1) |
---|
| 111 | else : |
---|
[6669] | 112 | OuFile = myval |
---|
[4289] | 113 | if Debug : print ("Out file set to " + OuFile) |
---|
[3738] | 114 | elif myopt in [ '-r', '--replace' ] : |
---|
[6669] | 115 | if OuFile is not None : |
---|
[3738] | 116 | print ("Error : you can not specify both -r|--replace and -o|--output" ) |
---|
| 117 | usage () |
---|
| 118 | sys.exit(1) |
---|
| 119 | else : |
---|
| 120 | if Debug : print ("Out file set to input file") |
---|
[6669] | 121 | OuFile = InFile |
---|
| 122 | elif myopt in [ '-p', '--perio' ] : nperio = int(myval) |
---|
[3741] | 123 | elif myopt in [ '-v', '--variable', '--variables' ] : |
---|
[6669] | 124 | if Exclude : ListExclude = myval.split(',') |
---|
[3741] | 125 | else : ListVarName = myval.split(',') |
---|
[3738] | 126 | elif myopt in [ '-x', '--exclude' ] : |
---|
| 127 | Exclude = True |
---|
[6669] | 128 | if ListVarName is not None : |
---|
[3738] | 129 | ListExclude = ListVarName |
---|
| 130 | ListVarName = None |
---|
[4289] | 131 | |
---|
[6669] | 132 | if OuFile is None : |
---|
[4289] | 133 | print ( 'Definition OuFile' ) |
---|
[3738] | 134 | OuFile = InFile.replace ( ".nc", "_filled.nc" ) |
---|
[4289] | 135 | print ("Creates output file name: " + OuFile) |
---|
[3738] | 136 | |
---|
| 137 | # Copy the input file if needed |
---|
| 138 | if OuFile != InFile : shutil.copyfile ( InFile, OuFile ) |
---|
| 139 | |
---|
| 140 | print ("Output file: " + OuFile ) |
---|
| 141 | |
---|
| 142 | # Open file |
---|
| 143 | OuFile = netCDF4.Dataset ( OuFile , "r+" ) |
---|
| 144 | |
---|
| 145 | # Try to guess periodicity type |
---|
[6669] | 146 | jpoi = OuFile.dimensions["x"].size |
---|
| 147 | jpoj = OuFile.dimensions["y"].size |
---|
| 148 | nperio = nemo.__guess_nperio__ (jpoj, jpoi, nperio=nperio, out='nperio') |
---|
| 149 | |
---|
| 150 | if nperio is None : |
---|
[3741] | 151 | print ("%(prog)s couldn't guess the periodicity type of your file") |
---|
| 152 | print ("Please specify -p|--perio") |
---|
[3738] | 153 | usage () |
---|
| 154 | sys.exit(1) |
---|
[6669] | 155 | |
---|
[3738] | 156 | # Get variables from file is need |
---|
[6669] | 157 | if ListVarName is None : ListVarName = list ( OuFile.variables.keys() ) |
---|
[3738] | 158 | |
---|
| 159 | # Exclude some var if needed |
---|
[6669] | 160 | for Var in ['time_centered', 'time_counter', 'time_centered_bounds', 'time_counter_bounds' ] : |
---|
| 161 | if Var in ListVarName : ListVarName.remove(Var) |
---|
| 162 | if Exclude : |
---|
| 163 | for Var in ListExclude : |
---|
[3738] | 164 | if Var in ListVarName : ListVarName.remove(Var) |
---|
| 165 | |
---|
| 166 | # Loop on variables |
---|
| 167 | for VarName in ListVarName : |
---|
| 168 | Var = OuFile.variables[VarName] |
---|
[3741] | 169 | if 'mask' in dir(Var[...]) : |
---|
[3738] | 170 | print ( "Working on " + VarName ) |
---|
[3741] | 171 | NewVar = MyFill ( InputData = Var[...] ) |
---|
[3738] | 172 | NewVar = nemo.lbc (NewVar, nperio=nperio, cd_type='T', psgn=1.0) |
---|
| 173 | OuFile.variables[VarName][:,:] = NewVar[:,:] |
---|
| 174 | else : |
---|
| 175 | print ( VarName + " is not masked" ) |
---|
[6669] | 176 | |
---|
| 177 | # Close file : writes update variables. |
---|
[3741] | 178 | OuFile.close() |
---|
[3738] | 179 | |
---|
| 180 | ## =========================================================================== |
---|
| 181 | ## |
---|
| 182 | ## That's all folk's !!! |
---|
| 183 | ## |
---|
| 184 | ## =========================================================================== |
---|