def getCubeCenterLatitude(cubePath, workDir='tmp'): """Calls caminfo on a cube and returns the CenterLatitude value""" # Make sure the requested file is present if not os.path.exists(cubePath): raise Exception('File ' + cubePath + ' does not exist!') # Call caminfo (from ISIS) on the input cube to find out the CenterLatitude value camInfoOuputPath = workDir + "/camInfoOutput.txt" cmd = 'caminfo from=' + cubePath + ' to=' + camInfoOuputPath print (cmd) os.system(cmd) if not os.path.exists(camInfoOuputPath): raise Exception('Call to caminfo failed on file ' + cubePath) # Read in the output file to extract the CenterLatitude value centerLatitude = -9999 infoFile = open(camInfoOuputPath, 'r') for line in infoFile: if (line.find('CenterLatitude') >= 0): centerLatitude = asp_string_utils.getNumberAfterEqualSign(line, ) break # Make sure we found the desired value if (centerLatitude == -9999) or (isinstance(centerLatitude, basestring)): raise Exception("Unable to find CenterLatitude from file " + cubePath) # Clean up temporary file os.remove(camInfoOuputPath) return centerLatitude
def getImageStats(imagePath): """Obtains some image statistics from gdalinfo""" if not os.path.exists(imagePath): raise Exception('Image file ' + imagePath + ' not found!') # Call command line tool silently cmd = ['gdalinfo', imagePath, '-stats'] p = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True) textOutput, err = p.communicate() # Statistics are computed seperately for each band bandStats = [] band = 0 while (True): # Loop until we run out of bands # Look for the stats line for this band bandString = 'Band ' + str(band + 1) + ' Block=' bandLoc = textOutput.find(bandString) if bandLoc < 0: return bandStats # Quit if we did not find it # Now parse out the statistics for this band bandMaxStart = textOutput.find('STATISTICS_MAXIMUM=', bandLoc) bandMeanStart = textOutput.find('STATISTICS_MEAN=', bandLoc) bandMinStart = textOutput.find('STATISTICS_MINIMUM=', bandLoc) bandStdStart = textOutput.find('STATISTICS_STDDEV=', bandLoc) bandMax = asp_string_utils.getNumberAfterEqualSign( textOutput, bandMaxStart) bandMean = asp_string_utils.getNumberAfterEqualSign( textOutput, bandMeanStart) bandMin = asp_string_utils.getNumberAfterEqualSign( textOutput, bandMinStart) bandStd = asp_string_utils.getNumberAfterEqualSign( textOutput, bandStdStart) # Add results to the output list bandStats.append((bandMin, bandMax, bandMean, bandStd)) band = band + 1 # Move to the next band
def getImageStats(imagePath): """Obtains some image statistics from gdalinfo""" if not os.path.exists(imagePath): raise Exception('Image file ' + imagePath + ' not found!') # Call command line tool silently cmd = ['gdalinfo', imagePath, '-stats'] p = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True) textOutput, err = p.communicate() # Statistics are computed seperately for each band bandStats = [] band = 0 while (True): # Loop until we run out of bands # Look for the stats line for this band bandString = 'Band ' + str(band+1) + ' Block=' bandLoc = textOutput.find(bandString) if bandLoc < 0: return bandStats # Quit if we did not find it # Now parse out the statistics for this band bandMaxStart = textOutput.find('STATISTICS_MAXIMUM=', bandLoc) bandMeanStart = textOutput.find('STATISTICS_MEAN=', bandLoc) bandMinStart = textOutput.find('STATISTICS_MINIMUM=', bandLoc) bandStdStart = textOutput.find('STATISTICS_STDDEV=', bandLoc) bandMax = asp_string_utils.getNumberAfterEqualSign(textOutput, bandMaxStart) bandMean = asp_string_utils.getNumberAfterEqualSign(textOutput, bandMeanStart) bandMin = asp_string_utils.getNumberAfterEqualSign(textOutput, bandMinStart) bandStd = asp_string_utils.getNumberAfterEqualSign(textOutput, bandStdStart) # Add results to the output list bandStats.append( (bandMin, bandMax, bandMean, bandStd) ) band = band + 1 # Move to the next band