Skip to content

Commit

Permalink
refactor(convert): Initial full refactor of the swap feature
Browse files Browse the repository at this point in the history
This is an initial full refactoring for swap feature.
Changes include the removal of Parsed2Zarr objects and its
downstream implementation in favor of direct zarr file writing
during the data parsing within the Parser object.
  • Loading branch information
lsetiawan committed Oct 2, 2023
1 parent ce1e614 commit 29d460b
Show file tree
Hide file tree
Showing 15 changed files with 470 additions and 1,847 deletions.
40 changes: 14 additions & 26 deletions echopype/convert/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
# fmt: off
# black and isort have conflicting ideas about how this should be formatted
from ..core import SONAR_MODELS
from .parsed_to_zarr import Parsed2Zarr

if TYPE_CHECKING:
from ..core import EngineHint, PathHint, SonarModelsHint
Expand All @@ -17,7 +16,9 @@
from ..utils.coding import COMPRESSION_SETTINGS
from ..utils.log import _init_logger
from ..utils.prov import add_processing_level
from .utils.ek import should_use_swap

# TODO: Fix and revive auto swap
# from .utils.ek import should_use_swap

BEAM_SUBGROUP_DEFAULT = "Beam_group1"

Expand Down Expand Up @@ -318,7 +319,7 @@ def open_raw(
use_swap: bool = False,
destination_path: Optional[str] = None,
destination_storage_options: Optional[Dict[str, str]] = None,
max_mb: int = 100,
max_chunk_size: str = "100MB",
) -> Optional[EchoData]:
"""Create an EchoData object containing parsed data from a single raw data file.
Expand Down Expand Up @@ -435,30 +436,28 @@ def open_raw(
else:
params = "ALL" # reserved to control if only wants to parse a certain type of datagram

# obtain dict associated with directly writing to zarr
dgram_zarr_vars = SONAR_MODELS[sonar_model]["dgram_zarr_vars"]

# Parse raw file and organize data into groups
parser = SONAR_MODELS[sonar_model]["parser"](
file_chk,
params=params,
storage_options=storage_options,
dgram_zarr_vars=dgram_zarr_vars,
sonar_model=sonar_model,
)

parser.parse_raw()

# Direct offload to zarr and rectangularization only available for some sonar models
if sonar_model in ["EK60", "ES70", "EK80", "ES80", "EA640"]:
# Determine whether to use swap
swap_map = {
"swap": True,
"no_swap": False,
}
if destination_path == "auto":
# Overwrite use_swap if it's True below
# Use local swap directory
use_swap = should_use_swap(parser.zarr_datagrams, dgram_zarr_vars, mem_mult=0.4)
# use_swap = should_use_swap(parser.zarr_datagrams, dgram_zarr_vars, mem_mult=0.4)
raise NotImplementedError("Automatic swap is not yet implemented.")
elif destination_path in swap_map:
use_swap = swap_map[destination_path]
else:
Expand All @@ -473,21 +472,13 @@ def open_raw(
)
)

if use_swap:
# Create sonar_model-specific p2z object
p2z = SONAR_MODELS[sonar_model]["parsed2zarr"](parser)
p2z.datagram_to_zarr(
dest_path=destination_path,
dest_storage_options=destination_storage_options,
max_mb=max_mb,
)
else:
p2z = Parsed2Zarr(parser) # Create general p2z object
parser.rectangularize_data()

else:
# No rectangularization for other sonar models
p2z = Parsed2Zarr(parser) # Create general p2z object
parser.rectangularize_data(
use_swap,
dest_path=destination_path,
dest_storage_options=destination_storage_options,
max_chunk_size=max_chunk_size,
)

setgrouper = SONAR_MODELS[sonar_model]["set_groups"](
parser,
Expand All @@ -496,7 +487,6 @@ def open_raw(
output_path=None,
sonar_model=sonar_model,
params=_set_convert_params(convert_params),
parsed2zarr_obj=p2z,
)

# Setup tree dictionary
Expand Down Expand Up @@ -547,9 +537,7 @@ def open_raw(
# Create tree and echodata
# TODO: make the creation of tree dynamically generated from yaml
tree = DataTree.from_dict(tree_dict, name="root")
echodata = EchoData(
source_file=file_chk, xml_path=xml_chk, sonar_model=sonar_model, parsed2zarr_obj=p2z
)
echodata = EchoData(source_file=file_chk, xml_path=xml_chk, sonar_model=sonar_model)
echodata._set_tree(tree)
echodata._load_tree()

Expand Down
Loading

0 comments on commit 29d460b

Please sign in to comment.