source: trunk/UTIL/geovista_plot.py @ 3806

Last change on this file since 3806 was 3752, checked in by afalco, 7 weeks ago

dynamico: example of geovista plot (interactive plot of start/startfi files with native grid).
AF

File size: 1.6 KB
Line 
1from __future__ import annotations
2import os
3import xarray as xr
4import geovista as gv
5# from geovista.pantry.data import dynamico
6import geovista.theme
7
8def open_file(filename="start.nc"):
9   file_type=os.path.splitext(filename)[0]
10   file = xr.open_dataset(filename)
11   # lon lat names
12   if file_type.endswith('startfi'):
13      lon_name, lat_name = 'bounds_lon', 'bounds_lat'
14   elif file_type.endswith('start'):
15      lon_name, lat_name = 'bounds_lon_mesh', 'bounds_lat_mesh'
16   elif file_type.endswith('Xhistins'):
17      lon_name, lat_name = 'lon', 'lat'
18   else:
19      raise 'ERROR: file_type must be start or startphy or hist'
20   return file, lon_name, lat_name
21
22
23def main(file="start.nc", var="ps") -> None:
24    """Plot a DYNAMICO unstructured mesh.
25
26    Notes
27    -----
28    .. versionadded:: 0.1.0
29
30    """
31    # Load the sample data.
32    # sample = dynamico()
33    data, lon, lat = open_file(file)
34
35    # Create the mesh from the sample data.
36    mesh = gv.Transform.from_unstructured(data[lon], data[lat], data=data[var])
37
38    # Plot the unstructured mesh.
39    p = gv.GeoPlotter()
40    sargs = {"title": f"{var} "}
41    p.add_mesh(mesh, scalar_bar_args=sargs)
42    p.add_coastlines()
43    p.add_graticule()
44    p.add_axes()
45    p.add_text(
46        "DYNAMICO Icosahedral (10m Coastlines)",
47        position="upper_left",
48        font_size=10,
49    )
50    p.view_xz(negative=True)
51    p.camera.zoom(1.3)
52    p.show()
53    # (f"{os.path.splitext(file)[0]}_{var}.pdf")
54
55
56if __name__ == "__main__":
57    # main("start.nc", "ps")
58    main("startfi.nc", "tsurf")
59    # main("startfi.nc", "n2_surf")
Note: See TracBrowser for help on using the repository browser.