Example #1
0
File: smf.py Project: banados/lsd
def add_lb(cols):
	(ra, dec) = cols['ra'], cols['dec']
	l = np.empty_like(ra)
	b = np.empty_like(dec)
	for i in xrange(len(ra)):
		(l[i], b[i]) = np.degrees(sla_eqgal(*np.radians((ra[i], dec[i]))))
	cols['l']      = l
	cols['b']      = b
Example #2
0
def raDec2thetaPhi(r, d):
    ra   = r * 15 * math.pi / 180
    dec  = d * math.pi / 180
    l, b = slalib.sla_eqgal(ra, dec)
    l *= 180 / math.pi
    b *= 180 / math.pi
    j = int(b + 91.5)
    j = 180 if j > 180 else j
    nl = int(l - 0.5)
    nl = 359 if l < 0.5 else nl
    i  = (nl / 4) + 1
    return i, j
Example #3
0
File: sdss.py Project: banados/lsd
def import_from_sweeps_aux(file, db, tabname, all=False):
	# import an SDSS run
	dat   = pyfits.getdata(file, 1)
	table = db.table(tabname)

	if not all:
		F1 = F1_BRIGHT | F1_SATURATED | F1_NODEBLEND | F1_EDGE;	# these must not be set for an object to qualify
		F2 = F2_DEBLENDED_AS_MOVING				# these must not be set for an object to qualify	# Compute which objects pass flag cuts

		f1 = dat.field('objc_flags')
		f2 = dat.field('objc_flags2')
		rs = dat.field('resolve_status')
		ok = (rs & RS_SURVEY_PRIMARY != 0) & (f1 & F1 == 0) & (f2 & F2 == 0)
		ok |= all
	else:
		ok = np.ones(len(dat.field('resolve_status')), dtype=bool)

	# Import objects passing some basic quality cuts
	if any(ok != 0):
		# Load the data, cutting on flags
		coldefs = sdss_table_def['schema']['main']['columns'] + sdss_table_def['schema']['survey']['columns']
		cols    = dict(( (name, dat.field(fitsname)[ok])   for (name, _, fitsname) in coldefs if fitsname != ''))
		(ext, flux, ivar, calib) = [ dat.field(col)[ok].transpose()    for col in ['extinction', 'modelflux', 'modelflux_ivar', 'calib_status'] ]

		# Compute magnitudes and related quantities for all bands
		for pos, band in enumerate('ugriz'):
			(fluxB, ivarB, extB, calibB) = (flux[pos], ivar[pos], ext[pos], calib[pos])

			# Compute magnitude from flux
			fluxB[fluxB <= 0] = 0.001
			mag = -2.5 * np.log10(fluxB) + 22.5
			magErr = (1.08574 / fluxB) / np.sqrt(ivarB)

			# Append these to the list of columns
			for suffix, col in izip(['', 'Err', 'Ext', 'Calib'], [mag, magErr, extB, calibB]):
				cols[band + suffix] = col

		# Add computed columns
		(ra, dec) = cols['ra'], cols['dec']
		l = np.empty_like(ra)
		b = np.empty_like(dec)
		for i in xrange(len(ra)):
			(l[i], b[i]) = np.degrees(sla_eqgal(*np.radians((ra[i], dec[i]))))
		cols['l']      = l
		cols['b']      = b

		ids = table.append(cols)
	else:
		ids = []

	yield (file, len(ids), len(ok))
def transform_celestial(coords, systems):
    lons, lats = np.radians(coords['lon']), np.radians(coords['lat'])

    out = Table()
    out['lon'] = np.zeros(len(coords), dtype='float64')
    out['lat'] = np.zeros(len(coords), dtype='float64')

    for ii, (lon, lat) in enumerate(zip(lons, lats)):

        # First convert to FK5 J2000 in all cases
        if systems['in'] == 'fk4':
            lon, lat = slalib.sla_fk45z(lon, lat, 2000.0012775136652)
        elif systems['in'] == 'icrs':
            lon, lat = slalib.sla_hfk5z(lon, lat, 2000)[:2]
        elif systems['in'] == 'galactic':
            lon, lat = slalib.sla_galeq(lon, lat)
        elif systems['in'] == 'ecliptic':
            lon, lat = slalib.sla_ecleq(lon, lat, 51544)

        # Now convert from FK5 J2000 to out system    
        if systems['out'] == 'fk4':
            # FK5 -> FK4 at BEPOCH 2000 assuming no proper motion or parallax
            lon, lat = slalib.sla_fk54z(lon, lat, 2000.0012775136652)[:2]
        elif systems['out'] == 'icrs':
            # FK5 -> Hipparcos (i.e. ICRF, which is as close as SLALIB
            # gets to ICRS) at epoch 2000 and with no proper motion
            lon, lat = slalib.sla_fk5hz(lon, lat, 2000)
        elif systems['out'] == 'galactic':
            # FK5 -> Galactic
            lon, lat = slalib.sla_eqgal(lon, lat)
        elif systems['out'] == 'ecliptic':
            # FK5 -> Ecliptic at TDB (MJD) 51544 (i.e. J2000)
            lon, lat = slalib.sla_eqecl(lon, lat, 51544)

        out[ii]['lon'] = np.degrees(lon)
        out[ii]['lat'] = np.degrees(lat)

    return out
