Module eoreader.products.sar.s1_product

Sentinel-1 products

Expand source code
# -*- coding: utf-8 -*-
# Copyright 2021, SERTIT-ICube - France, https://sertit.unistra.fr/
# This file is part of eoreader project
#     https://github.com/sertit/eoreader
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" Sentinel-1 products """
import glob
import logging
import os
import re
import tempfile
import warnings
import zipfile
from datetime import datetime
from enum import unique
from typing import Union

import geopandas as gpd
import rasterio
from lxml import etree

from eoreader.exceptions import InvalidProductError, InvalidTypeError
from eoreader.products.sar.sar_product import SarProduct
from eoreader.utils import DATETIME_FMT, EOREADER_NAME
from sertit import files, misc, strings, vectors
from sertit.misc import ListEnum

LOGGER = logging.getLogger(EOREADER_NAME)

# Disable georef warnings here as the SAR products are not georeferenced
warnings.filterwarnings("ignore", category=rasterio.errors.NotGeoreferencedWarning)


@unique
class S1ProductType(ListEnum):
    """
    S1 products types. Take a look here:
    https://earth.esa.int/web/sentinel/missions/sentinel-1/data-products
    """

    RAW = "RAW"
    """Raw products (lvl 0): **not used by EOReader**"""

    SLC = "SLC"
    """Single Look Complex (SLC, lvl 1)"""
    GRD = "GRD"
    """Ground Range Detected (GRD, lvl 1, phase lost)"""
    OCN = "OCN"
    """Ocean products (lvl 2): **not used by EOReader**"""


@unique
class S1SensorMode(ListEnum):
    """
    S1 sensor mode. Take a look here:
    https://earth.esa.int/web/sentinel/user-guides/sentinel-1-sar/acquisition-modes

    The primary conflict-free modes are IW, with VV+VH polarisation over land,
    and WV, with VV polarisation, over open ocean.
    EW mode is primarily used for wide area coastal monitoring including ship traffic, oil spill and sea-ice monitoring.
    SM mode is only used for small islands and on request for extraordinary events such as emergency management.
    """

    SM = "SM"
    """Stripmap (SM)"""

    IW = "IW"
    """Interferometric Wide swath (IW)"""

    EW = "EW"
    """Extra-Wide swath (EW)"""

    WV = "WV"
    """Wave (WV) -> single polarisation only (HH or VV)"""


