def get_maia_ct_flag(ct_flag):
    """Get maia cloud type flag."""
    # bits 4-8, start at 0 counting
    maia_ct_flag = (16 * np.bitwise_and(np.right_shift(ct_flag, 8), 1) +
                    8 * np.bitwise_and(np.right_shift(ct_flag, 7), 1) +
                    4 * np.bitwise_and(np.right_shift(ct_flag, 6), 1) +
                    2 * np.bitwise_and(np.right_shift(ct_flag, 5), 1) +
                    1 * np.bitwise_and(np.right_shift(ct_flag, 4), 1))
    return maia_ct_flag def get_day_night_twilight_info_maia(cm_flag):
    """Get day/night/twilight flag from MAIA cm_flag."""
    # bit 13-14
    maia_cm_flag = (2 * np.bitwise_and(np.right_shift(cm_flag, 14), 1) +
                    1 * np.bitwise_and(np.right_shift(cm_flag, 13), 1))
    day_flag = np.where(maia_cm_flag == 2, True, False)  # include also sunglint in day ==3
    night_flag = np.where(maia_cm_flag == 0, True, False)
    twilight_flag = np.where(maia_cm_flag == 1, True, False)
    logger.debug("number of day {:d}".format(np.sum(day_flag)))
    logger.debug("number of night {:d}".format(np.sum(night_flag)))
    logger.debug("number of twilight {:d}".format(np.sum(twilight_flag)))
    all_dnt_flag = np.ones(maia_cm_flag.shape)
    no_qflag = 0 * all_dnt_flag  # dummy flag
    return (no_qflag, night_flag, twilight_flag, day_flag, all_dnt_flag) 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)) 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 AuxiliaryObj(object):
    """Class to hold auxiliary cloudproduct data."""

    def __init__(self, array_dict):
        """Init with arrays from dictionary array_dict."""
        self.surftemp = None
        self.t500 = None
        self.t700 = None
        self.t850 = None
        self.t950 = None
        self.t1000 = None
        self.t900 = None
        self.t250 = None
        self.t800 = None
        self.psur = None
        self.ptro = None
        self.t2m = None
        self.ciwv = None
        self.text_r06 = None
        self.text_t11 = None
        self.text_t37t12 = None
        self.text_t11t12 = None
        self.text_t37 = None
        self.thr_t11ts_inv = None
        self.thr_t11t37_inv = None
        self.thr_t37t12_inv = None
        self.thr_t11t12_inv = None
        self.thr_t85t11_inv = None
        self.thr_t11ts = None
        self.thr_t11t37 = None
        self.thr_t37t12 = None
        self.thr_t11t12 = None
        self.thr_t85t11 = None
        self.thr_r06 = None
        self.thr_r09 = None
        self.emis1 = None
        self.emis6 = None
        self.emis8 = None
        self.emis9 = None
        self.snowa = None
        self.snowd = None
        self.seaice = None
        self.r37 = None
        self.landuse = None
        self.fractionofland = None
        self.elevation = 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 def create_imager_time(obt, values=None):
    """Make matrix with time for each pixel from start and end time."""
    if obt.sec1970_start < 0:
        obt.sec1970_start = calendar.timegm(values['date_time'])
    if obt.sec1970_end < obt.sec1970_start:
        obt.sec1970_end = calendar.timegm(values['date_time_end'])
    obt.time = np.linspace(obt.sec1970_start, obt.sec1970_end, obt.num_of_lines)
    return obt 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()
from atrain_match.utils.get_flag_info import get_maia_ct_flag, get_day_night_twilight_info_maia
from atrain_match.utils.runutils import do_some_geo_obj_logging
from atrain_match.cloudproducts.read_pps import (
    AllImagerData, AuxiliaryObj,
    CtypeObj, CtthObj, CmaObj,
    create_imager_time,
    ImagerAngObj)
from datetime import datetime
    from atrain_match.utils.runutils import unzip_file
import atrain_match.config as config
import os
import h5py
import numpy as np
import calendar
import logging
# -*- coding: utf-8 -*-
# Copyright (c) 2009-2019 atrain_match developers
#
# This file is part of atrain_match.
#
# atrain_match is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# atrain_match is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with atrain_match.  If not, see <http://www.gnu.org/licenses/>.
"""
  Use this module to read maia cloudproducts
  2016 SMHI, N.Hakansson
"""

logger = logging.getLogger(__name__)

ATRAIN_MATCH_NODATA = config.NODATA


def get_satid_datetime_orbit_from_fname_maia(imager_filename):
    # Get satellite name, time, and orbit number from imager_file
    # viiCT_npp_DB_20120817_S035411_E035535_DES_N_La052_Lo-027_00001.h5
    sl_ = os.path.basename(imager_filename).split('_')
    date_time = datetime.strptime(sl_[3] + sl_[4], '%Y%m%dS%H%M%S')

    sat_id = sl_[1].lower()
    values = {"satellite": sat_id,
              "date_time": date_time,
              "orbit": "99999",
              "date": date_time.strftime("%Y%m%d"),
              "year": date_time.year,
              "month": "%02d" % (date_time.month),
              "time": date_time.strftime("%H%M"),
              # "basename":sat_id + "_" + date_time.strftime("%Y%m%d_%H%M_99999"), # "20080613002200-ESACCI",
              "ccifilename": imager_filename,
              "ppsfilename": None}
    values['basename'] = values["satellite"] + "_" + \
        values["date"] + "_" + values["time"] + "_" + values["orbit"]
    return values


def maia_read_all(filename):
    """Read geolocation, angles info, ctth, and cloudtype
    """

    unzipped = unzip_file(filename)
    if unzipped:
        filename = unzipped

    logger.info("Opening file %s", filename)
    with h5py.File(filename, 'r') as maia_h5:
        logger.info("Reading longitude, latitude and time ...")
        cloudproducts = read_maia_geoobj(maia_h5, filename)
        logger.info("Reading angles ...")
        cloudproducts.imager_angles = read_maia_angobj(maia_h5)
        logger.info("Reading cloud type ...")
        # , angle_obj)
        ctype, cma, ctth = read_maia_ctype_cmask_ctth(maia_h5)
        cloudproducts.cma = cma
        cloudproducts.ctth = ctth
        cloudproducts.ctype = ctype

        logger.info("Reading surface temperature")
        surft = read_maia_surftemp(maia_h5)
        cloudproducts.nwp = AuxiliaryObj({'surftemp': surft})
        logger.info("Not reading cloud microphysical properties")
        logger.info("Not reading channel data")

    if unzipped:
        os.remove(unzipped)

    return cloudproducts


def read_maia_ctype_cmask_ctth(maia_h5):
    """Read cloudtype and flag info from filename
    """
    ctth = CtthObj()

    ctype = CtypeObj()