Example #5
0
def transform_celestial(coords, systems):
    lons, lats = np.radians(coords['lon']), np.radians(coords['lat'])

    out = Table()
    out['lon'] = np.zeros(len(coords), dtype='float64')
    out['lat'] = np.zeros(len(coords), dtype='float64')

    for ii, (lon, lat) in enumerate(zip(lons, lats)):

        # First convert to FK5 J2000 in all cases
        if systems['in'] == 'fk4':
            lon, lat = slalib.sla_fk45z(lon, lat, 2000.0012775136652)
        elif systems['in'] == 'icrs':
            lon, lat = slalib.sla_hfk5z(lon, lat, 2000)[:2]
        elif systems['in'] == 'galactic':
            lon, lat = slalib.sla_galeq(lon, lat)
        elif systems['in'] == 'ecliptic':
            lon, lat = slalib.sla_ecleq(lon, lat, 51544)

        # Now convert from FK5 J2000 to out system
        if systems['out'] == 'fk4':
            # FK5 -> FK4 at BEPOCH 2000 assuming no proper motion or parallax
            lon, lat = slalib.sla_fk54z(lon, lat, 2000.0012775136652)[:2]
        elif systems['out'] == 'icrs':
            # FK5 -> Hipparcos (i.e. ICRF, which is as close as SLALIB
            # gets to ICRS) at epoch 2000 and with no proper motion
            lon, lat = slalib.sla_fk5hz(lon, lat, 2000)
        elif systems['out'] == 'galactic':
            # FK5 -> Galactic
            lon, lat = slalib.sla_eqgal(lon, lat)
        elif systems['out'] == 'ecliptic':
            # FK5 -> Ecliptic at TDB (MJD) 51544 (i.e. J2000)
            lon, lat = slalib.sla_eqecl(lon, lat, 51544)

        out[ii]['lon'] = np.degrees(lon)
        out[ii]['lat'] = np.degrees(lat)

    return out
Example #6
0
def convert(coords, systems):
    
    if not set(systems.values()).issubset(SUPPORTED_SYSTEMS):
        return None

    lons, lats = np.radians(coords['lon']), np.radians(coords['lat'])

    for ii, (lon, lat) in enumerate(zip(lons, lats)):

        # First convert to FK5 J2000 in all cases
        if systems['in'] == 'fk4':
            lon, lat = slalib.sla_fk45z(lon, lat, 2000.0012775136652)
        elif systems['in'] == 'icrs':
            lon, lat = slalib.sla_hfk5z(lon, lat, 2000)[:2]
        elif systems['in'] == 'galactic':
            lon, lat = slalib.sla_galeq(lon, lat)
        elif systems['in'] == 'ecliptic':
            lon, lat = slalib.sla_ecleq(lon, lat, 51544)
        
        # Now convert from FK5 J2000 to out system    
        if systems['out'] == 'fk4':
            # FK5 -> FK4 at BEPOCH 2000 assuming no proper motion or parallax
            lon, lat = slalib.sla_fk54z (lon, lat, 2000.0012775136652)[:2]
        elif systems['out'] == 'icrs':
            # FK5 -> Hipparcos (i.e. ICRF, which is as close as SLALIB
            # gets to ICRS) at epoch 2000 and with no proper motion
            lon, lat = slalib.sla_fk5hz(lon, lat, 2000)
        elif systems['out'] == 'galactic':
            # FK5 -> Galactic
            lon, lat = slalib.sla_eqgal(lon, lat)
        elif systems['out'] == 'ecliptic':
            # FK5 -> Ecliptic at TDB (MJD) 51544 (i.e. J2000)
            lon, lat = slalib.sla_eqecl(lon, lat, 51544)

        lons[ii], lats[ii] = lon, lat

    return dict(lon=np.degrees(lons), lat=np.degrees(lats))
Example #7
0
File: dvo.py Project: banados/lsd
def import_from_dvo_aux(file, cat):
	# Load object data
	dat, hdr = pyfits.getdata(file, 1, header=1)
	cols     = dict(( (name, dat.field(fitsname))   for (name, _, fitsname) in astromCols if fitsname != ''))

	# Load magnitudes (they're stored in groups of 5 rows)
	mag = pyfits.getdata(file[:-1]+'s', 1)
	magRawCols = [ mag.field(fitsname) for fitsname, _, _ in magData ];
	photoData  = [ col[band::5]        for band in xrange(5)    for col in magRawCols ]
	assert len(photoData) == len(photoCols)
	for i, (col, _, _) in enumerate(photoCols):
		cols[col] = photoData[i]

	#idx = np.arange(len(cols['cat_id']))
	#i   = idx.argsort(
	#ra  = np.sort(cols['ra']);   dra = np.diff(ra)
	#dec = np.sort(cols['dec']); ddec = np.diff(dec)
	#assert (abs(dra) + abs(ddec) > 1.e-14).all(), str(sorted(dra)[:10]) + '\n' + str(sorted(ddec)[:10])
	#assert len(np.unique1d(cols['obj_id'])) == len(cols['obj_id'])
	#assert len(np.unique1d(cols['ext_id'])) == len(cols['ext_id'])
	#assert len(np.unique1d(cols['cat_id'])) == len(cols['cat_id'])

	#print ''
	#for i, col in enumerate(magRawCols):
	#	print magData[i][0],'=',col[10:15]
	#for col, _, _ in photoCols:
	#	print col,'=',cols[col][2]
	#exit()

	# Add computed columns
	(ra, dec) = cols['ra'], cols['dec']
	l = np.empty_like(ra)
	b = np.empty_like(dec)
	for i in xrange(len(ra)):
		(l[i], b[i]) = np.degrees(sla_eqgal(*np.radians((ra[i], dec[i]))))
	cols['l']      = l
	cols['b']      = b

	# Add the ID of the file the object came from
	fn = '/'.join(file.split('/')[-2:])
	assert(len(fn) < 20)
	cols['file_id'] = np.empty(len(l), dtype='a20')
	cols['file_id'][:] = fn

	# Add some blobs (this is mostly for testing)
	cols['hdr'] = np.empty(len(l), dtype=np.object_)
	cols['hdr'][:] = str(hdr)

	# BLOB checksum (for debugging)
	cols['cksum'] = np.empty(len(l), dtype='a32')
	cols['cksum'][:] = hashlib.md5(str(hdr)).hexdigest()

	# Add some array data
	cols['imageid'] = np.empty(len(l), dtype='64i8')
	cols['imageid'][:] = np.arange(len(l)*64, dtype='i8').reshape((len(l), 64))

	# Add a blob array
	s = np.array([ str(i) for i in np.random.random_integers(0, 100, 64) ])
	cols['blobarr'] = np.empty(len(l), dtype='64O')
	cols['blobarr'][:] = s[ np.random.random_integers(0, 63, len(l)*64) ].reshape((len(l), 64))

	# sanity check
	for (name, _, _) in astromCols + photoCols:
		assert name in cols or name in ['id', 'cached'], name

	ids = cat.append(cols)

	return (file, len(ids), len(ids))
