# Path: atrain_match/utils/runutils.py
# def do_some_geo_obj_logging(GeoObj):
#     import time
#     tim1 = time.strftime("%Y%m%d %H:%M",
#                          time.gmtime(GeoObj.sec1970_start))
#     tim2 = time.strftime("%Y%m%d %H:%M",
#                          time.gmtime(GeoObj.sec1970_end))
#     logger.debug("Starttime: %s, end time: %s", tim1, tim2)
#     logger.debug("Min lon: %f, max lon: %d",
#                  np.min(np.where(
#                      np.equal(GeoObj.longitude, GeoObj.nodata),
#                      99999,
#                      GeoObj.longitude)),
#                  np.max(GeoObj.longitude))
#     logger.debug("Min lat: %d, max lat: %d",
#                  np.min(np.where(
#                      np.equal(GeoObj.latitude, GeoObj.nodata),
#                      99999,
#                      GeoObj.latitude)),
#                  np.max(GeoObj.latitude))
# 
# Path: atrain_match/cloudproducts/read_pps.py
# class AllImagerData(object):
#     """Class to hold all imager cloudproduct data."""
# 
#     def __init__(self, array_dict=None):
#         """Init with data from array_dict, or empty."""
#         self.latitude = None
#         self.longitude = None
#         self.nodata = None  # latlon nodata
#         self.sec1970_end = None
#         self.sec1970_start = None
#         self.time = None
#         self.num_of_lines = None
#         self.instrument = "imager"
#         # self.imager_geo = None
#         self.imager_angles = None
#         self.imager_channeldata = None
#         self.ctype = None
#         self.cma = None
#         self.ctth = None
#         self.aux = AuxiliaryObj({'surftemp': None})
#         self.cpp = None
#         self.nwp_segments = None
#         if array_dict is not None:
#             self.__dict__.update(array_dict)
# 
# class CtypeObj:
#     """Class to hold imager data for cloud type."""
# 
#     def __init__(self):
#         """Init all datasets to None."""
#         self.cloudtype = None
#         self.ct_statusflag = None
#         self.ct_quality = None
#         self.ct_conditions = None
#         self.phaseflag = None  # v2012
# 
# class CtthObj:
#     """Class to hold imager data for CTTH."""
# 
#     def __init__(self):
#         """Init all datasets to None."""
#         self.height = None
#         self.height_corr = None
#         self.temperature = None
#         self.pressure = None
#         self.ctth_statusflag = None
# 
# class CmaObj:
#     """Class to hold imager data for CMA and CMAPROB."""
# 
#     def __init__(self):
#         """Init all datasets to None."""
#         self.cma_ext = None
#         self.cma_quality = None
#         self.cma_bin = None
#         self.cma_prob = None
#         self.cma_aflag = None
#         self.cma_testlist0 = None  # new in v2018
#         self.cma_testlist1 = None  # new in v2018
#         self.cma_testlist2 = None  # new in v2018
#         self.cma_testlist3 = None  # new in v2018
#         self.cma_testlist4 = None  # new in v2018
#         self.cma_testlist5 = None  # new in v2018
# 
# class ImagerAngObj(object):
#     """Class to hold imager angle data."""
# 
#     def __init__(self):
#         self.satz = SmallDataObject()
#         self.sunz = SmallDataObject()
#         self.satazimuth = SmallDataObject()
#         self.sunazimuth = SmallDataObject()
#         self.azidiff = SmallDataObject()


import atrain_match.config as config
import os
import netCDF4
import numpy as np
import calendar
import logging
from atrain_match.utils.runutils import do_some_geo_obj_logging
from atrain_match.cloudproducts.read_pps import (
    AllImagerData,
    CtypeObj, CtthObj, CmaObj,
    ImagerAngObj)
from datetime import datetime
from datetime import timedelta
    from pyhdf.SD import SD, SDC
    ctype.ct_conditions = None
    return ctype, cma, ctth


def read_patmosx_angobj(patmosx_nc):
    """Read angles info from filename."""
    angle_obj = ImagerAngObj()
    angle_obj.satz.data = patmosx_nc.variables['sensor_zenith_angle'][0, :, :].astype(np.float)
    angle_obj.sunz.data = patmosx_nc.variables['solar_zenith_angle'][0, :, :].astype(np.float)
    angle_obj.azidiff.data = None
    return angle_obj


def read_patmosx_geoobj(patmosx_nc, filename, cross, SETTINGS):
    """Read geolocation and time info from filename."""
    cloudproducts = AllImagerData()
    latitude_v = patmosx_nc.variables['latitude'][:].astype(np.float)
    longitude_v = patmosx_nc.variables['longitude'][:].astype(np.float)
    cloudproducts.latitude = np.repeat(latitude_v[:, np.newaxis], len(longitude_v), axis=1)
    cloudproducts.longitude = np.repeat(longitude_v[np.newaxis, :], len(latitude_v), axis=0)
    cloudproducts.nodata = -999
    date_time_start = cross.time
    date_time_end = cross.time + timedelta(seconds=SETTINGS['SAT_ORBIT_DURATION'])
    cloudproducts.sec1970_start = calendar.timegm(date_time_start.timetuple())
    cloudproducts.sec1970_end = calendar.timegm(date_time_end.timetuple())
    frac_hour = patmosx_nc.variables['scan_line_time'][0, :, :].astype(np.float)
    if np.ma.is_masked(frac_hour):
        frac_hour = frac_hour.data
    seconds = frac_hour*60*60.0
    cloudproducts.time = seconds + patmosx_nc.variables['time']

