Converts from the original ASCII format of the Chen+ (2014) 3D dust map to
    the HDF5 format.

    Args:
        dat_fname (:obj:`str`): Filename of the original ASCII .dat file.
        h5_fname (:obj:`str`): Output filename to write the resulting HDF5 file to.

def ascii2h5(dat_fname, h5_fname):
    """
    Converts from the original ASCII format of the Chen+ (2014) 3D dust map to
    the HDF5 format.

    Args:
        dat_fname (:obj:`str`): Filename of the original ASCII .dat file.
        h5_fname (:obj:`str`): Output filename to write the resulting HDF5 file to.
    """
    table = np.loadtxt(dat_fname, skiprows=1, dtype='f4')

    filter_kwargs = dict(
        chunks=True,
        compression='gzip',
        compression_opts=3)

    # Filter out pixels with all zeros
    idx = ~np.all(table[:,2:32] < 1.e-5, axis=1)

    with h5py.File(h5_fname, 'w') as f:
        d = np.arange(0., 4.351, 0.15).astype('f4')

        dset = f.create_dataset('dists', data=d, **filter_kwargs)
        dset.attrs['description'] = 'Distances at which extinction is measured'
        dset.attrs['units'] = 'kpc'

        dset = f.create_dataset('pix_lb', data=table[idx,0:2], **filter_kwargs)
        dset.attrs['description'] = 'Galactic (l, b) of each pixel'
        dset.attrs['units'] = 'deg'

        dset = f.create_dataset('A_r', data=table[idx,2:32], **filter_kwargs)
        dset.attrs['description'] = 'Extinction'
        dset.attrs['shape'] = '(pixel, distance)'
        dset.attrs['band'] = 'r'
        dset.attrs['units'] = 'mag'

        dset = f.create_dataset('A_r_err', data=table[idx,32:], **filter_kwargs)
        dset.attrs['description'] = 'Gaussian uncertainty in extinction'
        dset.attrs['shape'] = '(pixel, distance)'
        dset.attrs['band'] = 'r'
        dset.attrs['units'] = 'mag'