class S1Product(SarProduct):
    """
    Class for Sentinel-1 Products

    You can use directly the .zip file
    """

    def _set_resolution(self) -> float:
        """
        Set product default resolution (in meters)

        .. WARNING:: We assume being in High Resolution (except for WV where we must be in medium resolution)
        """
        def_res = None

        # Read metadata
        try:
            root, _ = self.read_mtd()

            for element in root:
                if element.tag == "imageAnnotation":
                    image_info = element.find("imageInformation")
                    def_res = float(image_info.findtext("rangePixelSpacing"))
                    break
        except (InvalidProductError, AttributeError):
            pass

        # If we cannot read it in MTD, initiate survival mode
        if not def_res:
            if self.sensor_mode in [S1SensorMode.SM, S1SensorMode.IW]:
                def_res = 10.0
            elif self.sensor_mode in [S1SensorMode.EW, S1SensorMode.WV]:
                def_res = 25.0
            else:
                raise InvalidTypeError(f"Unknown sensor mode {self.sensor_mode}")

            LOGGER.debug(
                f"Default resolution is set to {def_res}. "
                f"The product is considered being in "
                f"{'Medium' if self.sensor_mode == S1SensorMode.WV else 'High'}-Resolution"
            )

        return def_res

    def _post_init(self) -> None:
        """
        Function used to post_init the products
        (setting product-type, band names and so on)
        """
        # Private attributes
        self._raw_band_regex = "*-{!l}-*.tiff"  # Just get the SLC-iw1 image for now

        self._band_folder = os.path.join(self.path, "measurement")
        self._snap_path = self.path

        # Zipped and SNAP can process its archive
        self.needs_extraction = False

        # Post init done by the super class
        super()._post_init()

    def wgs84_extent(self) -> gpd.GeoDataFrame:
        """
        Get the WGS84 extent of the file before any reprojection.
        This is useful when the SAR pre-process has not been done yet.

        ```python
        >>> from eoreader.reader import Reader
        >>> path = r"S1A_IW_GRDH_1SDV_20191215T060906_20191215T060931_030355_0378F7_3696.zip"
        >>> prod = Reader().open(path)
        >>> prod.wgs84_extent()
                               Name  ...                                           geometry
        0  Sentinel-1 Image Overlay  ...  POLYGON ((0.85336 42.24660, -2.32032 42.65493,...
        [1 rows x 12 columns]
        ```

        Returns:
            gpd.GeoDataFrame: WGS84 extent as a gpd.GeoDataFrame

        """
        tmp_dir = tempfile.TemporaryDirectory()

        try:
            # Open the map-overlay file
            if self.is_archived:
                # We need to extract the file here as we need a proper file
                with zipfile.ZipFile(self.path, "r") as zip_ds:
                    filenames = [f.filename for f in zip_ds.filelist]
                    regex = re.compile(".*preview.*map-overlay.kml")
                    preview_overlay = zip_ds.extract(
                        list(filter(regex.match, filenames))[0], tmp_dir.name
                    )
            else:
                preview_overlay = os.path.join(self.path, "preview", "map-overlay.kml")

            if os.path.isfile(preview_overlay):
                # Open the KML file
                vectors.set_kml_driver()
                extent_wgs84 = gpd.read_file(preview_overlay)

                if extent_wgs84.empty:
                    # Convert KML to GeoJSON
                    gj_preview_overlay = preview_overlay.replace("kml", "geojson")
                    cmd_line = [
                        "ogr2ogr",
                        "-fieldTypeToString DateTime",  # Disable warning
                        "-f GeoJSON",
                        strings.to_cmd_string(gj_preview_overlay),
                        strings.to_cmd_string(preview_overlay),
                    ]
                    misc.run_cli(cmd_line)

                    # Open the geojson
                    extent_wgs84 = gpd.read_file(gj_preview_overlay)

                    if extent_wgs84.empty:
                        raise InvalidProductError(
                            f"Cannot determine the WGS84 extent of {self.name}"
                        )
            else:
                raise InvalidProductError(
                    f"Impossible to find the map-overlay.kml in {self.path}"
                )

        except Exception as ex:
            raise InvalidProductError(ex) from ex

        finally:
            tmp_dir.cleanup()

        return extent_wgs84

    def _set_product_type(self) -> None:
        """Get products type"""
        self._get_sar_product_type(
            prod_type_pos=2, gdrg_types=S1ProductType.GRD, cplx_types=S1ProductType.SLC
        )

    def _set_sensor_mode(self) -> None:
        """
        Get products type from S1 products name (could check the metadata too)
        """
        sensor_mode_name = self.split_name[1]

        # Get sensor mode
        for sens_mode in S1SensorMode:
            if sens_mode.value in sensor_mode_name:
                self.sensor_mode = sens_mode

        # Discard invalid sensor mode
        if self.sensor_mode != S1SensorMode.IW:
            raise NotImplementedError(
                f"For now, only IW sensor mode is used in EOReader processes: {self.name}"
            )
        if not self.sensor_mode:
            raise InvalidProductError(
                f"Invalid {self.platform.value} name: {self.name}"
            )

    def get_datetime(self, as_datetime: bool = False) -> Union[str, datetime]:
        """
        Get the product's acquisition datetime, with format `YYYYMMDDTHHMMSS` <-> `%Y%m%dT%H%M%S`

        ```python
        >>> from eoreader.reader import Reader
        >>> path = r"S1A_IW_GRDH_1SDV_20191215T060906_20191215T060931_030355_0378F7_3696.zip"
        >>> prod = Reader().open(path)
        >>> prod.get_datetime(as_datetime=True)
        datetime.datetime(2019, 12, 15, 6, 9, 6)
        >>> prod.get_datetime(as_datetime=False)
        '20191215T060906'
        ```

        Args:
            as_datetime (bool): Return the date as a datetime.datetime. If false, returns a string.

        Returns:
             Union[str, datetime.datetime]: Its acquisition datetime
        """
        date = self.split_name[4]

        if as_datetime:
            date = datetime.strptime(date, DATETIME_FMT)

        return date

    def read_mtd(self) -> (etree._Element, str):
        """
        Read metadata and outputs the metadata XML root and its namespace

        ```python
        >>> from eoreader.reader import Reader
        >>> path = r"S1A_IW_GRDH_1SDV_20191215T060906_20191215T060931_030355_0378F7_3696.zip"
        >>> prod = Reader().open(path)
        >>> prod.read_mtd()
        (<Element product at 0x1832895d788>, '')
        ```

        Returns:
            (etree._Element, str): Metadata XML root and its namespace
        """
        # Get MTD XML file
        if self.is_archived:
            root = files.read_archived_xml(self.path, ".*annotation.*\.xml")
        else:
            # Open metadata file
            try:
                mtd_file = glob.glob(os.path.join(self.path, "annotation", "*.xml"))[0]

                # pylint: disable=I1101:
                # Module 'lxml.etree' has no 'parse' member, but source is unavailable.
                xml_tree = etree.parse(mtd_file)
                root = xml_tree.getroot()
            except IndexError as ex:
                raise InvalidProductError(
                    f"Metadata file (product.xml) not found in {self.path}"
                ) from ex

        # Get namespace
        namespace = ""

        return root, namespace

