def __init__(self, fname, band=1, isNotebook=True):
        """
        :param ts: TATSSI qa_analytics object
        """
        # Clear cell
        clear_output()

        # Time series object
        self.ts = Analysis(fname=fname)

        self.isNotebook = isNotebook
        if self.isNotebook is True:
            # Smoothing methods
            # set in __fill_smoothing_method
            self.smoothing_methods = None

            # Display controls
            self.__display_controls()

        # Create plot objects
        self.__create_plot_objects()
        # Create plot
        self.__plot(band)

        # Disable RasterIO logging, just show ERRORS
        log = rio_logging.getLogger()
        log.setLevel(rio_logging.ERROR)
Example #2
0
    def __init__(self, fname, cmap='YlGn', isNotebook=True):
        """
        :param ts: TATSSI file time series
        """
        # Clear cell
        clear_output()

        # Time series object
        self.ts = Analysis(fname=fname)

        # Data variables
        # set in __fill_data_variables
        self.data_vars = None

        self.isNotebook = isNotebook
        if self.isNotebook is True:
            # Display controls
            self.__display_controls()

        # Default colormap
        self.cmap = cmap
        # Create plot objects
        self.__create_plot_objects()
        # Create plot
        self.__plot()

        # Disable RasterIO logging, just show ERRORS
        log = rio_logging.getLogger()
        log.setLevel(rio_logging.ERROR)

        self.cpt = importr('changepoint')
Example #3
0
    def __init__(self):
        # Create figure
        self.fig, self.ax = plt.subplots(figsize=(9.5,11))

        # Disable RasterIO logging, just show ERRORS
        log = rio_logging.getLogger()
        log.setLevel(rio_logging.ERROR)
Example #4
0
    def __get_dataset(self):
        """
        Load all layers from a GDAL compatible file into an xarray
        """
        # Disable RasterIO logging, just show ERRORS
        log = rio_logging.getLogger()
        log.setLevel(rio_logging.ERROR)

        try:
            # Get dataset
            tmp_ds = xr.open_rasterio(self.fname)
            tmp_ds = None
            del tmp_ds
        except rio.errors.RasterioIOError as e:
            raise e

        chunks = get_chunk_size(self.fname)
        data_array = xr.open_rasterio(self.fname, chunks=chunks)

        data_array = data_array.rename({
            'x': 'longitude',
            'y': 'latitude',
            'band': 'time'
        })

        # Check if file is a VRT
        name, extension = os.path.splitext(self.fname)
        if extension.lower() == '.vrt':
            times = get_times(self.fname)
        else:
            times = get_times_from_file_band(self.fname)
        data_array['time'] = times

        # Check that _FillValue is not NaN
        if data_array.nodatavals[0] is np.NaN:
            # Use _FillValue from band metadata
            _fill_value = get_fill_value_band_metadata(self.fname)

            data_array.attrs['nodatavals'] = \
                    tuple(np.full((len(data_array.nodatavals))
                        ,_fill_value))

        # Create new dataset
        self.dataset_name = self.__get_dataset_name()
        dataset = data_array.to_dataset(name=self.dataset_name)

        # Back to default logging settings
        logging.basicConfig(level=logging.INFO)

        # Set self.data
        self.data = dataset
Example #5
0
    def __init__(self, qa_analytics, isNotebook=True):
        """
        :param ts: TATSSI qa_analytics object
        """
        # Time series object
        self.ts = qa_analytics.ts
        # Source dir
        self.source_dir = qa_analytics.source_dir
        # Product
        self.product = qa_analytics.product
        self.version = qa_analytics.version

        # Mask
        self.mask = qa_analytics.mask

        # Data variables
        # set in __fill_data_variables
        self.data_vars = None

        # Interpolation methods
        # set in __fill_interpolation_method
        self.interpolation_methods = None

        self.isNotebook = isNotebook
        if self.isNotebook is True:
            # Clear cell
            clear_output()

            # Display controls
            self.__display_controls()

            # Create plot objects
            self.__create_plot_objects()
            # Create plot
            self.__plot()
        else:
            self.selected_data_var = \
                    qa_analytics.selected_data_var
            self.selected_interpolation_method = \
                    qa_analytics.selected_interpolation_method

        # Disable RasterIO logging, just show ERRORS
        log = rio_logging.getLogger()
        log.setLevel(rio_logging.ERROR)
