def test_darPlusDistortion():
    data_dir = module_dir + '/distortion/'
    file_geox_darunfix = data_dir + 'nirc2dist_xgeoim.fits'
    file_geoy_darunfix = data_dir + 'nirc2dist_ygeoim.fits'

    data_dir = '/u/ghezgroup/data/m92_test/08jul_new_on/'
    file_geox_darfix = data_dir + 'reduce/kp/gc_f1/ce0249geo_x.fits'
    file_geoy_darfix = data_dir + 'reduce/kp/gc_f1/ce0249geo_y.fits'

    xon = pyfits.getdata(file_geox_darfix)
    yon = pyfits.getdata(file_geoy_darfix)
    xoff = pyfits.getdata(file_geox_darunfix)
    yoff = pyfits.getdata(file_geoy_darunfix)

    # Make arrays with the coordinates for each
    imgsize = 1024
    axisX = np.arange(imgsize, dtype=float)
    axisY = np.arange(imgsize, dtype=float)
    xcoo2d, ycoo2d = np.meshgrid(axisX, axisY)

    # Lets trim so that we only keep every 20th pixel
    idx = np.arange(25, imgsize, 50)
    xon = xon.take(idx, axis=0).take(idx, axis=1)
    yon = yon.take(idx, axis=0).take(idx, axis=1)
    xoff = xoff.take(idx, axis=0).take(idx, axis=1)
    yoff = yoff.take(idx, axis=0).take(idx, axis=1)
    xcoo2d = xcoo2d.take(idx, axis=0).take(idx, axis=1)
    ycoo2d = ycoo2d.take(idx, axis=0).take(idx, axis=1)

    # Calculate differences
    xdiff = xon - xoff
    ydiff = yon - yoff

    # Make vector plots
    py.clf()
    qvr = py.quiver2([xcoo2d], [ycoo2d], [xdiff], [ydiff],
                     units='width',
                     scale=5,
                     width=0.005,
                     headwidth=3,
                     headlength=3,
                     headaxislength=3)
    py.quiverkey(qvr, 100, 1120, 1.0, '1 pixel', coordinates='data', color='r')
    py.xlabel('NIRC2 X (pixel)')
    py.ylabel('NIRC2 Y (pixel)')
    py.title('Arrows point to DAR Fix')
    #py.savefig('plots/vector_daroffon.png')
    py.show()
Exemple #2
0
def test_darPlusDistortion():
    data_dir = module_dir + '/distortion/'
    file_geox_darunfix = data_dir + 'nirc2dist_xgeoim.fits'
    file_geoy_darunfix = data_dir + 'nirc2dist_ygeoim.fits'

    data_dir = '/u/ghezgroup/data/m92_test/08jul_new_on/'
    file_geox_darfix = data_dir + 'reduce/kp/gc_f1/ce0249geo_x.fits'
    file_geoy_darfix = data_dir + 'reduce/kp/gc_f1/ce0249geo_y.fits'

    xon = pyfits.getdata(file_geox_darfix)
    yon = pyfits.getdata(file_geoy_darfix)
    xoff = pyfits.getdata(file_geox_darunfix)
    yoff = pyfits.getdata(file_geoy_darunfix)

    # Make arrays with the coordinates for each 
    imgsize = 1024
    axisX = np.arange(imgsize, dtype=float)
    axisY = np.arange(imgsize, dtype=float)
    xcoo2d, ycoo2d = np.meshgrid(axisX, axisY)

    # Lets trim so that we only keep every 20th pixel
    idx = np.arange(25, imgsize, 50)
    xon = xon.take(idx, axis=0).take(idx, axis=1)
    yon = yon.take(idx, axis=0).take(idx, axis=1)
    xoff = xoff.take(idx, axis=0).take(idx, axis=1)
    yoff = yoff.take(idx, axis=0).take(idx, axis=1)
    xcoo2d = xcoo2d.take(idx, axis=0).take(idx, axis=1)
    ycoo2d = ycoo2d.take(idx, axis=0).take(idx, axis=1)

    # Calculate differences
    xdiff = xon - xoff
    ydiff = yon - yoff

    # Make vector plots
    py.clf()
    qvr = py.quiver2([xcoo2d], [ycoo2d], [xdiff], [ydiff],
                     units='width', scale=5, 
                     width=0.005, headwidth=3, headlength=3, 
                     headaxislength=3)
    py.quiverkey(qvr, 100, 1120, 1.0, '1 pixel', coordinates='data', color='r')
    py.xlabel('NIRC2 X (pixel)')
    py.ylabel('NIRC2 Y (pixel)')
    py.title('Arrows point to DAR Fix')
    #py.savefig('plots/vector_daroffon.png')
    py.show()
