def write_output_image(propertiestocopy, outname, data, nodata): raster = copySchemaToNewImage(propertiestocopy, outname, datatype=GDT_Int16) raster_band = raster.GetRasterBand(1) raster_band.WriteArray(data, 0, 0) raster_band.SetNoDataValue(nodata) raster_band.FlushCache() raster_band = None raster = None
def process_reference(outputdir, signature, array, imageproperties, startDOY, doyinterval, bestguess, ndvalue, bounds, meantype=None, subset=None, fitmthd=None, thresh=None): #TODO docstrings outfileName = os.path.join(outputdir, signature.name) + ".tif" logfilename = os.path.join(outputdir, signature.name) + ".txt" reflog = log(logfilename, verbosity=LOGGINGLEVEL) try: #Create output rasters for each crop type to hold residual values from fit and arrays reflog.log("Creating {0} output raster...".format(signature.name)) outfileName = os.path.join(outputdir, signature.name) + ".tif" outfile = copySchemaToNewImage(imageproperties, outfileName, numberofbands=1) outdataset = outfile.GetRasterBand(1) outarray = numpy.zeros(shape=(imageproperties.rows, imageproperties.cols)) outarray[outarray == 0] = ndvalue reflog.log("Created {0} raster.".format(signature.name)) #Interpolate reference values and return dict with key as type and curve as value interpolatedCurve = interpolate.splrep(signature.daysofyear, signature.vivalues) #Iterate through each pixel and calculate the fit for each ref curve; write RMSE to array if subset: for row, col in subset: if row < 0 or col < 0 or row >= imageproperties.rows or col >= imageproperties.cols: #print("Pixel not in processing extent; skipping.") pass else: outarray = process_pixel(bestguess, col, signature.name, doyinterval, fitmthd, array, interpolatedCurve, outarray, row, startDOY, ndvalue, bounds, meantype=meantype, thresh=thresh, logging=reflog) else: for row in range(0, imageproperties.rows): for col in range(0, imageproperties.cols): outarray = process_pixel(bestguess, col, signature.name, doyinterval, fitmthd, array, interpolatedCurve, outarray, row, startDOY, ndvalue, bounds, meantype=meantype, thresh=thresh, logging=reflog) #Write output array values to file reflog.log("Writing {0} output file...".format(signature.name)) outdataset.WriteArray(outarray, 0, 0) outdataset.SetNoDataValue(ndvalue) reflog.log("Processing {0} finished.".format(signature.name)) except Exception as e: import traceback exc_type, exc_value, exc_traceback = sys.exc_info() reflog.log(e) traceback.print_exception(exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout) traceback.print_exception(exc_type, exc_value, exc_traceback, limit=2, file=reflog.file) try: print(row, col) except: pass finally: print("\nClosing files...") try: outdataset = None except: pass try: outfile = None except: pass