Commit dd44166d authored by DEVOS's avatar DEVOS
Browse files

Initiate nc_reader refactoring

parent e80e8cc3
Utility class to manage Sonar-netcdf dimensions
from utils.nc_reader import NcReader, open_nc_reader
def find_time_coordinate_variables(reader: NcReader) -> None:
Browse downward group hierarchy to find all coordinate variables based on time units
for all_variables in reader.walk_tree_variables():
for variable in all_variables:
if __name__ == "__main__":
with open_nc_reader("E:/ifremer/data/sonar-netcdf/ADCP/") as reader:
import sys
import traceback
from contextlib import contextmanager
from typing import Optional
import matplotlib.pyplot as plt
import netCDF4 as nc
import numpy as np
import matplotlib.pyplot as plt
from .print_color import header, warning, error, pprint
from .print_color import error, header, pprint, warning
# ensure minimum size for figures
plt.rcParams["figure.dpi"] = 270
......@@ -405,10 +408,36 @@ class NcReader:
return last_variable
def walk_tree_group(self, dataset: Optional[nc.Dataset] = None):
if dataset is None:
dataset = self.dataset
yield dataset.groups.values()
for value in dataset.groups.values():
yield from self.walk_tree_group(value)
def walk_tree_variables(self, dataset: Optional[nc.Dataset] = None):
for all_groups in self.walk_tree_group(dataset):
for group in all_groups:
yield group.variables
def open_nc_reader(sonar_path: str):
Define a With Statement Context Managers for a SounderDriver
Allow opening a SounderDriver in a With Statement
# Code to acquire resource, e.g.:
sonar_reader = NcReader(sonar_path)
yield sonar_reader
del sonar_reader
# in case we are started in standalone app, for debug only
if __name__ == "__main__":
#due to relative import should be run as a module (python -m ...)
# due to relative import should be run as a module (python -m ...)
file_path = "D:/data/file/XSF/Movies/"
file_path = "D:/XSF/"
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