SABER
avg.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.pyplot as plt
7 import numpy as np
8 import numpy.ma as ma
9 import os
10 
11 def avg(testdata, test, mpi, omp, suffix, testfig):
12  # Open file
13  f = Dataset(testdata + "/" + test + "/test_" + mpi + "-" + omp + "_" + suffix + ".nc", "r", format="NETCDF4")
14 
15  # Get vertical unit
16  vunit = f["vunit"][:]
17 
18  # Get number of levels
19  nl0 = vunit.shape[0]
20 
21  for group in f.groups:
22  # Get horizontal distance
23  disth = f.groups[group]["disth"][:]
24 
25  # Get number of horizontal classes
26  nc3 = disth.shape[0]
27 
28  # Get number of reduced levels
29  nl0r = f.groups[group]["cor_hist"][:,:,:,:].shape[1]
30 
31  # Get number of bins
32  nbins = f.groups[group]["cor_hist"][:,:,:,:].shape[3]
33 
34  # Read variable and bins
35  l0rl0_to_l0 = f.groups[group]["l0rl0_to_l0"][:,:]
36  m11_hist = f.groups[group]["m11_hist"][:,:,:,:]
37  m11_bins = f.groups[group]["m11_bins"][:,:,:,:]
38  m11m11_hist = f.groups[group]["m11m11_hist"][:,:,:,:]
39  m11m11_bins = f.groups[group]["m11m11_bins"][:,:,:,:]
40  m2m2_hist = f.groups[group]["m2m2_hist"][:,:,:,:]
41  m2m2_bins = f.groups[group]["m2m2_bins"][:,:,:,:]
42  m22_hist = f.groups[group]["m22_hist"][:,:,:,:]
43  m22_bins = f.groups[group]["m22_bins"][:,:,:,:]
44  cor_hist = f.groups[group]["cor_hist"][:,:,:,:]
45  cor_bins = f.groups[group]["cor_bins"][:,:,:,:]
46 
47  # Plots
48  for il0 in range(0, nl0):
49  for jl0r in range(0, nl0r):
50  for jc3 in range(0, nc3):
51  if np.any(cor_hist[il0,jl0r,jc3,:] > 0.0):
52  # Plots
53  fig, ax = plt.subplots(ncols=5)
54  fig.subplots_adjust(hspace=2.0)
55 
56  # Covariance
57  i = 0
58  ax[i].set_title("Covariance")
59  x = 0.5*(m11_bins[il0,jl0r,jc3,0:nbins]+m11_bins[il0,jl0r,jc3,1:nbins+1])
60  ax[i].plot(x, m11_hist[il0,jl0r,jc3,:], 'r-')
61 
62  # Covariance squared
63  i = 1
64  ax[i].set_title("Covariance squared")
65  x = 0.5*(m11m11_bins[il0,jl0r,jc3,0:nbins]+m11m11_bins[il0,jl0r,jc3,1:nbins+1])
66  ax[i].plot(x, m11m11_hist[il0,jl0r,jc3,:], 'r-')
67 
68  # Variance product
69  i = 2
70  ax[i].set_title("Variance product")
71  x = 0.5*(m2m2_bins[il0,jl0r,jc3,0:nbins]+m2m2_bins[il0,jl0r,jc3,1:nbins+1])
72  ax[i].plot(x, m2m2_hist[il0,jl0r,jc3,:], 'r-')
73 
74  # Fourth-order moment
75  i = 3
76  ax[i].set_title("Fourth-order moment")
77  x = 0.5*(m22_bins[il0,jl0r,jc3,0:nbins]+m22_bins[il0,jl0r,jc3,1:nbins+1])
78  ax[i].plot(x, m22_hist[il0,jl0r,jc3,:], 'r-')
79 
80  # Correlation
81  i = 4
82  ax[i].set_title("Correlation")
83  x = 0.5*(cor_bins[il0,jl0r,jc3,0:nbins]+cor_bins[il0,jl0r,jc3,1:nbins+1])
84  ax[i].plot(x, cor_hist[il0,jl0r,jc3,:], 'r-')
85 
86  # Add title
87  fig.suptitle("Between levels " + '%.2e'%vunit[il0] + " and " + '%.2e'%vunit[l0rl0_to_l0[il0,jl0r]-1] + " for distance class " + '%.2e'%disth[jc3])
88 
89  # Save and close figure
90  plt.savefig(testfig + "/test_" + mpi + "-" + omp + "_" + suffix + "_" + group + "_" + str(il0+1) + "-" + str(l0rl0_to_l0[il0,jl0r]) + "-" + str(jc3+1) + ".jpg", format="jpg", dpi=300)
91  plt.close()
avg.avg
def avg(testdata, test, mpi, omp, suffix, testfig)
Definition: avg.py:11