Changeset 2605 in lmdz_wrf for trunk/tools
- Timestamp:
- Jun 16, 2019, 10:37:06 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/geometry_tools.py
r2597 r2605 952 952 N = polygon.shape[0] 953 953 954 ipt = None955 ept = None956 957 dx = np.zeros((2), dtype=np.float)958 dy = np.zeros((2), dtype=np.float)959 icut = np.zeros((2), dtype=int)960 ecut = np.zeros((2), dtype=int)961 ipt = np.zeros((2,2), dtype=np.float)962 ept = np.zeros((2,2), dtype=np.float)963 964 954 if yval1 > yval2: 965 955 print errormsg … … 971 961 yvals = [yval1, yval2] 972 962 973 for ic in range(2): 974 yval = yvals[ic] 975 if type(polygon) == type(gen.mamat): 963 ipt = None 964 ept = None 965 966 cuts = {} 967 if type(polygon) == type(gen.mamat) and type(polygon.mask) != \ 968 type(gen.mamat.mask[1]): 969 for ic in range(2): 970 yval = yvals[ic] 971 # There might be more than 1 cut ... 972 icut = [] 973 ecut = [] 974 ipt = [] 975 ept = [] 976 Ncuts = 0 976 977 # Assuming clockwise polygons 977 978 for ip in range(N-1): … … 980 981 if eep == N: eep = 0 981 982 982 if polygon[ip,0] <= yval and polygon[eep,0] >= yval:983 icut [ic] = ip984 dx [ic]= polygon[eep,1] - polygon[ip,1]985 dy [ic]= polygon[eep,0] - polygon[ip,0]983 if val_consec_between(polygon[ip,0], polygon[eep,0], yval): 984 icut.append(ip) 985 dx = polygon[eep,1] - polygon[ip,1] 986 dy = polygon[eep,0] - polygon[ip,0] 986 987 dd = yval - polygon[ip,0] 987 ipt [ic,:] = [yval, polygon[ip,1]+dx[ic]*dd/dy[ic]]988 989 if polygon[ip,0] >= yval and polygon[eep,0] <= yval:990 ecut [ic] = ip991 dx [ic]= polygon[eep,1] - polygon[ip,1]992 dy [ic]= polygon[eep,0] - polygon[ip,0]988 ipt.append([yval, polygon[ip,1]+dx*dd/dy]) 989 990 if val_consec_between(polygon[eep,0], polygon[ip,0], yval): 991 ecut.append(ip) 992 dx = polygon[eep,1] - polygon[ip,1] 993 dy = polygon[eep,0] - polygon[ip,0] 993 994 dd = yval - polygon[ip,0] 994 ept[ic,:] = [yval, polygon[ip,1]+dx[ic]*dd/dy[ic]] 995 else: 995 ept.append([yval, polygon[ip,1]+dx*dd/dy]) 996 Ncuts = Ncuts + 1 997 998 # Looking for repeated 999 newicut = icut + [] 1000 newecut = ecut + [] 1001 newipt = ipt + [] 1002 newept = ept + [] 1003 newNcuts = Ncuts 1004 for icp in range(newNcuts-1): 1005 for ic2 in range(icp+1,newNcuts): 1006 if newipt[icp] == newipt[ic2]: 1007 Ncuts = Ncuts-1 1008 icut.pop(ic2) 1009 ecut.pop(ic2) 1010 ipt.pop(ic2) 1011 ept.pop(ic2) 1012 newNcuts = Ncuts + 0 1013 1014 cuts[ic] = [icut, ecut, ipt, ept, Ncuts] 1015 else: 1016 for ic in range(2): 1017 yval = yvals[ic] 1018 # There might be more than 1 cut ... 1019 icut = [] 1020 ecut = [] 1021 ipt = [] 1022 ept = [] 1023 Ncuts = 0 996 1024 # Assuming clockwise polygons 997 1025 for ip in range(N-1): … … 999 1027 if eep == N: eep = 0 1000 1028 1001 if polygon[ip,0] <= yval and polygon[eep,0] >= yval:1002 icut [ic] = ip1003 dx [ic]= polygon[eep,1] - polygon[ip,1]1004 dy [ic]= polygon[eep,0] - polygon[ip,0]1029 if val_consec_between(polygon[ip,0], polygon[eep,0], yval): 1030 icut.append(ip) 1031 dx = polygon[eep,1] - polygon[ip,1] 1032 dy = polygon[eep,0] - polygon[ip,0] 1005 1033 dd = yval - polygon[ip,0] 1006 ipt [ic,:] = [yval, polygon[ip,1]+dx[ic]*dd/dy[ic]]1007 1008 if polygon[ip,0] >= yval and polygon[eep,0] <= yval:1009 ecut [ic] = ip1010 dx [ic]= polygon[eep,1] - polygon[ip,1]1011 dy [ic]= polygon[eep,0] - polygon[ip,0]1034 ipt.append([yval, polygon[ip,1]+dx*dd/dy]) 1035 1036 if val_consec_between(polygon[eep,0], polygon[ip,0], yval): 1037 ecut.append(ip) 1038 dx = polygon[eep,1] - polygon[ip,1] 1039 dy = polygon[eep,0] - polygon[ip,0] 1012 1040 dd = yval - polygon[ip,0] 1013 ept[ic,:] = [yval, polygon[ip,1]+dx[ic]*dd/dy[ic]] 1014 1015 if ipt is None or ept is None: 1016 print errormsg 1017 print ' ' + fname + ': no cutting for polygon at y=', yval, '!!' 1018 1019 Npts = icut[1] - icut[0] + Nadd + ecut[0] - ecut[1] 1020 cutpolygon = np.zeros((Npts,2), dtype=np.float) 1021 cutpolygon[0,:] = ipt[0,:] 1022 cutpolygon[1:icut[1]-icut[0]+1,:] = polygon[icut[0]+1:icut[1]+1,:] 1023 iip = icut[1]-icut[0] 1024 1025 # cutting lines 1026 Nadd2 = int(Nadd/2) 1027 cutlines = np.zeros((2,Nadd2,2), dtype=np.float) 1028 1029 for ic in range(2): 1030 dx = (ept[ic,1] - ipt[ic,1])/(Nadd2-2) 1031 dy = (ept[ic,0] - ipt[ic,0])/(Nadd2-2) 1032 cutlines[ic,0,:] = ipt[ic,:] 1041 ept.append([yval, polygon[ip,1]+dx*dd/dy]) 1042 Ncuts = Ncuts + 1 1043 # Looking for repeated 1044 newicut = icut + [] 1045 newecut = ecut + [] 1046 newipt = ipt + [] 1047 newept = ept + [] 1048 newNcuts = Ncuts 1049 for icp in range(newNcuts-1): 1050 for ic2 in range(icp+1,newNcuts): 1051 if newipt[icp] == newipt[ic2]: 1052 Ncuts = Ncuts-1 1053 icut.pop(ic2) 1054 ecut.pop(ic2) 1055 ipt.pop(ic2) 1056 ept.pop(ic2) 1057 newNcuts = Ncuts + 0 1058 1059 cuts[ic] = [icut, ecut, ipt, ept, Ncuts] 1060 1061 Naddlines = {} 1062 for icc in range(2): 1063 cutv = cuts[icc] 1064 Ncuts = cutv[4] 1065 # Length of joining lines 1066 Nadds = [] 1067 if Ncuts > 1: 1068 Naddc = (Nadd-Ncuts)/(Ncuts) 1069 if Naddc < 3: 1070 print errormsg 1071 print ' ' + fname + ': too few points for jioning lines !!' 1072 print ' increase Nadd at least to:', Ncuts*3+Ncuts 1073 quit(-1) 1074 for ic in range(Ncuts-1): 1075 Nadds.append(Naddc) 1076 1077 Nadds.append(Nadd-Naddc*(Ncuts-1)) 1078 else: 1079 Nadds.append(Nadd) 1080 1081 # Total points cut polygon 1082 Ntotpts = 0 1083 Ncpts = [] 1084 for ic in range(Ncuts): 1085 dpts = ecut[ic] - icut[ic] + Nadds[ic] - 1 1086 1087 Ntotpts = Ntotpts + dpts 1088 Ncpts.append(ecut[ic] - icut[ic]) 1089 1090 Naddlines[icc] = [Nadds, Ntotpts, Ncpts] 1091 1092 cutv1 = cuts[0] 1093 addv1 = Naddlines[0] 1094 Nadds1 = addv1[0] 1095 Ncuts1 = cutv1[4] 1096 1097 cutv2 = cuts[1] 1098 addv2 = Naddlines[1] 1099 Nadds2 = addv2[0] 1100 Ncuts2 = cutv2[4] 1101 1102 if Ncuts1 != Ncuts2: 1103 print errormsg 1104 print ' ' + fname + ": different number of cuts !!" 1105 print ' yval1:', yval1, 'Ncuts=', Ncuts1 1106 print ' yval2:', yval2, 'Ncuts=', Ncuts2 1107 print ' I am not prepare to deal with it' 1108 quit(-1) 1109 #else: 1110 # print ' ' + fname + ' _______' 1111 # print ' yval1:', yval1, 'Ncuts=', Ncuts1 1112 # print ' yval2:', yval2, 'Ncuts=', Ncuts2 1113 1114 icut1 = cutv1[0] 1115 ecut1 = cutv1[1] 1116 ipt1 = cutv1[2] 1117 ept1 = cutv1[3] 1118 icut2 = cutv2[0] 1119 ecut2 = cutv2[1] 1120 ipt2 = cutv2[2] 1121 ept2 = cutv2[3] 1122 1123 # Looking for pairs of cuts. Grouping for smallest x distance between initial 1124 # points of each cut 1125 cutpolygons = [] 1126 for iic1 in range(Ncuts1): 1127 iip = 0 1128 cutpolygon = [] 1129 ic1 = icut1[iic1] 1130 ec1 = ecut1[iic1] 1131 ip1 = ipt1[iic1] 1132 ep1 = ept1[iic1] 1133 1134 ipx2s = [] 1135 for ip in range(Ncuts2): 1136 ip2 = ipt2[ip] 1137 ipx2s.append(ip2[1]) 1138 dxps = ipx2s - ip1[1] 1139 dxps = np.where(dxps < 0., gen.fillValueF, dxps) 1140 ndxps = np.min(dxps) 1141 iic12 = gen.index_vec(dxps,ndxps) 1142 1143 ic2 = icut2[iic12] 1144 ec2 = ecut2[iic12] 1145 ip2 = ipt2[iic12] 1146 ep2 = ept2[iic12] 1147 1148 #print 'Lluis iic1', iic1, 'ic1', ic1, 'ec1', ec1, 'ipt1', ip1, 'ept1', ep1, 'Nadds1', Nadds1 1149 #print ' iic12', iic12, 'ic2', ic2, 'ec2', ec2, 'ipt2', ip2, 'ept2', ep2, 'Nadds2', Nadds2 1150 1151 cutpolygon.append(ip1) 1152 for ip in range(ic1+1,ic2-1): 1153 cutpolygon.append(polygon[ip,:]) 1154 iip = ic2-ic1 1155 # cutting line 1 1156 Nadd2 = Nadds1[iic1] 1157 cutlines = np.zeros((Nadd2,2), dtype=np.float) 1158 dx = (ep2[1] - ip2[1])/(Nadd2-2) 1159 dy = (ep2[0] - ip2[0])/(Nadd2-2) 1160 cutlines[0,:] = ip2 1033 1161 for ip in range(1,Nadd2-1): 1034 cutlines[ic,ip,:] = ipt[ic,:] + np.array([dy*ip,dx*ip]) 1035 cutlines[ic,Nadd2-1,:] = ept[ic,:] 1036 1037 cutpolygon[iip:iip+Nadd2,:] = cutlines[1,:,:] 1038 iip = iip + Nadd2 1039 cutpolygon[iip:iip+(ecut[0]-ecut[1]),:] = polygon[ecut[1]+1:ecut[0]+1,:] 1040 iip = iip + ecut[0]-ecut[1] 1041 cutpolygon[iip:iip+Nadd2,:] = cutlines[0,::-1,:] 1042 1043 cutpolygon = ma.masked_equal(cutpolygon, gen.fillValueF) 1044 1045 return Npts, cutpolygon 1162 cutlines[ip,:] = ip2 + np.array([dy*ip,dx*ip]) 1163 cutlines[Nadd2-1,:] = ep2 1164 for ip in range(Nadd2): cutpolygon.append(cutlines[ip,:]) 1165 iip = iip + Nadd2 1166 1167 for ip in range(ec2,ec1): 1168 cutpolygon.append(polygon[ip,:]) 1169 iip = iip + ec1-ec2 1170 # cutting line 2 1171 Nadd2 = Nadds2[iic12] 1172 cutlines = np.zeros((Nadd2,2), dtype=np.float) 1173 dx = (ep1[1] - ip1[1])/(Nadd2-2) 1174 dy = (ep1[0] - ip1[0])/(Nadd2-2) 1175 cutlines[0,:] = ip1 1176 for ip in range(1,Nadd2-1): 1177 cutlines[ip,:] = ip1 + np.array([dy*ip,dx*ip]) 1178 cutlines[Nadd2-1,:] = ep1 1179 for ip in range(Nadd2-1,0,-1): 1180 cutpolygon.append(cutlines[ip,:]) 1181 1182 cutpolygon.append(ip1) 1183 1184 cutpolygon.append([gen.fillValueF,gen.fillValueF]) 1185 if len(cutpolygons) == 0: cutpolygons = cutpolygon 1186 else: cutpolygons = cutpolygons + cutpolygon 1187 1188 cutpolygons = np.array(cutpolygons) 1189 cutpolygons = ma.masked_equal(cutpolygons, gen.fillValueF) 1190 1191 Npts = cutpolygons.shape[0] 1192 1193 return Npts, cutpolygons 1046 1194 1047 1195 def cut_between_xpolygon(polygon, xval1, xval2, Nadd=20): … … 2389 2537 buoy = ma.masked_equal(buoy, gen.fillValueF) 2390 2538 2391 buoysecs = ['buoy', 'sign1', 'sign2', 'half k', 'halfy']2539 buoysecs = ['buoy', 'sign1', 'sign2', 'half1', 'half2'] 2392 2540 buoydic = {'buoy': [buoy[0:N2,:],'-','k',1.5], \ 2393 2541 'sign1': [buoy[N2+1:N2+N32+1,:],'-','k',1.5], \ … … 3185 3333 secvals=objdic[secn] 3186 3334 pvals = secvals[0] 3187 plt.fill(pvals[:,1], pvals[:,0], color=secvals[2]) 3335 fillsec = [] 3336 for ip in range(pvals.shape[0]): 3337 if type(pvals[ip,0]) != type(gen.mamat[1]) and type(pvals[ip,1]) != type(gen.mamat[1]): 3338 fillsec.append(pvals[ip,:]) 3339 #plt.fill(pvals[:,1], pvals[:,0], color=secvals[2]) 3340 fillsec = np.array(fillsec) 3341 plt.fill(fillsec[:,1], fillsec[:,0], color=secvals[2]) 3188 3342 3189 3343 return
Note: See TracChangeset
for help on using the changeset viewer.