import platform
import os
import subprocess


PathToExecutable = str('/var/tmp/XcodeBuilds/Release/')
PathToOssimPreference = str('/Users/sasha/ossim_preferences')
PathToGdalGrass  = str('/Users/Shared/source/grass7/distLEO/')

def WhichPlatform():
    global platform
    platforms = platform.system()
    return platforms



def OssimImg2rr(img1):
    myenv = os.environ.copy()
    systemplatform = WhichPlatform()
    if systemplatform == str('Darwin'):
        myenv['DYLD_FRAMEWORK_PATH'] = PathToExecutable
        myenv['GDAL_DRIVER_PATH'] = PathToGdalGrass
    myenv['OSSIM_PREFS_FILE'] = PathToOssimPreference
    output = subprocess.Popen([PathToExecutable+'img2rr' ,str(img1)], stdout=subprocess.PIPE, env = myenv).communicate()[0]



def OssimImageInfo(band1):
    myenv = os.environ.copy()
    systemplatform = WhichPlatform()
    if systemplatform == str('Darwin'):
        myenv['DYLD_FRAMEWORK_PATH'] = PathToExecutable
        myenv['GDAL_DRIVER_PATH'] = PathToGdalGrass
    myenv['OSSIM_PREFS_FILE'] = PathToOssimPreference
    output = subprocess.Popen([PathToExecutable+'image_info' ,str(band1)], stdout=subprocess.PIPE, env = myenv).communicate()[0]


def OssimBandMerge(band1,band2,band3,output):
    myenv = os.environ.copy()
    systemplatform = WhichPlatform()
    if systemplatform == str('Darwin'):
        myenv['DYLD_FRAMEWORK_PATH'] = PathToExecutable
        myenv['GDAL_DRIVER_PATH'] = PathToGdalGrass
    myenv['OSSIM_PREFS_FILE'] = PathToOssimPreference
    output = subprocess.Popen([PathToExecutable+'band_merge' , 'tiff_strip' ,str(band1) , str(band2) , str(band3) , str(output)],stdout=subprocess.PIPE, env = myenv).communicate()[0]


def OssimCmm(band1):
    myenv = os.environ.copy()
    systemplatform = WhichPlatform()
    if systemplatform == str('Darwin'):
        myenv['DYLD_FRAMEWORK_PATH'] = PathToExecutable
        myenv['GDAL_DRIVER_PATH'] = PathToGdalGrass
    myenv['OSSIM_PREFS_FILE'] = PathToOssimPreference
    output = subprocess.Popen([PathToExecutable+'cmm' ,str(band1)],stdout=subprocess.PIPE, env = myenv).communicate()[0]


def OssimHeight(x,y):
    myenv = os.environ.copy()
    systemplatform = WhichPlatform()
    if systemplatform == str('Darwin'):
        myenv['DYLD_FRAMEWORK_PATH'] = PathToExecutable
        myenv['GDAL_DRIVER_PATH'] = PathToGdalGrass
    myenv['OSSIM_PREFS_FILE'] = PathToOssimPreference
    output = subprocess.Popen([PathToExecutable'ossim_height', str(x) , str(y)],stdout=subprocess.PIPE, env = myenv).communicate()[0]
    for line in output.splitlines():
        if line.startswith('Height above MSL:'):
            data = line[17:].strip()
            return data
            msl = repr(float(data))
            return float(msl)
            print float(msl)


def OssimMosaic(img1,img2,output):
    myenv = os.environ.copy()
    systemplatform = WhichPlatform()
    if systemplatform == str('Darwin'):
        myenv['DYLD_FRAMEWORK_PATH'] = PathToExecutable
        myenv['GDAL_DRIVER_PATH'] = PathToGdalGrass
    myenv['OSSIM_PREFS_FILE'] = PathToOssimPreference
    output = subprocess.Popen([PathToExecutable+'mosaic' ,str(img1), str(img2), str(output)],stdout=subprocess.PIPE, env = myenv).communicate()[0]



        