-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathndvi_to_float.py
More file actions
40 lines (33 loc) · 1.1 KB
/
ndvi_to_float.py
File metadata and controls
40 lines (33 loc) · 1.1 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
from osgeo import gdal, ogr, osr, gdalconst
import glob
import os
indir='E:/tmp/exportJB'
outdir='E:/tmp/exportJBFloat/'
scale_factor=0.001
offset=0
nodata=-999
nodataOut=-999
# get list of files
lstFiles = glob.glob('{}/ndvi_*.tif'.format(indir))
for thisFilepath in lstFiles:
print 'Processing {}'.format(thisFilepath)
thisFile=os.path.basename(thisFilepath)
newFile=thisFile.replace('.tif','_float.tif')
outFile = os.path.join(outdir, newFile)
print outFile
thisFid = gdal.Open(thisFilepath, gdalconst.GA_ReadOnly)
ns = thisFid.RasterXSize
nl = thisFid.RasterYSize
thisData = thisFid.GetRasterBand(1).ReadAsArray(0,0,ns,nl)
wnodata = thisData==nodata
thisData = thisData * scale_factor + offset
if wnodata.any():
thisData[wnodata] = nodataOut
# create output
fidOut = gdal.GetDriverByName('gtiff').Create(outFile, ns, nl, 1, gdalconst.GDT_Float32, options=['compress=lzw'] )
#print 'Output: {} {}'.format(outFile, fidOut)
fidOut.SetProjection(thisFid.GetProjection())
fidOut.SetGeoTransform(thisFid.GetGeoTransform())
fidOut.GetRasterBand(1).WriteArray(thisData, 0 , 0)
fidOut = None
thisFile=None