from astropy.io import fits
import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate
from scipy.ndimage import interpolation
import scipy.optimize as opt
from photutils.centroids import centroid_com, centroid_2dg  # centroid_quadratic,

def rot(image, x0, y0, angle=0., reshape=True):
    # https://stackoverflow.com/questions/46657423/rotated-image-coordinates-after-scipy-ndimage-interpolation-rotate
    image_rotated = interpolation.rotate(image, angle=angle, reshape=reshape)  # CORRECT ROTATION! Now center it ahh
    # im_rot = rotate(image,angle)
    original_center = (np.array(image.shape[:2][::-1])-1)/2.
    rotated_center = (np.array(image_rotated.shape[:2][::-1])-1)/2.
    original_offset = [x0, y0] - original_center
    a = np.deg2rad(angle)
    new_offset = np.array([original_offset[0]*np.cos(a) + original_offset[1]*np.sin(a),
                           -original_offset[0]*np.sin(a) + original_offset[1]*np.cos(a)])
    new_point = new_offset + rotated_center
    return image_rotated, new_point

# 2D GAUSSIAN!
def twoD_Gaussian(coords, amplitude, xo, yo, sigma_x, sigma_y, theta, offset):
    x, y = coords
    a = (np.cos(theta)**2)/(2*sigma_x**2) + (np.sin(theta)**2)/(2*sigma_y**2)
    b = -(np.sin(2*theta))/(4*sigma_x**2) + (np.sin(2*theta))/(4*sigma_y**2)
    c = (np.sin(theta)**2)/(2*sigma_x**2) + (np.cos(theta)**2)/(2*sigma_y**2)
    g = offset + amplitude*np.exp( - (a*((x-xo)**2) + 2*b*(x-xo)*(y-yo) + c*((y-yo)**2)))

    return g.ravel()

def calc_noise(wht_data, sci_data, pxfs, x0, y0, xlen, ylen, nrow, ncol, amp=None, xstar=None, ystar=None, vmax=700.):
    import scipy.optimize as opt

    normwt = []
    for wd in range(len(wht_data)):
        unc_reg = wht_data[wd][y0-ylen:y0+ylen, x0-xlen:x0+xlen]
        noise = np.std(unc_reg, ddof=1)
        print(noise, noise / np.median(unc_reg))
        normwt.append(noise / np.median(unc_reg))

    #plt.imshow(wht_data, origin='lower')
    #plt.colorbar()
    #plt.plot(x0, y0, 'r*')
    #plt.show()

    if len(wht_data) == 1:
        plt.imshow(sci_data[0][ystar-15:ystar+15, xstar-15:xstar+15], origin='lower', vmax=vmax, vmin=0.)  # vmax=20.
        # 1934, 1118 --> need y, x -> 1118, 1934
        # amp = 37 for PGC 11179, 700 for NGC 384
        initial_guess = (amp, 15, 15, 2, 2, 0., 0.)  # amplitude, x0, y0, x_fwhm, y_fwhm, theta in radians, offset

        x = np.linspace(0, 30, 30)
        y = np.linspace(0, 30, 30)
        x, y = np.meshgrid(x, y)

        popt, pcov = opt.curve_fit(twoD_Gaussian, (x, y), sci_data[0][ystar-15:ystar+15, xstar-15:xstar+15].ravel(),
                                   p0=initial_guess)
        fwhms = [popt[3], popt[4]]
        print(popt)
        datafit = twoD_Gaussian((x,y), *popt)
        plt.contour(x,y, datafit.reshape(30,30), 4, colors='w')

        plt.text(10, 25, 'pxf' + pxfs[0], color='w')
        plt.text(1, 20, 'rms_dev/median=' + str(round(normwt[0],4)), color='w')
        plt.text(1, 5, 'xfwhm=' + str(round(fwhms[0],3)) + ', yfwhm=' + str(round(fwhms[1],3)), color='w')
        plt.show()

    else:
        # amp = 37 for PGC 11179, 700 for NGC 384
        initial_guess = (amp, 15, 15, 2, 2, 0., 0.)  # amplitude, x0, y0, x_fwhm, y_fwhm, theta in radians, offset

        x = np.linspace(0, 30, 30)
        y = np.linspace(0, 30, 30)
        x, y = np.meshgrid(x, y)

        fig, ax = plt.subplots(nrow, ncol, sharex=True, sharey=True, figsize=(8,8.25))
        plt.subplots_adjust(hspace=0., wspace=0.)
        for sd in range(len(sci_data)):
            if sd < ncol:
                row = 0
                col = sd
            else:
                row = 1
                col = sd - ncol
            ax[row, col].imshow(sci_data[sd][ystar - 15:ystar + 15, xstar - 15:xstar + 15], origin='lower', vmax=vmax,
                                vmin=0.)  # vmax = 20.
            # 1934, 1118 --> need y, x -> 1118, 1934
            # 1282:1312, 1153:1183

            popt, pcov = opt.curve_fit(twoD_Gaussian, (x, y),
                                       sci_data[sd][ystar-15:ystar+15, xstar-15:xstar+15].ravel(), p0=initial_guess)
            # 1282:1312, 1153:1183
            fwhms = [popt[3], popt[4]]
            print(popt)
            datafit = twoD_Gaussian((x,y), *popt)
            ax[row,col].contour(x,y, datafit.reshape(30,30), 4, colors='w')

            ax[row, col].text(10, 25, 'pxf' + pxfs[sd], color='w')
            ax[row, col].text(1, 20, 'rms_dev/median=' + str(round(normwt[sd],4)), color='w')
            ax[row, col].text(1, 5, 'xfwhm=' + str(round(fwhms[0],3)) + ', yfwhm=' + str(round(fwhms[1],3)), color='w')
        # plt.imshow(sci_data[1282:1312, 1153:1183], origin='lower', vmax=37.)
        #plt.colorbar()
        plt.show()

def find_center(idata, hdata):
    # '''  #
    '''  #
    # CENTROIDS: H-band:
    # h_data[np.abs(h_data) > 100] = 0.
    plt.imshow(h_data, origin='lower', vmax=4.5e5, vmin=0.)
    plt.colorbar()
    plt.plot(1390+76, 1325+73, 'r*')  # PGC 11179
    #plt.plot(1375+82, 1290+77, 'r*')  # from idx  # NGC 384
    #plt.plot(1375+82.034056318563543, 1290+76.753908662648286, 'c*')  # from centroid  # NGC 384
    plt.show()
    plt.imshow(i_data, origin='lower', vmax=100., vmin=0.)
    plt.colorbar()
    #plt.plot(1510+71, 1575+72, 'r*')  # from idx
    #plt.plot(1510+71.078015258901999, 1575+72.940969820368124, 'c*')  # from centroid
    plt.show()
    print(oop)
    #  '''
    # '''  #

    idata = np.nan_to_num(idata)

    #plt.imshow(idata, origin='lower', vmax=130)
    #plt.colorbar()
    #plt.plot(1927, 1922, 'w*')  # max
    #plt.plot(1875+52.13975768026607, 1875+47.180317370401404, 'c+')  # centroid matches max pretty well!!
    #plt.plot(1875+52.6664093, 1875+47.6567030, 'r*')  # 2D gaussian (not great)
    #plt.show()
    #print(oop)

    idata_small = idata[1875:1975, 1875:1975]

    #initial_guess = (59, 1582, 1648, 1.7, 2.1, 1.4, 5.)  # amplitude = 88.8/1.5, x0, y0 = 1582, 1648, x_fwhm~4 -> sigma~4/2.355=1.7,
    initial_guess = (130., 50, 50, 5., 5., 0.87, 0.3)  # amplitude = 88.8/1.5, x0, y0 = 1582, 1648, x_fwhm~4 -> sigma~4/2.355=1.7,
    # y_fwhm~5 -> sigma~5/2.355=2.1, theta in radians ~80 deg -> 1.4 rads; offset ~ 0?

    x = np.linspace(0, len(idata_small), len(idata_small))
    y = np.linspace(0, len(idata_small[0]), len(idata_small[0]))
    x, y = np.meshgrid(x, y)

    gg = twoD_Gaussian((x, y), 130., 50, 50, 5., 5., 0.87, 0.3)
    print(gg.shape)
    print(idata.shape)

    import scipy.optimize as opt
    popt, pcov = opt.curve_fit(twoD_Gaussian, (x, y), idata_small.ravel(), p0=initial_guess)
    print(popt)
    # [6.63812507e+01 5.26664093e+01 4.76567030e+01 6.05016694e+00 4.46070405e+00 6.05847758e+04 2.91658646e+00]
    print('above here is 2D gauss result!')
    #print(oop)

    # '''  #
    # THESE ARE FOR NGC 384
    hi,hf = 1375,1525 # 950,1800  # 900,1850
    hyi,hyf = 1290,1440  # hi,hf
    iy0,iy1 = 1875,1975  # pre-rot n384
    i0,i1 = 1875,1975  # pre-rot n384
    # hi,hf = 1390,1540  # pre-rot for p11179
    # hyi,hyf = 1325,1475  # pre-rot for p11179
    #hi, hf = 1405, 1555  # H-band rot p11179
    #hyi, hyf = 1625, 1775  # H-band rot p11179
    #i0, i1 = 1510, 1660  # 1000,2000  # 1200,2000
    #iy0, iy1 = 1575, 1725  # i0,i1
    hdata = hdata[hyi:hyf, hi:hf]
    idata = idata[iy0:iy1, i0:i1]
    hidx = np.unravel_index(np.argmax(hdata), hdata.shape)
    iidx = np.unravel_index(np.argmax(idata), idata.shape)
    print(hidx, iidx)  # ((78, 78), (72, 71))  # rotated H-band p11179!
    # pre-rot p11179 H-band: ((73, 76), (72, 71))
    # FOR NGC 384: (77, 82) 82 in x, 77 in y
    # plt.imshow(h_data, origin='lower', vmax=7e5, vmin=0.)
    # plt.colorbar()
    # plt.show()
    # print(oop)
    xh, yh = centroid_2dg(hdata, mask=np.isnan(hdata))
    print(xh, yh)  # Using 1290,1440 & 1375,1525: (82.034056318563543, 76.753908662648286)
    # using 900,1850: (556.0822735162559, 466.70973014659847)
    # Using 950,1800: (507.21197319028778, 416.78153137759034)  # better, still imperfect
    xi, yi = centroid_2dg(idata, mask=np.isnan(idata))
    print(xi, yi)  # Using 1575,1725 & 1510,1660: (72.940969820368124, 71.078015258901999)
    print(oop)
    # using 1200,2000: (383.27133931976698, 445.74008432184183)  # better, still imperfect
    # Using 1000,2000: (583.33511514826932, 645.7459270168938)
    # from scipy.ndimage import measurements
    # h_data[np.isnan(h_data)] = 0.
    # i_data[np.isnan(i_data)] = 0.
    # xh1,yh1 = measurements.center_of_mass(h_data)
    # print(xh1, yh1)  # (469.7267122214306, 550.71549453408932)  # worse, even accounting for flipping x,y!
    # xi1,yi1 = measurements.center_of_mass(i_data)
    # print(xi1, yi1)  # (420.9653334337014, 390.56088692909975)  # worse, even accounting for flipping x,y!
    plt.imshow(hdata, origin='lower', vmax=4.5e5, vmin=0.)
    plt.colorbar()
    plt.plot(xh, yh, 'r*')
    plt.plot(hidx[1], hidx[0], 'c*')
    # plt.plot(yh1, xh1, 'c*')
    plt.show()
    plt.imshow(idata, origin='lower', vmax=100., vmin=0.)
    plt.colorbar()
    plt.plot(xi, yi, 'r*')
    plt.plot(iidx[1], iidx[0], 'c*')
    # plt.plot(yi1, xi1, 'c*')
    plt.show()
    print(oop)

