Module eoreader.products.sar.rs2_product

RADARSAT-2 products. More info here.

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.
"""
RADARSAT-2 products.
More info [here](https://www.pcigeomatics.com/geomatica-help/references/gdb_r/RADARSAT-2.html#RADARSAT2__rs2_sfs).
"""
import difflib
import glob
import logging
import os
import warnings
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, vectors
from sertit.misc import ListEnum
from sertit.vectors import WGS84

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 Rs2ProductType(ListEnum):
    """
    RADARSAT-2 projection identifier.
    Take a look [here](https://www.pcigeomatics.com/geomatica-help/references/gdb_r/RADARSAT-2.html)
    """

    SLC = "SLC"
    """Single-look complex"""

    SGX = "SGX"
    """SAR georeferenced extra"""

    SGF = "SGF"
    """SAR georeferenced fine"""

    SCN = "SCN"
    """ScanSAR narrow beam"""

    SCW = "SCW"
    """ScanSAR wide beam"""

    SCF = "SCF"
    """ScanSAR fine"""

    SCS = "SCS"
    """ScanSAR sampled"""

    SSG = "SSG"
    """SAR systematic geocorrected"""

    SPG = "SPG"
    """SAR precision geocorrected"""


@unique
class Rs2SensorMode(ListEnum):
    """
    RADARSAT-2 sensor mode.
    Take a look [here](https://www.pcigeomatics.com/geomatica-help/references/gdb_r/RADARSAT-2.html)

    .. WARNING:: The name in the metadata may vary !
    """

    # Single Beam Modes
    S = "Standard"
    """Standard Mode"""

    W = "Wide"
    """Spotlight Mode"""

    F = "Fine"
    """Wide Mode"""

    WF = "Wide Fine"
    """Wide Fine Mode"""

    MF = "Multi-Look Fine"
    """Multi-Look Fine Mode"""

    WMF = "Wide Multi-Look Fine"
    """Wide Multi-Look Fine Mode"""

    XF = "Extra-Fine"
    """Extra-Fine Mode"""

    U = "Ultra-Fine"
    """Ultra-Fine Mode"""

    WU = "Wide Ultra-Fine"
    """Wide Ultra-Fine Mode"""

    EH = "Extended High"
    """Extended High Mode"""

    EL = "Extended Low"
    """Extended Low Mode"""

    SQ = "Standard Quad-Pol"
    """Standard Quad-Pol Mode"""

    WSQ = "Wide Standard Quad-Pol"
    """Wide Standard Quad-Pol Mode"""

    FQ = "Fine Quad-Pol"
    """Fine Quad-Pol Mode"""

    WFQ = "Wide Fine Quad-Pol"
    """Spotlight Mode"""

    # ScanSAR Modes
    SCN = "ScanSAR Narrow"
    """Spotlight Mode"""

    SCW = "ScanSAR Wide"
    """Spotlight Mode"""

    OSVN = "Ocean Surveillance"
    """Ocean Surveillance Mode"""

    DVWF = "Ship Detection"
    """Ship Detection Mode"""

    # Spotlight Mode
    SLA = "Spotlight"
    """Spotlight Mode"""


@unique
class Rs2Polarization(ListEnum):
    """
    RADARSAT-2 polarization mode.
    Take a look [here](https://www.pcigeomatics.com/geomatica-help/references/gdb_r/RADARSAT-2.html#RADARSAT2__rs2_sfs)
    """

    HH = "HH"
    VV = "VV"
    VH = "VH"
    HV = "HV"


