import time
import pickle

class Solve:
    def __init__(self, hang, lie, hanglt, lielt):
        self.hang = hang
        self.lie = lie
        self.hanglt = hanglt
        self.lielt = lielt
        self.hanglt_all = []
        self.lielt_all = []
        for i in range(hang):
            tt = time.time()
            make = Make(hanglt[i], lie)
            self.hanglt_all.append(make.ltoutput[:])
            with open("temp/hanglt_all{}".format(i), mode="wb") as f:
                pickle.dump(self.hanglt_all[0], f)
            ttt = time.time()
            if (ttt - tt) > 1:
                print("第{}行初始化完成，用时{:.2f}秒".format(i, ttt - tt))
            del self.hanglt_all[0]
        for i in range(lie):
            tt = time.time()
            make = Make(lielt[i], hang)
            self.lielt_all.append(make.ltoutput[:])
            with open("temp/lielt_all{}".format(i), mode="wb") as f:
                pickle.dump(self.lielt_all[0], f)
            ttt = time.time()
            if (ttt - tt) > 1:
                print("第{}列初始化完成，用时{:.2f}秒".format(i, ttt - tt))
            del self.lielt_all[0]
        self.board = []  # 初始化解题板
        for i in range(hang):  # 将解题版全部填 -1 ，其中 -1 为待填入，0 为空，1 为实
            self.board.append(self.lie * [-1])

    def dohang(self):
        for i in range(self.hang):
            tt = time.time()
            board = self.board[i]
            temp_hanglt = len(board) * [0]
            with open("temp/hanglt_all{}".format(i), mode="rb") as f:
                hanglt_all = pickle.load(f)
            for j in range(len(hanglt_all) - 1, -1, -1):
                for k in range(len(board)):
                    if board[k] != -1:
                        if board[k] != hanglt_all[j][k]:
                            del hanglt_all[j]
                            break
            for k in range(len(board)):
                for each in hanglt_all:
                    temp_hanglt[k] += each[k]
            for j in range(len(temp_hanglt)):
                if temp_hanglt[j] == len(hanglt_all):
                    temp_hanglt[j] = 1
                elif temp_hanglt[j] == 0:
                    temp_hanglt[j] = 0
                else:
                    temp_hanglt[j] = -1
            self.board[i] = temp_hanglt[:]
            with open("temp/hanglt_all{}".format(i), mode="wb") as f:
                pickle.dump(hanglt_all, f)
            ttt = time.time()
            if (ttt - tt) > 1:
                print("第{}行已填，用时{:.2f}秒".format(i, ttt - tt))

    def dolie(self):
        for i in range(self.lie):
            tt = time.time()
            board = [0] * self.hang
            for m in range(self.hang):
                board[m] = self.board[m][i]
            temp_lielt = len(board) * [0]
            with open("temp/lielt_all{}".format(i), mode="rb") as f:
                lielt_all = pickle.load(f)
            for j in range(len(lielt_all) - 1, -1, -1):
                for k in range(len(board)):
                    if board[k] != -1:
                        if board[k] != lielt_all[j][k]:
                            del lielt_all[j]
                            break
            for k in range(len(board)):
                for each in lielt_all:
                    temp_lielt[k] += each[k]
            for j in range(len(temp_lielt)):
                if temp_lielt[j] == len(lielt_all):
                    temp_lielt[j] = 1
                elif temp_lielt[j] == 0:
                    temp_lielt[j] = 0
                else:
                    temp_lielt[j] = -1
            for m in range(self.hang):
                self.board[m][i] = temp_lielt[m]
            with open("temp/lielt_all{}".format(i), mode="wb") as f:
                pickle.dump(lielt_all, f)
            ttt = time.time()
            if (ttt - tt) > 1:
                print("第{}列已填，用时{:.2f}秒".format(i, ttt - tt))

    def Qprint(self):
        for i in self.board:
            for j in i:
                if j == 1:
                    print("█", end="")
                elif j == 0:
                    print("XX", end="")
                else:
                    print('--', end="")
            print("")

class Make:  # 用于处理输入的列表
    def __init__(self, lt, ltlen):  # 构造函数，lt为信息的某一行（列），ltlen为这个信息的满长度
        self.lt = lt  # 信息的某一行（列）
        self.ltlen = ltlen  # 该行（列）的满长度
        self.ltout = []  # 初始化所有可能情况的列表，是带有数字的
        self.ltturn = list(map(lambda x: x + 1, self.lt))  # 把信息全部加一
        self.kong = self.ltlen + 1  # 该行（列）的空格数
        for each in self.ltturn:  # 计算该行（列）的空格数
            self.kong -= each
        self.begin = self.ltturn[:]
        self.begin = self.begin + [0] * self.kong  # 构造所有可能情况的初始值
        self.ltout.append(self.begin[:])  # 将初始值加入所有情况列表
        self.ltoutput = []  # 初始化所有可能情况的列表，只有0和1
        self.ltdo = [0] * self.ltlen
        self.m2n()
        self.change()

    def m2n(self):  # 根据输入的信息和空的数量构造所有情况的列表
        while True:  # 无限循环
            if self.begin[0:self.kong] == [0] * self.kong:  # 当最前面全部为0
                self.ltout.append(self.begin[:])  # 此情况加入所有情况列表
                break  # 结束循环
            elif self.begin[-1] == 0:  # 当最后一位为0时，把从后数第一个非0值向后挪1
                for i in range(len(self.begin)):
                    if self.begin[-1 * i - 1] != 0:
                        self.begin[-1 * i - 1], self.begin[-1 * i] = self.begin[-1 * i], self.begin[-1 * i - 1]
                        break
                self.ltout.append(self.begin[:])  # 此情况加入所有情况列表
            else:
                for i in range(len(self.begin)):  # 当最后一位不为0时，把a000xyz中的a向后移一位，xyz接到a后
                    if self.begin[-1 * i - 1] == 0 and self.begin[-1 * i - 2] != 0:
                        self.begin[-1 * i - 1], self.begin[-1 * i - 2] = self.begin[-1 * i - 2], self.begin[
                            -1 * i - 1]
                        while True:
                            if self.begin[-1 * i] == 0:
                                self.begin.pop(-1 * i)
                                self.begin.append(0)
                                continue
                            break
                        break
                self.ltout.append(self.begin[:])  # 此情况加入所有情况列表

    def change(self):  # 将数字m转化为m长度的1，并存入self.ltoutput
        for each in self.ltout:
            ltouti = []
            for eachi in each:
                if eachi == 0:
                    ltouti.append(0)
                else:
                    for m in range(eachi):
                        if m != eachi - 1:
                            ltouti.append(1)
                        else:
                            ltouti.append(0)
            ltouti.pop(-1)
            self.ltoutput.append(ltouti)

    def tian(self, yiyou):
        A = list(range(len(self.ltoutput)))
        A.reverse()
        for j in A:
            for i in range(self.ltlen):
                if yiyou[i] != -1:
                    if self.ltoutput[j][i] != yiyou[i]:
                        del self.ltoutput[j]
                        break
        for i in range(self.ltlen):
            templs = []
            for j in range(len(self.ltoutput)):
                templs.append(self.ltoutput[j][i])
            if sum(templs) == len(self.ltoutput):
                self.ltdo[i] = 1
            elif sum(templs) == 0:
                self.ltdo[i] = 0
            else:
                self.ltdo[i] = -1

t_all = time.time()
t_1 = time.time()
with open("inp10.txt", mode="r") as f:  # 打开输入文档
    txt = f.read()  # 读取输入文档
    txt = txt.split("\n")  # 文档用换行分隔，存为列表
    txt = [x for x in txt if x != ""]  # 列表去空元素
    a = txt[0].split(",")  # a为行数和列数
    hangN, lieN = int(a[0]), int(a[1])  # hangN为行数，lieN为列数
    del a  # 去除a
    txt.pop(0)  # 将输入的列表第一行去掉
    hanglt = []  # 初始化行的信息
    lielt = []  # 初始化列的信息
    for i in range(hangN):  # 根据行数循环
        a = txt[i]  # a 为第一行的信息
        a = a.split(",")  # a用逗号分隔后存为列表
        a = list(map(int, a))  # 列表的字符串转数字
        hanglt.append(a)  # 以列表形式存入hanglt
    for i in range(hangN):  # 此循环去除行的信息，使最前面为列信息
        txt.pop(0)
    for i in range(lieN):  # 根据列数循环
        a = txt[i]  # a 为第一列的信息
        a = a.split(",")  # a用逗号分隔后存为列表
        a = list(map(int, a))  # 列表的字符串转为数字
        lielt.append(a)  # 以列表的形式存入lielt
print("导入时间：{:.2f}秒".format(time.time() - t_1))
t_1 = time.time()
solve = Solve(hangN, lieN, hanglt, lielt)  # 根据hangN，lieN，hanglt，lielt（行数，列数，行信息，列信息）从Solve类构造solve对象
print("初始化时间：{:.2f}秒".format(time.time() - t_1))
tempsolve = solve.board[:]
while True:
    t_1 = time.time()
    solve.dohang()
    solve.dolie()
    solve.Qprint()
    print("")
    if tempsolve == solve.board:
        break
    tempsolve = solve.board[:]
    print("本次时间：{:.2f}秒".format(time.time() - t_1))
solve.Qprint()
print("总时间：{:.2f}秒".format(time.time() - t_all))


import json
import base64
import os

def decode_base64_to_pdf(base64_string, output_pdf_path):
    try:
        # Decode the base64 string
        decoded_data = base64.b64decode(base64_string)

        # Write the decoded data to a PDF file
        with open(output_pdf_path, "wb") as pdf_file:
            pdf_file.write(decoded_data)

        print(f"Decoding successful. PDF file saved at: {output_pdf_path}")

    except Exception as e:
        print(f"Error: {e}")

def decode_base64_values_from_json_to_pdfs(json_file_path, output_directory):
    try:
        # Read the JSON content from the input file
        with open(json_file_path, "r") as json_file:
            json_data = json.load(json_file)

        # Ensure the output directory exists
        os.makedirs(output_directory, exist_ok=True)

        # Iterate over each object in the JSON array
        for i, data_entry in enumerate(json_data):
            # Extract the base64-encoded string from the "Value" field
            base64_string = data_entry.get("Value", "")

            # Create a unique output PDF file path based on the index
            output_pdf_path = f"{output_directory}/parcel_{i + 1}.pdf"  #

            # Decode and save as a PDF file
            decode_base64_to_pdf(base64_string, output_pdf_path)

    except Exception as e:
        print(f"Error: {e}")

# Example usage
input_json_file_path = "D:/Coding/PYTHON/PythonProjects/BASE64toPDF/data/input.json"
output_directory = "D:/Coding/PYTHON/PythonProjects/BASE64toPDF/data"

decode_base64_values_from_json_to_pdfs(input_json_file_path, output_directory)


from aminos.processing.mutations import Mutation

mutation_dict = {
    "inframe_deletion": "D",
    "inframe_insertion": "I",
    "missense": "M",
}

def parse_amino_acid_seq_position(input_seq):

    position = ''
    sequence = ''
    for char in input_seq:
        if char.isdigit():
            position += char
        else:
            sequence += char

    if not sequence:
        sequence = '*'

    return int(position), sequence