g2698 = '/Users/jonathancohn/Documents/dyn_mod/galfit/u2698/'
gf = '/Users/jonathancohn/Documents/dyn_mod/galfit/'
fj = '/Users/jonathancohn/Documents/dyn_mod/for_jonathan/'
hst_p = '/Users/jonathancohn/Documents/hst_pgc11179/'
hst_n = '/Users/jonathancohn/Documents/hst_ngc384/'
n384_h = fj + 'NGC0384_F160W_drz_sci.fits'
n384_adj_h = fj + 'NGC0384_F160W_drz_sci_adjusted.fits'
n384_adj_h_n7 = fj + 'NGC0384_F160W_drz_sci_adjusted_n7.fits'
n384_adj_h_inclsky = fj + 'NGC0384_F160W_drz_sci_adjusted_inclsky.fits'
n384_hmask = fj + 'NGC0384_F160W_drz_mask.fits'
n384_hmask_extended = fj + 'NGC0384_F160W_drz_mask_extended.fits'
n384_f814w_pixscale03 = fj + 'NGC0384_F814W_drc_sci.fits'
n384_f814w_pxf001_ps03_wht = hst_n + 'n384_f814w_drizflc_pxf001_003_wht.fits'
n384_f814w_pxf001_wht = hst_n + 'n384_f814w_drizflc_pxf001_006_wht.fits'
n384_f814w_pxf01_wht = hst_n + 'n384_f814w_drizflc_pxf01_006_wht.fits'
n384_f814w_pxf04_wht = hst_n + 'n384_f814w_drizflc_pxf04_006_wht.fits'
n384_f814w_pxf06_wht = hst_n + 'n384_f814w_drizflc_pxf06_006_wht.fits'
n384_f814w_pxf08_wht = hst_n + 'n384_f814w_drizflc_pxf08_006_wht.fits'
n384_f814w_pxf1_wht = hst_n + 'n384_f814w_drizflc_pxf1_006_wht.fits'
n384_f814w_pxf001_ps03 = hst_n + 'n384_f814w_drizflc_pxf001_003_sci.fits'
n384_f814w_pxf001 = hst_n + 'n384_f814w_drizflc_pxf001_006_sci.fits'
n384_f814w_pxf01 = hst_n + 'n384_f814w_drizflc_pxf01_006_sci.fits'
n384_f814w_pxf04 = hst_n + 'n384_f814w_drizflc_pxf04_006_sci.fits'
n384_f814w_pxf06 = hst_n + 'n384_f814w_drizflc_pxf06_006_sci.fits'
n384_f814w_pxf08 = hst_n + 'n384_f814w_drizflc_pxf08_006_sci.fits'
n384_f814w_pxf1 = hst_n + 'n384_f814w_drizflc_pxf1_006_sci.fits'
n384_psf_f814w = hst_n + 'n384_f814w00.fits'
p11179_h = fj + 'PGC11179_F160W_drz_sci.fits'
p11179_adj_h = fj + 'PGC11179_F160W_drz_sci_adjusted.fits'
p11179_adj_h_n7 = fj + 'PGC11179_F160W_drz_sci_adjusted_n7.fits'
p11179_adj_h_inclsky = fj + 'PGC11179_F160W_drz_sci_adjusted_inclsky.fits'
p11179_f814w_pxf001_wht = hst_p + 'p11179_f814w_drizflc_pxf001_006_wht.fits'
p11179_f814w_pxf01_wht = hst_p + 'p11179_f814w_drizflc_pxf01_006_wht.fits'
p11179_f814w_pxf02_wht = hst_p + 'p11179_f814w_drizflc_pxf02_006_wht.fits'
p11179_f814w_pxf04_wht = hst_p + 'p11179_f814w_drizflc_pxf04_006_wht.fits'
p11179_f814w_pxf06_wht = hst_p + 'p11179_f814w_drizflc_pxf06_006_wht.fits'
p11179_f814w_pxf08_wht = hst_p + 'p11179_f814w_drizflc_006_wht.fits'  # pxf08!
p11179_f814w_pxf09_wht = hst_p + 'p11179_f814w_drizflc_pxf09_006_wht.fits'
p11179_f814w_pxf1_wht = hst_p + 'p11179_f814w_drizflc_pxf1_006_wht.fits'
p11179_f814w_pxf001 = hst_p + 'p11179_f814w_drizflc_pxf001_006_sci.fits'
p11179_f814w_pxf01 = hst_p + 'p11179_f814w_drizflc_pxf01_006_sci.fits'
p11179_f814w_pxf02 = hst_p + 'p11179_f814w_drizflc_pxf02_006_sci.fits'
p11179_f814w_pxf04 = hst_p + 'p11179_f814w_drizflc_pxf04_006_sci.fits'
p11179_f814w_pxf06 = hst_p + 'p11179_f814w_drizflc_pxf06_006_sci.fits'
p11179_f814w_pxf08 = hst_p + 'p11179_f814w_drizflc_006_sci.fits'  # pxf08!
p11179_f814w_pxf09 = hst_p + 'p11179_f814w_drizflc_pxf09_006_sci.fits'
p11179_f814w_pxf1 = hst_p + 'p11179_f814w_drizflc_pxf1_006_sci.fits'
p11179_psf_f814w = hst_p + 'psf_f814w00.fits'
p11179_psf534_f814w = hst_p + 'psf_f814w_534diameter_00.fits'
# PSFs
p11179_f814w_psfm = hst_p + 'psf_ic0b14rmq_flc.fits'
p11179_f814w_psfx = hst_p + 'psf_ic0b14rxq_flc.fits'
p11179_f814w_psfz = hst_p + 'psf_ic0b14rzq_flc.fits'
n384_f814w_psf5 = hst_n + 'psf_ic0b09v5q_flc.fits'
n384_f814w_psfl = hst_n + 'psf_ic0b09vlq_flc.fits'
n384_f814w_psfo = hst_n + 'psf_ic0b09voq_flc.fits'

zp_H = 24.662  # https://www.stsci.edu/hst/instrumentation/wfc3/data-analysis/photometric-calibration/ir-photometric-calibration
zp_I = 24.684  # UVIS2 according to fits header; https://www.stsci.edu/hst/instrumentation/wfc3/data-analysis/photometric-calibration/uvis-photometric-calibration

with fits.open(n384_adj_h) as hdu:
    nh_hdr = hdu[0].header
    nh_data = hdu[0].data

with fits.open(n384_f814w_pixscale03) as hdu:
    ni_hdr = hdu[0].header
    ni_data = hdu[0].data

with fits.open(n384_f814w_pxf1_wht) as hdu:
    n_whdr_pxf1 = hdu[0].header
    n_wht_pxf1 = hdu[0].data

with fits.open(n384_f814w_pxf08_wht) as hdu:
    n_whdr_pxf8 = hdu[0].header
    n_wht_pxf8 = hdu[0].data

with fits.open(n384_f814w_pxf06_wht) as hdu:
    n_whdr_pxf6 = hdu[0].header
    n_wht_pxf6 = hdu[0].data

with fits.open(n384_f814w_pxf04_wht) as hdu:
    n_whdr_pxf4 = hdu[0].header
    n_wht_pxf4 = hdu[0].data

with fits.open(n384_f814w_pxf01_wht) as hdu:
    n_whdr_pxf01 = hdu[0].header
    n_wht_pxf01 = hdu[0].data

with fits.open(n384_f814w_pxf001_wht) as hdu:
    n_whdr_pxf001 = hdu[0].header
    n_wht_pxf001 = hdu[0].data

with fits.open(n384_f814w_pxf001_ps03_wht) as hdu:
    n_whdr_pxf001_ps03 = hdu[0].header
    n_wht_pxf001_ps03 = hdu[0].data

with fits.open(n384_f814w_pxf1) as hdu:
    ni_hdr_pxf1 = hdu[0].header
    ni_data_pxf1 = hdu[0].data

with fits.open(n384_f814w_pxf08) as hdu:
    ni_hdr_pxf08 = hdu[0].header
    ni_data_pxf08 = hdu[0].data

with fits.open(n384_f814w_pxf06) as hdu:
    ni_hdr_pxf06 = hdu[0].header
    ni_data_pxf06 = hdu[0].data

with fits.open(n384_f814w_pxf04) as hdu:
    ni_hdr_pxf04 = hdu[0].header
    ni_data_pxf04 = hdu[0].data

with fits.open(n384_f814w_pxf01) as hdu:
    ni_hdr_pxf01 = hdu[0].header
    ni_data_pxf01 = hdu[0].data

with fits.open(n384_f814w_pxf001) as hdu:
    ni_hdr_pxf001 = hdu[0].header
    ni_data_pxf001 = hdu[0].data

with fits.open(n384_f814w_pxf001_ps03) as hdu:
    ni_hdr_pxf001_ps03 = hdu[0].header
    ni_data_pxf001_ps03 = hdu[0].data

with fits.open(n384_psf_f814w) as hdu:
    nhdr_psfi = hdu[0].header
    ndat_psfi = hdu[0].data

with fits.open(p11179_h) as hdu:
    h_hdr = hdu[0].header
    h_data = hdu[0].data

with fits.open(p11179_f814w_pxf001) as hdu:
    i_hdr_pxf01 = hdu[0].header
    i_data_pxf01 = hdu[0].data

with fits.open(p11179_f814w_pxf01) as hdu:
    i_hdr_pxf1 = hdu[0].header
    i_data_pxf1 = hdu[0].data

with fits.open(p11179_f814w_pxf02) as hdu:
    i_hdr_pxf2 = hdu[0].header
    i_data_pxf2 = hdu[0].data

with fits.open(p11179_f814w_pxf04) as hdu:
    i_hdr_pxf4 = hdu[0].header
    i_data_pxf4 = hdu[0].data

with fits.open(p11179_f814w_pxf06) as hdu:
    i_hdr_pxf6 = hdu[0].header
    i_data_pxf6 = hdu[0].data

with fits.open(p11179_f814w_pxf08) as hdu:
    i_hdr_pxf8 = hdu[0].header
    i_data_pxf8 = hdu[0].data

with fits.open(p11179_f814w_pxf09) as hdu:
    i_hdr_pxf9 = hdu[0].header
    i_data_pxf9 = hdu[0].data

with fits.open(p11179_f814w_pxf1) as hdu:
    i_hdr_pxf10 = hdu[0].header
    i_data_pxf10 = hdu[0].data

with fits.open(p11179_f814w_pxf001_wht) as hdu:
    whdr_pxf01 = hdu[0].header
    wht_pxf01 = hdu[0].data

with fits.open(p11179_f814w_pxf01_wht) as hdu:
    whdr_pxf1 = hdu[0].header
    wht_pxf1 = hdu[0].data

with fits.open(p11179_f814w_pxf02_wht) as hdu:
    whdr_pxf2 = hdu[0].header
    wht_pxf2 = hdu[0].data

with fits.open(p11179_f814w_pxf04_wht) as hdu:
    whdr_pxf4 = hdu[0].header
    wht_pxf4 = hdu[0].data

with fits.open(p11179_f814w_pxf06_wht) as hdu:
    whdr_pxf6 = hdu[0].header
    wht_pxf6 = hdu[0].data

with fits.open(p11179_f814w_pxf08_wht) as hdu:
    whdr_pxf8 = hdu[0].header
    wht_pxf8 = hdu[0].data

with fits.open(p11179_f814w_pxf09_wht) as hdu:
    whdr_pxf9 = hdu[0].header
    wht_pxf9 = hdu[0].data

with fits.open(p11179_f814w_pxf1_wht) as hdu:
    whdr_pxf10 = hdu[0].header
    wht_pxf10 = hdu[0].data

with fits.open(p11179_psf_f814w) as hdu:
    hdr_psfi = hdu[0].header
    dat_psfi = hdu[0].data

with fits.open(p11179_psf534_f814w) as hdu:
    hdr_psfi534 = hdu[0].header
    dat_psfi534 = hdu[0].data

#find_center(ni_data_pxf08, nh_data)
#print(oop)
#nxctr = 1927
#nyctr = 1922
#plt.imshow(ndat_psfi, origin='lower')
#plt.colorbar()
#plt.show()
#print(oop)

psf_0 = np.zeros(shape=i_data_pxf8.shape)  # rmq -> pattstep = 0, no pattern, no dither. Just center it over galaxy!
psf_1 = np.zeros(shape=i_data_pxf8.shape)  # rxq -> pattstep = 1, pattern1=LINE (0,0) [must be 0,0, since step=2 exists]
psf_2 = np.zeros(shape=i_data_pxf8.shape)  # rzq -> pattstep = 2, pattern1=LINE (2.5, 2.5)
# IF it should be LINE-3PT: (0,0) , (2.33,2.33), (4.67,4.67)
# For xc0, yc0: POS TARG = -1.7952, -1.9130 [unit = arcsec]! (https://www.stsci.edu/itt/APT_help20/WFC3/appendixC3.html)
## For UVIS: POS TARG X ~ 0.0396 ["/pix] * x [pix]; POS TARG Y ~ 0.0027 ["/pix] * x [pix] + 0.0395 ["/pix] * y [pix]
## -> x [pix] = POS TARG X / 0.0396; y [pix] = (POS TARG Y - 0.0027 * x) / 0.0395
#  Also clarified here: http://guaix.fis.ucm.es/~agpaz/Instrumentacion_Espacio_2010/Espacio_Docs/HST/hst_c15_phaseII.pdf
# SAME FOR BOTH GALAXIES!! Just Change xctr, yctr
pxctr = 1581
pyctr = 1647
nxctr = 1927
nyctr = 1922
xctr,yctr = nxctr,nyctr  # pxctr,pyctr  # pxctr,pyctr ;; nxctr,nyctr
xc0 = xctr+(-1.7952 / 0.0396)  # = -45.3333333
xc1 = xctr+0
xc2 = xctr+2.5
yc0 = yctr+((-1.9130 - 0.0027 * (-1.7952 / 0.0396)) / 0.0395)  # = -45.3316456
yc1 = yctr+0
yc2 = yctr+2.5

xorig = np.zeros(shape=len(dat_psfi))
yorig = np.zeros(shape=len(dat_psfi[0]))
#origres = 0.02358  # arcsec/pix; from Tiny Tim: "Using undistorted critical sampling pixel size of 0.02358 arcsec"
print(dat_psfi.shape)  # 89, 89. I used 3.0 arcsec for pixel size in Tiny Tim! So: 0.0337078652 arcsec/pix
# WANT: 0.06"/pix. 89 pix * 0.06 arcsec/pix = 5.34 arcsec. Try in Tiny Tim!
print(dat_psfi534.shape)  # 159, 159. 5.34" size -> 0.0335849057 arcsec/pix. About the same as before!
origres = 0.0337078652
for i in range(len(xorig)):
    xorig[i] = origres * (i - len(dat_psfi) / 2.)
for i in range(len(yorig)):
    yorig[i] = origres * (i - len(dat_psfi[0]) / 2.)

x0 = np.zeros(shape=len(psf_0))
x1 = np.zeros(shape=len(psf_1))
x2 = np.zeros(shape=len(psf_2))
y0 = np.zeros(shape=len(psf_0[0]))
y1 = np.zeros(shape=len(psf_1[0]))
y2 = np.zeros(shape=len(psf_2[0]))
targetres = 0.06  # arcsec/pix; same as i_data res!

for i in range(len(x0)):
    x0[i] = targetres * (i - xc0)
    x1[i] = targetres * (i - xc1)
    x2[i] = targetres * (i - xc2)
for i in range(len(y0)):
    y0[i] = targetres * (i - yc0)
    y1[i] = targetres * (i - yc1)
    y2[i] = targetres * (i - yc2)

f_psf = interpolate.interp2d(xorig,yorig,dat_psfi)

