Example #1
0
    def _preflight_check(self):
        """
        Check time_index and coordinates accross files
        """
        time_index = None
        lat_lon = None

        bad_files = []
        for file in self.h5_files:
            with Resource(file) as f:
                if 'time_index' in f:
                    ti = f.time_index
                    if time_index is None:
                        time_index = ti.copy()
                    else:
                        check = time_index.equals(ti)
                        if not check:
                            bad_files.append(file)

                ll = f.lat_lon
                if lat_lon is None:
                    lat_lon = ll.copy()
                else:
                    check = np.allclose(lat_lon, ll)
                    if not check:
                        bad_files.append(file)

        bad_files = list(set(bad_files))
        if bad_files:
            msg = ("The following files' coordinates and time-index do not "
                   "match:\n{}".format(bad_files))
            raise ResourceRuntimeError(msg)
Example #2
0
    def _init_multi_h5(h5_source, check_files=False):
        """
        Initialize MultiH5 handler class based on input type

        Parameters
        ----------
        h5_source : str | list
            Unix shell style pattern path with * wildcards to multi-file
            resource file sets. Files must have the same time index and
            coordinates but can have different datasets. Can also be an
            explicit list of complete filepaths.
        check_files : bool
            Check to ensure files have the same coordinates and time_index

        Returns
        -------
        multi_h5 : MultiH5 | MultiH5Path
            Initialized multi h5 handler
        """
        if isinstance(h5_source, str):
            multi_h5 = MultiH5Path(h5_source, check_files=check_files)
        elif isinstance(h5_source, (list, tuple)):
            multi_h5 = MultiH5(h5_source, check_files=check_files)
        else:
            msg = ('Cannot initialize MultiH5 from {}, expecting a path or a '
                   'list of .h5 file paths'.format(type(h5_source)))
            raise ResourceRuntimeError(msg)

        return multi_h5
Example #3
0
    def curtail_windspeed(self, gids, curtailment):
        """
        Apply temporal curtailment mask to windspeed resource at given sites

        Parameters
        ----------
        gids : int | list
            gids for site or list of sites to curtail
        curtailment : ndarray
            Temporal multiplier for curtailment
        """
        shape = (self.shape[0], )
        if isinstance(gids, int):
            site_pos = self.sites.index(gids)
        else:
            shape += (len(gids), )
            site_pos = [self.sites.index(id) for id in gids]

        if curtailment.shape != shape:
            msg = "curtailment must be of shape: {}".format(shape)
            logger.error(msg)
            raise ResourceValueError(msg)

        if 'windspeed' in self._res_arrays:
            self._res_arrays['windspeed'][:, site_pos] *= curtailment
        else:
            msg = 'windspeed has not be loaded!'
            logger.error(msg)
            raise ResourceRuntimeError(msg)
Example #4
0
    def runnable(self):
        """
        Check to see if SAMResource iterator is runnable:
        - Meta must be loaded
        - Variables in var_list must be loaded

        Returns
        ------
        bool
            Returns True if runnable check passes
        """
        if self._meta is None:
            msg = 'meta has not been set!'
            logger.error(msg)
            raise ResourceRuntimeError(msg)
        else:
            for var in self.var_list:
                if var not in self._res_arrays.keys():
                    msg = '{} has not been set!'.format(var)
                    logger.error(msg)
                    raise ResourceRuntimeError(msg)

        return True
Example #5
0
    def __getitem__(self, keys):
        ds, ds_slice = parse_keys(keys)
        _, ds_name = os.path.split(ds)
        if 'SAM' in ds_name:
            site = ds_slice[0]
            if isinstance(site, (int, np.integer)):
                _, height = self._parse_name(ds_name)
                out = self.get_SAM_df(site, height)
            else:
                msg = "Can only extract SAM DataFrame for a single site"
                raise ResourceRuntimeError(msg)
        else:
            out = super().__getitem__(keys)

        return out
Example #6
0
    def __getitem__(self, keys):
        ds, ds_slice = parse_keys(keys)

        if ds.endswith('time_index'):
            out = self._get_time_index(ds, ds_slice)
        elif ds.endswith('meta'):
            out = self._get_meta(ds, ds_slice)
        elif 'SAM' in ds:
            site = ds_slice[0]
            if isinstance(site, int):
                out = self._get_SAM_df(ds, site)
            else:
                msg = "Can only extract SAM DataFrame for a single site"
                raise ResourceRuntimeError(msg)
        else:
            out = self._get_ds(ds, ds_slice)

        return out
Example #7
0
File: resource.py Project: NREL/rex
    def __getitem__(self, keys):
        ds, ds_slice = parse_keys(keys)
        _, ds_name = os.path.split(ds)
        if ds_name.startswith('time_index'):
            out = self._get_time_index(ds, ds_slice)
        elif ds_name.startswith('meta'):
            out = self._get_meta(ds, ds_slice)
        elif ds_name.startswith('coordinates'):
            out = self._get_coords(ds, ds_slice)
        elif 'SAM' in ds_name:
            site = ds_slice[0]
            if isinstance(site, (int, np.integer)):
                out = self.get_SAM_df(site)  # pylint: disable=E1111
            else:
                msg = "Can only extract SAM DataFrame for a single site"
                raise ResourceRuntimeError(msg)
        else:
            out = self._get_ds(ds, ds_slice)

        return out
Example #8
0
    def _get_var_mean(self, var, *var_slice):
        """
        Get variable means

        Parameters
        ----------
        var : str
            Resource variable name
        var_slice : int | list | slice
            Slice of variable array to extract

        Returns
        -------
        means : ndarray
            Vector of variable means
        """
        if self._mean_arrays is None:
            msg = "Variable means were not computed, you must set 'means=True'"
            logger.error(msg)
            raise ResourceRuntimeError(msg)

        if var in self.var_list:
            try:
                var_array = self._mean_arrays[var]
            except KeyError:
                msg = '{} has yet to be set!'.format(var)
                logger.error(msg)
                raise ResourceKeyError(msg)

            means = var_array[var_slice]
        else:
            msg = '{} not in {}'.format(var, self.var_list)
            logger.error(msg)
            raise ResourceKeyError(msg)

        return means