def parse_amino_acid_field(input_string):
    parsed_strings = input_string.split('>')
    if len(parsed_strings) != 2:
        raise ValueError(f"The parsed string has a length of: {len(parsed_strings)}, expected only two")

    try:
        ref_pos, ref_seq = parse_amino_acid_seq_position(parsed_strings[0])
    except ValueError as e:
        raise Exception(f"While extracting the sequence and the position of the reference the following error was encountered: {e}")

    try:
        mut_pos, mut_seq = parse_amino_acid_seq_position(parsed_strings[1])
    except ValueError as e:
        raise Exception(f"While extracting the sequence and the position of the reference the following error was encountered: {e}")

    return ref_pos, ref_seq, mut_pos, mut_seq

# take a bcftools csq string and return the transcript and instruction
# an instruction is defined as:
# (mutation_code, ref_pos, ref_seq, mut_pos, mut_seq)

def process_csq(transcript_id, csq, valid_mutation_types=['missense', 'inframe_insertion', 'inframe_deletion']):
    csq = csq.split('|')

    if len(csq) != 7:
        return None

    if transcript_id != csq[2]:
        return None

    mutation_type = csq[0]

    if mutation_type not in valid_mutation_types:
        return None

    mutation_code = mutation_dict[mutation_type]

    ref_pos, ref_seq, mut_pos, mut_seq = parse_amino_acid_field(csq[5])

    return Mutation(mut_code=mutation_code, ref_pos=ref_pos, ref_seq=ref_seq, alt_pos=mut_pos, alt_seq=mut_seq)


import os
import pydicom
import numpy as np
import matplotlib.pyplot as plt
from flask import Flask, request, render_template, url_for, redirect
from magicsauce import *
import keras
import tensorflow as tf
import cv2

model = keras.models.load_model(weights_file, custom_objects={'correct_positive_diagnoses': correct_positive_diagnoses})
model._make_predict_function()
app = Flask(__name__, static_url_path='/static')

@app.route("/")
def fileFrontPage():
    return render_template('base.html')

@app.route("/handleUpload", methods=['POST'])
def handleFileUpload():
    if 'brainscan' in request.files:
        dcm = request.files['brainscan']
        if dcm.filename != ' ':
            ds = pydicom.dcmread(dcm)

        dcmarray = ds.pixel_array
        fname = dcm.filename

        img = window_and_scale_brain_subdural_soft(ds)
        img = cv2.resize(img, INPUT_SHAPE[:2], interpolation=cv2.INTER_LINEAR)
        tensor = np.expand_dims(img, axis=0)

        graph = tf.get_default_graph()
        with graph.as_default():
            p = model.predict(tensor)

        print(p)
        fname = dcm.filename
        max_intensity = np.max(dcmarray)
        plt.imshow(dcmarray, cmap=plt.cm.bone)
        plt.savefig('static/images/'+fname+'_raw_img.png')
        metadata = metaprint(ds)
        bsb= bsb_window(ds, fname)
    return render_template('index.html', max=max_intensity, metadata= metadata,
        url = '/static/images/'+fname+'_raw_img.png', windowing = bsb, prediction=p)

def metaprint(dataset):
    metadata = []
    for data_element in dataset:
        if data_element.name == 'Pixel Data':
            name = data_element.name
            value = 'Array of 524288 elements'
        else:
            name = data_element.name
            value = data_element.value
        metadata.append({'name' : name, 'value': value})
    return metadata

def correct_dcm(dcm):
    x = dcm.pixel_array + 1000
    px_mode = 4096
    x[x>=px_mode] = x[x>=px_mode] - px_mode
    dcm.PixelData = x.tobytes()
    dcm.RescaleIntercept = -1000

def window_image(dcm, window_center, window_width):

    if (dcm.BitsStored == 12) and (dcm.PixelRepresentation == 0) and (int(dcm.RescaleIntercept) > -100):
        correct_dcm(dcm)

    img = dcm.pixel_array * dcm.RescaleSlope + dcm.RescaleIntercept
    img_min = window_center - window_width // 2
    img_max = window_center + window_width // 2
    img = np.clip(img, img_min, img_max)

    return img

def bsb_window(dcm, filename):
    brain_img = window_image(dcm, 40, 80)
    subdural_img = window_image(dcm, 80, 200)
    bone_img = window_image(dcm, 600, 2800)

    brain_img = (brain_img - 0) / 80
    subdural_img = (subdural_img - (-20)) / 200
    bone_img = (bone_img + 800) / 2800

    plt.imshow(brain_img, cmap=plt.cm.bone)
    plt.title('Brain Matter')
    bmpath = 'static/images/'+filename+'_brain_img.png'
    plt.savefig(bmpath)

    plt.imshow(subdural_img, cmap=plt.cm.bone)
    plt.title('Subdural/Blood')
    sdpath = 'static/images/'+filename+'_subdural_img.png'
    plt.savefig(sdpath)

    #plt.imshow(bone_img, cmap=plt.cm.bone)
    plt.imshow(bone_img, cmap=plt.cm.bone)
    plt.title('Bone')
    bopath = 'static/images/'+filename+'_bone.png'
    plt.savefig(bopath)

    windowing = [
    {'url': bmpath},
    {'url': sdpath},
    {'url': bopath}]

    #bsb_img = np.array([brain_img, subdural_img, soft_img]).transpose(1,2,0)

    return windowing

if __name__ == '__main__':
    app.run(debug=True, port=9999)


import matplotlib.pyplot as plt
import utils
import pai_io
import numpy as np

if __name__ == '__main__' :
    #filename ='../images/gray/rice.jpg'

    filename ='../images/gray/ten_coins.png'
    image=pai_io.imread(filename)
    image_1 = image.copy()

    bin_image = utils.threshold(image, 100)
    image = utils.set_image_on_lsb(image, bin_image)
    hidden_image = utils.get_image_from_lsb(image)
    fig, xs = plt.subplots(1,3)
    for i in range(3):
        xs[i].set_axis_off()
    xs[0].imshow(image_1, cmap = 'gray')
    xs[0].set_title('Original')
    xs[1].imshow(image, cmap = 'gray')
    xs[1].set_title('LSB')
    xs[2].imshow(hidden_image * 255, cmap = 'gray')
    xs[2].set_title('Hidden')
    plt.show()


import argparse
import logging
import os
import random
import shutil
import sys
import numpy as np
import torch
import torch.backends.cudnn as cudnn
import torch.optim as optim
from tensorboardX import SummaryWriter
from torch.nn.modules.loss import CrossEntropyLoss
from torch.utils.data import DataLoader
from torchvision import transforms
from tqdm import tqdm
from dataloaders.dataset import (RandomGenerator,BaseDataSets1,TwoStreamBatchSampler)
from networks.net_factory import net_factory
from utils import losses, ramps

parser = argparse.ArgumentParser()

parser.add_argument('--exp', type=str,
                    default='results', help='experiment_name')
parser.add_argument('--model', type=str,
                    default='unet', help='model_name')
parser.add_argument('--max_iterations', type=int,
                    default=60000, help='maximum epoch number to train')
parser.add_argument('--batch_size', type=int, default=2,
                    help='batch_size per gpu')
parser.add_argument('--deterministic', type=int,  default=1,
                    help='whether use deterministic training')
parser.add_argument('--base_lr', type=float,  default=0.01,
                    help='segmentation network learning rate')

parser.add_argument('--seed', type=int,  default=1337, help='random seed')
parser.add_argument('--num_classes', type=int,  default=3,
                    help='output channel of network')
# label and unlabel
parser.add_argument('--labeled_bs', type=int, default=1,
                    help='labeled_batch_size per gpu')
parser.add_argument('--labeled_num', type=int, default=7,
                    help='labeled data')
# costs
parser.add_argument('--ema_decay', type=float,  default=0.99, help='ema_decay')
parser.add_argument('--consistency_type', type=str,
                    default="mse", help='consistency_type')
parser.add_argument('--consistency', type=float,
                    default=0.1, help='consistency')
parser.add_argument('--consistency_rampup', type=float,
                    default=200.0, help='consistency_rampup')
args = parser.parse_args()

def get_current_consistency_weight(epoch):
    # Consistency ramp-up from https://arxiv.org/abs/1610.02242
    return args.consistency * ramps.sigmoid_rampup(epoch, args.consistency_rampup)

def update_ema_variables(model, ema_model, alpha, global_step):
    # Use the true average until the exponential average is more correct
    alpha = min(1 - 1 / (global_step + 1), alpha)
    for ema_param, param in zip(ema_model.parameters(), model.parameters()):
        ema_param.data.mul_(alpha).add_(param.data, alpha=1 - alpha)

def train(args, snapshot_path):
    base_lr = args.base_lr
    num_classes = args.num_classes
    batch_size = args.batch_size
    max_iterations = args.max_iterations

    def create_model(ema=False):
        # Network definition
        model = net_factory(net_type=args.model, in_chns=1,
                            class_num=num_classes)
        if ema:
            for param in model.parameters():
                param.detach_()
        return model

    model = create_model()
    ema_model = create_model(ema=True)

    def worker_init_fn(worker_id):
        random.seed(args.seed + worker_id)

    db_train = BaseDataSets1()

    total_slices = len(db_train)
    labeled_slice=430
    print("Total silices is: {}, labeled slices is: {}".format(total_slices, labeled_slice))
    labeled_idxs = list(range(0, labeled_slice))
    unlabeled_idxs = list(range(labeled_slice, total_slices))
    batch_sampler = TwoStreamBatchSampler(labeled_idxs, unlabeled_idxs, batch_size, batch_size-args.labeled_bs)
    trainloader = DataLoader(db_train, batch_sampler=batch_sampler,num_workers=0, pin_memory=True, worker_init_fn=worker_init_fn)

    model.train()

    optimizer = optim.SGD(model.parameters(), lr=base_lr,momentum=0.9, weight_decay=0.0001)
    ce_loss = CrossEntropyLoss()
    dice_loss = losses.DiceLoss(num_classes)
    writer = SummaryWriter(snapshot_path + '/log')
    logging.info("{} iterations per epoch".format(len(trainloader)))
    iter_num = 0
    max_epoch = max_iterations // len(trainloader) + 1
    iterator = tqdm(range(max_epoch), ncols=70)

    for epoch_num in iterator:
        for i_batch, sampled_batch in enumerate(trainloader):
            volume_batch, label_batch = sampled_batch['image'], sampled_batch['label']
            volume_batch, label_batch = volume_batch.cuda(), label_batch.cuda()
            unlabeled_volume_batch = volume_batch[args.labeled_bs:]
            noise = torch.clamp(torch.randn_like(unlabeled_volume_batch) * 0.1, -0.2, 0.2)
            ema_inputs = unlabeled_volume_batch + noise
            outputs = model(volume_batch)
            outputs_soft = torch.softmax(outputs, dim=1)
            with torch.no_grad():
                ema_output = ema_model(ema_inputs)
                ema_output_soft = torch.softmax(ema_output, dim=1)
            loss_ce = ce_loss(outputs[:args.labeled_bs],label_batch[:][:args.labeled_bs].long())
            loss_dice = dice_loss(outputs_soft[:args.labeled_bs], label_batch[:args.labeled_bs].unsqueeze(1))
            supervised_loss = 0.5 * (loss_dice + loss_ce)
            consistency_weight = get_current_consistency_weight(iter_num//150)
            if iter_num < 1000:
                consistency_loss = 0.0
            else:
                consistency_loss = torch.mean(
                    (outputs_soft[args.labeled_bs:]-ema_output_soft)**2)
            loss = supervised_loss + consistency_weight * consistency_loss
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            update_ema_variables(model, ema_model, args.ema_decay, iter_num)
            lr_ = base_lr * (1.0 - iter_num / max_iterations) ** 0.9
            for param_group in optimizer.param_groups:
                param_group['lr'] = lr_

            iter_num = iter_num + 1
            writer.add_scalar('info/lr', lr_, iter_num)
            writer.add_scalar('info/total_loss', loss, iter_num)
            writer.add_scalar('info/loss_ce', loss_ce, iter_num)
            writer.add_scalar('info/loss_dice', loss_dice, iter_num)
            writer.add_scalar('info/consistency_loss',
                              consistency_loss, iter_num)
            writer.add_scalar('info/consistency_weight',
                              consistency_weight, iter_num)

            if iter_num % 20== 0:
                image = volume_batch[1, 0:1, :, :]
                writer.add_image('train/Image', image, iter_num)
                outputs = torch.argmax(torch.softmax(
                    outputs, dim=1), dim=1, keepdim=True)
                writer.add_image('train/Prediction',
                                 outputs[1, ...] * 50, iter_num)
                labs = label_batch[1, ...].unsqueeze(0) * 50
                writer.add_image('train/GroundTruth', labs, iter_num)
                logging.info( 'iteration %d : loss : %f, loss_ce: %f, loss_dice: %f' %
                    (iter_num, loss.item(), loss_ce.item(), loss_dice.item()))

            if iter_num % 2000 == 0:
                save_mode_path = os.path.join(
                    snapshot_path, 'iter_' + str(iter_num) + '.pth')
                torch.save(model.state_dict(), save_mode_path)
                logging.info("save model to {}".format(save_mode_path))

            if iter_num >= max_iterations:
                break
        if iter_num >= max_iterations:
            iterator.close()
            break
    writer.close()
    return "Training Finished!"

if __name__ == "__main__":
    if not args.deterministic:
        cudnn.benchmark = True
        cudnn.deterministic = False
    else:
        cudnn.benchmark = False
        cudnn.deterministic = True

    random.seed(args.seed)
    np.random.seed(args.seed)
    torch.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)

    snapshot_path = "../model/{}_{}_labeled/{}".format(
        args.exp, args.labeled_num, args.model)
    if not os.path.exists(snapshot_path):
        os.makedirs(snapshot_path)
    if os.path.exists(snapshot_path + '/code'):
        shutil.rmtree(snapshot_path + '/code')
    shutil.copytree('.', snapshot_path + '/code',
                    shutil.ignore_patterns(['.git', '__pycache__']))

    logging.basicConfig(filename=snapshot_path+"/log.txt", level=logging.INFO,
                        format='[%(asctime)s.%(msecs)03d] %(message)s', datefmt='%H:%M:%S')
    logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
    logging.info(str(args))
    train(args, snapshot_path)


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