Classes

class S1ProductType (value, names=None, *, module=None, qualname=None, type=None, start=1)
Expand source code
class S1ProductType(ListEnum):
    """
    S1 products types. Take a look here:
    https://earth.esa.int/web/sentinel/missions/sentinel-1/data-products
    """

    RAW = "RAW"
    """Raw products (lvl 0): **not used by EOReader**"""

    SLC = "SLC"
    """Single Look Complex (SLC, lvl 1)"""
    GRD = "GRD"
    """Ground Range Detected (GRD, lvl 1, phase lost)"""
    OCN = "OCN"
    """Ocean products (lvl 2): **not used by EOReader**"""

Ancestors

  • sertit.misc.ListEnum
  • enum.Enum

Class variables

var RAW

Raw products (lvl 0): not used by EOReader

var SLC

Single Look Complex (SLC, lvl 1)

var GRD

Ground Range Detected (GRD, lvl 1, phase lost)

var OCN

Ocean products (lvl 2): not used by EOReader

class S1SensorMode (value, names=None, *, module=None, qualname=None, type=None, start=1)

S1 sensor mode. Take a look here: https://earth.esa.int/web/sentinel/user-guides/sentinel-1-sar/acquisition-modes

The primary conflict-free modes are IW, with VV+VH polarisation over land, and WV, with VV polarisation, over open ocean. EW mode is primarily used for wide area coastal monitoring including ship traffic, oil spill and sea-ice monitoring. SM mode is only used for small islands and on request for extraordinary events such as emergency management.

Expand source code
class S1SensorMode(ListEnum):
    """
    S1 sensor mode. Take a look here:
    https://earth.esa.int/web/sentinel/user-guides/sentinel-1-sar/acquisition-modes

    The primary conflict-free modes are IW, with VV+VH polarisation over land,
    and WV, with VV polarisation, over open ocean.
    EW mode is primarily used for wide area coastal monitoring including ship traffic, oil spill and sea-ice monitoring.
    SM mode is only used for small islands and on request for extraordinary events such as emergency management.
    """

    SM = "SM"
    """Stripmap (SM)"""

    IW = "IW"
    """Interferometric Wide swath (IW)"""

    EW = "EW"
    """Extra-Wide swath (EW)"""

    WV = "WV"
    """Wave (WV) -> single polarisation only (HH or VV)"""

Ancestors

  • sertit.misc.ListEnum
  • enum.Enum

Class variables

var SM

Stripmap (SM)

var IW

Interferometric Wide swath (IW)

var EW

Extra-Wide swath (EW)

var WV

Wave (WV) -> single polarisation only (HH or VV)

class S1Product (product_path, archive_path=None, output_path=None)

Class for Sentinel-1 Products

You can use directly the .zip file