psf_0 = f_psf(x0, y0)
psf_1 = f_psf(x1, y1)
psf_2 = f_psf(x2, y2)
#plt.imshow(psf_0, origin='lower')  # 1535, 1601
#plt.colorbar()
#plt.show()
psftest = f_psf(xorig,yorig)
print(xorig, yorig)
print(x0, y0)
#print(oop)

'''  # NGC 384 EXAMINE PSF DITHERS!
plt.imshow(psf_0[1805:1955, 1805:1955], origin='lower')  # center at 76,71 (lean toward lower)
plt.colorbar()
plt.show()
plt.imshow(psf_1[1805:1955, 1805:1955], origin='lower')  # center at 122,117 (lean toward lower)
plt.colorbar()
plt.show()
plt.imshow(psf_2[1805:1955, 1805:1955], origin='lower')  # center at 124,119 (lean toward higher)
plt.colorbar()
plt.show()
# '''  #

with fits.open(hst_n + 'ic0b09v5q_flc.fits', 'update') as hdu:
    hdr_v5q = hdu[0].header
    dat_v5q = hdu['SCI'].data
    hdr_v5q['history'] = 'Replaced ic0b09v5q_flc.fits science image with PSF data!'
#fits.writeto(n384_f814w_psf5, psf_0, hdr_v5q)
    # data_v5q = psf_0
    hdu['SCI',1].data = psf_0
    #hdu.flush()
    #fits.update(n384_f814w_psf5, psf_0, hdr_v5q, 'sci', 1)

    print(hdu.info())
print(oop)

with fits.open(hst_n + 'ic0b09vlq_flc.fits', 'update') as hdu:
    hdr_vlq = hdu[0].header
    dat_vlq = hdu[0].data

    hdr_vlq['history'] = 'Replaced ic0b09vlq_flc.fits science image with PSF data!'
#fits.writeto(n384_f814w_psfl, psf_1, hdr_vlq)
    #fits.update(n384_f814w_psfl, psf_1, hdr_vlq, 'sci')
    # dat_vlq = psf_1
    hdu['SCI',1].data = psf_1
    hdu.flush()

with fits.open(hst_n + 'ic0b09voq_flc.fits', 'update') as hdu:
    hdr_voq = hdu[0].header
    dat_voq = hdu[0].data

    hdr_voq['history'] = 'Replaced ic0b09voq_flc.fits science image with PSF data!'
#fits.writeto(n384_f814w_psfo, psf_2, hdr_voq)
    # fits.update(n384_f814w_psfo, psf_2, hdr_voq, 'sci')
    # dat_voq = psf_2
    hdu['SCI',1].data = psf_2
    hdu.flush()
print(oop)

# '''  # PGC 11179 EXAMINE PSF DITHERS
plt.imshow(psf_0[1575:1725, 1505:1655], origin='lower')  # center at 26,30 (lean toward lower)
plt.colorbar()
plt.show()
plt.imshow(psf_1[1575:1725, 1505:1655], origin='lower')  # center at 76,72 (lean toward lower)
plt.colorbar()
plt.show()
plt.imshow(psf_2[1575:1725, 1505:1655], origin='lower')  # center at 78,74 (lean toward higher)
plt.colorbar()
plt.show()

with fits.open(hst_p + 'ic0b14rmq_flc.fits', 'rb+') as hdu:
    hdr_rmq = hdu[0].header
    dat_rmq = hdu[0].data

hdr_rmq['history'] = 'Replaced ic0b14rmq_flc.fits science image with PSF data!'
#fits.writeto(p11179_f814w_psfm, psf_0, hdr_rmq)
fits.update(p11179_f814w_psfm, psf_0, hdr_rmq, 'sci')

with fits.open(hst_p + 'ic0b14rxq_flc.fits', 'rb+') as hdu:
    hdr_rxq = hdu[0].header
    dat_rxq = hdu[0].data

hdr_rxq['history'] = 'Replaced ic0b14rxq_flc.fits science image with PSF data!'
#fits.writeto(p11179_f814w_psfx, psf_1, hdr_rxq)
fits.update(p11179_f814w_psfx, psf_1, hdr_rxq, 'sci')

with fits.open(hst_p + 'ic0b14rzq_flc.fits', 'rb+') as hdu:
    hdr_rzq = hdu[0].header
    dat_rzq = hdu[0].data

hdr_rzq['history'] = 'Replaced ic0b14rzq_flc.fits science image with PSF data!'
# fits.writeto(p11179_f814w_psfz, psf_2, hdr_rzq)
fits.update(p11179_f814w_psfz, psf_2, hdr_rzq, 'sci')
print(oop)
# '''  #

#plt.imshow(ni_data_pxf08, origin='lower', vmax=10)  # N384: star at: x=1692, y=2538
#plt.imshow(ni_data_pxf001_ps03, origin='lower', vmax=10)  # this finally bad!
#plt.colorbar()
#plt.show()
#print(oop)

nweights = [n_wht_pxf001,n_wht_pxf01,n_wht_pxf4,n_wht_pxf6,n_wht_pxf8,n_wht_pxf1]
ndats = [ni_data_pxf001,ni_data_pxf01,ni_data_pxf04,ni_data_pxf06,ni_data_pxf08,ni_data_pxf1]
npxfs = ['0.01','0.1','0.4','0.6','0.8','1.0']
nnrow = 2
nncol = 3
# nguess = (130., 50, 50, 5., 5., 0.87, 0.3)
calc_noise(nweights, ndats, npxfs, nxctr, nyctr, 50, 50, nnrow, nncol, 700., 1692, 2538)
print(oop)

weights = [wht_pxf01,wht_pxf1,wht_pxf2,wht_pxf4,wht_pxf6,wht_pxf8,wht_pxf9,wht_pxf10]
dats = [i_data_pxf01,i_data_pxf1,i_data_pxf2,i_data_pxf4,i_data_pxf6,i_data_pxf8,i_data_pxf9,i_data_pxf10]
pxfs = ['0.01', '0.1', '0.2', '0.4', '0.6', '0.8', '0.9', '1.0']  # '0.01', '0.6', '0.9'
nrow = 2
ncol = 4
calc_noise(weights, dats, pxfs, 1581, 1647, 50, 50, nrow, ncol)  # 1934, 1118
# wht_pxf9 ; i_data_pxf9 ;; wht_pxf6 ; i_data_pxf6 ;; wht_pxf01 ; i_data_pxf01
print(oop)

i_data = i_data_pxf8
print(h_data.shape)  # (2637, 2710)
#print(h_hdr)
#print(oop)
#  I-band has been aligned with north, H-band has not!!!!!! So, ROTATE IT!
h_data, newpt = rot(h_data, h_hdr['CRPIX1'], h_hdr['CRPIX2'], angle=-102.7825141362319, reshape=True)
#plt.imshow(h_data, origin='lower', vmax=7.e5, vmin=0.)
#plt.colorbar()
#plt.show()
#print(oop)
#magi = zp_I - 2.5 * np.log10(i_data/1.5)
#plt.imshow(magi, origin='lower', vmax=28.)
#plt.colorbar()
#plt.show()
#print(oop)
print(h_data.shape)  # (3226, 3171)
print(i_data.shape)  # (3369, 3501)  # this one's bigger! (even after H-band rotation!)
# print(oop)

# '''  #
h_matchingi = np.zeros(shape=i_data.shape)  # H-band, matching the I-band shape!
# I want H[1367, 1457] to line up with I[1647, 1581]
# So: H extends from yy=1647-1367:yy+2710(?), and xx=1581-1466:xx+2637
#xx = 1647-(1325+73)  # pre-rotated values
#yy = 1581-(1390+76)  # pre-rotated values
xx = 1647-(1625+78)  # 78 from hidx[0]
yy = 1581-(1405+78)  # 78 from hidx[1]
if xx<0:
    print(h_data[-xx:,:].shape, 'hm', h_data.shape[0]-xx, h_data.shape[1]-yy)
    print(h_matchingi[:h_data.shape[0]+xx, yy:yy+h_data.shape[1]].shape)
    h_matchingi[:h_data.shape[0]+xx, yy:yy+h_data.shape[1]] = h_data[-xx:,:]
else:
    h_matchingi[xx:xx+h_data.shape[0], yy:yy+h_data.shape[1]] = h_data
# Gain = electrons/count
# convert i_data to counts, from counts/s
h_matchingi = h_matchingi / 1354.463046 / 2.5  #  H-band image units: e-, want counts/s: e- / s / (e-/count)
i_data /= 1.5  #  I-band units are electrons/s, want counts/s, GAIN here is 1.5, not 2.5!
# h_matchingi[xx:xx+2637, yy:yy+2710] = h_data / 1354.463046 / 2.5  # H-Band image units: e- / s / (e-/count)
#plt.imshow(i_data - h_matchingi, origin='lower')#, vmax=7e5, vmin=0.)
'''  # GOOD NOW!
plt.imshow(i_data, origin='lower', vmax=100, vmin=0.)
plt.plot(1510+71, 1575+72, 'r*')  # from idx
plt.plot()
plt.colorbar()
plt.show()
plt.imshow(h_matchingi, origin='lower', vmax=100, vmin=0.)
plt.plot(1510+71, 1575+72, 'r*')  # from idx
plt.colorbar()
plt.show()
# '''  #
mag_i = zp_I - 2.5 * np.log10(i_data)
mag_h = zp_H - 2.5 * np.log10(h_matchingi)
i_minus_h = mag_i - mag_h
i_minus_h[np.isnan(i_minus_h)] = 0.
vmax = np.amax(i_minus_h[1600:1700, 1525:1625])
i_minus_h = i_minus_h[1400:1900, 1325:1825]  # yrange, xrange
#xctr = 1581.05908-1325
xctr = 1510+71-1325
#yctr = 1645.05595-1400
yctr = 1575+72-1400
xax = np.zeros(shape=len(i_minus_h))
yax = np.zeros(shape=len(i_minus_h[0]))
for xi in range(len(i_minus_h)):
    xax[xi] = -0.06 * (xi - xctr)  # (arcsec/pix) * N_pix = arcsec
for yi in range(len(i_minus_h[0])):
    yax[yi] = 0.06 * (yi - yctr)  # (arcsec/pix) * N_pix = arcsec
extent = [xax[0], xax[-1], yax[0], yax[-1]]
plt.imshow(i_minus_h, origin='lower', vmin=0., vmax=vmax, extent=extent)#, vmax=3.)  #, vmax=12.)  # , vmax=0.1, vmin=-50 # , vmax=7e5, vmin=0.
plt.colorbar()
# plt.plot(1510+71, 1575+72, 'r*')  # from idx  # good when not using extent
plt.xlabel(r'$\Delta$ RA [arcsec]')
plt.ylabel(r'$\Delta$ Dec [arcsec]')
plt.title(r'PGC 11179 HST $I-H$')
plt.show()
print(oop)

# '''  #
'''  #
# CENTROIDS: H-band:
# h_data[np.abs(h_data) > 100] = 0.
plt.imshow(h_data, origin='lower', vmax=4.5e5, vmin=0.)
plt.colorbar()
plt.plot(1390+76, 1325+73, 'r*')  # PGC 11179
#plt.plot(1375+82, 1290+77, 'r*')  # from idx  # NGC 384
#plt.plot(1375+82.034056318563543, 1290+76.753908662648286, 'c*')  # from centroid  # NGC 384
plt.show()
plt.imshow(i_data, origin='lower', vmax=100., vmin=0.)
plt.colorbar()
#plt.plot(1510+71, 1575+72, 'r*')  # from idx
#plt.plot(1510+71.078015258901999, 1575+72.940969820368124, 'c*')  # from centroid
plt.show()
print(oop)
#  '''
# '''  #

i_data = np.nan_to_num(i_data)

initial_guess = (59,1582,1648,1.7,2.1,1.4,5.)  # amplitude = 88.8/1.5, x0, y0 = 1582, 1648, x_fwhm~4 -> sigma~4/2.355=1.7,
# y_fwhm~5 -> sigma~5/2.355=2.1, theta in radians ~80 deg -> 1.4 rads; offset ~ 0?

x = np.linspace(0, len(i_data), len(i_data))
y = np.linspace(0, len(i_data[0]), len(i_data[0]))
x, y = np.meshgrid(x, y)

gg = twoD_Gaussian((x,y), 59, 1582, 1648, 1.7, 2.1, 1.4, 5.)
print(gg.shape)
print(i_data.shape)
#print(i_data.ravel.shape)

popt, pcov = opt.curve_fit(twoD_Gaussian, (x, y), i_data.ravel(), p0=initial_guess)

print(popt)
# [ 3.29487947e-01  1.58105908e+03  1.64505595e+03 -1.07963065e+00 -1.29535590e+01  1.55820184e+00  4.78887205e-02]
## amplitude = 0.33, x0=1581.05908, y0=1645.05595, sigmax=-1.08, sigmay=-1.295, theta=1.55820184, offset=0.0478887205
print(oop)

