Exemplo n.º 1
0
def test_get_images_async_1():
    payload = ukidss.core.Ukidss.get_images_async(coord.ICRSCoordinates(
        ra=83.633083, dec=22.0145, unit=(u.deg, u.deg)),
                                                  radius=20 * u.arcmin,
                                                  get_query_payload=True)
    assert 'xsize' not in payload
    assert 'ysize' not in payload

    payload = ukidss.core.Ukidss.get_images_async(coord.ICRSCoordinates(
        ra=83.633083, dec=22.0145, unit=(u.deg, u.deg)),
                                                  get_query_payload=True)
    assert payload['xsize'] == payload['ysize']
    assert payload['xsize'] == 1

    test_mockreturn = get_mockreturn(ukidss.core.Ukidss.ARCHIVE_URL, payload)
Exemplo n.º 2
0
    def test_file_write(self):
        """Check that we are writing the file."""

        et = ephem_target.EphemTarget("")
        coordinate = coordinates.ICRSCoordinates(
            "01:27:30.26 +13:40:30.60",
            unit=(units.hour, units.degree),
            obstime=Time("2014-12-28 09:59:59", scale='utc'))
        et.append(coordinate)
        coordinate = coordinates.ICRSCoordinates(
            "01:27:29.39 +13:40:20.90",
            unit=(units.hour, units.degree),
            obstime=Time("2014-12-29 09:59:59", scale='utc'))
        et.append(coordinate)
        f_handle = cStringIO.StringIO()
        et.writer(f_handle)
        f_handle.seek(0)
        f_handle.close()
Exemplo n.º 3
0
def eqToGal(ras, decs):
    glons = []
    glats = []
    for ra, dec in zip(ras, decs):
        c = coord.ICRSCoordinates(ra=ra,
                                  dec=dec,
                                  unit=(units.radian, units.radian))
        g = c.galactic
        glons.append(g.l.radians)
        glats.append(g.b.radians)
    return glats, glons
Exemplo n.º 4
0
def data():
    # Now, select Catalina RR Lyrae
    catalina = read_catalina()

    targets = dict()
    targets["Lambda"] = []
    targets["Beta"] = []
    targets["ra"] = []
    targets["dec"] = []
    targets["V"] = []
    targets["dist"] = []
    targets["zsun"] = []
    for star in catalina:
        dist = rr_lyrae_photometric_distance(star["V"], -1.5)
        dist_from_plane = distance_to_sgr_plane(star["ra"], star["dec"], dist)

        icrs = coord.ICRSCoordinates(star["ra"],
                                     star["dec"],
                                     unit=(u.degree, u.degree))
        sgr = icrs.transform_to(SgrCoordinates)

        # select Sgr associated stuff in northern sky
        if abs(dist_from_plane) < 10. \
            and sgr.Lambda.degrees > 180. and sgr.Lambda.degrees < 315 \
            and dist < 31*u.kpc:
            targets["ra"].append(star["ra"])
            targets["dec"].append(star["ra"])
            targets["V"].append(star["V"])
            targets["Lambda"].append(sgr.Lambda.degrees)
            targets["Beta"].append(sgr.Beta.degrees)
            targets["dist"].append(dist.value)
            targets["zsun"].append(dist.value * np.sin(sgr.Beta.radians))

    t = at.Table(targets)
    t = t['ra', 'dec', 'Lambda', 'Beta', 'V', 'dist', 'zsun']
    print("{0} stars selected".format(len(t)))

    t.write("data/catalog/Catalina_sgr_nearby_wrap.txt", format="ascii")

    return

    plt.figure(figsize=(8, 8))
    plt.title("RR Lyrae from Catalina Sky Survey")
    plt.scatter(targets["Lambda"], targets["zsun"], marker='.', color='k')
    plt.xlim(160, 340)
    plt.ylim(-20, 20)
    plt.xlabel("$\Lambda_\odot$ [deg]")
    plt.ylabel("$z_{\odot,sgr}$ [kpc]")
    plt.show()
Exemplo n.º 5
0
 def test_precision_and_wrap(self):
     """Check that the precision in the output fits within allowed space and values to round to 60s"""
     target_name = "test"
     coordinate = coordinates.ICRSCoordinates(
         "11:59:59.9999999999 -10:00:59.9999999999",
         unit=(units.hour, units.degree),
         obstime=Time("2000-01-01 10:00:00", scale='utc'))
     et = ephem_target.EphemTarget(target_name)
     et.append(coordinate)
     line = et.doc.getElementsByTagName(
         "CSV")[0].firstChild.wholeText.split('\n')[-2]
     values = line.split('|')
     self.assertEqual(len(values), 4)
     self.assertEqual(values[1], "12:00:00.00")
     self.assertEqual(values[2], "-10:01:00.0")
Exemplo n.º 6
0
def format_ra_dec(ra_deg, dec_deg):
    """
    Converts RA and DEC values from degrees into the formatting required
    by the Minor Planet Center:

    Formats:
      RA: 'HH MM SS.ddd'
      DEC: 'sDD MM SS.dd' (with 's' being the sign)

    (From: http://www.minorplanetcenter.net/iau/info/OpticalObs.html)

    Args:
      ra_deg: float
        Right ascension in degrees
      dec_deg: float
        Declination in degrees

    Returns:
      formatted_ra: str
      formatted_dec: str
    """
    coords = coordinates.ICRSCoordinates(ra=ra_deg,
                                         dec=dec_deg,
                                         unit=(units.degree, units.degree))

    # decimal=False results in using sexagesimal form
    formatted_ra = coords.ra.format(unit=units.hour,
                                    decimal=False,
                                    sep=" ",
                                    precision=3,
                                    alwayssign=False,
                                    pad=True)

    formatted_dec = coords.dec.format(unit=units.degree,
                                      decimal=False,
                                      sep=" ",
                                      precision=2,
                                      alwayssign=True,
                                      pad=True)

    return formatted_ra, formatted_dec