Expand source code
class S1Product(SarProduct):
    """
    Class for Sentinel-1 Products

    You can use directly the .zip file
    """

    def _set_resolution(self) -> float:
        """
        Set product default resolution (in meters)

        .. WARNING:: We assume being in High Resolution (except for WV where we must be in medium resolution)
        """
        def_res = None

        # Read metadata
        try:
            root, _ = self.read_mtd()

            for element in root:
                if element.tag == "imageAnnotation":
                    image_info = element.find("imageInformation")
                    def_res = float(image_info.findtext("rangePixelSpacing"))
                    break
        except (InvalidProductError, AttributeError):
            pass

        # If we cannot read it in MTD, initiate survival mode
        if not def_res:
            if self.sensor_mode in [S1SensorMode.SM, S1SensorMode.IW]:
                def_res = 10.0
            elif self.sensor_mode in [S1SensorMode.EW, S1SensorMode.WV]:
                def_res = 25.0
            else:
                raise InvalidTypeError(f"Unknown sensor mode {self.sensor_mode}")

            LOGGER.debug(
                f"Default resolution is set to {def_res}. "
                f"The product is considered being in "
                f"{'Medium' if self.sensor_mode == S1SensorMode.WV else 'High'}-Resolution"
            )

        return def_res

    def _post_init(self) -> None:
        """
        Function used to post_init the products
        (setting product-type, band names and so on)
        """
        # Private attributes
        self._raw_band_regex = "*-{!l}-*.tiff"  # Just get the SLC-iw1 image for now

        self._band_folder = os.path.join(self.path, "measurement")
        self._snap_path = self.path

        # Zipped and SNAP can process its archive
        self.needs_extraction = False

        # Post init done by the super class
        super()._post_init()

    def wgs84_extent(self) -> gpd.GeoDataFrame:
        """
        Get the WGS84 extent of the file before any reprojection.
        This is useful when the SAR pre-process has not been done yet.

        ```python
        >>> from eoreader.reader import Reader
        >>> path = r"S1A_IW_GRDH_1SDV_20191215T060906_20191215T060931_030355_0378F7_3696.zip"
        >>> prod = Reader().open(path)
        >>> prod.wgs84_extent()
                               Name  ...                                           geometry
        0  Sentinel-1 Image Overlay  ...  POLYGON ((0.85336 42.24660, -2.32032 42.65493,...
        [1 rows x 12 columns]
        ```

        Returns:
            gpd.GeoDataFrame: WGS84 extent as a gpd.GeoDataFrame

        """
        tmp_dir = tempfile.TemporaryDirectory()

        try:
            # Open the map-overlay file
            if self.is_archived:
                # We need to extract the file here as we need a proper file
                with zipfile.ZipFile(self.path, "r") as zip_ds:
                    filenames = [f.filename for f in zip_ds.filelist]
                    regex = re.compile(".*preview.*map-overlay.kml")
                    preview_overlay = zip_ds.extract(
                        list(filter(regex.match, filenames))[0], tmp_dir.name
                    )
            else:
                preview_overlay = os.path.join(self.path, "preview", "map-overlay.kml")

            if os.path.isfile(preview_overlay):
                # Open the KML file
                vectors.set_kml_driver()
                extent_wgs84 = gpd.read_file(preview_overlay)

                if extent_wgs84.empty:
                    # Convert KML to GeoJSON
                    gj_preview_overlay = preview_overlay.replace("kml", "geojson")
                    cmd_line = [
                        "ogr2ogr",
                        "-fieldTypeToString DateTime",  # Disable warning
                        "-f GeoJSON",
                        strings.to_cmd_string(gj_preview_overlay),
                        strings.to_cmd_string(preview_overlay),
                    ]
                    misc.run_cli(cmd_line)

                    # Open the geojson
                    extent_wgs84 = gpd.read_file(gj_preview_overlay)

                    if extent_wgs84.empty:
                        raise InvalidProductError(
                            f"Cannot determine the WGS84 extent of {self.name}"
                        )
            else:
                raise InvalidProductError(
                    f"Impossible to find the map-overlay.kml in {self.path}"
                )

        except Exception as ex:
            raise InvalidProductError(ex) from ex

        finally:
            tmp_dir.cleanup()

        return extent_wgs84

    def _set_product_type(self) -> None:
        """Get products type"""
        self._get_sar_product_type(
            prod_type_pos=2, gdrg_types=S1ProductType.GRD, cplx_types=S1ProductType.SLC
        )

    def _set_sensor_mode(self) -> None:
        """
        Get products type from S1 products name (could check the metadata too)
        """
        sensor_mode_name = self.split_name[1]

        # Get sensor mode
        for sens_mode in S1SensorMode:
            if sens_mode.value in sensor_mode_name:
                self.sensor_mode = sens_mode

        # Discard invalid sensor mode
        if self.sensor_mode != S1SensorMode.IW:
            raise NotImplementedError(
                f"For now, only IW sensor mode is used in EOReader processes: {self.name}"
            )
        if not self.sensor_mode:
            raise InvalidProductError(
                f"Invalid {self.platform.value} name: {self.name}"
            )

    def get_datetime(self, as_datetime: bool = False) -> Union[str, datetime]:
        """
        Get the product's acquisition datetime, with format `YYYYMMDDTHHMMSS` <-> `%Y%m%dT%H%M%S`

        ```python
        >>> from eoreader.reader import Reader
        >>> path = r"S1A_IW_GRDH_1SDV_20191215T060906_20191215T060931_030355_0378F7_3696.zip"
        >>> prod = Reader().open(path)
        >>> prod.get_datetime(as_datetime=True)
        datetime.datetime(2019, 12, 15, 6, 9, 6)
        >>> prod.get_datetime(as_datetime=False)
        '20191215T060906'
        ```

        Args:
            as_datetime (bool): Return the date as a datetime.datetime. If false, returns a string.

        Returns:
             Union[str, datetime.datetime]: Its acquisition datetime
        """
        date = self.split_name[4]

        if as_datetime:
            date = datetime.strptime(date, DATETIME_FMT)

        return date

    def read_mtd(self) -> (etree._Element, str):
        """
        Read metadata and outputs the metadata XML root and its namespace

        ```python
        >>> from eoreader.reader import Reader
        >>> path = r"S1A_IW_GRDH_1SDV_20191215T060906_20191215T060931_030355_0378F7_3696.zip"
        >>> prod = Reader().open(path)
        >>> prod.read_mtd()
        (<Element product at 0x1832895d788>, '')
        ```

        Returns:
            (etree._Element, str): Metadata XML root and its namespace
        """
        # Get MTD XML file
        if self.is_archived:
            root = files.read_archived_xml(self.path, ".*annotation.*\.xml")
        else:
            # Open metadata file
            try:
                mtd_file = glob.glob(os.path.join(self.path, "annotation", "*.xml"))[0]

                # pylint: disable=I1101:
                # Module 'lxml.etree' has no 'parse' member, but source is unavailable.
                xml_tree = etree.parse(mtd_file)
                root = xml_tree.getroot()
            except IndexError as ex:
                raise InvalidProductError(
                    f"Metadata file (product.xml) not found in {self.path}"
                ) from ex

        # Get namespace
        namespace = ""

        return root, namespace

