# Check version of python:print(sys.version)# We can check which version we're running by printing the "__version__" variableprint("GDAL's version is: "+gdal.__version__)# This shows where the version of GDAL is located (in this case, in a conda environment)print(gdal)# This allows GDAL to throw Python Exceptionsgdal.UseExceptions()

in_ds=gdal.Open(LC08band2_path,gdal.GA_ReadOnly)# Find the number of bands in the dataset object.print("[ NUMBER OF BANDS ] = ",in_ds.RasterCount)

[ NUMBER OF BANDS ] = 1

In [7]:

# Create a GDAL raster band object from the only band in the dataset object.in_band=in_ds.GetRasterBand(1)print("[ BAND DATA TYPE ] = ",gdal.GetDataTypeName(in_band.DataType))print("[ NO DATA VALUE ] = ",in_band.GetNoDataValue())

in_ds=gdal.Open(LC08band2_path,gdal.GA_ReadOnly)# Define path to save copy and with what driver.dst_filename=os.path.join(LC08proc_path,"LC08_B2.vrt")driver=gdal.GetDriverByName("VRT")dst_ds=driver.CreateCopy(dst_filename,in_ds,strict=0)print("VRT copy of Band 2 has been created.")dst_filename=os.path.join(LC08proc_path,"LC08_B2.dat")driver=gdal.GetDriverByName("ENVI")dst_ds=driver.CreateCopy(dst_filename,in_ds,strict=0)print("ENVI copy of Band 2 has been created.")# Close initial and resulting datasets.dst_ds=Nonein_ds=None

VRT copy of Band 2 has been created.
ENVI copy of Band 2 has been created.

Get Sentinel-2 bands in a sorted order accessible via key-value pairs.

In [14]:

# Create dictionary with key-value pairs for easier access to bands.S2_bands={}forentryinos.listdir(S2_path):if(entry.endswith('.jp2')):# This makes bands accessible by name (e.g. B01, B8A).band_name=entry[-7:-4]S2_bands[band_name]=os.path.join(S2_path,entry)# Using the dictionary, individual bands can be accessed so:print("Band 8A is called: "+os.path.basename(S2_bands['B8A']))delband_name