# '''  #
#THESE ARE FOR NGC 384
#hi,hf = 1375,1525 # 950,1800  # 900,1850
#hyi,hyf = 1290,1440  # hi,hf
#hi,hf = 1390,1540  # pre-rot for p11179
#hyi,hyf = 1325,1475  # pre-rot for p11179
hi,hf = 1405,1555  # H-band rot p11179
hyi,hyf = 1625,1775  # H-band rot p11179
i0,i1 = 1510,1660  # 1000,2000  # 1200,2000
iy0,iy1 = 1575,1725  # i0,i1
h_data = h_data[hyi:hyf, hi:hf]
i_data = i_data[iy0:iy1, i0:i1]
hidx = np.unravel_index(np.argmax(h_data), h_data.shape)
iidx = np.unravel_index(np.argmax(i_data), i_data.shape)
print(hidx, iidx)  # ((78, 78), (72, 71))  # rotated H-band p11179!
# pre-rot p11179 H-band: ((73, 76), (72, 71))
# FOR NGC 384: (77, 82) 82 in x, 77 in y
#plt.imshow(h_data, origin='lower', vmax=7e5, vmin=0.)
#plt.colorbar()
#plt.show()
#print(oop)
xh, yh = centroid_2dg(h_data, mask=np.isnan(h_data))
print(xh, yh)  # Using 1290,1440 & 1375,1525: (82.034056318563543, 76.753908662648286)
# using 900,1850: (556.0822735162559, 466.70973014659847)
# Using 950,1800: (507.21197319028778, 416.78153137759034)  # better, still imperfect
xi, yi = centroid_2dg(i_data, mask=np.isnan(i_data))
print(xi, yi)  # Using 1575,1725 & 1510,1660: (72.940969820368124, 71.078015258901999)
# using 1200,2000: (383.27133931976698, 445.74008432184183)  # better, still imperfect
# Using 1000,2000: (583.33511514826932, 645.7459270168938)
# from scipy.ndimage import measurements
#h_data[np.isnan(h_data)] = 0.
#i_data[np.isnan(i_data)] = 0.
#xh1,yh1 = measurements.center_of_mass(h_data)
#print(xh1, yh1)  # (469.7267122214306, 550.71549453408932)  # worse, even accounting for flipping x,y!
#xi1,yi1 = measurements.center_of_mass(i_data)
#print(xi1, yi1)  # (420.9653334337014, 390.56088692909975)  # worse, even accounting for flipping x,y!
plt.imshow(h_data, origin='lower', vmax=4.5e5, vmin=0.)
plt.colorbar()
plt.plot(xh, yh, 'r*')
plt.plot(hidx[1], hidx[0], 'c*')
#plt.plot(yh1, xh1, 'c*')
plt.show()
plt.imshow(i_data, origin='lower', vmax=100., vmin=0.)
plt.colorbar()
plt.plot(xi, yi, 'r*')
plt.plot(iidx[1], iidx[0], 'c*')
#plt.plot(yi1, xi1, 'c*')
plt.show()
print(oop)


import time

time1 = time.time()
def func():
    input_f = open('input_10.1.txt', 'r')
    input_d = [int(i) for i in input_f.read().split()]
    input_d.sort()

    max_j = input_d[-1] + 3
    j_1 = 0
    j_3 = 1
    curr_j = 0
    for i in input_d:
        if i - curr_j == 1:
            j_1 += 1
        if i - curr_j == 3:
            j_3 += 1
        curr_j = i
        if i + 3 == max_j:
            break

    return j_1 * j_3

print(func())
print("time: " + str((time.time() - time1) * 1000) + "ms")


# -*- coding: utf-8 -*-
'''
Created on 2017年5月10日

@author: chenyitao
'''

from PIL import ImageEnhance

from math import ceil

class ImageHelper(object):

    @staticmethod
    def to_grey(img, threshold=140):
        img_grey = img.convert('L')
        table = [0 if i < threshold else 1 for i in range(256)]
        out = img_grey.point(table, '1')
        return out

    @staticmethod
    def point_reduce_the_noise(image):
        width = image.width
        height = image.height
        box = (0, 0, width, height)
        new = image.crop(box)
        for i in range(0, height):
            for j in range(0, width):
                real_pixel = ImageHelper.real_pixel(new, j, i)
                if real_pixel == 0:
                    continue
                new.putpixel((j, i), real_pixel)
        return new

    @staticmethod
    def plus_sign_reduce_the_noise(image):
        width = image.width
        height = image.height
        box = (0, 0, width, height)
        new = image.crop(box)
        for i in range(0, height):
            for j in range(0, width):
                points = ImageHelper.plus_sign_identify(new, j, i)
                if not points:
                    continue
                for x, y in points:
                    new.putpixel((x, y), 1)
                new.putpixel((j, i), 1)
        return new

    @staticmethod
    def real_pixel(img, x, y):
        if img.getpixel((x, y)) == 1:
            return 0
        w, h = img.size
        data = list(img.getdata())
        data = [data[i: i + w] for i in range(0, len(data), w)]
        if x == 0 and y == 0:
            points = [(x, y + 1), (x + 1, y), (x + 1, y + 1)]
        elif x == w - 1 and y == 0:
            points = [(x - 1, y), (x, y + 1), (x - 1, y + 1)]
        elif x == w - 1 and y == h - 1:
            points = [(x - 1, y), (x, y - 1), (x - 1, y - 1)]
        elif x == 0 and y == h - 1:
            points = [(x, y - 1), (x + 1, y - 1), (x + 1, y)]
        elif w - 1 > x > 0 and y == 0:
            points = [(x - 1, y), (x - 1, y + 1), (x, y + 1), (x + 1, y + 1), (x + 1, y)]
        elif x == w - 1 and h > y > 0:
            points = [(x, y - 1), (x - 1, y - 1), (x - 1, y), (x - 1, y + 1), (x, y + 1)]
        elif w > x > 0 and y == h - 1:
            points = [(x - 1, y), (x - 1, y - 1), (x, y - 1), (x + 1, y - 1), (x + 1, y)]
        elif x == 0 and h - 1 > y > 0:
            points = [(x, y - 1), (x, y + 1), (x + 1, y - 1), (x + 1, y), (x + 1, y + 1)]
        else:
            points = [(x - 1, y - 1), (x, y - 1), (x + 1, y - 1), (x + 1, y),
                      (x + 1, y + 1), (x, y + 1), (x - 1, y + 1), (x - 1, y)]
        black_point_num = sum([0 if data[y][x] else 1 for x, y in points])
        return 1 if black_point_num < 3 else 0

    @staticmethod
    def plus_sign_identify(img, x, y):
        if img.getpixel((x, y)) == 1:
            return None
        w, h = img.size
        data = list(img.getdata())
        data = [data[i: i + w] for i in range(0, len(data), w)]
        if w - 1 > x > 0 and h - 1 > y > 0:
            points = [(x - 1, y - 1), (x, y - 1), (x + 1, y - 1), (x + 1, y),
                      (x + 1, y + 1), (x, y + 1), (x - 1, y + 1), (x - 1, y)]
        else:
            return None
        black_points = [0 if data[y][x] else 1 for x, y in points]
        return points if black_points == [0, 1, 0, 1, 0, 1, 0, 1] else None

    @staticmethod
    def sum_9_region(img, x, y):
        cur_pixel = img.getpixel((x, y))
        width = img.width
        height = img.height
        if cur_pixel == 1:
            return 0
        if y == 0:
            if x == 0:
                total = cur_pixel \
                        + img.getpixel((x, y + 1)) \
                        + img.getpixel((x + 1, y)) \
                        + img.getpixel((x + 1, y + 1))
                return 4 - total
            elif x == width - 1:
                total = cur_pixel \
                        + img.getpixel((x, y + 1)) \
                        + img.getpixel((x - 1, y)) \
                        + img.getpixel((x - 1, y + 1))
                return 4 - total
            else:
                total = cur_pixel \
                        + img.getpixel((x - 1, y)) \
                        + img.getpixel((x - 1, y + 1)) \
                        + img.getpixel((x, y + 1)) \
                        + img.getpixel((x + 1, y)) \
                        + img.getpixel((x + 1, y + 1))
                return 6 - total
        elif y == height - 1:
            if x == 0:
                total = cur_pixel \
                        + img.getpixel((x + 1, y)) \
                        + img.getpixel((x + 1, y - 1)) \
                        + img.getpixel((x, y - 1))
                return 4 - total
            elif x == width - 1:
                total = cur_pixel \
                        + img.getpixel((x, y - 1)) \
                        + img.getpixel((x - 1, y)) \
                        + img.getpixel((x - 1, y - 1))
                return 4 - total
            else:
                total = cur_pixel \
                        + img.getpixel((x - 1, y)) \
                        + img.getpixel((x + 1, y)) \
                        + img.getpixel((x, y - 1)) \
                        + img.getpixel((x - 1, y - 1)) \
                        + img.getpixel((x + 1, y - 1))
                return 6 - total
        else:
            if x == 0:
                total = cur_pixel \
                        + img.getpixel((x, y - 1)) \
                        + img.getpixel((x, y + 1)) \
                        + img.getpixel((x + 1, y - 1)) \
                        + img.getpixel((x + 1, y)) \
                        + img.getpixel((x + 1, y + 1))
                return 6 - total
            elif x == width - 1:
                total = cur_pixel \
                        + img.getpixel((x, y - 1)) \
                        + img.getpixel((x, y + 1)) \
                        + img.getpixel((x - 1, y - 1)) \
                        + img.getpixel((x - 1, y)) \
                        + img.getpixel((x - 1, y + 1))
                return 6 - total
            else:
                total = cur_pixel \
                        + img.getpixel((x - 1, y - 1)) \
                        + img.getpixel((x - 1, y)) \
                        + img.getpixel((x - 1, y + 1)) \
                        + img.getpixel((x, y - 1)) \
                        + img.getpixel((x, y + 1)) \
                        + img.getpixel((x + 1, y - 1)) \
                        + img.getpixel((x + 1, y)) \
                        + img.getpixel((x + 1, y + 1))
                return 9 - total

    @staticmethod
    def interference_line_reduce_the_noise(img):
        enhancer = ImageEnhance.Contrast(img)
        img = enhancer.enhance(2)
        img = ImageHelper.to_grey(img)
        data = img.getdata()
        w, h = img.size
        black_point = 0
        for x in range(0, w - 1):
            for y in range(0, h - 1):
                mid_pixel = data[w * y + x]
                if mid_pixel == 0:
                    top_pixel = data[w * (y - 1) + (x + 1)]
                    left_pixel = data[w * y + x]
                    down_pixel = data[w * (y + 1) + (x + 1)]
                    right_pixel = data[w * y + (x + 2)]
                    if top_pixel == 0:
                        black_point += 1
                    if left_pixel == 0:
                        black_point += 1
                    if down_pixel == 0:
                        black_point += 1
                    if right_pixel == 0:
                        black_point += 1
                    if black_point < 3:
                        img.putpixel((x, y), 1)
                    black_point = 0
        return img

    @staticmethod
    def rough_cut(img):
        try:
            left, top, right, bottom = ImageHelper.get_edge(img)
        except Exception as e:
            return None
        return img.crop((left, top, right, bottom))

    @staticmethod
    def fine_cut(img, default_size):
        left = 0
        sub = []
        while left < img.width:
            try:
                right = ImageHelper.get_right_edge(img, left)
                tmp = img.crop((left, 0, right + 1, img.height))
                sub_img = ImageHelper.rough_cut(tmp)
                if not sub_img:
                    return sub
                if sub_img.width > default_size[0] * 1.5:
                    imgs = ImageHelper.split_adhesion_character_split(sub_img, default_size)
                    sub.extend(imgs)
                else:
                    if sub_img.width > 3 and sub_img.height > 10:
                        sub.append(sub_img)
                left = ImageHelper.get_next_left(img, right + 1)
            except Exception as e:
                return sub
        return sub

    @staticmethod
    def get_next_left(img, right):
        w, h = img.size
        data = list(img.getdata())
        data = [data[i: i + w] for i in range(0, len(data), w)]
        left = 0
        for x in range(right, w - 2):
            if sum([data[y][x] for y in range(h)]) == h:
                left = x
            else:
                if sum([data[y][x + 1] for y in range(h)]) != h \
                        and sum([data[y][x + 2] for y in range(h)]) != h:
                    break
                left = x
        else:
            left = 0xffff
        return left

    @staticmethod
    def get_right_edge(img, left):
        w, h = img.size
        data = list(img.getdata())
        data = [data[i: i + w] for i in range(0, len(data), w)]
        right = 0
        for x in range(left, w):
            if sum([data[y][x] for y in range(h)]) != h:
                right = x
            else:
                if x > w - 3:
                    break
                if sum([data[y][x + 1] for y in range(h)]) == h \
                        and sum([data[y][x + 2] for y in range(h)]) == h:
                    break
                right = x
        return right

    @staticmethod
    def get_edge(img):
        w, h = img.size
        data = list(img.getdata())
        data = [data[i: i + w] for i in range(0, len(data), w)]
        left = top = 0
        right = w
        bottom = h
        for y in range(h):
            if y > h - 3:
                break
            if sum(data[y]) == w:
                top = y
            else:
                if sum(data[y + 1]) == w \
                        and sum(data[y + 2]) == w:
                    top = y
                else:
                    break
        for y in range(h - 1, -1, -1):
            if y < 2:
                break
            if sum(data[y]) == w:
                bottom = y - 1
            else:
                if sum(data[y - 1]) == w \
                        and sum(data[y - 2]) == w:
                    bottom = y - 1
                else:
                    break
        for x in range(w):
            if x > w - 3:
                break
            if sum([data[y][x] for y in range(h)]) == h:
                left = x
            else:
                if sum([data[y][x + 1] for y in range(h)]) == h \
                        or sum([data[y][x + 2] for y in range(h)]) == h:
                    left = x
                else:
                    break
        for x in range(w - 1, -1, -1):
            if x < 2:
                break
            if sum([data[y][x] for y in range(h)]) == h:
                right = x
            else:
                if sum([data[y][x - 1] for y in range(h)]) == h \
                        or sum([data[y][x - 2] for y in range(h)]) == h:
                    right = x
                else:
                    break
        return left, top, right, bottom

    @staticmethod
    def split_adhesion_character_split(img, default_size):
        w, h = img.size
        left = 0
        sub_img = []
        cnt = ceil(w / default_size[0])
        sub_width = w / cnt
        while left < w:
            sub = img.crop((left, 0, left + sub_width, h))
            if sub.width > 3:
                sub_img.append(sub)
            left += sub_width
        return sub_img


import html