Example #8
0
vals = {}
for system in 'FK4', 'Ecliptic', 'Galactic', 'ICRS':
    vals[system] = [np.zeros_like(ra_j2000_fk5), np.zeros_like(dec_j2000_fk5)]

for ii, (raj, decj) in enumerate(zip(ra_j2000_fk5, dec_j2000_fk5)):

    # FK5 -> FK4 at BEPOCH 2000.0 assuming no proper motion or parallax
    r1950, d1950, dr1950, dd1950 = S.sla_fk54z(raj, decj, 2000.0)
    vals['FK4'][0][ii], vals['FK4'][1][ii] = r1950, d1950

    # FK5 -> Ecliptic at TDB (MJD) 51544.0 (i.e. J2000)
    dl, db = S.sla_eqecl(raj, decj, 51544.0)
    vals['Ecliptic'][0][ii], vals['Ecliptic'][1][ii] = dl, db

    # FK5 -> Galactic
    dl, db = S.sla_eqgal(raj, decj)
    vals['Galactic'][0][ii], vals['Galactic'][1][ii] = dl, db

    # FK5 -> Hipparcos (i.e. ICRF, which is as close as SLALIB
    # gets to ICRS) at epoch 2000.0 and with no proper motion
    rh, dh = S.sla_fk5hz(raj, decj, 2000.0)
    vals['ICRS'][0][ii], vals['ICRS'][1][ii] = rh, dh

glon = vals['Galactic'][0]
glat = vals['Galactic'][1]
np.savetxt('coords_galactic.txt',
           zip(np.degrees(glon), np.degrees(glat)),
           fmt="%20.15f")