Exemplo n.º 7
0
def parse_coordinates(coordinates):
    """
    Takes a string or astropy.coordinates object. Checks if the
    string is parsable as an astropy.coordinates.ICRSCoordinates
    object or is a name that is resolvable. Otherwise asserts
    that the argument is an astropy.coordinates object.

    Parameters
    ----------
    coordinates : str/astropy.coordinates object
        Astronomical coordinate

    Returns
    -------
    a subclass of `astropy.coordinates.SphericalCoordinatesBase`

    Raises
    ------
    astropy.units.UnitsException
    TypeError
    """
    if isinstance(coordinates, basestring):
        try:
            c = coord.ICRSCoordinates.from_name(coordinates)
        except coord.name_resolve.NameResolveError:
            try:
                c = coord.ICRSCoordinates(coordinates)
                warnings.warn(
                    "Coordinate string is being interpreted as an ICRS coordinate."
                )
            except u.UnitsException:
                warnings.warn(
                    "Only ICRS coordinates can be entered as strings\n"
                    "For other systems please use the appropriate "
                    "astropy.coordinates object")
                raise u.UnitsException
    elif isinstance(coordinates, coord.SphericalCoordinatesBase):
        c = coordinates
    else:
        raise TypeError("Argument cannot be parsed as a coordinate")
    return c
Exemplo n.º 8
0
 def predict(self, date, obs_code=568):
     """
     use the bk predict method to compute the location of the source on the given date.
     """
     time = Time(date, scale='utc', precision=6)
     jd = ctypes.c_double(time.jd)
     # call predict with agbfile, jdate, obscode
     self.orbfit.predict.restype = ctypes.POINTER(ctypes.c_double * 5)
     self.orbfit.predict.argtypes = [
         ctypes.c_char_p, ctypes.c_double, ctypes.c_int
     ]
     predict = self.orbfit.predict(ctypes.c_char_p(self.abg.name), jd,
                                   ctypes.c_int(obs_code))
     self.coordinate = coordinates.ICRSCoordinates(predict.contents[0],
                                                   predict.contents[1],
                                                   unit=(units.degree,
                                                         units.degree))
     self.dra = predict.contents[2]
     self.ddec = predict.contents[3]
     self.pa = predict.contents[4]
     self.date = str(time)
Exemplo n.º 9
0
def parse_grbview(grbs):
    """Parse the returns from grbview to get unique triggers
    """
    if len(grbs) == 1:
        return grbs
    grbs.sort(key=lambda grb: grb.detector)
    pairs = itertools.combinations(grbs, 2)
    grb_triggers = []
    keep = numpy.zeros(len(grbs)).astype(bool)
    for i,pair in enumerate(pairs):
        a,b = pair
        if a.trig_id is not None and a.trig_id == b.trig_id:
            new = GammaRayBurst()
            for attr in a.__slots__:
                if hasattr(a, attr) and getattr(a, attr):
                    setattr(new, attr, getattr(a, attr))
            for attr in b.__slots__:
                if attr == 'time' and b.time.iso.endswith('00:00:00.000'):
                    continue
                elif hasattr(b, attr) and getattr(b, attr):
                    setattr(new, attr, getattr(b, attr))
            try:
                new.coordinates = acoords.ICRSCoordinates(float(new.ra),
                                                          float(new.dec),
                                                          obstime=new.time,
                                                          unit=(aunits.radian,
                                                                aunits.radian))
            except AttributeError:
                pass
            if a in grb_triggers:
                grb_triggers.pop(grb_triggers.index(a))
            if b in grb_triggers:
                grb_triggers.pop(grb_triggers.index(b))
            grb_triggers.append(new)
        elif a not in grb_triggers:
            grb_triggers.append(a)
        elif b not in grb_triggers:
            grb_triggers.append(b)

    return grb_triggers
Exemplo n.º 10
0
def load_header(obsstruct):
    tbl = ascii.read('targets.txt')
    source_name = obsstruct['SOURCE_NAME'][0]
    match = np.array([source_name == x.lower() for x in tbl['col1']])
    ra, dec = tbl['col4'][match][0], tbl['col5'][match][0]
    coord = coords.ICRSCoordinates(ra + " " + dec, unit=('hour', 'deg'))

    header = fits.Header()
    header['CRVAL1'] = coord.ra.degree
    header['CRVAL2'] = coord.dec.degree
    shape = obsstruct['MAP'][0].shape
    header['CRPIX1'] = shape[1] / 2. + 1  # may be offset by 0.5-1 pixel!?
    header['CRPIX2'] = shape[0] / 2. + 1  # may be offset by 0.5-1 pixel!?
    header['CDELT1'] = obsstruct['OMEGA_PIX_AM'][0]**0.5 / 60
    header['CDELT2'] = obsstruct['OMEGA_PIX_AM'][0]**0.5 / 60
    header['CUNIT1'] = 'deg'
    header['CUNIT2'] = 'deg'
    header['CTYPE1'] = 'RA---TAN'
    header['CTYPE2'] = 'DEC--TAN'
    header['BUNIT'] = 'Jy/beam'

    return header
