# -*- coding: utf-8 -*-
"""
Created on Sat Oct 22 08:36:59 2016

@author: Jacob Hedelius

A collection of functions made to read in subfiles for aod calculations
v - 22-Oct-2016

"""

import numpy as np
import re
import os
curr_dir = os.getcwd()
os.chdir('../../python_subs')
import egi_subs as egs
os.chdir(curr_dir)
import pandas as pd

def read_aod_settings(settings_fname):
    # This reader reads in the aod settings file
        
    # example input
    # settings_fname = '..//i0_aod_settings.dat'
    
    fobj = open(settings_fname, 'r') # gets file object for reading
    file_lines = fobj.readlines() #reads in entire file all at once, line by line
    fobj.close()
    hd_lines=3 #manually set
    i2linep = re.split('[ |\n]+', file_lines[hd_lines-1]) #splits line
    hdrs = list(filter(None, i2linep)) #remove empties  
        
    set_raw = {} #pre-allocate dictionary
    set_raw['header'] = hdrs 
    
    i1 = 0
    flg_num = 0
    for line_i1 in file_lines:
        if i1 >= hd_lines:
            flg_num += 1 #iterate flag number
            line_i1_spt_p = re.split('[ |\n]+', line_i1) #split up line
            line_i1_spt = list(filter(None, line_i1_spt_p)) #remove blanks
            
            if len(line_i1_spt) >=3: #avoid blank lines
                set_raw[line_i1_spt[0]] = [flg_num, float(line_i1_spt[1]), float(line_i1_spt[2])] #saves flag number and upper/lower limits
            
        i1 +=1
    
    
    while '\n' in file_lines:
        file_lines.remove('\n') #remove blank lines
        
    inqctext = file_lines[1:] #skip the first header line
    inqctext[0] = ' Flag  nflgd ' + inqctext[0]
    inqctext[1] = '  1     0  ' + inqctext[1]
    
    out = set_raw
    out['inqctext'] = inqctext #prepare writing of .qc lines in output file
    return out


#%% function to read in .ic files
def read_ic(ic_fname):
    
    # basic function written to read in .ic files into a structure
    # example input
    #    ic_fname = '../870_nm.dn20160825.ic'
    
    icfile_raw = {} #pre-form dictionary
    
    
    fobj = open(ic_fname, 'r') # gets file object for reading
    file_lines = fobj.readlines() #reads in entire file all at once, line by line
    fobj.close()
    line_i1_spt_p = re.split('[ |\n]+', file_lines[2]) #split up line
    line_i1_spt = list(filter(None, line_i1_spt_p)) #remove blanks
    icfile_raw['nm_ctr_wdt']  = np.array(list(map(float, line_i1_spt[0:2]))) #center nm and width in nm
    
    file_raw = pd.read_csv(ic_fname, skiprows=3, delim_whitespace=True)
    
    for hdr_i1 in file_raw.columns.values: #convert DataFrame to a dictionary
        icfile_raw[hdr_i1] = np.array(file_raw[hdr_i1]) 
        if type(file_raw[hdr_i1][0]) == str:
            icfile_raw[hdr_i1] = (icfile_raw[hdr_i1]).tolist() #convert string arrays to lists
    
    return icfile_raw             


#%% function to read in .map files

def read_map(filename):
    # Read GGG2012 map files
    
    # example input
#    filename = '../dn20160825.map'
    
        
    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()
    
    linep = re.split('[ |\n]+', file_lines[0]) #splits line
    hd_col = np.array(list(filter(None, linep))) #remove empties
    
    intname = file_lines[1] #internal name
    dt = egs.dt_from6vec(float(intname[3:7]), float(intname[7:9]), float(intname[9:11]), 0, 0, 0) #date in matlab format
    dt_vec = egs.dt_to6vec(dt) #convert back to vector
                               
    info = file_lines[2] #GSETUP version
    skp_ln = 1 #indicator of lines to skip
    if info[0:7] == ' GSETUP':
        info = file_lines[2:4]
        skp_ln = 2
    
    # save some of the leading information
    out = {} #pre-form dictionary
    out['info'] = info
    out['time'] = dt
    out['year'] = dt_vec[0,0]
    out['month'] = dt_vec[0,1]
    out['day'] = dt_vec[0,2]
    
    linep = list(filter(None, re.split('[ |\n:]+', file_lines[3 + skp_ln])))
    out['Avogadro'] = float(linep[2])
    linep = list(filter(None, re.split('[ |\n:]+', file_lines[4 + skp_ln])))
    out['MDry_Air'] = float(linep[2])
    linep = list(filter(None, re.split('[ |\n:]+', file_lines[5 + skp_ln])))
    out['MH2O'] = float(linep[2])
    linep = list(filter(None, re.split('[ |\n:]+', file_lines[6 + skp_ln])))
    out['Latitude'] = float(linep[2])
    
    
    head = list(filter(None, re.split('[ |\n,]+', file_lines[7 + skp_ln])))
    units = list(filter(None, re.split('[ |\n,]+', file_lines[8 + skp_ln])))
    
    file_raw = pd.read_csv(filename, skiprows=int(hd_col[0]), header=None)
    
    i1 = -1 #initialize
    for hdr_i1 in head: #convert DataFrame to a dictionary
        i1 += 1 
        hdr_units = hdr_i1 + '_' + units[i1]
        out[hdr_units] = np.array(file_raw[i1]) #converts to an array
        
        if type(file_raw[i1][0]) == str: 
            out[hdr_units] = (out[hdr_units]).tolist()
        
        
    return out