ra_fk4 = vals['FK4'][0]
dec_fk4 = vals['FK4'][1]
Example #9
0
File: ptf.py Project: schlafly/lsd
def import_from_catalogs_aux(file, det_table, exp_table, djm, all=False):
    # read SExtractor catalog file
    # 	print >>sys.stderr, file
    try:
        hdus = pyfits.open(file)
    except IOError:
        yield (file, 0, 1, 0)
    else:
        dat = hdus[1].data
        hdr_det = hdus[1].header
        hdr_exp = hdus[2].header
        hdus.close()

        # delete PV coefficients from the header
        for i in np.arange(17):
            del hdr_exp["PV1_" + str(i)]
            del hdr_exp["PV2_" + str(i)]
            # test WCS of this image
        try:
            wcs = pywcs.WCS(hdr_exp)
        except:
            yield (file, 0, 4, 0)
        else:
            # import header data into exposures table
            try:
                coldefs = (
                    exp_table_def["schema"]["main"]["columns"] + exp_table_def["schema"]["image_quality"]["columns"]
                )
                exp_cols = dict(
                    (name, np.array([hdr_exp.get(fitsname)]).astype(coltype))
                    for (name, coltype, fitsname, _) in coldefs
                    if fitsname != ""
                )
                coldefs = exp_table_def["schema"]["abs_photo"]["columns"]
                exp_cols.update(
                    dict(
                        (name, np.array([0]).astype(coltype))
                        for (name, coltype, fitsname, _) in coldefs
                        if fitsname != ""
                    )
                )
                exp_cols.update(
                    dict(
                        (name, np.array([hdr_exp.get(fitsname)]).astype(coltype))
                        for (name, coltype, fitsname, _) in coldefs
                        if (fitsname != "") & (hdr_exp.has_key(fitsname))
                    )
                )
            except TypeError:
                yield (file, 0, 2, 0)
            else:
                # find the RA and Dec for the center of this exposure
                pixcrd = np.array([[1024, 2048]], np.float_)
                sky = wcs.all_pix2sky(pixcrd, 1)
                exp_cols["ra"] = np.array([sky[0][0]])
                exp_cols["dec"] = np.array([sky[0][1]])
                (l, b) = np.degrees(sla_eqgal(*np.radians((exp_cols["ra"].squeeze(), exp_cols["dec"].squeeze()))))
                exp_cols["l"] = np.array([l])
                exp_cols["b"] = np.array([b])
                exp_cols["mjd_import"] = np.array([djm]).astype("u4")
                exp_cols["mumax_med"] = np.array([-99.99]).astype("f4")
                exp_cols["mumax_rms"] = np.array([-99.99]).astype("f4")
                exp_cols["n_bright"] = np.array([0]).astype("i2")
                # Import sources into detection table
                coldefs = det_table_def["schema"]["main"]["columns"]
                try:
                    if conversion_to_int == 1:
                        det_cols = dict(
                            (
                                (name, np.around(dat.field(fitsname) * factor).astype(coltype[-2:]))
                                for (name, _, coltype, factor, fitsname, _) in coldefs
                                if fitsname != "" and factor > 1
                            )
                        )
                        det_cols.update(
                            dict(
                                (
                                    (name, (dat.field(fitsname) * factor).astype(coltype[-2:]))
                                    for (name, _, coltype, factor, fitsname, _) in coldefs
                                    if fitsname != "" and factor == 1
                                )
                            )
                        )
                    else:
                        det_cols = dict(
                            (
                                (name, dat.field(fitsname).astype(coltype[-2:]))
                                for (name, coltype, _, _, fitsname, _) in coldefs
                                if fitsname != ""
                            )
                        )
                except TypeError:
                    yield (file, 0, 3, 0)
                else:
                    det_cols["mjd"] = (np.zeros(len(det_cols["ra"])) + hdr_exp["OBSMJD"]).astype("f8")
                    det_cols["fid"] = (np.zeros(len(det_cols["ra"])) + hdr_exp["DBFID"]).astype("u1")
                    # 					det_cols['mjd_import'] = (np.zeros(len(det_cols['ra'])) + djm).astype('u2')
                    det_cols["mag_abs"] = (np.zeros(len(det_cols["ra"])) + 32000).astype("u2")

                    # if there is Eran's absolute photometry...
                    if exp_cols["apbsrms"] > 0:
                        det_cols["mag_abs"] = (
                            np.round(
                                (
                                    dat.field("MAG_AUTO")
                                    + 2.5 * np.log10(hdr_exp["AEXPTIME"])
                                    + hdr_exp["APPAR01"]
                                    + hdr_exp["APPAR03"] * hdr_exp["AIRMASS"]
                                    + hdr_exp["APPAR05"] * (hdr_exp["OBSJD"] - hdr_exp["APMEDJD"])
                                    + hdr_exp["APPAR06"] * (hdr_exp["OBSJD"] - hdr_exp["APMEDJD"]) ** 2
                                    + hdr_exp["APPAR07"] * (dat.field("X_IMAGE") - 1024.0) / 2048.0
                                    + hdr_exp["APPAR08"] * (dat.field("Y_IMAGE") - 2048.0) / 4096.0
                                    + hdr_exp["APPAR09"] * ((dat.field("Y_IMAGE") - 2048.0) / 4096.0) ** 2
                                    + hdr_exp["APPAR10"] * ((dat.field("Y_IMAGE") - 2048.0) / 4096.0) ** 3
                                    + hdr_exp["APPAR11"]
                                    * (dat.field("X_IMAGE") - 1024.0)
                                    / 2048.0
                                    * (dat.field("Y_IMAGE") - 2048.0)
                                    / 4096.0
                                )
                                * 1000.0
                            )
                            .clip(min=5000, max=32000)
                            .astype("u2")
                        )

                        # use bright stars to correct for seeing

                        # import header data into exposures table
            try:
                coldefs = (
                    exp_table_def["schema"]["main"]["columns"] + exp_table_def["schema"]["image_quality"]["columns"]
                )
                exp_cols = dict(
                    (name, np.array([hdr_exp.get(fitsname)]).astype(coltype))
                    for (name, coltype, fitsname, _) in coldefs
                    if fitsname != ""
                )
                coldefs = exp_table_def["schema"]["abs_photo"]["columns"]
                exp_cols.update(
                    dict(
                        (name, np.array([0]).astype(coltype))
                        for (name, coltype, fitsname, _) in coldefs
                        if fitsname != ""
                    )
                )
                exp_cols.update(
                    dict(
                        (name, np.array([hdr_exp.get(fitsname)]).astype(coltype))
                        for (name, coltype, fitsname, _) in coldefs
                        if (fitsname != "") & (hdr_exp.has_key(fitsname))
                    )
                )
            except TypeError:
                yield (file, 0, 2, 0)
            else:
                # find the RA and Dec for the center of this exposure
                pixcrd = np.array([[1024, 2048]], np.float_)
                sky = wcs.all_pix2sky(pixcrd, 1)
                exp_cols["ra"] = np.array([sky[0][0]])
                exp_cols["dec"] = np.array([sky[0][1]])
                (l, b) = np.degrees(sla_eqgal(*np.radians((exp_cols["ra"].squeeze(), exp_cols["dec"].squeeze()))))
                exp_cols["l"] = np.array([l])
                exp_cols["b"] = np.array([b])
                exp_cols["mjd_import"] = np.array([djm]).astype("u4")
                exp_cols["mumax_med"] = np.array([-99.99]).astype("f4")
                exp_cols["mumax_rms"] = np.array([-99.99]).astype("f4")
                exp_cols["n_bright"] = np.array([0]).astype("i2")
                # Import sources into detection table
                coldefs = det_table_def["schema"]["main"]["columns"]
                try:
                    if conversion_to_int == 1:
                        det_cols = dict(
                            (
                                (name, np.around(dat.field(fitsname) * factor).astype(coltype[-2:]))
                                for (name, _, coltype, factor, fitsname, _) in coldefs
                                if fitsname != "" and factor > 1
                            )
                        )
                        det_cols.update(
                            dict(
                                (
                                    (name, (dat.field(fitsname) * factor).astype(coltype[-2:]))
                                    for (name, _, coltype, factor, fitsname, _) in coldefs
                                    if fitsname != "" and factor == 1
                                )
                            )
                        )
                    else:
                        det_cols = dict(
                            (
                                (name, dat.field(fitsname).astype(coltype[-2:]))
                                for (name, coltype, _, _, fitsname, _) in coldefs
                                if fitsname != ""
                            )
                        )
                except TypeError:
                    yield (file, 0, 3, 0)
                else:
                    det_cols["mjd"] = (np.zeros(len(det_cols["ra"])) + hdr_exp["OBSMJD"]).astype("f8")
                    det_cols["fid"] = (np.zeros(len(det_cols["ra"])) + hdr_exp["DBFID"]).astype("u1")
                    # 					det_cols['mjd_import'] = (np.zeros(len(det_cols['ra'])) + djm).astype('u2')
                    det_cols["mag_abs"] = (np.zeros(len(det_cols["ra"])) + 32000).astype("u2")

                    # if there is Eran's absolute photometry...
                    if exp_cols["apbsrms"] > 0:
                        det_cols["mag_abs"] = (
                            np.round(
                                (
                                    dat.field("MAG_AUTO")
                                    + 2.5 * np.log10(hdr_exp["AEXPTIME"])
                                    + hdr_exp["APPAR01"]
                                    + hdr_exp["APPAR03"] * hdr_exp["AIRMASS"]
                                    + hdr_exp["APPAR05"] * (hdr_exp["OBSJD"] - hdr_exp["APMEDJD"])
                                    + hdr_exp["APPAR06"] * (hdr_exp["OBSJD"] - hdr_exp["APMEDJD"]) ** 2
                                    + hdr_exp["APPAR07"] * (dat.field("X_IMAGE") - 1024.0) / 2048.0
                                    + hdr_exp["APPAR08"] * (dat.field("Y_IMAGE") - 2048.0) / 4096.0
                                    + hdr_exp["APPAR09"] * ((dat.field("Y_IMAGE") - 2048.0) / 4096.0) ** 2
                                    + hdr_exp["APPAR10"] * ((dat.field("Y_IMAGE") - 2048.0) / 4096.0) ** 3
                                    + hdr_exp["APPAR11"]
                                    * (dat.field("X_IMAGE") - 1024.0)
                                    / 2048.0
                                    * (dat.field("Y_IMAGE") - 2048.0)
                                    / 4096.0
                                )
                                * 1000.0
                            )
                            .clip(min=5000, max=32000)
                            .astype("u2")
                        )

                        # use bright stars to correct for seeing
                        bright = np.where((det_cols["mag_abs"] > 14000) & (det_cols["mag_abs"] < 17000))
                        if len(bright[0]) > 0:
                            sg = dat.field("MU_MAX") - dat.field("MAG_AUTO")
                            mumax_med = np.median(sg[bright])
                            mumax_rms = 0.741 * (np.percentile(sg[bright], 75) - np.percentile(sg[bright], 25))
                            exp_cols["mumax_med"] = np.array([mumax_med]).astype("f4")
                            exp_cols["mumax_rms"] = np.array([mumax_rms]).astype("f4")
                            exp_cols["n_bright"] = np.array([len(bright[0])]).astype("i2")

                    (exp_id,) = exp_table.append(exp_cols)
                    det_cols["exp_id"] = (np.zeros(len(det_cols["ra"])) + hdr_exp["DBPID"]).astype("u8")
                    det_cols["exp_id"][:] = exp_id
                    ids = det_table.append(det_cols)
                    yield (file, len(ids), 0, exp_id)
