I am trying to interpret HDF-files or more precisely the Rasterbands inside the files. There is a Band that is used for Quality Assessment which represents Bit-Information as corresponding integers (e.g. 01000000 as 64).
Depending on specific bits I want to get a count, e.g. how many of the Pixels are 1 on bit position 5. If they are taken into account by a previous count, the should not get count again. Right now I am changing the entries in each cell depending on a priority-list by myself. This takes ages and I am really sure that there are faster ways since I have never worked with bits before.
Here is my code:
from osgeo import gdal
import numpy as np
QA_Band = gdal.Open(hdf.GetSubDatasets()[B][0],gdal.GA_ReadOnly)
QA = QA_Band.ReadAsArray()
# Calculate Bit-Representation of QA-Band
bin_vec = np.vectorize(np.binary_repr)
QAB = bin_vec(QA, width = 8)
# Filter by Bit-Values
QAB = np.where(QAB == '11111111', "OutOfSwath", QAB)
for i in range(QAB.shape[0]):
for j in range(QAB.shape[1]):
if QAB[i,j][6] == '1':
QAB[i,j] = "Cloud"
Cloud = (QAB == "Cloud").sum()
elif QAB[i,j][4] == '1':
QAB[i,j] = "Cloud Shadow"
Shadow = (QAB == "Cloud Shadow").sum()
elif QAB[i,j][5] == '1':
QAB[i,j] = "Adjacent Cloud"
AC = (QAB == "Adjacent Cloud").sum()
elif QAB[i,j][7] == '1':
QAB[i,j] = "Cirrus"
Cirrus = (QAB == "Cirrus").sum()
elif QAB[i,j][3] == '1':
QAB[i,j] = "Snow/Ice"
SnowC = (QAB == "Snow/Ice").sum()
elif QAB[i,j][2] == '1':
QAB[i,j] = "Water"
WaterC = (QAB == "Water").sum()
elif QAB[i,j][0:1] == '11':
QAB[i,j] = "High Aerosol"
elif QAB[i,j][0:1] == '10':
QAB[i,j] = "Avrg. Aerosol"
elif QAB[i,j][0:1] == '01':
QAB[i,j] = "Low Aerosol"
elif QAB[i,j][0:1] == '00':
QAB[i,j] = "Aerosol Climatology"
This will lead to an array of strings representing the different things, but takes ages as said before.
Any help in how to access the Bit-Representation would be helpful :)