src/pys5p/s5p_xarray.py
def h5_to_xr(h5_dset, data_sel=None, *, dims=None, field=None):
    """
    Create xarray::DataArray from a HDF5 dataset (with dimension scales)

    Parameters
    ----------
    h5_dset :  h5py.Dataset
       Data, dimensions, coordinates and attributes are read for this dataset
    data_sel :  slice, optional
       A numpy slice generated for example numpy.s_
    dims :  list of strings, optional
       Alternative names for the dataset dimensions if not attached to dataset
    field : str, optional
       Name of field in compound dataset or None

    Returns
    -------
    xarray.DataArray

    Notes
    -----
    If data_sel is used to select data from a dataset then the number of
    dimensions of data_sel should agree with the HDF5 dataset or one and
    only one Ellipsis has to be used.
    Thus allowed values for data_sel are:
    * [always]: (), np.s_[:], np.s_[...]
    * [1-D dataset]: np.s_[:-1], np.s_[0]
    * [2-D dataset]: np.s_[:-1, :], np.s_[0, :], np.s_[:-1, 0]
    * [3-D dataset]: np.s_[:-1, :, 2:4], np.s_[0, :, :], np.s_[:-1, 0, 2:4]
    * [Ellipsis] np.s_[0, ...], np.s_[..., 4], np.s_[0, ..., 4]

    Examples
    --------
    >>> fid = h5py.File(flname, 'r')        # flname is a HDF5/netCDF4 file
    >>> xdata = h5_to_xr(fid['signal'])
    >>> fid.close()

    Combine Tropomi SWIR data of band7 and band8
    >>> fid = h5py.File(s5p_b7_prod, 'r')   # Tropomi band7 product
    >>> xdata7 = h5_to_xr(fid['signal'])
    >>> fid.close()
    >>> fid = h5py.File(s5p_b8_prod, 'r')   # Tropomi band8 product
    >>> xdata8 = h5_to_xr(fid['signal'])
    >>> fid.close()
    >>> xdata = xr.concat((xdata7, xdata8), dim='spectral_channel')

    Optionally, fix the 'column' dimension
    >>> xdata = xdata.assign_coords(
    >>> ... column=np.arange(xdata.column.size, dtype='u4'))
    """
    if data_sel is not None:
        if data_sel in (np.s_[:], np.s_[...], np.s_[()]):
            data_sel = None
        elif np.isscalar(data_sel):
            data_sel = (np.s_[data_sel:data_sel+1],)
        else:
            buff = ()
            for val in data_sel:
                if val == Ellipsis:
                    for _ in range(h5_dset.ndim - len(data_sel) + 1):
                        buff += np.index_exp[:]
                elif np.isscalar(val):
                    buff += (np.s_[val:val+1],)
                else:
                    buff += (val,)
            data_sel = buff

    # Name of this array
    name = PurePath(h5_dset.name).name

    # Values for this array
    data = __get_data(h5_dset, data_sel, field)

    # Coordinates (tick labels) to use for indexing along each dimension
    coords = []
    if dims is None:
        coords = __get_coords(h5_dset, data_sel)
    if not coords:
        coords = __set_coords(h5_dset, data_sel, dims)

    # Attributes to assign to the array
    attrs = __get_attrs(h5_dset, field)

    # check if dimension of dataset and coordinates agree
    if data.ndim < len(coords):
        for ii in reversed(range(len(coords))):
            if np.isscalar(coords[ii][1]):
                del coords[ii]

    return xr.DataArray(data, name=name, attrs=attrs, coords=coords)

    Create xarray::DataArray from a HDF5 dataset (with dimension scales)

    Parameters
    ----------
    h5_dset :  h5py.Dataset
       Data, dimensions, coordinates and attributes are read for this dataset
    data_sel :  slice, optional
       A numpy slice generated for example numpy.s_
    dims :  list of strings, optional
       Alternative names for the dataset dimensions if not attached to dataset
    field : str, optional
       Name of field in compound dataset or None

    Returns
    -------
    xarray.DataArray

    Notes
    -----
    If data_sel is used to select data from a dataset then the number of
    dimensions of data_sel should agree with the HDF5 dataset or one and
    only one Ellipsis has to be used.
    Thus allowed values for data_sel are:
    * [always]: (), np.s_[:], np.s_[...]
    * [1-D dataset]: np.s_[:-1], np.s_[0]
    * [2-D dataset]: np.s_[:-1, :], np.s_[0, :], np.s_[:-1, 0]
    * [3-D dataset]: np.s_[:-1, :, 2:4], np.s_[0, :, :], np.s_[:-1, 0, 2:4]
    * [Ellipsis] np.s_[0, ...], np.s_[..., 4], np.s_[0, ..., 4]

    Examples
    --------
    >>> fid = h5py.File(flname, 'r')        # flname is a HDF5/netCDF4 file
    >>> xdata = h5_to_xr(fid['signal'])
    >>> fid.close()

    Combine Tropomi SWIR data of band7 and band8
    >>> fid = h5py.File(s5p_b7_prod, 'r')   # Tropomi band7 product
    >>> xdata7 = h5_to_xr(fid['signal'])
    >>> fid.close()
    >>> fid = h5py.File(s5p_b8_prod, 'r')   # Tropomi band8 product
    >>> xdata8 = h5_to_xr(fid['signal'])
    >>> fid.close()
    >>> xdata = xr.concat((xdata7, xdata8), dim='spectral_channel')

    Optionally, fix the 'column' dimension
    >>> xdata = xdata.assign_coords(
    >>> ... column=np.arange(xdata.column.size, dtype='u4'))
    