# In[31]:

from pathlib import Path
import os, sys

from astropy.nddata import CCDData
from astropy.io import fits
from astropy.stats import mad_std

import ccdproc as ccdp
import matplotlib.pyplot as plt
import numpy as np

from convenience_functions import show_image

import numpy as np
import matplotlib.pyplot as plt
from astropy.visualization import SqrtStretch
from astropy.visualization.mpl_normalize import ImageNormalize

from photutils import make_source_mask
from astropy.stats import sigma_clipped_stats

from photutils import aperture_photometry
from photutils import CircularAnnulus
from photutils.aperture import ApertureStats
from photutils.aperture import CircularAperture

from matplotlib.patches import Patch
from matplotlib.lines import Line2D
from IPython.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))

# In[171]:

# cloudy_path = Path('dao_data/images/im_div_mf/cloudy/')
# clouds = ccdp.ImageFileCollection(cloudy_path)
# clouds.summary['date-obs'][-1]

# In[172]:

# Defining file paths for the data bullshit ############
dark_path = Path('plaskett_calibration/darks')
flats_path = Path('plaskett_calibration/flats')
bias_path = Path('plaskett_calibration/bias')
image_path = Path('plaskett_images')

# Now taking the data bullshit and creating an image file collection #########
dark_frames = ccdp.ImageFileCollection(dark_path)
flats_frames = ccdp.ImageFileCollection(flats_path)
bias_frames = ccdp.ImageFileCollection(bias_path)
image_frames = ccdp.ImageFileCollection(image_path)

# In[173]:

# Trimming the files
darks = dark_frames.files
flats = flats_frames.files
images = image_frames.files
for i in range(len(images)):
    image = CCDData.read(image_path / images[i], unit='adu')
    image_oc = ccdp.subtract_overscan(image,
                                         overscan=image[2307:, :],
                                         overscan_axis=0)
    image_trim = ccdp.trim_image(image_oc[:2307, :])
    image_trim.write(image_path / "images_trimmed/{}".format(images[i]), overwrite=True)
    if i < len(darks):
        dark = CCDData.read(dark_path / darks[i], unit='adu')
        dark_oc = ccdp.subtract_overscan(dark,
                                         overscan=dark[2307:, :],
                                         overscan_axis=0)
        dark_trim = ccdp.trim_image(dark_oc[:2307, :])
        dark_trim.write(dark_path / "darks_trimmed/{}".format(darks[i]), overwrite=True)
    if i < len(flats):
        flat = CCDData.read(flats_path / flats[i], unit='adu')
        flat_oc = ccdp.subtract_overscan(flat,
                                         overscan=flat[2307:, :],
                                         overscan_axis=0)
        flat_trim = ccdp.trim_image(flat_oc[:2307, :])
        flat_trim.write(flats_path / "flats_trimmed/{}".format(flats[i]), overwrite=True)

# In[174]:

print(images[0])

# In[32]:

# Redefining file paths for the data bullshit ############
dark_path = Path('plaskett_calibration/darks/darks_trimmed')
flats_path = Path('plaskett_calibration/flats/flats_trimmed')
image_path = Path('plaskett_images/images_trimmed')

# Now taking the data bullshit and creating an image file collection #########
dark_frames = ccdp.ImageFileCollection(dark_path)
flats_frames = ccdp.ImageFileCollection(flats_path)
image_frames = ccdp.ImageFileCollection(image_path)

# In[176]:

dark_frames.summary

# In[177]:

flats_frames.summary

# In[178]:

image_frames.summary

# In[179]:

# The Hunt For the Exposure;  it's unimportant at this point

# image_name = 'dao_c182_2022_006213.fits.gz'.rstrip()

# image_path = Path(dark_path) / image_name

# hdu_list = fits.open(image_path)
# hdu_list.info()

# hdu = hdu_list[0]
# hdu.header

# with fits.open(all_flats[9]) as hdu_flats:
#     hdu_flats.verify("fix")
# hdu_flats = fits.open(all_flats[10])
# hdu_flats.info()

# hdu = hdu_flats[0]
# hdu.header['EXPOSURE']
# hdul[0].header

# In[180]:

# METHOD 2: ##########################

# A) Creating Raw Master Dark by combining all dark frames
calibrated_darks = dark_frames.files_filtered(include_path=True)

raw_master_dark = ccdp.combine(calibrated_darks,
                             method='median',
                             sigma_clip=True, sigma_clip_low_thresh=5, sigma_clip_high_thresh=5,
                             sigma_clip_func=np.ma.median, sigma_clip_dev_func=mad_std,
                             unit='adu'
                            )

# Saving the file (if I run this uncommented after file already created, it gets angry)
raw_master_dark.meta["combined"] = True

raw_master_dark.write('plaskett_calibration/raw_master_dark.fits')

# In[181]:

#print(raw_master_dark)
#print(type(raw_master_dark))
# B1) Subtracting Raw Master Dark from each flat frame:
from astropy.io import fits as pyfits

a_flat = CCDData.read(flats_frames.files_filtered(include_path=True)[0], unit='adu')

show_image(a_flat, cmap='gray')

all_flats = flats_frames.files_filtered(include_path=True)
print(all_flats)
print(type(all_flats))

def subtractImg(fout, fitsToSubtract, filelist): # Slightly modified Boley function
    '''takes(fout,fitsToSubtract,filelist)  '''
    import pdb
    hdulref = fits.open(fitsToSubtract)
    head = hdulref[0].header
    refdata = hdulref[0].data
    hdulref.close()

    #fh = open(filelist, "r")
    iter = 0
    #for line in fh:
    for line in filelist: # This is basically all I changed
        fpath = line.rstrip()
        hdul = fits.open(fpath)

        head = hdul[0].header
        imgdata = hdul[0].data

        Ndata = imgdata.size

        Nref = refdata.size

        if not Ndata == Nref:
            print("Mismatch between data {} and reference {}".format(Ndata, Nref))
            sys.exit()

        NY, NX = imgdata.shape

        corrected = imgdata * 0
        for j in range(NY):
            for i in range(NX):
                if refdata[j][i] > imgdata[j][i]:
                    corrected[j][i] = 0
                else:
                    corrected[j][i] = imgdata[j][i] - refdata[j][i]

        head.set('comment', 'Subtracted {} from file {}'.format(fitsToSubtract, fpath))
        hdul[0].data = corrected

        hdul.writeto(fout + "-" + repr(iter).zfill(3) + ".fits", overwrite=True)
        hdul.close()
        iter += 1

subtractImg("plaskett_calibration/flats/flats_trimmed/flats_minus_rmd/flats_minus_rmd",
            'plaskett_calibration/raw_master_dark.fits',
            all_flats)

# In[182]:

# B2) Combining flats - rmd into Master Flat:
def inv_median(a):
    return 1 / np.median(a)

flats_rmd_path = flats_path / "flats_minus_rmd"
flats_rmd_frames = ccdp.ImageFileCollection(flats_rmd_path)
flats_minus_rmd = flats_rmd_frames.files_filtered(include_path=True)
master_flat = ccdp.combine(flats_minus_rmd,
                             method='median',scale=inv_median,
                             sigma_clip=True, sigma_clip_low_thresh=5, sigma_clip_high_thresh=5,
                             sigma_clip_func=np.ma.median, sigma_clip_dev_func=mad_std,
                             unit='adu'
                            )

# Saving the file (if I run this uncommented after file already created, it gets angry)
master_flat.meta["combined"] = True

master_flat.write('plaskett_calibration/master_flat.fits')

# In[183]:

CCDData.read('plaskett_calibration/master_flat.fits').data.min()

# In[184]:

# C3) Subtracting raw master dark from each science frame:
all_images = image_frames.files_filtered(include_path=True)

subtractImg("plaskett_images/images_trimmed/im_minus_rmd/im_minus_rmd",
            "plaskett_calibration/raw_master_dark.fits", all_images)

# In[189]:

# C4) Attempt at dividing im_minus_rmd files by master flat:

def divImg(fout, fitsToDivide, filelist):  # Literally Boley's - func swtiched to /
    '''takes(fout,fitsToSubtract,filelist)  '''

    hdulref = fits.open(fitsToDivide)
    head = hdulref[0].header
    refdata = hdulref[0].data
    hdulref.close()

    #fh = open(filelist, "r")
    iter = 0
    #for line in fh:
    for line in filelist:
        fpath = line.rstrip()
        hdul = fits.open(fpath)

        head = hdul[0].header
        imgdata = hdul[0].data

        Ndata = imgdata.size

        Nref = refdata.size

        if not Ndata == Nref:
            print("Mismatch between data {} and reference {}".format(Ndata, Nref))
            sys.exit()

        NY, NX = imgdata.shape

        corrected = imgdata * 0
        for j in range(NY):
            for i in range(NX):
                if refdata[j][i] == 0: # The most notable change I made
                    corrected[j][i] = 0
                else:
                    corrected[j][i] = imgdata[j][i] / refdata[j][i]

        head.set('comment', 'Divided {} from file {}'.format(fitsToDivide, fpath))
        hdul[0].data = corrected

        hdul.writeto(fout + "-" + repr(iter).zfill(3) + ".fits")
        hdul.close()
        iter += 1