def orbitsAnimate(years=None, root="./", align="align/align_d_rms_1000_abs_t", poly="polyfit_d/fit"):

    ##########
    #
    # START - Modify stuff in here only
    #
    ##########
    # Today's date
    today = 2008.5

    # Load up a starset of just those stars in orbits_movie.dat
    s = getOrbitStars(orbitFile="orbits_movie.dat", root=root, align=align, poly=poly)
    tab = asciidata.open("/u/ghezgroup/data/gc/source_list/orbits_movie.dat")

    ##########
    #
    # STOP - Modify stuff in here only
    #
    ##########

    name = s.getArray("name")
    mag = s.getArray("mag")

    # Get plotting properties from the orbits.dat file
    discovered = tab[9].tonumpy()  # Discovery date
    xshift1 = tab[10].tonumpy()  # Shifts for labels (in first frame)
    yshift1 = tab[11].tonumpy()
    xshift2 = tab[12].tonumpy()  # Shifts for labels (in last frame)
    yshift2 = tab[13].tonumpy()
    colors = [tab[14][ss].strip() for ss in range(tab.nrows)]

    # Determine the mass assuming a distance of 8.0 kpc
    star0orb = s.stars[0].orbit
    dist = 8000.0  # in parsec
    axis = (star0orb.a / 1000.0) * dist  # in au
    mass = (axis) ** 3 / star0orb.p ** 2

    # Set the duration of the animation from the years keyword
    if years == None:
        idx = name.index("S0-2")

        # Use S0-2's orbital period, rounded up to the nearest year
        years = math.ceil(s.stars[idx].orbit.p)

    # Array of time steps (0.1 yr steps)
    t = na.arange(1995.5, 1995.5 + years, 0.2, type=na.Float)

    # Do a flux scaling so that all the stars look good in our image.
    flux = 10.0 ** (mag / -3.0)
    flux /= flux.max()

    # Loop through all the stars and make an array of the X and Y positions
    # as a function of time. Store this on the star object as
    #   star.xanim -- array of X positions at each time step in t
    #   star.yanim -- array of Y positions at each time step in t
    for star in s.stars:
        (r, v, a) = star.orbit.kep2xyz(t, mass=mass, dist=dist)

        star.xanim = r[:, 0].copy()
        star.yanim = r[:, 1].copy()

    ## Make an image 500x500 pixels (1" x 1")
    # imgSize = 500 # pixels
    # scale = 1.0 / imgSize
    # xaxis = (na.arange(imgSize, type=na.Float) - (imgSize/2.0)) # xextent
    # xaxis *= -scale
    # yaxis = (na.arange(imgSize, type=na.Float) - (imgSize/2.0)) # yextent
    # yaxis *= scale

    # Make an image 1920x1080 pixels (1.7" x 1")
    ximgSize = 1920  # pixels
    yimgSize = 1080  # pixels
    xscale = (16.0 / 9.0) / ximgSize  # arcsec per pixel (16/9" from left to right)
    yscale = 1.0 / yimgSize  # arcsec per pixel (1" from top to bottom)
    xaxis = na.arange(ximgSize, type=na.Float) - (ximgSize / 2.0)  # xextent
    xaxis *= -xscale
    yaxis = na.arange(yimgSize, type=na.Float) - (yimgSize / 2.0)  # yextent
    yaxis *= yscale

    # Make grids of X/Y value at each pixel
    xx, yy = pylab.meshgrid(xaxis, yaxis)

    ##########
    #
    # Create image with gaussian PSF for each star
    #
    ##########
    fwhm = 0.020  # Make 20 mas instead of 55 mas

    for tt in range(1):
        # for tt in range(len(t)):
        time = t[tt]
        img = na.zeros((ximgSize, yimgSize), type=na.Float)
        xorb = []
        yorb = []

        for ss in range(1):
            # for ss in range(len(s.stars)):
            star = s.stars[ss]

            xpos = star.xanim[tt]
            ypos = star.yanim[tt]

            # Make a 2D gaussian for this star
            psf = na.exp(-((xx - xpos) ** 2 + (yy - ypos) ** 2) / fwhm ** 2)

            pdb.set_trace()
            img += flux[ss] * psf

        pylab.close(2)
        # pylab.figure(2, figsize=(5,5))
        pylab.figure(2, figsize=(16, 9))
        pylab.clf()
        pylab.axes([0.0, 0.0, 1.0, 1.0])
        pylab.axis("off")
        cmap = gccolors.idl_rainbow()
        pylab.imshow(
            sqrt(img),
            origin="lowerleft",
            cmap=cmap,
            extent=[xaxis[0], xaxis[-1], yaxis[0], yaxis[-1]],
            vmin=sqrt(0.01),
            vmax=sqrt(1.0),
        )

        # Plot the trails for each star
        for ss in range(len(s.stars)):
            star = s.stars[ss]

            before = where((t < time) & (t < discovered[ss]))[0]
            during = where((t < time) & (t >= discovered[ss]) & (t <= today))[0]
            future = where((t < time) & (t > today))[0]

            # Dashed before discovery and in the future
            if len(before) > 0:
                pylab.plot(star.xanim[before], star.yanim[before], "--", color=colors[ss], linewidth=2)
            if len(during) > 0:
                pylab.plot(star.xanim[during], star.yanim[during], "-", color=colors[ss], linewidth=2)
            if len(future) > 0:
                pylab.plot(star.xanim[future], star.yanim[future], "--", color=colors[ss], linewidth=2)
            # Label the stars in the first and last image
            if tt == 0:
                pylab.text(star.xanim[tt] + xshift1[ss], star.yanim[tt] + yshift1[ss], name[ss], color="y", fontsize=10)
            if tt == (len(t) - 1):
                pylab.text(star.xanim[tt] + xshift2[ss], star.yanim[tt] + yshift2[ss], name[ss], color="y", fontsize=10)
            # Label the first LGSAO image
            # diff = (abs(2005.5 - t).argsort())[0]
            # if (tt == diff):
            #    pylab.text(star.xanim[tt]+0.05,star.yanim[tt]+0.05,name[ss],color='y')

        ## Draw an outline box
        # bx = 0.49
        # pylab.plot([bx, -bx, -bx, bx, bx], [-bx, -bx, bx, bx, -bx],
        #           color='white', linewidth=2)

        # pylab.text(0.45, 0.4, t[tt], color='white',
        #           fontsize=16, fontweight='bold',
        #           horizontalalignment='left', verticalalignment='bottom')
        # pylab.text(-0.3, -0.4, 'Keck/UCLA Galactic',
        #           color='white', fontsize=10, fontweight='bold',
        #           horizontalalignment='center', verticalalignment='top')
        # pylab.text(-0.3, -0.44, 'Center Group',
        #           color='white', fontsize=10, fontweight='bold',
        #           horizontalalignment='center', verticalalignment='top')

        # Plot a scale (make it slightly larger than 0.1", otherwise overlapping
        # arrows look funny
        pylab.quiver2([0.45], [-0.1], [0], [0.105], color="w", width=0.005, scale=1)
        pylab.quiver2([0.45], [0.0], [0], [-0.105], color="w", width=0.005, scale=1)
        pylab.text(
            0.4,
            -0.045,
            '0.1"',
            color="white",
            fontsize=14,
            fontweight="bold",
            horizontalalignment="center",
            verticalalignment="top",
        )

        # Draw a star at the position of Sgr A* (large at first, then smaller)
        sgraColor = "white"
        if tt == 0:
            star = gccolors.Star(0, 0, 0.08)
            pylab.fill(star[0], star[1], fill=True, edgecolor=sgraColor, linewidth=1.5, facecolor=sgraColor)
        if tt == 1:
            star = gccolors.Star(0, 0, 0.07)
            pylab.fill(star[0], star[1], fill=True, edgecolor=sgraColor, linewidth=1.5, facecolor=sgraColor)
        if tt == 2:
            star = gccolors.Star(0, 0, 0.06)
            pylab.fill(star[0], star[1], fill=True, edgecolor=sgraColor, linewidth=1.5, facecolor=sgraColor)
        if tt == 3:
            star = gccolors.Star(0, 0, 0.05)
            pylab.fill(star[0], star[1], fill=True, edgecolor=sgraColor, linewidth=1.5, facecolor=sgraColor)
        if tt == 4:
            star = gccolors.Star(0, 0, 0.04)
            pylab.fill(star[0], star[1], fill=True, edgecolor=sgraColor, linewidth=1.5, facecolor=sgraColor)
        if tt == 5:
            star = gccolors.Star(0, 0, 0.04)
            pylab.fill(star[0], star[1], fill=True, edgecolor=sgraColor, linewidth=1.5, facecolor=sgraColor)
        if tt > 5:
            star = gccolors.Star(0, 0, 0.03)
            pylab.fill(star[0], star[1], fill=False, edgecolor=sgraColor, linewidth=1.5)
        pylab.axis([0.5, -0.5, -0.5, 0.5])
        # Save as png for the best animation image quality and smallest animation!!!!
        pylab.savefig("/u/ghezgroup/public_html/gc/images/media/orbits_anim_HD/img_%s.png" % str(t[tt]), dpi=100)