vals = {}
for system in 'FK4', 'Ecliptic', 'Galactic', 'ICRS':
   vals[system] = [np.zeros_like(ra_j2000_fk5), np.zeros_like(dec_j2000_fk5)]

for ii, (raj, decj) in enumerate(zip(ra_j2000_fk5, dec_j2000_fk5)):

   # FK5 -> FK4 at BEPOCH 2000.0 assuming no proper motion or parallax
   r1950, d1950, dr1950, dd1950 = S.sla_fk54z (raj, decj, 2000.0)
   vals['FK4'][0][ii],vals['FK4'][1][ii] = r1950, d1950

   # FK5 -> Ecliptic at TDB (MJD) 51544.0 (i.e. J2000)
   dl, db = S.sla_eqecl(raj, decj, 51544.0)
   vals['Ecliptic'][0][ii],vals['Ecliptic'][1][ii] = dl, db

   # FK5 -> Galactic
   dl, db = S.sla_eqgal(raj, decj)
   vals['Galactic'][0][ii],vals['Galactic'][1][ii] = dl, db

   # FK5 -> Hipparcos (i.e. ICRF, which is as close as SLALIB
   # gets to ICRS) at epoch 2000.0 and with no proper motion
   rh, dh = S.sla_fk5hz(raj, decj, 2000.0)
   vals['ICRS'][0][ii],vals['ICRS'][1][ii] = rh, dh


glon = vals['Galactic'][0]
glat = vals['Galactic'][1]
np.savetxt('coords_galactic.txt', zip(np.degrees(glon), 
                                       np.degrees(glat)), fmt="%20.15f")

ra_fk4 = vals['FK4'][0]
dec_fk4 = vals['FK4'][1]
Example #11
0
    def get_vobs(self, mjdtmp, xtmp, ytmp, mode, offx, offy, dcos, offmode):

        mode = mode.lower()
        offmode = offmode.lower()
        ### for 'coord == horizontal' skip
        try:
            mode = self.coord_dict[mode]
        except:
            xxtmp = xtmp
            yytmp = ytmp
        try:
            offmode = self.coord_dict[offmode]
        except:
            xxtmp = xtmp
            yytmp = ytmp

        if mode == 1:  #j2000
            if offmode == 0 or offmode == 1:  #same or j2000
                yytmp = ytmp + offy
                if dcos == 0:
                    xxtmp = xtmp + offx
                else:
                    xxtmp = xtmp + offx / math.cos(yytmp)

            elif offmode == 2:  #b1950
                ret = slalib.sla_fk54z(xtmp, ytmp, 2000)
                xtmp_b = ret[0]
                ytmp_b = ret[1]
                ytmp_b += offy
                if dcos == 0:
                    xtmp_b += offx
                else:
                    xtmp_b += offx / math.cos(ytmp_b)
                ret_1 = slalib.sla_fk45z(xtmp_b, ytmp_b, 1950)
                xxtmp = ret_1[0]
                yytmp = ret_1[1]

            elif offmode == 3:  #lb,galactic,gal
                ret = slalib.sla_eqgal(xtmp, ytmp)
                xtmp_g = ret[0]
                ytmp_g = ret[1]
                ytmp_g += offy
                if dcos == 0:
                    xtmp_g += offx
                else:
                    xtmp_g += offx / math.cos(ytmp_g)
                ret_1 = slalib.sla_galeq(xtmp_g, ytmp_g)
                xxtmp = ret_1[0]
                yytmp = ret_1[1]

        if mode == 2:  #b1950
            if offmode == 1:  #j2000
                ret = slalib.sla_fk45z(xtmp, ytmp, 1950)
                xtmp_j = ret[0]
                ytmp_j = ret[1]
                yytmp = ytmp_j + offy
                if dcos == 0:
                    xxtmp = xtmp_j + offx
                else:
                    xxtmp = xtmp_j + offx / math.cos(ytmp_j)

            elif offmode == 2 or offmode == 0:  #b1950
                ytmp += offy
                if dcos == 0:
                    xtmp += offx
                else:
                    xtmp += offx / math.cos(ytmp)
                ret = slalib.sla_fk45z(xtmp, ytmp, 1950)
                xxtmp = ret[0]
                yytmp = ret[1]

            elif offmode == 3:  #lb,galactic,gal
                ret = slalib.sla_eg50(xtmp, ytmp)
                xtmp_g = ret[0]
                ytmp_g = ret[1]
                ytmp_g += offy
                if dcos == 0:
                    xtmp_g += offx
                else:
                    xtmp_g += offx / math.cos(ytmp_g)
                ret = slalib.sla_galeq(xtmp_g, ytmp_g)
                xxtmp = ret[0]
                yytmp = ret[1]

        if mode == 3:  #lb,galactic,gal
            if offmode == 1:  #j2000
                ret_2 = slalib.sla_galeq(xtmp, ytmp)
                xtmp_j = ret_2[0]
                ytmp_j = ret_2[1]
                yytmp = ytmp_j + offy
                if dcos == 0:
                    xxtmp = xtmp_j + offx
                else:
                    xxtmp = xtmp_j + offx / math.cos(ytmp_j)

            elif offmode == 2:  #b1950
                ret = slalib.sla_ge50(xtmp, ytmp)
                xtmp_b = ret[0]
                ytmp_b = ret[1]
                ytmp_b += offy
                if dcos == 0:
                    xtmp_b += offx
                else:
                    xtmp_b += offx / math.cos(ytmp_b)
                ret = slalib.sla_fk45z(xtmp_b, ytmp_b, 1950)
                xxtmp = ret[0]
                yytmp = ret[1]

            elif offmode == 0 or offmode == 3:  #gal,lb,galactic
                ytmp += offy
                if dcos == 0:
                    xtmp += offx
                else:
                    xtmp += offx / math.cos(ytmp)
                ret = slalib.sla_galeq(xtmp, ytmp)
                xxtmp = ret[0]
                yytmp = ret[1]

        vobs = self.calc_vobs(mjdtmp + 2400000.5, xxtmp, yytmp)
        #print('vobs',vobs,type(vobs))
        return vobs
