pyeo.queries_and_downloads

Functions for querying, filtering and downloading data.

Key functions

check_for_s2_data_by_date() Queries the Sentinel 2 archive for products between two dates download_s2_data() Downloads Sentinel 2 data from Scihub by default

SAFE files

Sentinel 2 data is downloaded in the form of a .SAFE file; all download functions will end with data in this structure. This is a directory structure that contains the imagery, metadata and supplementary data of a Sentinel 2 image. The rasters themeselves are the in the GRANULE/[granule_id]/IMG_DATA/[resolution]/ folder; each band is contained in its own .jp2 file. For full details, see https://sentinel.esa.int/web/sentinel/user-guides/sentinel-2-msi/data-formats

There are two ways to refer to a given Sentinel-2 products: the UUID and the product ID. The UUID is a set of five five-character strings (EXMAPLE HERE) The product ID is a human-readable string (more or less) containing all the information needed for unique identification of an product, split by the underscore character. For more information on the structure of a product ID, see (EXAMPLE HERE)

Query data structure

All query functions return a dictionary. The key of the dictionary is the UUID of the product id; the product is a further set of nested dictionaries containing information about the product to be downloaded. (PUT STRUCTURE HERE)

Data download sources

This library presently offers three options for download sources; Scihub and Amazon Web Services, for Sentinel, and USGS, for Landsat. If in doubt, use Scihub.

  • Scihub

    The Copernicus Open-Access Hub is the default option for downloading sentinel-2 images. Images are downloaded in .zip format, and then automatically unzipped. Users are required to register with a username and password before downloading, and there is a limit to no more than two concurrent downloads per username at a time. Scihub is entirely free.

  • AWS

    Sentinel data is also publicly hosted on Amazon Web Services. This storage is provided by Sinergise, and is normally updated a few hours after new products are made available. There is a small charge associated with downloading this data. To access the AWS repository, you are required to register an Amazon Web Services account (including providing payment details) and obtain an API key for that account. See https://aws.amazon.com/s3/pricing/ for pricing details; the relevant table is Data Transfer Pricing for the EU (Frankfurt) region. There is no limit to the concurrent downloads for the AWS bucket.

  • USGS

    Landsat data is hosted and provided by the US Geological Survey. You can sign up at https://ers.cr.usgs.gov/register/

Functions

pyeo.queries_and_downloads.check_for_s2_data_by_date(aoi_path, start_date, end_date, conf, cloud_cover=50)

Gets all the products between start_date and end_date. Wraps sent2_query to avoid having passwords and long-format timestamps in code.

Parameters
  • aoi_path (str) – Path to a geojson file containing a polygon of the outline of the area you wish to download. See www.geojson.io for a tool to build these.

  • start_date (str) – Start date in the format yyyymmdd.

  • end_date (str) – End date of the query in the format yyyymmdd

  • conf (dict) –

    Output from a configuration file containing your username and password for the ESA hub. If needed, this can be dummied with a dictionary of the following format:

    conf={'sent_2':{'user':'your_username', 'pass':'your_pass'}}
    

  • cloud_cover (int) – The maximum level of cloud cover in images to be downloaded.

Returns

result – A dictionary of Sentinel 2 products.

Return type

dict

pyeo.queries_and_downloads.download_from_aws_with_rollback(product_id, folder, uuid, user, passwd)

Attempts to download a single product from AWS using product_id; if not found, rolls back to Scihub using the UUID

Parameters
  • product_id (str) – The product ID (“L2A_…”)

  • folder (str) – The folder to download the .SAFE file to.

  • uuid (str) – The product UUID (4dfB4-432df….)

  • user (str) – Scihub username

  • passwd (str) – Scihub password

pyeo.queries_and_downloads.download_from_scihub(product_uuid, out_folder, user, passwd)

Downloads and unzips product_uuid from scihub

Parameters
  • product_uuid (str) – The product UUID (4dfB4-432df….)

  • out_folder (str) – The folder to save the .SAFE file to

  • user (str) – Scihub username

  • passwd (str) – Scihub password

Notes

If interrupted mid-download, there will be a .incomplete file in the download folder. You might need to remove this for further processing.

pyeo.queries_and_downloads.download_landsat_data(products, out_dir, conf)

Given an output from landsat_query, will download al L1C products to out_dir.

