# Path: kquant_data/xio/h5.py
# def write_dataframe_set_struct_keep_head(path, data, dtype, dateset_name):
#     """
#     保存DataFrame数据
#     保留表头
#     可以用来存K线，除权除息等信息
#     :param path:
#     :param data:
#     :param dtype:
#     :param dateset_name:
#     :return:
#     """
#     f = h5py.File(path, 'w')
# 
#     r = data.to_records(index=False)
#     d = np.array(r, dtype=dtype)
# 
#     f.create_dataset(dateset_name, data=d, compression="gzip", compression_opts=6)
#     f.close()
#     return
# 
# Path: kquant_data/utils/xdatetime.py
# def yyyyMMddHHmm_2_datetime(dt):
#     """
#     输入一个长整型yyyyMMddhmm，返回对应的时间
#     :param dt:
#     :return:
#     """
#     dt = int(dt)  # FIXME：在python2下会有问题吗？
#     (yyyyMMdd, hh) = divmod(dt, 10000)
#     (yyyy, MMdd) = divmod(yyyyMMdd, 10000)
#     (MM, dd) = divmod(MMdd, 100)
#     (hh, mm) = divmod(hh, 100)
# 
#     return datetime(yyyy, MM, dd, hh, mm)


import os
import numpy as np
import pandas as pd
from ..xio.h5 import write_dataframe_set_struct_keep_head
from ..utils.xdatetime import yyyyMMddHHmm_2_datetime


def read_file(path, instrument_type='stock'):
    """
    http://www.tdx.com.cn/list_66_68.html
    通达信本地目录有day/lc1/lc5三种后缀名，两种格式
    从通达信官网下载的5分钟后缀只有5这种格式，为了处理方便，时间精度都只到分钟
    :param path:
    :return:
    """
    columns = ['DateTime', 'Open', 'High', 'Low', 'Close', 'Amount', 'Volume', 'na']

    file_ext = os.path.splitext(path)[1][1:]
    if instrument_type == 'stock':
        ohlc_type = {'day': 'i4', '5': 'i4', 'lc1': 'f4', 'lc5': 'f4'}[file_ext]
        formats = ['i4'] + [ohlc_type] * 4 + ['f4'] + ['i4'] * 2
    elif instrument_type == 'option':
        ohlc_type = {'day': 'f4', '5': 'i4', 'lc1': 'f4', 'lc5': 'f4'}[file_ext]
        formats = ['i4'] + [ohlc_type] * 4 + ['i4'] + ['i4'] * 2
    date_parser = {'day': day_datetime_long,
                   '5': min_datetime_long,
                   'lc1': min_datetime_long,
                   'lc5': min_datetime_long,
                   }[file_ext]

    dtype = np.dtype({'names': columns, 'formats': formats})
    data = np.fromfile(path, dtype=dtype)
    df = pd.DataFrame(data)
    # 为了处理的方便，存一套long类型的时间
    df['DateTime'] = df['DateTime'].apply(date_parser)

# Path: kquant_data/xio/h5.py
# def write_dataframe_set_struct_keep_head(path, data, dtype, dateset_name):
#     """
#     保存DataFrame数据
#     保留表头
#     可以用来存K线，除权除息等信息
#     :param path:
#     :param data:
#     :param dtype:
#     :param dateset_name:
#     :return:
#     """
#     f = h5py.File(path, 'w')
# 
#     r = data.to_records(index=False)
#     d = np.array(r, dtype=dtype)
# 
#     f.create_dataset(dateset_name, data=d, compression="gzip", compression_opts=6)
#     f.close()
#     return
# 
# Path: kquant_data/utils/xdatetime.py
# def yyyyMMddHHmm_2_datetime(dt):
#     """
#     输入一个长整型yyyyMMddhmm，返回对应的时间
#     :param dt:
#     :return:
#     """
#     dt = int(dt)  # FIXME：在python2下会有问题吗？
#     (yyyyMMdd, hh) = divmod(dt, 10000)
#     (yyyy, MMdd) = divmod(yyyyMMdd, 10000)
#     (MM, dd) = divmod(MMdd, 100)
#     (hh, mm) = divmod(hh, 100)
# 
#     return datetime(yyyy, MM, dd, hh, mm)


# Path: kquant_data/stock/tdx.py
import os
import numpy as np
import pandas as pd
from ..xio.h5 import write_dataframe_set_struct_keep_head
from ..utils.xdatetime import yyyyMMddHHmm_2_datetime



def read_file(path, instrument_type='stock'):
    """
    http://www.tdx.com.cn/list_66_68.html
    通达信本地目录有day/lc1/lc5三种后缀名，两种格式
    从通达信官网下载的5分钟后缀只有5这种格式，为了处理方便，时间精度都只到分钟
    :param path:
    :return:
    """
    columns = ['DateTime', 'Open', 'High', 'Low', 'Close', 'Amount', 'Volume', 'na']

    file_ext = os.path.splitext(path)[1][1:]
    if instrument_type == 'stock':
        ohlc_type = {'day': 'i4', '5': 'i4', 'lc1': 'f4', 'lc5': 'f4'}[file_ext]
        formats = ['i4'] + [ohlc_type] * 4 + ['f4'] + ['i4'] * 2
    elif instrument_type == 'option':
        ohlc_type = {'day': 'f4', '5': 'i4', 'lc1': 'f4', 'lc5': 'f4'}[file_ext]
        formats = ['i4'] + [ohlc_type] * 4 + ['i4'] + ['i4'] * 2
    date_parser = {'day': day_datetime_long,
                   '5': min_datetime_long,
                   'lc1': min_datetime_long,
                   'lc5': min_datetime_long,
                   }[file_ext]

    dtype = np.dtype({'names': columns, 'formats': formats})
    data = np.fromfile(path, dtype=dtype)
    df = pd.DataFrame(data)
    # 为了处理的方便，存一套long类型的时间
    df['DateTime'] = df['DateTime'].apply(date_parser)

    df['datetime'] = df['DateTime'].apply(yyyyMMddHHmm_2_datetime)