Exemple #1
0
 def isin4timeslice_region(self, name, start, stop, region_id):
     """Return values for specified variable from start to stop filtered by region map
     
     The values are flattened by concatenating time steps of all grid points
     
     Parameters
     ----------
     name      : string
                 name of the variable
     start     : datetime.datetime
                 start time
     stop      : datetime.datetime
                 stop time
     region_id : string
                 id of region
     
     Returns
     -------
     xarray.DataArray with stacked grid points of specified variable filtered by region map
     """
     try:
         contained = np.load(os.path.join(isin_path, 'isinDE.npy'))
     except:
         log.info(f'isin file not found in {data_path}')
         contained = self.util.check_isin_region(region_id)
     return self.wdata[name].sel(time=slice(start, stop)).where(contained, other=np.nan, drop=False)\
                .stack(loc=(lon_col, lat_col)).dropna('loc').transpose()
Exemple #2
0
    def plot_isin_region(self, region_id):
        """Plot map showing which grid points are within specified region

        Parameters
        ----------
        region_id : string
                    the id of the region as string

        Returns
        -------
        None
        """
        util = Utility()
        try:
            contained = np.load(os.path.join(isin_path,
                                             f'isin{region_id}.npy'))
        except:
            log.info(
                f'isin file not found in {isin_path} for region {region_id}')
            contained = util.check_isin_region(region_id)

        fig, ax = plt.subplots()
        ax.imshow(contained, cmap=plt.cm.Greys, extent=bbox)

        ax.set_ylabel(lat_col)
        ax.set_xlabel(lon_col)

        file_name = os.path.join(
            figure_path, f'isin{util.get_region_name(region_id)}_{region_id}')
        self.__save_show_fig(fig, figure_path, file_name)
Exemple #3
0
    def reduce_lonlat_slice(self, name, func, start, stop):
        """Return data for specified variable
        
        Date is filtered by time slice and function is applied
        to reduce it along longitude and latitude axes
           
        Parameters
        ----------
        name  : string
                name of the variable
        func  : numpy function
                a numpy function
        start : datetime.datetime
                start time for values to be returned
        stop  : datetime.datetime
                stop time for values to be returned
        
        Returns
        -------
        xarray.DataArray with values reduced along longitude and latitude for time slice
        """
        assert name in self.var_names, f'column {name} not found'

        data = self.wdata[name].sel(time=slice(start, stop))

        if self.isin:
            try:
                contained = np.load(os.path.join(isin_path, 'isinDE.npy'))
            except:
                log.info(f'isin file not found in {data_path}, creating new')
                contained = self.util.check_isinDE()
            data = data.where(contained, other=np.nan, drop=False)
        return data.reduce(func, dim=[lon_col, lat_col])
Exemple #4
0
    def reduce_lonlat(self, name, func):
        """Return data for specified variable after applying function to reduce along longitude and latitude axes
           
        Parameters
        ----------
        name  : string
                name of the variable
        func  : numpy function
                a numpy function
        
        Returns
        -------
        xarray.DataArray with values reduced along longitude and latitude
        """
        assert name in self.var_names, f'column {name} not found'

        if self.isin:
            try:
                contained = np.load(os.path.join(isin_path, 'isinDE.npy'))
            except:
                log.info(f'isin file not found in {data_path}, creating new')
                contained = self.util.check_isinDE()
            return self.wdata[name].where(contained, other=np.nan, drop=False)\
                                   .reduce(func, dim=[lon_col, lat_col])
        else:
            return self.wdata[name].reduce(func, dim=[lon_col, lat_col])
Exemple #5
0
 def summary(self):
     """Prints summary
     
     Returns
     -------
     None
     """
     log.info(f'\n{self.__armax_result__.summary()}')
Exemple #6
0
 def save(self):
     """Stores instance to pickle
     
     Returns
     -------
     file name of stored instance as string
     """
     dir_pth = os.path.join(data_path, 'ARMAX')
     if not os.path.exists(dir_pth):
         os.mkdir(dir_pth)
     fname = os.path.join(dir_pth,
                          f'start{self.start.strftime("%Y%m%d%H")}_stop'\
                          f'{self.stop.strftime("%Y%m%d%H")}_p{self.p}q{self.q}'\
                          f'{"" if self.exog is None else "_"+"_".join(self.exog)}.pkl')
     log.info(f'saving armax as {fname}')
     pickle.dump(self, open(fname, 'wb'))
     return fname
Exemple #7
0
    def vals4time(self, name, datetime, isin=False):
        """Returns the values for specified variable and time
        
        Parameters
        ----------
        name     : string
                   name of variable in nc file that should be contained in df
        datetime : datetime.datetime
                   the specified datetime for which the data is returned
        isin     : bool
                   whether to only get points within germany or all
        
        Returns
        -------
        xarray.DataArray :
            2D data with values for variable over latitude and longitude
            long name of variable
        
        ready for plotting with imshow
        """
        assert name in self.var_names, f'column {name} not found'

        # filter data by name and time
        data = self.wdata[name].sel(time=datetime)

        if isin:
            try:
                contained = np.load(os.path.join(isin_path, 'isinDE.npy'))
            except:
                log.info(f'isin file not found in {data_path}')
                contained = self.util.check_isinDE()
            data = data.where(contained, other=np.nan, drop=False)

        # update DataArray attributes to add min and max values for complete time
        data.attrs.update({
            'vmin': np.floor(self.wdata[name].min().values),
            'vmax': np.ceil(self.wdata[name].max().values)
        })

        return data