Create xarray::DataArray from a HDF5 dataset (with dimension scales)
Parameters

h5_dset :  h5py.Dataset
Data, dimensions, coordinates and attributes are read for this dataset
data_sel :  slice, optional
A numpy slice generated for example numpy.s_
dims :  list of strings, optional
Alternative names for the dataset dimensions if not attached to dataset
field : str, optional
Name of field in compound dataset or None

Returns



Notes

If data_sel is used to select data from a dataset then the number of
dimensions of data_sel should agree with the HDF5 dataset or one and
only one Ellipsis has to be used.

Examples






Create xarray :: DataArray from a HDF5 dataset ( with dimension scales ) Parameters h5_dset : h5py . Dataset Data dimensions coordinates and attributes are read for this dataset data_sel : slice optional A numpy slice generated for example numpy . s_ dims : list of strings optional Alternative names for the dataset dimensions if not attached to dataset field : str optional Name of field in compound dataset or None Returns Notes If data_sel is used to select data from a dataset then the number of dimensions of data_sel should agree with the HDF5 dataset or one and only one Ellipsis has to be used . Examples
def h5_to_xr(h5_dset, data_sel=None, *, dims=None, field=None):
    if data_sel is not None:
        if data_sel in (np.s_[:], np.s_[...], np.s_[()]):
            data_sel = None
        elif np.isscalar(data_sel):
            data_sel = (np.s_[data_sel:data_sel+1],)
        else:
            buff = ()
            for val in data_sel:
                if val == Ellipsis:
                    for _ in range(h5_dset.ndim - len(data_sel) + 1):
                        buff += np.index_exp[:]
                elif np.isscalar(val):
                    buff += (np.s_[val:val+1],)
                else:
                    buff += (val,)
            data_sel = buff
    name = PurePath(h5_dset.name).name
    data = __get_data(h5_dset, data_sel, field)
    coords = []
    if dims is None:
        coords = __get_coords(h5_dset, data_sel)
    if not coords:
        coords = __set_coords(h5_dset, data_sel, dims)
    attrs = __get_attrs(h5_dset, field)
    if data.ndim < len(coords):
        for ii in reversed(range(len(coords))):
            if np.isscalar(coords[ii][1]):
                del coords[ii]
    return xr.DataArray(data, name=name, attrs=attrs, coords=coords)