Ancestors

Instance variables

var sar_prod_type

Inherited from: SarProduct.sar_prod_type

SAR product type, either Single Look Complex or Ground Range

var sensor_mode

Inherited from: SarProduct.sensor_mode

Sensor Mode of the current product

var pol_channels

Inherited from: SarProduct.pol_channels

Polarization Channels stored in the current product

var output

Inherited from: SarProduct.output

Output directory of the product, to write orthorectified data for example.

var name

Inherited from: SarProduct.name

Product name (its filename without any extension).

var split_name

Inherited from: SarProduct.split_name

Split name, to retrieve every information from its filename (dates, tile, product type…).

var archive_path

Inherited from: SarProduct.archive_path

Archive path, same as the product path if not specified. Useful when you want to know where both the extracted and archived version of your product …

var path

Inherited from: SarProduct.path

Usable path to the product, either extracted or archived path, according to the satellite.

var is_archived

Inherited from: SarProduct.is_archived

Is the archived product is processed (a products is considered as archived if its products path is a directory).

var needs_extraction

Inherited from: SarProduct.needs_extraction

Does this products needs to be extracted to be processed ? (True by default).

var date

Inherited from: SarProduct.date

Acquisition date.

var datetime

Inherited from: SarProduct.datetime

Acquisition datetime.

var tile_name

Inherited from: SarProduct.tile_name

Tile if possible (for data that can be piled, for example S2 and Landsats).

var sensor_type

Inherited from: SarProduct.sensor_type

Sensor type, SAR or optical.

var product_type

Inherited from: SarProduct.product_type

Product type, satellite-related field, such as L1C or L2A for Sentinel-2 data.

var band_names

Inherited from: SarProduct.band_names

Band mapping between band wrapping names such as GREEN and band real number such as 03 for Sentinel-2.

var is_reference