im_rmd_path = image_path / "im_minus_rmd"
im_rmd_frames = ccdp.ImageFileCollection(im_rmd_path)
all_im_minus_rmd = im_rmd_frames.files_filtered(include_path=True)

divImg("plaskett_images/images_trimmed/processed_images/proc_im",
       "plaskett_calibration/master_flat.fits", all_im_minus_rmd)

# # Data analysis

# ## Background estimation
#
# All of these notes are copied from https://buildmedia.readthedocs.org/media/pdf/photutils/v0.3/photutils.pdf
#
# Having an accurate estimate of the background noise is important for determining the significance of source detections and for estimating photometric errors.
#
# Unfortunately, accurate background and background noise estimation is a difficult task. Further, because astronomical images can cover a wide variety of scenes, there is not a single background estimation method that will always be applicable. Photutils provides tools for estimating the background and background noise in your data, but they will likely require some tweaking to optimize the background estimate for your data.
#
# If the background level and noise are relatively constant across an image, the simplest way to estimate these values is to derive scalar quantities using simple approximations. Of course, when computing the image statistics one must take into account the astronomical sources present in the images, which add a positive tail to the distribution of pixel intensities. For example, one may consider using the image median as the background level and the image standard deviation as the 1-sigma background noise, but the resulting values are obviously biased by the presence of real sources.
#
# A slightly better method involves using statistics that are robust against the presence of outliers, such as the biweight location for the background level and biweight midvariance or median absolute deviation (MAD) for the background noise estimation. However, for most astronomical scenes these methods will also be biased by the presence of astro- nomical sources in the image.
#

# In[38]:

import matplotlib.pyplot as plt
from astropy.visualization import SqrtStretch
from astropy.visualization.mpl_normalize import ImageNormalize
get_ipython().run_line_magic('matplotlib', 'inline')

# In[39]:

rmd = CCDData.read("plaskett_calibration/raw_master_dark.fits")
mf = CCDData.read("plaskett_calibration/master_flat.fits", unit='adu')
exraw = CCDData.read('plaskett_images/dao_c182_2022_004803.fits.gz', unit='adu')
exproc = CCDData.read(image_path / "processed_images/proc_im-000.fits", unit='adu')
fig, ax = plt.subplots(2,2,figsize=(10,20))
show_image(rmd, cmap='Greys_r', percl=99.9, fig=fig, ax=ax[0,0], show_colorbar=False)
ax[0,0].set_title('Raw Master Dark', fontsize=20)
show_image(mf, cmap='Greys_r', percl=99.9, fig=fig, ax=ax[0,1])
ax[0,1].set_title('Master Flat', fontsize=20)
show_image(exraw, cmap='Greys_r', percl=99.9, fig=fig, ax=ax[1,0], show_colorbar=False)
ax[1,0].set_title('Example raw image', fontsize=20)
show_image(exproc, cmap='Greys_r', percl=99.95, fig=fig, ax=ax[1,1], show_colorbar=False)
ax[1,1].set_title('Example processed image', fontsize=20)
norm = ImageNormalize(stretch=SqrtStretch())
for i in range(2):
    for j in range(2):
        ax[i,j].tick_params(labelsize=18)
        ax[i,j].set_xlabel('X pixel', fontsize=20)
        ax[i,j].set_ylabel('Y pixel', fontsize=20)
plt.subplots_adjust(wspace=0.3)
plt.savefig("RMD_MF_Img-Comp.jpg")

# In[6]:

# aimgf2.summary['date-obs'][-1]

# In[40]:

from astropy.stats import sigma_clipped_stats
mean, median, std = sigma_clipped_stats(exproc, sigma=3.0)#, iters=5)
print((mean, median, std))

# In[41]:

from photutils import make_source_mask
from astropy.stats import sigma_clipped_stats
mask = make_source_mask(exproc, nsigma=3, npixels=5, dilate_size=11)
mean, median, std = sigma_clipped_stats(exproc, sigma=4.0, mask=mask)
print((mean, median, std))

# In[42]:

from photutils.detection import DAOStarFinder
pathway = image_path / "processed_images"
imframe = ccdp.ImageFileCollection(pathway)
imframes = imframe.files_filtered(include_path=True)[:10]
counts = []
median_bg = []
std_bg = []
for frame in imframes:
    mask = make_source_mask(exproc, nsigma=3, npixels=5, dilate_size=11)
    mean, median, std = sigma_clipped_stats(exproc, sigma=4.0, mask=mask)
    median_bg.append(median)
    std_bg.append(std)
    daofind = DAOStarFinder(fwhm=3.0, threshold=500.*std)
    sources = daofind(exproc - median)
    for col in sources.colnames:
        sources[col].info.format = '%.8g'  # for consistent table output
    source = sources[(sources['ycentroid']>=1500)]#&(sources['ycentroid']<=600)]
    counts.append(np.mean(source['peak'].value))
#     print(sources)

# In[43]:

positions = np.transpose((sources['xcentroid'], sources['ycentroid']))
apertures = CircularAperture(positions, r=50.);
norm = ImageNormalize(stretch=SqrtStretch())
fig, ax = plt.subplots(figsize=(30,15))
#plt.imshow(exproc, cmap='Greys_r', origin='lower', norm=norm,
#           interpolation='nearest')
show_image(exproc, cmap='Greys_r', percl=99.95, fig=fig, ax=ax)
apertures.plot(color='r', lw=1.5, alpha=0.5);
# fig.savefig("Identified_DAO_Stars.jpg")

# In[44]:

xpixels = sources['xcentroid'].value.astype(int)
ypixels = sources['ycentroid'].value.astype(int)
xpixels

# In[45]:

xindex0 = np.digitize(xpixels, bins=np.arange(0,1000, 100))
yindex0 = np.digitize(ypixels, bins=np.arange(0,2000, 100))

# In[ ]:

# Separating Day 1 and Day 2 images
proc_frames = ccdp.ImageFileCollection(image_path / "processed_images",
                                       keywords='*')

for hdu, fname in proc_frames.hdus(return_fname=True):
    if hdu.header['UT-DATE'] == "2022-02-26":
        hdu.writeto(image_path / "processed_images/day1" / fname)
    else:
         hdu.writeto(image_path / "processed_images/day2" / fname)

# In[46]:

img_path1 = Path(image_path / 'processed_images/day1/')
imgf1 = ccdp.ImageFileCollection(img_path1)
img_path2 = Path(image_path / 'processed_images/day2/')
imgf2 = ccdp.ImageFileCollection(img_path2)
imgf1.summary

# In[31]:

imgf2.summary

# ## Aligned images in AstroImageJ

# In[47]:

aimg_path1 = Path(image_path / 'processed_images/aligned/day1/')
aimgf1 = ccdp.ImageFileCollection(aimg_path1)
aimg_path2 = Path(image_path / 'processed_images/aligned/day2/')
aimgf2 = ccdp.ImageFileCollection(aimg_path2)

# In[48]:

aimgf2.summary

# In[49]:

tobs2 = np.array(aimgf2.summary['date-obs'].value)
hr2 = []
min2 = []
sec2 = []
for t in tobs2:
    hr = t[11:13]
    minute = t[14:16]
    sec = t[17:19]
    hr2.append(hr)
    min2.append(minute)
    sec2.append(sec)
hr2 = np.array(hr2).astype(float)
min2 = np.array(min2).astype(float)
sec2 = np.array(sec2).astype(float)
time2 = (hr2-hr2.min())*60 + min2 + sec2/60

# In[50]:

tobs1 = np.array(aimgf1.summary['date-obs'].value)
hr1 = []
min1 = []
sec1 = []
for t in tobs1:
    hr = t[11:13]
    minute = t[14:16]
    sec = t[17:19]
    hr1.append(hr)
    min1.append(minute)
    sec1.append(sec)
hr1 = np.array(hr1).astype(float)
min1 = np.array(min1).astype(float)
sec1 = np.array(sec1).astype(float)
time1 = (hr1-hr1.min())*60 + min1 + sec1/60

# ## Day 1 analysis:

# In[51]:

aimgf1.summary

# In[52]:

# Initial guess:
target = (614,1187)
comp = [(357,1228), (614,1468), (660,1585)]
ourstars = [(614,1187), (357,1228), (614,1468), (660,1585)]

# In[53]:

exproc = CCDData.read(aimg_path1/aimgf1.files[0], unit='adu')
fig, ax = plt.subplots(figsize=(10,10))
show_image(exproc, cmap='Greys', percl=99.9, fig=fig, ax=ax, show_colorbar=False)
apertures = CircularAperture(target, r=11.);
apertures.plot(color='r', lw=2.5, alpha=1);
apertures = CircularAperture(target, r=12.);
apertures.plot(color='xkcd:black', ls='--', lw=1.5, alpha=1);
apertures = CircularAperture(target, r=19.);
apertures.plot(color='xkcd:black', lw=1.5, ls='--', alpha=1);

rvals = [11, 11, 11, 11]
for r, compstar in enumerate(comp):
    apertures = CircularAperture(compstar, r=rvals[r]);
    apertures.plot(color='xkcd:bright blue', lw=3, alpha=1);
    apertures = CircularAperture(compstar, r=rvals[r]+2);
    apertures.plot(color='xkcd:bright pink', lw=2, ls='--', alpha=1);
    apertures = CircularAperture(compstar, r=rvals[r]+8);
    apertures.plot(color='xkcd:bright pink', lw=2, ls='--', alpha=1);
#plt.xlim(250,750)
#plt.ylim(1100,1700)
ax.tick_params(labelsize=18)
plt.xlabel('X pixel', fontsize=20)
plt.ylabel('Y pixel', fontsize=20)
plt.title("Example Day 1 Aperture Image", fontsize=16)
legend_elements = [Line2D([0], [0], lw=3, color='xkcd:bright blue', ls='-', label='Calib. star\naperture'),
                   Line2D([0], [0], lw=3, color='xkcd:bright pink', ls='--', label='Calib. star\nBG annulus'),
                   Line2D([0], [0], color='r', lw=3, label='Target aperture'),
                  Line2D([0], [0], color='k', lw=3, label='Target BG annulus')]

ax.legend(handles=legend_elements, loc='lower left', bbox_to_anchor= (1.02, 0.4), ncol=1,
            borderaxespad=0, frameon=True, handlelength=3, fontsize=16)
plt.savefig("Aperture_Image_Full.jpg")

# In[54]:

# Zoomed in:

fig, ax = plt.subplots(figsize=(10,10))
show_image(exproc, cmap='Greys', percl=99.9, fig=fig, ax=ax, show_colorbar=False)
apertures = CircularAperture(target, r=11.);
apertures.plot(color='r', lw=2.5, alpha=1);
apertures = CircularAperture(target, r=12.);
apertures.plot(color='k', ls='--', lw=2, alpha=1);
apertures = CircularAperture(target, r=19.);
apertures.plot(color='k', lw=2, ls='--', alpha=1);

