Changeset 2606 in lmdz_wrf for trunk/tools
- Timestamp:
- Jun 16, 2019, 11:43:31 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/geometry_tools.py
r2605 r2606 1204 1204 N = polygon.shape[0] 1205 1205 1206 if xval1 > xval2: 1207 print errormsg 1208 print ' ' + fname + ': wrong between cut values !!' 1209 print ' it is expected xval1 < xval2' 1210 print ' values provided xval1: (', xval1, ')> xval2 (', xval2, ')' 1211 quit(-1) 1212 1213 xvals = [xval1, xval2] 1214 1215 ipt = None 1216 ept = None 1217 1218 cuts = {} 1219 if type(polygon) == type(gen.mamat) and type(polygon.mask) != \ 1220 type(gen.mamat.mask[1]): 1221 for ic in range(2): 1222 xval = xvals[ic] 1223 # There might be more than 1 cut ... 1224 icut = [] 1225 ecut = [] 1226 ipt = [] 1227 ept = [] 1228 Ncuts = 0 1229 # Assuming clockwise polygons 1230 for ip in range(N-1): 1231 if not polygon.mask[ip,0]: 1232 eep = ip + 1 1233 if eep == N: eep = 0 1234 1235 if val_consec_between(polygon[ip,1], polygon[eep,1], xval): 1236 icut.append(ip) 1237 dx = polygon[eep,1] - polygon[ip,1] 1238 dy = polygon[eep,0] - polygon[ip,0] 1239 dd = xval - polygon[ip,1] 1240 ipt.append([polygon[ip,0]+dy*dd/dx, xval]) 1241 1242 if val_consec_between(polygon[eep,1], polygon[ip,1], xval): 1243 ecut.append(ip) 1244 dx = polygon[eep,1] - polygon[ip,1] 1245 dy = polygon[eep,0] - polygon[ip,0] 1246 dd = xval - polygon[ip,1] 1247 ept.append([polygon[ip,0]+dy*dd/dx, xval]) 1248 Ncuts = Ncuts + 1 1249 1250 # Looking for repeated 1251 newicut = icut + [] 1252 newecut = ecut + [] 1253 newipt = ipt + [] 1254 newept = ept + [] 1255 newNcuts = Ncuts 1256 for icp in range(newNcuts-1): 1257 for ic2 in range(icp+1,newNcuts): 1258 if newipt[icp] == newipt[ic2]: 1259 Ncuts = Ncuts-1 1260 icut.pop(ic2) 1261 ecut.pop(ic2) 1262 ipt.pop(ic2) 1263 ept.pop(ic2) 1264 newNcuts = Ncuts + 0 1265 1266 cuts[ic] = [icut, ecut, ipt, ept, Ncuts] 1267 else: 1268 for ic in range(2): 1269 xval = xvals[ic] 1270 # There might be more than 1 cut ... 1271 icut = [] 1272 ecut = [] 1273 ipt = [] 1274 ept = [] 1275 Ncuts = 0 1276 # Assuming clockwise polygons 1277 for ip in range(N-1): 1278 eep = ip + 1 1279 if eep == N: eep = 0 1280 1281 if val_consec_between(polygon[ip,1], polygon[eep,1], xval): 1282 icut.append(ip) 1283 dx = polygon[eep,1] - polygon[ip,1] 1284 dy = polygon[eep,0] - polygon[ip,0] 1285 dd = xval - polygon[ip,1] 1286 ipt.append([polygon[ip,0]+dy*dd/dx, xval]) 1287 1288 if val_consec_between(polygon[eep,1], polygon[ip,1], xval): 1289 ecut.append(ip) 1290 dx = polygon[eep,1] - polygon[ip,1] 1291 dy = polygon[eep,0] - polygon[ip,0] 1292 dd = xval - polygon[ip,1] 1293 ept.append([polygon[ip,0]+dy*dd/dx, xval]) 1294 Ncuts = Ncuts + 1 1295 # Looking for repeated 1296 newicut = icut + [] 1297 newecut = ecut + [] 1298 newipt = ipt + [] 1299 newept = ept + [] 1300 newNcuts = Ncuts 1301 for icp in range(newNcuts-1): 1302 for ic2 in range(icp+1,newNcuts): 1303 if newipt[icp] == newipt[ic2]: 1304 Ncuts = Ncuts-1 1305 icut.pop(ic2) 1306 ecut.pop(ic2) 1307 ipt.pop(ic2) 1308 ept.pop(ic2) 1309 newNcuts = Ncuts + 0 1310 1311 cuts[ic] = [icut, ecut, ipt, ept, Ncuts] 1312 1313 for iic in range(0): 1314 cutvs = cuts[iic] 1315 icut = cutvs[0] 1316 ecut = cutvs[1] 1317 ipt = cutvs[2] 1318 ept = cutvs[3] 1319 Ncuts = cutvs[4] 1320 if Ncuts > 1: 1321 # Re-shifting cuts by closest heigth. 1322 yis = [] 1323 yes = [] 1324 for ic in range(Ncuts): 1325 yp = ipt[ic] 1326 yis.append(yp[0]) 1327 yp = ept[ic] 1328 yes.append(yp[0]) 1329 ys = yis + yes 1330 ys.sort() 1331 newicut = icut + [] 1332 newecut = ecut + [] 1333 newipt = ipt + [] 1334 newept = ept + [] 1335 icut = [] 1336 ecut = [] 1337 ipt = [] 1338 ept = [] 1339 for yv in ys: 1340 ic = yis.count(yv) 1341 if ic != 0: 1342 icc = yis.index(yv) 1343 if len(icut) > len(ecut): 1344 ecut.append(newicut[icc]) 1345 ept.append(newipt[icc]) 1346 else: 1347 icut.append(newicut[icc]) 1348 ipt.append(newipt[icc]) 1349 else: 1350 icc = yes.index(yv) 1351 if len(icut) > len(ecut): 1352 ecut.append(newecut[icc]) 1353 ept.append(newept[icc]) 1354 else: 1355 icut.append(newecut[icc]) 1356 ipt.append(newept[icc]) 1357 1358 cuts[iic] = [icut, ecut, ipt, ept, Ncuts] 1359 1360 Naddlines = {} 1361 for icc in range(2): 1362 cutv = cuts[icc] 1363 Ncuts = cutv[4] 1364 # Length of joining lines 1365 Nadds = [] 1366 if Ncuts > 1: 1367 Naddc = (Nadd-Ncuts)/(Ncuts) 1368 if Naddc < 3: 1369 print errormsg 1370 print ' ' + fname + ': too few points for jioning lines !!' 1371 print ' increase Nadd at least to:', Ncuts*3+Ncuts 1372 quit(-1) 1373 for ic in range(Ncuts-1): 1374 Nadds.append(Naddc) 1375 1376 Nadds.append(Nadd-Naddc*(Ncuts-1)) 1377 else: 1378 Nadds.append(Nadd) 1379 1380 Naddlines[icc] = Nadds 1381 1382 # sides 1383 sides = {} 1384 for iic in range(2): 1385 cutvs = cuts[iic] 1386 icut = cutvs[0] 1387 ecut = cutvs[1] 1388 ipt = cutvs[2] 1389 ept = cutvs[3] 1390 Ncuts = cutvs[4] 1391 Nadds = Naddlines[iic] 1392 cutpolygon = [] 1393 # left side 1394 if iic == 0: 1395 for ic in range(Ncuts-1): 1396 cutpolygon.append(ipt[ic]) 1397 1398 if ic == 0: 1399 for ip in range(icut[ic],N-1): cutpolygon.append(polygon[ip,:]) 1400 for ip in range(icut[ic],ecut[ic]): cutpolygon.append(polygon[ip,:]) 1401 # line 1402 print 'Lluis ipt+1', ipt[ic+1], 'ept', ept[ic], 'Nadds:', Nadds[ic] 1403 dx = (ipt[ic+1][1] - ept[ic][1])/(Nadds[ic]-1) 1404 dy = (ipt[ic+1][0] - ept[ic][0])/(Nadds[ic]-1) 1405 for ip in range(1,Nadds[ic]-1): 1406 cutpolygon.append([ept[ic][0]+dy*ip, ept[ic][0]+dy*ip]) 1407 for ip in range(icut[ic],ecut[ic]): cutpolygon.append(polygon[ip,:]) 1408 # right side 1409 else: 1410 for ic in range(Ncuts-1): 1411 cutpolygon.append(ipt[ic]) 1412 1413 for ip in range(icut[ic],ecut[ic]): cutpolygon.append(polygon[ip,:]) 1414 # line 1415 dx = (ipt[ic+1][1] - ept[ic][1])/(Nadds[ic]-1) 1416 dy = (ipt[ic+1][0] - ept[ic][0])/(Nadds[ic]-1) 1417 for ip in range(1,Nadds[ic]-1): 1418 cutpolygon.append([ept[ic,0]+dy*ip, ept[ic,0]+dy*ip]) 1419 for ip in range(icut[ic],ecut[ic]): cutpolygon.append(polygon[ip,:]) 1420 for ip in range(ecut[ic],N-1): cutpolygon.append(polygon[ip,:]) 1421 sides[iic] = cutpolygon 1422 1423 # joining sides by e1[Ncuts1-1] --> i2[0]; e2[Ncuts2-1] --> i1[0] 1424 cutv1 = cuts[0] 1425 Ncuts1 = cutv1[4] 1426 ec1 = cutv1[2][Ncuts1-1] 1427 ic1 = cutv1[1][0] 1428 ept1 = cutv1[3][Ncuts1-1] 1429 ipt1 = cutv1[4][0] 1430 1431 cutv2 = cuts[0] 1432 Ncuts2 = cutv2[4] 1433 ec2 = cutv2[2][Ncuts1-1] 1434 ic2 = cutv2[1][0] 1435 ept2 = cutv2[3][Ncuts1-1] 1436 ipt2 = cutv2[4][0] 1437 1438 finalcutpolygon = sides[0] 1439 for ip in range(ec1,ic2): finalcutpolygon.append(polygon[ip,:]) 1440 finalcutpolygon = finalcutpolygon + sides[1] 1441 for ip in range(ec2,ic1): finalcutpolygon.append(polygon[ip,:]) 1442 1443 1444 finalcutpolygon = np.array(finalcutpolygon) 1445 finalcutpolygon = ma.masked_equal(finalcutpolygon, gen.fillValueF) 1446 1447 Npts = finalcutpolygon.shape[0] 1448 1449 return Npts, finalcutpolygon 1450 1451 def cut_between_xpolygon_old(polygon, xval1, xval2, Nadd=20): 1452 """ Function to cut a polygon between 2 given value of the x-axis 1453 polygon: polygon to cut 1454 xval1: first value to use to cut the polygon 1455 xval2: first value to use to cut the polygon 1456 Nadd: additional points to add to draw the line (20, default) 1457 """ 1458 fname = 'cut_betwen_xpolygon' 1459 1460 N = polygon.shape[0] 1461 1206 1462 ipt = None 1207 1463 ept = None
Note: See TracChangeset
for help on using the changeset viewer.