import googletrans

import octobot
import octobot.localization

translator = googletrans.Translator()

inf = octobot.PluginInfo(octobot.localizable("Google Translate"))

TRANSLATION_TEMPLATE = octobot.localizable("From {source_language} to {target_language}\n{text}")

@octobot.CommandHandler(["tl", "translate"], description=octobot.localizable("Translates text using Google Translate"))
def gtl(bot: octobot.OctoBot, context: octobot.Context):
    if context.update.message is not None and context.update.message.reply_to_message is not None:
        reply = context.update.message.reply_to_message
        if reply.caption is not None:
            text = reply.caption
        else:
            text = reply.text
        text_type = "reply"
    else:
        text = str(context.query)
        text_type = "args"
    source_language = None
    destination_language = None
    default_language = octobot.localization.get_chat_locale(context.update)
    if len(context.args) > 0:
        arg_0 = context.args[0]
        if arg_0 in googletrans.LANGUAGES:
            destination_language = arg_0
            destination_language.replace("_", "-")
            if text_type == "args":
                text = " ".join(context.args[1:])
        elif arg_0.count("-") == 1:
            source_language, destination_language = arg_0.split("-")
            source_language.replace("_", "-")
            destination_language.replace("_", "-")
            if text_type == "args":
                text = " ".join(context.args[1:])
    if source_language not in googletrans.LANGUAGES:
        source_language = "auto"
    if destination_language not in googletrans.LANGUAGES:
        destination_language = default_language
    translation = translator.translate(text, dest=destination_language, src=source_language)
    return context.reply(context.localize(TRANSLATION_TEMPLATE).format(
        source_language=googletrans.LANGUAGES[translation.src].title(),
        target_language=googletrans.LANGUAGES[translation.dest].title(),
        text=html.escape(translation.text)
    ), parse_mode="HTML")


import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from agent.discrete.seperate.ppo import PPOLSTM
from agent.utils import get_gaes, split_episode
from tensorboardX import SummaryWriter
from model import CNNActorLSTM, CNNCriticLSTM
import cv2
import gym

#writer = SummaryWriter()
sess = tf.Session()
num_worker = 1
window_size, output_size, lstm_units, lstm_layers = 84, 3, 512, 1
old_actor = CNNActorLSTM('old_actor', window_size, output_size, lstm_units, lstm_layers)
actor = CNNActorLSTM('actor', window_size, output_size, lstm_units, lstm_layers)
critic = CNNCriticLSTM('critic', window_size, lstm_units, lstm_layers)
agent = PPOLSTM(sess, output_size, num_worker, None, old_actor, actor, critic)
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
#saver.restore(sess, 'breakout_ppo_1env1lstm/model')

learning = True
normalize = True
global_update = 0
sample_idx = 0
score = 0
episode = 0

train_size = 16

env = gym.make('BreakoutDeterministic-v4')

total_state, total_reward, total_done, total_next_state, total_action = [], [], [], [], []
total_value, total_next_value = [], []
episode = 0
while True:

    episode += 1
    _ = env.reset()
    env.step(1)
    state = cv2.resize(env.env.ale.getScreenGrayscale().squeeze().astype('float32'), (84, 84))
    state *= (1.0/255.0)
    state = np.reshape(state, [window_size, window_size, 1])
    actor_init, critic_init = agent.get_init()
    done = False

    while not done:
        #env.render()
        action, value, actor_init, value_init = agent.get_action([state], actor_init, critic_init)
        action = action[0]
        _, reward, done, info = env.step(action+1)

        next_state = cv2.resize(env.env.ale.getScreenGrayscale().squeeze().astype('float32'), (84, 84))
        next_state *= (1.0/255.0)
        next_state = np.reshape(next_state, [window_size, window_size, 1])

        _, next_value, _, _ = agent.get_action([next_state], actor_init, critic_init)

        if info['ale.lives'] != 5:
            done = True
            reward = -1

        total_next_value.append(next_value)
        total_value.append(value)
        total_state.append(state)
        total_next_state.append(next_state)
        total_done.append(done)
        total_reward.append(reward)
        total_action.append(action)

        state = next_state

    if episode % train_size == 0:
        total_state = np.stack(total_state)
        total_next_state = np.stack(total_next_state)
        total_value = np.stack(total_value)
        total_next_value = np.stack(total_next_value)
        total_action = np.stack(total_action)
        total_done = np.stack(total_done)
        total_reward = np.stack(total_reward)

        adv, target = get_gaes(total_reward, total_done, total_value, total_next_value,
                                agent.gamma, agent.lamda, True)

        total_state, total_reward, total_done, total_next_state, total_action = [], [], [], [], []
        total_value, total_next_value = [], []


"""change in table

Revision ID: 6b4b6eb83c0a
Revises: af692ff1d1c4
Create Date: 2016-02-29 12:47:00.164474

"""

# revision identifiers, used by Alembic.
revision = '6b4b6eb83c0a'
down_revision = 'af692ff1d1c4'

from alembic import op
import sqlalchemy as sa

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('posts', 'body_html')
    op.drop_column('users', 'last_seen')
    ### end Alembic commands ###

def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.add_column('users', sa.Column('last_seen', sa.DATETIME(), nullable=True))
    op.add_column('posts', sa.Column('body_html', sa.TEXT(), nullable=True))
    ### end Alembic commands ###


#!/usr/bin/env python3

import cv2 as cv

if __name__ == '__main__':

    # arguments
    cap = cv.VideoCapture(0)  # lit depuis un fichier

    while(cap.isOpened()):
        ret, frame = cap.read()
        if not ret:
            break

        blur = cv.Canny(cv.GaussianBlur(frame, (9, 9), 1.5), 0, 40)

        cv.imshow('bmx', blur)
        if cv.waitKey(30) == 27:
            break


import tkinter as tk

class Janela:

    def __init__(self):

        self.janela = tk.Tk()
        self.janela.title('Nomes:')

        #Criando frames
        self.frameTopo = tk.Frame(self.janela)
        self.frameBase = tk.Frame(self.janela)

        #Rotulando frames
        self.nome1 = tk.Label(self.frameTopo, text = ('João Lucas Ribeiro'), font = ("Arial", 14))
        self.nome2 = tk.Label(self.frameBase, text = ('Diana Cristina dos Santos Borges'), font = ("Arial", 14))

        #Empacotando rotulos dos frames como uma lista
        self.nome1.pack(side = "top")
        self.nome2.pack(side = "top")

        #Empacotando os frames
        self.frameTopo.pack(side = "top")
        self.frameBase.pack(side = "top")

        #Pondo a janela em loop
        self.janela.mainloop()

def Chama():
    Janela()

Chama()


import numpy as np
from matplotlib.pylab import *
import lensdemo_funcs as ldf
def pw2(x):
	res = x*x
	return res
def lens_equation_point(x1,x2,re):
	xq = np.sqrt(x1*x1+x2*x2)
	req = re**2.0
	y1 = x1*(1.0-req/xq**2.0)
	y2 = x2*(1.0-req/xq**2.0)
	mu = (1.0-(re/xq)**4.0)**(-1.0)
	return y1,y2,mu
def lens_equation_sie(x1,x2,lpar):
	re  = lpar[4]
	x1  = x1/re
	x2  = x2/re
	xc1 = lpar[0]/re
	xc2 = lpar[1]/re
	q   = lpar[2]
	rc  = lpar[3]/re
	phi = lpar[5]

	phirad = np.deg2rad(phi)
	xt1 = ((x1-xc1)*np.cos(phirad)+(x2-xc2)*np.sin(phirad))
	xt2 = ((x2-xc2)*np.cos(phirad)-(x1-xc1)*np.sin(phirad))

	phi = np.sqrt(xt2*xt2+xt1*xt1*q*q+rc*rc*q*q)
	sq = np.sqrt(1.0-q*q)
	fx1 = sq*xt1/(phi+rc)
	fx2 = sq*xt2/(phi+rc*q*q)
	qs = np.sqrt(q)

	y1 = xt1-qs/sq*np.arctan(fx1)
	y2 = xt2-qs/sq*np.arctanh(fx2)

	dphi_x1 = xt1*q*q/phi
	dphi_x2 = xt2/phi
	dfx1_x1 = sq*(1.0/(phi+rc)-xt1/pw2(phi+rc)*dphi_x1)
	dfx2_x2 = sq*(1.0/(phi+rc*q*q) -xt2/pw2(phi+rc*q*q)*dphi_x2)
	dfx1_x2 = sq*xt1*dphi_x2/pw2(phi+rc)
	dfx2_x1 = sq*xt2*dphi_x1/pw2(phi+rc*q*q)

	psy1_x1 = 1.0-qs/sq/(1.0+fx1*fx1)*dfx1_x1
	psy2_x2 = 1.0-qs/sq/(1.0-fx2*fx2)*dfx2_x2
	psy1_x2 = qs/(sq*(1.0+fx1*fx1))*dfx1_x2
	psy2_x1 = qs/(sq*(1.0-fx2*fx2))*dfx2_x1

	jacobian = psy1_x1*psy2_x2-psy1_x2*psy2_x1
	mu = 1.0/(psy1_x1*psy2_x2-psy1_x2*psy2_x1)

	res1 = (y1-xt1)*np.cos(phirad) - (y2-xt2)*np.sin(phirad)
	res2 = (y2-xt2)*np.cos(phirad) + (y1-xt1)*np.sin(phirad)
	return res1*re,res2*re,jacobian
#----------------------------------------------------------------------
def le_sie_subs(x1,x2,lpar1,lpar2):
	rea  = lpar1[4]
	xac1 = lpar1[0]
	xac2 = lpar1[1]
	qa   = lpar1[2]
	rca  = lpar1[3]/rea
	pha  = lpar1[5]

	reb  = lpar2[4]
	xbc1 = lpar2[0]
	xbc2 = lpar2[1]
	qb   = lpar2[2]
	rcb  = lpar2[3]/reb
	phb  = lpar2[5]
#------------------------------------halo a-----------------------------
	phirada = np.deg2rad(pha)
	xta1 = ((x1-xac1)*np.cos(phirada)+(x2-xac2)*np.sin(phirada))/rea
	xta2 = ((x2-xac2)*np.cos(phirada)-(x1-xac1)*np.sin(phirada))/rea

	phia = np.sqrt(xta2*xta2+xta1*xta1*qa*qa+rca*rca*qa*qa)
	sqa = np.sqrt(1.0-qa*qa)
	fxa1 = sqa*xta1/(phia+rca)
	fxa2 = sqa*xta2/(phia+rca*qa*qa)
	qsa = np.sqrt(qa)

	aa1 = -qsa/sqa*np.arctan(fxa1)
	aa2 = -qsa/sqa*np.arctanh(fxa2)
#--------------------------------halo b---------------------------------
	phiradb = np.deg2rad(phb)
	xtb1 = ((x1-xbc1)*np.cos(phiradb)+(x2-xbc2)*np.sin(phiradb))/reb
	xtb2 = ((x2-xbc2)*np.cos(phiradb)-(x1-xbc1)*np.sin(phiradb))/reb

	phib = np.sqrt(xtb2*xtb2+xtb1*xtb1*qb*qb+rcb*rcb*qb*qb)
	sqb = np.sqrt(1.0-qb*qb)
	fxb1 = sqb*xtb1/(phib+rcb)
	fxb2 = sqb*xtb2/(phib+rcb*qb*qb)
	qsb = np.sqrt(qb)

	ab1 = -qsb/sqb*np.arctan(fxb1)
	ab2 = -qsb/sqb*np.arctanh(fxb2)
#----------------------------------------------------------------------
	resa1 = (aa1)*np.cos(phirada)-(aa2)*np.sin(phirada)
	resa2 = (aa2)*np.cos(phirada)+(aa1)*np.sin(phirada)

	resb1 = (ab1)*np.cos(phiradb)-(ab2)*np.sin(phiradb)
	resb2 = (ab2)*np.cos(phiradb)+(ab1)*np.sin(phiradb)

	res1 = resa1*rea+resb1*reb
	res2 = resa2*rea+resb2*reb
