def runCorrection(insigma, inlinc, inpix, outsigma, thetaref=39.0, nFactor=1.0, filterSize=None): controls = applier.ApplierControls() # Set up input images infiles = applier.FilenameAssociations() infiles.insigma = insigma infiles.inlinc = inlinc infiles.inpix = inpix # Set up output image outfiles = applier.FilenameAssociations() outfiles.outimage = outsigma # Set format for output image outControls = getOutDriver(outsigma) # Set options controls.setOutputDriverName(outControls['gdalFormat']) controls.setCreationOptions(outControls['gdalCOOptions']) controls.setCalcStats(outControls['calcStats']) # Set up parameters otherargs = applier.OtherInputs() otherargs.thetaref = thetaref otherargs.nFactor = nFactor otherargs.filterSize = filterSize # Run correction controls.progress = cuiprogress.CUIProgressBar() applier.apply(castelCorrection, infiles, outfiles, otherargs, controls=controls)
def writeOutImg(inputArray, outfile, n, m, c, TLX, TLY, nulVal, proj, dType): # Write the output DEM into an image file with GDAL nBands = 1 drvr = gdal.GetDriverByName('HFA') ds = drvr.Create(outfile, n, m, nBands, dType, ['COMPRESS=YES']) band = ds.GetRasterBand(1) band.WriteArray(inputArray) ds.SetGeoTransform((TLX, c, 0, TLY, 0, -c)) ds.SetProjection(proj) progress = cuiprogress.CUIProgressBar() calcstats.calcStats(ds, progress, ignore=nulVal) del ds
def calcAverage(file1, file2, avgfile): """ Use RIOS to calculate the average of two files. Allows nearest-neighbour resampling of the second file. """ infiles = applier.FilenameAssociations() outfiles = applier.FilenameAssociations() infiles.img = [file1, file2] outfiles.avg = avgfile controls = applier.ApplierControls() controls.setReferenceImage(file1) controls.setResampleMethod('near') controls.setProgress(cuiprogress.CUIProgressBar()) applier.apply(doAvg, infiles, outfiles, controls=controls)
def apply_rf_image(in_data_stack, out_image, rf_model, nodata_vals): """ Apply Random Forests model generated by scikit-learn to an input data stack and output image Requires: * in_data_stack - stack of all layers * out_image - output image * rf_model - model produced by scikit-learn * nodata_vals - array with a no-data value for each band Returns the mean and standard deviation of the output (predicted) image. """ # Apply to image infiles = applier.FilenameAssociations() infiles.inimage = in_data_stack outfiles = applier.FilenameAssociations() outfiles.outimage = out_image otherargs = applier.OtherInputs() otherargs.rf = rf_model otherargs.predict_sm = None # Array to store output SM # Pass in list of no data values for each layer otherargs.nodata_vals_list = nodata_vals controls = applier.ApplierControls() controls.setOutputDriverName( upscaling_utilities.get_gdal_format(out_image)) controls.setCalcStats(False) controls.progress = cuiprogress.CUIProgressBar() applier.apply(_rios_apply_rf_image, infiles, outfiles, otherargs, controls=controls) average_sm_predict = otherargs.predict_sm.mean() sd_sm_predict = otherargs.predict_sm.std() return average_sm_predict, sd_sm_predict
def createPlots(imageA, imageB, outPlot, bandA=1, bandB=1, labelA=None, labelB=None, plotMin=None, plotMax=None, scale=1, independentXY=False): controls = applier.ApplierControls() # Set up input images infiles = applier.FilenameAssociations() infiles.imageA = imageA infiles.imageB = imageB outfiles = applier.FilenameAssociations() # Set up parameters otherargs = applier.OtherInputs() otherargs.bandA = bandA - 1 otherargs.bandB = bandB - 1 otherargs.outdataA = numpy.array([], dtype=numpy.float32) otherargs.outdataB = numpy.array([], dtype=numpy.float32) otherargs.scale = scale # Get data print('Extracting data') controls.progress = cuiprogress.CUIProgressBar() # If necessary reproject image B to image A controls.setReferenceImage(infiles.imageA) controls.setResampleMethod('near') applier.apply(getData, infiles, outfiles, otherargs, controls=controls) # Produce plot print('Saving plot') plotData(otherargs.outdataA, otherargs.outdataB, outPlot, labelA, labelB, plotMin, plotMax, independentXY)
def run_s2cloudless(input_img, out_prob_img, out_cloud_msk, gdalformat): """ Function which runs the S2Cloudless methods in 256 x 256 pixel blocks across an image. :param input_img: input sentinel-2 image with all 13 bands. :param output_img: the output image cloud mask :param gdalformat: the GDAL image file format of the output image file. """ s2_pxl_cloud_detect = S2PixelCloudDetector(all_bands=True) infiles = applier.FilenameAssociations() infiles.s2image = input_img outfiles = applier.FilenameAssociations() outfiles.out_prob_img = out_prob_img outfiles.out_cloud_msk = out_cloud_msk otherargs = applier.OtherInputs() otherargs.s2_pxl_cloud_detect = s2_pxl_cloud_detect aControls = applier.ApplierControls() aControls.progress = cuiprogress.CUIProgressBar() aControls.drivername = gdalformat aControls.omitPyramids = True aControls.calcStats = False def _applyS2Cloudless(info, inputs, outputs, otherargs): """ This is an internal rios function """ # Current shape is: [13 x n x m] # Image data needs to be in shape [1 x n x m x 13] s2img_reshp = numpy.expand_dims(numpy.stack([inputs.s2image[0], inputs.s2image[1], inputs.s2image[2], inputs.s2image[3], inputs.s2image[4], inputs.s2image[5], inputs.s2image[6], inputs.s2image[7], inputs.s2image[8], inputs.s2image[9], inputs.s2image[10], inputs.s2image[11], inputs.s2image[12]], axis=2), axis=0) s2img_reshp_toa = s2img_reshp/10000.0 outputs.out_prob_img = otherargs.s2_pxl_cloud_detect.get_cloud_probability_maps(s2img_reshp_toa) outputs.out_cloud_msk = otherargs.s2_pxl_cloud_detect.get_mask_from_prob(outputs.out_prob_img) applier.apply(_applyS2Cloudless, infiles, outfiles, otherargs, controls=aControls)
def converttodB(info, inputs, outputs): """ Converts to dB using: 10*log10(b1) """ outputs.outimage = np.where(inputs.inimage > 0, 10 * np.log10(inputs.inimage), 0) if len(sys.argv) == 3: inImage = sys.argv[1] outImage = sys.argv[2] else: print('''Not enough parameters provided. Usage: convert2dB.py inImage outImage ''') exit() infiles = applier.FilenameAssociations() infiles.inimage = inImage outfiles = applier.FilenameAssociations() outfiles.outimage = outImage controls = applier.ApplierControls() controls.progress = cuiprogress.CUIProgressBar() controls.setCalcStats(True) applier.apply(converttodB, infiles, outfiles, controls=controls)
def calcSolarAzimuthZenith(inputImg, inImgDateTime, outputImg, gdalformat): """ Function which calculate a solar azimuth (band 1) and zenith (band 2) image. :param inputImg: input image file (can be any image with a spatial header) :param inImgDateTime: a datatime object for the data/time of the acquasition :param outputImg: output image file and path :param gdalformat: output file format (e.g., KEA) """ if not havePysolar: raise Exception( "The PySolar module required for this function could not be imported." ) if not haveRIOS: raise Exception( "The RIOS module required for this function could not be imported." ) infiles = applier.FilenameAssociations() infiles.image1 = inputImg outfiles = applier.FilenameAssociations() outfiles.outimage = outputImg otherargs = applier.OtherInputs() otherargs.dateTime = inImgDateTime aControls = applier.ApplierControls() aControls.progress = cuiprogress.CUIProgressBar() aControls.drivername = gdalformat wgs84latlonProj = osr.SpatialReference() wgs84latlonProj.ImportFromEPSG(4326) otherargs.wgs84latlonProj = wgs84latlonProj def _calcSolarAzimuthZenith(info, inputs, outputs, otherargs): """ Internal functions used within calcSolarAzimuthZenith() - don't call independently. """ xBlock, yBlock = info.getBlockCoordArrays() inProj = osr.SpatialReference() inProj.ImportFromWkt(info.getProjection()) transform = osr.CoordinateTransformation(inProj, otherargs.wgs84latlonProj) xBlockF = xBlock.flatten() yBlockF = yBlock.flatten() pts = numpy.zeros((xBlockF.shape[0], 2), dtype=numpy.float) pts[..., 0] = xBlockF pts[..., 1] = yBlockF outPts = numpy.array(transform.TransformPoints(pts)) outAz = numpy.zeros_like(xBlockF, dtype=numpy.float) outZen = numpy.zeros_like(xBlockF, dtype=numpy.float) for i in range(outPts.shape[0]): outAz[i] = 90 - Pysolar.solar.GetAltitude( outPts[i, 1], outPts[i, 0], otherargs.dateTime) outZen[i] = ((-1) * Pysolar.solar.GetAzimuth( outPts[i, 1], outPts[i, 0], otherargs.dateTime)) - 180 outAz = numpy.reshape(outAz, xBlock.shape) outZen = numpy.reshape(outZen, xBlock.shape) #print("Block End") outputs.outimage = numpy.stack((outAz, outZen)) # Apply the multiply function. applier.apply(_calcSolarAzimuthZenith, infiles, outfiles, otherargs, controls=aControls)