Parameters
  • products (str) – Dictionary of landsat products; must include downloadUrl and displayId

  • out_dir (str) – Directory to save Landsat files in. Folder structure is out_dir->displayId->products

  • conf (dict) – Dictionary containing USGS login credentials. See docs for landsat_query().

pyeo.queries_and_downloads.download_s2_data(new_data, l1_dir, l2_dir, source='scihub', user=None, passwd=None, try_scihub_on_fail=False)

Downloads S2 imagery from AWS, google_cloud or scihub. new_data is a dict from Sentinel_2.

Parameters
  • new_data (dict) – A query dictionary contining the products you want to download

  • l1_dir (str) – The directory to download level 1 products to.

  • l2_dir (str) – The directory to download level 2 products to.

  • source ({'scihub', 'aws'}) – The source to download the data from. Can be ‘scihub’ or ‘aws’; see section introduction for details

  • user (str, optional) – The username for sentinelhub

  • passwd (str, optional) – The password for sentinelhub

  • try_scihub_on_fail (bool, optional) – If true, this function will roll back to downloading from Scihub on a failure of any other downloader. Defaults to False.

Raises

BadDataSource – Raised when passed either a bad datasource or a bad image ID

pyeo.queries_and_downloads.download_s2_pairs(l1_dir, l2_dir, conf)

Given a pair of folders, one containing l1 products and the other containing l2 products, will query and download missing data. At the end of the run, you will have two folders with a set of paired L1 and L2 products. :param l1_dir: The directory to download level 1 products to. May contain existing products. :type l1_dir: str :param l2_dir: The directory to download level 2 products to. May contain existing products. :type l2_dir: str :param conf: A dictionary containing [‘sent_2’][‘user’] and [‘sent_2’][‘pass’] :type conf: dict

pyeo.queries_and_downloads.filter_non_matching_s2_data(query_output)

Filters a query such that it only contains paired level 1 and level 2 data products.

Parameters

query_output (dict) – Query list

Returns

filtered_query – A dictionary of products containing only L1 and L2 data.

Return type

dict

pyeo.queries_and_downloads.filter_to_l1_data(query_output)

Takes list of products from check_for_s2_data_by_date and removes all non Level 1 products.

Parameters

query_output (dict) – A dictionary of products from a S2 query

Returns

filtered_query – A dictionary of products containing only the L1C data products

Return type

dict

pyeo.queries_and_downloads.filter_to_l2_data(query_output)

Takes list of products from check_for_s2_data_by_date and removes all non Level 2A products.

Parameters

query_output (dict) – A dictionary of products from a S2 query

Returns

filtered_query – A dictionary of products containing only the L2A data products

Return type

dict

pyeo.queries_and_downloads.get_granule_identifiers(safe_product_id)

Returns the parts of a S2 name that uniquely identify that granulate at a moment in time :param safe_product_id: The filename of a SAFE product :type safe_product_id: str

Returns

  • satellite (str) – A string of either “L2A” or “L2B”

  • intake_date (str) – The timestamp of the data intake of this granule

  • orbit number (str) – The orbit number of this granule

  • granule (str) – The ID of this granule

pyeo.queries_and_downloads.get_query_datatake(query_item)

Gets the datatake timestamp of a query item.

Parameters

query_item (dict) – An item from a query results dictionary.

Returns

timestamp – The timestamp of that item’s datatake in the format yyyymmddThhmmss (Ex: 20190613T123002)

Return type

str

pyeo.queries_and_downloads.get_query_granule(query_item)

Gets the granule ID (ex: 48MXU) of a query

Parameters

query_item (dict) – An item from a query results dictionary.

Returns

granule_id – The granule ID of that item.

Return type

str

pyeo.queries_and_downloads.get_query_level(query_item)

Returns the processing level of the query item.

Parameters

query_item (dict) – An item from a query results dictionary.

Returns

query_level – A string of either ‘Level-1C’ or ‘Level-2A’.

Return type

str

pyeo.queries_and_downloads.get_query_processing_time(query_item)

Returns the processing timestamps of a query item

Parameters

query_item (dict) – An item from a query results dictionary.

Returns

processing_time – The date processing timestamp in the format yyyymmddThhmmss (Ex: 20190613T123002)

Return type

str