# Path: atrain_match/utils/runutils.py
# def do_some_geo_obj_logging(GeoObj):
#     import time
#     tim1 = time.strftime("%Y%m%d %H:%M",
#                          time.gmtime(GeoObj.sec1970_start))
#     tim2 = time.strftime("%Y%m%d %H:%M",
#                          time.gmtime(GeoObj.sec1970_end))
#     logger.debug("Starttime: %s, end time: %s", tim1, tim2)
#     logger.debug("Min lon: %f, max lon: %d",
#                  np.min(np.where(
#                      np.equal(GeoObj.longitude, GeoObj.nodata),
#                      99999,
#                      GeoObj.longitude)),
#                  np.max(GeoObj.longitude))
#     logger.debug("Min lat: %d, max lat: %d",
#                  np.min(np.where(
#                      np.equal(GeoObj.latitude, GeoObj.nodata),
#                      99999,
#                      GeoObj.latitude)),
#                  np.max(GeoObj.latitude))
# 
# Path: atrain_match/cloudproducts/read_pps.py
# class AllImagerData(object):
#     """Class to hold all imager cloudproduct data."""
# 
#     def __init__(self, array_dict=None):
#         """Init with data from array_dict, or empty."""
#         self.latitude = None
#         self.longitude = None
#         self.nodata = None  # latlon nodata
#         self.sec1970_end = None
#         self.sec1970_start = None
#         self.time = None
#         self.num_of_lines = None
#         self.instrument = "imager"
#         # self.imager_geo = None
#         self.imager_angles = None
#         self.imager_channeldata = None
#         self.ctype = None
#         self.cma = None
#         self.ctth = None
#         self.aux = AuxiliaryObj({'surftemp': None})
#         self.cpp = None
#         self.nwp_segments = None
#         if array_dict is not None:
#             self.__dict__.update(array_dict)
# 
# class CtypeObj:
#     """Class to hold imager data for cloud type."""
# 
#     def __init__(self):
#         """Init all datasets to None."""
#         self.cloudtype = None
#         self.ct_statusflag = None
#         self.ct_quality = None
#         self.ct_conditions = None
#         self.phaseflag = None  # v2012
# 
# class CtthObj:
#     """Class to hold imager data for CTTH."""
# 
#     def __init__(self):
#         """Init all datasets to None."""
#         self.height = None
#         self.height_corr = None
#         self.temperature = None
#         self.pressure = None
#         self.ctth_statusflag = None
# 
# class CmaObj:
#     """Class to hold imager data for CMA and CMAPROB."""
# 
#     def __init__(self):
#         """Init all datasets to None."""
#         self.cma_ext = None
#         self.cma_quality = None
#         self.cma_bin = None
#         self.cma_prob = None
#         self.cma_aflag = None
#         self.cma_testlist0 = None  # new in v2018
#         self.cma_testlist1 = None  # new in v2018
#         self.cma_testlist2 = None  # new in v2018
#         self.cma_testlist3 = None  # new in v2018
#         self.cma_testlist4 = None  # new in v2018
#         self.cma_testlist5 = None  # new in v2018
# 
# class ImagerAngObj(object):
#     """Class to hold imager angle data."""
# 
#     def __init__(self):
#         self.satz = SmallDataObject()
#         self.sunz = SmallDataObject()
#         self.satazimuth = SmallDataObject()
#         self.sunazimuth = SmallDataObject()
#         self.azidiff = SmallDataObject()


# Path: atrain_match/cloudproducts/read_patmosx.py
import atrain_match.config as config
import os
import netCDF4
import numpy as np
import calendar
import logging
from atrain_match.utils.runutils import do_some_geo_obj_logging
from atrain_match.cloudproducts.read_pps import (
    AllImagerData,
    CtypeObj, CtthObj, CmaObj,
    ImagerAngObj)
from datetime import datetime
from datetime import timedelta
    from pyhdf.SD import SD, SDC

    ctype.ct_conditions = None
    return ctype, cma, ctth


def read_patmosx_angobj(patmosx_nc):
    """Read angles info from filename."""
    angle_obj = ImagerAngObj()
    angle_obj.satz.data = patmosx_nc.variables['sensor_zenith_angle'][0, :, :].astype(np.float)
    angle_obj.sunz.data = patmosx_nc.variables['solar_zenith_angle'][0, :, :].astype(np.float)
    angle_obj.azidiff.data = None
    return angle_obj


def read_patmosx_geoobj(patmosx_nc, filename, cross, SETTINGS):
    """Read geolocation and time info from filename."""
    cloudproducts = AllImagerData()
    latitude_v = patmosx_nc.variables['latitude'][:].astype(np.float)
    longitude_v = patmosx_nc.variables['longitude'][:].astype(np.float)
    cloudproducts.latitude = np.repeat(latitude_v[:, np.newaxis], len(longitude_v), axis=1)
    cloudproducts.longitude = np.repeat(longitude_v[np.newaxis, :], len(latitude_v), axis=0)
    cloudproducts.nodata = -999
    date_time_start = cross.time
    date_time_end = cross.time + timedelta(seconds=SETTINGS['SAT_ORBIT_DURATION'])
    cloudproducts.sec1970_start = calendar.timegm(date_time_start.timetuple())
    cloudproducts.sec1970_end = calendar.timegm(date_time_end.timetuple())
    frac_hour = patmosx_nc.variables['scan_line_time'][0, :, :].astype(np.float)
    if np.ma.is_masked(frac_hour):
        frac_hour = frac_hour.data
    seconds = frac_hour*60*60.0
    cloudproducts.time = seconds + patmosx_nc.variables['time']

    do_some_geo_obj_logging(cloudproducts)