|
21 | 21 |
|
22 | 22 | import matplotlib.testing.compare as mpl_compare
|
23 | 23 |
|
| 24 | +import cartopy.crs as ccrs |
| 25 | + |
24 | 26 | import cfplot as cfp
|
25 | 27 | import cf
|
26 | 28 |
|
@@ -1323,6 +1325,141 @@ def test_example_43(self):
|
1323 | 1325 | # https://ncas-cms.github.io/cf-plot/build/unstructured.html#unstructured)
|
1324 | 1326 |
|
1325 | 1327 |
|
| 1328 | +class UnnumberedExamplesTest(unittest.TestCase): |
| 1329 | + """Run all other documentation examples and compare to reference plots.""" |
| 1330 | + |
| 1331 | + data_dir = DATA_DIR |
| 1332 | + save_gen_dir = TEST_GEN_DIR |
| 1333 | + ref_dir = TEST_REF_DIR |
| 1334 | + test_id = None |
| 1335 | + |
| 1336 | + def setUp(self): |
| 1337 | + """Preparations called immediately before each test method.""" |
| 1338 | + # Get a filename fname with the ID of test_example_X component X |
| 1339 | + test_method_name = unittest.TestCase.id(self).split(".")[-1] |
| 1340 | + self.test_id = test_method_name.rsplit("test_example_")[1] |
| 1341 | + fname = f"{self.save_gen_dir}/" f"gen_fig_{self.test_id}.png" |
| 1342 | + cfp.setvars( |
| 1343 | + file=fname, |
| 1344 | + viewer="matplotlib", |
| 1345 | + ) |
| 1346 | + |
| 1347 | + def tearDown(self): |
| 1348 | + """Preparations called immediately after each test method.""" |
| 1349 | + cfp.reset() |
| 1350 | + |
| 1351 | + # @compare_plot_results # SLB TODO add expected plot |
| 1352 | + def test_example_unstructured_lfric_1(self): |
| 1353 | + """Test example for unstructured grids: LFRic example 1.""" |
| 1354 | + f = cf.read("cfplot_data/lfric_initial.nc") |
| 1355 | + |
| 1356 | + # Select the relevant fields for the objects required for the plot, |
| 1357 | + # taking the air potential temperature as a variable to choose to view. |
| 1358 | + pot = f.select_by_identity("air_potential_temperature")[0] |
| 1359 | + lats = f.select_by_identity("latitude")[0] |
| 1360 | + lons = f.select_by_identity("longitude")[0] |
| 1361 | + faces = f.select_by_identity("cf_role=face_edge_connectivity")[0] |
| 1362 | + |
| 1363 | + # Reduce the variable to match the shapes |
| 1364 | + pot = pot[4,:] |
| 1365 | + |
| 1366 | + cfp.levs(240, 310, 5) |
| 1367 | + |
| 1368 | + cfp.con( |
| 1369 | + f=pot, face_lons=lons, face_lats=lats, |
| 1370 | + face_connectivity=faces, lines=False |
| 1371 | + ) |
| 1372 | + |
| 1373 | + # @compare_plot_results # SLB TODO add expected plot |
| 1374 | + def test_example_unstructured_lfric_2(self): |
| 1375 | + """Test example for unstructured grids: LFRic example 2.""" |
| 1376 | + f = cf.read("cfplot_data/lfric_initial.nc") |
| 1377 | + |
| 1378 | + # Select the relevant fields for the objects required for the plot, |
| 1379 | + # taking the air potential temperature as a variable to choose to view. |
| 1380 | + pot = f.select_by_identity("air_potential_temperature")[0] |
| 1381 | + lats = f.select_by_identity("latitude")[0] |
| 1382 | + lons = f.select_by_identity("longitude")[0] |
| 1383 | + faces = f.select_by_identity("cf_role=face_edge_connectivity")[0] |
| 1384 | + |
| 1385 | + # Reduce the variable to match the shapes |
| 1386 | + pot = pot[4,:] |
| 1387 | + |
| 1388 | + cfp.levs(240, 310, 5) |
| 1389 | + |
| 1390 | + # This time set the projection to a polar one for a different view |
| 1391 | + cfp.mapset(proj="npstere") |
| 1392 | + cfp.con( |
| 1393 | + f=pot, face_lons=lons, |
| 1394 | + face_lats=lats, face_connectivity=faces, lines=False |
| 1395 | + ) |
| 1396 | + |
| 1397 | + # @compare_plot_results # SLB TODO add expected plot |
| 1398 | + def test_example_unstructured_lfric_3(self): |
| 1399 | + """Test example for unstructured grids: LFRic example 3.""" |
| 1400 | + f = cf.read("cfplot_data/lfric_initial.nc") |
| 1401 | + pot = f.select_by_identity("air_potential_temperature")[0] |
| 1402 | + |
| 1403 | + g = pot[0, :] |
| 1404 | + cfp.con(g, lines=False) |
| 1405 | + |
| 1406 | + @unittest.expectedFailure # suspected cf-plot bug, needs investigating |
| 1407 | + def test_example_unstructured_orca_1(self): |
| 1408 | + """Test example for unstructured grids: ORCA grid example 1.""" |
| 1409 | + # NOTE: this is taken from the 'unstructured.rst/html' page, but |
| 1410 | + # is very similar to 'test_example_26', so coordinate with that. |
| 1411 | + f = cf.read("cfplot_data/orca2.nc") |
| 1412 | + |
| 1413 | + # Get an Orca grid and flatten the arrays |
| 1414 | + lons = f.select_by_identity("ncvar%longitude")[0] |
| 1415 | + lats = f.select_by_identity("ncvar%latitude")[0] |
| 1416 | + temp = f.select_by_identity("ncvar%sst")[0] |
| 1417 | + |
| 1418 | + lons.flatten(inplace=True) |
| 1419 | + lats.flatten(inplace=True) |
| 1420 | + temp.flatten(inplace=True) |
| 1421 | + |
| 1422 | + cfp.con(x=lons, y=lats, f=temp, ptype=1) |
| 1423 | + |
| 1424 | + @unittest.expectedFailure # text input based, needs investigating |
| 1425 | + def test_example_unstructured_station_data_1(self): |
| 1426 | + """Test example for unstructured grids: station data example 1.""" |
| 1427 | + # NOTE: this is taken from the 'unstructured.rst/html' page, but |
| 1428 | + # is very similar to 'test_example_24/25', so coordinate with that. |
| 1429 | + |
| 1430 | + # Part 1: docs title 'Station data' |
| 1431 | + |
| 1432 | + # Arrays for data |
| 1433 | + lons=[] |
| 1434 | + lats=[] |
| 1435 | + pressure=[] |
| 1436 | + temp=[] |
| 1437 | + |
| 1438 | + # Read data and make the contour plot |
| 1439 | + f = open('cfplot_data/synop_data.txt') |
| 1440 | + lines = f.readlines() |
| 1441 | + for line in lines: |
| 1442 | + mysplit=line.split() |
| 1443 | + lons=np.append(lons, float(mysplit[1])) |
| 1444 | + lats=np.append(lats, float(mysplit[2])) |
| 1445 | + pressure=np.append(pressure, float(mysplit[3])) |
| 1446 | + temp=np.append(temp, float(mysplit[4])) |
| 1447 | + |
| 1448 | + cfp.con( |
| 1449 | + x=lons, y=lats, f=temp, ptype=1, colorbar_orientation='vertical') |
| 1450 | + |
| 1451 | + # Part 2: docs title 'Station data - check of data values' |
| 1452 | + cfp.gopen() |
| 1453 | + for i in np.arange(len(lines)): |
| 1454 | + cfp.plotvars.mymap.text( |
| 1455 | + float(lons[i]), float(lats[i]), str(temp[i]), |
| 1456 | + horizontalalignment='center',verticalalignment='center', |
| 1457 | + transform=ccrs.PlateCarree() |
| 1458 | + ) |
| 1459 | + |
| 1460 | + cfp.gclose() |
| 1461 | + |
| 1462 | + |
1326 | 1463 | if __name__ == "__main__":
|
1327 | 1464 | print("==================\n" "Regression testing\n" "==================\n")
|
1328 | 1465 | cov = coverage.Coverage()
|
|
0 commit comments