pyeo.queries_and_downloads.landsat_query(conf, geojsonfile, start_date, end_date, cloud=50)

Queries the USGS dataset LANDSAT_8_C1 for imagery between the start_date and end_date, inclusive. This downloads all imagery touched by the bounding box of the provided geojson file.

Parameters
  • conf (dict) – A dictionary with [‘landsat’][‘user’] and [‘landsat’][‘pass’] values, containing your USGS credentials.

  • geojsonfile (str) – The geojson file

  • start_date (str) – The start date, in “yyyymmdd” format. Will truncate any longer string.

  • end_date (str) – The end query date, in “yyyymmdd” format. Will truncate any longer string.

  • cloud (float) – The maximum cloud cover to return.

Returns

products – A list of products; each item being a dictionary returned from the USGS API. See https://earthexplorer.usgs.gov/inventory/documentation/datamodel#Scene

Return type

list of dict

pyeo.queries_and_downloads.load_api_key(path_to_api)

Returns an API key from a single-line text file containing that API

Parameters

path_to_api (str) – The path a text file containing only the API key

Returns

api_key – Returns the API key

Return type

str

pyeo.queries_and_downloads.planet_query(aoi_path, start_date, end_date, out_path, api_key, item_type='PSScene4Band', search_name='auto', asset_type='analytic', threads=5)

Downloads data from Planetlabs for a given time period in the given AOI

Parameters
  • aoi (str) – Filepath of a single-polygon geojson containing the aoi

  • start_date (str) – the inclusive start of the time window in UTC format

  • end_date (str) – the inclusive end of the time window in UTC format

  • out_path (filepath-like object) – A path to the output folder Any identically-named imagery will be overwritten

  • item_type (str) – Image type to download (see Planet API docs)

  • search_name (str) – A name to refer to the search (required for large searches)

  • asset_type (str) – Planet asset type to download (see Planet API docs)

  • threads (int) – The number of downloads to perform concurrently

Notes

IMPORTANT: Will not run for searches returning greater than 250 items.

pyeo.queries_and_downloads.query_for_corresponding_image(prod, conf)

Queries Copernicus Hub for the corresponding l1/l2 image to ‘prod’

Parameters
  • prod (str) – The product name to query

  • conf (dict) – A dictionary containing [‘sent_2’][‘user’] and [‘sent_2’][‘pass’]

Returns

out – A Sentinel-2 product dictionary

Return type

dict

pyeo.queries_and_downloads.read_aoi(aoi_path)

Opens the geojson file for the aoi. If FeatureCollection, return the first feature.

Parameters

aoi_path (str) – The path to the geojson file

Returns

aoi_dict – A dictionary translation of the feature inside the .json file

Return type

dict

pyeo.queries_and_downloads.sent2_query(user, passwd, geojsonfile, start_date, end_date, cloud=50)

Fetches a list of Sentienl-2 products

Parameters
  • user (string) – Username for ESA hub. Register at https://scihub.copernicus.eu/dhus/#/home

  • passwd (string) – password for the ESA Open Access hub

  • geojsonfile (string) – Path to a geometry file containing a polygon of the outline of the area you wish to download. Can be a geojson (.json/.geojson) or a shapefile (.shp) See www.geojson.io for a tool to build these.

  • start_date (string) – Date of beginning of search in the format YYYY-MM-DDThh:mm:ssZ (ISO standard)

  • end_date (string) – Date of end of search in the format yyyy-mm-ddThh:mm:ssZ See https://www.w3.org/TR/NOTE-datetime, or use cehck_for_s2_data_by_date

  • cloud (int, optional) – The maximum cloud clover percentage (as calculated by Copernicus) to download. Defaults to 50%

Returns

products – A dictionary of Sentinel-2 granule products that are touched by your AOI polygon, keyed by product ID. Returns both level 1 and level 2 data.

Return type

dict

Notes

If you get a ‘request too long’ error, it is likely that your polygon is too complex. The following functions download by granule; there is no need to have a precise polygon at this stage.

pyeo.queries_and_downloads.shapefile_to_wkt(shapefile_path)

Converts a shapefile to a well-known text (wkt) format

Parameters

shapefile_path (str) – Path to the shapefile to convert

Returns

wkt – A wkt - string containing the geometry of the first feature of the first layer of the shapefile shapefile

Return type

str