Example #1
0
def bisym_mod(vel, evel, guess0, vary, n_it, rstart, rfinal, ring_space,
              frac_pixel, r_back, delta, pixel_scale, r_bar_max, errors,
              config, e_ISM):
    #def bisym_mod(vel, evel, guess0, vary, n_it=5,rstart = 2, rfinal = 55, ring_space = 2, frac_pixel = 0.7, r_back = 20, delta = 1, pixel_scale = 0.2, r_bar_max = 20):

    vrot0, vr20, pa0, inc0, x0, y0, vsys0, vtan, theta_b = guess0
    vmode = "bisymmetric"
    [ny, nx] = vel.shape
    shape = [ny, nx]
    """

		 					BYSIMETRIC MODEL


		"""

    chisq_global = 1e4
    PA, INC, XC, YC, VSYS = 0, 0, 0, 0, 0
    Vrot, Vrad, Vsys, Vtan = [], [], [], []
    R = 0

    if r_back < ring_space:
        r_back = ring_space

    for jj in np.arange(0, r_back, ring_space):
        for it in range(n_it):
            guess = [vrot0, vr20, pa0, inc0, x0, y0, vsys0, 0, theta_b]

            #theta_b = np.arctan(np.tan(theta_b*np.pi/180-pa0*np.pi/180)/np.cos(inc0*np.pi/180))*180/np.pi

            xi_sq_array = np.asarray([])
            N = np.asarray([])

            vrot_model, vrad_model, vtan_model = np.asarray([]), np.asarray(
                []), np.asarray([])
            los_vel = np.array([])
            e_los_vel = np.array([])
            x_pos = np.array([])
            y_pos = np.array([])

            los_vel = []
            e_los_vel = []
            x_pos = []
            y_pos = []
            xy_pos = []
            r = []

            for j in np.arange(rstart, rfinal - jj, ring_space):

                XY_mesh, vel_val, e_vel, f_pixel = pixels(
                    vel,
                    evel,
                    guess,
                    ringpos=j,
                    delta=delta,
                    ring="ARCSEC",
                    pixel_scale=pixel_scale)
                npixels = len(XY_mesh[0])

                if j < 10:
                    f_pixel = 1

                if f_pixel > frac_pixel:

                    if j < r_bar_max:

                        # Create bisymetric model
                        try:
                            w_sys, w_rot, w_rad, w_tan, vrot, vrad, vsys, vtan = M_radial(
                                XY_mesh, 0, 0, pa0, inc0, x0, y0, vsys0, 0,
                                theta_b, vel_val - vsys0, e_vel, vmode)
                            vrot_model, vrad_model, vtan_model = np.append(
                                vrot_model,
                                vrot), np.append(vrad_model, vrad), np.append(
                                    vtan_model, vtan)
                        except (np.linalg.LinAlgError):
                            vrot_model, vrad_model, vtan_model = np.append(
                                vrot_model,
                                100), np.append(vrad_model,
                                                10), np.append(vtan_model, 10)

                    else:

                        # Create ciruclar model
                        w_rot, w_rad, vrot, vrad = M_radial(XY_mesh,
                                                            0,
                                                            0,
                                                            pa0,
                                                            inc0,
                                                            x0,
                                                            y0,
                                                            vsys0,
                                                            0,
                                                            theta_b,
                                                            vel_val - vsys0,
                                                            e_vel,
                                                            vmode="circular")
                        vrot_model, vrad_model, vtan_model = np.append(
                            vrot_model,
                            vrot), np.append(vrad_model,
                                             0), np.append(vtan_model, 0)

                    r.append(j)
                    los_vel.append(vel_val)
                    e_los_vel.append(e_vel)
                    x_pos.append(XY_mesh[0])
                    y_pos.append(XY_mesh[1])
                    xy_pos.append(XY_mesh)

            guess = [
                vrot_model + 1, vrad_model + 1, pa0, inc0, x0, y0, vsys0,
                vtan_model + 1, theta_b
            ]

            vrot, vrad, vsys0, pa0, inc0, x0, y0, vtan, theta_b, xi_sq, n_data = fit(
                shape,
                los_vel,
                e_los_vel,
                xy_pos,
                guess,
                vary,
                vmode,
                config,
                fit_method="Powell",
                r_bar_max=r_bar_max,
                e_ISM=e_ISM)

            if xi_sq < chisq_global:

                PA, INC, XC, YC, VSYS, THETA = pa0, inc0, x0, y0, vsys0, theta_b
                LOS_VEL, eLOS_VEL = los_vel, e_los_vel
                Vrot = vrot
                Vrad = vrad
                Vtan = vtan
                R = r
                XY_PIX_POS = xy_pos
                chisq_global = xi_sq
                #print("chisq_global1 = ", chisq_global)

            if it == n_it - 1:

                vrot_poly = legendre(R, Vrot)
                vrad_poly = legendre(R, Vrad)
                vtan_poly = legendre(R, Vtan)
                guess_end = [
                    vrot_poly, vrad_poly, PA, INC, XC, YC, VSYS, vtan_poly,
                    THETA
                ]
                guess_end = [
                    vrot_poly, Vrad, PA, INC, XC, YC, VSYS, Vtan, THETA
                ]
                vary_end = [
                    True, True, False, False, False, False, False, True, False
                ]
                #vary_end = [True,True,True,True,True,True,True,True,True]

                Vrot, Vrad, vsys0, pa0, inc0, x0, y0, Vtan, theta_b, xi_sq, n_data = fit(
                    shape,
                    LOS_VEL,
                    eLOS_VEL,
                    XY_PIX_POS,
                    guess_end,
                    vary_end,
                    vmode,
                    "",
                    fit_method="Powell",
                    r_bar_max=r_bar_max,
                    e_ISM=e_ISM)

                #print("chisq_global2=", xi_sq)

                MODEL_not_interp = np.zeros((ny, nx))
                N = len(LOS_VEL)
                for k in range(N):
                    for mm, nn in zip(XY_PIX_POS[k][0], XY_PIX_POS[k][1]):
                        MODEL_not_interp[nn][mm] = vlos(
                            mm, nn, Vrot[k], Vrad[k], PA, INC, XC, YC, VSYS,
                            Vtan[k], THETA, vmode) - VSYS

                MODEL_interp = vlos_interp(XY_PIX_POS, R, Vrot, Vrad, PA, INC,
                                           XC, YC, VSYS, Vtan, THETA, vmode,
                                           shape, pixel_scale)

                # For error estimation:
                LoS, eLoS, XY_pixels = los_vel, e_los_vel, xy_pos
                best = guess

    MODEL_not_interp[MODEL_not_interp == 0] = np.nan
    MODELS = [MODEL_interp, MODEL_not_interp]

    R = np.asarray(R)
    Vtan = np.asarray(Vtan)
    Vrad = np.asarray(Vrad)
    Vrot = np.asarray(Vrot)
    PA_bar_major = pa_bar_sky(PA, INC, THETA)
    PA_bar_minor = pa_bar_sky(PA, INC, THETA - 90)

    if errors == 1:
        from mcmc import fit_mcmc
        res_mcmc = fit_mcmc(shape,
                            LOS_VEL,
                            eLOS_VEL,
                            XY_PIX_POS,
                            guess_end,
                            vary_end,
                            vmode,
                            "",
                            fit_method="emcee",
                            e_ISM=e_ISM)
    else:
        res_mcmc = Vrot * 0, [Vrot * 0, Vrot * 0], Vrot * 0, [
            Vrot * 0, Vrot * 0
        ], 0, [0, 0], 0, [0, 0], 0, [0, 0], 0, [0, 0], 0, [0, 0], 0, [
            0, 0
        ], Vrot * 0, [Vrot * 0, Vrot * 0]

    return PA, INC, XC, YC, VSYS, THETA, R, Vrot, Vrad, Vtan, MODELS, PA_bar_major, PA_bar_minor, chisq_global, res_mcmc