Inherited from: SarProduct.is_reference

If the product is a reference, used for algorithms that need pre and post data, such as fire detection.

var corresponding_ref

Inherited from: SarProduct.corresponding_ref

The corresponding reference products to the current one (if the product is not a reference but has a reference data corresponding to it). A list …

var nodata

Inherited from: SarProduct.nodata

Product nodata, set to 0 by default. Please do not touch this or all index will fail.

var platform

Inherited from: SarProduct.platform

Product platform, such as Sentinel-2

var resolution

Inherited from: SarProduct.resolution

Default resolution in meters of the current product. For SAR product, we use Ground Range resolution as we will automatically orthorectify the tiles.

var condensed_name

Inherited from: SarProduct.condensed_name

Condensed name, the filename with only useful data to keep the name unique (ie. 20191215T110441_S2_30TXP_L2A_122756). Used to shorten names and paths.

var sat_id

Inherited from: SarProduct.sat_id

Satellite ID, i.e. S2 for Sentinel-2

Methods

def wgs84_extent(

self)

Inherited from: SarProduct.wgs84_extent

Get the WGS84 extent of the file before any reprojection. This is useful when the SAR pre-process has not been done yet …

Expand source code
def wgs84_extent(self) -> gpd.GeoDataFrame:
    """
    Get the WGS84 extent of the file before any reprojection.
    This is useful when the SAR pre-process has not been done yet.

    ```python
    >>> from eoreader.reader import Reader
    >>> path = r"S1A_IW_GRDH_1SDV_20191215T060906_20191215T060931_030355_0378F7_3696.zip"
    >>> prod = Reader().open(path)
    >>> prod.wgs84_extent()
                           Name  ...                                           geometry
    0  Sentinel-1 Image Overlay  ...  POLYGON ((0.85336 42.24660, -2.32032 42.65493,...
    [1 rows x 12 columns]
    ```

    Returns:
        gpd.GeoDataFrame: WGS84 extent as a gpd.GeoDataFrame

    """
    tmp_dir = tempfile.TemporaryDirectory()

    try:
        # Open the map-overlay file
        if self.is_archived:
            # We need to extract the file here as we need a proper file
            with zipfile.ZipFile(self.path, "r") as zip_ds:
                filenames = [f.filename for f in zip_ds.filelist]
                regex = re.compile(".*preview.*map-overlay.kml")
                preview_overlay = zip_ds.extract(
                    list(filter(regex.match, filenames))[0], tmp_dir.name
                )
        else:
            preview_overlay = os.path.join(self.path, "preview", "map-overlay.kml")

        if os.path.isfile(preview_overlay):
            # Open the KML file
            vectors.set_kml_driver()
            extent_wgs84 = gpd.read_file(preview_overlay)

            if extent_wgs84.empty:
                # Convert KML to GeoJSON
                gj_preview_overlay = preview_overlay.replace("kml", "geojson")
                cmd_line = [
                    "ogr2ogr",
                    "-fieldTypeToString DateTime",  # Disable warning
                    "-f GeoJSON",
                    strings.to_cmd_string(gj_preview_overlay),
                    strings.to_cmd_string(preview_overlay),
                ]
                misc.run_cli(cmd_line)

                # Open the geojson
                extent_wgs84 = gpd.read_file(gj_preview_overlay)

                if extent_wgs84.empty:
                    raise InvalidProductError(
                        f"Cannot determine the WGS84 extent of {self.name}"
                    )
        else:
            raise InvalidProductError(
                f"Impossible to find the map-overlay.kml in {self.path}"
            )

    except Exception as ex:
        raise InvalidProductError(ex) from ex

    finally:
        tmp_dir.cleanup()

    return extent_wgs84

def get_datetime(

self,
as_datetime=False)

Get the product's acquisition datetime, with format YYYYMMDDTHHMMSS <-> %Y%m%dT%H%M%S

>>> from eoreader.reader import Reader
>>> path = r"S1A_IW_GRDH_1SDV_20191215T060906_20191215T060931_030355_0378F7_3696.zip"
>>> prod = Reader().open(path)
>>> prod.get_datetime(as_datetime=True)
datetime.datetime(2019, 12, 15, 6, 9, 6)
>>> prod.get_datetime(as_datetime=False)
'20191215T060906'

Args

