import logging
import time

from django.db.backends.util import CursorWrapper

logger = logging.getLogger(__name__)

class CursorLoggingWrapper(CursorWrapper):

    def execute(self, sql, params=None):
        executor = self.cursor.execute
        self.db.validate_no_broken_transaction()
        with self.db.wrap_database_errors:
            if params is None:
                return self.run_sql(executor, sql)
            return self.run_sql(executor, sql, params)

    def executemany(self, sql, param_list):
        executor = self.cursor.executemany
        self.db.validate_no_broken_transaction()
        with self.db.wrap_database_errors:
            return self.run_sql(executor, sql, param_list)

    def run_sql(self, executor, sql, params=None):
        start = time.time()
        cursor = executor(sql) if not params else executor(sql, params)
        end = time.time()
        logger.info("[QUERY]: %s - params %s" % (sql, str(params)))
        logger.info("[TIME]: %.6f" % (end - start))
        return cursor


#coding:utf-8
"""
info:   观看视频
author: NetFj@sina.com
file:   12_playtv.py.py
time:   2019/2/26.18:33
"""

import time,webbrowser,pyautogui,random

class study():
    def __init__(self):     #入口：定义软件名称，默认网站
        self.brower = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
        self.site_name = r'https://www.xuexi.cn/'

    def open_site(self):    # 打开一个网站
        webbrowser.register('chrome', None, webbrowser.BackgroundBrowser(self.brower))
        webbrowser.get('chrome').open(self.site_name, new=1, autoraise=True)
        time.sleep(5)

        #窗口最大化
        pyautogui.keyDown('alt')
        pyautogui.press('space')
        pyautogui.press('x')
        pyautogui.keyUp('alt')

        #定位到页首
        pyautogui.hotkey('ctrl','home')
        time.sleep(2)

    def open_link(self,x,y):    #打开指定位置的链接
        time.sleep(1)
        pyautogui.click(x, y, button='left')
        time.sleep(3)

    def open_tv_xwlb_old(self):
        self.open_link(620, 513)
        pyautogui.press('pagedown', presses=2)
        pyautogui.moveTo(452, 219)
        time.sleep(5)
        pyautogui.moveTo(1114, 278)
        time.sleep(3)
        pyautogui.click(button='left')
        time.sleep(5)
        self.open_link(417, 850)
        pyautogui.press('down', presses=5)

    def open_tv_short(self):
        print('播放短片开始...')
        self.open_site()
        self.open_link(620, 513)
        pyautogui.press('pagedown', presses=2)
        self.open_link(460, 500)

        ts = 20+random.randint(0,30)

        self.open_link(435,850)
        pyautogui.press('down', presses=10)
        time.sleep(ts)
        pyautogui.hotkey('ctrl', 'w')  # 关闭

        self.open_link(435, 890)
        pyautogui.press('down', presses=10)
        time.sleep(ts)
        pyautogui.hotkey('ctrl', 'w')  # 关闭

        self.open_link(435, 930)
        pyautogui.press('down', presses=10)
        time.sleep(ts)
        pyautogui.hotkey('ctrl', 'w')  # 关闭

        self.open_link(435, 970)
        pyautogui.press('down', presses=10)
        time.sleep(ts)
        pyautogui.hotkey('ctrl', 'w')  # 关闭
        pyautogui.hotkey('ctrl', 'w')  # 关闭
        pyautogui.hotkey('ctrl', 'w')  # 关闭
        time.sleep(3)
        print('播放短片...结束!')

    def open_tv_xwlb(self):
        print('播放新闻联播开始...')
        self.open_link(620, 513)
        pyautogui.press('pagedown', presses=2)
        self.open_link(460, 500)
        self.open_link(1040, 795)
        self.open_link(405, 850)
        pyautogui.press('down', presses=5)
        print('将要播放30分钟...')
        time.sleep(30*60+random.randint(0,100))     #播放30多分钟
        pyautogui.hotkey('ctrl', 'w')  # 关闭
        pyautogui.hotkey('ctrl', 'w')  # 关闭
        pyautogui.hotkey('ctrl', 'w')  # 关闭
        pyautogui.hotkey('ctrl', 'w')  # 关闭
        time.sleep(3)
        print('播放新闻联播...结束!')

def main():
    stu = study()
    stu.open_tv_short()
    stu.open_tv_xwlb()

if __name__ == "__main__":
    main()


# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Execute benchmark."""
from __future__ import print_function

import argparse
import datetime
import importlib
import json
import logging
import os
import re
import sys
import time
import traceback

import perfzero.perfzero_config as perfzero_config
from perfzero.process_info_tracker import ProcessInfoTracker
import perfzero.report_utils as report_utils
from perfzero.tensorflow_profiler import TensorFlowProfiler
import perfzero.utils as utils

class BenchmarkRunner(object):
  """Execute benchmark and report results."""

  def __init__(self, config):
    self.config = config
    project_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
    self.workspace_dir = os.path.join(project_dir, config.workspace)
    self.site_packages_dir = os.path.join(self.workspace_dir, 'site-packages')
    self.root_output_dir = os.path.join(self.workspace_dir, 'output')
    self.benchmark_execution_time = {}

  def _setup(self):
    """Download data and checkout git repository."""

    # Acticate gcloud service
    start_time = time.time()
    utils.setup_python_path(self.site_packages_dir, self.config.python_path_str)
    utils.active_gcloud_service(self.config.gcloud_key_file_url,
                                self.workspace_dir)
    utils.make_dir_if_not_exist(self.root_output_dir)
    self.benchmark_execution_time['activate_gcloud_service'] = (
        time.time() - start_time)

    # Download data
    start_time = time.time()
    utils.download_data(utils.parse_data_downloads_str(
        self.config.root_data_dir, self.config.gcs_downloads_str))
    utils.download_data(utils.parse_data_downloads_str(
        self.config.root_data_dir, self.config.data_downloads_str))
    self.benchmark_execution_time['download_data'] = time.time() - start_time

    # Checkout git repositories
    start_time = time.time()
    site_package_info = utils.checkout_git_repos(
        self.config.get_git_repos(self.site_packages_dir),
        self.config.force_update)
    self.benchmark_execution_time['checkout_repository'] = (
        time.time() - start_time)

    self.stream_handler = logging.StreamHandler(sys.stdout)
    self.stream_handler.setFormatter(
        logging.Formatter('%(asctime)s %(levelname)s: %(message)s'))
    logging.getLogger().addHandler(self.stream_handler)
    return site_package_info

  def _get_benchmark_methods(self):
    """Returns list of benchmark methods to execute."""
    filter_prefix = 'filter:'
    benchmark_methods = []
    for benchmark_method_pattern in self.config.benchmark_method_patterns:
      if filter_prefix not in benchmark_method_pattern:
        benchmark_methods.append(benchmark_method_pattern)
      else:
        index = benchmark_method_pattern.find(filter_prefix)
        benchmark_class = benchmark_method_pattern[:index - 1]
        pattern = benchmark_method_pattern[index + len(filter_prefix):]
        class_instance = self._instantiate_benchmark_class(benchmark_class,
                                                           '/dev/null',
                                                           '')
        for benchmark_method_name in dir(class_instance):
          if re.match(pattern, benchmark_method_name):
            benchmark_methods.append(benchmark_class + '.' +
                                     benchmark_method_name)
    return benchmark_methods

  def run_benchmark(self):
    """Run benchmark."""
    site_package_info = self._setup()
    has_exception = False
    benchmark_success_results = {}
    benchmark_output_dirs = {}

    for benchmark_method in self._get_benchmark_methods():
      start_timestamp = time.time()
      execution_timestamp = start_timestamp
      method_has_exception = False
      execution_id = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S-%f')
      output_dir = os.path.join(self.root_output_dir, execution_id)
      utils.make_dir_if_not_exist(output_dir)
      benchmark_output_dirs[benchmark_method] = output_dir
      benchmark_class, benchmark_method_name = benchmark_method.rsplit('.', 1)
      benchmark_class_name = benchmark_class.rsplit('.', 1)[1]

      tensorflow_profiler = TensorFlowProfiler(
          self.config.profiler_enabled_time_str, output_dir)
      process_info_tracker = ProcessInfoTracker(output_dir)
      process_info = None

      # Setup per-method file logger
      filehandler = logging.FileHandler(
          filename=os.path.join(output_dir, 'perfzero.log'), mode='w')
      filehandler.setFormatter(
          logging.Formatter('%(asctime)s %(levelname)s: %(message)s'))
      logging.getLogger().addHandler(filehandler)

      try:
        class_instance = self._instantiate_benchmark_class(
            benchmark_class, output_dir, self.config.root_data_dir)
        # tf.test.Benchmark.report_benchmark() writes results to a file with
        # path benchmark_result_file_path_prefix + benchmark_method
        benchmark_result_file_path_prefix = os.path.join(output_dir, 'proto_')
        os.environ['TEST_REPORT_FILE_PREFIX'] = benchmark_result_file_path_prefix  # pylint: disable=line-too-long
        benchmark_result_file_path = '{}{}.{}'.format(
            benchmark_result_file_path_prefix,
            benchmark_class_name,
            benchmark_method_name)

        # Start background threads for profiler and system info tracker
        tensorflow_profiler.start()
        process_info_tracker.start()

        # Run benchmark method
        execution_timestamp = time.time()
        logging.info('Starting benchmark execution: %s', benchmark_method)
        getattr(class_instance, benchmark_method_name)()
        logging.info('Stopped benchmark: %s', benchmark_method)

        # Read and build benchmark results
        raw_benchmark_result = utils.read_benchmark_result(
            benchmark_result_file_path)
        # Explicitly overwrite the name to be the full path to benchmark method
        raw_benchmark_result['name'] = benchmark_method
      except Exception:  # pylint: disable=broad-except
        logging.error('Benchmark execution for %s failed due to error:\n %s',
                      benchmark_method, traceback.format_exc())
        method_has_exception = True
        has_exception = True
        raw_benchmark_result = {}
        raw_benchmark_result['name'] = benchmark_method
        raw_benchmark_result['wall_time'] = -1
        raw_benchmark_result['extras'] = {}
      finally:
        # Stop background threads for profiler and system info tracker
        process_info = process_info_tracker.stop()
        tensorflow_profiler.stop()

      upload_timestamp = time.time()
      benchmark_result = report_utils.build_benchmark_result(
          raw_benchmark_result, method_has_exception)
      benchmark_success_results[benchmark_method] = benchmark_result['succeeded']  # pylint: disable=line-too-long
      execution_summary = report_utils.build_execution_summary(
          execution_timestamp,
          execution_id,
          self.config.ml_framework_build_label,
          self.config.execution_label,
          self.config.platform_name,
          self.config.system_name,
          self.config.output_gcs_url,
          benchmark_result,
          self.config.get_env_vars(),
          self.config.get_flags(),
          site_package_info,
          process_info,
          method_has_exception)
      report_utils.upload_execution_summary(
          self.config.bigquery_project_name,
          self.config.bigquery_dataset_table_name,
          execution_summary)
      logging.info('Benchmark execution for %s completed with summary:\n %s',
                   benchmark_method, json.dumps(execution_summary, indent=2))
      utils.maybe_upload_to_gcs(output_dir, self.config.output_gcs_url)
      logging.getLogger().removeHandler(filehandler)
      self.benchmark_execution_time[benchmark_method] = {
          'class_initialization': execution_timestamp - start_timestamp,
          'method_execution': upload_timestamp - execution_timestamp,
          'log_upload': time.time() - upload_timestamp
      }

      if self.config.profiler_enabled_time_str:
        relative_output_dir = output_dir[output_dir.find('benchmark'):]
        print('\nExecute the command below to start tensorboard server using '
              'the collected profiler data:\ntensorboard --logdir={}\n\n'
              'Open localhost:6006 in your browser to access the Tensorbord '
              'GUI. Use ssh with port forwarding if tensorboard is running on '
              'a remote machine.\n'.format(relative_output_dir))

    print('Benchmark execution time in seconds by operation:\n {}'.format(
        json.dumps(self.benchmark_execution_time, indent=2)))
    print('Benchmark success results:\n{}'.format(
        json.dumps(benchmark_success_results, indent=2)))
    print('Benchmark local output directories:\n{}'.format(
        json.dumps(benchmark_output_dirs, indent=2)))
    if has_exception:
      sys.exit(1)

  def _instantiate_benchmark_class(self, benchmark_class,
                                   output_dir, root_data_dir):
    """Return initialized benchmark class."""
    module_import_path, class_name = benchmark_class.rsplit('.', 1)
    module = importlib.import_module(module_import_path)
    class_ = getattr(module, class_name)
    instance = class_(output_dir=output_dir, root_data_dir=root_data_dir)

    return instance

