Beispiel #1
0
 def _gen_idx(self, from_ts_type, to_ts_type, min_num_obs):
     if isnumeric(min_num_obs):
         return [(to_ts_type.val, int(min_num_obs))]
     if not isinstance(min_num_obs, dict):
         raise ValueError('Invalid input for min_num_obs, need dictionary '
                          'or integer, got {}'.format(min_num_obs))
     valid = self.valid_base_ts_types
     from_mul = from_ts_type.mulfac
     if from_mul != 1:
         const.print_log.warn('Ignoring multiplication factor {} in '
                              'data with resolution {} in resampling method'
                              .format(from_mul, from_ts_type))
     start = valid.index(from_ts_type.base)
     stop = valid.index(to_ts_type.base) 
     
     last_from = valid[start]
     idx = []
     for i in range(start+1, stop+1):
         to = valid[i]
         if to in min_num_obs and last_from in min_num_obs[to]:
             
             min_num = min_num_obs[to][last_from]
             last_from = to
             idx.append((to, min_num))
     if len(idx) == 0 or not idx[-1][0] == to_ts_type.base:
         idx.append((to_ts_type.base, 0))
     return idx
Beispiel #2
0
    def _gen_idx(self, from_ts_type, to_ts_type, min_num_obs, how):
        if isnumeric(min_num_obs):
            if not isinstance(how, str):
                raise ValueError('Error initialising resampling constraints. '
                                 'min_num_obs is numeric ({}) and input how '
                                 'is {} (would need to be string, e.g. mean)'
                                 .format(min_num_obs, how))
            return [(to_ts_type.val, int(min_num_obs), how)]
        if not isinstance(min_num_obs, dict):
            raise ValueError('Invalid input for min_num_obs, need dictionary '
                             'or integer, got {}'.format(min_num_obs))

        how_default = how if isinstance(how, str) else 'mean'

        valid = self.valid_base_ts_types
        from_mul = from_ts_type.mulfac
        if from_mul != 1:
            const.print_log.warning('Ignoring multiplication factor {} in '
                                 'data with resolution {} in resampling method'
                                 .format(from_mul, from_ts_type))
        start = valid.index(from_ts_type.base)
        stop = valid.index(to_ts_type.base)

        last_from = valid[start]
        idx = []
        for i in range(start+1, stop+1):
            to = valid[i]
            if to in min_num_obs and last_from in min_num_obs[to]:
                if isinstance(how, dict) and to in how and last_from in how[to]:
                    _how = how[to][last_from]
                else:
                    _how = how_default
                min_num = min_num_obs[to][last_from]
                last_from = to
                idx.append((to, min_num, _how))
        if len(idx) == 0  or not idx[-1][0] == to_ts_type.val:
            idx.append((to_ts_type.val, 0, how_default))
        return idx
Beispiel #3
0
def _regrid_gridded(gridded, regrid_scheme, regrid_res_deg):
    """
    Regrid instance of `GriddedData`

    Makes sure to handle different input options for `regrid_res_deg`.

    Parameters
    ----------
    gridded : GriddedData
        instance of :class:`GriddedData` that is supposed to be regridded.
    regrid_scheme : str
        iris scheme used for regridding (defaults to area weighted regridding)
    regrid_res_deg : int or dict, optional
        regrid resolution in degrees. If specified, the input gridded data
        objects will be regridded in lon / lat dimension to the input
        resolution (if input is integer, both lat and lon are regridded to that
        resolution, if input is dict, use keys `lat_res_deg` and `lon_res_deg`
        to specify regrid resolutions, respectively).

    Raises
    ------
    ValueError
        If input for `regrid_res_deg` is invalid.

    Returns
    -------
    GriddedData
        regridded data object

    """
    if not isinstance(regrid_res_deg, dict):
        if not isnumeric(regrid_res_deg):
            raise ValueError('Invalid input for regrid_res_deg. Need integer '
                             'or dict specifying lat and lon res')
        regrid_res_deg = dict(lat_res_deg=regrid_res_deg,
                              lon_res_deg=regrid_res_deg)

    return gridded.regrid(scheme=regrid_scheme, **regrid_res_deg)
Beispiel #4
0
def get_country_info_coords(coords):
    """
    Get country information for input lat/lon coordinates

    Parameters
    ----------
    coords : list or tuple
        list of coord tuples (lat, lon) or single coord tuple

    Raises
    ------
    ModuleNotFoundError
        if library reverse_geocode is not installed
    ValueError
        if input format is incorrect

    Returns
    -------
    list
        list of dictionaries containing country information for each input
        coordinate
    """
    try:
        import reverse_geocode as rg
    except ModuleNotFoundError:
        raise ModuleNotFoundError('Cannot retrieve country information for '
                                  'lat / lon coordinates. Please install '
                                  'library reverse_geocode')
    if isinstance(coords, np.ndarray):
        coords = list(coords)
    if not isinstance(coords, (list, tuple)):
        raise ValueError(
            'Invalid input for coords, need list or tuple or array')

    if isnumeric(coords[0]) and len(coords) == 2:  #only one coordinate
        return [rg.get(coords)]

    return rg.search(coords)
Beispiel #5
0
def test_isnumeric():
    assert helpers.isnumeric(3)
    assert helpers.isnumeric(3.3455)
    assert helpers.isnumeric(np.complex(1, 2))