#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on May 25 2020

@author: JKH

This function takes a runlog, copies it and creates a version that is (mildly) 
filtered.

"""
import pandas as pd
import numpy as np
import os


curr_dir = os.getcwd()
os.chdir('../python_subs')
import egi_subs as egs
os.chdir(curr_dir)


#rl_fname = '/home/osboxes/research/tccon/egi2020/results/xd/daily/20180716/runlog/xd20180716.grl'
#flag_lst = ['OPD', 'gt', 0.5, 'and', 'SNR', 'ge', 3]

def egi_filter_grl(rl_fname, flag_lst=['OPD', 'gt', 0.2, 'and', 'SNR', 'ge', 2]):
  """
  Takes the runlog and list of flags and creates a filtered runlog.
  Also filters the *.lse file.
  """
  rl_daydir = os.path.dirname(rl_fname)
  xxYYYYMMDD = os.path.basename(rl_fname)[0:10]
  lse_fname = rl_daydir + '/' + xxYYYYMMDD + '.lse'
  
  
  ### Read in/copy runlog files
  with open(rl_fname, 'r') as fobj: # gets file object for reading
    rl_raw = fobj.readlines() 
  
  with open(rl_fname + '.uf', "w") as fobj:
    fobj.writelines(rl_raw) #writes to file
  
  ### Read in/copy LSE file
  with open(lse_fname, 'r') as fobj: # gets file object for reading
    lse_raw = fobj.readlines() 
  
  with open(lse_fname + '.uf', "w") as fobj:
    fobj.writelines(lse_raw) #writes to file
  
  lse_nhdcol = np.int32(lse_raw[0].split())
  
  # get number of lines to skip and read into a dataframe
  n_hd_col = np.int32(rl_raw[0].split())
  
  raw_df = pd.read_csv(rl_fname, delim_whitespace=True, header=n_hd_col[0]-1) #read into a dataframe
  
  ### Make boolean flag
  bool_flag = egs.oof_bool_flag(raw_df, flag_lst)
  
  
  ### Get the instrument configuration from *_mmtch.dat file
  mm_fname = os.path.dirname(rl_daydir) + '/matchup/' + xxYYYYMMDD + '_mmtch.dat'
    
  with open(mm_fname, 'r') as fobj: # gets file object for reading
    mm_raw = fobj.readlines() 
  
  inst_config = mm_raw[0].strip().split(' ')[1] #strip off the new line
  
  ### If it's a dual channel instrument, make boolean flag for pairs of lines so that
  # you don't inadvertantly filter a line that's bad for one channel but okay for the other
  if inst_config == 'em27_dual':
    bool_2col = np.reshape(bool_flag, [len(bool_flag)//2, 2])
    bool_both = np.all(bool_2col, 1) #both columns need to be true
    
    bool_flag = np.repeat(bool_both, 2)
  
  ### Write unflagged lines to runlog file
  with open(rl_fname, "w") as fobj:
    for i1 in range(n_hd_col[0]): #copy over header lines as-is
      fobj.write(rl_raw[i1])
    
    for i1 in range(len(bool_flag)):
      if bool_flag[i1]: #line isn't filtered, copy it over
        fobj.write(rl_raw[n_hd_col[0] + i1])
  
  ### Write unflagged lines to LSE file
  with open(lse_fname, "w") as fobj_lse:
    for i1 in range(lse_nhdcol[0]): #copy over header lines as-is
      fobj_lse.write(lse_raw[i1])
    
    for i1 in range(len(bool_flag)):
      if bool_flag[i1]: #line isn't filtered, copy it over
        fobj_lse.write(lse_raw[lse_nhdcol[0] + i1])
  
  
  
  
  
  
  
  
  
  