# Path: arsf_envi_reader/numpy_bin_reader.py
# HAVE_ARSF_BINARYREADER = False
#     HAVE_ARSF_BINARYREADER = True
# class _BinaryReader(collections.Iterator):
# class BilReader(_BinaryReader):
# class BsqReader(_BinaryReader):
#     def __init__(self, input_file):
#     def __next__(self): pass
#     def read_line(self, line_number):
#     def read_band(self, band_number):
#     def read_pixel(self, sample_number, line_number):
#     def next(self):
#     def get_hdr_dict(self):
#     def get_num_samples(self):
#     def get_num_lines(self):
#     def get_num_bands(self):
#     def have_arsf_binaryreader(self):
#     def check_interleave(self, interleave):
#     def check_size(binary_file):
#     def __del__(self):
#     def __next__(self):
#     def read_line(self, line_number):
#     def read_band(self, band_number):
#     def read_pixel(self, sample_number, line_number):
#     def __next__(self):
#     def read_line(self, line_number):
#     def read_band(self, band_number):
#     def read_pixel(self, sample_number, line_number):
# 
# Path: arsf_envi_reader/envi_header.py
# ENVI_TO_NUMPY_DTYPE = {'1':  numpy.uint8,
#                        '2':  numpy.int16,
#                        '3':  numpy.int32,
#                        '4':  numpy.float32,
#                        '5':  numpy.float64,
#                        '6':  numpy.complex64,
#                        '9':  numpy.complex128,
#                        '12': numpy.uint16,
#                        '13': numpy.uint32,
#                        '14': numpy.int64,
#                        '15': numpy.uint64}
# def find_hdr_file(rawfilename):
# def read_hdr_file(hdrfilename, keep_case=False):
# def write_envi_header(filename, header_dict):


import argparse
from scipy import signal
from arsf_envi_reader import numpy_bin_reader
from arsf_envi_reader import envi_header
if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("inputimage", nargs=1,
                        type=str, help="Input image")
    parser.add_argument("outputimage", nargs=1,
                        type=str, help="Output image")
    args = parser.parse_args()

    # Open input file
    in_data = numpy_bin_reader.BilReader(args.inputimage[0])

    # Open file for output
    out_file = open(args.outputimage[0], "wb")

    for i, line in enumerate(in_data):
        print(" Line {0}/{1}".format(str(i).zfill(len(str(in_data.lines))),
                                     in_data.lines), end="\r")
        # Apply filter
        out_line = signal.savgol_filter(line,
                                        WINDOW_SIZE, POLY_ORDER,
                                        axis=0)
        # Remove negative values
        out_line[out_line < 0] = 0
        # Set output to the same as input
        out_line = out_line.astype(line.dtype)
        out_line.tofile(out_file)

    print(" Line {0}/{0}".format(in_data.lines), end="\n")

    # Copy header

# Path: arsf_envi_reader/numpy_bin_reader.py
# HAVE_ARSF_BINARYREADER = False
#     HAVE_ARSF_BINARYREADER = True
# class _BinaryReader(collections.Iterator):
# class BilReader(_BinaryReader):
# class BsqReader(_BinaryReader):
#     def __init__(self, input_file):
#     def __next__(self): pass
#     def read_line(self, line_number):
#     def read_band(self, band_number):
#     def read_pixel(self, sample_number, line_number):
#     def next(self):
#     def get_hdr_dict(self):
#     def get_num_samples(self):
#     def get_num_lines(self):
#     def get_num_bands(self):
#     def have_arsf_binaryreader(self):
#     def check_interleave(self, interleave):
#     def check_size(binary_file):
#     def __del__(self):
#     def __next__(self):
#     def read_line(self, line_number):
#     def read_band(self, band_number):
#     def read_pixel(self, sample_number, line_number):
#     def __next__(self):
#     def read_line(self, line_number):
#     def read_band(self, band_number):
#     def read_pixel(self, sample_number, line_number):
# 
# Path: arsf_envi_reader/envi_header.py
# ENVI_TO_NUMPY_DTYPE = {'1':  numpy.uint8,
#                        '2':  numpy.int16,
#                        '3':  numpy.int32,
#                        '4':  numpy.float32,
#                        '5':  numpy.float64,
#                        '6':  numpy.complex64,
#                        '9':  numpy.complex128,
#                        '12': numpy.uint16,
#                        '13': numpy.uint32,
#                        '14': numpy.int64,
#                        '15': numpy.uint64}
# def find_hdr_file(rawfilename):
# def read_hdr_file(hdrfilename, keep_case=False):
# def write_envi_header(filename, header_dict):


# Path: apply_savgol_filter.py
import argparse
from scipy import signal
from arsf_envi_reader import numpy_bin_reader
from arsf_envi_reader import envi_header

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("inputimage", nargs=1,
                        type=str, help="Input image")
    parser.add_argument("outputimage", nargs=1,
                        type=str, help="Output image")
    args = parser.parse_args()

    # Open input file
    in_data = numpy_bin_reader.BilReader(args.inputimage[0])

    # Open file for output
    out_file = open(args.outputimage[0], "wb")

    for i, line in enumerate(in_data):
        print(" Line {0}/{1}".format(str(i).zfill(len(str(in_data.lines))),
                                     in_data.lines), end="\r")
        # Apply filter
        out_line = signal.savgol_filter(line,
                                        WINDOW_SIZE, POLY_ORDER,
                                        axis=0)
        # Remove negative values
        out_line[out_line < 0] = 0
        # Set output to the same as input
        out_line = out_line.astype(line.dtype)
        out_line.tofile(out_file)

    print(" Line {0}/{0}".format(in_data.lines), end="\n")

    # Copy header

    envi_header.write_envi_header(args.outputimage[0] + ".hdr",