def h5_to_xr ( h5_dset , data_sel = None , * , dims = None , field = None ) : if data_sel is not None : if data_sel in ( np . s_ [ : ] , np . s_ [ ... ] , np . s_ [ ( ) ] ) : data_sel = None elif np . isscalar ( data_sel ) : data_sel = ( np . s_ [ data_sel : data_sel + 1 ] , ) else : buff = ( ) for val in data_sel : if val == Ellipsis : for _ in range ( h5_dset . ndim - len ( data_sel ) + 1 ) : buff += np . index_exp [ : ] elif np . isscalar ( val ) : buff += ( np . s_ [ val : val + 1 ] , ) else : buff += ( val , ) data_sel = buff name = PurePath ( h5_dset . name ) . name data = __get_data ( h5_dset , data_sel , field ) coords = [ ] if dims is None : coords = __get_coords ( h5_dset , data_sel ) if not coords : coords = __set_coords ( h5_dset , data_sel , dims ) attrs = __get_attrs ( h5_dset , field ) if data . ndim < len ( coords ) : for ii in reversed ( range ( len ( coords ) ) ) : if np . isscalar ( coords [ ii ] [ 1 ] ) : del coords [ ii ] return xr . DataArray ( data , name = name , attrs = attrs , coords = coords )
Create xarray::DataArray from a HDF5 dataset (with dimension scales)
Parameters
Create xarray :: DataArray from a HDF5 dataset ( with dimension scales ) Parameters
"""
    Create xarray::DataArray from a HDF5 dataset (with dimension scales)

    Parameters
    ----------
    h5_dset :  h5py.Dataset
       Data, dimensions, coordinates and attributes are read for this dataset
    data_sel :  slice, optional
       A numpy slice generated for example numpy.s_
    dims :  list of strings, optional
       Alternative names for the dataset dimensions if not attached to dataset
    field : str, optional
       Name of field in compound dataset or None

    Returns
    -------
    xarray.DataArray

    Notes
    -----
    If data_sel is used to select data from a dataset then the number of
    dimensions of data_sel should agree with the HDF5 dataset or one and
    only one Ellipsis has to be used.
    Thus allowed values for data_sel are:
    * [always]: (), np.s_[:], np.s_[...]
    * [1-D dataset]: np.s_[:-1], np.s_[0]
    * [2-D dataset]: np.s_[:-1, :], np.s_[0, :], np.s_[:-1, 0]
    * [3-D dataset]: np.s_[:-1, :, 2:4], np.s_[0, :, :], np.s_[:-1, 0, 2:4]
    * [Ellipsis] np.s_[0, ...], np.s_[..., 4], np.s_[0, ..., 4]

    Examples
    --------
    >>> fid = h5py.File(flname, 'r')        # flname is a HDF5/netCDF4 file
    >>> xdata = h5_to_xr(fid['signal'])
    >>> fid.close()

    Combine Tropomi SWIR data of band7 and band8
    >>> fid = h5py.File(s5p_b7_prod, 'r')   # Tropomi band7 product
    >>> xdata7 = h5_to_xr(fid['signal'])
    >>> fid.close()
    >>> fid = h5py.File(s5p_b8_prod, 'r')   # Tropomi band8 product
    >>> xdata8 = h5_to_xr(fid['signal'])
    >>> fid.close()
    >>> xdata = xr.concat((xdata7, xdata8), dim='spectral_channel')

    Optionally, fix the 'column' dimension
    >>> xdata = xdata.assign_coords(
    >>> ... column=np.arange(xdata.column.size, dtype='u4'))
    """ # Name of this array # Values for this array # Coordinates (tick labels) to use for indexing along each dimension # Attributes to assign to the array # check if dimension of dataset and coordinates agree
h5_dset
data_sel
dims
field