Example #6
0
    def ClippedByPolygon(self, polygon_shapefile):
        """
		return all pixel values within a given polygon shapefile.
		according to
		https://gis.stackexchange.com/questions/260304/extract-raster-values-within-shapefile-with-pygeoprocessing-or-gdal
		"""
        from rasterio import logging
        log = logging.getLogger()
        log.setLevel(logging.ERROR)

        import rasterio
        from rasterio.mask import mask
        import geopandas as gpd
        from shapely.geometry import mapping

        shapefile = gpd.read_file(polygon_shapefile)
        geoms = shapefile.geometry.values
        # geometry = geoms[0] # shapely geometry
        # geoms = [mapping(geoms[0])] # transform to GeJSON format
        geoms = [mapping(geoms[i]) for i in range(len(geoms))]
        with rasterio.open(self.fpath) as src:
            out_image, out_transform = mask(src,
                                            geoms,
                                            crop=True,
                                            nodata=-9999.0)
            # The out_image result is a Numpy masked array
            # no_data = src.nodata
            # if no_data is None:
            # 	no_data = 0.0
            nodata = -9999.0
        # print(out_image)
        # print(out_image.data.shape)
        # print(out_image.data)
        try:
            clipped_data = out_image.data[0]
        except NotImplementedError:
            clipped_data = out_image[0]
        # PROBABLY HAVE TO CHANGE TO out_image[0] HERE
        # extract the valid values
        # and return them as a numpy 1-D array
        # return np.extract(clipped_data != nodata, clipped_data)
        return clipped_data
Example #7
0
    def __init__(self, fname, parent=None):
        super(TimeSeriesAnalysisUI, self).__init__(parent)
        uic.loadUi('time_series_analysis.ui', self)
        self.parent = parent

        # List of dialogs
        self.dialogs = list()

        # Set input file name
        self.fname = fname

        # Disable RasterIO logging, just show ERRORS
        log = rio_logging.getLogger()
        log.setLevel(rio_logging.ERROR)

        # Change point R library
        self.cpt = importr('changepoint')

        # Plot input data
        self._plot()

        self.show()
Example #8
0
    def __get_datasets(self, vrt_fnames, level=0, chunked=False):
        """
        Load all VRTs from vrt_fnames list into a xarray dataset
        """
        # Disable RasterIO logging, just show ERRORS
        log = rio_logging.getLogger()
        log.setLevel(rio_logging.ERROR)

        datasets = None
        subdataset_name = None
        times = None
        _fill_value = None

        for vrt in vrt_fnames:
            # Read each VRT file
            if chunked == True:
                chunks = get_chunk_size(vrt)
                data_array = xr.open_rasterio(vrt, chunks=chunks)
            else:
                data_array = xr.open_rasterio(vrt)

            data_array = data_array.rename({
                'x': 'longitude',
                'y': 'latitude',
                'band': 'time'
            })

            # Extract time from metadata
            if times is None:
                times = get_times(vrt)
            data_array['time'] = times

            dataset_name = Path(vrt).parents[0].name
            if level == 0:
                # Standard layer has an _ prefix
                dataset_name = f"_{dataset_name}"

            # Check that _FillValue is not NaN
            if data_array.nodatavals[0] is np.NaN:
                # Use _FillValue from VRT firts band metadata
                if _fill_value is None:
                    _fill_value = get_fill_value_band_metadata(vrt)

                data_array.attrs['nodatavals'] = \
                        tuple(np.full((len(data_array.nodatavals))
                            ,_fill_value))

            if datasets is None:
                # Create new dataset
                datasets = data_array.to_dataset(name=dataset_name)
            else:
                # Merge with existing dataset
                tmp_dataset = data_array.to_dataset(name=dataset_name)

                datasets = datasets.merge(tmp_dataset)
                tmp_dataset = None
                subdataset_name = None

        # Back to default logging settings
        logging.basicConfig(level=logging.INFO)

        # If a specific temporal subset is requested, create subset
        if self.year is not None:
            time_slice = slice(f'{self.year-1}-11-29', f'{self.year+1}-02-01')
            datasets = datasets.sel(time=time_slice)
        elif self.start is not None and self.end is not None:
            time_slice = slice(f'{self.start}', f'{self.end}')
            datasets = datasets.sel(time=time_slice)

        return datasets
Example #9
0
"""

import xarray as xr
import numpy as np
from keras.models import load_model
import keras
import sys
import math
from osgeo import gdal
from osgeo import osr

from utils import data_utils as du
from unet import unet_builder

from rasterio import logging
log = logging.getLogger()
log.setLevel(logging.ERROR)
np.set_printoptions(suppress=True)


def normalize_channel(channel_values):
    """Normalize one channel """
    chan_mean = np.mean(channel_values)
    chan_std = np.std(channel_values)
    normalized = (channel_values - chan_mean) / (chan_std + 1e-8)
    return normalized


def generate_padding(stack, width=256, height=256):
    """Pads array to get it into suitable shape"""
    x_pad = (math.ceil(stack.shape[2] / width) * width - stack.shape[2]) // 2