Salutare,
Incerc sa implementeaz un dialog box pentru selectarea unui working directory pentru un script de Python. As vrea sa fac in asa fel incat scriptul sa nu aiba niciun working directory prestabilit ci sa trebuiasca definit atunci cand e rulat scriptul printr-un un mic dialog box.
Asta e scriptul:
# import modules
import os, numpy, sys, time
from osgeo import gdal, ogr
from osgeo.gdalconst import *
startTime = time.time()
# set the working directory
os.chdir(r'F:\NDVI_Py\data')
# register all of the GDAL drivers
gdal.AllRegister()
# open the image
inDs = gdal.Open('L82015.img', GA_ReadOnly)
if inDs is None:
print 'Could not open L82015.img'
sys.exit(1)
# get image size
rows = inDs.RasterYSize
cols = inDs.RasterXSize
bands = inDs.RasterCount
# get the bands and block sizes
inBand4 = inDs.GetRasterBand(4)
inBand5 = inDs.GetRasterBand(5)
blockSizes = inBand4.GetBlockSize()
xBlockSize = blockSizes[0]
yBlockSize = blockSizes[1]
# create the output image
driver = gdal.GetDriverByName('HFA')
outDs = driver.Create('ndvi_L82015.img', cols, rows, 1, GDT_Float32)
if outDs is None:
print 'Could not create ndvi_L82015.img.img'
sys.exit(1)
outBand = outDs.GetRasterBand(1)
# loop through the rows
for i in range(0, rows, yBlockSize):
if i + yBlockSize < rows:
numRows = yBlockSize
else:
numRows = rows - i
# loop through the columns
for j in range(0, cols, xBlockSize):
if j + xBlockSize < cols:
numCols = xBlockSize
else:
numCols = cols - j
# read the data in
data4 = inBand4.ReadAsArray(j, i, numCols, numRows).astype(numpy.float)
data5 = inBand5.ReadAsArray(j, i, numCols, numRows).astype(numpy.float)
# do the calculations
mask = numpy.greater(data4 + data5, 0)
ndvi = numpy.choose(mask, (-99, (data5 - data4) / (data5 + data4 + 0.00000000001)))
# write the data
outBand.WriteArray(ndvi, j, i)
# flush data to disk, set the NoData value and calculate stats
outBand.FlushCache()
outBand.SetNoDataValue(-99)
stats = outBand.GetStatistics(0, 1)
# georeference the image and set the projection
outDs.SetGeoTransform(inDs.GetGeoTransform())
outDs.SetProjection(inDs.GetProjection())
# build pyramids
gdal.SetConfigOption('HFA_USE_RRD', 'YES')
outDs.BuildOverviews(overviewlist=[2,4,8,16,32,64,128])
inDs = None
outDs = None
print 'NDVI Calculated and Outputted to File F:\NDVI_Py\data\L82015.img'
print 'script took', time.time() - startTime, 'seconds to run'
Stiu ca se foloseste tkFileDialog.askdirectory pt asta dar nu stiu cum ar trebui scris in loc de:
# set the working directory
os.chdir(r'F:\NDVI_Py\data')
Apreciez orice sfat. Multumesc!