SABER
contour_positive.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 import argparse
4 from netCDF4 import Dataset
5 import matplotlib
6 import matplotlib.cm as cm
7 import matplotlib.pyplot as plt
8 import matplotlib.tri as tri
9 import numpy as np
10 import numpy.ma as ma
11 import os
12 
13 def contour_positive(testdata, test, mpi, omp, suffix, testfig):
14  # Open file
15  f = Dataset(testdata + "/" + test + "/test_" + mpi + "-" + omp + "_" + suffix + ".nc", "r", format="NETCDF4")
16 
17  # Get _FillValue
18  _FillValue = f.__dict__["_FillValue"]
19 
20  # Get lon/lat
21  lon = f["lon"][:]
22  lat = f["lat"][:]
23 
24  # Get vertical unit
25  vunit = f["vunit"][:,:]
26 
27  # Get number of levels
28  nl0 = vunit.shape[0]
29 
30  for group in f.groups:
31  for var in f.groups[group].variables:
32  # Read variable
33  field = f.groups[group][var][:,:]
34 
35  # Set masked values and levels
36  field = ma.masked_invalid(field)
37  vmax = np.max(field)
38  levels = np.linspace(0, vmax, 21)
39  field = field.filled(fill_value=-1.0e38)
40 
41  # Plots
42  fig, ax = plt.subplots(nrows=nl0)
43  fig.subplots_adjust(hspace=0.4, right=0.8)
44  for il0 in range(0, nl0):
45  ax[il0].set_title(group + " - " + var + " @ " + str(il0))
46  im = ax[il0].tricontourf(lon, lat, field[il0,:], levels=levels, cmap="YlOrRd")
47 
48  # Colorbar
49  cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
50  fig.colorbar(im, cax=cbar_ax)
51 
52  # Save and close figure
53  plt.savefig(testfig + "/test_" + mpi + "-" + omp + "_" + suffix + "_" + group + "_" + var + ".jpg", format="jpg", dpi=300)
54  plt.close()
55 
56  for subgroup in f.groups[group].groups:
57  for var in f.groups[group].groups[subgroup].variables:
58  # Read variable
59  field = f.groups[group].groups[subgroup][var][:,:]
60 
61  # Set masked values and levels
62  field = ma.masked_invalid(field)
63  vmin = np.min(field)
64  if (vmin < 0.0):
65  vmax = np.max(np.abs(field))
66  levels = np.linspace(-vmax, vmax, 21)
67  else:
68  vmax = np.max(field)
69  if (vmax > 0.0):
70  levels = np.linspace(0, vmax, 21)
71  else:
72  levels = np.linspace(0, 1.0, 21)
73  field = field.filled(fill_value=-1.0e38)
74 
75  # Plots
76  fig, ax = plt.subplots(nrows=nl0)
77  fig.subplots_adjust(hspace=0.4, right=0.8)
78  for il0 in range(0, nl0):
79  ax[il0].set_title(group + " - " + subgroup + " - " + var + " @ " + str(il0))
80  im = ax[il0].tricontourf(lon, lat, field[il0,:], levels=levels, cmap="YlOrRd")
81 
82  # Colorbar
83  cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
84  fig.colorbar(im, cax=cbar_ax)
85 
86  # Save and close figure
87  plt.savefig(testfig + "/test_" + mpi + "-" + omp + "_" + suffix + "_" + group + "_" + subgroup + "_" + var + ".jpg", format="jpg", dpi=300)
88  plt.close()
contour_positive.contour_positive
def contour_positive(testdata, test, mpi, omp, suffix, testfig)
Definition: contour_positive.py:13