"""
Thin wrapper to return xarray Dataset for an ADCP, after parsing
using the regular rdradcp code.
"""
import xarray as xr
import numpy as np
from .. import utils
from . import rdradcp

def adcp_to_dataset(adcp):
    ds=xr.Dataset()
    ds.attrs['adcp']=adcp
    
    # convert to xarray
    for field in adcp.bin_data.dtype.names:
        dims=('time','bin')
        
        if adcp.bin_data[field].ndim==3:
            dims=dims+('beam',)
            
        ds[field]=dims, adcp.bin_data[field]

    for field in adcp.ensemble_data.dtype.names:
        dims=('time',)
        if adcp.ensemble_data[field].ndim==2:
            dims=dims+('beam',)
        ds[field]=dims,adcp.ensemble_data[field]
        
    ds['config']=(),adcp.config
    ds['name']=(),adcp.name

    # ds['bin_dist']=adcp.config.bin1_dist + adcp.config.cell_size*np.arange(ds.dims['bin'])
    ds['range']=('bin',),adcp.config.ranges

    ds['time']=('time',), utils.to_dt64(ds.mtime.values)
    return ds

def rdradcp_xr(*a,**kw):
    adcp=rdradcp.rdradcp(*a,**kw)
    ds=adcp_to_dataset(adcp)
    ds.attrs['src']=kw.get('name',None) or a[0]
    return ds


@@ -695,24 +695,16 @@
 %5Bfield%5D%0A
-        
 %0A    ds%5B
@@ -956,47 +956,315 @@
 es)%0A
-    return ds%0A%0Adef rdradcp_xr(*a,**kw):
+%0A    # Copy config to a dummy variable in addition to adcp.config%0A    ds%5B'config'%5D=(),0%0A    cfg=ds.attrs%5B'adcp'%5D.config%0A    for k in cfg.__dict__:%0A        ds.config.attrs%5Bk%5D=getattr(cfg,k)%0A    %0A    return ds%0A%0Adef rdradcp_xr(*a,**kw):%0A    %22%22%22%0A    Read raw T-RDI ADCP data, return as an xarray Dataset%0A    %22%22%22
 %0A   

Copy config values out to dummy variable to facilitate netcdf writing