def search_scenes(args, log):
    scenes = []
    for granule in args.granules:
        data = {'location': ('T' + granule), 'maxRecords': 500}
        if args.level:
            data['processingLevel'] = ('LEVEL' + args.level)
        if args.start:
            data['startDate'] = args.start
        if args.end:
            data['completionDate'] = args.end
        url = (THEIA_URL + '/resto2/api/collections/SENTINEL2/search.json?')
        data = json.loads(requests.get(url, params=data).text)
        assert data, 'Unable to perform query: {}'.format(url)
        if (not len(data['features'])):
            log.log('======================================================')
            log.log('WARNING: No scene found for {} granule'.format(granule))
            log.log('======================================================')
            continue
        log.log('=============================================================================')
        log.log('Selected scenes for {} granule'.format(granule))
        log.log('-----------------------------------------------------------------------------')
        log.log('   Date        Clouds              Scene UUID                 Processing     ')
        log.log('-----------------------------------------------------------------------------')
        features = ([feature for feature in data['features'] if (int(feature['properties']['cloudCover']) <= args.clouds)] if (args.clouds is not None) else data['features'])
        features = [feature for (i, feature) in enumerate(features) if (i < args.limit)]
        for feature in features:
            date = datetime.strptime(feature['properties']['startDate'], '%Y-%m-%dT%H:%M:%SZ').date()
            cover = str(feature['properties']['cloudCover']).rjust(3, ' ')
            scenes.append({'uuid': feature['id'], 'checksum': feature['properties']['services']['download']['checksum'], 'dir': feature['properties']['title']})
            log.log('{}\t{}\t{}\t{}'.format(date, cover, feature['id'], feature['properties']['processingLevel']))
    log.log('=============================================================================')
    return scenes
def search_scenes(args, log):
    scenes = []
    for granule in args.granules:
        data = {'location': ('T' + granule), 'maxRecords': 500}
        if args.level:
            data['processingLevel'] = ('LEVEL' + args.level)
        if args.start:
            data['startDate'] = args.start
        if args.end:
            data['completionDate'] = args.end
        url = (THEIA_URL + '/resto2/api/collections/SENTINEL2/search.json?')
        data = json.loads(requests.get(url, params=data).text)
        assert data, 'Unable to perform query: {}'.format(url)
        if (not len(data['features'])):
            log.log('======================================================')
            log.log('WARNING: No scene found for {} granule'.format(granule))
            log.log('======================================================')
            continue
        log.log('=============================================================================')
        log.log('Selected scenes for {} granule'.format(granule))
        log.log('-----------------------------------------------------------------------------')
        log.log('   Date        Clouds              Scene UUID                 Processing     ')
        log.log('-----------------------------------------------------------------------------')
        features = ([feature for feature in data['features'] if (int(feature['properties']['cloudCover']) <= args.clouds)] if (args.clouds is not None) else data['features'])
        features = [feature for (i, feature) in enumerate(features) if (i < args.limit)]
        for feature in features:
            date = datetime.strptime(feature['properties']['startDate'], '%Y-%m-%dT%H:%M:%SZ').date()
            cover = str(feature['properties']['cloudCover']).rjust(3, ' ')
            scenes.append({'uuid': feature['id'], 'checksum': feature['properties']['services']['download']['checksum'], 'dir': feature['properties']['title']})
            log.log('{}\t{}\t{}\t{}'.format(date, cover, feature['id'], feature['properties']['processingLevel']))
    log.log('=============================================================================')
    return scenes