#%% read runlogs

def read_runlog(filename):
    # reads in runlogs from GGG
    
    # example input
    #filename = '../dn20160825.grl'
    
    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()
    
    linep = re.split('[ |\n]+', file_lines[0]) #splits line
    hd_col = np.array(list(filter(None, linep))) #remove empties
        
    #headers = file_lines[int(hd_col[0])-1]
    
    file_raw = pd.read_csv(filename, skiprows=int(hd_col[0])-1, delim_whitespace=True) #read in file
    headers = file_raw.columns.values #save the column headers
    
    runlog = {} #pre-form dictionary output
    runlog['nheaderlines'] = int(hd_col[0])
    runlog['ncolumns'] = int(hd_col[1])
    
    
    for hdr_i1 in headers: #convert DataFrame to a dictionary
        runlog[hdr_i1] = np.array(file_raw[hdr_i1]) #converts to an array
            
        if type(file_raw[hdr_i1][0]) == str: #convert strings to lists instead
            runlog[hdr_i1] = (runlog[hdr_i1]).tolist()
    
    t_zeros = np.zeros(np.shape(runlog['Year']))
    t_zeros = t_zeros.reshape(len(t_zeros),1)
    t_ones = np.ones(np.shape(runlog['Year'])) #months actually require ones for input (using zero reveals a bug)
    t_ones = t_ones.reshape(len(t_ones),1)
    
    runlog['time'] = egs.dt_from6vec(np.reshape(runlog['Year'],t_zeros.shape), t_ones, \
      np.reshape(runlog['Day'],t_zeros.shape), np.reshape(runlog['Hour'],t_zeros.shape), t_zeros, t_zeros) #save utc time in matlab format
    
    return runlog


#%% read runlogs

def read_sat(sat_fname):
    
    # basic function written to read in .sat files into a structure
    # The .sat files contain 1) an sza list, 2) airmass (for water)
    # 3) various Transmissions for gases at different windows.
    
    #example inputs
    # sat_fname = '../dn20160825_synrl.sat'
    
    fobj = open(sat_fname, 'r') # gets file object for reading
    file_lines = fobj.readlines() #reads in entire file all at once, line by line
    fobj.close()
    
    linep = re.split('[ |\n]+', file_lines[0]) #splits line
    hd_col = np.array(list(filter(None, linep))) #remove empties
    
    file_raw = pd.read_csv(sat_fname, skiprows=int(hd_col[0])-1, delim_whitespace=True) #read in file
    headers = list(file_raw.columns.values) #save the column headers
    
    satfile_raw = {} #pre-form dictionary
    for hdr_i1 in headers: #convert DataFrame to a dictionary
        satfile_raw[hdr_i1] = np.array(file_raw[hdr_i1]) #converts to an array
        if type(file_raw[hdr_i1][0]) == str: #convert strings to lists instead
            satfile_raw[hdr_i1] = (satfile_raw[hdr_i1]).tolist()
    
    satfile_raw['headers'] = headers
    
    win_nm = egs.nan_ary([len(headers)-2,1]) #window wavenumbers. Skipping first 2 columns. Pre-allocating
    T_name_gas = list(headers[2:]) #full names, skipping first 2 rows
    i1 = -1 #initialize
    for T_i1 in headers[2:]: #for each T
        i1 +=1
        st_ind = 1 #starting string index for wavelength
        linep = re.split('[ |\n_]+', T_i1[st_ind:]) #splits line
        win_nm[i1] = float(linep[0]) #saves the central wavenumber
        T_name_gas = T_name_gas + [linep[1]]
    
    T_name_gas = np.transpose(np.reshape(T_name_gas, [2, len(win_nm)])) #output into shape with full T-hdr in 1st row and species in second
    
    
    out = satfile_raw #file output
    out['win_wn'] = win_nm
    out['T_name_gas'] = T_name_gas
    
    return out


#%% read gamma H2O file

def read_h2ogam(h2ogam_fname):
    #reads in the h2o gamma file (may have extension .sf)
    
    #example input
    # h2ogam_fname = '../dn20160825.h2o_.sf'
    
    file_raw = pd.read_csv(h2ogam_fname, skiprows=1, delim_whitespace=True) #read in file
    headers = list(file_raw.columns.values) #save the column headers
    
    h2oam_raw = {} #pre-form dictionary
    for hdr_i1 in headers: #convert DataFrame to a dictionary
        h2oam_raw[hdr_i1] = np.array(file_raw[hdr_i1]) #converts to an array
        if type(file_raw[hdr_i1][0]) == str: #convert strings to lists instead
            h2oam_raw[hdr_i1] = (h2oam_raw[hdr_i1]).tolist()
    
    return h2oam_raw
    
    