rvals = [11, 11, 11, 11]
for r, compstar in enumerate(comp):
    apertures = CircularAperture(compstar, r=rvals[r+1]);
    apertures.plot(color='xkcd:bright blue', lw=3, alpha=1);
    apertures = CircularAperture(compstar, r=rvals[r+1]+2);
    apertures.plot(color='xkcd:bright pink', lw=2, ls='--', alpha=1);
    apertures = CircularAperture(compstar, r=rvals[r+1]+8);
    apertures.plot(color='xkcd:bright pink', lw=2, ls='--', alpha=1);
plt.xlim(250,750)
plt.ylim(1095,1705)
ax.tick_params(labelsize=18)
plt.xlabel('X pixel', fontsize=20)
plt.ylabel('Y pixel', fontsize=20)
plt.title("Zoomed in Day 1 Apertures", fontsize=20)
legend_elements = [Line2D([0], [0], lw=3, color='xkcd:bright blue', ls='-', label='Calib. star\naperture'),
                   Line2D([0], [0], lw=3, color='xkcd:bright pink', ls='--', label='Calib. star\nBG annulus'),
                   Line2D([0], [0], color='r', lw=3, label='Target aperture'),
                  Line2D([0], [0], color='k', lw=3, label='Target BG annulus')]

# plt.legend(handles=legend_elements, loc='lower left', bbox_to_anchor= (0.04, 1.01), ncol=2,
#             borderaxespad=0, frameon=True, handlelength=3, fontsize=16)
plt.legend(handles=legend_elements, loc='best', fontsize=16)
plt.savefig("day1_apertures_zoomed.jpg")

# In[55]:

# apertures = CircularAperture(ourstars, r=10)

# # testing this method from a website (don't use this one!)

# annulus_apertures = CircularAnnulus(ourstars, r_in=11., r_out=20.)
# aperstats = ApertureStats(exproc.data, annulus_apertures)
# bkg_mean = aperstats.mean
# print(bkg_mean)
# phot_table = aperture_photometry(exproc, apertures)
# for col in phot_table.colnames:
#     phot_table[col].info.format = '%.8g'  # for consistent table output
# print(phot_table)
# aperture_area = apertures.area_overlap(exproc)
# total_bkg = bkg_mean * aperture_area
# phot_bkgsub = phot_table['aperture_sum'].value - total_bkg

# phot_table['total_bkg'] = total_bkg
# phot_table['aperture_sum_bkgsub'] = phot_bkgsub
# for col in phot_table.colnames:
#     phot_table[col].info.format = '%.8g'  # for consistent table output
# print(phot_table)

# In[56]:

import pandas as pd
data = pd.DataFrame(columns=['time', 'star', 'aperture_sum', 'total_bkg_local',
                             'aperture_sum_bkgsub_local', 'aperture_sum_bkgsub_global', 'aperstats_sum'])
apertures = CircularAperture(ourstars, r=11.);
annulus_apertures = CircularAnnulus(ourstars, r_in=12., r_out=19.)
bgmeans = []
bgstds = []

for i in range(len(aimgf1.files)):
    exproc = CCDData.read(aimg_path1/aimgf1.files[i], unit='adu')

    mask = make_source_mask(exproc, nsigma=3, npixels=5, dilate_size=11)
    mean, median, std = sigma_clipped_stats(exproc, sigma=4.0, mask=mask)
    bgmeans.append(mean)
    bgstds.append(std)
    phot_table_local = aperture_photometry(exproc - median, apertures)

    aperstats = ApertureStats(exproc.data, annulus_apertures)
    sums = aperstats.sum
    bkg_mean = aperstats.mean
    print(bkg_mean)
    phot_table = aperture_photometry(exproc, apertures)
    for col in phot_table.colnames:
        phot_table[col].info.format = '%.8g'  # for consistent table output
    aperture_area = apertures.area_overlap(exproc)
    total_bkg = bkg_mean * aperture_area
    phot_bkgsub = phot_table['aperture_sum'].value - total_bkg

    phot_table['total_bkg'] = total_bkg
    phot_table['aperture_sum_bkgsub'] = phot_bkgsub
    for col in phot_table.colnames:
        phot_table[col].info.format = '%.8g'  # for consistent table output

    dat = np.array([time1[i]*np.ones(4), phot_table['id'].value.astype(int), phot_table['aperture_sum'].value.astype(float),
                    phot_table['total_bkg'].value.astype(float), phot_table['aperture_sum_bkgsub'].value.astype(float),
                    phot_table_local['aperture_sum'].value.astype(float), sums]).T
    data = data.append(pd.DataFrame(dat, columns=data.columns))

# In[57]:

bgmeans
phot_table

# In[58]:

binary = data.loc[data.star==1].copy()
comp1 = data.loc[data.star.isin([2,3,4])].copy()

# In[59]:

plt.scatter(binary.time.values, -2.5*np.log10(binary.aperture_sum_bkgsub_local.astype(float)), s=10)

# In[60]:

import astropy.units as u
Vstar2 = 12.00
uVstar2 = 0.21
pstar2 = 1.5002 * u.mas
upstar2 = 0.0129 * u.mas
Vstar3 = 14.69
uVstar3 = 0.08
pstar3 = 0.9215 * u.mas
upstar3 = 0.0207 * u.mas
Vstar4 = 13.15
uVstar4 = 0.05
pstar4 = 1.8988
upstar4 = 0.0183

Vstars = np.array([Vstar2, Vstar3, Vstar4])
uVstars = np.array([uVstar2, uVstar3, uVstar4])
#np.concatenate(np.repeat([Vstars], 120, axis=0))

# In[61]:

[(357,1228), (614,1468), (660,1585)]

# In[62]:

comp1['Amag'] = -2.5 * np.log10(comp1.aperture_sum_bkgsub_local.values.astype(float));
comp1['Vmag'] = np.concatenate(np.repeat([Vstars], 120, axis=0));
comp1['uVmag'] = np.concatenate(np.repeat([uVstars], 120, axis=0));
# comp1['Vmag'] = np.concatenate(np.repeat([Vstars], 212, axis=0));
# comp1['uVmag'] = np.concatenate(np.repeat([uVstars], 212, axis=0));

# In[63]:

fig, ax = plt.subplots()
binmags = - 2.5 * np.log10(binary.aperture_sum_bkgsub_local.values.astype(float)/comp1.loc[comp1.star==2].aperture_sum_bkgsub_local.values.astype(float))
mag = binmags + 12.00
plt.scatter(time1, mag)
ax.invert_yaxis()

# In[64]:

comp1["Vmag"]

# In[65]:

test = comp1.iloc[0:3]
np.mean(test.Vmag+test.Amag)
np.std(test.Vmag+test.Amag)

# In[66]:

dtime.iloc[0].Amag

# In[67]:

mjdt1 = np.array(aimgf1.summary['mjd-obs'].value)
dVavgs = []
corrs = []
corr1 = []
corr2 = []
corr3 = []
for i,t in enumerate(time1):
    dtime = comp1.loc[comp1.time.values==t]
    dV = dtime.Vmag.values - (2.5*np.log10(dtime.aperture_sum_bkgsub_local.values.astype(float)))
    dVl = dtime.Vmag.values - dtime.uVmag.values - (2.5*np.log10(dtime.aperture_sum_bkgsub_local.values.astype(float)))
    dVh = dtime.Vmag.values + dtime.uVmag.values - (2.5*np.log10(dtime.aperture_sum_bkgsub_local.values.astype(float)))
    dVavg = np.mean(dV)
    dVavgs.append(dVavg)
    dVhavg = np.mean(dVh) - dVavg
    dVlavg = dVavg - np.mean(dVl)
    binmag = 2.5 * np.log10(binary.loc[binary.time.values==t].aperture_sum_bkgsub_local.values.astype(float))
    #plt.scatter(t/60, binmag+dVavg, color='k', s=10)
    C = np.mean(-1*dtime.Amag + dtime.Vmag)
    corr1.append(dtime.iloc[0].Vmag-dtime.iloc[0].Amag)
    corr2.append(dtime.iloc[1].Vmag-dtime.iloc[1].Amag)
    corr3.append(dtime.iloc[2].Vmag-dtime.iloc[2].Amag)
    dC = np.sqrt(np.sum(dtime.uVmag**2)) / len(dtime.uVmag)
    corrs.append(C)
dVavgs = np.array(dVavgs)
corrs = np.array(corrs)

# In[68]:

corr1 = np.array(corr1)
corr2 = np.array(corr2)
corr3 = np.array(corr3)
dC

# In[69]:

fig, ax = plt.subplots(figsize=(10,9))
binmags = - 2.5 * np.log10(binary.aperture_sum_bkgsub_local.values.astype(float))
maxmag = (binmags+dVavgs).min()
plt.scatter(time1/60, (binmags+corrs), color='k', s=20)
plt.fill_between(time1/60, (binmags+corrs-dC), (binmags+corrs+dC),
                 zorder=0, color='xkcd:light grey')
plt.xlabel(tobs1[0][:10] + ': ' + 'Hours since '+ tobs1[0][11:] + ' UTC', fontsize=20)
plt.ylabel('Apparent V magnitude', fontsize=20)
ax.tick_params(labelsize=18)
plt.axvline(time1[15]/60, color='r', lw=1, ls='--')
plt.fill_between(np.linspace(time1[13],time1[17],10)/60, 13.9, 15.2, zorder=0, color='r', alpha=0.075)
plt.axvline(149.7/60, color='r', lw=1, ls='--')
plt.axhline((binmags+corrs).min(), zorder=0, color='xkcd:grey', ls='--')
plt.fill_between(np.linspace(time1[60],time1[68],10)/60, 13.9, 15.2, zorder=0, color='r', alpha=0.075)
#plt.axvline(0.16, color='r', lw=1, ls='--')
#plt.fill_between(np.linspace(0.155,0.18,10), 13.9, 15.2, zorder=0, color='r', alpha=0.1)
legend_elements = [Line2D([0], [0], marker='o', color='k', ls='', label='Corrected V\nmagnitude'),
                   Line2D([0], [0], color='xkcd:light grey', lw=10, label='$\sigma(V)$'),
                    Line2D([0], [0], color='r', lw=5, ls='--', label='orbit extremas'),
                  Line2D([0], [0], color='r', lw=10, alpha=0.1, label=r'$\sigma(\rm{extremas})$'),
                  Line2D([0], [0], color='xkcd:grey', lw=5, ls='--', label=r'V$_{\rm{max}}$'+'$\simeq${:.3}'.format((binmags+corrs).min()))]
# ax.legend(handles=legend_elements, loc='best', bbox_to_anchor= (1.01, 0.45), ncol=1,
#             borderaxespad=0, frameon=True, handlelength=3, fontsize=16, markerscale=2)
ax.legend(handles=legend_elements, loc='lower center', ncol=1, frameon=True,
          handlelength=2, fontsize=16, markerscale=2)
plt.ylim(13.95, 15.15)
ax.invert_yaxis()
ax.set_title('Light curve of eclipsing binary J074658', fontsize=22)
plt.savefig("Binary_Light_Curve.png")
print((binmags + corrs).min())

# In[70]:

(149.7-time1[15])*4/60

# In[71]:

5.6/60

# In[72]:

# fig, ax = plt.subplots(1,3,figsize=(20,6))
fig, ax = plt.subplots(3, 1,figsize=(9, 24))

binmags = - 2.5 * np.log10(binary.aperture_sum_bkgsub_local.values.astype(float))
ax[0].scatter(time1/60, (binmags+corr1), color='k', s=20)
ax[0].fill_between(time1/60, (binmags+corr1-0.21), (binmags+corr1+0.21),
                 zorder=0, color='xkcd:light grey')
