def robustLronacPcAlignCall(demPointsPath, lolaPath, outputPrefix):

    # Use pc-align to compare points to LOLA DEM, compute rotation and offset
    
    # The max-displacement threshold will be adjusted until we are either using a certain number of LOLA points
    #  or until we are using a certain percentage of the input points.
    MIN_NUM_LOLA_POINTS    = 4000
    MIN_LOLA_PERCENTAGE    = 0.005
    STARTING_DISPLACEMENT  = 50    # --max-displacement starts at this value..
    DISPLACEMENT_INCREMENT = 50    #                    Increments by this value...
    MAX_MAX_DISPLACEMENT   = 1800  #                    And we quit when it hits this value.


    # Determine the number of points we want
    numLolaPoints          = IrgFileFunctions.getFileLineCount(lolaPath) - 1
    currentMaxDisplacement = STARTING_DISPLACEMENT
    minNumPointsToUse      = min(MIN_NUM_LOLA_POINTS, int(MIN_LOLA_PERCENTAGE*float(numLolaPoints)))
    
    print 'Using pc_align to compute a transform between intersected points and LOLA points...'
    print 'Starting pc_align, looking for ' + str(minNumPointsToUse) + ' lola point matches.' 
   
    transformPath  = outputPrefix + '-inverse-transform.txt'
    endErrorPath   = outputPrefix + '-end_errors.csv'
        
    while(True):
        cmd = ('pc_align --highest-accuracy --max-displacement ' + str(currentMaxDisplacement) + ' --datum D_MOON ' + 
               '--save-inv-transformed-reference-points ' + demPointsPath + 
               ' ' + lolaPath + ' -o ' + outputPrefix + ' --compute-translation-only')
        print cmd
        os.system(cmd)
        
        if not os.path.exists(endErrorPath):
            numLolaPointsUsed = 0 # Failed to produce any output, maybe raising the error cutoff will help?
        else:
            numLolaPointsUsed = IrgFileFunctions.getFileLineCount(endErrorPath) - 1
    
        if (numLolaPointsUsed >= minNumPointsToUse):
            break # Success!
        elif (currentMaxDisplacement >= MAX_MAX_DISPLACEMENT): # Hit the maximum max limit!
            raise Exception('Error! Unable to find a good value for max-displacement in pc_align.  Wanted '
                            + str(minNumPointsToUse) + ' points, only found ' + str(numLolaPointsUsed))
        else: # Try again with a higher max limit
            print ('Trying pc_align again, only got ' + str(numLolaPointsUsed)
                   + ' lola point matches with value ' + str(currentMaxDisplacement))
            currentMaxDisplacement = currentMaxDisplacement + DISPLACEMENT_INCREMENT            


    if not os.path.exists(transformPath):
        raise Exception('pc_align call failed!')

    return True    
def writeColorMapInfo(colormapPath, lutFilePath, demPath, outputPath):
    """Writes a file containing the color map information"""
    
    colormapPercentiles = []
    numColorSteps = IrgFileFunctions.getFileLineCount(lutFilePath)
    for i in range(0,numColorSteps): # This loop generates the percentage values from the color profile we are using
        colormapPercentiles.append(i / float(numColorSteps-1))
    # Get the min and max elevation of the DEM
    elevationBounds = IrgGeoFunctions.getImageStats(demPath) 
    # Get elevation values for each percentile
    colormapPercentileValues = IrgMathFunctions.getPercentileValues(elevationBounds[0][0], elevationBounds[0][1], colormapPercentiles)
    
    # Now write out a version of the LUT file with values added
    inputFile  = open(lutFilePath, 'r')
    outputFile = open(outputPath,  'w')

    # Add a header line to the output file
    outputFile.write('Percent of range, R, G, B, Elevation (meters above datum)\n')

    # Write a copy of the input file with the elevation values appended to each line    
    for (line, value) in zip(inputFile, colormapPercentileValues):
        newLine = line[:-1] + ', ' + str(round(value,2)) + '\n'
        outputFile.write(newLine) 
        
    inputFile.close()
    outputFile.close()