#----------------------------------------------------------------------
	dphia_x1 = xta1*qa*qa/phia
	dphia_x2 = xta2/phia
	dfx1a_x1 = sqa*(1.0/(phia+rca)-xta1/pw2(phia+rca)*dphia_x1)
	dfx2a_x2 = sqa*(1.0/(phia+rca*qa*qa)-xta2/pw2(phia+rca*qa*qa)*dphia_x2)
	dfx1a_x2 = sqa*xta1*dphia_x2/pw2(phia+rca)
	dfx2a_x1 = sqa*xta2*dphia_x1/pw2(phia+rca*qa*qa)

	dphib_x1 = xtb1*qb*qb/phib
	dphib_x2 = xtb2/phib
	dfx1b_x1 = sqb*(1.0/(phib+rcb)-xtb1/pw2(phib+rcb)*dphib_x1)
	dfx2b_x2 = sqb*(1.0/(phib+rcb*qb*qb)-xtb2/pw2(phib+rcb*qb*qb)*dphib_x2)
	dfx1b_x2 = sqb*xtb1*dphib_x2/pw2(phib+rcb)
	dfx2b_x1 = sqb*xtb2*dphib_x1/pw2(phib+rcb*qb*qb)

	#psy1_x1 = 1.0-qsa/(sqa*(1.0+fxa1*fxa1))*dfx1a_x1 \
	#		-qsb/(sqb*(1.0+fxb1*fxb1))*dfx1b_x1
	#psy2_x2 = 1.0-qsa/(sqa*(1.0-fxa2*fxa2))*dfx2a_x2 \
	#		-qsb/(sqb*(1.0-fxb2*fxb2))*dfx2b_x2
	#psy1_x2 = qsa/(sqa*(1.0+fxa1*fxa1))*dfx1a_x2 \
	#		+qsb/(sqb*(1.0+fxb1*fxb1))*dfx1b_x2
	#psy2_x1 = qsa/(sqa*(1.0-fxa2*fxa2))*dfx2a_x1 \
	#		+qsb/(sqb*(1.0-fxb2*fxb2))*dfx2b_x1

	#psy1_x1 = 1.0-qsa/(sqa*(1.0+fxa1*fxa1))*dfx1a_x1
	#psy2_x2 = 1.0-qsa/(sqa*(1.0-fxa2*fxa2))*dfx2a_x2
	#psy1_x2 = qsa/(sqa*(1.0+fxa1*fxa1))*dfx1a_x2
	#psy2_x1 = qsa/(sqa*(1.0-fxa2*fxa2))*dfx2a_x1

	y11p1 = qsa/(sqa*(1.0+fxa1*fxa1))*dfx1a_x1
	y11p2 = qsb/(sqb*(1.0+fxb1*fxb1))*dfx1b_x1
	y22p1 = qsa/(sqa*(1.0-fxa2*fxa2))*dfx2a_x2
	y22p2 = qsb/(sqb*(1.0-fxb2*fxb2))*dfx2b_x2
	y12p1 = qsa/(sqa*(1.0+fxa1*fxa1))*dfx1a_x2
	y12p2 = qsb/(sqb*(1.0+fxb1*fxb1))*dfx1b_x2
	y21p1 = qsa/(sqa*(1.0-fxa2*fxa2))*dfx2a_x1
	y21p2 = qsb/(sqb*(1.0-fxb2*fxb2))*dfx2b_x1

	y11 = 1.0-y11p1-y11p2
	y22 = 1.0-y22p1-y22p2
	y12 = y12p1+y12p2
	y21 = y21p1+y21p2
	jacobian = y11*y22-y12*y21
	mu = 1.0/jacobian

	return res1,res2,mu
#----------------------------------------------------------------------
g_amp = 1.0   # peak brightness value
g_sig = 0.05  # Gaussian "sigma" (i.e., size)
g_xcen = 0.0  # x position of center
g_ycen = 0.0  # y position of center
g_axrat = 1.0 # minor-to-major axis ratio
g_pa = 0.0    # major-axis position angle (degrees) c.c.w. from x axis
gpar = np.asarray([g_amp, g_sig, g_xcen, g_ycen, g_axrat, g_pa])
##----------------------------------------------------------------------
l_xcen = 0.5
l_ycen = 0.5
l_q = 0.99999999999
l_rc = 0.00000000001
l_re = 1.00000000001
l_pa = 0.0
l_par = np.asarray([l_xcen, l_ycen, l_q, l_rc,l_re,l_pa])
#----------------------------------------------------------------------
l_xcens = -0.5
l_ycens = -0.5
l_qs = 0.99999999999
l_rcs = 0.00000000001
l_res = 0.00000000001
l_pas = 0.0
l_pars = np.asarray([l_xcens, l_ycens, l_qs, l_rcs,l_res,l_pas])
#----------------------------------------------------------------------
boxsize = 5.0
nn = 512

ri = np.linspace(0.0,boxsize/2.0,nn)
ti = np.linspace(0.0,2.0*np.pi,nn)
ri,ti = np.meshgrid(ri,ti)

xi1 = ri*np.cos(ti)
xi2 = ri*np.sin(ti)

g_image = ldf.gauss_2d(xi1, xi2, gpar)
(ai1, ai2, mui) = le_sie_subs(xi1,xi2,l_par,l_pars)

yi1 = xi1+ai1
yi2 = xi2+ai2
g_lensimage = ldf.gauss_2d(yi1, yi2, gpar)
#--------------------------lens images contour------------------------
levels = [0.15,0.30,0.45,0.60,0.75,0.9,1.05]
lev2 = [1000]
figure(num=None,figsize=(10,5),dpi=80, facecolor='w', edgecolor='k')

a = axes([0.05,0.1,0.4,0.8])
a.set_xlim(-2.5,2.5)
a.set_ylim(-2.5,2.5)
a.contourf(xi1,xi2,g_image,levels)
a.contour(yi1,yi2,np.abs(mui),lev2,colors=('g'),linewidths = 2.0)

b = axes([0.55,0.1,0.4,0.8])
b.set_xlim(-2.5,2.5)
b.set_ylim(-2.5,2.5)
b.contourf(xi1,xi2,g_lensimage,levels)
b.contour(xi1,xi2,np.abs(mui),lev2,colors=('k'),linewidths = 2.0)
savefig('output.eps')
show()
#------------------------------------------------------
#from mpl_toolkits.mplot3d import axes3d
#fig = figure()
#ax = fig.add_subplot(111, projection='3d')
#ax.plot_wireframe(xi1, xi2, mui, rstride=10, cstride=10)
##ax.plot_surface(xi1, xi2, mui, rstride=10, cstride=10)
#ax.set_zlim3d(-40,40)
#plt.show()
#colorbar(r


def quickSort(list):
	"""Quick sort using list comprehensions"""
	if list == []:
		return []
	else:
		pivot = list[0]
		lesser = quickSort([x for x in list[1:] if x < pivot])
		greater = quickSort([x for x in list[1:] if x > pivot])
		return lesser + [pivot] + greater

# This version of the quick sort algorithm is the fastest and uses recursion


from bs4 import BeautifulSoup
import csv

def clean_str(text):
    return text.strip().replace('\n', '')

number_files = 16
elements = []
for file in range(1, number_files):
    with open(f"messages{file}.html") as f:
        contents = f.read()
        soup = BeautifulSoup(contents, "html.parser")
        messages = soup.find_all(class_="message")
        last_from_name = ''
        last_message_time = ''
        for message in messages:
            for body in message.find_all(class_="body"):
                is_forwarded = ''
                if 'forwarded' in body['class']:
                    is_forwarded = True

                message_time = body.find(class_="pull_right date details")
                if message_time is not None:
                    message_time = message_time['title']
                    last_message_time = message_time
                elif 'service' in message['class']:
                    message_time = last_message_time

                message_text = body.find(class_="text")
                if message_text is not None:
                    message_text = clean_str(message_text.text)
                else:
                    description = message.find(class_="description")
                    if description is not None:
                        if 'Not included, change data' in description.text:
                            message_text = 'Media not included'
                    elif 'details' in body['class']:
                        message_text = clean_str(body.text)

                message_from_name = body.find(class_="from_name")
                if message_from_name is not None:
                    message_from_name = clean_str(message_from_name.text)
                    last_from_name = message_from_name
                elif 'joined' in message['class']:
                    message_from_name = last_from_name
                elif 'service' in message['class']:
                    message_from_name = 'Service message'

                reply_to = body.find(class_="reply_to")
                if reply_to is not None:
                    reply_to = clean_str(reply_to.text)

                if message_time is None and \
                   message_text is None and \
                   message_from_name is None and \
                   reply_to is None:
                    continue

                elements.append([
                    message_time,
                    is_forwarded,
                    message_from_name,
                    message_text,
                    reply_to]
                )

with open('mensajes.csv', 'w') as f:
    writer = csv.writer(f)
    writer.writerows(elements)


from FnAssetAPI.ui.toolkit import QtGui
from FnAssetAPI.ui.widgets import RegistrationManagerOptionsWidget

from .. import FTrackPublish

class FTrackRegistrationOptionsWidget(RegistrationManagerOptionsWidget):
  """

  Presents options that can affect the publishing of assets. This is used for
  time when assets are to be published to pre-determined entity refs, and so we
  have no way of appending options to the ref string. It is also only used in
  situations whereby the context is preserved between UI interaction, and
  registration activities.

  Presently, we use this to allow the user to customise the target task type,
  but we could also allow it to set an initial status, etc...

  NOTE: The context should NEVER be modified directly, all options should be
  passed back through getOptions()

  """

  def __init__(self, specification, context, parent=None):
    super(FTrackRegistrationOptionsWidget, self).__init__(specification,
        context, parent=parent)

    layout = QtGui.QVBoxLayout()
    layout.setContentsMargins(0,0,0,0)

    self.setLayout(layout)
    self._initUI(layout)

  def _initUI(self, layout):

    self._taskTypeCombo = None

    # See what options we need based on the spec

    if self._specification.isOfType("group"):

      # If we don't have any options, hide ourself
      self.setHidden(True)

    else:

      # If we're not a group, assume for now, were some kind of asset

      # Task Type
      # {

      self._taskTypeCombo = QtGui.QComboBox()
      self._taskTypeCombo.addItems(("Compositing", "Editing"))

      # Get a default
      taskType, taskName = FTrackPublish.getTaskTypeAndName(self._specification)
      index = self._taskTypeCombo.findText(taskType)
      if index > -1:
        self._taskTypeCombo.setCurrentIndex(index)

      taskLayout = QtGui.QHBoxLayout()
      taskLayout.addWidget(QtGui.QLabel("Create under task:"))
      taskLayout.addWidget(self._taskTypeCombo)
      layout.addLayout(taskLayout)

      self._taskTypeCombo.currentIndexChanged.connect(self._emitOptionsChanged)
      # }

  def getOptions(self):

    options = {}

    if self._taskTypeCombo:
      options[FTrackPublish.kTaskTypeKey] = str(self._taskTypeCombo.currentText())

    return options

  def setOptions(self, options):

    if self._taskTypeCombo:
      taskType = options.get(FTrackPublish.kTaskTypeKey, None)
      if taskType:
        index = self._taskTypeCombo.findText(taskType)
        if index > -1:
          self._taskTypeCombo.setCurrentIndex(index)


# -*- coding: GB2312 -*-
#Time: 2017/7/19 10:02:40
#Description:
#File Name: aps.py
#!/usr/local/bin/python
import os
from pytz import utc
from apscheduler.schedulers.background import BackgroundScheduler
#from apscheduler.jobstores.mongodb import MongoDBJobStore
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
from apscheduler.executors.pool import ThreadPoolExecutor, ProcessPoolExecutor
#from apscheduler.events import EVENT_JOB_EXECUTED, EVENT_JOB_ERROR
import logging
from datetime import date
#import time
from sqlalchemy import Column
from sqlalchemy.types import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S',
                    filename='apsch.log',
                    filemode='a')

BaseModel = declarative_base()
class ApsJobs(BaseModel):
    __tablename__ = 'ApsJobs'
    id = Column(VARCHAR(30), primary_key=True)
    command = Column(VARCHAR(1000),nullable=False)
    trigger=Column(VARCHAR(20),nullable=False)
    expression = Column(VARCHAR(200),nullable=False)
    flag = Column(VARCHAR(1),default='1',nullable=False)

def init_db(engine):
    BaseModel.metadata.create_all(engine)
def drop_db(engine):
    BaseModel.metadata.drop_all(engine)

def my_listener(event):
    if event.exception:
        print ('task wrong!!! please check logfile apsch.log')
    else:
        pass

#@event.listens_for(ApsJobs,'after_insert')
#def run_task(mapper, connection, target):
def run_task():
    #print(session)
    #print("after_insert")
    print("run_task")
    query= session.query(ApsJobs)
    #print(query.filter(CronApsJobs.flag == '0').all())
    for instance in query.filter(ApsJobs.flag == '1').all():
        sched_time=eval(instance.expression)
        if instance.trigger.startswith("c"):
            scheduler.add_job(func=os.system, args=(instance.command,), trigger='cron', \
                            year=sched_time.get("year","*"),\
                            month=sched_time.get("month","*"),\
                            day=sched_time.get("day","*"),\
                            week=sched_time.get("week","*"),\
                            day_of_week=sched_time.get("day_of_week","*"),\
                            hour=sched_time.get("hour","*"),\
                            minute=sched_time.get("minute","*"),\
                            second=sched_time.get("second","*"),\
                            start_date=sched_time.get("start_date",date(2017, 1, 1)),\
                            end_date=sched_time.get("end_date",date(2099, 12, 31)),\
                            #timezone=sched_time.get("timezone","*"),\
                    id=instance.id,max_instances=5,replace_existing=True)
        elif instance.trigger.startswith("i"):
            scheduler.add_job(func=os.system, args=(instance.command,), trigger='interval', \
                                weeks=sched_time.get("weeks",0),\
                                days=sched_time.get("days",0),\
                                hours=sched_time.get("hours",0),\
                                minutes=sched_time.get("minutes",0),\
                                seconds=sched_time.get("seconds",0),\
                                start_date=sched_time.get("start_date",date(2017, 1, 1)),\
                                end_date=sched_time.get("end_date",date(9999, 12, 31)),\
                                #timezone=sched_time.get("timezone","*"),\
                                id=instance.id,max_instances=5,replace_existing=True)
        else:
            scheduler.add_job(func=os.system, args=(instance.command,), trigger='interval', \
                                run_date=sched_time.get("run_date",date(2017, 1,1)),\
                                #timezone=sched_time.get("timezone","*"),\
                                id=instance.id,max_instances=5,replace_existing=True)
#     session.flush()
#     session.commit()
    query.filter(ApsJobs.flag == '1').update({ApsJobs.flag : '0'})
    session.commit()

        #print (instance.id,instance.command,instance.cron_expression,instance.flag)
#         print(type(instance.cron_expression),instance.cron_expression)
#         print(type(eval(instance.cron_expression)),eval(instance.cron_expression))
#
    #scheduler.add_job(func=os.system, args=('echo%time%',), trigger='cron', second='*/1',id='cron_006',replace_existing=True)

if __name__=='__main__':

    engine = create_engine("sqlite:///apsjobs.db",echo=False)