def circ_mod(vel, evel, guess0, vary, n_it, rstart, rfinal, ring_space,
             frac_pixel, delta, pixel_scale, bar_min_max, errors, config,
             e_ISM):

    vrot0, vr20, pa0, inc0, x0, y0, vsys0, vtan, theta_b = guess0
    vmode = "circular"
    [ny, nx] = vel.shape
    shape = [ny, nx]
    """

		 					CIRCULAR MODEL


		"""

    chisq_global = 1e10
    PA, INC, XC, YC, VSYS = 0, 0, 0, 0, 0
    Vrot, Vrad, Vsys, Vtan = [], [], [], []
    R = 0
    best_xy_pix = []

    rings = np.arange(rstart, rfinal, ring_space)

    for it in np.arange(n_it):
        guess = [vrot0, vr20, pa0, inc0, x0, y0, vsys0, 0, theta_b]

        vrot_tab, vrad_tab, vtan_tab = np.asarray([]), np.asarray(
            []), np.asarray([])
        index = 0

        nrings = len(rings)
        n_annulus = nrings - 1

        R_pos = np.asarray([])
        for ring in rings:

            from pixel_params import pixels
            fpix = pixels(shape,
                          vel,
                          pa0,
                          inc0,
                          x0,
                          y0,
                          ring,
                          delta=delta,
                          pixel_scale=pixel_scale)
            if fpix > frac_pixel:

                v_rot_k, v_rad_k, v_tan_k = M_tab(pa0,
                                                  inc0,
                                                  x0,
                                                  y0,
                                                  theta_b,
                                                  ring,
                                                  delta,
                                                  index,
                                                  shape,
                                                  vel - vsys0,
                                                  evel,
                                                  pixel_scale=pixel_scale,
                                                  vmode=vmode)

                vrot_tab = np.append(vrot_tab, v_rot_k)
                R_pos = np.append(R_pos, ring)

        if np.nanmean(vrot_tab) < 0:
            vrot_tab = abs(vrot_tab)
            pa0 = pa0 - 180
            if pa0 < 0: pa0 = pa0 + 360

        guess = [
            vrot_tab + 1, vrad_tab + 1, pa0, inc0, x0, y0, vsys0, vtan_tab,
            theta_b
        ]
        v_2D_mdl, kin_2D_modls, vrot, vsys0, pa0, inc0, x0, y0, xi_sq, n_data, Errors = fit(
            shape,
            vel,
            evel,
            guess,
            vary,
            vmode,
            config,
            R_pos,
            fit_method="powell",
            e_ISM=e_ISM,
            pixel_scale=pixel_scale,
            ring_space=ring_space)

        if xi_sq < chisq_global:

            PA, INC, XC, YC, VSYS, THETA = pa0, inc0, x0, y0, vsys0, theta_b
            Vrot = vrot
            chisq_global = xi_sq
            best_vlos_2D_model = v_2D_mdl
            best_kin_2D_models = kin_2D_modls
            Rings = R_pos
            std_errors = Errors
            GUESS = [Vrot, 0, PA, INC, XC, YC, VSYS, 0, 0]

    Vrot = np.array(Vrot)

    if errors == 1:

        from mcmc import fit_mcmc
        res_mcmc = fit_mcmc(shape,
                            vel,
                            evel,
                            GUESS,
                            vary,
                            vmode,
                            "",
                            Rings,
                            fit_method="emcee",
                            e_ISM=e_ISM,
                            pixel_scale=pixel_scale,
                            ring_space=ring_space)

    else:

        std_Vrot, std_Vrad, std_pa, std_inc, std_x0, std_y0, std_Vsys, std_theta, std_Vtan = std_errors

        res_mcmc = Vrot * 0, [std_Vrot, std_Vrot], Vrot * 0, [
            std_Vrad, std_Vrad
        ], 0, [std_pa,
               std_pa], 0, [std_inc, std_inc], 0, [std_x0, std_x0], 0, [
                   std_y0, std_y0
               ], 0, [std_Vsys,
                      std_Vsys], 0, [std_theta, std_theta
                                     ], Vrot * 0, [std_Vtan, std_Vtan]

    return PA, INC, XC, YC, VSYS, 0, Rings, Vrot, 0 * Vrot, 0 * Vrot, best_vlos_2D_model, best_kin_2D_models, chisq_global, res_mcmc
def bisym_mod(vel, evel, guess0, vary, n_it, rstart, rfinal, ring_space,
              frac_pixel, delta, pixel_scale, bar_min_max, errors, config,
              e_ISM):
    #def bisym_mod(vel, evel, guess0, vary, n_it=5,rstart = 2, rfinal = 55, ring_space = 2, frac_pixel = 0.7, r_back = 20, delta = 1, pixel_scale = 0.2, r_bar_max = 20):

    vrot0, vr20, pa0, inc0, x0, y0, vsys0, vtan, theta_b = guess0
    vmode = "bisymmetric"
    [ny, nx] = vel.shape
    shape = [ny, nx]
    r_bar_min, r_bar_max = bar_min_max
    """

		 					BYSIMETRIC MODEL


		"""

    chisq_global = 1e10
    PA, INC, XC, YC, VSYS = 0, 0, 0, 0, 0
    Vrot, Vrad, Vsys, Vtan = [], [], [], []
    R = 0
    best_xy_pix = []

    rings = np.arange(rstart, rfinal, ring_space)

    for it in np.arange(n_it):
        guess = [vrot0, vr20, pa0, inc0, x0, y0, vsys0, 0, theta_b]

        vrot_tab, vrad_tab, vtan_tab = np.asarray([]), np.asarray(
            []), np.asarray([])
        index = 0

        nrings = len(rings)
        n_annulus = nrings - 1

        R_pos = np.asarray([])

        for ring in rings:

            from pixel_params import pixels
            fpix = pixels(shape,
                          vel,
                          pa0,
                          inc0,
                          x0,
                          y0,
                          ring,
                          delta=delta,
                          pixel_scale=pixel_scale)

            if fpix > frac_pixel:

                if ring >= r_bar_min and ring <= r_bar_max:

                    # Create bisymetric model
                    try:

                        v_rot_k, v_rad_k, v_tan_k = M_tab(
                            pa0,
                            inc0,
                            x0,
                            y0,
                            theta_b,
                            ring,
                            delta,
                            index,
                            shape,
                            vel - vsys0,
                            evel,
                            pixel_scale=pixel_scale,
                            vmode=vmode)

                        vrot_tab = np.append(vrot_tab, v_rot_k)
                        vrad_tab = np.append(vrad_tab, v_rad_k)
                        vtan_tab = np.append(vtan_tab, v_tan_k)
                        R_pos = np.append(R_pos, ring)

                    except (np.linalg.LinAlgError):
                        vrot_tab, vrad_tab, vtan_tab = np.append(
                            vrot_tab,
                            100), np.append(vrad_tab,
                                            10), np.append(vtan_tab, 10)
                        R_pos = np.append(R_pos, ring)
                else:

                    # Create ciruclar model

                    v_rot_k, v_rad_k, v_tan_k = M_tab(pa0,
                                                      inc0,
                                                      x0,
                                                      y0,
                                                      theta_b,
                                                      ring,
                                                      delta,
                                                      index,
                                                      shape,
                                                      vel - vsys0,
                                                      evel,
                                                      pixel_scale=pixel_scale,
                                                      vmode="circular")
                    vrot_tab = np.append(vrot_tab, v_rot_k)
                    vrad_tab = np.append(vrad_tab, 0)
                    vtan_tab = np.append(vtan_tab, 0)
                    R_pos = np.append(R_pos, ring)

        if np.nanmean(vrot_tab) < 0:
            vrot_tab = abs(vrot_tab)
            pa0 = pa0 - 180
            if pa0 < 0: pa0 = pa0 + 360

        guess = [
            vrot_tab, vrad_tab, pa0, inc0, x0, y0, vsys0, vtan_tab, theta_b
        ]
        v_2D_mdl, kin_2D_modls, vrot, vrad, vsys0, pa0, inc0, x0, y0, vtan, theta_b, xi_sq, n_data, Errors = fit(
            shape,
            vel,
            evel,
            guess,
            vary,
            vmode,
            config,
            R_pos,
            fit_method="Powell",
            e_ISM=e_ISM,
            pixel_scale=pixel_scale,
            ring_space=ring_space)

        if xi_sq < chisq_global:

            PA, INC, XC, YC, VSYS, THETA = pa0, inc0, x0, y0, vsys0, theta_b
            Vrot = vrot
            Vrad = vrad
            Vtan = vtan
            chisq_global = xi_sq
            best_vlos_2D_model = v_2D_mdl
            best_kin_2D_models = kin_2D_modls

            Rings = R_pos
            std_errors = Errors
            GUESS = [
                vrot_tab, vrad_tab, PA, INC, XC, YC, VSYS, vtan_tab, THETA
            ]
            GUESS = [Vrot, Vrad, PA, INC, XC, YC, VSYS, Vtan, THETA]

    # Maybe the code has found the bar minor axis, in such case Vrad <0 and Vtan <0

    if np.nanmean(Vrad) < 0 or np.nanmean(Vtan) < 0:
        GUESS[-1] = GUESS[-1] - 90
        NEW = GUESS[-1]
        if NEW < 0: GUESS[-1] = 180 + NEW

        VARY = [True, True, False, False, False, False, False, True, True]
        v_2D_mdl_, kin_2D_modls_, vrot_, vrad_, _vsys0_, pa0_, inc0_, x0_, y0_, vtan_, theta_b_, xi_sq_, n_data_, Errors_ = fit(
            shape,
            vel,
            evel,
            GUESS,
            VARY,
            vmode,
            config,
            Rings,
            fit_method="Powell",
            e_ISM=e_ISM,
            pixel_scale=pixel_scale,
            ring_space=ring_space)
        if xi_sq_ < chisq_global:
            Vrot = vrot_
            Vrad = vrad_
            Vtan = vtan_
            THETA = theta_b_
            best_vlos_2D_model = v_2D_mdl_
            best_kin_2D_models = kin_2D_modls_
            std_errors = Errors_

    Vtan = np.asarray(Vtan)
    Vrad = np.asarray(Vrad)
    Vrot = np.asarray(Vrot)
    PA_bar_major = pa_bar_sky(PA, INC, THETA)
    PA_bar_minor = pa_bar_sky(PA, INC, THETA - 90)

    if errors == 1:
        from mcmc import fit_mcmc
        res_mcmc = fit_mcmc(shape,
                            vel,
                            evel,
                            GUESS,
                            vary,
                            vmode,
                            "",
                            Rings,
                            fit_method="emcee",
                            e_ISM=e_ISM,
                            pixel_scale=pixel_scale,
                            ring_space=ring_space)
    else:
        std_Vrot, std_Vrad, std_pa, std_inc, std_x0, std_y0, std_Vsys, std_theta, std_Vtan = std_errors

        res_mcmc = Vrot * 0, [std_Vrot, std_Vrot], Vrot * 0, [
            std_Vrad, std_Vrad
        ], 0, [std_pa,
               std_pa], 0, [std_inc, std_inc], 0, [std_x0, std_x0], 0, [
                   std_y0, std_y0
               ], 0, [std_Vsys,
                      std_Vsys], 0, [std_theta, std_theta
                                     ], Vrot * 0, [std_Vtan, std_Vtan]

    return PA, INC, XC, YC, VSYS, THETA, Rings, Vrot, Vrad, Vtan, best_vlos_2D_model, best_kin_2D_models, PA_bar_major, PA_bar_minor, chisq_global, res_mcmc