# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/cli/db_to_nc.ipynb.

# %% auto 0
__all__ = ['import_handler', 'main']

# %% ../../nbs/cli/db_to_nc.ipynb 2
from fastcore.script import *
from typing import Optional
from fastcore.style import S
import sys
import importlib

# %% ../../nbs/cli/db_to_nc.ipynb 3
def import_handler(handler_name, fn_name='encode'):
    try:
        handler = importlib.import_module(handler_name)
        return getattr(handler, fn_name)

    except (ImportError, AttributeError):
        print(f"Failed to import function: {fn_name}")

# %% ../../nbs/cli/db_to_nc.ipynb 4
@call_parse
def main(
    src: str,  # Path to MARIS database dump as `.txt` file
    dest: str, # Output path for NetCDF file(s)
    ref_ids: str = '',  # Optional comma-separated reference IDs (e.g., "123,456,789")
) -> None:
    """Convert MARIS legacy database to NetCDF4 format.
    
    If ref_ids is provided as comma-separated values, only encodes those subsets.
    """
    print('Encoding MARIS legacy database...')
    
    ids = [int(id.strip()) for id in ref_ids.split(',')] if ref_ids else None
    if ids: print(f"Processing reference IDs: {', '.join(map(str, ids))}")
    encode = import_handler('marisco.handlers.maris_legacy')
    encode(fname_in=src, dir_dest=dest, ref_ids=ids)
