"""Reader for GAC POD data."""

import logging

import numpy as np

from pygac.gac_reader import GACReader
from pygac.pod_reader import PODReader, main_pod

LOG = logging.getLogger(__name__)

scanline = np.dtype([("scan_line_number", ">i2"),
                     ("time_code", ">u2", (3, )),
                     ("quality_indicators", ">u4"),
                     ("calibration_coefficients", ">i4", (10, )),
                     ("number_of_meaningful_zenith_angles_and_earth_location_appended",
                      ">u1"),
                     ("solar_zenith_angles", "i1", (51, )),
                     ("earth_location", [("lats", ">i2"),
                                         ("lons", ">i2")], (51,)),
                     ("telemetry", ">u4", (35, )),
                     ("sensor_data", ">u4", (682, )),
                     ("add_on_zenith", ">u2", (10, )),
                     ("clock_drift_delta", ">u2"),
                     ("spare3", "u2", (11, ))])


class GACPODReader(GACReader, PODReader):
    """The GAC POD reader class.

    The `scan_points` attributes provides the position of the longitude and latitude points to
    compute relative to the full swath width.

    The offset attribute tells where in the file the scanline data starts.
    """

    def __init__(self, *args, **kwargs):
        """Init the GAC POD reader."""
        GACReader.__init__(self, *args, **kwargs)
        self.scanline_type = scanline
        # GAC POD files were originally written to tapes using 6440 byte physical records
        # with two 3220 logical records per physical record. As the header is in the first
        # physical record the scanline data will start at offset 6440 in the filestream.
        # This means the second logical record (at offset 3220) is not used and contains
        # junk / padding data. The data often appears to be a valid scanline, but it is a
        # duplicate of a data appearing later in the file and should not be used.
        self.offset = 6440
        self.scan_points = np.arange(3.5, 2048, 5)


def main(filename, start_line, end_line):
    """Generate a l1c file."""
    return main_pod(GACPODReader, filename, start_line, end_line)


if __name__ == "__main__":
    import sys
    main(sys.argv[1], sys.argv[2], sys.argv[3])