Example #12
0
File: ptf.py Project: bsesar/lsd
def import_from_catalogs_aux(file, det_table, exp_table, djm, all=False):
    # read SExtractor catalog file
#	print >>sys.stderr, file
	try:
		hdus = pyfits.open(file)
	except IOError:
		yield (file, 0, 1, 0)
	else:
		dat = hdus[1].data
		hdr_det = hdus[1].header
		hdr_exp = hdus[2].header
		hdus.close()

		# delete PV coefficients from the header
		for i in np.arange(17):
			del hdr_exp['PV1_'+str(i)]
			del hdr_exp['PV2_'+str(i)]
		# test WCS of this image
		try:
			wcs = pywcs.WCS(hdr_exp)
			# find the RA and Dec for the center of this exposure
			pixcrd = np.array([[1024,2048]], np.float_)
			sky = wcs.all_pix2sky(pixcrd, 1)
		except:
			yield (file, 0, 4, 0)
		else:
			# test if astrometry is good
			pixcrd = [[i,0] for i in np.arange(2048)]+[[i,4095] for i in np.arange(2048)]+[[0,i] for i in np.arange(4096)]+[[2047,i] for i in np.arange(4096)]
			n_pixel = len(pixcrd)
			hdr_exp['CRVAL1']=20
			hdr_exp['CRVAL2']=0
			hdr_exp['CRPIX1']=1024
			hdr_exp['CRPIX2']=2048
			wcs = pywcs.WCS(hdr_exp)
			test_sky = wcs.all_pix2sky(pixcrd, 0)
#			bad = np.where((test_sky[:,0] < 19.7) + (test_sky[:,0] > 20.3) + (test_sky[:,1] < -0.58) + (test_sky[:,1] > 0.58)) # 5sigma cut
			bad = np.where((test_sky[:,0] < 19.7095) + (test_sky[:,0] > 20.2902) + (test_sky[:,1] < -0.5783) + (test_sky[:,1] > 0.5782)) # 3sigma cut
			bad_pixels = len(bad[0])
			ratio = 1.0*bad_pixels/n_pixel
#			print file, ratio, min(sky[:,0]), max(sky[:,0]), min(sky[:,1]), max(sky[:,1])
			# import header data into exposures table
			try:
				coldefs = exp_table_def['schema']['main']['columns'] + exp_table_def['schema']['image_quality']['columns']
				exp_cols = dict( (name, np.array([hdr_exp.get(fitsname)]).astype(coltype)) for (name, coltype, fitsname, _) in coldefs if fitsname != '')
				coldefs = exp_table_def['schema']['abs_photo']['columns']
				exp_cols.update(dict( (name, np.array([0]).astype(coltype)) for (name, coltype, fitsname, _) in coldefs if fitsname != ''))
				exp_cols.update(dict( (name, np.array([hdr_exp.get(fitsname)]).astype(coltype)) for (name, coltype, fitsname, _) in coldefs if (fitsname != '') & (hdr_exp.has_key(fitsname))))
			except TypeError:
				yield (file, 0, 2, 0)
			else:
				exp_cols['ra'] = np.array([sky[0][0]])
				exp_cols['dec'] = np.array([sky[0][1]])
				(l, b) = np.degrees(sla_eqgal(*np.radians((exp_cols['ra'].squeeze(), exp_cols['dec'].squeeze()))))
				exp_cols['l'] = np.array([l])
				exp_cols['b'] = np.array([b])
				exp_cols['bad_astro'] = np.array([ratio]).astype('f4')
				exp_cols['mjd_import'] = np.array([djm]).astype('u4')
				exp_cols['mumax_med'] = np.array([-99.99]).astype('f4')
				exp_cols['mumax_rms'] = np.array([-99.99]).astype('f4')
				exp_cols['n_bright'] = np.array([0]).astype('i2')
				# Import sources into detection table
				coldefs = det_table_def['schema']['main']['columns']
				try:
					if (conversion_to_int == 1):
						det_cols = dict(( (name, np.around(dat.field(fitsname)*factor).astype(coltype[-2:])) for (name, _, coltype, factor, fitsname, _) in coldefs if fitsname != '' and factor > 1))
						det_cols.update(dict(( (name, dat.field(fitsname).astype(coltype[-2:])) for (name, _, coltype, factor, fitsname, _) in coldefs if fitsname != '' and factor == 1)))
					else:
						det_cols = dict(( (name, dat.field(fitsname).astype(coltype[-2:])) for (name, coltype, _, _, fitsname, _) in coldefs if fitsname != ''))
				except TypeError:
					yield (file, 0, 3, 0)
				else:
					det_cols['mjd'] = (np.zeros(len(det_cols['ra'])) + hdr_exp['OBSMJD']).astype('f8')
					det_cols['fid'] = (np.zeros(len(det_cols['ra'])) + hdr_exp['DBFID']).astype('u1')
