# -*- coding: utf-8 -*-
"""
Created on Sat Aug  1 14:02:59 2015

@author: Jacob Hedelius
"""

#!/bin/python
# Filename read_bin2asc_spectrum.py

#__author__ = 'Jacob Hedelius'

def read_bin2asc_spectrum(filename):
    
    # This function is meant to read in bin2asc specutrum files.  THis reader is specialized
    # for these formats and writes outputs to a
    # them to a "structure"/dictionary
    
    # example data
    #    ex_filename = 'C:\\Users\\Jacob Hedelius\\Documents\\Research\\python\\example_data\\asc_cn20140613s0e00a.0002'
    #    filename = ex_filename
    
    ## import packages
    import re
    import numpy as np
    
    
    # read in file in entirety
    fobj = open(filename, 'r') # gets file object for reading
    file_lines = fobj.readlines() #reads in entire file all at once, line by line
    fobj.close() #closes open file
    
    n_spc=re.split('[,|/|:|\n|^M| ]+', file_lines[0])
    n=list(filter(None, n_spc)) #contains strings
    
    nhead = float(n[0]) #ggg specified number of header rows
    ncol = float(n[1]) #ggg specified number of data columns
    
    if nhead > 4:
        bin2asc_info = file_lines[1]
        headers_pre = re.split('[,|/|:|\n| ]+', file_lines[2])
        headers = list(filter(None, headers_pre))
        hvl = 3 #row with header values
    else:
        bin2asc_info = ''
        headers_pre = re.split('[,|/|:|\n|^M| ]+', file_lines[1])
        headers = list(filter(None, headers_pre))
        hvl = 2
    
    #forms_ds = ''
    runlog = {'Spectrum_File_Name': ''}  #preparing dictionary for elements
    hvs_pre = re.split('[,|/|:|\n|^M| ]+', file_lines[hvl]) #header values, split
    hvs = list(filter(None, hvs_pre)) #header values
    
    i1 = 0
    for header_i1 in headers:
        if (header_i1 == 'Spectrum_File_Name' or header_i1 == 'APF'):
            runlog[header_i1] = hvs[i1]
        else:
            runlog[header_i1] = float(hvs[i1])
            
        i1 = i1+1
    
    spec_col_stringsp = re.split('[\n| ]+', file_lines[hvl + 1])
    spec_col_strings = list(filter(None, spec_col_stringsp))
    
    spec_col_strings[0] = 'Frequency' #Manually set the title of the first column
    
    # pre-allocate dictionary for spectrum values
    empt_ary = np.zeros([len(file_lines)-nhead,1])
    empt_ary[:] = np.NaN
    spectrum = {spec_col_strings[0]: empt_ary} #pre-allocating dictionary
    i2 = 0
    for scs_i2 in spec_col_strings:
        spectrum[scs_i2] = np.zeros([len(file_lines)-nhead,1])
        i2 += 1 #plus 1
    
    #write spectrum values
    i1 = 0
    for line_i1 in file_lines:
        if i1 >= nhead: #skip the header rows
            line_i1_list_pre = re.split('[\n| ]+', line_i1)
            line_i1_list = list(filter(None, line_i1_list_pre))
            spectrum[spec_col_strings[0]][i1-nhead] = float(line_i1_list[0])
            spectrum[spec_col_strings[1]][i1-nhead] = float(line_i1_list[1])
            
        #    elif i1 == nhead:
        #        break
        
        i1 +=1 #counter
    
    
    out = {'Spectrum': spectrum, 'runlog': runlog, 'bin2asc_info': bin2asc_info}
    
    return out
    