if __name__ == '__main__':
  parser = argparse.ArgumentParser(
      formatter_class=argparse.ArgumentDefaultsHelpFormatter)
  perfzero_config.add_benchmark_parser_arguments(parser)
  FLAGS, unparsed = parser.parse_known_args()

  level = logging.DEBUG if FLAGS.debug else logging.INFO
  logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s',
                      level=level)

  if unparsed:
    logging.warning('Arguments %s are not recognized', unparsed)

  config_ = perfzero_config.PerfZeroConfig(mode='flags', flags=FLAGS)
  benchmark_runner = BenchmarkRunner(config_)
  benchmark_runner.run_benchmark()


from rest_framework.views import APIView
from rest_framework.generics import RetrieveDestroyAPIView
from rest_framework import status
from rest_framework.response import Response
from api_v1.serializers.applications import ApplicationSerializer
from api_v1.models import Application

class ApplicationList(APIView):
    """
        List all applications or create a Application instance.
    """
    serializer_class = ApplicationSerializer
    def get(self, request, format=None):
        """
            Return a list of all Applications.
        """
        application = Application.objects.all()
        serializer = ApplicationSerializer(application, many=True, context={'request': request})
        return Response(serializer.data)

    def post(self, request ,format=None):
        """
            Return a Application instance if application created.
        """
        serializer = ApplicationSerializer(data=request.data)

        if serializer.is_valid():
            instance = serializer.save()
            serializer = ApplicationSerializer(instance, context={'request': request})
            return Response(serializer.data, status=status.HTTP_201_CREATED)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

class ApplicationDetail(RetrieveDestroyAPIView):
    """
        Retrieve or delete a Application instance.
    """
    queryset = Application.objects.all()
    serializer_class = ApplicationSerializer


from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.authtoken.models import Token
from rest_framework.authentication import BasicAuthentication
from rest_framework.authentication import TokenAuthentication
from rest_framework import permissions
from rest_framework import parsers
import json
from django.shortcuts import redirect
from django.contrib.auth.models import User
from django.db.models import Q

from categories.models      import Category
from direct_messages.models import DirectMessage
from direct_messages.models import DirectMessageContent
from favorite.models        import Favorite
from items.models           import Item
from item_contacts.models   import ItemContact
from profiles.models        import Profile
from solicitudes.models     import Solicitud

from api.constants import SerializerContextKey
from api.constants import BtnChoice
from api.constants import CategoryValue

from api.serializers import ItemSerializer
from api.serializers import ProfileSerializer
from api.serializers import SolicitudSerializer
from api.serializers import ItemContactSerializer
from api.serializers import SolicitudSerializer

from api.utils import getTokenFromHeader
from api.utils import getUserByToken, getItemDetailUrl

class ItemListAPIView(APIView):
    """
    endpoint: 'items/list/'
    name: 'api:item_list'
    #dar,venderを一覧するView
    """

    def get(self, request, *args, **kwargs):
        serializerContext = {}
        item_objects = Item.objects.all().exclude(active=False).order_by("-created_at")
        serializer = ItemSerializer(item_objects, many=True)
        serializerContext[SerializerContextKey.ITEM_OBJECTS] = serializer.data

        return Response(serializerContext)

class ItemHomeListAPIView(APIView):
    """
    endpoint: 'api/items/home/list'
    name: "api:item_home_list"
    """
    def get(self, request, *args, **kwargs):
        serializerContext = {}
        #las cosas
        #item_objects_cosas = Item.objects.filter(category__number="1").filter(category__number="2").filter(category__number="3").order_by("-created_at")
        item_objects_cosas = Item.objects.filter(Q(category__number="1")|Q(category__number="2")|Q(category__number="3")).exclude(active=False).order_by("-id")
        serializer_cosas = ItemSerializer(item_objects_cosas, many=True)

        item_objects_habitacion = Item.objects.filter(Q(category__number="4")|Q(category__number="5")|Q(category__number="6")|Q(category__number="7")).exclude(active=False).order_by("-id")
        serializer_habitacion = ItemSerializer(item_objects_habitacion, many=True)

        item_objects_trabajo = Item.objects.filter(Q(category__number="8")|Q(category__number="9")).exclude(active=False).order_by("-id")
        serializer_trabajo = ItemSerializer(item_objects_trabajo, many=True)

        item_objects_tienda = Item.objects.filter(category__number="10").exclude(active=False).order_by("-id")
        serializer_tienda = ItemSerializer(item_objects_tienda, many=True)

        serializerContext[SerializerContextKey.ITEM_OBJECTS_COSAS]      = serializer_cosas.data
        serializerContext[SerializerContextKey.ITEM_OBJECTS_HABITACION] = serializer_habitacion.data
        serializerContext[SerializerContextKey.ITEM_OBJECTS_TRABAJO]    = serializer_trabajo.data
        serializerContext[SerializerContextKey.ITEM_OBJECTS_TIENDA]     = serializer_tienda.data
        return Response(serializerContext)

class ItemCategoryListAPIView(APIView):

    def get(self, request, *args, **kwargs):
        """機能

        endpoint: 'api/items/category/<int:pk>/items/list/'
        name: -
        """
        """テスト項目
        """
        category_number = self.kwargs["pk"]

        if category_number == 999:
            return redirect('api:item_home_list')
        elif category_number == 500:
            #500 --- Las Cosas...(1,2,3) dar o donar, busca donante, quiere avisarのカテゴリを一括にしたもの / categories.models.py 参照すること
            itemObjects = Item.objects.filter(Q(category__number="1")|Q(category__number="2")|Q(category__number="3")).exclude(active=False).order_by("-id")
            serializer  = ItemSerializer(itemObjects, many=True)
            serializerContext = {}
            serializerContext[SerializerContextKey.ITEM_OBJECTS] = serializer.data
            return Response(serializerContext)

        elif category_number == 600:
            #600 --- Habitacion...(4,5,6,7) buscar habitacion, alquilar habitacion, dar pencionado, busco pencionista
            itemObjects = Item.objects.filter(Q(category__number="4")|Q(category__number="5")|Q(category__number="6")|Q(category__number="7")).exclude(active=False).order_by("-id")
            serializer = ItemSerializer(itemObjects, many=True)
            serializerContext = {}
            serializerContext[SerializerContextKey.ITEM_OBJECTS] = serializer.data
            return Response(serializerContext)

        elif category_number == 700:
            #700 --- trabajo ...(8,9) buscar empleo, buscar trabajador
            itemObjects = Item.objects.filter(Q(category__number="8")|Q(category__number="9")).exclude(active=False).order_by("-id")
            serializer = ItemSerializer(itemObjects, many=True)
            serializerContext = {}
            serializerContext[SerializerContextKey.ITEM_OBJECTS] = serializer.data
            return Response(serializerContext)

        elif category_number == 800:
            #800 ___ Empresas y Servicios ...(10) publicidad de Enpresas y Servicios
            itemObjects = Item.objects.filter(category__number="10").exclude(active=False).order_by("-id")
            serializer = ItemSerializer(itemObjects, many=True)
            serializerContext = {}
            serializerContext[SerializerContextKey.ITEM_OBJECTS] = serializer.data
            return Response(serializerContext)

        #print(category_number)
        categoryObj = Category.objects.get(number=category_number)
        itemObjects = Item.objects.filter(category=categoryObj).exclude(active=False).exclude(active=False).order_by("-created_at")
        serializer  = ItemSerializer(itemObjects, many=True)
        serializerContext = {}
        serializerContext[SerializerContextKey.ITEM_OBJECTS] = serializer.data

        return Response(serializerContext)

class ItemCategoryLocalListAPIView(APIView):
    #dar,venderを一覧するView
    # このAPIViewを発行するときはクライアントは必ずユーザー認証が終わっているので、ユーザー認証等は考える必要ない。

    def get(self, request, *args, **kwargs):
        """機能

        endpoint: 'api/items/category/<int:pk>/items/list/local/'
        name: -
        """
        """テスト項目
        """

        token = getTokenFromHeader(self)
        userObj = getUserByToken(token)
        profileObj  = Profile.objects.get(user=userObj)

        category_number = self.kwargs["pk"]
        #print(category_number)
        categoryObj = Category.objects.get(number=category_number)
        itemObjects = Item.objects.filter(category=categoryObj).filter(adm1=profileObj.adm1).exclude(active=False).order_by("-created_at")
        serializer  = ItemSerializer(itemObjects, many=True)
        serializerContext = {}
        serializerContext[SerializerContextKey.ITEM_OBJECTS] = serializer.data

        return Response(serializerContext)

