Created
March 29, 2023 13:04
-
-
Save slarosa/82263ae240923685016138ae168c174e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import eumdac | |
import time | |
import fnmatch | |
import shutil | |
import datetime | |
# Authentication | |
consumer_key = '<consumer_key>' | |
consumer_secret = '<consumer_secret>' | |
credentials = (consumer_key, consumer_secret) | |
token = eumdac.AccessToken(credentials) | |
# Selecting and filtering a product from the Data Store | |
datastore = eumdac.DataStore(token) | |
selected_collection = datastore.get_collection('EO:EUM:DAT:METOP:IASSND02') | |
start = datetime.datetime(2021, 6, 24, 00) | |
end = datetime.datetime(2021, 7, 3, 20) | |
geometry = [[13.24, 37.28], [13.24, 42.28], [19.12, 42.28], [19.12, 37.28], [13.24, 37.28]] | |
products = selected_collection.search( | |
geo='POLYGON(({}))'.format(','.join(["{} {}".format(*coord) for coord in geometry])), | |
dtstart=start, | |
dtend=end, | |
sat='Metop-B' | |
) | |
# Customization | |
datatailor = eumdac.DataTailor(token) | |
chain = eumdac.tailor_models.Chain( | |
product='IASSND02', | |
format='netcdf4_satellite' | |
) | |
# Download | |
for product in products: | |
customisation = datatailor.new_customisation(product, chain) | |
status = "QUEUED" | |
sleep_time = 10 # seconds | |
while status == "QUEUED" or status == "RUNNING": | |
status = customisation.status | |
if "DONE" in status: | |
print(f"SUCCESS") | |
break | |
elif "ERROR" in status or 'KILLED' in status: | |
print(f"UNSUCCESS, exiting") | |
break | |
elif "QUEUED" in status: | |
print(f"QUEUED") | |
elif "RUNNING" in status: | |
print(f"RUNNING") | |
elif "INACTIVE" in status: | |
sleep_time = max(60*10, sleep_time*2) | |
print(f"INACTIVE, doubling status polling time to {sleep_time} (max 10 mins)") | |
time.sleep(sleep_time) | |
nc, = fnmatch.filter(customisation.outputs, '*.nc') | |
with customisation.stream_output(nc,) as stream, \ | |
open(stream.name, mode='wb') as fdst: | |
shutil.copyfileobj(stream, fdst) | |
print(f"Dowloaded the NetCDF output of the customisation {stream.name}") | |
customisation.delete() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment