def cli( user, password, geometry, start, end, uuid, name, download, quicklook, sentinel, producttype, instrument, cloud, footprints, path, query, url, order_by, location, limit, info, ): """Search for Sentinel products and, optionally, download all the results and/or create a geojson file with the search result footprints. Beyond your Copernicus Open Access Hub user and password, you must pass a geojson file containing the geometry of the area you want to search for or the UUIDs of the products. If you don't specify the start and end dates, it will search in the last 24 hours. """ _set_logger_handler() if user is None or password is None: try: user, password = requests.utils.get_netrc_auth(url) except TypeError: pass if user is None or password is None: raise click.UsageError( "Missing --user and --password. Please see docs " "for environment variables and .netrc support.") api = SentinelAPI(user, password, url) if info: ctx = click.get_current_context() click.echo("DHuS version: " + api.dhus_version) ctx.exit() search_kwargs = {} if sentinel and not (producttype or instrument): search_kwargs["platformname"] = "Sentinel-" + sentinel if instrument and not producttype: search_kwargs["instrumentshortname"] = instrument if producttype: search_kwargs["producttype"] = producttype if cloud: if sentinel not in ["2", "3"]: logger.error("Cloud cover is only supported for Sentinel 2 and 3.") exit(1) search_kwargs["cloudcoverpercentage"] = (0, cloud) if query is not None: search_kwargs.update((x.split("=") for x in query)) if location is not None: wkt, info = placename_to_wkt(location) minX, minY, maxX, maxY = info["bbox"] r = 6371 # average radius, km extent_east = r * math.radians(maxX - minX) * math.cos( math.radians((minY + maxY) / 2)) extent_north = r * math.radians(maxY - minY) logger.info( "Querying location: '%s' with %.1f x %.1f km, %f, %f to %f, %f bounding box", info["display_name"], extent_north, extent_east, minY, minX, maxY, maxX, ) search_kwargs["area"] = wkt if geometry is not None: # check if the value is an existing path if os.path.exists(geometry): search_kwargs["area"] = geojson_to_wkt(read_geojson(geometry)) # check if the value is a GeoJSON else: if geometry.startswith("{"): try: geometry = json.loads(geometry) search_kwargs["area"] = geojson_to_wkt(geometry) except json_parse_exception: raise click.UsageError( "geometry string starts with '{' but is not a valid GeoJSON." ) # check if the value is a WKT elif is_wkt(geometry): search_kwargs["area"] = geometry else: raise click.UsageError( "The geometry input is neither a GeoJSON file with a valid path, " "a GeoJSON String nor a WKT string.") if uuid is not None: uuid_list = [x.strip() for x in uuid] products = {} for productid in uuid_list: try: products[productid] = api.get_product_odata(productid) except InvalidKeyError: logger.error("No product with ID '%s' exists on server", productid) exit(1) elif name is not None: search_kwargs["identifier"] = name[0] if len( name) == 1 else "(" + " OR ".join(name) + ")" products = api.query(order_by=order_by, limit=limit, **search_kwargs) else: start = start or "19000101" end = end or "NOW" products = api.query(date=(start, end), order_by=order_by, limit=limit, **search_kwargs) if footprints is True: footprints_geojson = api.to_geojson(products) with open(os.path.join(path, "search_footprints.geojson"), "w") as outfile: outfile.write(gj.dumps(footprints_geojson)) if quicklook: downloaded_quicklooks, failed_quicklooks = api.download_all_quicklooks( products, path) if failed_quicklooks: api.logger.warning("Some quicklooks failed: %s out of %s", len(failed_quicklooks), len(products)) if download is True: product_infos, triggered, failed_downloads = api.download_all( products, path) if len(failed_downloads) > 0: with open(os.path.join(path, "corrupt_scenes.txt"), "w") as outfile: for failed_id in failed_downloads: outfile.write("%s : %s\n" % (failed_id, products[failed_id]["title"])) else: for product_id, props in products.items(): if uuid is None: logger.info("Product %s - %s", product_id, props["summary"]) else: # querying uuids has no summary key logger.info( "Product %s - %s - %s MB", product_id, props["title"], round(int(props["size"]) / (1024.0 * 1024.0), 2), ) if uuid is None: logger.info("---") logger.info( "%s scenes found with a total size of %.2f GB", len(products), api.get_products_size(products), )
print("%s %s %s" % (prod_id, products[prod_id]["filename"], products[prod_id]["size"])) dir_actual = path = os.getcwd() dir_dados = dir_actual + "/" + data_str #str_data_ini print("A criar pasta: " + dir_dados) try: os.mkdir(dir_dados) except OSError: print("Falha ao criar directoria %s." % dir_dados) #sys.exit(0) #criar ficheiro de footprints.geojson para usar na mascara de transparencias oGeoJSON = api.to_geojson(products) with open(dir_dados + '/search_footprints.geojson', 'w') as outfile: json.dump(oGeoJSON, outfile) # connect to the api api_session = api.session api_url = "https://scihub.copernicus.eu/apihub/odata/v1/" #obter os quicklooks api.download_all_quicklooks(products) #chamar o download de produtos usando 2 threads! pool = ThreadPoolExecutor(max_workers=2) for download in pool.map(download_prods_bands, products): print(download) print("Fim.")