as_datetime : bool
Return the date as a datetime.datetime. If false, returns a string.

Returns

Union[str, datetime.datetime]
Its acquisition datetime
Expand source code
def get_datetime(self, as_datetime: bool = False) -> Union[str, datetime]:
    """
    Get the product's acquisition datetime, with format `YYYYMMDDTHHMMSS` <-> `%Y%m%dT%H%M%S`

    ```python
    >>> from eoreader.reader import Reader
    >>> path = r"S1A_IW_GRDH_1SDV_20191215T060906_20191215T060931_030355_0378F7_3696.zip"
    >>> prod = Reader().open(path)
    >>> prod.get_datetime(as_datetime=True)
    datetime.datetime(2019, 12, 15, 6, 9, 6)
    >>> prod.get_datetime(as_datetime=False)
    '20191215T060906'
    ```

    Args:
        as_datetime (bool): Return the date as a datetime.datetime. If false, returns a string.

    Returns:
         Union[str, datetime.datetime]: Its acquisition datetime
    """
    date = self.split_name[4]

    if as_datetime:
        date = datetime.strptime(date, DATETIME_FMT)

    return date

def read_mtd(

self)

Read metadata and outputs the metadata XML root and its namespace

>>> from eoreader.reader import Reader
>>> path = r"S1A_IW_GRDH_1SDV_20191215T060906_20191215T060931_030355_0378F7_3696.zip"
>>> prod = Reader().open(path)
>>> prod.read_mtd()
(<Element product at 0x1832895d788>, '')

Returns

(etree._Element, str): Metadata XML root and its namespace

Expand source code
def read_mtd(self) -> (etree._Element, str):
    """
    Read metadata and outputs the metadata XML root and its namespace

    ```python
    >>> from eoreader.reader import Reader
    >>> path = r"S1A_IW_GRDH_1SDV_20191215T060906_20191215T060931_030355_0378F7_3696.zip"
    >>> prod = Reader().open(path)
    >>> prod.read_mtd()
    (<Element product at 0x1832895d788>, '')
    ```

    Returns:
        (etree._Element, str): Metadata XML root and its namespace
    """
    # Get MTD XML file
    if self.is_archived:
        root = files.read_archived_xml(self.path, ".*annotation.*\.xml")
    else:
        # Open metadata file
        try:
            mtd_file = glob.glob(os.path.join(self.path, "annotation", "*.xml"))[0]

            # pylint: disable=I1101:
            # Module 'lxml.etree' has no 'parse' member, but source is unavailable.
            xml_tree = etree.parse(mtd_file)
            root = xml_tree.getroot()
        except IndexError as ex:
            raise InvalidProductError(
                f"Metadata file (product.xml) not found in {self.path}"
            ) from ex

    # Get namespace
    namespace = ""

    return root, namespace

def get_default_band(

self)

Inherited from: SarProduct.get_default_band

Get default band: The first existing one between VV and HH for SAR data …

def get_default_band_path(

self)

Inherited from: SarProduct.get_default_band_path

Get default band path (the first existing one between VV and HH for SAR data), ready to use (orthorectified) …

def extent(

self)

Inherited from: SarProduct.extent

Get UTM extent of the tile …

def crs(

self)

Inherited from: SarProduct.crs

Get UTM projection …

def get_band_paths(

self,
band_list,
resolution=None)

Inherited from: SarProduct.get_band_paths

Return the paths of required bands …

def get_existing_band_paths(

self)

Inherited from: SarProduct.get_existing_band_paths

Return the existing orthorectified band paths (including despeckle bands) …

def get_existing_bands(

self)

Inherited from: SarProduct.get_existing_bands

Return the existing orthorectified bands (including despeckle bands) …

def footprint(

self)

Inherited from: SarProduct.footprint

Get UTM footprint of the products (without nodata, in french == emprise utile) …

def get_date(

self,
as_date=False)

Inherited from: SarProduct.get_date

Get the product's acquisition date …

def load(

self,
bands,
resolution=None,
size=None)

Inherited from: SarProduct.load

Open the bands and compute the wanted index …

def has_band(

self,
band)

Inherited from: SarProduct.has_band

Does this products has the specified band ? …

def stack(

self,
bands,
resolution=None,
stack_path=None,
save_as_int=False)

Inherited from: SarProduct.stack

Stack bands and index of a products …