#     #engine = create_engine('sqlite:///:memory:')
#
    #drop_db(engine)
    init_db(engine)

    DBSession = sessionmaker(bind=engine)
    session = DBSession()
    session.execute("CREATE INDEX  IF NOT EXISTS index_ApsJobs_001 ON ApsJobs(flag)")
    session.commit

    jobstores = {
       'default': SQLAlchemyJobStore(url='sqlite:///apsjobs.db')
    }
    executors = {
       'default': ThreadPoolExecutor(20),
       'processpool': ProcessPoolExecutor(5)
    }
    job_defaults = {
       'coalesce': False,
       'max_instances': 3
    }
    scheduler = BackgroundScheduler(jobstores=jobstores, executors=executors, job_defaults=job_defaults,timezone=utc)

    # scheduler.add_job(func=cron_checksupply, args=('check_suuply000.sh',), trigger='cron', second='*/1',id='cron_001',replace_existing=True)
    # scheduler.add_job(func=cron_checksupply, args=('check_suuply222.sh',), trigger='cron', second='*/1',id='cron_003',replace_existing=True)
    # scheduler.add_job(func=cron_checksupply, args=('check_suuply333.sh',), trigger='cron', second='*/1',id='cron_004',replace_existing=True)
    # scheduler.add_job(func=cron_checksupply, args=('check_suuply444.sh',), trigger='cron', second='*/1',id='cron_005',replace_existing=True)
    scheduler.add_job(func=run_task,trigger='interval', seconds=10,id='0000000',replace_existing=True)

    #scheduler.add_job(func=cron_checkdim_etl_cycle, args=('check_dim_etl_cycle.sh',), trigger='cron',second='*/2',id='cron_002',replace_existing=True)
    #scheduler.add_listener(my_listener, EVENT_JOB_EXECUTED | EVENT_JOB_ERROR)
    scheduler._logger = logging

    scheduler.start()
    try:
        while True:
            input_str = input("please input get,pause,resume,shutdown,start,print\n")
            if input_str == "get":
            # scheduler.print_ApsJobs()
                print(scheduler.get_jobs())
            if input_str.startswith("pause"):
                if input_str=="pauseall":
                    scheduler.pause()
                else:
                    try:
                        scheduler.pause_job(input_str.replace("pause",''))
                    except:
                        print("job id is invalid")
            if input_str.startswith("resume"):
                if input_str=="resumeall":
                    scheduler.resume()
                else:
                    try:
                        scheduler.resume_job(input_str.replace("resume",''))
                    except:
                        print("job id is invalid")
            if input_str.startswith("remove"):
                if input_str=="removeall":
                    #scheduler.remove_all_jobs()
                    print("be carefull!")
                else:
                    try:
                        scheduler.remove_job(input_str.replace("remove",''))
                    except:
                        print("job id is invalid")
            if input_str == "shutdown":
                scheduler.shutdown(wait=False)
            if input_str == "print":
                scheduler.print_jobs()
            if input_str == "start":
                scheduler.start()
            #===================================================================
            # if input_str.startswith("addjob") :
            #     input_str=input_str.replace("addjob", '')
            #===================================================================
            #===================================================================
            # if input_str.startswith("insert") :
            #     job1=ApsJobs(id="cron001",command="echo cron001:%time%",trigger="cron",expression="{'second' : '*/3'}",flag="1")
            #     session.add(job1)
            #     session.commit()
            #===================================================================
    except KeyboardInterrupt:
        scheduler.shutdown(wait=False)


#!/usr/bin/env python
#coding=utf-8
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
import logging, json

client = AcsClient('LTAI7Z5xMiJkqnNV', '2F0VHUeUdmkJ4Msbwxe8hPmA2FWu5u', 'default')

def createRequest(name):

	request = CommonRequest()
	request.set_accept_format('json')
	request.set_domain('alidns.aliyuncs.com')
	request.set_method('POST')
	request.set_protocol_type('https') # https | http
	request.set_version('2015-01-09')
	request.set_action_name(name)

	return request

def describeDomainRecord(domain, RR):
	logging.info("describeDomainRecord:")
	response = describeDomainRecords(domain)
	response = json.loads(str(response, encoding = "utf-8"))
	records = response["DomainRecords"]
	for record in records["Record"]:
		logging.info(record)
		if RR == record["RR"]:
			return record

def describeDomainRecords(domain, pageNumber = 1, pageSize = 500):
	logging.info("describeDomainRecords:")
	request = createRequest('DescribeDomainRecords')

	request.add_query_param('DomainName', domain)

	response = client.do_action(request)
	# python2:  print(response)
	logging.debug(response)

	return response

def addDomainRecord(domain, value, RR, recordType = 'A'):
	createRequest('addDomainRecord')

	request.add_query_param('DomainName', 'hsjrzb.com')
	request.add_query_param('Type', 'A')
	request.add_query_param('Value', '1.1.1.1')
	request.add_query_param('RR', 'www1')

	response = client.do_action(request)
	# python2:  print(response)
	print(str(response, encoding = 'utf-8'))
	# python2:  print(response)
	logging.info(response)
	return response

def updateDomainRecord(recordId, RR, recordType, recordValue):
	logging.info("updateDomainRecord:")
	request = createRequest('UpdateDomainRecord')

	request.add_query_param('RecordId', recordId)
	request.add_query_param('RR', RR)
	request.add_query_param('Type', recordType)
	request.add_query_param('Value', recordValue)

	response = client.do_action(request)
	# python2:  print(response)
	logging.info(str(response, encoding = 'utf-8'))

if __name__ == '__main__':
	logging.basicConfig(level = logging.INFO)
	#addDomainRecord("hsjrzb.com", "1.2.3.4", "www")
	record = describeDomainRecord("404page.org", "ml")
	logging.info(record["RecordId"])

	#updateDomainRecord(record["RecordId"], "ml", "A", "52.81.63.36")

	updateDomainRecord(record["RecordId"], "ml", "A", "54.223.190.111")


# -*- coding: utf-8 -*-
import unidecode
import time
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
from chatterbot.logic import LogicAdapter
from chatterbot.response_selection import get_first_response
from chatterbot.comparisons import levenshtein_distance
from chatterbot.response_selection import *  # get_first_response
from chatterbot.comparisons import *  # levenshtein_distance
from chatterbot import *
import csv
import json
import sys
import os
from flask import Flask, render_template, request
from bs4 import BeautifulSoup
import requests
import re
import nltk
from newspaper import Article
from newspaper import news_pool
from googlesearch import search
import wikipedia
from time import sleep
from logging import *
import logging
import urllib3
import sys
from chatterbot.adapters import Adapter
from chatterbot.storage import StorageAdapter
from chatterbot.search import IndexedTextSearch
from chatterbot.conversation import Statement

# logging.basicConfig(filename="Log_Test_File.txt", level=logging.INFO, filemode='a')
logging.basicConfig(level=logging.INFO)

app = Flask(__name__)

searchbot = ChatBot(
    "Chatterbot", read_only=True,
    input_adapter="chatterbot.input.VariableInputTypeAdapter",
    output_adapter="chatterbot.output.OutputAdapter",
    preprocessors=['chatterbot.preprocessors.clean_whitespace', 'chatterbot.preprocessors.convert_to_ascii'],
    storage_adapter='chatterbot.storage.SQLStorageAdapter',
    database_uri='sqlite:///database.db',
    statement_comparison_function=levenshtein_distance,
    response_selection_method=get_first_response,
    logic_adapters=[
            {
                'import_path': 'chatterbot.logic.BestMatch',
                "statement_comparision_function": "chatterbot.comparisions.levenshtein_distance",
                "response_selection_method": "chatterbot.response_selection.get_first_response",
                'default_response': 'Desculpe, não compreendi a pergunta.',
                'maximum_similarity_threshold': 0.70
            },
]
)

trainer = ListTrainer(searchbot)

conv = open('chats.csv', encoding='utf-8').readlines()
#convj = open('export.json', encoding='utf-8').readlines()

trainer.train(conv)

#trainer.export_for_training('./export.json')

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

@app.route("/get")
# function for the bot response
def get_bot_response():
    while True:
        userText = request.args.get('msg')
        msg = str(userText)
        entrada = msg.lower()
        f = csv.writer(open('inputs.csv', 'a', encoding='utf-8'))
        f.writerow([msg])
        response = searchbot.get_response(userText)
        if float(response.confidence) >= 0.8:
            return str(searchbot.get_response(userText))
        elif userText == str('NÃO'):
            return str('Refaça a pergunta, por favor!')
        elif userText == str("SIM"):
            return str("Agradecemos o seu contato")
        elif float(response.confidence) == 0.0:
            entrada = msg
            # print(entrada)
            p1 = 'http://receita.economia.gov.br/@@busca?advanced_search=False&sort_on=&SearchableText='
            p2 = '&portal_type%3Alist=Document&created.query%3Arecord%3Alist%3Adate=1970-01-02&created.range%3Arecord=min'
            html = str(p1 + entrada + p2)
            stop2 = nltk.corpus.stopwords.words('portuguese')
            stop2.append('faço')
            stop2.append('um')
            stop2.append('gostaria')
            stop2.append('fazer')
            stop2.append('saber')
            stop2.append('posso')
            stop2.append('como')
            splitter = re.compile('\\W+')

            lista_palavras = []
            lista = [p for p in splitter.split(entrada) if p != '']
            for p in lista:
                if p not in stop2:
                    if len(p) > 1:
                        lista_palavras.append(p)
            ar = len(lista_palavras)
            ax = str(lista_palavras[0:ar])
            e = str(ax).replace(',', ' ').strip('[]')
            e.strip("'")
            #headers = {'User-Agent': 'Mozilla/5.0'}
            headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
            try:
                page = requests.get(html, headers=headers, verify=False, stream=False, timeout=7)
                soup = BeautifulSoup(page.content, 'lxml')
                cla = soup.find(class_='searchResults')
                links = cla.find_all('a')
            except (KeyError, IndexError, AttributeError):
                pass
            # namess = soup.find_all('a')
            # ra = (lista_palavras)
            # CRIAR A LISTA DE LINKS SITE RFB

            listr = []
            for link in links:
                texto = str(link.get_text()).lower().replace('ã', 'a').replace('-', ' ').replace('ç', 'c').split()
                time.sleep(0.5)
                # print(len(texto))
                url = str(link.get('href'))
                time.sleep(0.5)
                # print(len(url))
                urls = str(link.get('href')).lower().replace('/', ' ').replace('-', ' ').replace('.', ' ').split()
                time.sleep(0.5)
                # print(len(urls))
                if entrada in texto:
                    listr.append(url)
                for i in range(0, ar):
                    if lista_palavras[i] in texto:
                        listr.append(url)
                    elif lista_palavras[i] in urls:
                        listr.append(url)
            else:
                listr == []
                pass

            listag = []
            rec = 'site:receita.economia.gov.br intitle:' + msg + " -filetype:pdf -.pdf"
            for urla in search(rec, tld='com.br', lang='pt-br', stop=4, pause=8):
                time.sleep(1)
                listag.append(urla)

            g = int(len(listag))
            # print(g)

            listago = []
            for z in range(0, g):
                ur = str(listag[z])
                listago.append(ur)

            # print(listago)
            # print(len(listago))
            qo = int(len(listago))
            # print(listr)
            # print(len(listr))
            listaunida = listago + listr
            conj = list(set(listaunida))
            # print(conj)
            # print(len(conj))
            # print(type(conj))

            # print(p)
            # print(len(p))
            j = len(conj)

            reports2 = []
            news_pool.set(reports2, threads_per_source=2)
            news_pool.join()
            for r in range(0, j):

                try:
                    ia = str(conj[r])
                    article = Article(ia, language="pt")
                    article.download()
                    article.parse()
                    article.text
                    article.nlp()
                    article.summary
                except:
                    pass
                reports2.append(str(article.summary).replace('\n', ' '))
            # print(len(reports2))

            resposta_finalc = set(reports2)
            print(resposta_finalc)

            if resposta_finalc == set():
                wikipedia.set_lang("pt")
                a = msg
                result = wikipedia.search(a, results=1)
                page = wikipedia.summary(result, sentences=6)
                content = page
                return str(content)
            else:
                try:
                    resposta_final = (str(resposta_finalc).replace('\n', ' ').replace('[', ' ').replace(']', ' ').replace(',', ' ').replace("'", ' ').replace('{', ' ').replace("}", ' '))
                    f = csv.writer(open('chats.csv', 'a', encoding='utf-8'))
                    f.writerow([msg + '\n' + resposta_final])
                    return str(resposta_final + '\n' + 'Encontrou a resposta que precisava? SIM ou NÃO?')
                except:
                    return str('Desculpe! Não encontrei uma resposta para sua pergunta. Poderia repetir com outros termos?')

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


from django.shortcuts import render
from django.urls import reverse, reverse_lazy
from django.http import HttpResponse, HttpResponseRedirect
from django.views.generic import UpdateView, ListView, DetailView, UpdateView, DeleteView
from customer.views import CustomSuccessMessageMixin
from django.contrib.auth.mixins import LoginRequiredMixin
from orders.models import Order
from cart.models import Cart
from customer.models import UserProfile
from orders.forms import CheckoutForm

# Create your views here.
class Checkout(CustomSuccessMessageMixin, UpdateView):
    model = Order
    template_name = 'orders/checkout.html'
    success_url = reverse_lazy('home-page')
    success_msg = 'Заказ успешно оформлен! Мы свяжемся с вами для уточнения данных.'
    form_class = CheckoutForm

    def get_object(self, *args, **kwargs):
        order_id = self.request.GET.get('order')
        current_cart_pk = self.request.session['current_cart_pk']
        if not order_id:
            current_cart = Cart.objects.filter(pk = current_cart_pk).first()
            current_buyer = self.request.user
            if current_buyer.is_anonymous:
                current_order, order_created = Order.objects.get_or_create(
                    cart = current_cart,
                    defaults = {
                    'address' : '-',
                    'contact': '-',
                    'phone' : '-',
                    'summ': current_cart.total_summ,
                    'comment':'-',
                    'status': 'Обрабатывается'})
                return current_order
            else:
                current_buyer = self.request.user.profile
                phone = current_buyer.phone
                contact = self.request.user.first_name
                current_order, order_created = Order.objects.get_or_create(
                    cart = current_cart,
                    defaults = {
                    'address' : '-',
                    'contact': contact,
                    'phone' : phone,
                    'summ': current_cart.total_summ,
                    'comment':'-',
                    'status': 'Обрабатывается'})
                return current_order
        else:
            current_order = Order.objects.get(pk = order_id)
            return current_order

    def get_success_url(self):
        self.request.session['current_cart_pk'] = None
        return self.success_url