Exemplo n.º 11
0
def parse_files(filenms):
    info = []
    for filenm in filenms:
        d = {}
        try:
            hdulist = fits.open(filenm, ignore_missing_end=True)
            hdr0 = hdulist[0].header
            hdr1 = hdulist[1].header
            hdulist.close()
            d["BeamID"] = int(hdr0["SRC_NAME"].strip("GBNCC"))
            d["RightAscension"] = ra_to_rad(hdr0["RA"])*RADTODEG
            d["Declination"] = dec_to_rad(hdr0["DEC"])*RADTODEG
            c = coordinates.ICRSCoordinates(ra=d["RightAscension"],
                                            dec=d["Declination"],
                                            unit=(units.degree,units.degree))
            d["GalacticLon"] = c.galactic.l.degrees
            d["GalacticLat"] = c.galactic.b.degrees
            d["DateObserved"] = hdr0["STT_IMJD"] + hdr0["STT_SMJD"]/SECPERDAY
            d["CenterFreq"] = hdr0["OBSFREQ"]
            d["BW"] = abs(hdr0["OBSBW"])
            d["SamplingTime"] = hdr1["TBIN"]*1000000
            d["IntTime"] = hdr1["NAXIS2"]*hdr1["NSBLK"]*hdr1["TBIN"]
            d["FileName"] = os.path.split(filenm)[1]
            d["FilePath"] = os.path.split(os.path.abspath(filenm))[0]
            d["ArchiveLocation"] = "NULL"
            d["FileSize"] = os.path.getsize(filenm)
            d["ProcessingStatus"] = "u"
            d["ProcessingAttempts"] = 0
            d["ProcessingSite"] = "NULL"
            d["ProcessingID"] = "NULL"
            d["ProcessingDate"] = "NULL"
            d["PipelineVersion"] = "NULL"
            d["UploadDate"] = datetime.datetime.fromtimestamp(os.path.getmtime(filenm)).strftime("%Y-%m-%dT%H:%M:%S")
            info.append(d)

        except:
            print("Failed to read %s"%filenm)
    
    return info