#					det_cols['mjd_import'] = (np.zeros(len(det_cols['ra'])) + djm).astype('u2')
					det_cols['mag_abs'] = (np.zeros(len(det_cols['ra'])) + 32000).astype('u2')

					# if there is Eran's absolute photometry...
					if exp_cols['apbsrms'] > 0:
						det_cols['mag_abs'] = np.round((dat.field('MAG_AUTO') + 2.5*np.log10(hdr_exp['AEXPTIME'])+ hdr_exp['APPAR01'] + hdr_exp['APPAR03']*hdr_exp['AIRMASS'] + hdr_exp['APPAR05']*(hdr_exp['OBSJD'] - hdr_exp['APMEDJD']) + hdr_exp['APPAR06']*(hdr_exp['OBSJD'] - hdr_exp['APMEDJD'])**2 + hdr_exp['APPAR07']*(dat.field('X_IMAGE') - 1024.)/2048. + hdr_exp['APPAR08']*(dat.field('Y_IMAGE') - 2048.)/4096. + hdr_exp['APPAR09']*((dat.field('Y_IMAGE') - 2048.)/4096.)**2 + hdr_exp['APPAR10']*((dat.field('Y_IMAGE') - 2048.)/4096.)**3 + hdr_exp['APPAR11']*(dat.field('X_IMAGE') - 1024.)/2048. * (dat.field('Y_IMAGE') - 2048.)/4096.)*1000.).clip(min=5000, max=32000).astype('u2')

						# use bright stars to correct for seeing
						bright = np.where( (det_cols['mag_abs'] > 14000) & (det_cols['mag_abs'] < 17000) )
						if len(bright[0]) > 0:
							sg = dat.field('MU_MAX') - dat.field('MAG_AUTO')
							mumax_med = np.median(sg[bright])
							mumax_rms = 0.741*(np.percentile(sg[bright],75) - np.percentile(sg[bright],25))
							exp_cols['mumax_med'] = np.array([mumax_med]).astype('f4')
							exp_cols['mumax_rms'] = np.array([mumax_rms]).astype('f4')
							exp_cols['n_bright'] = np.array([len(bright[0])]).astype('i2')

					(exp_id,) = exp_table.append(exp_cols)
					det_cols['exp_id'] = (np.zeros(len(det_cols['ra'])) + hdr_exp['DBPID']).astype('u8')
					det_cols['exp_id'][:] = exp_id
					ids = det_table.append(det_cols)
					yield (file, len(ids), 0, exp_id)
Example #13
0
    def read(self, parfilenm):
        self.FILE = parfilenm
        pf = open(parfilenm)
        for line in pf.readlines():
            # Convert any 'D-' or 'D+' to 'E-' or 'E+'
            line = line.replace("D-", "E-")
            line = line.replace("D+", "E+")
            try:
                splitline = line.split()
                key = splitline[0]
                if key in str_keys:
                    setattr(self, key, splitline[1])
                elif key in float_keys:
                    try:
                        setattr(self, key, float(splitline[1]))
                    except ValueError:
                        pass
                if len(
                        splitline
                ) == 3:  # Some parfiles don't have flags, but do have errors
                    if splitline[2] not in ['0', '1']:
                        setattr(self, key + '_ERR', float(splitline[2]))
                if len(splitline) == 4:
                    setattr(self, key + '_ERR', float(splitline[3]))
            except:
                print ''