class CustomerOrderList(LoginRequiredMixin, ListView):
    model = Order
    template_name = 'customer-order-list.html'
    def get_queryset(self):
       current_buyer = self.request.user
       current_buyer_carts = Cart.objects.filter(buyer = current_buyer)
       return current_buyer_carts

class CustomerOrderDetail(LoginRequiredMixin, DetailView):
    model = Order
    template_name = 'customer-order-detail.html'

class CustomerOrderUpdate(CustomSuccessMessageMixin, LoginRequiredMixin, UpdateView):
    model = Order
    form_class = CheckoutForm
    template_name = 'customer-order-update.html'
    success_url = reverse_lazy('orders:customer-order-list')
    success_msg = 'Заказ успешно изменён'

class CustomerOrderDelete(CustomSuccessMessageMixin, LoginRequiredMixin, DeleteView):
    model = Order
    template_name = 'customer-order-confirm-delete.html'
    success_msg = 'Заказ успешно удален'

    def delete(self, request, *args, **kwargs):
        self.object = self.get_object()
        success_url = reverse_lazy('orders:customer-order-list')
        cart_on_delete = Cart.objects.filter(pk = self.object.pk)
        self.object.delete()
        cart_on_delete.delete()
        return HttpResponseRedirect(success_url)

class ManagerOrderList(LoginRequiredMixin, ListView):
    model = Order
    template_name = 'manager-order-list.html'

class ManagerOrderDetail(LoginRequiredMixin, DetailView):
    model = Order
    template_name = 'manager-order-detail.html'

class ManagerOrderUpdate(CustomSuccessMessageMixin, LoginRequiredMixin, UpdateView):
    model = Order
    template_name = 'manager-order-update.html'
    success_url = reverse_lazy('orders:manager-order-list')
    success_msg = 'Заказ успешно изменён'
    fields = ('address', 'phone', 'contact', 'comment', 'status')

class ManagerOrderDelete(CustomSuccessMessageMixin, LoginRequiredMixin, DeleteView):
    model = Order
    template_name = 'manager-order-confirm-delete.html'
    success_msg = 'Заказ успешно удален'

    def delete(self, request, *args, **kwargs):
        self.object = self.get_object()
        success_url = reverse_lazy('orders:manager-order-list')
        cart_on_delete = Cart.objects.filter(pk = self.object.pk)
        self.object.delete()
        cart_on_delete.delete()
        return HttpResponseRedirect(success_url)


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

import serial
import sqlite3
import os
import db
import re
import datetime
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from enum import Enum

class etatEnregistrement(Enum):
      ok = 1
      aValider = 2
      rejete = 3

serLec = serial.Serial('/dev/ttyUSB0', 9600, timeout = 1) #Connexion au lecteur
serBal = serial.Serial('/dev/ttyUSB1', 9600, timeout = 1) #Connexion a la balance

f = open('releveText.txt','w+') #Creation d'un fichier test backup
f.write("Acquisition des donnees \n\n") #Initialisation du fichier texte

if(not os.path.isfile('releve.db')):
      db.setDb();

conn = create_engine('sqlite:///releve.db') #Creation de la base de donnees

session = sessionmaker()

session.configure(bind=conn)

s = session()

nomEspece = "Undulata"

print("Demarrage")

def testIfEntryExists(uidToTest): #Test outarde deja scannee et sauvegardee
      test = s.query(db.Poids).filter(db.Poids.FK_UID == uidToTest).scalar() #requetesqlalchimy

      theOutarde = s.query(db.Outarde).filter(db.Outarde.UID == str(uidToTest).strip()).first()

      print(theOutarde)

      if test is not None:
            print("L'outarde : " + theOutarde.BagueID + " a deja ete scannee")
            return {"isNew" : False, "BagueID" : theOutarde.BagueID} #dictionnaire
      elif theOutarde is not None:
            return {"isNew" : True, "BagueID" : theOutarde.BagueID} #dictionnaire
      else:
            print("Attention outarde non presente dans la base de donnees locale")
            return {"isNew" : False, "BagueID" : "Inconnue"} #dictionnaire

def testPoids(poidsMes):
      testEspece = s.query(db.Ref).filter(db.Ref.Espece == nomEspece).first()
      print("pour infos")
      print(testEspece.PoidsMinImp)
      print(testEspece.PoidsMaxImp)
      print("poids")
      print(poidsMes)
      if testEspece is not None:
            print("L'outarde est de l'espece : " + nomEspece)
            if poidsMes <= testEspece.PoidsMinImp or poidsMes >= testEspece.PoidsMaxImp:
                  print("Ce poids est impossible")
                  return etatEnregistrement.rejete
            elif poidsMes <= testEspece.PoidsMinPath:
                  print("Attention, cet animal est trop maigre par rapport a ses dernieres donnees")
                  return etatEnregistrement.aValider
            elif poidsMes >= testEspece.PoidsMaxPath:
                  print("Attention, cet animal est trop gros par rapport a ses dernieres donnees")
                  return etatEnregistrement.aValider
            else:
                  return etatEnregistrement.ok

def enregistrementPoids(mesure,laSession):
      laSession.add(mesure)
      laSession.commit()

while 1: #Boucle infini
      serLec.flush()
      uid = serLec.readline() #Lecture du port du lecteur
      if uid:
            print("Donnee identifiant acquise")
            strUid = str(uid) #Conversion binaire vers string
            print("stroudell" + strUid)
            result = testIfEntryExists(strUid)
            if(result["isNew"]):
                  print("Donnees identifiant valide")
                  print(result)
                  flagWait = True
                  while flagWait:
                        serBal.flush()
                        poids = serBal.readline() #Lecture du port de la balance
                        if poids:
                              print("Donnee poids acquise pour : " + result["BagueID"])
                              strPoids = str(poids) #Conversion binaire vers string

                              """Partie base de donnees"""

                              uidDB = strUid #Garde que la valeur numerique et l'unite
                              poidsDB = float(re.findall("\d+\.\d+", strPoids)[0])  #Garde que la valeur numerique et l'unite

                              validation = testPoids(poidsDB)

                              if (validation == etatEnregistrement.ok):
                                    newPoids = db.Poids(FK_UID = uidDB, Poids = poidsDB, Date = datetime.datetime.now())
                                    enregistrementPoids(newPoids,s)
                                    flagWait = False

                              elif (validation == etatEnregistrement.aValider):
                                    print("Veulliez valider la commande en appuyant sur Y/N")
                                    tape = "a"
                                    while tape != "Y" or tape!="y" or tape!="N" or tape!= "n":
                                          tape=input()
                                    if tape == "y" or tape =="Y":
                                          newPoids = db.Poids(FK_UID = uidDB, Poids = poidsDB, Date = datetime.datetime.now())
                                          enregistrementPoids(newPoids,s)
                                          flagWait = False
                                    else:
                                          flagWait = False

                              elif (validation == etatEnregistrement.rejete):
                                    flagWait = False

                              """Partie fichier texte"""
                              """Potentiellement useless"""
                              uidText = strUid  #.replace('UFDXB', ' ') #Garde que la valeur numerique
                              poidsText = strPoids  #.replace('W:+', '') #Garde que la valeur numerique

                              f.write(uidText)
                              f.write(poidsText)
                              f.write('\n')
                              f.flush()

serBal.close()
serLec.close()
conn.close()


# uncompyle6 version 3.7.4
# Python bytecode 2.7 (62211)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build/bdist.linux-x86_64/egg/bauble/task.py
# Compiled at: 2016-10-03 09:39:22
"""
The bauble.task module allows you to queue up long running tasks. The
running tasks still block but allows the GUI to update.
"""
import fibra, gtk, bauble, logging
logger = logging.getLogger(__name__)
schedule = fibra.schedule()
__running = False
__kill = False
__message_ids = None

def running():
    """
    Return True/False if a task is running.
    """
    global __running
    return __running

def kill():
    """
    Kill the current task.

    This will kill the task when it goes idle and not while it's
    running.  A task is idle after it yields.
    """
    global __kill
    __kill = True

def _idle():
    """
    Called when a task is idle.
    """
    global __kill
    while gtk.events_pending():
        gtk.main_iteration(block=False)

    if __kill:
        __kill = False
        raise StopIteration()

schedule.register_idle_func(_idle)

def queue(task):
    """Run a task.

    task should be a generator with side effects. it does not matter what it
    yields, it is important that it does stop from time to time yielding
    whatever it wants to, and causing the side effect it has to cause.

    """
    global __running
    schedule.install(task)
    if bauble.gui is not None:
        bauble.gui.set_busy(True)
        bauble.gui.progressbar.show()
        bauble.gui.progressbar.set_pulse_step(1.0)
        bauble.gui.progressbar.set_fraction(0)
    __running = True
    try:
        try:
            schedule.run()
            __running = False
        except:
            raise

    finally:
        __running = False
        if bauble.gui is not None:
            bauble.gui.progressbar.set_pulse_step(0)
            bauble.gui.progressbar.set_fraction(0)
            bauble.gui.progressbar.hide()
            bauble.gui.set_busy(False)
        clear_messages()

    return

__message_ids = []

def set_message(msg):
    """
    A convenience function for setting a message on the
    statusbar. Returns the message id
    """
    global __message_ids
    global _context_id
    if bauble.gui is None or bauble.gui.widgets is None:
        return
    try:
        _context_id
    except NameError as e:
        logger.info(e)
        _context_id = bauble.gui.widgets.statusbar.get_context_id('__task')
        logger.info('new context id: %s' % _context_id)

    msg_id = bauble.gui.widgets.statusbar.push(_context_id, msg)
    __message_ids.append(msg_id)
    return msg_id

def clear_messages():
    """
    Clear all the messages from the statusbar that were set with
    :func:`bauble.task.set_message`
    """
    if bauble.gui is None or bauble.gui.widgets is None or bauble.gui.widgets.statusbar is None:
        return
    for mid in __message_ids:
        bauble.gui.widgets.statusbar.remove(_context_id, mid)

    return


"""Implementation of selection sort Algorithm"""

def SelectionSort(Array):
    for i in range(len(Array)):
        MinIndex=i
        MinValue=Array[i]
        for j in range(i,len(Array)):
            if MinValue>Array[j]:
                MinValue=Array[j]
                MinIndex=j
        temp=Array[i]
        Array[i]=Array[MinIndex]
        Array[MinIndex]=temp
    print(Array)
SelectionSort([22, 11, 99, 88, 9, 7, 42])

"""
in selection sort algorithm 1st we find minimum value and
replace it with 1st element(index 0) in given array,
then find second smallest value in array and replace it
with 2nd position (index 1) and so on..so forth
"""


from flask_restful import Resource, reqparse
from models.rules import RuleModel
from models.network import NetworkModel

class ACL(Resource):
    parser = reqparse.RequestParser()
    parser.add_argument('dl_src', required=False)
    parser.add_argument('dl_dst', required=False)
    parser.add_argument('nw_src', required=False)
    parser.add_argument('nw_dst', required=False)
    parser.add_argument('nw_proto', required=False)
    parser.add_argument('tp_src', required=False)
    parser.add_argument('tp_dst', required=False)
    parser.add_argument('actions', required=False)

    def get(self, role, device):
        if role == 'switch':
            return {'message':'This feature is not applicable for switches.'},400
        elif NetworkModel.fetch_db(role, device):
            rules = RuleModel.get_rules(device)
            if rules:
                return {'rules':[rule.json() for rule in rules]},201
            return {'message':'No rule present for this device'},201
        return {'message':'No such device present with name {}.'.format(device)},404

    def post(self, role, device):
        if NetworkModel.fetch_db(role, device):
            rule = ACL.parser.parse_args()
            if RuleModel.get_rules(device, **rule):
                return {'message':'Rule already exit for the device.'},400
            RuleModel(device, **rule).save_to_db()
            return {'message':'Rule successfully added.'}

        return {'message':'{} device do not exist.'.format(device)},404

    def delete(self, role, device):
        if role == 'switch':
            return {'message':'No rule(s) exist for switch.'},400
        elif NetworkModel.fetch_db(role, device):
            del_rule = ACL.parser.parse_args()
            rules = RuleModel.get_rules(device, **del_rule)
            if rules:
                for rule in rules:
                    rule.delete_from_db()
                return {'message':'Rule is deleted.'},201
            else:
                return {'message':'No rule exist. Please check inputs.'},400


# (C) Copyright 2021-2022 Intel Corporation.
#
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
"""Build crt_utils component"""

LIB_SRC = ['crt_utils.c']

####################################
# Create cart utility share objects
####################################
def build_utility_shared_obj(env):
    """Build cart utility library"""
    cart_utils_objs = env.SharedObject(LIB_SRC, SHOBJPREFIX='s_')
    Export('cart_utils_objs')

def scons():
    """Scons function"""

    Import('env', 'prereqs', 'conf_dir')

    prereqs.require(env, 'protobufc')

    env.d_add_build_rpath()

    # Generate cart utility shared objects
    build_utility_shared_obj(env)

    env.Install(conf_dir, ['memcheck-cart.supp', 'fault-inject-cart.yaml'])

if __name__ == "SCons.Script":
    scons()