Commit 3e5890d6 authored by cponcele's avatar cponcele
Browse files

add a silent mode to nc_reader

parent 7f11e768
......@@ -62,6 +62,8 @@ class NcReader:
# open the file
self.file_name = filename
self.dataset = nc.Dataset(self.file_name)
self.quiet = False
def __del__(self):
# close the file
......@@ -214,8 +216,8 @@ class NcReader:
#values = np.empty(shape=reduced_shape, dtype=object)
values=np.squeeze(vlen_variable[:])
pprint(f"Vlen variable {variable_path} : reduced shape for display is {reduced_dimensions} ({reduced_shape})")
if not self.quiet:
pprint(f"Vlen variable {variable_path} : reduced shape for display is {reduced_dimensions} ({reduced_shape})")
if len(reduced_shape) == 1:
# dimension is 1 like a ping indexed variable, we can parse all vlen data and build a matrix from it
values = np.squeeze(values)
......@@ -244,9 +246,10 @@ class NcReader:
Print variable
:return: None
"""
header("\n\n")
header(f"Variable {variable_name} : {variable_path}")
pprint(f"{self.dataset[variable_path]}")
if not self.quiet:
header("\n\n")
header(f"Variable {variable_name} : {variable_path}")
pprint(f"{self.dataset[variable_path]}")
def _display_variable(
self,
......@@ -272,26 +275,29 @@ class NcReader:
pprint(f"Variable {variable_name} {len(values)} values (['{values[0]}',...,'{values[-1]}']")
else:
v = self._get_variable_data(variable_path, slice_index)
pprint("Variable : " + variable_path + " size =" + str(v.shape))
if not self.quiet:
pprint("Variable : " + variable_path + " size =" + str(v.shape))
if len(v.shape) == 1:
if v.shape[0] > 0:
pprint("Statistics min:" + str(np.nanmin(v)) + " max:" + str(np.nanmax(v)))
plt.plot(v)
plt.show()
if not self.quiet:
pprint("Statistics min:" + str(np.nanmin(v)) + " max:" + str(np.nanmax(v)))
plt.plot(v)
plt.show()
else:
warning("1D variable with a null size" + variable_path + " size =" + str(v.shape))
elif len(v.shape) == 2:
if v.shape[0] > 0 and v.shape[1] > 0:
pprint("statistics min:" + str(np.nanmin(v)) + " max:" + str(np.nanmax(v)))
if vmin is None:
vmin = np.nanmin(v)
if vmax is None:
vmax = np.nanmax(v)
fig, ax = plt.subplots()
im = ax.imshow(v, aspect="auto", cmap=cmap, vmin=vmin, vmax=vmax)
fig.colorbar(im)
plt.show()
if not self.quiet:
pprint("statistics min:" + str(np.nanmin(v)) + " max:" + str(np.nanmax(v)))
if vmin is None:
vmin = np.nanmin(v)
if vmax is None:
vmax = np.nanmax(v)
fig, ax = plt.subplots()
im = ax.imshow(v, aspect="auto", cmap=cmap, vmin=vmin, vmax=vmax)
fig.colorbar(im)
plt.show()
else:
warning(f"Empty values dimensions = {v.shape}")
else:
......@@ -405,7 +411,8 @@ class NcReader:
else:
# root dataset has not name
header(f"Root Group {dataset.path}")
pprint(f"{dataset}")
if not self.quiet:
pprint(f"{dataset}")
for variable in sorted(dataset.variables):
last_variable = self._print_variable(
dataset[variable], slice_index, ignored_variable_list, cmap=cmap, vmin=vmin, vmax=vmax
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment