Ejemplo n.º 1
0
    def __init__(self, datahub, catalog=None, url=None):
        """
        :param datahub: Data source (<enum 'Datahub'>).
        :param catalog: Only applicable if datahub is 'STAC_local'. Can be one of the following types:
                        Path to STAC Catalog file catalog.json (String, Path).
                        Pystac Catalog or Collection object (pystac.catalog.Catalog, pystac.collection.Collection).
                        None initializes an empty catalog.
                        (default: None)
        :param url: Only applicable if datahub is 'STAC_API'. STAC Server endpoint, reads from STAC_API_URL environment
                        variable by default
                        (default: None)
        """
        self.src = datahub

        if self.src == Datahub.STAC_local:
            # connect to STAC Catalog
            if isinstance(
                    catalog,
                (pystac.catalog.Catalog, pystac.collection.Collection)):
                self.api = catalog
            elif isinstance(catalog, (str, Path)):
                href = Path(catalog).resolve().as_uri()
                self.api = pystac.catalog.Catalog.from_file(href)
            elif catalog is None:
                self.api = self._init_catalog()
            else:
                raise AttributeError(
                    f"{catalog} is not a valid STAC Catalog [catalog.json, pystac.catalog.Catalog, "
                    f"pystac.collection.Collection, None] ")

        elif self.src == Datahub.STAC_API:
            if url:
                self.api = StacApi(url=url)
            else:
                self.api = StacApi()

        elif self.src == Datahub.EarthExplorer:
            import landsatxplore.api

            # connect to Earthexplorer
            self.user = env_get("EARTHEXPLORER_USER")
            self.pw = env_get("EARTHEXPLORER_PW")
            self.api = landsatxplore.api.API(self.user, self.pw)

        elif self.src == Datahub.Scihub:
            # connect to Scihub
            self.user = env_get("SCIHUB_USER")
            self.pw = env_get("SCIHUB_PW")
            self.api = sentinelsat.SentinelAPI(
                self.user,
                self.pw,
                "https://apihub.copernicus.eu/apihub",
                show_progressbars=False,
            )

        else:
            raise NotImplementedError(
                f"{datahub} is not supported [STAC_local, STAC_API, EarthExplorer, Scihub]"
            )
Ejemplo n.º 2
0
    def test_env(self):
        with self.assertRaises(KeyError, msg=f"No environment variable Key found"):
            psf.env_get("Key")

        os.environ["FUN"] = "True"
        self.assertTrue(psf.env_get("FUN", boolean=True))
        os.environ["ISITWEEKENDYET"] = "0"
        self.assertFalse(psf.env_get("ISITWEEKENDYET", boolean=True))
Ejemplo n.º 3
0
    def __init__(self, datahub, datadir=None, datadir_substr=None):
        """
        :param datahub: Data source (<enum 'Datahub'>).
        :param datadir: Path to directory that holds the metadata if datahub is 'File' (String).
        :param datadir_substr: Optional substring patterns to identify metadata in datadir if datahub
            is 'File' (List of String).
        """
        self.src = datahub

        if self.src == Datahub.File:
            if not datadir:
                raise AttributeError(
                    f"'datadir' has to be set if datahub is 'File'.")
            else:
                self.api = datadir
                if datadir_substr is None:
                    self.api_substr = [""]
                else:
                    self.api_substr = datadir_substr

        elif self.src == Datahub.EarthExplorer:
            # connect to Earthexplorer
            self.user = env_get("EARTHEXPLORER_USER")
            self.pw = env_get("EARTHEXPLORER_PW")
            self.api = landsatxplore.api.API(self.user, self.pw)

        elif self.src == Datahub.Scihub:
            # connect to Scihub
            self.user = env_get("SCIHUB_USER")
            self.pw = env_get("SCIHUB_PW")
            self.api = sentinelsat.SentinelAPI(
                self.user,
                self.pw,
                "https://scihub.copernicus.eu/dhus",
                show_progressbars=False,
            )

        else:
            raise NotImplementedError(
                f"{datahub} is not supported [File, EarthExplorer, Scihub]")