Changeset 1232 in lmdz_wrf for trunk/tools
- Timestamp:
- Oct 26, 2016, 12:04:14 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/generic_tools.py
r1230 r1232 9825 9825 return timestep 9826 9826 9827 def operations(opN,matA,matB=None):9827 def matoperations(opN,matA,matB=None): 9828 9828 """ Function to perform different operations to a pair of matrices of values 9829 9829 opN: name of the operation 9830 9830 'add': adding matB to matA (matA + matB) 9831 'addc',[modval1]: add [modval1] 9831 9832 'centerderiv',[N],[ord],[dim]: un-scaled center [N]-derivative of order [ord] along dimension [dim] of matA 9832 9833 'div': dividing by matB (matA / matB) 9834 'divc',[modval1]: divide by [modval1] 9833 9835 'forwrdderiv',[N],[ord],[dim]: un-scaled forward [N]-derivative of order [ord] along dimension [dim] of matA 9834 9836 'inv': inverting matA (1/matA) 9837 'lowthres',[modval1],[modval2]: if [val] < [modval1]; val = [modval2] 9838 'lowthres@oper',[modval1],[oper],[modval2]: if [val] < [modval1]; val = [oper] (operation as [modval2]) 9835 9839 'mul': multiplying ny matB (matA * matB) 9840 'mulc',[modval1]: multiply by [modval1] 9836 9841 'pot': powering with matB (matA ** matB) 9842 'potc',[modval1]: [val] ** [modval1] 9837 9843 'sub': substracting matB to matA (matA - matB) 9844 'subc',[modval1]: remove [modval1] 9845 'upthres',[modval1],[modval2]: if [val] > [modval1]; val = [modval2] 9846 'upthres@oper',[modval1],[oper],[modval2]: if [val] > [modval1]; val = [oper] (operation with [modval2]) 9838 9847 matA: initial values 9839 9848 matB: values to operate with 9840 >>> operations('centerderiv,1,8,0',np.arange(81.).reshape(9,9))9849 >>> matoperations('centerderiv,1,8,0',np.arange(81.).reshape(9,9)) 9841 9850 [[ 0. 0. 0. 0. 0. 0. 0. 0. 0.] 9842 9851 [ 0. 0. 0. 0. 0. 0. 0. 0. 0.] … … 9847 9856 [ 0. 0. 0. 0. 0. 0. 0. 0. 0.] 9848 9857 [ 0. 0. 0. 0. 0. 0. 0. 0. 0.] 9849 [ 0. 0. 0. 0. 0. 0. 0. 0. 0.]] 9850 >>> operations('forwrdderiv,1,6,0',np.arange(81.).reshape(9,9)) 9851 [[ 9. 9. 9. 9. 9. 9. 9. 9. 9.] 9852 [ 9. 9. 9. 9. 9. 9. 9. 9. 9.] 9853 [ 9. 9. 9. 9. 9. 9. 9. 9. 9.] 9854 [ 0. 0. 0. 0. 0. 0. 0. 0. 0.] 9855 [ 0. 0. 0. 0. 0. 0. 0. 0. 0.] 9856 [ 0. 0. 0. 0. 0. 0. 0. 0. 0.] 9857 [ 0. 0. 0. 0. 0. 0. 0. 0. 0.] 9858 [ 0. 0. 0. 0. 0. 0. 0. 0. 0.] 9859 [ 0. 0. 0. 0. 0. 0. 0. 0. 0.]] 9860 >>> operations('forwrdderiv,2,2,0',np.arange(81.).reshape(9,9)) 9858 [ 0. 0. 0. 0. 0. 0. 0. 0. 0.]], '1-center_deriv_ord(8)[0]' 9859 >>> matoperations('forwrdderiv,2,2,0',np.arange(81.).reshape(9,9)) 9861 9860 [[ 0. 0. 0. 0. 0. 0. 0. 0. 0.] 9862 9861 [ 0. 0. 0. 0. 0. 0. 0. 0. 0.] … … 9867 9866 [ 0. 0. 0. 0. 0. 0. 0. 0. 0.] 9868 9867 [ 0. 0. 0. 0. 0. 0. 0. 0. 0.] 9869 [ 0. 0. 0. 0. 0. 0. 0. 0. 0.]] 9870 """ 9871 fname = 'operations' 9868 [ 0. 0. 0. 0. 0. 0. 0. 0. 0.]], '2-forward_deriv_ord(2)[0]' 9869 >>> matoperations('upthres,4,3',np.arange(81).reshape(9,9)) 9870 [[0 1 2 3 4 3 3 3 3] 9871 [3 3 3 3 3 3 3 3 3] 9872 [3 3 3 3 3 3 3 3 3] 9873 [3 3 3 3 3 3 3 3 3] 9874 [3 3 3 3 3 3 3 3 3] 9875 [3 3 3 3 3 3 3 3 3] 9876 [3 3 3 3 3 3 3 3 3] 9877 [3 3 3 3 3 3 3 3 3] 9878 [3 3 3 3 3 3 3 3 3]], '>4 [=3]' 9879 >>> matoperations('lowthres@oper,37,divc,7',np.arange(81).reshape(9,9)) 9880 [[ 0 0 0 0 0 0 0 1 1] 9881 [ 1 1 1 1 1 2 2 2 2] 9882 [ 2 2 2 3 3 3 3 3 3] 9883 [ 3 4 4 4 4 4 4 4 5] 9884 [ 5 37 38 39 40 41 42 43 44] 9885 [45 46 47 48 49 50 51 52 53] 9886 [54 55 56 57 58 59 60 61 62] 9887 [63 64 65 66 67 68 69 70 71] 9888 [72 73 74 75 76 77 78 79 80]], '<37 [=/7]' 9889 """ 9890 fname = 'matoperations' 9872 9891 newmat = None 9873 9892 … … 9948 9967 9949 9968 # Dictionary with the description of the operations 9950 Lopn = {'add': '+', 'div': '/', 'inv': '^(-1)', 'mul': '*', 'pot': '^', \ 9951 'sub': '-'} 9952 9953 opavail = ['add', 'centerderiv', 'div', 'forwrdderiv', 'inv', 'mul', 'pot', 'sub'] 9954 9955 if opN[0:11] == 'centerderiv': 9969 Lopn = {'add': '+', 'addc': '+', 'div': '/', 'divc': '/', 'inv': '^(-1)', \ 9970 'lowthres': '<', 'mul': '*', 'mulc': '*', 'pot': '^', 'potc': '^', \ 9971 'sub': '-', 'subc': '-', 'upthres': '<'} 9972 Lopnc = {'addc': '+', 'divc': '/', 'inv': '^{-1)', 'mulc': '*', 'potc': '^', \ 9973 'subc': '-'} 9974 9975 opavail = ['add', 'addc', 'centerderiv', 'div', 'divc', 'forwrdderiv', 'inv', \ 9976 'lowthres', 'lowthres@oper', 'mul', 'mulc', 'pot', 'potc', 'sub', 'subc', \ 9977 'upthres', 'upthres@oper'] 9978 opavailc = ['addc', 'divc', 'inv', 'mulc', 'potc', 'subc'] 9979 9980 valtype = matA.dtype 9981 9982 if opN[0:4] == 'addc': 9983 opn = 'addc' 9984 value1 = retype(opN.split(',')[1], valtype) 9985 Lopn['addc'] = '+' + str(value1) 9986 elif opN[0:11] == 'centerderiv': 9956 9987 opn = 'centerderiv' 9957 9988 Nderiv = int(opN.split(',')[1]) … … 9978 10009 quit(-1) 9979 10010 derivcoeff = centerderiv[Nderiv-1,order-1,:] 9980 10011 elif opN[0:4] == 'divc': 10012 opn = 'divc' 10013 value1 = retype(opN.split(',')[1], valtype) 10014 Lopn['divc'] = '/' + str(value1) 9981 10015 elif opN[0:11] == 'forwrdderiv': 9982 10016 opn = 'forwrdderiv' … … 10004 10038 quit(-1) 10005 10039 derivcoeff = forwrdderiv[Nderiv-1,order-1,:] 10040 elif opN[0:8] == 'lowthres' and opN.find('@') == -1: 10041 opn = 'lowthres' 10042 value1 = retype(opN.split(',')[1], valtype) 10043 value2 = retype(opN.split(',')[2], valtype) 10044 Lopn['lowthres'] = '<' + str(value1) + ' [=' + str(value2) +']' 10045 elif opN[0:13] == 'lowthres@oper': 10046 opn = 'lowthres@oper' 10047 value1 = retype(opN.split(',')[1], valtype) 10048 value2 = opN.split(',')[2] 10049 value3 = retype(opN.split(',')[3], valtype) 10050 if not Lopnc.has_key(value2): 10051 print errormsg 10052 print ' ' + fname + ": second operation '" + value2 + "' not ready !!" 10053 print ' available ones:', opavailc 10054 quit(-1) 10055 Lopn['lowthres@oper'] = '<' + str(value1) + ' [=' + Lopnc[value2] + \ 10056 str(value3) + ']' 10057 elif opN[0:4] == 'mulc': 10058 opn = 'mulc' 10059 value1 = retype(opN.split(',')[1], valtype) 10060 Lopn['mulc'] = '*' + str(value1) 10061 elif opN[0:4] == 'potc': 10062 opn = 'potc' 10063 value1 = retype(opN.split(',')[1], valtype) 10064 Lopn['potc'] = '^' + str(value1) 10065 elif opN[0:4] == 'subc': 10066 opn = 'subc' 10067 value1 = retype(opN.split(',')[1], valtype) 10068 Lopn['subc'] = '-' + str(value1) 10069 elif opN[0:7] == 'upthres' and opN.find('@') == -1: 10070 opn = 'upthres' 10071 value1 = retype(opN.split(',')[1], valtype) 10072 value2 = retype(opN.split(',')[2], valtype) 10073 Lopn['upthres'] = '>' + str(value1) + ' [=' + str(value2) +']' 10074 elif opN[0:12] == 'upthres@oper': 10075 opn = 'upthres@oper' 10076 value1 = retype(opN.split(',')[1], valtype) 10077 value2 = opN.split(',')[2] 10078 value3 = retype(opN.split(',')[3], valtype) 10079 if not Lopnc.has_key(value2): 10080 print errormsg 10081 print ' ' + fname + ": second operation '" + value2 + "' not ready !!" 10082 print ' available ones:', opavailc 10083 quit(-1) 10084 Lopn['upthres@oper'] = '>' + str(value1) + ' [=' + Lopnc[value2] + \ 10085 str(value3) +']' 10006 10086 else: 10007 10087 opn = opN 10008 10009 valtype = mat.dtype 10010 10088 10089 # Carrying on the operation 10011 10090 if opn == 'add': 10012 10091 newmat = matA + matB 10092 elif opn == 'addc': 10093 newmat = matA + value1 10013 10094 elif opn == 'centerderiv': 10014 10095 sdim = matAshape[dim] … … 10074 10155 elif opn == 'div': 10075 10156 newmat = matA / matB 10157 elif opn == 'divc': 10158 newmat = matA / value1 10076 10159 elif opn == 'forwrdderiv': 10077 10160 sdim = matAshape[dim] … … 10152 10235 elif opn == 'inv': 10153 10236 newmat = retype(1., valtype) / matA 10237 elif opn == 'lowthres': 10238 newmat = np.where(matA < value1, value2, matA) 10239 elif opn == 'lowthres@oper': 10240 if value2 == 'addc': 10241 newmat = np.where(matA < value1, matA + value3, matA) 10242 elif value2 == 'divc': 10243 newmat = np.where(matA < value1, matA / value3, matA) 10244 elif value2 == 'inv': 10245 newmat = np.where(matA < value1, retype(1.,valtype) / matA, matA) 10246 elif value2 == 'mulc': 10247 newmat = np.where(matA < value1, matA * value3, matA) 10248 elif value2 == 'potc': 10249 newmat = np.where(matA < value1, matA ** value3, matA) 10250 elif value2 == 'subc': 10251 newmat = np.where(matA < value1, matA - value3, matA) 10252 else: 10253 print errormsg 10254 print ' ' + fname + ": second operation '" + value2 + "' not ready !!" 10255 print ' available ones:', opavailc 10256 quit(-1) 10154 10257 elif opn == 'mul': 10155 10258 newmat = matA * matB 10259 elif opn == 'mulc': 10260 newmat = matA * value1 10156 10261 elif opn == 'pot': 10157 10262 newmat = matA ** matB 10263 elif opn == 'potc': 10264 newmat = matA ** value1 10158 10265 elif opn == 'sub': 10159 10266 newmat = matA - matB 10267 elif opn == 'subc': 10268 newmat = matA - value1 10269 elif opn == 'upthres': 10270 newmat = np.where(matA > value1, value2, matA) 10271 elif opn == 'upthres@oper': 10272 if value2 == 'addc': 10273 newmat = np.where(matA > value1, matA + value3, matA) 10274 elif value2 == 'divc': 10275 newmat = np.where(matA > value1, matA / value3, matA) 10276 elif value2 == 'inv': 10277 newmat = np.where(matA > value1, retype(1.,valtype) / matA, matA) 10278 elif value2 == 'mulc': 10279 newmat = np.where(matA > value1, matA * value3, matA) 10280 elif value2 == 'potc': 10281 newmat = np.where(matA > value1, matA ** value3, matA) 10282 elif value2 == 'subc': 10283 newmat = np.where(matA > value1, matA - value3, matA) 10284 else: 10285 print errormsg 10286 print ' ' + fname + ": second operation '" + value2 + "' not ready !!" 10287 print ' available ones:', opavailc 10288 quit(-1) 10160 10289 else: 10161 10290 print ' ' + fname + ": operation '" + opn + "' does not exist !!" … … 10163 10292 quit(-1) 10164 10293 10165 return newmat 10294 return newmat, Lopn[opn] 10295 print matoperations('centerderiv,1,8,0',np.arange(81.).reshape(9,9)) 10296 print matoperations('forwrdderiv,2,2,0',np.arange(81.).reshape(9,9)) 10297 print matoperations('upthres,4,3',np.arange(81).reshape(9,9)) 10298 print matoperations('lowthres@oper,37,divc,7',np.arange(81).reshape(9,9)) 10166 10299 10167 10300 #quit()
Note: See TracChangeset
for help on using the changeset viewer.