Changeset 3741
- Timestamp:
- 04/12/18 12:10:00 (7 years ago)
- Location:
- TOOLS/CPLRESTART
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
TOOLS/CPLRESTART/FillOceRestart.py
r3738 r3741 32 32 From https://stackoverflow.com/questions/5551286/filling-gaps-in-a-numpy-array 33 33 34 Input: InputData : numpy maskedarray of any dimension.34 Input: InputData : numpy.ma array of any dimension. 35 35 36 36 Output: Return a filled array. … … 44 44 45 45 def usage () : 46 texte= """%(prog)s usage :47 python %(prog)s [-d] [-i <orca grid file>] [-n <perio>]46 __help__ = """%(prog)s usage : 47 python %(prog)s [-d] -i <sstoce file> [-n <perio>] [-v variables] [-o <output file>] 48 48 -d | --debug : debug 49 49 -i <file> | --input=<file> : input file (default: none) 50 -v <varlist> | --variable=<variables> : list of variable to fill (defautl: none) 50 -o <file> | --output=<file> : output file (default : build a name form input file) 51 -r | --replace : replace input file by new file with filled variables 52 -v <varlist> | --variable=<variables> : list of variable to fill (defautlt: all variable in file) 53 -x | --exclude : fills all variable in files, except those given in -v|--variable 51 54 -n <perio> | --perio=<perio> : periodicity type (default: try to guess) 52 #1, 4, 6 : Cyclic on i dimension (generaly longitudes)53 #2 : Obsolete (was symmetric condition at southern boundary ?)54 #3, 4 : North fold T-point pivot (legacy ORCA2)55 #5, 6 : North fold F-point pivot (ORCA1, ORCA025, ORCA2 with new grid for paleo)55 1, 4, 6 : Cyclic on i dimension (generaly longitudes) 56 2 : Obsolete (was symmetric condition at southern boundary ?) 57 3, 4 : North fold T-point pivot (legacy ORCA2) 58 5, 6 : North fold F-point pivot (ORCA1, ORCA025, ORCA2 with new grid for paleo) 56 59 If <perio> is not specified, %(prog)s will try to guess it from the grid dimensions 57 60 example : … … 59 62 python %(prog)s -n 4 -i sstoce_ORCA2.3.nc -v O_SSTSST,OIceFrc 60 63 """ 61 print ( texte% { 'prog':sys.argv[0] } )64 print ( __help__ % { 'prog':sys.argv[0] } ) 62 65 63 66 ## Default input parameters … … 72 75 ## Command line options 73 76 try: 74 myopts, myargs = getopt.getopt ( sys.argv[1:], 'i:o:rv:xp:hd', [ 'input=', 'output=', 'replace', 'exclude', 'variable=', 'perio=', 'debug', 'help' ] ) 77 myopts, myargs = getopt.getopt ( sys.argv[1:], 'i:o:rv:xp:hd', 78 [ 'input=', 'output=', 'replace', 'exclude', 'variable=', 'variables=', 'perio=', 'debug', 'help' ] ) 75 79 except getopt.GetoptError as cmdle : 76 80 print ( "Command line error : "+str(cmdle)+"\n" ) … … 97 101 else : 98 102 if Debug : print ("Out file set to input file") 99 OuFile = InFile ; s103 OuFile = InFile ; 100 104 elif myopt in [ '-p', '--perio' ] : nperio = int(myval) ; 101 elif myopt in [ '-v', '--variable' ] : 102 if Exclude : 103 ListExclude = myval.split(',') 104 else : 105 ListVarName = myval.split(',') 105 elif myopt in [ '-v', '--variable', '--variables' ] : 106 if Exclude : ListExclude = myval.split(',') 107 else : ListVarName = myval.split(',') 106 108 elif myopt in [ '-x', '--exclude' ] : 107 109 Exclude = True … … 124 126 # Try to guess periodicity type 125 127 if nperio == None : 126 print ( "Trying to guess nperio parameter")128 print ( "Trying to guess nperio parameter" ) 127 129 jpoi = OuFile.dimensions["x"].size 128 130 jpoj = OuFile.dimensions["y"].size 129 131 print ("Grid dimensions: ("+str(jpoj)+", "+str(jpoi)+")") 130 if (jpoj, jpoi) == (149, 182) : 131 print ("ORCA 2 grid found: nperio may vary for this configuration") 132 if 'ORCA2' in InFile : 133 print ("ORCA 2 grid found from file name: nperio may vary for this configuration") 134 print ("Choosen nperio=4") 135 elif 'ORCA1' in InFile : 136 if 'eORCA1' in InFile : 137 print ("eORCA 1 grid found from file name, nperio=6") 138 else : 139 print ("ORCA 1 grid found from file name, nperio=6") 140 elif (jpoj, jpoi) == (149, 182) : 141 print ("ORCA 2 grid found from dimension: nperio may vary for this configuration") 132 142 print ("Choosen nperio=4") 133 143 nperio = 4 134 if (jpoj, jpoi) == (332, 292) :144 elif (jpoj, jpoi) == (332, 292) : 135 145 nperio = 6 136 print ("ORCA1 grid found , nperio=6" )137 if (jpoj, jpoi) == (332, 362) :146 print ("ORCA1 grid found from dimensions, nperio=6" ) 147 elif (jpoj, jpoi) == (332, 362) : 138 148 nperio = 6 139 print ("eORCA1 grid found , nperio=6" )140 149 print ("eORCA1 grid found from dimensions, nperio=6" ) 150 141 151 if nperio == None : 142 print ("%(prog)s could not guess the periodicity type of your file")143 print ("Please specify - n|--nperio")152 print ("%(prog)s couldn't guess the periodicity type of your file") 153 print ("Please specify -p|--perio") 144 154 usage () 145 155 sys.exit(1) … … 149 159 150 160 # Exclude some var if needed 151 if Exclude : 161 if Exclude : 152 162 for Var in ListExclude : 153 163 if Var in ListVarName : ListVarName.remove(Var) … … 155 165 # Loop on variables 156 166 for VarName in ListVarName : 157 158 167 Var = OuFile.variables[VarName] 159 if 'mask' in dir(Var[ :,...]) :168 if 'mask' in dir(Var[...]) : 160 169 print ( "Working on " + VarName ) 161 NewVar = MyFill ( InputData = Var[ :,:] )170 NewVar = MyFill ( InputData = Var[...] ) 162 171 NewVar = nemo.lbc (NewVar, nperio=nperio, cd_type='T', psgn=1.0) 163 172 OuFile.variables[VarName][:,:] = NewVar[:,:] … … 166 175 167 176 # Close file : writes update variables. 168 #OuFile.close()177 OuFile.close() 169 178 170 179 ## =========================================================================== -
TOOLS/CPLRESTART/nemo.py
r3739 r3741 22 22 23 23 def lbc (ptab, nperio=6, cd_type='T', psgn=1.0) : 24 """25 ptab : Input array24 """ 25 ptab : Input array 26 26 rank 2 at least : patb[...., lat, lon] 27 nperio : Type of periodicity 28 1, 4, 6 : Cyclic on i dimension (generaly longitudes) 29 2 : Obsolete (was symmetric condition at southern boundary ?) 30 3, 4 : North fold T-point pivot (legacy ORCA2) 31 5, 6 : North fold F-point pivot (ORCA1, ORCA025, ORCA2 with new grid for paleo) 32 cd_type : Grid specification : T, U, V or F 33 psgn : For change of sign for vector components 27 nperio : Type of periodicity 28 1, 4, 6 : Cyclic on i dimension (generaly longitudes) 29 2 : Obsolete (was symmetric condition at southern boundary ?) 30 3, 4 : North fold T-point pivot (legacy ORCA2) 31 5, 6 : North fold F-point pivot (ORCA1, ORCA025, ORCA2 with new grid for paleo) 32 cd_type : Grid specification : T, U, V or F 33 psgn : For change of sign for vector components 34 35 See NEMO documentation for further details 36 """ 37 jpi = ptab.shape[-1] 34 38 35 See NEMO documentation for further details 36 """ 37 38 jpi = ptab.shape[-1] 39 40 # 41 #> East-West boundary conditions 42 # -------------------------------- 43 44 if nperio in [1, 4, 6] : 45 # ... cyclic 46 ptab [...,:, 0] = ptab [...,:,-2] 47 ptab [...,:, -1] = ptab [...,:, 1] 48 49 # 50 #> North-South boundary conditions 51 # ---------------------------------- 52 if nperio in [3, 4] : # North fold T-point pivot 53 if cd_type in [ 'T', 'W' ] : # T-, W-point 54 ptab[..., -1, 1: ] = psgn * ptab[..., -3, -1:0:-1 ] 55 ptab[..., -1, 0 ] = psgn * ptab[..., -3, 2 ] 56 ptab[..., -2, jpi//2: ] = psgn * ptab[..., -2, jpi//2:0:-1 ] 57 58 if cd_type == 'U' : 59 ptab[..., -1, 0:-1 ] = psgn * ptab[..., -3, -1:0:-1 ] 60 ptab[..., -1, 0 ] = psgn * ptab[..., -3, 1 ] 61 ptab[..., -1, -1 ] = psgn * ptab[..., -3, -2 ] 62 ptab[..., -2, jpi//2-1:] = psgn * ptab[..., -2, jpi//2+1:0:-1] 39 # 40 #> East-West boundary conditions 41 # -------------------------------- 42 43 if nperio in [1, 4, 6] : 44 # ... cyclic 45 ptab [...,:, 0] = ptab [...,:,-2] 46 ptab [...,:, -1] = ptab [...,:, 1] 47 48 # 49 #> North-South boundary conditions 50 # ---------------------------------- 51 if nperio in [3, 4] : # North fold T-point pivot 52 if cd_type in [ 'T', 'W' ] : # T-, W-point 53 ptab[..., -1, 1: ] = psgn * ptab[..., -3, -1:0:-1 ] 54 ptab[..., -1, 0 ] = psgn * ptab[..., -3, 2 ] 55 ptab[..., -2, jpi//2: ] = psgn * ptab[..., -2, jpi//2:0:-1 ] 56 57 if cd_type == 'U' : 58 ptab[..., -1, 0:-1 ] = psgn * ptab[..., -3, -1:0:-1 ] 59 ptab[..., -1, 0 ] = psgn * ptab[..., -3, 1 ] 60 ptab[..., -1, -1 ] = psgn * ptab[..., -3, -2 ] 61 ptab[..., -2, jpi//2-1:] = psgn * ptab[..., -2, jpi//2+1:0:-1] 62 63 if cd_type == 'V' : 64 ptab[..., -2, 1: ] = psgn * ptab[..., -3, jpi-1:0:-1 ] 65 ptab[..., -1, 1: ] = psgn * ptab[..., -4, -1:0:-1 ] 66 ptab[..., -1, 0 ] = psgn * ptab[..., -4, 2 ] 67 68 if cd_type == 'F' : 69 ptab[..., -2, 0:-1 ] = psgn * ptab[..., -3, -1:0:-1 ] 70 ptab[..., -1, 0:-1 ] = psgn * ptab[..., -4, -1:0:-1 ] 71 ptab[..., -1, 0 ] = psgn * ptab[..., -4, 1 ] 72 ptab[..., -1, -1 ] = psgn * ptab[..., -4, -2 ] 73 74 if nperio in [5, 6] : # North fold F-point pivot 75 if cd_type in ['T', 'W'] : 76 ptab[..., -1, 0: ] = psgn * ptab[..., -2, -1::-1 ] 63 77 64 if cd_type == 'V' : 65 ptab[..., -2, 1: ] = psgn * ptab[..., -3, jpi-1:0:-1 ] 66 ptab[..., -1, 1: ] = psgn * ptab[..., -4, -1:0:-1 ] 67 ptab[..., -1, 0 ] = psgn * ptab[..., -4, 2 ] 68 69 if cd_type == 'F' : 70 ptab[..., -2, 0:-1 ] = psgn * ptab[..., -3, -1:0:-1 ] 71 ptab[..., -1, 0:-1 ] = psgn * ptab[..., -4, -1:0:-1 ] 72 ptab[..., -1, 0 ] = psgn * ptab[..., -4, 1 ] 73 ptab[..., -1, -1 ] = psgn * ptab[..., -4, -2 ] 74 75 if nperio in [5, 6] : # North fold F-point pivot 76 if cd_type in ['T', 'W'] : 77 ptab[..., -1, 0: ] = psgn * ptab[..., -2, -1::-1 ] 78 79 if cd_type == 'U' : 80 ptab[..., -1, 0:-1 ] = psgn * ptab[..., -2, -2::-1 ] 81 ptab[..., -1, -1 ] = psgn * ptab[..., -2, 0 ] 82 83 if cd_type == 'V' : 84 ptab[..., -1, 0: ] = psgn * ptab[..., -3, -1::-1 ] 85 ptab[..., -2, jpi/2: ] = psgn * ptab[..., -2, jpi/2-1::-1 ] 86 87 if cd_type == 'F' : 88 ptab[..., -1, 0:-1 ] = psgn * ptab[..., -3, -2::-1 ] 89 ptab[..., -1, -1 ] = psgn * ptab[..., -3, 0 ] 90 ptab[..., -2, jpi//2:-1] = psgn * ptab[..., -2, jpi//2-2::-1 ] 91 92 return ptab 93 78 if cd_type == 'U' : 79 ptab[..., -1, 0:-1 ] = psgn * ptab[..., -2, -2::-1 ] 80 ptab[..., -1, -1 ] = psgn * ptab[..., -2, 0 ] 81 82 if cd_type == 'V' : 83 ptab[..., -1, 0: ] = psgn * ptab[..., -3, -1::-1 ] 84 ptab[..., -2, jpi/2: ] = psgn * ptab[..., -2, jpi/2-1::-1 ] 85 86 if cd_type == 'F' : 87 ptab[..., -1, 0:-1 ] = psgn * ptab[..., -3, -2::-1 ] 88 ptab[..., -1, -1 ] = psgn * ptab[..., -3, 0 ] 89 ptab[..., -2, jpi//2:-1] = psgn * ptab[..., -2, jpi//2-2::-1 ] 90 91 return ptab 92 94 93 ## =========================================================================== 95 94 ##
Note: See TracChangeset
for help on using the changeset viewer.