ax[0].axhline((binmags+corr1).min(), zorder=0, color='xkcd:grey', ls='--')

legend_elements = [Line2D([0], [0], marker='o', color='k', ls='', label='Corrected V\nmagnitude'),
                   Line2D([0], [0], color='xkcd:light grey', lw=10, label='$\sigma(V)$'),
                  Line2D([0], [0], color='xkcd:grey', lw=5, ls='--', label=r'V$_{\rm{max}}$'+'$\simeq${:.3}'.format((binmags+corr1).min()))]
ax[0].legend(handles=legend_elements, loc='lower center', ncol=1, frameon=True,
          handlelength=2, fontsize=16, markerscale=2)

ax[1].scatter(time1/60, (binmags+corr2), color='k', s=20)
ax[1].fill_between(time1/60, (binmags+corr2-0.08), (binmags+corr2+0.08),
                 zorder=0, color='xkcd:light grey')
ax[1].axhline((binmags+corr2).min(), zorder=0, color='xkcd:grey', ls='--')

legend_elements = [Line2D([0], [0], marker='o', color='k', ls='', label='Corrected V\nmagnitude'),
                   Line2D([0], [0], color='xkcd:light grey', lw=2, label='$\sigma(V)$'),
                  Line2D([0], [0], color='xkcd:grey', lw=2, ls='--', label=r'V$_{\rm{max}}$'+'$\simeq${:.3}'.format((binmags+corr2).min()))]
ax[1].legend(handles=legend_elements, loc='lower center', ncol=1, frameon=True,
          handlelength=2, fontsize=16, markerscale=2)

ax[2].scatter(time1/60, (binmags+corr3), color='k', s=20)
ax[2].fill_between(time1/60, (binmags+corr3-0.05), (binmags+corr3+0.05),
                 zorder=0, color='xkcd:light grey')
ax[2].axhline((binmags+corr3).min(), zorder=0, color='xkcd:grey', ls='--')

legend_elements = [Line2D([0], [0], marker='o', color='k', ls='', label='Corrected V\nmagnitude'),
                   Line2D([0], [0], color='xkcd:light grey', lw=10, label='$\sigma(V)$'),
                  Line2D([0], [0], color='xkcd:grey', lw=5, ls='--', label=r'V$_{\rm{max}}$'+'$\simeq${:.3}'.format((binmags+corr3).min()))]
ax[2].legend(handles=legend_elements, loc='lower center', ncol=1, frameon=True,
          handlelength=2, fontsize=16, markerscale=2)

labels=['TYC 1912-1125-1','GSC 01912-01105','GSC 01912-01015']
for i in range(3):
#     ax[i].set_xlabel('Hours since '+ tobs1[0][11:] + ' UTC', fontsize=20)
    ax[i].set_ylabel('Apparent V magnitude', fontsize=20)
    ax[i].tick_params(labelsize=20)
    ax[i].invert_yaxis()
    ax[i].set_title("Light Curve Corrected with {}".format(labels[i]), fontsize=20)
fig.supxlabel('Hours since '+ tobs1[0][11:] + ' UTC', fontsize=30, y=0.08)
# plt.subplots_adjust(wspace=0.3)
plt.savefig("Cal_Stars_Light_Curve.jpg")

# In[ ]:

# In[73]:

# fig, ax = plt.subplots(figsize=(10,8))
# mag_corr = binmags+dVavgs
# # maxmag = (binmags+dVavgs).max()
# maxmag = mag_corr.max()

# binmags = - 2.5 * np.log10(binary.aperture_sum_bkgsub_local.values.astype(float))
# plt.scatter(time1/60/24, (binmags+dVavgs), color='k', s=20)
# plt.fill_between(time1/60/24, (binmags+dVavgs-dVhavg), (binmags+dVavgs+dVhavg),
#                  zorder=0, color='xkcd:light grey')
# plt.xlabel(tobs1[0][:10] + ': ' + 'Days since '+ tobs1[0][11:] + ' UTC', fontsize=20)
# plt.ylabel('Apparent V magnitude', fontsize=20)
# ax.tick_params(labelsize=18)
# plt.axvline(time1[15]/60/24, color='r', lw=1, ls='--')
# plt.fill_between(np.linspace(time1[13],time1[17],10)/60/24, 11.5,12.5, zorder=0, color='r', alpha=0.075)
# plt.axvline(149.7/60/24, color='r', lw=1, ls='--')
# plt.axhline(mag_corr.max(), zorder=0, color='xkcd:grey', ls='--')
# plt.fill_between(np.linspace(time1[60],time1[68],10)/60/24, 11.5,12.5, zorder=0, color='r', alpha=0.075)
# #plt.axvline(0.16, color='r', lw=1, ls='--')
# #plt.fill_between(np.linspace(0.155,0.18,10), 11.5,12.5, zorder=0, color='r', alpha=0.1)
# legend_elements = [Line2D([0], [0], marker='o', color='k', ls='', label='Corrected V\nmagnitude'),
#                    Line2D([0], [0], color='xkcd:light grey', lw=10, label='$\sigma(V)$'),
#                     Line2D([0], [0], color='r', lw=5, ls='--', label='orbit extremas'),
#                   Line2D([0], [0], color='r', lw=10, alpha=0.1, label=r'$\sigma(\rm{extremas})$'),
#                   Line2D([0], [0], color='xkcd:grey', lw=5, ls='--', label=r'V$_{\rm{max}}$'+'$\simeq${:.3}'.format(mag_corr.max()))]
# # plt.ylim(11.5,12.5)
# ax.legend(handles=legend_elements, loc='lower left', bbox_to_anchor= (1.01, 0.45), ncol=1,
#             borderaxespad=0, frameon=True, handlelength=3, fontsize=16, markerscale=2)

# In[74]:

time1[63]/60/24

# In[75]:

(time1[67]+time1[62])/2/60/24

# In[76]:

mag_corr = binmags + corrs

# In[77]:

from scipy.optimize import curve_fit
from scipy import asarray as ar,exp

x = time1[0:33]/60 #np.arange(-30, 31, step) #
y = mag_corr[0:33]#azP

n = len(x)                          #the number of data
mean = np.mean(x)            #note this correction
# sigma = sum(y*(x-mean)**2)/n        #note this correction
sigma = np.std(x)

def gaus(x,a,x0,sigma,c):
#     return a * (x-x0)**2 +c
    return a*np.exp(-(x-x0)**2/(2*sigma**2)) + c

popt2,pcov2 = curve_fit(gaus,x,y, p0=[1, mean, sigma, 14.2])
print(popt2)

fig, ax = plt.subplots(figsize=(10,8))
plt.scatter(x,y,color='k',label='data',s=10)
plt.plot(np.linspace(x.min(),x.max(),1000),
         gaus(np.linspace(x.min(),x.max(),1000),*popt2),color='r',ls='--',label='fit', lw=1.0)
ax.legend(loc="best", fontsize=20, markerscale=2)
ax.invert_yaxis()
plt.title('Fit for "peak" around t = {:.4} hours'.format(time1[15]/60), fontsize=25)
plt.xlabel('Time (days)', fontsize=20)
plt.ylabel('Apparent V magnitude', fontsize=20)
plt.savefig("day1_1st_lc_peak_fit.jpg")
plt.show()

# In[78]:

pcov2
perr2 = np.sqrt(np.diag(pcov2))
perr2

# In[79]:

from scipy.optimize import curve_fit
from scipy import asarray as ar,exp

x = time1[15:110]/60/24 #np.arange(-30, 31, step) #
y = mag_corr[15:110]#azP

n = len(x)                          #the number of data
mean = np.mean(x)            #note this correction
print(mean)
# sigma = sum(y*(x-mean)**2)/n        #note this correction
sigma = np.std(x)

def gaus(x,a,x0,sigma,c):
    return a*np.exp(-(x-x0)**2/(2*sigma**2)) + c

popt,pcov = curve_fit(gaus, x, y, p0=[-1, mean, sigma, 14.6])
print(popt)

fig, ax = plt.subplots(figsize=(10,8))
plt.scatter(x,y,color='k',label='data',s=10)
plt.plot(np.linspace(x[0], x[-1],1000),
         gaus(np.linspace(x[0], x[-1],1000),*popt),color='r',ls='--',label='fit', lw=1.0)
plt.legend(loc="best", fontsize=20, markerscale=2)
ax.invert_yaxis()
plt.title('Fit for peak Centred Near 2.5 Hours', fontsize=25)
plt.xlabel('Time (days)', fontsize=20)
plt.ylabel('Apparent V magnitude', fontsize=20)
plt.savefig("day1_2nd_lc_peak_fit.jpg")
plt.show()

# In[80]:

pcov
perr = np.sqrt(np.abs(np.diag(pcov)))
perr

# In[81]:

(time1[62]-time1[15])/60/24*4

# In[96]:

# Calculating Luminosities of stars from Light curve data
au = 1.495978707e11 # m
p = 2.74e-3
up = 0.02e-3

A_plaskett = np.pi * (1.8 / 2) ** 2
D_bin = 206264 / p * au
uD_bin = (206264 / p) *  up * au

K = A_plaskett / D_bin ** 2
uK = K * 2 / (D_bin) * uD_bin

Vmin1 = mag_corr.max()
uVmin = dC
Vmin2 = mag_corr[0:33].max()
Vmax = mag_corr.min()

L1 = 4 / K * 10 ** (-1 * Vmin1 / 2.5) # W
uL1 = np.sqrt((L1 / K * uK) ** 2 + (4 / K * 2 * np.log(10) / (5 * 10 ** (-1 * Vmin1 / 2.5)) * uVmin) ** 2) # W

L2 = 4 / K * (10 ** (-1 * Vmax / 2.5) - 10 ** (-1 * Vmin1 / 2.5)) # W

uL2 = np.sqrt((L2 / K * uK) ** 2 + (4 / K * 2 * np.log(10) / (5 * 10 ** (-1 * Vmin1 / 2.5)) * uVmin) ** 2 + (4 / K * 2 * np.log(10) / (5 * 10 ** (-1 * Vmax / 2.5)) * uVmin) ** 2) # W

f = open("L_and_M.txt", 'w')
L = []
L1string = "The Luminosity of 1 is {} \pm {} Watts \n".format(L1, uL1)
Vm1string = "The first V min is {} \pm {} \n".format(Vmin1, uVmin)
L2string = "The Luminosity of 2 is {} \pm {} Watts \n".format(L2, uL2)
Vm2string = "The second V min is {} \pm {} \n".format(Vmin2, uVmin)
Vmaxstring = "Max V is {} \pm {} \n".format(Vmax, uVmin)
D_binstring = "The distance to the binary is {} \pm {} metres \n".format(D_bin, uD_bin)
Kstring = "The constant K is {} \pm {} \n".format(K, uK)

L.append(D_binstring)
L.append(Kstring)
L.append(Vm1string)
L.append(Vm2string)
L.append(Vmaxstring)
L.append(L1string)
L.append(L2string)

Lsun = 3.828e26 # W
Msun = 1.9885e30 # kg

M1 = Msun * (L1 / (1.4 * Lsun)) ** (2 / 7)
uM1 =  (2 / 7) * Msun * (L1 / (1.4 * Lsun)) ** (-5 / 7) * (1 / (1.4 * Lsun)) * uL1

