-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountSamples.py
More file actions
executable file
·70 lines (49 loc) · 1.8 KB
/
Copy pathCountSamples.py
File metadata and controls
executable file
·70 lines (49 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/Library/Frameworks/EPD64.framework/Versions/Current/bin/python
import glob
import os
import sys
import numpy
import osgeo.gdal as gdal
from osgeo.gdalconst import *
def GetFileList(DataDir):
FileList = glob.glob(DataDir + '/AVH09C1.A???????.N??.???.?????????????.tif')
FileList.sort()
Year = numpy.zeros((len(FileList)), numpy.int16)
DoY = numpy.zeros((len(FileList)), numpy.int16)
i = 0
for File in FileList:
# Get Year and DoY from filename
YearOfObservation = os.path.basename(File).split('.')[1][1:5]
DoYOfObservation = os.path.basename(File).split('.')[1][5:8]
Year[i] = YearOfObservation
DoY[i] = DoYOfObservation
i += 1
return FileList, Year, DoY
def GetDimensions(File):
dataset = gdal.Open(File, GA_ReadOnly)
# Usually the AVH09C1 layerstack should contain 10 bands
rows, cols, NumberOfBands = dataset.RasterYSize, dataset.RasterXSize, dataset.RasterCount
dataset = None
return rows, cols, NumberOfBands
#--------------------------------------------------------------------------------#
from IPython import embed
DataDir = sys.argv[1]
FileList, Year, DoY = GetFileList(DataDir)
# From the first file get dimensions
rows, cols, NumberOfBands = GetDimensions(FileList[0])
# Create aray where to store the NumberOfSamples
NumberOfSamples = numpy.zeros((rows, cols), numpy.float32)
FileNumber = 1
for File in FileList:
print File
dataset = gdal.Open(File, GA_ReadOnly)
reflectance = dataset.GetRasterBand(1).ReadAsArray()
indices = numpy.where(reflectance > 0)
NumberOfSamples[indices] = NumberOfSamples[indices] + 1.0
dataset = None
print "Writing results to a file..."
format = "GTiff"
driver = gdal.GetDriverByName(format)
new_dataset = driver.Create( 'NumberOfSamples.tif', cols, rows, GDT_Float32 )
new_dataset.GetRasterBand(1).WriteArray(NumberOfSamples)
new_dataset = None