Extract 2 satellite image files in a list within ".tar.gz"

1

I have a list of Landsat satellite images. Within the tar.gz file are the bands

ex: File " LC08018051fecha.tar.gz "
Contains:

  

LC08_fecha_band1.tif
  LC08_fecha_band2.tif
  LC08_fecha_band3.tif
  LC08_fecha_band4.tif
  LC08_fecha_band5.tif
  LC08_fecha_band6.tif
  LC08_fecha_band7.tif
  LC08_fecha_azimuth_band4.tif
  LC08_fecha_zenith_band4.tif
  LC08_fecha_pixel_qa.tif
  LC08_fecha_aerosol.tif

I need to extract from all files .tar.gz only LC08_date_band3.tif and < em> LC08_date_band4.tif to calculate an index.

The following code works for a specific file, extracting everything that says band

fileName = "LC08018051fecha.tar.gz"
tfile = tarfile.open(fileName, 'r:gz')
membersList = tfile.getmembers()
namesList = tfile.getnames()
bandsList = [x for x, y in zip(membersList, namesList) if "band" in y]
print("extracting...")
tfile.extractall("folder/",members=bandsList)
print ("Done")

I need to make it generic for all tar.gz within a folder and just extract band 3 and 4.

    
asked by OSCAR_P 08.06.2017 в 17:44
source

1 answer

1

It occurs to me that you could use glob to filter the files and then go through each one, for example something like this:

import glob

files = glob.glob('LC*.tar.gz')
if not files: 
   raise Exception('No se han encontrado archivos a procesar')

for fileName in files:
    tfile = tarfile.open(fileName, 'r:gz')
    membersList = tfile.getmembers()
    namesList = tfile.getnames()
    bandsList = [x for x, y in zip(membersList, namesList) if "band" in y]
    print("extracting...")
    tfile.extractall("folder/",members=bandsList)
    print ("Done")
    
answered by 08.06.2017 в 17:56