def orbitsAnimate(years=None,
                  root='./',
                  align='align/align_d_rms_1000_abs_t',
                  poly='polyfit_d/fit'):

    ##########
    #
    # START - Modify stuff in here only
    #
    ##########
    # Today's date
    today = 2008.5

    # Load up a starset of just those stars in orbits_movie.dat
    s = getOrbitStars(orbitFile='orbits_movie.dat',
                      root=root,
                      align=align,
                      poly=poly)
    tab = asciidata.open('/u/ghezgroup/data/gc/source_list/orbits_movie.dat')

    ##########
    #
    # STOP - Modify stuff in here only
    #
    ##########

    name = s.getArray('name')
    mag = s.getArray('mag')

    # Get plotting properties from the orbits.dat file
    discovered = tab[9].tonumpy()  # Discovery date
    xshift1 = tab[10].tonumpy()  # Shifts for labels (in first frame)
    yshift1 = tab[11].tonumpy()
    xshift2 = tab[12].tonumpy()  # Shifts for labels (in last frame)
    yshift2 = tab[13].tonumpy()
    colors = [tab[14][ss].strip() for ss in range(tab.nrows)]

    # Determine the mass assuming a distance of 8.0 kpc
    star0orb = s.stars[0].orbit
    dist = 8000.0  # in parsec
    axis = (star0orb.a / 1000.0) * dist  # in au
    mass = (axis)**3 / star0orb.p**2

    # Set the duration of the animation from the years keyword
    if (years == None):
        idx = name.index('S0-2')

        # Use S0-2's orbital period, rounded up to the nearest year
        years = math.ceil(s.stars[idx].orbit.p)

    # Array of time steps (0.1 yr steps)
    t = na.arange(1995.5, 1995.5 + years, 0.2, type=na.Float)

    # Do a flux scaling so that all the stars look good in our image.
    flux = 10.0**(mag / -3.0)
    flux /= flux.max()

    # Loop through all the stars and make an array of the X and Y positions
    # as a function of time. Store this on the star object as
    #   star.xanim -- array of X positions at each time step in t
    #   star.yanim -- array of Y positions at each time step in t
    for star in s.stars:
        (r, v, a) = star.orbit.kep2xyz(t, mass=mass, dist=dist)

        star.xanim = r[:, 0].copy()
        star.yanim = r[:, 1].copy()

    ## Make an image 500x500 pixels (1" x 1")
    #imgSize = 500 # pixels
    #scale = 1.0 / imgSize
    #xaxis = (na.arange(imgSize, type=na.Float) - (imgSize/2.0)) # xextent
    #xaxis *= -scale
    #yaxis = (na.arange(imgSize, type=na.Float) - (imgSize/2.0)) # yextent
    #yaxis *= scale

    # Make an image 1920x1080 pixels (1.7" x 1")
    ximgSize = 1920  # pixels
    yimgSize = 1080  # pixels
    xscale = (16.0 /
              9.) / ximgSize  # arcsec per pixel (16/9" from left to right)
    yscale = 1.0 / yimgSize  # arcsec per pixel (1" from top to bottom)
    xaxis = (na.arange(ximgSize, type=na.Float) - (ximgSize / 2.0))  # xextent
    xaxis *= -xscale
    yaxis = (na.arange(yimgSize, type=na.Float) - (yimgSize / 2.0))  # yextent
    yaxis *= yscale

    # Make grids of X/Y value at each pixel
    xx, yy = pylab.meshgrid(xaxis, yaxis)

    ##########
    #
    # Create image with gaussian PSF for each star
    #
    ##########
    fwhm = 0.020  # Make 20 mas instead of 55 mas

    for tt in range(1):
        #for tt in range(len(t)):
        time = t[tt]
        img = na.zeros((ximgSize, yimgSize), type=na.Float)
        xorb = []
        yorb = []

        for ss in range(1):
            #for ss in range(len(s.stars)):
            star = s.stars[ss]

            xpos = star.xanim[tt]
            ypos = star.yanim[tt]

            # Make a 2D gaussian for this star
            psf = na.exp(-((xx - xpos)**2 + (yy - ypos)**2) / fwhm**2)

            pdb.set_trace()
            img += flux[ss] * psf

        pylab.close(2)
        #pylab.figure(2, figsize=(5,5))
        pylab.figure(2, figsize=(16, 9))
        pylab.clf()
        pylab.axes([0.0, 0.0, 1.0, 1.0])
        pylab.axis('off')
        cmap = gccolors.idl_rainbow()
        pylab.imshow(sqrt(img),
                     origin='lowerleft',
                     cmap=cmap,
                     extent=[xaxis[0], xaxis[-1], yaxis[0], yaxis[-1]],
                     vmin=sqrt(0.01),
                     vmax=sqrt(1.0))

        # Plot the trails for each star
        for ss in range(len(s.stars)):
            star = s.stars[ss]

            before = where((t < time) & (t < discovered[ss]))[0]
            during = where((t < time) & (t >= discovered[ss])
                           & (t <= today))[0]
            future = where((t < time) & (t > today))[0]

            # Dashed before discovery and in the future
            if (len(before) > 0):
                pylab.plot(star.xanim[before],
                           star.yanim[before],
                           '--',
                           color=colors[ss],
                           linewidth=2)
            if (len(during) > 0):
                pylab.plot(star.xanim[during],
                           star.yanim[during],
                           '-',
                           color=colors[ss],
                           linewidth=2)
            if (len(future) > 0):
                pylab.plot(star.xanim[future],
                           star.yanim[future],
                           '--',
                           color=colors[ss],
                           linewidth=2)
            # Label the stars in the first and last image
            if (tt == 0):
                pylab.text(star.xanim[tt] + xshift1[ss],
                           star.yanim[tt] + yshift1[ss],
                           name[ss],
                           color='y',
                           fontsize=10)
            if (tt == (len(t) - 1)):
                pylab.text(star.xanim[tt] + xshift2[ss],
                           star.yanim[tt] + yshift2[ss],
                           name[ss],
                           color='y',
                           fontsize=10)
            # Label the first LGSAO image
            #diff = (abs(2005.5 - t).argsort())[0]
            #if (tt == diff):
            #    pylab.text(star.xanim[tt]+0.05,star.yanim[tt]+0.05,name[ss],color='y')

        ## Draw an outline box
        #bx = 0.49
        #pylab.plot([bx, -bx, -bx, bx, bx], [-bx, -bx, bx, bx, -bx],
        #           color='white', linewidth=2)

        #pylab.text(0.45, 0.4, t[tt], color='white',
        #           fontsize=16, fontweight='bold',
        #           horizontalalignment='left', verticalalignment='bottom')
        #pylab.text(-0.3, -0.4, 'Keck/UCLA Galactic',
        #           color='white', fontsize=10, fontweight='bold',
        #           horizontalalignment='center', verticalalignment='top')
        #pylab.text(-0.3, -0.44, 'Center Group',
        #           color='white', fontsize=10, fontweight='bold',
        #           horizontalalignment='center', verticalalignment='top')

        # Plot a scale (make it slightly larger than 0.1", otherwise overlapping
        # arrows look funny
        pylab.quiver2([0.45], [-0.1], [0], [0.105],
                      color='w',
                      width=0.005,
                      scale=1)
        pylab.quiver2([0.45], [0.0], [0], [-0.105],
                      color='w',
                      width=0.005,
                      scale=1)
        pylab.text(0.4,
                   -0.045,
                   '0.1\"',
                   color='white',
                   fontsize=14,
                   fontweight='bold',
                   horizontalalignment='center',
                   verticalalignment='top')

        # Draw a star at the position of Sgr A* (large at first, then smaller)
        sgraColor = 'white'
        if (tt == 0):
            star = gccolors.Star(0, 0, 0.08)
            pylab.fill(star[0],
                       star[1],
                       fill=True,
                       edgecolor=sgraColor,
                       linewidth=1.5,
                       facecolor=sgraColor)
        if (tt == 1):
            star = gccolors.Star(0, 0, 0.07)
            pylab.fill(star[0],
                       star[1],
                       fill=True,
                       edgecolor=sgraColor,
                       linewidth=1.5,
                       facecolor=sgraColor)
        if (tt == 2):
            star = gccolors.Star(0, 0, 0.06)
            pylab.fill(star[0],
                       star[1],
                       fill=True,
                       edgecolor=sgraColor,
                       linewidth=1.5,
                       facecolor=sgraColor)
        if (tt == 3):
            star = gccolors.Star(0, 0, 0.05)
            pylab.fill(star[0],
                       star[1],
                       fill=True,
                       edgecolor=sgraColor,
                       linewidth=1.5,
                       facecolor=sgraColor)
        if (tt == 4):
            star = gccolors.Star(0, 0, 0.04)
            pylab.fill(star[0],
                       star[1],
                       fill=True,
                       edgecolor=sgraColor,
                       linewidth=1.5,
                       facecolor=sgraColor)
        if (tt == 5):
            star = gccolors.Star(0, 0, 0.04)
            pylab.fill(star[0],
                       star[1],
                       fill=True,
                       edgecolor=sgraColor,
                       linewidth=1.5,
                       facecolor=sgraColor)
        if (tt > 5):
            star = gccolors.Star(0, 0, 0.03)
            pylab.fill(star[0],
                       star[1],
                       fill=False,
                       edgecolor=sgraColor,
                       linewidth=1.5)
        pylab.axis([0.5, -0.5, -0.5, 0.5])
        # Save as png for the best animation image quality and smallest animation!!!!
        pylab.savefig(
            '/u/ghezgroup/public_html/gc/images/media/orbits_anim_HD/img_%s.png'
            % str(t[tt]),
            dpi=100)