Exemplo n.º 12
0
    """
    L = sgr_coord.Lambda.radian
    B = sgr_coord.Beta.radian

    Xs = cos(B) * cos(L)
    Ys = cos(B) * sin(L)
    Zs = sin(B)
    Zs = -Zs

    X, Y, Z = sgr_matrix.T.dot(np.array([Xs, Ys, Zs]))

    l = degrees(np.arctan2(Y, X))
    b = degrees(np.arcsin(Z / np.sqrt(X * X + Y * Y + Z * Z)))

    if l < 0:
        l += 360

    return coord.GalacticCoordinates(l,
                                     b,
                                     distance=sgr_coord.distance,
                                     unit=(u.degree, u.degree))


if __name__ == "__main__":
    # Example use case for our newly defined coordinate class
    ra_dec = coord.ICRSCoordinates(152.88572,
                                   11.57281,
                                   unit=(u.degree, u.degree))
    sgr = ra_dec.transform_to(SgrCoordinates)
    print(sgr)
Exemplo n.º 13
0
outfile=sys.argv[2]
i=0;
sources=[];
with open(file) as myfile:
        for line in myfile:
		bits=line.split(', ')
		sources.append(Bunch(counter=i,l=line,b=bits,fsn=-1,pr=0));
#		print bits
		if ((len(bits)>1) and (('POINT' in bits[1]) or ('GAUSSIAN' in bits[1]))):
			dec=bits[3]
			dec=dec.replace(".","X")
			dec=dec.replace('X',':',2)
			dec=dec.replace('X','.')
			print 'using ',bits[2]+' '+dec
			try:
				c=coord.ICRSCoordinates(bits[2]+' '+dec,unit=(u.hour,u.degree))
			except:
				print "Bah!"
				next
			dra=np.cos(3.14159*c.dec.degrees/180)*(hdu.data.field('ra')-c.ra.degrees)
			ddec=hdu.data.field('dec')-c.dec.degrees
			dtot=np.sqrt(dra**2+ddec**2)
			fsn=np.argmin(dtot)

# does this source meet our criteria?
			min=3600*dtot[fsn]
			flux=hdu.data.field('fint')[fsn]
			if (min<10 and flux>5):

# check if an earlier (hence brighter) source has been associated with this one already
				associated=-1;
Exemplo n.º 14
0
Parse coordinate string
>>> import astropy.coordinates as coords
>>> c = coords.ICRSCoordinates("00h42m44.3s +41d16m9s")

Access the RA/Dec values
>>> c.ra
<RA 10.68458 deg>
>>> c.dec
<Dec 41.26917 deg>
>>> c.ra.degrees
10.68458333333333
>>> c.ra.hms
(0.0, 42, 44.299999999999784)

Convert to Galactic coordinates
>>> c.galactic.l
<Angle 121.17431 deg>
>>> c.galactic.b
<Angle -21.57280 deg>

Create a separate object in Galactic coordinates
>>> from astropy import units as u
>>> g = c.transform_to(coords.GalacticCoordinates)
>>> g.l.format(u.degree, sep=":", precision=3)
'121:10:27.499'

Set the distance and view the cartesian coordinates
>>> c.distance = coords.Distance(770., u.kpc)
>>> c.x
568.7128882165681
>>> c.y
Exemplo n.º 15
0
    def search(self, **params):
        """For compatibility with generic star catalog search.
        """

        self.logger.debug("search params=%s" % (str(params)))
        ra, dec = params['ra'], params['dec']
        if not (':' in ra):
            # Assume RA and DEC are in degrees
            ra_deg = float(ra)
            dec_deg = float(dec)
        else:
            # Assume RA and DEC are in standard string notation
            ra_deg = wcs.hmsStrToDeg(ra)
            dec_deg = wcs.dmsStrToDeg(dec)

        # Convert to degrees for search radius
        radius_deg = float(params['r']) / 60.0
        #radius_deg = float(params['r'])

        c = coordinates.ICRSCoordinates(ra_deg,
                                        dec_deg,
                                        unit=(units.degree, units.degree))
        self.logger.info("Querying catalog: %s" % (self.full_name))
        time_start = time.time()
        results = conesearch.conesearch(c,
                                        radius_deg * units.degree,
                                        catalog_db=self.full_name)
        time_elapsed = time.time() - time_start

        numsources = results.array.size
        self.logger.info("Found %d sources in %.2f sec" %
                         (numsources, time_elapsed))

        # Scan the returned fields to find ones we need to extract
        # particulars from (ra, dec, id, magnitude)
        mags = []
        ext = {}
        fields = results.array.dtype.names
        for name in fields:
            ucd = results.get_field_by_id(name).ucd
            ucd = str(ucd).lower()
            if ucd == 'id_main':
                ext['id'] = name
            elif ucd == 'pos_eq_ra_main':
                ext['ra'] = name
            elif ucd == 'pos_eq_dec_main':
                ext['dec'] = name
            if ('phot_' in ucd) or ('phot.' in ucd):
                mags.append(name)
        self.logger.debug("possible magnitude fields: %s" % str(mags))
        if len(mags) > 0:
            magfield = mags[0]
        else:
            magfield = None

        # prepare the result list
        starlist = []
        arr = results.array
        for i in range(numsources):
            source = dict(zip(fields, arr[i]))
            starlist.append(self.toStar(source, ext, magfield))

        # metadata about the list
        columns = [
            ('Name', 'name'),
            ('RA', 'ra'),
            ('DEC', 'dec'),
            ('Mag', 'mag'),
            ('Preference', 'preference'),
            ('Priority', 'priority'),
            ('Description', 'description'),
        ]
        # Append extra columns returned by search to table header
        # TODO: what if not all sources have same record structure?
        # is this possible with VO?
        cols = list(fields)
        cols.remove(ext['ra'])
        cols.remove(ext['dec'])
        cols.remove(ext['id'])
        columns.extend(zip(cols, cols))

        # which column is the likely one to color source circles
        colorCode = 'Mag'

        info = Bunch.Bunch(columns=columns, color=colorCode)
        return starlist, info
Exemplo n.º 16
0
@remote_data 
def test_utils():
    response = urllib2.urlopen('http://www.ebay.com')
    C = chunk_read(response, report_hook=chunk_report)
    print C

def test_class_or_instance():
    assert SimpleQueryClass.query() == "class"
    U = SimpleQueryClass()
    assert U.query() == "instance"
    assert SimpleQueryClass.query.__doc__ == " docstring "

@pytest.mark.parametrize(('coordinates'),
                         [coord.ICRSCoordinates(ra=148,
                                                dec=69,
                                                unit=(u.deg, u.deg)),
                          "00h42m44.3s +41d16m9s"
                          ])
def test_parse_coordinates_1(coordinates):
    c = commons.parse_coordinates(coordinates)
    assert c != None

@remote_data
def test_parse_coordinates_2():
    c = commons.parse_coordinates("m81")
    assert c != None

def test_parse_coordinates_3():
    with pytest.raises(Exception):
        commons.parse_coordinates(9.8 * u.kg)
Exemplo n.º 17
0
def query(name, detector=None):
    grb = name.upper()
    if grb.startswith("GRB"):
        grb = grb[3:]
    records = _query(grb)
    if not re.search('[A-Z]\Z', grb):
        i = 0
        while True:
            grb2 = grb + string.uppercase[i]
            new = _query(grb2)
            if new:
                records.update(new)
            else:
                break
            i +=1
    if len(records) == 0:
        raise ValueError("No records were found matching GRB='%s'" % name)
    grbs = []
    for key, params in records.iteritems():
        grb = GammaRayBurst()
        grb.name = params.get("grbname", None)
        grb.detector = key[0]
        grb.url = params.get("ftext", None)
        grb.time = params.get("uttime", None)
        grb.trig_id = int(params.get('trig', params.get('trig1', None)))
        if grb.time and grb.time != '-':
            grb.time = time.Time(grb.time, scale="utc")
        ra = params.get("ra", None)
        if ra == '-':
            ra = None
        dec = params.get("decl", None)
        if dec == '-':
            dec = None
        if ra and dec:
            grb.coordinates = acoords.ICRSCoordinates(float(ra), float(dec),
                                                      obstime=grb.time,
                                                      unit=(aunits.degree,
                                                            aunits.degree))
        err = params.get("err", None)
        if err and err != '-':
            grb.error = aunits.Quantity(float(err), unit=aunits.degree)
        t90 = params.get("t90", None)
        if t90 and t90 != '-':
            grb.t90 = aunits.Quantity(float(t90), unit=aunits.second)
        t1 = params.get("t1", None)
        if t1 and t1 != '-':
            grb.t1 = float(t1)
        t2 = params.get("t2", None)
        if t2 and t2 != '-':
            grb.t2 = float(t2)
        fluence = params.get("fluence", None)
        if fluence and fluence != '-':
            grb.fluence = aunits.Quantity(float(fluence), "erg / cm**2")
        grbs.append(grb)
    grbs = parse_grbview(grbs)
    detlist = set([grb.detector for grb in grbs])
    if detector:
        grbs = filter(lambda grb: re.match(detector, grb.detector, re.I), grbs)
    if len(grbs) == 0:
        raise KeyError("Records were found matching detectors ('%s'), but "
                       "not '%s'" % ("', '".join(detlist), detector))
    return sorted(grbs, key=lambda b: b.name)
Exemplo n.º 18
0
def test_get_images_async_2(patch_get, patch_get_readable_fileobj):

    image_urls = ukidss.core.Ukidss.get_images_async(
        coord.ICRSCoordinates(ra=83.633083, dec=22.0145, unit=(u.deg, u.deg)))
    assert len(image_urls) == 1
Exemplo n.º 19
0
def align_to_reference(ROOT_DIRECT,
                       ALIGN_IMAGE,
                       fitgeometry="shift",
                       clean=True,
                       verbose=False,
                       ALIGN_EXTENSION=0,
                       toler=3,
                       skip_swarp=False,
                       align_sdss_ds9=False,
                       catalog=None):
    """