class ItemFavoriteListAPIVIiew(APIView):
    """
    requestUserがのfav記事リスト一覧を表示する
    endpoint: 'items/user/item_favorite_list/'
    name: -s
    """

    def get(self, request, *args, **kwargs):

        #アクセスユーザーの制限
        token = getTokenFromHeader(self)
        requestUser = getUserByToken(token)
        if requestUser == None:
            return Response({"result": "fail"})

        favItemObjects = Item.objects.filter(favorite_users=requestUser).exclude(active=False)
        serializer = ItemSerializer(favItemObjects, many=True)

        #serializerContextに表示するデータを格納
        serializerContext={}
        serializerContext[SerializerContextKey.ITEM_OBJECTS] = serializer.data

        #成功した場合のレスポンスを返す
        return Response(serializerContext)

class ItemDetailSerializerAPIView(APIView):

    authentication_classes = ()
    #permission_classes = (permissions.IsAuthenticated,)

    def get_object(self):

        try:
            pk = self.kwargs["pk"]
            return Item.objects.get(pk=pk)
        except Item.DoesNotExist:
            raise Response({"result":"fail", "detail":"no exists"})

    def isChangedCategoryValue(self, item_obj, jsonData):
        """機能
        patchのみで使うメソッド
        patchで送信されたデータと変更対象となるデータの比較を行い、categoryだけ変更があった場合にはTrueを返す
        """

        if item_obj.category.number != jsonData["category"]["number"]:
            return True
        return False

    def get(self, request, *args, **kwargs):

        """機能
        endpointに対応する記事データを返す

        endpoint: "api/items/<int:pk>/"
        name: "item_detail"

        必要なオブジェクト
        item_obj, profile_obj, solicitudes_objects, direct_messages_objects
        ItemContactに関しては最新の５コメントしか表示しない仕様である。

        """
        """テスト項目
        済 ItemオブジェクトがResponseとして返される
        済 endpoint内のintに対応するidの記事データが返される
        済 ItemContactオブジェクトがItemオブジェクトのプロパティとしてResponseで返される
        済 UserオブジェクトがItemオブジェクトのプロパティとしてResponseで返される
        済 SolicitudオブジェクトがItemオブジェクトのプロパティとしてResponseで返される
        """

        pk = self.kwargs["pk"]
        item_obj = Item.objects.get(id=pk)
        item_obj_serializer = ItemSerializer(item_obj)
        user_obj = item_obj.user
        profile_obj = Profile.objects.get(user=item_obj.user)
        profile_obj_serializer  = ProfileSerializer(profile_obj)
        solicitudes_objects     = item_obj.solicitudes.all()
        solicitudSerializer     = SolicitudSerializer(solicitudes_objects, many=True)
        item_contact_objects    = item_obj.item_contacts.all().order_by("-timestamp")[:5]
        item_contact_objects_serializer = ItemContactSerializer(item_contact_objects, many=True)
        direct_messages_objects = DirectMessage.objects.filter(item=item_obj)

        # 共通のデータをserializer_contextに格納する
        serializer_context = {}
        serializer_context["item_obj_serializer"] = item_obj_serializer.data
        serializer_context["profile_obj_serializer"] = profile_obj_serializer.data

        token = getTokenFromHeader(self)
        accessUser = getUserByToken(token)

        # serializer_context[SerializerContextKey.BTN_CHOICE]を設定
        if accessUser == None:
            serializer_context[SerializerContextKey.BTN_CHOICE] = BtnChoice.ANONYMOUS_USER_ACCESS

        elif accessUser == user_obj and solicitudes_objects.count() == 0:
            serializer_context[SerializerContextKey.BTN_CHOICE] = BtnChoice.NO_SOLICITUDES

        elif accessUser == user_obj and solicitudes_objects.count() != 0 and direct_messages_objects.count() == 0:
            serializer_context[SerializerContextKey.BTN_CHOICE] = BtnChoice.SELECT_SOLICITUDES

        elif accessUser == user_obj and direct_messages_objects.count() != 0:
            serializer_context[SerializerContextKey.BTN_CHOICE] = BtnChoice.GO_TRANSACTION

        elif accessUser != user_obj and solicitudes_objects.filter(applicant=Profile.objects.get(user=accessUser)).count() == 0 and direct_messages_objects.filter(item=item_obj).count() == 0:
            serializer_context[SerializerContextKey.BTN_CHOICE] = BtnChoice.SOLICITAR

        elif accessUser != user_obj and solicitudes_objects.filter(applicant=Profile.objects.get(user=accessUser)).count() != 0 and direct_messages_objects.count() == 0:
            serializer_context[SerializerContextKey.BTN_CHOICE] = BtnChoice.SOLICITADO

        elif accessUser != user_obj and direct_messages_objects.filter(participant=Profile.objects.get(user=accessUser)).count() == 1:
            serializer_context[SerializerContextKey.BTN_CHOICE] = BtnChoice.GO_TRANSACTION

        elif accessUser != user_obj and direct_messages_objects.filter(participant=Profile.objects.get(user=accessUser)).count() == 0:
            serializer_context[SerializerContextKey.BTN_CHOICE] = BtnChoice.CANNOT_TRANSACTION

        #データを送信
        return Response(serializer_context)

    def patch(self, request, *args, **kwargs):
        """
        endpoint: /api/items/<int:pk>/
        name: -
        """
        """テスト項目
        タイトルのみの変更の場合にはタイトルが変更される。
        カテゴリーだけの変更の場合にはカテゴリーの変更が反映される。

        """
        pk = self.kwargs["pk"]
        item_obj = Item.objects.get(id=pk)

        print("ここ通っている？？？")
        print(request.data)

        strJsonData = request.data["jsonData"]
        jsonData = json.loads(strJsonData)

        #categoryName = jsonData.pop("category")["name"]
        #print("CATEGORY_NAME" + categoryName)
        #category_obj = Category.objects.get(name=categoryName)

        imagesData = request.FILES
        #print("IMAGES_DATA    :   ", imagesData)

        isChanged = self.isChangedCategoryValue(item_obj, jsonData)
        if isChanged == True:
            category_obj = Category.objects.get(number=jsonData["category"]["number"])
            item_obj.category = category_obj
            item_obj.save()

        serializer = ItemSerializer(item_obj, data=jsonData, partial=True)
        print("is_valid? : ",serializer.is_valid())
        if serializer.is_valid():
            serializer.save()

            itemId = serializer.instance.id
            url = getItemDetailUrl(itemId)

            if len(request.FILES.keys()) == 0:
                return Response({"result":"success", "detail":url })

            #if len(request.FILES.keys()) > 0:
            print("TRUEが🐸")
            #item_objに画像データを追加修正する。
            try:
                serializer.instance.image1 = imagesData[SerializerContextKey.IMAGE1]
                #item_obj.image1 = imagesData[SerializerContextKey.IMAGE1]
            except:
                pass
            try:
                serializer.instance.image2 = imagesData[SerializerContextKey.IMAGE2]
                #item_obj.image2 = imagesData[SerializerContextKey.IMAGE2]
            except:
                pass
            try:
                serializer.instance.image3 = imagesData[SerializerContextKey.IMAGE3]
                #item_obj.image3 = imagesData[SerializerContextKey.IMAGE3]
            except:
                pass

            serializer.instance.save()
            return Response({"result":"success", "detail":url })

        return Response({"result":"success", "detail":url })

#mypages/views.MyItemListViewに対応
class MyItemListSerializerAPIView(APIView):
    """
    endpoint: api/mylist/
    name:
    """

    def get(self, request, *args, **kwargs):
        serializerContext = {}
        #ユーザー認証されていないときは、ログインページにつなぐ
        if request.auth == None:
            return Response({"result":"fail"})
            # Android端末では"ログインして下さい"を表示させる。

        #自分が作成した記事を表示する
        item_objects = Item.objects.filter(user=request.user).order_by("-created_at")
        if item_objects.count() > 0:

            item_objects_serializer = ItemSerializer(item_objects, many=True)

            print("maji??")
            #print(item_objects_serializer.data)
            return Response({"itemSerializer":item_objects_serializer.data})
        else:
            print("koko??")
            return Response({"itemSerializer":"jeje"})

class ItemFavoriteAPIView(APIView):

    def patch(self, request, *args, **kwargs):
        """
        endpoint: 'api/item/<int:pk>/favorite/'
        name: 'api:item_favorite'

        #機能：Item.favorite_users(ManyToManyField)にFavoriteオブジェクトを追加する。または削除する。
        機能：Item.favorite_users(ManyToManyField)にUserオブジェクトを追加する。または削除する。

        補足:
        このViewを通るのはすべて認証ユーザーである。
        未認証ユーザーがFavボタンを押しても、端末に”ログインしてください”と表示されるのみで通信が行われないから。

        """

        fav_status = None
        users   = []

        token       = getTokenFromHeader(self)
        requestUser = getUserByToken(token)
        itemObjId = self.kwargs["pk"]
        item_obj  = Item.objects.get(id=itemObjId)

        #item_obj.favorite_users.all()を使ってFavoriteオブジェクトに結びついたUserオブジェクトをリスト化する
        for user_obj in item_obj.favorite_users.all():
            users.append(user_obj)
            #if user_obj == requestUser:
            #    fav_status = "Favを既に押している"

        if requestUser in users:
            item_obj.favorite_users.remove(requestUser)

        elif requestUser not in users:
            item_obj.favorite_users.add(requestUser)

        return Response({"result":"success"})

class ItemCreateAPIViewMulti(APIView):

    """
    Itemオブジェクトを生成するために、
    user_obj, title, description, category_obj, adm0, adm1, adm2, image1, image2, image3を取得する。
    段階に分けて
    ① user_obj,
    ② category_obj,
    ③ title, description, adm0, adm1, adm2,
    ④ image1, image2, image3
    でItemオブジェクトを生成する。

    """

    authentication_classes = (TokenAuthentication,)
    permission_classes = (permissions.IsAuthenticated,)
    parser_classes = (parsers.MultiPartParser,)

    def post(self, request, *args, **kwargs):

        #① user_objを取得
        token = getTokenFromHeader(self)
        user_obj = getUserByToken(token)
        #print(token)

        #② category_objを取得
        #print("REQUEST_DATA")
        #print(request.data)
        strJsonData = request.data["jsonData"]
        jsonData = json.loads(strJsonData)
        #print(jsonData)

        categoryNumber = jsonData.pop("category")["number"]
        #print("CATEGORY_NAME" + categoryNumber)
        category_obj = Category.objects.get(number=categoryNumber)
        jsonData["category"] = category_obj.id

        #③ title, description, adm0, adm1, adm2を取得
        #serializer.is_valid()がTrueのとき各要素を取得する

        imagesData = request.FILES
        #print("IMAGES_DATA    :   ", imagesData)

        serializer = ItemSerializer(data=jsonData)

        if serializer.is_valid():
            print("SERIALIZER.IS_VALID : TRUE")

            item_obj = Item.objects.create(
                title = jsonData["title"],
                user = user_obj,
                description = jsonData["description"],
                price = jsonData["price"],
                category = category_obj,
                adm0 = jsonData["adm0"],
                adm1 = jsonData["adm1"],
                adm2 = serializer.validated_data["adm2"]
                )

            try:
                point = jsonData["point"]
                item_obj.point = point
                item_obj.save()
            except:
                pass
            try:
                radius = jsonData["radius"]
                item_obj.radius = radius
                item_obj.save()
            except:
                pass

            url = getItemDetailUrl(item_obj.id)

            #print("ItemObject生成成功")

            if len(request.FILES.keys()) > 0:
                print("TRUEが🐸")
                #item_objに画像データを追加修正する。
                try:
                    item_obj.image1 = imagesData[SerializerContextKey.IMAGE1]
                except:
                    pass
                try:
                    item_obj.image2 = imagesData[SerializerContextKey.IMAGE2]
                except:
                    pass
                try:
                    item_obj.image3 = imagesData[SerializerContextKey.IMAGE3]
                except:
                    pass

                item_obj.save()
                return Response({"result":"success", "detail":url })

            return Response({"result":"success", "detail":url})

        elif serializer.is_valid() == False:
            print("SERIALIZER.IS_VALID : False")
            for ele in serializer:
                print(ele)

            return  Response({"result":"fail"})

