def read_igor_marginals_txt(marginals_file_name , dim_names=False):
    """Load raw IGoR model marginals.
    
    Parameters
    ----------
    marginals_file_name : str
        File name for a IGOR model marginals file.

    Returns
    -------
    model_dict : dict
        Dictionary with model marginals.
    dimension_names_dict : dict
        Dictionary that defines IGoR model dependecies.
    
    """
    with open(marginals_file_name,'r') as file:
        #Model parameters are stored inside a dictionary of ndarrays
        model_dict = {}
        dimension_names_dict = {}
        element_name=""
        first = True
        first_dim_line = False
        element_marginal_array = []
        indices_array = []

        for line in file:
            strip_line = line.rstrip('\n') #Remove end of line character
            if strip_line[0]=='@':
                first_dim_line = True
                if not(first):
                    #Add the previous to the dictionnary
                    model_dict[element_name] = element_marginal_array
                else:
                    first = False
				
                element_name = strip_line[1:]

            if strip_line[0]=='$':
                #define array dimensions
                coma_index = strip_line.find(',')
                dimensions = []

                #Get rid of $Dim[
                previous_coma_index = 4
                while coma_index != -1:
                    dimensions.append(int(strip_line[previous_coma_index+1:coma_index]))
                    previous_coma_index = coma_index
                    coma_index = strip_line.find(',',coma_index+1)
			
                #Add last dimension and get rid of the closing bracket 
                dimensions.append(int(strip_line[previous_coma_index+1:-1]))

                element_marginal_array = np.ndarray(shape=dimensions)

            if strip_line[0]=='#':
                if first_dim_line:
                    dimensions_names = []
                    if len(dimensions) > 1:
                        comma_index = strip_line.find(',')
                        opening_bracket_index = strip_line.find('[')
                        while opening_bracket_index != -1:
                            dimensions_names.append(strip_line[opening_bracket_index+1:comma_index])
                            opening_bracket_index = strip_line.find('[',comma_index) 
                            comma_index = strip_line.find(',',opening_bracket_index)
                    first_dim_line = False
                    dimensions_names.append(element_name)
                    dimension_names_dict[element_name] = dimensions_names
                    
                
                #update indices
                indices_array = []
                if len(dimensions) > 1:
                    comma_index = strip_line.find(',')
                    closing_brack_index = strip_line.find(']')					
                    while closing_brack_index != -1:
                        indices_array.append(int(strip_line[comma_index+1:closing_brack_index]))
                        opening_bracket_index = strip_line.find('[',closing_brack_index) 
                        comma_index = strip_line.find(',',opening_bracket_index)
                        closing_brack_index = strip_line.find(']',closing_brack_index+1)
				

            if strip_line[0]=='%':
                #read doubles
                coma_index = strip_line.find(',')
                marginals_values = []

                #Get rid of the %
                previous_coma_index = 0
                while coma_index != -1:
                    marginals_values.append(float(strip_line[previous_coma_index+1:coma_index]))
                    previous_coma_index = coma_index
                    coma_index = strip_line.find(',',coma_index+1)
			
                #Add last dimension and get rid of the closing bracket 
                marginals_values.append(float(strip_line[previous_coma_index+1:]))
                if len(marginals_values)!=dimensions[-1]:
                    print "problem"
                element_marginal_array[tuple(indices_array)] = marginals_values
        model_dict[element_name] = element_marginal_array				
        
        
    return [model_dict,dimension_names_dict]