def matrix(self):
"""
This function transforms the images into a single numpy array with dimensions
pixels by observations. If the image has 100 pixels for 1 year (23 observations)
then this matrix should have dimensions 100 rows by 23 columns. The matrix
includes the quality mask dataset. This matrix is not yet masked for quality control.
"""
dataCount = self.subset.count('1')
dataNames = sorted(glob.glob(self.fullPath + '/*.tif'))
dataNames = dataNames[0:dataCount]
subsetInt = [int(s) for s in self.subset.split() if s.isdigit()]
DC = np.empty(shape = (self.rows*self.columns*self.observations,0))
DCs = np.empty(shape = (self.rows*self.columns*self.observations, subsetInt.count(1)))
for i in range(dataCount):
name = str(dataNames[i])
dataList = sorted(glob.glob(self.fullPath + '/*' + name[-10:-4] + '.tif'))
bandDC = np.empty((0, 1))
for b in dataList:
data = gdal.Open(str(b), GA_ReadOnly).ReadAsArray()
vec = data.reshape((self.rows*self.columns, 1))
bandDC = np.append(bandDC, vec, axis = 0)
DC = np.append(DC, bandDC, axis = 1)
del vec, bandDC, data
#apply fill values
if self.dataset == 'MOD15A2.005' or self.dataset == 'MOD17A2.005':
DC[DC>self.fillValue] = 9999.0
if self.dataset == 'MOD11A2.005':
DC[:,0][DC[:,0] == self.fillValue] = 9999.0 #this should have fixed it!
else:
DC[DC == self.fillValue] = 9999.0
#scale dataset
count = 0
for i in range(len(subsetInt)):
if subsetInt[i] == 1:
DCs[:,count] = np.multiply(DC[:,count], self.scale[i])
count += 1
DCs[DC == 9999.0] = 9999.0
self.DC = DCs
del DC
#metadata function
with open(self.fullPath + '/' + 'metadata_' + self.dataset + '.txt', 'w') as f:
f.write(' '.join(["self.%s = %s" % (k,v) for k,v in self.__dict__.iteritems()]))
logger.log('SUCCESS', 'The %s data was transformed into an array with dimensions %d rows by %d columns. No data value set to 9999. A metadata file with object attributes was created. To access the matrix, simply call object.DC' % (str(self.outformat), self.DC.shape[0], self.DC.shape[1]))
tif = sorted(glob.glob(self.fullPath + '/*.tif'))
for t in tif:
os.remove(t)
评论列表
文章目录