Exemple #1
0
def cdf_units(cdf_, keys=None, manual_units=None):
    """
    Takes the CDF File and the required keys, and finds the units of the
    selected keys.

    Parameters
    ----------
    cdf_ : cdf
        Opened cdf file
    keys : dict, optional
        If the user knows the units they wish to extract
        from the CDF file keys, keys can be passed as an arugment.
        If not, the function extracts all the keys present which
        have an UNIT attribute.

    Returns
    -------
    out : :class:`collections.OrderedDict`
        Returns an OrderedDict with units of the selected keys.
    """
    units = coll.OrderedDict()
    if keys is None:
        message = "No keys assigned for the CDF. Extracting manually."
        warnings.warn(message, Warning)
        keys = dict(zip(list(cdf_.keys()), list(cdf_.keys())))
    for key, val in keys.items():
        try:
            temp_unit = u.Unit(cdf_[key].attrs['UNITS'])
        except ValueError:
            if manual_units is not None:
                if key in manual_units:
                    continue
            unknown_unit = (cdf_[key].attrs['UNITS'])
            temp_unit = helper.cdf_dict(unknown_unit)
            if temp_unit is None:
                message = "The CDF provided units ({}) for key '{}' \
                are unknown".format(unknown_unit, key)
                warnings.warn(message, Warning)
        except KeyError:
            continue
        if isinstance(val, list):
            units.update(coll.OrderedDict.fromkeys(val, temp_unit))
        else:
            if temp_unit is not None:
                units[val] = temp_unit
    if manual_units is not None:
        units.update(manual_units)
    return units
Exemple #2
0
def cdf_units(cdf_, manual_units=None, length=None):
    """
    Takes the CDF File and the required keys, and finds the units of the
    selected keys.

    Parameters
    ----------
    cdf_ : cdf
        Opened cdf file
    manual_units : ~collections.OrderedDict, optional
        Manually defined units to be attached to the data that will be
        returned.

    Returns
    -------
    out : :class:`collections.OrderedDict`
        Returns an OrderedDict with units of the selected keys.
    """
    units = coll.OrderedDict()
    key_dict = {}
    var_list = []
    if manual_units is None:
        manual_units = {'NOTEXIST': u.dimensionless_unscaled}
    # To figure out whether rVariable or zVariable needs to be taken
    for attr in list(cdf_.cdf_info().keys()):
        if 'variable' in attr.lower():
            if len(cdf_.cdf_info()[attr]) > 0:
                var_list += [attr]

    # Extract the list of valid keys in the zVar or rVar
    for attr in var_list:
        for key in cdf_.cdf_info()[attr]:
            try:
                y = cdf_.varget(key)
                ncols = y.shape
                if len(ncols) == 1:
                    key_dict[key] = key
                if len(ncols) > 1:
                    val = []
                    val.append(key)
                    for x in range(0, ncols[1]):
                        field = key + "{}".format('_' + str(x))
                        val.append(field)
                    key_dict[key] = val
            except Exception as e:
                print("{}-Variable{}".format(e, key))
                continue

    # Assigning units to the keys
    for key, val in key_dict.items():
        unit_str = None
        temp_unit = None
        try:
            unit_str = cdf_.varattsget(key)['UNITS']
        except KeyError:
            if key in manual_units:
                temp_unit = manual_units[key]
            else:
                continue

        if temp_unit is None:
            try:
                temp_unit = u.Unit(unit_str)
            except (TypeError, ValueError):
                if key in manual_units:
                    temp_unit = manual_units[key]
                elif helper.cdf_dict(unit_str):
                    temp_unit = helper.cdf_dict(unit_str)
                if unit_str is None:
                    message = "The CDF provided units ({}) for key '{}' \
                    are unknown".format(unit_str, key)
                    warnings.warn(message, Warning)
                    continue
        if temp_unit is not None:
            if isinstance(val, list):
                units.update(coll.OrderedDict.fromkeys(val, temp_unit))
            else:
                units[val] = temp_unit
    units.update(manual_units)
    return units
Exemple #3
0
def cdf_units(cdf_, manual_units=None, length=None):
    """
    Takes the CDF File and the required keys, and finds the units of the
    selected keys.

    Parameters
    ----------
    cdf_ : cdf
        Opened cdf file
    manual_units : ~collections.OrderedDict, optional
        Manually defined units to be attached to the data that will be
        returned.

    Returns
    -------
    out : :class:`collections.OrderedDict`
        Returns an OrderedDict with units of the selected keys.
    """
    units = coll.OrderedDict()

    # Get the list of all variables
    var_list = _get_cdf_vars(cdf_)
    logger.info(f'Found the following variables in CDF: {var_list}')
    # Get a mapping from each key to any sub-keys
    key_dict = {}
    # Extract the list of valid keys in the zVar or rVar
    for key in var_list:
        y = np.array(cdf_.varget(key))
        ncols = y.shape
        if len(ncols) == 1:
            key_dict[key] = key
        if len(ncols) > 1:
            val = []
            val.append(key)
            for x in range(0, ncols[1]):
                field = f'{key}_{x}'
                val.append(field)
            key_dict[key] = val

    logger.info(f'Getting units for {key_dict}')
    # Assigning units to the keys
    for key, val in key_dict.items():
        unit_str = None
        temp_unit = None
        # Try to get a unit string from the CDF file
        try:
            unit_str = cdf_.varattsget(key)['UNITS']
        # Fallback on user provided units
        except KeyError:
            if manual_units and key in manual_units:
                temp_unit = manual_units[key]
            else:
                continue

        if temp_unit is None:
            try:
                temp_unit = u.Unit(unit_str)
            except (TypeError, ValueError):
                if manual_units and key in manual_units:
                    temp_unit = manual_units[key]
                elif helper.cdf_dict(unit_str) is not None:
                    temp_unit = helper.cdf_dict(unit_str)

                if temp_unit is None:
                    message = (f"The CDF provided units ('{unit_str}') for"
                               f" key '{key}' are unknown")
                    warnings.warn(message)
                    continue

        if isinstance(val, list):
            for v in val:
                units[v] = temp_unit
        else:
            units[val] = temp_unit

    if manual_units:
        units.update(manual_units)
    logger.info(f'Extracted following units: {units}')
    return units