'''
# items/views.py ItemCreateView(View)に対応
class ItemCreateAPIView(APIView):

    """
    endpoint: 'api/item_create/'
    name: -

    """

    authentication_classes = (TokenAuthentication,)
    permission_classes = (permissions.IsAuthenticated,)

    #parser_classes = (parsers.MultiPartParser,)

    def post(self, request, *args, **kwargs):

        print("REQUEST.DATA   :   ")
        print(request.data)

        print(type(request.data))

        token = getTokenFromHeader(self)
        user_obj = getUserByToken(token)

        data = request.data
        categoryName = data.pop("category")["name"]
        category_obj = Category.objects.get(name=categoryName)

        serializer = ItemSerializer(data=request.data)
        if serializer.is_valid():
            print("SERIALIZER.IS_VALID : TRUE")
            #serializer.validated_data["user"] = user_obj

            item_obj = Item.objects.create(
                title = request.data["title"],
                user = user_obj,
                description = request.data["description"],
                category = category_obj,
                adm0 = request.data["adm0"],
                adm1 = request.data["adm1"],
                #adm2 = request.data["adm2"]
                adm2 = serializer.validated_data["adm2"]
                )

            print("ItemObject生成成功")
            #item_obj.user = user_obj
            #item_obj.category = category_obj

            return Response({"result":"success"})

        elif serializer.is_valid() == False:
            print("SERIALIZER.IS_VALID : False")
            #print(serializer.error_messages)

            #print(serializer.user.error_messages)
            for ele in serializer:
                print(ele)

            #print(serializer.errors())

            return  Response({"result":"fail"})

'''


import numpy as np
import cv2
import matplotlib
matplotlib.use('TkAgg') #backend (alternatives available)
import matplotlib.pyplot as plt
img= cv2.imread('/Users/tusharnema/Desktop/dog.jpg',cv2.IMREAD_COLOR) #for reading the image

cv2.line(img,(0,0),(150,150),(255,255,255),15) #for drawing a line in the image

cv2.rectangle(img,(15,25),(150,150),(0,255,0),5) #for drawing a rectangle in the image

cv2.circle(img,(350,250),(150),(0,255,255),15) #for drawing a circle in the image

pts=np.array([[10,5],[20,30],[80,65],[400,22],[350,250]],np.int32)
#pts=pts.reshape(-1,1,2)
cv2.polylines(img,[pts],True,(0,123,255),12) #for drawing a polygon in the image

font=cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'Yo doggy',(122,321),font,4,(21,33,255),2,cv2.LINE_AA)

imS = cv2.resize(img, (460, 340)) #for resizing the window

cv2.imshow('image',imS)
cv2.waitKey(0)
cv2.destroyAllWindows()


import json
from random import randrange

MISSING_VALUE_PERCENTAGE = 8
GROUND_TRUTH_PERCENTAGE = 10
BLOCK_SIZE = 10

delta = 0.0
masks = 1
eval_masks = 0
data = {}
data['forward'] = []
firstIteration = True
block = 0

c_m = 0
c_em = 0
c_b = 0

file = open("chlorine_normal_first_column.txt", "r")