# Read PSR name
        if hasattr(self, 'PSR'):
            setattr(self, 'PSR', self.PSR)
        if hasattr(self, 'PSRJ'):
            setattr(self, 'PSRJ', self.PSRJ)
        # Deal with Ecliptic coords
        if (hasattr(self, 'BETA') and hasattr(self, 'LAMBDA')):
            self.use_eclip = True
            setattr(self, 'ELAT', self.BETA)
            setattr(self, 'ELONG', self.LAMBDA)
        if (slalib and hasattr(self, 'ELAT') and hasattr(self, 'ELONG')):
            self.use_eclip = True
            if hasattr(self, 'POSEPOCH'):
                epoch = self.POSEPOCH
            else:
                epoch = self.PEPOCH
            ra_rad, dec_rad = sla_ecleq(self.ELONG * pu.DEGTORAD,
                                        self.ELAT * pu.DEGTORAD, epoch)
            rstr = pu.coord_to_string(*pu.rad_to_hms(ra_rad))
            dstr = pu.coord_to_string(*pu.rad_to_dms(dec_rad))
            setattr(self, 'RAJ', rstr)
            setattr(self, 'DECJ', dstr)
        if hasattr(self, 'RAJ'):
            setattr(self, 'RA_RAD', pu.ra_to_rad(self.RAJ))
        if hasattr(self, 'DECJ'):
            setattr(self, 'DEC_RAD', pu.dec_to_rad(self.DECJ))
        # Compute the Galactic coords
        if (slalib and hasattr(self, 'RA_RAD') and hasattr(self, 'DEC_RAD')):
            l, b = sla_eqgal(self.RA_RAD, self.DEC_RAD)
            setattr(self, 'GLONG', l * pu.RADTODEG)
            setattr(self, 'GLAT', b * pu.RADTODEG)
        # Compute the Ecliptic coords
        if (slalib and hasattr(self, 'RA_RAD') and hasattr(self, 'DEC_RAD')):
            if hasattr(self, 'POSEPOCH'):
                epoch = self.POSEPOCH
            else:
                epoch = self.PEPOCH
            elon, elat = sla_eqecl(self.RA_RAD, self.DEC_RAD, epoch)
            setattr(self, 'ELONG', elon * pu.RADTODEG)
            setattr(self, 'ELAT', elat * pu.RADTODEG)
        if hasattr(self, 'P'):
            setattr(self, 'P0', self.P)
        if hasattr(self, 'P0'):
            setattr(self, 'F0', 1.0 / self.P0)
        if hasattr(self, 'F0'):
            setattr(self, 'P0', 1.0 / self.F0)
        if hasattr(self, 'F1'):
            setattr(self, 'P1', -self.F1 / (self.F0 * self.F0))
        if hasattr(self, 'FB0'):
            setattr(self, 'PB', (1.0 / self.FB0) / 86400.0)
        if hasattr(self, 'P0_ERR'):
            if hasattr(self, 'P1_ERR'):
                f, ferr, fd, fderr = pu.pferrs(self.P0, self.P0_ERR, self.P1,
                                               self.P1_ERR)
                setattr(self, 'F0_ERR', ferr)
                setattr(self, 'F1', fd)
                setattr(self, 'F1_ERR', fderr)
            else:
                f, fd, = pu.p_to_f(self.P0, self.P1)
                setattr(self, 'F0_ERR', self.P0_ERR / (self.P0 * self.P0))
                setattr(self, 'F1', fd)
        if hasattr(self, 'F0_ERR'):
            if hasattr(self, 'F1_ERR'):
                p, perr, pd, pderr = pu.pferrs(self.F0, self.F0_ERR, self.F1,
                                               self.F1_ERR)
                setattr(self, 'P0_ERR', perr)
                setattr(self, 'P1', pd)
                setattr(self, 'P1_ERR', pderr)
            else:
                p, pd, = pu.p_to_f(self.F0, self.F1)
                setattr(self, 'P0_ERR', self.F0_ERR / (self.F0 * self.F0))
                setattr(self, 'P1', pd)
        if hasattr(self, 'DM'):
            setattr(self, 'DM', self.DM)
        if hasattr(self, 'EPS1') and hasattr(self, 'EPS2'):
            self.use_ell = True
            ecc = math.sqrt(self.EPS1 * self.EPS1 + self.EPS2 * self.EPS2)
            omega = math.atan2(self.EPS1, self.EPS2)
            setattr(self, 'ECC', ecc)
            setattr(self, 'OM', omega)
        if hasattr(self, 'PB') and hasattr(self,
                                           'A1') and not hasattr(self, 'ECC'):
            setattr(self, 'ECC', 0.0)
        pf.close()
    def get_vobs(self, mjdtmp, xtmp, ytmp, mode, offx, offy, dcos, offmode):

        mode = mode.lower()
        offmode = offmode.lower()
        ### for 'coord == horizontal' skip
        try :
            mode = self.coord_dict[mode]
        except:
            xxtmp = xtmp
            yytmp = ytmp
        try:
            offmode = self.coord_dict[offmode]
        except:
            xxtmp = xtmp
            yytmp = ytmp

        if mode == 1:#j2000
            if offmode == 0 or offmode == 1:#same or j2000
                yytmp = ytmp+offy
                if dcos == 0 :
                    xxtmp = xtmp+offx
                else :  
                    xxtmp = xtmp+offx/math.cos(yytmp)
                    
            elif offmode == 2:#b1950
                ret = slalib.sla_fk54z(xtmp, ytmp, 2000)
                xtmp_b = ret[0]
                ytmp_b = ret[1]
                ytmp_b += offy
                if dcos == 0 :
                    xtmp_b += offx
                else :
                    xtmp_b += offx/math.cos(ytmp_b)
                ret_1 = slalib.sla_fk45z(xtmp_b, ytmp_b,1950)
                xxtmp = ret_1[0]
                yytmp = ret_1[1]
                
            elif offmode == 3:#lb,galactic,gal
                ret = slalib.sla_eqgal(xtmp,ytmp)
                xtmp_g = ret[0]
                ytmp_g = ret[1]
                ytmp_g += offy
                if dcos == 0 :
                    xtmp_g += offx
                else :
                    xtmp_g += offx/math.cos(ytmp_g)
                ret_1 = slalib.sla_galeq(xtmp_g, ytmp_g)
                xxtmp = ret_1[0]
                yytmp = ret_1[1]
                
        if mode == 2:#b1950
            if offmode == 1:#j2000
                ret = slalib.sla_fk45z(xtmp, ytmp, 1950)
                xtmp_j = ret[0]
                ytmp_j = ret[1]
                yytmp = ytmp_j+offy
                if dcos == 0 :
                    xxtmp = xtmp_j+offx
                else :  
                    xxtmp = xtmp_j+offx/math.cos(ytmp_j)
                    
            elif offmode == 2 or offmode ==0:#b1950
                ytmp += offy
                if dcos == 0 :
                    xtmp += offx
                else:   
                    xtmp += offx/math.cos(ytmp)
                ret = slalib.sla_fk45z(xtmp, ytmp, 1950)
                xxtmp = ret[0]
                yytmp = ret[1]
                
            elif offmode == 3:#lb,galactic,gal
                ret = slalib.sla_eg50(xtmp,ytmp)
                xtmp_g = ret[0]
                ytmp_g = ret[1]
                ytmp_g += offy
                if dcos == 0 :
                    xtmp_g += offx
                else :  
                    xtmp_g += offx/math.cos(ytmp_g)
                ret = slalib.sla_galeq(xtmp_g, ytmp_g)
                xxtmp = ret[0]
                yytmp = ret[1]
                
        if mode == 3:#lb,galactic,gal
            if offmode == 1:#j2000
                ret_2 = slalib.sla_galeq(xtmp, ytmp)
                xtmp_j = ret_2[0]
                ytmp_j = ret_2[1]
                yytmp = ytmp_j+offy
                if dcos == 0 :
                    xxtmp = xtmp_j+offx
                else :
                    xxtmp = xtmp_j+offx/math.cos(ytmp_j)
                    
            elif offmode == 2:#b1950
                ret = slalib.sla_ge50(xtmp,ytmp)
                xtmp_b = ret[0]
                ytmp_b = ret[1]
                ytmp_b += offy
                if dcos == 0 :
                    xtmp_b += offx
                else :  
                    xtmp_b += offx/math.cos(ytmp_b)
                ret = slalib.sla_fk45z(xtmp_b, ytmp_b, 1950)
                xxtmp = ret[0]
                yytmp = ret[1]
                    
            elif offmode == 0 or offmode == 3:#gal,lb,galactic
                ytmp += offy
                if dcos == 0 :
                    xtmp += offx
                else :  
                    xtmp += offx/math.cos(ytmp)
                ret = slalib.sla_galeq(xtmp, ytmp)
                xxtmp = ret[0]
                yytmp = ret[1]
                
        vobs = self.calc_vobs(mjdtmp+2400000.5, xxtmp, yytmp)
        #print('vobs',vobs,type(vobs))
        return vobs