xshift, yshift, rot, scale, xrms, yrms = align_to_reference()
    """
    import os
    import glob
    import shutil

    from pyraf import iraf
    from iraf import stsdas, dither

    import threedhst
    from threedhst import catIO

    no = iraf.no
    yes = iraf.yes
    INDEF = iraf.INDEF

    #### Clean slate
    rmfiles = [
        'SCI.fits', 'WHT.fits', 'align.cat', 'direct.cat'
        'align.map', 'align.match', 'align.reg', 'align.xy', 'direct.reg',
        'direct.xy', 'ds9_align.tsv'
    ]

    for file in rmfiles:
        try:
            os.remove(file)
        except:
            pass

    if catalog is not None:
        align_sdss_ds9 = True

    #### Get only images that overlap from the ALIGN_IMAGE list
    if not align_sdss_ds9:
        align_img_list = find_align_images_that_overlap(
            ROOT_DIRECT + '_drz.fits',
            ALIGN_IMAGE,
            ALIGN_EXTENSION=ALIGN_EXTENSION)
        if not align_img_list:
            print 'threedhst.shifts.align_to_reference: no alignment images overlap.'
            return 0, 0

    #### Use swarp to combine the alignment images to the same image
    #### dimensions as the direct mosaic
    if (not skip_swarp) & (not align_sdss_ds9):
        try:
            os.remove(ROOT_DIRECT + '_align.fits')
        except:
            pass
        matchImagePixels(input=align_img_list,
                         matchImage=ROOT_DIRECT + '_drz.fits',
                         output=ROOT_DIRECT + '_align.fits',
                         match_extension=1,
                         input_extension=ALIGN_EXTENSION)

    #### Run SExtractor on the direct image, with the WHT
    #### extension as a weight image
    se = threedhst.sex.SExtractor()
    se.aXeParams()
    se.copyConvFile()
    se.overwrite = True
    se.options['CHECKIMAGE_TYPE'] = 'NONE'
    se.options['WEIGHT_TYPE'] = 'MAP_WEIGHT'
    se.options['WEIGHT_IMAGE'] = 'WHT.fits'
    se.options['FILTER'] = 'Y'
    ## Detect thresholds (default = 1.5)
    THRESH = 10
    if align_sdss_ds9:
        if 'Vizier' not in REFERENCE_CATALOG:
            THRESH = 20

    se.options['DETECT_THRESH'] = '%d' % (THRESH)
    se.options['ANALYSIS_THRESH'] = '%d' % (THRESH)
    se.options['MAG_ZEROPOINT'] = str(threedhst.options['MAG_ZEROPOINT'])

    #### Run SExtractor on direct and alignment images
    ## direct image
    se.options['CATALOG_NAME'] = 'direct.cat'
    iraf.imcopy(ROOT_DIRECT + '_drz.fits[1]', "SCI.fits")
    iraf.imcopy(ROOT_DIRECT + '_drz.fits[2]', "WHT.fits")
    status = se.sextractImage('SCI.fits')

    ## Read the catalog
    directCat = threedhst.sex.mySexCat('direct.cat')

    if align_sdss_ds9:
        ### Use ds9 SDSS catalog to refine alignment
        import threedhst.dq
        import pywcs
        import threedhst.catIO as catIO

        wcs = pywcs.WCS(pyfits.getheader('SCI.fits', 0))
        #wcs = pywcs.WCS(pyfits.getheader('Q0821+3107-F140W_drz.fits', 1))

        if 'Vizier' in REFERENCE_CATALOG:
            #### Use (unstable) astroquery Vizier search
            #### CFHTLS-Deep: 'Vizier.II/317'
            VIZIER_CAT = REFERENCE_CATALOG.split('Vizier.')[1]
            print 'Align to Vizier catalog: http://vizier.u-strasbg.fr/viz-bin/VizieR?-source=%s' % (
                VIZIER_CAT)

            import astroquery
            if astroquery.__version__ < '0.0.dev1078':
                from astroquery import vizier

                query = {}
                query["-source"] = VIZIER_CAT
                #query["-out"] = ["_r", "CFHTLS", "rmag"]
                query["-out"] = ["_RAJ2000", "_DEJ2000"]  ### Just RA/Dec.

                #### Center position and query radius
                r0, d0 = wcs.wcs_pix2sky([[wcs.naxis1 / 2., wcs.naxis2 / 2.]],
                                         1)[0]
                rll, dll = wcs.wcs_pix2sky([[0, 0]], 1)[0]
                corner_radius = np.sqrt(
                    (r0 - rll)**2 * np.cos(d0 / 360. * 2 * np.pi)**2 +
                    (d0 - dll)**2) * 60. * 1.5
                h = query["-c"] = "%.6f %.6f" % (r0, d0)
                query["-c.rm"] = "%.3f" % (corner_radius
                                           )  ### xxx check image size

                #### Run the query
                vt = vizier.vizquery(query)
            else:
                #### Newer astroquery
                from astroquery.vizier import Vizier
                import astropy.coordinates as coord
                import astropy.units as u

                Vizier.ROW_LIMIT = -1

                r0, d0 = wcs.wcs_pix2sky([[wcs.naxis1 / 2., wcs.naxis2 / 2.]],
                                         1)[0]
                rll, dll = wcs.wcs_pix2sky([[0, 0]], 1)[0]
                corner_radius = np.sqrt(
                    (r0 - rll)**2 * np.cos(d0 / 360. * 2 * np.pi)**2 +
                    (d0 - dll)**2) * 60. * 1.5
                #
                c = coord.ICRSCoordinates(ra=r0, dec=d0, unit=(u.deg, u.deg))
                #### something with astropy.coordinates
                # c.icrs.ra.degree = c.icrs.ra.degrees
                # c.icrs.dec.degree = c.icrs.dec.degrees
                #
                vt = Vizier.query_region(c,
                                         width=u.Quantity(
                                             corner_radius, u.arcminute),
                                         catalog=[VIZIER_CAT])[0]

            #### Make a region file
            ra_list, dec_list = vt['RAJ2000'], vt['DEJ2000']
            print 'Vizier, found %d objects.' % (len(ra_list))
            fp = open('%s.vizier.reg' % (ROOT_DIRECT), 'w')
            fp.write('# %s, r=%.1f\'\nfk5\n' % (VIZIER_CAT, corner_radius))
            for ra, dec in zip(ra_list, dec_list):
                fp.write('circle(%.6f, %.6f, 0.5")\n' % (ra, dec))
            #
            fp.close()
        else:
            #### Use DS9 catalog
            ds9 = threedhst.dq.myDS9()
            ds9.set('file SCI.fits')
            #ds9.set('file Q0821+3107-F140W_drz.fits')
            ds9.set('catalog %s' % (REFERENCE_CATALOG))
            ### Can't find XPA access point for "copy to regions"
            ds9.set('catalog export tsv ds9_align.tsv')
            lines = open('ds9_align.tsv').readlines()
            ra_list, dec_list = [], []
            for line in lines[1:]:
                spl = line.split()
                ra, dec = float(spl[0]), float(spl[1])
                ra_list.append(ra)
                dec_list.append(dec)
            #
            del (ds9)

        x_image, y_image = [], []
        for ra, dec in zip(ra_list, dec_list):
            x, y = wcs.wcs_sky2pix([[ra, dec]], 1)[0]
            x_image.append(x)
            y_image.append(y)

        alignCat = catIO.EmptyCat()
        alignCat['X_IMAGE'] = np.array(x_image)
        alignCat['Y_IMAGE'] = np.array(y_image)

    else:
        ## alignment image
        se.options['CATALOG_NAME'] = 'align.cat'
        status = se.sextractImage(ROOT_DIRECT + '_align.fits')
        alignCat = threedhst.sex.mySexCat('align.cat')

    xshift = 0
    yshift = 0
    rot = 0
    scale = 1.

    xrms = 2
    yrms = 2

    NITER = 5
    IT = 0
    while (IT < NITER):
        IT = IT + 1

        #### Get x,y coordinates of detected objects
        ## direct image
        fp = open('direct.xy', 'w')
        for i in range(len(directCat.X_IMAGE)):
            fp.write('%s  %s\n' % (directCat.X_IMAGE[i], directCat.Y_IMAGE[i]))
        fp.close()

        ## alignment image
        fp = open('align.xy', 'w')
        for i in range(len(alignCat.X_IMAGE)):
            fp.write('%s  %s\n' % (np.float(alignCat.X_IMAGE[i]) + xshift,
                                   np.float(alignCat.Y_IMAGE[i]) + yshift))
        fp.close()

        iraf.flpr()
        iraf.flpr()
        iraf.flpr()
        #### iraf.xyxymatch to find matches between the two catalogs
        pow = toler * 1.
        try:
            os.remove('align.match')
        except:
            pass

        status1 = iraf.xyxymatch(input="direct.xy",
                                 reference="align.xy",
                                 output="align.match",
                                 tolerance=2**pow,
                                 separation=0,
                                 verbose=yes,
                                 Stdout=1)

        nmatch = 0
        while status1[-1].startswith('0') | (nmatch < 10):
            pow += 1
            os.remove('align.match')
            status1 = iraf.xyxymatch(input="direct.xy",
                                     reference="align.xy",
                                     output="align.match",
                                     tolerance=2**pow,
                                     separation=0,
                                     verbose=yes,
                                     Stdout=1)
            #
            nmatch = 0
            for line in open('align.match').xreadlines():
                nmatch += 1

        if verbose:
            for line in status1:
                print line

        #### Compute shifts with iraf.geomap
        iraf.flpr()
        iraf.flpr()
        iraf.flpr()
        try:
            os.remove("align.map")
        except:
            pass

        status2 = iraf.geomap(input="align.match",
                              database="align.map",
                              fitgeometry=fitgeometry,
                              interactive=no,
                              xmin=INDEF,
                              xmax=INDEF,
                              ymin=INDEF,
                              ymax=INDEF,
                              maxiter=10,
                              reject=2.0,
                              Stdout=1)
        if verbose:
            for line in status2:
                print line

        #fp = open(root+'.iraf.log','a')
        #fp.writelines(status1)
        #fp.writelines(status2)
        #fp.close()

        #### Parse geomap.output
        fp = open("align.map", "r")
        for line in fp.readlines():
            spl = line.split()
            if spl[0].startswith('xshift'):
                xshift += float(spl[1])
            if spl[0].startswith('yshift'):
                yshift += float(spl[1])
            if spl[0].startswith('xrotation'):
                rot = float(spl[1])
            if spl[0].startswith('xmag'):
                scale = float(spl[1])
            if spl[0].startswith('xrms'):
                xrms = float(spl[1])
            if spl[0].startswith('yrms'):
                yrms = float(spl[1])

        fp.close()

        #os.system('wc align.match')
        print 'Shift iteration #%d, xshift=%f, yshift=%f, rot=%f, scl=%f (rms: %5.2f,%5.2f)' % (
            IT, xshift, yshift, rot, scale, xrms, yrms)

    im = pyfits.open('SCI.fits')

    shutil.copy('align.map', ROOT_DIRECT + '_align.map')
    shutil.copy('align.match', ROOT_DIRECT + '_align.match')

    #### Cleanup
    if clean:
        rmfiles = [
            'SCI.fits', 'WHT.fits', 'align.cat', 'align.map', 'align.match',
            'align.reg', 'align.xy', 'direct.cat', 'direct.reg', 'direct.xy',
            'drz_sci.fits', 'drz_wht.fits', 'bg.fits'
        ]

        for file in rmfiles:
            try:
                os.remove(file)
            except:
                pass

    return xshift, yshift, rot, scale, xrms, yrms
Exemplo n.º 20
0
def get_vizier_cat(image='RXJ2248-IR_sci.fits', ext=0, catalog="II/246"):
    """
    Get a list of RA/Dec coords from a Vizier catalog that can be used
    for WCS alignment.
    
    `catalog` is any catalog ID recognized by Vizier, e.g.: 
        "II/328/allwise": WISE
        "II/246": 2MASS
        "V/139": SDSS DR9
    """
    import threedhst.dq
    import astropy.wcs as pywcs
    from astropy.table import Table as table
    import astropy.io.fits as pyfits
    
    import astroquery
    from astroquery.vizier import Vizier
    import astropy.coordinates as coord
    import astropy.units as u
    
    im = pyfits.open(image)
    
    wcs = pywcs.WCS(im[ext].header)
    #wcs = pywcs.WCS(pyfits.getheader('Q0821+3107-F140W_drz.fits', 1))

    Vizier.ROW_LIMIT = -1
            
    r0, d0 = wcs.wcs_pix2world([[im[ext].header['NAXIS1']/2., im[ext].header['NAXIS2']/2.]], 1)[0]
    foot = wcs.calc_footprint()
    
    corner_radius = np.sqrt((foot[:,0]-r0)**2/np.cos(d0/360.*2*np.pi)**2 + (foot[:,1]-d0)**2).max()*60*1.1

    try:
        c = coord.ICRS(ra=r0, dec=d0, unit=(u.deg, u.deg))
    except:
        c = coord.ICRSCoordinates(ra=r0, dec=d0, unit=(u.deg, u.deg))
        
    #### something with astropy.coordinates
    # c.icrs.ra.degree = c.icrs.ra.degrees
    # c.icrs.dec.degree = c.icrs.dec.degrees
    #
    vt = Vizier.query_region(c, radius=u.Quantity(corner_radius, u.arcminute), catalog=[catalog])
    if not vt:
        threedhst.showMessage('No matches found in Vizier %s @ (%.6f, %.6f).\n\nhttp://vizier.u-strasbg.fr/viz-bin/VizieR?-c=%.6f+%.6f&-c.rs=8' %(catalog, r0, d0, r0, d0), warn=True)
        return False
    
    vt = vt[0]
            
    #### Make a region file
    ra_list, dec_list = vt['RAJ2000'], vt['DEJ2000']
    print 'Vizier, found %d objects in %s.' %(len(ra_list), catalog)
    
    fp = open('%s.vizier.radec' %(image.split('.fits')[0]), 'w')
    fpr = open('%s.vizier.reg' %(image.split('.fits')[0]), 'w')
    
    fp.write('# %s, r=%.1f\'\n' %(catalog, corner_radius))
    fpr.write('# %s, r=%.1f\'\nfk5\n' %(catalog, corner_radius))
    for ra, dec in zip(ra_list, dec_list):
        fp.write('%.7f %.7f\n' %(ra, dec))
        fpr.write('circle(%.6f, %.6f, 0.5")\n' %(ra, dec))
    
    fpr.close()
    fp.close()
    
    return True
Exemplo n.º 21
0
def read_quest():
    """ Read in the QUEST data -- RR Lyrae from the QUEST survey,
        Vivas et al. 2004. 
        
        - Photometry from:
            http://vizier.cfa.harvard.edu/viz-bin/VizieR?-source=J/AJ/127/1158
        - Spectral data from:
            http://iopscience.iop.org/1538-3881/129/1/189/fulltext/204289.tables.html
            Spectroscopy of bright QUEST RR Lyrae stars (Vivas+, 2008)
            
    """
    phot_filename = os.path.join(project_root, "data", "catalog", \
                                 "quest_vivas2004_phot.tsv")
    phot_data = ascii.read(phot_filename, delimiter="\t", data_start=3)

    # With more spectral data, add here
    vivas2004_spec = ascii.read(os.path.join(project_root, "data", "catalog",
                                             "quest_vivas2004_spec.tsv"),
                                delimiter="\t")

    vivas2008_spec = ascii.read(os.path.join(project_root, "data", "catalog",
                                             "quest_vivas2008_spec.tsv"),
                                delimiter="\t",
                                data_start=3)

    vivas2008_spec.rename_column('HJD0', 'HJD')
    spec_data = vstack((vivas2004_spec, vivas2008_spec))
    all_data = join(left=phot_data,
                    right=spec_data,
                    keys=['[VZA2004]'],
                    join_type='outer')

    new_columns = dict()
    new_columns['ra'] = []
    new_columns['dec'] = []
    new_columns['V'] = []
    new_columns['dist'] = []
    new_columns['Type'] = []
    new_columns['Per'] = []
    new_columns['HJD'] = []
    for row in all_data:
        if not isinstance(row["_RAJ2000_1"], np.ma.core.MaskedConstant):
            icrs = coord.ICRSCoordinates(row["_RAJ2000_1"],
                                         row["_DEJ2000_1"],
                                         unit=(u.degree, u.degree))
        elif not isinstance(row["_RAJ2000_2"], np.ma.core.MaskedConstant):
            icrs = coord.ICRSCoordinates(row["_RAJ2000_2"],
                                         row["_DEJ2000_2"],
                                         unit=(u.degree, u.degree))
        else:
            raise TypeError()

        new_columns['ra'].append(icrs.ra.degrees)
        new_columns['dec'].append(icrs.dec.degrees)

        if not isinstance(row["Type_1"], np.ma.core.MaskedConstant):
            new_columns['Type'].append(row['Type_1'])
        elif not isinstance(row["Type_2"], np.ma.core.MaskedConstant):
            new_columns['Type'].append(row['Type_2'])
        else:
            raise TypeError()

        if not isinstance(row["Per_1"], np.ma.core.MaskedConstant):
            new_columns['Per'].append(row['Per_1'])
        elif not isinstance(row["Per_2"], np.ma.core.MaskedConstant):
            new_columns['Per'].append(row['Per_2'])
        else:
            raise TypeError()

        if not isinstance(row["HJD_1"], np.ma.core.MaskedConstant):
            new_columns['HJD'].append(row['HJD_1'])
        elif not isinstance(row["HJD_2"], np.ma.core.MaskedConstant):
            new_columns['HJD'].append(row['HJD_2'])
        else:
            raise TypeError()

        v1 = row['Vmag_1']
        v2 = row['Vmag_2']
        if v1 != None:
            new_columns['V'].append(v1)
        else:
            new_columns['V'].append(v2)

        if row['Dist'] != None:
            d = row['Dist']
        else:
            d = rrl_photometric_distance(new_columns['V'][-1], -1.5)
        new_columns['dist'].append(d)

    for name, data in new_columns.items():
        all_data.add_column(Column(data, name=name))

    all_data["ra"].units = u.degree
    all_data["dec"].units = u.degree
    all_data["dist"].units = u.kpc

    all_data.remove_column('Lambda')
    all_data.remove_column('Beta')

    has_spectrum = np.logical_not(np.array(all_data['Vgsr'].mask))
    all_data.add_column(Column(has_spectrum, name='has_spectrum'))

    return all_data
def make_plots(dirname, fnames):
    # get the highest-level directory, assume it is the target source ID
    obj = os.path.split(dirname)[-1]

    files = {}
    data = {}

    for fn in fnames:
        for k,v in keys.iteritems():
            if v in fn:
                if k in files and not 'coadd' in fn:
                    continue
                files[k] = os.path.join(dirname,fn)
                data[k] = fits.getdata(os.path.join(dirname,fn))

    if len(files) == 0:
        return

    if 'SCUBA' in files and 'ATLASGAL' in files:
        del files['SCUBA']
        del data['SCUBA']

    print "Making figures for ",dirname, files

    if 'Band0' in files:
        header0 = fits.getheader(files['Band0'])
        center = coords.ICRSCoordinates("%s %s" % (header0['CRVAL1'],header0['CRVAL2']),unit=('deg','deg'))
    elif 'BGPS' in files:
        header0 = fits.getheader(files['BGPS'])
        center = coords.GalacticCoordinates("%s %s" % (header0['CRVAL1'],header0['CRVAL2']),unit=('deg','deg')).fk5
    elif 'ATLASGAL' in files:
        header0 = fits.getheader(files['ATLASGAL'])
        center = coords.GalacticCoordinates("%s %s" % (header0['CRVAL1'],header0['CRVAL2']),unit=('deg','deg')).fk5
    else:
        raise ValueError("Really?  No BGPS, MUSIC, or ATLASGAL?  WTF.  BRB.  BBQ.")

    for ii,(k,fn) in enumerate(files.iteritems()):
        print k,fn,data[k][data[k]==data[k]].max()

    fig = pl.figure(1)
    pl.clf()
    for ii,(k,fn) in enumerate(files.iteritems()):
        #print k,fn,data[k][data[k]==data[k]].max()
        try:
            F = aplpy.FITSFigure(fn, subplot=(2,3,ii+1), convention='calabretta', figure=fig, north=True)
            F.show_colorscale(vmin=-0.5,vmax=5,cmap=pl.cm.hot)
            F._ax1.set_title(k)
            F.tick_labels.set_xformat('d.dd')
            if (ii) % 3 == 0:
                F.tick_labels.set_yformat('d.dd')
            else:
                F.tick_labels.hide_y()
            F.add_colorbar()
        except Exception as e:
            print "Montage error: ",e
            continue
        try:
            F.recenter(center.ra.degree, center.dec.degree, 5/60.)
        except:
            continue
        #pl.subplot(2,3,ii+1)
        #pl.imshow(data[k], vmin=-0.5, vmax=5)
        #pl.title(k)
        #pl.colorbar()

    prefix = os.path.join(dirname,obj)
    pl.savefig(prefix+"_compare_MUSIC_BGPS.png",bbox_inches='tight')