for v in file:
    value = float(v)
    eval = float(v)
    rand_m = randrange(100//MISSING_VALUE_PERCENTAGE)
    rand_g = randrange(100//GROUND_TRUTH_PERCENTAGE)

    if rand_m == 0 and block == 0:
        block = BLOCK_SIZE
        c_b+=1

    if block > 0:
        masks = 0
        eval_masks = 0
        value = 0.0
        eval = 0.0
        if not firstIteration:
            delta = delta + 1.0
        block = block - 1
    else:
        if rand_g == 0:
            masks = 0
            eval_masks = 1
            value = 0.0
            if not firstIteration:
                delta = delta + 1.0
        else:
            masks = 1
            eval_masks = 0

    data['forward'].append({
        'evals': [eval],
        'deltas': [delta],
        'forwards': [0.0],
        'masks': [masks],
        'values': [value],
        'eval_masks': [eval_masks],
    })

    firstIteration = False

    if masks == 0:
        c_m+=1

    if eval_masks == 1:
        c_em+=1

data['backward'] = []

firstIteration = True
delta = 0.0
for e in reversed(data['forward']):
    newDict = e.copy()
    if not firstIteration and newDict['masks'][0] == 0:
        delta = delta - 1.0
    newDict['deltas'] = [delta]
    data['backward'].append(newDict)

    firstIteration = False

data['label'] = 0

with open('json_univariate_missing_blocks', 'w') as outfile:
    json.dump(data, outfile)

print('finished')
print(c_m)
print(c_em)
print(c_b)


#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
pyqt_sw10_TicTacToe.py

TicTacToe-Spiel mit QPushButton
erweitert durch Vererbung um
reihe, spalte

Author: Olaf Just
Website: robotik.bocholt@w-hs.de
Last edited: 20.11.2018
"""

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QWidget, QPushButton, QApplication, QLabel)

class myTicTacButton(QPushButton):
    # Konstruktor
    # benötigt Angabe von Reihe Spalte
    def __init__(self, r, s, parent=None):  #####<==== kein PROBLEM mehr
        super(myTicTacButton,self).__init__(parent)
        #QPushButton.__init__(self,'Text')

        # setzen der zusaetzlichen Attribute
        self._reihe = r
        self._spalte = s
        # setzen der Attribute der Elternklasse QPushButton
        self.setText(' ')
        self.resize(50,50)
        self.move((r+1)*50, (s+1)*50)

    # zusaetzliche Fkt von myTicTacButton
    def KlickSlot(self):
        clkSender = self.sender()    #Sender holen
        print(clkSender)
        clkReihe  = clkSender._reihe #Werte vom Sender
        clkSpalte = clkSender._spalte
        print('geklickt auf ' + str(clkReihe) + ' ' + str(clkSpalte))
        if clkSender.text()==' ':
            if ex.XistDran==True:
                clkSender.setText('X')
                ex.XistDran = False
                ex.lblStatus.setText('O ist dran')
            else:
                clkSender.setText('O')
                ex.XistDran = True
                ex.lblStatus.setText('X ist dran')

        #Aufruf der Funktion der Hauptklasse ex
        ex.checkGameOver()
        if ex.gameState==3:
            ex.lblStatus.setText('Spiel zu Ende - Alles Belegt')
        if ex.gameState==1:
            ex.lblStatus.setText('X hat gewonnen')
        if ex.gameState==2:
            ex.lblStatus.setText('O hat gewonnen')

class Game(QWidget):

    # List
    gameState = 0  # 0=noch was frei, 1 = X,  2=O  hat gewonnen, 3=alles belegt

    def __init__(self): #Konstrukor
        #Konstruktor der Elternklasse aufrufen
        super(Game, self).__init__()
        self.initUI()

    def initUI(self):

        self.XistDran = True
        # Instanziierung der Widgets
        # Tupel fuer Reihen und Spalten Indizierung
        rangeReihenIndex  = (0,1,2)
        rangeSpaltenIndex = (0,1,2)
        self.matrix = [[myTicTacButton(r,s,self) for s in range(3)] for r in range(3)]

        ##        self.btn = QPushButton('Text',self)# ====> funkt nur mit self
        #self.btn = myTicTacButton(0,0,self)
        #(0,0,self)===> TypeError: __init__() takes exactly 3 arguments (4 given)
        #self is passed implicitly when you call an instance method on an instance
        # => Paramter parent einfügen

        for reihe in rangeReihenIndex:
          for spalte in rangeSpaltenIndex:
            #Signal und Slot verbinden
            self.matrix[reihe][spalte].clicked.connect(self.matrix[reihe][spalte].KlickSlot)

        self.lblStatus = QLabel('X ist dran      ',self)
        self.lblStatus.setGeometry(60, 350, 200, 50)
        # self.lblStatus.move(60, 350)

        #Fenster Konfigurieren
        self.setGeometry(20, 20, 400, 400)
        self.setWindowTitle('Tic Tac Toe')
        self.show()

    def checkGameOver(self):
        #Noch ein Platz unbelegt?
        allesBelegt = False
#        for s in range(3):
#            for r in range(3):
#                if self.matrix[r][s].text==' ':
#                    allesBelegt = False
#                    print(self.matrix[r][s].text)
        if allesBelegt == False:
            if self.matrix[0][0].text() =='X' and self.matrix[0][1].text()=='X' and self.matrix[0][2].text() =='X':
                self.gameState = 1
            if self.matrix[0][0].text() =='O' and self.matrix[0][1].text()=='O' and self.matrix[0][2].text() =='O':
                self.gameState = 2
        else:
            self.gameState = 3 #alles belegt, aber keiner gewonnen

        return allesBelegt

if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Game()
    sys.exit(app.exec_())


from __future__ import print_function
import sys
from . import getmac

def main():
    try:
        import argparse
    except ImportError:
        print("You must install argparse on Python 2.6 and below ('pip install argparse')")
        sys.exit(1)

    description = 'Get the MAC address of system network ' \
                  'interfaces or remote hosts on the LAN'
    parser = argparse.ArgumentParser('getmac', description=description)
    parser.add_argument('--version', action='version',
                        version='getmac %s' % getmac.__version__)
    parser.add_argument('-d', '--debug', action='count',
                        help='Enable debugging output. Add characters to '
                             'increase verbosity of output, e.g. \'-dd\'.')
    parser.add_argument('--no-network-requests', action='store_true',
                        help='Do not send a UDP packet to refresh the ARP table')

    group = parser.add_mutually_exclusive_group(required=False)
    group.add_argument('-i', '--interface', type=str, default=None,
                       help='Name of a network interface on the system')
    group.add_argument('-4', '--ip', type=str, default=None,
                       help='IPv4 address of a remote host')
    group.add_argument('-6', '--ip6', type=str, default=None,
                       help='IPv6 address of a remote host')
    group.add_argument('-n', '--hostname', type=str, default=None,
                       help='Hostname of a remote host')

    args = parser.parse_args()

    if args.debug:
        getmac.DEBUG = args.debug

    mac = getmac.get_mac_address(
        interface=args.interface, ip=args.ip,
        ip6=args.ip6, hostname=args.hostname,
        network_request=not args.no_network_requests)

    if mac is not None:
        print(mac)
        sys.exit(0)
    else:
        sys.exit(1)

if __name__ == '__main__':
    main()


import os, hashlib, base64, hmac
from flask import Flask, Response, request
from utils import PullRequest
from config import REQUIRED_LABELS_ALL, REQUIRED_LABELS_ANY, BANNED_LABELS, GITHUB_SECRET

app = Flask(__name__)

@app.route('/', methods=["POST"])
def main():
    event_json = request.get_json()
    signature = request.headers['X-Hub-Signature'].split('=')[1]
    REQUIRED_ALL = request.args.get('required_all')
    REQUIRED_ANY = request.args.get('required_any')
    BANNED = request.args.get('banned')

    if not webhook_signature_is_valid(GITHUB_SECRET, request.data, signature):
        return Response(
        'Could not verify your access level for that URL.\n',
        401)

    if event_warrants_label_check(event_json):
        pull_request = PullRequest(event_json)
        print("Checking labels for PR {}".format(pull_request.issue_url))
        if REQUIRED_ALL or REQUIRED_ANY:
            status_code = pull_request.compute_and_post_status(REQUIRED_ALL, REQUIRED_ANY, BANNED)
        else:
            status_code = pull_request.compute_and_post_status(REQUIRED_LABELS_ANY, REQUIRED_LABELS_ALL, BANNED_LABELS)
        return str(status_code)
    else:
        return 'No label check needed'

@app.route('/config', methods=["GET"])
def config():
    return """
    Any: {}

    All: {}

    Banned: {}

    """.format(REQUIRED_LABELS_ANY, REQUIRED_LABELS_ALL, BANNED_LABELS)

@app.route('/health', methods=["GET"])
def health():
    return 'OK'

def event_warrants_label_check(pr_event_json):
    try:
        return pr_event_json['action'] in ['opened', 'reopened', 'labeled', 'unlabeled', 'synchronize']
    except KeyError:
        return False

def webhook_signature_is_valid(secret, payload, supplied):
    signature = hmac.new(key=secret.encode(), msg=payload, digestmod=hashlib.sha1).hexdigest()
    return hmac.compare_digest(signature, supplied)

if __name__ == "__main__":
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)


import os

from flask import Flask

def create_app(test_config=None):
	app = Flask(__name__, instance_relative_config=True)
	app.config.from_mapping(
		SECRET_KEY='dev',
		DATABASE=os.path.join(app.instance_path, 'coach_scheduler.sqlite'),
	)

	if test_config is None:
		app.config.from_pyfile('config.py', silent=True)
	else:
		app.config.from_mapping(test_config)

	try:
		os.makedirs(app.instance_path)
	except OSError:
		pass

	from . import db
	db.init_app(app)

	from . import auth
	app.register_blueprint(auth.bp)

	from . import schedule
	app.register_blueprint(schedule.bp)
	app.add_url_rule('/', endpoint='index')

	return app


# -*- mode: python; coding: utf-8 -*-
# Copyright (c) 2019 Radio Astronomy Software Group
# Licensed under the 2-clause BSD License

"""Class for reading MWA correlator FITS files."""
import warnings
import itertools
import numpy as np
from astropy.io import fits
from astropy.time import Time
from astropy import constants as const
from pyuvdata.data import DATA_PATH

from .. import _corr_fits

from . import UVData
from .. import utils as uvutils

__all__ = ["input_output_mapping", "MWACorrFITS"]

def input_output_mapping():
    """Build a mapping dictionary from pfb input to output numbers."""
    # the polyphase filter bank maps inputs to outputs, which the MWA
    # correlator then records as the antenna indices.
    # the following is taken from mwa_build_lfiles/mwac_utils.c
    # inputs are mapped to outputs via pfb_mapper as follows
    # (from mwa_build_lfiles/antenna_mapping.h):
    # floor(index/4) + index%4 * 16 = input
    # for the first 64 outputs, pfb_mapper[output] = input
    return _corr_fits.input_output_mapping()

class MWACorrFITS(UVData):
    """
    UVData subclass for reading MWA correlator fits files.

    This class should not be interacted with directly; instead use the
    read_mwa_corr_fits method on the UVData class.
    """

    def correct_cable_length(self, cable_lens):
        """
        Apply a cable length correction to the data array.

        Parameters
        ----------
        cable_lens : list of strings
        A list of strings containing the cable lengths for each antenna.
        """
        # from MWA_Tools/CONV2UVFITS/convutils.h
        cable_len_diffs = _corr_fits.get_cable_len_diffs(
            self.Nblts, self.ant_1_array, self.ant_2_array, cable_lens
        )
        self.data_array *= np.exp(
            -1j
            * 2
            * np.pi
            * cable_len_diffs.reshape(self.Nblts, 1)
            / const.c.to("m/s").value
            * self.freq_array.reshape(1, self.Nfreqs)
        )[:, :, None]

    def flag_init(
        self,
        num_fine_chan,
        edge_width=80e3,
        start_flag=2.0,
        end_flag=0.0,
        flag_dc_offset=True,
    ):
        """
        Apply routine flagging to the MWA Correlator FITS file data.

        Includes options to flag the coarse channel edges, beginning and end
        of obs, as well as the center fine channel of each coarse channel.

        Parameters
        ----------
        edge_width: float
            The width to flag on the edge of each coarse channel, in hz. Set to
            0 for no edge flagging.
        start_flag: float
            The number of seconds to flag at the beginning of the observation.
            Set to 0 for no flagging.
        end_flag: floats
            The number of seconds to flag at the end of the observation. Set to
            0 for no flagging.
        flag_dc_offset: bool
            Set to True to flag the center fine channel of each coarse channel.

        Raises
        ------
        ValueError
            If edge_width is not an integer multiple of the channel_width of
            the data (0 also acceptable).
            If start_flag is not an integer multiple of the integration time
            (0 also acceptable).
            If end_flag is not an integer multiple of the integration time
            (0 also acceptable).

        """
        if (edge_width % self.channel_width) > 0:
            raise ValueError(
                "The edge_width must be an integer multiple of the "
                "channel_width of the data or zero."
            )
        if (start_flag % self.integration_time[0]) > 0:
            raise ValueError(
                "The start_flag must be an integer multiple of the "
                "integration_time of the data or zero."
            )
        if (end_flag % self.integration_time[0]) > 0:
            raise ValueError(
                "The end_flag must be an integer multiple of the "
                "integration_time of the data or zero."
            )

        num_ch_flag = int(edge_width / self.channel_width)
        num_start_flag = int(start_flag / self.integration_time[0])
        num_end_flag = int(end_flag / self.integration_time[0])

        if num_ch_flag > 0:
            edge_inds = []
            for ch_count in range(num_ch_flag):
                # count up from the left
                left_chans = list(range(ch_count, self.Nfreqs, num_fine_chan))
                # count down from the right
                right_chans = list(range(self.Nfreqs - 1 - ch_count, 0, -num_fine_chan))
                edge_inds = edge_inds + left_chans + right_chans

            self.flag_array[:, :, edge_inds, :] = True

        if flag_dc_offset:
            center_inds = list(range(num_fine_chan // 2, self.Nfreqs, num_fine_chan))

            self.flag_array[:, :, center_inds, :] = True

        if (num_start_flag > 0) or (num_end_flag > 0):
            shape = self.flag_array.shape
            # TODO: Spw axis to be collapsed in future release
            # Asserting this here because this is effectively a stripped down UVFITS
            # reader, and thus assuming that this should only support simple tables
            assert shape[1] == 1
            reshape = [self.Ntimes, self.Nbls, 1, self.Nfreqs, self.Npols]
            self.flag_array = np.reshape(self.flag_array, reshape)
            if num_start_flag > 0:
                self.flag_array[:num_start_flag, :, :, :, :] = True
            if num_end_flag > 0:
                self.flag_array[-num_end_flag:, :, :, :, :] = True
            self.flag_array = np.reshape(self.flag_array, shape)

    def read_mwa_corr_fits(
        self,
        filelist,
        use_cotter_flags=None,
        remove_dig_gains=True,
        remove_coarse_band=True,
        correct_cable_len=False,
        phase_to_pointing_center=False,
        propagate_coarse_flags=True,
        flag_init=True,
        edge_width=80e3,
        start_flag="goodtime",
        end_flag=0.0,
        flag_dc_offset=True,
        background_lsts=True,
        read_data=True,
        data_array_dtype=np.complex64,
        nsample_array_dtype=np.float32,
        run_check=True,
        check_extra=True,
        run_check_acceptability=True,
        strict_uvw_antpos_check=False,
    ):
        """
        Read in MWA correlator gpu box files.

        The default settings remove some of the instrumental effects in the bandpass
        by dividing out the digital gains and the coarse band shape.
        If the desired output is raw correlator data, set remove_dig_gains=False,
        remove_coarse_band=False, correct_cable_len=False, and
        phase_to_pointing_center=False.

        Parameters
        ----------
        filelist : list of str
            The list of MWA correlator files to read from. Must include at
            least one fits file and only one metafits file per data set.
            Can also be a list of lists to read multiple data sets.
        axis : str
            Axis to concatenate files along. This enables fast concatenation
            along the specified axis without the normal checking that all other
            metadata agrees. This method does not guarantee correct resulting
            objects. Please see the docstring for fast_concat for details.
            Allowed values are: 'blt', 'freq', 'polarization'. Only used if
            multiple files are passed.
        use_cotter_flags : bool
            Option to use cotter output mwaf flag files. Defaults to true if
            cotter flag files are submitted.
        remove_dig_gains : bool
            Option to divide out digital gains.
        remove_coarse_band : bool
            Option to divide out coarse band shape.
        correct_cable_len : bool
            Option to apply a cable delay correction.
        phase_to_pointing_center : bool
            Option to phase to the observation pointing center.
        propagate_coarse_flags : bool
            Option to propagate flags for missing coarse channel integrations
            across frequency.
        flag_init: bool
            Set to True in order to do routine flagging of coarse channel edges,
            start or end integrations, or the center fine channel of each coarse
            channel. See associated keywords.
        edge_width: float
            Only used if flag_init is True. The width to flag on the edge of
            each coarse channel, in hz. Errors if not equal to integer multiple
            of channel_width. Set to 0 for no edge flagging.
        start_flag: float or str
            Only used if flag_init is True. The number of seconds to flag at the
            beginning of the observation. Set to 0 for no flagging. Default is
            'goodtime', which uses information in the metafits file to determine
            the length of time that should be flagged. Errors if input is not a
            float or 'goodtime'. Errors if float input is not equal to an
            integer multiple of the integration time.
        end_flag: floats
            Only used if flag_init is True. The number of seconds to flag at the
            end of the observation. Set to 0 for no flagging. Errors if not
            equal to an integer multiple of the integration time.
        flag_dc_offset: bool
            Only used if flag_init is True. Set to True to flag the center fine
            channel of each coarse channel.
        background_lsts : bool
            When set to True, the lst_array is calculated in a background thread.
        read_data : bool
            Read in the visibility, nsample and flag data. If set to False, only
            the metadata will be read in. Setting read_data to False results in
            a metadata only object.
        data_array_dtype : numpy dtype
            Datatype to store the output data_array as. Must be either
            np.complex64 (single-precision real and imaginary) or np.complex128
            (double-precision real and imaginary).
        nsample_array_dtype : numpy dtype
            Datatype to store the output nsample_array as. Must be either
            np.float64 (double-precision), np.float32 (single-precision), or
            np.float16 (half-precision). Half-precision is only recommended for
            cases where no sampling or averaging of baselines will occur,
            because round-off errors can be quite large (~1e-3).
        run_check : bool
            Option to check for the existence and proper shapes of parameters
            after after reading in the file (the default is True,
            meaning the check will be run).
        check_extra : bool
            Option to check optional parameters as well as required ones (the
            default is True, meaning the optional parameters will be checked).
        run_check_acceptability : bool
            Option to check acceptable range of the values of parameters after
            reading in the file (the default is True, meaning the acceptable
            range check will be done).
        strict_uvw_antpos_check : bool
            Option to raise an error rather than a warning if the check that
            uvws match antenna positions does not pass.

        Raises
        ------
        ValueError
            If required files are missing or multiple files metafits files are
            included in filelist.
            If files from different observations are included in filelist.
            If files in fileslist have different fine channel widths
            If file types other than fits, metafits, and mwaf files are included
            in filelist.

        """
        metafits_file = None
        ppds_file = None
        obs_id = None
        file_dict = {}
        start_time = 0.0
        end_time = 0.0
        included_file_nums = []
        included_flag_nums = []
        cotter_warning = False
        num_fine_chans = 0

        # do datatype checks
        if data_array_dtype not in (np.complex64, np.complex128):
            raise ValueError("data_array_dtype must be np.complex64 or np.complex128")
        if nsample_array_dtype not in (np.float64, np.float32, np.float16):
            raise ValueError(
                "nsample_array_dtype must be one of: np.float64, np.float32, np.float16"
            )
        # do start_flag check
        if not isinstance(start_flag, (int, float)):
            if start_flag != "goodtime":
                raise ValueError("start_flag must be int or float or 'goodtime'")

        # iterate through files and organize
        # create a list of included coarse channels
        # find the first and last times that have data
        for file in filelist:
            if file.lower().endswith(".metafits"):
                # force only one metafits file
                if metafits_file is not None:
                    raise ValueError("multiple metafits files in filelist")
                metafits_file = file
            elif file.lower().endswith(".fits"):
                # check if ppds file
                try:
                    fits.getheader(file, extname="ppds")
                    ppds_file = file
                except Exception:
                    # check obsid
                    head0 = fits.getheader(file, 0)
                    if obs_id is None:
                        obs_id = head0["OBSID"]
                    else:
                        if head0["OBSID"] != obs_id:
                            raise ValueError(
                                "files from different observations submitted "
                                "in same list"
                            )
                    # check headers for first and last times containing data
                    headstart = fits.getheader(file, 1)
                    headfin = fits.getheader(file, -1)
                    first_time = headstart["TIME"] + headstart["MILLITIM"] / 1000.0
                    last_time = headfin["TIME"] + headfin["MILLITIM"] / 1000.0
                    if start_time == 0.0:
                        start_time = first_time
                    # check that files with a timing offset can be aligned
                    elif np.abs(start_time - first_time) % headstart["INTTIME"] != 0.0:
                        raise ValueError(
                            "coarse channel start times are misaligned by an amount that is not \
                                an integer multiple of the integration time"
                        )
                    elif start_time > first_time:
                        start_time = first_time
                    if end_time < last_time:
                        end_time = last_time
                    # get number of fine channels
                    if num_fine_chans == 0:
                        num_fine_chans = headstart["NAXIS2"]
                    elif num_fine_chans != headstart["NAXIS2"]:
                        raise ValueError(
                            "files submitted have different fine channel widths"
                        )
                    # get the file number from the file name;
                    # this will later be mapped to a coarse channel
                    file_num = int(file.split("_")[-2][-2:])
                    if file_num not in included_file_nums:
                        included_file_nums.append(file_num)
                    # organize files
                    if "data" not in file_dict.keys():
                        file_dict["data"] = [file]
                    else:
                        file_dict["data"].append(file)
            # look for flag files
            elif file.lower().endswith(".mwaf"):
                if use_cotter_flags is None:
                    use_cotter_flags = True
                flag_num = int(file.split("_")[-1][0:2])
                included_flag_nums.append(flag_num)
                if use_cotter_flags is False and cotter_warning is False:
                    warnings.warn("mwaf files submitted with use_cotter_flags=False")
                    cotter_warning = True
                elif "flags" not in file_dict.keys():
                    file_dict["flags"] = [file]
                else:
                    file_dict["flags"].append(file)
            else:
                raise ValueError("only fits, metafits, and mwaf files supported")

        # checks:
        if metafits_file is None and ppds_file is None:
            raise ValueError("no metafits file submitted")
        elif metafits_file is None:
            metafits_file = ppds_file
        elif ppds_file is not None:
            ppds = fits.getheader(ppds_file, 0)
            meta = fits.getheader(metafits_file, 0)
            for key in ppds.keys():
                if key not in meta.keys():
                    self.extra_keywords[key] = ppds[key]
        if "data" not in file_dict.keys():
            raise ValueError("no data files submitted")
        if "flags" not in file_dict.keys() and use_cotter_flags:
            raise ValueError(
                "no flag files submitted. Rerun with flag files \
                             or use_cotter_flags=False"
            )

        # first set parameters that are always true
        self.Nspws = 1
        self.spw_array = np.array([0])
        self.phase_type = "drift"
        self.vis_units = "uncalib"
        self.Npols = 4
        self.xorientation = "east"

        # get information from metafits file
        with fits.open(metafits_file, memmap=True) as meta:
            meta_hdr = meta[0].header

            # get a list of coarse channels
            coarse_chans = meta_hdr["CHANNELS"].split(",")
            coarse_chans = np.array(sorted(int(i) for i in coarse_chans))

            # integration time in seconds
            int_time = meta_hdr["INTTIME"]

            # pointing center in degrees
            ra_deg = meta_hdr["RA"]
            dec_deg = meta_hdr["DEC"]
            ra_rad = np.pi * ra_deg / 180
            dec_rad = np.pi * dec_deg / 180

            # set start_flag with goodtime
            if flag_init and start_flag == "goodtime":
                # ppds file does not contain this key
                try:
                    if meta_hdr["GOODTIME"] > start_time:
                        start_flag = meta_hdr["GOODTIME"] - start_time
                    else:
                        start_flag = 0.0
                except KeyError:
                    raise ValueError(
                        "To use start_flag='goodtime', a .metafits file must \
                            be submitted"
                    )

            # get parameters from header
            # this assumes no averaging by this code so will need to be updated
            self.channel_width = float(meta_hdr.pop("FINECHAN") * 1000)
            if "HISTORY" in meta_hdr:
                self.history = str(meta_hdr["HISTORY"])
                meta_hdr.remove("HISTORY", remove_all=True)
            else:
                self.history = ""
            if not uvutils._check_history_version(
                self.history, self.pyuvdata_version_str
            ):
                self.history += self.pyuvdata_version_str
            self.instrument = meta_hdr["TELESCOP"]
            self.telescope_name = meta_hdr.pop("TELESCOP")
            self.object_name = meta_hdr.pop("FILENAME")

            # get rid of the instrument keyword so it doesn't get put back in
            meta_hdr.remove("INSTRUME")
            # get rid of keywords that uvfits.py gets rid of
            bad_keys = ["SIMPLE", "EXTEND", "BITPIX", "NAXIS", "DATE-OBS"]
            for key in bad_keys:
                meta_hdr.remove(key, remove_all=True)
            # store remaining keys in extra keywords
            for key in meta_hdr:
                if key == "COMMENT":
                    self.extra_keywords[key] = str(meta_hdr.get(key))
                elif key != "":
                    self.extra_keywords[key] = meta_hdr.get(key)
            # get antenna data from metafits file table
            meta_tbl = meta[1].data

            # because of polarization, each antenna # is listed twice
            antenna_numbers = meta_tbl["Antenna"][1::2]
            antenna_names = meta_tbl["TileName"][1::2]
            antenna_flags = meta_tbl["Flag"][1::2]
            cable_lens = np.asarray(meta_tbl["Length"][1::2]).astype(np.str_)
            dig_gains = meta_tbl["Gains"][1::2, :].astype(np.float64)

            # get antenna postions in enu coordinates
            antenna_positions = np.zeros((len(antenna_numbers), 3))
            antenna_positions[:, 0] = meta_tbl["East"][1::2]
            antenna_positions[:, 1] = meta_tbl["North"][1::2]
            antenna_positions[:, 2] = meta_tbl["Height"][1::2]

        # reorder antenna parameters from metafits ordering
        reordered_inds = antenna_numbers.argsort()
        self.antenna_numbers = antenna_numbers[reordered_inds]
        self.antenna_names = list(antenna_names[reordered_inds])
        antenna_positions = antenna_positions[reordered_inds, :]
        antenna_flags = antenna_flags[reordered_inds]
        cable_lens = cable_lens[reordered_inds]
        dig_gains = dig_gains[reordered_inds, :]

        # find flagged antenna
        flagged_ants = self.antenna_numbers[np.where(antenna_flags == 1)]

        # set parameters from other parameters
        self.Nants_data = len(self.antenna_numbers)
        self.Nants_telescope = len(self.antenna_numbers)
        self.Nbls = int(
            len(self.antenna_numbers) * (len(self.antenna_numbers) + 1) / 2.0
        )

        # get telescope parameters
        self.set_telescope_params()

        # build time array of centers
        time_array = np.arange(
            start_time + int_time / 2.0, end_time + int_time / 2.0 + int_time, int_time
        )

        # convert to time to jd floats
        float_time_array = Time(time_array, format="unix", scale="utc").jd.astype(float)
        # build into time array
        self.time_array = np.repeat(float_time_array, self.Nbls)

        self.Ntimes = len(time_array)

        self.Nblts = int(self.Nbls * self.Ntimes)

        # convert times to lst
        proc = self.set_lsts_from_time_array(background=background_lsts)

        self.integration_time = np.full((self.Nblts), int_time)

        # convert antenna positions from enu to ecef
        # antenna positions are "relative to
        # the centre of the array in local topocentric \"east\", \"north\",
        # \"height\". Units are meters."
        antenna_positions_ecef = uvutils.ECEF_from_ENU(
            antenna_positions, *self.telescope_location_lat_lon_alt
        )
        # make antenna positions relative to telescope location
        self.antenna_positions = antenna_positions_ecef - self.telescope_location

        # make initial antenna arrays, where ant_1 <= ant_2
        # itertools.combinations_with_replacement returns
        # all pairs in the range 0...Nants_telescope
        # including pairs with the same number (e.g. (0,0) auto-correlation).
        # this is a little faster than having nested for-loops moving over the
        # upper triangle of antenna-pair combinations matrix.
        ant_1_array, ant_2_array = np.transpose(
            list(
                itertools.combinations_with_replacement(
                    np.arange(self.Nants_telescope), 2
                )
            )
        )

        self.ant_1_array = np.tile(np.array(ant_1_array), self.Ntimes)
        self.ant_2_array = np.tile(np.array(ant_2_array), self.Ntimes)

        self.baseline_array = self.antnums_to_baseline(
            self.ant_1_array, self.ant_2_array
        )

        # create self.uvw_array
        self.set_uvws_from_antenna_positions(allow_phasing=False)

        # coarse channel mapping:
        # channels in group 0-128 go in order; channels in group 129-155 go in
        # reverse order
        # that is, if the lowest channel is 127, it will be assigned to the
        # first file
        # channel 128 will be assigned to the second file
        # then the highest channel will be assigned to the third file
        # and the next hightest channel assigned to the fourth file, and so on
        count = np.count_nonzero(coarse_chans <= 128)
        # map all file numbers to coarse channel numbers
        file_nums_to_coarse = {
            i + 1: coarse_chans[i]
            if i < count
            else coarse_chans[(len(coarse_chans) + count - i - 1)]
            for i in range(len(coarse_chans))
        }
        # map included coarse channels to file numbers
        coarse_to_incl_files = {}
        for i in included_file_nums:
            coarse_to_incl_files[file_nums_to_coarse[i]] = i
        # sort included coarse channels
        included_coarse_chans = sorted(coarse_to_incl_files.keys())
        # map included file numbers to an index that orders them
        file_nums_to_index = {}
        for i in included_coarse_chans:
            file_nums_to_index[coarse_to_incl_files[i]] = included_coarse_chans.index(i)
        # check that coarse channels are contiguous.
        chans = np.array(included_coarse_chans)
        for i in np.diff(chans):
            if i != 1:
                warnings.warn("coarse channels are not contiguous for this observation")
                break

        # warn user if not all coarse channels are included
        if len(included_coarse_chans) != len(coarse_chans):
            warnings.warn("some coarse channel files were not submitted")

        # build frequency array
        self.Nfreqs = len(included_coarse_chans) * num_fine_chans
        # TODO: Spw axis to be collapsed in future release
        self.freq_array = np.zeros((1, self.Nfreqs))

        # each coarse channel is split into 128 fine channels of width 10 kHz.
        # The first fine channel for each coarse channel is centered on the
        # lower bound frequency of that channel and its center frequency is
        # computed as fine_center = coarse_channel_number * 1280-640 (kHz).
        # If the fine channels have been averaged (added) by some factor, the
        # center of the resulting channel is found by averaging the centers of
        # the first and last fine channels it is made up of.
        # That is, avg_fine_center=(lowest_fine_center+highest_fine_center)/2
        # where highest_fine_center=lowest_fine_center+(avg_factor-1)*10 kHz
        # so avg_fine_center=(lowest_fine_center+lowest_fine_center+(avg_factor-1)*10)/2
        #                   =lowest_fine_center+((avg_factor-1)*10)/2
        #                   =lowest_fine_center+offset
        # Calculate offset=((avg_factor-1)*10)/2 to build the frequency array
        avg_factor = self.channel_width / 10000
        width = self.channel_width / 1000
        offset = (avg_factor - 1) * 10 / 2.0

        for i in range(len(included_coarse_chans)):
            # get the lowest fine freq of the coarse channel (kHz)
            lower_fine_freq = included_coarse_chans[i] * 1280 - 640
            # find the center of the lowest averaged channel
            first_center = lower_fine_freq + offset
            # add the channel centers for this coarse channel into
            # the frequency array (converting from kHz to Hz)
            self.freq_array[
                0, int(i * num_fine_chans) : int((i + 1) * num_fine_chans)
            ] = (
                np.arange(first_center, first_center + num_fine_chans * width, width)
                * 1000
            )
        # polarizations are ordered yy, yx, xy, xx
        self.polarization_array = np.array([-6, -8, -7, -5])

        if read_data:
            # read data into an array with dimensions (time, uv, baselines*pols)
            self.data_array = np.zeros(
                (self.Ntimes, self.Nfreqs, self.Nbls * self.Npols),
                dtype=data_array_dtype,
            )
            self.nsample_array = np.zeros(
                (self.Ntimes, self.Nbls, self.Nfreqs, self.Npols),
                dtype=nsample_array_dtype,
            )
            self.flag_array = np.full(
                (self.Ntimes, self.Nbls, len(included_coarse_chans), self.Npols), True
            )

            # read data files
            for file in file_dict["data"]:
                # get the file number from the file name
                file_num = int(file.split("_")[-2][-2:])
                # map file number to frequency index
                freq_ind = file_nums_to_index[file_num] * num_fine_chans
                with fits.open(
                    file, memmap=False, do_not_scale_image_data=False
                ) as hdu_list:
                    # count number of times
                    end_list = len(hdu_list)
                    for i in range(1, end_list):
                        time = (
                            hdu_list[i].header["TIME"]
                            + hdu_list[i].header["MILLITIM"] / 1000.0
                            + int_time / 2.0
                        )
                        time_ind = np.where(time_array == time)[0][0]
                        # dump data into matrix
                        # and take data from real to complex numbers
                        self.data_array[
                            time_ind, freq_ind : freq_ind + num_fine_chans, :
                        ] = (hdu_list[i].data[:, 0::2] + 1j * hdu_list[i].data[:, 1::2])
                        self.nsample_array[
                            time_ind, :, freq_ind : freq_ind + num_fine_chans, :
                        ] = 1.0
                        self.flag_array[
                            time_ind, :, file_nums_to_index[file_num], :
                        ] = False

            # build mapper from antenna numbers and polarizations to pfb inputs
            corr_ants_to_pfb_inputs = {}
            for i in range(len(antenna_numbers)):
                for p in range(2):
                    corr_ants_to_pfb_inputs[(antenna_numbers[i], p)] = 2 * i + p

            # for mapping, start with a pair of antennas/polarizations
            # this is the pair we want to find the data for
            # map the pair to the corresponding pfb input indices
            # map the pfb input indices to the pfb output indices
            # these are the indices for the data corresponding to the initial
            # antenna/pol pair
            # generate a mapping index array
            map_inds = np.zeros((self.Nbls * self.Npols), dtype=np.int32)
            # generate a conjugation array
            conj = np.full((self.Nbls * self.Npols), False, dtype=np.bool_)
            pfb_inputs_to_outputs = input_output_mapping()

            map_inds, conj = _corr_fits.generate_map(
                corr_ants_to_pfb_inputs, pfb_inputs_to_outputs, map_inds, conj,
            )

            # propagate coarse flags
            if propagate_coarse_flags:
                self.flag_array = np.any(self.flag_array, axis=2)
                self.flag_array = self.flag_array[:, :, np.newaxis, :]
                self.flag_array = np.repeat(self.flag_array, self.Nfreqs, axis=2)
            else:
                self.flag_array = np.repeat(self.flag_array, num_fine_chans, axis=2)

            # reorder data
            self.data_array = np.take(self.data_array, map_inds, axis=2)

            # conjugate data
            self.data_array[:, :, conj] = np.conj(self.data_array[:, :, conj])

            # reshape data
            self.data_array = self.data_array.reshape(
                (self.Ntimes, self.Nfreqs, self.Nbls, self.Npols)
            )

            self.data_array = np.swapaxes(self.data_array, 1, 2)

            # generage baseline flags for flagged ants
            bad_ant_inds = np.nonzero(
                np.logical_or(
                    np.in1d(ant_1_array, flagged_ants),
                    np.in1d(ant_2_array, flagged_ants),
                )
            )[0]
            self.flag_array[:, bad_ant_inds, :, :] = True

            # combine baseline and time axes
            self.data_array = self.data_array.reshape(
                (self.Nblts, self.Nfreqs, self.Npols)
            )
            self.flag_array = self.flag_array.reshape(
                (self.Nblts, self.Nfreqs, self.Npols)
            )
            self.nsample_array = self.nsample_array.reshape(
                (self.Nblts, self.Nfreqs, self.Npols)
            )

            # divide out digital gains
            if remove_dig_gains:
                # get gains for included coarse channels
                coarse_inds = np.where(np.isin(coarse_chans, included_coarse_chans))[0]
                # during commissioning a shift in the bit selection in the digital
                # receiver was implemented which effectively multiplies the data by
                # a factor of 64. To account for this, the digital gains are divided
                # by a factor of 64 here. For a more detailed explanation, see PR #908.
                dig_gains = dig_gains[:, coarse_inds] / 64
                dig_gains = np.repeat(dig_gains, num_fine_chans, axis=1)
                dig_gains1 = dig_gains[self.ant_1_array, :]
                dig_gains2 = dig_gains[self.ant_2_array, :]
                dig_gains1 = dig_gains1[:, :, np.newaxis]
                dig_gains2 = dig_gains2[:, :, np.newaxis]
                if self.data_array.dtype != np.complex128:
                    self.data_array = self.data_array.astype(np.complex128)
                self.data_array = self.data_array / (dig_gains1 * dig_gains2)
                if self.data_array.dtype != data_array_dtype:
                    self.data_array = self.data_array.astype(data_array_dtype)
            # divide out coarse band shape
            if remove_coarse_band:
                # get coarse band shape
                with open(
                    DATA_PATH + "/mwa_config_data/MWA_rev_cb_10khz_doubles.txt", "r"
                ) as f:
                    cb = f.read().splitlines()
                cb_array = np.array(cb).astype(np.float64)
                cb_array = cb_array.reshape(int(128 / avg_factor), int(avg_factor))
                cb_array = np.average(cb_array, axis=1)
                cb_array = cb_array[0:num_fine_chans]
                cb_array = np.tile(cb_array, len(included_coarse_chans))
                cb_array = cb_array[:, np.newaxis]
                if self.data_array.dtype != np.complex128:
                    self.data_array = self.data_array.astype(np.complex128)
                self.data_array = self.data_array / cb_array
                if self.data_array.dtype != data_array_dtype:
                    self.data_array = self.data_array.astype(data_array_dtype)

            # cable delay corrections
            if correct_cable_len:
                self.correct_cable_length(cable_lens)

            # add spectral window index
            self.data_array = self.data_array[:, np.newaxis, :, :]
            self.flag_array = self.flag_array[:, np.newaxis, :, :]
            self.nsample_array = self.nsample_array[:, np.newaxis, :, :]

            # because of an annoying discrepancy between file conventions, in order
            # to be consistent with the uvw vector direction, all the data must
            # be conjugated
            self.data_array = np.conj(self.data_array)

        # wait for LSTs if set in background
        if proc is not None:
            proc.join()

        if not self.metadata_only:
            # reorder polarizations
            # reorder pols calls check so must come after
            # lst thread is re-joined.
            self.reorder_pols()

        # phasing
        if phase_to_pointing_center:
            self.phase(ra_rad, dec_rad)

        if not self.metadata_only:
            if flag_init:
                self.flag_init(
                    num_fine_chans,
                    edge_width=edge_width,
                    start_flag=start_flag,
                    end_flag=end_flag,
                    flag_dc_offset=flag_dc_offset,
                )

            if use_cotter_flags:
                # throw an error if matching files not submitted
                if included_file_nums != included_flag_nums:
                    raise ValueError(
                        "flag file coarse bands do not match data file coarse bands"
                    )
                warnings.warn(
                    "coarse channel, start time, and end time flagging will default \
                        to the more aggressive of flag_init and AOFlagger"
                )
                for file in file_dict["flags"]:
                    flag_num = int(file.split("_")[-1][0:2])
                    # map file number to frequency index
                    freq_ind = file_nums_to_index[flag_num] * num_fine_chans
                    with fits.open(file) as aoflags:
                        flags = aoflags[1].data.field("FLAGS")
                    # some flag files are longer than data; crop the ends
                    flags = flags[: self.Nblts, :]
                    # some flag files are shorter than data; assume same end time
                    blt_ind = self.Nblts - len(flags)
                    flags = flags[:, np.newaxis, :, np.newaxis]
                    self.flag_array[
                        blt_ind:, :, freq_ind : freq_ind + num_fine_chans, :
                    ] = np.logical_or(
                        self.flag_array[
                            blt_ind:, :, freq_ind : freq_ind + num_fine_chans, :
                        ],
                        flags,
                    )

            # check if object is self-consistent
            # uvws are calcuated using pyuvdata, so turn off the check for speed.
            if run_check:
                self.check(
                    check_extra=check_extra,
                    run_check_acceptability=run_check_acceptability,
                    strict_uvw_antpos_check=strict_uvw_antpos_check,
                )


#!/usr/bin/env python
# coding: utf-8

# DECLARING REQUIRED PACKAGES
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
from scipy.constants import e
from IPython import display
from bokeh.plotting import figure, show
from bokeh.io import output_notebook
from bokeh.models import Label, LabelSet
#output_notebook()

# DEFINING A FUNCTION THAT SHOWS A 2D XY PLOT USING MATPLOTLIB
def xyPlot(particles,target_pos):
    plt.scatter(target_pos[:,0], target_pos[:,1], s=20, c='r', marker="o")
    for particle in particles:
        rgb = np.random.rand(3,)
        positions = particle.getPositions()
        plt.scatter(positions[:,0], positions[:,1], s=1, c=rgb, marker=".")

    plt.xlabel("X")
    plt.ylabel("Y")
    plt.title('Rutherford Scattering 2D representation')
    #plt.xlim([-0.2,0.2])
    #plt.ylim([0,0.1e-20])
    plt.show()

# DEFINING A FUNCTION THAT SHOWS A 2D XY PLOT USING BOKEH LIBRARY
def trajectoryPlot(particles, target_pos):
    p = figure(title="Rutherford Scattering 2D Representation", x_axis_label='X(m)', y_axis_label='Y(m)')
    p.circle_x(target_pos[:,0], target_pos[:,1], size=20, color="black", alpha=0.5)
    for particle in particles:
        positions = particle.getPositions()
        p.line(positions[:,0], positions[:,1], line_width=2, color=particle.getColor())
    citation = Label(x=395, y=225, x_units='screen', y_units='screen',
                 text='Gold Nucleus', render_mode='css',
                 background_fill_color='white', background_fill_alpha=1.0)
    p.add_layout(citation)
    show(p)

# DEFINING A FUNCTION THAT SHOWS A 2D XY PLOT FOR A UNIFORM DISTRIBUTION OF ENERGIES
def trajectoryPlot2(particles, target_pos):
    p = figure(title="Rutherford Scattering 2D Representation for uniform distribution of energies", x_axis_label='X(m)', y_axis_label='Y(m)')
    p.circle_x(target_pos[:,0], target_pos[:,1], size=20, color="black", alpha=0.5)
    for particle in particles:
        positions = particle.getPositions()
        energy = str(round(particle.getEnergy()/e,1))
        angle = str(round(particle.getScatteringAngle()*180/np.pi+180 ,1))
        p.line(positions[:,0], positions[:,1], line_width=2, color=particle.getColor(), legend_label = energy + ' eV • θ = ' + angle + '°')
    p.legend.location = "top_right"
    show(p)

# DEFINING A FUNCTION THAT GENERATES AN ANIMATION OF THE SCATTERING PROCESS
def animation(particles, target, steps):
    fig1 = plt.figure()
    positionTarget = target.getPositions()

    for i in range(steps):
        plt.title('Rutherford Scattering')
        plt.xlim([-0.02, 0.0025])
        plt.ylim([-0.01, 0.01])
        for particle in particles:
            positions = particle.getPositions()[i,:]
            plt.plot(positionTarget[0,0], positionTarget[0,1], 'o', color='black', markersize = 10)
            plt.plot(positions[0], positions[1], 'o', color=particle.getColorRGBA(), markersize = 3)
        display.clear_output(wait=True)
        display.display(plt.gcf())
        plt.clf()
    plt.clf()


# coding: utf-8
import cPickle
import os
import utils
import re
import numpy as np
import scipy
import scipy.spatial.distance
import scipy.stats

with open("genome_num_rep_info.txt",'rb') as f:
    genomeList = cPickle.load(f)
    genome2scaffold = cPickle.load(f)

read_dir = './genomes_num_represent/'
window = 1000# window = the width of slide window
k = 4# k = length of kmer

with open('win'+str(window)+'_'+str(k)+'mer','rb') as f:
    tnf = cPickle.load(f)
    label = cPickle.load(f)

genomelabel2index = [[] for i in xrange(len(genomeList))]# i th genome have indexes in list genomelabel2index[i]
for i in xrange(np.shape(label)[0]):
    genomelabel2index[label[i,0]].append(i)

def distance(v1,v2):
    return scipy.spatial.distance.euclidean(v1,v2)

'''
# calculate centroid vector in each genome
centroids = np.zeros((len(genomeList),np.power(4,k)),np.float32)
distance2centroids = [[] for i in xrange(len(genomeList))]
centroids_std = [[] for i in xrange(len(genomeList))]
centroids_mean = [[] for i in xrange(len(genomeList))]
for i in xrange(len(genomeList)):
    vectors = tnf[genomelabel2index[i],:]
    if len(vectors) == 0:
        vectors = np.zeros((1,np.power(4,k)),np.float32)
    centroids[i,:] = np.sum(vectors,axis=0)
    centroids[i,:] /= np.shape(vectors)[0]
    distance2centroids[i] = [distance(centroids[i,:],vectors[j,:]) for j in xrange(np.shape(vectors)[0])]
    centroids_std[i] = np.std(distance2centroids[i])
    centroids_mean[i] = np.mean(distance2centroids[i])

dist_btwn_centroids = np.zeros((len(genomeList),len(genomeList)),np.float32)
for i in xrange(1,len(genomeList)):
    for j in xrange(i):
        dist_btwn_centroids[i,j] = scipy.spatial.distance.euclidean(centroids[i,:],centroids[j,:])
        dist_btwn_centroids[j,i] = dist_btwn_centroids[i,j]

# extract scaffold vectors whose centroid distance to a certain centroid < std_var of that genome
'''
#1,3,5,7 is one scaffold genome
import random
ia = 5
ib = 6
alen = len(genomelabel2index[ia])
a = tnf[genomelabel2index[ia],:]
blen = len(genomelabel2index[ib])
b = tnf[genomelabel2index[ib],:]

sample_num = 100000
adist = [[] for i in xrange(sample_num)]
bdist = [[] for i in xrange(sample_num)]
axbdist = [[] for i in xrange(sample_num)]
for i in xrange(sample_num):
    m,n = random.sample(range(alen),2)
    adist[i] = scipy.spatial.distance.euclidean(a[m,:],a[n,:])

for i in xrange(sample_num):
    m,n = random.sample(range(blen),2)
    bdist[i] = scipy.spatial.distance.euclidean(b[m,:],b[n,:])

for i in xrange(sample_num):
    m = random.sample(range(alen),1)
    n = random.sample(range(blen),1)
    axbdist[i] = scipy.spatial.distance.euclidean(a[m,:],b[n,:])

import matplotlib.pyplot as plt
plt.figure(figsize=(12,8))
plt.subplots_adjust(hspace=0.45)
display_min = min(min(adist),min(bdist),min(axbdist))
display_max = max(max(adist),max(bdist),max(axbdist))
# picture 1: histogram A
coverage = adist
dist_mean=np.mean(coverage)
dist_std=np.std(coverage)
ax1 = plt.subplot(311)
plt.hist(coverage,bins=100,color='green',range=[display_min,display_max])
plt.title("Histogram of Genome A:%s length=%d\n  mean=%.4f  std_var=%.4f  "%(genomeList[ia],len(a)*window,dist_mean,dist_std))
# picture 2: histogram B
coverage = bdist
dist_mean=np.mean(coverage)
dist_std=np.std(coverage)
ax2 = plt.subplot(312)
plt.hist(coverage,bins=100,color='green',range=[display_min,display_max])
plt.title("Histogram of Genome B:%s length=%d\n  mean=%.4f  std_var=%.4f  "%(genomeList[ib],len(b)*window,dist_mean,dist_std))
# picture 3: histogram AxB
coverage = axbdist
dist_mean=np.mean(coverage)
dist_std=np.std(coverage)
ax3 = plt.subplot(313)
plt.hist(coverage,bins=100,color='blue',range=[display_min,display_max])
plt.title("Histogram of Genome AxB   mean=%.4f  std_var=%.4f  "%(dist_mean,dist_std))
#plt.show()
save_dir = "/home/xyjin/"
plt.savefig(save_dir+"non_overlap"+str(ia)+'_'+str(ib)+'_'+"win="+str(window)+"_k="+str(k)+'.png')
#plt.clf()
#plt.close()