Skip to content

Instantly share code, notes, and snippets.

@asjohnston-asf
Created August 18, 2020 18:14
Show Gist options
  • Save asjohnston-asf/07afc1fbac96a0fe687b716315f363c3 to your computer and use it in GitHub Desktop.
Save asjohnston-asf/07afc1fbac96a0fe687b716315f363c3 to your computer and use it in GitHub Desktop.
from requests import Session
search_url = 'https://cmr.earthdata.nasa.gov/search/granules'
parameters = {
'scroll': 'true',
'provider': 'ASF',
'page_size': 2000,
# set any search criteria here
#'short_name': ['SENTINEL-1A_SP_GRD_HIGH','SENTINEL-1B_SP_GRD_HIGH','SENTINEL-1A_DP_GRD_HIGH','SENTINEL-1B_DP_GRD_HIGH'],
}
output_format = 'umm_json'
def process_granules(granules):
for granule in granules:
# do whatever you need to do with each granule here
urls = [item['URL'] for item in granule['umm']['RelatedUrls'] if item['Type'] in ['GET DATA', 'GET RELATED VISUALIZATION']]
for url in urls:
print(url)
return len(granules)
session = Session()
response = session.get(f'{search_url}.{output_format}', params=parameters)
response.raise_for_status()
scroll_id = response.headers['CMR-Scroll-Id']
hits = int(response.headers['CMR-Hits'])
granules_processed = 0
while granules_processed < hits:
granules = response.json()['items'] # adjust for your output format
granules_processed += process_granules(granules)
if granules_processed >= hits:
break
response = session.get(search_url, headers={'CMR-Scroll-Id': scroll_id})
response.raise_for_status()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment