NAME:

       cyl_to_rect

    PURPOSE:

       convert from cylindrical to rectangular coordinates

    INPUT:

       R, phi, Z - cylindrical coordinates

    OUTPUT:

       X,Y,Z

    HISTORY:

       2011-02-23 - Written - Bovy (NYU)
968877
def cyl_to_rect(R,phi,Z):
    
    return (R*sc.cos(phi),R*sc.sin(phi),Z)
162476
def rect2pathd(rect):
    """"""
    x0, y0 = float(rect.get('x', 0)), float(rect.get('y', 0))
    w, h = float(rect.get('width', 0)), float(rect.get('height', 0))
    x1, y1 = x0 + w, y0
    x2, y2 = x0 + w, y0 + h
    x3, y3 = x0, y0 + h

    d = ("M{} {} L {} {} L {} {} L {} {} z"
         "".format(x0, y0, x1, y1, x2, y2, x3, y3))
    return d
1115171
def wxRect_to_Rect(self, wr):
        
        r = fitz.Rect(wr.x, wr.y, wr.x + wr.width, wr.y + wr.height)
        return r * self.shrink
784449
def transform(self, y):
        
        if self.transform_type == 'log':
            return np.log(y)
        elif self.transform_type == 'exp':
            return np.exp(y)
        elif self.transform_type == 'sqrt':
            return np.sqrt(y)
        elif self.transform_type == 'sin':
            return np.sin(y)
        elif self.transform_type == 'cos':
            return np.cos(y)
        elif self.transform_type == 'tan':
            return np.tan(y)
        elif self.transform_type == 'abs':
             return np.abs(y)
705141
def to_aperture(self):
        

        from .rectangle import RectangularAperture

        xpos = (self.extent[1] + self.extent[0]) / 2.
        ypos = (self.extent[3] + self.extent[2]) / 2.
        xypos = (xpos, ypos)
        h, w = self.shape

        return RectangularAperture(xypos, w=w, h=h, theta=0.)
1074259
def xyz2lonlat(x__, y__, z__):
    
    R = 6370997.0
    lons = da.rad2deg(da.arccos(x__ / da.sqrt(x__ ** 2 + y__ ** 2))) * da.sign(y__)
    lats = da.sign(z__) * (90 - da.rad2deg(da.arcsin(da.sqrt(x__ ** 2 + y__ ** 2) / R)))

    return lons, lats
1084220
def to(self, unit):
        """"""
        from astropy.units import rad
        return (self.radians * rad).to(unit)

        # Or should this do:
        from astropy.coordinates import Angle
        from astropy.units import rad
        return Angle(self.radians, rad).to(unit)
562632
def console_print_rect_ex(
    con: tcod.console.Console,
    x: int,
    y: int,
    w: int,
    h: int,
    flag: int,
    alignment: int,
    fmt: str,
) -> int:
    
    return int(
        lib.TCOD_console_printf_rect_ex(
            _console(con), x, y, w, h, flag, alignment, _fmt(fmt)
        )
    )
932534
def coords_to_vec(lon, lat):
    
    phi = np.radians(lon)
    theta = (np.pi / 2) - np.radians(lat)
    sin_t = np.sin(theta)
    cos_t = np.cos(theta)

    xVals = sin_t * np.cos(phi)
    yVals = sin_t * np.sin(phi)
    zVals = cos_t

    # Stack them into the output array
    out = np.vstack((xVals, yVals, zVals)).swapaxes(0, 1)
    return out
404882
def transSect(theta=None, rho=None, gamma=None):
    
    m = np.eye(6, 6, dtype=np.float64)
    if None in (theta, rho, gamma):
        print("warning: 'theta', 'rho', 'gamma' should be positive float numbers.")
        return m
    else:
        rc = rho * np.cos(theta)
        rs = rho * np.sin(theta)
        m[0, 0] = m[1, 1] = rc / rho
        m[0, 1] = rs
        m[0, 5] = rho - rc
        m[1, 0] = -np.sin(theta) / rho
        m[1, 5] = rs / rho
        m[2, 3] = rho * np.sin(theta)
        m[4, 0] = m[1, 5]
        m[4, 1] = m[0, 5]
        m[4, 5] = rho * np.sin(theta) / gamma / gamma - rho * theta + rs
        return m
1088823
def ZernikeSequence(x,y,maxRadial=5,epsilon=1e-10):
    
    if maxRadial>5:
        raise ValueError('Code for higher radial orders not implemented')
    # Derive radius and exp(i*theta) 
    temp = x + 1j*y
    r1 = np.abs(temp)
    e1 = temp/(r1+epsilon)
     # Generate powers of r recursively
    r2 = r1*r1
    r3 = r2*r1
    r4 = r3*r1
    r5 = r4*r1
    # Generate cos and sin terms recursively from exp(i*theta)
    e2 = e1*e1
    e3 = e2*e1
    e4 = e3*e1
    e5 = e4*e1
    ctheta = e1.real
    stheta = e1.imag
    c2theta = e2.real
    s2theta = e2.imag
    c3theta = e3.real
    s3theta = e3.imag
    c4theta = e4.real
    s4theta = e4.imag
    c5theta = e5.real
    s5theta = e5.imag
    # Generate all the zernikes
    zernike = np.zeros((21,)+x.shape )
    zernike[0] =  1.0
    zernike[1] =  2.0*r1*ctheta
    zernike[2] =  2.0*r1*stheta
    zernike[3] =  sqrt(3.0)*(2.0*r2 - 1.0)
    zernike[4] =  sqrt(6.0)*r2*s2theta
    zernike[5] =  sqrt(6.0)*r2*c2theta
    zernike[6] =  sqrt(8.0)*(3.0*r3 - 2.0*r1)*stheta
    zernike[7] =  sqrt(8.0)*(3.0*r3 - 2.0*r1)*ctheta
    zernike[8] =  sqrt(8.0)*r3*s3theta
    zernike[9] = sqrt(8.0)*r3*c3theta
    zernike[10] = sqrt(5.0)*(6.*r4 - 6.*r2 + 1.)
    zernike[11] = sqrt(10.)*(4.*r4 - 3.*r2)*c2theta
    zernike[12] = sqrt(10.)*(4.*r4 - 3.*r2)*s2theta
    zernike[13] = sqrt(10.)*r4*c4theta
    zernike[14] = sqrt(10.)*r4*s4theta
    zernike[15] = sqrt(12.)*(10*r5-12*r3+3*r1)*ctheta
    zernike[16] = sqrt(12.)*(10*r5-12*r3+3*r1)*stheta
    zernike[17] = sqrt(12.)*(5*r5-4*r3)*c3theta
    zernike[18] = sqrt(12.)*(5*r5-4*r3)*s3theta
    zernike[19] = sqrt(12.)*r5*c5theta
    zernike[20] = sqrt(12.)*r5*s5theta
    # Make zernike zero outside unit circle (useful for dot product)
    zernike = zernike*np.less_equal(r1, 1.0)
    return(zernike[:NumZernike(maxRadial)])
516229
private int[] rect() {
		 if(np==0)return null;
		int xMin = x[0], yMin=y[0], xMax=x[0],yMax=y[0];

		 for(int i=1;i<np;i++) {
			 if(x[i]<xMin)xMin=x[i];
			 else if(x[i]>xMax)xMax=x[i];
			 if(y[i]<yMin)yMin=y[i];
			 else if(y[i]>yMax)yMax=y[i];
		 }

		 return new int[] { xMin, yMin, xMax-xMin, yMax-yMin };
	}
135006
def convert_coordinates(coords, origin, wgs84, wrapped):
    
    if isinstance(coords, list) or isinstance(coords, tuple):
        try:
            if isinstance(coords[0], list) or isinstance(coords[0], tuple):
                return [convert_coordinates(list(c), origin, wgs84, wrapped) for c in coords]
            elif isinstance(coords[0], float):
                c = list(transform(origin, wgs84, *coords))
                if wrapped and c[0] < -170:
                    c[0] = c[0] + 360
                return c

        except IndexError:
            pass

    return None
1295132
def jac(self, xy=None):
        
        if xy is not None:
            self.set_xy(xy)

        ux = numpy.dot(self.ax, self.xy_list[: len(self.ax)])
        vx = numpy.dot(self.bx, self.xy_list[: len(self.bx)])
        uy = numpy.dot(self.ay, self.xy_list[: len(self.ay)])
        vy = numpy.dot(self.by, self.xy_list[: len(self.by)])

        ux_dx = numpy.dot(self.ax, self.dx_list[: len(self.ax)])
        vx_dx = numpy.dot(self.bx, self.dx_list[: len(self.bx)])
        uy_dx = numpy.dot(self.ay, self.dx_list[: len(self.ay)])
        vy_dx = numpy.dot(self.by, self.dx_list[: len(self.by)])

        ux_dy = numpy.dot(self.ax, self.dy_list[: len(self.ax)])
        vx_dy = numpy.dot(self.bx, self.dy_list[: len(self.bx)])
        uy_dy = numpy.dot(self.ay, self.dy_list[: len(self.ay)])
        vy_dy = numpy.dot(self.by, self.dy_list[: len(self.by)])

        jac = numpy.array(
            [
                [
                    (ux_dx * vx - vx_dx * ux) / vx ** 2,
                    (ux_dy * vx - vx_dy * ux) / vx ** 2,
                ],
                [
                    (uy_dx * vy - vy_dx * uy) / vy ** 2,
                    (uy_dy * vy - vy_dy * uy) / vy ** 2,
                ],
            ]
        )
        return jac
17442
def console_rect(
    con: tcod.console.Console,
    x: int,
    y: int,
    w: int,
    h: int,
    clr: bool,
    flag: int = BKGND_DEFAULT,
) -> None:
    
    lib.TCOD_console_rect(_console(con), x, y, w, h, clr, flag)
1211680
def rectified(self):
        
        return Rect(np.minimum(self.mins, self.maxes),
                    np.maximum(self.maxes, self.mins))
1259465
def rd2xy(input,ra=None,dec=None,coordfile=None,colnames=None,
            precision=6,output=None,verbose=True):
    
    single_coord = False
    if coordfile is not None:
        if colnames in blank_list:
            colnames = ['c1','c2']
        elif isinstance(colnames,type('a')):
            colnames = colnames.split(',')
        # convert input file coordinates to lists of decimal degrees values
        xlist,ylist = tweakutils.readcols(coordfile,cols=colnames)
    else:
        if isinstance(ra,np.ndarray):
            ralist = ra.tolist()
            declist = dec.tolist()
        elif not isinstance(ra, list):
            ralist = [ra]
            declist = [dec]
        else:
            ralist = ra
            declist = dec
        xlist  = [0]*len(ralist)
        ylist = [0]*len(ralist)
        if len(xlist) == 1:
            single_coord = True
        for i,(r,d) in enumerate(zip(ralist,declist)):
            # convert input value into decimal degrees value
            xval,yval = tweakutils.parse_skypos(r,d)
            xlist[i] = xval
            ylist[i] = yval

    # start by reading in WCS+distortion info for input image
    inwcs = wcsutil.HSTWCS(input)
    if inwcs.wcs.is_unity():
        print("####\nNo valid WCS found in {}.\n  Results may be invalid.\n####\n".format(input))

    # Now, convert pixel coordinates into sky coordinates
    try:
        outx,outy = inwcs.all_world2pix(xlist,ylist,1)
    except RuntimeError:
        outx,outy = inwcs.wcs_world2pix(xlist,ylist,1)

    # add formatting based on precision here...
    xstr = []
    ystr = []
    fmt = "%."+repr(precision)+"f"
    for x,y in zip(outx,outy):
        xstr.append(fmt%x)
        ystr.append(fmt%y)

    if verbose or (not verbose and util.is_blank(output)):
        print ('# Coordinate transformations for ',input)
        print('# X      Y         RA             Dec\n')
        for x,y,r,d in zip(xstr,ystr,xlist,ylist):
            print("%s  %s    %s  %s"%(x,y,r,d))

    # Create output file, if specified
    if output:
        f = open(output,mode='w')
        f.write("# Coordinates converted from %s\n"%input)
        for x,y in zip(xstr,ystr):
            f.write('%s    %s\n'%(x,y))
        f.close()
        print('Wrote out results to: ',output)

    if single_coord:
        outx = outx[0]
        outy = outy[0]

    return outx, outy