def main():

    print '#################################################################################'
    print "Running lronac2dem.py"

    try:
        try:
            usage = "usage: lronac2dem.py [--output <path>][--manual]\n  "
            parser = optparse.OptionParser(usage=usage)

            parser.set_defaults(keep=False)

            inputGroup = optparse.OptionGroup(parser, 'Input Paths')
            inputGroup.add_option("--left",  dest="leftPath",  help="Path to LE .IMG file")
            inputGroup.add_option("--right", dest="rightPath", help="Path to RE .IMG file")            
            inputGroup.add_option("--stereo-left",  dest="stereoLeft", 
                                  help="Path to LE .IMG file with overlapping view of --left file")
            inputGroup.add_option("--stereo-right", dest="stereoRight", 
                                  help="Path to RE .IMG file with overlapping view of --right file")

            inputGroup.add_option("--lola",    dest="lolaPath", help="Path to LOLA DEM")
            inputGroup.add_option("--asu",     dest="asuPath",  help="Path to ASU DEM")
            
            inputGroup.add_option("--node-file", dest="nodeFilePath", 
                                  help="Path to list of available computing nodes")

            parser.add_option_group(inputGroup)

            # The default working directory path is kind of ugly...
            parser.add_option("--workDir", dest="workDir",  help="Folder to store temporary files in")

            parser.add_option("--outputPrefix",  dest="outputPrefix",   help="Output prefix.")

            parser.add_option("--crop",  dest="cropAmount", 
                              help="Crops the output image to reduce processing time.")

            parser.add_option("--manual", action="callback", callback=man,
                              help="Read the manual.")
            parser.add_option("--keep", action="store_true", dest="keep",
                              help="Do not delete the temporary files.")
            (options, args) = parser.parse_args()

            if not options.leftPath: 
                parser.error("Need left input path")
            if not options.rightPath: 
                parser.error("Need right input path")
            if not options.stereoLeft: 
                parser.error("Need stereo left input path")
            if not options.stereoRight: 
                parser.error("Need stereo right input path")
            if not options.lolaPath: 
                parser.error("Need LOLA data path")
            if not options.outputPrefix: 
                parser.error("Need output prefix")

        except optparse.OptionError, msg:
            raise Usage(msg)

        startTime = time.time()

        # Set up the output folders
        outputFolder  = os.path.dirname(options.outputPrefix)
        inputBaseName = os.path.basename(options.leftPath)
        tempFolder    = outputFolder + '/' + inputBaseName + '_stereoCalibrationTemp/'
        if (options.workDir):
            tempFolder = options.workDir
        IrgFileFunctions.createFolder(outputFolder)
        hadToCreateTempFolder = not os.path.exists(tempFolder)
        IrgFileFunctions.createFolder(tempFolder)


        # Set up logging
        logPath = options.outputPrefix + '-Log.txt'
        logging.basicConfig(filename=logPath,level=logging.INFO)

        # These are the output mosaic paths
        filename         = os.path.splitext(options.leftPath)[0] + '.correctedMosaic.cub'
        mainMosaicPath   = os.path.join(tempFolder, os.path.basename(filename))
        filename         = os.path.splitext(options.stereoLeft)[0] + '.correctedMosaic.cub'
        stereoMosaicPath = os.path.join(tempFolder, os.path.basename(filename))


        # List of all the output files that will be created by makeDemAndCompare.py
        demPath               = options.outputPrefix + '-DEM.tif'
        intersectionErrorPath = options.outputPrefix + '-IntersectionErr.tif'
        hillshadePath         = options.outputPrefix + '-Hillshade.tif'
        colormapPath          = options.outputPrefix + '-Colormap.tif'
        colormapLegendPath    = options.outputPrefix + '-ColormapLegend.csv'
        confidencePath        = options.outputPrefix + '-Confidence.tif'
        confidenceLegendPath  = options.outputPrefix + '-ConfidenceLegend.csv'
        mapProjectLeftPath    = options.outputPrefix + '-MapProjLeft.tif'
        mapProjectRightPath   = options.outputPrefix + '-MapProjRight.tif'
        
        compressedInputPath  = options.outputPrefix + '-CompressedInputs.tar.bz2'
        compressedOutputPath = options.outputPrefix + '-CompressedOutputs.tar'
        compressedDebugPath  = options.outputPrefix + '-CompressedDiagnostics.tar.bz2'
        
        # List of .LBL text files to be created
        demLabelPath               = options.outputPrefix + '-DEM.LBL' 
        intersectionErrorLabelPath = options.outputPrefix + '-IntersectionErr.LBL'
        hillshadeLabelPath         = options.outputPrefix + '-Hillshade.LBL'
        colormapLabelPath          = options.outputPrefix + '-Colormap.LBL'
        confidenceLabelPath        = options.outputPrefix + '-Confidence.LBL'
        mapProjectLeftLabelPath    = options.outputPrefix + '-MapProjLeft.LBL'
        mapProjectRightLabelPath   = options.outputPrefix + '-MapProjRight.LBL'

        # List of diagnostic files we want to archive
        #  Currently specified below.  Should be expanded.



        # Call lronac2refinedMosaics.py if outputs do not already exist
        if ( (not os.path.exists(mainMosaicPath) or not os.path.exists(stereoMosaicPath)) 
             and not os.path.exists(demPath) ):
            refineTemp = os.path.join(tempFolder, 'refinement')
            cmdArgs = ['--left',          options.leftPath,
                       '--right',         options.rightPath,
                       '--stereo-left',   options.stereoLeft,
                       '--stereo-right',  options.stereoRight,
                       '--lola',          options.lolaPath,
                       '--workDir',       refineTemp,
                       '--output-folder', tempFolder,
                       '--log-path',      logPath]
            if options.keep:
                cmdArgs.append('--keep')
            print cmdArgs
            lronac2refinedMosaics.main(cmdArgs)
        
            # Copy the pc_align log file to the output folder
            pcAlignLogPath = os.path.join(tempFolder, 'pcAlignLog.txt')
            shutil.copyfile(pcAlignLogPath, options.outputPrefix + '-PcAlignLog.txt')

            # Check that we successfully created the output files
            if (not os.path.exists(mainMosaicPath) or not os.path.exists(stereoMosaicPath)):
                raise Exception('lronac2refinedMosaics failed to produce mosaics!')
 
        # Call makeDemAndCompare.py (This won't do much if outputs already exist)
        cmdArgs = ['--left',     mainMosaicPath, 
                   '--right',    stereoMosaicPath, 
                   '--lola',     options.lolaPath, 
                   '--workDir',  tempFolder, 
                   '--prefix',   options.outputPrefix, 
                   '--log-path', logPath]
        if options.keep:
            cmdArgs.append('--keep')
        if options.asuPath:
            cmdArgs.append('--asu')
            cmdArgs.append(options.asuPath)
        if options.cropAmount:
            cmdArgs.append('--crop')
            cmdArgs.append(str(options.cropAmount))
        if options.nodeFilePath:
            cmdArgs.append('--node-file')
            cmdArgs.append(options.nodeFilePath)
        print cmdArgs
        makeDemAndCompare.main(cmdArgs)
        
        print 'Finished image processing, setting up final output files...'
        logging.info('Started label file generation')

        # Get the data set name
        if (options.asuPath):  # If ASU has a name for this set, use it
            startPos    = options.asuPath.rfind('_') + 1
            stopPos     = options.asuPath.rfind('.') - 1
            dataSetName = options.asuPath[startPos:stopPos]
        else: # Generate a data set in format NAC_DTM_MXXXXXX_MXXXXXXX
            dataSetName = IsisTools.makeDataSetName(options.leftPath, options.stereoLeft)

        # Get some data for the label file
        thisFilePath = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'lronac2dem.py')
        versionId   = IrgFileFunctions.getLastGitTag(thisFilePath)

        demDescription        = 'High-resolution NAC digital terrain models in GeoTIFF format. DEM is IEEE floating point TIFF, 32 bits/sample, 1 samples/pixel in single image plane configuration. The NoDATA value is -3.40282266e+038.'
        hillshadeDescription  = 'Shaded-relief derived from NAC digital terrain models. Image is a TIFF image, 8 bits/sample, 1 samples/pixel in single image plane configuration.'
        colormapDescription   = 'Color shaded-relief derived from NAC digital terrain models. Image is 3-channel RGB TIFF, 8 bits/sample, 3 samples/pixel in single image plane configuration.'
        confidenceDescription = 'Discretized visualization of intersection error in NAC digital terrain model. Image is a TIFF image, 8 bits/sample, 1 samples/pixel in single image plane configuration.'
        mapProjectDescription = 'High-resolution NAC orthoprojected image in GeoTIFF format. Image is IEEE floating point TIFF, 32 bits/sample, 1 samples/pixel in single image plane configuration. The NoDATA value is -3.40282266e+038.'

        # Copy the colormap legend file in to the colormap label file!
        colormapLegendText = ('\n/* Colormap legend information: */\n')
        colormapLegendFile = open(colormapLegendPath, 'r')
        for line in colormapLegendFile:
            colormapLegendText = colormapLegendText + '/*  ' + line.strip() + '     */\n'
            
        # Copy the confidence legend file in to the confidence label file!
        confidenceLegendText = ('\n/* Confidence legend information: */\n')
        confidenceLegendFile = open(confidenceLegendPath, 'r')
        for line in confidenceLegendFile:
            confidenceLegendText = confidenceLegendText + '/*  ' + line.strip() + '     */\n'

        # Generate label files for each of the output files
        writeLabelFile(demPath,               demLabelPath,               dataSetName, versionId, demDescription)
        writeLabelFile(hillshadePath,         hillshadeLabelPath,         dataSetName, versionId, hillshadeDescription)
        writeLabelFile(colormapPath,          colormapLabelPath,          dataSetName, versionId, colormapDescription,   colormapLegendText)
        writeLabelFile(confidencePath,        confidenceLabelPath,        dataSetName, versionId, confidenceDescription, confidenceLegendText)
        writeLabelFile(mapProjectLeftPath,    mapProjectLeftLabelPath,    dataSetName, versionId, mapProjectDescription)
        writeLabelFile(mapProjectRightPath,   mapProjectRightLabelPath,   dataSetName, versionId, mapProjectDescription)
        
        logging.info('Done with label files, compressing results.')

        # Compress the input files to save disk space
        if not os.path.exists(compressedInputPath):
            
            listOfInputFiles = [options.leftPath, options.rightPath, options.stereoLeft, options.stereoRight, options.lolaPath]
            if options.asuPath: # Handle optional input
                listOfInputFiles.append(options.asuPath)
            IrgFileFunctions.tarFileList(listOfInputFiles, compressedInputPath, True)
            

        # Tar the output files to save file count (they are already compressed)
        if not os.path.exists(compressedOutputPath):
            
            listOfDeliveryFiles = [demPath,      hillshadePath,      colormapPath,      confidencePath,      mapProjectLeftPath,      mapProjectRightPath,
                                   demLabelPath, hillshadeLabelPath, colormapLabelPath, confidenceLabelPath, mapProjectLeftLabelPath, mapProjectRightLabelPath]

            # The file names as they should appear in the tar file
            baseTarPath  = os.path.join(outputFolder, dataSetName + '_')
            demTarBase   = baseTarPath + 'DEM'
            hillTarBase  = baseTarPath + 'Hillshade'
            colorTarBase = baseTarPath + 'Colormap'
            confTarBase  = baseTarPath + 'Confidence'
            leftTarBase  = baseTarPath + 'MapProjLeft'
            rightTarBase = baseTarPath + 'MapProjRight'

            listOfTarDeliveryFiles = [demTarBase+'.TIF', hillTarBase+'.TIF', colorTarBase+'.TIF', confTarBase+'.TIF', leftTarBase+'.TIF', rightTarBase+'.TIF',
                                      demTarBase+'.LBL', hillTarBase+'.LBL', colorTarBase+'.LBL', confTarBase+'.LBL', leftTarBase+'.LBL', rightTarBase+'.LBL']

            # This function can handle renaming the files as they get put in the tar.
            IrgFileFunctions.tarFileList(listOfDeliveryFiles, compressedOutputPath, False, listOfTarDeliveryFiles)


        # Compress the diagnostic files to save disk space
        if not os.path.exists(compressedDebugPath):
            
            listOfDebugFiles = [options.outputPrefix + '-Log.txt',
                                options.outputPrefix + '-PcAlignLog.txt',
                                options.outputPrefix + '-LOLA_diff_points.csv',
                                options.outputPrefix + '-LOLA_diff_points.kml',]
            IrgFileFunctions.tarFileList(listOfDebugFiles, compressedDebugPath, True)


        if not options.keep:
            pass
            #print 'Deleting temporary files'
            #IrgFileFunctions.removeIfExists(mainMosaicPath)
            #IrgFileFunctions.removeIfExists(stereoMosaicPath)
        
        endTime = time.time()

        logging.info('lronac2dem finished in %f seconds', endTime - startTime)
        print "Finished in " + str(endTime - startTime) + " seconds."
        print '#################################################################################'
        return 0
    fullWidth   = int(projectionInfo[widthStart+7  : heightStart-1])
    fullHeight  = int(projectionInfo[heightStart+8 : heightEnd])
    print 'Output image size is ' + str(fullWidth) + ' by ' + str(fullHeight) + ' pixels.'

    # For now we just break up the image into a user-specified tile size (default 1000x1000)
    numTilesX, numTilesY, tileList = generateTileList(fullWidth, fullHeight, options.tileSize)
    numTiles = numTilesX * numTilesY

    print 'Splitting into ' + str(numTilesX) + ' by ' + str(numTilesY) + ' tiles.'

    # Set up output folder
    outputFolder = os.path.dirname(options.outputPath)
    if outputFolder == '':
        outputFolder = './' # Handle calls in same directory
    outputName   = os.path.basename(options.outputPath)
    IrgFileFunctions.createFolder(outputFolder)
    
    # Make a temporary directory to store the tiles
    if options.workDir:
        tempFolder = options.workDir
    else: # No folder provided, create a default one
        tempFolder = os.path.join(outputFolder, outputName.replace('.', '_') + '_tiles/')
    IrgFileFunctions.createFolder(tempFolder)

    
    # Generate a text file that contains the boundaries for each tile
    argumentFilePath = os.path.join(tempFolder, 'argumentList.txt')
    argumentFile     = file(argumentFilePath, 'w')
    for tile in tileList:
        argumentFile.write(str(tile[0]) + '\t' + str(tile[1]) + '\t' + str(tile[2]) + '\t' + str(tile[3]) + '\n')
    argumentFile.close()