# -*- coding: utf-8 -*-

def makeSpectralClass(teff, logg):

    #//Add subclass to each spectral class (lburns)
    spectralClass = " "
    subClass = " " #//Create a variable for the subclass of the star. lburns
    luminClass = "V" #//defaults to V
    #//Determine the spectralClass and subClass of main sequence stars, subdwarfs and white dwarfs
    #//var luminClass = "V" or luminClass = "VI" or luminClass = "WD"
    #// Based on the data in Appendix G of An Introduction to Modern Astrophysics, 2nd Ed. by
    #// Carroll & Ostlie
    if ((logg >= 4.0) and (logg <= 6.0)):
        if (teff < 3000.0):
            spectralClass = "L";
        elif ((teff >= 3000.0) and (teff < 3900.0)):
            spectralClass = "M";
            if ((teff >= 3000.0) and (teff <= 3030.0)):
                subClass = "6";
            elif ((teff > 3030.0) and (teff <= 3170.0)):
                subClass = "5";
            elif ((teff > 3170.0) and (teff <= 3290.0)):
                subClass = "4";
            elif ((teff > 3290.0) and (teff <= 3400.0)):
                subClass = "3";
            elif ((teff > 3400.0) and (teff <= 3520.0)):
                subClass = "2";
            elif ((teff > 3520.0) and (teff <= 3660.0)):
                subClass = "1";
            elif ((teff > 3660.0) and (teff < 3900.0)):
                subClass = "0";
            
        elif ((teff >= 3900.0) and (teff < 5200.0)):
            spectralClass = "K";
            if ((teff >= 3900.0) and (teff <= 4150.0)):
                subClass = "7";
            elif ((teff > 4150.0) and (teff <= 4410.0)):
                subClass = "5";
            elif ((teff > 4410.0) and (teff <= 4540.0)):
                subClass = "4";
            elif ((teff > 4540.0) and (teff <= 4690.0)):
                subClass = "3";
            elif ((teff > 4690.0) and (teff <= 4990.0)):
                subClass = "1";
            elif ((teff > 4990.0) and (teff < 5200.0)):
                subClass = "0";
            
        elif ((teff >= 5200.0) and (teff < 5950.0)):
            spectralClass = "G";
            if ((teff >= 5200.0) and (teff <= 5310.0)):
                subClass = "8";
            elif ((teff > 5310.0) and (teff <= 5790.0)):
                subClass = "2";
            elif ((teff > 5790.0) and (teff < 5950.0)):
                subClass = "0";
            
        elif ((teff >= 5950.0) and (teff < 7300.0)):
            spectralClass = "F";
            if ((teff >= 5950.0) and (teff <= 6250.0)):
                subClass = "8";
            elif ((teff > 6250.0) and (teff <= 6650.0)):
                subClass = "5";
            elif ((teff > 6650.0) and (teff <= 7050.0)):
                subClass = "2";
            elif ((teff > 7050.0) and (teff < 7300.0)):
                subClass = "0";
            
        elif ((teff >= 7300.0) and (teff < 9800.0)):
            spectralClass = "A";
            if ((teff >= 7300.0) and (teff <= 7600.0)):
                subClass = "8";
            elif ((teff > 7600.0) and (teff <= 8190.0)):
                subClass = "5";
            elif ((teff > 8190.0) and (teff <= 9020.0)):
                subClass = "2";
            elif ((teff > 9020.0) and (teff <= 9400.0)):
                subClass = "1";
            elif ((teff > 9400.0) and (teff < 9800.0)):
                subClass = "0";
            
        elif ((teff >= 9800.0) and (teff < 30000.0)):
            spectralClass = "B";
            if ((teff >= 9300.0) and (teff <= 10500.0)):
                subClass = "9";
            elif ((teff > 10500.0) and (teff <= 11400.0)):
                subClass = "8";
            elif ((teff > 11400.0) and (teff <= 12500.0)):
                subClass = "7";
            elif ((teff > 12500.0) and (teff <= 13700.0)):
                subClass = "6";
            elif ((teff > 13700.0) and (teff <= 15200.0)):
                subClass = "5";
            elif ((teff > 15200.0) and (teff <= 18800.0)):
                subClass = "3";
            elif ((teff > 18800.0) and (teff <= 20900.0)):
                subClass = "2";
            elif ((teff > 20900.0) and (teff <= 25400.0)):
                subClass = "1";
            elif ((teff > 25400.0) and (teff < 30000.0)):
                subClass = "0";
            
        elif (teff >= 30000.0):
            spectralClass = "O";
            if ((teff >= 30000.0) and (teff <= 35800.0)):
                subClass = "8";
            elif ((teff > 35800.0) and (teff <= 37500.0)):
                subClass = "7";
            elif ((teff > 37500.0) and (teff <= 39500.0)):
                subClass = "6";
            elif ((teff > 39500.0) and (teff <= 42000.0)):
                subClass = "5";
            
        
    
    #//Determine the spectralClass and subClass of giants and subgiants. lburns
    #//var luminClass = "III" or luminClass = "IV"
    #//Determine the spectralClass and subClass of giants and subgiants. lburns
    #//var luminClass = "III" or luminClass = "IV"
    if ((logg >= 1.5) and (logg < 4.0)):
        if (teff < 3000.0):
            spectralClass = "L";
        elif ((teff >= 3000.0) and (teff < 3700.0)):
            spectralClass = "M";
            if ((teff >= 3000.0) and (teff <= 3330.0)):
                subClass = "6";
            elif ((teff > 3330.0) and (teff <= 3380.0)):
                subclass = "5";
            elif ((teff > 3380.0) and (teff <= 3440.0)):
                subClass = "4";
            elif ((teff > 3440.0) and (teff <= 3480.0)):
                subClass = "3";
            elif ((teff > 3480.0) and (teff <= 3540.0)):
                subClass = "2";
            elif ((teff > 3540.0) and (teff <= 3600.0)):
                subClass = "1";
            elif ((teff > 3600.0) and (teff < 3700.0)):
                subClass = "0";
            
        elif ((teff >= 3700.0) and (teff < 4700.0)):
            spectralClass = "K"
            if ((teff >= 3700.0) and (teff <= 3870.0)):
                subClass = "7";
            elif ((teff > 3870.0) and (teff <= 4050.0)):
                subClass = "5";
            elif ((teff > 4050.0) and (teff <= 4150.0)):
                subClass = "4";
            elif ((teff > 4150.0) and (teff <= 4260.0)):
                subClass = "3";
            elif ((teff > 4260.0) and (teff <= 4510.0)):
                subClass = "1";
            elif ((teff > 4510.0) and (teff < 4700.0)):
                subClass = "0";
            
        elif ((teff >= 4700.0) and (teff < 5500.0)):
            spectralClass = "G";
            if ((teff >= 4700.0) and (teff <= 4800.0)):
                subClass = "8";
            elif ((teff > 4800.0) and (teff <= 5300.0)):
                subClass = "2";
            elif ((teff > 5300.0) and (teff < 5500.0)):
                subClass = "0";
            
        elif ((teff >= 5500.0) and (teff < 7500.0)):
            spectralClass = "F";
            if ((teff >= 5500.0) and (teff <= 6410.0)):
                subClass = "5";
            elif ((teff > 6410.0) and (teff <= 7000.0)):
                subClass = "2";
            elif ((teff > 7000.0) and (teff < 7500.0)):
                subClass = "0";
            
        elif ((teff >= 7500.0) and (teff < 10300.0)):
            spectralClass = "A";
            if ((teff >= 7500.0) and (teff <= 7830.0)):
                subClass = "8";
            elif ((teff > 7830.0) and (teff <= 8550.0)):
                subClass = "5";
            elif ((teff > 8550.0) and (teff <= 9460.0)):
                subClass = "2";
            elif ((teff > 9460.0) and (teff <= 9820.0)):
                subClass = "1";
            elif ((teff > 9820.0) and (teff < 10300.0)):
                subClass = "0";
            
        elif ((teff >= 10300.0) and (teff < 29300.0)):
            spectralClass = "B";
            if ((teff >= 10300.0) and (teff <= 10900.0)):
                subClass = "9";
            elif ((teff > 10900.0) and (teff <= 11700.0)):
                subClass = "8";
            elif ((teff > 11700.0) and (teff <= 12700.0)):
                subClass = "7";
            elif ((teff > 12700.0) and (teff <= 13800.0)):
                subClass = "6";
            elif ((teff > 13800.0) and (teff <= 15100.0)):
                subClass = "5";
            elif ((teff > 15100.0) and (teff <= 18300.0)):
                subClass = "3";
            elif ((teff > 18300.0) and (teff <= 20200.0)):
                subClass = "2";
            elif ((teff > 20200.0) and (teff <= 24500.0)):
                subClass = "1";
            elif ((teff > 24500.0) and (teff < 29300.0)):
                subClass = "0";
            
        elif ((teff >= 29300.0) and (teff < 40000.0)):
            spectralClass = "O";
            if ((teff >= 29300.0) and (teff <= 35000.0)):
                subClass = "8";
            elif ((teff > 35000.0) and (teff <= 36500.0)):
                subClass = "7";
            elif ((teff > 36500.0) and (teff <= 37800.0)):
                subClass = "6";
            elif ((teff > 37800.0) and (teff < 40000.0)):
                subClass = "5";
            
        
    
    
    #//Determine the spectralClass and subClass of supergiants and bright giants. lburns
    #//var luminClass = "I" or luminClass = "II"
    if ((logg >= 0.0) and (logg < 1.5)):
        if (teff < 2700.0):
            spectralClass = "L";
        elif ((teff >= 2700.0) and (teff < 3650.0)):
            spectralClass = "M";
            if ((teff >= 2700.0) and (teff <= 2710.0)):
                subClass = "6";
            elif ((teff > 2710.0) and (teff <= 2880.0)):
                subClass = "5";
            elif ((teff > 2880.0) and (teff <= 3060.0)):
                subClass = "4";
            elif ((teff > 3060.0) and (teff <= 3210.0)):
                subClass = "3";
            elif ((teff > 3210.0) and (teff <= 3370.0)):
                subClass = "2";
            elif ((teff > 3370.0) and (teff <= 3490.0)):
                subClass = "1";
            elif ((teff > 3490.0) and (teff < 3650.0)):
                subClass = "0";
            
        elif ((teff >= 3650.0) and (teff < 4600.0)):
            spectralClass = "K";
            if ((teff >= 3650.0) and (teff <= 3830.0)):
                subClass = "7";
            elif ((teff > 3830.0) and (teff <= 3990.0)):
                subClass = "5";
            elif ((teff > 3990.0) and (teff <= 4090.0)):
                subClass = "4";
            elif ((teff > 4090.0) and (teff <= 4190.0)):
                subClass = "3";
            elif ((teff > 4190.0) and (teff <= 4430.0)):
                subClass = "1";
            elif ((teff > 4430.0) and (teff < 4600.0)):
                subClass = "0";
            
        elif ((teff >= 4600.0) and (teff < 5500.0)):
            spectralClass = "G";
            if ((teff >= 4600.0) and (teff <= 4700.0)):
                subClass = "8";
            elif ((teff > 4700.0) and (teff <= 5190.0)):
                subClass = "2";
            elif ((teff > 5190.0) and (teff < 5500.0)):
                subClass = "0";
            
        elif ((teff >= 5500.0) and (teff < 7500.0)):
            spectralClass = "F";
            if ((teff >= 5500.0) and (teff <= 5750.0)):
                subClass = "8";
            elif ((teff > 5750.0) and (teff <= 6370.0)):
                subClass = "5";
            elif ((teff > 6370.0) and (teff <= 7030.0)):
                subClass = "2";
            elif ((teff > 7030.0) and (teff < 7500.0)):
                subClass = "0";
            
        elif ((teff >= 7500.0) and (teff < 10000.0)):
            spectralClass = "A";
            if ((teff >= 7500.0) and (teff <= 7910.0)):
                subClass = "8";
            elif ((teff > 7910.0) and (teff <= 8610.0)):
                subClass = "5";
            elif ((teff > 8610.0) and (teff <= 9380.0)):
                subClass = "2";
            elif ((teff > 9380.0) and (teff < 10000.0)):
                subClass = "0";
            
        elif ((teff >= 10000.0) and (teff < 27000.0)):
            spectralClass = "B";
            if ((teff >= 10000.0) and (teff <= 10500.0)):
                subClass = "9";
            elif ((teff > 10500.0) and (teff <= 11100.0)):
                subClass = "8";
            elif ((teff > 11100.0) and (teff <= 11800.0)):
                subClass = "7";
            elif ((teff > 11800.0) and (teff <= 12600.0)):
                subClass = "6";
            elif ((teff > 12600.0) and (teff <= 13600.0)):
                subClass = "5";
            elif ((teff > 13600.0) and (teff <= 16000.0)):
                subClass = "3";
            elif ((teff > 16000.0) and (teff <= 17600.0)):
                subClass = "2";
            elif ((teff > 17600.0) and (teff <= 21400.0)):
                subClass = "1";
            elif ((teff > 21400.0) and (teff < 27000.0)):
                subClass = "0";
            
        elif ((teff >= 27000.0) and (teff < 42000.0)):
            spectralClass = "O";
            if ((teff >= 27000.0) and (teff <= 34000.0)):
                subClass = "8";
            elif ((teff > 34000.0) and (teff <= 36200.0)):
                subClass = "7";
            elif ((teff > 36200.0) and (teff <= 38500.0)):
                subClass = "6";
            elif ((teff > 38500.0) and (teff < 42000.0)):
                subClass = "5";
            
        
    
    
    #//Determine luminClass based on logg
    if ((logg >= 0.5) and (logg < 1.0)):
        luminClass = "I";
    elif ((logg >= 1.0) and (logg < 1.5)):
        luminClass = "II";
    elif ((logg >= 1.5) and (logg < 3.0)):
        luminClass = "III";
    elif ((logg >= 3.0) and (logg < 4.0)):
        luminClass = "IV";
    elif ((logg >= 4.0) and (logg < 5.0)):
        luminClass = "V";
    elif ((logg >= 5.0) and (logg < 6.0)):
        luminClass = "VI";
    elif ((logg >= 5.0)):
        luminClass = "WD";
        
    
    spectralType = spectralClass + subClass + " " + luminClass
    
    return spectralType