M2 = Msun * (L2 / (1.4 * Lsun)) ** (2 / 7)
uM2 =  (2 / 7) * Msun * (L2 / (1.4 * Lsun)) ** (-5 / 7) * (1 / (1.4 * Lsun)) * uL2

q = M2 / M1
uq = np.sqrt((1 / M1 * uM2) ** 2 + (-1 * M2 / (M1 ** 2) * uM2) ** 2)

M1string = "Star 1's mass is approximately {} \pm {} kg \n".format(M1, uM1)
M2string = "Star 2's mass is approximately {} \pm {} kg \n".format(M2, uM2)

qstring = "the ratio of the masses, q = M2 / M1 is {} \pm {} \n".format(q, uq)

L.append(M1string)
L.append(M2string)
L.append(qstring)

f.writelines(L)
f.close()

# In[97]:

print(L)

# ## Day 2:

# In[37]:

aimgf2.summary

# In[38]:

# Initial guess:
target2 = (590,1125)
comp2 = [(332,1166), (589,1406), (636,1523)]
ourstars2 = [(590,1125), (332,1166), (589,1406), (636,1523)]

# In[39]:

exproc = CCDData.read(aimg_path2/aimgf2.files[0], unit='adu')
fig, ax = plt.subplots(figsize=(10,10))
show_image(exproc, cmap='Greys', percl=99.9, fig=fig, ax=ax, show_colorbar=False)
apertures = CircularAperture(target, r=11.);
apertures.plot(color='r', lw=2.5, alpha=1);
apertures = CircularAperture(target, r=12.);
apertures.plot(color='xkcd:black', ls='--', lw=1.5, alpha=1);
apertures = CircularAperture(target, r=19.);
apertures.plot(color='xkcd:black', lw=1.5, ls='--', alpha=1);

rvals = [11, 11, 11, 11]
for r, compstar in enumerate(comp2):
    apertures = CircularAperture(compstar, r=rvals[r]);
    apertures.plot(color='xkcd:bright blue', lw=3, alpha=1);
    apertures = CircularAperture(compstar, r=rvals[r]+2);
    apertures.plot(color='xkcd:bright pink', lw=2, ls='--', alpha=1);
    apertures = CircularAperture(compstar, r=rvals[r]+8);
    apertures.plot(color='xkcd:bright pink', lw=2, ls='--', alpha=1);
#plt.xlim(250,750)
plt.ylim(top=2200)
ax.tick_params(labelsize=18)
plt.xlabel('X pixel', fontsize=20)
plt.ylabel('Y pixel', fontsize=20)

legend_elements = [Line2D([0], [0], lw=3, color='xkcd:bright blue', ls='-', label='Calib. star\naperture'),
                   Line2D([0], [0], lw=3, color='xkcd:bright pink', ls='--', label='Calib. star\nBG annulus'),
                   Line2D([0], [0], color='r', lw=3, label='Target aperture'),
                  Line2D([0], [0], color='k', lw=3, label='Target BG annulus')]

ax.legend(handles=legend_elements, loc='lower left', bbox_to_anchor= (1.02, 0.4), ncol=1,
            borderaxespad=0, frameon=True, handlelength=3, fontsize=16)

# In[40]:

# Zoomed in:

fig, ax = plt.subplots(figsize=(10,10))
show_image(exproc, cmap='Greys', percl=99.9, fig=fig, ax=ax, show_colorbar=False)
apertures = CircularAperture(target, r=11.);
apertures.plot(color='r', lw=2.5, alpha=1);
apertures = CircularAperture(target, r=12.);
apertures.plot(color='k', ls='--', lw=2, alpha=1);
apertures = CircularAperture(target, r=19.);
apertures.plot(color='k', lw=2, ls='--', alpha=1);

rvals = [11, 11, 11, 11]
for r, compstar in enumerate(comp):
    apertures = CircularAperture(compstar, r=rvals[r+1]);
    apertures.plot(color='xkcd:bright blue', lw=3, alpha=1);
    apertures = CircularAperture(compstar, r=rvals[r+1]+2);
    apertures.plot(color='xkcd:bright pink', lw=2, ls='--', alpha=1);
    apertures = CircularAperture(compstar, r=rvals[r+1]+8);
    apertures.plot(color='xkcd:bright pink', lw=2, ls='--', alpha=1);
plt.xlim(250,750)
plt.ylim(1000,1650)
ax.tick_params(labelsize=18)
plt.xlabel('X pixel', fontsize=20)
plt.ylabel('Y pixel', fontsize=20)

legend_elements = [Line2D([0], [0], lw=3, color='xkcd:bright blue', ls='-', label='Calib. star\naperture'),
                   Line2D([0], [0], lw=3, color='xkcd:bright pink', ls='--', label='Calib. star\nBG annulus'),
                   Line2D([0], [0], color='r', lw=3, label='Target aperture'),
                  Line2D([0], [0], color='k', lw=3, label='Target BG annulus')]

ax.legend(handles=legend_elements, loc='lower left', bbox_to_anchor= (0.01, 1.01), ncol=2,
            borderaxespad=0, frameon=True, handlelength=3, fontsize=16)

# In[41]:

phot_table

# In[42]:

aimg_path2

# In[43]:

import pandas as pd
data2 = pd.DataFrame(columns=['time', 'star', 'aperture_sum', 'total_bkg_local',
                             'aperture_sum_bkgsub_local', 'aperture_sum_bkgsub_global', 'aperstats_sum'])
apertures = CircularAperture(ourstars2, r=11.);
annulus_apertures = CircularAnnulus(ourstars2, r_in=12., r_out=19.)

for i in range(len(aimgf2.files)):
    exproc = CCDData.read(aimg_path2/aimgf2.files[i], unit='adu')

    mask = make_source_mask(exproc, nsigma=3, npixels=5, dilate_size=11)
    mean, median, std = sigma_clipped_stats(exproc, sigma=3.0, mask=mask)
    phot_table_local = aperture_photometry(exproc - median, apertures)

    aperstats = ApertureStats(exproc.data, annulus_apertures)
    sums = aperstats.sum
    bkg_mean = aperstats.median
    phot_table = aperture_photometry(exproc, apertures)
    for col in phot_table.colnames:
        phot_table[col].info.format = '%.8g'  # for consistent table output
    aperture_area = apertures.area_overlap(exproc)
    total_bkg = bkg_mean * aperture_area
    phot_bkgsub = phot_table['aperture_sum'].value - total_bkg

    phot_table['total_bkg'] = total_bkg
    phot_table['aperture_sum_bkgsub'] = phot_bkgsub
    for col in phot_table.colnames:
        phot_table[col].info.format = '%.8g'  # for consistent table output

    dat = np.array([time2[i]*np.ones(4), phot_table['id'].value.astype(int), phot_table['aperture_sum'].value.astype(float),
                    phot_table['total_bkg'].value.astype(float), phot_table['aperture_sum_bkgsub'].value.astype(float),
                    phot_table_local['aperture_sum'].value.astype(float), sums]).T
    data2 = data2.append(pd.DataFrame(dat, columns=data2.columns))

# In[44]:

binary2 = data2.loc[data2.star==1]
comp2 = data2.loc[data2.star.isin([2,3,4])]

# In[45]:

plt.scatter(binary2.time.values, -2.5*np.log10(binary2.aperture_sum_bkgsub_local.astype(float)), s=10)

# In[46]:

mjdt1 = np.array(aimgf1.summary['mjd-obs'].value)
dVavgs = []
fig, ax = plt.subplots(figsize=(10,8))
for i,t in enumerate(time2):
    dtime = comp2.loc[comp2.time.values==t]
    dV = dtime.Vmag.values - (2.5*np.log10(dtime.aperstats_sum.values.astype(float)))
    dVl = dtime.Vmag.values - dtime.uVmag.values - (2.5*np.log10(dtime.aperstats_sum.values.astype(float)))
    dVh = dtime.Vmag.values + dtime.uVmag.values - (2.5*np.log10(dtime.aperstats_sum.values.astype(float)))
    dVavg = np.mean(dV)
    dVavgs.append(dVavg)
    dVhavg = np.mean(dVh) - dVavg
    dVlavg = dVavg - np.mean(dVl)
    binmag = 2.5 * np.log10(binary2.loc[binary2.time.values==t].aperstats_sum.values.astype(float))
    #plt.scatter(t/60, binmag+dVavg, color='k', s=10)
binmags = 2.5 * np.log10(binary2.aperstats_sum.values.astype(float))
dVavgs = np.array(dVavgs)
maxmag = (binmags+dVavgs).max()
plt.scatter(time2/60, (binmags-dVavgs), color='k', s=20)
#plt.fill_between(time2/60, (binmags+dVavgs-dVhavg), (binmags+dVavgs+dVhavg), zorder=0, color='xkcd:light grey')
plt.xlabel(tobs1[0][:10] + ': ' + 'Hours since '+ tobs1[0][11:] + ' UTC', fontsize=20)
plt.ylabel('Apparent V magnitude', fontsize=20)
ax.tick_params(labelsize=18)

legend_elements = [Line2D([0], [0], marker='o', color='k', ls='', label='Corrected V\nmagnitude'),
                   Line2D([0], [0], color='xkcd:light grey', lw=10, label='$\sigma(V)$')]

#ax.legend(handles=legend_elements, loc='upper left', fontsize=18,markerscale=2)

# In[ ]:

comp2['Amag'] = -2.5 * np.log10(comp2.aperstats_sum.values.astype(float));
comp2['Vmag'] = np.concatenate(np.repeat([Vstars], 39, axis=0));
comp2['uVmag'] = np.concatenate(np.repeat([uVstars], 39, axis=0));


from odf import easyliststyle
from odf.opendocument import OpenDocumentText
from odf.style import Style, TextProperties
from odf.text import P, List, ListItem

"""
This program shows the easyliststyle.py module.
It creates a file named "easylist_odfpy.odt"
with a bulleted list, a numbered list, and a
mixed list.
"""

bulletListSpec = '*,>,#,%'
mixedListSpec = u'1.!\u273f!a)'
numberListSpecArray = ('I', '1:', 'a')

itemList = (
    "Cats",
    ">Domestic Shorthair",
    ">Domestic Longhair",
    ">Purebred",
    ">>Russian Blue",
    ">>Siamese",
    ">>>Seal Point",
     ">>>Flame Point",
    "Dogs",
    ">Retrievers",
    ">>Golden Retriever",
    ">>Labrador Retriever",
    ">Poodles",
    ">>Toy Poodle",
    ">>Standard Poodle"
)

def createList(itemList, indentDelim, styleName):
    listArray = []
    listItem = ListItem()
    level = 0
    lastLevel = 0

    for levCount in range(0,10):
        listArray.append(None)
    listArray[0] = List()

    for item in itemList:
        level = 0;
        while (level < len(item) and item[level] == indentDelim):
            level +=1
        item = item[level:]

        if (level > lastLevel):    # open the sub-levels
            for levCount in range(lastLevel+1, level+1):
                listArray[levCount] = List()
        elif (level < lastLevel):    # close off the intervening lists
            for levCount in range(lastLevel, level, -1):
                listArray[levCount-1].childNodes[-1].addElement(listArray[levCount])

        # now that we are at the proper level, add the item.
        listArray[level].setAttribute( 'stylename', styleName );
        listItem = ListItem()
        para = P(text=item);
        listItem.addElement(para);
        listArray[level].addElement(listItem);
        lastLevel = level;

    # close off any remaining open lists
    for levCount in range(lastLevel, 0, -1):
        listArray[levCount-1].childNodes[-1].addElement(listArray[levCount])
    return listArray[0]

