1 | import xarray as xr |
---|
2 | import matplotlib.pyplot as plt |
---|
3 | import numpy as np |
---|
4 | |
---|
5 | |
---|
6 | def plot_var(xx, yy, var2d, cmap='magma', extend='both', label='', title='', output="dynamico_plot.png"): |
---|
7 | plt.figure(figsize=(5.5, 5)) |
---|
8 | plt.subplots_adjust(left=0.01, bottom=0.01, right=0.96, top=0.94) |
---|
9 | plt.tricontourf(xx, yy, var2d, cmap=cmap) |
---|
10 | plt.colorbar(extend=extend, label=label, pad=0.01) |
---|
11 | plt.title(title, size=14) |
---|
12 | plt.savefig(output) |
---|
13 | |
---|
14 | filename_start = 'start.nc' |
---|
15 | filename_startfi = 'startfi.nc' |
---|
16 | |
---|
17 | def open_file(filename="start.nc", file_type="start"): |
---|
18 | file = xr.open_dataset(filename) |
---|
19 | # lon lat names |
---|
20 | if file_type == 'startphy': |
---|
21 | lon_name, lat_name = 'longitude', 'latitude' |
---|
22 | elif file_type == 'start': |
---|
23 | lon_name, lat_name = 'lon_mesh', 'lat_mesh' |
---|
24 | elif file_type == 'hist': |
---|
25 | lon_name, lat_name = 'lon', 'lat' |
---|
26 | else: |
---|
27 | raise 'ERROR: file_type must be start or startphy or hist' |
---|
28 | return file, lon_name, lat_name |
---|
29 | |
---|
30 | file_start, lon_start, lat_start = open_file(filename_start, "start") |
---|
31 | file_startfi, lon_startfi, lat_startfi = open_file(filename_startfi, "startphy") |
---|
32 | |
---|
33 | var_name = 'ps' |
---|
34 | title = 'Surface Pressure' |
---|
35 | unit = "kg/kg" |
---|
36 | plot_var(file_start[lon_start], file_start[lat_start], file_start[var_name], label=unit, title=title, output=f"start_{var_name}.png") |
---|
37 | |
---|
38 | var_name = 'n2' |
---|
39 | title = 'N2 surf' |
---|
40 | unit = "kg/kg" |
---|
41 | plot_var(file_startfi[lon_startfi], file_startfi[lat_startfi], file_startfi[var_name], label=unit, title=title, output=f"startfi_{var_name}.png") |
---|
42 | |
---|