Exemple #8
0
    def plot_isin(self):
        """Plot map showing which grid points are within germany
        
        Returns
        -------
        None
        """
        try:
            contained = np.load(os.path.join(isin_path, 'isinDE.npy'))
        except:
            log.info(f'isin file not found in {data_path}')
            util = Utility()
            contained = util.check_isinDE()

        fig, ax = plt.subplots()
        ax.imshow(contained, cmap=plt.cm.Greys, extent=bbox)

        ax.set_ylabel(lat_col)
        ax.set_xlabel(lon_col)

        file_name = os.path.join(figure_path, 'isinDE')
        self.__save_show_fig(fig, figure_path, file_name)
Exemple #9
0
 def __save_show_fig(self, fig, dir_pth, file_name):
     """Save and show the passed figure if specified and finally close it
     
     Parameters
     ----------
     fig       : matplotlib.figure.Figure
                 the created figure
     dir_pth   : string
                 the path to the directory to save to
     file_name : string
                 the file name to save to
     
     Returns
     -------
     None
     """
     if self.save:
         log.info(f'saving plot in {file_name}')
         if not os.path.exists(dir_pth):
             os.makedirs(dir_pth)
         if type(self.fmt) is list:
             for f in self.fmt:
                 fig.savefig(f'{file_name}.{f}',
                             bbox_inches='tight',
                             format=f,
                             optimize=True,
                             dpi=150)
         else:
             fig.savefig(f'{file_name}.{self.fmt}',
                         bbox_inches='tight',
                         format=self.fmt,
                         optimize=True,
                         dpi=150)
     if self.show:
         plt.show()
     # close figures as they won't be closed automatically by python during runtime
     plt.close(fig)
Exemple #10
0
    def check_isin_region(self, region_id):
        """Computes which points are within specified region
        
        Parameters
        ----------
        region_id : string
                    id of a region
        
        Returns
        -------
        2D numpy.ndarray containing booleans specifying whether point lies within region or not
        """
        region_poly = None
        # try to find desired region
        region_poly = Polygon(self.get_region(region_id).shape.points)
        if region_poly is None:
            raise Exception(
                f'shape for region {region_id} could not be found!')
        # create 2D numpy.ndarray of points to ease vectorized computation
        coords = np.empty((len(self.lats), len(self.lons)), np.dtype(Point))
        for y in range(len(self.lats)):
            for x in range(len(self.lons)):
                lo = self.lons[x]
                la = self.lats[y]
                coords[y, x] = Point(lo, la)
        # checks for each point whether it is within the polygon of the desired region
        contains = np.vectorize(lambda point: point.within(region_poly) or
                                point.touches(region_poly))
        contained = contains(coords)
        if not os.path.exists(isin_path):
            os.mkdir(isin_path)
        filename = os.path.join(isin_path, f'isin{region_id}')
        log.info(f'isin saved as {filename}')
        np.save(filename, contained)

        return contained
Exemple #11
0
    def plot_armax_forecast(self,
                            tstart,
                            tstop,
                            forecast_end,
                            p,
                            q,
                            exog=None,
                            save_armax=False,
                            plot_range=None):
        """Plot an ARMAX forecast for the given parameters
        
        Parameters
        ----------
        tstart       : datetime.datetime
                       start time
        tstop        : datetime.datetime
                       stop time
        forecast_end : datetime.datetime
                       stop time of forecast (=tstop + forecast length)
        p            : integer
                       specifies number of AR coefficients
        q            : integer
                       specifies number of MA coefficients
        exog         : list of strings
                       specifies the variables to include as exogenous variables
        save_armax   : bool
                       specifies whether to save the armax or not
        plot_range   : None or tuple of datetime
                       specifies wether to plot specific range of forecast
        
        Returns
        -------
        None
        """
        armax = ARMAXForecast(tstart, tstop, p, q, exog=exog, const=False)
        armax.train()
        armax.summary()
        if save_armax:
            armax.save()
        forecast = armax.predict_one_step_ahead(forecast_end)
        fig, ax = plt.subplots()
        # for i,hours in enumerate(hours_range):
        #     ax.plot(fc_range,forecast[i],label=f'{hours}H forecast')
        #     log.info(armax.forecasts[i])
        log.info(armax.forecasts[0])

        if plot_range is None:
            fc_range = pd.date_range(tstop + timedelta(hours=1),
                                     forecast_end,
                                     freq='1H')
            ax.plot(fc_range, forecast.forecast, label='1H forecast')
            ax.plot(fc_range, forecast.actual, label='actual value')
        else:
            fc_range = pd.date_range(plot_range[0], plot_range[1], freq='1H')
            df = forecast.sel(plot_range[0], plot_range[1])
            ax.plot(fc_range, df['forecast'].values, label='1H forecast')
            ax.plot(fc_range, df['actual'].values, label='actual value')

        ax.set_ylabel('load [MW]')
        plt.setp(ax.get_xticklabels(),
                 rotation=30,
                 horizontalalignment='right')
        plt.legend()

        dir_pth = os.path.join(figure_path, 'ARMAXfc')
        file_name = os.path.join(
            dir_pth,
            f'ARMAX_p{p}q{q}_data{tstart.year}to{tstop.year}_fcto{forecast_end.strftime("%Y%m%d%H")}'
            f'{"" if exog is None else "_" + exog[0] if len(exog) == 1 else "_" + "_".join(exog)}'
            f'{"" if plot_range is None else "_plot_range" + plot_range[0].strftime("%Y%m%d%H")}'
            f'{"" if plot_range is None else "_" + plot_range[1].strftime("%Y%m%d%H")}'
        )
        self.__save_show_fig(fig, dir_pth, file_name)