class Rs2Product(SarProduct):
    """
    Class for RADARSAT-2 Products

    You can use directly the .zip file
    """

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

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

            for element in root:
                if element.tag == namespace + "imageAttributes":
                    raster_attr = element.find(namespace + "rasterAttributes")
                    def_res = float(
                        raster_attr.findtext(namespace + "sampledPixelSpacing")
                    )
                    break
        except (InvalidProductError, AttributeError):
            pass

        # If we cannot read it in MTD, initiate survival mode
        if not def_res:
            if self.sensor_mode == Rs2SensorMode.SLA:
                def_res = 1.0 if self.product_type == Rs2ProductType.SGX else 0.5
            elif self.sensor_mode in [Rs2SensorMode.U, Rs2SensorMode.WU]:
                def_res = 1.0 if self.product_type == Rs2ProductType.SGX else 1.56
            elif self.sensor_mode in [
                Rs2SensorMode.MF,
                Rs2SensorMode.WMF,
                Rs2SensorMode.F,
                Rs2SensorMode.WF,
            ]:
                def_res = 3.13 if self.product_type == Rs2ProductType.SGX else 6.25
            elif self.sensor_mode == Rs2SensorMode.XF:
                def_res = 2.0 if self.product_type == Rs2ProductType.SGX else 3.13
                if self.product_type in [Rs2ProductType.SGF, Rs2ProductType.SGX]:
                    LOGGER.debug(
                        "This product is considered to have one look (not checked in metadata)"
                    )  # TODO
            elif self.sensor_mode in [Rs2SensorMode.S, Rs2SensorMode.EH]:
                def_res = 8.0 if self.product_type == Rs2ProductType.SGX else 12.5
            elif self.sensor_mode in [Rs2SensorMode.W, Rs2SensorMode.EL]:
                def_res = 10.0 if self.product_type == Rs2ProductType.SGX else 12.5
            elif self.sensor_mode in [Rs2SensorMode.FQ, Rs2SensorMode.WQ]:
                def_res = 3.13
            elif self.sensor_mode in [Rs2SensorMode.SQ, Rs2SensorMode.WSQ]:
                raise NotImplementedError(
                    "Not squared pixels management are not implemented in EOReader."
                )
            elif self.sensor_mode == Rs2SensorMode.SCN:
                def_res = 25.0
            elif self.sensor_mode == Rs2SensorMode.SCW:
                def_res = 50.0
            elif self.sensor_mode == Rs2SensorMode.DVWF:
                def_res = 40.0 if self.product_type == Rs2ProductType.SCF else 20.0
            elif self.sensor_mode == Rs2SensorMode.SCW:
                if self.product_type == Rs2ProductType.SCF:
                    def_res = 50.0
                else:
                    raise NotImplementedError(
                        "Not squared pixels management are not implemented in EOReader."
                    )
            else:
                raise InvalidTypeError(f"Unknown sensor mode {self.sensor_mode}")

        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 = "*imagery_{}.tif"
        self._band_folder = self.path
        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"RS2_OK73950_PK661843_DK590667_U25W2_20160228_112418_HH_SGF.zip"
        >>> prod = Reader().open(path)
        >>> prod.wgs84_extent()
                                                    geometry
        1  POLYGON ((106.57999 -6.47363, 107.06926 -6.473...
        ```

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

        """
        # Open extent KML file
        try:
            if self.is_archived:
                product_kml = files.read_archived_vector(self.path, ".*product\.kml")
            else:
                extent_file = glob.glob(os.path.join(self.path, "product.kml"))[0]
                vectors.set_kml_driver()
                product_kml = gpd.read_file(extent_file)
        except IndexError as ex:
            raise InvalidProductError(
                f"Extent file (product.kml) not found in {self.path}"
            ) from ex

        extent_wgs84 = product_kml[
            product_kml.Name == "Polygon Outline"
        ].envelope.to_crs(WGS84)

        return gpd.GeoDataFrame(geometry=extent_wgs84.geometry, crs=extent_wgs84.crs)

    def _set_product_type(self) -> None:
        """Get products type"""
        self._get_sar_product_type(
            prod_type_pos=-1,
            gdrg_types=Rs2ProductType.SGF,
            cplx_types=Rs2ProductType.SLC,
        )
        if self.product_type != Rs2ProductType.SGF:
            LOGGER.warning(
                "Other products type than SGF has not been tested for %s data. "
                "Use it at your own risks !",
                self.platform.value,
            )

    def _set_sensor_mode(self) -> None:
        """
        Get products type from RADARSAT-2 products name (could check the metadata too)
        """
        # Get metadata
        root, namespace = self.read_mtd()

        # Get sensor mode
        sensor_mode_xml = None
        for element in root:
            if element.tag == namespace + "sourceAttributes":
                radar_param = element.find(namespace + "radarParameters")

                # WARNING: this word may differ from the Enum !!! (no docs available)
                # Get the closest match
                sensor_mode_xml = radar_param.findtext(namespace + "acquisitionType")
                break

        if sensor_mode_xml:
            sensor_mode = difflib.get_close_matches(
                sensor_mode_xml, Rs2SensorMode.list_values()
            )[0]
            try:
                self.sensor_mode = Rs2SensorMode.from_value(sensor_mode)
            except ValueError as ex:
                raise InvalidTypeError(f"Invalid sensor mode for {self.name}") from ex
        else:
            raise InvalidTypeError(f"Invalid sensor mode for {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"RS2_OK73950_PK661843_DK590667_U25W2_20160228_112418_HH_SGF.zip"
        >>> prod = Reader().open(path)
        >>> prod.get_datetime(as_datetime=True)
        datetime.datetime(2016, 2, 28, 11, 24, 18)
        >>> prod.get_datetime(as_datetime=False)
        '20160228T112418'
        ```

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

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

        date = f"{split_name[5]}T{split_name[6]}"

        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"LC08_L1GT_023030_20200518_20200527_01_T2"
        >>> prod = Reader().open(path)
        >>> prod.read_mtd()
        (<Element {http://www.rsi.ca/rs2/prod/xml/schemas}product at 0x1c0efbd37c8>,
        '{http://www.rsi.ca/rs2/prod/xml/schemas}')
        ```

        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, ".*product\.xml")
        else:
            # Open metadata file
            try:
                mtd_file = glob.glob(os.path.join(self.path, "product.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
        idx = root.tag.rindex("}")
        namespace = root.tag[: idx + 1]

        return root, namespace

Classes

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

RADARSAT-2 projection identifier. Take a look here

Expand source code
class Rs2ProductType(ListEnum):
    """
    RADARSAT-2 projection identifier.
    Take a look [here](https://www.pcigeomatics.com/geomatica-help/references/gdb_r/RADARSAT-2.html)
    """

    SLC = "SLC"
    """Single-look complex"""

    SGX = "SGX"
    """SAR georeferenced extra"""

    SGF = "SGF"
    """SAR georeferenced fine"""

    SCN = "SCN"
    """ScanSAR narrow beam"""

    SCW = "SCW"
    """ScanSAR wide beam"""

    SCF = "SCF"
    """ScanSAR fine"""

    SCS = "SCS"
    """ScanSAR sampled"""

    SSG = "SSG"
    """SAR systematic geocorrected"""

    SPG = "SPG"
    """SAR precision geocorrected"""

Ancestors

  • sertit.misc.ListEnum
  • enum.Enum

Class variables

var SLC

Single-look complex

var SGX

SAR georeferenced extra

var SGF

SAR georeferenced fine

var SCN

ScanSAR narrow beam

var SCW

ScanSAR wide beam

var SCF

ScanSAR fine

var SCS

ScanSAR sampled

var SSG

SAR systematic geocorrected

var SPG

SAR precision geocorrected

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

RADARSAT-2 sensor mode. Take a look here

Warning: The name in the metadata may vary !

Expand source code
class Rs2SensorMode(ListEnum):
    """
    RADARSAT-2 sensor mode.
    Take a look [here](https://www.pcigeomatics.com/geomatica-help/references/gdb_r/RADARSAT-2.html)

    .. WARNING:: The name in the metadata may vary !
    """

    # Single Beam Modes
    S = "Standard"
    """Standard Mode"""

    W = "Wide"
    """Spotlight Mode"""

    F = "Fine"
    """Wide Mode"""

    WF = "Wide Fine"
    """Wide Fine Mode"""

    MF = "Multi-Look Fine"
    """Multi-Look Fine Mode"""

    WMF = "Wide Multi-Look Fine"
    """Wide Multi-Look Fine Mode"""

    XF = "Extra-Fine"
    """Extra-Fine Mode"""

    U = "Ultra-Fine"
    """Ultra-Fine Mode"""

    WU = "Wide Ultra-Fine"
    """Wide Ultra-Fine Mode"""

    EH = "Extended High"
    """Extended High Mode"""

    EL = "Extended Low"
    """Extended Low Mode"""

    SQ = "Standard Quad-Pol"
    """Standard Quad-Pol Mode"""

    WSQ = "Wide Standard Quad-Pol"
    """Wide Standard Quad-Pol Mode"""

    FQ = "Fine Quad-Pol"
    """Fine Quad-Pol Mode"""

    WFQ = "Wide Fine Quad-Pol"
    """Spotlight Mode"""

    # ScanSAR Modes
    SCN = "ScanSAR Narrow"
    """Spotlight Mode"""

    SCW = "ScanSAR Wide"
    """Spotlight Mode"""

    OSVN = "Ocean Surveillance"
    """Ocean Surveillance Mode"""

    DVWF = "Ship Detection"
    """Ship Detection Mode"""

    # Spotlight Mode
    SLA = "Spotlight"
    """Spotlight Mode"""

Ancestors

  • sertit.misc.ListEnum
  • enum.Enum

Class variables

var S

Standard Mode

var W

Spotlight Mode

var F

Wide Mode

var WF

Wide Fine Mode

var MF

Multi-Look Fine Mode

var WMF

Wide Multi-Look Fine Mode

var XF

Extra-Fine Mode

var U

Ultra-Fine Mode

var WU

Wide Ultra-Fine Mode

var EH

Extended High Mode

var EL

Extended Low Mode

var SQ

Standard Quad-Pol Mode

var WSQ

Wide Standard Quad-Pol Mode

var FQ

Fine Quad-Pol Mode

var WFQ

Spotlight Mode

var SCN

Spotlight Mode

var SCW

Spotlight Mode

var OSVN

Ocean Surveillance Mode

var DVWF

Ship Detection Mode

var SLA

Spotlight Mode

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

RADARSAT-2 polarization mode. Take a look here

Expand source code
class Rs2Polarization(ListEnum):
    """
    RADARSAT-2 polarization mode.
    Take a look [here](https://www.pcigeomatics.com/geomatica-help/references/gdb_r/RADARSAT-2.html#RADARSAT2__rs2_sfs)
    """

    HH = "HH"
    VV = "VV"
    VH = "VH"
    HV = "HV"

Ancestors

  • sertit.misc.ListEnum
  • enum.Enum

Class variables

var HH
var VV
var VH
var HV
class Rs2Product (product_path, archive_path=None, output_path=None)

Class for RADARSAT-2 Products

You can use directly the .zip file

Expand source code
class Rs2Product(SarProduct):
    """
    Class for RADARSAT-2 Products

    You can use directly the .zip file
    """

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

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

            for element in root:
                if element.tag == namespace + "imageAttributes":
                    raster_attr = element.find(namespace + "rasterAttributes")
                    def_res = float(
                        raster_attr.findtext(namespace + "sampledPixelSpacing")
                    )
                    break
        except (InvalidProductError, AttributeError):
            pass

        # If we cannot read it in MTD, initiate survival mode
        if not def_res:
            if self.sensor_mode == Rs2SensorMode.SLA:
                def_res = 1.0 if self.product_type == Rs2ProductType.SGX else 0.5
            elif self.sensor_mode in [Rs2SensorMode.U, Rs2SensorMode.WU]:
                def_res = 1.0 if self.product_type == Rs2ProductType.SGX else 1.56
            elif self.sensor_mode in [
                Rs2SensorMode.MF,
                Rs2SensorMode.WMF,
                Rs2SensorMode.F,
                Rs2SensorMode.WF,
            ]:
                def_res = 3.13 if self.product_type == Rs2ProductType.SGX else 6.25
            elif self.sensor_mode == Rs2SensorMode.XF:
                def_res = 2.0 if self.product_type == Rs2ProductType.SGX else 3.13
                if self.product_type in [Rs2ProductType.SGF, Rs2ProductType.SGX]:
                    LOGGER.debug(
                        "This product is considered to have one look (not checked in metadata)"
                    )  # TODO
            elif self.sensor_mode in [Rs2SensorMode.S, Rs2SensorMode.EH]:
                def_res = 8.0 if self.product_type == Rs2ProductType.SGX else 12.5
            elif self.sensor_mode in [Rs2SensorMode.W, Rs2SensorMode.EL]:
                def_res = 10.0 if self.product_type == Rs2ProductType.SGX else 12.5
            elif self.sensor_mode in [Rs2SensorMode.FQ, Rs2SensorMode.WQ]:
                def_res = 3.13
            elif self.sensor_mode in [Rs2SensorMode.SQ, Rs2SensorMode.WSQ]:
                raise NotImplementedError(
                    "Not squared pixels management are not implemented in EOReader."
                )
            elif self.sensor_mode == Rs2SensorMode.SCN:
                def_res = 25.0
            elif self.sensor_mode == Rs2SensorMode.SCW:
                def_res = 50.0
            elif self.sensor_mode == Rs2SensorMode.DVWF:
                def_res = 40.0 if self.product_type == Rs2ProductType.SCF else 20.0
            elif self.sensor_mode == Rs2SensorMode.SCW:
                if self.product_type == Rs2ProductType.SCF:
                    def_res = 50.0
                else:
                    raise NotImplementedError(
                        "Not squared pixels management are not implemented in EOReader."
                    )
            else:
                raise InvalidTypeError(f"Unknown sensor mode {self.sensor_mode}")

        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 = "*imagery_{}.tif"
        self._band_folder = self.path
        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"RS2_OK73950_PK661843_DK590667_U25W2_20160228_112418_HH_SGF.zip"
        >>> prod = Reader().open(path)
        >>> prod.wgs84_extent()
                                                    geometry
        1  POLYGON ((106.57999 -6.47363, 107.06926 -6.473...
        ```

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

        """
        # Open extent KML file
        try:
            if self.is_archived:
                product_kml = files.read_archived_vector(self.path, ".*product\.kml")
            else:
                extent_file = glob.glob(os.path.join(self.path, "product.kml"))[0]
                vectors.set_kml_driver()
                product_kml = gpd.read_file(extent_file)
        except IndexError as ex:
            raise InvalidProductError(
                f"Extent file (product.kml) not found in {self.path}"
            ) from ex

        extent_wgs84 = product_kml[
            product_kml.Name == "Polygon Outline"
        ].envelope.to_crs(WGS84)

        return gpd.GeoDataFrame(geometry=extent_wgs84.geometry, crs=extent_wgs84.crs)

    def _set_product_type(self) -> None:
        """Get products type"""
        self._get_sar_product_type(
            prod_type_pos=-1,
            gdrg_types=Rs2ProductType.SGF,
            cplx_types=Rs2ProductType.SLC,
        )
        if self.product_type != Rs2ProductType.SGF:
            LOGGER.warning(
                "Other products type than SGF has not been tested for %s data. "
                "Use it at your own risks !",
                self.platform.value,
            )

    def _set_sensor_mode(self) -> None:
        """
        Get products type from RADARSAT-2 products name (could check the metadata too)
        """
        # Get metadata
        root, namespace = self.read_mtd()

        # Get sensor mode
        sensor_mode_xml = None
        for element in root:
            if element.tag == namespace + "sourceAttributes":
                radar_param = element.find(namespace + "radarParameters")

                # WARNING: this word may differ from the Enum !!! (no docs available)
                # Get the closest match
                sensor_mode_xml = radar_param.findtext(namespace + "acquisitionType")
                break

        if sensor_mode_xml:
            sensor_mode = difflib.get_close_matches(
                sensor_mode_xml, Rs2SensorMode.list_values()
            )[0]
            try:
                self.sensor_mode = Rs2SensorMode.from_value(sensor_mode)
            except ValueError as ex:
                raise InvalidTypeError(f"Invalid sensor mode for {self.name}") from ex
        else:
            raise InvalidTypeError(f"Invalid sensor mode for {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"RS2_OK73950_PK661843_DK590667_U25W2_20160228_112418_HH_SGF.zip"
        >>> prod = Reader().open(path)
        >>> prod.get_datetime(as_datetime=True)
        datetime.datetime(2016, 2, 28, 11, 24, 18)
        >>> prod.get_datetime(as_datetime=False)
        '20160228T112418'
        ```

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

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

        date = f"{split_name[5]}T{split_name[6]}"

        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"LC08_L1GT_023030_20200518_20200527_01_T2"
        >>> prod = Reader().open(path)
        >>> prod.read_mtd()
        (<Element {http://www.rsi.ca/rs2/prod/xml/schemas}product at 0x1c0efbd37c8>,
        '{http://www.rsi.ca/rs2/prod/xml/schemas}')
        ```

        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, ".*product\.xml")
        else:
            # Open metadata file
            try:
                mtd_file = glob.glob(os.path.join(self.path, "product.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
        idx = root.tag.rindex("}")
        namespace = root.tag[: idx + 1]

        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)

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

>>> from eoreader.reader import Reader
>>> path = r"RS2_OK73950_PK661843_DK590667_U25W2_20160228_112418_HH_SGF.zip"
>>> prod = Reader().open(path)
>>> prod.wgs84_extent()
                                            geometry
1  POLYGON ((106.57999 -6.47363, 107.06926 -6.473...

Returns

gpd.GeoDataFrame
WGS84 extent as a gpd.GeoDataFrame
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"RS2_OK73950_PK661843_DK590667_U25W2_20160228_112418_HH_SGF.zip"
    >>> prod = Reader().open(path)
    >>> prod.wgs84_extent()
                                                geometry
    1  POLYGON ((106.57999 -6.47363, 107.06926 -6.473...
    ```

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

    """
    # Open extent KML file
    try:
        if self.is_archived:
            product_kml = files.read_archived_vector(self.path, ".*product\.kml")
        else:
            extent_file = glob.glob(os.path.join(self.path, "product.kml"))[0]
            vectors.set_kml_driver()
            product_kml = gpd.read_file(extent_file)
    except IndexError as ex:
        raise InvalidProductError(
            f"Extent file (product.kml) not found in {self.path}"
        ) from ex

    extent_wgs84 = product_kml[
        product_kml.Name == "Polygon Outline"
    ].envelope.to_crs(WGS84)

    return gpd.GeoDataFrame(geometry=extent_wgs84.geometry, crs=extent_wgs84.crs)

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"RS2_OK73950_PK661843_DK590667_U25W2_20160228_112418_HH_SGF.zip"
>>> prod = Reader().open(path)
>>> prod.get_datetime(as_datetime=True)
datetime.datetime(2016, 2, 28, 11, 24, 18)
>>> prod.get_datetime(as_datetime=False)
'20160228T112418'

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"RS2_OK73950_PK661843_DK590667_U25W2_20160228_112418_HH_SGF.zip"
    >>> prod = Reader().open(path)
    >>> prod.get_datetime(as_datetime=True)
    datetime.datetime(2016, 2, 28, 11, 24, 18)
    >>> prod.get_datetime(as_datetime=False)
    '20160228T112418'
    ```

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

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

    date = f"{split_name[5]}T{split_name[6]}"

    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"LC08_L1GT_023030_20200518_20200527_01_T2"
>>> prod = Reader().open(path)
>>> prod.read_mtd()
(<Element {http://www.rsi.ca/rs2/prod/xml/schemas}product at 0x1c0efbd37c8>,
'{http://www.rsi.ca/rs2/prod/xml/schemas}')

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"LC08_L1GT_023030_20200518_20200527_01_T2"
    >>> prod = Reader().open(path)
    >>> prod.read_mtd()
    (<Element {http://www.rsi.ca/rs2/prod/xml/schemas}product at 0x1c0efbd37c8>,
    '{http://www.rsi.ca/rs2/prod/xml/schemas}')
    ```

    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, ".*product\.xml")
    else:
        # Open metadata file
        try:
            mtd_file = glob.glob(os.path.join(self.path, "product.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
    idx = root.tag.rindex("}")
    namespace = root.tag[: idx + 1]

    return root, namespace

def footprint(

self)

Inherited from: SarProduct.footprint

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

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 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 …