[3752] | 1 | from __future__ import annotations |
---|
| 2 | import os |
---|
| 3 | import xarray as xr |
---|
| 4 | import geovista as gv |
---|
| 5 | # from geovista.pantry.data import dynamico |
---|
| 6 | import geovista.theme |
---|
| 7 | |
---|
| 8 | def 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 | |
---|
| 23 | def 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 | |
---|
| 56 | if __name__ == "__main__": |
---|
| 57 | # main("start.nc", "ps") |
---|
| 58 | main("startfi.nc", "tsurf") |
---|
| 59 | # main("startfi.nc", "n2_surf") |
---|