textdoc = OpenDocumentText()

s = textdoc.styles
listStyle = easyliststyle.styleFromString('bullet1', bulletListSpec,
    ',', '0.6cm', easyliststyle.SHOW_ONE_LEVEL)
s.addElement(listStyle)

listElement = createList(itemList, '>', 'bullet1')
textdoc.text.addElement(listElement)

para = P(text="-----------------------");
textdoc.text.addElement(para)

listStyle = easyliststyle.styleFromList('num1', numberListSpecArray,
    '0.25in', easyliststyle.SHOW_ALL_LEVELS)
s.addElement(listStyle)

listElement = createList(itemList, '>', 'num1')
textdoc.text.addElement(listElement)

para = P(text="-----------------------");
textdoc.text.addElement(para)

listStyle = easyliststyle.styleFromString('mix1', mixedListSpec,
    '!', '0.8cm', easyliststyle.SHOW_ONE_LEVEL)
s.addElement(listStyle)

listElement = createList(itemList, '>', 'mix1')
textdoc.text.addElement(listElement)

textdoc.save("easylist_odfpy.odt")

# vim: set expandtab sw=4 :


# names=["nana","weiwei","yazheng","yanmei"]
# lon=len(names)
# for i in range(0,lon):
#     #print(names[i])

# pep=['xue','meizhi','junmei','lina']
# bulai='junmei'
# pep.remove('junmei')
# pep.append('yanxia')
# lon=len(pep)
# print(bulai+" will not come, what a pity!")
# print("I found a bigger desk !")
# pep.insert(0,'tingting')
# pep.insert(int(lon/2),'siyao')
# pep.append('wenwen')
# lon2=len(pep)
# for i in range(0,lon2):
#     print("I want to invite ",pep[i].title()," to have dinner today.")
# print("I want to invite ",len(pep)," people to have dinner today.")
#
# print("I could only invite two of them !")
# while len(pep) > 2:
#     p = pep.pop()
#     print("I am sorry that I couldn't invite ",p," to have dinner today.")
# lon3=len(pep)
# for i in range(0,lon3):
#     print("hey you can still come to the dinner " , pep[i].title())
# while pep:
#     del pep[0]
# print(pep)

#3-8、9、10
#
# place=['yunnan','lanzhou','xizang','qinghai','xinjiang','xiamen','taiwan']
# print(place)
# print(sorted(place))
# print(place)
# place.reverse()
# print(place)
# place.reverse()
# print(place)
# place.sort()
# print(place)
# place.sort()
# print(place）

#4-3、4、5、6、7、8、9

# for i in range(1,21):
#     print(i)

# list1=range(1,1000001)
# print(min(list1))
# print(max(list1))
# print(sum(list1))

#
# list1=range(1,20,2)
# for i in list1:
#     print(i)

#
# dig=[]
# for i in range(3,30):
#     if i%3==0:
#        dig.append(i)
#
# for i in dig:
#     print(i)

#
# list1=[dig**3 for dig in range(1,11)]
# print(list1)

#4-10\11\12

# place=['taiwan','riben','xizang','qinghai','shanghai','yunnan']
# print('The first three items are : ',place[0:3])
# print('The items from the middle of the list are : ',place[1:4])
# print('The last three items in the list are : ',place[-3:])
#
# place1=place[:]
# place1.append('qinghai')
# print('I want travel to :')
# for i in place:
#     print(i)
#
# print('my friends want to travel to :')
# for i in place1:
#     print(i)

#4-13

# foods=('baozi','dabing','miantiao','lvhuo','roujiamo')
# for food in foods:
#     print(food)
#
# #foods[2]='mantou'  #错误代码
#
# foods=('zhou','tang','miantiao','lvhuo','roujiamo')
# for food in foods:
#     print(food)

#5-3\4\5\6\7

# alien_color='green'
# if alien_color == 'green':
#     print("you got 5 points!")
# elif alien_color == 'yello':
#     print("you got 10 points!")
# elif alien_color == 'red':
#     print("you got 15 points!")
#
# fruits=['apple','banana','grape']
# if 'apple' in fruits:
#     print('You really like apples !')
# if 'banana' in fruits:
#     print('You really like bananas !')

#5-10\11
# current_users=['mingming','lilei','hongmei','sunshaoping','tianxiaoxia']
# new_users=['Sunshaoping','Tianxiaoxia','fengtang','yuhua','muxin']
#
# for new_user in new_users:
#     if new_user.lower() in current_users:
#         print(new_user,'already used !')
#     else:
#         print(new_user,'is OK !')

#
# num=[1,2,3,4,5,6,7,8,9]
# for i in num:
#     if i == 1:
#         print("1st")
#     elif i == 2:
#         print('2nd')
#     elif i == 3:
#         print('3rd')
#     else:
#         print(i,'th',sep='')

#6-1\2\3
# xue={'name':'xue','age':'29','city':'Tianjin'}
# # print(xue)
# # num={'xue':8,'di':8,'fang':6,'wen':9,'na':5}
# # print("xue's favourite num is",num['xue'],'.\n'
# #     "di's favourite num is", num['di'], '.\n'
# #     "fang's favourite num is", num['fang'], '.\n'
# #     "wen's favourite num is", num['wen'], '.\n'
# #     "na's favourite num is", num['na'], '.\n')

#6-5\6
# rivers={'nile': 'egypt','changjaing':'china','huangnhe':'zhonghua'}
# for key in rivers.keys():
#     print('The', key,'runs through ',rivers[key])
#
# for name in rivers.keys():
#     print(name)
#
# for value in rivers.values():
#     print(value)

# namelist=['mingming','lilei','hongmei','sunshaoping','tianxiaoxia']
# namein={'1':'mingming','2':'lilei'}
# for name in namelist:
#     if name in namein.values():
#         print(name,"thank you !")
#     else:
#         print(name,"please come to join us !")

#6-7\8\9\10
#
# xue={'name':'xue','age':'29','city':'Tianjin'}
# zheng={'name':'zheng','age':'20','city':'xilinhaote'}
# zhi={'name':'zhi','age':'32','city':'Tianjin'}
#
# people=[xue,zheng,zhi]
#
# for pe in people:
#     print(pe)
#
#
# favorite_places={
#     'wang': ['yingguo','meiguo','aodaliya'],
#     'liu': ['hainan','sichuan','shandong'],
# }
#
# for name,places in favorite_places.items():
#     print(name,"'s favourite places are:",sep='')
#     for place in places:
#         print(place)

# cities={
#     'tianjin':{'nation':'zhongguo','renkou':'100','shishi':'dougen'},
#     'beijing':{'nation':'china','renkou':'1000','shishi':'zhuai'},
#     'shanghai':{'nation':'zhongguo','renkou':'10000','shishi':'dese'}
#     }
#
# for city,dics in cities.items():
#     print(city,"is a city that:")
#     nation = dics['nation']
#     renkou = dics['renkou']
#     shishi = dics['shishi']
#     print('nation is :',nation,'renkou is :',renkou,'shishi is :',shishi,'\n')

#7-1、2、3、

car=input('what car do you want to rent ?')
print('Let me see if I can find you a',car)

cus=int(input('how many people are having lunch ?'))
if cus > 8:
    print('please wait !')
else:
    print('you can come in.')

num=int(input('please input a int number :'))
if num % 10 ==0:
    print('yes')
else:
    print('no')

ing="please input what you need to make a pizza :"
ing += "\nif you want to quit , please input 'quit'.\n"

while True:
    a = input(ing)
    if a != 'quit':
        print("we will input ",a,"to your pizza.")
    else:
        break

tip='please input your age:'
tip += "\nif you want to quit , please input '0'.\n"

while True:
    age = int(input(tip))
    if age >0 and age < 3:
        print('free')
    elif age >= 3 and age <= 12:
        print('10 dollars')
    elif age > 12:
        print("15 dollars")
    else:
        break


import argparse
import os.path

class ComLine():
	'Class for implementing command line options'

	def __init__(self, args):
		parser = argparse.ArgumentParser()
		parser.add_argument("-r", "--results",
							dest='results',
							default="results.zip",
							help="Provide the results.zip file from your admixpipe run. Specifying the name is only necessary if you have changed it from the default results.zip."
		)
		parser.add_argument("-l", "--ll",
							dest='ll',
							default="ll_all.txt",
							help="Provide the tab delmimited file of log likelihood values. Specifying the name is only necessary if you have changed it from the default ll_all.txt from the distructRerun.py module.."
		)
		parser.add_argument("-p", "--prefix",
							dest='prefix',
							help="Specify the prefix from your admixpipe run. This will be the same name as your VCF file without the .vcf file extension. Required for -w and -M options."
		)
		parser.add_argument("-e", "--email",
							dest='email',
							help="Specify your email address for the CLUMPAK submission form. Required for -w option."
		)
		parser.add_argument("-m", "--MCL",
							dest='MCL',
							#default=1.0,
							type=float,
							help="[optional] Provide user-defined MCL threshold for similarity scores. Must be >=0 and <=0.99."
		)
		parser.add_argument("-d", "--DISTRUCT",
							dest='DISTRUCT',
							#default=1.0,
							type=float,
							help="[optional] Provide user-defined DISTRUCT threshold for minimal. Must be >=0 and <=0.95."
		)
		parser.add_argument("-w", "--web",
							dest='web',
							action='store_true',
							help="Submit to CLUMPAK server instead of running locally."
		)
		parser.add_argument("-M", "--mainpipeline",
							dest='mainpipeline',
							action='store_true',
							help="Run CLUMPAK main pipeline locally."
		)
		parser.add_argument("-b", "--bestk",
							dest='bestk',
							action='store_true',
							help="Run CLUMPAK BestK pipeline locally."
		)

		self.args = parser.parse_args()

		booleans=[self.args.web, self.args.mainpipeline, self.args.bestk]
		if self.xOrMoreAreTrue(booleans) > 1:
			print("Only one CLUMPAK pipeline option can be used at once (-b, -M, or -w).")
			print("Exiting program...")
			print("")
			raise SystemExit
		elif any(booleans):
			if self.args.web:
				print("Attempting to submit to CLUMPAK web server.")
				#check if files exist
				self.exists( self.args.results )
				if not self.args.email:
					print("Must specify email address with -e option.")
					print("")
					raise SystemExit
				if not self.args.prefix:
					print("Must specify prefix of your input .vcf or PLINK files with -p option.")
					print("")
					raise SystemExit
			if self.args.mainpipeline:
				print("Running CLUMPAK main pipeline locally.")
				#check if files exist
				self.exists( self.args.results )
				if not self.args.prefix:
					print("Must specify prefix of your input .vcf or PLINK files with -p option.")
					print("")
					raise SystemExit
			if self.args.bestk:
				print("Running CLUMPAK bestk pipeline locally.")
				#check if files exist
				self.exists( self.args.ll )
		else:
			print("ERROR: Must specify one of the CLUMPAK pipeline options (-b, -M, or -w).")
			print("Exiting program...")
			print("")
			raise SystemExit

	def exists(self, filename):
		if( os.path.isfile(filename) != True ):
			print( filename, "does not exist" )
			print( "Exiting program..." )
			print( "" );
			raise SystemExit

	def xOrMoreAreTrue(self, booleans):
		count = 0
		for boolean in booleans:
			if(boolean):
				count += 1
		return count