This video explores how to create a script to extract (unzip) a Tar file and create a Folder tree to hold our Landsat images and folders for future analysis.
The previous videos in the series are useful knowledge for topics in this video.
MyPhysical World on Facebook: www.facebook\myphysicalworld
Twitter: @my_physical
Instagram: @MyPhysicalWorld
Map Store: myphysicalworld.company.site
PyCharm is available at: www.jetbrain.com (free for the Community version)
ArcGIS is a registered product of Environmental Systems Research Institute (ESRI): www.esri.com
QGIS is opensource GIS software available at: www.qgis.org
Code used in this video:
import os
import tarfile
import arcpy
------------------------------------------------Variable definitions
imgZipDir = r"D:\GISData\LandsatZips"
imgMain = r"D:\GISProgramming\Landsat"
imgFile = "LC08_L1TP_042035_20200912_20200912_01_RT.tar.gz"
imgFRoot = imgFile[0:40] + '_'
imgFDate = imgFile[17:25]
imgPathrow = imgFile[10:16]
imgFP = os.path.join(imgZipDir, imgFile)
print(os.path.join(imgMain, "LS_" + imgFDate))
imgNewDir = os.path.join(imgMain, "LS_" + imgFDate)
----------------------------------------------Create Satellite Image Directory Tree
try:
os.makedirs(imgNewDir)
os.makedirs(imgNewDir + "\Radiance")
os.makedirs(imgNewDir + "\Reflectance")
os.makedirs(imgNewDir + "\Composite")
os.makedirs(imgNewDir + "\Indices")
except OSError:
print("Creation of the Folder %s failed" % imgFDate)
else:
print("New Folder %s has been created." % imgFDate)
--------------------------------------------- Extract the tar.gz file
with tarfile.open(imgFP, 'r') as tarObj:
tarObj.extractall(imgNewDir)
tarObj.close()
print(imgFRoot + " Has been extracted...")
---------------------------------------------- Process the image Metafile