usgsdownload/usgs.py
def download(self, bands=None, download_dir=None, metadata=False):
        """Download remote .tar.bz file."""
        if not download_dir:
            download_dir = DOWNLOAD_DIR

        if bands is None:
            bands = list(range(1, 12)) + ['BQA']
        else:
            self.validate_bands(bands)

        pattern = re.compile('^[^\s]+_(.+)\.tiff?', re.I)
        band_list = ['B%i' % (i,) if isinstance(i, int) else i for i in bands]
        image_list = []

        # Connect Earth explore
        self.connect_earthexplorer()

        # tgz name
        tgzname = self.sceneInfo.name + '.tgz'
        dest_dir = check_create_folder(join(download_dir, self.sceneInfo.name))

        # Download File
        downloaded = self.download_file(self.url, dest_dir, tgzname)

        # Log
        logger.debug('Status downloaded %s' % downloaded)
        print('\n Status downloaded %s' % downloaded)

        if downloaded['sucess']:

            # Log
            print('\n Downloaded sucess')
            logger.debug('Downloaded sucess of scene: %s' % self.sceneInfo.name)

            try:

                tar = tarfile.open(downloaded['file_path'], 'r')
                folder_path = join(download_dir, self.sceneInfo.name)
                tar.extractall(folder_path)
                remove(downloaded['file_path'])
                images_path = listdir(folder_path)

                for image_path in images_path:
                    matched = pattern.match(image_path)
                    file_path = join(folder_path, image_path)
                    if matched and matched.group(1) in band_list:
                        image_list.append([file_path, getsize(file_path)])
                    elif matched:
                        remove(file_path)
            except tarfile.ReadError as error:
                print('\nError when extracting files. %s' % error)
                logger.error('Error when extracting files. %s' % error)
            return image_list
        else:
            logger.debug('Info downloaded: %s' % downloaded)
            print('\n Info downloaded: %s' % downloaded)
            return downloaded
def download(self, bands=None, download_dir=None, metadata=False):
        """Download remote .tar.bz file."""
        if not download_dir:
            download_dir = DOWNLOAD_DIR

        if bands is None:
            bands = list(range(1, 12)) + ['BQA']
        else:
            self.validate_bands(bands)

        pattern = re.compile('^[^\s]+_(.+)\.tiff?', re.I)
        band_list = ['B%i' % (i,) if isinstance(i, int) else i for i in bands]
        image_list = []

        # Connect Earth explore
        self.connect_earthexplorer()

        # tgz name
        tgzname = self.sceneInfo.name + '.tgz'
        dest_dir = check_create_folder(join(download_dir, self.sceneInfo.name))

        # Download File
        downloaded = self.download_file(self.url, dest_dir, tgzname)

        # Log
        logger.debug('Status downloaded %s' % downloaded)
        print('\n Status downloaded %s' % downloaded)

        if downloaded['sucess']:

            # Log
            print('\n Downloaded sucess')
            logger.debug('Downloaded sucess of scene: %s' % self.sceneInfo.name)

            try:

                tar = tarfile.open(downloaded['file_path'], 'r')
                folder_path = join(download_dir, self.sceneInfo.name)
                tar.extractall(folder_path)
                remove(downloaded['file_path'])
                images_path = listdir(folder_path)

                for image_path in images_path:
                    matched = pattern.match(image_path)
                    file_path = join(folder_path, image_path)
                    if matched and matched.group(1) in band_list:
                        image_list.append([file_path, getsize(file_path)])
                    elif matched:
                        remove(file_path)
            except tarfile.ReadError as error:
                print('\nError when extracting files. %s' % error)
                logger.error('Error when extracting files. %s' % error)
            return image_list
        else:
            logger.debug('Info downloaded: %s' % downloaded)
            print('\n Info downloaded: %s' % downloaded)
            return downloaded
Download remote .tar.bz file.